sm.c (0b636a850b5aec69232e35b19e700474ed61460f) sm.c (0a1a2d88e7802e157f2233466f98e9131aef9b2f)
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

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

2916 if (connection == NULL) return;
2917
2918 log_info_key("ltk", setup->sm_ltk);
2919 // calc CSRK next
2920 sm_d1_d_prime(setup->sm_local_div, 1, sm_aes128_plaintext);
2921 sm_aes128_state = SM_AES128_ACTIVE;
2922 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);
2923}
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

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

2916 if (connection == NULL) return;
2917
2918 log_info_key("ltk", setup->sm_ltk);
2919 // calc CSRK next
2920 sm_d1_d_prime(setup->sm_local_div, 1, sm_aes128_plaintext);
2921 sm_aes128_state = SM_AES128_ACTIVE;
2922 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);
2923}
2924static bool sm_ctkd_from_le(void){
2924static bool sm_ctkd_from_le(sm_connection_t *sm_connection) {
2925#ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION
2926 // requirements to derive link key from LE:
2927 // - use secure connections
2928 if (setup->sm_use_secure_connections == 0) return false;
2929 // - bonding needs to be enabled:
2930 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;
2931 if (!bonding_enabled) return false;
2932 // - need identity address
2933 bool have_identity_address_info = ((setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION) != 0);
2934 if (!have_identity_address_info) return false;
2925#ifdef ENABLE_CROSS_TRANSPORT_KEY_DERIVATION
2926 // requirements to derive link key from LE:
2927 // - use secure connections
2928 if (setup->sm_use_secure_connections == 0) return false;
2929 // - bonding needs to be enabled:
2930 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;
2931 if (!bonding_enabled) return false;
2932 // - need identity address
2933 bool have_identity_address_info = ((setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION) != 0);
2934 if (!have_identity_address_info) return false;
2935 // - 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)
2936 // this requirement is motivated by BLURtooth paper. The paper recommends to not overwrite keys at all.
2937 // If SC is authenticated, we consider it safe to overwrite a stored key.
2938 // 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.
2939 uint8_t link_key[16];
2940 link_key_type_t link_key_type;
2941 bool have_link_key = gap_get_link_key_for_bd_addr(setup->sm_peer_address, link_key, &link_key_type);
2942 bool link_key_authenticated = gap_authenticated_for_link_key_type(link_key_type) != 0;
2943 bool derived_key_authenticated = sm_connection->sm_connection_authenticated != 0;
2944 if (have_link_key && link_key_authenticated && !derived_key_authenticated) {
2945 return false;
2946 }
2935 // get started (all of the above are true)
2936 return true;
2937#else
2938 return false;
2939#endif
2940}
2941
2942static void sm_handle_encryption_result_enc_csrk(void *arg){

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

2951 if (setup->sm_key_distribution_send_set){
2952 connection->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS;
2953 } else {
2954 // no keys to send, just continue
2955 if (IS_RESPONDER(connection->sm_role)){
2956 // slave -> receive master keys
2957 connection->sm_engine_state = SM_PH3_RECEIVE_KEYS;
2958 } else {
2947 // get started (all of the above are true)
2948 return true;
2949#else
2950 return false;
2951#endif
2952}
2953
2954static void sm_handle_encryption_result_enc_csrk(void *arg){

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

2963 if (setup->sm_key_distribution_send_set){
2964 connection->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS;
2965 } else {
2966 // no keys to send, just continue
2967 if (IS_RESPONDER(connection->sm_role)){
2968 // slave -> receive master keys
2969 connection->sm_engine_state = SM_PH3_RECEIVE_KEYS;
2970 } else {
2959 if (sm_ctkd_from_le()){
2971 if (sm_ctkd_from_le(connection)){
2960 connection->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
2961 } else {
2962 sm_master_pairing_success(connection);
2963 }
2964 }
2965 }
2966 sm_trigger_run();
2967}

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

4004 break;
4005 }
4006 // done with key distribution?
4007 if (sm_key_distribution_all_received(sm_conn)){
4008
4009 sm_key_distribution_handle_all_received(sm_conn);
4010
4011 if (IS_RESPONDER(sm_conn->sm_role)){
2972 connection->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
2973 } else {
2974 sm_master_pairing_success(connection);
2975 }
2976 }
2977 }
2978 sm_trigger_run();
2979}

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

4016 break;
4017 }
4018 // done with key distribution?
4019 if (sm_key_distribution_all_received(sm_conn)){
4020
4021 sm_key_distribution_handle_all_received(sm_conn);
4022
4023 if (IS_RESPONDER(sm_conn->sm_role)){
4012 if (sm_ctkd_from_le()){
4024 if (sm_ctkd_from_le(sm_conn)){
4013 sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
4014 } else {
4015 sm_conn->sm_engine_state = SM_RESPONDER_IDLE;
4016 sm_notify_client_status_reason(sm_conn, ERROR_CODE_SUCCESS, 0);
4017 sm_done_for_handle(sm_conn->sm_handle);
4018 }
4019 } else {
4020 if (setup->sm_use_secure_connections){

--- 512 unchanged lines hidden ---
4025 sm_conn->sm_engine_state = SM_SC_W2_CALCULATE_H6_ILK;
4026 } else {
4027 sm_conn->sm_engine_state = SM_RESPONDER_IDLE;
4028 sm_notify_client_status_reason(sm_conn, ERROR_CODE_SUCCESS, 0);
4029 sm_done_for_handle(sm_conn->sm_handle);
4030 }
4031 } else {
4032 if (setup->sm_use_secure_connections){

--- 512 unchanged lines hidden ---