ref: 0ce98c7ac7ba66acaf504be9bb042796e12f2733
src/libs/mynewt-nimble/nimble/drivers/dialog_cmac/src/ble_hw.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #include <assert.h> #include <stdint.h> #include "mcu/mcu.h" #include "nimble/ble.h" #include "controller/ble_hw.h" #include "CMAC.h" #include "cmac_driver/cmac_shared.h" #include "mcu/mcu.h" #include "tinycrypt/aes.h" static struct tc_aes_key_sched_struct g_ctx; int ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias) { cmac_rand_set_isr_cb(cb); return 0; } int ble_hw_rng_start(void) { /* Chime the M33 in case we need random numbers generated */ cmac_rand_start(); CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_CMAC2SYS_IRQ_SET_Msk; return 0; } int ble_hw_rng_stop(void) { cmac_rand_stop(); return 0; } #define BLE_HW_RESOLV_LIST_SIZE (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE)) struct ble_hw_resolv_irk { uint32_t key[4]; }; struct ble_hw_resolv_list { uint8_t count; struct ble_hw_resolv_irk irk[BLE_HW_RESOLV_LIST_SIZE]; }; struct ble_hw_resolv_proc { uint32_t hash; uint8_t f_configured; uint8_t f_active; uint8_t f_match; uint8_t f_done; struct ble_hw_resolv_irk *irk; struct ble_hw_resolv_irk *irk_end; uint32_t crypto_prand_in[4]; uint32_t crypto_e_out[4]; }; static struct ble_hw_resolv_list g_ble_hw_resolv_list; static struct ble_hw_resolv_proc g_ble_hw_resolv_proc; int ble_hw_get_public_addr(ble_addr_t *addr) { return -1; } int ble_hw_get_static_addr(ble_addr_t *addr) { return -1; } void ble_hw_whitelist_clear(void) { } int ble_hw_whitelist_add(const uint8_t *addr, uint8_t addr_type) { return 0; } void ble_hw_whitelist_rmv(const uint8_t *addr, uint8_t addr_type) { } uint8_t ble_hw_whitelist_size(void) { return 0; } void ble_hw_whitelist_enable(void) { } void ble_hw_whitelist_disable(void) { } int ble_hw_whitelist_match(void) { return 0; } int ble_hw_encrypt_block(struct ble_encryption_block *ecb) { uint32_t in_addr; uint32_t out_addr; /* * The following code bears some explanation. This function is called by * the LL task to encrypt blocks and calculate session keys. Address * resolution also calls this function. Furthermore, during connections, * the M0 crypto accelerator is used but this function is not called when * using it. During the entire connection event, the M0 crypto block cannot * be used as the crypto state (some of it) needs to remain un-changed. * Note that this is also true when address resolution is enabled: the * HW crypto block is set up and cannot be modified. * * Rather than attempt to share the M0 crypto block between the various * controller features which require it, we decided to use software to * perform the encryption task for anything being done at the link-layer * (outside of an ISR). If this function is called inside an ISR, and it * is when resolving addresses, the crypto accelerator is not being used * by a connection event. Thus, we check to see if we are inside of an ISR. * If so, we use the M0 crypto block. If outside of an ISR, we use the M33 */ if (!os_arch_in_isr()) { tc_aes128_set_encrypt_key(&g_ctx, ecb->key); tc_aes_encrypt(ecb->cipher_text, ecb->plain_text, &g_ctx); return 0; } /* Need to retain state of in/out pointers */ in_addr = CMAC->CM_CRYPTO_IN_ADR2_REG; out_addr = CMAC->CM_CRYPTO_OUT_ADR_REG; while (CMAC->CM_CRYPTO_STAT_REG & CMAC_CM_CRYPTO_STAT_REG_CM_CRYPTO_BUSY_Msk); /* RECB, memory in/out, encryption */ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ECB_ENC_EN_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_IN_SEL_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_OUT_SEL_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ENC_DECN_Msk; CMAC->CM_CRYPTO_KEY_31_0_REG = get_le32(&ecb->key[0]); CMAC->CM_CRYPTO_KEY_63_32_REG = get_le32(&ecb->key[4]); CMAC->CM_CRYPTO_KEY_95_64_REG = get_le32(&ecb->key[8]); CMAC->CM_CRYPTO_KEY_127_96_REG = get_le32(&ecb->key[12]); CMAC->CM_CRYPTO_IN_ADR2_REG = (uint32_t)ecb->plain_text; CMAC->CM_CRYPTO_OUT_ADR_REG = (uint32_t)ecb->cipher_text; CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk; CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk; while (!(CMAC->CM_EXC_STAT_REG & CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk)); CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk; CMAC->CM_CRYPTO_IN_ADR2_REG = in_addr; CMAC->CM_CRYPTO_OUT_ADR_REG = out_addr; return 0; } void ble_hw_resolv_list_clear(void) { g_ble_hw_resolv_list.count = 0; } int ble_hw_resolv_list_add(uint8_t *irk) { struct ble_hw_resolv_irk *e; if (g_ble_hw_resolv_list.count == BLE_HW_RESOLV_LIST_SIZE) { return BLE_ERR_MEM_CAPACITY; } e = &g_ble_hw_resolv_list.irk[g_ble_hw_resolv_list.count]; /* Prepare key here so we do not need to do it during resolving */ e->key[0] = get_le32(&irk[0]); e->key[1] = get_le32(&irk[4]); e->key[2] = get_le32(&irk[8]); e->key[3] = get_le32(&irk[12]); g_ble_hw_resolv_list.count++; return BLE_ERR_SUCCESS; } void ble_hw_resolv_list_rmv(int index) { struct ble_hw_resolv_irk *e; if (index < g_ble_hw_resolv_list.count) { g_ble_hw_resolv_list.count--; e = &g_ble_hw_resolv_list.irk[index]; memmove(e, e + 1, (g_ble_hw_resolv_list.count - index) * sizeof(e->key)); } } uint8_t ble_hw_resolv_list_size(void) { return BLE_HW_RESOLV_LIST_SIZE; } int ble_hw_resolv_list_match(void) { return g_ble_hw_resolv_proc.f_match ? g_ble_hw_resolv_proc.irk - g_ble_hw_resolv_list.irk : -1; } static void ble_hw_resolv_proc_next(void) { void *src = &g_ble_hw_resolv_proc.irk->key; if (g_ble_hw_resolv_proc.irk == g_ble_hw_resolv_proc.irk_end) { g_ble_hw_resolv_proc.f_done = 1; g_ble_hw_resolv_proc.f_active = 0; } else { __asm__ volatile (".syntax unified \n" " ldm %[ptr]!, {r1, r2, r3, r4} \n" " ldr %[ptr], =%[reg] \n" " stm %[ptr]!, {r1, r2, r3, r4} \n" : [ptr] "+l" (src) : [reg] "i" (&CMAC->CM_CRYPTO_KEY_31_0_REG) : "r1", "r2", "r3", "r4", "memory"); CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk; } } void ble_hw_resolv_proc_enable(void) { assert(!g_ble_hw_resolv_proc.f_active); CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_ABORT_Msk; CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ECB_ENC_EN_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_IN_SEL_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_OUT_SEL_Msk | CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ENC_DECN_Msk; CMAC->CM_CRYPTO_IN_ADR2_REG = (uint32_t)g_ble_hw_resolv_proc.crypto_prand_in; CMAC->CM_CRYPTO_OUT_ADR_REG = (uint32_t)g_ble_hw_resolv_proc.crypto_e_out; g_ble_hw_resolv_proc.irk = g_ble_hw_resolv_list.irk; g_ble_hw_resolv_proc.irk_end = g_ble_hw_resolv_list.irk + g_ble_hw_resolv_list.count; g_ble_hw_resolv_proc.f_configured = 1; g_ble_hw_resolv_proc.f_active = 0; /* * It would be better to enable IRQ in ble_hw_resolv_proc_start, but this * would introduce a bit of latency when starting resolving procedure and * we need to save every us possible there in order to be able to resolve * RPA on time. */ NVIC_ClearPendingIRQ(CRYPTO_IRQn); NVIC_EnableIRQ(CRYPTO_IRQn); } void ble_hw_resolv_proc_disable(void) { g_ble_hw_resolv_proc.f_configured = 0; g_ble_hw_resolv_proc.f_active = 0; g_ble_hw_resolv_proc.f_match = 0; g_ble_hw_resolv_proc.f_done = 1; NVIC_DisableIRQ(CRYPTO_IRQn); } void ble_hw_resolv_proc_start(const uint8_t *addr) { assert(g_ble_hw_resolv_proc.f_configured); /* crypto_prand_in is already zeroed so prand is properly padded */ g_ble_hw_resolv_proc.crypto_prand_in[3] = get_be24(&addr[3]) << 8; g_ble_hw_resolv_proc.hash = get_be24(&addr[0]); g_ble_hw_resolv_proc.f_match = 0; g_ble_hw_resolv_proc.f_done = 0; g_ble_hw_resolv_proc.f_active = 1; ble_hw_resolv_proc_next(); } void CRYPTO_IRQHandler(void) { uint32_t hash; CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk; hash = g_ble_hw_resolv_proc.crypto_e_out[3] >> 8; if (g_ble_hw_resolv_proc.hash == hash) { g_ble_hw_resolv_proc.f_active = 0; g_ble_hw_resolv_proc.f_match = 1; g_ble_hw_resolv_proc.f_done = 1; } else { g_ble_hw_resolv_proc.irk++; ble_hw_resolv_proc_next(); } } |