uint32_t conn_mw_ecb_block_encrypt(uint8_t const * const p_rx_buf, uint32_t rx_buf_len, uint8_t * const p_tx_buf, uint32_t * const p_tx_buf_len) { SER_ASSERT_NOT_NULL(p_rx_buf); SER_ASSERT_NOT_NULL(p_tx_buf); SER_ASSERT_NOT_NULL(p_tx_buf_len); nrf_ecb_hal_data_t ecb_data; nrf_ecb_hal_data_t * p_ecb_data = &ecb_data; uint32_t err_code = NRF_SUCCESS; uint32_t sd_err_code; err_code = ecb_block_encrypt_req_dec(p_rx_buf, rx_buf_len, &p_ecb_data); SER_ASSERT(err_code == NRF_SUCCESS, err_code); sd_err_code = sd_ecb_block_encrypt(p_ecb_data); err_code = ecb_block_encrypt_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_ecb_data); SER_ASSERT(err_code == NRF_SUCCESS, err_code); return err_code; }
static void hash128(uint8_t* input, uint8_t len, uint8_t* output) { nrf_ecb_hal_data_t ecb_data; memcpy(ecb_data.key, default_key, 16); if (len>16) len=16; memcpy(ecb_data.cleartext, input, len); sd_ecb_block_encrypt(&ecb_data); memcpy(output, ecb_data.ciphertext, 16); }
/**@brief Function for calculating the ah() hash function described in Bluetooth core specification * 4.2 section 3.H.2.2.2. * * @detail BLE uses a hash function to calculate the first half of a resolvable address * from the second half of the address and an irk. This function will use the ECB * periferal to hash these data acording to the Bluetooth core specification. * * @note The ECB expect little endian input and output. * This function expect big endian and will reverse the data as necessary. * * @param[in] p_k The key used in the hash function. * For address resolution this is should be the irk. * The array must have a length of 16. * @param[in] p_r The rand used in the hash function. For generating a new address * this would be a random number. For resolving a resolvable address * this would be the last half of the address being resolved. * The array must have a length of 3. * @param[out] p_local_hash The result of the hash operation. For address resolution this * will match the first half of the address being resolved if and only * if the irk used in the hash function is the same one used to generate * the address. * The array must have a length of 16. */ void ah(uint8_t const * p_k, uint8_t const * p_r, uint8_t * p_local_hash) { ret_code_t err_code; nrf_ecb_hal_data_t ecb_hal_data; for (uint32_t i = 0; i < SOC_ECB_KEY_LENGTH; i++) { ecb_hal_data.key[i] = p_k[SOC_ECB_KEY_LENGTH - 1 - i]; } memset(ecb_hal_data.cleartext, 0, SOC_ECB_KEY_LENGTH - IM_ADDR_CLEARTEXT_LENGTH); for (uint32_t i = 0; i < IM_ADDR_CLEARTEXT_LENGTH; i++) { ecb_hal_data.cleartext[SOC_ECB_KEY_LENGTH - 1 - i] = p_r[i]; } err_code = sd_ecb_block_encrypt(&ecb_hal_data); // Can only return NRF_SUCCESS. UNUSED_VARIABLE(err_code); for (uint32_t i = 0; i < IM_ADDR_CIPHERTEXT_LENGTH; i++) { p_local_hash[i] = ecb_hal_data.ciphertext[SOC_ECB_KEY_LENGTH - 1 - i]; } }