sm.c (1de8bf6a9e7a2d10b53f59e1c19d2f35ce353103) sm.c (70b44dd47b25a2f6b9ca02ee6c8d0c1c044a7c0e)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 460 unchanged lines hidden (view full) ---

469static int sm_is_null_random(uint8_t random[8]){
470 return sm_is_null(random, 8);
471}
472
473static int sm_is_null_key(uint8_t * key){
474 return sm_is_null(key, 16);
475}
476
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 460 unchanged lines hidden (view full) ---

469static int sm_is_null_random(uint8_t random[8]){
470 return sm_is_null(random, 8);
471}
472
473static int sm_is_null_key(uint8_t * key){
474 return sm_is_null(key, 16);
475}
476
477// sm_trigger_run allows to schedule callback from main run loop // reduces stack depth
478static void sm_run_timer_handler(btstack_timer_source_t * ts){
479 UNUSED(ts);
480 sm_run();
481}
482static void sm_trigger_run(void){
483 static btstack_timer_source_t sm_run_timer;
484 btstack_run_loop_remove_timer(&sm_run_timer);
485 btstack_run_loop_set_timer_handler(&sm_run_timer, &sm_run_timer_handler);
486 btstack_run_loop_set_timer(&sm_run_timer, 0);
487 btstack_run_loop_add_timer(&sm_run_timer);
488}
489
477// Key utils
478static void sm_reset_tk(void){
479 int i;
480 for (i=0;i<16;i++){
481 setup->sm_tk[i] = 0;
482 }
483}
484

--- 74 unchanged lines hidden (view full) ---

559static gap_random_address_type_t gap_random_adress_type;
560static btstack_timer_source_t gap_random_address_update_timer;
561static uint32_t gap_random_adress_update_period;
562
563static void gap_random_address_trigger(void){
564 log_info("gap_random_address_trigger, state %u", rau_state);
565 if (rau_state != RAU_IDLE) return;
566 rau_state = RAU_GET_RANDOM;
490// Key utils
491static void sm_reset_tk(void){
492 int i;
493 for (i=0;i<16;i++){
494 setup->sm_tk[i] = 0;
495 }
496}
497

--- 74 unchanged lines hidden (view full) ---

572static gap_random_address_type_t gap_random_adress_type;
573static btstack_timer_source_t gap_random_address_update_timer;
574static uint32_t gap_random_adress_update_period;
575
576static void gap_random_address_trigger(void){
577 log_info("gap_random_address_trigger, state %u", rau_state);
578 if (rau_state != RAU_IDLE) return;
579 rau_state = RAU_GET_RANDOM;
567 sm_run();
580 sm_trigger_run();
568}
569
570static void gap_random_address_update_handler(btstack_timer_source_t * timer){
571 UNUSED(timer);
572
573 log_info("GAP Random Address Update due");
574 btstack_run_loop_set_timer(&gap_random_address_update_timer, gap_random_adress_update_period);
575 btstack_run_loop_add_timer(&gap_random_address_update_timer);

--- 293 unchanged lines hidden (view full) ---

869 // already in list
870 return BTSTACK_BUSY;
871 }
872 entry = btstack_memory_sm_lookup_entry_get();
873 if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED;
874 entry->address_type = (bd_addr_type_t) address_type;
875 (void)memcpy(entry->address, address, 6);
876 btstack_linked_list_add(&sm_address_resolution_general_queue, (btstack_linked_item_t *) entry);
581}
582
583static void gap_random_address_update_handler(btstack_timer_source_t * timer){
584 UNUSED(timer);
585
586 log_info("GAP Random Address Update due");
587 btstack_run_loop_set_timer(&gap_random_address_update_timer, gap_random_adress_update_period);
588 btstack_run_loop_add_timer(&gap_random_address_update_timer);

--- 293 unchanged lines hidden (view full) ---

882 // already in list
883 return BTSTACK_BUSY;
884 }
885 entry = btstack_memory_sm_lookup_entry_get();
886 if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED;
887 entry->address_type = (bd_addr_type_t) address_type;
888 (void)memcpy(entry->address, address, 6);
889 btstack_linked_list_add(&sm_address_resolution_general_queue, (btstack_linked_item_t *) entry);
877 sm_run();
890 sm_trigger_run();
878 return 0;
879}
880
881// CMAC calculation using AES Engineq
882#ifdef USE_CMAC_ENGINE
883
884static void sm_cmac_done_trampoline(void * arg){
885 UNUSED(arg);
886 sm_cmac_active = 0;
887 (*sm_cmac_done_callback)(sm_cmac_hash);
891 return 0;
892}
893
894// CMAC calculation using AES Engineq
895#ifdef USE_CMAC_ENGINE
896
897static void sm_cmac_done_trampoline(void * arg){
898 UNUSED(arg);
899 sm_cmac_active = 0;
900 (*sm_cmac_done_callback)(sm_cmac_hash);
888 sm_run();
901 sm_trigger_run();
889}
890
891int sm_cmac_ready(void){
892 return sm_cmac_active == 0u;
893}
894#endif
895
896#ifdef ENABLE_LE_SECURE_CONNECTIONS

