static void gzp_process_id_req(uint8_t* rx_payload) { uint8_t temp_host_id[GZP_HOST_ID_LENGTH]; if(gzp_pairing_enabled_f) { if(!gzp_id_req_received()) { gzp_crypt_set_session_token(&rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN]); gzp_id_req_stat = GZP_ID_REQ_PENDING; } // If host ID not generated yet if(!gzp_get_host_id(temp_host_id)) { // Generate new host ID from "session counter" and received "session token" gzp_get_session_counter(temp_host_id); if(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH) { gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_SESSION_TOKEN_LENGTH); } else { gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_HOST_ID_LENGTH); } gzp_set_host_id(temp_host_id); } } }
static void gzp_process_id_req(uint8_t* rx_payload) { int i; uint8_t temp_host_id[GZP_HOST_ID_LENGTH]; if(gzp_pairing_enabled_f) { if(!gzp_id_req_received()) { gzp_crypt_set_session_token(&rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN]); gzp_id_req_stat = GZP_ID_REQ_PENDING; } gzp_get_host_id(temp_host_id); // Added: for(i = 0; i < GZP_HOST_ID_LENGTH; i++) { if(temp_host_id[i] != 0xFF) { break; } } if(i == GZP_HOST_ID_LENGTH) // If host not generated yet { gzp_get_session_counter(temp_host_id); //lint -save -e506 Constantvalue Boolean if(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH) { //lint -restore gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_SESSION_TOKEN_LENGTH); } else { gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_HOST_ID_LENGTH); } gzp_set_host_id(temp_host_id); } } }
void gzp_crypt(uint8_t* dst, const uint8_t* src, uint8_t length) { uint8_t i; uint8_t key[16]; uint8_t iv[16]; // Build AES key based on "gzp_key_select" switch(gzp_key_select) { case GZP_ID_EXCHANGE: memcpy(key, (void const*)gzp_secret_key, 16); break; case GZP_KEY_EXCHANGE: memcpy(key, (void const*)gzp_secret_key, 16); gzp_get_host_id(key); break; case GZP_DATA_EXCHANGE: memcpy(key, (void const*)gzp_secret_key, 16); memcpy(key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH); break; default: return; } // Build init vector from "gzp_session_token" for(i = 0; i < 16; i++) { if(i < GZP_SESSION_TOKEN_LENGTH) { iv[i] = gzp_session_token[i]; } else { iv[i] = 0; } } // Set up hal_aes using new key and init vector (void)nrf_ecb_init(); nrf_ecb_set_key(key); //hal_aes_setup(false, ECB, key, NULL); // Note, here we skip the IV as we use ECB mode // Encrypt IV using ECB mode (void)nrf_ecb_crypt(iv, iv); // Encrypt data by XOR'ing with AES output gzp_xor_cipher(dst, src, iv, length); }