Branch data Line data Source code
1 : : /*
2 : : Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
3 : : file at the top-level directory of this distribution.
4 : :
5 : : Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 : : http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 : : <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 : : option. This file may not be copied, modified, or distributed
9 : : except according to those terms.
10 : : */
11 : :
12 : : #include <stdio.h>
13 : : #include <zephyr/kernel.h>
14 : : #include <zephyr/ztest.h>
15 : :
16 : : #include <edhoc.h>
17 : : #include "txrx_wrapper.h"
18 : : #include "edhoc_test_vectors_rfc9529.h"
19 : :
20 : : volatile uint8_t msg_cnt = 1;
21 : :
22 : : /* Messages of the RFC 9529 trace that the tx/rx callbacks expect or replay */
23 : : struct rfc9529_messages {
24 : : const uint8_t *msg[4];
25 : : const uint32_t *len[4];
26 : : };
27 : :
28 : : static const struct rfc9529_messages t1_messages = {
29 : : .msg = { T1_RFC9529__MESSAGE_1, T1_RFC9529__MESSAGE_2,
30 : : T1_RFC9529__MESSAGE_3, T1_RFC9529__MESSAGE_4 },
31 : : .len = { &T1_RFC9529__MESSAGE_1_LEN, &T1_RFC9529__MESSAGE_2_LEN,
32 : : &T1_RFC9529__MESSAGE_3_LEN, &T1_RFC9529__MESSAGE_4_LEN },
33 : : };
34 : :
35 : : static const struct rfc9529_messages t2_messages = {
36 : : .msg = { T2_RFC9529__MESSAGE_1, T2_RFC9529__MESSAGE_2,
37 : : T2_RFC9529__MESSAGE_3, T2_RFC9529__MESSAGE_4 },
38 : : .len = { &T2_RFC9529__MESSAGE_1_LEN, &T2_RFC9529__MESSAGE_2_LEN,
39 : : &T2_RFC9529__MESSAGE_3_LEN, &T2_RFC9529__MESSAGE_4_LEN },
40 : : };
41 : :
42 : : /* RFC 9529 Section 3 with C_R = h'', see edhoc_test_vectors_rfc9529.c */
43 : : static const struct rfc9529_messages t2_empty_c_r_messages = {
44 : : .msg = { T2_EMPTY_C_R__MESSAGE_1, T2_EMPTY_C_R__MESSAGE_2,
45 : : T2_EMPTY_C_R__MESSAGE_3, T2_EMPTY_C_R__MESSAGE_4 },
46 : : .len = { &T2_EMPTY_C_R__MESSAGE_1_LEN, &T2_EMPTY_C_R__MESSAGE_2_LEN,
47 : : &T2_EMPTY_C_R__MESSAGE_3_LEN, &T2_EMPTY_C_R__MESSAGE_4_LEN },
48 : : };
49 : :
50 : : static const struct rfc9529_messages *messages = &t1_messages;
51 : :
52 : 9 : enum err tx_fkt(void *sock, struct byte_array *data)
53 : : {
54 [ + - + - ]: 9 : if (msg_cnt >= 1 && msg_cnt <= 4) {
55 : 9 : zassert_equal(data->len, *messages->len[msg_cnt - 1],
56 : : "wrong message%d length", msg_cnt);
57 : 9 : zassert_mem_equal__(data->ptr, messages->msg[msg_cnt - 1],
58 : : data->len, "wrong message%d", msg_cnt);
59 : : }
60 : :
61 : 9 : msg_cnt++;
62 : 9 : return ok;
63 : : }
64 : :
65 : 9 : enum err rx_fkt(void *sock, struct byte_array *data)
66 : : {
67 [ + - + - ]: 9 : if (msg_cnt >= 1 && msg_cnt <= 4) {
68 [ - + ]: 9 : TRY(_memcpy_s(data->ptr, data->len, messages->msg[msg_cnt - 1],
69 : : *messages->len[msg_cnt - 1]));
70 : 9 : data->len = *messages->len[msg_cnt - 1];
71 : : }
72 : :
73 : 9 : msg_cnt++;
74 : 9 : return ok;
75 : : }
76 : :
77 : 9 : enum err ead_fkt(void *params, struct byte_array *ead13)
78 : : {
79 : 9 : return ok;
80 : : }
81 : :
82 : 1 : void test_edhoc_initiator_x509_x5t_rfc9529(void)
83 : : {
84 : 1 : messages = &t1_messages;
85 : 1 : msg_cnt = 1;
86 : 1 : enum err r;
87 : 1 : struct other_party_cred cred_r;
88 : 1 : struct edhoc_initiator_context c_i;
89 : :
90 : 1 : uint8_t I_PRK_out_buf[32];
91 : 1 : struct byte_array I_PRK_out = { .ptr = I_PRK_out_buf,
92 : : .len = sizeof(I_PRK_out_buf) };
93 : :
94 : 1 : uint8_t I_err_msg_buf[0];
95 : 1 : struct byte_array I_err_msg = { .ptr = I_err_msg_buf,
96 : : .len = sizeof(I_err_msg_buf) };
97 : :
98 : 1 : c_i.sock = NULL;
99 : 1 : c_i.c_i.len = T1_RFC9529__C_I_LEN;
100 : 1 : c_i.c_i.ptr = (uint8_t *)T1_RFC9529__C_I;
101 : 1 : c_i.method = (enum method_type)T1_RFC9529__METHOD;
102 : 1 : c_i.suites_i.len = T1_RFC9529__SUITES_I_LEN;
103 : 1 : c_i.suites_i.ptr = (uint8_t *)T1_RFC9529__SUITES_I;
104 : 1 : c_i.ead_1.len = 0;
105 : 1 : c_i.ead_1.ptr = NULL;
106 : 1 : c_i.ead_3.len = 0;
107 : 1 : c_i.ead_3.ptr = NULL;
108 : 1 : c_i.id_cred_i.len = T1_RFC9529__ID_CRED_I_LEN;
109 : 1 : c_i.id_cred_i.ptr = (uint8_t *)T1_RFC9529__ID_CRED_I;
110 : 1 : c_i.cred_i.len = T1_RFC9529__CRED_I_LEN;
111 : 1 : c_i.cred_i.ptr = (uint8_t *)T1_RFC9529__CRED_I;
112 : 1 : c_i.g_x.len = T1_RFC9529__G_X_LEN;
113 : 1 : c_i.g_x.ptr = (uint8_t *)T1_RFC9529__G_X;
114 : 1 : c_i.x.len = T1_RFC9529__X_LEN;
115 : 1 : c_i.x.ptr = (uint8_t *)T1_RFC9529__X;
116 : 1 : c_i.g_i.len = 0;
117 : 1 : c_i.g_i.ptr = NULL;
118 : 1 : c_i.i.len = 0;
119 : 1 : c_i.i.ptr = NULL;
120 : 1 : c_i.sk_i.len = T1_RFC9529__SK_I_LEN;
121 : 1 : c_i.sk_i.ptr = (uint8_t *)T1_RFC9529__SK_I;
122 : 1 : c_i.pk_i.len = T1_RFC9529__PK_I_LEN;
123 : 1 : c_i.pk_i.ptr = (uint8_t *)T1_RFC9529__PK_I;
124 : :
125 : 1 : cred_r.id_cred.len = T1_RFC9529__ID_CRED_R_LEN;
126 : 1 : cred_r.id_cred.ptr = (uint8_t *)T1_RFC9529__ID_CRED_R;
127 : 1 : cred_r.cred.len = T1_RFC9529__CRED_R_LEN;
128 : 1 : cred_r.cred.ptr = (uint8_t *)T1_RFC9529__CRED_R;
129 : 1 : cred_r.g.len = 0;
130 : 1 : cred_r.g.ptr = NULL;
131 : 1 : cred_r.pk.len = T1_RFC9529__PK_R_LEN;
132 : 1 : cred_r.pk.ptr = (uint8_t *)T1_RFC9529__PK_R;
133 : 1 : cred_r.ca.len = 0;
134 : 1 : cred_r.ca.ptr = NULL;
135 : 1 : cred_r.ca_pk.len = 0;
136 : 1 : cred_r.ca_pk.ptr = NULL;
137 : :
138 : 1 : struct cred_array cred_r_array = { .len = 1, .ptr = &cred_r };
139 : :
140 : 1 : r = edhoc_initiator_run(&c_i, &cred_r_array, &I_err_msg, &I_PRK_out,
141 : : tx_fkt, rx_fkt, ead_fkt);
142 : :
143 : 1 : zassert_mem_equal__(I_PRK_out.ptr, T1_RFC9529__PRK_out, I_PRK_out.len,
144 : : "wrong PRK_out");
145 : :
146 : 1 : msg_cnt = 1;
147 : 1 : }
148 : :
149 : 1 : void test_edhoc_responder_x509_x5t_rfc9529(void)
150 : : {
151 : 1 : messages = &t1_messages;
152 : 1 : msg_cnt = 1;
153 : 1 : enum err r;
154 : 1 : struct other_party_cred cred_i;
155 : 1 : struct edhoc_responder_context c_r;
156 : :
157 : 1 : uint8_t R_PRK_out_buf[32];
158 : 1 : struct byte_array R_PRK_out = { .ptr = R_PRK_out_buf,
159 : : .len = sizeof(R_PRK_out_buf) };
160 : :
161 : 1 : uint8_t R_err_msg_buf[0];
162 : 1 : struct byte_array R_err_msg = { .ptr = R_err_msg_buf,
163 : : .len = sizeof(R_err_msg_buf) };
164 : :
165 : 1 : c_r.sock = NULL;
166 : 1 : c_r.c_r.ptr = (uint8_t *)T1_RFC9529__C_R;
167 : 1 : c_r.c_r.len = T1_RFC9529__C_R_LEN;
168 : 1 : c_r.suites_r.len = T1_RFC9529__SUITES_R_LEN;
169 : 1 : c_r.suites_r.ptr = (uint8_t *)T1_RFC9529__SUITES_R;
170 : 1 : c_r.ead_2.len = 0;
171 : 1 : c_r.ead_2.ptr = NULL;
172 : 1 : c_r.ead_4.len = 0;
173 : 1 : c_r.ead_4.ptr = NULL;
174 : 1 : c_r.id_cred_r.len = T1_RFC9529__ID_CRED_R_LEN;
175 : 1 : c_r.id_cred_r.ptr = (uint8_t *)T1_RFC9529__ID_CRED_R;
176 : 1 : c_r.cred_r.len = T1_RFC9529__CRED_R_LEN;
177 : 1 : c_r.cred_r.ptr = (uint8_t *)T1_RFC9529__CRED_R;
178 : 1 : c_r.g_y.len = T1_RFC9529__G_Y_LEN;
179 : 1 : c_r.g_y.ptr = (uint8_t *)T1_RFC9529__G_Y;
180 : 1 : c_r.y.len = T1_RFC9529__Y_LEN;
181 : 1 : c_r.y.ptr = (uint8_t *)T1_RFC9529__Y;
182 : 1 : c_r.g_r.len = 0;
183 : 1 : c_r.g_r.ptr = NULL;
184 : 1 : c_r.r.len = 0;
185 : 1 : c_r.r.ptr = NULL;
186 : 1 : c_r.sk_r.len = T1_RFC9529__SK_R_LEN;
187 : 1 : c_r.sk_r.ptr = (uint8_t *)T1_RFC9529__SK_R;
188 : 1 : c_r.pk_r.len = T1_RFC9529__PK_R_LEN;
189 : 1 : c_r.pk_r.ptr = (uint8_t *)T1_RFC9529__PK_R;
190 : :
191 : 1 : cred_i.id_cred.len = T1_RFC9529__ID_CRED_I_LEN;
192 : 1 : cred_i.id_cred.ptr = (uint8_t *)T1_RFC9529__ID_CRED_I;
193 : 1 : cred_i.cred.len = T1_RFC9529__CRED_I_LEN;
194 : 1 : cred_i.cred.ptr = (uint8_t *)T1_RFC9529__CRED_I;
195 : 1 : cred_i.g.len = 0;
196 : 1 : cred_i.g.ptr = NULL;
197 : 1 : cred_i.pk.len = T1_RFC9529__PK_I_LEN;
198 : 1 : cred_i.pk.ptr = (uint8_t *)T1_RFC9529__PK_I;
199 : 1 : cred_i.ca.len = 0;
200 : 1 : cred_i.ca.ptr = NULL;
201 : 1 : cred_i.ca_pk.len = 0;
202 : 1 : cred_i.ca_pk.ptr = NULL;
203 : :
204 : 1 : struct cred_array cred_i_array = { .len = 1, .ptr = &cred_i };
205 : :
206 : 1 : r = edhoc_responder_run(&c_r, &cred_i_array, &R_err_msg, &R_PRK_out,
207 : : tx_fkt, rx_fkt, ead_fkt);
208 : :
209 : 1 : zassert_mem_equal__(R_PRK_out.ptr, T1_RFC9529__PRK_out, R_PRK_out.len,
210 : : "wrong PRK_out");
211 : :
212 : 1 : msg_cnt = 1;
213 : 1 : }
214 : :
215 : 2 : static void initiator_static_dh_kid(const struct rfc9529_messages *m,
216 : : const uint8_t *prk_out)
217 : : {
218 : 2 : enum err r;
219 : 2 : struct other_party_cred cred_r;
220 : 2 : struct edhoc_initiator_context c_i;
221 : :
222 : 2 : uint8_t I_PRK_out_buf[32];
223 : 2 : struct byte_array I_PRK_out = { .ptr = I_PRK_out_buf,
224 : : .len = sizeof(I_PRK_out_buf) };
225 : :
226 : 2 : uint8_t I_err_msg_buf[0];
227 : 2 : struct byte_array I_err_msg = { .ptr = I_err_msg_buf,
228 : : .len = sizeof(I_err_msg_buf) };
229 : :
230 : 2 : messages = m;
231 : 2 : msg_cnt = 1;
232 : :
233 : 2 : c_i.sock = NULL;
234 : 2 : c_i.c_i.len = T2_RFC9529__C_I_LEN;
235 : 2 : c_i.c_i.ptr = (uint8_t *)T2_RFC9529__C_I;
236 : 2 : c_i.method = (enum method_type)T2_RFC9529__METHOD;
237 : 2 : c_i.suites_i.len = T2_RFC9529__SUITES_I_LEN;
238 : 2 : c_i.suites_i.ptr = (uint8_t *)T2_RFC9529__SUITES_I;
239 : 2 : c_i.ead_1.len = 0;
240 : 2 : c_i.ead_1.ptr = NULL;
241 : 2 : c_i.ead_3.len = 0;
242 : 2 : c_i.ead_3.ptr = NULL;
243 : 2 : c_i.id_cred_i.len = T2_RFC9529__ID_CRED_I_LEN;
244 : 2 : c_i.id_cred_i.ptr = (uint8_t *)T2_RFC9529__ID_CRED_I;
245 : 2 : c_i.cred_i.len = T2_RFC9529__CRED_I_LEN;
246 : 2 : c_i.cred_i.ptr = (uint8_t *)T2_RFC9529__CRED_I;
247 : 2 : c_i.g_x.len = T2_RFC9529__G_X_LEN;
248 : 2 : c_i.g_x.ptr = (uint8_t *)T2_RFC9529__G_X;
249 : 2 : c_i.x.len = T2_RFC9529__X_LEN;
250 : 2 : c_i.x.ptr = (uint8_t *)T2_RFC9529__X;
251 : 2 : c_i.g_i.len = T2_RFC9529__G_I_LEN;
252 : 2 : c_i.g_i.ptr = (uint8_t *)T2_RFC9529__G_I;
253 : 2 : c_i.i.len = T2_RFC9529__I_LEN;
254 : 2 : c_i.i.ptr = (uint8_t *)T2_RFC9529__I;
255 : 2 : c_i.sk_i.len = 0;
256 : 2 : c_i.sk_i.ptr = NULL;
257 : 2 : c_i.pk_i.len = 0;
258 : 2 : c_i.pk_i.ptr = NULL;
259 : :
260 : 2 : cred_r.id_cred.len = T2_RFC9529__ID_CRED_R_LEN;
261 : 2 : cred_r.id_cred.ptr = (uint8_t *)T2_RFC9529__ID_CRED_R;
262 : 2 : cred_r.cred.len = T2_RFC9529__CRED_R_LEN;
263 : 2 : cred_r.cred.ptr = (uint8_t *)T2_RFC9529__CRED_R;
264 : 2 : cred_r.g.len = T2_RFC9529__G_R_LEN;
265 : 2 : cred_r.g.ptr = (uint8_t *)T2_RFC9529__G_R;
266 : 2 : cred_r.pk.len = 0;
267 : 2 : cred_r.pk.ptr = NULL;
268 : 2 : cred_r.ca.len = 0;
269 : 2 : cred_r.ca.ptr = NULL;
270 : 2 : cred_r.ca_pk.len = 0;
271 : 2 : cred_r.ca_pk.ptr = NULL;
272 : :
273 : 2 : struct cred_array cred_r_array = { .len = 1, .ptr = &cred_r };
274 : :
275 : 2 : r = edhoc_initiator_run(&c_i, &cred_r_array, &I_err_msg, &I_PRK_out,
276 : : tx_fkt, rx_fkt, ead_fkt);
277 : :
278 : 2 : zassert_equal(r, ok, "edhoc_initiator_run failed: %d", r);
279 : 2 : zassert_mem_equal__(I_PRK_out.ptr, prk_out, I_PRK_out.len,
280 : : "wrong PRK_out");
281 : :
282 : 2 : msg_cnt = 1;
283 : 2 : }
284 : :
285 : 2 : static void responder_static_dh_kid(const struct rfc9529_messages *m,
286 : : const uint8_t *id_c_r, uint32_t id_c_r_len,
287 : : const uint8_t *prk_out)
288 : : {
289 : 2 : enum err r;
290 : 2 : struct other_party_cred cred_i;
291 : 2 : struct edhoc_responder_context c_r;
292 : :
293 : 2 : uint8_t R_PRK_out_buf[32];
294 : 2 : struct byte_array R_PRK_out = { .ptr = R_PRK_out_buf,
295 : : .len = sizeof(R_PRK_out_buf) };
296 : :
297 : 2 : uint8_t R_err_msg_buf[0];
298 : 2 : struct byte_array R_err_msg = { .ptr = R_err_msg_buf,
299 : : .len = sizeof(R_err_msg_buf) };
300 : :
301 : 2 : messages = m;
302 : 2 : msg_cnt = 1;
303 : :
304 : 2 : c_r.sock = NULL;
305 : 2 : c_r.c_r.ptr = (uint8_t *)id_c_r;
306 : 2 : c_r.c_r.len = id_c_r_len;
307 : 2 : c_r.suites_r.len = T2_RFC9529__SUITES_R_LEN;
308 : 2 : c_r.suites_r.ptr = (uint8_t *)T2_RFC9529__SUITES_R;
309 : 2 : c_r.ead_2.len = 0;
310 : 2 : c_r.ead_2.ptr = NULL;
311 : 2 : c_r.ead_4.len = 0;
312 : 2 : c_r.ead_4.ptr = NULL;
313 : 2 : c_r.id_cred_r.len = T2_RFC9529__ID_CRED_R_LEN;
314 : 2 : c_r.id_cred_r.ptr = (uint8_t *)T2_RFC9529__ID_CRED_R;
315 : 2 : c_r.cred_r.len = T2_RFC9529__CRED_R_LEN;
316 : 2 : c_r.cred_r.ptr = (uint8_t *)T2_RFC9529__CRED_R;
317 : 2 : c_r.g_y.len = T2_RFC9529__G_Y_LEN;
318 : 2 : c_r.g_y.ptr = (uint8_t *)T2_RFC9529__G_Y;
319 : 2 : c_r.y.len = T2_RFC9529__Y_LEN;
320 : 2 : c_r.y.ptr = (uint8_t *)T2_RFC9529__Y;
321 : 2 : c_r.g_r.len = T2_RFC9529__G_R_LEN;
322 : 2 : c_r.g_r.ptr = (uint8_t *)T2_RFC9529__G_R;
323 : 2 : c_r.r.len = T2_RFC9529__R_LEN;
324 : 2 : c_r.r.ptr = (uint8_t *)T2_RFC9529__R;
325 : 2 : c_r.sk_r.len = 0;
326 : 2 : c_r.sk_r.ptr = NULL;
327 : 2 : c_r.pk_r.len = 0;
328 : 2 : c_r.pk_r.ptr = NULL;
329 : :
330 : 2 : cred_i.id_cred.len = T2_RFC9529__ID_CRED_I_LEN;
331 : 2 : cred_i.id_cred.ptr = (uint8_t *)T2_RFC9529__ID_CRED_I;
332 : 2 : cred_i.cred.len = T2_RFC9529__CRED_I_LEN;
333 : 2 : cred_i.cred.ptr = (uint8_t *)T2_RFC9529__CRED_I;
334 : 2 : cred_i.g.len = T2_RFC9529__G_I_LEN;
335 : 2 : cred_i.g.ptr = (uint8_t *)T2_RFC9529__G_I;
336 : 2 : cred_i.pk.len = 0;
337 : 2 : cred_i.pk.ptr = NULL;
338 : 2 : cred_i.ca.len = 0;
339 : 2 : cred_i.ca.ptr = NULL;
340 : 2 : cred_i.ca_pk.len = 0;
341 : 2 : cred_i.ca_pk.ptr = NULL;
342 : :
343 : 2 : struct cred_array cred_i_array = { .len = 1, .ptr = &cred_i };
344 : :
345 : 2 : r = edhoc_responder_run(&c_r, &cred_i_array, &R_err_msg, &R_PRK_out,
346 : : tx_fkt, rx_fkt, ead_fkt);
347 : :
348 : 2 : zassert_equal(r, ok, "edhoc_responder_run failed: %d", r);
349 : 2 : zassert_mem_equal__(R_PRK_out.ptr, prk_out, R_PRK_out.len,
350 : : "wrong PRK_out");
351 : :
352 : 2 : msg_cnt = 1;
353 : 2 : }
354 : :
355 : 1 : void test_edhoc_initiator_static_dh_kid_rfc9529(void)
356 : : {
357 : 1 : initiator_static_dh_kid(&t2_messages, T2_RFC9529__PRK_out);
358 : 1 : }
359 : :
360 : 1 : void test_edhoc_responder_static_dh_kid_rfc9529(void)
361 : : {
362 : 1 : responder_static_dh_kid(&t2_messages, T2_RFC9529__C_R,
363 : : T2_RFC9529__C_R_LEN, T2_RFC9529__PRK_out);
364 : 1 : }
365 : :
366 : 1 : void test_edhoc_initiator_empty_c_r(void)
367 : : {
368 : 1 : initiator_static_dh_kid(&t2_empty_c_r_messages, T2_EMPTY_C_R__PRK_out);
369 : 1 : }
370 : :
371 : 1 : void test_edhoc_responder_empty_c_r(void)
372 : : {
373 : : /* non-NULL pointer, as an application would pass a buffer */
374 : 1 : static const uint8_t empty_c_r[1];
375 : :
376 : 1 : responder_static_dh_kid(&t2_empty_c_r_messages, empty_c_r, 0,
377 : : T2_EMPTY_C_R__PRK_out);
378 : 1 : }
|