--- 622 unchanged lines hidden (view full) ---

1519 }
1520 sm_notify_client_status_reason(sm_conn, ERROR_CODE_SUCCESS, 0);
1521 sm_done_for_handle(sm_conn->sm_handle);
1522 break;
1523 default:
1524 log_error("sm_sc_cmac_done in state %u", sm_conn->sm_engine_state);
1525 break;
1526 }
902}
903
904int sm_cmac_ready(void){
905 return sm_cmac_active == 0u;
906}
907#endif
908
909#ifdef ENABLE_LE_SECURE_CONNECTIONS

--- 622 unchanged lines hidden (view full) ---

1532 }
1533 sm_notify_client_status_reason(sm_conn, ERROR_CODE_SUCCESS, 0);
1534 sm_done_for_handle(sm_conn->sm_handle);
1535 break;
1536 default:
1537 log_error("sm_sc_cmac_done in state %u", sm_conn->sm_engine_state);
1538 break;
1539 }
1527 sm_run();
1540 sm_trigger_run();
1528}
1529
1530static 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){
1531 const uint16_t message_len = 65;
1532 sm_cmac_connection = sm_conn;
1533 (void)memcpy(sm_cmac_sc_buffer, u, 32);
1534 (void)memcpy(sm_cmac_sc_buffer + 32, v, 32);
1535 sm_cmac_sc_buffer[64] = z;

--- 168 unchanged lines hidden (view full) ---

1704
1705 log_info("dhkey");
1706 log_info_hexdump(&setup->sm_dhkey[0], 32);
1707 setup->sm_state_vars |= SM_STATE_VAR_DHKEY_CALCULATED;
1708 // trigger next step
1709 if (sm_conn->sm_engine_state == SM_SC_W4_CALCULATE_DHKEY){
1710 sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_SALT;
1711 }
1541}
1542
1543static 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){
1544 const uint16_t message_len = 65;
1545 sm_cmac_connection = sm_conn;
1546 (void)memcpy(sm_cmac_sc_buffer, u, 32);
1547 (void)memcpy(sm_cmac_sc_buffer + 32, v, 32);
1548 sm_cmac_sc_buffer[64] = z;

--- 168 unchanged lines hidden (view full) ---

1717
1718 log_info("dhkey");
1719 log_info_hexdump(&setup->sm_dhkey[0], 32);
1720 setup->sm_state_vars |= SM_STATE_VAR_DHKEY_CALCULATED;
1721 // trigger next step
1722 if (sm_conn->sm_engine_state == SM_SC_W4_CALCULATE_DHKEY){
1723 sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_F5_SALT;
1724 }
1712 sm_run();
1725 sm_trigger_run();
1713}
1714
1715static void sm_sc_calculate_f6_for_dhkey_check(sm_connection_t * sm_conn){
1716 // calculate DHKCheck
1717 sm_key56_t bd_addr_master, bd_addr_slave;
1718 bd_addr_master[0] = setup->sm_m_addr_type;
1719 bd_addr_slave[0] = setup->sm_s_addr_type;
1720 (void)memcpy(&bd_addr_master[1], setup->sm_m_address, 6);

--- 112 unchanged lines hidden (view full) ---

1833 sm_connection->sm_actual_encryption_key_size = (setup->sm_local_rand[7u] & 0x0fu) + 1u;
1834 // no db for authenticated flag hack: flag is stored in bit 4 of LSB
1835 sm_connection->sm_connection_authenticated = (setup->sm_local_rand[7u] & 0x10u) >> 4u;
1836 // Legacy paring -> not SC
1837 sm_connection->sm_connection_sc = 0;
1838 log_info("sm: received ltk request with key size %u, authenticated %u",
1839 sm_connection->sm_actual_encryption_key_size, sm_connection->sm_connection_authenticated);
1840 sm_connection->sm_engine_state = SM_RESPONDER_PH4_Y_GET_ENC;
1726}
1727
1728static void sm_sc_calculate_f6_for_dhkey_check(sm_connection_t * sm_conn){
1729 // calculate DHKCheck
1730 sm_key56_t bd_addr_master, bd_addr_slave;
1731 bd_addr_master[0] = setup->sm_m_addr_type;
1732 bd_addr_slave[0] = setup->sm_s_addr_type;
1733 (void)memcpy(&bd_addr_master[1], setup->sm_m_address, 6);

--- 112 unchanged lines hidden (view full) ---

1846 sm_connection->sm_actual_encryption_key_size = (setup->sm_local_rand[7u] & 0x0fu) + 1u;
1847 // no db for authenticated flag hack: flag is stored in bit 4 of LSB
1848 sm_connection->sm_connection_authenticated = (setup->sm_local_rand[7u] & 0x10u) >> 4u;
1849 // Legacy paring -> not SC
1850 sm_connection->sm_connection_sc = 0;
1851 log_info("sm: received ltk request with key size %u, authenticated %u",
1852 sm_connection->sm_actual_encryption_key_size, sm_connection->sm_connection_authenticated);
1853 sm_connection->sm_engine_state = SM_RESPONDER_PH4_Y_GET_ENC;
1841 sm_run();
1854 sm_trigger_run();
1842}
1843#endif
1844
1845// distributed key generation
1846static bool sm_run_dpkg(void){
1847 switch (dkg_state){
1848 case DKG_CALC_IRK:
1849 // already busy?

--- 922 unchanged lines hidden (view full) ---

2772 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2773 sm_aes128_state = SM_AES128_IDLE;
2774
2775 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2776 if (connection == NULL) return;
2777
2778 log_info_key("c1!", setup->sm_local_confirm);
2779 connection->sm_engine_state = SM_PH2_C1_SEND_PAIRING_CONFIRM;
1855}
1856#endif
1857
1858// distributed key generation
1859static bool sm_run_dpkg(void){
1860 switch (dkg_state){
1861 case DKG_CALC_IRK:
1862 // already busy?

--- 922 unchanged lines hidden (view full) ---

2785 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2786 sm_aes128_state = SM_AES128_IDLE;
2787
2788 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2789 if (connection == NULL) return;
2790
2791 log_info_key("c1!", setup->sm_local_confirm);
2792 connection->sm_engine_state = SM_PH2_C1_SEND_PAIRING_CONFIRM;
2780 sm_run();
2793 sm_trigger_run();
2781}
2782
2783// sm_aes128_state stays active
2784static void sm_handle_encryption_result_enc_c(void *arg){
2785 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2786 sm_aes128_state = SM_AES128_IDLE;
2787
2788 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);

--- 10 unchanged lines hidden (view full) ---

2799
2800 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2801 if (connection == NULL) return;
2802
2803 log_info_key("c1!", sm_aes128_ciphertext);
2804 if (memcmp(setup->sm_peer_confirm, sm_aes128_ciphertext, 16) != 0){
2805 setup->sm_pairing_failed_reason = SM_REASON_CONFIRM_VALUE_FAILED;
2806 connection->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED;
2794}
2795
2796// sm_aes128_state stays active
2797static void sm_handle_encryption_result_enc_c(void *arg){
2798 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2799 sm_aes128_state = SM_AES128_IDLE;
2800
2801 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);

--- 10 unchanged lines hidden (view full) ---

2812
2813 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2814 if (connection == NULL) return;
2815
2816 log_info_key("c1!", sm_aes128_ciphertext);
2817 if (memcmp(setup->sm_peer_confirm, sm_aes128_ciphertext, 16) != 0){
2818 setup->sm_pairing_failed_reason = SM_REASON_CONFIRM_VALUE_FAILED;
2819 connection->sm_engine_state = SM_GENERAL_SEND_PAIRING_FAILED;
2807 sm_run();
2820 sm_trigger_run();
2808 return;
2809 }
2810 if (IS_RESPONDER(connection->sm_role)){
2811 connection->sm_engine_state = SM_PH2_SEND_PAIRING_RANDOM;
2821 return;
2822 }
2823 if (IS_RESPONDER(connection->sm_role)){
2824 connection->sm_engine_state = SM_PH2_SEND_PAIRING_RANDOM;
2812 sm_run();
2825 sm_trigger_run();
2813 } else {
2814 sm_s1_r_prime(setup->sm_peer_random, setup->sm_local_random, sm_aes128_plaintext);
2815 sm_aes128_state = SM_AES128_ACTIVE;
2816 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);
2817 }
2818}
2819
2820static void sm_handle_encryption_result_enc_stk(void *arg){

--- 5 unchanged lines hidden (view full) ---

2826
2827 sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size);
2828 log_info_key("stk", setup->sm_ltk);
2829 if (IS_RESPONDER(connection->sm_role)){
2830 connection->sm_engine_state = SM_RESPONDER_PH2_SEND_LTK_REPLY;
2831 } else {
2832 connection->sm_engine_state = SM_INITIATOR_PH3_SEND_START_ENCRYPTION;
2833 }
2826 } else {
2827 sm_s1_r_prime(setup->sm_peer_random, setup->sm_local_random, sm_aes128_plaintext);
2828 sm_aes128_state = SM_AES128_ACTIVE;
2829 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);
2830 }
2831}
2832
2833static void sm_handle_encryption_result_enc_stk(void *arg){

--- 5 unchanged lines hidden (view full) ---

2839
2840 sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size);
2841 log_info_key("stk", setup->sm_ltk);
2842 if (IS_RESPONDER(connection->sm_role)){
2843 connection->sm_engine_state = SM_RESPONDER_PH2_SEND_LTK_REPLY;
2844 } else {
2845 connection->sm_engine_state = SM_INITIATOR_PH3_SEND_START_ENCRYPTION;
2846 }
2834 sm_run();
2847 sm_trigger_run();
2835}
2836
2837// sm_aes128_state stays active
2838static void sm_handle_encryption_result_enc_ph3_y(void *arg){
2839 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2840 sm_aes128_state = SM_AES128_IDLE;
2841
2842 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);

