From 13b66950557c8ae360083d564da71ce718bbf2f2 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 14 Feb 2020 18:19:02 +0700 Subject: [PATCH] more with BLEPairing --- cores/nRF5/common_func.h | 2 +- .../pairing_display/pairing_display.ino | 2 +- .../Bluefruit52Lib/src/BLEConnection.cpp | 10 ++- libraries/Bluefruit52Lib/src/BLEPairing.cpp | 63 ++++++++++++------- libraries/Bluefruit52Lib/src/BLEPairing.h | 9 +-- libraries/Bluefruit52Lib/src/bluefruit.cpp | 4 -- libraries/Bluefruit52Lib/src/bluefruit.h | 5 -- 7 files changed, 55 insertions(+), 40 deletions(-) diff --git a/cores/nRF5/common_func.h b/cores/nRF5/common_func.h index 27be23ee..2a40da28 100644 --- a/cores/nRF5/common_func.h +++ b/cores/nRF5/common_func.h @@ -157,7 +157,7 @@ const char* dbg_err_str(int32_t err_id); // TODO move to other place #define PRINT_BUFFER(buf, n) \ do {\ uint8_t const* p8 = (uint8_t const*) (buf);\ - PRINTF(#buf ": \n");\ + PRINTF(#buf ": ");\ for(uint32_t i=0; i<(n); i++) {\ if (i%16 == 0) PRINTF("\n"); \ PRINTF("%02x ", p8[i]); \ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino b/libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino index cd9279fa..635bdd42 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino @@ -197,7 +197,7 @@ void loop() void pairing_display_callback(uint16_t conn_handle, uint8_t const passkey[6]) { Serial.println("Enter this code on your phone to pair with Bluefruit:"); - Serial.printf(" %.6s\n", passkey); + Serial.printf(" %.3s %.3s\n", passkey, passkey+3); #if TFT_IN_USE != TFT_NONE tft.printf("Enter this code on your phone to pair with Bluefruit:\n\n"); diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 738cfd98..4311358c 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -52,6 +52,9 @@ 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); @@ -66,7 +69,10 @@ BLEConnection::~BLEConnection() vSemaphoreDelete( _hvn_sem ); vSemaphoreDelete( _wrcmd_sem ); - if ( _hvc_sem ) vSemaphoreDelete( _hvc_sem ); + if (_hvc_sem ) vSemaphoreDelete(_hvc_sem ); + if (_pair_sem ) vSemaphoreDelete(_pair_sem); + + if (_bond_keys ) rtos_free(_bond_keys); } uint16_t BLEConnection::handle (void) @@ -228,7 +234,7 @@ bool BLEConnection::requestPairing(void) // skip if already paired if ( _paired ) return true; - ble_gap_sec_params_t sec_param = Bluefruit.getSecureParam(); + ble_gap_sec_params_t sec_param = Bluefruit.Pairing.getSecureParam(); // on-the-fly semaphore _pair_sem = xSemaphoreCreateBinary(); diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index 762030d1..1503d130 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -33,16 +33,16 @@ // convert 32-byte Number from Big <-> Little Endian to use with BLE // Public Key = 32-byte N1 + 32-byte N2 -void swap_key_endian(uint8_t rawkey[]) +static void swap_key_endian(uint8_t key[]) { for(uint8_t i=0; i<32/2; i++) { uint8_t const p1 = i; uint8_t const p2 = (31-i); - uint8_t temp = rawkey[p1]; - rawkey[p1] = rawkey[p2]; - rawkey[p2] = temp; + uint8_t temp = key[p1]; + key[p1] = key[p2]; + key[p2] = temp; } } @@ -62,11 +62,17 @@ BLEPairing::BLEPairing(void) .kdist_peer = { .enc = 1, .id = 1}, }); +#ifdef NRF_CRYPTOCELL +// _sec_param.lesc = 1; // enable LESC if CryptoCell is present +#endif + _ediv = EDIV_INVALID; _bond_keys = NULL; _display_cb = NULL; _complete_cb = NULL; + +// _peer_pubkey_raw = NULL; } bool BLEPairing::begin(void) @@ -87,24 +93,18 @@ bool BLEPairing::begin(void) nRFCrypto_ECC::genKeyPair(_private_key, pubkey); // Convert to Raw bytes to response to LESC event - pubkey.toRaw(_public_key_raw, sizeof(_public_key_raw)); + pubkey.toRaw(_pubkey_raw, sizeof(_pubkey_raw)); pubkey.end(); // BLE use Little Endian, swap public key endian // Public Key = 32-byte N1 + 32-byte N2 - swap_key_endian(_public_key_raw+1); - swap_key_endian(_public_key_raw+1+32); + swap_key_endian(_pubkey_raw+1); + swap_key_endian(_pubkey_raw+1+32); #endif return true; } -uint8_t* BLEPairing::getPublicKey(void) -{ - // skip header byte - return _public_key_raw+1; -} - // Use Legacy SC static Passkey bool BLEPairing::setPIN(const char* pin) { @@ -151,7 +151,6 @@ bool BLEPairing::setDisplayCallback(pair_display_cb_t fp) // TODO NRF_CRYPTOCELL // _sec_param.lesc = 0; -// _sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY; _sec_param.lesc = 1; _sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY; @@ -200,6 +199,11 @@ 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, ); + _ediv = EDIV_INVALID; /* Step 1: Pairing/Bonding @@ -217,7 +221,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) .p_enc_key = &_bond_keys->own_enc, .p_id_key = NULL, .p_sign_key = NULL, - .p_pk = (ble_gap_lesc_p256_pk_t*) (_public_key_raw+1) + .p_pk = (ble_gap_lesc_p256_pk_t*) (_pubkey_raw+1) }, .keys_peer = { @@ -264,22 +268,35 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) // Out of Band not supported yet } - uint8_t peer_raw_pubkey[64+1]; - ble_gap_lesc_dhkey_t dhkey; + // 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_raw_pubkey[0] = CRYS_EC_PointUncompressed; - memcpy(peer_raw_pubkey+1, dhkey_req->p_pk_peer->pk, 64); - - swap_key_endian(peer_raw_pubkey+1); - swap_key_endian(peer_raw_pubkey+1+32); + // Swap Endian data from air + swap_key_endian(_peer_pubkey_raw+1); + swap_key_endian(_peer_pubkey_raw+1+32); + // Create nRFCrypto pubkey from raw format nRFCrypto_ECC_PublicKey peer_pubkey; peer_pubkey.begin(CRYS_ECPKI_DomainID_secp256r1); - peer_pubkey.fromRaw(peer_raw_pubkey, sizeof(peer_raw_pubkey)); + peer_pubkey.fromRaw(_peer_pubkey_raw, 1+64); + + // 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)); + + //peer_pubkey.end(); +// rtos_free(_peer_pubkey_raw); +// _peer_pubkey_raw = NULL; + + // Swap Endian before sending to air swap_key_endian(dhkey.key); + // Swap back the peer pubkey since SoftDevice still need this until Authentication is complete + swap_key_endian(_peer_pubkey_raw+1); + swap_key_endian(_peer_pubkey_raw+1+32); + sd_ble_gap_lesc_dhkey_reply(conn_hdl, &dhkey); } break; diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.h b/libraries/Bluefruit52Lib/src/BLEPairing.h index 028a52f2..e513b5da 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.h +++ b/libraries/Bluefruit52Lib/src/BLEPairing.h @@ -41,7 +41,6 @@ class BLEPairing BLEPairing(void); bool begin(void); - uint8_t* getPublicKey(void); // Static Passkey bool setPIN(const char* pin); @@ -55,16 +54,18 @@ class BLEPairing * Although declare as public, it is meant to be invoked by internal * code. User should not call these directly *------------------------------------------------------------------*/ + ble_gap_sec_params_t getSecureParam(void) { return _sec_param; } void _eventHandler(ble_evt_t* evt); private: ble_gap_sec_params_t _sec_param; - uint8_t _public_key_raw[1+64]; // raw key: 1 header + 64 data - uint8_t _peer_pubkey_raw[1+64]; - 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 + uint16_t _ediv; bond_keys_t* _bond_keys; // Shared keys with bonded device, size ~ 80 bytes diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index fa62e387..bf7bbc1c 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -177,10 +177,6 @@ AdafruitBluefruit::AdafruitBluefruit(void) _event_cb = NULL; _rssi_cb = NULL; - -#ifdef NRF_CRYPTOCELL -// _sec_param.lesc = 1; // enable LESC if CryptoCell is present -#endif } void AdafruitBluefruit::configServiceChanged(bool changed) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 06f8537d..3992d5d5 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -171,11 +171,6 @@ class AdafruitBluefruit BLEConnection* Connection(uint16_t conn_hdl); - //--------------------------------------------------------------------+ - // Security - //--------------------------------------------------------------------+ - ble_gap_sec_params_t getSecureParam(void) { return _sec_param; } - /*------------------------------------------------------------------*/ /* Callbacks *------------------------------------------------------------------*/