@@ -760,14 +760,22 @@ nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager()
760
760
return _security_manager;
761
761
}
762
762
763
- bool is_rand_invalid (const uint8_t * rand)
763
+ /* *
764
+ * EDIV and Rand are invalid if both are zero
765
+ */
766
+ bool is_ediv_rand_valid (const uint16_t ediv, const uint8_t * rand)
764
767
{
765
768
for (int i = 0 ; i < BLE_GAP_SEC_RAND_LEN; ++i) {
766
769
if (rand[i]) {
767
- return false ;
770
+ return true ;
768
771
}
769
772
}
770
- return true ;
773
+
774
+ if (ediv != 0 ) {
775
+ return true ;
776
+ }
777
+
778
+ return false ;
771
779
}
772
780
773
781
bool nRF5xSecurityManager::sm_handler (const ble_evt_t *evt)
@@ -856,17 +864,16 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
856
864
const ble_gap_evt_sec_info_request_t & req =
857
865
gap_evt.params .sec_info_request ;
858
866
859
- if (req.master_id .ediv == 0 &&
860
- is_rand_invalid (req.master_id .rand )
861
- ) {
862
- // request ltk generated with secure connection
863
- handler->on_ltk_request (connection);
864
- } else {
867
+ if (is_ediv_rand_valid (req.master_id .ediv , req.master_id .rand )) {
865
868
handler->on_ltk_request (
866
869
connection,
867
870
ediv_t ((uint8_t *)(&req.master_id .ediv )),
868
871
rand_t (req.master_id .rand )
869
872
);
873
+ } else {
874
+ /* no valid EDIV and Rand
875
+ * request ltk generated with secure connection */
876
+ handler->on_ltk_request (connection);
870
877
}
871
878
872
879
return true ;
@@ -965,27 +972,24 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
965
972
peer_dist = pairing_cb->initiator_dist ;
966
973
}
967
974
968
- if (pairing_cb->own_enc_key .master_id .ediv == 0 &&
969
- is_rand_invalid (pairing_cb->own_enc_key .master_id .rand )
975
+ if (is_ediv_rand_valid (
976
+ pairing_cb->own_enc_key .master_id .ediv ,
977
+ pairing_cb->own_enc_key .master_id .rand
978
+ )
970
979
) {
971
- handler->on_secure_connections_ltk_generated (
972
- connection,
973
- ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
974
- );
975
- } else {
976
980
if (own_dist.get_encryption ()) {
977
- handler->on_keys_distributed_local_ltk (
978
- connection,
979
- ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
980
- );
981
-
982
- handler->on_keys_distributed_local_ediv_rand (
983
- connection,
984
- ediv_t (reinterpret_cast <uint8_t *>(
985
- &pairing_cb->own_enc_key .master_id .ediv
986
- )),
987
- pairing_cb->own_enc_key .master_id .rand
988
- );
981
+ handler->on_keys_distributed_local_ltk (
982
+ connection,
983
+ ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
984
+ );
985
+
986
+ handler->on_keys_distributed_local_ediv_rand (
987
+ connection,
988
+ ediv_t (reinterpret_cast <uint8_t *>(
989
+ &pairing_cb->own_enc_key .master_id .ediv
990
+ )),
991
+ pairing_cb->own_enc_key .master_id .rand
992
+ );
989
993
}
990
994
991
995
if (peer_dist.get_encryption ()) {
@@ -1002,6 +1006,13 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
1002
1006
pairing_cb->peer_enc_key .master_id .rand
1003
1007
);
1004
1008
}
1009
+ } else {
1010
+ /* no valid EDIV and Rand meaning this is a
1011
+ * Secure Connections key */
1012
+ handler->on_secure_connections_ltk_generated (
1013
+ connection,
1014
+ ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
1015
+ );
1005
1016
}
1006
1017
1007
1018
if (peer_dist.get_identity ()) {
0 commit comments