reused c1 calculation for initiator role

This commit is contained in:
matthias.ringwald 2014-06-13 11:03:05 +00:00
parent 8d6bb42426
commit 6d85ed58f1

View File

@ -66,6 +66,11 @@ typedef enum {
SM_STATE_PH2_C1_GET_RANDOM_B, SM_STATE_PH2_C1_GET_RANDOM_B,
SM_STATE_PH2_C1_W4_RANDOM_B, SM_STATE_PH2_C1_W4_RANDOM_B,
SM_STATE_PH2_C1_GET_ENC_A,
SM_STATE_PH2_C1_W4_ENC_A,
SM_STATE_PH2_C1_GET_ENC_B,
SM_STATE_PH2_C1_W4_ENC_B,
// SLAVE ROLE // SLAVE ROLE
SM_STATE_SEND_SECURITY_REQUEST, SM_STATE_SEND_SECURITY_REQUEST,
@ -82,10 +87,6 @@ typedef enum {
// Phase 2: Authenticating and Encrypting // Phase 2: Authenticating and Encrypting
// calculate confirm values for local and remote connection // calculate confirm values for local and remote connection
SM_STATE_PH2_C1_GET_ENC_A,
SM_STATE_PH2_C1_W4_ENC_A,
SM_STATE_PH2_C1_GET_ENC_B,
SM_STATE_PH2_C1_W4_ENC_B,
SM_STATE_PH2_C1_SEND_PAIRING_CONFIRM, SM_STATE_PH2_C1_SEND_PAIRING_CONFIRM,
SM_STATE_PH2_W4_PAIRING_RANDOM, SM_STATE_PH2_W4_PAIRING_RANDOM,
SM_STATE_PH2_C1_GET_ENC_C, SM_STATE_PH2_C1_GET_ENC_C,
@ -131,10 +132,7 @@ typedef enum {
SM_STATE_INITIATOR_W4_PAIRING_RESPONSE, SM_STATE_INITIATOR_W4_PAIRING_RESPONSE,
// PH2 // PH2
SM_STATE_INITIATOR_PH2_C1_GET_RANDOM_A, SM_STATE_INITIATOR_PH2_W4_PAIRING_CONFIRM,
SM_STATE_INITIATOR_PH2_C1_GET_ENC_A,
SM_STATE_INITIATOR_PH1_SEND_PAIRING_CONFIRM,
} security_manager_state_t; } security_manager_state_t;
@ -1054,7 +1052,11 @@ static void sm_run(void){
// already busy? // already busy?
if (sm_aes128_state == SM_AES128_ACTIVE) break; if (sm_aes128_state == SM_AES128_ACTIVE) break;
// calculate s_confirm using aes128 engine - step 1 // calculate s_confirm using aes128 engine - step 1
sm_c1_t1(setup->sm_s_random, (uint8_t*) &setup->sm_m_preq, (uint8_t*) &setup->sm_s_pres, setup->sm_m_addr_type, setup->sm_s_addr_type, plaintext); if (connection->sm_role){
sm_c1_t1(setup->sm_s_random, (uint8_t*) &setup->sm_m_preq, (uint8_t*) &setup->sm_s_pres, setup->sm_m_addr_type, setup->sm_s_addr_type, plaintext);
} else {
sm_c1_t1(setup->sm_m_random, (uint8_t*) &setup->sm_m_preq, (uint8_t*) &setup->sm_s_pres, setup->sm_m_addr_type, setup->sm_s_addr_type, plaintext);
}
sm_aes128_start(setup->sm_tk, plaintext); sm_aes128_start(setup->sm_tk, plaintext);
sm_next_responding_state(); sm_next_responding_state();
break; break;
@ -1078,10 +1080,18 @@ static void sm_run(void){
case SM_STATE_PH2_C1_SEND_PAIRING_CONFIRM: { case SM_STATE_PH2_C1_SEND_PAIRING_CONFIRM: {
uint8_t buffer[17]; uint8_t buffer[17];
buffer[0] = SM_CODE_PAIRING_CONFIRM; buffer[0] = SM_CODE_PAIRING_CONFIRM;
swap128(setup->sm_s_confirm, &buffer[1]); if (connection->sm_role){
swap128(setup->sm_s_confirm, &buffer[1]);
} else {
swap128(setup->sm_m_confirm, &buffer[1]);
}
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(); sm_2timeout_reset();
connection->sm_state_responding = SM_STATE_PH2_W4_PAIRING_RANDOM; if (connection->sm_role){
connection->sm_state_responding = SM_STATE_PH2_W4_PAIRING_RANDOM;
} else {
connection->sm_state_responding = SM_STATE_INITIATOR_PH2_W4_PAIRING_CONFIRM;
}
return; return;
} }
case SM_STATE_PH2_SEND_STK: { case SM_STATE_PH2_SEND_STK: {
@ -1251,9 +1261,14 @@ static void sm_handle_encryption_result(uint8_t * data){
sm_next_responding_state(); sm_next_responding_state();
return; return;
case SM_STATE_PH2_C1_W4_ENC_B: case SM_STATE_PH2_C1_W4_ENC_B:
swap128(data, setup->sm_s_confirm); if (connection->sm_role){
print_key("c1!", setup->sm_s_confirm); swap128(data, setup->sm_s_confirm);
sm_next_responding_state(); print_key("c1!", setup->sm_s_confirm);
} else {
swap128(data, setup->sm_m_confirm);
print_key("c1!", setup->sm_m_confirm);
}
connection->sm_state_responding = SM_STATE_PH2_C1_SEND_PAIRING_CONFIRM;
return; return;
case SM_STATE_PH2_C1_W4_ENC_D: case SM_STATE_PH2_C1_W4_ENC_D:
{ {
@ -1362,7 +1377,7 @@ static void sm_handle_random_result(uint8_t * data){
if (connection->sm_role){ if (connection->sm_role){
connection->sm_state_responding = SM_STATE_PH1_SEND_PAIRING_RESPONSE; connection->sm_state_responding = SM_STATE_PH1_SEND_PAIRING_RESPONSE;
} else { } else {
connection->sm_state_responding = SM_STATE_INITIATOR_PH2_C1_GET_RANDOM_A; connection->sm_state_responding = SM_STATE_PH2_C1_GET_RANDOM_A;
} }
return; return;
} }
@ -1377,11 +1392,10 @@ static void sm_handle_random_result(uint8_t * data){
case SM_STATE_PH2_C1_W4_RANDOM_B: case SM_STATE_PH2_C1_W4_RANDOM_B:
if (connection->sm_role){ if (connection->sm_role){
memcpy(&setup->sm_s_random[8], data, 8); // random endinaness memcpy(&setup->sm_s_random[8], data, 8); // random endinaness
connection->sm_state_responding = SM_STATE_PH2_C1_GET_ENC_A;
} else { } else {
memcpy(&setup->sm_m_random[8], data, 8); // random endinaness memcpy(&setup->sm_m_random[8], data, 8); // random endinaness
connection->sm_state_responding = SM_STATE_INITIATOR_PH2_C1_GET_ENC_A;
} }
connection->sm_state_responding = SM_STATE_PH2_C1_GET_ENC_A;
return; return;
case SM_STATE_PH3_W4_RANDOM: case SM_STATE_PH3_W4_RANDOM:
swap64(data, setup->sm_s_rand); swap64(data, setup->sm_s_rand);
@ -1663,7 +1677,7 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac
break; break;
} }
connection->sm_state_responding = SM_STATE_INITIATOR_PH1_SEND_PAIRING_CONFIRM; connection->sm_state_responding = SM_STATE_PH2_C1_GET_RANDOM_A;
break; break;
// Responder // Responder