From fbb926d77e2d7b44c6b61ec1bf141039ee30002c Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Fri, 3 Aug 2018 15:28:41 +0100 Subject: [PATCH 1/5] call the correct ltk function when SC is used --- .../source/nRF5xPalSecurityManager.cpp | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp index 49ea2de83d..3c49c922c9 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp @@ -846,11 +846,19 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) const ble_gap_evt_sec_info_request_t& req = gap_evt.params.sec_info_request; - handler->on_ltk_request( - connection, - ediv_t((uint8_t*)(&req.master_id.ediv)), - rand_t(req.master_id.rand) - ); + uint8_t invalid_rand[BLE_GAP_SEC_RAND_LEN] = { 0 }; + if (req.master_id.ediv == 0 && + memcmp(req.master_id.rand, invalid_rand, sizeof(invalid_rand) == 0) + ) { + // request ltk generated with secure connection + handler->on_ltk_request(connection); + } else { + handler->on_ltk_request( + connection, + ediv_t((uint8_t*)(&req.master_id.ediv)), + rand_t(req.master_id.rand) + ); + } return true; } @@ -948,34 +956,41 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) peer_dist = pairing_cb->initiator_dist; } - if (own_dist.get_encryption()) { - handler->on_keys_distributed_local_ltk( + if (status.lesc) { + handler->on_secure_connections_ltk_generated( connection, ltk_t(pairing_cb->own_enc_key.enc_info.ltk) ); + } else { + if (own_dist.get_encryption()) { + handler->on_keys_distributed_local_ltk( + connection, + ltk_t(pairing_cb->own_enc_key.enc_info.ltk) + ); - handler->on_keys_distributed_local_ediv_rand( - connection, - ediv_t(reinterpret_cast( - &pairing_cb->own_enc_key.master_id.ediv - )), - pairing_cb->own_enc_key.master_id.rand - ); - } + handler->on_keys_distributed_local_ediv_rand( + connection, + ediv_t(reinterpret_cast( + &pairing_cb->own_enc_key.master_id.ediv + )), + pairing_cb->own_enc_key.master_id.rand + ); + } - if (peer_dist.get_encryption()) { - handler->on_keys_distributed_ltk( - connection, - ltk_t(pairing_cb->peer_enc_key.enc_info.ltk) - ); + if (peer_dist.get_encryption()) { + handler->on_keys_distributed_ltk( + connection, + ltk_t(pairing_cb->peer_enc_key.enc_info.ltk) + ); - handler->on_keys_distributed_ediv_rand( - connection, - ediv_t(reinterpret_cast( - &pairing_cb->peer_enc_key.master_id.ediv - )), - pairing_cb->peer_enc_key.master_id.rand - ); + handler->on_keys_distributed_ediv_rand( + connection, + ediv_t(reinterpret_cast( + &pairing_cb->peer_enc_key.master_id.ediv + )), + pairing_cb->peer_enc_key.master_id.rand + ); + } } if (peer_dist.get_identity()) { From 021c452823830264d36ad9f6aaef2ae5f8e7c847 Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Mon, 6 Aug 2018 10:34:42 +0100 Subject: [PATCH 2/5] replaced lesc field usage with ediv&rand check --- .../TARGET_NRF52/source/nRF5xPalSecurityManager.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp index 3c49c922c9..b9bcc4f44f 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp @@ -956,7 +956,10 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) peer_dist = pairing_cb->initiator_dist; } - if (status.lesc) { + uint8_t invalid_rand[BLE_GAP_SEC_RAND_LEN] = { 0 }; + if (pairing_cb->own_enc_key.master_id.ediv == 0 && + memcmp(pairing_cb->own_enc_key.master_id.rand, invalid_rand, sizeof(invalid_rand) == 0) + ) { handler->on_secure_connections_ltk_generated( connection, ltk_t(pairing_cb->own_enc_key.enc_info.ltk) From c1ce511dff26c5a43d6e98530b87e9213fa4ad0e Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Wed, 8 Aug 2018 14:19:06 +0100 Subject: [PATCH 3/5] factor out invalid rand function --- .../source/nRF5xPalSecurityManager.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp index b9bcc4f44f..997f8e7d11 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp @@ -760,6 +760,16 @@ nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager() return _security_manager; } +bool is_rand_invalid(const uint8_t* rand) +{ + for (int i = 0; i < 8; ++i) { + if (rand[i]) { + return false; + } + } + return true; +} + bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) { nRF5xSecurityManager& self = nRF5xSecurityManager::get_security_manager(); @@ -846,9 +856,8 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) const ble_gap_evt_sec_info_request_t& req = gap_evt.params.sec_info_request; - uint8_t invalid_rand[BLE_GAP_SEC_RAND_LEN] = { 0 }; if (req.master_id.ediv == 0 && - memcmp(req.master_id.rand, invalid_rand, sizeof(invalid_rand) == 0) + is_rand_invalid(req.master_id.rand) ) { // request ltk generated with secure connection handler->on_ltk_request(connection); @@ -956,9 +965,8 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) peer_dist = pairing_cb->initiator_dist; } - uint8_t invalid_rand[BLE_GAP_SEC_RAND_LEN] = { 0 }; if (pairing_cb->own_enc_key.master_id.ediv == 0 && - memcmp(pairing_cb->own_enc_key.master_id.rand, invalid_rand, sizeof(invalid_rand) == 0) + is_rand_invalid(pairing_cb->own_enc_key.master_id.rand) ) { handler->on_secure_connections_ltk_generated( connection, From cbf1776f14ba9f748d4ad4cb7886f8d4f573d0c4 Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Wed, 8 Aug 2018 14:26:31 +0100 Subject: [PATCH 4/5] MACRO the size of rand --- .../TARGET_NRF52/source/nRF5xPalSecurityManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp index 997f8e7d11..c4e5d2f867 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp @@ -762,7 +762,7 @@ nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager() bool is_rand_invalid(const uint8_t* rand) { - for (int i = 0; i < 8; ++i) { + for (int i = 0; i < BLE_GAP_SEC_RAND_LEN; ++i) { if (rand[i]) { return false; } From 6c9bd163b1c3d699e5d5c99ecfd113f2a06b75ed Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Wed, 8 Aug 2018 17:02:43 +0100 Subject: [PATCH 5/5] factored out whole ediv rand check --- .../source/nRF5xPalSecurityManager.cpp | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp index c4e5d2f867..5eb1310bb4 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp @@ -760,14 +760,22 @@ nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager() return _security_manager; } -bool is_rand_invalid(const uint8_t* rand) +/** + * EDIV and Rand are invalid if both are zero + */ +bool is_ediv_rand_valid(const uint16_t ediv, const uint8_t* rand) { for (int i = 0; i < BLE_GAP_SEC_RAND_LEN; ++i) { if (rand[i]) { - return false; + return true; } } - return true; + + if (ediv != 0) { + return true; + } + + return false; } bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) @@ -856,17 +864,16 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) const ble_gap_evt_sec_info_request_t& req = gap_evt.params.sec_info_request; - if (req.master_id.ediv == 0 && - is_rand_invalid(req.master_id.rand) - ) { - // request ltk generated with secure connection - handler->on_ltk_request(connection); - } else { + if (is_ediv_rand_valid(req.master_id.ediv, req.master_id.rand)) { handler->on_ltk_request( connection, ediv_t((uint8_t*)(&req.master_id.ediv)), rand_t(req.master_id.rand) ); + } else { + /* no valid EDIV and Rand + * request ltk generated with secure connection */ + handler->on_ltk_request(connection); } return true; @@ -965,27 +972,24 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) peer_dist = pairing_cb->initiator_dist; } - if (pairing_cb->own_enc_key.master_id.ediv == 0 && - is_rand_invalid(pairing_cb->own_enc_key.master_id.rand) + if (is_ediv_rand_valid( + pairing_cb->own_enc_key.master_id.ediv, + pairing_cb->own_enc_key.master_id.rand + ) ) { - handler->on_secure_connections_ltk_generated( - connection, - ltk_t(pairing_cb->own_enc_key.enc_info.ltk) - ); - } else { if (own_dist.get_encryption()) { - handler->on_keys_distributed_local_ltk( - connection, - ltk_t(pairing_cb->own_enc_key.enc_info.ltk) - ); + handler->on_keys_distributed_local_ltk( + connection, + ltk_t(pairing_cb->own_enc_key.enc_info.ltk) + ); - handler->on_keys_distributed_local_ediv_rand( - connection, - ediv_t(reinterpret_cast( - &pairing_cb->own_enc_key.master_id.ediv - )), - pairing_cb->own_enc_key.master_id.rand - ); + handler->on_keys_distributed_local_ediv_rand( + connection, + ediv_t(reinterpret_cast( + &pairing_cb->own_enc_key.master_id.ediv + )), + pairing_cb->own_enc_key.master_id.rand + ); } if (peer_dist.get_encryption()) { @@ -1002,6 +1006,13 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) pairing_cb->peer_enc_key.master_id.rand ); } + } else { + /* no valid EDIV and Rand meaning this is a + * Secure Connections key */ + handler->on_secure_connections_ltk_generated( + connection, + ltk_t(pairing_cb->own_enc_key.enc_info.ltk) + ); } if (peer_dist.get_identity()) {