13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sm.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald #include <string.h> 413deb3ec6SMatthias Ringwald #include <inttypes.h> 423deb3ec6SMatthias Ringwald 433edc84c5SMatthias Ringwald #include "ble/le_device_db.h" 4459c6af15SMatthias Ringwald #include "ble/core.h" 453edc84c5SMatthias Ringwald #include "ble/sm.h" 4661f37892SMatthias Ringwald #include "bluetooth_company_id.h" 47d7f1c72eSMatthias Ringwald #include "btstack_bool.h" 48d1a1f6a4SMatthias Ringwald #include "btstack_crypto.h" 490e2df43fSMatthias Ringwald #include "btstack_debug.h" 500e2df43fSMatthias Ringwald #include "btstack_event.h" 510e2df43fSMatthias Ringwald #include "btstack_linked_list.h" 520e2df43fSMatthias Ringwald #include "btstack_memory.h" 53899e6e02SMatthias Ringwald #include "btstack_tlv.h" 54f7a05cdaSMatthias Ringwald #include "gap.h" 550e2df43fSMatthias Ringwald #include "hci.h" 5613377825SMatthias Ringwald #include "hci_dump.h" 570e2df43fSMatthias Ringwald #include "l2cap.h" 583deb3ec6SMatthias Ringwald 591a682202SMatthias Ringwald #if !defined(ENABLE_LE_PERIPHERAL) && !defined(ENABLE_LE_CENTRAL) 601a682202SMatthias Ringwald #error "LE Security Manager used, but neither ENABLE_LE_PERIPHERAL nor ENABLE_LE_CENTRAL defined. Please add at least one to btstack_config.h." 611a682202SMatthias Ringwald #endif 621a682202SMatthias Ringwald 637ece0eaaSMatthias Ringwald #if defined(ENABLE_CROSS_TRANSPORT_KEY_DERIVATION) && (!defined(ENABLE_CLASSIC) || !defined(ENABLE_LE_SECURE_CONNECTIONS)) 647ece0eaaSMatthias Ringwald #error "Cross Transport Key Derivation requires support for LE Secure Connections and BR/EDR (Classic)" 656857ad8fSMatthias Ringwald #endif 666857ad8fSMatthias Ringwald 67d1a1f6a4SMatthias Ringwald // assert SM Public Key can be sent/received 68d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 69d1a1f6a4SMatthias Ringwald #if HCI_ACL_PAYLOAD_SIZE < 69 70d1a1f6a4SMatthias Ringwald #error "HCI_ACL_PAYLOAD_SIZE must be at least 69 bytes when using LE Secure Conection. Please increase HCI_ACL_PAYLOAD_SIZE or disable ENABLE_LE_SECURE_CONNECTIONS" 71d1a1f6a4SMatthias Ringwald #endif 72d1a1f6a4SMatthias Ringwald #endif 73d1a1f6a4SMatthias Ringwald 7442134bc6SMatthias Ringwald #if defined(ENABLE_LE_PERIPHERAL) && defined(ENABLE_LE_CENTRAL) 7542134bc6SMatthias Ringwald #define IS_RESPONDER(role) (role) 7642134bc6SMatthias Ringwald #else 7742134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7842134bc6SMatthias Ringwald // only central - never responder (avoid 'unused variable' warnings) 7942134bc6SMatthias Ringwald #define IS_RESPONDER(role) (0 && role) 8042134bc6SMatthias Ringwald #else 8142134bc6SMatthias Ringwald // only peripheral - always responder (avoid 'unused variable' warnings) 8242134bc6SMatthias Ringwald #define IS_RESPONDER(role) (1 || role) 8342134bc6SMatthias Ringwald #endif 8442134bc6SMatthias Ringwald #endif 8542134bc6SMatthias Ringwald 867a766ebfSMatthias Ringwald #if defined(ENABLE_LE_SIGNED_WRITE) || defined(ENABLE_LE_SECURE_CONNECTIONS) 87d1a1f6a4SMatthias Ringwald #define USE_CMAC_ENGINE 887a766ebfSMatthias Ringwald #endif 897a766ebfSMatthias Ringwald 906857ad8fSMatthias Ringwald 91968b2eafSMatthias Ringwald #define BTSTACK_TAG32(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D)) 92899e6e02SMatthias Ringwald 933deb3ec6SMatthias Ringwald // 943deb3ec6SMatthias Ringwald // SM internal types and globals 953deb3ec6SMatthias Ringwald // 963deb3ec6SMatthias Ringwald 973deb3ec6SMatthias Ringwald typedef enum { 983deb3ec6SMatthias Ringwald DKG_W4_WORKING, 993deb3ec6SMatthias Ringwald DKG_CALC_IRK, 1003deb3ec6SMatthias Ringwald DKG_CALC_DHK, 1013deb3ec6SMatthias Ringwald DKG_READY 1023deb3ec6SMatthias Ringwald } derived_key_generation_t; 1033deb3ec6SMatthias Ringwald 1043deb3ec6SMatthias Ringwald typedef enum { 1053deb3ec6SMatthias Ringwald RAU_IDLE, 106fbd4e238SMatthias Ringwald RAU_GET_RANDOM, 1073deb3ec6SMatthias Ringwald RAU_W4_RANDOM, 1083deb3ec6SMatthias Ringwald RAU_GET_ENC, 1093deb3ec6SMatthias Ringwald RAU_W4_ENC, 1103deb3ec6SMatthias Ringwald RAU_SET_ADDRESS, 1113deb3ec6SMatthias Ringwald } random_address_update_t; 1123deb3ec6SMatthias Ringwald 1133deb3ec6SMatthias Ringwald typedef enum { 1143deb3ec6SMatthias Ringwald CMAC_IDLE, 1153deb3ec6SMatthias Ringwald CMAC_CALC_SUBKEYS, 1163deb3ec6SMatthias Ringwald CMAC_W4_SUBKEYS, 1173deb3ec6SMatthias Ringwald CMAC_CALC_MI, 1183deb3ec6SMatthias Ringwald CMAC_W4_MI, 1193deb3ec6SMatthias Ringwald CMAC_CALC_MLAST, 1203deb3ec6SMatthias Ringwald CMAC_W4_MLAST 1213deb3ec6SMatthias Ringwald } cmac_state_t; 1223deb3ec6SMatthias Ringwald 1233deb3ec6SMatthias Ringwald typedef enum { 1243deb3ec6SMatthias Ringwald JUST_WORKS, 12527c32905SMatthias Ringwald PK_RESP_INPUT, // Initiator displays PK, responder inputs PK 12627c32905SMatthias Ringwald PK_INIT_INPUT, // Responder displays PK, initiator inputs PK 12747fb4255SMatthias Ringwald PK_BOTH_INPUT, // Only input on both, both input PK 12847fb4255SMatthias Ringwald NUMERIC_COMPARISON, // Only numerical compparison (yes/no) on on both sides 12947fb4255SMatthias Ringwald OOB // OOB available on one (SC) or both sides (legacy) 1303deb3ec6SMatthias Ringwald } stk_generation_method_t; 1313deb3ec6SMatthias Ringwald 1323deb3ec6SMatthias Ringwald typedef enum { 1333deb3ec6SMatthias Ringwald SM_USER_RESPONSE_IDLE, 1343deb3ec6SMatthias Ringwald SM_USER_RESPONSE_PENDING, 1353deb3ec6SMatthias Ringwald SM_USER_RESPONSE_CONFIRM, 1363deb3ec6SMatthias Ringwald SM_USER_RESPONSE_PASSKEY, 1373deb3ec6SMatthias Ringwald SM_USER_RESPONSE_DECLINE 1383deb3ec6SMatthias Ringwald } sm_user_response_t; 1393deb3ec6SMatthias Ringwald 1403deb3ec6SMatthias Ringwald typedef enum { 1413deb3ec6SMatthias Ringwald SM_AES128_IDLE, 1423deb3ec6SMatthias Ringwald SM_AES128_ACTIVE 1433deb3ec6SMatthias Ringwald } sm_aes128_state_t; 1443deb3ec6SMatthias Ringwald 1453deb3ec6SMatthias Ringwald typedef enum { 1463deb3ec6SMatthias Ringwald ADDRESS_RESOLUTION_IDLE, 1473deb3ec6SMatthias Ringwald ADDRESS_RESOLUTION_GENERAL, 1483deb3ec6SMatthias Ringwald ADDRESS_RESOLUTION_FOR_CONNECTION, 1493deb3ec6SMatthias Ringwald } address_resolution_mode_t; 1503deb3ec6SMatthias Ringwald 1513deb3ec6SMatthias Ringwald typedef enum { 152a66b030fSMatthias Ringwald ADDRESS_RESOLUTION_SUCCEEDED, 1533deb3ec6SMatthias Ringwald ADDRESS_RESOLUTION_FAILED, 1543deb3ec6SMatthias Ringwald } address_resolution_event_t; 155901c000fSMatthias Ringwald 156901c000fSMatthias Ringwald typedef enum { 15734b6528fSMatthias Ringwald EC_KEY_GENERATION_IDLE, 1587df18c15SMatthias Ringwald EC_KEY_GENERATION_ACTIVE, 1597df18c15SMatthias Ringwald EC_KEY_GENERATION_DONE, 1607df18c15SMatthias Ringwald } ec_key_generation_state_t; 1617df18c15SMatthias Ringwald 1627df18c15SMatthias Ringwald typedef enum { 1633cf37b8cSMatthias Ringwald SM_STATE_VAR_DHKEY_NEEDED = 1 << 0, 1643cf37b8cSMatthias Ringwald SM_STATE_VAR_DHKEY_CALCULATED = 1 << 1, 1653cf37b8cSMatthias Ringwald SM_STATE_VAR_DHKEY_COMMAND_RECEIVED = 1 << 2, 166901c000fSMatthias Ringwald } sm_state_var_t; 167901c000fSMatthias Ringwald 168c59d0c92SMatthias Ringwald typedef enum { 169c59d0c92SMatthias Ringwald SM_SC_OOB_IDLE, 170d1a1f6a4SMatthias Ringwald SM_SC_OOB_W4_RANDOM, 171c59d0c92SMatthias Ringwald SM_SC_OOB_W2_CALC_CONFIRM, 172c59d0c92SMatthias Ringwald SM_SC_OOB_W4_CONFIRM, 173c59d0c92SMatthias Ringwald } sm_sc_oob_state_t; 174c59d0c92SMatthias Ringwald 1756420f61eSMatthias Ringwald typedef uint8_t sm_key24_t[3]; 1766420f61eSMatthias Ringwald typedef uint8_t sm_key56_t[7]; 1776420f61eSMatthias Ringwald typedef uint8_t sm_key256_t[32]; 1786420f61eSMatthias Ringwald 1793deb3ec6SMatthias Ringwald // 1803deb3ec6SMatthias Ringwald // GLOBAL DATA 1813deb3ec6SMatthias Ringwald // 1823deb3ec6SMatthias Ringwald 1832d2d4d3cSMatthias Ringwald static bool sm_initialized; 1842d2d4d3cSMatthias Ringwald 185841468bbSMatthias Ringwald static bool test_use_fixed_local_csrk; 186841468bbSMatthias Ringwald static bool test_use_fixed_local_irk; 1873deb3ec6SMatthias Ringwald 188192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 189192365feSMatthias Ringwald static uint8_t test_pairing_failure; 190192365feSMatthias Ringwald #endif 191192365feSMatthias Ringwald 1923deb3ec6SMatthias Ringwald // configuration 1933deb3ec6SMatthias Ringwald static uint8_t sm_accepted_stk_generation_methods; 1943deb3ec6SMatthias Ringwald static uint8_t sm_max_encryption_key_size; 1953deb3ec6SMatthias Ringwald static uint8_t sm_min_encryption_key_size; 1963deb3ec6SMatthias Ringwald static uint8_t sm_auth_req = 0; 1973deb3ec6SMatthias Ringwald static uint8_t sm_io_capabilities = IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 1983deb3ec6SMatthias Ringwald static uint8_t sm_slave_request_security; 1994b8c611fSMatthias Ringwald static uint32_t sm_fixed_passkey_in_display_role; 2001979f09cSMatthias Ringwald static bool sm_reconstruct_ltk_without_le_device_db_entry; 2013deb3ec6SMatthias Ringwald 202c59d0c92SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 203c123d999SMatthias Ringwald static bool sm_sc_only_mode; 204c59d0c92SMatthias Ringwald static uint8_t sm_sc_oob_random[16]; 205c59d0c92SMatthias Ringwald static void (*sm_sc_oob_callback)(const uint8_t * confirm_value, const uint8_t * random_value); 206c59d0c92SMatthias Ringwald static sm_sc_oob_state_t sm_sc_oob_state; 207c59d0c92SMatthias Ringwald #endif 208c59d0c92SMatthias Ringwald 209899e6e02SMatthias Ringwald 2101979f09cSMatthias Ringwald static bool sm_persistent_keys_random_active; 211899e6e02SMatthias Ringwald static const btstack_tlv_t * sm_tlv_impl; 212899e6e02SMatthias Ringwald static void * sm_tlv_context; 213899e6e02SMatthias Ringwald 2143deb3ec6SMatthias Ringwald // Security Manager Master Keys, please use sm_set_er(er) and sm_set_ir(ir) with your own 128 bit random values 2153deb3ec6SMatthias Ringwald static sm_key_t sm_persistent_er; 2163deb3ec6SMatthias Ringwald static sm_key_t sm_persistent_ir; 2173deb3ec6SMatthias Ringwald 2183deb3ec6SMatthias Ringwald // derived from sm_persistent_ir 2193deb3ec6SMatthias Ringwald static sm_key_t sm_persistent_dhk; 2203deb3ec6SMatthias Ringwald static sm_key_t sm_persistent_irk; 2213deb3ec6SMatthias Ringwald static derived_key_generation_t dkg_state; 2223deb3ec6SMatthias Ringwald 2233deb3ec6SMatthias Ringwald // derived from sm_persistent_er 2243deb3ec6SMatthias Ringwald // .. 2253deb3ec6SMatthias Ringwald 2263deb3ec6SMatthias Ringwald // random address update 2273deb3ec6SMatthias Ringwald static random_address_update_t rau_state; 2283deb3ec6SMatthias Ringwald static bd_addr_t sm_random_address; 2293deb3ec6SMatthias Ringwald 230d1a1f6a4SMatthias Ringwald #ifdef USE_CMAC_ENGINE 231514d35fcSMatthias Ringwald // CMAC Calculation: General 232d1a1f6a4SMatthias Ringwald static btstack_crypto_aes128_cmac_t sm_cmac_request; 233d1a1f6a4SMatthias Ringwald static void (*sm_cmac_done_callback)(uint8_t hash[8]); 234d1a1f6a4SMatthias Ringwald static uint8_t sm_cmac_active; 235d1a1f6a4SMatthias Ringwald static uint8_t sm_cmac_hash[16]; 2367a766ebfSMatthias Ringwald #endif 237514d35fcSMatthias Ringwald 238514d35fcSMatthias Ringwald // CMAC for ATT Signed Writes 2397a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 240d1a1f6a4SMatthias Ringwald static uint16_t sm_cmac_signed_write_message_len; 241d1a1f6a4SMatthias Ringwald static uint8_t sm_cmac_signed_write_header[3]; 242d1a1f6a4SMatthias Ringwald static const uint8_t * sm_cmac_signed_write_message; 243d1a1f6a4SMatthias Ringwald static uint8_t sm_cmac_signed_write_sign_counter[4]; 2447a766ebfSMatthias Ringwald #endif 245514d35fcSMatthias Ringwald 246514d35fcSMatthias Ringwald // CMAC for Secure Connection functions 247514d35fcSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 248aec94140SMatthias Ringwald static sm_connection_t * sm_cmac_connection; 249514d35fcSMatthias Ringwald static uint8_t sm_cmac_sc_buffer[80]; 250514d35fcSMatthias Ringwald #endif 2513deb3ec6SMatthias Ringwald 2523deb3ec6SMatthias Ringwald // resolvable private address lookup / CSRK calculation 2533deb3ec6SMatthias Ringwald static int sm_address_resolution_test; 2543deb3ec6SMatthias Ringwald static int sm_address_resolution_ah_calculation_active; 2553deb3ec6SMatthias Ringwald static uint8_t sm_address_resolution_addr_type; 2563deb3ec6SMatthias Ringwald static bd_addr_t sm_address_resolution_address; 2573deb3ec6SMatthias Ringwald static void * sm_address_resolution_context; 2583deb3ec6SMatthias Ringwald static address_resolution_mode_t sm_address_resolution_mode; 2598f2a52f4SMatthias Ringwald static btstack_linked_list_t sm_address_resolution_general_queue; 2603deb3ec6SMatthias Ringwald 261d1a1f6a4SMatthias Ringwald // aes128 crypto engine. 2623deb3ec6SMatthias Ringwald static sm_aes128_state_t sm_aes128_state; 2633deb3ec6SMatthias Ringwald 264d1a1f6a4SMatthias Ringwald // crypto 265d1a1f6a4SMatthias Ringwald static btstack_crypto_random_t sm_crypto_random_request; 266d1a1f6a4SMatthias Ringwald static btstack_crypto_aes128_t sm_crypto_aes128_request; 267d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 268d1a1f6a4SMatthias Ringwald static btstack_crypto_ecc_p256_t sm_crypto_ecc_p256_request; 2697df1ef2fSMatthias Ringwald #endif 2707df1ef2fSMatthias Ringwald 271d1a1f6a4SMatthias Ringwald // temp storage for random data 272d1a1f6a4SMatthias Ringwald static uint8_t sm_random_data[8]; 273d1a1f6a4SMatthias Ringwald static uint8_t sm_aes128_key[16]; 274d1a1f6a4SMatthias Ringwald static uint8_t sm_aes128_plaintext[16]; 275d1a1f6a4SMatthias Ringwald static uint8_t sm_aes128_ciphertext[16]; 2763deb3ec6SMatthias Ringwald 277e03e489aSMatthias Ringwald // to receive hci events 278e03e489aSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 279e03e489aSMatthias Ringwald 28089a78d34SMatthias Ringwald /* to dispatch sm event */ 28189a78d34SMatthias Ringwald static btstack_linked_list_t sm_event_handlers; 28289a78d34SMatthias Ringwald 283ece00d2dSMatthias Ringwald /* to schedule calls to sm_run */ 284ece00d2dSMatthias Ringwald static btstack_timer_source_t sm_run_timer; 285ece00d2dSMatthias Ringwald 28609e4d397SMatthias Ringwald // LE Secure Connections 28709e4d397SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 28809e4d397SMatthias Ringwald static ec_key_generation_state_t ec_key_generation_state; 289fc5bff5fSMatthias Ringwald static uint8_t ec_q[64]; 29009e4d397SMatthias Ringwald #endif 291df86eb96SMatthias Ringwald 2923deb3ec6SMatthias Ringwald // 2933deb3ec6SMatthias Ringwald // Volume 3, Part H, Chapter 24 2943deb3ec6SMatthias Ringwald // "Security shall be initiated by the Security Manager in the device in the master role. 2953deb3ec6SMatthias Ringwald // The device in the slave role shall be the responding device." 2963deb3ec6SMatthias Ringwald // -> master := initiator, slave := responder 2973deb3ec6SMatthias Ringwald // 2983deb3ec6SMatthias Ringwald 2993deb3ec6SMatthias Ringwald // data needed for security setup 3003deb3ec6SMatthias Ringwald typedef struct sm_setup_context { 3013deb3ec6SMatthias Ringwald 302ec820d77SMatthias Ringwald btstack_timer_source_t sm_timeout; 3033deb3ec6SMatthias Ringwald 3043deb3ec6SMatthias Ringwald // used in all phases 3053deb3ec6SMatthias Ringwald uint8_t sm_pairing_failed_reason; 3063deb3ec6SMatthias Ringwald 3073deb3ec6SMatthias Ringwald // user response, (Phase 1 and/or 2) 3083deb3ec6SMatthias Ringwald uint8_t sm_user_response; 309dd4a08fbSMatthias Ringwald uint8_t sm_keypress_notification; // bitmap: passkey started, digit entered, digit erased, passkey cleared, passkey complete, 3 bit count 3103deb3ec6SMatthias Ringwald 3113deb3ec6SMatthias Ringwald // defines which keys will be send after connection is encrypted - calculated during Phase 1, used Phase 3 312715a43d1SMatthias Ringwald uint8_t sm_key_distribution_send_set; 313715a43d1SMatthias Ringwald uint8_t sm_key_distribution_sent_set; 314715a43d1SMatthias Ringwald uint8_t sm_key_distribution_received_set; 3153deb3ec6SMatthias Ringwald 3163deb3ec6SMatthias Ringwald // Phase 2 (Pairing over SMP) 3173deb3ec6SMatthias Ringwald stk_generation_method_t sm_stk_generation_method; 3183deb3ec6SMatthias Ringwald sm_key_t sm_tk; 319a680ba6bSMatthias Ringwald uint8_t sm_have_oob_data; 32027c32905SMatthias Ringwald uint8_t sm_use_secure_connections; 3213deb3ec6SMatthias Ringwald 3223deb3ec6SMatthias Ringwald sm_key_t sm_c1_t3_value; // c1 calculation 3233deb3ec6SMatthias Ringwald sm_pairing_packet_t sm_m_preq; // pairing request - needed only for c1 3243deb3ec6SMatthias Ringwald sm_pairing_packet_t sm_s_pres; // pairing response - needed only for c1 3253deb3ec6SMatthias Ringwald sm_key_t sm_local_random; 3263deb3ec6SMatthias Ringwald sm_key_t sm_local_confirm; 3273deb3ec6SMatthias Ringwald sm_key_t sm_peer_random; 3283deb3ec6SMatthias Ringwald sm_key_t sm_peer_confirm; 3293deb3ec6SMatthias Ringwald uint8_t sm_m_addr_type; // address and type can be removed 3303deb3ec6SMatthias Ringwald uint8_t sm_s_addr_type; // '' 3313deb3ec6SMatthias Ringwald bd_addr_t sm_m_address; // '' 3323deb3ec6SMatthias Ringwald bd_addr_t sm_s_address; // '' 3333deb3ec6SMatthias Ringwald sm_key_t sm_ltk; 3343deb3ec6SMatthias Ringwald 33568437d83SMatthias Ringwald uint8_t sm_state_vars; 336e53be891SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 337fc5bff5fSMatthias Ringwald uint8_t sm_peer_q[64]; // also stores random for EC key generation during init 338446a8c36SMatthias Ringwald sm_key_t sm_peer_nonce; // might be combined with sm_peer_random 339446a8c36SMatthias Ringwald sm_key_t sm_local_nonce; // might be combined with sm_local_random 340d08147dfSMatthias Ringwald uint8_t sm_dhkey[32]; 341e53be891SMatthias Ringwald sm_key_t sm_peer_dhkey_check; 342e53be891SMatthias Ringwald sm_key_t sm_local_dhkey_check; 343446a8c36SMatthias Ringwald sm_key_t sm_ra; 344446a8c36SMatthias Ringwald sm_key_t sm_rb; 3452bacf595SMatthias Ringwald sm_key_t sm_t; // used for f5 and h6 346a9f29768SMatthias Ringwald sm_key_t sm_mackey; 3477df18c15SMatthias Ringwald uint8_t sm_passkey_bit; // also stores number of generated random bytes for EC key generation 348e53be891SMatthias Ringwald #endif 34927c32905SMatthias Ringwald 3503deb3ec6SMatthias Ringwald // Phase 3 3513deb3ec6SMatthias Ringwald 3523deb3ec6SMatthias Ringwald // key distribution, we generate 3533deb3ec6SMatthias Ringwald uint16_t sm_local_y; 3543deb3ec6SMatthias Ringwald uint16_t sm_local_div; 3553deb3ec6SMatthias Ringwald uint16_t sm_local_ediv; 3563deb3ec6SMatthias Ringwald uint8_t sm_local_rand[8]; 3573deb3ec6SMatthias Ringwald sm_key_t sm_local_ltk; 3583deb3ec6SMatthias Ringwald sm_key_t sm_local_csrk; 3593deb3ec6SMatthias Ringwald sm_key_t sm_local_irk; 3603deb3ec6SMatthias Ringwald // sm_local_address/addr_type not needed 3613deb3ec6SMatthias Ringwald 3623deb3ec6SMatthias Ringwald // key distribution, received from peer 3633deb3ec6SMatthias Ringwald uint16_t sm_peer_y; 3643deb3ec6SMatthias Ringwald uint16_t sm_peer_div; 3653deb3ec6SMatthias Ringwald uint16_t sm_peer_ediv; 3663deb3ec6SMatthias Ringwald uint8_t sm_peer_rand[8]; 3673deb3ec6SMatthias Ringwald sm_key_t sm_peer_ltk; 3683deb3ec6SMatthias Ringwald sm_key_t sm_peer_irk; 3693deb3ec6SMatthias Ringwald sm_key_t sm_peer_csrk; 3703deb3ec6SMatthias Ringwald uint8_t sm_peer_addr_type; 3713deb3ec6SMatthias Ringwald bd_addr_t sm_peer_address; 372715a43d1SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 373715a43d1SMatthias Ringwald int sm_le_device_index; 374715a43d1SMatthias Ringwald #endif 3753deb3ec6SMatthias Ringwald } sm_setup_context_t; 3763deb3ec6SMatthias Ringwald 3773deb3ec6SMatthias Ringwald // 3783deb3ec6SMatthias Ringwald static sm_setup_context_t the_setup; 3793deb3ec6SMatthias Ringwald static sm_setup_context_t * setup = &the_setup; 3803deb3ec6SMatthias Ringwald 3813deb3ec6SMatthias Ringwald // active connection - the one for which the_setup is used for 3827149bde5SMatthias Ringwald static uint16_t sm_active_connection_handle = HCI_CON_HANDLE_INVALID; 3833deb3ec6SMatthias Ringwald 3843deb3ec6SMatthias Ringwald // @returns 1 if oob data is available 3853deb3ec6SMatthias Ringwald // stores oob data in provided 16 byte buffer if not null 3863deb3ec6SMatthias Ringwald static int (*sm_get_oob_data)(uint8_t addres_type, bd_addr_t addr, uint8_t * oob_data) = NULL; 3874acf7b7bSMatthias Ringwald static int (*sm_get_sc_oob_data)(uint8_t addres_type, bd_addr_t addr, uint8_t * oob_sc_peer_confirm, uint8_t * oob_sc_peer_random); 3883deb3ec6SMatthias Ringwald 3893deb3ec6SMatthias Ringwald static void sm_run(void); 390711e6c80SMatthias Ringwald static void sm_done_for_handle(hci_con_handle_t con_handle); 391711e6c80SMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle); 3923deb3ec6SMatthias Ringwald static inline int sm_calc_actual_encryption_key_size(int other); 3933deb3ec6SMatthias Ringwald static int sm_validate_stk_generation_method(void); 394d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_address_resolution(void *arg); 395d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_dkg_dhk(void *arg); 396d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_dkg_irk(void *arg); 397d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_a(void *arg); 398d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_b(void *arg); 399d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_c(void *arg); 400d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_csrk(void *arg); 401d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_d(void * arg); 402d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph3_ltk(void *arg); 403d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph3_y(void *arg); 4042a526f21SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 405d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph4_ltk(void *arg); 406d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph4_y(void *arg); 4072a526f21SMatthias Ringwald #endif 408d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_stk(void *arg); 409d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_rau(void *arg); 410d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_ph2_tk(void * arg); 411d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_rau(void * arg); 412d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4136d80b495SMatthias Ringwald static void sm_cmac_message_start(const sm_key_t key, uint16_t message_len, const uint8_t * message, void (*done_callback)(uint8_t * hash)); 41434b6528fSMatthias Ringwald static void sm_ec_generate_new_key(void); 4156ca80073SMatthias Ringwald static void sm_handle_random_result_sc_next_w2_cmac_for_confirmation(void * arg); 4166ca80073SMatthias Ringwald static void sm_handle_random_result_sc_next_send_pairing_random(void * arg); 4172a526f21SMatthias Ringwald static int sm_passkey_entry(stk_generation_method_t method); 418d1a1f6a4SMatthias Ringwald #endif 4190ccf6c9cSMatthias Ringwald static void sm_pairing_complete(sm_connection_t * sm_conn, uint8_t status, uint8_t reason); 4203deb3ec6SMatthias Ringwald 4213deb3ec6SMatthias Ringwald static void log_info_hex16(const char * name, uint16_t value){ 4223deb3ec6SMatthias Ringwald log_info("%-6s 0x%04x", name, value); 4233deb3ec6SMatthias Ringwald } 4243deb3ec6SMatthias Ringwald 4251fbd72c5SMatthias Ringwald // static inline uint8_t sm_pairing_packet_get_code(sm_pairing_packet_t packet){ 4261fbd72c5SMatthias Ringwald // return packet[0]; 4271fbd72c5SMatthias Ringwald // } 4281fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_io_capability(sm_pairing_packet_t packet){ 4291fbd72c5SMatthias Ringwald return packet[1]; 4301fbd72c5SMatthias Ringwald } 4311fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_oob_data_flag(sm_pairing_packet_t packet){ 4321fbd72c5SMatthias Ringwald return packet[2]; 4331fbd72c5SMatthias Ringwald } 4341fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_auth_req(sm_pairing_packet_t packet){ 4351fbd72c5SMatthias Ringwald return packet[3]; 4361fbd72c5SMatthias Ringwald } 4371fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_max_encryption_key_size(sm_pairing_packet_t packet){ 4381fbd72c5SMatthias Ringwald return packet[4]; 4391fbd72c5SMatthias Ringwald } 4401fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_initiator_key_distribution(sm_pairing_packet_t packet){ 4411fbd72c5SMatthias Ringwald return packet[5]; 4421fbd72c5SMatthias Ringwald } 4431fbd72c5SMatthias Ringwald static inline uint8_t sm_pairing_packet_get_responder_key_distribution(sm_pairing_packet_t packet){ 4441fbd72c5SMatthias Ringwald return packet[6]; 4451fbd72c5SMatthias Ringwald } 4461fbd72c5SMatthias Ringwald 4471fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_code(sm_pairing_packet_t packet, uint8_t code){ 4481fbd72c5SMatthias Ringwald packet[0] = code; 4491fbd72c5SMatthias Ringwald } 4501fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_io_capability(sm_pairing_packet_t packet, uint8_t io_capability){ 4511fbd72c5SMatthias Ringwald packet[1] = io_capability; 4521fbd72c5SMatthias Ringwald } 4531fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_oob_data_flag(sm_pairing_packet_t packet, uint8_t oob_data_flag){ 4541fbd72c5SMatthias Ringwald packet[2] = oob_data_flag; 4551fbd72c5SMatthias Ringwald } 4561fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_auth_req(sm_pairing_packet_t packet, uint8_t auth_req){ 4571fbd72c5SMatthias Ringwald packet[3] = auth_req; 4581fbd72c5SMatthias Ringwald } 4591fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_max_encryption_key_size(sm_pairing_packet_t packet, uint8_t max_encryption_key_size){ 4601fbd72c5SMatthias Ringwald packet[4] = max_encryption_key_size; 4611fbd72c5SMatthias Ringwald } 4621fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_initiator_key_distribution(sm_pairing_packet_t packet, uint8_t initiator_key_distribution){ 4631fbd72c5SMatthias Ringwald packet[5] = initiator_key_distribution; 4641fbd72c5SMatthias Ringwald } 4651fbd72c5SMatthias Ringwald static inline void sm_pairing_packet_set_responder_key_distribution(sm_pairing_packet_t packet, uint8_t responder_key_distribution){ 4661fbd72c5SMatthias Ringwald packet[6] = responder_key_distribution; 4671fbd72c5SMatthias Ringwald } 4681fbd72c5SMatthias Ringwald 4693deb3ec6SMatthias Ringwald // @returns 1 if all bytes are 0 4701979f09cSMatthias Ringwald static bool sm_is_null(uint8_t * data, int size){ 4713deb3ec6SMatthias Ringwald int i; 4723764b551SMatthias Ringwald for (i=0; i < size ; i++){ 47384c0c5c7SMatthias Ringwald if (data[i] != 0) { 4741979f09cSMatthias Ringwald return false; 47584c0c5c7SMatthias Ringwald } 4763deb3ec6SMatthias Ringwald } 4771979f09cSMatthias Ringwald return true; 4783deb3ec6SMatthias Ringwald } 4793deb3ec6SMatthias Ringwald 4801979f09cSMatthias Ringwald static bool sm_is_null_random(uint8_t random[8]){ 4813764b551SMatthias Ringwald return sm_is_null(random, 8); 4823764b551SMatthias Ringwald } 4833764b551SMatthias Ringwald 4841979f09cSMatthias Ringwald static bool sm_is_null_key(uint8_t * key){ 4853764b551SMatthias Ringwald return sm_is_null(key, 16); 4863764b551SMatthias Ringwald } 4873764b551SMatthias Ringwald 48870b44dd4SMatthias Ringwald // sm_trigger_run allows to schedule callback from main run loop // reduces stack depth 48970b44dd4SMatthias Ringwald static void sm_run_timer_handler(btstack_timer_source_t * ts){ 49070b44dd4SMatthias Ringwald UNUSED(ts); 49170b44dd4SMatthias Ringwald sm_run(); 49270b44dd4SMatthias Ringwald } 49370b44dd4SMatthias Ringwald static void sm_trigger_run(void){ 4942d2d4d3cSMatthias Ringwald if (!sm_initialized) return; 49584c0c5c7SMatthias Ringwald (void)btstack_run_loop_remove_timer(&sm_run_timer); 49670b44dd4SMatthias Ringwald btstack_run_loop_set_timer(&sm_run_timer, 0); 49770b44dd4SMatthias Ringwald btstack_run_loop_add_timer(&sm_run_timer); 49870b44dd4SMatthias Ringwald } 49970b44dd4SMatthias Ringwald 5003deb3ec6SMatthias Ringwald // Key utils 5013deb3ec6SMatthias Ringwald static void sm_reset_tk(void){ 5023deb3ec6SMatthias Ringwald int i; 5033deb3ec6SMatthias Ringwald for (i=0;i<16;i++){ 5043deb3ec6SMatthias Ringwald setup->sm_tk[i] = 0; 5053deb3ec6SMatthias Ringwald } 5063deb3ec6SMatthias Ringwald } 5073deb3ec6SMatthias Ringwald 5083deb3ec6SMatthias Ringwald // "For example, if a 128-bit encryption key is 0x123456789ABCDEF0123456789ABCDEF0 5093deb3ec6SMatthias Ringwald // and it is reduced to 7 octets (56 bits), then the resulting key is 0x0000000000000000003456789ABCDEF0."" 5103deb3ec6SMatthias Ringwald static void sm_truncate_key(sm_key_t key, int max_encryption_size){ 5113deb3ec6SMatthias Ringwald int i; 5123deb3ec6SMatthias Ringwald for (i = max_encryption_size ; i < 16 ; i++){ 5133deb3ec6SMatthias Ringwald key[15-i] = 0; 5143deb3ec6SMatthias Ringwald } 5153deb3ec6SMatthias Ringwald } 5163deb3ec6SMatthias Ringwald 517899e6e02SMatthias Ringwald // ER / IR checks 51821045273SMatthias Ringwald static void sm_er_ir_set_default(void){ 519899e6e02SMatthias Ringwald int i; 520899e6e02SMatthias Ringwald for (i=0;i<16;i++){ 521899e6e02SMatthias Ringwald sm_persistent_er[i] = 0x30 + i; 522899e6e02SMatthias Ringwald sm_persistent_ir[i] = 0x90 + i; 523899e6e02SMatthias Ringwald } 524899e6e02SMatthias Ringwald } 525899e6e02SMatthias Ringwald 526899e6e02SMatthias Ringwald static int sm_er_is_default(void){ 527899e6e02SMatthias Ringwald int i; 528899e6e02SMatthias Ringwald for (i=0;i<16;i++){ 529899e6e02SMatthias Ringwald if (sm_persistent_er[i] != (0x30+i)) return 0; 530899e6e02SMatthias Ringwald } 531899e6e02SMatthias Ringwald return 1; 532899e6e02SMatthias Ringwald } 533899e6e02SMatthias Ringwald 534899e6e02SMatthias Ringwald static int sm_ir_is_default(void){ 535899e6e02SMatthias Ringwald int i; 536899e6e02SMatthias Ringwald for (i=0;i<16;i++){ 537899e6e02SMatthias Ringwald if (sm_persistent_ir[i] != (0x90+i)) return 0; 538899e6e02SMatthias Ringwald } 539899e6e02SMatthias Ringwald return 1; 540899e6e02SMatthias Ringwald } 541899e6e02SMatthias Ringwald 54273102768SMatthias Ringwald static void sm_dispatch_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ 54373102768SMatthias Ringwald UNUSED(channel); 54473102768SMatthias Ringwald 54573102768SMatthias Ringwald // log event 54673102768SMatthias Ringwald hci_dump_packet(packet_type, 1, packet, size); 54773102768SMatthias Ringwald // dispatch to all event handlers 54873102768SMatthias Ringwald btstack_linked_list_iterator_t it; 54973102768SMatthias Ringwald btstack_linked_list_iterator_init(&it, &sm_event_handlers); 55073102768SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 55173102768SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 55273102768SMatthias Ringwald entry->callback(packet_type, 0, packet, size); 55373102768SMatthias Ringwald } 55473102768SMatthias Ringwald } 55573102768SMatthias Ringwald 55673102768SMatthias Ringwald static void sm_setup_event_base(uint8_t * event, int event_size, uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address){ 55773102768SMatthias Ringwald event[0] = type; 55873102768SMatthias Ringwald event[1] = event_size - 2; 55973102768SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 56073102768SMatthias Ringwald event[4] = addr_type; 56173102768SMatthias Ringwald reverse_bd_addr(address, &event[5]); 56273102768SMatthias Ringwald } 56373102768SMatthias Ringwald 56473102768SMatthias Ringwald static void sm_notify_client_base(uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address){ 56573102768SMatthias Ringwald uint8_t event[11]; 56673102768SMatthias Ringwald sm_setup_event_base(event, sizeof(event), type, con_handle, addr_type, address); 56773102768SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, event, sizeof(event)); 56873102768SMatthias Ringwald } 56973102768SMatthias Ringwald 57073102768SMatthias Ringwald static void sm_notify_client_passkey(uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint32_t passkey){ 57173102768SMatthias Ringwald uint8_t event[15]; 57273102768SMatthias Ringwald sm_setup_event_base(event, sizeof(event), type, con_handle, addr_type, address); 57373102768SMatthias Ringwald little_endian_store_32(event, 11, passkey); 57473102768SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, event, sizeof(event)); 57573102768SMatthias Ringwald } 57673102768SMatthias Ringwald 57773102768SMatthias Ringwald static void sm_notify_client_index(uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint16_t index){ 57873102768SMatthias Ringwald // fetch addr and addr type from db, only called for valid entries 57973102768SMatthias Ringwald bd_addr_t identity_address; 58073102768SMatthias Ringwald int identity_address_type; 58173102768SMatthias Ringwald le_device_db_info(index, &identity_address_type, identity_address, NULL); 58273102768SMatthias Ringwald 58373102768SMatthias Ringwald uint8_t event[20]; 58473102768SMatthias Ringwald sm_setup_event_base(event, sizeof(event), type, con_handle, addr_type, address); 58573102768SMatthias Ringwald event[11] = identity_address_type; 58673102768SMatthias Ringwald reverse_bd_addr(identity_address, &event[12]); 58773102768SMatthias Ringwald little_endian_store_16(event, 18, index); 58873102768SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, event, sizeof(event)); 58973102768SMatthias Ringwald } 59073102768SMatthias Ringwald 59173102768SMatthias Ringwald static void sm_notify_client_status(uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint8_t status){ 59273102768SMatthias Ringwald uint8_t event[12]; 59373102768SMatthias Ringwald sm_setup_event_base(event, sizeof(event), type, con_handle, addr_type, address); 59473102768SMatthias Ringwald event[11] = status; 59573102768SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, (uint8_t*) &event, sizeof(event)); 59673102768SMatthias Ringwald } 59773102768SMatthias Ringwald 59873102768SMatthias Ringwald 59973102768SMatthias Ringwald static void sm_reencryption_started(sm_connection_t * sm_conn){ 60073102768SMatthias Ringwald 6013ab61f77SMatthias Ringwald if (sm_conn->sm_reencryption_active) return; 6023ab61f77SMatthias Ringwald 6037b001f4eSMatthias Ringwald sm_conn->sm_reencryption_active = true; 60473102768SMatthias Ringwald 60573102768SMatthias Ringwald int identity_addr_type; 60673102768SMatthias Ringwald bd_addr_t identity_addr; 6073c0e26deSMatthias Ringwald if (sm_conn->sm_le_db_index >= 0){ 6083c0e26deSMatthias Ringwald // fetch addr and addr type from db, only called for valid entries 60973102768SMatthias Ringwald le_device_db_info(sm_conn->sm_le_db_index, &identity_addr_type, identity_addr, NULL); 6103c0e26deSMatthias Ringwald } else { 6113c0e26deSMatthias Ringwald // for legacy pairing with LTK re-construction, use current peer addr 6123c0e26deSMatthias Ringwald identity_addr_type = sm_conn->sm_peer_addr_type; 6133c0e26deSMatthias Ringwald memcpy(identity_addr, sm_conn->sm_peer_address, 6); 6143c0e26deSMatthias Ringwald } 61573102768SMatthias Ringwald 61673102768SMatthias Ringwald sm_notify_client_base(SM_EVENT_REENCRYPTION_STARTED, sm_conn->sm_handle, identity_addr_type, identity_addr); 61773102768SMatthias Ringwald } 61873102768SMatthias Ringwald 61973102768SMatthias Ringwald static void sm_reencryption_complete(sm_connection_t * sm_conn, uint8_t status){ 62073102768SMatthias Ringwald 62160be5b21SMatthias Ringwald if (!sm_conn->sm_reencryption_active) return; 62260be5b21SMatthias Ringwald 6237b001f4eSMatthias Ringwald sm_conn->sm_reencryption_active = false; 62473102768SMatthias Ringwald 62573102768SMatthias Ringwald int identity_addr_type; 62673102768SMatthias Ringwald bd_addr_t identity_addr; 6273c0e26deSMatthias Ringwald if (sm_conn->sm_le_db_index >= 0){ 6283c0e26deSMatthias Ringwald // fetch addr and addr type from db, only called for valid entries 62973102768SMatthias Ringwald le_device_db_info(sm_conn->sm_le_db_index, &identity_addr_type, identity_addr, NULL); 6303c0e26deSMatthias Ringwald } else { 6313c0e26deSMatthias Ringwald // for legacy pairing with LTK re-construction, use current peer addr 6323c0e26deSMatthias Ringwald identity_addr_type = sm_conn->sm_peer_addr_type; 6333c0e26deSMatthias Ringwald memcpy(identity_addr, sm_conn->sm_peer_address, 6); 6343c0e26deSMatthias Ringwald } 63573102768SMatthias Ringwald 63673102768SMatthias Ringwald sm_notify_client_status(SM_EVENT_REENCRYPTION_COMPLETE, sm_conn->sm_handle, identity_addr_type, identity_addr, status); 63773102768SMatthias Ringwald } 63873102768SMatthias Ringwald 639d3c12277SMatthias Ringwald static void sm_pairing_started(sm_connection_t * sm_conn){ 640d3c12277SMatthias Ringwald 641d3c12277SMatthias Ringwald if (sm_conn->sm_pairing_active) return; 642d3c12277SMatthias Ringwald 643d3c12277SMatthias Ringwald sm_conn->sm_pairing_active = true; 644d3c12277SMatthias Ringwald 645d3c12277SMatthias Ringwald uint8_t event[11]; 646d3c12277SMatthias Ringwald sm_setup_event_base(event, sizeof(event), SM_EVENT_PAIRING_STARTED, sm_conn->sm_handle, setup->sm_peer_addr_type, setup->sm_peer_address); 647d3c12277SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, (uint8_t*) &event, sizeof(event)); 648d3c12277SMatthias Ringwald } 649d3c12277SMatthias Ringwald 6500ccf6c9cSMatthias Ringwald static void sm_pairing_complete(sm_connection_t * sm_conn, uint8_t status, uint8_t reason){ 651f61072f5SMatthias Ringwald 652d77906ffSMatthias Ringwald if (!sm_conn->sm_pairing_active) return; 653d77906ffSMatthias Ringwald 6540ccf6c9cSMatthias Ringwald uint8_t event[13]; 6550ccf6c9cSMatthias Ringwald sm_setup_event_base(event, sizeof(event), SM_EVENT_PAIRING_COMPLETE, sm_conn->sm_handle, setup->sm_peer_addr_type, setup->sm_peer_address); 6560ccf6c9cSMatthias Ringwald event[11] = status; 6570ccf6c9cSMatthias Ringwald event[12] = reason; 6580ccf6c9cSMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, (uint8_t*) &event, sizeof(event)); 6590ccf6c9cSMatthias Ringwald } 6600ccf6c9cSMatthias Ringwald 6613deb3ec6SMatthias Ringwald // SMP Timeout implementation 6623deb3ec6SMatthias Ringwald 6633deb3ec6SMatthias Ringwald // Upon transmission of the Pairing Request command or reception of the Pairing Request command, 6643deb3ec6SMatthias Ringwald // the Security Manager Timer shall be reset and started. 6653deb3ec6SMatthias Ringwald // 6663deb3ec6SMatthias Ringwald // The Security Manager Timer shall be reset when an L2CAP SMP command is queued for transmission. 6673deb3ec6SMatthias Ringwald // 6683deb3ec6SMatthias Ringwald // If the Security Manager Timer reaches 30 seconds, the procedure shall be considered to have failed, 6693deb3ec6SMatthias Ringwald // and the local higher layer shall be notified. No further SMP commands shall be sent over the L2CAP 6703deb3ec6SMatthias Ringwald // Security Manager Channel. A new SM procedure shall only be performed when a new physical link has been 6713deb3ec6SMatthias Ringwald // established. 6723deb3ec6SMatthias Ringwald 673ec820d77SMatthias Ringwald static void sm_timeout_handler(btstack_timer_source_t * timer){ 6743deb3ec6SMatthias Ringwald log_info("SM timeout"); 675c5b64319SMatthias Ringwald sm_connection_t * sm_conn = (sm_connection_t*) btstack_run_loop_get_timer_context(timer); 6763deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_TIMEOUT; 67768a18fb9SMatthias Ringwald sm_reencryption_complete(sm_conn, ERROR_CODE_CONNECTION_TIMEOUT); 6780ccf6c9cSMatthias Ringwald sm_pairing_complete(sm_conn, ERROR_CODE_CONNECTION_TIMEOUT, 0); 6793deb3ec6SMatthias Ringwald sm_done_for_handle(sm_conn->sm_handle); 6803deb3ec6SMatthias Ringwald 6813deb3ec6SMatthias Ringwald // trigger handling of next ready connection 6823deb3ec6SMatthias Ringwald sm_run(); 6833deb3ec6SMatthias Ringwald } 6843deb3ec6SMatthias Ringwald static void sm_timeout_start(sm_connection_t * sm_conn){ 685528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&setup->sm_timeout); 68691a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&setup->sm_timeout, sm_conn); 687528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&setup->sm_timeout, sm_timeout_handler); 688528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&setup->sm_timeout, 30000); // 30 seconds sm timeout 689528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&setup->sm_timeout); 6903deb3ec6SMatthias Ringwald } 6913deb3ec6SMatthias Ringwald static void sm_timeout_stop(void){ 692528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&setup->sm_timeout); 6933deb3ec6SMatthias Ringwald } 6943deb3ec6SMatthias Ringwald static void sm_timeout_reset(sm_connection_t * sm_conn){ 6953deb3ec6SMatthias Ringwald sm_timeout_stop(); 6963deb3ec6SMatthias Ringwald sm_timeout_start(sm_conn); 6973deb3ec6SMatthias Ringwald } 6983deb3ec6SMatthias Ringwald 6993deb3ec6SMatthias Ringwald // end of sm timeout 7003deb3ec6SMatthias Ringwald 7013deb3ec6SMatthias Ringwald // GAP Random Address updates 7023deb3ec6SMatthias Ringwald static gap_random_address_type_t gap_random_adress_type; 703ec820d77SMatthias Ringwald static btstack_timer_source_t gap_random_address_update_timer; 7043deb3ec6SMatthias Ringwald static uint32_t gap_random_adress_update_period; 7053deb3ec6SMatthias Ringwald 7063deb3ec6SMatthias Ringwald static void gap_random_address_trigger(void){ 707899e6e02SMatthias Ringwald log_info("gap_random_address_trigger, state %u", rau_state); 708d1a1f6a4SMatthias Ringwald if (rau_state != RAU_IDLE) return; 709fbd4e238SMatthias Ringwald rau_state = RAU_GET_RANDOM; 71070b44dd4SMatthias Ringwald sm_trigger_run(); 7113deb3ec6SMatthias Ringwald } 7123deb3ec6SMatthias Ringwald 713ec820d77SMatthias Ringwald static void gap_random_address_update_handler(btstack_timer_source_t * timer){ 7149ec2630cSMatthias Ringwald UNUSED(timer); 7159ec2630cSMatthias Ringwald 7163deb3ec6SMatthias Ringwald log_info("GAP Random Address Update due"); 717528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&gap_random_address_update_timer, gap_random_adress_update_period); 718528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&gap_random_address_update_timer); 7193deb3ec6SMatthias Ringwald gap_random_address_trigger(); 7203deb3ec6SMatthias Ringwald } 7213deb3ec6SMatthias Ringwald 7223deb3ec6SMatthias Ringwald static void gap_random_address_update_start(void){ 723528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&gap_random_address_update_timer, gap_random_address_update_handler); 724528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&gap_random_address_update_timer, gap_random_adress_update_period); 725528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&gap_random_address_update_timer); 7263deb3ec6SMatthias Ringwald } 7273deb3ec6SMatthias Ringwald 7283deb3ec6SMatthias Ringwald static void gap_random_address_update_stop(void){ 729528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&gap_random_address_update_timer); 7303deb3ec6SMatthias Ringwald } 7313deb3ec6SMatthias Ringwald 7323deb3ec6SMatthias Ringwald // ah(k,r) helper 7333deb3ec6SMatthias Ringwald // r = padding || r 7343deb3ec6SMatthias Ringwald // r - 24 bit value 7354a6806f3SMatthias Ringwald static void sm_ah_r_prime(uint8_t r[3], uint8_t * r_prime){ 7363deb3ec6SMatthias Ringwald // r'= padding || r 7373deb3ec6SMatthias Ringwald memset(r_prime, 0, 16); 7386535961aSMatthias Ringwald (void)memcpy(&r_prime[13], r, 3); 7393deb3ec6SMatthias Ringwald } 7403deb3ec6SMatthias Ringwald 7413deb3ec6SMatthias Ringwald // d1 helper 7423deb3ec6SMatthias Ringwald // d' = padding || r || d 7433deb3ec6SMatthias Ringwald // d,r - 16 bit values 7444a6806f3SMatthias Ringwald static void sm_d1_d_prime(uint16_t d, uint16_t r, uint8_t * d1_prime){ 7453deb3ec6SMatthias Ringwald // d'= padding || r || d 7463deb3ec6SMatthias Ringwald memset(d1_prime, 0, 16); 747f8fbdce0SMatthias Ringwald big_endian_store_16(d1_prime, 12, r); 748f8fbdce0SMatthias Ringwald big_endian_store_16(d1_prime, 14, d); 7493deb3ec6SMatthias Ringwald } 7503deb3ec6SMatthias Ringwald 7513deb3ec6SMatthias Ringwald // calculate arguments for first AES128 operation in C1 function 7524a6806f3SMatthias Ringwald static void sm_c1_t1(sm_key_t r, uint8_t preq[7], uint8_t pres[7], uint8_t iat, uint8_t rat, uint8_t * t1){ 7533deb3ec6SMatthias Ringwald 7543deb3ec6SMatthias Ringwald // p1 = pres || preq || rat’ || iat’ 7553deb3ec6SMatthias Ringwald // "The octet of iat’ becomes the least significant octet of p1 and the most signifi- 7563deb3ec6SMatthias Ringwald // cant octet of pres becomes the most significant octet of p1. 7573deb3ec6SMatthias Ringwald // For example, if the 8-bit iat’ is 0x01, the 8-bit rat’ is 0x00, the 56-bit preq 7583deb3ec6SMatthias Ringwald // is 0x07071000000101 and the 56 bit pres is 0x05000800000302 then 7593deb3ec6SMatthias Ringwald // p1 is 0x05000800000302070710000001010001." 7603deb3ec6SMatthias Ringwald 7613deb3ec6SMatthias Ringwald sm_key_t p1; 7629c80e4ccSMatthias Ringwald reverse_56(pres, &p1[0]); 7639c80e4ccSMatthias Ringwald reverse_56(preq, &p1[7]); 7643deb3ec6SMatthias Ringwald p1[14] = rat; 7653deb3ec6SMatthias Ringwald p1[15] = iat; 7668314c363SMatthias Ringwald log_info_key("p1", p1); 7678314c363SMatthias Ringwald log_info_key("r", r); 7683deb3ec6SMatthias Ringwald 7693deb3ec6SMatthias Ringwald // t1 = r xor p1 7703deb3ec6SMatthias Ringwald int i; 7713deb3ec6SMatthias Ringwald for (i=0;i<16;i++){ 7723deb3ec6SMatthias Ringwald t1[i] = r[i] ^ p1[i]; 7733deb3ec6SMatthias Ringwald } 7748314c363SMatthias Ringwald log_info_key("t1", t1); 7753deb3ec6SMatthias Ringwald } 7763deb3ec6SMatthias Ringwald 7773deb3ec6SMatthias Ringwald // calculate arguments for second AES128 operation in C1 function 7784a6806f3SMatthias Ringwald static void sm_c1_t3(sm_key_t t2, bd_addr_t ia, bd_addr_t ra, uint8_t * t3){ 7793deb3ec6SMatthias Ringwald // p2 = padding || ia || ra 7803deb3ec6SMatthias Ringwald // "The least significant octet of ra becomes the least significant octet of p2 and 7813deb3ec6SMatthias Ringwald // the most significant octet of padding becomes the most significant octet of p2. 7823deb3ec6SMatthias Ringwald // For example, if 48-bit ia is 0xA1A2A3A4A5A6 and the 48-bit ra is 7833deb3ec6SMatthias Ringwald // 0xB1B2B3B4B5B6 then p2 is 0x00000000A1A2A3A4A5A6B1B2B3B4B5B6. 7843deb3ec6SMatthias Ringwald 7853deb3ec6SMatthias Ringwald sm_key_t p2; 7863deb3ec6SMatthias Ringwald memset(p2, 0, 16); 7876535961aSMatthias Ringwald (void)memcpy(&p2[4], ia, 6); 7886535961aSMatthias Ringwald (void)memcpy(&p2[10], ra, 6); 7898314c363SMatthias Ringwald log_info_key("p2", p2); 7903deb3ec6SMatthias Ringwald 7913deb3ec6SMatthias Ringwald // c1 = e(k, t2_xor_p2) 7923deb3ec6SMatthias Ringwald int i; 7933deb3ec6SMatthias Ringwald for (i=0;i<16;i++){ 7943deb3ec6SMatthias Ringwald t3[i] = t2[i] ^ p2[i]; 7953deb3ec6SMatthias Ringwald } 7968314c363SMatthias Ringwald log_info_key("t3", t3); 7973deb3ec6SMatthias Ringwald } 7983deb3ec6SMatthias Ringwald 7994a6806f3SMatthias Ringwald static void sm_s1_r_prime(sm_key_t r1, sm_key_t r2, uint8_t * r_prime){ 8008314c363SMatthias Ringwald log_info_key("r1", r1); 8018314c363SMatthias Ringwald log_info_key("r2", r2); 8026535961aSMatthias Ringwald (void)memcpy(&r_prime[8], &r2[8], 8); 8036535961aSMatthias Ringwald (void)memcpy(&r_prime[0], &r1[8], 8); 8043deb3ec6SMatthias Ringwald } 8053deb3ec6SMatthias Ringwald 806fbe050beSMatthias Ringwald 8073deb3ec6SMatthias Ringwald // decide on stk generation based on 8083deb3ec6SMatthias Ringwald // - pairing request 8093deb3ec6SMatthias Ringwald // - io capabilities 8103deb3ec6SMatthias Ringwald // - OOB data availability 8113deb3ec6SMatthias Ringwald static void sm_setup_tk(void){ 8123deb3ec6SMatthias Ringwald 8138334d3d8SMatthias Ringwald // horizontal: initiator capabilities 8148334d3d8SMatthias Ringwald // vertial: responder capabilities 8158334d3d8SMatthias Ringwald static const stk_generation_method_t stk_generation_method [5] [5] = { 8168334d3d8SMatthias Ringwald { JUST_WORKS, JUST_WORKS, PK_INIT_INPUT, JUST_WORKS, PK_INIT_INPUT }, 8178334d3d8SMatthias Ringwald { JUST_WORKS, JUST_WORKS, PK_INIT_INPUT, JUST_WORKS, PK_INIT_INPUT }, 8188334d3d8SMatthias Ringwald { PK_RESP_INPUT, PK_RESP_INPUT, PK_BOTH_INPUT, JUST_WORKS, PK_RESP_INPUT }, 8198334d3d8SMatthias Ringwald { JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS }, 8208334d3d8SMatthias Ringwald { PK_RESP_INPUT, PK_RESP_INPUT, PK_INIT_INPUT, JUST_WORKS, PK_RESP_INPUT }, 8218334d3d8SMatthias Ringwald }; 8228334d3d8SMatthias Ringwald 8238334d3d8SMatthias Ringwald // uses numeric comparison if one side has DisplayYesNo and KeyboardDisplay combinations 8248334d3d8SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 8258334d3d8SMatthias Ringwald static const stk_generation_method_t stk_generation_method_with_secure_connection[5][5] = { 8268334d3d8SMatthias Ringwald { JUST_WORKS, JUST_WORKS, PK_INIT_INPUT, JUST_WORKS, PK_INIT_INPUT }, 8278334d3d8SMatthias Ringwald { JUST_WORKS, NUMERIC_COMPARISON, PK_INIT_INPUT, JUST_WORKS, NUMERIC_COMPARISON }, 8288334d3d8SMatthias Ringwald { PK_RESP_INPUT, PK_RESP_INPUT, PK_BOTH_INPUT, JUST_WORKS, PK_RESP_INPUT }, 8298334d3d8SMatthias Ringwald { JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS }, 8308334d3d8SMatthias Ringwald { PK_RESP_INPUT, NUMERIC_COMPARISON, PK_INIT_INPUT, JUST_WORKS, NUMERIC_COMPARISON }, 8318334d3d8SMatthias Ringwald }; 8328334d3d8SMatthias Ringwald #endif 8338334d3d8SMatthias Ringwald 8343deb3ec6SMatthias Ringwald // default: just works 8353deb3ec6SMatthias Ringwald setup->sm_stk_generation_method = JUST_WORKS; 8363deb3ec6SMatthias Ringwald 83727c32905SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 83827c32905SMatthias Ringwald setup->sm_use_secure_connections = ( sm_pairing_packet_get_auth_req(setup->sm_m_preq) 83927c32905SMatthias Ringwald & sm_pairing_packet_get_auth_req(setup->sm_s_pres) 8404ea43905SMatthias Ringwald & SM_AUTHREQ_SECURE_CONNECTION ) != 0u; 84127c32905SMatthias Ringwald #else 84227c32905SMatthias Ringwald setup->sm_use_secure_connections = 0; 84327c32905SMatthias Ringwald #endif 84498d95509SMatthias Ringwald log_info("Secure pairing: %u", setup->sm_use_secure_connections); 84527c32905SMatthias Ringwald 84665a9a04eSMatthias Ringwald 84765a9a04eSMatthias Ringwald // decide if OOB will be used based on SC vs. Legacy and oob flags 8481979f09cSMatthias Ringwald bool use_oob; 84965a9a04eSMatthias Ringwald if (setup->sm_use_secure_connections){ 85065a9a04eSMatthias Ringwald // In LE Secure Connections pairing, the out of band method is used if at least 85165a9a04eSMatthias Ringwald // one device has the peer device's out of band authentication data available. 8521979f09cSMatthias Ringwald use_oob = (sm_pairing_packet_get_oob_data_flag(setup->sm_m_preq) | sm_pairing_packet_get_oob_data_flag(setup->sm_s_pres)) != 0; 85365a9a04eSMatthias Ringwald } else { 85465a9a04eSMatthias Ringwald // In LE legacy pairing, the out of band method is used if both the devices have 85565a9a04eSMatthias Ringwald // the other device's out of band authentication data available. 8561979f09cSMatthias Ringwald use_oob = (sm_pairing_packet_get_oob_data_flag(setup->sm_m_preq) & sm_pairing_packet_get_oob_data_flag(setup->sm_s_pres)) != 0; 85765a9a04eSMatthias Ringwald } 85865a9a04eSMatthias Ringwald if (use_oob){ 85965a9a04eSMatthias Ringwald log_info("SM: have OOB data"); 86065a9a04eSMatthias Ringwald log_info_key("OOB", setup->sm_tk); 86165a9a04eSMatthias Ringwald setup->sm_stk_generation_method = OOB; 86265a9a04eSMatthias Ringwald return; 86365a9a04eSMatthias Ringwald } 86465a9a04eSMatthias Ringwald 86527c32905SMatthias Ringwald // If both devices have not set the MITM option in the Authentication Requirements 86627c32905SMatthias Ringwald // Flags, then the IO capabilities shall be ignored and the Just Works association 86727c32905SMatthias Ringwald // model shall be used. 8684ea43905SMatthias Ringwald if (((sm_pairing_packet_get_auth_req(setup->sm_m_preq) & SM_AUTHREQ_MITM_PROTECTION) == 0u) 8694ea43905SMatthias Ringwald && ((sm_pairing_packet_get_auth_req(setup->sm_s_pres) & SM_AUTHREQ_MITM_PROTECTION) == 0u)){ 87027c32905SMatthias Ringwald log_info("SM: MITM not required by both -> JUST WORKS"); 87127c32905SMatthias Ringwald return; 87227c32905SMatthias Ringwald } 87327c32905SMatthias Ringwald 8743deb3ec6SMatthias Ringwald // Reset TK as it has been setup in sm_init_setup 8753deb3ec6SMatthias Ringwald sm_reset_tk(); 8763deb3ec6SMatthias Ringwald 8773deb3ec6SMatthias Ringwald // Also use just works if unknown io capabilites 8788da2e96dSMatthias Ringwald if ((sm_pairing_packet_get_io_capability(setup->sm_m_preq) > IO_CAPABILITY_KEYBOARD_DISPLAY) || (sm_pairing_packet_get_io_capability(setup->sm_s_pres) > IO_CAPABILITY_KEYBOARD_DISPLAY)){ 8793deb3ec6SMatthias Ringwald return; 8803deb3ec6SMatthias Ringwald } 8813deb3ec6SMatthias Ringwald 8823deb3ec6SMatthias Ringwald // Otherwise the IO capabilities of the devices shall be used to determine the 8833deb3ec6SMatthias Ringwald // pairing method as defined in Table 2.4. 88427c32905SMatthias Ringwald // see http://stackoverflow.com/a/1052837/393697 for how to specify pointer to 2-dimensional array 88527c32905SMatthias Ringwald const stk_generation_method_t (*generation_method)[5] = stk_generation_method; 88627c32905SMatthias Ringwald 88727c32905SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 888c6b7cbd9SMatthias Ringwald // table not define by default 88927c32905SMatthias Ringwald if (setup->sm_use_secure_connections){ 89027c32905SMatthias Ringwald generation_method = stk_generation_method_with_secure_connection; 89127c32905SMatthias Ringwald } 89227c32905SMatthias Ringwald #endif 89327c32905SMatthias Ringwald setup->sm_stk_generation_method = generation_method[sm_pairing_packet_get_io_capability(setup->sm_s_pres)][sm_pairing_packet_get_io_capability(setup->sm_m_preq)]; 89427c32905SMatthias Ringwald 8953deb3ec6SMatthias Ringwald log_info("sm_setup_tk: master io cap: %u, slave io cap: %u -> method %u", 8961ad129beSMatthias Ringwald sm_pairing_packet_get_io_capability(setup->sm_m_preq), sm_pairing_packet_get_io_capability(setup->sm_s_pres), setup->sm_stk_generation_method); 8973deb3ec6SMatthias Ringwald } 8983deb3ec6SMatthias Ringwald 8993deb3ec6SMatthias Ringwald static int sm_key_distribution_flags_for_set(uint8_t key_set){ 9003deb3ec6SMatthias Ringwald int flags = 0; 9013deb3ec6SMatthias Ringwald if (key_set & SM_KEYDIST_ENC_KEY){ 9023deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION; 9033deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_FLAG_MASTER_IDENTIFICATION; 9043deb3ec6SMatthias Ringwald } 9053deb3ec6SMatthias Ringwald if (key_set & SM_KEYDIST_ID_KEY){ 9063deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_FLAG_IDENTITY_INFORMATION; 9073deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION; 9083deb3ec6SMatthias Ringwald } 9093deb3ec6SMatthias Ringwald if (key_set & SM_KEYDIST_SIGN){ 9103deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION; 9113deb3ec6SMatthias Ringwald } 9123deb3ec6SMatthias Ringwald return flags; 9133deb3ec6SMatthias Ringwald } 9143deb3ec6SMatthias Ringwald 9153deb3ec6SMatthias Ringwald static void sm_setup_key_distribution(uint8_t key_set){ 9163deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set = 0; 9173deb3ec6SMatthias Ringwald setup->sm_key_distribution_send_set = sm_key_distribution_flags_for_set(key_set); 918715a43d1SMatthias Ringwald setup->sm_key_distribution_sent_set = 0; 9199790be5fSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 920715a43d1SMatthias Ringwald setup->sm_le_device_index = -1; 9219790be5fSMatthias Ringwald #endif 9223deb3ec6SMatthias Ringwald } 9233deb3ec6SMatthias Ringwald 9243deb3ec6SMatthias Ringwald // CSRK Key Lookup 9253deb3ec6SMatthias Ringwald 9263deb3ec6SMatthias Ringwald 9273deb3ec6SMatthias Ringwald static int sm_address_resolution_idle(void){ 9283deb3ec6SMatthias Ringwald return sm_address_resolution_mode == ADDRESS_RESOLUTION_IDLE; 9293deb3ec6SMatthias Ringwald } 9303deb3ec6SMatthias Ringwald 931711e6c80SMatthias Ringwald static void sm_address_resolution_start_lookup(uint8_t addr_type, hci_con_handle_t con_handle, bd_addr_t addr, address_resolution_mode_t mode, void * context){ 9326535961aSMatthias Ringwald (void)memcpy(sm_address_resolution_address, addr, 6); 9333deb3ec6SMatthias Ringwald sm_address_resolution_addr_type = addr_type; 9343deb3ec6SMatthias Ringwald sm_address_resolution_test = 0; 9353deb3ec6SMatthias Ringwald sm_address_resolution_mode = mode; 9363deb3ec6SMatthias Ringwald sm_address_resolution_context = context; 937711e6c80SMatthias Ringwald sm_notify_client_base(SM_EVENT_IDENTITY_RESOLVING_STARTED, con_handle, addr_type, addr); 9383deb3ec6SMatthias Ringwald } 9393deb3ec6SMatthias Ringwald 9403deb3ec6SMatthias Ringwald int sm_address_resolution_lookup(uint8_t address_type, bd_addr_t address){ 9413deb3ec6SMatthias Ringwald // check if already in list 942665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 9433deb3ec6SMatthias Ringwald sm_lookup_entry_t * entry; 944665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &sm_address_resolution_general_queue); 945665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 946665d90f2SMatthias Ringwald entry = (sm_lookup_entry_t *) btstack_linked_list_iterator_next(&it); 9473deb3ec6SMatthias Ringwald if (entry->address_type != address_type) continue; 9483deb3ec6SMatthias Ringwald if (memcmp(entry->address, address, 6)) continue; 9493deb3ec6SMatthias Ringwald // already in list 9503deb3ec6SMatthias Ringwald return BTSTACK_BUSY; 9513deb3ec6SMatthias Ringwald } 9523deb3ec6SMatthias Ringwald entry = btstack_memory_sm_lookup_entry_get(); 9533deb3ec6SMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 9543deb3ec6SMatthias Ringwald entry->address_type = (bd_addr_type_t) address_type; 9556535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 956665d90f2SMatthias Ringwald btstack_linked_list_add(&sm_address_resolution_general_queue, (btstack_linked_item_t *) entry); 95770b44dd4SMatthias Ringwald sm_trigger_run(); 9583deb3ec6SMatthias Ringwald return 0; 9593deb3ec6SMatthias Ringwald } 9603deb3ec6SMatthias Ringwald 961d1a1f6a4SMatthias Ringwald // CMAC calculation using AES Engineq 962d1a1f6a4SMatthias Ringwald #ifdef USE_CMAC_ENGINE 963514d35fcSMatthias Ringwald 964d1a1f6a4SMatthias Ringwald static void sm_cmac_done_trampoline(void * arg){ 965d1a1f6a4SMatthias Ringwald UNUSED(arg); 966d1a1f6a4SMatthias Ringwald sm_cmac_active = 0; 967d1a1f6a4SMatthias Ringwald (*sm_cmac_done_callback)(sm_cmac_hash); 96870b44dd4SMatthias Ringwald sm_trigger_run(); 9693deb3ec6SMatthias Ringwald } 970514d35fcSMatthias Ringwald 9714dfd504aSMatthias Ringwald int sm_cmac_ready(void){ 9724ea43905SMatthias Ringwald return sm_cmac_active == 0u; 9733deb3ec6SMatthias Ringwald } 9746d80b495SMatthias Ringwald #endif 9753deb3ec6SMatthias Ringwald 9766d80b495SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 977514d35fcSMatthias Ringwald // generic cmac calculation 978d1a1f6a4SMatthias Ringwald static void sm_cmac_message_start(const sm_key_t key, uint16_t message_len, const uint8_t * message, void (*done_callback)(uint8_t * hash)){ 979d1a1f6a4SMatthias Ringwald sm_cmac_active = 1; 980d1a1f6a4SMatthias Ringwald sm_cmac_done_callback = done_callback; 981d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_cmac_message(&sm_cmac_request, key, message_len, message, sm_cmac_hash, sm_cmac_done_trampoline, NULL); 9823deb3ec6SMatthias Ringwald } 9837a766ebfSMatthias Ringwald #endif 9843deb3ec6SMatthias Ringwald 985514d35fcSMatthias Ringwald // cmac for ATT Message signing 9867a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 987d1a1f6a4SMatthias Ringwald 988d1a1f6a4SMatthias Ringwald static void sm_cmac_generator_start(const sm_key_t key, uint16_t message_len, uint8_t (*get_byte_callback)(uint16_t offset), void (*done_callback)(uint8_t * hash)){ 989d1a1f6a4SMatthias Ringwald sm_cmac_active = 1; 990d1a1f6a4SMatthias Ringwald sm_cmac_done_callback = done_callback; 991d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_cmac_generator(&sm_cmac_request, key, message_len, get_byte_callback, sm_cmac_hash, sm_cmac_done_trampoline, NULL); 992d1a1f6a4SMatthias Ringwald } 993d1a1f6a4SMatthias Ringwald 9944dfd504aSMatthias Ringwald static uint8_t sm_cmac_signed_write_message_get_byte(uint16_t offset){ 995d1a1f6a4SMatthias Ringwald if (offset >= sm_cmac_signed_write_message_len) { 996d1a1f6a4SMatthias Ringwald log_error("sm_cmac_signed_write_message_get_byte. out of bounds, access %u, len %u", offset, sm_cmac_signed_write_message_len); 9974dfd504aSMatthias Ringwald return 0; 9984dfd504aSMatthias Ringwald } 9994dfd504aSMatthias Ringwald 1000d1a1f6a4SMatthias Ringwald offset = sm_cmac_signed_write_message_len - 1 - offset; 10014dfd504aSMatthias Ringwald 1002d1a1f6a4SMatthias Ringwald // sm_cmac_signed_write_header[3] | message[] | sm_cmac_signed_write_sign_counter[4] 10034dfd504aSMatthias Ringwald if (offset < 3){ 1004d1a1f6a4SMatthias Ringwald return sm_cmac_signed_write_header[offset]; 10054dfd504aSMatthias Ringwald } 1006d1a1f6a4SMatthias Ringwald int actual_message_len_incl_header = sm_cmac_signed_write_message_len - 4; 10074dfd504aSMatthias Ringwald if (offset < actual_message_len_incl_header){ 1008d1a1f6a4SMatthias Ringwald return sm_cmac_signed_write_message[offset - 3]; 10094dfd504aSMatthias Ringwald } 1010d1a1f6a4SMatthias Ringwald return sm_cmac_signed_write_sign_counter[offset - actual_message_len_incl_header]; 10114dfd504aSMatthias Ringwald } 10124dfd504aSMatthias Ringwald 10134dfd504aSMatthias Ringwald void sm_cmac_signed_write_start(const sm_key_t k, uint8_t opcode, hci_con_handle_t con_handle, uint16_t message_len, const uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t * hash)){ 1014514d35fcSMatthias Ringwald // ATT Message Signing 1015d1a1f6a4SMatthias Ringwald sm_cmac_signed_write_header[0] = opcode; 1016d1a1f6a4SMatthias Ringwald little_endian_store_16(sm_cmac_signed_write_header, 1, con_handle); 1017d1a1f6a4SMatthias Ringwald little_endian_store_32(sm_cmac_signed_write_sign_counter, 0, sign_counter); 1018514d35fcSMatthias Ringwald uint16_t total_message_len = 3 + message_len + 4; // incl. virtually prepended att opcode, handle and appended sign_counter in LE 1019d1a1f6a4SMatthias Ringwald sm_cmac_signed_write_message = message; 1020d1a1f6a4SMatthias Ringwald sm_cmac_signed_write_message_len = total_message_len; 1021d1a1f6a4SMatthias Ringwald sm_cmac_generator_start(k, total_message_len, &sm_cmac_signed_write_message_get_byte, done_handler); 10223deb3ec6SMatthias Ringwald } 10237a766ebfSMatthias Ringwald #endif 10243deb3ec6SMatthias Ringwald 10253deb3ec6SMatthias Ringwald static void sm_trigger_user_response(sm_connection_t * sm_conn){ 1026446a8c36SMatthias Ringwald // notify client for: JUST WORKS confirm, Numeric comparison confirm, PASSKEY display or input 10273deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_IDLE; 1028d77906ffSMatthias Ringwald sm_conn->sm_pairing_active = true; 10293deb3ec6SMatthias Ringwald switch (setup->sm_stk_generation_method){ 10303deb3ec6SMatthias Ringwald case PK_RESP_INPUT: 103142134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 10323deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PENDING; 10335611a760SMatthias Ringwald sm_notify_client_base(SM_EVENT_PASSKEY_INPUT_NUMBER, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address); 10343deb3ec6SMatthias Ringwald } else { 1035c9b8fdd9SMatthias Ringwald sm_notify_client_passkey(SM_EVENT_PASSKEY_DISPLAY_NUMBER, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, big_endian_read_32(setup->sm_tk, 12)); 10363deb3ec6SMatthias Ringwald } 10373deb3ec6SMatthias Ringwald break; 10383deb3ec6SMatthias Ringwald case PK_INIT_INPUT: 103942134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1040c9b8fdd9SMatthias Ringwald sm_notify_client_passkey(SM_EVENT_PASSKEY_DISPLAY_NUMBER, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, big_endian_read_32(setup->sm_tk, 12)); 10413deb3ec6SMatthias Ringwald } else { 10423deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PENDING; 10435611a760SMatthias Ringwald sm_notify_client_base(SM_EVENT_PASSKEY_INPUT_NUMBER, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address); 10443deb3ec6SMatthias Ringwald } 10453deb3ec6SMatthias Ringwald break; 104647fb4255SMatthias Ringwald case PK_BOTH_INPUT: 10473deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PENDING; 10485611a760SMatthias Ringwald sm_notify_client_base(SM_EVENT_PASSKEY_INPUT_NUMBER, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address); 10493deb3ec6SMatthias Ringwald break; 105047fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 1051446a8c36SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PENDING; 1052c8c46d51SMatthias Ringwald sm_notify_client_passkey(SM_EVENT_NUMERIC_COMPARISON_REQUEST, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, big_endian_read_32(setup->sm_tk, 12)); 105327c32905SMatthias Ringwald break; 10543deb3ec6SMatthias Ringwald case JUST_WORKS: 10553deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PENDING; 10565611a760SMatthias Ringwald sm_notify_client_base(SM_EVENT_JUST_WORKS_REQUEST, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address); 10573deb3ec6SMatthias Ringwald break; 10583deb3ec6SMatthias Ringwald case OOB: 10593deb3ec6SMatthias Ringwald // client already provided OOB data, let's skip notification. 10603deb3ec6SMatthias Ringwald break; 10617bbeb3adSMilanka Ringwald default: 10627bbeb3adSMilanka Ringwald btstack_assert(false); 10637bbeb3adSMilanka Ringwald break; 10643deb3ec6SMatthias Ringwald } 10653deb3ec6SMatthias Ringwald } 10663deb3ec6SMatthias Ringwald 10673deb3ec6SMatthias Ringwald static int sm_key_distribution_all_received(sm_connection_t * sm_conn){ 10683deb3ec6SMatthias Ringwald int recv_flags; 106942134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 107052f9cf63SMatthias Ringwald // slave / responder 10711ad129beSMatthias Ringwald recv_flags = sm_key_distribution_flags_for_set(sm_pairing_packet_get_initiator_key_distribution(setup->sm_s_pres)); 10723deb3ec6SMatthias Ringwald } else { 10733deb3ec6SMatthias Ringwald // master / initiator 10741ad129beSMatthias Ringwald recv_flags = sm_key_distribution_flags_for_set(sm_pairing_packet_get_responder_key_distribution(setup->sm_s_pres)); 10753deb3ec6SMatthias Ringwald } 1076eddc894fSMatthias Ringwald 1077eddc894fSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 1078eddc894fSMatthias Ringwald // LTK (= encyrption information & master identification) only used exchanged for LE Legacy Connection 1079eddc894fSMatthias Ringwald if (setup->sm_use_secure_connections){ 1080eddc894fSMatthias Ringwald recv_flags &= ~(SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION | SM_KEYDIST_FLAG_MASTER_IDENTIFICATION); 1081eddc894fSMatthias Ringwald } 1082eddc894fSMatthias Ringwald #endif 1083eddc894fSMatthias Ringwald 10843deb3ec6SMatthias Ringwald log_debug("sm_key_distribution_all_received: received 0x%02x, expecting 0x%02x", setup->sm_key_distribution_received_set, recv_flags); 108544263cccSMatthias Ringwald return (setup->sm_key_distribution_received_set & recv_flags) == recv_flags; 10863deb3ec6SMatthias Ringwald } 10873deb3ec6SMatthias Ringwald 1088711e6c80SMatthias Ringwald static void sm_done_for_handle(hci_con_handle_t con_handle){ 10897149bde5SMatthias Ringwald if (sm_active_connection_handle == con_handle){ 10903deb3ec6SMatthias Ringwald sm_timeout_stop(); 10917149bde5SMatthias Ringwald sm_active_connection_handle = HCI_CON_HANDLE_INVALID; 1092711e6c80SMatthias Ringwald log_info("sm: connection 0x%x released setup context", con_handle); 1093c085d9ffSMatthias Ringwald 1094c085d9ffSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 1095c085d9ffSMatthias Ringwald // generate new ec key after each pairing (that used it) 1096c085d9ffSMatthias Ringwald if (setup->sm_use_secure_connections){ 1097c085d9ffSMatthias Ringwald sm_ec_generate_new_key(); 1098c085d9ffSMatthias Ringwald } 1099c085d9ffSMatthias Ringwald #endif 11003deb3ec6SMatthias Ringwald } 11013deb3ec6SMatthias Ringwald } 11023deb3ec6SMatthias Ringwald 11037d1c0c3aSMatthias Ringwald static void sm_master_pairing_success(sm_connection_t *connection) {// master -> all done 11041dca9d8aSMatthias Ringwald connection->sm_engine_state = SM_INITIATOR_CONNECTED; 11050ccf6c9cSMatthias Ringwald sm_pairing_complete(connection, ERROR_CODE_SUCCESS, 0); 11061dca9d8aSMatthias Ringwald sm_done_for_handle(connection->sm_handle); 11071dca9d8aSMatthias Ringwald } 11081dca9d8aSMatthias Ringwald 11093deb3ec6SMatthias Ringwald static int sm_key_distribution_flags_for_auth_req(void){ 1110bb09604fSMatthias Ringwald 1111bb09604fSMatthias Ringwald int flags = SM_KEYDIST_ID_KEY; 11123deb3ec6SMatthias Ringwald if (sm_auth_req & SM_AUTHREQ_BONDING){ 1113bb09604fSMatthias Ringwald // encryption and signing information only if bonding requested 11143deb3ec6SMatthias Ringwald flags |= SM_KEYDIST_ENC_KEY; 1115bb09604fSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 1116bb09604fSMatthias Ringwald flags |= SM_KEYDIST_SIGN; 1117bb09604fSMatthias Ringwald #endif 11187ece0eaaSMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 11197ece0eaaSMatthias Ringwald // LinkKey for CTKD requires SC 11207ece0eaaSMatthias Ringwald if (sm_auth_req & SM_AUTHREQ_SECURE_CONNECTION){ 11217ece0eaaSMatthias Ringwald flags |= SM_KEYDIST_LINK_KEY; 11227ece0eaaSMatthias Ringwald } 11237ece0eaaSMatthias Ringwald #endif 11243deb3ec6SMatthias Ringwald } 11253deb3ec6SMatthias Ringwald return flags; 11263deb3ec6SMatthias Ringwald } 11273deb3ec6SMatthias Ringwald 1128d7471931SMatthias Ringwald static void sm_reset_setup(void){ 11293deb3ec6SMatthias Ringwald // fill in sm setup 1130901c000fSMatthias Ringwald setup->sm_state_vars = 0; 1131dd4a08fbSMatthias Ringwald setup->sm_keypress_notification = 0; 11323deb3ec6SMatthias Ringwald sm_reset_tk(); 1133d7471931SMatthias Ringwald } 1134d7471931SMatthias Ringwald 1135d7471931SMatthias Ringwald static void sm_init_setup(sm_connection_t * sm_conn){ 1136d7471931SMatthias Ringwald 1137d7471931SMatthias Ringwald // fill in sm setup 11383deb3ec6SMatthias Ringwald setup->sm_peer_addr_type = sm_conn->sm_peer_addr_type; 11396535961aSMatthias Ringwald (void)memcpy(setup->sm_peer_address, sm_conn->sm_peer_address, 6); 11403deb3ec6SMatthias Ringwald 1141a680ba6bSMatthias Ringwald // query client for Legacy Pairing OOB data 1142a680ba6bSMatthias Ringwald setup->sm_have_oob_data = 0; 11439305033eSMatthias Ringwald if (sm_get_oob_data != NULL) { 1144a680ba6bSMatthias Ringwald setup->sm_have_oob_data = (*sm_get_oob_data)(sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, setup->sm_tk); 11453deb3ec6SMatthias Ringwald } 11463deb3ec6SMatthias Ringwald 1147a680ba6bSMatthias Ringwald // if available and SC supported, also ask for SC OOB Data 1148a680ba6bSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 11494acf7b7bSMatthias Ringwald memset(setup->sm_ra, 0, 16); 11504acf7b7bSMatthias Ringwald memset(setup->sm_rb, 0, 16); 1151a680ba6bSMatthias Ringwald if (setup->sm_have_oob_data && (sm_auth_req & SM_AUTHREQ_SECURE_CONNECTION)){ 11529305033eSMatthias Ringwald if (sm_get_sc_oob_data != NULL){ 11534acf7b7bSMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1154a680ba6bSMatthias Ringwald setup->sm_have_oob_data = (*sm_get_sc_oob_data)( 1155a680ba6bSMatthias Ringwald sm_conn->sm_peer_addr_type, 1156a680ba6bSMatthias Ringwald sm_conn->sm_peer_address, 1157a680ba6bSMatthias Ringwald setup->sm_peer_confirm, 11584acf7b7bSMatthias Ringwald setup->sm_ra); 11594acf7b7bSMatthias Ringwald } else { 11604acf7b7bSMatthias Ringwald setup->sm_have_oob_data = (*sm_get_sc_oob_data)( 11614acf7b7bSMatthias Ringwald sm_conn->sm_peer_addr_type, 11624acf7b7bSMatthias Ringwald sm_conn->sm_peer_address, 11634acf7b7bSMatthias Ringwald setup->sm_peer_confirm, 11644acf7b7bSMatthias Ringwald setup->sm_rb); 11654acf7b7bSMatthias Ringwald } 1166a680ba6bSMatthias Ringwald } else { 1167a680ba6bSMatthias Ringwald setup->sm_have_oob_data = 0; 1168a680ba6bSMatthias Ringwald } 1169a680ba6bSMatthias Ringwald } 1170a680ba6bSMatthias Ringwald #endif 1171a680ba6bSMatthias Ringwald 11723deb3ec6SMatthias Ringwald sm_pairing_packet_t * local_packet; 117342134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 11743deb3ec6SMatthias Ringwald // slave 11753deb3ec6SMatthias Ringwald local_packet = &setup->sm_s_pres; 1176b95a5a35SMatthias Ringwald gap_le_get_own_address(&setup->sm_s_addr_type, setup->sm_s_address); 11773deb3ec6SMatthias Ringwald setup->sm_m_addr_type = sm_conn->sm_peer_addr_type; 11786535961aSMatthias Ringwald (void)memcpy(setup->sm_m_address, sm_conn->sm_peer_address, 6); 11793deb3ec6SMatthias Ringwald } else { 11803deb3ec6SMatthias Ringwald // master 11813deb3ec6SMatthias Ringwald local_packet = &setup->sm_m_preq; 1182b95a5a35SMatthias Ringwald gap_le_get_own_address(&setup->sm_m_addr_type, setup->sm_m_address); 11833deb3ec6SMatthias Ringwald setup->sm_s_addr_type = sm_conn->sm_peer_addr_type; 11846535961aSMatthias Ringwald (void)memcpy(setup->sm_s_address, sm_conn->sm_peer_address, 6); 11853deb3ec6SMatthias Ringwald 11863deb3ec6SMatthias Ringwald int key_distribution_flags = sm_key_distribution_flags_for_auth_req(); 11871ad129beSMatthias Ringwald sm_pairing_packet_set_initiator_key_distribution(setup->sm_m_preq, key_distribution_flags); 11881ad129beSMatthias Ringwald sm_pairing_packet_set_responder_key_distribution(setup->sm_m_preq, key_distribution_flags); 11893deb3ec6SMatthias Ringwald } 11903deb3ec6SMatthias Ringwald 119157132f12SMatthias Ringwald uint8_t auth_req = sm_auth_req & ~SM_AUTHREQ_CT2; 119257132f12SMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 119357132f12SMatthias Ringwald // set CT2 if SC + Bonding + CTKD 119457132f12SMatthias Ringwald const uint8_t auth_req_for_ct2 = SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING; 119557132f12SMatthias Ringwald if ((auth_req & auth_req_for_ct2) == auth_req_for_ct2){ 119657132f12SMatthias Ringwald auth_req |= SM_AUTHREQ_CT2; 119757132f12SMatthias Ringwald } 119857132f12SMatthias Ringwald #endif 11991ad129beSMatthias Ringwald sm_pairing_packet_set_io_capability(*local_packet, sm_io_capabilities); 1200a680ba6bSMatthias Ringwald sm_pairing_packet_set_oob_data_flag(*local_packet, setup->sm_have_oob_data); 1201df86eb96SMatthias Ringwald sm_pairing_packet_set_auth_req(*local_packet, auth_req); 12021ad129beSMatthias Ringwald sm_pairing_packet_set_max_encryption_key_size(*local_packet, sm_max_encryption_key_size); 12033deb3ec6SMatthias Ringwald } 12043deb3ec6SMatthias Ringwald 12053deb3ec6SMatthias Ringwald static int sm_stk_generation_init(sm_connection_t * sm_conn){ 12063deb3ec6SMatthias Ringwald 12073deb3ec6SMatthias Ringwald sm_pairing_packet_t * remote_packet; 12083deb3ec6SMatthias Ringwald int remote_key_request; 120942134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 121052f9cf63SMatthias Ringwald // slave / responder 12113deb3ec6SMatthias Ringwald remote_packet = &setup->sm_m_preq; 12121ad129beSMatthias Ringwald remote_key_request = sm_pairing_packet_get_responder_key_distribution(setup->sm_m_preq); 12133deb3ec6SMatthias Ringwald } else { 12143deb3ec6SMatthias Ringwald // master / initiator 12153deb3ec6SMatthias Ringwald remote_packet = &setup->sm_s_pres; 12161ad129beSMatthias Ringwald remote_key_request = sm_pairing_packet_get_initiator_key_distribution(setup->sm_s_pres); 12173deb3ec6SMatthias Ringwald } 12183deb3ec6SMatthias Ringwald 12193deb3ec6SMatthias Ringwald // check key size 12201ad129beSMatthias Ringwald sm_conn->sm_actual_encryption_key_size = sm_calc_actual_encryption_key_size(sm_pairing_packet_get_max_encryption_key_size(*remote_packet)); 12214ea43905SMatthias Ringwald if (sm_conn->sm_actual_encryption_key_size == 0u) return SM_REASON_ENCRYPTION_KEY_SIZE; 12223deb3ec6SMatthias Ringwald 1223eddc894fSMatthias Ringwald // decide on STK generation method / SC 12243deb3ec6SMatthias Ringwald sm_setup_tk(); 12253deb3ec6SMatthias Ringwald log_info("SMP: generation method %u", setup->sm_stk_generation_method); 12263deb3ec6SMatthias Ringwald 12273deb3ec6SMatthias Ringwald // check if STK generation method is acceptable by client 12283deb3ec6SMatthias Ringwald if (!sm_validate_stk_generation_method()) return SM_REASON_AUTHENTHICATION_REQUIREMENTS; 12293deb3ec6SMatthias Ringwald 1230eddc894fSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 12313cdbe9dbSMatthias Ringwald // check LE SC Only mode 12323cdbe9dbSMatthias Ringwald if (sm_sc_only_mode && (setup->sm_use_secure_connections == false)){ 12333cdbe9dbSMatthias Ringwald log_info("SC Only mode active but SC not possible"); 12343cdbe9dbSMatthias Ringwald return SM_REASON_AUTHENTHICATION_REQUIREMENTS; 12353cdbe9dbSMatthias Ringwald } 12363cdbe9dbSMatthias Ringwald 1237eddc894fSMatthias Ringwald // LTK (= encyrption information & master identification) only used exchanged for LE Legacy Connection 1238eddc894fSMatthias Ringwald if (setup->sm_use_secure_connections){ 1239eddc894fSMatthias Ringwald remote_key_request &= ~SM_KEYDIST_ENC_KEY; 1240eddc894fSMatthias Ringwald } 1241eddc894fSMatthias Ringwald #endif 1242eddc894fSMatthias Ringwald 124352f9cf63SMatthias Ringwald // identical to responder 124452f9cf63SMatthias Ringwald sm_setup_key_distribution(remote_key_request); 124552f9cf63SMatthias Ringwald 12463deb3ec6SMatthias Ringwald // JUST WORKS doens't provide authentication 1247c1ab6cc1SMatthias Ringwald sm_conn->sm_connection_authenticated = (setup->sm_stk_generation_method == JUST_WORKS) ? 0 : 1; 12483deb3ec6SMatthias Ringwald 12493deb3ec6SMatthias Ringwald return 0; 12503deb3ec6SMatthias Ringwald } 12513deb3ec6SMatthias Ringwald 12523deb3ec6SMatthias Ringwald static void sm_address_resolution_handle_event(address_resolution_event_t event){ 12533deb3ec6SMatthias Ringwald 12543deb3ec6SMatthias Ringwald // cache and reset context 12553deb3ec6SMatthias Ringwald int matched_device_id = sm_address_resolution_test; 12563deb3ec6SMatthias Ringwald address_resolution_mode_t mode = sm_address_resolution_mode; 12573deb3ec6SMatthias Ringwald void * context = sm_address_resolution_context; 12583deb3ec6SMatthias Ringwald 12593deb3ec6SMatthias Ringwald // reset context 12603deb3ec6SMatthias Ringwald sm_address_resolution_mode = ADDRESS_RESOLUTION_IDLE; 12613deb3ec6SMatthias Ringwald sm_address_resolution_context = NULL; 12623deb3ec6SMatthias Ringwald sm_address_resolution_test = -1; 1263711e6c80SMatthias Ringwald hci_con_handle_t con_handle = 0; 12643deb3ec6SMatthias Ringwald 12653deb3ec6SMatthias Ringwald sm_connection_t * sm_connection; 1266d2e90122SMatthias Ringwald sm_key_t ltk; 12671979f09cSMatthias Ringwald bool have_ltk; 12686c44b759SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12691979f09cSMatthias Ringwald bool trigger_pairing; 127042134bc6SMatthias Ringwald #endif 12713deb3ec6SMatthias Ringwald switch (mode){ 12723deb3ec6SMatthias Ringwald case ADDRESS_RESOLUTION_GENERAL: 12733deb3ec6SMatthias Ringwald break; 12743deb3ec6SMatthias Ringwald case ADDRESS_RESOLUTION_FOR_CONNECTION: 12753deb3ec6SMatthias Ringwald sm_connection = (sm_connection_t *) context; 1276711e6c80SMatthias Ringwald con_handle = sm_connection->sm_handle; 12776c44b759SMatthias Ringwald 12786c44b759SMatthias Ringwald // have ltk -> start encryption / send security request 12796c44b759SMatthias Ringwald // Core 5, Vol 3, Part C, 10.3.2 Initiating a Service Request 12806c44b759SMatthias Ringwald // "When a bond has been created between two devices, any reconnection should result in the local device 12816c44b759SMatthias Ringwald // enabling or requesting encryption with the remote device before initiating any service request." 12826c44b759SMatthias Ringwald 12833deb3ec6SMatthias Ringwald switch (event){ 1284a66b030fSMatthias Ringwald case ADDRESS_RESOLUTION_SUCCEEDED: 12853deb3ec6SMatthias Ringwald sm_connection->sm_irk_lookup_state = IRK_LOOKUP_SUCCEEDED; 12863deb3ec6SMatthias Ringwald sm_connection->sm_le_db_index = matched_device_id; 1287a66b030fSMatthias Ringwald log_info("ADDRESS_RESOLUTION_SUCCEEDED, index %d", sm_connection->sm_le_db_index); 12886c44b759SMatthias Ringwald 12896c44b759SMatthias Ringwald le_device_db_encryption_get(sm_connection->sm_le_db_index, NULL, NULL, ltk, NULL, NULL, NULL, NULL); 12906c44b759SMatthias Ringwald have_ltk = !sm_is_null_key(ltk); 12916c44b759SMatthias Ringwald 12926c39055aSMatthias Ringwald if (sm_connection->sm_role) { 12936c44b759SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1294212d735eSMatthias Ringwald // IRK required before, continue 12956c39055aSMatthias Ringwald if (sm_connection->sm_engine_state == SM_RESPONDER_PH0_RECEIVED_LTK_W4_IRK){ 12966c39055aSMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_PH0_RECEIVED_LTK_REQUEST; 12976c39055aSMatthias Ringwald break; 12986c39055aSMatthias Ringwald } 1299212d735eSMatthias Ringwald if (sm_connection->sm_engine_state == SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED_W4_IRK){ 1300212d735eSMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED; 1301212d735eSMatthias Ringwald break; 1302212d735eSMatthias Ringwald } 13037af5dcd5SMatthias Ringwald bool trigger_security_request = (sm_connection->sm_pairing_requested != 0) || (sm_slave_request_security != 0); 13047af5dcd5SMatthias Ringwald sm_connection->sm_pairing_requested = 0; 13056c44b759SMatthias Ringwald #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION 13067af5dcd5SMatthias Ringwald // trigger security request for Proactive Authentication if LTK available 13077af5dcd5SMatthias Ringwald trigger_security_request = trigger_security_request || have_ltk; 13086c44b759SMatthias Ringwald #endif 13097af5dcd5SMatthias Ringwald 13107af5dcd5SMatthias Ringwald log_info("peripheral: pairing request local %u, have_ltk %u => trigger_security_request %u", 13111979f09cSMatthias Ringwald sm_connection->sm_pairing_requested, (int) have_ltk, trigger_security_request); 13127af5dcd5SMatthias Ringwald 13137af5dcd5SMatthias Ringwald if (trigger_security_request){ 13147af5dcd5SMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST; 13157af5dcd5SMatthias Ringwald if (have_ltk){ 13167af5dcd5SMatthias Ringwald sm_reencryption_started(sm_connection); 13177af5dcd5SMatthias Ringwald } else { 13187af5dcd5SMatthias Ringwald sm_pairing_started(sm_connection); 13197af5dcd5SMatthias Ringwald } 13207af5dcd5SMatthias Ringwald sm_trigger_run(); 13216c44b759SMatthias Ringwald } 13226c44b759SMatthias Ringwald #endif 13236c44b759SMatthias Ringwald } else { 13246c44b759SMatthias Ringwald 132542134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 13266c44b759SMatthias Ringwald // check if pairing already requested and reset requests 13276c44b759SMatthias Ringwald trigger_pairing = sm_connection->sm_pairing_requested || sm_connection->sm_security_request_received; 13286c44b759SMatthias Ringwald log_info("central: pairing request local %u, remote %u => trigger_pairing %u. have_ltk %u", 13291979f09cSMatthias Ringwald sm_connection->sm_pairing_requested, sm_connection->sm_security_request_received, (int) trigger_pairing, (int) have_ltk); 13303deb3ec6SMatthias Ringwald sm_connection->sm_security_request_received = 0; 133109ea1b62SMatthias Ringwald sm_connection->sm_pairing_requested = 0; 133232bc5d65SMatthias Ringwald bool trigger_reencryption = false; 1333c245ca32SMatthias Ringwald 1334d4af1595SMatthias Ringwald if (have_ltk){ 13356a43f611SMatthias Ringwald #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION 1336b187cc61SMatthias Ringwald trigger_reencryption = true; 133732bc5d65SMatthias Ringwald #else 133832bc5d65SMatthias Ringwald if (trigger_pairing){ 133932bc5d65SMatthias Ringwald trigger_reencryption = true; 134032bc5d65SMatthias Ringwald } else { 134132bc5d65SMatthias Ringwald log_info("central: defer enabling encryption for bonded device"); 134232bc5d65SMatthias Ringwald } 134332bc5d65SMatthias Ringwald #endif 134432bc5d65SMatthias Ringwald } 134532bc5d65SMatthias Ringwald 134632bc5d65SMatthias Ringwald if (trigger_reencryption){ 13476c44b759SMatthias Ringwald log_info("central: enable encryption for bonded device"); 13485567aa60SMatthias Ringwald sm_connection->sm_engine_state = SM_INITIATOR_PH4_HAS_LTK; 1349d4af1595SMatthias Ringwald break; 1350d4af1595SMatthias Ringwald } 13516c44b759SMatthias Ringwald 13526c44b759SMatthias Ringwald // pairing_request -> send pairing request 13536c44b759SMatthias Ringwald if (trigger_pairing){ 13543deb3ec6SMatthias Ringwald sm_connection->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 1355d4af1595SMatthias Ringwald break; 13563deb3ec6SMatthias Ringwald } 135742134bc6SMatthias Ringwald #endif 1358298ab52bSMatthias Ringwald } 13593deb3ec6SMatthias Ringwald break; 13603deb3ec6SMatthias Ringwald case ADDRESS_RESOLUTION_FAILED: 13613deb3ec6SMatthias Ringwald sm_connection->sm_irk_lookup_state = IRK_LOOKUP_FAILED; 13626c39055aSMatthias Ringwald if (sm_connection->sm_role) { 13637af5dcd5SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 13646c39055aSMatthias Ringwald // LTK request received before, IRK required -> negative LTK reply 13656c39055aSMatthias Ringwald if (sm_connection->sm_engine_state == SM_RESPONDER_PH0_RECEIVED_LTK_W4_IRK){ 13666c39055aSMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY; 13676c39055aSMatthias Ringwald } 13687af5dcd5SMatthias Ringwald // send security request if requested 13697af5dcd5SMatthias Ringwald bool trigger_security_request = (sm_connection->sm_pairing_requested != 0) || (sm_slave_request_security != 0); 13707af5dcd5SMatthias Ringwald sm_connection->sm_pairing_requested = 0; 13717af5dcd5SMatthias Ringwald if (trigger_security_request){ 13727af5dcd5SMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST; 13737af5dcd5SMatthias Ringwald sm_pairing_started(sm_connection); 13747af5dcd5SMatthias Ringwald } 13756c39055aSMatthias Ringwald break; 13767af5dcd5SMatthias Ringwald #endif 13776c39055aSMatthias Ringwald } 137842134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 137909ea1b62SMatthias Ringwald if (!sm_connection->sm_pairing_requested && !sm_connection->sm_security_request_received) break; 13803deb3ec6SMatthias Ringwald sm_connection->sm_security_request_received = 0; 138109ea1b62SMatthias Ringwald sm_connection->sm_pairing_requested = 0; 13823deb3ec6SMatthias Ringwald sm_connection->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 138342134bc6SMatthias Ringwald #endif 13843deb3ec6SMatthias Ringwald break; 13857bbeb3adSMilanka Ringwald 13867bbeb3adSMilanka Ringwald default: 13877bbeb3adSMilanka Ringwald btstack_assert(false); 13887bbeb3adSMilanka Ringwald break; 13893deb3ec6SMatthias Ringwald } 13903deb3ec6SMatthias Ringwald break; 13913deb3ec6SMatthias Ringwald default: 13923deb3ec6SMatthias Ringwald break; 13933deb3ec6SMatthias Ringwald } 13943deb3ec6SMatthias Ringwald 13953deb3ec6SMatthias Ringwald switch (event){ 1396a66b030fSMatthias Ringwald case ADDRESS_RESOLUTION_SUCCEEDED: 1397711e6c80SMatthias Ringwald sm_notify_client_index(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, con_handle, sm_address_resolution_addr_type, sm_address_resolution_address, matched_device_id); 13983deb3ec6SMatthias Ringwald break; 13993deb3ec6SMatthias Ringwald case ADDRESS_RESOLUTION_FAILED: 1400711e6c80SMatthias Ringwald sm_notify_client_base(SM_EVENT_IDENTITY_RESOLVING_FAILED, con_handle, sm_address_resolution_addr_type, sm_address_resolution_address); 14013deb3ec6SMatthias Ringwald break; 14027bbeb3adSMilanka Ringwald default: 14037bbeb3adSMilanka Ringwald btstack_assert(false); 14047bbeb3adSMilanka Ringwald break; 14053deb3ec6SMatthias Ringwald } 14063deb3ec6SMatthias Ringwald } 14073deb3ec6SMatthias Ringwald 14083deb3ec6SMatthias Ringwald static void sm_key_distribution_handle_all_received(sm_connection_t * sm_conn){ 14093deb3ec6SMatthias Ringwald 14103deb3ec6SMatthias Ringwald int le_db_index = -1; 14113deb3ec6SMatthias Ringwald 141227ef8bc8SMatthias Ringwald // only store pairing information if both sides are bondable, i.e., the bonadble flag is set 14131979f09cSMatthias Ringwald bool bonding_enabed = ( sm_pairing_packet_get_auth_req(setup->sm_m_preq) 141427ef8bc8SMatthias Ringwald & sm_pairing_packet_get_auth_req(setup->sm_s_pres) 14154ea43905SMatthias Ringwald & SM_AUTHREQ_BONDING ) != 0u; 141627ef8bc8SMatthias Ringwald 141727ef8bc8SMatthias Ringwald if (bonding_enabed){ 141827ef8bc8SMatthias Ringwald 14193deb3ec6SMatthias Ringwald // lookup device based on IRK 14203deb3ec6SMatthias Ringwald if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ 14213deb3ec6SMatthias Ringwald int i; 1422092ec58eSMatthias Ringwald for (i=0; i < le_device_db_max_count(); i++){ 14233deb3ec6SMatthias Ringwald sm_key_t irk; 14243deb3ec6SMatthias Ringwald bd_addr_t address; 1425c7e2c1a5SMatthias Ringwald int address_type = BD_ADDR_TYPE_UNKNOWN; 14263deb3ec6SMatthias Ringwald le_device_db_info(i, &address_type, address, irk); 1427adf5eaa9SMatthias Ringwald // skip unused entries 1428c7e2c1a5SMatthias Ringwald if (address_type == BD_ADDR_TYPE_UNKNOWN) continue; 1429c7e2c1a5SMatthias Ringwald // compare IRK 1430c7e2c1a5SMatthias Ringwald if (memcmp(irk, setup->sm_peer_irk, 16) != 0) continue; 1431c7e2c1a5SMatthias Ringwald 14323deb3ec6SMatthias Ringwald log_info("sm: device found for IRK, updating"); 14333deb3ec6SMatthias Ringwald le_db_index = i; 14343deb3ec6SMatthias Ringwald break; 14353deb3ec6SMatthias Ringwald } 1436c7e2c1a5SMatthias Ringwald } else { 1437c7e2c1a5SMatthias Ringwald // assert IRK is set to zero 1438c7e2c1a5SMatthias Ringwald memset(setup->sm_peer_irk, 0, 16); 14393deb3ec6SMatthias Ringwald } 14403deb3ec6SMatthias Ringwald 14413deb3ec6SMatthias Ringwald // if not found, lookup via public address if possible 14423deb3ec6SMatthias Ringwald log_info("sm peer addr type %u, peer addres %s", setup->sm_peer_addr_type, bd_addr_to_str(setup->sm_peer_address)); 1443c1ab6cc1SMatthias Ringwald if ((le_db_index < 0) && (setup->sm_peer_addr_type == BD_ADDR_TYPE_LE_PUBLIC)){ 14443deb3ec6SMatthias Ringwald int i; 1445092ec58eSMatthias Ringwald for (i=0; i < le_device_db_max_count(); i++){ 14463deb3ec6SMatthias Ringwald bd_addr_t address; 1447adf5eaa9SMatthias Ringwald int address_type = BD_ADDR_TYPE_UNKNOWN; 14483deb3ec6SMatthias Ringwald le_device_db_info(i, &address_type, address, NULL); 1449adf5eaa9SMatthias Ringwald // skip unused entries 1450adf5eaa9SMatthias Ringwald if (address_type == BD_ADDR_TYPE_UNKNOWN) continue; 14513deb3ec6SMatthias Ringwald log_info("device %u, sm peer addr type %u, peer addres %s", i, address_type, bd_addr_to_str(address)); 14525df9dc78SMatthias Ringwald if ((address_type == BD_ADDR_TYPE_LE_PUBLIC) && (memcmp(address, setup->sm_peer_address, 6) == 0)){ 14533deb3ec6SMatthias Ringwald log_info("sm: device found for public address, updating"); 14543deb3ec6SMatthias Ringwald le_db_index = i; 14553deb3ec6SMatthias Ringwald break; 14563deb3ec6SMatthias Ringwald } 14573deb3ec6SMatthias Ringwald } 14583deb3ec6SMatthias Ringwald } 14593deb3ec6SMatthias Ringwald 14603deb3ec6SMatthias Ringwald // if not found, add to db 146102b02cffSMatthias Ringwald bool new_to_le_device_db = false; 14623deb3ec6SMatthias Ringwald if (le_db_index < 0) { 14633deb3ec6SMatthias Ringwald le_db_index = le_device_db_add(setup->sm_peer_addr_type, setup->sm_peer_address, setup->sm_peer_irk); 146402b02cffSMatthias Ringwald new_to_le_device_db = true; 14653deb3ec6SMatthias Ringwald } 14663deb3ec6SMatthias Ringwald 14673deb3ec6SMatthias Ringwald if (le_db_index >= 0){ 14683deb3ec6SMatthias Ringwald 146902b02cffSMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 147002b02cffSMatthias Ringwald if (!new_to_le_device_db){ 147102b02cffSMatthias Ringwald hci_remove_le_device_db_entry_from_resolving_list(le_db_index); 147202b02cffSMatthias Ringwald } 147302b02cffSMatthias Ringwald hci_load_le_device_db_entry_into_resolving_list(le_db_index); 147402b02cffSMatthias Ringwald #else 147502b02cffSMatthias Ringwald UNUSED(new_to_le_device_db); 147602b02cffSMatthias Ringwald #endif 147702b02cffSMatthias Ringwald 147848163929SMatthias Ringwald sm_notify_client_index(SM_EVENT_IDENTITY_CREATED, sm_conn->sm_handle, setup->sm_peer_addr_type, setup->sm_peer_address, le_db_index); 1479e1086030SMatthias Ringwald sm_conn->sm_irk_lookup_state = IRK_LOOKUP_SUCCEEDED; 148048163929SMatthias Ringwald 1481eda85fbfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 14823deb3ec6SMatthias Ringwald // store local CSRK 1483715a43d1SMatthias Ringwald setup->sm_le_device_index = le_db_index; 1484715a43d1SMatthias Ringwald if ((setup->sm_key_distribution_sent_set) & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 14853deb3ec6SMatthias Ringwald log_info("sm: store local CSRK"); 14863deb3ec6SMatthias Ringwald le_device_db_local_csrk_set(le_db_index, setup->sm_local_csrk); 14873deb3ec6SMatthias Ringwald le_device_db_local_counter_set(le_db_index, 0); 14883deb3ec6SMatthias Ringwald } 14893deb3ec6SMatthias Ringwald 14903deb3ec6SMatthias Ringwald // store remote CSRK 14913deb3ec6SMatthias Ringwald if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 14923deb3ec6SMatthias Ringwald log_info("sm: store remote CSRK"); 14933deb3ec6SMatthias Ringwald le_device_db_remote_csrk_set(le_db_index, setup->sm_peer_csrk); 14943deb3ec6SMatthias Ringwald le_device_db_remote_counter_set(le_db_index, 0); 14953deb3ec6SMatthias Ringwald } 1496eda85fbfSMatthias Ringwald #endif 149778f44163SMatthias Ringwald // store encryption information for secure connections: LTK generated by ECDH 149878f44163SMatthias Ringwald if (setup->sm_use_secure_connections){ 1499e6343eb6SMatthias Ringwald log_info("sm: store SC LTK (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 150078f44163SMatthias Ringwald uint8_t zero_rand[8]; 150178f44163SMatthias Ringwald memset(zero_rand, 0, 8); 150278f44163SMatthias Ringwald le_device_db_encryption_set(le_db_index, 0, zero_rand, setup->sm_ltk, sm_conn->sm_actual_encryption_key_size, 15033dc3a67dSMatthias Ringwald sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED, 1); 150478f44163SMatthias Ringwald } 150578f44163SMatthias Ringwald 1506e6343eb6SMatthias Ringwald // store encryption information for legacy pairing: peer LTK, EDIV, RAND 150778f44163SMatthias Ringwald else if ( (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION) 150878f44163SMatthias Ringwald && (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION )){ 1509e6343eb6SMatthias Ringwald log_info("sm: set encryption information (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 15103deb3ec6SMatthias Ringwald le_device_db_encryption_set(le_db_index, setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk, 15113dc3a67dSMatthias Ringwald sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED, 0); 151278f44163SMatthias Ringwald 15133deb3ec6SMatthias Ringwald } 15143deb3ec6SMatthias Ringwald } 151527ef8bc8SMatthias Ringwald } else { 151627ef8bc8SMatthias Ringwald log_info("Ignoring received keys, bonding not enabled"); 151727ef8bc8SMatthias Ringwald } 15183deb3ec6SMatthias Ringwald 15193deb3ec6SMatthias Ringwald // keep le_db_index 15203deb3ec6SMatthias Ringwald sm_conn->sm_le_db_index = le_db_index; 15213deb3ec6SMatthias Ringwald } 15223deb3ec6SMatthias Ringwald 1523688a08f9SMatthias Ringwald static void sm_pairing_error(sm_connection_t * sm_conn, uint8_t reason){ 1524688a08f9SMatthias Ringwald setup->sm_pairing_failed_reason = reason; 1525688a08f9SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; 1526688a08f9SMatthias Ringwald } 1527688a08f9SMatthias Ringwald 1528688a08f9SMatthias Ringwald static inline void sm_pdu_received_in_wrong_state(sm_connection_t * sm_conn){ 1529688a08f9SMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_UNSPECIFIED_REASON); 1530688a08f9SMatthias Ringwald } 1531688a08f9SMatthias Ringwald 15329af0f475SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 1533688a08f9SMatthias Ringwald 1534dc300847SMatthias Ringwald static void sm_sc_prepare_dhkey_check(sm_connection_t * sm_conn); 1535945888f5SMatthias Ringwald static int sm_passkey_used(stk_generation_method_t method); 1536f1c1783eSMatthias Ringwald static int sm_just_works_or_numeric_comparison(stk_generation_method_t method); 1537dc300847SMatthias Ringwald 1538b35a3de2SMatthias Ringwald static void sm_sc_start_calculating_local_confirm(sm_connection_t * sm_conn){ 1539b90c4e75SMatthias Ringwald if (setup->sm_stk_generation_method == OOB){ 15401f9d84e9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CMAC_FOR_CONFIRMATION; 1541b90c4e75SMatthias Ringwald } else { 1542b90c4e75SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_nonce, 16, &sm_handle_random_result_sc_next_w2_cmac_for_confirmation, (void *)(uintptr_t) sm_conn->sm_handle); 1543b35a3de2SMatthias Ringwald } 1544b35a3de2SMatthias Ringwald } 1545b35a3de2SMatthias Ringwald 1546688a08f9SMatthias Ringwald static void sm_sc_state_after_receiving_random(sm_connection_t * sm_conn){ 154742134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1548688a08f9SMatthias Ringwald // Responder 15494acf7b7bSMatthias Ringwald if (setup->sm_stk_generation_method == OOB){ 15504acf7b7bSMatthias Ringwald // generate Nb 15514acf7b7bSMatthias Ringwald log_info("Generate Nb"); 15526ca80073SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_nonce, 16, &sm_handle_random_result_sc_next_send_pairing_random, (void *)(uintptr_t) sm_conn->sm_handle); 15534acf7b7bSMatthias Ringwald } else { 1554688a08f9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PAIRING_RANDOM; 15554acf7b7bSMatthias Ringwald } 1556688a08f9SMatthias Ringwald } else { 1557688a08f9SMatthias Ringwald // Initiator role 1558688a08f9SMatthias Ringwald switch (setup->sm_stk_generation_method){ 1559688a08f9SMatthias Ringwald case JUST_WORKS: 1560dc300847SMatthias Ringwald sm_sc_prepare_dhkey_check(sm_conn); 1561688a08f9SMatthias Ringwald break; 1562688a08f9SMatthias Ringwald 156347fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 1564bd57ffebSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_G2; 1565688a08f9SMatthias Ringwald break; 1566688a08f9SMatthias Ringwald case PK_INIT_INPUT: 1567688a08f9SMatthias Ringwald case PK_RESP_INPUT: 156847fb4255SMatthias Ringwald case PK_BOTH_INPUT: 15694ea43905SMatthias Ringwald if (setup->sm_passkey_bit < 20u) { 1570b35a3de2SMatthias Ringwald sm_sc_start_calculating_local_confirm(sm_conn); 1571688a08f9SMatthias Ringwald } else { 1572dc300847SMatthias Ringwald sm_sc_prepare_dhkey_check(sm_conn); 1573688a08f9SMatthias Ringwald } 1574688a08f9SMatthias Ringwald break; 1575688a08f9SMatthias Ringwald case OOB: 157665a9a04eSMatthias Ringwald sm_sc_prepare_dhkey_check(sm_conn); 1577688a08f9SMatthias Ringwald break; 15787bbeb3adSMilanka Ringwald default: 15797bbeb3adSMilanka Ringwald btstack_assert(false); 15807bbeb3adSMilanka Ringwald break; 1581688a08f9SMatthias Ringwald } 1582688a08f9SMatthias Ringwald } 1583688a08f9SMatthias Ringwald } 1584688a08f9SMatthias Ringwald 1585aec94140SMatthias Ringwald static void sm_sc_cmac_done(uint8_t * hash){ 1586688a08f9SMatthias Ringwald log_info("sm_sc_cmac_done: "); 1587688a08f9SMatthias Ringwald log_info_hexdump(hash, 16); 1588688a08f9SMatthias Ringwald 1589c59d0c92SMatthias Ringwald if (sm_sc_oob_state == SM_SC_OOB_W4_CONFIRM){ 1590c59d0c92SMatthias Ringwald sm_sc_oob_state = SM_SC_OOB_IDLE; 1591a680ba6bSMatthias Ringwald (*sm_sc_oob_callback)(hash, sm_sc_oob_random); 1592c59d0c92SMatthias Ringwald return; 1593c59d0c92SMatthias Ringwald } 1594c59d0c92SMatthias Ringwald 1595bd57ffebSMatthias Ringwald sm_connection_t * sm_conn = sm_cmac_connection; 1596bd57ffebSMatthias Ringwald sm_cmac_connection = NULL; 15976857ad8fSMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 15982bacf595SMatthias Ringwald link_key_type_t link_key_type; 1599b4f65634SMatthias Ringwald #endif 1600bd57ffebSMatthias Ringwald 1601bd57ffebSMatthias Ringwald switch (sm_conn->sm_engine_state){ 1602aec94140SMatthias Ringwald case SM_SC_W4_CMAC_FOR_CONFIRMATION: 16036535961aSMatthias Ringwald (void)memcpy(setup->sm_local_confirm, hash, 16); 1604bd57ffebSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_CONFIRMATION; 1605aec94140SMatthias Ringwald break; 1606688a08f9SMatthias Ringwald case SM_SC_W4_CMAC_FOR_CHECK_CONFIRMATION: 1607688a08f9SMatthias Ringwald // check 1608688a08f9SMatthias Ringwald if (0 != memcmp(hash, setup->sm_peer_confirm, 16)){ 1609bd57ffebSMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_CONFIRM_VALUE_FAILED); 1610688a08f9SMatthias Ringwald break; 1611688a08f9SMatthias Ringwald } 1612bd57ffebSMatthias Ringwald sm_sc_state_after_receiving_random(sm_conn); 1613688a08f9SMatthias Ringwald break; 1614901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_G2: { 1615901c000fSMatthias Ringwald uint32_t vab = big_endian_read_32(hash, 12) % 1000000; 1616901c000fSMatthias Ringwald big_endian_store_32(setup->sm_tk, 12, vab); 1617901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_USER_RESPONSE; 1618901c000fSMatthias Ringwald sm_trigger_user_response(sm_conn); 1619019005a0SMatthias Ringwald break; 1620019005a0SMatthias Ringwald } 16210346c37cSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_SALT: 16226535961aSMatthias Ringwald (void)memcpy(setup->sm_t, hash, 16); 1623bd57ffebSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_MACKEY; 16240346c37cSMatthias Ringwald break; 16250346c37cSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_MACKEY: 16266535961aSMatthias Ringwald (void)memcpy(setup->sm_mackey, hash, 16); 1627bd57ffebSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_LTK; 16280346c37cSMatthias Ringwald break; 16290346c37cSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_LTK: 1630b18300a6SMatthias Ringwald // truncate sm_ltk, but keep full LTK for cross-transport key derivation in sm_local_ltk 1631b18300a6SMatthias Ringwald // Errata Service Release to the Bluetooth Specification: ESR09 1632b18300a6SMatthias Ringwald // E6405 – Cross transport key derivation from a key of size less than 128 bits 1633b18300a6SMatthias Ringwald // Note: When the BR/EDR link key is being derived from the LTK, the derivation is done before the LTK gets masked." 16346535961aSMatthias Ringwald (void)memcpy(setup->sm_ltk, hash, 16); 16356535961aSMatthias Ringwald (void)memcpy(setup->sm_local_ltk, hash, 16); 1636893e9333SMatthias Ringwald sm_truncate_key(setup->sm_ltk, sm_conn->sm_actual_encryption_key_size); 1637bd57ffebSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F6_FOR_DHKEY_CHECK; 1638019005a0SMatthias Ringwald break; 1639901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F6_FOR_DHKEY_CHECK: 16406535961aSMatthias Ringwald (void)memcpy(setup->sm_local_dhkey_check, hash, 16); 164142134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1642901c000fSMatthias Ringwald // responder 1643901c000fSMatthias Ringwald if (setup->sm_state_vars & SM_STATE_VAR_DHKEY_COMMAND_RECEIVED){ 1644901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F6_TO_VERIFY_DHKEY_CHECK; 1645901c000fSMatthias Ringwald } else { 1646901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_DHKEY_CHECK_COMMAND; 1647901c000fSMatthias Ringwald } 1648901c000fSMatthias Ringwald } else { 1649901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_DHKEY_CHECK_COMMAND; 1650901c000fSMatthias Ringwald } 1651901c000fSMatthias Ringwald break; 1652901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F6_TO_VERIFY_DHKEY_CHECK: 1653901c000fSMatthias Ringwald if (0 != memcmp(hash, setup->sm_peer_dhkey_check, 16) ){ 1654901c000fSMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_DHKEY_CHECK_FAILED); 1655aec94140SMatthias Ringwald break; 1656aec94140SMatthias Ringwald } 165742134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1658901c000fSMatthias Ringwald // responder 1659901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_DHKEY_CHECK_COMMAND; 1660901c000fSMatthias Ringwald } else { 1661901c000fSMatthias Ringwald // initiator 1662901c000fSMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH3_SEND_START_ENCRYPTION; 1663bd57ffebSMatthias Ringwald } 1664901c000fSMatthias Ringwald break; 16656857ad8fSMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 166657132f12SMatthias Ringwald case SM_SC_W4_CALCULATE_ILK: 16676535961aSMatthias Ringwald (void)memcpy(setup->sm_t, hash, 16); 166857132f12SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_BR_EDR_LINK_KEY; 16692bacf595SMatthias Ringwald break; 167057132f12SMatthias Ringwald case SM_SC_W4_CALCULATE_BR_EDR_LINK_KEY: 16712bacf595SMatthias Ringwald reverse_128(hash, setup->sm_t); 16722bacf595SMatthias Ringwald link_key_type = sm_conn->sm_connection_authenticated ? 16732bacf595SMatthias Ringwald AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256 : UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256; 16748974e43fSMatthias Ringwald log_info("Derived classic link key from LE using h6, type %u", (int) link_key_type); 167555160b1cSMatthias Ringwald gap_store_link_key_for_bd_addr(setup->sm_peer_address, setup->sm_t, link_key_type); 16768974e43fSMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 16772bacf595SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_IDLE; 16782bacf595SMatthias Ringwald } else { 16792bacf595SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED; 16802bacf595SMatthias Ringwald } 16810ccf6c9cSMatthias Ringwald sm_pairing_complete(sm_conn, ERROR_CODE_SUCCESS, 0); 16822bacf595SMatthias Ringwald sm_done_for_handle(sm_conn->sm_handle); 16832bacf595SMatthias Ringwald break; 1684bdb14b0eSMatthias Ringwald #endif 1685bd57ffebSMatthias Ringwald default: 1686bd57ffebSMatthias Ringwald log_error("sm_sc_cmac_done in state %u", sm_conn->sm_engine_state); 1687bd57ffebSMatthias Ringwald break; 1688bd57ffebSMatthias Ringwald } 168970b44dd4SMatthias Ringwald sm_trigger_run(); 1690aec94140SMatthias Ringwald } 1691aec94140SMatthias Ringwald 1692688a08f9SMatthias Ringwald static void f4_engine(sm_connection_t * sm_conn, const sm_key256_t u, const sm_key256_t v, const sm_key_t x, uint8_t z){ 1693dc300847SMatthias Ringwald const uint16_t message_len = 65; 1694aec94140SMatthias Ringwald sm_cmac_connection = sm_conn; 16956535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer, u, 32); 16966535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 32, v, 32); 1697aec94140SMatthias Ringwald sm_cmac_sc_buffer[64] = z; 1698aec94140SMatthias Ringwald log_info("f4 key"); 1699aec94140SMatthias Ringwald log_info_hexdump(x, 16); 1700aec94140SMatthias Ringwald log_info("f4 message"); 1701dc300847SMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1702d1a1f6a4SMatthias Ringwald sm_cmac_message_start(x, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 1703aec94140SMatthias Ringwald } 1704aec94140SMatthias Ringwald 17050346c37cSMatthias Ringwald static const uint8_t f5_key_id[] = { 0x62, 0x74, 0x6c, 0x65 }; 17060346c37cSMatthias Ringwald static const uint8_t f5_length[] = { 0x01, 0x00}; 17070346c37cSMatthias Ringwald 17080346c37cSMatthias Ringwald static void f5_calculate_salt(sm_connection_t * sm_conn){ 17098334d3d8SMatthias Ringwald 17108334d3d8SMatthias Ringwald static const sm_key_t f5_salt = { 0x6C ,0x88, 0x83, 0x91, 0xAA, 0xF5, 0xA5, 0x38, 0x60, 0x37, 0x0B, 0xDB, 0x5A, 0x60, 0x83, 0xBE}; 17118334d3d8SMatthias Ringwald 171240c5d850SMatthias Ringwald log_info("f5_calculate_salt"); 17130346c37cSMatthias Ringwald // calculate salt for f5 17140346c37cSMatthias Ringwald const uint16_t message_len = 32; 17150346c37cSMatthias Ringwald sm_cmac_connection = sm_conn; 17166535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer, setup->sm_dhkey, message_len); 1717d1a1f6a4SMatthias Ringwald sm_cmac_message_start(f5_salt, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 17180346c37cSMatthias Ringwald } 17190346c37cSMatthias Ringwald 17200346c37cSMatthias Ringwald static inline void f5_mackkey(sm_connection_t * sm_conn, sm_key_t t, const sm_key_t n1, const sm_key_t n2, const sm_key56_t a1, const sm_key56_t a2){ 17210346c37cSMatthias Ringwald const uint16_t message_len = 53; 17220346c37cSMatthias Ringwald sm_cmac_connection = sm_conn; 17230346c37cSMatthias Ringwald 17240346c37cSMatthias Ringwald // f5(W, N1, N2, A1, A2) = AES-CMACT (Counter = 0 || keyID || N1 || N2|| A1|| A2 || Length = 256) -- this is the MacKey 17250346c37cSMatthias Ringwald sm_cmac_sc_buffer[0] = 0; 17266535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 01, f5_key_id, 4); 17276535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 05, n1, 16); 17286535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 21, n2, 16); 17296535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 37, a1, 7); 17306535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 44, a2, 7); 17316535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 51, f5_length, 2); 17320346c37cSMatthias Ringwald log_info("f5 key"); 17330346c37cSMatthias Ringwald log_info_hexdump(t, 16); 17340346c37cSMatthias Ringwald log_info("f5 message for MacKey"); 17350346c37cSMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1736d1a1f6a4SMatthias Ringwald sm_cmac_message_start(t, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 17370346c37cSMatthias Ringwald } 17380346c37cSMatthias Ringwald 17390346c37cSMatthias Ringwald static void f5_calculate_mackey(sm_connection_t * sm_conn){ 17400346c37cSMatthias Ringwald sm_key56_t bd_addr_master, bd_addr_slave; 17410346c37cSMatthias Ringwald bd_addr_master[0] = setup->sm_m_addr_type; 17420346c37cSMatthias Ringwald bd_addr_slave[0] = setup->sm_s_addr_type; 17436535961aSMatthias Ringwald (void)memcpy(&bd_addr_master[1], setup->sm_m_address, 6); 17446535961aSMatthias Ringwald (void)memcpy(&bd_addr_slave[1], setup->sm_s_address, 6); 174542134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 17460346c37cSMatthias Ringwald // responder 17470346c37cSMatthias Ringwald f5_mackkey(sm_conn, setup->sm_t, setup->sm_peer_nonce, setup->sm_local_nonce, bd_addr_master, bd_addr_slave); 17480346c37cSMatthias Ringwald } else { 17490346c37cSMatthias Ringwald // initiator 17500346c37cSMatthias Ringwald f5_mackkey(sm_conn, setup->sm_t, setup->sm_local_nonce, setup->sm_peer_nonce, bd_addr_master, bd_addr_slave); 17510346c37cSMatthias Ringwald } 17520346c37cSMatthias Ringwald } 17530346c37cSMatthias Ringwald 17540346c37cSMatthias Ringwald // note: must be called right after f5_mackey, as sm_cmac_buffer[1..52] will be reused 17550346c37cSMatthias Ringwald static inline void f5_ltk(sm_connection_t * sm_conn, sm_key_t t){ 17560346c37cSMatthias Ringwald const uint16_t message_len = 53; 17570346c37cSMatthias Ringwald sm_cmac_connection = sm_conn; 17580346c37cSMatthias Ringwald sm_cmac_sc_buffer[0] = 1; 17590346c37cSMatthias Ringwald // 1..52 setup before 17600346c37cSMatthias Ringwald log_info("f5 key"); 17610346c37cSMatthias Ringwald log_info_hexdump(t, 16); 17620346c37cSMatthias Ringwald log_info("f5 message for LTK"); 17630346c37cSMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1764d1a1f6a4SMatthias Ringwald sm_cmac_message_start(t, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 17650346c37cSMatthias Ringwald } 1766f92edc8eSMatthias Ringwald 17670346c37cSMatthias Ringwald static void f5_calculate_ltk(sm_connection_t * sm_conn){ 17680346c37cSMatthias Ringwald f5_ltk(sm_conn, setup->sm_t); 17690346c37cSMatthias Ringwald } 17700346c37cSMatthias Ringwald 177131f061fbSMatthias Ringwald static void f6_setup(const sm_key_t n1, const sm_key_t n2, const sm_key_t r, const sm_key24_t io_cap, const sm_key56_t a1, const sm_key56_t a2){ 17726535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer, n1, 16); 17736535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 16, n2, 16); 17746535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 32, r, 16); 17756535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 48, io_cap, 3); 17766535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 51, a1, 7); 17776535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 58, a2, 7); 177831f061fbSMatthias Ringwald } 177931f061fbSMatthias Ringwald 178031f061fbSMatthias Ringwald static void f6_engine(sm_connection_t * sm_conn, const sm_key_t w){ 178131f061fbSMatthias Ringwald const uint16_t message_len = 65; 178231f061fbSMatthias Ringwald sm_cmac_connection = sm_conn; 1783dc300847SMatthias Ringwald log_info("f6 key"); 1784dc300847SMatthias Ringwald log_info_hexdump(w, 16); 1785dc300847SMatthias Ringwald log_info("f6 message"); 1786dc300847SMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1787d1a1f6a4SMatthias Ringwald sm_cmac_message_start(w, 65, sm_cmac_sc_buffer, &sm_sc_cmac_done); 1788dc300847SMatthias Ringwald } 1789dc300847SMatthias Ringwald 1790f92edc8eSMatthias Ringwald // g2(U, V, X, Y) = AES-CMACX(U || V || Y) mod 2^32 1791f92edc8eSMatthias Ringwald // - U is 256 bits 1792f92edc8eSMatthias Ringwald // - V is 256 bits 1793f92edc8eSMatthias Ringwald // - X is 128 bits 1794f92edc8eSMatthias Ringwald // - Y is 128 bits 1795bd57ffebSMatthias Ringwald static void g2_engine(sm_connection_t * sm_conn, const sm_key256_t u, const sm_key256_t v, const sm_key_t x, const sm_key_t y){ 1796bd57ffebSMatthias Ringwald const uint16_t message_len = 80; 1797bd57ffebSMatthias Ringwald sm_cmac_connection = sm_conn; 17986535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer, u, 32); 17996535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 32, v, 32); 18006535961aSMatthias Ringwald (void)memcpy(sm_cmac_sc_buffer + 64, y, 16); 1801f92edc8eSMatthias Ringwald log_info("g2 key"); 1802f92edc8eSMatthias Ringwald log_info_hexdump(x, 16); 1803f92edc8eSMatthias Ringwald log_info("g2 message"); 18042bacf595SMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1805d1a1f6a4SMatthias Ringwald sm_cmac_message_start(x, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 1806f92edc8eSMatthias Ringwald } 1807f92edc8eSMatthias Ringwald 1808b35a3de2SMatthias Ringwald static void g2_calculate(sm_connection_t * sm_conn) { 1809f92edc8eSMatthias Ringwald // calc Va if numeric comparison 181042134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1811f92edc8eSMatthias Ringwald // responder 1812fc5bff5fSMatthias Ringwald g2_engine(sm_conn, setup->sm_peer_q, ec_q, setup->sm_peer_nonce, setup->sm_local_nonce);; 1813f92edc8eSMatthias Ringwald } else { 1814f92edc8eSMatthias Ringwald // initiator 1815fc5bff5fSMatthias Ringwald g2_engine(sm_conn, ec_q, setup->sm_peer_q, setup->sm_local_nonce, setup->sm_peer_nonce); 1816f92edc8eSMatthias Ringwald } 1817f92edc8eSMatthias Ringwald } 1818f92edc8eSMatthias Ringwald 1819945888f5SMatthias Ringwald static void sm_sc_calculate_local_confirm(sm_connection_t * sm_conn){ 18209af0f475SMatthias Ringwald uint8_t z = 0; 182140c5d850SMatthias Ringwald if (sm_passkey_entry(setup->sm_stk_generation_method)){ 18229af0f475SMatthias Ringwald // some form of passkey 18239af0f475SMatthias Ringwald uint32_t pk = big_endian_read_32(setup->sm_tk, 12); 18244ea43905SMatthias Ringwald z = 0x80u | ((pk >> setup->sm_passkey_bit) & 1u); 18259af0f475SMatthias Ringwald setup->sm_passkey_bit++; 18269af0f475SMatthias Ringwald } 1827fc5bff5fSMatthias Ringwald f4_engine(sm_conn, ec_q, setup->sm_peer_q, setup->sm_local_nonce, z); 18289af0f475SMatthias Ringwald } 1829688a08f9SMatthias Ringwald 1830688a08f9SMatthias Ringwald static void sm_sc_calculate_remote_confirm(sm_connection_t * sm_conn){ 1831a680ba6bSMatthias Ringwald // OOB 1832a680ba6bSMatthias Ringwald if (setup->sm_stk_generation_method == OOB){ 18334acf7b7bSMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 18344acf7b7bSMatthias Ringwald f4_engine(sm_conn, setup->sm_peer_q, setup->sm_peer_q, setup->sm_ra, 0); 18354acf7b7bSMatthias Ringwald } else { 18364acf7b7bSMatthias Ringwald f4_engine(sm_conn, setup->sm_peer_q, setup->sm_peer_q, setup->sm_rb, 0); 18374acf7b7bSMatthias Ringwald } 1838a680ba6bSMatthias Ringwald return; 1839a680ba6bSMatthias Ringwald } 1840a680ba6bSMatthias Ringwald 1841688a08f9SMatthias Ringwald uint8_t z = 0; 184240c5d850SMatthias Ringwald if (sm_passkey_entry(setup->sm_stk_generation_method)){ 1843688a08f9SMatthias Ringwald // some form of passkey 1844688a08f9SMatthias Ringwald uint32_t pk = big_endian_read_32(setup->sm_tk, 12); 1845688a08f9SMatthias Ringwald // sm_passkey_bit was increased before sending confirm value 18464ea43905SMatthias Ringwald z = 0x80u | ((pk >> (setup->sm_passkey_bit-1u)) & 1u); 1847688a08f9SMatthias Ringwald } 1848fc5bff5fSMatthias Ringwald f4_engine(sm_conn, setup->sm_peer_q, ec_q, setup->sm_peer_nonce, z); 1849688a08f9SMatthias Ringwald } 1850688a08f9SMatthias Ringwald 18510346c37cSMatthias Ringwald static void sm_sc_prepare_dhkey_check(sm_connection_t * sm_conn){ 1852505f1c30SMatthias Ringwald log_info("sm_sc_prepare_dhkey_check, DHKEY calculated %u", (setup->sm_state_vars & SM_STATE_VAR_DHKEY_CALCULATED) ? 1 : 0); 18533cf37b8cSMatthias Ringwald 18543cf37b8cSMatthias Ringwald if (setup->sm_state_vars & SM_STATE_VAR_DHKEY_CALCULATED){ 18553cf37b8cSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_SALT; 18563cf37b8cSMatthias Ringwald return; 18573cf37b8cSMatthias Ringwald } else { 18583cf37b8cSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_CALCULATE_DHKEY; 18593cf37b8cSMatthias Ringwald } 1860d1a1f6a4SMatthias Ringwald } 18613cf37b8cSMatthias Ringwald 1862d1a1f6a4SMatthias Ringwald static void sm_sc_dhkey_calculated(void * arg){ 1863f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 1864f3582630SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 1865f3582630SMatthias Ringwald if (sm_conn == NULL) return; 1866f3582630SMatthias Ringwald 1867d1a1f6a4SMatthias Ringwald log_info("dhkey"); 1868d1a1f6a4SMatthias Ringwald log_info_hexdump(&setup->sm_dhkey[0], 32); 1869d1a1f6a4SMatthias Ringwald setup->sm_state_vars |= SM_STATE_VAR_DHKEY_CALCULATED; 1870d1a1f6a4SMatthias Ringwald // trigger next step 1871d1a1f6a4SMatthias Ringwald if (sm_conn->sm_engine_state == SM_SC_W4_CALCULATE_DHKEY){ 1872d1a1f6a4SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_SALT; 1873d1a1f6a4SMatthias Ringwald } 187470b44dd4SMatthias Ringwald sm_trigger_run(); 1875dc300847SMatthias Ringwald } 1876dc300847SMatthias Ringwald 1877dc300847SMatthias Ringwald static void sm_sc_calculate_f6_for_dhkey_check(sm_connection_t * sm_conn){ 1878dc300847SMatthias Ringwald // calculate DHKCheck 1879dc300847SMatthias Ringwald sm_key56_t bd_addr_master, bd_addr_slave; 1880dc300847SMatthias Ringwald bd_addr_master[0] = setup->sm_m_addr_type; 1881dc300847SMatthias Ringwald bd_addr_slave[0] = setup->sm_s_addr_type; 18826535961aSMatthias Ringwald (void)memcpy(&bd_addr_master[1], setup->sm_m_address, 6); 18836535961aSMatthias Ringwald (void)memcpy(&bd_addr_slave[1], setup->sm_s_address, 6); 1884dc300847SMatthias Ringwald uint8_t iocap_a[3]; 1885dc300847SMatthias Ringwald iocap_a[0] = sm_pairing_packet_get_auth_req(setup->sm_m_preq); 1886dc300847SMatthias Ringwald iocap_a[1] = sm_pairing_packet_get_oob_data_flag(setup->sm_m_preq); 1887dc300847SMatthias Ringwald iocap_a[2] = sm_pairing_packet_get_io_capability(setup->sm_m_preq); 1888dc300847SMatthias Ringwald uint8_t iocap_b[3]; 1889dc300847SMatthias Ringwald iocap_b[0] = sm_pairing_packet_get_auth_req(setup->sm_s_pres); 1890dc300847SMatthias Ringwald iocap_b[1] = sm_pairing_packet_get_oob_data_flag(setup->sm_s_pres); 1891dc300847SMatthias Ringwald iocap_b[2] = sm_pairing_packet_get_io_capability(setup->sm_s_pres); 189242134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1893dc300847SMatthias Ringwald // responder 189431f061fbSMatthias Ringwald f6_setup(setup->sm_local_nonce, setup->sm_peer_nonce, setup->sm_ra, iocap_b, bd_addr_slave, bd_addr_master); 189531f061fbSMatthias Ringwald f6_engine(sm_conn, setup->sm_mackey); 1896dc300847SMatthias Ringwald } else { 1897dc300847SMatthias Ringwald // initiator 189831f061fbSMatthias Ringwald f6_setup( setup->sm_local_nonce, setup->sm_peer_nonce, setup->sm_rb, iocap_a, bd_addr_master, bd_addr_slave); 189931f061fbSMatthias Ringwald f6_engine(sm_conn, setup->sm_mackey); 1900dc300847SMatthias Ringwald } 1901dc300847SMatthias Ringwald } 1902dc300847SMatthias Ringwald 1903019005a0SMatthias Ringwald static void sm_sc_calculate_f6_to_verify_dhkey_check(sm_connection_t * sm_conn){ 1904019005a0SMatthias Ringwald // validate E = f6() 1905019005a0SMatthias Ringwald sm_key56_t bd_addr_master, bd_addr_slave; 1906019005a0SMatthias Ringwald bd_addr_master[0] = setup->sm_m_addr_type; 1907019005a0SMatthias Ringwald bd_addr_slave[0] = setup->sm_s_addr_type; 19086535961aSMatthias Ringwald (void)memcpy(&bd_addr_master[1], setup->sm_m_address, 6); 19096535961aSMatthias Ringwald (void)memcpy(&bd_addr_slave[1], setup->sm_s_address, 6); 1910019005a0SMatthias Ringwald 1911019005a0SMatthias Ringwald uint8_t iocap_a[3]; 1912019005a0SMatthias Ringwald iocap_a[0] = sm_pairing_packet_get_auth_req(setup->sm_m_preq); 1913019005a0SMatthias Ringwald iocap_a[1] = sm_pairing_packet_get_oob_data_flag(setup->sm_m_preq); 1914019005a0SMatthias Ringwald iocap_a[2] = sm_pairing_packet_get_io_capability(setup->sm_m_preq); 1915019005a0SMatthias Ringwald uint8_t iocap_b[3]; 1916019005a0SMatthias Ringwald iocap_b[0] = sm_pairing_packet_get_auth_req(setup->sm_s_pres); 1917019005a0SMatthias Ringwald iocap_b[1] = sm_pairing_packet_get_oob_data_flag(setup->sm_s_pres); 1918019005a0SMatthias Ringwald iocap_b[2] = sm_pairing_packet_get_io_capability(setup->sm_s_pres); 191942134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 1920019005a0SMatthias Ringwald // responder 192131f061fbSMatthias Ringwald f6_setup(setup->sm_peer_nonce, setup->sm_local_nonce, setup->sm_rb, iocap_a, bd_addr_master, bd_addr_slave); 192231f061fbSMatthias Ringwald f6_engine(sm_conn, setup->sm_mackey); 1923019005a0SMatthias Ringwald } else { 1924019005a0SMatthias Ringwald // initiator 192531f061fbSMatthias Ringwald f6_setup(setup->sm_peer_nonce, setup->sm_local_nonce, setup->sm_ra, iocap_b, bd_addr_slave, bd_addr_master); 192631f061fbSMatthias Ringwald f6_engine(sm_conn, setup->sm_mackey); 1927019005a0SMatthias Ringwald } 1928019005a0SMatthias Ringwald } 19292bacf595SMatthias Ringwald 193055c62cf5SMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 19312bacf595SMatthias Ringwald 19322bacf595SMatthias Ringwald // 19332bacf595SMatthias Ringwald // Link Key Conversion Function h6 19342bacf595SMatthias Ringwald // 193557132f12SMatthias Ringwald // h6(W, keyID) = AES-CMAC_W(keyID) 19362bacf595SMatthias Ringwald // - W is 128 bits 19372bacf595SMatthias Ringwald // - keyID is 32 bits 19382bacf595SMatthias Ringwald static void h6_engine(sm_connection_t * sm_conn, const sm_key_t w, const uint32_t key_id){ 19392bacf595SMatthias Ringwald const uint16_t message_len = 4; 19402bacf595SMatthias Ringwald sm_cmac_connection = sm_conn; 19412bacf595SMatthias Ringwald big_endian_store_32(sm_cmac_sc_buffer, 0, key_id); 19422bacf595SMatthias Ringwald log_info("h6 key"); 19432bacf595SMatthias Ringwald log_info_hexdump(w, 16); 19442bacf595SMatthias Ringwald log_info("h6 message"); 19452bacf595SMatthias Ringwald log_info_hexdump(sm_cmac_sc_buffer, message_len); 1946d1a1f6a4SMatthias Ringwald sm_cmac_message_start(w, message_len, sm_cmac_sc_buffer, &sm_sc_cmac_done); 19472bacf595SMatthias Ringwald } 194857132f12SMatthias Ringwald // 194957132f12SMatthias Ringwald // Link Key Conversion Function h7 195057132f12SMatthias Ringwald // 195157132f12SMatthias Ringwald // h7(SALT, W) = AES-CMAC_SALT(W) 195257132f12SMatthias Ringwald // - SALT is 128 bits 195357132f12SMatthias Ringwald // - W is 128 bits 195457132f12SMatthias Ringwald static void h7_engine(sm_connection_t * sm_conn, const sm_key_t salt, const sm_key_t w) { 195557132f12SMatthias Ringwald const uint16_t message_len = 16; 195657132f12SMatthias Ringwald sm_cmac_connection = sm_conn; 195757132f12SMatthias Ringwald log_info("h7 key"); 195857132f12SMatthias Ringwald log_info_hexdump(salt, 16); 195957132f12SMatthias Ringwald log_info("h7 message"); 196057132f12SMatthias Ringwald log_info_hexdump(w, 16); 196157132f12SMatthias Ringwald sm_cmac_message_start(salt, message_len, w, &sm_sc_cmac_done); 196257132f12SMatthias Ringwald } 19632bacf595SMatthias Ringwald 1964b18300a6SMatthias Ringwald // For SC, setup->sm_local_ltk holds full LTK (sm_ltk is already truncated) 1965b18300a6SMatthias Ringwald // Errata Service Release to the Bluetooth Specification: ESR09 1966b18300a6SMatthias Ringwald // E6405 – Cross transport key derivation from a key of size less than 128 bits 1967b18300a6SMatthias Ringwald // "Note: When the BR/EDR link key is being derived from the LTK, the derivation is done before the LTK gets masked." 196857132f12SMatthias Ringwald 19692bacf595SMatthias Ringwald static void h6_calculate_ilk(sm_connection_t * sm_conn){ 1970b18300a6SMatthias Ringwald h6_engine(sm_conn, setup->sm_local_ltk, 0x746D7031); // "tmp1" 19712bacf595SMatthias Ringwald } 19722bacf595SMatthias Ringwald 19732bacf595SMatthias Ringwald static void h6_calculate_br_edr_link_key(sm_connection_t * sm_conn){ 19742bacf595SMatthias Ringwald h6_engine(sm_conn, setup->sm_t, 0x6c656272); // "lebr" 19752bacf595SMatthias Ringwald } 19762bacf595SMatthias Ringwald 197757132f12SMatthias Ringwald static void h7_calculate_ilk(sm_connection_t * sm_conn){ 197857132f12SMatthias Ringwald const uint8_t salt[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x6D, 0x70, 0x31}; // "tmp1" 197957132f12SMatthias Ringwald h7_engine(sm_conn, salt, setup->sm_local_ltk); 198057132f12SMatthias Ringwald } 19819af0f475SMatthias Ringwald #endif 19829af0f475SMatthias Ringwald 198355c62cf5SMatthias Ringwald #endif 198455c62cf5SMatthias Ringwald 1985613da3deSMatthias Ringwald // key management legacy connections: 1986613da3deSMatthias Ringwald // - potentially two different LTKs based on direction. each device stores LTK provided by peer 1987613da3deSMatthias Ringwald // - master stores LTK, EDIV, RAND. responder optionally stored master LTK (only if it needs to reconnect) 1988613da3deSMatthias Ringwald // - initiators reconnects: initiator uses stored LTK, EDIV, RAND generated by responder 1989613da3deSMatthias Ringwald // - responder reconnects: responder uses LTK receveived from master 1990613da3deSMatthias Ringwald 1991613da3deSMatthias Ringwald // key management secure connections: 1992613da3deSMatthias Ringwald // - both devices store same LTK from ECDH key exchange. 1993613da3deSMatthias Ringwald 199442134bc6SMatthias Ringwald #if defined(ENABLE_LE_SECURE_CONNECTIONS) || defined(ENABLE_LE_CENTRAL) 19955829ebe2SMatthias Ringwald static void sm_load_security_info(sm_connection_t * sm_connection){ 19965829ebe2SMatthias Ringwald int encryption_key_size; 19975829ebe2SMatthias Ringwald int authenticated; 19985829ebe2SMatthias Ringwald int authorized; 19993dc3a67dSMatthias Ringwald int secure_connection; 20005829ebe2SMatthias Ringwald 20015829ebe2SMatthias Ringwald // fetch data from device db - incl. authenticated/authorized/key size. Note all sm_connection_X require encryption enabled 20025829ebe2SMatthias Ringwald le_device_db_encryption_get(sm_connection->sm_le_db_index, &setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk, 20033dc3a67dSMatthias Ringwald &encryption_key_size, &authenticated, &authorized, &secure_connection); 20043dc3a67dSMatthias Ringwald log_info("db index %u, key size %u, authenticated %u, authorized %u, secure connetion %u", sm_connection->sm_le_db_index, encryption_key_size, authenticated, authorized, secure_connection); 20055829ebe2SMatthias Ringwald sm_connection->sm_actual_encryption_key_size = encryption_key_size; 20065829ebe2SMatthias Ringwald sm_connection->sm_connection_authenticated = authenticated; 20075829ebe2SMatthias Ringwald sm_connection->sm_connection_authorization_state = authorized ? AUTHORIZATION_GRANTED : AUTHORIZATION_UNKNOWN; 20083dc3a67dSMatthias Ringwald sm_connection->sm_connection_sc = secure_connection; 20095829ebe2SMatthias Ringwald } 201042134bc6SMatthias Ringwald #endif 2011bd57ffebSMatthias Ringwald 201242134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 201359066796SMatthias Ringwald static void sm_start_calculating_ltk_from_ediv_and_rand(sm_connection_t * sm_connection){ 20146535961aSMatthias Ringwald (void)memcpy(setup->sm_local_rand, sm_connection->sm_local_rand, 8); 201559066796SMatthias Ringwald setup->sm_local_ediv = sm_connection->sm_local_ediv; 201659066796SMatthias Ringwald // re-establish used key encryption size 201759066796SMatthias Ringwald // no db for encryption size hack: encryption size is stored in lowest nibble of setup->sm_local_rand 20184ea43905SMatthias Ringwald sm_connection->sm_actual_encryption_key_size = (setup->sm_local_rand[7u] & 0x0fu) + 1u; 201959066796SMatthias Ringwald // no db for authenticated flag hack: flag is stored in bit 4 of LSB 20204ea43905SMatthias Ringwald sm_connection->sm_connection_authenticated = (setup->sm_local_rand[7u] & 0x10u) >> 4u; 20213dc3a67dSMatthias Ringwald // Legacy paring -> not SC 20223dc3a67dSMatthias Ringwald sm_connection->sm_connection_sc = 0; 202359066796SMatthias Ringwald log_info("sm: received ltk request with key size %u, authenticated %u", 202459066796SMatthias Ringwald sm_connection->sm_actual_encryption_key_size, sm_connection->sm_connection_authenticated); 202559066796SMatthias Ringwald } 202642134bc6SMatthias Ringwald #endif 202759066796SMatthias Ringwald 20283deb3ec6SMatthias Ringwald // distributed key generation 2029d7f1c72eSMatthias Ringwald static bool sm_run_dpkg(void){ 20303deb3ec6SMatthias Ringwald switch (dkg_state){ 20313deb3ec6SMatthias Ringwald case DKG_CALC_IRK: 20323deb3ec6SMatthias Ringwald // already busy? 20333deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_IDLE) { 2034d1a1f6a4SMatthias Ringwald log_info("DKG_CALC_IRK started"); 20353deb3ec6SMatthias Ringwald // IRK = d1(IR, 1, 0) 2036d1a1f6a4SMatthias Ringwald sm_d1_d_prime(1, 0, sm_aes128_plaintext); // plaintext = d1 prime 2037d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2038d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_ir, sm_aes128_plaintext, sm_persistent_irk, sm_handle_encryption_result_dkg_irk, NULL); 2039d7f1c72eSMatthias Ringwald return true; 20403deb3ec6SMatthias Ringwald } 20413deb3ec6SMatthias Ringwald break; 20423deb3ec6SMatthias Ringwald case DKG_CALC_DHK: 20433deb3ec6SMatthias Ringwald // already busy? 20443deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_IDLE) { 2045d1a1f6a4SMatthias Ringwald log_info("DKG_CALC_DHK started"); 20463deb3ec6SMatthias Ringwald // DHK = d1(IR, 3, 0) 2047d1a1f6a4SMatthias Ringwald sm_d1_d_prime(3, 0, sm_aes128_plaintext); // plaintext = d1 prime 2048d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2049d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_ir, sm_aes128_plaintext, sm_persistent_dhk, sm_handle_encryption_result_dkg_dhk, NULL); 2050d7f1c72eSMatthias Ringwald return true; 20513deb3ec6SMatthias Ringwald } 20523deb3ec6SMatthias Ringwald break; 20533deb3ec6SMatthias Ringwald default: 20543deb3ec6SMatthias Ringwald break; 20553deb3ec6SMatthias Ringwald } 2056d7f1c72eSMatthias Ringwald return false; 2057d7f1c72eSMatthias Ringwald } 20583deb3ec6SMatthias Ringwald 20593deb3ec6SMatthias Ringwald // random address updates 2060d7f1c72eSMatthias Ringwald static bool sm_run_rau(void){ 20613deb3ec6SMatthias Ringwald switch (rau_state){ 2062fbd4e238SMatthias Ringwald case RAU_GET_RANDOM: 2063fbd4e238SMatthias Ringwald rau_state = RAU_W4_RANDOM; 20645b4dd597SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_address, 6, &sm_handle_random_result_rau, NULL); 2065d7f1c72eSMatthias Ringwald return true; 20663deb3ec6SMatthias Ringwald case RAU_GET_ENC: 20673deb3ec6SMatthias Ringwald // already busy? 20683deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_IDLE) { 2069d1a1f6a4SMatthias Ringwald sm_ah_r_prime(sm_random_address, sm_aes128_plaintext); 2070d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2071d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_irk, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_rau, NULL); 2072d7f1c72eSMatthias Ringwald return true; 20733deb3ec6SMatthias Ringwald } 20743deb3ec6SMatthias Ringwald break; 20753deb3ec6SMatthias Ringwald case RAU_SET_ADDRESS: 20763deb3ec6SMatthias Ringwald log_info("New random address: %s", bd_addr_to_str(sm_random_address)); 20773deb3ec6SMatthias Ringwald rau_state = RAU_IDLE; 20783deb3ec6SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, sm_random_address); 2079d7f1c72eSMatthias Ringwald return true; 20803deb3ec6SMatthias Ringwald default: 20813deb3ec6SMatthias Ringwald break; 20823deb3ec6SMatthias Ringwald } 2083d7f1c72eSMatthias Ringwald return false; 2084d7f1c72eSMatthias Ringwald } 20853deb3ec6SMatthias Ringwald 20863deb3ec6SMatthias Ringwald // CSRK Lookup 2087d7f1c72eSMatthias Ringwald static bool sm_run_csrk(void){ 2088d7f1c72eSMatthias Ringwald btstack_linked_list_iterator_t it; 2089d7f1c72eSMatthias Ringwald 20903deb3ec6SMatthias Ringwald // -- if csrk lookup ready, find connection that require csrk lookup 20913deb3ec6SMatthias Ringwald if (sm_address_resolution_idle()){ 20923deb3ec6SMatthias Ringwald hci_connections_get_iterator(&it); 2093665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 2094665d90f2SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 20953deb3ec6SMatthias Ringwald sm_connection_t * sm_connection = &hci_connection->sm_connection; 20963deb3ec6SMatthias Ringwald if (sm_connection->sm_irk_lookup_state == IRK_LOOKUP_W4_READY){ 20973deb3ec6SMatthias Ringwald // and start lookup 20983deb3ec6SMatthias Ringwald sm_address_resolution_start_lookup(sm_connection->sm_peer_addr_type, sm_connection->sm_handle, sm_connection->sm_peer_address, ADDRESS_RESOLUTION_FOR_CONNECTION, sm_connection); 20993deb3ec6SMatthias Ringwald sm_connection->sm_irk_lookup_state = IRK_LOOKUP_STARTED; 21003deb3ec6SMatthias Ringwald break; 21013deb3ec6SMatthias Ringwald } 21023deb3ec6SMatthias Ringwald } 21033deb3ec6SMatthias Ringwald } 21043deb3ec6SMatthias Ringwald 21053deb3ec6SMatthias Ringwald // -- if csrk lookup ready, resolved addresses for received addresses 21063deb3ec6SMatthias Ringwald if (sm_address_resolution_idle()) { 2107665d90f2SMatthias Ringwald if (!btstack_linked_list_empty(&sm_address_resolution_general_queue)){ 21083deb3ec6SMatthias Ringwald sm_lookup_entry_t * entry = (sm_lookup_entry_t *) sm_address_resolution_general_queue; 2109665d90f2SMatthias Ringwald btstack_linked_list_remove(&sm_address_resolution_general_queue, (btstack_linked_item_t *) entry); 21103deb3ec6SMatthias Ringwald sm_address_resolution_start_lookup(entry->address_type, 0, entry->address, ADDRESS_RESOLUTION_GENERAL, NULL); 21113deb3ec6SMatthias Ringwald btstack_memory_sm_lookup_entry_free(entry); 21123deb3ec6SMatthias Ringwald } 21133deb3ec6SMatthias Ringwald } 21143deb3ec6SMatthias Ringwald 21153deb3ec6SMatthias Ringwald // -- Continue with CSRK device lookup by public or resolvable private address 21163deb3ec6SMatthias Ringwald if (!sm_address_resolution_idle()){ 2117092ec58eSMatthias Ringwald log_info("LE Device Lookup: device %u/%u", sm_address_resolution_test, le_device_db_max_count()); 2118092ec58eSMatthias Ringwald while (sm_address_resolution_test < le_device_db_max_count()){ 2119adf5eaa9SMatthias Ringwald int addr_type = BD_ADDR_TYPE_UNKNOWN; 21203deb3ec6SMatthias Ringwald bd_addr_t addr; 21213deb3ec6SMatthias Ringwald sm_key_t irk; 21223deb3ec6SMatthias Ringwald le_device_db_info(sm_address_resolution_test, &addr_type, addr, irk); 21233deb3ec6SMatthias Ringwald log_info("device type %u, addr: %s", addr_type, bd_addr_to_str(addr)); 21243deb3ec6SMatthias Ringwald 2125adf5eaa9SMatthias Ringwald // skip unused entries 2126adf5eaa9SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_UNKNOWN){ 2127adf5eaa9SMatthias Ringwald sm_address_resolution_test++; 2128adf5eaa9SMatthias Ringwald continue; 2129adf5eaa9SMatthias Ringwald } 2130adf5eaa9SMatthias Ringwald 21315df9dc78SMatthias Ringwald if ((sm_address_resolution_addr_type == addr_type) && (memcmp(addr, sm_address_resolution_address, 6) == 0)){ 21323deb3ec6SMatthias Ringwald log_info("LE Device Lookup: found CSRK by { addr_type, address} "); 2133a66b030fSMatthias Ringwald sm_address_resolution_handle_event(ADDRESS_RESOLUTION_SUCCEEDED); 21343deb3ec6SMatthias Ringwald break; 21353deb3ec6SMatthias Ringwald } 21363deb3ec6SMatthias Ringwald 2137a9987c8eSMatthias Ringwald // if connection type is public, it must be a different one 2138a9987c8eSMatthias Ringwald if (sm_address_resolution_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 21393deb3ec6SMatthias Ringwald sm_address_resolution_test++; 21403deb3ec6SMatthias Ringwald continue; 21413deb3ec6SMatthias Ringwald } 21423deb3ec6SMatthias Ringwald 21433deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 21443deb3ec6SMatthias Ringwald 21453deb3ec6SMatthias Ringwald log_info("LE Device Lookup: calculate AH"); 21468314c363SMatthias Ringwald log_info_key("IRK", irk); 21473deb3ec6SMatthias Ringwald 21486535961aSMatthias Ringwald (void)memcpy(sm_aes128_key, irk, 16); 2149d1a1f6a4SMatthias Ringwald sm_ah_r_prime(sm_address_resolution_address, sm_aes128_plaintext); 21503deb3ec6SMatthias Ringwald sm_address_resolution_ah_calculation_active = 1; 2151d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2152d1a1f6a4SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_aes128_key, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_address_resolution, NULL); 2153d7f1c72eSMatthias Ringwald return true; 21543deb3ec6SMatthias Ringwald } 21553deb3ec6SMatthias Ringwald 2156092ec58eSMatthias Ringwald if (sm_address_resolution_test >= le_device_db_max_count()){ 21573deb3ec6SMatthias Ringwald log_info("LE Device Lookup: not found"); 21583deb3ec6SMatthias Ringwald sm_address_resolution_handle_event(ADDRESS_RESOLUTION_FAILED); 21593deb3ec6SMatthias Ringwald } 21603deb3ec6SMatthias Ringwald } 2161d7f1c72eSMatthias Ringwald return false; 2162d7f1c72eSMatthias Ringwald } 21633deb3ec6SMatthias Ringwald 2164d7f1c72eSMatthias Ringwald // SC OOB 2165d7f1c72eSMatthias Ringwald static bool sm_run_oob(void){ 2166c59d0c92SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 2167c59d0c92SMatthias Ringwald switch (sm_sc_oob_state){ 2168c59d0c92SMatthias Ringwald case SM_SC_OOB_W2_CALC_CONFIRM: 2169c59d0c92SMatthias Ringwald if (!sm_cmac_ready()) break; 2170c59d0c92SMatthias Ringwald sm_sc_oob_state = SM_SC_OOB_W4_CONFIRM; 2171c59d0c92SMatthias Ringwald f4_engine(NULL, ec_q, ec_q, sm_sc_oob_random, 0); 2172d7f1c72eSMatthias Ringwald return true; 2173c59d0c92SMatthias Ringwald default: 2174c59d0c92SMatthias Ringwald break; 2175c59d0c92SMatthias Ringwald } 2176c59d0c92SMatthias Ringwald #endif 2177d7f1c72eSMatthias Ringwald return false; 2178d7f1c72eSMatthias Ringwald } 2179275aafe8SMatthias Ringwald 218041d32297SMatthias Ringwald // handle basic actions that don't requires the full context 2181d7f1c72eSMatthias Ringwald static bool sm_run_basic(void){ 2182d7f1c72eSMatthias Ringwald btstack_linked_list_iterator_t it; 218341d32297SMatthias Ringwald hci_connections_get_iterator(&it); 2184e9af1bf6SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 218541d32297SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 218641d32297SMatthias Ringwald sm_connection_t * sm_connection = &hci_connection->sm_connection; 218741d32297SMatthias Ringwald switch(sm_connection->sm_engine_state){ 218841d32297SMatthias Ringwald // responder side 218941d32297SMatthias Ringwald case SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY: 219041d32297SMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_IDLE; 219141d32297SMatthias Ringwald hci_send_cmd(&hci_le_long_term_key_negative_reply, sm_connection->sm_handle); 2192d7f1c72eSMatthias Ringwald return true; 21934b8b5afeSMatthias Ringwald 21944b8b5afeSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 21954b8b5afeSMatthias Ringwald case SM_SC_RECEIVED_LTK_REQUEST: 21964b8b5afeSMatthias Ringwald switch (sm_connection->sm_irk_lookup_state){ 21974b8b5afeSMatthias Ringwald case IRK_LOOKUP_FAILED: 2198e9af1bf6SMatthias Ringwald log_info("LTK Request: IRK Lookup Failed)"); 21994b8b5afeSMatthias Ringwald sm_connection->sm_engine_state = SM_RESPONDER_IDLE; 22004b8b5afeSMatthias Ringwald hci_send_cmd(&hci_le_long_term_key_negative_reply, sm_connection->sm_handle); 2201d7f1c72eSMatthias Ringwald return true; 22024b8b5afeSMatthias Ringwald default: 22034b8b5afeSMatthias Ringwald break; 22044b8b5afeSMatthias Ringwald } 22054b8b5afeSMatthias Ringwald break; 22064b8b5afeSMatthias Ringwald #endif 220741d32297SMatthias Ringwald default: 220841d32297SMatthias Ringwald break; 220941d32297SMatthias Ringwald } 221041d32297SMatthias Ringwald } 2211d7f1c72eSMatthias Ringwald return false; 2212d7f1c72eSMatthias Ringwald } 22133deb3ec6SMatthias Ringwald 2214d7f1c72eSMatthias Ringwald static void sm_run_activate_connection(void){ 22153deb3ec6SMatthias Ringwald // Find connections that requires setup context and make active if no other is locked 2216d7f1c72eSMatthias Ringwald btstack_linked_list_iterator_t it; 22173deb3ec6SMatthias Ringwald hci_connections_get_iterator(&it); 22187149bde5SMatthias Ringwald while((sm_active_connection_handle == HCI_CON_HANDLE_INVALID) && btstack_linked_list_iterator_has_next(&it)){ 2219665d90f2SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 22203deb3ec6SMatthias Ringwald sm_connection_t * sm_connection = &hci_connection->sm_connection; 22213deb3ec6SMatthias Ringwald // - if no connection locked and we're ready/waiting for setup context, fetch it and start 22221979f09cSMatthias Ringwald bool done = true; 22233deb3ec6SMatthias Ringwald int err; 222442134bc6SMatthias Ringwald UNUSED(err); 222534b6528fSMatthias Ringwald 222634b6528fSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 222734b6528fSMatthias Ringwald // assert ec key is ready 2228505f1c30SMatthias Ringwald if ( (sm_connection->sm_engine_state == SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED) 2229178e8c1bSMatthias Ringwald || (sm_connection->sm_engine_state == SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST) 2230178e8c1bSMatthias Ringwald || (sm_connection->sm_engine_state == SM_RESPONDER_SEND_SECURITY_REQUEST)){ 223134b6528fSMatthias Ringwald if (ec_key_generation_state == EC_KEY_GENERATION_IDLE){ 223234b6528fSMatthias Ringwald sm_ec_generate_new_key(); 223334b6528fSMatthias Ringwald } 223434b6528fSMatthias Ringwald if (ec_key_generation_state != EC_KEY_GENERATION_DONE){ 223534b6528fSMatthias Ringwald continue; 223634b6528fSMatthias Ringwald } 223734b6528fSMatthias Ringwald } 223834b6528fSMatthias Ringwald #endif 223934b6528fSMatthias Ringwald 22403deb3ec6SMatthias Ringwald switch (sm_connection->sm_engine_state) { 224142134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 22423deb3ec6SMatthias Ringwald case SM_RESPONDER_SEND_SECURITY_REQUEST: 22433deb3ec6SMatthias Ringwald case SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED: 224442134bc6SMatthias Ringwald case SM_RESPONDER_PH0_RECEIVED_LTK_REQUEST: 2245549ad5d2SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 224606cd539fSMatthias Ringwald case SM_SC_RECEIVED_LTK_REQUEST: 224787014f74SMatthias Ringwald #endif 224887014f74SMatthias Ringwald #endif 224934c39fbdSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 22505567aa60SMatthias Ringwald case SM_INITIATOR_PH4_HAS_LTK: 225134c39fbdSMatthias Ringwald case SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST: 2252549ad5d2SMatthias Ringwald #endif 225387014f74SMatthias Ringwald // just lock context 225487014f74SMatthias Ringwald break; 22553deb3ec6SMatthias Ringwald default: 22561979f09cSMatthias Ringwald done = false; 22573deb3ec6SMatthias Ringwald break; 22583deb3ec6SMatthias Ringwald } 22593deb3ec6SMatthias Ringwald if (done){ 22607149bde5SMatthias Ringwald sm_active_connection_handle = sm_connection->sm_handle; 22617149bde5SMatthias Ringwald log_info("sm: connection 0x%04x locked setup context as %s, state %u", sm_active_connection_handle, sm_connection->sm_role ? "responder" : "initiator", sm_connection->sm_engine_state); 22623deb3ec6SMatthias Ringwald } 22633deb3ec6SMatthias Ringwald } 2264d7f1c72eSMatthias Ringwald } 2265d7f1c72eSMatthias Ringwald 2266403280b9SMatthias Ringwald static void sm_run_send_keypress_notification(sm_connection_t * connection){ 2267403280b9SMatthias Ringwald int i; 2268403280b9SMatthias Ringwald uint8_t flags = setup->sm_keypress_notification & 0x1fu; 2269403280b9SMatthias Ringwald uint8_t num_actions = setup->sm_keypress_notification >> 5; 2270403280b9SMatthias Ringwald uint8_t action = 0; 2271403280b9SMatthias Ringwald for (i=SM_KEYPRESS_PASSKEY_ENTRY_STARTED;i<=SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED;i++){ 2272403280b9SMatthias Ringwald if (flags & (1u<<i)){ 2273403280b9SMatthias Ringwald bool clear_flag = true; 2274403280b9SMatthias Ringwald switch (i){ 2275403280b9SMatthias Ringwald case SM_KEYPRESS_PASSKEY_ENTRY_STARTED: 2276403280b9SMatthias Ringwald case SM_KEYPRESS_PASSKEY_CLEARED: 2277403280b9SMatthias Ringwald case SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED: 2278403280b9SMatthias Ringwald default: 2279403280b9SMatthias Ringwald break; 2280403280b9SMatthias Ringwald case SM_KEYPRESS_PASSKEY_DIGIT_ENTERED: 2281403280b9SMatthias Ringwald case SM_KEYPRESS_PASSKEY_DIGIT_ERASED: 2282403280b9SMatthias Ringwald num_actions--; 2283403280b9SMatthias Ringwald clear_flag = num_actions == 0u; 2284403280b9SMatthias Ringwald break; 2285403280b9SMatthias Ringwald } 2286403280b9SMatthias Ringwald if (clear_flag){ 2287403280b9SMatthias Ringwald flags &= ~(1<<i); 2288403280b9SMatthias Ringwald } 2289403280b9SMatthias Ringwald action = i; 2290403280b9SMatthias Ringwald break; 2291403280b9SMatthias Ringwald } 2292403280b9SMatthias Ringwald } 2293403280b9SMatthias Ringwald setup->sm_keypress_notification = (num_actions << 5) | flags; 2294403280b9SMatthias Ringwald 2295403280b9SMatthias Ringwald // send keypress notification 2296403280b9SMatthias Ringwald uint8_t buffer[2]; 2297403280b9SMatthias Ringwald buffer[0] = SM_CODE_KEYPRESS_NOTIFICATION; 2298403280b9SMatthias Ringwald buffer[1] = action; 2299403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2300403280b9SMatthias Ringwald 2301403280b9SMatthias Ringwald // try 2302403280b9SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(sm_active_connection_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL); 2303403280b9SMatthias Ringwald } 2304403280b9SMatthias Ringwald 2305403280b9SMatthias Ringwald static void sm_run_distribute_keys(sm_connection_t * connection){ 2306403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION){ 2307403280b9SMatthias Ringwald setup->sm_key_distribution_send_set &= ~SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION; 2308403280b9SMatthias Ringwald setup->sm_key_distribution_sent_set |= SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION; 2309403280b9SMatthias Ringwald uint8_t buffer[17]; 2310403280b9SMatthias Ringwald buffer[0] = SM_CODE_ENCRYPTION_INFORMATION; 2311403280b9SMatthias Ringwald reverse_128(setup->sm_ltk, &buffer[1]); 2312403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2313403280b9SMatthias Ringwald sm_timeout_reset(connection); 2314403280b9SMatthias Ringwald return; 2315403280b9SMatthias Ringwald } 2316403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION){ 2317403280b9SMatthias Ringwald setup->sm_key_distribution_send_set &= ~SM_KEYDIST_FLAG_MASTER_IDENTIFICATION; 2318403280b9SMatthias Ringwald setup->sm_key_distribution_sent_set |= SM_KEYDIST_FLAG_MASTER_IDENTIFICATION; 2319403280b9SMatthias Ringwald uint8_t buffer[11]; 2320403280b9SMatthias Ringwald buffer[0] = SM_CODE_MASTER_IDENTIFICATION; 2321403280b9SMatthias Ringwald little_endian_store_16(buffer, 1, setup->sm_local_ediv); 2322403280b9SMatthias Ringwald reverse_64(setup->sm_local_rand, &buffer[3]); 2323403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2324403280b9SMatthias Ringwald sm_timeout_reset(connection); 2325403280b9SMatthias Ringwald return; 2326403280b9SMatthias Ringwald } 2327403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ 2328403280b9SMatthias Ringwald setup->sm_key_distribution_send_set &= ~SM_KEYDIST_FLAG_IDENTITY_INFORMATION; 2329403280b9SMatthias Ringwald setup->sm_key_distribution_sent_set |= SM_KEYDIST_FLAG_IDENTITY_INFORMATION; 2330403280b9SMatthias Ringwald uint8_t buffer[17]; 2331403280b9SMatthias Ringwald buffer[0] = SM_CODE_IDENTITY_INFORMATION; 2332403280b9SMatthias Ringwald reverse_128(sm_persistent_irk, &buffer[1]); 2333403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2334403280b9SMatthias Ringwald sm_timeout_reset(connection); 2335403280b9SMatthias Ringwald return; 2336403280b9SMatthias Ringwald } 2337403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION){ 2338403280b9SMatthias Ringwald setup->sm_key_distribution_send_set &= ~SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION; 2339403280b9SMatthias Ringwald setup->sm_key_distribution_sent_set |= SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION; 2340403280b9SMatthias Ringwald bd_addr_t local_address; 2341403280b9SMatthias Ringwald uint8_t buffer[8]; 2342403280b9SMatthias Ringwald buffer[0] = SM_CODE_IDENTITY_ADDRESS_INFORMATION; 2343403280b9SMatthias Ringwald switch (gap_random_address_get_mode()){ 2344403280b9SMatthias Ringwald case GAP_RANDOM_ADDRESS_TYPE_OFF: 2345403280b9SMatthias Ringwald case GAP_RANDOM_ADDRESS_TYPE_STATIC: 2346403280b9SMatthias Ringwald // public or static random 2347403280b9SMatthias Ringwald gap_le_get_own_address(&buffer[1], local_address); 2348403280b9SMatthias Ringwald break; 2349403280b9SMatthias Ringwald case GAP_RANDOM_ADDRESS_NON_RESOLVABLE: 2350403280b9SMatthias Ringwald case GAP_RANDOM_ADDRESS_RESOLVABLE: 2351403280b9SMatthias Ringwald // fallback to public 2352403280b9SMatthias Ringwald gap_local_bd_addr(local_address); 2353403280b9SMatthias Ringwald buffer[1] = 0; 2354403280b9SMatthias Ringwald break; 2355403280b9SMatthias Ringwald default: 2356403280b9SMatthias Ringwald btstack_assert(false); 2357403280b9SMatthias Ringwald break; 2358403280b9SMatthias Ringwald } 2359403280b9SMatthias Ringwald reverse_bd_addr(local_address, &buffer[2]); 2360403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2361403280b9SMatthias Ringwald sm_timeout_reset(connection); 2362403280b9SMatthias Ringwald return; 2363403280b9SMatthias Ringwald } 2364403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 2365403280b9SMatthias Ringwald setup->sm_key_distribution_send_set &= ~SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION; 2366403280b9SMatthias Ringwald setup->sm_key_distribution_sent_set |= SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION; 2367403280b9SMatthias Ringwald 2368403280b9SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 2369403280b9SMatthias Ringwald // hack to reproduce test runs 2370403280b9SMatthias Ringwald if (test_use_fixed_local_csrk){ 2371403280b9SMatthias Ringwald memset(setup->sm_local_csrk, 0xcc, 16); 2372403280b9SMatthias Ringwald } 2373403280b9SMatthias Ringwald 2374403280b9SMatthias Ringwald // store local CSRK 2375403280b9SMatthias Ringwald if (setup->sm_le_device_index >= 0){ 2376403280b9SMatthias Ringwald log_info("sm: store local CSRK"); 2377403280b9SMatthias Ringwald le_device_db_local_csrk_set(setup->sm_le_device_index, setup->sm_local_csrk); 2378403280b9SMatthias Ringwald le_device_db_local_counter_set(setup->sm_le_device_index, 0); 2379403280b9SMatthias Ringwald } 2380403280b9SMatthias Ringwald #endif 2381403280b9SMatthias Ringwald 2382403280b9SMatthias Ringwald uint8_t buffer[17]; 2383403280b9SMatthias Ringwald buffer[0] = SM_CODE_SIGNING_INFORMATION; 2384403280b9SMatthias Ringwald reverse_128(setup->sm_local_csrk, &buffer[1]); 2385403280b9SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2386403280b9SMatthias Ringwald sm_timeout_reset(connection); 2387403280b9SMatthias Ringwald return; 2388403280b9SMatthias Ringwald } 2389403280b9SMatthias Ringwald btstack_assert(false); 2390403280b9SMatthias Ringwald } 2391403280b9SMatthias Ringwald 2392bbd73538SMatthias Ringwald static bool sm_ctkd_from_le(sm_connection_t *sm_connection) { 2393bbd73538SMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 2394bbd73538SMatthias Ringwald // requirements to derive link key from LE: 2395bbd73538SMatthias Ringwald // - use secure connections 2396bbd73538SMatthias Ringwald if (setup->sm_use_secure_connections == 0) return false; 2397bbd73538SMatthias Ringwald // - bonding needs to be enabled: 2398bbd73538SMatthias Ringwald bool bonding_enabled = (sm_pairing_packet_get_auth_req(setup->sm_m_preq) & sm_pairing_packet_get_auth_req(setup->sm_s_pres) & SM_AUTHREQ_BONDING ) != 0u; 2399bbd73538SMatthias Ringwald if (!bonding_enabled) return false; 2400bbd73538SMatthias Ringwald // - need identity address / public addr 2401bbd73538SMatthias Ringwald bool have_identity_address_info = ((setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION) != 0) || (setup->sm_peer_addr_type == 0); 2402bbd73538SMatthias Ringwald if (!have_identity_address_info) return false; 2403bbd73538SMatthias Ringwald // - there is no stored BR/EDR link key or the derived key has at least the same level of authentication (bail if stored key has higher authentication) 2404bbd73538SMatthias Ringwald // this requirement is motivated by BLURtooth paper. The paper recommends to not overwrite keys at all. 2405bbd73538SMatthias Ringwald // If SC is authenticated, we consider it safe to overwrite a stored key. 2406bbd73538SMatthias Ringwald // If stored link key is not authenticated, it could already be compromised by a MITM attack. Allowing overwrite by unauthenticated derived key does not make it worse. 2407bbd73538SMatthias Ringwald uint8_t link_key[16]; 2408bbd73538SMatthias Ringwald link_key_type_t link_key_type; 2409bbd73538SMatthias Ringwald bool have_link_key = gap_get_link_key_for_bd_addr(setup->sm_peer_address, link_key, &link_key_type); 2410bbd73538SMatthias Ringwald bool link_key_authenticated = gap_authenticated_for_link_key_type(link_key_type) != 0; 2411bbd73538SMatthias Ringwald bool derived_key_authenticated = sm_connection->sm_connection_authenticated != 0; 2412bbd73538SMatthias Ringwald if (have_link_key && link_key_authenticated && !derived_key_authenticated) { 2413bbd73538SMatthias Ringwald return false; 2414bbd73538SMatthias Ringwald } 2415bbd73538SMatthias Ringwald // get started (all of the above are true) 2416bbd73538SMatthias Ringwald return true; 2417bbd73538SMatthias Ringwald #else 2418bbd73538SMatthias Ringwald UNUSED(sm_connection); 2419bbd73538SMatthias Ringwald return false; 2420bbd73538SMatthias Ringwald #endif 2421bbd73538SMatthias Ringwald } 2422bbd73538SMatthias Ringwald 24236f7422f1SMatthias Ringwald static void sm_key_distribution_complete_responder(sm_connection_t * connection){ 24246f7422f1SMatthias Ringwald if (sm_ctkd_from_le(connection)){ 24256f7422f1SMatthias Ringwald bool use_h7 = (sm_pairing_packet_get_auth_req(setup->sm_m_preq) & sm_pairing_packet_get_auth_req(setup->sm_s_pres) & SM_AUTHREQ_CT2) != 0; 24266f7422f1SMatthias Ringwald connection->sm_engine_state = use_h7 ? SM_SC_W2_CALCULATE_ILK_USING_H7 : SM_SC_W2_CALCULATE_ILK_USING_H6; 24276f7422f1SMatthias Ringwald } else { 24286f7422f1SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_IDLE; 24296f7422f1SMatthias Ringwald sm_pairing_complete(connection, ERROR_CODE_SUCCESS, 0); 24306f7422f1SMatthias Ringwald sm_done_for_handle(connection->sm_handle); 24316f7422f1SMatthias Ringwald } 24326f7422f1SMatthias Ringwald } 24336f7422f1SMatthias Ringwald 2434*af7ef9d1SMatthias Ringwald static void sm_key_distribution_complete_initiator(sm_connection_t * connection){ 2435*af7ef9d1SMatthias Ringwald if (sm_ctkd_from_le(connection)){ 2436*af7ef9d1SMatthias Ringwald bool use_h7 = (sm_pairing_packet_get_auth_req(setup->sm_m_preq) & sm_pairing_packet_get_auth_req(setup->sm_s_pres) & SM_AUTHREQ_CT2) != 0; 2437*af7ef9d1SMatthias Ringwald connection->sm_engine_state = use_h7 ? SM_SC_W2_CALCULATE_ILK_USING_H7 : SM_SC_W2_CALCULATE_ILK_USING_H6; 2438*af7ef9d1SMatthias Ringwald } else { 2439*af7ef9d1SMatthias Ringwald sm_master_pairing_success(connection); 2440*af7ef9d1SMatthias Ringwald } 2441*af7ef9d1SMatthias Ringwald } 2442*af7ef9d1SMatthias Ringwald 2443d7f1c72eSMatthias Ringwald static void sm_run(void){ 2444d7f1c72eSMatthias Ringwald 2445d7f1c72eSMatthias Ringwald // assert that stack has already bootet 2446d7f1c72eSMatthias Ringwald if (hci_get_state() != HCI_STATE_WORKING) return; 2447d7f1c72eSMatthias Ringwald 2448d7f1c72eSMatthias Ringwald // assert that we can send at least commands 2449d7f1c72eSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 2450d7f1c72eSMatthias Ringwald 2451d7f1c72eSMatthias Ringwald // pause until IR/ER are ready 2452d7f1c72eSMatthias Ringwald if (sm_persistent_keys_random_active) return; 2453d7f1c72eSMatthias Ringwald 2454d7f1c72eSMatthias Ringwald bool done; 2455d7f1c72eSMatthias Ringwald 2456d7f1c72eSMatthias Ringwald // 2457d7f1c72eSMatthias Ringwald // non-connection related behaviour 2458d7f1c72eSMatthias Ringwald // 2459d7f1c72eSMatthias Ringwald 2460d7f1c72eSMatthias Ringwald done = sm_run_dpkg(); 2461d7f1c72eSMatthias Ringwald if (done) return; 2462d7f1c72eSMatthias Ringwald 2463d7f1c72eSMatthias Ringwald done = sm_run_rau(); 2464d7f1c72eSMatthias Ringwald if (done) return; 2465d7f1c72eSMatthias Ringwald 2466d7f1c72eSMatthias Ringwald done = sm_run_csrk(); 2467d7f1c72eSMatthias Ringwald if (done) return; 2468d7f1c72eSMatthias Ringwald 2469d7f1c72eSMatthias Ringwald done = sm_run_oob(); 2470d7f1c72eSMatthias Ringwald if (done) return; 2471d7f1c72eSMatthias Ringwald 2472d7f1c72eSMatthias Ringwald // assert that we can send at least commands - cmd might have been sent by crypto engine 2473d7f1c72eSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 2474d7f1c72eSMatthias Ringwald 2475d7f1c72eSMatthias Ringwald // handle basic actions that don't requires the full context 2476d7f1c72eSMatthias Ringwald done = sm_run_basic(); 2477d7f1c72eSMatthias Ringwald if (done) return; 2478d7f1c72eSMatthias Ringwald 2479d7f1c72eSMatthias Ringwald // 2480d7f1c72eSMatthias Ringwald // active connection handling 2481d7f1c72eSMatthias Ringwald // -- use loop to handle next connection if lock on setup context is released 2482d7f1c72eSMatthias Ringwald 2483d7f1c72eSMatthias Ringwald while (true) { 2484d7f1c72eSMatthias Ringwald 2485d7f1c72eSMatthias Ringwald sm_run_activate_connection(); 2486d7f1c72eSMatthias Ringwald 2487d7f1c72eSMatthias Ringwald if (sm_active_connection_handle == HCI_CON_HANDLE_INVALID) return; 24883deb3ec6SMatthias Ringwald 24893deb3ec6SMatthias Ringwald // 24903deb3ec6SMatthias Ringwald // active connection handling 24913deb3ec6SMatthias Ringwald // 24923deb3ec6SMatthias Ringwald 24933cf37b8cSMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(sm_active_connection_handle); 24943cf37b8cSMatthias Ringwald if (!connection) { 24953cf37b8cSMatthias Ringwald log_info("no connection for handle 0x%04x", sm_active_connection_handle); 24963cf37b8cSMatthias Ringwald return; 24973cf37b8cSMatthias Ringwald } 24983cf37b8cSMatthias Ringwald 24993deb3ec6SMatthias Ringwald // assert that we could send a SM PDU - not needed for all of the following 25007149bde5SMatthias Ringwald if (!l2cap_can_send_fixed_channel_packet_now(sm_active_connection_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL)) { 25017149bde5SMatthias Ringwald log_info("cannot send now, requesting can send now event"); 25027149bde5SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(sm_active_connection_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL); 2503b170b20fSMatthias Ringwald return; 2504b170b20fSMatthias Ringwald } 25053deb3ec6SMatthias Ringwald 25063d7fe1e9SMatthias Ringwald // send keypress notifications 2507dd4a08fbSMatthias Ringwald if (setup->sm_keypress_notification){ 2508403280b9SMatthias Ringwald sm_run_send_keypress_notification(connection); 2509d7471931SMatthias Ringwald return; 25103d7fe1e9SMatthias Ringwald } 25113d7fe1e9SMatthias Ringwald 25123deb3ec6SMatthias Ringwald int key_distribution_flags; 251342134bc6SMatthias Ringwald UNUSED(key_distribution_flags); 2514b6afa23eSMatthias Ringwald int err; 2515b6afa23eSMatthias Ringwald UNUSED(err); 25169b75de03SMatthias Ringwald bool have_ltk; 25179b75de03SMatthias Ringwald uint8_t ltk[16]; 25183deb3ec6SMatthias Ringwald 25193deb3ec6SMatthias Ringwald log_info("sm_run: state %u", connection->sm_engine_state); 2520dd4a08fbSMatthias Ringwald if (!l2cap_can_send_fixed_channel_packet_now(sm_active_connection_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL)) { 2521dd4a08fbSMatthias Ringwald log_info("sm_run // cannot send"); 2522dd4a08fbSMatthias Ringwald } 25233deb3ec6SMatthias Ringwald switch (connection->sm_engine_state){ 25243deb3ec6SMatthias Ringwald 25253deb3ec6SMatthias Ringwald // general 25263deb3ec6SMatthias Ringwald case SM_GENERAL_SEND_PAIRING_FAILED: { 25273deb3ec6SMatthias Ringwald uint8_t buffer[2]; 25283deb3ec6SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_FAILED; 25293deb3ec6SMatthias Ringwald buffer[1] = setup->sm_pairing_failed_reason; 25303deb3ec6SMatthias Ringwald connection->sm_engine_state = connection->sm_role ? SM_RESPONDER_IDLE : SM_INITIATOR_CONNECTED; 25313deb3ec6SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 25320ccf6c9cSMatthias Ringwald sm_pairing_complete(connection, ERROR_CODE_AUTHENTICATION_FAILURE, setup->sm_pairing_failed_reason); 25333deb3ec6SMatthias Ringwald sm_done_for_handle(connection->sm_handle); 25343deb3ec6SMatthias Ringwald break; 25353deb3ec6SMatthias Ringwald } 25363deb3ec6SMatthias Ringwald 2537f32b7a88SMatthias Ringwald // secure connections, initiator + responding states 2538aec94140SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 2539aec94140SMatthias Ringwald case SM_SC_W2_CMAC_FOR_CONFIRMATION: 2540aec94140SMatthias Ringwald if (!sm_cmac_ready()) break; 2541aec94140SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CMAC_FOR_CONFIRMATION; 2542aec94140SMatthias Ringwald sm_sc_calculate_local_confirm(connection); 2543aec94140SMatthias Ringwald break; 2544688a08f9SMatthias Ringwald case SM_SC_W2_CMAC_FOR_CHECK_CONFIRMATION: 2545688a08f9SMatthias Ringwald if (!sm_cmac_ready()) break; 2546688a08f9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CMAC_FOR_CHECK_CONFIRMATION; 2547688a08f9SMatthias Ringwald sm_sc_calculate_remote_confirm(connection); 2548688a08f9SMatthias Ringwald break; 2549dc300847SMatthias Ringwald case SM_SC_W2_CALCULATE_F6_FOR_DHKEY_CHECK: 2550dc300847SMatthias Ringwald if (!sm_cmac_ready()) break; 2551dc300847SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_F6_FOR_DHKEY_CHECK; 2552dc300847SMatthias Ringwald sm_sc_calculate_f6_for_dhkey_check(connection); 2553dc300847SMatthias Ringwald break; 2554019005a0SMatthias Ringwald case SM_SC_W2_CALCULATE_F6_TO_VERIFY_DHKEY_CHECK: 2555b35a3de2SMatthias Ringwald if (!sm_cmac_ready()) break; 2556019005a0SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_F6_TO_VERIFY_DHKEY_CHECK; 25570346c37cSMatthias Ringwald sm_sc_calculate_f6_to_verify_dhkey_check(connection); 25580346c37cSMatthias Ringwald break; 25590346c37cSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_SALT: 2560b35a3de2SMatthias Ringwald if (!sm_cmac_ready()) break; 25610346c37cSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_F5_SALT; 25620346c37cSMatthias Ringwald f5_calculate_salt(connection); 25630346c37cSMatthias Ringwald break; 25640346c37cSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_MACKEY: 2565b35a3de2SMatthias Ringwald if (!sm_cmac_ready()) break; 25660346c37cSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_F5_MACKEY; 25670346c37cSMatthias Ringwald f5_calculate_mackey(connection); 25680346c37cSMatthias Ringwald break; 25690346c37cSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_LTK: 2570b35a3de2SMatthias Ringwald if (!sm_cmac_ready()) break; 25710346c37cSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_F5_LTK; 25720346c37cSMatthias Ringwald f5_calculate_ltk(connection); 2573019005a0SMatthias Ringwald break; 2574bd57ffebSMatthias Ringwald case SM_SC_W2_CALCULATE_G2: 2575b35a3de2SMatthias Ringwald if (!sm_cmac_ready()) break; 2576bd57ffebSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_G2; 2577b35a3de2SMatthias Ringwald g2_calculate(connection); 2578bd57ffebSMatthias Ringwald break; 25796857ad8fSMatthias Ringwald #ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 258057132f12SMatthias Ringwald case SM_SC_W2_CALCULATE_ILK_USING_H6: 25812bacf595SMatthias Ringwald if (!sm_cmac_ready()) break; 258257132f12SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_ILK; 25832bacf595SMatthias Ringwald h6_calculate_ilk(connection); 25842bacf595SMatthias Ringwald break; 258557132f12SMatthias Ringwald case SM_SC_W2_CALCULATE_BR_EDR_LINK_KEY: 25862bacf595SMatthias Ringwald if (!sm_cmac_ready()) break; 258757132f12SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_BR_EDR_LINK_KEY; 25882bacf595SMatthias Ringwald h6_calculate_br_edr_link_key(connection); 25892bacf595SMatthias Ringwald break; 259057132f12SMatthias Ringwald case SM_SC_W2_CALCULATE_ILK_USING_H7: 259157132f12SMatthias Ringwald if (!sm_cmac_ready()) break; 259257132f12SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CALCULATE_ILK; 259357132f12SMatthias Ringwald h7_calculate_ilk(connection); 259457132f12SMatthias Ringwald break; 2595aec94140SMatthias Ringwald #endif 2596a756d52bSMatthias Ringwald #endif 259741d32297SMatthias Ringwald 259842134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 25993deb3ec6SMatthias Ringwald // initiator side 2600f32b7a88SMatthias Ringwald 26015567aa60SMatthias Ringwald case SM_INITIATOR_PH4_HAS_LTK: { 2602f32b7a88SMatthias Ringwald sm_reset_setup(); 2603f32b7a88SMatthias Ringwald sm_load_security_info(connection); 2604daac6563SMatthias Ringwald sm_reencryption_started(connection); 2605f32b7a88SMatthias Ringwald 26063deb3ec6SMatthias Ringwald sm_key_t peer_ltk_flipped; 26079c80e4ccSMatthias Ringwald reverse_128(setup->sm_peer_ltk, peer_ltk_flipped); 26085567aa60SMatthias Ringwald connection->sm_engine_state = SM_PH4_W4_CONNECTION_ENCRYPTED; 26093deb3ec6SMatthias Ringwald log_info("sm: hci_le_start_encryption ediv 0x%04x", setup->sm_peer_ediv); 2610c9b8fdd9SMatthias Ringwald uint32_t rand_high = big_endian_read_32(setup->sm_peer_rand, 0); 2611c9b8fdd9SMatthias Ringwald uint32_t rand_low = big_endian_read_32(setup->sm_peer_rand, 4); 26123deb3ec6SMatthias Ringwald hci_send_cmd(&hci_le_start_encryption, connection->sm_handle,rand_low, rand_high, setup->sm_peer_ediv, peer_ltk_flipped); 26133deb3ec6SMatthias Ringwald return; 26143deb3ec6SMatthias Ringwald } 26153deb3ec6SMatthias Ringwald 2616b26f445fSMatthias Ringwald case SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST: 2617b26f445fSMatthias Ringwald sm_reset_setup(); 2618b26f445fSMatthias Ringwald sm_init_setup(connection); 2619b26f445fSMatthias Ringwald sm_timeout_start(connection); 2620d3c12277SMatthias Ringwald sm_pairing_started(connection); 2621b26f445fSMatthias Ringwald 26221ad129beSMatthias Ringwald sm_pairing_packet_set_code(setup->sm_m_preq, SM_CODE_PAIRING_REQUEST); 26233deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_INITIATOR_PH1_W4_PAIRING_RESPONSE; 26243deb3ec6SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_m_preq, sizeof(sm_pairing_packet_t)); 26253deb3ec6SMatthias Ringwald sm_timeout_reset(connection); 26263deb3ec6SMatthias Ringwald break; 262742134bc6SMatthias Ringwald #endif 26283deb3ec6SMatthias Ringwald 262927c32905SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 263041d32297SMatthias Ringwald 2631c6b7cbd9SMatthias Ringwald case SM_SC_SEND_PUBLIC_KEY_COMMAND: { 26321979f09cSMatthias Ringwald bool trigger_user_response = false; 26331979f09cSMatthias Ringwald bool trigger_start_calculating_local_confirm = false; 263427c32905SMatthias Ringwald uint8_t buffer[65]; 263527c32905SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_PUBLIC_KEY; 263627c32905SMatthias Ringwald // 2637fc5bff5fSMatthias Ringwald reverse_256(&ec_q[0], &buffer[1]); 2638fc5bff5fSMatthias Ringwald reverse_256(&ec_q[32], &buffer[33]); 2639e53be891SMatthias Ringwald 2640349d0adbSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 2641349d0adbSMatthias Ringwald if (test_pairing_failure == SM_REASON_DHKEY_CHECK_FAILED){ 2642349d0adbSMatthias Ringwald log_info("testing_support: invalidating public key"); 2643349d0adbSMatthias Ringwald // flip single bit of public key coordinate 2644349d0adbSMatthias Ringwald buffer[1] ^= 1; 2645349d0adbSMatthias Ringwald } 2646349d0adbSMatthias Ringwald #endif 2647349d0adbSMatthias Ringwald 264845a61d50SMatthias Ringwald // stk generation method 264945a61d50SMatthias Ringwald // passkey entry: notify app to show passkey or to request passkey 265045a61d50SMatthias Ringwald switch (setup->sm_stk_generation_method){ 265145a61d50SMatthias Ringwald case JUST_WORKS: 265247fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 265342134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 265407036a04SMatthias Ringwald // responder 26551979f09cSMatthias Ringwald trigger_start_calculating_local_confirm = true; 2656b90c4e75SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_LOCAL_NONCE; 265727c32905SMatthias Ringwald } else { 265807036a04SMatthias Ringwald // initiator 2659c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND; 266027c32905SMatthias Ringwald } 266145a61d50SMatthias Ringwald break; 266245a61d50SMatthias Ringwald case PK_INIT_INPUT: 266345a61d50SMatthias Ringwald case PK_RESP_INPUT: 266447fb4255SMatthias Ringwald case PK_BOTH_INPUT: 266507036a04SMatthias Ringwald // use random TK for display 26666535961aSMatthias Ringwald (void)memcpy(setup->sm_ra, setup->sm_tk, 16); 26676535961aSMatthias Ringwald (void)memcpy(setup->sm_rb, setup->sm_tk, 16); 266845a61d50SMatthias Ringwald setup->sm_passkey_bit = 0; 266907036a04SMatthias Ringwald 267042134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 267145a61d50SMatthias Ringwald // responder 2672c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CONFIRMATION; 267345a61d50SMatthias Ringwald } else { 267445a61d50SMatthias Ringwald // initiator 2675c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND; 267645a61d50SMatthias Ringwald } 26771979f09cSMatthias Ringwald trigger_user_response = true; 267845a61d50SMatthias Ringwald break; 267945a61d50SMatthias Ringwald case OOB: 268065a9a04eSMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 268165a9a04eSMatthias Ringwald // responder 268265a9a04eSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PAIRING_RANDOM; 268365a9a04eSMatthias Ringwald } else { 268465a9a04eSMatthias Ringwald // initiator 268565a9a04eSMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND; 268665a9a04eSMatthias Ringwald } 268745a61d50SMatthias Ringwald break; 26887bbeb3adSMilanka Ringwald default: 26897bbeb3adSMilanka Ringwald btstack_assert(false); 26907bbeb3adSMilanka Ringwald break; 269145a61d50SMatthias Ringwald } 269245a61d50SMatthias Ringwald 269327c32905SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 269427c32905SMatthias Ringwald sm_timeout_reset(connection); 2695644c6a1dSMatthias Ringwald 2696b90c4e75SMatthias Ringwald // trigger user response and calc confirm after sending pdu 2697644c6a1dSMatthias Ringwald if (trigger_user_response){ 2698644c6a1dSMatthias Ringwald sm_trigger_user_response(connection); 2699644c6a1dSMatthias Ringwald } 2700b90c4e75SMatthias Ringwald if (trigger_start_calculating_local_confirm){ 2701b90c4e75SMatthias Ringwald sm_sc_start_calculating_local_confirm(connection); 2702b90c4e75SMatthias Ringwald } 270327c32905SMatthias Ringwald break; 270427c32905SMatthias Ringwald } 2705c6b7cbd9SMatthias Ringwald case SM_SC_SEND_CONFIRMATION: { 2706e53be891SMatthias Ringwald uint8_t buffer[17]; 2707e53be891SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_CONFIRM; 27089af0f475SMatthias Ringwald reverse_128(setup->sm_local_confirm, &buffer[1]); 270942134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 2710c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PAIRING_RANDOM; 2711e53be891SMatthias Ringwald } else { 2712c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CONFIRMATION; 2713e53be891SMatthias Ringwald } 2714e53be891SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2715e53be891SMatthias Ringwald sm_timeout_reset(connection); 2716e53be891SMatthias Ringwald break; 2717e53be891SMatthias Ringwald } 2718c6b7cbd9SMatthias Ringwald case SM_SC_SEND_PAIRING_RANDOM: { 2719e53be891SMatthias Ringwald uint8_t buffer[17]; 2720e53be891SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_RANDOM; 2721e53be891SMatthias Ringwald reverse_128(setup->sm_local_nonce, &buffer[1]); 27229d0de1e9SMatthias Ringwald log_info("stk method %u, bit num: %u", setup->sm_stk_generation_method, setup->sm_passkey_bit); 27234ea43905SMatthias Ringwald if (sm_passkey_entry(setup->sm_stk_generation_method) && (setup->sm_passkey_bit < 20u)){ 272440c5d850SMatthias Ringwald log_info("SM_SC_SEND_PAIRING_RANDOM A"); 272542134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 272645a61d50SMatthias Ringwald // responder 2727c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_CONFIRMATION; 272845a61d50SMatthias Ringwald } else { 272945a61d50SMatthias Ringwald // initiator 2730c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PAIRING_RANDOM; 273145a61d50SMatthias Ringwald } 273245a61d50SMatthias Ringwald } else { 273340c5d850SMatthias Ringwald log_info("SM_SC_SEND_PAIRING_RANDOM B"); 273442134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 2735e53be891SMatthias Ringwald // responder 273647fb4255SMatthias Ringwald if (setup->sm_stk_generation_method == NUMERIC_COMPARISON){ 273740c5d850SMatthias Ringwald log_info("SM_SC_SEND_PAIRING_RANDOM B1"); 2738901c000fSMatthias Ringwald connection->sm_engine_state = SM_SC_W2_CALCULATE_G2; 27392886623dSMatthias Ringwald } else { 274040c5d850SMatthias Ringwald log_info("SM_SC_SEND_PAIRING_RANDOM B2"); 27412886623dSMatthias Ringwald sm_sc_prepare_dhkey_check(connection); 2742446a8c36SMatthias Ringwald } 2743e53be891SMatthias Ringwald } else { 2744136d331aSMatthias Ringwald // initiator 2745c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PAIRING_RANDOM; 2746e53be891SMatthias Ringwald } 274745a61d50SMatthias Ringwald } 2748e53be891SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2749e53be891SMatthias Ringwald sm_timeout_reset(connection); 2750e53be891SMatthias Ringwald break; 2751e53be891SMatthias Ringwald } 2752c6b7cbd9SMatthias Ringwald case SM_SC_SEND_DHKEY_CHECK_COMMAND: { 2753e083ca23SMatthias Ringwald uint8_t buffer[17]; 2754e083ca23SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_DHKEY_CHECK; 2755e53be891SMatthias Ringwald reverse_128(setup->sm_local_dhkey_check, &buffer[1]); 2756dc300847SMatthias Ringwald 275742134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 2758c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_LTK_REQUEST_SC; 2759e53be891SMatthias Ringwald } else { 2760c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_DHKEY_CHECK_COMMAND; 2761e53be891SMatthias Ringwald } 2762e083ca23SMatthias Ringwald 2763e53be891SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 2764e53be891SMatthias Ringwald sm_timeout_reset(connection); 2765e53be891SMatthias Ringwald break; 2766e53be891SMatthias Ringwald } 2767e53be891SMatthias Ringwald 2768e53be891SMatthias Ringwald #endif 276942134bc6SMatthias Ringwald 277042134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2771dd12a62bSMatthias Ringwald 277287014f74SMatthias Ringwald case SM_RESPONDER_SEND_SECURITY_REQUEST: { 277387014f74SMatthias Ringwald const uint8_t buffer[2] = {SM_CODE_SECURITY_REQUEST, sm_auth_req}; 277487014f74SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_REQUEST; 277587014f74SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t *) buffer, sizeof(buffer)); 2776c8d0ff33SMatthias Ringwald sm_timeout_start(connection); 277787014f74SMatthias Ringwald break; 277887014f74SMatthias Ringwald } 277987014f74SMatthias Ringwald 278038196718SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 278138196718SMatthias Ringwald case SM_SC_RECEIVED_LTK_REQUEST: 278238196718SMatthias Ringwald switch (connection->sm_irk_lookup_state){ 278338196718SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 278438196718SMatthias Ringwald // assuming Secure Connection, we have a stored LTK and the EDIV/RAND are null 278538196718SMatthias Ringwald // start using context by loading security info 278638196718SMatthias Ringwald sm_reset_setup(); 278738196718SMatthias Ringwald sm_load_security_info(connection); 278838196718SMatthias Ringwald if ((setup->sm_peer_ediv == 0u) && sm_is_null_random(setup->sm_peer_rand) && !sm_is_null_key(setup->sm_peer_ltk)){ 278938196718SMatthias Ringwald (void)memcpy(setup->sm_ltk, setup->sm_peer_ltk, 16); 279038196718SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH4_SEND_LTK_REPLY; 279142646f38SMatthias Ringwald sm_reencryption_started(connection); 279238196718SMatthias Ringwald sm_trigger_run(); 279338196718SMatthias Ringwald break; 279438196718SMatthias Ringwald } 279538196718SMatthias Ringwald log_info("LTK Request: ediv & random are empty, but no stored LTK (IRK Lookup Succeeded)"); 279638196718SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_IDLE; 279738196718SMatthias Ringwald hci_send_cmd(&hci_le_long_term_key_negative_reply, connection->sm_handle); 279838196718SMatthias Ringwald return; 279938196718SMatthias Ringwald default: 280038196718SMatthias Ringwald // just wait until IRK lookup is completed 280138196718SMatthias Ringwald break; 280238196718SMatthias Ringwald } 280338196718SMatthias Ringwald break; 280438196718SMatthias Ringwald #endif /* ENABLE_LE_SECURE_CONNECTIONS */ 280538196718SMatthias Ringwald 2806b6afa23eSMatthias Ringwald case SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED: 2807b6afa23eSMatthias Ringwald sm_reset_setup(); 28089b75de03SMatthias Ringwald 28099b75de03SMatthias Ringwald // handle Pairing Request with LTK available 28109b75de03SMatthias Ringwald switch (connection->sm_irk_lookup_state) { 28119b75de03SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 28129b75de03SMatthias Ringwald le_device_db_encryption_get(connection->sm_le_db_index, NULL, NULL, ltk, NULL, NULL, NULL, NULL); 28139b75de03SMatthias Ringwald have_ltk = !sm_is_null_key(ltk); 28149b75de03SMatthias Ringwald if (have_ltk){ 28159b75de03SMatthias Ringwald log_info("pairing request but LTK available"); 281619a40772SMatthias Ringwald // emit re-encryption start/fail sequence 28179b75de03SMatthias Ringwald sm_reencryption_started(connection); 28189b75de03SMatthias Ringwald sm_reencryption_complete(connection, ERROR_CODE_PIN_OR_KEY_MISSING); 28199b75de03SMatthias Ringwald } 28209b75de03SMatthias Ringwald break; 28219b75de03SMatthias Ringwald default: 28229b75de03SMatthias Ringwald break; 28239b75de03SMatthias Ringwald } 28249b75de03SMatthias Ringwald 2825b6afa23eSMatthias Ringwald sm_init_setup(connection); 2826d3c12277SMatthias Ringwald sm_pairing_started(connection); 282739543d07SMatthias Ringwald 2828b6afa23eSMatthias Ringwald // recover pairing request 2829b6afa23eSMatthias Ringwald (void)memcpy(&setup->sm_m_preq, &connection->sm_m_preq, sizeof(sm_pairing_packet_t)); 2830b6afa23eSMatthias Ringwald err = sm_stk_generation_init(connection); 2831b6afa23eSMatthias Ringwald 2832b6afa23eSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 2833b6afa23eSMatthias Ringwald if ((0 < test_pairing_failure) && (test_pairing_failure < SM_REASON_DHKEY_CHECK_FAILED)){ 2834b6afa23eSMatthias Ringwald log_info("testing_support: respond with pairing failure %u", test_pairing_failure); 2835b6afa23eSMatthias Ringwald err = test_pairing_failure; 2836b6afa23eSMatthias Ringwald } 2837b6afa23eSMatthias Ringwald #endif 28389305033eSMatthias Ringwald if (err != 0){ 2839b6afa23eSMatthias Ringwald setup->sm_pairing_failed_reason = err; 2840b6afa23eSMatthias Ringwald connection->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; 2841b6afa23eSMatthias Ringwald sm_trigger_run(); 2842b6afa23eSMatthias Ringwald break; 2843b6afa23eSMatthias Ringwald } 2844b6afa23eSMatthias Ringwald 2845b6afa23eSMatthias Ringwald sm_timeout_start(connection); 2846b6afa23eSMatthias Ringwald 2847b6afa23eSMatthias Ringwald // generate random number first, if we need to show passkey, otherwise send response 2848b6afa23eSMatthias Ringwald if (setup->sm_stk_generation_method == PK_INIT_INPUT){ 2849b6afa23eSMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph2_tk, (void *)(uintptr_t) connection->sm_handle); 2850b6afa23eSMatthias Ringwald break; 2851b6afa23eSMatthias Ringwald } 2852b6afa23eSMatthias Ringwald 2853b6afa23eSMatthias Ringwald /* fall through */ 2854b6afa23eSMatthias Ringwald 28553deb3ec6SMatthias Ringwald case SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE: 28561ad129beSMatthias Ringwald sm_pairing_packet_set_code(setup->sm_s_pres,SM_CODE_PAIRING_RESPONSE); 2857f55bd529SMatthias Ringwald 2858f55bd529SMatthias Ringwald // start with initiator key dist flags 28593deb3ec6SMatthias Ringwald key_distribution_flags = sm_key_distribution_flags_for_auth_req(); 28601ad129beSMatthias Ringwald 2861f55bd529SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 2862f55bd529SMatthias Ringwald // LTK (= encyrption information & master identification) only exchanged for LE Legacy Connection 2863f55bd529SMatthias Ringwald if (setup->sm_use_secure_connections){ 2864f55bd529SMatthias Ringwald key_distribution_flags &= ~SM_KEYDIST_ENC_KEY; 2865f55bd529SMatthias Ringwald } 2866f55bd529SMatthias Ringwald #endif 2867f55bd529SMatthias Ringwald // setup in response 2868f55bd529SMatthias Ringwald sm_pairing_packet_set_initiator_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_initiator_key_distribution(setup->sm_m_preq) & key_distribution_flags); 2869f55bd529SMatthias Ringwald sm_pairing_packet_set_responder_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_responder_key_distribution(setup->sm_m_preq) & key_distribution_flags); 2870f55bd529SMatthias Ringwald 2871f55bd529SMatthias Ringwald // update key distribution after ENC was dropped 2872f55bd529SMatthias Ringwald sm_setup_key_distribution(sm_pairing_packet_get_responder_key_distribution(setup->sm_s_pres)); 2873f55bd529SMatthias Ringwald 287427c32905SMatthias Ringwald if (setup->sm_use_secure_connections){ 2875c6b7cbd9SMatthias Ringwald connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND; 28760b8af2a5SMatthias Ringwald } else { 28770b8af2a5SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM; 287827c32905SMatthias Ringwald } 28790b8af2a5SMatthias Ringwald 28803deb3ec6SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t)); 28813deb3ec6SMatthias Ringwald sm_timeout_reset(connection); 2882446a8c36SMatthias Ringwald // SC Numeric Comparison will trigger user response after public keys & nonces have been exchanged 2883c1ab6cc1SMatthias Ringwald if (!setup->sm_use_secure_connections || (setup->sm_stk_generation_method == JUST_WORKS)){ 28843deb3ec6SMatthias Ringwald sm_trigger_user_response(connection); 2885446a8c36SMatthias Ringwald } 28863deb3ec6SMatthias Ringwald return; 288742134bc6SMatthias Ringwald #endif 28883deb3ec6SMatthias Ringwald 28893deb3ec6SMatthias Ringwald case SM_PH2_SEND_PAIRING_RANDOM: { 28903deb3ec6SMatthias Ringwald uint8_t buffer[17]; 28913deb3ec6SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_RANDOM; 28929c80e4ccSMatthias Ringwald reverse_128(setup->sm_local_random, &buffer[1]); 289342134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 28943deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH2_W4_LTK_REQUEST; 28953deb3ec6SMatthias Ringwald } else { 28963deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_RANDOM; 28973deb3ec6SMatthias Ringwald } 28983deb3ec6SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 28993deb3ec6SMatthias Ringwald sm_timeout_reset(connection); 29003deb3ec6SMatthias Ringwald break; 29013deb3ec6SMatthias Ringwald } 29023deb3ec6SMatthias Ringwald 2903d1a1f6a4SMatthias Ringwald case SM_PH2_C1_GET_ENC_A: 29043deb3ec6SMatthias Ringwald // already busy? 29053deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 2906d1a1f6a4SMatthias Ringwald // calculate confirm using aes128 engine - step 1 2907d1a1f6a4SMatthias Ringwald sm_c1_t1(setup->sm_local_random, (uint8_t*) &setup->sm_m_preq, (uint8_t*) &setup->sm_s_pres, setup->sm_m_addr_type, setup->sm_s_addr_type, sm_aes128_plaintext); 2908d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH2_C1_W4_ENC_A; 2909d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2910f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_enc_a, (void *)(uintptr_t) connection->sm_handle); 29113deb3ec6SMatthias Ringwald break; 29123deb3ec6SMatthias Ringwald 29133deb3ec6SMatthias Ringwald case SM_PH2_C1_GET_ENC_C: 29143deb3ec6SMatthias Ringwald // already busy? 29153deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 29163deb3ec6SMatthias Ringwald // calculate m_confirm using aes128 engine - step 1 2917d1a1f6a4SMatthias Ringwald sm_c1_t1(setup->sm_peer_random, (uint8_t*) &setup->sm_m_preq, (uint8_t*) &setup->sm_s_pres, setup->sm_m_addr_type, setup->sm_s_addr_type, sm_aes128_plaintext); 2918d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH2_C1_W4_ENC_C; 2919d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2920f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_enc_c, (void *)(uintptr_t) connection->sm_handle); 29213deb3ec6SMatthias Ringwald break; 2922d1a1f6a4SMatthias Ringwald 29233deb3ec6SMatthias Ringwald case SM_PH2_CALC_STK: 29243deb3ec6SMatthias Ringwald // already busy? 29253deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 29263deb3ec6SMatthias Ringwald // calculate STK 292742134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 2928d1a1f6a4SMatthias Ringwald sm_s1_r_prime(setup->sm_local_random, setup->sm_peer_random, sm_aes128_plaintext); 29293deb3ec6SMatthias Ringwald } else { 2930d1a1f6a4SMatthias Ringwald sm_s1_r_prime(setup->sm_peer_random, setup->sm_local_random, sm_aes128_plaintext); 29313deb3ec6SMatthias Ringwald } 2932d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH2_W4_STK; 2933d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2934f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, sm_aes128_plaintext, setup->sm_ltk, sm_handle_encryption_result_enc_stk, (void *)(uintptr_t) connection->sm_handle); 29353deb3ec6SMatthias Ringwald break; 2936d1a1f6a4SMatthias Ringwald 29373deb3ec6SMatthias Ringwald case SM_PH3_Y_GET_ENC: 29383deb3ec6SMatthias Ringwald // already busy? 29393deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 29403deb3ec6SMatthias Ringwald // PH3B2 - calculate Y from - enc 29419ad0dd7cSMatthias Ringwald 29429ad0dd7cSMatthias Ringwald // dm helper (was sm_dm_r_prime) 29439ad0dd7cSMatthias Ringwald // r' = padding || r 29449ad0dd7cSMatthias Ringwald // r - 64 bit value 29459ad0dd7cSMatthias Ringwald memset(&sm_aes128_plaintext[0], 0, 8); 29466535961aSMatthias Ringwald (void)memcpy(&sm_aes128_plaintext[8], setup->sm_local_rand, 8); 29479ad0dd7cSMatthias Ringwald 29483deb3ec6SMatthias Ringwald // Y = dm(DHK, Rand) 2949d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH3_Y_W4_ENC; 2950d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 2951f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_dhk, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_enc_ph3_y, (void *)(uintptr_t) connection->sm_handle); 2952d1a1f6a4SMatthias Ringwald break; 2953d1a1f6a4SMatthias Ringwald 29543deb3ec6SMatthias Ringwald case SM_PH2_C1_SEND_PAIRING_CONFIRM: { 29553deb3ec6SMatthias Ringwald uint8_t buffer[17]; 29563deb3ec6SMatthias Ringwald buffer[0] = SM_CODE_PAIRING_CONFIRM; 29579c80e4ccSMatthias Ringwald reverse_128(setup->sm_local_confirm, &buffer[1]); 295842134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 29593deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH2_W4_PAIRING_RANDOM; 29603deb3ec6SMatthias Ringwald } else { 29613deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_CONFIRM; 29623deb3ec6SMatthias Ringwald } 29633deb3ec6SMatthias Ringwald l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); 29643deb3ec6SMatthias Ringwald sm_timeout_reset(connection); 29653deb3ec6SMatthias Ringwald return; 29663deb3ec6SMatthias Ringwald } 296742134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 29683deb3ec6SMatthias Ringwald case SM_RESPONDER_PH2_SEND_LTK_REPLY: { 29693deb3ec6SMatthias Ringwald sm_key_t stk_flipped; 29709c80e4ccSMatthias Ringwald reverse_128(setup->sm_ltk, stk_flipped); 29713deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH2_W4_CONNECTION_ENCRYPTED; 29723deb3ec6SMatthias Ringwald hci_send_cmd(&hci_le_long_term_key_request_reply, connection->sm_handle, stk_flipped); 29733deb3ec6SMatthias Ringwald return; 29743deb3ec6SMatthias Ringwald } 2975d7471931SMatthias Ringwald case SM_RESPONDER_PH4_SEND_LTK_REPLY: { 29763deb3ec6SMatthias Ringwald sm_key_t ltk_flipped; 29779c80e4ccSMatthias Ringwald reverse_128(setup->sm_ltk, ltk_flipped); 29785567aa60SMatthias Ringwald connection->sm_engine_state = SM_PH4_W4_CONNECTION_ENCRYPTED; 29793deb3ec6SMatthias Ringwald hci_send_cmd(&hci_le_long_term_key_request_reply, connection->sm_handle, ltk_flipped); 29803deb3ec6SMatthias Ringwald return; 29813deb3ec6SMatthias Ringwald } 2982dd12a62bSMatthias Ringwald 2983dd12a62bSMatthias Ringwald case SM_RESPONDER_PH0_RECEIVED_LTK_REQUEST: 29843deb3ec6SMatthias Ringwald // already busy? 29853deb3ec6SMatthias Ringwald if (sm_aes128_state == SM_AES128_ACTIVE) break; 29863deb3ec6SMatthias Ringwald log_info("LTK Request: recalculating with ediv 0x%04x", setup->sm_local_ediv); 2987c61cfe5aSMatthias Ringwald 2988dd12a62bSMatthias Ringwald sm_reset_setup(); 2989dd12a62bSMatthias Ringwald sm_start_calculating_ltk_from_ediv_and_rand(connection); 2990dd12a62bSMatthias Ringwald 299142646f38SMatthias Ringwald sm_reencryption_started(connection); 299242646f38SMatthias Ringwald 2993c61cfe5aSMatthias Ringwald // dm helper (was sm_dm_r_prime) 2994c61cfe5aSMatthias Ringwald // r' = padding || r 2995c61cfe5aSMatthias Ringwald // r - 64 bit value 2996c61cfe5aSMatthias Ringwald memset(&sm_aes128_plaintext[0], 0, 8); 29976535961aSMatthias Ringwald (void)memcpy(&sm_aes128_plaintext[8], setup->sm_local_rand, 8); 2998c61cfe5aSMatthias Ringwald 29993deb3ec6SMatthias Ringwald // Y = dm(DHK, Rand) 3000d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH4_Y_W4_ENC; 3001d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3002f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_dhk, sm_aes128_plaintext, sm_aes128_ciphertext, sm_handle_encryption_result_enc_ph4_y, (void *)(uintptr_t) connection->sm_handle); 30033deb3ec6SMatthias Ringwald return; 300442134bc6SMatthias Ringwald #endif 300542134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 300642134bc6SMatthias Ringwald case SM_INITIATOR_PH3_SEND_START_ENCRYPTION: { 300742134bc6SMatthias Ringwald sm_key_t stk_flipped; 300842134bc6SMatthias Ringwald reverse_128(setup->sm_ltk, stk_flipped); 300942134bc6SMatthias Ringwald connection->sm_engine_state = SM_PH2_W4_CONNECTION_ENCRYPTED; 301042134bc6SMatthias Ringwald hci_send_cmd(&hci_le_start_encryption, connection->sm_handle, 0, 0, 0, stk_flipped); 301142134bc6SMatthias Ringwald return; 301242134bc6SMatthias Ringwald } 301342134bc6SMatthias Ringwald #endif 30143deb3ec6SMatthias Ringwald 30153deb3ec6SMatthias Ringwald case SM_PH3_DISTRIBUTE_KEYS: 3016403280b9SMatthias Ringwald if (setup->sm_key_distribution_send_set != 0){ 3017403280b9SMatthias Ringwald sm_run_distribute_keys(connection); 30183deb3ec6SMatthias Ringwald return; 30193deb3ec6SMatthias Ringwald } 30203deb3ec6SMatthias Ringwald 30213deb3ec6SMatthias Ringwald // keys are sent 302242134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 30233deb3ec6SMatthias Ringwald // slave -> receive master keys if any 30243deb3ec6SMatthias Ringwald if (sm_key_distribution_all_received(connection)){ 30253deb3ec6SMatthias Ringwald sm_key_distribution_handle_all_received(connection); 30263deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_IDLE; 30270ccf6c9cSMatthias Ringwald sm_pairing_complete(connection, ERROR_CODE_SUCCESS, 0); 30283deb3ec6SMatthias Ringwald sm_done_for_handle(connection->sm_handle); 30293deb3ec6SMatthias Ringwald } else { 30303deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH3_RECEIVE_KEYS; 30313deb3ec6SMatthias Ringwald } 30323deb3ec6SMatthias Ringwald } else { 30331dca9d8aSMatthias Ringwald sm_master_pairing_success(connection); 30343deb3ec6SMatthias Ringwald } 30353deb3ec6SMatthias Ringwald break; 30363deb3ec6SMatthias Ringwald 30373deb3ec6SMatthias Ringwald default: 30383deb3ec6SMatthias Ringwald break; 30393deb3ec6SMatthias Ringwald } 30403deb3ec6SMatthias Ringwald 30413deb3ec6SMatthias Ringwald // check again if active connection was released 30427149bde5SMatthias Ringwald if (sm_active_connection_handle != HCI_CON_HANDLE_INVALID) break; 30433deb3ec6SMatthias Ringwald } 30443deb3ec6SMatthias Ringwald } 30453deb3ec6SMatthias Ringwald 3046d1a1f6a4SMatthias Ringwald // sm_aes128_state stays active 3047d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_a(void *arg){ 3048f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 304904678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 305004678764SMatthias Ringwald 3051f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3052f3582630SMatthias Ringwald if (connection == NULL) return; 3053f3582630SMatthias Ringwald 3054d1a1f6a4SMatthias Ringwald sm_c1_t3(sm_aes128_ciphertext, setup->sm_m_address, setup->sm_s_address, setup->sm_c1_t3_value); 305504678764SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3056f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, setup->sm_c1_t3_value, setup->sm_local_confirm, sm_handle_encryption_result_enc_b, (void *)(uintptr_t) connection->sm_handle); 3057d1a1f6a4SMatthias Ringwald } 30583deb3ec6SMatthias Ringwald 3059d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_b(void *arg){ 3060f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 306104678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 306204678764SMatthias Ringwald 3063f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3064f3582630SMatthias Ringwald if (connection == NULL) return; 3065f3582630SMatthias Ringwald 30668314c363SMatthias Ringwald log_info_key("c1!", setup->sm_local_confirm); 30673deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH2_C1_SEND_PAIRING_CONFIRM; 306870b44dd4SMatthias Ringwald sm_trigger_run(); 3069d1a1f6a4SMatthias Ringwald } 3070d1a1f6a4SMatthias Ringwald 3071d1a1f6a4SMatthias Ringwald // sm_aes128_state stays active 3072d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_c(void *arg){ 3073f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 307404678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 307504678764SMatthias Ringwald 3076f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3077f3582630SMatthias Ringwald if (connection == NULL) return; 3078f3582630SMatthias Ringwald 3079d1a1f6a4SMatthias Ringwald sm_c1_t3(sm_aes128_ciphertext, setup->sm_m_address, setup->sm_s_address, setup->sm_c1_t3_value); 308004678764SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3081f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, setup->sm_c1_t3_value, sm_aes128_ciphertext, sm_handle_encryption_result_enc_d, (void *)(uintptr_t) connection->sm_handle); 3082d1a1f6a4SMatthias Ringwald } 3083d1a1f6a4SMatthias Ringwald 3084d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_d(void * arg){ 3085f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 308604678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 308704678764SMatthias Ringwald 3088f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3089f3582630SMatthias Ringwald if (connection == NULL) return; 3090f3582630SMatthias Ringwald 3091d1a1f6a4SMatthias Ringwald log_info_key("c1!", sm_aes128_ciphertext); 3092d1a1f6a4SMatthias Ringwald if (memcmp(setup->sm_peer_confirm, sm_aes128_ciphertext, 16) != 0){ 30933deb3ec6SMatthias Ringwald setup->sm_pairing_failed_reason = SM_REASON_CONFIRM_VALUE_FAILED; 30943deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; 309570b44dd4SMatthias Ringwald sm_trigger_run(); 30963deb3ec6SMatthias Ringwald return; 30973deb3ec6SMatthias Ringwald } 309842134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 30993deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH2_SEND_PAIRING_RANDOM; 310070b44dd4SMatthias Ringwald sm_trigger_run(); 31013deb3ec6SMatthias Ringwald } else { 3102d1a1f6a4SMatthias Ringwald sm_s1_r_prime(setup->sm_peer_random, setup->sm_local_random, sm_aes128_plaintext); 3103d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3104f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, setup->sm_tk, sm_aes128_plaintext, setup->sm_ltk, sm_handle_encryption_result_enc_stk, (void *)(uintptr_t) connection->sm_handle); 31053deb3ec6SMatthias Ringwald } 31063deb3ec6SMatthias Ringwald } 3107d1a1f6a4SMatthias Ringwald 3108d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_stk(void *arg){ 310904678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 3110f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 311104678764SMatthias Ringwald 3112f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3113f3582630SMatthias Ringwald if (connection == NULL) return; 3114f3582630SMatthias Ringwald 31153deb3ec6SMatthias Ringwald sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size); 31168314c363SMatthias Ringwald log_info_key("stk", setup->sm_ltk); 311742134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 31183deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH2_SEND_LTK_REPLY; 31193deb3ec6SMatthias Ringwald } else { 31203deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_INITIATOR_PH3_SEND_START_ENCRYPTION; 31213deb3ec6SMatthias Ringwald } 312270b44dd4SMatthias Ringwald sm_trigger_run(); 3123d1a1f6a4SMatthias Ringwald } 3124d1a1f6a4SMatthias Ringwald 3125d1a1f6a4SMatthias Ringwald // sm_aes128_state stays active 3126d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph3_y(void *arg){ 3127f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 312804678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 312904678764SMatthias Ringwald 3130f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3131f3582630SMatthias Ringwald if (connection == NULL) return; 3132f3582630SMatthias Ringwald 3133d1a1f6a4SMatthias Ringwald setup->sm_local_y = big_endian_read_16(sm_aes128_ciphertext, 14); 31343deb3ec6SMatthias Ringwald log_info_hex16("y", setup->sm_local_y); 31353deb3ec6SMatthias Ringwald // PH3B3 - calculate EDIV 31363deb3ec6SMatthias Ringwald setup->sm_local_ediv = setup->sm_local_y ^ setup->sm_local_div; 31373deb3ec6SMatthias Ringwald log_info_hex16("ediv", setup->sm_local_ediv); 31383deb3ec6SMatthias Ringwald // PH3B4 - calculate LTK - enc 31393deb3ec6SMatthias Ringwald // LTK = d1(ER, DIV, 0)) 3140d1a1f6a4SMatthias Ringwald sm_d1_d_prime(setup->sm_local_div, 0, sm_aes128_plaintext); 314104678764SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3142f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_er, sm_aes128_plaintext, setup->sm_ltk, sm_handle_encryption_result_enc_ph3_ltk, (void *)(uintptr_t) connection->sm_handle); 31433deb3ec6SMatthias Ringwald } 3144d1a1f6a4SMatthias Ringwald 31452a526f21SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3146d1a1f6a4SMatthias Ringwald // sm_aes128_state stays active 3147d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph4_y(void *arg){ 314804678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 3149f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 315004678764SMatthias Ringwald 3151f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3152f3582630SMatthias Ringwald if (connection == NULL) return; 3153f3582630SMatthias Ringwald 3154d1a1f6a4SMatthias Ringwald setup->sm_local_y = big_endian_read_16(sm_aes128_ciphertext, 14); 31553deb3ec6SMatthias Ringwald log_info_hex16("y", setup->sm_local_y); 31563deb3ec6SMatthias Ringwald 31573deb3ec6SMatthias Ringwald // PH3B3 - calculate DIV 31583deb3ec6SMatthias Ringwald setup->sm_local_div = setup->sm_local_y ^ setup->sm_local_ediv; 31593deb3ec6SMatthias Ringwald log_info_hex16("ediv", setup->sm_local_ediv); 31603deb3ec6SMatthias Ringwald // PH3B4 - calculate LTK - enc 31613deb3ec6SMatthias Ringwald // LTK = d1(ER, DIV, 0)) 3162d1a1f6a4SMatthias Ringwald sm_d1_d_prime(setup->sm_local_div, 0, sm_aes128_plaintext); 316304678764SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3164f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_er, sm_aes128_plaintext, setup->sm_ltk, sm_handle_encryption_result_enc_ph4_ltk, (void *)(uintptr_t) connection->sm_handle); 31653deb3ec6SMatthias Ringwald } 31662a526f21SMatthias Ringwald #endif 3167d1a1f6a4SMatthias Ringwald 3168d1a1f6a4SMatthias Ringwald // sm_aes128_state stays active 3169d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph3_ltk(void *arg){ 3170f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 317104678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 317204678764SMatthias Ringwald 3173f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3174f3582630SMatthias Ringwald if (connection == NULL) return; 3175f3582630SMatthias Ringwald 31768314c363SMatthias Ringwald log_info_key("ltk", setup->sm_ltk); 31773deb3ec6SMatthias Ringwald // calc CSRK next 3178d1a1f6a4SMatthias Ringwald sm_d1_d_prime(setup->sm_local_div, 1, sm_aes128_plaintext); 317904678764SMatthias Ringwald sm_aes128_state = SM_AES128_ACTIVE; 3180f3582630SMatthias Ringwald btstack_crypto_aes128_encrypt(&sm_crypto_aes128_request, sm_persistent_er, sm_aes128_plaintext, setup->sm_local_csrk, sm_handle_encryption_result_enc_csrk, (void *)(uintptr_t) connection->sm_handle); 3181d1a1f6a4SMatthias Ringwald } 3182d1a1f6a4SMatthias Ringwald 3183d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_csrk(void *arg){ 3184f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 318504678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 318604678764SMatthias Ringwald 3187f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3188f3582630SMatthias Ringwald if (connection == NULL) return; 3189f3582630SMatthias Ringwald 3190d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 31918314c363SMatthias Ringwald log_info_key("csrk", setup->sm_local_csrk); 31923deb3ec6SMatthias Ringwald if (setup->sm_key_distribution_send_set){ 31933deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS; 31943deb3ec6SMatthias Ringwald } else { 31953deb3ec6SMatthias Ringwald // no keys to send, just continue 319642134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 31973deb3ec6SMatthias Ringwald // slave -> receive master keys 31983deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH3_RECEIVE_KEYS; 31993deb3ec6SMatthias Ringwald } else { 3200*af7ef9d1SMatthias Ringwald sm_key_distribution_complete_initiator(connection); 32013deb3ec6SMatthias Ringwald } 32022bacf595SMatthias Ringwald } 320370b44dd4SMatthias Ringwald sm_trigger_run(); 3204d1a1f6a4SMatthias Ringwald } 3205d1a1f6a4SMatthias Ringwald 320642134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3207d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_enc_ph4_ltk(void *arg){ 3208f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 320904678764SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 321004678764SMatthias Ringwald 3211f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3212f3582630SMatthias Ringwald if (connection == NULL) return; 3213f3582630SMatthias Ringwald 32143deb3ec6SMatthias Ringwald sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size); 32158314c363SMatthias Ringwald log_info_key("ltk", setup->sm_ltk); 3216d7471931SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH4_SEND_LTK_REPLY; 321770b44dd4SMatthias Ringwald sm_trigger_run(); 3218d1a1f6a4SMatthias Ringwald } 3219d1a1f6a4SMatthias Ringwald #endif 3220d1a1f6a4SMatthias Ringwald 3221d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_address_resolution(void *arg){ 3222d1a1f6a4SMatthias Ringwald UNUSED(arg); 3223d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 322404678764SMatthias Ringwald 3225d1a1f6a4SMatthias Ringwald sm_address_resolution_ah_calculation_active = 0; 3226d1a1f6a4SMatthias Ringwald // compare calulated address against connecting device 3227d1a1f6a4SMatthias Ringwald uint8_t * hash = &sm_aes128_ciphertext[13]; 3228d1a1f6a4SMatthias Ringwald if (memcmp(&sm_address_resolution_address[3], hash, 3) == 0){ 3229d1a1f6a4SMatthias Ringwald log_info("LE Device Lookup: matched resolvable private address"); 3230a66b030fSMatthias Ringwald sm_address_resolution_handle_event(ADDRESS_RESOLUTION_SUCCEEDED); 323170b44dd4SMatthias Ringwald sm_trigger_run(); 32323deb3ec6SMatthias Ringwald return; 32333deb3ec6SMatthias Ringwald } 3234d1a1f6a4SMatthias Ringwald // no match, try next 3235d1a1f6a4SMatthias Ringwald sm_address_resolution_test++; 323670b44dd4SMatthias Ringwald sm_trigger_run(); 32373deb3ec6SMatthias Ringwald } 32383deb3ec6SMatthias Ringwald 3239d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_dkg_irk(void *arg){ 3240d1a1f6a4SMatthias Ringwald UNUSED(arg); 3241d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 324204678764SMatthias Ringwald 3243d1a1f6a4SMatthias Ringwald log_info_key("irk", sm_persistent_irk); 3244d1a1f6a4SMatthias Ringwald dkg_state = DKG_CALC_DHK; 324570b44dd4SMatthias Ringwald sm_trigger_run(); 32467df18c15SMatthias Ringwald } 3247d1a1f6a4SMatthias Ringwald 3248d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_dkg_dhk(void *arg){ 3249d1a1f6a4SMatthias Ringwald UNUSED(arg); 3250d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 325104678764SMatthias Ringwald 3252d1a1f6a4SMatthias Ringwald log_info_key("dhk", sm_persistent_dhk); 3253d1a1f6a4SMatthias Ringwald dkg_state = DKG_READY; 325470b44dd4SMatthias Ringwald sm_trigger_run(); 32557df18c15SMatthias Ringwald } 3256d1a1f6a4SMatthias Ringwald 3257d1a1f6a4SMatthias Ringwald static void sm_handle_encryption_result_rau(void *arg){ 3258d1a1f6a4SMatthias Ringwald UNUSED(arg); 3259d1a1f6a4SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 326004678764SMatthias Ringwald 32616535961aSMatthias Ringwald (void)memcpy(&sm_random_address[3], &sm_aes128_ciphertext[13], 3); 3262d1a1f6a4SMatthias Ringwald rau_state = RAU_SET_ADDRESS; 326370b44dd4SMatthias Ringwald sm_trigger_run(); 326451fa0b28SMatthias Ringwald } 32657df18c15SMatthias Ringwald 3266d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_rau(void * arg){ 3267d1a1f6a4SMatthias Ringwald UNUSED(arg); 32683deb3ec6SMatthias Ringwald // non-resolvable vs. resolvable 32693deb3ec6SMatthias Ringwald switch (gap_random_adress_type){ 32703deb3ec6SMatthias Ringwald case GAP_RANDOM_ADDRESS_RESOLVABLE: 32713deb3ec6SMatthias Ringwald // resolvable: use random as prand and calc address hash 32723deb3ec6SMatthias Ringwald // "The two most significant bits of prand shall be equal to ‘0’ and ‘1" 32734ea43905SMatthias Ringwald sm_random_address[0u] &= 0x3fu; 32744ea43905SMatthias Ringwald sm_random_address[0u] |= 0x40u; 32753deb3ec6SMatthias Ringwald rau_state = RAU_GET_ENC; 32763deb3ec6SMatthias Ringwald break; 32773deb3ec6SMatthias Ringwald case GAP_RANDOM_ADDRESS_NON_RESOLVABLE: 32783deb3ec6SMatthias Ringwald default: 32793deb3ec6SMatthias Ringwald // "The two most significant bits of the address shall be equal to ‘0’"" 32804ea43905SMatthias Ringwald sm_random_address[0u] &= 0x3fu; 32813deb3ec6SMatthias Ringwald rau_state = RAU_SET_ADDRESS; 32823deb3ec6SMatthias Ringwald break; 32833deb3ec6SMatthias Ringwald } 328470b44dd4SMatthias Ringwald sm_trigger_run(); 32853deb3ec6SMatthias Ringwald } 32863deb3ec6SMatthias Ringwald 3287c59d0c92SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 32886ca80073SMatthias Ringwald static void sm_handle_random_result_sc_next_send_pairing_random(void * arg){ 3289f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 3290f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3291f3582630SMatthias Ringwald if (connection == NULL) return; 3292c59d0c92SMatthias Ringwald 329365a9a04eSMatthias Ringwald connection->sm_engine_state = SM_SC_SEND_PAIRING_RANDOM; 329470b44dd4SMatthias Ringwald sm_trigger_run(); 329565a9a04eSMatthias Ringwald } 3296d1a1f6a4SMatthias Ringwald 32976ca80073SMatthias Ringwald static void sm_handle_random_result_sc_next_w2_cmac_for_confirmation(void * arg){ 32986ca80073SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 32996ca80073SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 33006ca80073SMatthias Ringwald if (connection == NULL) return; 33016ca80073SMatthias Ringwald 3302b35a3de2SMatthias Ringwald connection->sm_engine_state = SM_SC_W2_CMAC_FOR_CONFIRMATION; 330370b44dd4SMatthias Ringwald sm_trigger_run(); 3304d1a1f6a4SMatthias Ringwald } 3305f1c1783eSMatthias Ringwald #endif 3306f1c1783eSMatthias Ringwald 3307d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_ph2_random(void * arg){ 3308f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 3309f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3310f3582630SMatthias Ringwald if (connection == NULL) return; 3311f3582630SMatthias Ringwald 3312d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH2_C1_GET_ENC_A; 331370b44dd4SMatthias Ringwald sm_trigger_run(); 3314d1a1f6a4SMatthias Ringwald } 3315d1a1f6a4SMatthias Ringwald 3316d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_ph2_tk(void * arg){ 3317f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 3318f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3319f3582630SMatthias Ringwald if (connection == NULL) return; 3320f3582630SMatthias Ringwald 3321caf15bf3SMatthias Ringwald sm_reset_tk(); 3322caf15bf3SMatthias Ringwald uint32_t tk; 33234b8c611fSMatthias Ringwald if (sm_fixed_passkey_in_display_role == 0xffffffff){ 33243deb3ec6SMatthias Ringwald // map random to 0-999999 without speding much cycles on a modulus operation 3325d1a1f6a4SMatthias Ringwald tk = little_endian_read_32(sm_random_data,0); 33263deb3ec6SMatthias Ringwald tk = tk & 0xfffff; // 1048575 33274ea43905SMatthias Ringwald if (tk >= 999999u){ 33284ea43905SMatthias Ringwald tk = tk - 999999u; 33293deb3ec6SMatthias Ringwald } 3330caf15bf3SMatthias Ringwald } else { 3331caf15bf3SMatthias Ringwald // override with pre-defined passkey 33324b8c611fSMatthias Ringwald tk = sm_fixed_passkey_in_display_role; 3333caf15bf3SMatthias Ringwald } 3334f8fbdce0SMatthias Ringwald big_endian_store_32(setup->sm_tk, 12, tk); 333542134bc6SMatthias Ringwald if (IS_RESPONDER(connection->sm_role)){ 33363deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE; 33373deb3ec6SMatthias Ringwald } else { 3338b41539d5SMatthias Ringwald if (setup->sm_use_secure_connections){ 3339b41539d5SMatthias Ringwald connection->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND; 3340b41539d5SMatthias Ringwald } else { 33413deb3ec6SMatthias Ringwald connection->sm_engine_state = SM_PH1_W4_USER_RESPONSE; 33423deb3ec6SMatthias Ringwald sm_trigger_user_response(connection); 33433deb3ec6SMatthias Ringwald // response_idle == nothing <--> sm_trigger_user_response() did not require response 33443deb3ec6SMatthias Ringwald if (setup->sm_user_response == SM_USER_RESPONSE_IDLE){ 3345f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_random, 16, &sm_handle_random_result_ph2_random, (void *)(uintptr_t) connection->sm_handle); 33463deb3ec6SMatthias Ringwald } 33473deb3ec6SMatthias Ringwald } 3348b41539d5SMatthias Ringwald } 334970b44dd4SMatthias Ringwald sm_trigger_run(); 33503deb3ec6SMatthias Ringwald } 3351d1a1f6a4SMatthias Ringwald 3352d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_ph3_div(void * arg){ 3353f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 3354f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3355f3582630SMatthias Ringwald if (connection == NULL) return; 3356f3582630SMatthias Ringwald 3357d1a1f6a4SMatthias Ringwald // use 16 bit from random value as div 3358d1a1f6a4SMatthias Ringwald setup->sm_local_div = big_endian_read_16(sm_random_data, 0); 3359d1a1f6a4SMatthias Ringwald log_info_hex16("div", setup->sm_local_div); 3360d1a1f6a4SMatthias Ringwald connection->sm_engine_state = SM_PH3_Y_GET_ENC; 336170b44dd4SMatthias Ringwald sm_trigger_run(); 3362d1a1f6a4SMatthias Ringwald } 3363d1a1f6a4SMatthias Ringwald 3364d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_ph3_random(void * arg){ 3365f3582630SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg; 3366f3582630SMatthias Ringwald sm_connection_t * connection = sm_get_connection_for_handle(con_handle); 3367f3582630SMatthias Ringwald if (connection == NULL) return; 3368f3582630SMatthias Ringwald 3369d1a1f6a4SMatthias Ringwald reverse_64(sm_random_data, setup->sm_local_rand); 33703deb3ec6SMatthias Ringwald // no db for encryption size hack: encryption size is stored in lowest nibble of setup->sm_local_rand 33714ea43905SMatthias Ringwald setup->sm_local_rand[7u] = (setup->sm_local_rand[7u] & 0xf0u) + (connection->sm_actual_encryption_key_size - 1u); 33723deb3ec6SMatthias Ringwald // no db for authenticated flag hack: store flag in bit 4 of LSB 33734ea43905SMatthias Ringwald setup->sm_local_rand[7u] = (setup->sm_local_rand[7u] & 0xefu) + (connection->sm_connection_authenticated << 4u); 33748b3ffec5SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 2, &sm_handle_random_result_ph3_div, (void *)(uintptr_t) connection->sm_handle); 33753deb3ec6SMatthias Ringwald } 3376899e6e02SMatthias Ringwald static void sm_validate_er_ir(void){ 3377899e6e02SMatthias Ringwald // warn about default ER/IR 33781979f09cSMatthias Ringwald bool warning = false; 3379899e6e02SMatthias Ringwald if (sm_ir_is_default()){ 33801979f09cSMatthias Ringwald warning = true; 3381899e6e02SMatthias Ringwald log_error("Persistent IR not set with sm_set_ir. Use of private addresses will cause pairing issues"); 3382899e6e02SMatthias Ringwald } 3383899e6e02SMatthias Ringwald if (sm_er_is_default()){ 33841979f09cSMatthias Ringwald warning = true; 3385899e6e02SMatthias Ringwald log_error("Persistent ER not set with sm_set_er. Legacy Pairing LTK is not secure"); 3386899e6e02SMatthias Ringwald } 338721045273SMatthias Ringwald if (warning) { 3388899e6e02SMatthias Ringwald log_error("Please configure btstack_tlv to let BTstack setup ER and IR keys"); 3389899e6e02SMatthias Ringwald } 339021045273SMatthias Ringwald } 3391899e6e02SMatthias Ringwald 3392899e6e02SMatthias Ringwald static void sm_handle_random_result_ir(void *arg){ 33931979f09cSMatthias Ringwald sm_persistent_keys_random_active = false; 33949305033eSMatthias Ringwald if (arg != NULL){ 3395899e6e02SMatthias Ringwald // key generated, store in tlv 33964ea43905SMatthias Ringwald int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','I','R'), sm_persistent_ir, 16u); 3397899e6e02SMatthias Ringwald log_info("Generated IR key. Store in TLV status: %d", status); 3398e0d13a19SMilanka Ringwald UNUSED(status); 3399899e6e02SMatthias Ringwald } 3400899e6e02SMatthias Ringwald log_info_key("IR", sm_persistent_ir); 34018d9b6072SMatthias Ringwald dkg_state = DKG_CALC_IRK; 3402841468bbSMatthias Ringwald 3403841468bbSMatthias Ringwald if (test_use_fixed_local_irk){ 3404841468bbSMatthias Ringwald log_info_key("IRK", sm_persistent_irk); 3405841468bbSMatthias Ringwald dkg_state = DKG_CALC_DHK; 3406841468bbSMatthias Ringwald } 3407841468bbSMatthias Ringwald 340870b44dd4SMatthias Ringwald sm_trigger_run(); 3409899e6e02SMatthias Ringwald } 3410899e6e02SMatthias Ringwald 3411899e6e02SMatthias Ringwald static void sm_handle_random_result_er(void *arg){ 34121979f09cSMatthias Ringwald sm_persistent_keys_random_active = false; 34139305033eSMatthias Ringwald if (arg != 0){ 3414899e6e02SMatthias Ringwald // key generated, store in tlv 34154ea43905SMatthias Ringwald int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u); 3416899e6e02SMatthias Ringwald log_info("Generated ER key. Store in TLV status: %d", status); 3417e0d13a19SMilanka Ringwald UNUSED(status); 3418899e6e02SMatthias Ringwald } 3419899e6e02SMatthias Ringwald log_info_key("ER", sm_persistent_er); 3420899e6e02SMatthias Ringwald 3421899e6e02SMatthias Ringwald // try load ir 34224ea43905SMatthias Ringwald int key_size = sm_tlv_impl->get_tag(sm_tlv_context, BTSTACK_TAG32('S','M','I','R'), sm_persistent_ir, 16u); 3423899e6e02SMatthias Ringwald if (key_size == 16){ 3424899e6e02SMatthias Ringwald // ok, let's continue 3425899e6e02SMatthias Ringwald log_info("IR from TLV"); 3426899e6e02SMatthias Ringwald sm_handle_random_result_ir( NULL ); 3427899e6e02SMatthias Ringwald } else { 3428899e6e02SMatthias Ringwald // invalid, generate new random one 34291979f09cSMatthias Ringwald sm_persistent_keys_random_active = true; 3430899e6e02SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_persistent_ir, 16, &sm_handle_random_result_ir, &sm_persistent_ir); 3431899e6e02SMatthias Ringwald } 3432899e6e02SMatthias Ringwald } 34333deb3ec6SMatthias Ringwald 3434d9a7306aSMatthias Ringwald static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 34353deb3ec6SMatthias Ringwald 3436cbdfe9f7SMatthias Ringwald UNUSED(channel); // ok: there is no channel 3437cbdfe9f7SMatthias Ringwald UNUSED(size); // ok: fixed format HCI events 34389ec2630cSMatthias Ringwald 34393deb3ec6SMatthias Ringwald sm_connection_t * sm_conn; 3440711e6c80SMatthias Ringwald hci_con_handle_t con_handle; 3441fbe050beSMatthias Ringwald uint8_t status; 34423deb3ec6SMatthias Ringwald switch (packet_type) { 34433deb3ec6SMatthias Ringwald 34443deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 34450e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 34463deb3ec6SMatthias Ringwald 34473deb3ec6SMatthias Ringwald case BTSTACK_EVENT_STATE: 34483deb3ec6SMatthias Ringwald // bt stack activated, get started 3449be7cc9a0SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 34503deb3ec6SMatthias Ringwald log_info("HCI Working!"); 3451899e6e02SMatthias Ringwald 3452899e6e02SMatthias Ringwald // setup IR/ER with TLV 3453899e6e02SMatthias Ringwald btstack_tlv_get_instance(&sm_tlv_impl, &sm_tlv_context); 34549305033eSMatthias Ringwald if (sm_tlv_impl != NULL){ 34554ea43905SMatthias Ringwald int key_size = sm_tlv_impl->get_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u); 3456899e6e02SMatthias Ringwald if (key_size == 16){ 3457899e6e02SMatthias Ringwald // ok, let's continue 3458899e6e02SMatthias Ringwald log_info("ER from TLV"); 3459899e6e02SMatthias Ringwald sm_handle_random_result_er( NULL ); 3460899e6e02SMatthias Ringwald } else { 3461899e6e02SMatthias Ringwald // invalid, generate random one 34621979f09cSMatthias Ringwald sm_persistent_keys_random_active = true; 3463899e6e02SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_persistent_er, 16, &sm_handle_random_result_er, &sm_persistent_er); 3464899e6e02SMatthias Ringwald } 3465899e6e02SMatthias Ringwald } else { 3466899e6e02SMatthias Ringwald sm_validate_er_ir(); 34678d9b6072SMatthias Ringwald dkg_state = DKG_CALC_IRK; 3468841468bbSMatthias Ringwald 3469841468bbSMatthias Ringwald if (test_use_fixed_local_irk){ 3470841468bbSMatthias Ringwald log_info_key("IRK", sm_persistent_irk); 3471841468bbSMatthias Ringwald dkg_state = DKG_CALC_DHK; 3472841468bbSMatthias Ringwald } 3473899e6e02SMatthias Ringwald } 34741bf086daSMatthias Ringwald 34751bf086daSMatthias Ringwald // restart random address updates after power cycle 34761bf086daSMatthias Ringwald gap_random_address_set_mode(gap_random_adress_type); 34773deb3ec6SMatthias Ringwald } 34783deb3ec6SMatthias Ringwald break; 34793deb3ec6SMatthias Ringwald 34803deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 34813deb3ec6SMatthias Ringwald switch (packet[2]) { 34823deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 34833deb3ec6SMatthias Ringwald 34843deb3ec6SMatthias Ringwald log_info("sm: connected"); 34853deb3ec6SMatthias Ringwald 34863deb3ec6SMatthias Ringwald if (packet[3]) return; // connection failed 34873deb3ec6SMatthias Ringwald 3488711e6c80SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 3489711e6c80SMatthias Ringwald sm_conn = sm_get_connection_for_handle(con_handle); 34903deb3ec6SMatthias Ringwald if (!sm_conn) break; 34913deb3ec6SMatthias Ringwald 3492711e6c80SMatthias Ringwald sm_conn->sm_handle = con_handle; 34933deb3ec6SMatthias Ringwald sm_conn->sm_role = packet[6]; 34943deb3ec6SMatthias Ringwald sm_conn->sm_peer_addr_type = packet[7]; 349513377825SMatthias Ringwald reverse_bd_addr(&packet[8], sm_conn->sm_peer_address); 34963deb3ec6SMatthias Ringwald 34973deb3ec6SMatthias Ringwald log_info("New sm_conn, role %s", sm_conn->sm_role ? "slave" : "master"); 34983deb3ec6SMatthias Ringwald 34993deb3ec6SMatthias Ringwald // reset security properties 35003deb3ec6SMatthias Ringwald sm_conn->sm_connection_encrypted = 0; 35013deb3ec6SMatthias Ringwald sm_conn->sm_connection_authenticated = 0; 35023deb3ec6SMatthias Ringwald sm_conn->sm_connection_authorization_state = AUTHORIZATION_UNKNOWN; 35033deb3ec6SMatthias Ringwald sm_conn->sm_le_db_index = -1; 35047b001f4eSMatthias Ringwald sm_conn->sm_reencryption_active = false; 35053deb3ec6SMatthias Ringwald 35063deb3ec6SMatthias Ringwald // prepare CSRK lookup (does not involve setup) 35073deb3ec6SMatthias Ringwald sm_conn->sm_irk_lookup_state = IRK_LOOKUP_W4_READY; 35083deb3ec6SMatthias Ringwald 35093deb3ec6SMatthias Ringwald // just connected -> everything else happens in sm_run() 351042134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 35117af5dcd5SMatthias Ringwald // peripheral 35123deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_IDLE; 35133deb3ec6SMatthias Ringwald break; 35143deb3ec6SMatthias Ringwald } else { 35153deb3ec6SMatthias Ringwald // master 35163deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED; 35173deb3ec6SMatthias Ringwald } 35183deb3ec6SMatthias Ringwald break; 35193deb3ec6SMatthias Ringwald 35203deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_LONG_TERM_KEY_REQUEST: 3521711e6c80SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 3522711e6c80SMatthias Ringwald sm_conn = sm_get_connection_for_handle(con_handle); 35233deb3ec6SMatthias Ringwald if (!sm_conn) break; 35243deb3ec6SMatthias Ringwald 35253deb3ec6SMatthias Ringwald log_info("LTK Request: state %u", sm_conn->sm_engine_state); 35263deb3ec6SMatthias Ringwald if (sm_conn->sm_engine_state == SM_RESPONDER_PH2_W4_LTK_REQUEST){ 35273deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH2_CALC_STK; 35283deb3ec6SMatthias Ringwald break; 35293deb3ec6SMatthias Ringwald } 3530c6b7cbd9SMatthias Ringwald if (sm_conn->sm_engine_state == SM_SC_W4_LTK_REQUEST_SC){ 3531778b6aadSMatthias Ringwald // PH2 SEND LTK as we need to exchange keys in PH3 3532778b6aadSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH2_SEND_LTK_REPLY; 3533e53be891SMatthias Ringwald break; 3534e53be891SMatthias Ringwald } 35353deb3ec6SMatthias Ringwald 35363deb3ec6SMatthias Ringwald // store rand and ediv 35379c80e4ccSMatthias Ringwald reverse_64(&packet[5], sm_conn->sm_local_rand); 3538f8fbdce0SMatthias Ringwald sm_conn->sm_local_ediv = little_endian_read_16(packet, 13); 3539549ad5d2SMatthias Ringwald 3540549ad5d2SMatthias Ringwald // For Legacy Pairing (<=> EDIV != 0 || RAND != NULL), we need to recalculated our LTK as a 3541549ad5d2SMatthias Ringwald // potentially stored LTK is from the master 35424ea43905SMatthias Ringwald if ((sm_conn->sm_local_ediv != 0u) || !sm_is_null_random(sm_conn->sm_local_rand)){ 35436c39055aSMatthias Ringwald if (sm_reconstruct_ltk_without_le_device_db_entry){ 354406cd539fSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH0_RECEIVED_LTK_REQUEST; 3545549ad5d2SMatthias Ringwald break; 3546549ad5d2SMatthias Ringwald } 35476c39055aSMatthias Ringwald // additionally check if remote is in LE Device DB if requested 35486c39055aSMatthias Ringwald switch(sm_conn->sm_irk_lookup_state){ 35496c39055aSMatthias Ringwald case IRK_LOOKUP_FAILED: 35506c39055aSMatthias Ringwald log_info("LTK Request: device not in device db"); 35516c39055aSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY; 35526c39055aSMatthias Ringwald break; 35536c39055aSMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 35546c39055aSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH0_RECEIVED_LTK_REQUEST; 35556c39055aSMatthias Ringwald break; 35566c39055aSMatthias Ringwald default: 35576c39055aSMatthias Ringwald // wait for irk look doen 35586c39055aSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH0_RECEIVED_LTK_W4_IRK; 35596c39055aSMatthias Ringwald break; 35606c39055aSMatthias Ringwald } 35616c39055aSMatthias Ringwald break; 35626c39055aSMatthias Ringwald } 3563549ad5d2SMatthias Ringwald 3564549ad5d2SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 356506cd539fSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_RECEIVED_LTK_REQUEST; 3566549ad5d2SMatthias Ringwald #else 3567549ad5d2SMatthias Ringwald log_info("LTK Request: ediv & random are empty, but LE Secure Connections not supported"); 3568549ad5d2SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY; 3569549ad5d2SMatthias Ringwald #endif 35703deb3ec6SMatthias Ringwald break; 3571804d3e67SMatthias Ringwald 35723deb3ec6SMatthias Ringwald default: 35733deb3ec6SMatthias Ringwald break; 35743deb3ec6SMatthias Ringwald } 35753deb3ec6SMatthias Ringwald break; 35763deb3ec6SMatthias Ringwald 35773deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 35783b7fd749SMatthias Ringwald con_handle = hci_event_encryption_change_get_connection_handle(packet); 3579711e6c80SMatthias Ringwald sm_conn = sm_get_connection_for_handle(con_handle); 35803deb3ec6SMatthias Ringwald if (!sm_conn) break; 35813deb3ec6SMatthias Ringwald 35823b7fd749SMatthias Ringwald sm_conn->sm_connection_encrypted = hci_event_encryption_change_get_encryption_enabled(packet); 35833deb3ec6SMatthias Ringwald log_info("Encryption state change: %u, key size %u", sm_conn->sm_connection_encrypted, 35843deb3ec6SMatthias Ringwald sm_conn->sm_actual_encryption_key_size); 35853deb3ec6SMatthias Ringwald log_info("event handler, state %u", sm_conn->sm_engine_state); 358603a9359aSMatthias Ringwald 3587fbe050beSMatthias Ringwald switch (sm_conn->sm_engine_state){ 3588fbe050beSMatthias Ringwald 35895567aa60SMatthias Ringwald case SM_PH4_W4_CONNECTION_ENCRYPTED: 359003a9359aSMatthias Ringwald // encryption change event concludes re-encryption for bonded devices (even if it fails) 3591fbe050beSMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 3592fbe050beSMatthias Ringwald status = ERROR_CODE_SUCCESS; 35938d4eef95SMatthias Ringwald if (sm_conn->sm_role){ 35948d4eef95SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_IDLE; 35958d4eef95SMatthias Ringwald } else { 359603a9359aSMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED; 35978d4eef95SMatthias Ringwald } 3598fbe050beSMatthias Ringwald } else { 3599e28291c1SMatthias Ringwald status = hci_event_encryption_change_get_status(packet); 3600cb6d7eb0SMatthias Ringwald // set state to 'RE-ENCRYPTION FAILED' to allow pairing but prevent other interactions 36013b7fd749SMatthias Ringwald // also, gap_reconnect_security_setup_active will return true 3602cb6d7eb0SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_REENCRYPTION_FAILED; 36033b7fd749SMatthias Ringwald } 3604fbe050beSMatthias Ringwald 3605fbe050beSMatthias Ringwald // emit re-encryption complete 360673102768SMatthias Ringwald sm_reencryption_complete(sm_conn, status); 3607fbe050beSMatthias Ringwald 3608c245ca32SMatthias Ringwald // notify client, if pairing was requested before 3609c245ca32SMatthias Ringwald if (sm_conn->sm_pairing_requested){ 3610c245ca32SMatthias Ringwald sm_conn->sm_pairing_requested = 0; 36110ccf6c9cSMatthias Ringwald sm_pairing_complete(sm_conn, status, 0); 361203a9359aSMatthias Ringwald } 361303a9359aSMatthias Ringwald 36143deb3ec6SMatthias Ringwald sm_done_for_handle(sm_conn->sm_handle); 36153deb3ec6SMatthias Ringwald break; 3616fbe050beSMatthias Ringwald 36173deb3ec6SMatthias Ringwald case SM_PH2_W4_CONNECTION_ENCRYPTED: 3618fbe050beSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) break; 3619dd583d9fSMatthias Ringwald sm_conn->sm_connection_sc = setup->sm_use_secure_connections; 362042134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 36213deb3ec6SMatthias Ringwald // slave 3622bbf8db22SMatthias Ringwald if (setup->sm_use_secure_connections){ 3623bbf8db22SMatthias Ringwald sm_conn->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS; 3624bbf8db22SMatthias Ringwald } else { 3625f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph3_random, (void *)(uintptr_t) sm_conn->sm_handle); 3626bbf8db22SMatthias Ringwald } 36273deb3ec6SMatthias Ringwald } else { 36283deb3ec6SMatthias Ringwald // master 36293deb3ec6SMatthias Ringwald if (sm_key_distribution_all_received(sm_conn)){ 36303deb3ec6SMatthias Ringwald // skip receiving keys as there are none 36313deb3ec6SMatthias Ringwald sm_key_distribution_handle_all_received(sm_conn); 3632f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph3_random, (void *)(uintptr_t) sm_conn->sm_handle); 36333deb3ec6SMatthias Ringwald } else { 36343deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH3_RECEIVE_KEYS; 36353deb3ec6SMatthias Ringwald } 36363deb3ec6SMatthias Ringwald } 36373deb3ec6SMatthias Ringwald break; 36383deb3ec6SMatthias Ringwald default: 36393deb3ec6SMatthias Ringwald break; 36403deb3ec6SMatthias Ringwald } 36413deb3ec6SMatthias Ringwald break; 36423deb3ec6SMatthias Ringwald 36433deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3644711e6c80SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 3645711e6c80SMatthias Ringwald sm_conn = sm_get_connection_for_handle(con_handle); 36463deb3ec6SMatthias Ringwald if (!sm_conn) break; 36473deb3ec6SMatthias Ringwald 36483deb3ec6SMatthias Ringwald log_info("Encryption key refresh complete, key size %u", sm_conn->sm_actual_encryption_key_size); 36493deb3ec6SMatthias Ringwald log_info("event handler, state %u", sm_conn->sm_engine_state); 36503deb3ec6SMatthias Ringwald // continue if part of initial pairing 36513deb3ec6SMatthias Ringwald switch (sm_conn->sm_engine_state){ 36525567aa60SMatthias Ringwald case SM_PH4_W4_CONNECTION_ENCRYPTED: 36535567aa60SMatthias Ringwald if (sm_conn->sm_role){ 36545567aa60SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_IDLE; 36555567aa60SMatthias Ringwald } else { 36563deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED; 36575567aa60SMatthias Ringwald } 36583deb3ec6SMatthias Ringwald sm_done_for_handle(sm_conn->sm_handle); 36593deb3ec6SMatthias Ringwald break; 36603deb3ec6SMatthias Ringwald case SM_PH2_W4_CONNECTION_ENCRYPTED: 366142134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 36623deb3ec6SMatthias Ringwald // slave 3663f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph3_random, (void *)(uintptr_t) sm_conn->sm_handle); 36643deb3ec6SMatthias Ringwald } else { 36653deb3ec6SMatthias Ringwald // master 36663deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH3_RECEIVE_KEYS; 36673deb3ec6SMatthias Ringwald } 36683deb3ec6SMatthias Ringwald break; 36693deb3ec6SMatthias Ringwald default: 36703deb3ec6SMatthias Ringwald break; 36713deb3ec6SMatthias Ringwald } 36723deb3ec6SMatthias Ringwald break; 36733deb3ec6SMatthias Ringwald 36743deb3ec6SMatthias Ringwald 36753deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 3676711e6c80SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 3677711e6c80SMatthias Ringwald sm_done_for_handle(con_handle); 3678711e6c80SMatthias Ringwald sm_conn = sm_get_connection_for_handle(con_handle); 36793deb3ec6SMatthias Ringwald if (!sm_conn) break; 36803deb3ec6SMatthias Ringwald 368103f736b1SMatthias Ringwald // pairing failed, if it was ongoing 36827f3f442dSMatthias Ringwald switch (sm_conn->sm_engine_state){ 36837f3f442dSMatthias Ringwald case SM_GENERAL_IDLE: 36847f3f442dSMatthias Ringwald case SM_INITIATOR_CONNECTED: 36857f3f442dSMatthias Ringwald case SM_RESPONDER_IDLE: 36867f3f442dSMatthias Ringwald break; 36877f3f442dSMatthias Ringwald default: 368868a18fb9SMatthias Ringwald sm_reencryption_complete(sm_conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 36890ccf6c9cSMatthias Ringwald sm_pairing_complete(sm_conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION, 0); 36907f3f442dSMatthias Ringwald break; 369103f736b1SMatthias Ringwald } 3692accbde80SMatthias Ringwald 36933deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_IDLE; 36943deb3ec6SMatthias Ringwald sm_conn->sm_handle = 0; 36953deb3ec6SMatthias Ringwald break; 36963deb3ec6SMatthias Ringwald 36973deb3ec6SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 369833373e40SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){ 36999091c5f5SMatthias Ringwald // set local addr for le device db 370033373e40SMatthias Ringwald bd_addr_t addr; 370133373e40SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], addr); 37020c130b19SMatthias Ringwald le_device_db_set_local_bd_addr(addr); 370333373e40SMatthias Ringwald } 370465b44ffdSMatthias Ringwald break; 370565b44ffdSMatthias Ringwald default: 370665b44ffdSMatthias Ringwald break; 37073deb3ec6SMatthias Ringwald } 370865b44ffdSMatthias Ringwald break; 370965b44ffdSMatthias Ringwald default: 371065b44ffdSMatthias Ringwald break; 37113deb3ec6SMatthias Ringwald } 37123deb3ec6SMatthias Ringwald 37133deb3ec6SMatthias Ringwald sm_run(); 37143deb3ec6SMatthias Ringwald } 37153deb3ec6SMatthias Ringwald 37163deb3ec6SMatthias Ringwald static inline int sm_calc_actual_encryption_key_size(int other){ 37173deb3ec6SMatthias Ringwald if (other < sm_min_encryption_key_size) return 0; 37183deb3ec6SMatthias Ringwald if (other < sm_max_encryption_key_size) return other; 37193deb3ec6SMatthias Ringwald return sm_max_encryption_key_size; 37203deb3ec6SMatthias Ringwald } 37213deb3ec6SMatthias Ringwald 3722945888f5SMatthias Ringwald 372331c09488SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 3724945888f5SMatthias Ringwald static int sm_just_works_or_numeric_comparison(stk_generation_method_t method){ 3725945888f5SMatthias Ringwald switch (method){ 3726945888f5SMatthias Ringwald case JUST_WORKS: 372747fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 3728945888f5SMatthias Ringwald return 1; 3729945888f5SMatthias Ringwald default: 3730945888f5SMatthias Ringwald return 0; 3731945888f5SMatthias Ringwald } 3732945888f5SMatthias Ringwald } 373307036a04SMatthias Ringwald // responder 3734945888f5SMatthias Ringwald 3735688a08f9SMatthias Ringwald static int sm_passkey_used(stk_generation_method_t method){ 3736688a08f9SMatthias Ringwald switch (method){ 3737688a08f9SMatthias Ringwald case PK_RESP_INPUT: 3738688a08f9SMatthias Ringwald return 1; 3739688a08f9SMatthias Ringwald default: 3740688a08f9SMatthias Ringwald return 0; 3741688a08f9SMatthias Ringwald } 3742688a08f9SMatthias Ringwald } 374340c5d850SMatthias Ringwald 374440c5d850SMatthias Ringwald static int sm_passkey_entry(stk_generation_method_t method){ 374540c5d850SMatthias Ringwald switch (method){ 374640c5d850SMatthias Ringwald case PK_RESP_INPUT: 374740c5d850SMatthias Ringwald case PK_INIT_INPUT: 374847fb4255SMatthias Ringwald case PK_BOTH_INPUT: 374940c5d850SMatthias Ringwald return 1; 375040c5d850SMatthias Ringwald default: 375140c5d850SMatthias Ringwald return 0; 375240c5d850SMatthias Ringwald } 375340c5d850SMatthias Ringwald } 375440c5d850SMatthias Ringwald 375531c09488SMatthias Ringwald #endif 3756688a08f9SMatthias Ringwald 37573deb3ec6SMatthias Ringwald /** 37583deb3ec6SMatthias Ringwald * @return ok 37593deb3ec6SMatthias Ringwald */ 37603deb3ec6SMatthias Ringwald static int sm_validate_stk_generation_method(void){ 37613deb3ec6SMatthias Ringwald // check if STK generation method is acceptable by client 37623deb3ec6SMatthias Ringwald switch (setup->sm_stk_generation_method){ 37633deb3ec6SMatthias Ringwald case JUST_WORKS: 37644ea43905SMatthias Ringwald return (sm_accepted_stk_generation_methods & SM_STK_GENERATION_METHOD_JUST_WORKS) != 0u; 37653deb3ec6SMatthias Ringwald case PK_RESP_INPUT: 37663deb3ec6SMatthias Ringwald case PK_INIT_INPUT: 376747fb4255SMatthias Ringwald case PK_BOTH_INPUT: 37684ea43905SMatthias Ringwald return (sm_accepted_stk_generation_methods & SM_STK_GENERATION_METHOD_PASSKEY) != 0u; 37693deb3ec6SMatthias Ringwald case OOB: 37704ea43905SMatthias Ringwald return (sm_accepted_stk_generation_methods & SM_STK_GENERATION_METHOD_OOB) != 0u; 377147fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 37724ea43905SMatthias Ringwald return (sm_accepted_stk_generation_methods & SM_STK_GENERATION_METHOD_NUMERIC_COMPARISON) != 0u; 37733deb3ec6SMatthias Ringwald default: 37743deb3ec6SMatthias Ringwald return 0; 37753deb3ec6SMatthias Ringwald } 37763deb3ec6SMatthias Ringwald } 37773deb3ec6SMatthias Ringwald 37788334d3d8SMatthias Ringwald static void sm_pdu_handler(uint8_t packet_type, hci_con_handle_t con_handle, uint8_t *packet, uint16_t size){ 37798334d3d8SMatthias Ringwald 37804c1d1092SMatthias Ringwald // size of complete sm_pdu used to validate input 37814c1d1092SMatthias Ringwald static const uint8_t sm_pdu_size[] = { 37824c1d1092SMatthias Ringwald 0, // 0x00 invalid opcode 37834c1d1092SMatthias Ringwald 7, // 0x01 pairing request 37844c1d1092SMatthias Ringwald 7, // 0x02 pairing response 37854c1d1092SMatthias Ringwald 17, // 0x03 pairing confirm 37864c1d1092SMatthias Ringwald 17, // 0x04 pairing random 37874c1d1092SMatthias Ringwald 2, // 0x05 pairing failed 37884c1d1092SMatthias Ringwald 17, // 0x06 encryption information 37897a2e6387SMatthias Ringwald 11, // 0x07 master identification 37904c1d1092SMatthias Ringwald 17, // 0x08 identification information 37914c1d1092SMatthias Ringwald 8, // 0x09 identify address information 37924c1d1092SMatthias Ringwald 17, // 0x0a signing information 37934c1d1092SMatthias Ringwald 2, // 0x0b security request 37944c1d1092SMatthias Ringwald 65, // 0x0c pairing public key 37954c1d1092SMatthias Ringwald 17, // 0x0d pairing dhk check 37964c1d1092SMatthias Ringwald 2, // 0x0e keypress notification 37974c1d1092SMatthias Ringwald }; 37983deb3ec6SMatthias Ringwald 3799c1ab6cc1SMatthias Ringwald if ((packet_type == HCI_EVENT_PACKET) && (packet[0] == L2CAP_EVENT_CAN_SEND_NOW)){ 3800b170b20fSMatthias Ringwald sm_run(); 3801b170b20fSMatthias Ringwald } 3802b170b20fSMatthias Ringwald 38033deb3ec6SMatthias Ringwald if (packet_type != SM_DATA_PACKET) return; 38044ea43905SMatthias Ringwald if (size == 0u) return; 38054c1d1092SMatthias Ringwald 38064c1d1092SMatthias Ringwald uint8_t sm_pdu_code = packet[0]; 38074c1d1092SMatthias Ringwald 38084c1d1092SMatthias Ringwald // validate pdu size 38094c1d1092SMatthias Ringwald if (sm_pdu_code >= sizeof(sm_pdu_size)) return; 38107a2e6387SMatthias Ringwald if (sm_pdu_size[sm_pdu_code] != size) return; 38113deb3ec6SMatthias Ringwald 3812711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 38133deb3ec6SMatthias Ringwald if (!sm_conn) return; 38143deb3ec6SMatthias Ringwald 38154c1d1092SMatthias Ringwald if (sm_pdu_code == SM_CODE_PAIRING_FAILED){ 381668a18fb9SMatthias Ringwald sm_reencryption_complete(sm_conn, ERROR_CODE_AUTHENTICATION_FAILURE); 38170ccf6c9cSMatthias Ringwald sm_pairing_complete(sm_conn, ERROR_CODE_AUTHENTICATION_FAILURE, packet[1]); 3818accbde80SMatthias Ringwald sm_done_for_handle(con_handle); 38193deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = sm_conn->sm_role ? SM_RESPONDER_IDLE : SM_INITIATOR_CONNECTED; 38203deb3ec6SMatthias Ringwald return; 38213deb3ec6SMatthias Ringwald } 38223deb3ec6SMatthias Ringwald 38234c1d1092SMatthias Ringwald log_debug("sm_pdu_handler: state %u, pdu 0x%02x", sm_conn->sm_engine_state, sm_pdu_code); 38243deb3ec6SMatthias Ringwald 38253deb3ec6SMatthias Ringwald int err; 382642134bc6SMatthias Ringwald UNUSED(err); 38273deb3ec6SMatthias Ringwald 38284c1d1092SMatthias Ringwald if (sm_pdu_code == SM_CODE_KEYPRESS_NOTIFICATION){ 38293d7fe1e9SMatthias Ringwald uint8_t buffer[5]; 38303d7fe1e9SMatthias Ringwald buffer[0] = SM_EVENT_KEYPRESS_NOTIFICATION; 38313d7fe1e9SMatthias Ringwald buffer[1] = 3; 38323d7fe1e9SMatthias Ringwald little_endian_store_16(buffer, 2, con_handle); 38333d7fe1e9SMatthias Ringwald buffer[4] = packet[1]; 38343d7fe1e9SMatthias Ringwald sm_dispatch_event(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 38353d7fe1e9SMatthias Ringwald return; 38363d7fe1e9SMatthias Ringwald } 38373d7fe1e9SMatthias Ringwald 3838a6ca6916SDavid Lechner #ifdef ENABLE_LE_CENTRAL 3839212d735eSMatthias Ringwald int have_ltk; 3840212d735eSMatthias Ringwald uint8_t ltk[16]; 3841a6ca6916SDavid Lechner #endif 3842212d735eSMatthias Ringwald 38433deb3ec6SMatthias Ringwald switch (sm_conn->sm_engine_state){ 38443deb3ec6SMatthias Ringwald 3845c8d0ff33SMatthias Ringwald // a sm timeout requires a new physical connection 38463deb3ec6SMatthias Ringwald case SM_GENERAL_TIMEOUT: 38473deb3ec6SMatthias Ringwald return; 38483deb3ec6SMatthias Ringwald 384942134bc6SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 385042134bc6SMatthias Ringwald 38513deb3ec6SMatthias Ringwald // Initiator 38523deb3ec6SMatthias Ringwald case SM_INITIATOR_CONNECTED: 38534c1d1092SMatthias Ringwald if ((sm_pdu_code != SM_CODE_SECURITY_REQUEST) || (sm_conn->sm_role)){ 38543deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 38553deb3ec6SMatthias Ringwald break; 38563deb3ec6SMatthias Ringwald } 385734c39fbdSMatthias Ringwald 385834c39fbdSMatthias Ringwald // IRK complete? 385934c39fbdSMatthias Ringwald switch (sm_conn->sm_irk_lookup_state){ 386034c39fbdSMatthias Ringwald case IRK_LOOKUP_FAILED: 3861dc8ca372SMatthias Ringwald // start pairing 38623deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 38633deb3ec6SMatthias Ringwald break; 3864e061bbd4SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 3865e061bbd4SMatthias Ringwald le_device_db_encryption_get(sm_conn->sm_le_db_index, NULL, NULL, ltk, NULL, NULL, NULL, NULL); 3866e061bbd4SMatthias Ringwald have_ltk = !sm_is_null_key(ltk); 3867e061bbd4SMatthias Ringwald log_info("central: security request - have_ltk %u", have_ltk); 3868e061bbd4SMatthias Ringwald if (have_ltk){ 3869dc8ca372SMatthias Ringwald // start re-encrypt 38705567aa60SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH4_HAS_LTK; 3871e061bbd4SMatthias Ringwald } else { 3872dc8ca372SMatthias Ringwald // start pairing 3873e061bbd4SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 3874e061bbd4SMatthias Ringwald } 3875e061bbd4SMatthias Ringwald break; 387634c39fbdSMatthias Ringwald default: 38773deb3ec6SMatthias Ringwald // otherwise, store security request 38783deb3ec6SMatthias Ringwald sm_conn->sm_security_request_received = 1; 38793deb3ec6SMatthias Ringwald break; 3880dc8ca372SMatthias Ringwald } 3881dc8ca372SMatthias Ringwald break; 38823deb3ec6SMatthias Ringwald 38833deb3ec6SMatthias Ringwald case SM_INITIATOR_PH1_W4_PAIRING_RESPONSE: 3884aacfafc3SMatthias Ringwald // Core 5, Vol 3, Part H, 2.4.6: 3885aacfafc3SMatthias Ringwald // "The master shall ignore the slave’s Security Request if the master has sent a Pairing Request 3886aacfafc3SMatthias Ringwald // without receiving a Pairing Response from the slave or if the master has initiated encryption mode setup." 3887aacfafc3SMatthias Ringwald if (sm_pdu_code == SM_CODE_SECURITY_REQUEST){ 3888aacfafc3SMatthias Ringwald log_info("Ignoring Security Request"); 3889aacfafc3SMatthias Ringwald break; 3890aacfafc3SMatthias Ringwald } 3891aacfafc3SMatthias Ringwald 3892aacfafc3SMatthias Ringwald // all other pdus are incorrect 38934c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_RESPONSE){ 38943deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 38953deb3ec6SMatthias Ringwald break; 38963deb3ec6SMatthias Ringwald } 38970af429c6SMatthias Ringwald 38983deb3ec6SMatthias Ringwald // store pairing request 38996535961aSMatthias Ringwald (void)memcpy(&setup->sm_s_pres, packet, 39006535961aSMatthias Ringwald sizeof(sm_pairing_packet_t)); 39013deb3ec6SMatthias Ringwald err = sm_stk_generation_init(sm_conn); 39020af429c6SMatthias Ringwald 39030af429c6SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 39040af429c6SMatthias Ringwald if (0 < test_pairing_failure && test_pairing_failure < SM_REASON_DHKEY_CHECK_FAILED){ 39050af429c6SMatthias Ringwald log_info("testing_support: abort with pairing failure %u", test_pairing_failure); 39060af429c6SMatthias Ringwald err = test_pairing_failure; 39070af429c6SMatthias Ringwald } 39080af429c6SMatthias Ringwald #endif 39090af429c6SMatthias Ringwald 39109305033eSMatthias Ringwald if (err != 0){ 39113deb3ec6SMatthias Ringwald setup->sm_pairing_failed_reason = err; 39123deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; 39133deb3ec6SMatthias Ringwald break; 39143deb3ec6SMatthias Ringwald } 3915b41539d5SMatthias Ringwald 3916b41539d5SMatthias Ringwald // generate random number first, if we need to show passkey 3917b41539d5SMatthias Ringwald if (setup->sm_stk_generation_method == PK_RESP_INPUT){ 3918f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph2_tk, (void *)(uintptr_t) sm_conn->sm_handle); 3919b41539d5SMatthias Ringwald break; 3920b41539d5SMatthias Ringwald } 3921b41539d5SMatthias Ringwald 3922136d331aSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 3923136d331aSMatthias Ringwald if (setup->sm_use_secure_connections){ 39248cba5ca3SMatthias Ringwald // SC Numeric Comparison will trigger user response after public keys & nonces have been exchanged 39258cba5ca3SMatthias Ringwald if (setup->sm_stk_generation_method == JUST_WORKS){ 3926136d331aSMatthias Ringwald sm_conn->sm_engine_state = SM_PH1_W4_USER_RESPONSE; 3927136d331aSMatthias Ringwald sm_trigger_user_response(sm_conn); 3928136d331aSMatthias Ringwald if (setup->sm_user_response == SM_USER_RESPONSE_IDLE){ 3929c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND; 3930136d331aSMatthias Ringwald } 39318cba5ca3SMatthias Ringwald } else { 3932c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND; 39338cba5ca3SMatthias Ringwald } 3934136d331aSMatthias Ringwald break; 3935136d331aSMatthias Ringwald } 3936136d331aSMatthias Ringwald #endif 39373deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH1_W4_USER_RESPONSE; 39383deb3ec6SMatthias Ringwald sm_trigger_user_response(sm_conn); 39393deb3ec6SMatthias Ringwald // response_idle == nothing <--> sm_trigger_user_response() did not require response 39403deb3ec6SMatthias Ringwald if (setup->sm_user_response == SM_USER_RESPONSE_IDLE){ 3941f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_random, 16, &sm_handle_random_result_ph2_random, (void *)(uintptr_t) sm_conn->sm_handle); 39423deb3ec6SMatthias Ringwald } 39433deb3ec6SMatthias Ringwald break; 39443deb3ec6SMatthias Ringwald 39453deb3ec6SMatthias Ringwald case SM_INITIATOR_PH2_W4_PAIRING_CONFIRM: 39464c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_CONFIRM){ 39473deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 39483deb3ec6SMatthias Ringwald break; 39493deb3ec6SMatthias Ringwald } 39503deb3ec6SMatthias Ringwald 39513deb3ec6SMatthias Ringwald // store s_confirm 39529c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_confirm); 3953192365feSMatthias Ringwald 3954aa9b34e5SMatthias Ringwald // abort if s_confirm matches m_confirm 3955aa9b34e5SMatthias Ringwald if (memcmp(setup->sm_local_confirm, setup->sm_peer_confirm, 16) == 0){ 3956aa9b34e5SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 3957aa9b34e5SMatthias Ringwald break; 3958aa9b34e5SMatthias Ringwald } 3959aa9b34e5SMatthias Ringwald 3960192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3961192365feSMatthias Ringwald if (test_pairing_failure == SM_REASON_CONFIRM_VALUE_FAILED){ 3962192365feSMatthias Ringwald log_info("testing_support: reset confirm value"); 3963192365feSMatthias Ringwald memset(setup->sm_peer_confirm, 0, 16); 3964192365feSMatthias Ringwald } 3965192365feSMatthias Ringwald #endif 39663deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH2_SEND_PAIRING_RANDOM; 39673deb3ec6SMatthias Ringwald break; 39683deb3ec6SMatthias Ringwald 39693deb3ec6SMatthias Ringwald case SM_INITIATOR_PH2_W4_PAIRING_RANDOM: 39704c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_RANDOM){ 39713deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 39723deb3ec6SMatthias Ringwald break;; 39733deb3ec6SMatthias Ringwald } 39743deb3ec6SMatthias Ringwald 39753deb3ec6SMatthias Ringwald // received random value 39769c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_random); 39773deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH2_C1_GET_ENC_C; 39783deb3ec6SMatthias Ringwald break; 397942134bc6SMatthias Ringwald #endif 39803deb3ec6SMatthias Ringwald 398142134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 39823deb3ec6SMatthias Ringwald // Responder 39833deb3ec6SMatthias Ringwald case SM_RESPONDER_IDLE: 39843deb3ec6SMatthias Ringwald case SM_RESPONDER_SEND_SECURITY_REQUEST: 39853deb3ec6SMatthias Ringwald case SM_RESPONDER_PH1_W4_PAIRING_REQUEST: 39864c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_REQUEST){ 39873deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 39883deb3ec6SMatthias Ringwald break;; 39893deb3ec6SMatthias Ringwald } 39903deb3ec6SMatthias Ringwald 39913deb3ec6SMatthias Ringwald // store pairing request 3992212d735eSMatthias Ringwald (void)memcpy(&sm_conn->sm_m_preq, packet, sizeof(sm_pairing_packet_t)); 3993212d735eSMatthias Ringwald 3994212d735eSMatthias Ringwald // check if IRK completed 3995212d735eSMatthias Ringwald switch (sm_conn->sm_irk_lookup_state){ 3996212d735eSMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 3997212d735eSMatthias Ringwald case IRK_LOOKUP_FAILED: 39983deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED; 39993deb3ec6SMatthias Ringwald break; 4000212d735eSMatthias Ringwald default: 4001212d735eSMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED_W4_IRK; 4002212d735eSMatthias Ringwald break; 4003212d735eSMatthias Ringwald } 4004212d735eSMatthias Ringwald break; 400542134bc6SMatthias Ringwald #endif 40063deb3ec6SMatthias Ringwald 400727c32905SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4008c6b7cbd9SMatthias Ringwald case SM_SC_W4_PUBLIC_KEY_COMMAND: 40094c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_PUBLIC_KEY){ 401027c32905SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 401127c32905SMatthias Ringwald break; 401227c32905SMatthias Ringwald } 4013bccf5e67SMatthias Ringwald 4014e53be891SMatthias Ringwald // store public key for DH Key calculation 4015fc5bff5fSMatthias Ringwald reverse_256(&packet[01], &setup->sm_peer_q[0]); 4016fc5bff5fSMatthias Ringwald reverse_256(&packet[33], &setup->sm_peer_q[32]); 4017bccf5e67SMatthias Ringwald 4018d1a1f6a4SMatthias Ringwald // validate public key 4019d1a1f6a4SMatthias Ringwald err = btstack_crypto_ecc_p256_validate_public_key(setup->sm_peer_q); 40209305033eSMatthias Ringwald if (err != 0){ 4021bccf5e67SMatthias Ringwald log_error("sm: peer public key invalid %x", err); 4022349d0adbSMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_DHKEY_CHECK_FAILED); 4023bccf5e67SMatthias Ringwald break; 4024bccf5e67SMatthias Ringwald } 4025891bb64aSMatthias Ringwald 4026d1a1f6a4SMatthias Ringwald // start calculating dhkey 4027f3582630SMatthias Ringwald btstack_crypto_ecc_p256_calculate_dhkey(&sm_crypto_ecc_p256_request, setup->sm_peer_q, setup->sm_dhkey, sm_sc_dhkey_calculated, (void*)(uintptr_t) sm_conn->sm_handle); 40283cf37b8cSMatthias Ringwald 402965a9a04eSMatthias Ringwald 403065a9a04eSMatthias Ringwald log_info("public key received, generation method %u", setup->sm_stk_generation_method); 403142134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 4032136d331aSMatthias Ringwald // responder 4033c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND; 4034136d331aSMatthias Ringwald } else { 4035136d331aSMatthias Ringwald // initiator 4036a1e31e9cSMatthias Ringwald // stk generation method 4037a1e31e9cSMatthias Ringwald // passkey entry: notify app to show passkey or to request passkey 4038a1e31e9cSMatthias Ringwald switch (setup->sm_stk_generation_method){ 4039a1e31e9cSMatthias Ringwald case JUST_WORKS: 404047fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 4041c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_CONFIRMATION; 4042a1e31e9cSMatthias Ringwald break; 4043a1e31e9cSMatthias Ringwald case PK_RESP_INPUT: 404407036a04SMatthias Ringwald sm_sc_start_calculating_local_confirm(sm_conn); 404507036a04SMatthias Ringwald break; 404607036a04SMatthias Ringwald case PK_INIT_INPUT: 404747fb4255SMatthias Ringwald case PK_BOTH_INPUT: 404807036a04SMatthias Ringwald if (setup->sm_user_response != SM_USER_RESPONSE_PASSKEY){ 404907036a04SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_USER_RESPONSE; 405007036a04SMatthias Ringwald break; 405107036a04SMatthias Ringwald } 4052b35a3de2SMatthias Ringwald sm_sc_start_calculating_local_confirm(sm_conn); 4053a1e31e9cSMatthias Ringwald break; 4054a1e31e9cSMatthias Ringwald case OOB: 4055d1a1f6a4SMatthias Ringwald // generate Nx 40564acf7b7bSMatthias Ringwald log_info("Generate Na"); 40576ca80073SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_nonce, 16, &sm_handle_random_result_sc_next_send_pairing_random, (void*)(uintptr_t) sm_conn->sm_handle); 4058a1e31e9cSMatthias Ringwald break; 40597bbeb3adSMilanka Ringwald default: 40607bbeb3adSMilanka Ringwald btstack_assert(false); 40617bbeb3adSMilanka Ringwald break; 4062a1e31e9cSMatthias Ringwald } 4063136d331aSMatthias Ringwald } 406427c32905SMatthias Ringwald break; 4065e53be891SMatthias Ringwald 4066c6b7cbd9SMatthias Ringwald case SM_SC_W4_CONFIRMATION: 40674c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_CONFIRM){ 406845a61d50SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 406945a61d50SMatthias Ringwald break; 407045a61d50SMatthias Ringwald } 407145a61d50SMatthias Ringwald // received confirm value 407245a61d50SMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_confirm); 407345a61d50SMatthias Ringwald 4074192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 4075192365feSMatthias Ringwald if (test_pairing_failure == SM_REASON_CONFIRM_VALUE_FAILED){ 4076192365feSMatthias Ringwald log_info("testing_support: reset confirm value"); 4077192365feSMatthias Ringwald memset(setup->sm_peer_confirm, 0, 16); 4078192365feSMatthias Ringwald } 4079192365feSMatthias Ringwald #endif 408042134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 408145a61d50SMatthias Ringwald // responder 408207036a04SMatthias Ringwald if (sm_passkey_used(setup->sm_stk_generation_method)){ 408307036a04SMatthias Ringwald if (setup->sm_user_response != SM_USER_RESPONSE_PASSKEY){ 408407036a04SMatthias Ringwald // still waiting for passkey 408507036a04SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W4_USER_RESPONSE; 408607036a04SMatthias Ringwald break; 408707036a04SMatthias Ringwald } 408807036a04SMatthias Ringwald } 4089b35a3de2SMatthias Ringwald sm_sc_start_calculating_local_confirm(sm_conn); 409045a61d50SMatthias Ringwald } else { 409145a61d50SMatthias Ringwald // initiator 4092945888f5SMatthias Ringwald if (sm_just_works_or_numeric_comparison(setup->sm_stk_generation_method)){ 40936ca80073SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_nonce, 16, &sm_handle_random_result_sc_next_send_pairing_random, (void*)(uintptr_t) sm_conn->sm_handle); 4094f1c1783eSMatthias Ringwald } else { 4095c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PAIRING_RANDOM; 409645a61d50SMatthias Ringwald } 4097f1c1783eSMatthias Ringwald } 409845a61d50SMatthias Ringwald break; 409945a61d50SMatthias Ringwald 4100c6b7cbd9SMatthias Ringwald case SM_SC_W4_PAIRING_RANDOM: 41014c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_RANDOM){ 4102e53be891SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 4103136d331aSMatthias Ringwald break; 4104e53be891SMatthias Ringwald } 4105e53be891SMatthias Ringwald 4106e53be891SMatthias Ringwald // received random value 4107e53be891SMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_nonce); 4108e53be891SMatthias Ringwald 41095a293e6eSMatthias Ringwald // validate confirm value if Cb = f4(Pkb, Pka, Nb, z) 4110ae451ec5SMatthias Ringwald // only check for JUST WORK/NC in initiator role OR passkey entry 4111d686b2d0SMatthias Ringwald log_info("SM_SC_W4_PAIRING_RANDOM, responder: %u, just works: %u, passkey used %u, passkey entry %u", 4112d686b2d0SMatthias Ringwald IS_RESPONDER(sm_conn->sm_role), sm_just_works_or_numeric_comparison(setup->sm_stk_generation_method), 4113d686b2d0SMatthias Ringwald sm_passkey_used(setup->sm_stk_generation_method), sm_passkey_entry(setup->sm_stk_generation_method)); 411465a9a04eSMatthias Ringwald if ( (!IS_RESPONDER(sm_conn->sm_role) && sm_just_works_or_numeric_comparison(setup->sm_stk_generation_method)) 4115d686b2d0SMatthias Ringwald || (sm_passkey_entry(setup->sm_stk_generation_method)) ) { 4116688a08f9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CMAC_FOR_CHECK_CONFIRMATION; 4117ae451ec5SMatthias Ringwald break; 41185a293e6eSMatthias Ringwald } 41196f52a196SMatthias Ringwald 41204acf7b7bSMatthias Ringwald // OOB 41214acf7b7bSMatthias Ringwald if (setup->sm_stk_generation_method == OOB){ 41224acf7b7bSMatthias Ringwald 41234acf7b7bSMatthias Ringwald // setup local random, set to zero if remote did not receive our data 41244acf7b7bSMatthias Ringwald log_info("Received nonce, setup local random ra/rb for dhkey check"); 41254acf7b7bSMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 41264ea43905SMatthias Ringwald if (sm_pairing_packet_get_oob_data_flag(setup->sm_m_preq) == 0u){ 41274acf7b7bSMatthias Ringwald log_info("Reset rb as A does not have OOB data"); 41284acf7b7bSMatthias Ringwald memset(setup->sm_rb, 0, 16); 41294acf7b7bSMatthias Ringwald } else { 41306535961aSMatthias Ringwald (void)memcpy(setup->sm_rb, sm_sc_oob_random, 16); 41314acf7b7bSMatthias Ringwald log_info("Use stored rb"); 41324acf7b7bSMatthias Ringwald log_info_hexdump(setup->sm_rb, 16); 41334acf7b7bSMatthias Ringwald } 41344acf7b7bSMatthias Ringwald } else { 41354ea43905SMatthias Ringwald if (sm_pairing_packet_get_oob_data_flag(setup->sm_s_pres) == 0u){ 41364acf7b7bSMatthias Ringwald log_info("Reset ra as B does not have OOB data"); 41374acf7b7bSMatthias Ringwald memset(setup->sm_ra, 0, 16); 41384acf7b7bSMatthias Ringwald } else { 41396535961aSMatthias Ringwald (void)memcpy(setup->sm_ra, sm_sc_oob_random, 16); 41404acf7b7bSMatthias Ringwald log_info("Use stored ra"); 41414acf7b7bSMatthias Ringwald log_info_hexdump(setup->sm_ra, 16); 41424acf7b7bSMatthias Ringwald } 41434acf7b7bSMatthias Ringwald } 41444acf7b7bSMatthias Ringwald 4145a680ba6bSMatthias Ringwald // validate confirm value if Cb = f4(PKb, Pkb, rb, 0) for OOB if data received 41464acf7b7bSMatthias Ringwald if (setup->sm_have_oob_data){ 4147a680ba6bSMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CMAC_FOR_CHECK_CONFIRMATION; 4148a680ba6bSMatthias Ringwald break; 4149a680ba6bSMatthias Ringwald } 41504acf7b7bSMatthias Ringwald } 4151a680ba6bSMatthias Ringwald 4152a680ba6bSMatthias Ringwald // TODO: we only get here for Responder role with JW/NC 4153688a08f9SMatthias Ringwald sm_sc_state_after_receiving_random(sm_conn); 4154e53be891SMatthias Ringwald break; 4155e53be891SMatthias Ringwald 4156901c000fSMatthias Ringwald case SM_SC_W2_CALCULATE_G2: 4157901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_G2: 41583cf37b8cSMatthias Ringwald case SM_SC_W4_CALCULATE_DHKEY: 4159901c000fSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_SALT: 4160901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_SALT: 4161901c000fSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_MACKEY: 4162901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_MACKEY: 4163901c000fSMatthias Ringwald case SM_SC_W2_CALCULATE_F5_LTK: 4164901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F5_LTK: 4165901c000fSMatthias Ringwald case SM_SC_W2_CALCULATE_F6_FOR_DHKEY_CHECK: 4166c6b7cbd9SMatthias Ringwald case SM_SC_W4_DHKEY_CHECK_COMMAND: 4167901c000fSMatthias Ringwald case SM_SC_W4_CALCULATE_F6_FOR_DHKEY_CHECK: 4168d08147dfSMatthias Ringwald case SM_SC_W4_USER_RESPONSE: 41694c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_DHKEY_CHECK){ 4170e53be891SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 4171e53be891SMatthias Ringwald break; 4172e53be891SMatthias Ringwald } 4173e53be891SMatthias Ringwald // store DHKey Check 4174901c000fSMatthias Ringwald setup->sm_state_vars |= SM_STATE_VAR_DHKEY_COMMAND_RECEIVED; 4175e53be891SMatthias Ringwald reverse_128(&packet[01], setup->sm_peer_dhkey_check); 4176446a8c36SMatthias Ringwald 4177901c000fSMatthias Ringwald // have we been only waiting for dhkey check command? 4178901c000fSMatthias Ringwald if (sm_conn->sm_engine_state == SM_SC_W4_DHKEY_CHECK_COMMAND){ 4179019005a0SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F6_TO_VERIFY_DHKEY_CHECK; 4180bd57ffebSMatthias Ringwald } 4181bd57ffebSMatthias Ringwald break; 418227c32905SMatthias Ringwald #endif 418327c32905SMatthias Ringwald 418442134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 41853deb3ec6SMatthias Ringwald case SM_RESPONDER_PH1_W4_PAIRING_CONFIRM: 41864c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_CONFIRM){ 41873deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 418827c32905SMatthias Ringwald break; 41893deb3ec6SMatthias Ringwald } 41903deb3ec6SMatthias Ringwald 41913deb3ec6SMatthias Ringwald // received confirm value 41929c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_confirm); 41933deb3ec6SMatthias Ringwald 4194192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 4195192365feSMatthias Ringwald if (test_pairing_failure == SM_REASON_CONFIRM_VALUE_FAILED){ 4196192365feSMatthias Ringwald log_info("testing_support: reset confirm value"); 4197192365feSMatthias Ringwald memset(setup->sm_peer_confirm, 0, 16); 4198192365feSMatthias Ringwald } 4199192365feSMatthias Ringwald #endif 42003deb3ec6SMatthias Ringwald // notify client to hide shown passkey 42013deb3ec6SMatthias Ringwald if (setup->sm_stk_generation_method == PK_INIT_INPUT){ 42025611a760SMatthias Ringwald sm_notify_client_base(SM_EVENT_PASSKEY_DISPLAY_CANCEL, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address); 42033deb3ec6SMatthias Ringwald } 42043deb3ec6SMatthias Ringwald 42053deb3ec6SMatthias Ringwald // handle user cancel pairing? 42063deb3ec6SMatthias Ringwald if (setup->sm_user_response == SM_USER_RESPONSE_DECLINE){ 420716a1a3e5SMatthias Ringwald setup->sm_pairing_failed_reason = SM_REASON_PASSKEY_ENTRY_FAILED; 42083deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED; 42093deb3ec6SMatthias Ringwald break; 42103deb3ec6SMatthias Ringwald } 42113deb3ec6SMatthias Ringwald 42123deb3ec6SMatthias Ringwald // wait for user action? 42133deb3ec6SMatthias Ringwald if (setup->sm_user_response == SM_USER_RESPONSE_PENDING){ 42143deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH1_W4_USER_RESPONSE; 42153deb3ec6SMatthias Ringwald break; 42163deb3ec6SMatthias Ringwald } 42173deb3ec6SMatthias Ringwald 42183deb3ec6SMatthias Ringwald // calculate and send local_confirm 4219f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_random, 16, &sm_handle_random_result_ph2_random, (void *)(uintptr_t) sm_conn->sm_handle); 42203deb3ec6SMatthias Ringwald break; 42213deb3ec6SMatthias Ringwald 42223deb3ec6SMatthias Ringwald case SM_RESPONDER_PH2_W4_PAIRING_RANDOM: 42234c1d1092SMatthias Ringwald if (sm_pdu_code != SM_CODE_PAIRING_RANDOM){ 42243deb3ec6SMatthias Ringwald sm_pdu_received_in_wrong_state(sm_conn); 42253deb3ec6SMatthias Ringwald break;; 42263deb3ec6SMatthias Ringwald } 42273deb3ec6SMatthias Ringwald 42283deb3ec6SMatthias Ringwald // received random value 42299c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_random); 42303deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_PH2_C1_GET_ENC_C; 42313deb3ec6SMatthias Ringwald break; 423242134bc6SMatthias Ringwald #endif 42333deb3ec6SMatthias Ringwald 42343deb3ec6SMatthias Ringwald case SM_PH3_RECEIVE_KEYS: 42354c1d1092SMatthias Ringwald switch(sm_pdu_code){ 42363deb3ec6SMatthias Ringwald case SM_CODE_ENCRYPTION_INFORMATION: 42373deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set |= SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION; 42389c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_ltk); 42393deb3ec6SMatthias Ringwald break; 42403deb3ec6SMatthias Ringwald 42413deb3ec6SMatthias Ringwald case SM_CODE_MASTER_IDENTIFICATION: 42423deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set |= SM_KEYDIST_FLAG_MASTER_IDENTIFICATION; 4243f8fbdce0SMatthias Ringwald setup->sm_peer_ediv = little_endian_read_16(packet, 1); 42449c80e4ccSMatthias Ringwald reverse_64(&packet[3], setup->sm_peer_rand); 42453deb3ec6SMatthias Ringwald break; 42463deb3ec6SMatthias Ringwald 42473deb3ec6SMatthias Ringwald case SM_CODE_IDENTITY_INFORMATION: 42483deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set |= SM_KEYDIST_FLAG_IDENTITY_INFORMATION; 42499c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_irk); 42503deb3ec6SMatthias Ringwald break; 42513deb3ec6SMatthias Ringwald 42523deb3ec6SMatthias Ringwald case SM_CODE_IDENTITY_ADDRESS_INFORMATION: 42533deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set |= SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION; 42543deb3ec6SMatthias Ringwald setup->sm_peer_addr_type = packet[1]; 4255724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], setup->sm_peer_address); 42563deb3ec6SMatthias Ringwald break; 42573deb3ec6SMatthias Ringwald 42583deb3ec6SMatthias Ringwald case SM_CODE_SIGNING_INFORMATION: 42593deb3ec6SMatthias Ringwald setup->sm_key_distribution_received_set |= SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION; 42609c80e4ccSMatthias Ringwald reverse_128(&packet[1], setup->sm_peer_csrk); 42613deb3ec6SMatthias Ringwald break; 42623deb3ec6SMatthias Ringwald default: 42633deb3ec6SMatthias Ringwald // Unexpected PDU 42643deb3ec6SMatthias Ringwald log_info("Unexpected PDU %u in SM_PH3_RECEIVE_KEYS", packet[0]); 42653deb3ec6SMatthias Ringwald break; 42663deb3ec6SMatthias Ringwald } 42673deb3ec6SMatthias Ringwald // done with key distribution? 42683deb3ec6SMatthias Ringwald if (sm_key_distribution_all_received(sm_conn)){ 42693deb3ec6SMatthias Ringwald 42703deb3ec6SMatthias Ringwald sm_key_distribution_handle_all_received(sm_conn); 42713deb3ec6SMatthias Ringwald 427242134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 42736f7422f1SMatthias Ringwald sm_key_distribution_complete_responder(sm_conn); 42743deb3ec6SMatthias Ringwald } else { 4275625f00b2SMatthias Ringwald if (setup->sm_use_secure_connections){ 4276625f00b2SMatthias Ringwald sm_conn->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS; 4277bbf8db22SMatthias Ringwald } else { 4278f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, sm_random_data, 8, &sm_handle_random_result_ph3_random, (void *)(uintptr_t) sm_conn->sm_handle); 4279625f00b2SMatthias Ringwald } 42803deb3ec6SMatthias Ringwald } 42813deb3ec6SMatthias Ringwald } 42823deb3ec6SMatthias Ringwald break; 42833deb3ec6SMatthias Ringwald default: 42843deb3ec6SMatthias Ringwald // Unexpected PDU 42853deb3ec6SMatthias Ringwald log_info("Unexpected PDU %u in state %u", packet[0], sm_conn->sm_engine_state); 42863deb3ec6SMatthias Ringwald break; 42873deb3ec6SMatthias Ringwald } 42883deb3ec6SMatthias Ringwald 428970b44dd4SMatthias Ringwald // try to send next pdu 429070b44dd4SMatthias Ringwald sm_trigger_run(); 42913deb3ec6SMatthias Ringwald } 42923deb3ec6SMatthias Ringwald 42933deb3ec6SMatthias Ringwald // Security Manager Client API 4294a680ba6bSMatthias Ringwald void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data)){ 42953deb3ec6SMatthias Ringwald sm_get_oob_data = get_oob_data_callback; 42963deb3ec6SMatthias Ringwald } 42973deb3ec6SMatthias Ringwald 42984acf7b7bSMatthias Ringwald void sm_register_sc_oob_data_callback( int (*get_sc_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_sc_peer_confirm, uint8_t * oob_sc_peer_random)){ 4299a680ba6bSMatthias Ringwald sm_get_sc_oob_data = get_sc_oob_data_callback; 4300a680ba6bSMatthias Ringwald } 4301a680ba6bSMatthias Ringwald 430289a78d34SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 430389a78d34SMatthias Ringwald btstack_linked_list_add_tail(&sm_event_handlers, (btstack_linked_item_t*) callback_handler); 430489a78d34SMatthias Ringwald } 430589a78d34SMatthias Ringwald 43063deb3ec6SMatthias Ringwald void sm_set_accepted_stk_generation_methods(uint8_t accepted_stk_generation_methods){ 43073deb3ec6SMatthias Ringwald sm_accepted_stk_generation_methods = accepted_stk_generation_methods; 43083deb3ec6SMatthias Ringwald } 43093deb3ec6SMatthias Ringwald 43103deb3ec6SMatthias Ringwald void sm_set_encryption_key_size_range(uint8_t min_size, uint8_t max_size){ 43113deb3ec6SMatthias Ringwald sm_min_encryption_key_size = min_size; 43123deb3ec6SMatthias Ringwald sm_max_encryption_key_size = max_size; 43133deb3ec6SMatthias Ringwald } 43143deb3ec6SMatthias Ringwald 43153deb3ec6SMatthias Ringwald void sm_set_authentication_requirements(uint8_t auth_req){ 431698d95509SMatthias Ringwald #ifndef ENABLE_LE_SECURE_CONNECTIONS 431798d95509SMatthias Ringwald if (auth_req & SM_AUTHREQ_SECURE_CONNECTION){ 431898d95509SMatthias Ringwald log_error("ENABLE_LE_SECURE_CONNECTIONS not defined, but requested by app. Dropping SC flag"); 431998d95509SMatthias Ringwald auth_req &= ~SM_AUTHREQ_SECURE_CONNECTION; 432098d95509SMatthias Ringwald } 432198d95509SMatthias Ringwald #endif 43223deb3ec6SMatthias Ringwald sm_auth_req = auth_req; 43233deb3ec6SMatthias Ringwald } 43243deb3ec6SMatthias Ringwald 43253deb3ec6SMatthias Ringwald void sm_set_io_capabilities(io_capability_t io_capability){ 43263deb3ec6SMatthias Ringwald sm_io_capabilities = io_capability; 43273deb3ec6SMatthias Ringwald } 43283deb3ec6SMatthias Ringwald 432942134bc6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 43303deb3ec6SMatthias Ringwald void sm_set_request_security(int enable){ 43313deb3ec6SMatthias Ringwald sm_slave_request_security = enable; 43323deb3ec6SMatthias Ringwald } 433342134bc6SMatthias Ringwald #endif 43343deb3ec6SMatthias Ringwald 43353deb3ec6SMatthias Ringwald void sm_set_er(sm_key_t er){ 43366535961aSMatthias Ringwald (void)memcpy(sm_persistent_er, er, 16); 43373deb3ec6SMatthias Ringwald } 43383deb3ec6SMatthias Ringwald 43393deb3ec6SMatthias Ringwald void sm_set_ir(sm_key_t ir){ 43406535961aSMatthias Ringwald (void)memcpy(sm_persistent_ir, ir, 16); 43413deb3ec6SMatthias Ringwald } 43423deb3ec6SMatthias Ringwald 43433deb3ec6SMatthias Ringwald // Testing support only 43443deb3ec6SMatthias Ringwald void sm_test_set_irk(sm_key_t irk){ 43456535961aSMatthias Ringwald (void)memcpy(sm_persistent_irk, irk, 16); 4346103fa6b0SMatthias Ringwald dkg_state = DKG_CALC_DHK; 4347841468bbSMatthias Ringwald test_use_fixed_local_irk = true; 43483deb3ec6SMatthias Ringwald } 43493deb3ec6SMatthias Ringwald 43503deb3ec6SMatthias Ringwald void sm_test_use_fixed_local_csrk(void){ 4351841468bbSMatthias Ringwald test_use_fixed_local_csrk = true; 43523deb3ec6SMatthias Ringwald } 43533deb3ec6SMatthias Ringwald 4354d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4355d1a1f6a4SMatthias Ringwald static void sm_ec_generated(void * arg){ 4356d1a1f6a4SMatthias Ringwald UNUSED(arg); 4357d1a1f6a4SMatthias Ringwald ec_key_generation_state = EC_KEY_GENERATION_DONE; 435834b6528fSMatthias Ringwald // trigger pairing if pending for ec key 435970b44dd4SMatthias Ringwald sm_trigger_run(); 4360d1a1f6a4SMatthias Ringwald } 4361674e5b4aSMatthias Ringwald static void sm_ec_generate_new_key(void){ 436234b6528fSMatthias Ringwald log_info("sm: generate new ec key"); 4363674e5b4aSMatthias Ringwald ec_key_generation_state = EC_KEY_GENERATION_ACTIVE; 4364674e5b4aSMatthias Ringwald btstack_crypto_ecc_p256_generate_key(&sm_crypto_ecc_p256_request, ec_q, &sm_ec_generated, NULL); 4365674e5b4aSMatthias Ringwald } 4366d1a1f6a4SMatthias Ringwald #endif 4367d1a1f6a4SMatthias Ringwald 4368192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 4369192365feSMatthias Ringwald void sm_test_set_pairing_failure(int reason){ 4370192365feSMatthias Ringwald test_pairing_failure = reason; 4371192365feSMatthias Ringwald } 4372192365feSMatthias Ringwald #endif 4373192365feSMatthias Ringwald 43743deb3ec6SMatthias Ringwald void sm_init(void){ 43752d2d4d3cSMatthias Ringwald 43762d2d4d3cSMatthias Ringwald if (sm_initialized) return; 43772d2d4d3cSMatthias Ringwald 4378899e6e02SMatthias Ringwald // set default ER and IR values (should be unique - set by app or sm later using TLV) 4379899e6e02SMatthias Ringwald sm_er_ir_set_default(); 4380899e6e02SMatthias Ringwald 43813deb3ec6SMatthias Ringwald // defaults 43823deb3ec6SMatthias Ringwald sm_accepted_stk_generation_methods = SM_STK_GENERATION_METHOD_JUST_WORKS 43833deb3ec6SMatthias Ringwald | SM_STK_GENERATION_METHOD_OOB 4384b4343428SMatthias Ringwald | SM_STK_GENERATION_METHOD_PASSKEY 4385b4343428SMatthias Ringwald | SM_STK_GENERATION_METHOD_NUMERIC_COMPARISON; 4386b4343428SMatthias Ringwald 43873deb3ec6SMatthias Ringwald sm_max_encryption_key_size = 16; 43883deb3ec6SMatthias Ringwald sm_min_encryption_key_size = 7; 43893deb3ec6SMatthias Ringwald 43904b8c611fSMatthias Ringwald sm_fixed_passkey_in_display_role = 0xffffffff; 43911979f09cSMatthias Ringwald sm_reconstruct_ltk_without_le_device_db_entry = true; 4392caf15bf3SMatthias Ringwald 4393d1a1f6a4SMatthias Ringwald #ifdef USE_CMAC_ENGINE 4394d1a1f6a4SMatthias Ringwald sm_cmac_active = 0; 43957a766ebfSMatthias Ringwald #endif 43968d9b6072SMatthias Ringwald dkg_state = DKG_W4_WORKING; 4397fbd4e238SMatthias Ringwald rau_state = RAU_IDLE; 43983deb3ec6SMatthias Ringwald sm_aes128_state = SM_AES128_IDLE; 43993deb3ec6SMatthias Ringwald sm_address_resolution_test = -1; // no private address to resolve yet 44003deb3ec6SMatthias Ringwald sm_address_resolution_ah_calculation_active = 0; 44013deb3ec6SMatthias Ringwald sm_address_resolution_mode = ADDRESS_RESOLUTION_IDLE; 44023deb3ec6SMatthias Ringwald sm_address_resolution_general_queue = NULL; 44033deb3ec6SMatthias Ringwald 44043deb3ec6SMatthias Ringwald gap_random_adress_update_period = 15 * 60 * 1000L; 44057149bde5SMatthias Ringwald sm_active_connection_handle = HCI_CON_HANDLE_INVALID; 44063deb3ec6SMatthias Ringwald 4407841468bbSMatthias Ringwald test_use_fixed_local_csrk = false; 44083deb3ec6SMatthias Ringwald 440984c0c5c7SMatthias Ringwald btstack_run_loop_set_timer_handler(&sm_run_timer, &sm_run_timer_handler); 441084c0c5c7SMatthias Ringwald 4411e03e489aSMatthias Ringwald // register for HCI Events from HCI 4412e03e489aSMatthias Ringwald hci_event_callback_registration.callback = &sm_event_packet_handler; 4413e03e489aSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 4414e03e489aSMatthias Ringwald 4415d1a1f6a4SMatthias Ringwald // 4416d1a1f6a4SMatthias Ringwald btstack_crypto_init(); 4417d1a1f6a4SMatthias Ringwald 441851bd74d1SMatthias Ringwald // init le_device_db 441951bd74d1SMatthias Ringwald le_device_db_init(); 442051bd74d1SMatthias Ringwald 4421b170b20fSMatthias Ringwald // and L2CAP PDUs + L2CAP_EVENT_CAN_SEND_NOW 4422e03e489aSMatthias Ringwald l2cap_register_fixed_channel(sm_pdu_handler, L2CAP_CID_SECURITY_MANAGER_PROTOCOL); 442327c32905SMatthias Ringwald 442409e4d397SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4425674e5b4aSMatthias Ringwald sm_ec_generate_new_key(); 4426a3aba2f9SMatthias Ringwald #endif 44272d2d4d3cSMatthias Ringwald 44282d2d4d3cSMatthias Ringwald sm_initialized = true; 44293deb3ec6SMatthias Ringwald } 44303deb3ec6SMatthias Ringwald 44314b8c611fSMatthias Ringwald void sm_use_fixed_passkey_in_display_role(uint32_t passkey){ 44324b8c611fSMatthias Ringwald sm_fixed_passkey_in_display_role = passkey; 4433caf15bf3SMatthias Ringwald } 4434caf15bf3SMatthias Ringwald 44356c39055aSMatthias Ringwald void sm_allow_ltk_reconstruction_without_le_device_db_entry(int allow){ 44361979f09cSMatthias Ringwald sm_reconstruct_ltk_without_le_device_db_entry = allow != 0; 44376c39055aSMatthias Ringwald } 44386c39055aSMatthias Ringwald 4439711e6c80SMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 4440711e6c80SMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 44413deb3ec6SMatthias Ringwald if (!hci_con) return NULL; 44423deb3ec6SMatthias Ringwald return &hci_con->sm_connection; 44433deb3ec6SMatthias Ringwald } 44443deb3ec6SMatthias Ringwald 44456bc3aba4SMatthias Ringwald // @deprecated: map onto sm_request_pairing 4446711e6c80SMatthias Ringwald void sm_send_security_request(hci_con_handle_t con_handle){ 4447711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 44483deb3ec6SMatthias Ringwald if (!sm_conn) return; 44496bc3aba4SMatthias Ringwald if (!IS_RESPONDER(sm_conn->sm_role)) return; 44506bc3aba4SMatthias Ringwald sm_request_pairing(con_handle); 44513deb3ec6SMatthias Ringwald } 44523deb3ec6SMatthias Ringwald 44533deb3ec6SMatthias Ringwald // request pairing 4454711e6c80SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){ 4455711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 44563deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 44573deb3ec6SMatthias Ringwald 44587af5dcd5SMatthias Ringwald bool have_ltk; 44597af5dcd5SMatthias Ringwald uint8_t ltk[16]; 44603deb3ec6SMatthias Ringwald log_info("sm_request_pairing in role %u, state %u", sm_conn->sm_role, sm_conn->sm_engine_state); 446142134bc6SMatthias Ringwald if (IS_RESPONDER(sm_conn->sm_role)){ 446224c20dc4SMatthias Ringwald switch (sm_conn->sm_engine_state){ 446324c20dc4SMatthias Ringwald case SM_GENERAL_IDLE: 446424c20dc4SMatthias Ringwald case SM_RESPONDER_IDLE: 44657af5dcd5SMatthias Ringwald switch (sm_conn->sm_irk_lookup_state){ 44667af5dcd5SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 44677af5dcd5SMatthias Ringwald le_device_db_encryption_get(sm_conn->sm_le_db_index, NULL, NULL, ltk, NULL, NULL, NULL, NULL); 44687af5dcd5SMatthias Ringwald have_ltk = !sm_is_null_key(ltk); 44697af5dcd5SMatthias Ringwald log_info("have ltk %u", have_ltk); 44707af5dcd5SMatthias Ringwald if (have_ltk){ 44717af5dcd5SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 447224c20dc4SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST; 44737af5dcd5SMatthias Ringwald sm_reencryption_started(sm_conn); 44747af5dcd5SMatthias Ringwald break; 44757af5dcd5SMatthias Ringwald } 44767af5dcd5SMatthias Ringwald /* fall through */ 44777af5dcd5SMatthias Ringwald 44787af5dcd5SMatthias Ringwald case IRK_LOOKUP_FAILED: 44797af5dcd5SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 44807af5dcd5SMatthias Ringwald sm_conn->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST; 44817af5dcd5SMatthias Ringwald sm_pairing_started(sm_conn); 44827af5dcd5SMatthias Ringwald break; 44837af5dcd5SMatthias Ringwald default: 44847af5dcd5SMatthias Ringwald log_info("irk lookup pending"); 44857af5dcd5SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 44867af5dcd5SMatthias Ringwald break; 44877af5dcd5SMatthias Ringwald } 448824c20dc4SMatthias Ringwald break; 448924c20dc4SMatthias Ringwald default: 449024c20dc4SMatthias Ringwald break; 449124c20dc4SMatthias Ringwald } 44923deb3ec6SMatthias Ringwald } else { 44933deb3ec6SMatthias Ringwald // used as a trigger to start central/master/initiator security procedures 4494175b7faaSMatthias Ringwald switch (sm_conn->sm_engine_state){ 4495175b7faaSMatthias Ringwald case SM_INITIATOR_CONNECTED: 44963deb3ec6SMatthias Ringwald switch (sm_conn->sm_irk_lookup_state){ 44973deb3ec6SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 44983dc3a67dSMatthias Ringwald le_device_db_encryption_get(sm_conn->sm_le_db_index, NULL, NULL, ltk, NULL, NULL, NULL, NULL); 4499f53ec649SMatthias Ringwald have_ltk = !sm_is_null_key(ltk); 4500f53ec649SMatthias Ringwald log_info("have ltk %u", have_ltk); 4501f53ec649SMatthias Ringwald if (have_ltk){ 4502c245ca32SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 45035567aa60SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH4_HAS_LTK; 4504c245ca32SMatthias Ringwald break; 4505f53ec649SMatthias Ringwald } 4506cf373d3aSMatthias Ringwald /* fall through */ 4507c245ca32SMatthias Ringwald 450834c39fbdSMatthias Ringwald case IRK_LOOKUP_FAILED: 45093deb3ec6SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 45103deb3ec6SMatthias Ringwald break; 45113deb3ec6SMatthias Ringwald default: 4512d1a1f6a4SMatthias Ringwald log_info("irk lookup pending"); 451309ea1b62SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 45143deb3ec6SMatthias Ringwald break; 45153deb3ec6SMatthias Ringwald } 4516175b7faaSMatthias Ringwald break; 4517cb6d7eb0SMatthias Ringwald case SM_GENERAL_REENCRYPTION_FAILED: 4518cb6d7eb0SMatthias Ringwald sm_conn->sm_engine_state = SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST; 4519cb6d7eb0SMatthias Ringwald break; 4520175b7faaSMatthias Ringwald case SM_GENERAL_IDLE: 452109ea1b62SMatthias Ringwald sm_conn->sm_pairing_requested = 1; 4522175b7faaSMatthias Ringwald break; 4523175b7faaSMatthias Ringwald default: 4524175b7faaSMatthias Ringwald break; 45253deb3ec6SMatthias Ringwald } 45263deb3ec6SMatthias Ringwald } 452770b44dd4SMatthias Ringwald sm_trigger_run(); 45283deb3ec6SMatthias Ringwald } 45293deb3ec6SMatthias Ringwald 45303deb3ec6SMatthias Ringwald // called by client app on authorization request 4531711e6c80SMatthias Ringwald void sm_authorization_decline(hci_con_handle_t con_handle){ 4532711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 45333deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 45343deb3ec6SMatthias Ringwald sm_conn->sm_connection_authorization_state = AUTHORIZATION_DECLINED; 4535589f5a99SMatthias Ringwald sm_notify_client_status(SM_EVENT_AUTHORIZATION_RESULT, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, 0); 45363deb3ec6SMatthias Ringwald } 45373deb3ec6SMatthias Ringwald 4538711e6c80SMatthias Ringwald void sm_authorization_grant(hci_con_handle_t con_handle){ 4539711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 45403deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 45413deb3ec6SMatthias Ringwald sm_conn->sm_connection_authorization_state = AUTHORIZATION_GRANTED; 4542589f5a99SMatthias Ringwald sm_notify_client_status(SM_EVENT_AUTHORIZATION_RESULT, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, 1); 45433deb3ec6SMatthias Ringwald } 45443deb3ec6SMatthias Ringwald 45453deb3ec6SMatthias Ringwald // GAP Bonding API 45463deb3ec6SMatthias Ringwald 4547711e6c80SMatthias Ringwald void sm_bonding_decline(hci_con_handle_t con_handle){ 4548711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 45493deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 45503deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_DECLINE; 45510af429c6SMatthias Ringwald log_info("decline, state %u", sm_conn->sm_engine_state); 45520af429c6SMatthias Ringwald switch(sm_conn->sm_engine_state){ 45530af429c6SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 45540af429c6SMatthias Ringwald case SM_SC_W4_USER_RESPONSE: 45550af429c6SMatthias Ringwald case SM_SC_W4_CONFIRMATION: 45560af429c6SMatthias Ringwald case SM_SC_W4_PUBLIC_KEY_COMMAND: 45570af429c6SMatthias Ringwald #endif 45580af429c6SMatthias Ringwald case SM_PH1_W4_USER_RESPONSE: 4559de2fd182SMatthias Ringwald switch (setup->sm_stk_generation_method){ 4560de2fd182SMatthias Ringwald case PK_RESP_INPUT: 4561de2fd182SMatthias Ringwald case PK_INIT_INPUT: 456247fb4255SMatthias Ringwald case PK_BOTH_INPUT: 45630af429c6SMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_PASSKEY_ENTRY_FAILED); 4564de2fd182SMatthias Ringwald break; 456547fb4255SMatthias Ringwald case NUMERIC_COMPARISON: 4566de2fd182SMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_NUMERIC_COMPARISON_FAILED); 4567de2fd182SMatthias Ringwald break; 4568de2fd182SMatthias Ringwald case JUST_WORKS: 4569de2fd182SMatthias Ringwald case OOB: 4570de2fd182SMatthias Ringwald sm_pairing_error(sm_conn, SM_REASON_UNSPECIFIED_REASON); 4571de2fd182SMatthias Ringwald break; 45727bbeb3adSMilanka Ringwald default: 45737bbeb3adSMilanka Ringwald btstack_assert(false); 45747bbeb3adSMilanka Ringwald break; 4575de2fd182SMatthias Ringwald } 45760af429c6SMatthias Ringwald break; 45770af429c6SMatthias Ringwald default: 45780af429c6SMatthias Ringwald break; 45793deb3ec6SMatthias Ringwald } 458070b44dd4SMatthias Ringwald sm_trigger_run(); 45813deb3ec6SMatthias Ringwald } 45823deb3ec6SMatthias Ringwald 4583711e6c80SMatthias Ringwald void sm_just_works_confirm(hci_con_handle_t con_handle){ 4584711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 45853deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 45863deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_CONFIRM; 45873deb3ec6SMatthias Ringwald if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){ 4588136d331aSMatthias Ringwald if (setup->sm_use_secure_connections){ 4589c6b7cbd9SMatthias Ringwald sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND; 4590bbf8db22SMatthias Ringwald } else { 4591f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_random, 16, &sm_handle_random_result_ph2_random, (void *)(uintptr_t) sm_conn->sm_handle); 4592136d331aSMatthias Ringwald } 45933deb3ec6SMatthias Ringwald } 45940346c37cSMatthias Ringwald 45950346c37cSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4596c6b7cbd9SMatthias Ringwald if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){ 4597dc300847SMatthias Ringwald sm_sc_prepare_dhkey_check(sm_conn); 4598446a8c36SMatthias Ringwald } 45990346c37cSMatthias Ringwald #endif 46000346c37cSMatthias Ringwald 460170b44dd4SMatthias Ringwald sm_trigger_run(); 46023deb3ec6SMatthias Ringwald } 46033deb3ec6SMatthias Ringwald 4604c8c46d51SMatthias Ringwald void sm_numeric_comparison_confirm(hci_con_handle_t con_handle){ 4605c8c46d51SMatthias Ringwald // for now, it's the same 4606c8c46d51SMatthias Ringwald sm_just_works_confirm(con_handle); 4607c8c46d51SMatthias Ringwald } 4608c8c46d51SMatthias Ringwald 4609711e6c80SMatthias Ringwald void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey){ 4610711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 46113deb3ec6SMatthias Ringwald if (!sm_conn) return; // wrong connection 46123deb3ec6SMatthias Ringwald sm_reset_tk(); 4613f8fbdce0SMatthias Ringwald big_endian_store_32(setup->sm_tk, 12, passkey); 46143deb3ec6SMatthias Ringwald setup->sm_user_response = SM_USER_RESPONSE_PASSKEY; 46153deb3ec6SMatthias Ringwald if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){ 4616f3582630SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_request, setup->sm_local_random, 16, &sm_handle_random_result_ph2_random, (void *)(uintptr_t) sm_conn->sm_handle); 46173deb3ec6SMatthias Ringwald } 46181c516d8fSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 46196535961aSMatthias Ringwald (void)memcpy(setup->sm_ra, setup->sm_tk, 16); 46206535961aSMatthias Ringwald (void)memcpy(setup->sm_rb, setup->sm_tk, 16); 462107036a04SMatthias Ringwald if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){ 462207036a04SMatthias Ringwald sm_sc_start_calculating_local_confirm(sm_conn); 462307036a04SMatthias Ringwald } 46241c516d8fSMatthias Ringwald #endif 462570b44dd4SMatthias Ringwald sm_trigger_run(); 46263deb3ec6SMatthias Ringwald } 46273deb3ec6SMatthias Ringwald 46283d7fe1e9SMatthias Ringwald void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action){ 46293d7fe1e9SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 46303d7fe1e9SMatthias Ringwald if (!sm_conn) return; // wrong connection 46313d7fe1e9SMatthias Ringwald if (action > SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED) return; 4632dd4a08fbSMatthias Ringwald uint8_t num_actions = setup->sm_keypress_notification >> 5; 46334ea43905SMatthias Ringwald uint8_t flags = setup->sm_keypress_notification & 0x1fu; 4634dd4a08fbSMatthias Ringwald switch (action){ 4635dd4a08fbSMatthias Ringwald case SM_KEYPRESS_PASSKEY_ENTRY_STARTED: 4636dd4a08fbSMatthias Ringwald case SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED: 46374ea43905SMatthias Ringwald flags |= (1u << action); 4638dd4a08fbSMatthias Ringwald break; 4639dd4a08fbSMatthias Ringwald case SM_KEYPRESS_PASSKEY_CLEARED: 4640dd4a08fbSMatthias Ringwald // clear counter, keypress & erased flags + set passkey cleared 46414ea43905SMatthias Ringwald flags = (flags & 0x19u) | (1u << SM_KEYPRESS_PASSKEY_CLEARED); 4642dd4a08fbSMatthias Ringwald break; 4643dd4a08fbSMatthias Ringwald case SM_KEYPRESS_PASSKEY_DIGIT_ENTERED: 46444ea43905SMatthias Ringwald if (flags & (1u << SM_KEYPRESS_PASSKEY_DIGIT_ERASED)){ 4645dd4a08fbSMatthias Ringwald // erase actions queued 4646dd4a08fbSMatthias Ringwald num_actions--; 46474ea43905SMatthias Ringwald if (num_actions == 0u){ 4648dd4a08fbSMatthias Ringwald // clear counter, keypress & erased flags 46494ea43905SMatthias Ringwald flags &= 0x19u; 4650dd4a08fbSMatthias Ringwald } 4651dd4a08fbSMatthias Ringwald break; 4652dd4a08fbSMatthias Ringwald } 4653dd4a08fbSMatthias Ringwald num_actions++; 46544ea43905SMatthias Ringwald flags |= (1u << SM_KEYPRESS_PASSKEY_DIGIT_ENTERED); 4655dd4a08fbSMatthias Ringwald break; 4656dd4a08fbSMatthias Ringwald case SM_KEYPRESS_PASSKEY_DIGIT_ERASED: 46574ea43905SMatthias Ringwald if (flags & (1u << SM_KEYPRESS_PASSKEY_DIGIT_ENTERED)){ 4658dd4a08fbSMatthias Ringwald // enter actions queued 4659dd4a08fbSMatthias Ringwald num_actions--; 46604ea43905SMatthias Ringwald if (num_actions == 0u){ 4661dd4a08fbSMatthias Ringwald // clear counter, keypress & erased flags 46624ea43905SMatthias Ringwald flags &= 0x19u; 4663dd4a08fbSMatthias Ringwald } 4664dd4a08fbSMatthias Ringwald break; 4665dd4a08fbSMatthias Ringwald } 4666dd4a08fbSMatthias Ringwald num_actions++; 46674ea43905SMatthias Ringwald flags |= (1u << SM_KEYPRESS_PASSKEY_DIGIT_ERASED); 4668dd4a08fbSMatthias Ringwald break; 4669dd4a08fbSMatthias Ringwald default: 4670dd4a08fbSMatthias Ringwald break; 4671dd4a08fbSMatthias Ringwald } 4672dd4a08fbSMatthias Ringwald setup->sm_keypress_notification = (num_actions << 5) | flags; 467370b44dd4SMatthias Ringwald sm_trigger_run(); 46743d7fe1e9SMatthias Ringwald } 46753d7fe1e9SMatthias Ringwald 4676c59d0c92SMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 4677d1a1f6a4SMatthias Ringwald static void sm_handle_random_result_oob(void * arg){ 4678d1a1f6a4SMatthias Ringwald UNUSED(arg); 4679d1a1f6a4SMatthias Ringwald sm_sc_oob_state = SM_SC_OOB_W2_CALC_CONFIRM; 468070b44dd4SMatthias Ringwald sm_trigger_run(); 4681d1a1f6a4SMatthias Ringwald } 4682c59d0c92SMatthias Ringwald uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value)){ 46838334d3d8SMatthias Ringwald 46848334d3d8SMatthias Ringwald static btstack_crypto_random_t sm_crypto_random_oob_request; 46858334d3d8SMatthias Ringwald 4686c59d0c92SMatthias Ringwald if (sm_sc_oob_state != SM_SC_OOB_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4687c59d0c92SMatthias Ringwald sm_sc_oob_callback = callback; 4688d1a1f6a4SMatthias Ringwald sm_sc_oob_state = SM_SC_OOB_W4_RANDOM; 4689d1a1f6a4SMatthias Ringwald btstack_crypto_random_generate(&sm_crypto_random_oob_request, sm_sc_oob_random, 16, &sm_handle_random_result_oob, NULL); 4690c59d0c92SMatthias Ringwald return 0; 4691c59d0c92SMatthias Ringwald } 4692c59d0c92SMatthias Ringwald #endif 4693c59d0c92SMatthias Ringwald 46943deb3ec6SMatthias Ringwald /** 4695ba394633SMatthias Ringwald * @brief Get Identity Resolving state 4696ba394633SMatthias Ringwald * @param con_handle 4697ba394633SMatthias Ringwald * @return irk_lookup_state_t 4698ba394633SMatthias Ringwald */ 4699ba394633SMatthias Ringwald irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){ 4700ba394633SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 4701ba394633SMatthias Ringwald if (!sm_conn) return IRK_LOOKUP_IDLE; 4702ba394633SMatthias Ringwald return sm_conn->sm_irk_lookup_state; 4703ba394633SMatthias Ringwald } 4704ba394633SMatthias Ringwald 4705ba394633SMatthias Ringwald /** 47063deb3ec6SMatthias Ringwald * @brief Identify device in LE Device DB 47073deb3ec6SMatthias Ringwald * @param handle 47083deb3ec6SMatthias Ringwald * @returns index from le_device_db or -1 if not found/identified 47093deb3ec6SMatthias Ringwald */ 4710711e6c80SMatthias Ringwald int sm_le_device_index(hci_con_handle_t con_handle ){ 4711711e6c80SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 47123deb3ec6SMatthias Ringwald if (!sm_conn) return -1; 47133deb3ec6SMatthias Ringwald return sm_conn->sm_le_db_index; 47143deb3ec6SMatthias Ringwald } 47153deb3ec6SMatthias Ringwald 47168f57b085SMatthias Ringwald static int gap_random_address_type_requires_updates(void){ 471747ba4de1SMatthias Ringwald switch (gap_random_adress_type){ 471847ba4de1SMatthias Ringwald case GAP_RANDOM_ADDRESS_TYPE_OFF: 471947ba4de1SMatthias Ringwald case GAP_RANDOM_ADDRESS_TYPE_STATIC: 472047ba4de1SMatthias Ringwald return 0; 472147ba4de1SMatthias Ringwald default: 47228f57b085SMatthias Ringwald return 1; 47238f57b085SMatthias Ringwald } 472447ba4de1SMatthias Ringwald } 4725d70217a2SMatthias Ringwald 472633373e40SMatthias Ringwald static uint8_t own_address_type(void){ 4727b95a5a35SMatthias Ringwald switch (gap_random_adress_type){ 4728b95a5a35SMatthias Ringwald case GAP_RANDOM_ADDRESS_TYPE_OFF: 4729b95a5a35SMatthias Ringwald return BD_ADDR_TYPE_LE_PUBLIC; 4730b95a5a35SMatthias Ringwald default: 4731b95a5a35SMatthias Ringwald return BD_ADDR_TYPE_LE_RANDOM; 4732b95a5a35SMatthias Ringwald } 473333373e40SMatthias Ringwald } 47348f57b085SMatthias Ringwald 47353deb3ec6SMatthias Ringwald // GAP LE API 47363deb3ec6SMatthias Ringwald void gap_random_address_set_mode(gap_random_address_type_t random_address_type){ 47373deb3ec6SMatthias Ringwald gap_random_address_update_stop(); 47383deb3ec6SMatthias Ringwald gap_random_adress_type = random_address_type; 4739b95a5a35SMatthias Ringwald hci_le_set_own_address_type(own_address_type()); 47408f57b085SMatthias Ringwald if (!gap_random_address_type_requires_updates()) return; 47413deb3ec6SMatthias Ringwald gap_random_address_update_start(); 47423deb3ec6SMatthias Ringwald gap_random_address_trigger(); 47433deb3ec6SMatthias Ringwald } 47443deb3ec6SMatthias Ringwald 47453deb3ec6SMatthias Ringwald gap_random_address_type_t gap_random_address_get_mode(void){ 47463deb3ec6SMatthias Ringwald return gap_random_adress_type; 47473deb3ec6SMatthias Ringwald } 47483deb3ec6SMatthias Ringwald 47493deb3ec6SMatthias Ringwald void gap_random_address_set_update_period(int period_ms){ 47503deb3ec6SMatthias Ringwald gap_random_adress_update_period = period_ms; 47518f57b085SMatthias Ringwald if (!gap_random_address_type_requires_updates()) return; 47523deb3ec6SMatthias Ringwald gap_random_address_update_stop(); 47533deb3ec6SMatthias Ringwald gap_random_address_update_start(); 47543deb3ec6SMatthias Ringwald } 47553deb3ec6SMatthias Ringwald 4756667ba9d1SMatthias Ringwald void gap_random_address_set(const bd_addr_t addr){ 47578f57b085SMatthias Ringwald gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_STATIC); 47586535961aSMatthias Ringwald (void)memcpy(sm_random_address, addr, 6); 47597e252622SMatthias Ringwald rau_state = RAU_SET_ADDRESS; 476070b44dd4SMatthias Ringwald sm_trigger_run(); 47617e252622SMatthias Ringwald } 47627e252622SMatthias Ringwald 4763d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 47643deb3ec6SMatthias Ringwald /* 47653deb3ec6SMatthias Ringwald * @brief Set Advertisement Paramters 47663deb3ec6SMatthias Ringwald * @param adv_int_min 47673deb3ec6SMatthias Ringwald * @param adv_int_max 47683deb3ec6SMatthias Ringwald * @param adv_type 47693deb3ec6SMatthias Ringwald * @param direct_address_type 47703deb3ec6SMatthias Ringwald * @param direct_address 47713deb3ec6SMatthias Ringwald * @param channel_map 47723deb3ec6SMatthias Ringwald * @param filter_policy 47733deb3ec6SMatthias Ringwald * 47743deb3ec6SMatthias Ringwald * @note own_address_type is used from gap_random_address_set_mode 47753deb3ec6SMatthias Ringwald */ 47763deb3ec6SMatthias Ringwald void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 47773deb3ec6SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy){ 4778b95a5a35SMatthias Ringwald hci_le_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 47793deb3ec6SMatthias Ringwald direct_address_typ, direct_address, channel_map, filter_policy); 47803deb3ec6SMatthias Ringwald } 4781d70217a2SMatthias Ringwald #endif 4782dcd6c9b5SMatthias Ringwald 4783dcd6c9b5SMatthias Ringwald int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){ 4784dcd6c9b5SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 4785dcd6c9b5SMatthias Ringwald // wrong connection 4786dcd6c9b5SMatthias Ringwald if (!sm_conn) return 0; 4787dcd6c9b5SMatthias Ringwald // already encrypted 4788dcd6c9b5SMatthias Ringwald if (sm_conn->sm_connection_encrypted) return 0; 4789dcd6c9b5SMatthias Ringwald // irk status? 4790dcd6c9b5SMatthias Ringwald switch(sm_conn->sm_irk_lookup_state){ 4791dcd6c9b5SMatthias Ringwald case IRK_LOOKUP_FAILED: 4792dcd6c9b5SMatthias Ringwald // done, cannot setup encryption 4793dcd6c9b5SMatthias Ringwald return 0; 4794dcd6c9b5SMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 4795dcd6c9b5SMatthias Ringwald break; 4796dcd6c9b5SMatthias Ringwald default: 4797dcd6c9b5SMatthias Ringwald // IR Lookup pending 4798dcd6c9b5SMatthias Ringwald return 1; 4799dcd6c9b5SMatthias Ringwald } 4800f0674e22SMatthias Ringwald // IRK Lookup Succeeded, re-encryption should be initiated. When done, state gets reset or indicates failure 4801f0674e22SMatthias Ringwald if (sm_conn->sm_engine_state == SM_GENERAL_REENCRYPTION_FAILED) return 0; 4802b15d5ceaSMatthias Ringwald if (sm_conn->sm_role){ 4803b15d5ceaSMatthias Ringwald return sm_conn->sm_engine_state != SM_RESPONDER_IDLE; 4804b15d5ceaSMatthias Ringwald } else { 4805dcd6c9b5SMatthias Ringwald return sm_conn->sm_engine_state != SM_INITIATOR_CONNECTED; 4806dcd6c9b5SMatthias Ringwald } 4807b15d5ceaSMatthias Ringwald } 48083cdbe9dbSMatthias Ringwald 48093cdbe9dbSMatthias Ringwald void sm_set_secure_connections_only_mode(bool enable){ 48103cdbe9dbSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 48113cdbe9dbSMatthias Ringwald sm_sc_only_mode = enable; 48123cdbe9dbSMatthias Ringwald #else 48133cdbe9dbSMatthias Ringwald // SC Only mode not possible without support for SC 48143cdbe9dbSMatthias Ringwald btstack_assert(enable == false); 48153cdbe9dbSMatthias Ringwald #endif 48163cdbe9dbSMatthias Ringwald } 4817052bbdc5SMatthias Ringwald 4818052bbdc5SMatthias Ringwald const uint8_t * gap_get_persistent_irk(void){ 4819052bbdc5SMatthias Ringwald return sm_persistent_irk; 4820052bbdc5SMatthias Ringwald } 48214f384501SMatthias Ringwald 48224f384501SMatthias Ringwald void gap_delete_bonding(bd_addr_type_t address_type, bd_addr_t address){ 48234f384501SMatthias Ringwald uint16_t i; 48244f384501SMatthias Ringwald for (i=0; i < le_device_db_max_count(); i++){ 48254f384501SMatthias Ringwald bd_addr_t entry_address; 48264f384501SMatthias Ringwald int entry_address_type = BD_ADDR_TYPE_UNKNOWN; 48274f384501SMatthias Ringwald le_device_db_info(i, &entry_address_type, entry_address, NULL); 48284f384501SMatthias Ringwald // skip unused entries 48294f384501SMatthias Ringwald if (entry_address_type == (int) BD_ADDR_TYPE_UNKNOWN) continue; 48304f384501SMatthias Ringwald if ((entry_address_type == (int) address_type) && (memcmp(entry_address, address, 6) == 0)){ 48314f384501SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 48324f384501SMatthias Ringwald hci_remove_le_device_db_entry_from_resolving_list(i); 48334f384501SMatthias Ringwald #endif 48344f384501SMatthias Ringwald le_device_db_remove(i); 48354f384501SMatthias Ringwald break; 48364f384501SMatthias Ringwald } 48374f384501SMatthias Ringwald } 48384f384501SMatthias Ringwald } 4839