--- 68 unchanged lines hidden (view full) ---

2911 } else {
2912 if (setup->sm_use_secure_connections && (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION)){
2913 connection->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
2914 } else {
2915 sm_master_pairing_success(connection);
2916 }
2917 }
2918 }
2848}
2849
2850// sm_aes128_state stays active
2851static void sm_handle_encryption_result_enc_ph3_y(void *arg){
2852 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2853 sm_aes128_state = SM_AES128_IDLE;
2854
2855 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);

--- 68 unchanged lines hidden (view full) ---

2924 } else {
2925 if (setup->sm_use_secure_connections && (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION)){
2926 connection->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
2927 } else {
2928 sm_master_pairing_success(connection);
2929 }
2930 }
2931 }
2919 sm_run();
2932 sm_trigger_run();
2920}
2921
2922#ifdef ENABLE_LE_PERIPHERAL
2923static void sm_handle_encryption_result_enc_ph4_ltk(void *arg){
2924 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2925 sm_aes128_state = SM_AES128_IDLE;
2926
2927 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2928 if (connection == NULL) return;
2929
2930 sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size);
2931 log_info_key("ltk", setup->sm_ltk);
2932 connection->sm_engine_state = SM_RESPONDER_PH4_SEND_LTK_REPLY;
2933}
2934
2935#ifdef ENABLE_LE_PERIPHERAL
2936static void sm_handle_encryption_result_enc_ph4_ltk(void *arg){
2937 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
2938 sm_aes128_state = SM_AES128_IDLE;
2939
2940 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
2941 if (connection == NULL) return;
2942
2943 sm_truncate_key(setup->sm_ltk, connection->sm_actual_encryption_key_size);
2944 log_info_key("ltk", setup->sm_ltk);
2945 connection->sm_engine_state = SM_RESPONDER_PH4_SEND_LTK_REPLY;
2933 sm_run();
2946 sm_trigger_run();
2934}
2935#endif
2936
2937static void sm_handle_encryption_result_address_resolution(void *arg){
2938 UNUSED(arg);
2939 sm_aes128_state = SM_AES128_IDLE;
2940
2941 sm_address_resolution_ah_calculation_active = 0;
2942 // compare calulated address against connecting device
2943 uint8_t * hash = &sm_aes128_ciphertext[13];
2944 if (memcmp(&sm_address_resolution_address[3], hash, 3) == 0){
2945 log_info("LE Device Lookup: matched resolvable private address");
2946 sm_address_resolution_handle_event(ADDRESS_RESOLUTION_SUCEEDED);
2947}
2948#endif
2949
2950static void sm_handle_encryption_result_address_resolution(void *arg){
2951 UNUSED(arg);
2952 sm_aes128_state = SM_AES128_IDLE;
2953
2954 sm_address_resolution_ah_calculation_active = 0;
2955 // compare calulated address against connecting device
2956 uint8_t * hash = &sm_aes128_ciphertext[13];
2957 if (memcmp(&sm_address_resolution_address[3], hash, 3) == 0){
2958 log_info("LE Device Lookup: matched resolvable private address");
2959 sm_address_resolution_handle_event(ADDRESS_RESOLUTION_SUCEEDED);
2947 sm_run();
2960 sm_trigger_run();
2948 return;
2949 }
2950 // no match, try next
2951 sm_address_resolution_test++;
2961 return;
2962 }
2963 // no match, try next
2964 sm_address_resolution_test++;
2952 sm_run();
2965 sm_trigger_run();
2953}
2954
2955static void sm_handle_encryption_result_dkg_irk(void *arg){
2956 UNUSED(arg);
2957 sm_aes128_state = SM_AES128_IDLE;
2958
2959 log_info_key("irk", sm_persistent_irk);
2960 dkg_state = DKG_CALC_DHK;
2966}
2967
2968static void sm_handle_encryption_result_dkg_irk(void *arg){
2969 UNUSED(arg);
2970 sm_aes128_state = SM_AES128_IDLE;
2971
2972 log_info_key("irk", sm_persistent_irk);
2973 dkg_state = DKG_CALC_DHK;
2961 sm_run();
2974 sm_trigger_run();
2962}
2963
2964static void sm_handle_encryption_result_dkg_dhk(void *arg){
2965 UNUSED(arg);
2966 sm_aes128_state = SM_AES128_IDLE;
2967
2968 log_info_key("dhk", sm_persistent_dhk);
2969 dkg_state = DKG_READY;
2975}
2976
2977static void sm_handle_encryption_result_dkg_dhk(void *arg){
2978 UNUSED(arg);
2979 sm_aes128_state = SM_AES128_IDLE;
2980
2981 log_info_key("dhk", sm_persistent_dhk);
2982 dkg_state = DKG_READY;
2970 sm_run();
2983 sm_trigger_run();
2971}
2972
2973static void sm_handle_encryption_result_rau(void *arg){
2974 UNUSED(arg);
2975 sm_aes128_state = SM_AES128_IDLE;
2976
2977 (void)memcpy(&sm_random_address[3], &sm_aes128_ciphertext[13], 3);
2978 rau_state = RAU_SET_ADDRESS;
2984}
2985
2986static void sm_handle_encryption_result_rau(void *arg){
2987 UNUSED(arg);
2988 sm_aes128_state = SM_AES128_IDLE;
2989
2990 (void)memcpy(&sm_random_address[3], &sm_aes128_ciphertext[13], 3);
2991 rau_state = RAU_SET_ADDRESS;
2979 sm_run();
2992 sm_trigger_run();
2980}
2981
2982static void sm_handle_random_result_rau(void * arg){
2983 UNUSED(arg);
2984 // non-resolvable vs. resolvable
2985 switch (gap_random_adress_type){
2986 case GAP_RANDOM_ADDRESS_RESOLVABLE:
2987 // resolvable: use random as prand and calc address hash

--- 4 unchanged lines hidden (view full) ---

2992 break;
2993 case GAP_RANDOM_ADDRESS_NON_RESOLVABLE:
2994 default:
2995 // "The two most significant bits of the address shall be equal to ‘0’""
2996 sm_random_address[0u] &= 0x3fu;
2997 rau_state = RAU_SET_ADDRESS;
2998 break;
2999 }
2993}
2994
2995static void sm_handle_random_result_rau(void * arg){
2996 UNUSED(arg);
2997 // non-resolvable vs. resolvable
2998 switch (gap_random_adress_type){
2999 case GAP_RANDOM_ADDRESS_RESOLVABLE:
3000 // resolvable: use random as prand and calc address hash

--- 4 unchanged lines hidden (view full) ---

3005 break;
3006 case GAP_RANDOM_ADDRESS_NON_RESOLVABLE:
3007 default:
3008 // "The two most significant bits of the address shall be equal to ‘0’""
3009 sm_random_address[0u] &= 0x3fu;
3010 rau_state = RAU_SET_ADDRESS;
3011 break;
3012 }
3000 sm_run();
3013 sm_trigger_run();
3001}
3002
3003#ifdef ENABLE_LE_SECURE_CONNECTIONS
3004static void sm_handle_random_result_sc_next_send_pairing_random(void * arg){
3005 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3006 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3007 if (connection == NULL) return;
3008
3009 connection->sm_engine_state = SM_SC_SEND_PAIRING_RANDOM;
3014}
3015
3016#ifdef ENABLE_LE_SECURE_CONNECTIONS
3017static void sm_handle_random_result_sc_next_send_pairing_random(void * arg){
3018 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3019 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3020 if (connection == NULL) return;
3021
3022 connection->sm_engine_state = SM_SC_SEND_PAIRING_RANDOM;
3010 sm_run();
3023 sm_trigger_run();
3011}
3012
3013static void sm_handle_random_result_sc_next_w2_cmac_for_confirmation(void * arg){
3014 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3015 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3016 if (connection == NULL) return;
3017
3018 connection->sm_engine_state = SM_SC_W2_CMAC_FOR_CONFIRMATION;
3024}
3025
3026static void sm_handle_random_result_sc_next_w2_cmac_for_confirmation(void * arg){
3027 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3028 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3029 if (connection == NULL) return;
3030
3031 connection->sm_engine_state = SM_SC_W2_CMAC_FOR_CONFIRMATION;
3019 sm_run();
3032 sm_trigger_run();
3020}
3021#endif
3022
3023static void sm_handle_random_result_ph2_random(void * arg){
3024 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3025 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3026 if (connection == NULL) return;
3027
3028 connection->sm_engine_state = SM_PH2_C1_GET_ENC_A;
3033}
3034#endif
3035
3036static void sm_handle_random_result_ph2_random(void * arg){
3037 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3038 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3039 if (connection == NULL) return;
3040
3041 connection->sm_engine_state = SM_PH2_C1_GET_ENC_A;
3029 sm_run();
3042 sm_trigger_run();
3030}
3031
3032static void sm_handle_random_result_ph2_tk(void * arg){
3033 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3034 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3035 if (connection == NULL) return;
3036
3037 sm_reset_tk();

--- 19 unchanged lines hidden (view full) ---

3057 connection->sm_engine_state = SM_PH1_W4_USER_RESPONSE;
3058 sm_trigger_user_response(connection);
3059 // response_idle == nothing <--> sm_trigger_user_response() did not require response
3060 if (setup->sm_user_response == SM_USER_RESPONSE_IDLE){
3061 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);
3062 }
3063 }
3064 }
3043}
3044
3045static void sm_handle_random_result_ph2_tk(void * arg){
3046 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3047 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3048 if (connection == NULL) return;
3049
3050 sm_reset_tk();

--- 19 unchanged lines hidden (view full) ---

3070 connection->sm_engine_state = SM_PH1_W4_USER_RESPONSE;
3071 sm_trigger_user_response(connection);
3072 // response_idle == nothing <--> sm_trigger_user_response() did not require response
3073 if (setup->sm_user_response == SM_USER_RESPONSE_IDLE){
3074 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);
3075 }
3076 }
3077 }
3065 sm_run();
3078 sm_trigger_run();
3066}
3067
3068static void sm_handle_random_result_ph3_div(void * arg){
3069 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3070 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3071 if (connection == NULL) return;
3072
3073 // use 16 bit from random value as div
3074 setup->sm_local_div = big_endian_read_16(sm_random_data, 0);
3075 log_info_hex16("div", setup->sm_local_div);
3076 connection->sm_engine_state = SM_PH3_Y_GET_ENC;
3079}
3080
3081static void sm_handle_random_result_ph3_div(void * arg){
3082 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3083 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3084 if (connection == NULL) return;
3085
3086 // use 16 bit from random value as div
3087 setup->sm_local_div = big_endian_read_16(sm_random_data, 0);
3088 log_info_hex16("div", setup->sm_local_div);
3089 connection->sm_engine_state = SM_PH3_Y_GET_ENC;
3077 sm_run();
3090 sm_trigger_run();
3078}
3079
3080static void sm_handle_random_result_ph3_random(void * arg){
3081 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3082 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3083 if (connection == NULL) return;
3084
3085 reverse_64(sm_random_data, setup->sm_local_rand);

--- 29 unchanged lines hidden (view full) ---

3115 log_info_key("IR", sm_persistent_ir);
3116 dkg_state = DKG_CALC_IRK;
3117
3118 if (test_use_fixed_local_irk){
3119 log_info_key("IRK", sm_persistent_irk);
3120 dkg_state = DKG_CALC_DHK;
3121 }
3122
3091}
3092
3093static void sm_handle_random_result_ph3_random(void * arg){
3094 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) arg;
3095 sm_connection_t * connection = sm_get_connection_for_handle(con_handle);
3096 if (connection == NULL) return;
3097
3098 reverse_64(sm_random_data, setup->sm_local_rand);

