Skip to content

Commit de13d36

Browse files
cvinayakkartben
authored andcommitted
Bluetooth: Controller: Refactor isr_ecb and unit testing
Refactor the NRF_ECB isr_ecb function to reduce redundant common code. Updated the unit tests to use Bluetooth Specification defined test vectors. Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent 1dfebd6 commit de13d36

File tree

3 files changed

+79
-53
lines changed
  • subsys/bluetooth/controller

3 files changed

+79
-53
lines changed
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016 Nordic Semiconductor ASA
2+
* Copyright (c) 2016-2024 Nordic Semiconductor ASA
33
* Copyright (c) 2016 Vinayak Kariappa Chettimada
44
*
55
* SPDX-License-Identifier: Apache-2.0
@@ -8,22 +8,24 @@
88
typedef void (*ecb_fp) (uint32_t status, uint8_t *cipher_be, void *context);
99

1010
struct ecb {
11-
uint8_t in_key_be[16];
12-
uint8_t in_clear_text_be[16];
13-
uint8_t out_cipher_text_be[16];
11+
uint8_t in_key_be[16];
12+
uint8_t in_clear_text_be[16];
13+
uint8_t out_cipher_text_be[16];
14+
1415
/* if not null reverse copy into in_key_be */
15-
uint8_t *in_key_le;
16+
uint8_t *in_key_le;
1617
/* if not null reverse copy into in_clear_text_be */
17-
uint8_t *in_clear_text_le;
18-
ecb_fp fp_ecb;
19-
void *context;
18+
uint8_t *in_clear_text_le;
19+
20+
ecb_fp fp_ecb;
21+
void *context;
2022
};
2123

2224
void ecb_encrypt_be(uint8_t const *const key_be, uint8_t const *const clear_text_be,
2325
uint8_t * const cipher_text_be);
2426
void ecb_encrypt(uint8_t const *const key_le, uint8_t const *const clear_text_le,
2527
uint8_t * const cipher_text_le, uint8_t * const cipher_text_be);
26-
uint32_t ecb_encrypt_nonblocking(struct ecb *ecb);
27-
void isr_ecb(void *param);
2828

29-
uint32_t ecb_ut(void);
29+
void ecb_encrypt_nonblocking(struct ecb *e);
30+
31+
int ecb_ut(void);

subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/ecb.c

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void ecb_encrypt(uint8_t const *const key_le, uint8_t const *const clear_text_le
141141
}
142142
}
143143

144-
uint32_t ecb_encrypt_nonblocking(struct ecb *ecb)
144+
void ecb_encrypt_nonblocking(struct ecb *ecb)
145145
{
146146
/* prepare to be used in a BE AES h/w */
147147
if (ecb->in_key_le) {
@@ -194,43 +194,40 @@ uint32_t ecb_encrypt_nonblocking(struct ecb *ecb)
194194

195195
/* start the encryption h/w */
196196
nrf_ecb_task_trigger(NRF_ECB, NRF_ECB_TASK_STARTECB);
197-
198-
return 0;
199197
}
200198

201-
static void ecb_cleanup(void)
199+
static void isr_ecb(const void *arg)
202200
{
203-
/* stop h/w */
204-
NRF_ECB->TASKS_STOPECB = 1;
201+
#if defined(NRF54L_SERIES)
202+
struct ecb *ecb = (void *)((uint8_t *)NRF_ECB->ECBDATAPTR -
203+
sizeof(struct ecb));
204+
#else /* !NRF54L_SERIES */
205+
struct ecb *ecb = (void *)NRF_ECB->ECBDATAPTR;
206+
#endif /* !NRF54L_SERIES */
207+
208+
ARG_UNUSED(arg);
209+
210+
/* Stop ECB h/w */
205211
nrf_ecb_task_trigger(NRF_ECB, NRF_ECB_TASK_STOPECB);
206212

207-
/* cleanup interrupt */
213+
/* We are done or encountered error, disable interrupt */
208214
irq_disable(ECB_IRQn);
209-
}
210-
211-
void isr_ecb(void *param)
212-
{
213-
ARG_UNUSED(param);
214215

215216
if (NRF_ECB->EVENTS_ERRORECB) {
216-
struct ecb *ecb = (struct ecb *)NRF_ECB->ECBDATAPTR;
217+
NRF_ECB->EVENTS_ERRORECB = 0U;
217218

218-
ecb_cleanup();
219-
220-
ecb->fp_ecb(1, NULL, ecb->context);
219+
ecb->fp_ecb(1U, NULL, ecb->context);
221220
}
222221

223222
else if (NRF_ECB->EVENTS_ENDECB) {
224-
struct ecb *ecb = (struct ecb *)NRF_ECB->ECBDATAPTR;
225-
226-
ecb_cleanup();
223+
NRF_ECB->EVENTS_ENDECB = 0U;
227224

228-
ecb->fp_ecb(0, &ecb->out_cipher_text_be[0],
225+
ecb->fp_ecb(0U, &ecb->out_cipher_text_be[0],
229226
ecb->context);
230227
}
231228

232229
else {
233-
LL_ASSERT(0);
230+
LL_ASSERT(false);
234231
}
235232
}
236233

@@ -253,35 +250,62 @@ static void ecb_cb(uint32_t status, uint8_t *cipher_be, void *context)
253250
}
254251
}
255252

