-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
65 lines (57 loc) · 1.96 KB
/
Copy pathexample_test.go
File metadata and controls
65 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cose_test
import (
"bytes"
"fmt"
"github.com/filecoin-project/go-fee/cose"
)
// Example shows the envelope lifecycle: build a detached COSE_Encrypt, derive
// the AAD for the body AEAD, append the (already-encrypted) ciphertext, then
// decode the blob back and recover the recipient's wrapped key. The actual
// encryption and key wrap are the caller's responsibility — cose only handles
// the envelope and the Enc_structure.
func Example() {
const exampleType = "application/example"
env := &cose.Envelope{
Headers: cose.Headers{
Protected: cose.Header{}.
Set(cose.HeaderLabelAlg, cose.AlgA256GCM).
Set(cose.HeaderLabelType, exampleType),
Unprotected: cose.Header{}.
Set(cose.HeaderLabelIV, bytes.Repeat([]byte{0x00}, 12)),
},
Recipients: []*cose.Recipient{{
Headers: cose.Headers{
Protected: cose.Header{}.Set(cose.HeaderLabelAlg, cose.AlgECDHESA256KW),
Unprotected: cose.Header{}.Set(cose.HeaderLabelKID, []byte("key-1")),
},
Ciphertext: []byte("wrapped-key-bytes"),
}},
}
// AAD that the body AEAD must authenticate.
aad, err := env.EncStructure(nil)
if err != nil {
panic(err)
}
_ = aad // pass to your AEAD as additional data
// Serialize the envelope and append the detached ciphertext.
envelope, err := env.Encode()
if err != nil {
panic(err)
}
blob := append(envelope, []byte("...detached ciphertext...")...)
// Decode, pinning the expected type, and recover the trailing payload.
decoded, ciphertext, err := cose.Decode(blob, cose.WithExpectedType(exampleType))
if err != nil {
panic(err)
}
kid, _ := decoded.Recipients[0].Headers.Unprotected.Bytes(cose.HeaderLabelKID)
fmt.Printf("recipients: %d\n", len(decoded.Recipients))
fmt.Printf("kid: %s\n", kid)
fmt.Printf("wrapped key: %s\n", decoded.Recipients[0].Ciphertext)
fmt.Printf("detached ciphertext: %s\n", ciphertext)
// Output:
// recipients: 1
// kid: key-1
// wrapped key: wrapped-key-bytes
// detached ciphertext: ...detached ciphertext...
}