--- 29 unchanged lines hidden (view full) ---

3128 log_info_key("IR", sm_persistent_ir);
3129 dkg_state = DKG_CALC_IRK;
3130
3131 if (test_use_fixed_local_irk){
3132 log_info_key("IRK", sm_persistent_irk);
3133 dkg_state = DKG_CALC_DHK;
3134 }
3135
3123 sm_run();
3136 sm_trigger_run();
3124}
3125
3126static void sm_handle_random_result_er(void *arg){
3127 sm_persistent_keys_random_active = 0;
3128 if (arg){
3129 // key generated, store in tlv
3130 int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u);
3131 log_info("Generated ER key. Store in TLV status: %d", status);

--- 847 unchanged lines hidden (view full) ---

3979 }
3980 break;
3981 default:
3982 // Unexpected PDU
3983 log_info("Unexpected PDU %u in state %u", packet[0], sm_conn->sm_engine_state);
3984 break;
3985 }
3986
3137}
3138
3139static void sm_handle_random_result_er(void *arg){
3140 sm_persistent_keys_random_active = 0;
3141 if (arg){
3142 // key generated, store in tlv
3143 int status = sm_tlv_impl->store_tag(sm_tlv_context, BTSTACK_TAG32('S','M','E','R'), sm_persistent_er, 16u);
3144 log_info("Generated ER key. Store in TLV status: %d", status);

--- 847 unchanged lines hidden (view full) ---

3992 }
3993 break;
3994 default:
3995 // Unexpected PDU
3996 log_info("Unexpected PDU %u in state %u", packet[0], sm_conn->sm_engine_state);
3997 break;
3998 }
3999
3987 // try to send preparared packet
3988 sm_run();
4000 // try to send next pdu
4001 sm_trigger_run();
3989}
3990
3991// Security Manager Client API
3992void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data)){
3993 sm_get_oob_data = get_oob_data_callback;
3994}
3995
3996void 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)){

--- 52 unchanged lines hidden (view full) ---

4049 test_use_fixed_local_csrk = true;
4050}
4051
4052#ifdef ENABLE_LE_SECURE_CONNECTIONS
4053static void sm_ec_generated(void * arg){
4054 UNUSED(arg);
4055 ec_key_generation_state = EC_KEY_GENERATION_DONE;
4056 // trigger pairing if pending for ec key
4002}
4003
4004// Security Manager Client API
4005void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data)){
4006 sm_get_oob_data = get_oob_data_callback;
4007}
4008
4009void 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)){

--- 52 unchanged lines hidden (view full) ---

4062 test_use_fixed_local_csrk = true;
4063}
4064
4065#ifdef ENABLE_LE_SECURE_CONNECTIONS
4066static void sm_ec_generated(void * arg){
4067 UNUSED(arg);
4068 ec_key_generation_state = EC_KEY_GENERATION_DONE;
4069 // trigger pairing if pending for ec key
4057 sm_run();
4070 sm_trigger_run();
4058}
4059static void sm_ec_generate_new_key(void){
4060 log_info("sm: generate new ec key");
4061 ec_key_generation_state = EC_KEY_GENERATION_ACTIVE;
4062 btstack_crypto_ecc_p256_generate_key(&sm_crypto_ecc_p256_request, ec_q, &sm_ec_generated, NULL);
4063}
4064#endif
4065

