diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c index b850f2411..785ee1054 100644 --- a/nimble/host/src/ble_sm_alg.c +++ b/nimble/host/src/ble_sm_alg.c @@ -413,31 +413,36 @@ ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y, int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, - uint32_t *our_priv_key, void *out_dhkey) + uint8_t *our_priv_key, uint8_t *out_dhkey) { - uint32_t dh[8]; - EccPoint pk; + uint8_t dh[32]; + uint8_t pk[64]; + uint8_t priv[32]; + int rc; - memcpy(pk.x, peer_pub_key_x, 32); - memcpy(pk.y, peer_pub_key_y, 32); + swap_buf(pk, peer_pub_key_x, 32); + swap_buf(&pk[32], peer_pub_key_y, 32); + swap_buf(priv, our_priv_key, 32); - if (ecc_valid_public_key(&pk) < 0) { + if (uECC_valid_public_key(pk, &curve_secp256r1) < 0) { return BLE_HS_EUNKNOWN; } - if (ecdh_shared_secret(dh, &pk, our_priv_key) == TC_CRYPTO_FAIL) { + rc = uECC_shared_secret(pk, priv, dh, &curve_secp256r1); + if (rc == TC_CRYPTO_FAIL) { return BLE_HS_EUNKNOWN; } - memcpy(out_dhkey, dh, 32); + swap_buf(out_dhkey, dh, 32); return 0; } /* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */ -static const uint32_t ble_sm_alg_dbg_priv_key[8] = { - 0xcd3c1abd, 0x5899b8a6, 0xeb40b799, 0x4aff607b, 0xd2103f50, 0x74c9b3e3, - 0xa3c55f38, 0x3f49f6d4 +static const uint8_t ble_sm_alg_dbg_priv_key[32] = { + 0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3, + 0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99, + 0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd }; /** @@ -445,31 +450,35 @@ static const uint32_t ble_sm_alg_dbg_priv_key[8] = { * priv: 32 bytes */ int -ble_sm_alg_gen_key_pair(void *pub, uint32_t *priv) +ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) { - uint32_t random[16]; - EccPoint pkey; - int rc; + uint8_t pk[64]; do { - rc = ble_hs_hci_util_rand(random, sizeof random); - if (rc != 0) { - return rc; - } - - rc = ecc_make_key(&pkey, priv, random); - if (rc != TC_CRYPTO_SUCCESS) { + if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) { return BLE_HS_EUNKNOWN; } /* Make sure generated key isn't debug key. */ } while (memcmp(priv, ble_sm_alg_dbg_priv_key, 32) == 0); - memcpy(pub + 0, pkey.x, 32); - memcpy(pub + 32, pkey.y, 32); + swap_buf(pub, pk, 32); + swap_buf(&pub[32], &pk[32], 32); + swap_in_place(priv, 32); return 0; } +/* used by uECC to get random data */ +int +default_CSPRNG(uint8_t *dst, unsigned int size) +{ + if (ble_hs_hci_util_rand(dst, size)) { + return 0; + } + + return 1; +} + #endif #endif diff --git a/nimble/host/src/ble_sm_priv.h b/nimble/host/src/ble_sm_priv.h index 650db6c14..f7547065a 100644 --- a/nimble/host/src/ble_sm_priv.h +++ b/nimble/host/src/ble_sm_priv.h @@ -325,8 +325,8 @@ int ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2, const uint8_t *a1, uint8_t a2t, const uint8_t *a2, uint8_t *check); int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, - uint32_t *our_priv_key, void *out_dhkey); -int ble_sm_alg_gen_key_pair(void *pub, uint32_t *priv); + uint8_t *our_priv_key, uint8_t *out_dhkey); +int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv); void ble_sm_enc_change_rx(struct hci_encrypt_change *evt); void ble_sm_enc_key_refresh_rx(struct hci_encrypt_key_refresh *evt); diff --git a/nimble/host/src/ble_sm_sc.c b/nimble/host/src/ble_sm_sc.c index 8f6bc8526..8e8abefc1 100644 --- a/nimble/host/src/ble_sm_sc.c +++ b/nimble/host/src/ble_sm_sc.c @@ -29,19 +29,8 @@ #define BLE_SM_SC_PASSKEY_BYTES 4 #define BLE_SM_SC_PASSKEY_BITS 20 -/** - * The public and private keys are stored in unions. Some crypto functions - * accept pointers to uint32_t; others accept pointers to uint8_t. The use of - * unions ensures the keys are properly aligned for pointers to uint32_t. - */ -static union { - uint32_t u32[16]; - uint8_t u8[64]; -} ble_sm_sc_pub_key; -static union { - uint32_t u32[8]; - uint8_t u8[32]; -} ble_sm_sc_priv_key; +static uint8_t ble_sm_sc_pub_key[64]; +static uint8_t ble_sm_sc_priv_key[32]; /** * Whether our public-private key pair has been generated. We generate it on @@ -161,7 +150,7 @@ ble_sm_sc_io_action(struct ble_sm_proc *proc, uint8_t *action) } static int -ble_sm_gen_pub_priv(void *pub, uint32_t *priv) +ble_sm_gen_pub_priv(uint8_t *pub, uint8_t *priv) { int rc; @@ -188,8 +177,7 @@ ble_sm_sc_ensure_keys_generated(void) int rc; if (!ble_sm_sc_keys_generated) { - rc = ble_sm_gen_pub_priv(ble_sm_sc_pub_key.u32, - ble_sm_sc_priv_key.u32); + rc = ble_sm_gen_pub_priv(ble_sm_sc_pub_key, ble_sm_sc_priv_key); if (rc != 0) { return rc; } @@ -299,7 +287,7 @@ ble_sm_sc_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) return; } - rc = ble_sm_alg_f4(ble_sm_sc_pub_key.u8, proc->pub_key_peer.x, + rc = ble_sm_alg_f4(ble_sm_sc_pub_key, proc->pub_key_peer.x, ble_sm_our_pair_rand(proc), proc->ri, cmd->value); if (rc != 0) { os_mbuf_free_chain(txom); @@ -329,11 +317,11 @@ ble_sm_sc_gen_numcmp(struct ble_sm_proc *proc, struct ble_sm_result *res) uint8_t *pkb; if (proc->flags & BLE_SM_PROC_F_INITIATOR) { - pka = ble_sm_sc_pub_key.u8; + pka = ble_sm_sc_pub_key; pkb = proc->pub_key_peer.x; } else { pka = proc->pub_key_peer.x; - pkb = ble_sm_sc_pub_key.u8; + pkb = ble_sm_sc_pub_key; } res->app_status = ble_sm_alg_g2(pka, pkb, proc->randm, proc->rands, &res->passkey_params.numcmp); @@ -433,7 +421,7 @@ ble_sm_sc_random_rx(struct ble_sm_proc *proc, struct ble_sm_result *res) ble_hs_log_flat_buf(proc->tk, 16); BLE_HS_LOG(DEBUG, "\n"); - rc = ble_sm_alg_f4(proc->pub_key_peer.x, ble_sm_sc_pub_key.u8, + rc = ble_sm_alg_f4(proc->pub_key_peer.x, ble_sm_sc_pub_key, ble_sm_peer_pair_rand(proc), proc->ri, confirm_val); if (rc != 0) { @@ -525,8 +513,8 @@ ble_sm_sc_public_key_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, return; } - memcpy(cmd->x, ble_sm_sc_pub_key.u8 + 0, 32); - memcpy(cmd->y, ble_sm_sc_pub_key.u8 + 32, 32); + memcpy(cmd->x, ble_sm_sc_pub_key + 0, 32); + memcpy(cmd->y, ble_sm_sc_pub_key + 32, 32); res->app_status = ble_sm_tx(proc->conn_handle, txom); if (res->app_status != 0) { @@ -591,7 +579,7 @@ ble_sm_sc_public_key_rx(uint16_t conn_handle, struct os_mbuf **om, memcpy(&proc->pub_key_peer, cmd, sizeof(*cmd)); rc = ble_sm_alg_gen_dhkey(proc->pub_key_peer.x, proc->pub_key_peer.y, - ble_sm_sc_priv_key.u32, + ble_sm_sc_priv_key, proc->dhkey); if (rc != 0) { res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_DHKEY);