From 71b2c1453013731c41d4fe3b610e5043cf8504c0 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 31 Jul 2015 15:55:31 +0200 Subject: [PATCH 1/5] generalize context argument for aes128 and random number generation --- ble/sm.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index 683a64ebb..c647a3dfc 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -149,12 +149,12 @@ static int sm_central_ah_calculation_active; static uint8_t sm_central_device_addr_type; static bd_addr_t sm_central_device_address; -// aes128 crypto engine. store current sm_connection_t in sm_aes128_connection_source +// aes128 crypto engine. store current sm_connection_t in sm_aes128_context static sm_aes128_state_t sm_aes128_state; -static sm_connection_t * sm_aes128_connection_source; +static void * sm_aes128_context; -// random engine. store current sm_connection_t in sm_random -static sm_connection_t * sm_random_connection_source; +// random engine. store context (ususally sm_connection_t) +static void * sm_random_context; // CSRK calculation static sm_connection_t * sm_csrk_connection_source; @@ -351,19 +351,19 @@ static void gap_random_address_update_stop(void){ } -static void sm_random_start(sm_connection_t * sm_conn){ - sm_random_connection_source = sm_conn; +static void sm_random_start(void * context){ + sm_random_context = context; hci_send_cmd(&hci_le_rand); } // pre: sm_aes128_state != SM_AES128_ACTIVE, hci_can_send_command == 1 -// sm_conn is made availabe to aes128 result handler by this -static void sm_aes128_start(sm_key_t key, sm_key_t plaintext, sm_connection_t * sm_conn){ +// context is made availabe to aes128 result handler by this +static void sm_aes128_start(sm_key_t key, sm_key_t plaintext, void * context){ sm_aes128_state = SM_AES128_ACTIVE; sm_key_t key_flipped, plaintext_flipped; swap128(key, key_flipped); swap128(plaintext, plaintext_flipped); - sm_aes128_connection_source = sm_conn; + sm_aes128_context = context; hci_send_cmd(&hci_le_encrypt, key_flipped, plaintext_flipped); } @@ -1412,7 +1412,7 @@ static void sm_handle_encryption_result(uint8_t * data){ } // retrieve sm_connection provided to sm_aes128_start_encryption - sm_connection_t * connection = sm_aes128_connection_source; + sm_connection_t * connection = (sm_connection_t*) sm_aes128_context; if (!connection) return; switch (connection->sm_engine_state){ case SM_PH2_C1_W4_ENC_A: @@ -1534,7 +1534,7 @@ static void sm_handle_random_result(uint8_t * data){ } // retrieve sm_connection provided to sm_random_start - sm_connection_t * connection = sm_random_connection_source; + sm_connection_t * connection = (sm_connection_t *) sm_random_context; if (!connection) return; switch (connection->sm_engine_state){ case SM_PH2_W4_RANDOM_TK: From 07a9209480741bd78075e364636f6da8a0b50d94 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 31 Jul 2015 15:59:18 +0200 Subject: [PATCH 2/5] generalize sm_csrk context arguments --- ble/sm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index c647a3dfc..64deede9d 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -157,7 +157,8 @@ static void * sm_aes128_context; static void * sm_random_context; // CSRK calculation -static sm_connection_t * sm_csrk_connection_source; +static void * sm_csrk_context; + // // Volume 3, Part H, Chapter 24 // "Security shall be initiated by the Security Manager in the device in the master role. @@ -548,7 +549,7 @@ static void sm_central_device_start_lookup(sm_connection_t * sm_conn, uint8_t ad sm_central_device_addr_type = addr_type; sm_central_device_test = 0; sm_central_device_matched = -1; - sm_csrk_connection_source = sm_conn; + sm_csrk_context = sm_conn; sm_notify_client(SM_IDENTITY_RESOLVING_STARTED, addr_type, addr, 0, 0); } @@ -944,6 +945,7 @@ static void sm_run(void){ // -- Continue with CSRK device lookup by public or resolvable private address if (sm_central_device_test >= 0){ log_info("LE Device Lookup: device %u/%u", sm_central_device_test, le_device_db_count()); + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_csrk_context; while (sm_central_device_test < le_device_db_count()){ int addr_type; bd_addr_t addr; @@ -959,14 +961,14 @@ static void sm_run(void){ // re-use stored LTK/EDIV/RAND if requested & we're master // TODO: replace global with flag in sm_connection_t - if (sm_authenticate_outgoing_connections && sm_csrk_connection_source->sm_role == 0){ - sm_csrk_connection_source->sm_engine_state = SM_INITIATOR_PH0_HAS_LTK; + if (sm_authenticate_outgoing_connections && sm_csrk_connection->sm_role == 0){ + sm_csrk_connection->sm_engine_state = SM_INITIATOR_PH0_HAS_LTK; log_info("sm: Setting up previous ltk/ediv/rand"); } // ready for other requests - sm_csrk_connection_source->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_csrk_connection_source = NULL; + sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; + sm_csrk_connection = NULL; break; } @@ -983,15 +985,15 @@ static void sm_run(void){ sm_key_t r_prime; sm_ah_r_prime(sm_central_device_address, r_prime); sm_central_ah_calculation_active = 1; - sm_aes128_start(irk, r_prime, sm_csrk_connection_source); + sm_aes128_start(irk, r_prime, sm_csrk_connection); return; } if (sm_central_device_test >= le_device_db_count()){ log_info("LE Device Lookup: not found"); sm_central_device_test = -1; - sm_csrk_connection_source->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_csrk_connection_source = NULL; + sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; + sm_csrk_connection = NULL; sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_central_device_addr_type, sm_central_device_address, 0, 0); } } @@ -1356,8 +1358,9 @@ static void sm_handle_encryption_result(uint8_t * data){ // found sm_central_device_matched = sm_central_device_test; sm_central_device_test = -1; - sm_csrk_connection_source->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_csrk_connection_source = NULL; + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_csrk_context; + sm_csrk_context = NULL; + sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); log_info("LE Device Lookup: matched resolvable private address"); return; From 69d313c5b07228c90e8df53a9097830dfa75d43b Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 31 Jul 2015 16:19:02 +0200 Subject: [PATCH 3/5] rename sm_central_device* into sm_address_resolution* --- ble/sm.c | 90 +++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index 64deede9d..6259975e1 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -142,12 +142,13 @@ static uint8_t sm_cmac_block_current; static uint8_t sm_cmac_block_count; static void (*sm_cmac_done_handler)(uint8_t hash[8]); -// resolvable private address lookup -static int sm_central_device_test; -static int sm_central_device_matched; -static int sm_central_ah_calculation_active; -static uint8_t sm_central_device_addr_type; -static bd_addr_t sm_central_device_address; +// resolvable private address lookup / CSRK calculation +static int sm_address_resolution_device_test; +static int sm_address_resolution_device_matched; +static int sm_address_resolution_ah_calculation_active; +static uint8_t sm_address_resolution_device_addr_type; +static bd_addr_t sm_address_resolution_device_address; +static void * sm_address_resolution_device_context; // aes128 crypto engine. store current sm_connection_t in sm_aes128_context static sm_aes128_state_t sm_aes128_state; @@ -156,9 +157,6 @@ static void * sm_aes128_context; // random engine. store context (ususally sm_connection_t) static void * sm_random_context; -// CSRK calculation -static void * sm_csrk_context; - // // Volume 3, Part H, Chapter 24 // "Security shall be initiated by the Security Manager in the device in the master role. @@ -540,16 +538,16 @@ static void sm_setup_key_distribution(uint8_t key_set){ // CSRK Key Lookup -/* static */ int sm_central_device_lookup_active(void){ - return sm_central_device_test >= 0; +/* static */ int sm_address_resolution_device_lookup_active(void){ + return sm_address_resolution_device_test >= 0; } -static void sm_central_device_start_lookup(sm_connection_t * sm_conn, uint8_t addr_type, bd_addr_t addr){ - memcpy(sm_central_device_address, addr, 6); - sm_central_device_addr_type = addr_type; - sm_central_device_test = 0; - sm_central_device_matched = -1; - sm_csrk_context = sm_conn; +static void sm_address_resolution_device_start_lookup(sm_connection_t * sm_conn, uint8_t addr_type, bd_addr_t addr){ + memcpy(sm_address_resolution_device_address, addr, 6); + sm_address_resolution_device_addr_type = addr_type; + sm_address_resolution_device_test = 0; + sm_address_resolution_device_matched = -1; + sm_address_resolution_device_context = sm_conn; sm_notify_client(SM_IDENTITY_RESOLVING_STARTED, addr_type, addr, 0, 0); } @@ -928,14 +926,14 @@ static void sm_run(void){ // CSRK Lookup // -- if csrk lookup ready, find connection that require csrk lookup - if (!sm_central_device_lookup_active()){ + if (!sm_address_resolution_device_lookup_active()){ hci_connections_get_iterator(&it); while(linked_list_iterator_has_next(&it)){ hci_connection_t * hci_connection = (hci_connection_t *) linked_list_iterator_next(&it); sm_connection_t * sm_connection = &hci_connection->sm_connection; if (sm_connection->sm_csrk_lookup_state == CSRK_LOOKUP_W4_READY){ // and start lookup - sm_central_device_start_lookup(sm_connection, sm_connection->sm_peer_addr_type, sm_connection->sm_peer_address); + sm_address_resolution_device_start_lookup(sm_connection, sm_connection->sm_peer_addr_type, sm_connection->sm_peer_address); sm_connection->sm_csrk_lookup_state = CSRK_LOOKUP_STARTED; break; } @@ -943,21 +941,21 @@ static void sm_run(void){ } // -- Continue with CSRK device lookup by public or resolvable private address - if (sm_central_device_test >= 0){ - log_info("LE Device Lookup: device %u/%u", sm_central_device_test, le_device_db_count()); - sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_csrk_context; - while (sm_central_device_test < le_device_db_count()){ + if (sm_address_resolution_device_test >= 0){ + log_info("LE Device Lookup: device %u/%u", sm_address_resolution_device_test, le_device_db_count()); + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_device_context; + while (sm_address_resolution_device_test < le_device_db_count()){ int addr_type; bd_addr_t addr; sm_key_t irk; - le_device_db_info(sm_central_device_test, &addr_type, addr, irk); + le_device_db_info(sm_address_resolution_device_test, &addr_type, addr, irk); log_info("device type %u, addr: %s", addr_type, bd_addr_to_str(addr)); - if (sm_central_device_addr_type == addr_type && memcmp(addr, sm_central_device_address, 6) == 0){ + if (sm_address_resolution_device_addr_type == addr_type && memcmp(addr, sm_address_resolution_device_address, 6) == 0){ log_info("LE Device Lookup: found CSRK by { addr_type, address} "); - sm_central_device_matched = sm_central_device_test; - sm_central_device_test = -1; - sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); + sm_address_resolution_device_matched = sm_address_resolution_device_test; + sm_address_resolution_device_test = -1; + sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, sm_address_resolution_device_matched); // re-use stored LTK/EDIV/RAND if requested & we're master // TODO: replace global with flag in sm_connection_t @@ -972,8 +970,8 @@ static void sm_run(void){ break; } - if (sm_central_device_addr_type == 0){ - sm_central_device_test++; + if (sm_address_resolution_device_addr_type == 0){ + sm_address_resolution_device_test++; continue; } @@ -983,18 +981,18 @@ static void sm_run(void){ log_key("IRK", irk); sm_key_t r_prime; - sm_ah_r_prime(sm_central_device_address, r_prime); - sm_central_ah_calculation_active = 1; + sm_ah_r_prime(sm_address_resolution_device_address, r_prime); + sm_address_resolution_ah_calculation_active = 1; sm_aes128_start(irk, r_prime, sm_csrk_connection); return; } - if (sm_central_device_test >= le_device_db_count()){ + if (sm_address_resolution_device_test >= le_device_db_count()){ log_info("LE Device Lookup: not found"); - sm_central_device_test = -1; + sm_address_resolution_device_test = -1; sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; sm_csrk_connection = NULL; - sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_central_device_addr_type, sm_central_device_address, 0, 0); + sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, 0); } } @@ -1349,24 +1347,24 @@ static void sm_handle_encryption_result(uint8_t * data){ sm_aes128_state = SM_AES128_IDLE; - if (sm_central_ah_calculation_active){ - sm_central_ah_calculation_active = 0; + if (sm_address_resolution_ah_calculation_active){ + sm_address_resolution_ah_calculation_active = 0; // compare calulated address against connecting device uint8_t hash[3]; swap24(data, hash); - if (memcmp(&sm_central_device_address[3], hash, 3) == 0){ + if (memcmp(&sm_address_resolution_device_address[3], hash, 3) == 0){ // found - sm_central_device_matched = sm_central_device_test; - sm_central_device_test = -1; - sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_csrk_context; - sm_csrk_context = NULL; + sm_address_resolution_device_matched = sm_address_resolution_device_test; + sm_address_resolution_device_test = -1; + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_device_context; + sm_address_resolution_device_context = NULL; sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); + sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, sm_address_resolution_device_matched); log_info("LE Device Lookup: matched resolvable private address"); return; } // no match - sm_central_device_test++; + sm_address_resolution_device_test++; return; } switch (dkg_state){ @@ -2066,8 +2064,8 @@ void sm_init(void){ dkg_state = DKG_W4_WORKING; rau_state = RAU_W4_WORKING; sm_aes128_state = SM_AES128_IDLE; - sm_central_device_test = -1; // no private address to resolve yet - sm_central_ah_calculation_active = 0; + sm_address_resolution_device_test = -1; // no private address to resolve yet + sm_address_resolution_ah_calculation_active = 0; gap_random_adress_update_period = 15 * 60 * 1000L; From 3299fc1742a9d1fcec1cbba8bcf9f4580bd102c6 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 31 Jul 2015 16:25:27 +0200 Subject: [PATCH 4/5] rename sm_address_resolution_device* into sm_address_resolution* --- ble/sm.c | 76 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index 6259975e1..815f75e42 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -143,12 +143,12 @@ static uint8_t sm_cmac_block_count; static void (*sm_cmac_done_handler)(uint8_t hash[8]); // resolvable private address lookup / CSRK calculation -static int sm_address_resolution_device_test; -static int sm_address_resolution_device_matched; +static int sm_address_resolution_test; +static int sm_address_resolution_matched; static int sm_address_resolution_ah_calculation_active; -static uint8_t sm_address_resolution_device_addr_type; -static bd_addr_t sm_address_resolution_device_address; -static void * sm_address_resolution_device_context; +static uint8_t sm_address_resolution_addr_type; +static bd_addr_t sm_address_resolution_address; +static void * sm_address_resolution_context; // aes128 crypto engine. store current sm_connection_t in sm_aes128_context static sm_aes128_state_t sm_aes128_state; @@ -538,16 +538,16 @@ static void sm_setup_key_distribution(uint8_t key_set){ // CSRK Key Lookup -/* static */ int sm_address_resolution_device_lookup_active(void){ - return sm_address_resolution_device_test >= 0; +/* static */ int sm_address_resolution_lookup_active(void){ + return sm_address_resolution_test >= 0; } -static void sm_address_resolution_device_start_lookup(sm_connection_t * sm_conn, uint8_t addr_type, bd_addr_t addr){ - memcpy(sm_address_resolution_device_address, addr, 6); - sm_address_resolution_device_addr_type = addr_type; - sm_address_resolution_device_test = 0; - sm_address_resolution_device_matched = -1; - sm_address_resolution_device_context = sm_conn; +static void sm_address_resolution_start_lookup(uint8_t addr_type, bd_addr_t addr, void * context){ + memcpy(sm_address_resolution_address, addr, 6); + sm_address_resolution_addr_type = addr_type; + sm_address_resolution_test = 0; + sm_address_resolution_matched = -1; + sm_address_resolution_context = context; sm_notify_client(SM_IDENTITY_RESOLVING_STARTED, addr_type, addr, 0, 0); } @@ -926,14 +926,14 @@ static void sm_run(void){ // CSRK Lookup // -- if csrk lookup ready, find connection that require csrk lookup - if (!sm_address_resolution_device_lookup_active()){ + if (!sm_address_resolution_lookup_active()){ hci_connections_get_iterator(&it); while(linked_list_iterator_has_next(&it)){ hci_connection_t * hci_connection = (hci_connection_t *) linked_list_iterator_next(&it); sm_connection_t * sm_connection = &hci_connection->sm_connection; if (sm_connection->sm_csrk_lookup_state == CSRK_LOOKUP_W4_READY){ // and start lookup - sm_address_resolution_device_start_lookup(sm_connection, sm_connection->sm_peer_addr_type, sm_connection->sm_peer_address); + sm_address_resolution_start_lookup(sm_connection->sm_peer_addr_type, sm_connection->sm_peer_address, sm_connection); sm_connection->sm_csrk_lookup_state = CSRK_LOOKUP_STARTED; break; } @@ -941,21 +941,21 @@ static void sm_run(void){ } // -- Continue with CSRK device lookup by public or resolvable private address - if (sm_address_resolution_device_test >= 0){ - log_info("LE Device Lookup: device %u/%u", sm_address_resolution_device_test, le_device_db_count()); - sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_device_context; - while (sm_address_resolution_device_test < le_device_db_count()){ + if (sm_address_resolution_test >= 0){ + log_info("LE Device Lookup: device %u/%u", sm_address_resolution_test, le_device_db_count()); + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_context; + while (sm_address_resolution_test < le_device_db_count()){ int addr_type; bd_addr_t addr; sm_key_t irk; - le_device_db_info(sm_address_resolution_device_test, &addr_type, addr, irk); + le_device_db_info(sm_address_resolution_test, &addr_type, addr, irk); log_info("device type %u, addr: %s", addr_type, bd_addr_to_str(addr)); - if (sm_address_resolution_device_addr_type == addr_type && memcmp(addr, sm_address_resolution_device_address, 6) == 0){ + if (sm_address_resolution_addr_type == addr_type && memcmp(addr, sm_address_resolution_address, 6) == 0){ log_info("LE Device Lookup: found CSRK by { addr_type, address} "); - sm_address_resolution_device_matched = sm_address_resolution_device_test; - sm_address_resolution_device_test = -1; - sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, sm_address_resolution_device_matched); + sm_address_resolution_matched = sm_address_resolution_test; + sm_address_resolution_test = -1; + sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_addr_type, sm_address_resolution_address, 0, sm_address_resolution_matched); // re-use stored LTK/EDIV/RAND if requested & we're master // TODO: replace global with flag in sm_connection_t @@ -970,8 +970,8 @@ static void sm_run(void){ break; } - if (sm_address_resolution_device_addr_type == 0){ - sm_address_resolution_device_test++; + if (sm_address_resolution_addr_type == 0){ + sm_address_resolution_test++; continue; } @@ -981,18 +981,18 @@ static void sm_run(void){ log_key("IRK", irk); sm_key_t r_prime; - sm_ah_r_prime(sm_address_resolution_device_address, r_prime); + sm_ah_r_prime(sm_address_resolution_address, r_prime); sm_address_resolution_ah_calculation_active = 1; sm_aes128_start(irk, r_prime, sm_csrk_connection); return; } - if (sm_address_resolution_device_test >= le_device_db_count()){ + if (sm_address_resolution_test >= le_device_db_count()){ log_info("LE Device Lookup: not found"); - sm_address_resolution_device_test = -1; + sm_address_resolution_test = -1; sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; sm_csrk_connection = NULL; - sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, 0); + sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_address_resolution_addr_type, sm_address_resolution_address, 0, 0); } } @@ -1352,19 +1352,19 @@ static void sm_handle_encryption_result(uint8_t * data){ // compare calulated address against connecting device uint8_t hash[3]; swap24(data, hash); - if (memcmp(&sm_address_resolution_device_address[3], hash, 3) == 0){ + if (memcmp(&sm_address_resolution_address[3], hash, 3) == 0){ // found - sm_address_resolution_device_matched = sm_address_resolution_device_test; - sm_address_resolution_device_test = -1; - sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_device_context; - sm_address_resolution_device_context = NULL; + sm_address_resolution_matched = sm_address_resolution_test; + sm_address_resolution_test = -1; + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_context; + sm_address_resolution_context = NULL; sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_device_addr_type, sm_address_resolution_device_address, 0, sm_address_resolution_device_matched); + sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_addr_type, sm_address_resolution_address, 0, sm_address_resolution_matched); log_info("LE Device Lookup: matched resolvable private address"); return; } // no match - sm_address_resolution_device_test++; + sm_address_resolution_test++; return; } switch (dkg_state){ @@ -2064,7 +2064,7 @@ void sm_init(void){ dkg_state = DKG_W4_WORKING; rau_state = RAU_W4_WORKING; sm_aes128_state = SM_AES128_IDLE; - sm_address_resolution_device_test = -1; // no private address to resolve yet + sm_address_resolution_test = -1; // no private address to resolve yet sm_address_resolution_ah_calculation_active = 0; gap_random_adress_update_period = 15 * 60 * 1000L; From ce1e26281b4c227595d88d15671f4d2cd13248f2 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 31 Jul 2015 16:59:07 +0200 Subject: [PATCH 5/5] make address resolution/identify resolving public --- ble/sm.c | 30 +++++++++++++++++++++--------- ble/sm.h | 12 ++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index 815f75e42..fb44598a0 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -538,9 +538,6 @@ static void sm_setup_key_distribution(uint8_t key_set){ // CSRK Key Lookup -/* static */ int sm_address_resolution_lookup_active(void){ - return sm_address_resolution_test >= 0; -} static void sm_address_resolution_start_lookup(uint8_t addr_type, bd_addr_t addr, void * context){ memcpy(sm_address_resolution_address, addr, 6); @@ -551,6 +548,14 @@ static void sm_address_resolution_start_lookup(uint8_t addr_type, bd_addr_t addr sm_notify_client(SM_IDENTITY_RESOLVING_STARTED, addr_type, addr, 0, 0); } +void sm_address_resolution_lookup(uint8_t addr_type, bd_addr_t addr){ + sm_address_resolution_start_lookup(addr_type, addr, NULL); +} + +int sm_address_resolution_idle(void){ + return sm_address_resolution_test < 0; +} + // CMAC Implementation using AES128 engine static void sm_shift_left_by_one_bit_inplace(int len, uint8_t * data){ int i; @@ -926,7 +931,7 @@ static void sm_run(void){ // CSRK Lookup // -- if csrk lookup ready, find connection that require csrk lookup - if (!sm_address_resolution_lookup_active()){ + if (sm_address_resolution_idle()){ hci_connections_get_iterator(&it); while(linked_list_iterator_has_next(&it)){ hci_connection_t * hci_connection = (hci_connection_t *) linked_list_iterator_next(&it); @@ -943,7 +948,6 @@ static void sm_run(void){ // -- Continue with CSRK device lookup by public or resolvable private address if (sm_address_resolution_test >= 0){ log_info("LE Device Lookup: device %u/%u", sm_address_resolution_test, le_device_db_count()); - sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_context; while (sm_address_resolution_test < le_device_db_count()){ int addr_type; bd_addr_t addr; @@ -953,10 +957,15 @@ static void sm_run(void){ if (sm_address_resolution_addr_type == addr_type && memcmp(addr, sm_address_resolution_address, 6) == 0){ log_info("LE Device Lookup: found CSRK by { addr_type, address} "); + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_context; + sm_address_resolution_context = NULL; sm_address_resolution_matched = sm_address_resolution_test; sm_address_resolution_test = -1; sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_address_resolution_addr_type, sm_address_resolution_address, 0, sm_address_resolution_matched); + // if context given, we're resolving an active connection + if (!sm_csrk_connection) break; + // re-use stored LTK/EDIV/RAND if requested & we're master // TODO: replace global with flag in sm_connection_t if (sm_authenticate_outgoing_connections && sm_csrk_connection->sm_role == 0){ @@ -966,7 +975,6 @@ static void sm_run(void){ // ready for other requests sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_csrk_connection = NULL; break; } @@ -983,16 +991,20 @@ static void sm_run(void){ sm_key_t r_prime; sm_ah_r_prime(sm_address_resolution_address, r_prime); sm_address_resolution_ah_calculation_active = 1; - sm_aes128_start(irk, r_prime, sm_csrk_connection); + sm_aes128_start(irk, r_prime, sm_address_resolution_context); // keep context return; } if (sm_address_resolution_test >= le_device_db_count()){ log_info("LE Device Lookup: not found"); + sm_connection_t * sm_csrk_connection = (sm_connection_t *) sm_address_resolution_context; + sm_address_resolution_context = NULL; sm_address_resolution_test = -1; - sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; - sm_csrk_connection = NULL; sm_notify_client(SM_IDENTITY_RESOLVING_FAILED, sm_address_resolution_addr_type, sm_address_resolution_address, 0, 0); + + if (sm_address_resolution_context) { + sm_csrk_connection->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE; + } } } diff --git a/ble/sm.h b/ble/sm.h index e0ec5b0a3..699081caa 100644 --- a/ble/sm.h +++ b/ble/sm.h @@ -256,6 +256,18 @@ void sm_authorization_grant(uint8_t addr_type, bd_addr_t address); int sm_cmac_ready(void); void sm_cmac_start(sm_key_t k, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8])); +/** + * @brief Address Resolution Engine idle + * @return 1 if idle, 0 otherwise + */ +int sm_address_resolution_idle(void); + +/* + * @brief Match address against bonded devices + * @note Triggers SM_IDENTITY_RESOLVING_* events + */ + void sm_address_resolution_lookup(uint8_t addr_type, bd_addr_t addr); + /** * @brief Identify device in LE Device DB. * @param handle