--- 67 unchanged lines hidden (view full) ---

4133 return &hci_con->sm_connection;
4134}
4135
4136static void sm_send_security_request_for_connection(sm_connection_t * sm_conn){
4137 switch (sm_conn->sm_engine_state){
4138 case SM_GENERAL_IDLE:
4139 case SM_RESPONDER_IDLE:
4140 sm_conn->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST;
4071}
4072static void sm_ec_generate_new_key(void){
4073 log_info("sm: generate new ec key");
4074 ec_key_generation_state = EC_KEY_GENERATION_ACTIVE;
4075 btstack_crypto_ecc_p256_generate_key(&sm_crypto_ecc_p256_request, ec_q, &sm_ec_generated, NULL);
4076}
4077#endif
4078

--- 67 unchanged lines hidden (view full) ---

4146 return &hci_con->sm_connection;
4147}
4148
4149static void sm_send_security_request_for_connection(sm_connection_t * sm_conn){
4150 switch (sm_conn->sm_engine_state){
4151 case SM_GENERAL_IDLE:
4152 case SM_RESPONDER_IDLE:
4153 sm_conn->sm_engine_state = SM_RESPONDER_SEND_SECURITY_REQUEST;
4141 sm_run();
4154 sm_trigger_run();
4142 break;
4143 default:
4144 break;
4145 }
4146}
4147
4148/**
4149 * @brief Trigger Security Request

--- 38 unchanged lines hidden (view full) ---

4188 log_info("irk lookup pending");
4189 sm_conn->sm_pairing_requested = 1;
4190 break;
4191 }
4192 } else if (sm_conn->sm_engine_state == SM_GENERAL_IDLE){
4193 sm_conn->sm_pairing_requested = 1;
4194 }
4195 }
4155 break;
4156 default:
4157 break;
4158 }
4159}
4160
4161/**
4162 * @brief Trigger Security Request

--- 38 unchanged lines hidden (view full) ---

4201 log_info("irk lookup pending");
4202 sm_conn->sm_pairing_requested = 1;
4203 break;
4204 }
4205 } else if (sm_conn->sm_engine_state == SM_GENERAL_IDLE){
4206 sm_conn->sm_pairing_requested = 1;
4207 }
4208 }
4196 sm_run();
4209 sm_trigger_run();
4197}
4198
4199// called by client app on authorization request
4200void sm_authorization_decline(hci_con_handle_t con_handle){
4201 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4202 if (!sm_conn) return; // wrong connection
4203 sm_conn->sm_connection_authorization_state = AUTHORIZATION_DECLINED;
4204 sm_notify_client_status(SM_EVENT_AUTHORIZATION_RESULT, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, 0);

--- 33 unchanged lines hidden (view full) ---

4238 case OOB:
4239 sm_pairing_error(sm_conn, SM_REASON_UNSPECIFIED_REASON);
4240 break;
4241 }
4242 break;
4243 default:
4244 break;
4245 }
4210}
4211
4212// called by client app on authorization request
4213void sm_authorization_decline(hci_con_handle_t con_handle){
4214 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4215 if (!sm_conn) return; // wrong connection
4216 sm_conn->sm_connection_authorization_state = AUTHORIZATION_DECLINED;
4217 sm_notify_client_status(SM_EVENT_AUTHORIZATION_RESULT, sm_conn->sm_handle, sm_conn->sm_peer_addr_type, sm_conn->sm_peer_address, 0);

--- 33 unchanged lines hidden (view full) ---

4251 case OOB:
4252 sm_pairing_error(sm_conn, SM_REASON_UNSPECIFIED_REASON);
4253 break;
4254 }
4255 break;
4256 default:
4257 break;
4258 }
4246 sm_run();
4259 sm_trigger_run();
4247}
4248
4249void sm_just_works_confirm(hci_con_handle_t con_handle){
4250 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4251 if (!sm_conn) return; // wrong connection
4252 setup->sm_user_response = SM_USER_RESPONSE_CONFIRM;
4253 if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){
4254 if (setup->sm_use_secure_connections){

--- 4 unchanged lines hidden (view full) ---

4259 }
4260
4261#ifdef ENABLE_LE_SECURE_CONNECTIONS
4262 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
4263 sm_sc_prepare_dhkey_check(sm_conn);
4264 }
4265#endif
4266
4260}
4261
4262void sm_just_works_confirm(hci_con_handle_t con_handle){
4263 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4264 if (!sm_conn) return; // wrong connection
4265 setup->sm_user_response = SM_USER_RESPONSE_CONFIRM;
4266 if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){
4267 if (setup->sm_use_secure_connections){

--- 4 unchanged lines hidden (view full) ---

4272 }
4273
4274#ifdef ENABLE_LE_SECURE_CONNECTIONS
4275 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
4276 sm_sc_prepare_dhkey_check(sm_conn);
4277 }
4278#endif
4279
4267 sm_run();
4280 sm_trigger_run();
4268}
4269
4270void sm_numeric_comparison_confirm(hci_con_handle_t con_handle){
4271 // for now, it's the same
4272 sm_just_works_confirm(con_handle);
4273}
4274
4275void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey){

--- 7 unchanged lines hidden (view full) ---

4283 }
4284#ifdef ENABLE_LE_SECURE_CONNECTIONS
4285 (void)memcpy(setup->sm_ra, setup->sm_tk, 16);
4286 (void)memcpy(setup->sm_rb, setup->sm_tk, 16);
4287 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
4288 sm_sc_start_calculating_local_confirm(sm_conn);
4289 }
4290#endif
4281}
4282
4283void sm_numeric_comparison_confirm(hci_con_handle_t con_handle){
4284 // for now, it's the same
4285 sm_just_works_confirm(con_handle);
4286}
4287
4288void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey){

--- 7 unchanged lines hidden (view full) ---

4296 }
4297#ifdef ENABLE_LE_SECURE_CONNECTIONS
4298 (void)memcpy(setup->sm_ra, setup->sm_tk, 16);
4299 (void)memcpy(setup->sm_rb, setup->sm_tk, 16);
4300 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
4301 sm_sc_start_calculating_local_confirm(sm_conn);
4302 }
4303#endif
4291 sm_run();
4304 sm_trigger_run();
4292}
4293
4294void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action){
4295 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4296 if (!sm_conn) return; // wrong connection
4297 if (action > SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED) return;
4298 uint8_t num_actions = setup->sm_keypress_notification >> 5;
4299 uint8_t flags = setup->sm_keypress_notification & 0x1fu;

--- 31 unchanged lines hidden (view full) ---

4331 }
4332 num_actions++;
4333 flags |= (1u << SM_KEYPRESS_PASSKEY_DIGIT_ERASED);
4334 break;
4335 default:
4336 break;
4337 }
4338 setup->sm_keypress_notification = (num_actions << 5) | flags;
4305}
4306
4307void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action){
4308 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
4309 if (!sm_conn) return; // wrong connection
4310 if (action > SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED) return;
4311 uint8_t num_actions = setup->sm_keypress_notification >> 5;
4312 uint8_t flags = setup->sm_keypress_notification & 0x1fu;

--- 31 unchanged lines hidden (view full) ---

4344 }
4345 num_actions++;
4346 flags |= (1u << SM_KEYPRESS_PASSKEY_DIGIT_ERASED);
4347 break;
4348 default:
4349 break;
4350 }
4351 setup->sm_keypress_notification = (num_actions << 5) | flags;
4339 sm_run();
4352 sm_trigger_run();
4340}
4341
4342#ifdef ENABLE_LE_SECURE_CONNECTIONS
4343static void sm_handle_random_result_oob(void * arg){
4344 UNUSED(arg);
4345 sm_sc_oob_state = SM_SC_OOB_W2_CALC_CONFIRM;
4353}
4354
4355#ifdef ENABLE_LE_SECURE_CONNECTIONS
4356static void sm_handle_random_result_oob(void * arg){
4357 UNUSED(arg);
4358 sm_sc_oob_state = SM_SC_OOB_W2_CALC_CONFIRM;
4346 sm_run();
4359 sm_trigger_run();
4347}
4348uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value)){
4349
4350 static btstack_crypto_random_t sm_crypto_random_oob_request;
4351
4352 if (sm_sc_oob_state != SM_SC_OOB_IDLE) return ERROR_CODE_COMMAND_DISALLOWED;
4353 sm_sc_oob_callback = callback;
4354 sm_sc_oob_state = SM_SC_OOB_W4_RANDOM;

--- 63 unchanged lines hidden (view full) ---

4418 gap_random_address_update_stop();
4419 gap_random_address_update_start();
4420}
4421
4422void gap_random_address_set(const bd_addr_t addr){
4423 gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_STATIC);
4424 (void)memcpy(sm_random_address, addr, 6);
4425 rau_state = RAU_SET_ADDRESS;
4360}
4361uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value)){
4362
4363 static btstack_crypto_random_t sm_crypto_random_oob_request;
4364
4365 if (sm_sc_oob_state != SM_SC_OOB_IDLE) return ERROR_CODE_COMMAND_DISALLOWED;
4366 sm_sc_oob_callback = callback;
4367 sm_sc_oob_state = SM_SC_OOB_W4_RANDOM;

--- 63 unchanged lines hidden (view full) ---

4431 gap_random_address_update_stop();
4432 gap_random_address_update_start();
4433}
4434
4435void gap_random_address_set(const bd_addr_t addr){
4436 gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_STATIC);
4437 (void)memcpy(sm_random_address, addr, 6);
4438 rau_state = RAU_SET_ADDRESS;
4426 sm_run();
4439 sm_trigger_run();
4427}
4428
4429#ifdef ENABLE_LE_PERIPHERAL
4430/*
4431 * @brief Set Advertisement Paramters
4432 * @param adv_int_min
4433 * @param adv_int_max
4434 * @param adv_type

--- 49 unchanged lines hidden ---
4440}
4441
4442#ifdef ENABLE_LE_PERIPHERAL
4443/*
4444 * @brief Set Advertisement Paramters
4445 * @param adv_int_min
4446 * @param adv_int_max
4447 * @param adv_type

--- 49 unchanged lines hidden ---