1 /*
2  * Copyright (c) 2021 Fraunhofer AISEC.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <oscore.h>
8 
9 /**
10  * Test 1:
11  * - Client Key derivation with master salt see RFC8613 Appendix C.1.1
12  * - Generating OSCORE request with key form C.1.1 see RFC8613 Appendix C.4
13  */
14 
15 /* Test vector C1.1: Key derivation with Master Salt */
16 const uint8_t T1__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
17 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
18 					0x0d, 0x0e, 0x0f, 0x10 };
19 uint8_t T1__MASTER_SECRET_LEN = sizeof(T1__MASTER_SECRET);
20 
21 const uint8_t *T1__SENDER_ID;
22 uint8_t T1__SENDER_ID_LEN;
23 
24 const uint8_t T1__RECIPIENT_ID[1] = { 0x01 };
25 uint8_t T1__RECIPIENT_ID_LEN = sizeof(T1__RECIPIENT_ID);
26 
27 const uint8_t T1__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
28 				     0x23, 0x78, 0x63, 0x40 };
29 uint8_t T1__MASTER_SALT_LEN = sizeof(T1__MASTER_SALT);
30 
31 const uint8_t *T1__ID_CONTEXT;
32 uint8_t T1__ID_CONTEXT_LEN;
33 
34 /* Test vector C4: Generating a OSCORE Packet with key material form test vector
35  * C.1
36  */
37 const uint8_t T1__COAP_REQ[] = { 0x44, 0x01, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74,
38 				 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f,
39 				 0x73, 0x74, 0x83, 0x74, 0x76, 0x31 };
40 uint16_t T1__COAP_REQ_LEN = sizeof(T1__COAP_REQ);
41 
42 /* Expected result */
43 const uint8_t T1__SENDER_KEY[] = { 0xf0, 0x91, 0x0e, 0xd7, 0x29, 0x5e,
44 				   0x6a, 0xd4, 0xb5, 0x4f, 0xc7, 0x93,
45 				   0x15, 0x43, 0x02, 0xff };
46 uint8_t T1__SENDER_KEY_LEN = sizeof(T1__SENDER_KEY);
47 
48 const uint8_t T1__RECIPIENT_KEY[] = { 0xff, 0xb1, 0x4e, 0x09, 0x3c, 0x94,
49 				      0xc9, 0xca, 0xc9, 0x47, 0x16, 0x48,
50 				      0xb4, 0xf9, 0x87, 0x10 };
51 uint8_t T1__RECIPIENT_KEY_LEN = sizeof(T1__RECIPIENT_KEY);
52 
53 const uint8_t T1__COMMON_IV[] = { 0x46, 0x22, 0xd4, 0xdd, 0x6d, 0x94, 0x41,
54 				  0x68, 0xee, 0xfb, 0x54, 0x98, 0x7c };
55 uint8_t T1__COMMON_IV_LEN = sizeof(T1__COMMON_IV);
56 
57 const uint8_t T1__OSCORE_REQ[] = { 0x44, 0x02, 0x5d, 0x1f, 0x00, 0x00, 0x39,
58 				   0x74, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
59 				   0x68, 0x6f, 0x73, 0x74, 0x62, 0x09, 0x14,
60 				   0xff, 0x61, 0x2f, 0x10, 0x92, 0xf1, 0x77,
61 				   0x6f, 0x1c, 0x16, 0x68, 0xb3, 0x82, 0x5e };
62 uint8_t T1__OSCORE_REQ_LEN = sizeof(T1__OSCORE_REQ);
63 
64 /**
65  * Test 2:
66  * - Server Key derivation with master salt see RFC8613 Appendix C.1.2
67  * - Generating OSCORE response with key form C.1.2 see RFC8613 Appendix C.7
68  */
69 const uint8_t T2__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
70 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
71 					0x0d, 0x0e, 0x0f, 0x10 };
72 uint8_t T2__MASTER_SECRET_LEN = sizeof(T2__MASTER_SECRET);
73 
74 uint8_t T2__SENDER_ID[] = { 0x01 };
75 uint8_t T2__SENDER_ID_LEN = sizeof(T2__SENDER_ID);
76 
77 uint8_t *T2__RECIPIENT_ID;
78 uint8_t T2__RECIPIENT_ID_LEN;
79 
80 const uint8_t T2__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
81 				     0x23, 0x78, 0x63, 0x40 };
82 uint8_t T2__MASTER_SALT_LEN = sizeof(T2__MASTER_SALT);
83 
84 uint8_t *T2__ID_CONTEXT;
85 uint8_t T2__ID_CONTEXT_LEN;
86 
87 /* The OSCORE message created in C4 (35 Byte). Constructed from a CoAP request
88  * of length 22. This request contains no payload. The request contains only
89  * Uri-host (locahost) and Uri-path option (tv1). In the OSCORE packet Uri-host
90  * option is transferred as plain normal option, The Uri-path is contained in
91  * the ciphertext.
92  */
93 const uint8_t T2__OSCORE_REQ[] = { 0x44, 0x02, 0x5d, 0x1f, 0x00, 0x00, 0x39,
94 				   0x74, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
95 				   0x68, 0x6f, 0x73, 0x74, 0x62, 0x09, 0x14,
96 				   0xff, 0x61, 0x2f, 0x10, 0x92, 0xf1, 0x77,
97 				   0x6f, 0x1c, 0x16, 0x68, 0xb3, 0x82, 0x5e };
98 uint8_t T2__OSCORE_REQ_LEN = sizeof(T2__OSCORE_REQ);
99 
100 /* Unprotected CoAP response (21 bytes) */
101 /* Contains the payload "Hello World!" */
102 const uint8_t T2__COAP_RESPONSE[] = {
103 	0x64, 0x45, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, 0xff, 0x48, 0x65,
104 	0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21
105 };
106 uint8_t T2__COAP_RESPONSE_LEN = sizeof(T2__COAP_RESPONSE);
107 
108 /* Expected result */
109 /* the  reconstructed coap request see Appendix C4 */
110 const uint8_t T2__COAP_REQ[] = { 0x44, 0x01, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74,
111 				 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f,
112 				 0x73, 0x74, 0x83, 0x74, 0x76, 0x31 };
113 uint8_t T2__COAP_REQ_LEN = sizeof(T2__COAP_REQ);
114 
115 const uint8_t T2__OSCORE_RESP[] = { 0x64, 0x44, 0x5D, 0x1F, 0x00, 0x00, 0x39,
116 				    0x74, 0x90, 0xFF, 0xDB, 0xAA, 0xD1, 0xE9,
117 				    0xA7, 0xE7, 0xB2, 0xA8, 0x13, 0xD3, 0xC3,
118 				    0x15, 0x24, 0x37, 0x83, 0x03, 0xCD, 0xAF,
119 				    0xAE, 0x11, 0x91, 0x06 };
120 uint8_t T2__OSCORE_RESP_LEN = sizeof(T2__OSCORE_RESP);
121 
122 /**
123  * Test 3:
124  * - Client Key derivation without master salt see RFC8613 Appendix C.2.1
125  * - Generating OSCORE request with key form C.2.1 see RFC8613 Appendix C.5
126  */
127 /* Test vector C2.1: Key derivation without Master Salt */
128 const uint8_t T3__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
129 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
130 					0x0d, 0x0e, 0x0f, 0x10 };
131 uint8_t T3__MASTER_SECRET_LEN = sizeof(T3__MASTER_SECRET);
132 
133 const uint8_t T3__SENDER_ID[1] = { 0x00 };
134 uint8_t T3__SENDER_ID_LEN = sizeof(T3__SENDER_ID);
135 
136 const uint8_t T3__RECIPIENT_ID[1] = { 0x01 };
137 uint8_t T3__RECIPIENT_ID_LEN = sizeof(T3__RECIPIENT_ID);
138 
139 const uint8_t *T3__MASTER_SALT;
140 uint8_t T3__MASTER_SALT_LEN;
141 
142 const uint8_t *T3__ID_CONTEXT;
143 uint8_t T3__ID_CONTEXT_LEN;
144 
145 /* Test vector C5: Generating a OSCORE Packet with key material form test vector
146  * C.2.1
147  */
148 const uint8_t T3__COAP_REQ[] = { 0x44, 0x01, 0x71, 0xc3, 0x00, 0x00, 0xb9, 0x32,
149 				 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f,
150 				 0x73, 0x74, 0x83, 0x74, 0x76, 0x31 };
151 uint16_t T3__COAP_REQ_LEN = sizeof(T3__COAP_REQ);
152 
153 /* expected result */
154 const uint8_t T3__OSCORE_REQ[] = {
155 	0x44, 0x02, 0x71, 0xc3, 0x00, 0x00, 0xb9, 0x32, 0x39, 0x6c, 0x6f, 0x63,
156 	0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x63, 0x09, 0x14, 0x00, 0xff, 0x4e,
157 	0xd3, 0x39, 0xa5, 0xa3, 0x79, 0xb0, 0xb8, 0xbc, 0x73, 0x1f, 0xff, 0xb0
158 };
159 uint8_t T3__OSCORE_REQ_LEN = sizeof(T3__OSCORE_REQ);
160 
161 /**
162  * Test 4:
163  * - Server Key derivation without master salt see RFC8613 Appendix C.2.2
164  */
165 /*Test vector C2.2: Key derivation without Master Salt*/
166 const uint8_t T4__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
167 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
168 					0x0d, 0x0e, 0x0f, 0x10 };
169 uint8_t T4__MASTER_SECRET_LEN = sizeof(T4__MASTER_SECRET);
170 
171 const uint8_t T4__SENDER_ID[1] = { 0x01 };
172 uint8_t T4__SENDER_ID_LEN = sizeof(T4__SENDER_ID);
173 
174 const uint8_t T4__RECIPIENT_ID[1] = { 0x00 };
175 uint8_t T4__RECIPIENT_ID_LEN = sizeof(T4__RECIPIENT_ID);
176 
177 const uint8_t *T4__MASTER_SALT;
178 uint8_t T4__MASTER_SALT_LEN;
179 
180 const uint8_t *T4__ID_CONTEXT;
181 uint8_t T4__ID_CONTEXT_LEN;
182 
183 /* expected result */
184 const uint8_t T4__SENDER_KEY[] = { 0xe5, 0x7b, 0x56, 0x35, 0x81, 0x51,
185 				   0x77, 0xcd, 0x67, 0x9a, 0xb4, 0xbc,
186 				   0xec, 0x9d, 0x7d, 0xda };
187 uint8_t T4__SENDER_KEY_LEN = sizeof(T4__SENDER_KEY);
188 
189 const uint8_t T4__RECIPIENT_KEY[] = { 0x32, 0x1b, 0x26, 0x94, 0x32, 0x53,
190 				      0xc7, 0xff, 0xb6, 0x00, 0x3b, 0x0b,
191 				      0x64, 0xd7, 0x40, 0x41 };
192 uint8_t T4__RECIPIENT_KEY_LEN = sizeof(T4__RECIPIENT_KEY);
193 
194 const uint8_t T4__COMMON_IV[] = { 0xbe, 0x35, 0xae, 0x29, 0x7d, 0x2d, 0xac,
195 				  0xe9, 0x10, 0xc5, 0x2e, 0x99, 0xf9 };
196 uint8_t T4__COMMON_IV_LEN = sizeof(T4__COMMON_IV);
197 
198 /**
199  * Test 5 :
200  * - Client Key derivation with ID Context see Appendix 3.1
201  * - OSCORE request generation see Appendix C6
202  */
203 /* Test vector C3.1: Key derivation with ID Context */
204 const uint8_t T5__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
205 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
206 					0x0d, 0x0e, 0x0f, 0x10 };
207 uint8_t T5__MASTER_SECRET_LEN = sizeof(T5__MASTER_SECRET);
208 
209 const uint8_t *T5__SENDER_ID;
210 uint8_t T5__SENDER_ID_LEN;
211 
212 const uint8_t T5__RECIPIENT_ID[1] = { 0x01 };
213 uint8_t T5__RECIPIENT_ID_LEN = sizeof(T5__RECIPIENT_ID);
214 
215 const uint8_t T5__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
216 				     0x23, 0x78, 0x63, 0x40 };
217 uint8_t T5__MASTER_SALT_LEN = sizeof(T5__MASTER_SALT);
218 
219 const uint8_t T5__ID_CONTEXT[8] = { 0x37, 0xcb, 0xf3, 0x21,
220 				    0x00, 0x17, 0xa2, 0xd3 };
221 uint8_t T5__ID_CONTEXT_LEN = sizeof(T5__ID_CONTEXT);
222 
223 /* Test vector C6: Generating a OSCORE Packet with key material form test vector
224  * C.2.1
225  */
226 const uint8_t T5__COAP_REQ[] = { 0x44, 0x01, 0x2f, 0x8e, 0xef, 0x9b, 0xbf, 0x7a,
227 				 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f,
228 				 0x73, 0x74, 0x83, 0x74, 0x76, 0x31 };
229 uint16_t T5__COAP_REQ_LEN = sizeof(T5__COAP_REQ);
230 
231 /* Expected result */
232 const uint8_t T5__OSCORE_REQ[] = {
233 	0x44, 0x02, 0x2f, 0x8e, 0xef, 0x9b, 0xbf, 0x7a, 0x39, 0x6c, 0x6f,
234 	0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x6b, 0x19, 0x14, 0x08,
235 	0x37, 0xcb, 0xf3, 0x21, 0x00, 0x17, 0xa2, 0xd3, 0xff, 0x72, 0xcd,
236 	0x72, 0x73, 0xfd, 0x33, 0x1a, 0xc4, 0x5c, 0xff, 0xbe, 0x55, 0xc3
237 };
238 uint8_t T5__OSCORE_REQ_LEN = sizeof(T5__OSCORE_REQ);
239 
240 /**
241  * Test 6:
242  * - Server Key derivation with ID context see RFC8613 Appendix C.3.2
243  */
244 const uint8_t T6__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
245 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
246 					0x0d, 0x0e, 0x0f, 0x10 };
247 uint8_t T6__MASTER_SECRET_LEN = sizeof(T6__MASTER_SECRET);
248 
249 const uint8_t T6__SENDER_ID[1] = { 0x01 };
250 uint8_t T6__SENDER_ID_LEN = sizeof(T6__SENDER_ID);
251 
252 const uint8_t *T6__RECIPIENT_ID;
253 uint8_t T6__RECIPIENT_ID_LEN;
254 
255 const uint8_t T6__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
256 				     0x23, 0x78, 0x63, 0x40 };
257 uint8_t T6__MASTER_SALT_LEN = sizeof(T5__MASTER_SALT);
258 
259 const uint8_t T6__ID_CONTEXT[8] = { 0x37, 0xcb, 0xf3, 0x21,
260 				    0x00, 0x17, 0xa2, 0xd3 };
261 uint8_t T6__ID_CONTEXT_LEN = sizeof(T5__ID_CONTEXT);
262 
263 /* expected result */
264 const uint8_t T6__SENDER_KEY[] = { 0xe3, 0x9a, 0x0c, 0x7c, 0x77, 0xb4,
265 				   0x3f, 0x03, 0xb4, 0xb3, 0x9a, 0xb9,
266 				   0xa2, 0x68, 0x69, 0x9f };
267 uint8_t T6__SENDER_KEY_LEN = sizeof(T6__SENDER_KEY);
268 
269 const uint8_t T6__RECIPIENT_KEY[] = { 0xaf, 0x2a, 0x13, 0x00, 0xa5, 0xe9,
270 				      0x57, 0x88, 0xb3, 0x56, 0x33, 0x6e,
271 				      0xee, 0xcd, 0x2b, 0x92 };
272 uint8_t T6__RECIPIENT_KEY_LEN = sizeof(T6__RECIPIENT_KEY);
273 
274 const uint8_t T6__COMMON_IV[] = { 0x2c, 0xa5, 0x8f, 0xb8, 0x5f, 0xf1, 0xb8,
275 				  0x1c, 0x0b, 0x71, 0x81, 0xb8, 0x5e };
276 uint8_t T6__COMMON_IV_LEN = sizeof(T6__COMMON_IV);
277 
278 /**
279  * Test 7:
280  * - Server with partial IV see Appendix C8
281  * - currently not supported
282  */
283 const uint8_t T7__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
284 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
285 					0x0d, 0x0e, 0x0f, 0x10 };
286 uint8_t T7__MASTER_SECRET_LEN = sizeof(T7__MASTER_SECRET);
287 
288 const uint8_t T7__SENDER_ID[] = { 0x01 };
289 uint8_t T7__SENDER_ID_LEN = sizeof(T7__SENDER_ID);
290 
291 const uint8_t *T7__RECIPIENT_ID;
292 uint8_t T7__RECIPIENT_ID_LEN;
293 
294 const uint8_t T7__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
295 				     0x23, 0x78, 0x63, 0x40 };
296 uint8_t T7__MASTER_SALT_LEN = sizeof(T7__MASTER_SALT);
297 
298 const uint8_t *T7__ID_CONTEXT;
299 uint8_t T7__ID_CONTEXT_LEN;
300 
301 /* Test vector C4: Generating a OSCORE Packet with key material form test vector
302  * C.1
303  */
304 /* the OSCORE message created in C4 */
305 const uint8_t T7__OSCORE_REQ[] = { 0x44, 0x02, 0x5d, 0x1f, 0x00, 0x00, 0x39,
306 				   0x74, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
307 				   0x68, 0x6f, 0x73, 0x74, 0x62, 0x09, 0x14,
308 				   0xff, 0x61, 0x2f, 0x10, 0x92, 0xf1, 0x77,
309 				   0x6f, 0x1c, 0x16, 0x68, 0xb3, 0x82, 0x5e };
310 uint8_t T7__OSCORE_REQ_LEN = sizeof(T7__OSCORE_REQ);
311 
312 /* unprotected CoAP response */
313 const uint8_t T7__COAP_RESPONSE[] = {
314 	0x64, 0x45, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, 0xff, 0x48, 0x65,
315 	0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21
316 };
317 uint8_t T7__COAP_RESPONSE_LEN = sizeof(T7__COAP_RESPONSE);
318 
319 /* expected result */
320 const uint8_t T7__OSCORE_RES[] = { 0x64, 0x44, 0x5d, 0x1f, 0x00, 0x00, 0x39,
321 				   0x74, 0x92, 0x01, 0x00, 0xff, 0x4d, 0x4c,
322 				   0x13, 0x66, 0x93, 0x84, 0xb6, 0x73, 0x54,
323 				   0xb2, 0xb6, 0x17, 0x5f, 0xf4, 0xb8, 0x65,
324 				   0x8c, 0x66, 0x6a, 0x6c, 0xf8, 0x8e };
325 uint8_t T7__OSCORE_RES_LEN = sizeof(T7__OSCORE_RES);
326 
327 /**
328  * Test 8:
329  * - Simple ACK packet should not be encrypted and result should be the same as
330  *   input buffer (see RFC8613 Section 4.2)
331  */
332 const uint8_t T8__MASTER_SECRET[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
333 					0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
334 					0x0d, 0x0e, 0x0f, 0x10 };
335 uint8_t T8__MASTER_SECRET_LEN = sizeof(T2__MASTER_SECRET);
336 
337 const uint8_t T8__SENDER_ID[] = { 0x01 };
338 uint8_t T8__SENDER_ID_LEN = sizeof(T2__SENDER_ID);
339 
340 const uint8_t T8__MASTER_SALT[8] = { 0x9e, 0x7c, 0xa9, 0x22,
341 				     0x23, 0x78, 0x63, 0x40 };
342 uint8_t T8__MASTER_SALT_LEN = sizeof(T2__MASTER_SALT);
343 
344 /* Simple ACK message (code 0=EMPTY, type 2=ACK, no payload). */
345 const uint8_t T8__COAP_ACK[] = { 0x60, 0x00, 0x45, 0x69 };
346 uint8_t T8__COAP_ACK_LEN = sizeof(T8__COAP_ACK);
347