1; Adapted from the CDDL fragments in RFC 8152 (https://datatracker.ietf.org/doc/html/rfc8152)
2
3Headers = (
4	protected : empty_or_serialized_map,
5	unprotected : header_map
6)
7
8label = int / tstr
9values = any
10
11header_map = {
12	Generic_Headers,
13	* label => values
14}
15
16empty_or_serialized_map = bstr .cbor header_map / bstr .size 0
17
18Generic_Headers = (
19	? 1 => int / tstr,  ; algorithm identifier
20	? 2 => [+label],    ; criticality
21	? 3 => tstr / int,  ; content type
22	? 4 => bstr,        ; key identifier
23	? 5 => bstr,        ; IV
24	? 6 => bstr,        ; Partial IV
25
26	; Comment out the counter signature since it creates a circular type.
27	; It is optional, so commenting it out is still conformant.
28	;? 7 => COSE_Signature / [+COSE_Signature] ; Counter signature
29)
30
31COSE_Sign_Tagged = #6.98(COSE_Sign)
32
33COSE_Sign = [
34	Headers,
35	payload : bstr / nil,
36	signatures : [+ COSE_Signature]
37]
38
39COSE_Signature =  [
40	Headers,
41	signature : bstr
42]
43
44COSE_Sign1_Tagged = #6.18(COSE_Sign1)
45
46COSE_Sign1 = [
47	Headers,
48	payload : bstr / nil,
49	signature : bstr
50]
51
52Sig_structure = [
53	context : "Signature" / "Signature1" / "CounterSignature",
54	body_protected : empty_or_serialized_map,
55	? sign_protected : empty_or_serialized_map,
56	external_aad : bstr,
57	payload : bstr
58]
59
60COSE_Encrypt_Tagged = #6.96(COSE_Encrypt)
61
62COSE_Encrypt = [
63	Headers,
64	ciphertext : bstr / nil,
65	recipients : [+COSE_recipient]
66]
67
68COSE_recipient = [
69	Headers,
70	ciphertext : bstr / nil,
71
72	; Comment out the recipient list since it creates a circular type.
73	; It is optional, so commenting it out is still conformant.
74	;? recipients : [+COSE_recipient]
75]
76
77COSE_Encrypt0_Tagged = #6.16(COSE_Encrypt0)
78
79COSE_Encrypt0 = [
80	Headers,
81	ciphertext : bstr / nil,
82]
83
84Enc_structure = [
85	context : "Encrypt" / "Encrypt0" / "Enc_Recipient" /
86		"Mac_Recipient" / "Rec_Recipient",
87	protected : empty_or_serialized_map,
88	external_aad : bstr
89]
90
91COSE_Mac_Tagged = #6.97(COSE_Mac)
92
93COSE_Mac = [
94	Headers,
95	payload : bstr / nil,
96	tag : bstr,
97	recipients :[+COSE_recipient]
98]
99
100COSE_Mac0_Tagged = #6.17(COSE_Mac0)
101
102COSE_Mac0 = [
103	Headers,
104	payload : bstr / nil,
105	tag : bstr,
106]
107
108MAC_structure = [
109	context : "MAC" / "MAC0",
110	protected : empty_or_serialized_map,
111	external_aad : bstr,
112	payload : bstr
113]
114
115COSE_Key = {
116	1 => tstr / int,          ; kty
117	? 2 => bstr,              ; kid
118	? 3 => tstr / int,        ; alg
119	? 4 => [+ (tstr / int) ], ; key_ops
120	? 5 => bstr,              ; Base IV
121	* label => values
122}
123
124COSE_KeySet = [+COSE_Key]
125
126PartyInfo = (
127	identity : bstr / nil,
128	nonce : bstr / int / nil,
129	other : bstr / nil
130)
131
132COSE_KDF_Context = [
133	AlgorithmID : int / tstr,
134	PartyUInfo : [ PartyInfo ],
135	PartyVInfo : [ PartyInfo ],
136	SuppPubInfo : [
137		keyDataLength : uint,
138		protected : empty_or_serialized_map,
139		? other : bstr
140	],
141	? SuppPrivInfo : bstr
142]
143
144COSE_Messages = COSE_Untagged_Message / COSE_Tagged_Message
145
146COSE_Untagged_Message = COSE_Sign / COSE_Sign1 /
147	COSE_Encrypt / COSE_Encrypt0 /
148	COSE_Mac / COSE_Mac0
149
150COSE_Tagged_Message = COSE_Sign_Tagged / COSE_Sign1_Tagged /
151	COSE_Encrypt_Tagged / COSE_Encrypt0_Tagged /
152	COSE_Mac_Tagged / COSE_Mac0_Tagged
153