getting passed the LESC DHKEY
This commit is contained in:
1 parent
6f03286dd9
commit
4402af2139
4 files changed
+125
-15
No files matched your search
@@ -157,8 +157,11 @@ 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 ": ");\
|
||||
for(uint32_t i=0; i<(n); i++) PRINTF("%02x ", p8[i]);\
|
||||
PRINTF(#buf ": \n");\
|
||||
for(uint32_t i=0; i<(n); i++) {\
|
||||
if (i%16 == 0) PRINTF("\n"); \
|
||||
PRINTF("%02x ", p8[i]); \
|
||||
}\
|
||||
PRINTF("\n");\
|
||||
}while(0)
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <bluefruit.h>
|
||||
#include <Adafruit_LittleFS.h>
|
||||
#include <InternalFileSystem.h>
|
||||
#include <Adafruit_nRFCrypto.h>
|
||||
|
||||
/* This sketch demonstrates Pairing process using static Passkey aka PIN.
|
||||
* This sketch is essentially the same as bleuart.ino except the BLE Uart
|
||||
@@ -84,6 +85,7 @@ BLEUart bleuart; // uart over ble
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while(!Serial) delay(1);
|
||||
|
||||
#if TFT_IN_USE == TFT_GIZMO
|
||||
tft.init(240, 240);
|
||||
|
||||
@@ -36,6 +36,20 @@
|
||||
|
||||
#include "bluefruit.h"
|
||||
|
||||
|
||||
#ifdef NRF_CRYPTOCELL
|
||||
#include "Adafruit_nRFCrypto.h"
|
||||
|
||||
extern nRFCrypto_ECC _ecpki;
|
||||
extern nRFCrypto_ECC_PrivateKey _private_key;
|
||||
extern nRFCrypto_ECC_PublicKey _public_key;
|
||||
|
||||
extern uint8_t _lesc_public_rawkey[1+64]; // 1 byte for header
|
||||
static ble_gap_lesc_p256_pk_t _lesc_peer_pubkey;
|
||||
|
||||
extern void swap_key_endian(uint8_t rawkey[]);
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -304,10 +318,11 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
/* First-time Pairing
|
||||
* BLE_GAP_EVT_LESC_DHKEY_REQUEST
|
||||
* Connect -> SEC_PARAMS_REQUEST -> PASSKEY_DISPLAY -> CONN_SEC_UPDATE -> AUTH_STATUS
|
||||
* 1. Either we or peer initiate the process
|
||||
* 2. Peer ask for Secure Parameter ( I/O Caps ) BLE_GAP_EVT_SEC_PARAMS_REQUEST
|
||||
* 3. Pair Key exchange ( PIN code)
|
||||
* 3. Pair PassKey exchange
|
||||
* 4. Connection is secured BLE_GAP_EVT_CONN_SEC_UPDATE
|
||||
* 5. Long term Keys exchanged BLE_GAP_EVT_AUTH_STATUS
|
||||
*
|
||||
@@ -342,23 +357,56 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
|
||||
.p_enc_key = &_bond_keys->own_enc,
|
||||
.p_id_key = NULL,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
.p_pk = (ble_gap_lesc_p256_pk_t*) (_lesc_public_rawkey+1)
|
||||
},
|
||||
|
||||
.keys_peer = {
|
||||
.p_enc_key = &_bond_keys->peer_enc,
|
||||
.p_id_key = &_bond_keys->peer_id,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
.p_pk = &_lesc_peer_pubkey
|
||||
}
|
||||
};
|
||||
|
||||
ble_gap_sec_params_t sec_param = Bluefruit.getSecureParam();
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(_conn_hdl,
|
||||
BLE_GAP_SEC_STATUS_SUCCESS,
|
||||
_role == BLE_GAP_ROLE_PERIPH ? &sec_param : NULL,
|
||||
&keyset),
|
||||
);
|
||||
|
||||
// use LESC when both support it
|
||||
if ( peer->lesc && sec_param.lesc )
|
||||
{
|
||||
// keyset.
|
||||
}
|
||||
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(_conn_hdl, BLE_GAP_SEC_STATUS_SUCCESS,
|
||||
_role == BLE_GAP_ROLE_PERIPH ? &sec_param : NULL, &keyset), );
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
|
||||
{
|
||||
ble_gap_evt_lesc_dhkey_request_t* dhkey_req = &evt->evt.gap_evt.params.lesc_dhkey_request;
|
||||
|
||||
if ( dhkey_req->oobd_req )
|
||||
{
|
||||
// Out of Band not supported yet
|
||||
}
|
||||
|
||||
uint8_t peer_raw_pubkey[64+1];
|
||||
ble_gap_lesc_dhkey_t dhkey;
|
||||
|
||||
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);
|
||||
|
||||
nRFCrypto_ECC_PublicKey peer_pubkey;
|
||||
peer_pubkey.begin(CRYS_ECPKI_DomainID_secp256r1);
|
||||
peer_pubkey.fromRaw(peer_raw_pubkey, sizeof(peer_raw_pubkey));
|
||||
|
||||
nRFCrypto_ECC::SVDP_DH(_private_key, peer_pubkey, dhkey.key, sizeof(dhkey.key));
|
||||
swap_key_endian(dhkey.key);
|
||||
|
||||
sd_ble_gap_lesc_dhkey_reply(_conn_hdl, &dhkey);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@@ -37,6 +37,32 @@
|
||||
#include "bluefruit.h"
|
||||
#include "utility/bonding.h"
|
||||
|
||||
#ifdef NRF_CRYPTOCELL
|
||||
#include "Adafruit_nRFCrypto.h"
|
||||
|
||||
nRFCrypto_ECC _ecpki;
|
||||
nRFCrypto_ECC_PrivateKey _private_key;
|
||||
nRFCrypto_ECC_PublicKey _public_key;
|
||||
|
||||
uint8_t _lesc_public_rawkey[1+64]; // 1 byte for header
|
||||
|
||||
// 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[])
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef CFG_BLE_TX_POWER_LEVEL
|
||||
#define CFG_BLE_TX_POWER_LEVEL 0
|
||||
#endif
|
||||
@@ -113,6 +139,8 @@ static void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info)
|
||||
{
|
||||
typedef struct
|
||||
{
|
||||
// convert from Big to Little Endian to use with BLE
|
||||
// Public Key = 32-byte N1 + 32-byte N2
|
||||
uint16_t line_num; /**< The line number where the error occurred. */
|
||||
uint8_t const * p_file_name; /**< The file in which the error occurred. */
|
||||
} assert_info_t;
|
||||
@@ -176,6 +204,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_event_cb = NULL;
|
||||
_rssi_cb = NULL;
|
||||
_pair_display_cb = NULL;
|
||||
_pair_complete_cb = NULL;
|
||||
|
||||
_sec_param = ((ble_gap_sec_params_t)
|
||||
{
|
||||
@@ -190,6 +219,10 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
.kdist_own = { .enc = 1, .id = 1},
|
||||
.kdist_peer = { .enc = 1, .id = 1},
|
||||
});
|
||||
|
||||
#ifdef NRF_CRYPTOCELL
|
||||
// _sec_param.lesc = 1; // enable LESC if CryptoCell is present
|
||||
#endif
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::configServiceChanged(bool changed)
|
||||
@@ -489,6 +522,26 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
|
||||
// Initialize bonding
|
||||
bond_init();
|
||||
|
||||
// Initalize Crypto lib for LESC
|
||||
#ifdef NRF_CRYPTOCELL
|
||||
nRFCrypto.begin();
|
||||
_ecpki.begin();
|
||||
|
||||
_private_key.begin(CRYS_ECPKI_DomainID_secp256r1);
|
||||
_public_key.begin(CRYS_ECPKI_DomainID_secp256r1);
|
||||
|
||||
nRFCrypto_ECC::genKeyPair(_private_key, _public_key);
|
||||
|
||||
_public_key.toRaw(_lesc_public_rawkey, sizeof(_lesc_public_rawkey));
|
||||
// PRINT_BUFFER(_lesc_public_rawkey+1, sizeof(_lesc_public_rawkey)-1);
|
||||
|
||||
// Public Key = 32-byte N1 + 32-byte N2
|
||||
swap_key_endian(_lesc_public_rawkey+1);
|
||||
swap_key_endian(_lesc_public_rawkey+1+32);
|
||||
|
||||
// PRINT_BUFFER(_lesc_public_rawkey+1, sizeof(_lesc_public_rawkey)-1);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -662,7 +715,7 @@ bool AdafruitBluefruit::setPIN(const char* pin)
|
||||
{
|
||||
_sec_param.bond = 1;
|
||||
_sec_param.mitm = 0;
|
||||
_sec_param.lesc = 0;
|
||||
_sec_param.lesc = 0; // TODO NRF_CRYPTOCELL
|
||||
_sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
|
||||
}else
|
||||
{
|
||||
@@ -697,8 +750,12 @@ bool AdafruitBluefruit::setPairingDisplayCallback(pair_display_cb_t fp)
|
||||
{
|
||||
_sec_param.bond = 1;
|
||||
_sec_param.mitm = 1;
|
||||
_sec_param.lesc = 0;
|
||||
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
|
||||
|
||||
// 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_NONE;
|
||||
|
||||
ble_opt_t opt;
|
||||
opt.gap_opt.passkey.p_passkey = NULL; // generate Passkey randomly
|
||||
@@ -716,7 +773,7 @@ void AdafruitBluefruit::setPairingCompleteCallback(pair_complete_cb_t fp)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Thread & SoftDevice Event handler
|
||||
*------------------------------------------------------------------*/
|
||||
void SD_EVT_IRQHandler(void)
|
||||
extern "C" void SD_EVT_IRQHandler(void)
|
||||
{
|
||||
// Notify both BLE & SOC Task
|
||||
xSemaphoreGiveFromISR(Bluefruit._soc_event_sem, NULL);
|
||||
@@ -1038,7 +1095,7 @@ void Bluefruit_printInfo(void)
|
||||
|
||||
void AdafruitBluefruit::printInfo(void)
|
||||
{
|
||||
// Skip if Serial is not initialised
|
||||
// Skip if Serial is not initialized
|
||||
if ( !Serial ) return;
|
||||
|
||||
// Skip if Bluefruit.begin() is not called
|
||||
|
||||
Reference in new issue
Block a user