diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 4311358c..848e3865 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -52,9 +52,6 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e _peer_addr = evt_connected->peer_addr; _role = evt_connected->role; - _ediv = 0xFFFF; - _bond_keys = NULL; - _hvn_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_qsize); _wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize); @@ -62,6 +59,10 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e _hvc_sem = NULL; _hvc_received = false; _pair_sem = NULL; + + _ediv = 0xFFFF; + _bond_keys = NULL; + _peer_pubkey = NULL; } BLEConnection::~BLEConnection() @@ -69,10 +70,12 @@ BLEConnection::~BLEConnection() vSemaphoreDelete( _hvn_sem ); vSemaphoreDelete( _wrcmd_sem ); + //------------- on-the-fly data must be freed -------------// if (_hvc_sem ) vSemaphoreDelete(_hvc_sem ); if (_pair_sem ) vSemaphoreDelete(_pair_sem); - if (_bond_keys ) rtos_free(_bond_keys); + if (_bond_keys ) rtos_free(_bond_keys); + if (_peer_pubkey ) rtos_free(_peer_pubkey); } uint16_t BLEConnection::handle (void) diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.h b/libraries/Bluefruit52Lib/src/BLEConnection.h index 4be26b83..b22180ca 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.h +++ b/libraries/Bluefruit52Lib/src/BLEConnection.h @@ -64,9 +64,10 @@ class BLEConnection // On-demand semaphore/data that are created on the fly SemaphoreHandle_t _hvc_sem; - SemaphoreHandle_t _pair_sem; - bond_keys_t* _bond_keys; // Shared keys with bonded device, size ~ 80 bytes + + bond_keys_t* _bond_keys; // Shared keys with bonded device, size ~ 80 bytes + uint8_t* _peer_pubkey; // LESC Peer public key public: BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const * evt_connected, uint8_t hvn_qsize, uint8_t wrcmd_qsize); @@ -111,6 +112,8 @@ class BLEConnection * Although declare as public, it is meant to be invoked by internal code. *------------------------------------------------------------------*/ void _eventHandler(ble_evt_t* evt); + + friend class BLEPairing; }; diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index c74b0257..0ae1e921 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -71,8 +71,6 @@ BLEPairing::BLEPairing(void) _display_cb = NULL; _complete_cb = NULL; - -// _peer_pubkey_raw = NULL; } bool BLEPairing::begin(void) @@ -232,10 +230,10 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) VERIFY(_bond_keys, ); memclr(_bond_keys, sizeof(bond_keys_t)); - // storing peer public key -// if (_peer_pubkey_raw) rtos_free(_peer_pubkey_raw); -// _peer_pubkey_raw = (uint8_t*) rtos_malloc(1+64); -// VERIFY(_peer_pubkey_raw, ); + // storing peer public key in connection + if (conn->_peer_pubkey) rtos_free(conn->_peer_pubkey); + conn->_peer_pubkey = (uint8_t*) rtos_malloc(1+BLE_GAP_LESC_P256_PK_LEN); + VERIFY(conn->_peer_pubkey, ); _ediv = EDIV_INVALID; @@ -261,7 +259,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) .p_enc_key = &_bond_keys->peer_enc, .p_id_key = &_bond_keys->peer_id, .p_sign_key = NULL, - .p_pk = (ble_gap_lesc_p256_pk_t*) (_peer_pubkey_raw+1) + .p_pk = (ble_gap_lesc_p256_pk_t*) (conn->_peer_pubkey+1) } }; @@ -302,31 +300,32 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) } // Raw public key from peer device is uncompressed - // _peer_pubkey_raw is dhkey_req->p_pk_peer->pk - _peer_pubkey_raw[0] = CRYS_EC_PointUncompressed; + // _peer_pubkey + 1 == dhkey_req->p_pk_peer->pk + uint8_t* peer_pubkey = conn->_peer_pubkey; + peer_pubkey[0] = CRYS_EC_PointUncompressed; // Swap Endian data from air - swap_endian(_peer_pubkey_raw+1 , 32); - swap_endian(_peer_pubkey_raw+1+32, 32); + swap_endian(peer_pubkey+1 , 32); + swap_endian(peer_pubkey+1+32, 32); // Create nRFCrypto pubkey from raw format - nRFCrypto_ECC_PublicKey peer_pubkey; - peer_pubkey.begin(CRYS_ECPKI_DomainID_secp256r1); + nRFCrypto_ECC_PublicKey peerPublickKey; + peerPublickKey.begin(CRYS_ECPKI_DomainID_secp256r1); - peer_pubkey.fromRaw(_peer_pubkey_raw, 1+64); + peerPublickKey.fromRaw(peer_pubkey, 1+BLE_GAP_LESC_P256_PK_LEN); // Create shared secret derivation primitive using ECC Diffie-Hellman ble_gap_lesc_dhkey_t dhkey; - nRFCrypto_ECC::SVDP_DH(_private_key, peer_pubkey, dhkey.key, sizeof(dhkey.key)); + nRFCrypto_ECC::SVDP_DH(_private_key, peerPublickKey, dhkey.key, sizeof(dhkey.key)); - peer_pubkey.end(); + peerPublickKey.end(); // Swap Endian before sending to air swap_endian(dhkey.key, 32); // Swap back the peer pubkey since SoftDevice still need this until Authentication is complete - swap_endian(_peer_pubkey_raw+1 , 32); - swap_endian(_peer_pubkey_raw+1+32, 32); + swap_endian(peer_pubkey+1 , 32); + swap_endian(peer_pubkey+1+32, 32); sd_ble_gap_lesc_dhkey_reply(conn_hdl, &dhkey); } @@ -340,8 +339,8 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) LOG_LV2("PAIR", "Auth Status = 0x%02X, Bonded = %d, LESC = %d, Our Kdist = 0x%02X, Peer Kdist = 0x%02X ", status->auth_status, status->bonded, status->lesc, *((uint8_t*) &status->kdist_own), *((uint8_t*) &status->kdist_peer)); -// rtos_free(_peer_pubkey_raw); -// _peer_pubkey_raw = NULL; + rtos_free(conn->_peer_pubkey); + conn->_peer_pubkey = NULL; // Pairing succeeded --> save encryption keys ( Bonding ) if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.h b/libraries/Bluefruit52Lib/src/BLEPairing.h index 84270e86..9c716cb9 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.h +++ b/libraries/Bluefruit52Lib/src/BLEPairing.h @@ -65,9 +65,7 @@ class BLEPairing nRFCrypto_ECC_PrivateKey _private_key; - uint8_t _pubkey_raw[1+64]; // raw key: 1 header + 64 data - - uint8_t _peer_pubkey_raw[1+64]; // filled by SoftDevice + uint8_t _pubkey_raw[1+BLE_GAP_LESC_P256_PK_LEN]; // raw key: 1 header + 64 data uint16_t _ediv; bond_keys_t* _bond_keys; // Shared keys with bonded device, size ~ 80 bytes