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