|
1 | 1 | /* |
2 | | - * Copyright (c) 2016 Nordic Semiconductor ASA |
| 2 | + * Copyright (c) 2016-2024 Nordic Semiconductor ASA |
3 | 3 | * Copyright (c) 2016 Vinayak Kariappa Chettimada |
4 | 4 | * |
5 | 5 | * SPDX-License-Identifier: Apache-2.0 |
6 | 6 | */ |
7 | 7 |
|
| 8 | +#include <stdint.h> |
8 | 9 | #include <string.h> |
9 | 10 |
|
| 11 | +#include <zephyr/sys/byteorder.h> |
| 12 | + |
10 | 13 | #include <hal/nrf_ecb.h> |
11 | 14 |
|
12 | 15 | #include "util/mem.h" |
|
16 | 19 |
|
17 | 20 | #include "hal/debug.h" |
18 | 21 |
|
| 22 | +#if defined(NRF54L_SERIES) |
| 23 | +#define NRF_ECB NRF_ECB00 |
| 24 | +#define ECB_IRQn ECB00_IRQn |
| 25 | +#define ECB_INTENSET_ERRORECB_Msk ECB_INTENSET_ERROR_Msk |
| 26 | +#define ECB_INTENSET_ENDECB_Msk ECB_INTENSET_END_Msk |
| 27 | +#define TASKS_STARTECB TASKS_START |
| 28 | +#define TASKS_STOPECB TASKS_STOP |
| 29 | +#define EVENTS_ENDECB EVENTS_END |
| 30 | +#define EVENTS_ERRORECB EVENTS_ERROR |
| 31 | +#define NRF_ECB_TASK_STARTECB NRF_ECB_TASK_START |
| 32 | +#define NRF_ECB_TASK_STOPECB NRF_ECB_TASK_STOP |
| 33 | +#define ECBDATAPTR IN.PTR |
| 34 | + |
| 35 | +struct ecb_job_ptr { |
| 36 | + void *ptr; |
| 37 | + struct { |
| 38 | + uint32_t length:24; |
| 39 | + uint32_t attribute:8; |
| 40 | + } __packed; |
| 41 | +} __packed; |
| 42 | + |
| 43 | +/* Product Specification recommends a value of 11, but prior work had used 7 */ |
| 44 | +#define ECB_JOB_PTR_ATTRIBUTE 7U |
| 45 | +#endif /* NRF54L_SERIES */ |
| 46 | + |
19 | 47 | struct ecb_param { |
20 | 48 | uint8_t key[16]; |
21 | 49 | uint8_t clear_text[16]; |
22 | 50 | uint8_t cipher_text[16]; |
| 51 | + |
| 52 | +#if defined(NRF54L_SERIES) |
| 53 | + struct ecb_job_ptr in[2]; |
| 54 | + struct ecb_job_ptr out[2]; |
| 55 | +#endif /* NRF54L_SERIES */ |
23 | 56 | } __packed; |
24 | 57 |
|
25 | 58 | static void do_ecb(struct ecb_param *ecb) |
26 | 59 | { |
27 | 60 | do { |
28 | 61 | nrf_ecb_task_trigger(NRF_ECB, NRF_ECB_TASK_STOPECB); |
| 62 | + |
| 63 | +#if defined(NRF54L_SERIES) |
| 64 | + NRF_ECB->KEY.VALUE[3] = sys_get_be32(&ecb->key[0]); |
| 65 | + NRF_ECB->KEY.VALUE[2] = sys_get_be32(&ecb->key[4]); |
| 66 | + NRF_ECB->KEY.VALUE[1] = sys_get_be32(&ecb->key[8]); |
| 67 | + NRF_ECB->KEY.VALUE[0] = sys_get_be32(&ecb->key[12]); |
| 68 | + |
| 69 | + ecb->in[0].ptr = ecb->clear_text; |
| 70 | + ecb->in[0].length = sizeof(ecb->clear_text); |
| 71 | + ecb->in[0].attribute = ECB_JOB_PTR_ATTRIBUTE; |
| 72 | + ecb->in[1].ptr = NULL; |
| 73 | + ecb->in[1].length = 0U; |
| 74 | + ecb->in[1].attribute = 0U; |
| 75 | + |
| 76 | + ecb->out[0].ptr = ecb->cipher_text; |
| 77 | + ecb->out[0].length = sizeof(ecb->cipher_text); |
| 78 | + ecb->out[0].attribute = ECB_JOB_PTR_ATTRIBUTE; |
| 79 | + ecb->out[1].ptr = NULL; |
| 80 | + ecb->out[1].length = 0U; |
| 81 | + ecb->out[1].attribute = 0U; |
| 82 | + |
| 83 | + NRF_ECB->IN.PTR = (uint32_t)ecb->in; |
| 84 | + NRF_ECB->OUT.PTR = (uint32_t)ecb->out; |
| 85 | +#else /* !NRF54L_SERIES */ |
29 | 86 | NRF_ECB->ECBDATAPTR = (uint32_t)ecb; |
| 87 | +#endif /* !NRF54L_SERIES */ |
| 88 | + |
30 | 89 | NRF_ECB->EVENTS_ENDECB = 0; |
31 | 90 | NRF_ECB->EVENTS_ERRORECB = 0; |
32 | 91 | nrf_ecb_task_trigger(NRF_ECB, NRF_ECB_TASK_STARTECB); |
@@ -96,7 +155,34 @@ uint32_t ecb_encrypt_nonblocking(struct ecb *ecb) |
96 | 155 | } |
97 | 156 |
|
98 | 157 | /* setup the encryption h/w */ |
| 158 | +#if defined(NRF54L_SERIES) |
| 159 | + NRF_ECB->KEY.VALUE[3] = sys_get_be32(&ecb->in_key_be[0]); |
| 160 | + NRF_ECB->KEY.VALUE[2] = sys_get_be32(&ecb->in_key_be[4]); |
| 161 | + NRF_ECB->KEY.VALUE[1] = sys_get_be32(&ecb->in_key_be[8]); |
| 162 | + NRF_ECB->KEY.VALUE[0] = sys_get_be32(&ecb->in_key_be[12]); |
| 163 | + |
| 164 | + struct ecb_job_ptr *in = (void *)((uint8_t *)ecb + sizeof(*ecb)); |
| 165 | + struct ecb_job_ptr *out = (void *)((uint8_t *)in + 16U); |
| 166 | + |
| 167 | + in[0].ptr = ecb->in_clear_text_be; |
| 168 | + in[0].length = sizeof(ecb->in_clear_text_be); |
| 169 | + in[0].attribute = ECB_JOB_PTR_ATTRIBUTE; |
| 170 | + in[1].ptr = NULL; |
| 171 | + in[1].length = 0U; |
| 172 | + in[1].attribute = 0U; |
| 173 | + |
| 174 | + out[0].ptr = ecb->out_cipher_text_be; |
| 175 | + out[0].length = sizeof(ecb->out_cipher_text_be); |
| 176 | + out[0].attribute = ECB_JOB_PTR_ATTRIBUTE; |
| 177 | + out[1].ptr = NULL; |
| 178 | + out[1].length = 0U; |
| 179 | + out[1].attribute = 0U; |
| 180 | + |
| 181 | + NRF_ECB->IN.PTR = (uint32_t)in; |
| 182 | + NRF_ECB->OUT.PTR = (uint32_t)out; |
| 183 | +#else /* !NRF54L_SERIES */ |
99 | 184 | NRF_ECB->ECBDATAPTR = (uint32_t)ecb; |
| 185 | +#endif /* !NRF54L_SERIES */ |
100 | 186 | NRF_ECB->EVENTS_ENDECB = 0; |
101 | 187 | NRF_ECB->EVENTS_ERRORECB = 0; |
102 | 188 | nrf_ecb_int_enable(NRF_ECB, ECB_INTENSET_ERRORECB_Msk |
|
0 commit comments