@@ -760,6 +760,24 @@ nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager()
760
760
return _security_manager;
761
761
}
762
762
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)
767
+ {
768
+ for (int i = 0 ; i < BLE_GAP_SEC_RAND_LEN; ++i) {
769
+ if (rand[i]) {
770
+ return true ;
771
+ }
772
+ }
773
+
774
+ if (ediv != 0 ) {
775
+ return true ;
776
+ }
777
+
778
+ return false ;
779
+ }
780
+
763
781
bool nRF5xSecurityManager::sm_handler (const ble_evt_t *evt)
764
782
{
765
783
nRF5xSecurityManager& self = nRF5xSecurityManager::get_security_manager ();
@@ -846,11 +864,17 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
846
864
const ble_gap_evt_sec_info_request_t & req =
847
865
gap_evt.params .sec_info_request ;
848
866
849
- handler->on_ltk_request (
850
- connection,
851
- ediv_t ((uint8_t *)(&req.master_id .ediv )),
852
- rand_t (req.master_id .rand )
853
- );
867
+ if (is_ediv_rand_valid (req.master_id .ediv , req.master_id .rand )) {
868
+ handler->on_ltk_request (
869
+ connection,
870
+ ediv_t ((uint8_t *)(&req.master_id .ediv )),
871
+ rand_t (req.master_id .rand )
872
+ );
873
+ } else {
874
+ /* no valid EDIV and Rand
875
+ * request ltk generated with secure connection */
876
+ handler->on_ltk_request (connection);
877
+ }
854
878
855
879
return true ;
856
880
}
@@ -948,33 +972,46 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
948
972
peer_dist = pairing_cb->initiator_dist ;
949
973
}
950
974
951
- if (own_dist.get_encryption ()) {
952
- handler->on_keys_distributed_local_ltk (
953
- connection,
954
- ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
955
- );
956
-
957
- handler->on_keys_distributed_local_ediv_rand (
958
- connection,
959
- ediv_t (reinterpret_cast <uint8_t *>(
960
- &pairing_cb->own_enc_key .master_id .ediv
961
- )),
975
+ if (is_ediv_rand_valid (
976
+ pairing_cb->own_enc_key .master_id .ediv ,
962
977
pairing_cb->own_enc_key .master_id .rand
963
- );
964
- }
965
-
966
- if (peer_dist.get_encryption ()) {
967
- handler->on_keys_distributed_ltk (
968
- connection,
969
- ltk_t (pairing_cb->peer_enc_key .enc_info .ltk )
970
- );
978
+ )
979
+ ) {
980
+ if (own_dist.get_encryption ()) {
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
+ );
993
+ }
971
994
972
- handler->on_keys_distributed_ediv_rand (
995
+ if (peer_dist.get_encryption ()) {
996
+ handler->on_keys_distributed_ltk (
997
+ connection,
998
+ ltk_t (pairing_cb->peer_enc_key .enc_info .ltk )
999
+ );
1000
+
1001
+ handler->on_keys_distributed_ediv_rand (
1002
+ connection,
1003
+ ediv_t (reinterpret_cast <uint8_t *>(
1004
+ &pairing_cb->peer_enc_key .master_id .ediv
1005
+ )),
1006
+ pairing_cb->peer_enc_key .master_id .rand
1007
+ );
1008
+ }
1009
+ } else {
1010
+ /* no valid EDIV and Rand meaning this is a
1011
+ * Secure Connections key */
1012
+ handler->on_secure_connections_ltk_generated (
973
1013
connection,
974
- ediv_t (reinterpret_cast <uint8_t *>(
975
- &pairing_cb->peer_enc_key .master_id .ediv
976
- )),
977
- pairing_cb->peer_enc_key .master_id .rand
1014
+ ltk_t (pairing_cb->own_enc_key .enc_info .ltk )
978
1015
);
979
1016
}
980
1017
0 commit comments