1 /*
2 * Copyright (c) 2020 Demant
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/types.h>
8 #include <zephyr/ztest.h>
9
10 #define ULL_LLCP_UNITTEST
11
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <zephyr/sys/slist.h>
15 #include <zephyr/sys/util.h>
16 #include "hal/ccm.h"
17
18 #include "util/util.h"
19 #include "util/mem.h"
20 #include "util/memq.h"
21 #include "util/dbuf.h"
22
23 #include "pdu_df.h"
24 #include "lll/pdu_vendor.h"
25 #include "pdu.h"
26 #include "ll.h"
27 #include "ll_feat.h"
28 #include "ll_settings.h"
29
30 #include "lll.h"
31 #include "lll/lll_df_types.h"
32 #include "lll_conn.h"
33 #include "lll_conn_iso.h"
34
35 #include "ull_tx_queue.h"
36
37 #include "isoal.h"
38 #include "ull_iso_types.h"
39 #include "ull_conn_iso_types.h"
40
41 #include "ull_internal.h"
42 #include "ull_conn_types.h"
43 #include "ull_llcp.h"
44 #include "ull_conn_internal.h"
45 #include "ull_llcp_internal.h"
46
47 #include "helper_pdu.h"
48 #include "helper_util.h"
49
50 /* Tx/Rx pause flag */
51 #define RESUMED 0U
52 #define PAUSED 1U
53
54 /* Tx/Rx encryption flag */
55 #define UNENCRYPTED 0U
56 #define ENCRYPTED 1U
57
58 /* Check Rx Pause and Encryption state */
59 #define CHECK_RX_PE_STATE(_conn, _pause, _enc) \
60 do { \
61 zassert_equal(_conn.pause_rx_data, _pause, "Rx Data pause state is wrong.");\
62 zassert_equal(_conn.lll.enc_rx, _enc, "Rx Encryption state is wrong."); \
63 } while (0)
64
65 /* Check Tx Pause and Encryption state */
66 #define CHECK_TX_PE_STATE(_conn, _pause, _enc) \
67 do { \
68 zassert_equal(_conn.tx_q.pause_data, _pause, "Tx Data pause state is wrong.");\
69 zassert_equal(_conn.lll.enc_tx, _enc, "Tx Encryption state is wrong."); \
70 } while (0)
71
72 /* CCM direction flag */
73 #define CCM_DIR_M_TO_S 1U
74 #define CCM_DIR_S_TO_M 0U
75
76 /* Check Rx CCM state */
77 #define CHECK_RX_CCM_STATE(_conn, _sk_be, _iv, _cnt, _dir) \
78 do { \
79 zassert_mem_equal(_conn.lll.ccm_rx.key, _sk_be, sizeof(_sk_be), \
80 "CCM Rx SK not equal to expected SK"); \
81 zassert_mem_equal(_conn.lll.ccm_rx.iv, _iv, sizeof(_iv), \
82 "CCM Rx IV not equal to (IVm | IVs)"); \
83 zassert_equal(_conn.lll.ccm_rx.counter, _cnt, "CCM Rx Counter is wrong"); \
84 zassert_equal(_conn.lll.ccm_rx.direction, _dir, "CCM Rx Direction is wrong");\
85 } while (0)
86
87 /* Check Tx CCM state */
88 #define CHECK_TX_CCM_STATE(_conn, _sk_be, _iv, _cnt, _dir) \
89 do { \
90 zassert_mem_equal(_conn.lll.ccm_tx.key, _sk_be, sizeof(_sk_be), \
91 "CCM Tx SK not equal to expected SK"); \
92 zassert_mem_equal(_conn.lll.ccm_tx.iv, _iv, sizeof(_iv), \
93 "CCM Tx IV not equal to (IVm | IVs)"); \
94 zassert_equal(_conn.lll.ccm_tx.counter, _cnt, "CCM Tx Counter is wrong"); \
95 zassert_equal(_conn.lll.ccm_tx.direction, _dir, "CCM Tx Direction is wrong");\
96 } while (0)
97
98 static struct ll_conn conn;
99
enc_setup(void * data)100 static void enc_setup(void *data)
101 {
102 test_setup(&conn);
103
104 /* Fake that a Feature exchange proceudre has been executed */
105 conn.llcp.fex.valid = 1U;
106 conn.llcp.fex.features_used |= LL_FEAT_BIT_EXT_REJ_IND;
107 }
108
ecb_encrypt(uint8_t const * const key_le,uint8_t const * const clear_text_le,uint8_t * const cipher_text_le,uint8_t * const cipher_text_be)109 void ecb_encrypt(uint8_t const *const key_le, uint8_t const *const clear_text_le,
110 uint8_t *const cipher_text_le, uint8_t *const cipher_text_be)
111 {
112 ztest_check_expected_data(key_le, 16);
113 ztest_check_expected_data(clear_text_le, 16);
114 if (cipher_text_le) {
115 ztest_copy_return_data(cipher_text_le, 16);
116 }
117
118 if (cipher_text_be) {
119 ztest_copy_return_data(cipher_text_be, 16);
120 }
121 }
122
lll_csrand_get(void * buf,size_t len)123 int lll_csrand_get(void *buf, size_t len)
124 {
125 ztest_check_expected_value(len);
126 ztest_copy_return_data(buf, len);
127 return ztest_get_return_value();
128 }
129
130 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 6, Part C
131 * 1 ENCRYPTION SAMPLE DATA
132 */
133 #define RAND 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90
134 #define EDIV 0x24, 0x74
135 #define LTK \
136 0x4C, 0x68, 0x38, 0x41, 0x39, 0xF5, 0x74, 0xD8, 0x36, 0xBC, 0xF3, 0x4E, 0x9D, 0xFB, 0x01,\
137 0xBF
138 #define SKDM 0xAC, 0xBD, 0xCE, 0xDF, 0xE0, 0xF1, 0x02, 0x13
139 #define SKDS 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79
140 #define IVM 0xBA, 0xDC, 0xAB, 0x24
141 #define IVS 0xDE, 0xAF, 0xBA, 0xBE
142
143 #define SK_BE \
144 0x66, 0xC6, 0xC2, 0x27, 0x8E, 0x3B, 0x8E, 0x05, 0x3E, 0x7E, 0xA3, 0x26, 0x52, 0x1B, 0xAD,\
145 0x99
146 /* +-----+ +-------+ +-----+
147 * | UT | | LL_A | | LT |
148 * +-----+ +-------+ +-----+
149 * | | |
150 * | Initiate | |
151 * | Encryption Start Proc. | |
152 * |--------------------------->| |
153 * | -----------------\ | |
154 * | | Empty Tx queue |-| |
155 * | |----------------| | |
156 * | | |
157 * | | LL_ENC_REQ |
158 * | |-------------------->|
159 * | | |
160 * | | LL_ENC_RSP |
161 * | |<--------------------|
162 * | | |
163 * | | LL_START_ENC_REQ |
164 * | |<--------------------|
165 * | ----------------\ | |
166 * | | Tx Encryption |-| |
167 * | | Rx Decryption | | |
168 * | |---------------| | |
169 * | | |
170 * | | LL_START_ENC_RSP |
171 * | |-------------------->|
172 * | | |
173 * | | LL_START_ENC_RSP |
174 * | |<--------------------|
175 * | | |
176 * | Encryption Start Proc. | |
177 * | Complete | |
178 * |<---------------------------| |
179 * | | |
180 */
ZTEST(encryption_start,test_encryption_start_central_loc)181 ZTEST(encryption_start, test_encryption_start_central_loc)
182 {
183 uint8_t err;
184 struct node_tx *tx;
185 struct node_rx_pdu *ntf;
186
187 const uint8_t rand[] = { RAND };
188 const uint8_t ediv[] = { EDIV };
189 const uint8_t ltk[] = { LTK };
190 const uint8_t skd[] = { SKDM, SKDS };
191 const uint8_t sk_be[] = { SK_BE };
192 const uint8_t iv[] = { IVM, IVS };
193
194 /* Prepare expected LL_ENC_REQ */
195 struct pdu_data_llctrl_enc_req exp_enc_req = {
196 .rand = { RAND },
197 .ediv = { EDIV },
198 .skdm = { SKDM },
199 .ivm = { IVM },
200 };
201
202 /* Prepare LL_ENC_RSP */
203 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
204
205 /* Prepare mocked call to lll_csrand_get */
206 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
207 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
208 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
209
210 /* Prepare mocked call to ecb_encrypt */
211 ztest_expect_data(ecb_encrypt, key_le, ltk);
212 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
213 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
214
215 /* Role */
216 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
217
218 /* Connect */
219 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
220
221 /* Check state */
222 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
223 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
224
225 /* Initiate an Encryption Start Procedure */
226 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
227 zassert_equal(err, BT_HCI_ERR_SUCCESS);
228
229 /* Prepare */
230 event_prepare(&conn);
231
232 /* Tx Queue should have one LL Control PDU */
233 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
234 lt_rx_q_is_empty(&conn);
235
236 /* Check state */
237 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
238 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
239
240 /* Release Tx */
241 ull_cp_release_tx(&conn, tx);
242
243 /* Rx */
244 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
245
246 /* Rx */
247 lt_tx(LL_START_ENC_REQ, &conn, NULL);
248
249 /* Done */
250 event_done(&conn);
251
252 /* Check state */
253 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
254 CHECK_TX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Tx paused & enc. */
255
256 /* CCM Tx/Rx SK should match SK */
257 /* CCM Tx/Rx IV should match the IV */
258 /* CCM Tx/Rx Counter should be zero */
259 /* CCM Rx Direction should be S->M */
260 /* CCM Tx Direction should be M->S */
261 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
262 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
263
264 /* Prepare */
265 event_prepare(&conn);
266
267 /* Tx Queue should have one LL Control PDU */
268 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
269 lt_rx_q_is_empty(&conn);
270
271 /* Release Tx */
272 ull_cp_release_tx(&conn, tx);
273
274 /* Check state */
275 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
276 CHECK_TX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Tx paused & enc. */
277
278 /* Rx */
279 lt_tx(LL_START_ENC_RSP, &conn, NULL);
280
281 /* Done */
282 event_done(&conn);
283
284 /* Check state */
285 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
286 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
287
288 /* There should be one host notification */
289 ut_rx_pdu(LL_START_ENC_RSP, &ntf, NULL);
290 ut_rx_q_is_empty();
291
292 /* Release Ntf */
293 release_ntf(ntf);
294
295 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
296 "Free CTX buffers %d", llcp_ctx_buffers_free());
297 }
298
299 /* +-----+ +-------+ +-----+
300 * | UT | | LL_A | | LT |
301 * +-----+ +-------+ +-----+
302 * | -----------------\ | |
303 * | | Reserver all |-| |
304 * | | Tx/Ntf buffers | | |
305 * | |----------------| | |
306 * | | |
307 * | Initiate | |
308 * | Encryption Start Proc. | |
309 * |--------------------------->| |
310 * | -----------------\ | |
311 * | | Empty Tx queue |-| |
312 * | |----------------| | |
313 * | | |
314 * | | LL_ENC_REQ |
315 * | |-------------------->|
316 * | | |
317 * | | LL_ENC_RSP |
318 * | |<--------------------|
319 * | | |
320 * | | LL_START_ENC_REQ |
321 * | |<--------------------|
322 * | ----------------\ | |
323 * | | Tx Encryption |-| |
324 * | | Rx Decryption | | |
325 * | |---------------| | |
326 * | | |
327 * | | LL_START_ENC_RSP |
328 * | |-------------------->|
329 * | | |
330 * | | LL_START_ENC_RSP |
331 * | |<--------------------|
332 * | | |
333 * | Encryption Start Proc. | |
334 * | Complete | |
335 * |<---------------------------| |
336 * | | |
337 */
ZTEST(encryption_start,test_encryption_start_central_loc_limited_memory)338 ZTEST(encryption_start, test_encryption_start_central_loc_limited_memory)
339 {
340 uint8_t err;
341 struct node_tx *tx;
342 struct node_rx_pdu *ntf;
343 struct proc_ctx *ctx = NULL;
344
345 const uint8_t rand[] = { RAND };
346 const uint8_t ediv[] = { EDIV };
347 const uint8_t ltk[] = { LTK };
348 const uint8_t skd[] = { SKDM, SKDS };
349 const uint8_t sk_be[] = { SK_BE };
350 const uint8_t iv[] = { IVM, IVS };
351
352 /* Prepare expected LL_ENC_REQ */
353 struct pdu_data_llctrl_enc_req exp_enc_req = {
354 .rand = { RAND },
355 .ediv = { EDIV },
356 .skdm = { SKDM },
357 .ivm = { IVM },
358 };
359
360 /* Prepare LL_ENC_RSP */
361 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
362
363 /* Prepare mocked call to lll_csrand_get */
364 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
365 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
366 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
367
368 /* Prepare mocked call to ecb_encrypt */
369 ztest_expect_data(ecb_encrypt, key_le, ltk);
370 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
371 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
372
373 /* Role */
374 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
375
376 /* Connect */
377 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
378
379 /* Check state */
380 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
381 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
382
383 /* Allocate dummy procedure used to steal all buffers */
384 ctx = llcp_create_local_procedure(PROC_VERSION_EXCHANGE);
385
386 /* Steal all tx buffers */
387 while (llcp_tx_alloc_peek(&conn, ctx)) {
388 tx = llcp_tx_alloc(&conn, ctx);
389 zassert_not_null(tx, NULL);
390 }
391
392 /* Dummy remove, as above loop might queue up ctx */
393 llcp_tx_alloc_unpeek(ctx);
394
395 /* Initiate an Encryption Start Procedure */
396 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
397 zassert_equal(err, BT_HCI_ERR_SUCCESS);
398
399 /* Prepare */
400 event_prepare(&conn);
401
402 /* Tx Queue should have no LL Control PDU */
403 lt_rx_q_is_empty(&conn);
404
405 /* Check state */
406 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
407 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
408
409 /* Done */
410 event_done(&conn);
411
412 /* Check state */
413 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
414 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
415
416 /* Release Tx */
417 ull_cp_release_tx(&conn, tx);
418
419 /* Prepare */
420 event_prepare(&conn);
421
422 /* Tx Queue should have one LL Control PDU */
423 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
424 lt_rx_q_is_empty(&conn);
425
426 /* Check state */
427 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
428 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
429
430 /* Rx */
431 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
432
433 /* Rx */
434 lt_tx(LL_START_ENC_REQ, &conn, NULL);
435
436 /* Done */
437 event_done(&conn);
438
439 /* Check state */
440 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
441 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
442
443 /* Tx Queue should have no LL Control PDU */
444 lt_rx_q_is_empty(&conn);
445
446 /* Release Tx */
447 ull_cp_release_tx(&conn, tx);
448
449 /* Prepare */
450 event_prepare(&conn);
451
452 /* Tx Queue should have no LL Control PDU */
453 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
454 lt_rx_q_is_empty(&conn);
455
456 /* Check state */
457 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
458 CHECK_TX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Tx paused & enc. */
459
460 /* CCM Tx/Rx SK should match SK */
461 /* CCM Tx/Rx IV should match the IV */
462 /* CCM Tx/Rx Counter should be zero */
463 /* CCM Tx Direction should be M->S */
464 /* CCM Rx Direction should be S->M */
465 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
466 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
467
468 /* Release Tx */
469 ull_cp_release_tx(&conn, tx);
470
471 /* Rx */
472 lt_tx(LL_START_ENC_RSP, &conn, NULL);
473
474 /* Done */
475 event_done(&conn);
476
477 /* Check state */
478 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
479 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
480
481 /* There should be one host notification */
482 ut_rx_pdu(LL_START_ENC_RSP, &ntf, NULL);
483 ut_rx_q_is_empty();
484
485 /* Release Ntf */
486 release_ntf(ntf);
487
488 /* Tx Encryption should be enabled */
489 zassert_equal(conn.lll.enc_tx, 1U);
490
491 /* Rx Decryption should be enabled */
492 zassert_equal(conn.lll.enc_rx, 1U);
493
494 /* Release dummy procedure */
495 llcp_proc_ctx_release(ctx);
496
497 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
498 "Free CTX buffers %d", llcp_ctx_buffers_free());
499 }
500
501 /* +-----+ +-------+ +-----+
502 * | UT | | LL_A | | LT |
503 * +-----+ +-------+ +-----+
504 * | | |
505 * | Initiate | |
506 * | Encryption Start Proc. | |
507 * |--------------------------->| |
508 * | -----------------\ | |
509 * | | Empty Tx queue |-| |
510 * | |----------------| | |
511 * | | |
512 * | | LL_ENC_REQ |
513 * | |-------------------->|
514 * | | |
515 * | | LL_REJECT_EXT_IND |
516 * | |<--------------------|
517 * | | |
518 * | Encryption Start Proc. | |
519 * | Complete | |
520 * |<---------------------------| |
521 * | | |
522 */
ZTEST(encryption_start,test_encryption_start_central_loc_reject_ext)523 ZTEST(encryption_start, test_encryption_start_central_loc_reject_ext)
524 {
525 uint8_t err;
526 struct node_tx *tx;
527 struct node_rx_pdu *ntf;
528
529 const uint8_t rand[] = { RAND };
530 const uint8_t ediv[] = { EDIV };
531 const uint8_t ltk[] = { LTK };
532
533 /* Prepare expected LL_ENC_REQ */
534 struct pdu_data_llctrl_enc_req exp_enc_req = {
535 .rand = { RAND },
536 .ediv = { EDIV },
537 .skdm = { SKDM },
538 .ivm = { IVM },
539 };
540
541 /* Prepare mocked call to lll_csrand_get */
542 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
543 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
544 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
545
546 struct pdu_data_llctrl_reject_ind reject_ind = { .error_code =
547 BT_HCI_ERR_UNSUPP_REMOTE_FEATURE };
548
549 struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
550 .reject_opcode = PDU_DATA_LLCTRL_TYPE_ENC_REQ,
551 .error_code = BT_HCI_ERR_UNSUPP_REMOTE_FEATURE
552 };
553
554 /* Role */
555 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
556
557 /* Connect */
558 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
559
560 /* Check state */
561 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
562 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
563
564 /* Initiate an Encryption Start Procedure */
565 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
566 zassert_equal(err, BT_HCI_ERR_SUCCESS);
567
568 /* Prepare */
569 event_prepare(&conn);
570
571 /* Tx Queue should have one LL Control PDU */
572 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
573 lt_rx_q_is_empty(&conn);
574
575 /* Check state */
576 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
577 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
578
579 /* Release Tx */
580 ull_cp_release_tx(&conn, tx);
581
582 /* Rx */
583 lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
584
585 /* Done */
586 event_done(&conn);
587
588 /* Check state */
589 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
590 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
591
592 /* There should be one host notification */
593 ut_rx_pdu(LL_REJECT_IND, &ntf, &reject_ind);
594 ut_rx_q_is_empty();
595
596 /* Release Ntf */
597 release_ntf(ntf);
598
599 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
600 "Free CTX buffers %d", llcp_ctx_buffers_free());
601 }
602
603 /* +-----+ +-------+ +-----+
604 * | UT | | LL_A | | LT |
605 * +-----+ +-------+ +-----+
606 * | | |
607 * | Initiate | |
608 * | Encryption Start Proc. | |
609 * |--------------------------->| |
610 * | -----------------\ | |
611 * | | Empty Tx queue |-| |
612 * | |----------------| | |
613 * | | |
614 * | | LL_ENC_REQ |
615 * | |-------------------->|
616 * | | |
617 * | | LL_REJECT_IND |
618 * | |<--------------------|
619 * | | |
620 * | Encryption Start Proc. | |
621 * | Complete | |
622 * |<---------------------------| |
623 * | | |
624 */
ZTEST(encryption_start,test_encryption_start_central_loc_reject)625 ZTEST(encryption_start, test_encryption_start_central_loc_reject)
626 {
627 uint8_t err;
628 struct node_tx *tx;
629 struct node_rx_pdu *ntf;
630
631 const uint8_t rand[] = { RAND };
632 const uint8_t ediv[] = { EDIV };
633 const uint8_t ltk[] = { LTK };
634
635 /* Prepare expected LL_ENC_REQ */
636 struct pdu_data_llctrl_enc_req exp_enc_req = {
637 .rand = { RAND },
638 .ediv = { EDIV },
639 .skdm = { SKDM },
640 .ivm = { IVM },
641 };
642
643 /* Prepare mocked call to lll_csrand_get */
644 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
645 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
646 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
647
648 struct pdu_data_llctrl_reject_ind reject_ind = { .error_code =
649 BT_HCI_ERR_UNSUPP_REMOTE_FEATURE };
650
651 /* Role */
652 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
653
654 /* Connect */
655 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
656
657 /* Check state */
658 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
659 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
660
661 /* Initiate an Encryption Start Procedure */
662 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
663 zassert_equal(err, BT_HCI_ERR_SUCCESS);
664
665 /* Prepare */
666 event_prepare(&conn);
667
668 /* Tx Queue should have one LL Control PDU */
669 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
670 lt_rx_q_is_empty(&conn);
671
672 /* Check state */
673 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
674 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
675
676 /* Release Tx */
677 ull_cp_release_tx(&conn, tx);
678
679 /* Rx */
680 lt_tx(LL_REJECT_IND, &conn, &reject_ind);
681
682 /* Done */
683 event_done(&conn);
684
685 /* Check state */
686 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
687 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
688
689 /* There should be one host notification */
690 ut_rx_pdu(LL_REJECT_IND, &ntf, &reject_ind);
691 ut_rx_q_is_empty();
692
693 /* Release Ntf */
694 release_ntf(ntf);
695
696 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
697 "Free CTX buffers %d", llcp_ctx_buffers_free());
698 }
699
700 /* +-----+ +-------+ +-----+
701 * | UT | | LL_A | | LT |
702 * +-----+ +-------+ +-----+
703 * | | |
704 * | Initiate | |
705 * | Encryption Start Proc. | |
706 * |--------------------------->| |
707 * | -----------------\ | |
708 * | | Empty Tx queue |-| |
709 * | |----------------| | |
710 * | | |
711 * | | LL_ENC_REQ |
712 * | |-------------------->|
713 * | | |
714 * | | LL_ENC_RSP |
715 * | |<--------------------|
716 * | | |
717 * | | LL_REJECT_EXT_IND |
718 * | |<--------------------|
719 * | | |
720 * | Encryption Start Proc. | |
721 * | Complete | |
722 * |<---------------------------| |
723 * | | |
724 */
ZTEST(encryption_start,test_encryption_start_central_loc_no_ltk)725 ZTEST(encryption_start, test_encryption_start_central_loc_no_ltk)
726 {
727 uint8_t err;
728 struct node_tx *tx;
729 struct node_rx_pdu *ntf;
730
731 const uint8_t rand[] = { RAND };
732 const uint8_t ediv[] = { EDIV };
733 const uint8_t ltk[] = { LTK };
734
735 /* Prepare expected LL_ENC_REQ */
736 struct pdu_data_llctrl_enc_req exp_enc_req = {
737 .rand = { RAND },
738 .ediv = { EDIV },
739 .skdm = { SKDM },
740 .ivm = { IVM },
741 };
742
743 /* Prepare LL_ENC_RSP */
744 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
745
746 /* Prepare mocked call to lll_csrand_get */
747 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
748 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
749 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
750
751 struct pdu_data_llctrl_reject_ind reject_ind = { .error_code =
752 BT_HCI_ERR_PIN_OR_KEY_MISSING };
753
754 struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
755 .reject_opcode = PDU_DATA_LLCTRL_TYPE_ENC_REQ,
756 .error_code = BT_HCI_ERR_PIN_OR_KEY_MISSING
757 };
758
759 /* Role */
760 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
761
762 /* Connect */
763 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
764
765 /* Check state */
766 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
767 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
768
769 /* Initiate an Encryption Start Procedure */
770 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
771 zassert_equal(err, BT_HCI_ERR_SUCCESS);
772
773 /* Prepare */
774 event_prepare(&conn);
775
776 /* Tx Queue should have one LL Control PDU */
777 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
778 lt_rx_q_is_empty(&conn);
779
780 /* Check state */
781 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
782 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
783
784 /* Release Tx */
785 ull_cp_release_tx(&conn, tx);
786
787 /* Rx */
788 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
789
790 /* Rx */
791 lt_tx(LL_REJECT_EXT_IND, &conn, &reject_ext_ind);
792
793 /* Done */
794 event_done(&conn);
795
796 /* Check state */
797 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
798 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
799
800 /* There should be one host notification */
801 ut_rx_pdu(LL_REJECT_IND, &ntf, &reject_ind);
802 ut_rx_q_is_empty();
803
804 /* Release Ntf */
805 release_ntf(ntf);
806
807 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
808 "Free CTX buffers %d", llcp_ctx_buffers_free());
809 }
810
811 /* +-----+ +-------+ +-----+
812 * | UT | | LL_A | | LT |
813 * +-----+ +-------+ +-----+
814 * | | |
815 * | Initiate | |
816 * | Encryption Start Proc. | |
817 * |--------------------------->| |
818 * | -----------------\ | |
819 * | | Empty Tx queue |-| |
820 * | |----------------| | |
821 * | | |
822 * | | LL_ENC_REQ |
823 * | |-------------------->|
824 * | | |
825 * | | LL_ENC_RSP |
826 * | |<--------------------|
827 * | | |
828 * | | LL_REJECT_IND |
829 * | |<--------------------|
830 * | | |
831 * | Encryption Start Proc. | |
832 * | Complete | |
833 * |<---------------------------| |
834 * | | |
835 */
ZTEST(encryption_start,test_encryption_start_central_loc_no_ltk_2)836 ZTEST(encryption_start, test_encryption_start_central_loc_no_ltk_2)
837 {
838 uint8_t err;
839 struct node_tx *tx;
840 struct node_rx_pdu *ntf;
841
842 const uint8_t rand[] = { RAND };
843 const uint8_t ediv[] = { EDIV };
844 const uint8_t ltk[] = { LTK };
845
846 /* Prepare expected LL_ENC_REQ */
847 struct pdu_data_llctrl_enc_req exp_enc_req = {
848 .rand = { RAND },
849 .ediv = { EDIV },
850 .skdm = { SKDM },
851 .ivm = { IVM },
852 };
853
854 /* Prepare LL_ENC_RSP */
855 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
856
857 /* Prepare mocked call to lll_csrand_get */
858 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
859 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
860 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
861
862 struct pdu_data_llctrl_reject_ind reject_ind = { .error_code =
863 BT_HCI_ERR_PIN_OR_KEY_MISSING };
864
865 /* Role */
866 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
867
868 /* Connect */
869 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
870
871 /* Check state */
872 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
873 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
874
875 /* Initiate an Encryption Start Procedure */
876 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
877 zassert_equal(err, BT_HCI_ERR_SUCCESS);
878
879 /* Prepare */
880 event_prepare(&conn);
881
882 /* Tx Queue should have one LL Control PDU */
883 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
884 lt_rx_q_is_empty(&conn);
885
886 /* Check state */
887 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
888 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
889
890 /* Release Tx */
891 ull_cp_release_tx(&conn, tx);
892
893 /* Rx */
894 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
895
896 /* Rx */
897 lt_tx(LL_REJECT_IND, &conn, &reject_ind);
898
899 /* Done */
900 event_done(&conn);
901
902 /* Check state */
903 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
904 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
905
906 /* There should be one host notification */
907 ut_rx_pdu(LL_REJECT_IND, &ntf, &reject_ind);
908 ut_rx_q_is_empty();
909
910 /* Release Ntf */
911 release_ntf(ntf);
912
913 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
914 "Free CTX buffers %d", llcp_ctx_buffers_free());
915 }
916
917 /* +-----+ +-------+ +-----+
918 * | UT | | LL_A | | LT |
919 * +-----+ +-------+ +-----+
920 * | | |
921 * | Initiate | |
922 * | Encryption Start Proc. | |
923 * |--------------------------->| |
924 * | -----------------\ | |
925 * | | Empty Tx queue |-| |
926 * | |----------------| | |
927 * | | |
928 * | | LL_ENC_REQ |
929 * | |-------------------->|
930 * | | |
931 * | | LL_ENC_RSP |
932 * | |<--------------------|
933 * | | |
934 * | | LL_VERSION_IND |
935 * | |<--------------------|
936 * | | |
937 * | Encryption Start Proc. | |
938 * | Complete | |
939 * |<---------------------------| |
940 * | | |
941 */
ZTEST(encryption_start,test_encryption_start_central_loc_mic)942 ZTEST(encryption_start, test_encryption_start_central_loc_mic)
943 {
944 uint8_t err;
945 struct node_tx *tx;
946
947 const uint8_t rand[] = { RAND };
948 const uint8_t ediv[] = { EDIV };
949 const uint8_t ltk[] = { LTK };
950
951 /* Prepare expected LL_ENC_REQ */
952 struct pdu_data_llctrl_enc_req exp_enc_req = {
953 .rand = { RAND },
954 .ediv = { EDIV },
955 .skdm = { SKDM },
956 .ivm = { IVM },
957 };
958
959 /* Prepare LL_ENC_RSP */
960 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
961
962 struct pdu_data_llctrl_version_ind remote_version_ind = {
963 .version_number = 0x55,
964 .company_id = 0xABCD,
965 .sub_version_number = 0x1234,
966 };
967
968 /* Prepare mocked call to lll_csrand_get */
969 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
970 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
971 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
972
973 /* Role */
974 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
975
976 /* Connect */
977 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
978
979 /* Check state */
980 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
981 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
982
983 /* Initiate an Encryption Start Procedure */
984 err = ull_cp_encryption_start(&conn, rand, ediv, ltk);
985 zassert_equal(err, BT_HCI_ERR_SUCCESS);
986
987 /* Prepare */
988 event_prepare(&conn);
989
990 /* Tx Queue should have one LL Control PDU */
991 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
992 lt_rx_q_is_empty(&conn);
993
994 /* Check state */
995 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
996 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
997
998 /* Release Tx */
999 ull_cp_release_tx(&conn, tx);
1000
1001 /* Rx */
1002 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
1003
1004 /* Rx */
1005 lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
1006
1007 /* Done */
1008 event_done(&conn);
1009
1010 /* Check state */
1011 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1012 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1013
1014 /* There should not be a host notification */
1015 ut_rx_q_is_empty();
1016
1017 /**/
1018 zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL,
1019 "Expected termination due to MIC failure");
1020
1021 /*
1022 * For a 40s procedure response timeout with a connection interval of
1023 * 7.5ms, a total of 5333.33 connection events are needed, verify that
1024 * the state doesn't change for that many invocations.
1025 */
1026 for (int n = 5334; n > 0; n--) {
1027 /* Prepare */
1028 event_prepare(&conn);
1029
1030 /* Tx Queue should NOT have a LL Control PDU */
1031 lt_rx_q_is_empty(&conn);
1032
1033 /* Done */
1034 event_done(&conn);
1035
1036 /* Check state */
1037 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1038 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1039
1040 /* There should NOT be a host notification */
1041 ut_rx_q_is_empty();
1042 }
1043
1044 /* Note that for this test the context is not released */
1045 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt() - 1,
1046 "Free CTX buffers %d", llcp_ctx_buffers_free());
1047 }
1048
1049 /* +-----+ +-------+ +-----+
1050 * | UT | | LL_A | | LT |
1051 * +-----+ +-------+ +-----+
1052 * | | |
1053 * | | LL_ENC_REQ |
1054 * | |<--------------------|
1055 * | -----------------\ | |
1056 * | | Empty Tx queue |-| |
1057 * | |----------------| | |
1058 * | | |
1059 * | | LL_ENC_RSP |
1060 * | |-------------------->|
1061 * | | |
1062 * | LTK Request | |
1063 * |<----------------------| |
1064 * | | |
1065 * | LTK Request Reply | |
1066 * |---------------------->| |
1067 * | | |
1068 * | | LL_START_ENC_REQ |
1069 * | |-------------------->|
1070 * | ----------------\ | |
1071 * | | Rx Decryption |-| |
1072 * | |---------------| | |
1073 * | | |
1074 * | | LL_START_ENC_RSP |
1075 * | |<--------------------|
1076 * | | |
1077 * | Encryption Change | |
1078 * |<----------------------| |
1079 * | | |
1080 * | | LL_START_ENC_RSP |
1081 * | |-------------------->|
1082 * | ----------------\ | |
1083 * | | Tx Encryption |-| |
1084 * | |---------------| | |
1085 * | | |
1086 */
ZTEST(encryption_start,test_encryption_start_periph_rem)1087 ZTEST(encryption_start, test_encryption_start_periph_rem)
1088 {
1089 struct node_tx *tx;
1090 struct node_rx_pdu *ntf;
1091
1092 const uint8_t ltk[] = { LTK };
1093 const uint8_t skd[] = { SKDM, SKDS };
1094 const uint8_t sk_be[] = { SK_BE };
1095 const uint8_t iv[] = { IVM, IVS };
1096
1097 /* Prepare LL_ENC_REQ */
1098 struct pdu_data_llctrl_enc_req enc_req = {
1099 .rand = { RAND },
1100 .ediv = { EDIV },
1101 .skdm = { SKDM },
1102 .ivm = { IVM },
1103 };
1104
1105 struct pdu_data_llctrl_enc_rsp exp_enc_rsp = {
1106 .skds = { SKDS },
1107 .ivs = { IVS },
1108 };
1109
1110 /* Prepare mocked call to lll_csrand_get */
1111 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1112 ztest_return_data(lll_csrand_get, buf, exp_enc_rsp.skds);
1113 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1114
1115 /* Prepare mocked call to ecb_encrypt */
1116 ztest_expect_data(ecb_encrypt, key_le, ltk);
1117 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
1118 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
1119
1120 /* Role */
1121 test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1122
1123 /* Connect */
1124 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1125
1126 /* Check state */
1127 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1128 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1129
1130 /* Prepare */
1131 event_prepare(&conn);
1132
1133 /* Check state */
1134 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1135 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1136
1137 /* Rx */
1138 lt_tx(LL_ENC_REQ, &conn, &enc_req);
1139
1140 /* Done */
1141 event_done(&conn);
1142
1143 /* Check state */
1144 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1145 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1146
1147 /* Prepare */
1148 event_prepare(&conn);
1149
1150 /* Check state */
1151 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1152 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1153
1154 /* Tx Queue should have one LL Control PDU */
1155 lt_rx(LL_ENC_RSP, &conn, &tx, &exp_enc_rsp);
1156 lt_rx_q_is_empty(&conn);
1157
1158 /* Done */
1159 event_done(&conn);
1160
1161 /* Check state */
1162 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1163 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1164
1165 /* Release Tx */
1166 ull_cp_release_tx(&conn, tx);
1167
1168 /* There should be a host notification */
1169 ut_rx_pdu(LL_ENC_REQ, &ntf, &enc_req);
1170 ut_rx_q_is_empty();
1171
1172 /* Release Ntf */
1173 release_ntf(ntf);
1174
1175 /* LTK request reply */
1176 ull_cp_ltk_req_reply(&conn, ltk);
1177
1178 /* Check state */
1179 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1180 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1181
1182 /* Prepare */
1183 event_prepare(&conn);
1184
1185 /* Tx Queue should have one LL Control PDU */
1186 lt_rx(LL_START_ENC_REQ, &conn, &tx, NULL);
1187 lt_rx_q_is_empty(&conn);
1188
1189 /* Check state */
1190 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1191 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1192
1193 /* Done */
1194 event_done(&conn);
1195
1196 /* Check state */
1197 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1198 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1199
1200 /* Release Tx */
1201 ull_cp_release_tx(&conn, tx);
1202
1203 /* CCM Rx SK should match SK */
1204 /* CCM Rx IV should match the IV */
1205 /* CCM Rx Counter should be zero */
1206 /* CCM Rx Direction should be M->S */
1207 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
1208
1209 /* Prepare */
1210 event_prepare(&conn);
1211
1212 /* Check state */
1213 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1214 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1215
1216 /* Rx */
1217 lt_tx(LL_START_ENC_RSP, &conn, NULL);
1218
1219 /* Done */
1220 event_done(&conn);
1221
1222 /* Check state */
1223 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
1224 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
1225
1226 /* There should be a host notification */
1227 ut_rx_pdu(LL_START_ENC_RSP, &ntf, NULL);
1228 ut_rx_q_is_empty();
1229
1230 /* Prepare */
1231 event_prepare(&conn);
1232
1233 /* Tx Queue should have one LL Control PDU */
1234 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
1235 lt_rx_q_is_empty(&conn);
1236
1237 /* Check state */
1238 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
1239 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
1240
1241 /* Done */
1242 event_done(&conn);
1243
1244 /* Check state */
1245 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
1246 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
1247
1248 /* Release Tx */
1249 ull_cp_release_tx(&conn, tx);
1250
1251 /* CCM Tx SK should match SK */
1252 /* CCM Tx IV should match the IV */
1253 /* CCM Tx Counter should be zero */
1254 /* CCM Tx Direction should be S->M */
1255 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
1256
1257 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1258 "Free CTX buffers %d", llcp_ctx_buffers_free());
1259 }
1260
1261 /* +-----+ +-------+ +-----+
1262 * | UT | | LL_A | | LT |
1263 * +-----+ +-------+ +-----+
1264 * | -----------------\ | |
1265 * | | Reserver all |-| |
1266 * | | Tx/Ntf buffers | | |
1267 * | |----------------| | |
1268 * | | |
1269 * | | LL_ENC_REQ |
1270 * | |<--------------------|
1271 * | -----------------\ | |
1272 * | | Empty Tx queue |-| |
1273 * | |----------------| | |
1274 * | | |
1275 * | | LL_ENC_RSP |
1276 * | |-------------------->|
1277 * | | |
1278 * | LTK Request | |
1279 * |<----------------------| |
1280 * | | |
1281 * | LTK Request Reply | |
1282 * |---------------------->| |
1283 * | | |
1284 * | | LL_START_ENC_REQ |
1285 * | |-------------------->|
1286 * | ----------------\ | |
1287 * | | Rx Decryption |-| |
1288 * | |---------------| | |
1289 * | | |
1290 * | | LL_START_ENC_RSP |
1291 * | |<--------------------|
1292 * | | |
1293 * | Encryption Change | |
1294 * |<----------------------| |
1295 * | | |
1296 * | | LL_START_ENC_RSP |
1297 * | |-------------------->|
1298 * | ----------------\ | |
1299 * | | Tx Encryption |-| |
1300 * | |---------------| | |
1301 * | | |
1302 */
ZTEST(encryption_start,test_encryption_start_periph_rem_limited_memory)1303 ZTEST(encryption_start, test_encryption_start_periph_rem_limited_memory)
1304 {
1305 struct node_tx *tx;
1306 struct node_rx_pdu *ntf;
1307 struct proc_ctx *ctx = NULL;
1308
1309 const uint8_t ltk[] = { LTK };
1310 const uint8_t skd[] = { SKDM, SKDS };
1311 const uint8_t sk_be[] = { SK_BE };
1312 const uint8_t iv[] = { IVM, IVS };
1313
1314 /* Prepare LL_ENC_REQ */
1315 struct pdu_data_llctrl_enc_req enc_req = {
1316 .rand = { RAND },
1317 .ediv = { EDIV },
1318 .skdm = { SKDM },
1319 .ivm = { IVM },
1320 };
1321
1322 struct pdu_data_llctrl_enc_rsp exp_enc_rsp = {
1323 .skds = { SKDS },
1324 .ivs = { IVS },
1325 };
1326
1327 /* Prepare mocked call to lll_csrand_get */
1328 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1329 ztest_return_data(lll_csrand_get, buf, exp_enc_rsp.skds);
1330 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1331
1332 /* Prepare mocked call to ecb_encrypt */
1333 ztest_expect_data(ecb_encrypt, key_le, ltk);
1334 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
1335 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
1336
1337 /* Role */
1338 test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1339
1340 /* Connect */
1341 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1342
1343 /* Allocate dummy procedure used to steal all buffers */
1344 ctx = llcp_create_local_procedure(PROC_VERSION_EXCHANGE);
1345
1346 /* Steal all tx buffers */
1347 while (llcp_tx_alloc_peek(&conn, ctx)) {
1348 tx = llcp_tx_alloc(&conn, ctx);
1349 zassert_not_null(tx, NULL);
1350 }
1351
1352 /* Dummy remove, as above loop might queue up ctx */
1353 llcp_tx_alloc_unpeek(ctx);
1354
1355 /* Check state */
1356 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1357 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1358
1359 /* Prepare */
1360 event_prepare(&conn);
1361
1362 /* Rx */
1363 lt_tx(LL_ENC_REQ, &conn, &enc_req);
1364
1365 /* Tx Queue should not have a LL Control PDU */
1366 lt_rx_q_is_empty(&conn);
1367
1368 /* Check state */
1369 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1370 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1371
1372 /* Done */
1373 event_done(&conn);
1374
1375 /* Check state */
1376 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1377 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1378
1379 /* Release tx */
1380 ull_cp_release_tx(&conn, tx);
1381
1382 /* Prepare */
1383 event_prepare(&conn);
1384
1385 /* Tx Queue should have one LL Control PDU */
1386 lt_rx(LL_ENC_RSP, &conn, &tx, &exp_enc_rsp);
1387 lt_rx_q_is_empty(&conn);
1388
1389 /* Check state */
1390 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1391 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1392
1393 /* There should be one host notification */
1394 ut_rx_pdu(LL_ENC_REQ, &ntf, &enc_req);
1395 ut_rx_q_is_empty();
1396
1397 /* Release ntf */
1398 release_ntf(ntf);
1399
1400 /* Done */
1401 event_done(&conn);
1402
1403 /* Check state */
1404 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1405 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1406
1407 /* LTK request reply */
1408 ull_cp_ltk_req_reply(&conn, ltk);
1409
1410 /* Check state */
1411 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1412 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1413
1414 /* Prepare */
1415 event_prepare(&conn);
1416
1417 /* Tx Queue should not have one LL Control PDU */
1418 lt_rx_q_is_empty(&conn);
1419
1420 /* Check state */
1421 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1422 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1423
1424 /* Done */
1425 event_done(&conn);
1426
1427 /* Check state */
1428 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1429 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1430
1431 /* Release tx */
1432 ull_cp_release_tx(&conn, tx);
1433
1434 /* Prepare */
1435 event_prepare(&conn);
1436
1437 /* Tx Queue should have one LL Control PDU */
1438 lt_rx(LL_START_ENC_REQ, &conn, &tx, NULL);
1439 lt_rx_q_is_empty(&conn);
1440
1441 /* Check state */
1442 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1443 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1444
1445 /* Done */
1446 event_done(&conn);
1447
1448 /* Check state */
1449 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1450 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1451
1452 /* CCM Rx SK should match SK */
1453 /* CCM Rx IV should match the IV */
1454 /* CCM Rx Counter should be zero */
1455 /* CCM Rx Direction should be M->S */
1456 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
1457
1458 /* Prepare */
1459 event_prepare(&conn);
1460
1461 /* Rx */
1462 lt_tx(LL_START_ENC_RSP, &conn, NULL);
1463
1464 /* Check state */
1465 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1466 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1467
1468 /* Done */
1469 event_done(&conn);
1470
1471 /* Check state */
1472 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1473 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1474
1475 /* There should be one host notification */
1476 ut_rx_pdu(LL_START_ENC_RSP, &ntf, NULL);
1477 ut_rx_q_is_empty();
1478
1479 /* Prepare */
1480 event_prepare(&conn);
1481
1482 /* Tx Queue should not have a LL Control PDU */
1483 lt_rx_q_is_empty(&conn);
1484
1485 /* Check state */
1486 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1487 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1488
1489 /* Done */
1490 event_done(&conn);
1491
1492 /* Check state */
1493 CHECK_RX_PE_STATE(conn, PAUSED, ENCRYPTED); /* Rx paused & enc. */
1494 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1495
1496 /* Release tx */
1497 ull_cp_release_tx(&conn, tx);
1498
1499 /* Prepare */
1500 event_prepare(&conn);
1501
1502 /* Tx Queue should have one LL Control PDU */
1503 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
1504 lt_rx_q_is_empty(&conn);
1505
1506 /* Check state */
1507 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
1508 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
1509
1510 /* Done */
1511 event_done(&conn);
1512
1513 /* Check state */
1514 CHECK_RX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Rx enc. */
1515 CHECK_TX_PE_STATE(conn, RESUMED, ENCRYPTED); /* Tx enc. */
1516
1517 /* CCM Tx SK should match SK */
1518 /* CCM Tx IV should match the IV */
1519 /* CCM Tx Counter should be zero */
1520 /* CCM Tx Direction should be S->M */
1521 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
1522
1523 /* Release dummy procedure */
1524 llcp_proc_ctx_release(ctx);
1525
1526 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1527 "Free CTX buffers %d", llcp_ctx_buffers_free());
1528 }
1529
1530 /* +-----+ +-------+ +-----+
1531 * | UT | | LL_A | | LT |
1532 * +-----+ +-------+ +-----+
1533 * | | |
1534 * | | LL_ENC_REQ |
1535 * | |<--------------------|
1536 * | -----------------\ | |
1537 * | | Empty Tx queue |-| |
1538 * | |----------------| | |
1539 * | | |
1540 * | | LL_ENC_RSP |
1541 * | |-------------------->|
1542 * | | |
1543 * | LTK Request | |
1544 * |<----------------------| |
1545 * | | |
1546 * | LTK Request Reply | |
1547 * |---------------------->| |
1548 * | | |
1549 * | | LL_REJECT_EXT_IND |
1550 * | |-------------------->|
1551 */
ZTEST(encryption_start,test_encryption_start_periph_rem_no_ltk)1552 ZTEST(encryption_start, test_encryption_start_periph_rem_no_ltk)
1553 {
1554 struct node_tx *tx;
1555 struct node_rx_pdu *ntf;
1556
1557 /* Prepare LL_ENC_REQ */
1558 struct pdu_data_llctrl_enc_req enc_req = {
1559 .rand = { RAND },
1560 .ediv = { EDIV },
1561 .skdm = { SKDM },
1562 .ivm = { IVM },
1563 };
1564
1565 struct pdu_data_llctrl_enc_rsp exp_enc_rsp = {
1566 .skds = { SKDS },
1567 .ivs = { IVS },
1568 };
1569
1570 struct pdu_data_llctrl_reject_ext_ind reject_ext_ind = {
1571 .reject_opcode = PDU_DATA_LLCTRL_TYPE_ENC_REQ,
1572 .error_code = BT_HCI_ERR_PIN_OR_KEY_MISSING
1573 };
1574
1575 /* Prepare mocked call to lll_csrand_get */
1576 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1577 ztest_return_data(lll_csrand_get, buf, exp_enc_rsp.skds);
1578 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1579
1580 /* Role */
1581 test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1582
1583 /* Connect */
1584 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1585
1586 /* Check state */
1587 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1588 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1589
1590 /* Prepare */
1591 event_prepare(&conn);
1592
1593 /* Rx */
1594 lt_tx(LL_ENC_REQ, &conn, &enc_req);
1595
1596 /* Check state */
1597 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1598 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1599
1600 /* Done */
1601 event_done(&conn);
1602
1603 /* Check state */
1604 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1605 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1606
1607 /* Prepare */
1608 event_prepare(&conn);
1609
1610 /* Tx Queue should have one LL Control PDU */
1611 lt_rx(LL_ENC_RSP, &conn, &tx, &exp_enc_rsp);
1612 lt_rx_q_is_empty(&conn);
1613
1614 /* Check state */
1615 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1616 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1617
1618 /* Done */
1619 event_done(&conn);
1620
1621 /* Check state */
1622 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1623 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1624
1625 /* Release Tx */
1626 ull_cp_release_tx(&conn, tx);
1627
1628 /* There should be a host notification */
1629 ut_rx_pdu(LL_ENC_REQ, &ntf, &enc_req);
1630 ut_rx_q_is_empty();
1631
1632 /* Release Ntf */
1633 release_ntf(ntf);
1634
1635 /* LTK request reply */
1636 ull_cp_ltk_req_neq_reply(&conn);
1637
1638 /* Check state */
1639 /* TODO(thoh): THIS IS WRONG! */
1640 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1641 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1642
1643 /* Prepare */
1644 event_prepare(&conn);
1645
1646 /* Tx Queue should have one LL Control PDU */
1647 lt_rx(LL_REJECT_EXT_IND, &conn, &tx, &reject_ext_ind);
1648 lt_rx_q_is_empty(&conn);
1649
1650 /* Check state */
1651 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1652 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1653
1654 /* Done */
1655 event_done(&conn);
1656
1657 /* Check state */
1658 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1659 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1660
1661 /* Release Tx */
1662 ull_cp_release_tx(&conn, tx);
1663
1664 /* There should not be a host notification */
1665 ut_rx_q_is_empty();
1666
1667 /* All contexts should be released until now. This is a side-effect of a call to
1668 * ull_cp_tx_ntf that internall calls rr_check_done and lr_check_done.
1669 */
1670 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1671 "Free CTX buffers %d", llcp_ctx_buffers_free());
1672 }
1673
1674 /* +-----+ +-------+ +-----+
1675 * | UT | | LL_A | | LT |
1676 * +-----+ +-------+ +-----+
1677 * | | |
1678 * | | LL_ENC_REQ |
1679 * | |<--------------------|
1680 * | -----------------\ | |
1681 * | | Empty Tx queue |-| |
1682 * | |----------------| | |
1683 * | | |
1684 * | | LL_ENC_RSP |
1685 * | |-------------------->|
1686 * | | |
1687 * | | LL_VERSION_IND |
1688 * | |<--------------------|
1689 * | | |
1690 */
ZTEST(encryption_start,test_encryption_start_periph_rem_mic)1691 ZTEST(encryption_start, test_encryption_start_periph_rem_mic)
1692 {
1693 struct node_tx *tx;
1694 struct node_rx_pdu *ntf;
1695
1696 /* Prepare LL_ENC_REQ */
1697 struct pdu_data_llctrl_enc_req enc_req = {
1698 .rand = { RAND },
1699 .ediv = { EDIV },
1700 .skdm = { SKDM },
1701 .ivm = { IVM },
1702 };
1703
1704 struct pdu_data_llctrl_enc_rsp exp_enc_rsp = {
1705 .skds = { SKDS },
1706 .ivs = { IVS },
1707 };
1708
1709 struct pdu_data_llctrl_version_ind remote_version_ind = {
1710 .version_number = 0x55,
1711 .company_id = 0xABCD,
1712 .sub_version_number = 0x1234,
1713 };
1714
1715 /* Prepare mocked call to lll_csrand_get */
1716 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1717 ztest_return_data(lll_csrand_get, buf, exp_enc_rsp.skds);
1718 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1719
1720 /* Role */
1721 test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
1722
1723 /* Connect */
1724 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1725
1726 /* Check state */
1727 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1728 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1729
1730 /* Prepare */
1731 event_prepare(&conn);
1732
1733 /* Rx */
1734 lt_tx(LL_ENC_REQ, &conn, &enc_req);
1735
1736 /* Check state */
1737 CHECK_RX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Rx unenc. */
1738 CHECK_TX_PE_STATE(conn, RESUMED, UNENCRYPTED); /* Tx unenc. */
1739
1740 /* Done */
1741 event_done(&conn);
1742
1743 /* Check state */
1744 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1745 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1746
1747 /* Prepare */
1748 event_prepare(&conn);
1749
1750 /* Tx Queue should have one LL Control PDU */
1751 lt_rx(LL_ENC_RSP, &conn, &tx, &exp_enc_rsp);
1752 lt_rx_q_is_empty(&conn);
1753
1754 /* Check state */
1755 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1756 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1757
1758 /* Done */
1759 event_done(&conn);
1760
1761 /* Check state */
1762 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1763 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1764
1765 /* Release Tx */
1766 ull_cp_release_tx(&conn, tx);
1767
1768 /* There should be a host notification */
1769 ut_rx_pdu(LL_ENC_REQ, &ntf, &enc_req);
1770 ut_rx_q_is_empty();
1771
1772 /* Release Ntf */
1773 release_ntf(ntf);
1774
1775 /* Prepare */
1776 event_prepare(&conn);
1777
1778 /* Rx */
1779 lt_tx(LL_VERSION_IND, &conn, &remote_version_ind);
1780
1781 /* Done */
1782 event_done(&conn);
1783
1784 /* Check state */
1785 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1786 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1787
1788 /* There should not be a host notification */
1789 ut_rx_q_is_empty();
1790
1791 /**/
1792 zassert_equal(conn.llcp_terminate.reason_final, BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL,
1793 "Expected termination due to MIC failure");
1794
1795 /*
1796 * For a 40s procedure response timeout with a connection interval of
1797 * 7.5ms, a total of 5333.33 connection events are needed, verify that
1798 * the state doesn't change for that many invocations.
1799 */
1800 for (int n = 5334; n > 0; n--) {
1801 /* Prepare */
1802 event_prepare(&conn);
1803
1804 /* Tx Queue should NOT have a LL Control PDU */
1805 lt_rx_q_is_empty(&conn);
1806
1807 /* Done */
1808 event_done(&conn);
1809
1810 /* Check state */
1811 CHECK_RX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Rx paused & unenc. */
1812 CHECK_TX_PE_STATE(conn, PAUSED, UNENCRYPTED); /* Tx paused & unenc. */
1813
1814 /* There should NOT be a host notification */
1815 ut_rx_q_is_empty();
1816 }
1817
1818 /* Note that for this test the context is not released */
1819 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt() - 1,
1820 "Free CTX buffers %d", llcp_ctx_buffers_free());
1821 }
1822
1823
ZTEST(encryption_pause,test_encryption_pause_central_loc)1824 ZTEST(encryption_pause, test_encryption_pause_central_loc)
1825 {
1826 uint8_t err;
1827 struct node_tx *tx;
1828 struct node_rx_pdu *ntf;
1829
1830 const uint8_t rand[] = { RAND };
1831 const uint8_t ediv[] = { EDIV };
1832 const uint8_t ltk[] = { LTK };
1833 const uint8_t skd[] = { SKDM, SKDS };
1834 const uint8_t sk_be[] = { SK_BE };
1835 const uint8_t iv[] = { IVM, IVS };
1836
1837 /* Prepare expected LL_ENC_REQ */
1838 struct pdu_data_llctrl_enc_req exp_enc_req = {
1839 .rand = { RAND },
1840 .ediv = { EDIV },
1841 .skdm = { SKDM },
1842 .ivm = { IVM },
1843 };
1844
1845 /* Prepare LL_ENC_RSP */
1846 struct pdu_data_llctrl_enc_rsp enc_rsp = { .skds = { SKDS }, .ivs = { IVS } };
1847
1848 /* Prepare mocked call to lll_csrand_get */
1849 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
1850 ztest_return_data(lll_csrand_get, buf, exp_enc_req.skdm);
1851 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_req.skdm) + sizeof(exp_enc_req.ivm));
1852
1853 /* Prepare mocked call to ecb_encrypt */
1854 ztest_expect_data(ecb_encrypt, key_le, ltk);
1855 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
1856 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
1857
1858 /* Role */
1859 test_set_role(&conn, BT_HCI_ROLE_CENTRAL);
1860
1861 /* Connect */
1862 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
1863
1864 /* Fake that encryption is already active */
1865 conn.lll.enc_rx = 1U;
1866 conn.lll.enc_tx = 1U;
1867
1868 /**** ENCRYPTED ****/
1869
1870 /* Initiate an Encryption Pause Procedure */
1871 err = ull_cp_encryption_pause(&conn, rand, ediv, ltk);
1872 zassert_equal(err, BT_HCI_ERR_SUCCESS);
1873
1874 /* Prepare */
1875 event_prepare(&conn);
1876
1877 /* Tx Queue should have one LL Control PDU */
1878 lt_rx(LL_PAUSE_ENC_REQ, &conn, &tx, NULL);
1879 lt_rx_q_is_empty(&conn);
1880
1881 /* Release Tx */
1882 ull_cp_release_tx(&conn, tx);
1883
1884 /* Rx */
1885 lt_tx(LL_PAUSE_ENC_RSP, &conn, NULL);
1886
1887 /* Done */
1888 event_done(&conn);
1889
1890 /* Prepare */
1891 event_prepare(&conn);
1892
1893 /* Tx Queue should have one LL Control PDU */
1894 lt_rx(LL_PAUSE_ENC_RSP, &conn, &tx, NULL);
1895
1896 /* Release Tx */
1897 ull_cp_release_tx(&conn, tx);
1898
1899 /* Tx Encryption should be disabled */
1900 zassert_equal(conn.lll.enc_tx, 0U);
1901
1902 /* Rx Decryption should be disabled */
1903 zassert_equal(conn.lll.enc_rx, 0U);
1904
1905 /**** UNENCRYPTED ****/
1906
1907 /* Tx Queue should have one LL Control PDU */
1908 lt_rx(LL_ENC_REQ, &conn, &tx, &exp_enc_req);
1909 lt_rx_q_is_empty(&conn);
1910
1911 /* Release Tx */
1912 ull_cp_release_tx(&conn, tx);
1913
1914 /* Rx */
1915 lt_tx(LL_ENC_RSP, &conn, &enc_rsp);
1916
1917 /* Rx */
1918 lt_tx(LL_START_ENC_REQ, &conn, NULL);
1919
1920 /* Done */
1921 event_done(&conn);
1922
1923 /* Prepare */
1924 event_prepare(&conn);
1925
1926 /* Tx Queue should have one LL Control PDU */
1927 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
1928 lt_rx_q_is_empty(&conn);
1929
1930 /* CCM Tx/Rx SK should match SK */
1931 /* CCM Tx/Rx IV should match the IV */
1932 /* CCM Tx/Rx Counter should be zero */
1933 /* CCM Rx Direction should be S->M */
1934 /* CCM Tx Direction should be M->S */
1935 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
1936 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
1937
1938 /* Tx Encryption should be enabled */
1939 zassert_equal(conn.lll.enc_tx, 1U);
1940
1941 /* Rx Decryption should be enabled */
1942 zassert_equal(conn.lll.enc_rx, 1U);
1943
1944 /* Release Tx */
1945 ull_cp_release_tx(&conn, tx);
1946
1947 /* Rx */
1948 lt_tx(LL_START_ENC_RSP, &conn, NULL);
1949
1950 /* Done */
1951 event_done(&conn);
1952
1953 /* There should be one host notification */
1954 ut_rx_node(NODE_ENC_REFRESH, &ntf, NULL);
1955 ut_rx_q_is_empty();
1956
1957 /* Release Ntf */
1958 release_ntf(ntf);
1959
1960 /* Tx Encryption should be enabled */
1961 zassert_equal(conn.lll.enc_tx, 1U);
1962
1963 /* Rx Decryption should be enabled */
1964 zassert_equal(conn.lll.enc_rx, 1U);
1965
1966 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
1967 "Free CTX buffers %d", llcp_ctx_buffers_free());
1968 }
1969
ZTEST(encryption_pause,test_encryption_pause_periph_rem)1970 ZTEST(encryption_pause, test_encryption_pause_periph_rem)
1971 {
1972 struct node_tx *tx;
1973 struct node_rx_pdu *ntf;
1974
1975 const uint8_t ltk[] = { LTK };
1976 const uint8_t skd[] = { SKDM, SKDS };
1977 const uint8_t sk_be[] = { SK_BE };
1978 const uint8_t iv[] = { IVM, IVS };
1979
1980 /* Prepare LL_ENC_REQ */
1981 struct pdu_data_llctrl_enc_req enc_req = {
1982 .rand = { RAND },
1983 .ediv = { EDIV },
1984 .skdm = { SKDM },
1985 .ivm = { IVM },
1986 };
1987
1988 struct pdu_data_llctrl_enc_rsp exp_enc_rsp = {
1989 .skds = { SKDS },
1990 .ivs = { IVS },
1991 };
1992
1993 /* Prepare mocked call to lll_csrand_get */
1994 ztest_returns_value(lll_csrand_get, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1995 ztest_return_data(lll_csrand_get, buf, exp_enc_rsp.skds);
1996 ztest_expect_value(lll_csrand_get, len, sizeof(exp_enc_rsp.skds) + sizeof(exp_enc_rsp.ivs));
1997
1998 /* Prepare mocked call to ecb_encrypt */
1999 ztest_expect_data(ecb_encrypt, key_le, ltk);
2000 ztest_expect_data(ecb_encrypt, clear_text_le, skd);
2001 ztest_return_data(ecb_encrypt, cipher_text_be, sk_be);
2002
2003 /* Role */
2004 test_set_role(&conn, BT_HCI_ROLE_PERIPHERAL);
2005
2006 /* Connect */
2007 ull_cp_state_set(&conn, ULL_CP_CONNECTED);
2008
2009 /* Fake that encryption is already active */
2010 conn.lll.enc_rx = 1U;
2011 conn.lll.enc_tx = 1U;
2012
2013 /**** ENCRYPTED ****/
2014
2015 /* Prepare */
2016 event_prepare(&conn);
2017
2018 /* Rx */
2019 lt_tx(LL_PAUSE_ENC_REQ, &conn, NULL);
2020
2021 /* Done */
2022 event_done(&conn);
2023
2024 /* Prepare */
2025 event_prepare(&conn);
2026
2027 /* Tx Queue should have one LL Control PDU */
2028 lt_rx(LL_PAUSE_ENC_RSP, &conn, &tx, NULL);
2029 lt_rx_q_is_empty(&conn);
2030
2031 /* Rx Decryption should be disabled */
2032 zassert_equal(conn.lll.enc_rx, 0U);
2033
2034 /* Rx */
2035 lt_tx(LL_PAUSE_ENC_RSP, &conn, NULL);
2036
2037 /* Done */
2038 event_done(&conn);
2039
2040 /* Release Tx */
2041 ull_cp_release_tx(&conn, tx);
2042
2043 /* Tx Encryption should be disabled */
2044 zassert_equal(conn.lll.enc_tx, 0U);
2045
2046 /**** UNENCRYPTED ****/
2047
2048 /* Prepare */
2049 event_prepare(&conn);
2050
2051 /* Rx */
2052 lt_tx(LL_ENC_REQ, &conn, &enc_req);
2053
2054 /* Done */
2055 event_done(&conn);
2056
2057 /* Prepare */
2058 event_prepare(&conn);
2059
2060 /* Tx Queue should have one LL Control PDU */
2061 lt_rx(LL_ENC_RSP, &conn, &tx, &exp_enc_rsp);
2062 lt_rx_q_is_empty(&conn);
2063
2064 /* Done */
2065 event_done(&conn);
2066
2067 /* Release Tx */
2068 ull_cp_release_tx(&conn, tx);
2069
2070 /* There should be a host notification */
2071 ut_rx_pdu(LL_ENC_REQ, &ntf, &enc_req);
2072 ut_rx_q_is_empty();
2073
2074 /* Release Ntf */
2075 release_ntf(ntf);
2076
2077 /* LTK request reply */
2078 ull_cp_ltk_req_reply(&conn, ltk);
2079
2080 /* Prepare */
2081 event_prepare(&conn);
2082
2083 /* Tx Queue should have one LL Control PDU */
2084 lt_rx(LL_START_ENC_REQ, &conn, &tx, NULL);
2085 lt_rx_q_is_empty(&conn);
2086
2087 /* Done */
2088 event_done(&conn);
2089
2090 /* Release Tx */
2091 ull_cp_release_tx(&conn, tx);
2092
2093 /* CCM Rx SK should match SK */
2094 /* CCM Rx IV should match the IV */
2095 /* CCM Rx Counter should be zero */
2096 /* CCM Rx Direction should be M->S */
2097 CHECK_RX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_M_TO_S);
2098
2099 /* Rx Decryption should be enabled */
2100 zassert_equal(conn.lll.enc_rx, 1U);
2101
2102 /* Prepare */
2103 event_prepare(&conn);
2104
2105 /* Rx */
2106 lt_tx(LL_START_ENC_RSP, &conn, NULL);
2107
2108 /* Done */
2109 event_done(&conn);
2110
2111 /* There should be a host notification */
2112 ut_rx_node(NODE_ENC_REFRESH, &ntf, NULL);
2113 ut_rx_q_is_empty();
2114
2115 /* Prepare */
2116 event_prepare(&conn);
2117
2118 /* Tx Queue should have one LL Control PDU */
2119 lt_rx(LL_START_ENC_RSP, &conn, &tx, NULL);
2120 lt_rx_q_is_empty(&conn);
2121
2122 /* Done */
2123 event_done(&conn);
2124
2125 /* Release Tx */
2126 ull_cp_release_tx(&conn, tx);
2127
2128 /* CCM Tx SK should match SK */
2129 /* CCM Tx IV should match the IV */
2130 /* CCM Tx Counter should be zero */
2131 /* CCM Tx Direction should be S->M */
2132 CHECK_TX_CCM_STATE(conn, sk_be, iv, 0U, CCM_DIR_S_TO_M);
2133
2134 /* Tx Encryption should be enabled */
2135 zassert_equal(conn.lll.enc_tx, 1U);
2136
2137 zassert_equal(llcp_ctx_buffers_free(), test_ctx_buffers_cnt(),
2138 "Free CTX buffers %d", llcp_ctx_buffers_free());
2139 }
2140
2141 ZTEST_SUITE(encryption_start, NULL, NULL, enc_setup, NULL, NULL);
2142 ZTEST_SUITE(encryption_pause, NULL, NULL, enc_setup, NULL, NULL);
2143