1 /* 2 * Copyright (C) 2017 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #define BTSTACK_FILE__ "btstack_crypto.c" 33 34 /* 35 * btstack_crypto.h 36 * 37 * Central place for all crypto-related functions with completion callbacks to allow 38 * using of MCU crypto peripherals or the Bluetooth controller 39 */ 40 41 #include "btstack_crypto.h" 42 43 #include "btstack_debug.h" 44 #include "btstack_event.h" 45 #include "btstack_linked_list.h" 46 #include "btstack_util.h" 47 #include "hci.h" 48 49 // 50 // AES128 Configuration 51 // 52 53 // By default, AES128 is computed by Bluetooth Controller using HCI Command/Event asynchronously 54 // as fallback/alternative, a software implementation can be used 55 // configure ECC implementations 56 #if defined(HAVE_AES128) && defined(ENABLE_SOFTWARE_AES128) 57 #error "If you have custom AES128 implementation (HAVE_AES128), please disable software AES128 (ENABLE_SOFTWARE_AES128) in bstack_config.h" 58 #endif 59 60 #ifdef ENABLE_SOFTWARE_AES128 61 #define HAVE_AES128 62 #include "rijndael.h" 63 #endif 64 65 #ifdef HAVE_AES128 66 #define USE_BTSTACK_AES128 67 #endif 68 69 // 70 // ECC Configuration 71 // 72 73 // backwards-compatitility ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS -> ENABLE_MICRO_ECC_P256 74 #if defined(ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS) && !defined(ENABLE_MICRO_ECC_P256) 75 #define ENABLE_MICRO_ECC_P256 76 #endif 77 78 // configure ECC implementations 79 #if defined(ENABLE_MICRO_ECC_P256) && defined(HAVE_MBEDTLS_ECC_P256) 80 #error "If you have mbedTLS (HAVE_MBEDTLS_ECC_P256), please disable uECC (ENABLE_MICRO_ECC_P256) in bstack_config.h" 81 #endif 82 83 // Software ECC-P256 implementation provided by micro-ecc 84 #ifdef ENABLE_MICRO_ECC_P256 85 #define ENABLE_ECC_P256 86 #define USE_MICRO_ECC_P256 87 #define USE_SOFTWARE_ECC_P256_IMPLEMENTATION 88 #include "uECC.h" 89 #endif 90 91 // Software ECC-P256 implementation provided by mbedTLS 92 #ifdef HAVE_MBEDTLS_ECC_P256 93 #define ENABLE_ECC_P256 94 #define USE_MBEDTLS_ECC_P256 95 #define USE_SOFTWARE_ECC_P256_IMPLEMENTATION 96 #include "mbedtls/config.h" 97 #include "mbedtls/platform.h" 98 #include "mbedtls/ecp.h" 99 #endif 100 101 #if defined(ENABLE_LE_SECURE_CONNECTIONS) && !defined(ENABLE_ECC_P256) 102 #define ENABLE_ECC_P256 103 #endif 104 105 // debugging 106 // #define DEBUG_CCM 107 108 typedef enum { 109 CMAC_IDLE, 110 CMAC_CALC_SUBKEYS, 111 CMAC_W4_SUBKEYS, 112 CMAC_CALC_MI, 113 CMAC_W4_MI, 114 CMAC_CALC_MLAST, 115 CMAC_W4_MLAST 116 } btstack_crypto_cmac_state_t; 117 118 typedef enum { 119 ECC_P256_KEY_GENERATION_IDLE, 120 ECC_P256_KEY_GENERATION_GENERATING_RANDOM, 121 ECC_P256_KEY_GENERATION_ACTIVE, 122 ECC_P256_KEY_GENERATION_W4_KEY, 123 ECC_P256_KEY_GENERATION_DONE, 124 } btstack_crypto_ecc_p256_key_generation_state_t; 125 126 static void btstack_crypto_run(void); 127 128 static const uint8_t zero[16] = { 0 }; 129 130 static uint8_t btstack_crypto_initialized; 131 static btstack_linked_list_t btstack_crypto_operations; 132 static btstack_packet_callback_registration_t hci_event_callback_registration; 133 static uint8_t btstack_crypto_wait_for_hci_result; 134 135 // state for AES-CMAC 136 #ifndef USE_BTSTACK_AES128 137 static btstack_crypto_cmac_state_t btstack_crypto_cmac_state; 138 static sm_key_t btstack_crypto_cmac_k; 139 static sm_key_t btstack_crypto_cmac_x; 140 static sm_key_t btstack_crypto_cmac_subkey; 141 static uint8_t btstack_crypto_cmac_block_current; 142 static uint8_t btstack_crypto_cmac_block_count; 143 #endif 144 145 // state for AES-CCM 146 static uint8_t btstack_crypto_ccm_s[16]; 147 148 #ifdef ENABLE_ECC_P256 149 150 static uint8_t btstack_crypto_ecc_p256_public_key[64]; 151 static uint8_t btstack_crypto_ecc_p256_random[64]; 152 static uint8_t btstack_crypto_ecc_p256_random_len; 153 static uint8_t btstack_crypto_ecc_p256_random_offset; 154 static btstack_crypto_ecc_p256_key_generation_state_t btstack_crypto_ecc_p256_key_generation_state; 155 156 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 157 static uint8_t btstack_crypto_ecc_p256_d[32]; 158 #endif 159 160 // Software ECDH implementation provided by mbedtls 161 #ifdef USE_MBEDTLS_ECC_P256 162 static mbedtls_ecp_group mbedtls_ec_group; 163 #endif 164 165 #endif /* ENABLE_ECC_P256 */ 166 167 #ifdef ENABLE_SOFTWARE_AES128 168 // AES128 using public domain rijndael implementation 169 void btstack_aes128_calc(const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext){ 170 uint32_t rk[RKLENGTH(KEYBITS)]; 171 int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS); 172 rijndaelEncrypt(rk, nrounds, plaintext, ciphertext); 173 } 174 #endif 175 176 static void btstack_crypto_done(btstack_crypto_t * btstack_crypto){ 177 btstack_linked_list_pop(&btstack_crypto_operations); 178 (*btstack_crypto->context_callback.callback)(btstack_crypto->context_callback.context); 179 } 180 181 static void btstack_crypto_cmac_shift_left_by_one_bit_inplace(int len, uint8_t * data){ 182 int i; 183 int carry = 0; 184 for (i=len-1; i >= 0 ; i--){ 185 int new_carry = data[i] >> 7; 186 data[i] = (data[i] << 1) | carry; 187 carry = new_carry; 188 } 189 } 190 191 static uint8_t btstack_crypto_cmac_get_byte(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac, uint16_t pos){ 192 if (btstack_crypto_cmac->btstack_crypto.operation == BTSTACK_CRYPTO_CMAC_GENERATOR){ 193 return (*btstack_crypto_cmac->data.get_byte_callback)(pos); 194 } else { 195 return btstack_crypto_cmac->data.message[pos]; 196 } 197 } 198 199 #ifdef USE_BTSTACK_AES128 200 201 static void btstack_crypto_cmac_calc_subkeys(sm_key_t k0, sm_key_t k1, sm_key_t k2){ 202 memcpy(k1, k0, 16); 203 btstack_crypto_cmac_shift_left_by_one_bit_inplace(16, k1); 204 if (k0[0] & 0x80){ 205 k1[15] ^= 0x87; 206 } 207 memcpy(k2, k1, 16); 208 btstack_crypto_cmac_shift_left_by_one_bit_inplace(16, k2); 209 if (k1[0] & 0x80){ 210 k2[15] ^= 0x87; 211 } 212 } 213 214 static void btstack_crypto_cmac_calc(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac) { 215 sm_key_t k0, k1, k2; 216 uint16_t i; 217 218 btstack_aes128_calc(btstack_crypto_cmac->key, zero, k0); 219 btstack_crypto_cmac_calc_subkeys(k0, k1, k2); 220 221 uint16_t cmac_block_count = (btstack_crypto_cmac->size + 15) / 16; 222 223 // step 3: .. 224 if (cmac_block_count==0){ 225 cmac_block_count = 1; 226 } 227 228 // Step 5 229 sm_key_t cmac_x; 230 memset(cmac_x, 0, 16); 231 232 // Step 6 233 sm_key_t cmac_y; 234 int block; 235 for (block = 0 ; block < cmac_block_count-1 ; block++){ 236 for (i=0;i<16;i++){ 237 cmac_y[i] = cmac_x[i] ^ btstack_crypto_cmac_get_byte(btstack_crypto_cmac, (block*16) + i); 238 } 239 btstack_aes128_calc(btstack_crypto_cmac->key, cmac_y, cmac_x); 240 } 241 242 // step 4: set m_last 243 sm_key_t cmac_m_last; 244 bool last_block_complete = btstack_crypto_cmac->size != 0 && (btstack_crypto_cmac->size & 0x0f) == 0; 245 if (last_block_complete){ 246 for (i=0;i<16;i++){ 247 cmac_m_last[i] = btstack_crypto_cmac_get_byte(btstack_crypto_cmac, btstack_crypto_cmac->size - 16 + i) ^ k1[i]; 248 } 249 } else { 250 uint16_t valid_octets_in_last_block = btstack_crypto_cmac->size & 0x0f; 251 for (i=0;i<16;i++){ 252 if (i < valid_octets_in_last_block){ 253 cmac_m_last[i] = btstack_crypto_cmac_get_byte(btstack_crypto_cmac, (btstack_crypto_cmac->size & 0xfff0) + i) ^ k2[i]; 254 continue; 255 } 256 if (i == valid_octets_in_last_block){ 257 cmac_m_last[i] = 0x80 ^ k2[i]; 258 continue; 259 } 260 cmac_m_last[i] = k2[i]; 261 } 262 } 263 264 for (i=0;i<16;i++){ 265 cmac_y[i] = cmac_x[i] ^ cmac_m_last[i]; 266 } 267 268 // Step 7 269 btstack_aes128_calc(btstack_crypto_cmac->key, cmac_y, btstack_crypto_cmac->hash); 270 } 271 #else 272 273 static void btstack_crypto_aes128_start(const sm_key_t key, const sm_key_t plaintext){ 274 uint8_t key_flipped[16]; 275 uint8_t plaintext_flipped[16]; 276 reverse_128(key, key_flipped); 277 reverse_128(plaintext, plaintext_flipped); 278 btstack_crypto_wait_for_hci_result = 1; 279 hci_send_cmd(&hci_le_encrypt, key_flipped, plaintext_flipped); 280 } 281 282 static inline void btstack_crypto_cmac_next_state(void){ 283 btstack_crypto_cmac_state = (btstack_crypto_cmac_state_t) (((int)btstack_crypto_cmac_state) + 1); 284 } 285 286 static int btstack_crypto_cmac_last_block_complete(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac){ 287 uint16_t len = btstack_crypto_cmac->size; 288 if (len == 0u) return 0u; 289 return (len & 0x0fu) == 0u; 290 } 291 292 static void btstack_crypto_cmac_handle_aes_engine_ready(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac){ 293 switch (btstack_crypto_cmac_state){ 294 case CMAC_CALC_SUBKEYS: { 295 btstack_crypto_cmac_next_state(); 296 btstack_crypto_aes128_start(btstack_crypto_cmac_k, zero); 297 break; 298 } 299 case CMAC_CALC_MI: { 300 int j; 301 sm_key_t y; 302 for (j=0;j<16;j++){ 303 y[j] = btstack_crypto_cmac_x[j] ^ btstack_crypto_cmac_get_byte(btstack_crypto_cmac, (btstack_crypto_cmac_block_current*16u) + j); 304 } 305 btstack_crypto_cmac_block_current++; 306 btstack_crypto_cmac_next_state(); 307 btstack_crypto_aes128_start(btstack_crypto_cmac_k, y); 308 break; 309 } 310 case CMAC_CALC_MLAST: { 311 sm_key_t k1; 312 (void)memcpy(k1, btstack_crypto_cmac_subkey, 16); 313 btstack_crypto_cmac_shift_left_by_one_bit_inplace(16, k1); 314 if (btstack_crypto_cmac_subkey[0u] & 0x80u){ 315 k1[15u] ^= 0x87u; 316 } 317 sm_key_t k2; 318 (void)memcpy(k2, k1, 16); 319 btstack_crypto_cmac_shift_left_by_one_bit_inplace(16, k2); 320 if (k1[0u] & 0x80u){ 321 k2[15u] ^= 0x87u; 322 } 323 324 log_info_key("k", btstack_crypto_cmac_k); 325 log_info_key("k1", k1); 326 log_info_key("k2", k2); 327 328 // step 4: set m_last 329 int i; 330 sm_key_t btstack_crypto_cmac_m_last; 331 if (btstack_crypto_cmac_last_block_complete(btstack_crypto_cmac)){ 332 for (i=0;i<16;i++){ 333 btstack_crypto_cmac_m_last[i] = btstack_crypto_cmac_get_byte(btstack_crypto_cmac, btstack_crypto_cmac->size - 16u + i) ^ k1[i]; 334 } 335 } else { 336 int valid_octets_in_last_block = btstack_crypto_cmac->size & 0x0fu; 337 for (i=0;i<16;i++){ 338 if (i < valid_octets_in_last_block){ 339 btstack_crypto_cmac_m_last[i] = btstack_crypto_cmac_get_byte(btstack_crypto_cmac, (btstack_crypto_cmac->size & 0xfff0u) + i) ^ k2[i]; 340 continue; 341 } 342 if (i == valid_octets_in_last_block){ 343 btstack_crypto_cmac_m_last[i] = 0x80u ^ k2[i]; 344 continue; 345 } 346 btstack_crypto_cmac_m_last[i] = k2[i]; 347 } 348 } 349 sm_key_t y; 350 for (i=0;i<16;i++){ 351 y[i] = btstack_crypto_cmac_x[i] ^ btstack_crypto_cmac_m_last[i]; 352 } 353 btstack_crypto_cmac_block_current++; 354 btstack_crypto_cmac_next_state(); 355 btstack_crypto_aes128_start(btstack_crypto_cmac_k, y); 356 break; 357 } 358 default: 359 log_info("btstack_crypto_cmac_handle_aes_engine_ready called in state %u", btstack_crypto_cmac_state); 360 break; 361 } 362 } 363 364 static void btstack_crypto_cmac_handle_encryption_result(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac, sm_key_t data){ 365 switch (btstack_crypto_cmac_state){ 366 case CMAC_W4_SUBKEYS: 367 (void)memcpy(btstack_crypto_cmac_subkey, data, 16); 368 // next 369 btstack_crypto_cmac_state = (btstack_crypto_cmac_block_current < (btstack_crypto_cmac_block_count - 1u)) ? CMAC_CALC_MI : CMAC_CALC_MLAST; 370 break; 371 case CMAC_W4_MI: 372 (void)memcpy(btstack_crypto_cmac_x, data, 16); 373 btstack_crypto_cmac_state = (btstack_crypto_cmac_block_current < (btstack_crypto_cmac_block_count - 1u)) ? CMAC_CALC_MI : CMAC_CALC_MLAST; 374 break; 375 case CMAC_W4_MLAST: 376 // done 377 log_info("Setting CMAC Engine to IDLE"); 378 btstack_crypto_cmac_state = CMAC_IDLE; 379 log_info_key("CMAC", data); 380 (void)memcpy(btstack_crypto_cmac->hash, data, 16); 381 btstack_linked_list_pop(&btstack_crypto_operations); 382 (*btstack_crypto_cmac->btstack_crypto.context_callback.callback)(btstack_crypto_cmac->btstack_crypto.context_callback.context); 383 break; 384 default: 385 log_info("btstack_crypto_cmac_handle_encryption_result called in state %u", btstack_crypto_cmac_state); 386 break; 387 } 388 } 389 390 static void btstack_crypto_cmac_start(btstack_crypto_aes128_cmac_t * btstack_crypto_cmac){ 391 392 (void)memcpy(btstack_crypto_cmac_k, btstack_crypto_cmac->key, 16); 393 memset(btstack_crypto_cmac_x, 0, 16); 394 btstack_crypto_cmac_block_current = 0; 395 396 // step 2: n := ceil(len/const_Bsize); 397 btstack_crypto_cmac_block_count = (btstack_crypto_cmac->size + 15u) / 16u; 398 399 // step 3: .. 400 if (btstack_crypto_cmac_block_count==0u){ 401 btstack_crypto_cmac_block_count = 1; 402 } 403 log_info("btstack_crypto_cmac_start: len %u, block count %u", btstack_crypto_cmac->size, btstack_crypto_cmac_block_count); 404 405 // first, we need to compute l for k1, k2, and m_last 406 btstack_crypto_cmac_state = CMAC_CALC_SUBKEYS; 407 408 // let's go 409 btstack_crypto_cmac_handle_aes_engine_ready(btstack_crypto_cmac); 410 } 411 #endif 412 413 /* 414 To encrypt the message data we use Counter (CTR) mode. We first 415 define the key stream blocks by: 416 417 S_i := E( K, A_i ) for i=0, 1, 2, ... 418 419 The values A_i are formatted as follows, where the Counter field i is 420 encoded in most-significant-byte first order: 421 422 Octet Number Contents 423 ------------ --------- 424 0 Flags 425 1 ... 15-L Nonce N 426 16-L ... 15 Counter i 427 428 Bit Number Contents 429 ---------- ---------------------- 430 7 Reserved (always zero) 431 6 Reserved (always zero) 432 5 ... 3 Zero 433 2 ... 0 L' 434 */ 435 436 static void btstack_crypto_ccm_setup_a_i(btstack_crypto_ccm_t * btstack_crypto_ccm, uint16_t counter){ 437 btstack_crypto_ccm_s[0] = 1; // L' = L - 1 438 (void)memcpy(&btstack_crypto_ccm_s[1], btstack_crypto_ccm->nonce, 13); 439 big_endian_store_16(btstack_crypto_ccm_s, 14, counter); 440 #ifdef DEBUG_CCM 441 printf("btstack_crypto_ccm_setup_a_%u\n", counter); 442 printf("%16s: ", "ai"); 443 printf_hexdump(btstack_crypto_ccm_s, 16); 444 #endif 445 } 446 447 /* 448 The first step is to compute the authentication field T. This is 449 done using CBC-MAC [MAC]. We first define a sequence of blocks B_0, 450 B_1, ..., B_n and then apply CBC-MAC to these blocks. 451 452 The first block B_0 is formatted as follows, where l(m) is encoded in 453 most-significant-byte first order: 454 455 Octet Number Contents 456 ------------ --------- 457 0 Flags 458 1 ... 15-L Nonce N 459 16-L ... 15 l(m) 460 461 Within the first block B_0, the Flags field is formatted as follows: 462 463 Bit Number Contents 464 ---------- ---------------------- 465 7 Reserved (always zero) 466 6 Adata 467 5 ... 3 M' 468 2 ... 0 L' 469 */ 470 471 static void btstack_crypto_ccm_setup_b_0(btstack_crypto_ccm_t * btstack_crypto_ccm, uint8_t * b0){ 472 uint8_t m_prime = (btstack_crypto_ccm->auth_len - 2u) / 2u; 473 uint8_t Adata = btstack_crypto_ccm->aad_len ? 1 : 0; 474 b0[0u] = (Adata << 6u) | (m_prime << 3u) | 1u ; // Adata, M', L' = L - 1 475 (void)memcpy(&b0[1], btstack_crypto_ccm->nonce, 13); 476 big_endian_store_16(b0, 14, btstack_crypto_ccm->message_len); 477 #ifdef DEBUG_CCM 478 printf("%16s: ", "B0"); 479 printf_hexdump(b0, 16); 480 #endif 481 } 482 483 #ifdef ENABLE_ECC_P256 484 485 static void btstack_crypto_log_ec_publickey(const uint8_t * ec_q){ 486 log_info("Elliptic curve: X"); 487 log_info_hexdump(&ec_q[0],32); 488 log_info("Elliptic curve: Y"); 489 log_info_hexdump(&ec_q[32],32); 490 } 491 492 #if (defined(USE_MICRO_ECC_P256) && !defined(WICED_VERSION)) || defined(USE_MBEDTLS_ECC_P256) 493 // @return OK 494 static int sm_generate_f_rng(unsigned char * buffer, unsigned size){ 495 if (btstack_crypto_ecc_p256_key_generation_state != ECC_P256_KEY_GENERATION_ACTIVE) return 0; 496 log_info("sm_generate_f_rng: size %u - offset %u", (int) size, btstack_crypto_ecc_p256_random_offset); 497 while (size) { 498 *buffer++ = btstack_crypto_ecc_p256_random[btstack_crypto_ecc_p256_random_offset++]; 499 size--; 500 } 501 return 1; 502 } 503 #endif 504 #ifdef USE_MBEDTLS_ECC_P256 505 // @return error - just wrap sm_generate_f_rng 506 static int sm_generate_f_rng_mbedtls(void * context, unsigned char * buffer, size_t size){ 507 UNUSED(context); 508 return sm_generate_f_rng(buffer, size) == 0; 509 } 510 #endif /* USE_MBEDTLS_ECC_P256 */ 511 512 static void btstack_crypto_ecc_p256_generate_key_software(void){ 513 514 btstack_crypto_ecc_p256_random_offset = 0; 515 516 // generate EC key 517 #ifdef USE_MICRO_ECC_P256 518 519 #ifndef WICED_VERSION 520 log_info("set uECC RNG for initial key generation with 64 random bytes"); 521 // micro-ecc from WICED SDK uses its wiced_crypto_get_random by default - no need to set it 522 uECC_set_rng(&sm_generate_f_rng); 523 #endif /* WICED_VERSION */ 524 525 #if uECC_SUPPORTS_secp256r1 526 // standard version 527 uECC_make_key(btstack_crypto_ecc_p256_public_key, btstack_crypto_ecc_p256_d, uECC_secp256r1()); 528 529 // disable RNG again, as returning no randmon data lets shared key generation fail 530 log_info("disable uECC RNG in standard version after key generation"); 531 uECC_set_rng(NULL); 532 #else 533 // static version 534 uECC_make_key(btstack_crypto_ecc_p256_public_key, btstack_crypto_ecc_p256_d); 535 #endif 536 #endif /* USE_MICRO_ECC_P256 */ 537 538 #ifdef USE_MBEDTLS_ECC_P256 539 mbedtls_mpi d; 540 mbedtls_ecp_point P; 541 mbedtls_mpi_init(&d); 542 mbedtls_ecp_point_init(&P); 543 int res = mbedtls_ecp_gen_keypair(&mbedtls_ec_group, &d, &P, &sm_generate_f_rng_mbedtls, NULL); 544 log_info("gen keypair %x", res); 545 mbedtls_mpi_write_binary(&P.X, &btstack_crypto_ecc_p256_public_key[0], 32); 546 mbedtls_mpi_write_binary(&P.Y, &btstack_crypto_ecc_p256_public_key[32], 32); 547 mbedtls_mpi_write_binary(&d, btstack_crypto_ecc_p256_d, 32); 548 mbedtls_ecp_point_free(&P); 549 mbedtls_mpi_free(&d); 550 #endif /* USE_MBEDTLS_ECC_P256 */ 551 } 552 553 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 554 static void btstack_crypto_ecc_p256_calculate_dhkey_software(btstack_crypto_ecc_p256_t * btstack_crypto_ec_p192){ 555 memset(btstack_crypto_ec_p192->dhkey, 0, 32); 556 557 #ifdef USE_MICRO_ECC_P256 558 #if uECC_SUPPORTS_secp256r1 559 // standard version 560 uECC_shared_secret(btstack_crypto_ec_p192->public_key, btstack_crypto_ecc_p256_d, btstack_crypto_ec_p192->dhkey, uECC_secp256r1()); 561 #else 562 // static version 563 uECC_shared_secret(btstack_crypto_ec_p192->public_key, btstack_crypto_ecc_p256_d, btstack_crypto_ec_p192->dhkey); 564 #endif 565 #endif 566 567 #ifdef USE_MBEDTLS_ECC_P256 568 // da * Pb 569 mbedtls_mpi d; 570 mbedtls_ecp_point Q; 571 mbedtls_ecp_point DH; 572 mbedtls_mpi_init(&d); 573 mbedtls_ecp_point_init(&Q); 574 mbedtls_ecp_point_init(&DH); 575 mbedtls_mpi_read_binary(&d, btstack_crypto_ecc_p256_d, 32); 576 mbedtls_mpi_read_binary(&Q.X, &btstack_crypto_ec_p192->public_key[0] , 32); 577 mbedtls_mpi_read_binary(&Q.Y, &btstack_crypto_ec_p192->public_key[32], 32); 578 mbedtls_mpi_lset(&Q.Z, 1); 579 mbedtls_ecp_mul(&mbedtls_ec_group, &DH, &d, &Q, NULL, NULL); 580 mbedtls_mpi_write_binary(&DH.X, btstack_crypto_ec_p192->dhkey, 32); 581 mbedtls_ecp_point_free(&DH); 582 mbedtls_mpi_free(&d); 583 mbedtls_ecp_point_free(&Q); 584 #endif 585 586 log_info("dhkey"); 587 log_info_hexdump(btstack_crypto_ec_p192->dhkey, 32); 588 } 589 #endif 590 591 #endif 592 593 static void btstack_crypto_ccm_next_block(btstack_crypto_ccm_t * btstack_crypto_ccm, btstack_crypto_ccm_state_t state_when_done){ 594 uint16_t bytes_to_process = btstack_min(btstack_crypto_ccm->block_len, 16); 595 // next block 596 btstack_crypto_ccm->counter++; 597 btstack_crypto_ccm->input += bytes_to_process; 598 btstack_crypto_ccm->output += bytes_to_process; 599 btstack_crypto_ccm->block_len -= bytes_to_process; 600 btstack_crypto_ccm->message_len -= bytes_to_process; 601 #ifdef DEBUG_CCM 602 printf("btstack_crypto_ccm_next_block (message len %u, block_len %u)\n", btstack_crypto_ccm->message_len, btstack_crypto_ccm->block_len); 603 #endif 604 if (btstack_crypto_ccm->message_len == 0u){ 605 btstack_crypto_ccm->state = CCM_CALCULATE_S0; 606 } else { 607 btstack_crypto_ccm->state = state_when_done; 608 if (btstack_crypto_ccm->block_len == 0u){ 609 btstack_crypto_done(&btstack_crypto_ccm->btstack_crypto); 610 } 611 } 612 } 613 614 // If Controller is used for AES128, data is little endian 615 static void btstack_crypto_ccm_handle_s0(btstack_crypto_ccm_t * btstack_crypto_ccm, const uint8_t * data){ 616 int i; 617 for (i=0;i<16;i++){ 618 #ifdef USE_BTSTACK_AES128 619 btstack_crypto_ccm->x_i[i] = btstack_crypto_ccm->x_i[i] ^ data[i]; 620 #else 621 btstack_crypto_ccm->x_i[i] = btstack_crypto_ccm->x_i[i] ^ data[15-i]; 622 #endif 623 } 624 btstack_crypto_done(&btstack_crypto_ccm->btstack_crypto); 625 } 626 627 // If Controller is used for AES128, data is little endian 628 static void btstack_crypto_ccm_handle_sn(btstack_crypto_ccm_t * btstack_crypto_ccm, const uint8_t * data){ 629 int i; 630 uint16_t bytes_to_process = btstack_min(btstack_crypto_ccm->block_len, 16); 631 for (i=0;i<bytes_to_process;i++){ 632 #ifdef USE_BTSTACK_AES128 633 btstack_crypto_ccm->output[i] = btstack_crypto_ccm->input[i] ^ data[i]; 634 #else 635 btstack_crypto_ccm->output[i] = btstack_crypto_ccm->input[i] ^ data[15-i]; 636 #endif 637 } 638 switch (btstack_crypto_ccm->btstack_crypto.operation){ 639 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK: 640 btstack_crypto_ccm->state = CCM_CALCULATE_XN; 641 break; 642 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK: 643 btstack_crypto_ccm_next_block(btstack_crypto_ccm, CCM_CALCULATE_XN); 644 break; 645 default: 646 btstack_assert(false); 647 break; 648 } 649 } 650 651 static void btstack_crypto_ccm_handle_aad_xn(btstack_crypto_ccm_t * btstack_crypto_ccm) { 652 #ifdef DEBUG_CCM 653 printf("%16s: ", "Xn+1 AAD"); 654 printf_hexdump(btstack_crypto_ccm->x_i, 16); 655 #endif 656 // more aad? 657 if (btstack_crypto_ccm->aad_offset < (btstack_crypto_ccm->aad_len + 2u)){ 658 btstack_crypto_ccm->state = CCM_CALCULATE_AAD_XN; 659 } else { 660 // done 661 btstack_crypto_done((btstack_crypto_t *) btstack_crypto_ccm); 662 } 663 } 664 665 static void btstack_crypto_ccm_handle_x1(btstack_crypto_ccm_t * btstack_crypto_ccm) { 666 #ifdef DEBUG_CCM 667 printf("%16s: ", "Xi"); 668 printf_hexdump(btstack_crypto_ccm->x_i, 16); 669 #endif 670 switch (btstack_crypto_ccm->btstack_crypto.operation){ 671 case BTSTACK_CRYPTO_CCM_DIGEST_BLOCK: 672 btstack_crypto_ccm->aad_remainder_len = 0; 673 btstack_crypto_ccm->state = CCM_CALCULATE_AAD_XN; 674 break; 675 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK: 676 btstack_crypto_ccm->state = CCM_CALCULATE_SN; 677 break; 678 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK: 679 btstack_crypto_ccm->state = CCM_CALCULATE_XN; 680 break; 681 default: 682 btstack_assert(false); 683 break; 684 } 685 } 686 687 static void btstack_crypto_ccm_handle_xn(btstack_crypto_ccm_t * btstack_crypto_ccm) { 688 #ifdef DEBUG_CCM 689 printf("%16s: ", "Xn+1"); 690 printf_hexdump(btstack_crypto_ccm->x_i, 16); 691 #endif 692 switch (btstack_crypto_ccm->btstack_crypto.operation){ 693 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK: 694 btstack_crypto_ccm_next_block(btstack_crypto_ccm, CCM_CALCULATE_SN); 695 break; 696 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK: 697 btstack_crypto_ccm->state = CCM_CALCULATE_SN; 698 break; 699 default: 700 btstack_assert(false); 701 break; 702 } 703 } 704 705 static void btstack_crypto_ccm_calc_s0(btstack_crypto_ccm_t * btstack_crypto_ccm){ 706 #ifdef DEBUG_CCM 707 printf("btstack_crypto_ccm_calc_s0\n"); 708 #endif 709 btstack_crypto_ccm->state = CCM_W4_S0; 710 btstack_crypto_ccm_setup_a_i(btstack_crypto_ccm, 0); 711 #ifdef USE_BTSTACK_AES128 712 uint8_t data[16]; 713 btstack_aes128_calc(btstack_crypto_ccm->key, btstack_crypto_ccm_s, data); 714 btstack_crypto_ccm_handle_s0(btstack_crypto_ccm, data); 715 #else 716 btstack_crypto_aes128_start(btstack_crypto_ccm->key, btstack_crypto_ccm_s); 717 #endif 718 } 719 720 static void btstack_crypto_ccm_calc_sn(btstack_crypto_ccm_t * btstack_crypto_ccm){ 721 #ifdef DEBUG_CCM 722 printf("btstack_crypto_ccm_calc_s%u\n", btstack_crypto_ccm->counter); 723 #endif 724 btstack_crypto_ccm->state = CCM_W4_SN; 725 btstack_crypto_ccm_setup_a_i(btstack_crypto_ccm, btstack_crypto_ccm->counter); 726 #ifdef USE_BTSTACK_AES128 727 uint8_t data[16]; 728 btstack_aes128_calc(btstack_crypto_ccm->key, btstack_crypto_ccm_s, data); 729 btstack_crypto_ccm_handle_sn(btstack_crypto_ccm, data); 730 #else 731 btstack_crypto_aes128_start(btstack_crypto_ccm->key, btstack_crypto_ccm_s); 732 #endif 733 } 734 735 static void btstack_crypto_ccm_calc_x1(btstack_crypto_ccm_t * btstack_crypto_ccm){ 736 uint8_t btstack_crypto_ccm_buffer[16]; 737 btstack_crypto_ccm->state = CCM_W4_X1; 738 btstack_crypto_ccm_setup_b_0(btstack_crypto_ccm, btstack_crypto_ccm_buffer); 739 #ifdef USE_BTSTACK_AES128 740 btstack_aes128_calc(btstack_crypto_ccm->key, btstack_crypto_ccm_buffer, btstack_crypto_ccm->x_i); 741 btstack_crypto_ccm_handle_x1(btstack_crypto_ccm); 742 #else 743 btstack_crypto_aes128_start(btstack_crypto_ccm->key, btstack_crypto_ccm_buffer); 744 #endif 745 } 746 747 static void btstack_crypto_ccm_calc_xn(btstack_crypto_ccm_t * btstack_crypto_ccm, const uint8_t * plaintext){ 748 uint8_t btstack_crypto_ccm_buffer[16]; 749 btstack_crypto_ccm->state = CCM_W4_XN; 750 751 #ifdef DEBUG_CCM 752 printf("%16s: ", "bn"); 753 printf_hexdump(plaintext, 16); 754 #endif 755 uint8_t i; 756 uint8_t bytes_to_decrypt = btstack_crypto_ccm->block_len; 757 // use explicit min implementation as c-stat worried about out-of-bounds-reads 758 if (bytes_to_decrypt > 16u) { 759 bytes_to_decrypt = 16; 760 } 761 for (i = 0; i < bytes_to_decrypt ; i++){ 762 btstack_crypto_ccm_buffer[i] = btstack_crypto_ccm->x_i[i] ^ plaintext[i]; 763 } 764 (void)memcpy(&btstack_crypto_ccm_buffer[i], &btstack_crypto_ccm->x_i[i], 765 16u - bytes_to_decrypt); 766 #ifdef DEBUG_CCM 767 printf("%16s: ", "Xn XOR bn"); 768 printf_hexdump(btstack_crypto_ccm_buffer, 16); 769 #endif 770 771 #ifdef USE_BTSTACK_AES128 772 btstack_aes128_calc(btstack_crypto_ccm->key, btstack_crypto_ccm_buffer, btstack_crypto_ccm->x_i); 773 btstack_crypto_ccm_handle_xn(btstack_crypto_ccm); 774 #else 775 btstack_crypto_aes128_start(btstack_crypto_ccm->key, btstack_crypto_ccm_buffer); 776 #endif 777 } 778 779 static void btstack_crypto_ccm_calc_aad_xn(btstack_crypto_ccm_t * btstack_crypto_ccm){ 780 // store length 781 if (btstack_crypto_ccm->aad_offset == 0u){ 782 uint8_t len_buffer[2]; 783 big_endian_store_16(len_buffer, 0, btstack_crypto_ccm->aad_len); 784 btstack_crypto_ccm->x_i[0] ^= len_buffer[0]; 785 btstack_crypto_ccm->x_i[1] ^= len_buffer[1]; 786 btstack_crypto_ccm->aad_remainder_len += 2u; 787 btstack_crypto_ccm->aad_offset += 2u; 788 } 789 790 // fill from input 791 uint16_t bytes_free = 16u - btstack_crypto_ccm->aad_remainder_len; 792 uint16_t bytes_to_copy = btstack_min(bytes_free, btstack_crypto_ccm->block_len); 793 while (bytes_to_copy){ 794 btstack_crypto_ccm->x_i[btstack_crypto_ccm->aad_remainder_len++] ^= *btstack_crypto_ccm->input++; 795 btstack_crypto_ccm->aad_offset++; 796 btstack_crypto_ccm->block_len--; 797 bytes_to_copy--; 798 bytes_free--; 799 } 800 801 // if last block, fill with zeros 802 if (btstack_crypto_ccm->aad_offset == (btstack_crypto_ccm->aad_len + 2u)){ 803 btstack_crypto_ccm->aad_remainder_len = 16; 804 } 805 // if not full, notify done 806 if (btstack_crypto_ccm->aad_remainder_len < 16u){ 807 btstack_crypto_done(&btstack_crypto_ccm->btstack_crypto); 808 return; 809 } 810 811 // encrypt block 812 #ifdef DEBUG_CCM 813 printf("%16s: ", "Xn XOR Bn (aad)"); 814 printf_hexdump(btstack_crypto_ccm->x_i, 16); 815 #endif 816 817 btstack_crypto_ccm->aad_remainder_len = 0; 818 btstack_crypto_ccm->state = CCM_W4_AAD_XN; 819 #ifdef USE_BTSTACK_AES128 820 btstack_aes128_calc(btstack_crypto_ccm->key, btstack_crypto_ccm->x_i, btstack_crypto_ccm->x_i); 821 btstack_crypto_ccm_handle_aad_xn(btstack_crypto_ccm); 822 #else 823 btstack_crypto_aes128_start(btstack_crypto_ccm->key, btstack_crypto_ccm->x_i); 824 #endif 825 } 826 827 static void btstack_crypto_run(void){ 828 829 btstack_crypto_aes128_t * btstack_crypto_aes128; 830 btstack_crypto_ccm_t * btstack_crypto_ccm; 831 btstack_crypto_aes128_cmac_t * btstack_crypto_cmac; 832 #ifdef ENABLE_ECC_P256 833 btstack_crypto_ecc_p256_t * btstack_crypto_ec_p192; 834 #endif 835 836 // stack up and running? 837 if (hci_get_state() != HCI_STATE_WORKING) return; 838 839 // try to do as much as possible 840 while (true){ 841 842 // anything to do? 843 if (btstack_linked_list_empty(&btstack_crypto_operations)) return; 844 845 // already active? 846 if (btstack_crypto_wait_for_hci_result) return; 847 848 // can send a command? 849 if (!hci_can_send_command_packet_now()) return; 850 851 // ok, find next task 852 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 853 switch (btstack_crypto->operation){ 854 case BTSTACK_CRYPTO_RANDOM: 855 btstack_crypto_wait_for_hci_result = 1; 856 hci_send_cmd(&hci_le_rand); 857 break; 858 case BTSTACK_CRYPTO_AES128: 859 btstack_crypto_aes128 = (btstack_crypto_aes128_t *) btstack_crypto; 860 #ifdef USE_BTSTACK_AES128 861 btstack_aes128_calc(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext, btstack_crypto_aes128->ciphertext); 862 btstack_crypto_done(btstack_crypto); 863 #else 864 btstack_crypto_aes128_start(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext); 865 #endif 866 break; 867 868 case BTSTACK_CRYPTO_CMAC_MESSAGE: 869 case BTSTACK_CRYPTO_CMAC_GENERATOR: 870 btstack_crypto_cmac = (btstack_crypto_aes128_cmac_t *) btstack_crypto; 871 #ifdef USE_BTSTACK_AES128 872 btstack_crypto_cmac_calc( btstack_crypto_cmac ); 873 btstack_crypto_done(btstack_crypto); 874 #else 875 btstack_crypto_wait_for_hci_result = 1; 876 if (btstack_crypto_cmac_state == CMAC_IDLE){ 877 btstack_crypto_cmac_start(btstack_crypto_cmac); 878 } else { 879 btstack_crypto_cmac_handle_aes_engine_ready(btstack_crypto_cmac); 880 } 881 #endif 882 break; 883 884 case BTSTACK_CRYPTO_CCM_DIGEST_BLOCK: 885 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK: 886 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK: 887 btstack_crypto_ccm = (btstack_crypto_ccm_t *) btstack_crypto; 888 switch (btstack_crypto_ccm->state){ 889 case CCM_CALCULATE_AAD_XN: 890 #ifdef DEBUG_CCM 891 printf("CCM_CALCULATE_AAD_XN\n"); 892 #endif 893 btstack_crypto_ccm_calc_aad_xn(btstack_crypto_ccm); 894 break; 895 case CCM_CALCULATE_X1: 896 #ifdef DEBUG_CCM 897 printf("CCM_CALCULATE_X1\n"); 898 #endif 899 btstack_crypto_ccm_calc_x1(btstack_crypto_ccm); 900 break; 901 case CCM_CALCULATE_S0: 902 #ifdef DEBUG_CCM 903 printf("CCM_CALCULATE_S0\n"); 904 #endif 905 btstack_crypto_ccm_calc_s0(btstack_crypto_ccm); 906 break; 907 case CCM_CALCULATE_SN: 908 #ifdef DEBUG_CCM 909 printf("CCM_CALCULATE_SN\n"); 910 #endif 911 btstack_crypto_ccm_calc_sn(btstack_crypto_ccm); 912 break; 913 case CCM_CALCULATE_XN: 914 #ifdef DEBUG_CCM 915 printf("CCM_CALCULATE_XN\n"); 916 #endif 917 btstack_crypto_ccm_calc_xn(btstack_crypto_ccm, (btstack_crypto->operation == BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK) ? btstack_crypto_ccm->input : btstack_crypto_ccm->output); 918 break; 919 default: 920 break; 921 } 922 break; 923 924 #ifdef ENABLE_ECC_P256 925 case BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY: 926 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto; 927 switch (btstack_crypto_ecc_p256_key_generation_state){ 928 case ECC_P256_KEY_GENERATION_DONE: 929 // done 930 btstack_crypto_log_ec_publickey(btstack_crypto_ecc_p256_public_key); 931 (void)memcpy(btstack_crypto_ec_p192->public_key, 932 btstack_crypto_ecc_p256_public_key, 64); 933 btstack_linked_list_pop(&btstack_crypto_operations); 934 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context); 935 break; 936 case ECC_P256_KEY_GENERATION_IDLE: 937 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 938 log_info("start ecc random"); 939 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_GENERATING_RANDOM; 940 btstack_crypto_ecc_p256_random_offset = 0; 941 btstack_crypto_wait_for_hci_result = 1; 942 hci_send_cmd(&hci_le_rand); 943 #else 944 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_W4_KEY; 945 btstack_crypto_wait_for_hci_result = 1; 946 hci_send_cmd(&hci_le_read_local_p256_public_key); 947 #endif 948 break; 949 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 950 case ECC_P256_KEY_GENERATION_GENERATING_RANDOM: 951 log_info("more ecc random"); 952 btstack_crypto_wait_for_hci_result = 1; 953 hci_send_cmd(&hci_le_rand); 954 break; 955 #endif 956 default: 957 break; 958 } 959 break; 960 case BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY: 961 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto; 962 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 963 btstack_crypto_ecc_p256_calculate_dhkey_software(btstack_crypto_ec_p192); 964 // done 965 btstack_linked_list_pop(&btstack_crypto_operations); 966 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context); 967 #else 968 btstack_crypto_wait_for_hci_result = 1; 969 hci_send_cmd(&hci_le_generate_dhkey, &btstack_crypto_ec_p192->public_key[0], &btstack_crypto_ec_p192->public_key[32]); 970 #endif 971 break; 972 973 #endif /* ENABLE_ECC_P256 */ 974 975 default: 976 break; 977 } 978 } 979 } 980 981 static void btstack_crypto_handle_random_data(const uint8_t * data, uint16_t len){ 982 btstack_crypto_random_t * btstack_crypto_random; 983 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 984 uint16_t bytes_to_copy; 985 if (!btstack_crypto) return; 986 switch (btstack_crypto->operation){ 987 case BTSTACK_CRYPTO_RANDOM: 988 btstack_crypto_random = (btstack_crypto_random_t*) btstack_crypto; 989 bytes_to_copy = btstack_min(btstack_crypto_random->size, len); 990 (void)memcpy(btstack_crypto_random->buffer, data, bytes_to_copy); 991 btstack_crypto_random->buffer += bytes_to_copy; 992 btstack_crypto_random->size -= bytes_to_copy; 993 // data processed, more? 994 if (!btstack_crypto_random->size) { 995 // done 996 btstack_linked_list_pop(&btstack_crypto_operations); 997 (*btstack_crypto_random->btstack_crypto.context_callback.callback)(btstack_crypto_random->btstack_crypto.context_callback.context); 998 } 999 break; 1000 #ifdef ENABLE_ECC_P256 1001 case BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY: 1002 (void)memcpy(&btstack_crypto_ecc_p256_random[btstack_crypto_ecc_p256_random_len], 1003 data, 8); 1004 btstack_crypto_ecc_p256_random_len += 8u; 1005 if (btstack_crypto_ecc_p256_random_len >= 64u) { 1006 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_ACTIVE; 1007 btstack_crypto_ecc_p256_generate_key_software(); 1008 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_DONE; 1009 } 1010 break; 1011 #endif 1012 default: 1013 break; 1014 } 1015 // more work? 1016 btstack_crypto_run(); 1017 } 1018 1019 #ifndef USE_BTSTACK_AES128 1020 static void btstack_crypto_handle_encryption_result(const uint8_t * data){ 1021 btstack_crypto_aes128_t * btstack_crypto_aes128; 1022 btstack_crypto_aes128_cmac_t * btstack_crypto_cmac; 1023 btstack_crypto_ccm_t * btstack_crypto_ccm; 1024 uint8_t result[16]; 1025 1026 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 1027 if (!btstack_crypto) return; 1028 switch (btstack_crypto->operation){ 1029 case BTSTACK_CRYPTO_AES128: 1030 btstack_crypto_aes128 = (btstack_crypto_aes128_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 1031 reverse_128(data, btstack_crypto_aes128->ciphertext); 1032 btstack_crypto_done(btstack_crypto); 1033 break; 1034 case BTSTACK_CRYPTO_CMAC_GENERATOR: 1035 case BTSTACK_CRYPTO_CMAC_MESSAGE: 1036 btstack_crypto_cmac = (btstack_crypto_aes128_cmac_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 1037 reverse_128(data, result); 1038 btstack_crypto_cmac_handle_encryption_result(btstack_crypto_cmac, result); 1039 break; 1040 case BTSTACK_CRYPTO_CCM_DIGEST_BLOCK: 1041 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK: 1042 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK: 1043 btstack_crypto_ccm = (btstack_crypto_ccm_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 1044 switch (btstack_crypto_ccm->state){ 1045 case CCM_W4_X1: 1046 reverse_128(data, btstack_crypto_ccm->x_i); 1047 btstack_crypto_ccm_handle_x1(btstack_crypto_ccm); 1048 break; 1049 case CCM_W4_XN: 1050 reverse_128(data, btstack_crypto_ccm->x_i); 1051 btstack_crypto_ccm_handle_xn(btstack_crypto_ccm); 1052 break; 1053 case CCM_W4_AAD_XN: 1054 reverse_128(data, btstack_crypto_ccm->x_i); 1055 btstack_crypto_ccm_handle_aad_xn(btstack_crypto_ccm); 1056 break; 1057 case CCM_W4_S0: 1058 btstack_crypto_ccm_handle_s0(btstack_crypto_ccm, data); 1059 break; 1060 case CCM_W4_SN: 1061 btstack_crypto_ccm_handle_sn(btstack_crypto_ccm, data); 1062 break; 1063 default: 1064 break; 1065 } 1066 break; 1067 default: 1068 break; 1069 } 1070 } 1071 #endif 1072 1073 static void btstack_crypto_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1074 UNUSED(cid); // ok: there is no channel 1075 UNUSED(size); // ok: fixed format events read from HCI buffer 1076 1077 #ifdef ENABLE_ECC_P256 1078 #ifndef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 1079 btstack_crypto_ecc_p256_t * btstack_crypto_ec_p192; 1080 #endif 1081 #endif 1082 1083 if (packet_type != HCI_EVENT_PACKET) return; 1084 1085 switch (hci_event_packet_get_type(packet)){ 1086 case BTSTACK_EVENT_STATE: 1087 log_info("BTSTACK_EVENT_STATE"); 1088 if (btstack_event_state_get_state(packet) != HCI_STATE_HALTING) break; 1089 if (!btstack_crypto_wait_for_hci_result) break; 1090 // request stack to defer shutdown a bit 1091 hci_halting_defer(); 1092 break; 1093 1094 case HCI_EVENT_COMMAND_COMPLETE: 1095 #ifndef USE_BTSTACK_AES128 1096 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_encrypt)){ 1097 if (!btstack_crypto_wait_for_hci_result) return; 1098 btstack_crypto_wait_for_hci_result = 0; 1099 btstack_crypto_handle_encryption_result(&packet[6]); 1100 } 1101 #endif 1102 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_rand)){ 1103 if (!btstack_crypto_wait_for_hci_result) return; 1104 btstack_crypto_wait_for_hci_result = 0; 1105 btstack_crypto_handle_random_data(&packet[6], 8); 1106 } 1107 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1108 int ecdh_operations_supported = (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+34u] & 0x06u) == 0x06u; 1109 UNUSED(ecdh_operations_supported); 1110 log_info("controller supports ECDH operation: %u", ecdh_operations_supported); 1111 #ifdef ENABLE_ECC_P256 1112 #ifndef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 1113 if (!ecdh_operations_supported){ 1114 // mbedTLS can also be used if already available (and malloc is supported) 1115 log_error("ECC-P256 support enabled, but HCI Controller doesn't support it. Please add ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS to btstack_config.h"); 1116 } 1117 #endif 1118 #endif 1119 } 1120 break; 1121 1122 #ifdef ENABLE_ECC_P256 1123 #ifndef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 1124 case HCI_EVENT_LE_META: 1125 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations); 1126 if (!btstack_crypto_ec_p192) break; 1127 switch (hci_event_le_meta_get_subevent_code(packet)){ 1128 case HCI_SUBEVENT_LE_READ_LOCAL_P256_PUBLIC_KEY_COMPLETE: 1129 if (btstack_crypto_ec_p192->btstack_crypto.operation != BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY) break; 1130 if (!btstack_crypto_wait_for_hci_result) return; 1131 btstack_crypto_wait_for_hci_result = 0; 1132 if (hci_subevent_le_read_local_p256_public_key_complete_get_status(packet)){ 1133 log_error("Read Local P256 Public Key failed"); 1134 } 1135 hci_subevent_le_read_local_p256_public_key_complete_get_dhkey_x(packet, &btstack_crypto_ecc_p256_public_key[0]); 1136 hci_subevent_le_read_local_p256_public_key_complete_get_dhkey_y(packet, &btstack_crypto_ecc_p256_public_key[32]); 1137 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_DONE; 1138 break; 1139 case HCI_SUBEVENT_LE_GENERATE_DHKEY_COMPLETE: 1140 if (btstack_crypto_ec_p192->btstack_crypto.operation != BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY) break; 1141 if (!btstack_crypto_wait_for_hci_result) return; 1142 btstack_crypto_wait_for_hci_result = 0; 1143 if (hci_subevent_le_generate_dhkey_complete_get_status(packet)){ 1144 log_error("Generate DHKEY failed -> abort"); 1145 } 1146 hci_subevent_le_generate_dhkey_complete_get_dhkey(packet, btstack_crypto_ec_p192->dhkey); 1147 // done 1148 btstack_linked_list_pop(&btstack_crypto_operations); 1149 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context); 1150 break; 1151 default: 1152 break; 1153 } 1154 break; 1155 #endif 1156 #endif 1157 default: 1158 break; 1159 } 1160 1161 // try processing 1162 btstack_crypto_run(); 1163 } 1164 1165 void btstack_crypto_init(void){ 1166 if (btstack_crypto_initialized) return; 1167 btstack_crypto_initialized = 1; 1168 1169 // register with HCI 1170 hci_event_callback_registration.callback = &btstack_crypto_event_handler; 1171 hci_add_event_handler(&hci_event_callback_registration); 1172 1173 #ifdef USE_MBEDTLS_ECC_P256 1174 mbedtls_ecp_group_init(&mbedtls_ec_group); 1175 mbedtls_ecp_group_load(&mbedtls_ec_group, MBEDTLS_ECP_DP_SECP256R1); 1176 #endif 1177 } 1178 1179 void btstack_crypto_random_generate(btstack_crypto_random_t * request, uint8_t * buffer, uint16_t size, void (* callback)(void * arg), void * callback_arg){ 1180 request->btstack_crypto.context_callback.callback = callback; 1181 request->btstack_crypto.context_callback.context = callback_arg; 1182 request->btstack_crypto.operation = BTSTACK_CRYPTO_RANDOM; 1183 request->buffer = buffer; 1184 request->size = size; 1185 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1186 btstack_crypto_run(); 1187 } 1188 1189 void btstack_crypto_aes128_encrypt(btstack_crypto_aes128_t * request, const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext, void (* callback)(void * arg), void * callback_arg){ 1190 request->btstack_crypto.context_callback.callback = callback; 1191 request->btstack_crypto.context_callback.context = callback_arg; 1192 request->btstack_crypto.operation = BTSTACK_CRYPTO_AES128; 1193 request->key = key; 1194 request->plaintext = plaintext; 1195 request->ciphertext = ciphertext; 1196 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1197 btstack_crypto_run(); 1198 } 1199 1200 void btstack_crypto_aes128_cmac_generator(btstack_crypto_aes128_cmac_t * request, const uint8_t * key, uint16_t size, uint8_t (*get_byte_callback)(uint16_t pos), uint8_t * hash, void (* callback)(void * arg), void * callback_arg){ 1201 request->btstack_crypto.context_callback.callback = callback; 1202 request->btstack_crypto.context_callback.context = callback_arg; 1203 request->btstack_crypto.operation = BTSTACK_CRYPTO_CMAC_GENERATOR; 1204 request->key = key; 1205 request->size = size; 1206 request->data.get_byte_callback = get_byte_callback; 1207 request->hash = hash; 1208 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1209 btstack_crypto_run(); 1210 } 1211 1212 void btstack_crypto_aes128_cmac_message(btstack_crypto_aes128_cmac_t * request, const uint8_t * key, uint16_t size, const uint8_t * message, uint8_t * hash, void (* callback)(void * arg), void * callback_arg){ 1213 request->btstack_crypto.context_callback.callback = callback; 1214 request->btstack_crypto.context_callback.context = callback_arg; 1215 request->btstack_crypto.operation = BTSTACK_CRYPTO_CMAC_MESSAGE; 1216 request->key = key; 1217 request->size = size; 1218 request->data.message = message; 1219 request->hash = hash; 1220 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1221 btstack_crypto_run(); 1222 } 1223 1224 void btstack_crypto_aes128_cmac_zero(btstack_crypto_aes128_cmac_t * request, uint16_t len, const uint8_t * message, uint8_t * hash, void (* callback)(void * arg), void * callback_arg){ 1225 request->btstack_crypto.context_callback.callback = callback; 1226 request->btstack_crypto.context_callback.context = callback_arg; 1227 request->btstack_crypto.operation = BTSTACK_CRYPTO_CMAC_MESSAGE; 1228 request->key = zero; 1229 request->size = len; 1230 request->data.message = message; 1231 request->hash = hash; 1232 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1233 btstack_crypto_run(); 1234 } 1235 1236 #ifdef ENABLE_ECC_P256 1237 void btstack_crypto_ecc_p256_generate_key(btstack_crypto_ecc_p256_t * request, uint8_t * public_key, void (* callback)(void * arg), void * callback_arg){ 1238 // reset key generation 1239 if (btstack_crypto_ecc_p256_key_generation_state == ECC_P256_KEY_GENERATION_DONE){ 1240 btstack_crypto_ecc_p256_random_len = 0; 1241 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_IDLE; 1242 } 1243 request->btstack_crypto.context_callback.callback = callback; 1244 request->btstack_crypto.context_callback.context = callback_arg; 1245 request->btstack_crypto.operation = BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY; 1246 request->public_key = public_key; 1247 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1248 btstack_crypto_run(); 1249 } 1250 1251 void btstack_crypto_ecc_p256_calculate_dhkey(btstack_crypto_ecc_p256_t * request, const uint8_t * public_key, uint8_t * dhkey, void (* callback)(void * arg), void * callback_arg){ 1252 request->btstack_crypto.context_callback.callback = callback; 1253 request->btstack_crypto.context_callback.context = callback_arg; 1254 request->btstack_crypto.operation = BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY; 1255 request->public_key = (uint8_t *) public_key; 1256 request->dhkey = dhkey; 1257 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1258 btstack_crypto_run(); 1259 } 1260 1261 int btstack_crypto_ecc_p256_validate_public_key(const uint8_t * public_key){ 1262 1263 // validate public key using micro-ecc 1264 int err = 0; 1265 1266 #ifdef USE_MICRO_ECC_P256 1267 #if uECC_SUPPORTS_secp256r1 1268 // standard version 1269 err = uECC_valid_public_key(public_key, uECC_secp256r1()) == 0; 1270 #else 1271 // static version 1272 err = uECC_valid_public_key(public_key) == 0; 1273 #endif 1274 #endif 1275 1276 #ifdef USE_MBEDTLS_ECC_P256 1277 mbedtls_ecp_point Q; 1278 mbedtls_ecp_point_init( &Q ); 1279 mbedtls_mpi_read_binary(&Q.X, &public_key[0], 32); 1280 mbedtls_mpi_read_binary(&Q.Y, &public_key[32], 32); 1281 mbedtls_mpi_lset(&Q.Z, 1); 1282 err = mbedtls_ecp_check_pubkey(&mbedtls_ec_group, &Q); 1283 mbedtls_ecp_point_free( & Q); 1284 #endif 1285 1286 if (err){ 1287 log_error("public key invalid %x", err); 1288 } 1289 return err; 1290 } 1291 #endif 1292 1293 void btstack_crypto_ccm_init(btstack_crypto_ccm_t * request, const uint8_t * key, const uint8_t * nonce, uint16_t message_len, uint16_t additional_authenticated_data_len, uint8_t auth_len){ 1294 request->key = key; 1295 request->nonce = nonce; 1296 request->message_len = message_len; 1297 request->aad_len = additional_authenticated_data_len; 1298 request->aad_offset = 0; 1299 request->auth_len = auth_len; 1300 request->counter = 1; 1301 request->state = CCM_CALCULATE_X1; 1302 } 1303 1304 void btstack_crypto_ccm_digest(btstack_crypto_ccm_t * request, uint8_t * additional_authenticated_data, uint16_t additional_authenticated_data_len, void (* callback)(void * arg), void * callback_arg){ 1305 // not implemented yet 1306 request->btstack_crypto.context_callback.callback = callback; 1307 request->btstack_crypto.context_callback.context = callback_arg; 1308 request->btstack_crypto.operation = BTSTACK_CRYPTO_CCM_DIGEST_BLOCK; 1309 request->block_len = additional_authenticated_data_len; 1310 request->input = additional_authenticated_data; 1311 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1312 btstack_crypto_run(); 1313 } 1314 1315 void btstack_crypto_ccm_get_authentication_value(btstack_crypto_ccm_t * request, uint8_t * authentication_value){ 1316 (void)memcpy(authentication_value, request->x_i, request->auth_len); 1317 } 1318 1319 void btstack_crypto_ccm_encrypt_block(btstack_crypto_ccm_t * request, uint16_t block_len, const uint8_t * plaintext, uint8_t * ciphertext, void (* callback)(void * arg), void * callback_arg){ 1320 #ifdef DEBUG_CCM 1321 printf("\nbtstack_crypto_ccm_encrypt_block, len %u\n", block_len); 1322 #endif 1323 request->btstack_crypto.context_callback.callback = callback; 1324 request->btstack_crypto.context_callback.context = callback_arg; 1325 request->btstack_crypto.operation = BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK; 1326 request->block_len = block_len; 1327 request->input = plaintext; 1328 request->output = ciphertext; 1329 if (request->state != CCM_CALCULATE_X1){ 1330 request->state = CCM_CALCULATE_XN; 1331 } 1332 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1333 btstack_crypto_run(); 1334 } 1335 1336 void btstack_crypto_ccm_decrypt_block(btstack_crypto_ccm_t * request, uint16_t block_len, const uint8_t * ciphertext, uint8_t * plaintext, void (* callback)(void * arg), void * callback_arg){ 1337 request->btstack_crypto.context_callback.callback = callback; 1338 request->btstack_crypto.context_callback.context = callback_arg; 1339 request->btstack_crypto.operation = BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK; 1340 request->block_len = block_len; 1341 request->input = ciphertext; 1342 request->output = plaintext; 1343 if (request->state != CCM_CALCULATE_X1){ 1344 request->state = CCM_CALCULATE_SN; 1345 } 1346 btstack_linked_list_add_tail(&btstack_crypto_operations, (btstack_linked_item_t*) request); 1347 btstack_crypto_run(); 1348 } 1349 1350 // PTS only 1351 void btstack_crypto_ecc_p256_set_key(const uint8_t * public_key, const uint8_t * private_key){ 1352 #ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION 1353 (void)memcpy(btstack_crypto_ecc_p256_d, private_key, 32); 1354 (void)memcpy(btstack_crypto_ecc_p256_public_key, public_key, 64); 1355 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_DONE; 1356 #else 1357 UNUSED(public_key); 1358 UNUSED(private_key); 1359 #endif 1360 } 1361 // Unit testing 1362 int btstack_crypto_idle(void){ 1363 return btstack_linked_list_empty(&btstack_crypto_operations); 1364 } 1365 void btstack_crypto_reset(void){ 1366 btstack_crypto_operations = NULL; 1367 btstack_crypto_wait_for_hci_result = 0; 1368 } 1369