256-
uint32_t ecb_ut(void)
253+
int ecb_ut(void)
257254
{
258-
uint8_t key[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
259-
0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
260-
uint8_t clear_text[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
261-
0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
262-
0x55 };
263-
uint8_t cipher_text[16];
264-
uint32_t status = 0U;
265-
struct ecb ecb;
266-
struct ecb_ut_context context;
255+
uint8_t key[] = {
256+
0xbf, 0x01, 0xfb, 0x9d, 0x4e, 0xf3, 0xbc, 0x36,
257+
0xd8, 0x74, 0xf5, 0x39, 0x41, 0x38, 0x68, 0x4c
258+
};
259+
uint8_t clear_text[] = {
260+
0x13, 0x02, 0xf1, 0xe0, 0xdf, 0xce, 0xbd, 0xac,
261+
0x79, 0x68, 0x57, 0x46, 0x35, 0x24, 0x13, 0x02
262+
};
263+
uint8_t cipher_text_expected[] = {
264+
0x66, 0xc6, 0xc2, 0x27, 0x8e, 0x3b, 0x8e, 0x05,
265+
0x3e, 0x7e, 0xa3, 0x26, 0x52, 0x1b, 0xad, 0x99
266+
};
267+
uint8_t cipher_text_actual[16];
268+
int status;
269+
270+
(void)memset(cipher_text_actual, 0, sizeof(cipher_text_actual));
271+
ecb_encrypt(key, clear_text, cipher_text_actual, NULL);
272+
273+
status = memcmp(cipher_text_actual, cipher_text_expected,
274+
sizeof(cipher_text_actual));
275+
if (status) {
276+
return status;
277+
}
267278

268-
ecb_encrypt(key, clear_text, cipher_text, NULL);
279+
#if defined(CONFIG_BT_CTLR_DYNAMIC_INTERRUPTS)
280+
irq_connect_dynamic(ECB_IRQn, CONFIG_BT_CTLR_ULL_LOW_PRIO, isr_ecb, NULL, 0);
281+
#else /* !CONFIG_BT_CTLR_DYNAMIC_INTERRUPTS */
282+
IRQ_CONNECT(ECB_IRQn, CONFIG_BT_CTLR_ULL_LOW_PRIO, isr_ecb, NULL, 0);
283+
#endif /* !CONFIG_BT_CTLR_DYNAMIC_INTERRUPTS */
269284

270-
context.done = 0U;
271-
ecb.in_key_le = key;
272-
ecb.in_clear_text_le = clear_text;
273-
ecb.fp_ecb = ecb_cb;
274-
ecb.context = &context;
275-
status = ecb_encrypt_nonblocking(&ecb);
285+
uint8_t ecb_mem[sizeof(struct ecb) + 32U];
286+
struct ecb *ecb = (void *)ecb_mem;
287+
struct ecb_ut_context context;
288+
289+
(void)memset(&context, 0, sizeof(context));
290+
ecb->in_key_le = key;
291+
ecb->in_clear_text_le = clear_text;
292+
ecb->fp_ecb = ecb_cb;
293+
ecb->context = &context;
294+
ecb_encrypt_nonblocking(ecb);
276295
do {
296+
#if defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
297+
k_busy_wait(10);
298+
#else
277299
cpu_sleep();
300+
#endif
278301
} while (!context.done);
279302

280303
if (context.status != 0U) {
281304
return context.status;
282305
}
283306

284-
status = memcmp(cipher_text, context.cipher_text, sizeof(cipher_text));
307+
status = memcmp(cipher_text_expected, context.cipher_text,
308+
sizeof(cipher_text_expected));
285309
if (status) {
286310
return status;
287311
}

subsys/bluetooth/controller/ll_sw/openisa/hal/RV32M1/ecb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ void ecb_encrypt(uint8_t const *const key_le, uint8_t const *const clear_text_le
104104
}
105105
}
106106

107-
uint32_t ecb_encrypt_nonblocking(struct ecb *ecb)
107+
void ecb_encrypt_nonblocking(struct ecb *e)
108108
{
109-
return 0;
109+
return;
110110
}
111111

112112
void isr_ecb(void *param)
@@ -123,7 +123,7 @@ void isr_ecb(void *param)
123123
&conn->llcp.encryption.skd[0], NULL,
124124
&lll->ccm_rx.key[0]);
125125
*/
126-
uint32_t ecb_ut(void)
126+
int ecb_ut(void)
127127
{
128128
/*
129129
* LTK = 0x4C68384139F574D836BCF34E9DFB01BF (MSO to LSO)

0 commit comments

Comments
 (0)