Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 22c8a95a authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

SMP_Encrypt refactor

* key must be 16bit, so no need to pass it's length, and check it's
  value
* p_out being null is a bad call that should crash to be catched, not an
  error we should handle
* hash computation always succeed, don't return any errors

Test: existing unit tests cover it, i.e. AesCmacTest
Change-Id: Ifba8902f3f1a7be3c54dd7a33d9ff1e52ffd237b
parent 4aff08bf
Loading
Loading
Loading
Loading
+39 −52
Original line number Diff line number Diff line
@@ -44,9 +44,6 @@
#include "osi/include/osi.h"
#include "smp_api.h"

extern bool aes_cipher_msg_auth_code(BT_OCTET16 key, uint8_t* input,
                                     uint16_t length, uint16_t tlen,
                                     uint8_t* p_signature);
extern void gatt_notify_phy_updated(uint8_t status, uint16_t handle,
                                    uint8_t tx_phy, uint8_t rx_phy);
extern void btm_ble_advertiser_notify_terminated_legacy(
@@ -2177,11 +2174,12 @@ bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text,
  tBTM_SEC_DEV_REC* p_rec = btm_find_dev(bd_addr);

  BTM_TRACE_DEBUG("%s", __func__);
  bool ret = false;
  if (p_rec == NULL) {
    BTM_TRACE_ERROR("%s-data signing can not be done from unknown device",
                    __func__);
  } else {
    return false;
  }

  uint8_t* p_mac = (uint8_t*)signature;
  uint8_t* pp;
  uint8_t* p_buf = (uint8_t*)osi_malloc(len + 4);
@@ -2197,12 +2195,9 @@ bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text,
  UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
  UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);

    ret = aes_cipher_msg_auth_code(p_rec->ble.keys.lcsrk, p_buf,
                                   (uint16_t)(len + 4), BTM_CMAC_TLEN_SIZE,
                                   p_mac);
    if (ret) {
  aes_cipher_msg_auth_code(p_rec->ble.keys.lcsrk, p_buf, (uint16_t)(len + 4),
                           BTM_CMAC_TLEN_SIZE, p_mac);
  btm_ble_increment_sign_ctr(bd_addr, true);
    }

  BTM_TRACE_DEBUG("%s p_mac = %d", __func__, p_mac);
  BTM_TRACE_DEBUG(
@@ -2214,8 +2209,7 @@ bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text,
      "0x%02x",
      *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
  osi_free(p_buf);
  }
  return ret;
  return true;
}

/*******************************************************************************
@@ -2249,14 +2243,13 @@ bool BTM_BleVerifySignature(const RawAddress& bd_addr, uint8_t* p_orig,
    BTM_TRACE_DEBUG("%s rcv_cnt=%d >= expected_cnt=%d", __func__, counter,
                    p_rec->ble.keys.counter);

    if (aes_cipher_msg_auth_code(p_rec->ble.keys.pcsrk, p_orig, len,
                                 BTM_CMAC_TLEN_SIZE, p_mac)) {
    aes_cipher_msg_auth_code(p_rec->ble.keys.pcsrk, p_orig, len,
                             BTM_CMAC_TLEN_SIZE, p_mac);
    if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0) {
      btm_ble_increment_sign_ctr(bd_addr, false);
      verified = true;
    }
  }
  }
  return verified;
}

@@ -2476,7 +2469,6 @@ static void btm_ble_process_irk(tSMP_ENC* p) {
 ******************************************************************************/
static void btm_ble_process_dhk(tSMP_ENC* p) {
  uint8_t btm_ble_irk_pt = 0x01;
  tSMP_ENC output;

  BTM_TRACE_DEBUG("btm_ble_process_dhk");

@@ -2484,14 +2476,10 @@ static void btm_ble_process_dhk(tSMP_ENC* p) {
    memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
    BTM_TRACE_DEBUG("BLE DHK generated.");

    tSMP_ENC output;
    /* IRK = D1(IR, 1) */
    if (!SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_irk_pt,
                     1, &output)) {
      /* reset all identity root related key */
      memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
    } else {
    SMP_Encrypt(btm_cb.devcb.id_keys.ir, &btm_ble_irk_pt, 1, &output);
    btm_ble_process_irk(&output);
    }
  } else {
    /* reset all identity root related key */
    memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
@@ -2525,8 +2513,7 @@ void btm_ble_reset_id(void) {
      memcpy(&btm_cb.devcb.id_keys.ir[8], rand, BT_OCTET8_LEN);
      /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */

      SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt, 1,
                  &output);
      SMP_Encrypt(btm_cb.devcb.id_keys.ir, &btm_ble_dhk_pt, 1, &output);
      btm_ble_process_dhk(&output);

      BTM_TRACE_DEBUG("BLE IR generated.");
+4 −8
Original line number Diff line number Diff line
@@ -94,13 +94,9 @@ void btm_gen_resolve_paddr_low(BT_OCTET8 rand) {
  p_cb->private_addr.address[0] = rand[2];

  /* encrypt with ur IRK */
  if (!SMP_Encrypt(btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN, rand, 3,
                   &output)) {
    btm_gen_resolve_paddr_cmpl(NULL);
  } else {
  SMP_Encrypt(btm_cb.devcb.id_keys.irk, rand, 3, &output);
  btm_gen_resolve_paddr_cmpl(&output);
}
}
/*******************************************************************************
 *
 * Function         btm_gen_resolvable_private_addr
@@ -244,7 +240,7 @@ bool btm_ble_addr_resolvable(const RawAddress& rpa,
    rand[2] = rpa.address[0];

    /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
    SMP_Encrypt(p_dev_rec->ble.keys.irk, BT_OCTET16_LEN, &rand[0], 3, &output);
    SMP_Encrypt(p_dev_rec->ble.keys.irk, &rand[0], 3, &output);

    rand[0] = rpa.address[5];
    rand[1] = rpa.address[4];
@@ -292,7 +288,7 @@ static bool btm_ble_match_random_bda(void* data, void* context) {
    return true;

  /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
  SMP_Encrypt(p_dev_rec->ble.keys.irk, BT_OCTET16_LEN, &rand[0], 3, &output);
  SMP_Encrypt(p_dev_rec->ble.keys.irk, &rand[0], 3, &output);
  // if it was match, finish iteration, otherwise continue
  return !btm_ble_proc_resolve_x(output, *random_bda);
}
+1 −2
Original line number Diff line number Diff line
@@ -204,8 +204,7 @@ class BleAdvertisingManagerImpl
    BTM_GetDeviceIDRoot(irk);
    tSMP_ENC output;

    if (!SMP_Encrypt(irk, BT_OCTET16_LEN, rand, 3, &output))
      LOG_ASSERT(false) << "SMP_Encrypt failed";
    SMP_Encrypt(irk, rand, 3, &output);

    /* set hash to be LSB of rpAddress */
    bda.address[5] = output.param_buf[0];
+5 −6
Original line number Diff line number Diff line
@@ -180,8 +180,8 @@ extern void SMP_SecureConnectionOobDataReply(uint8_t* p_data);
 *
 * Description      Encrypt the data with the specified key.
 *
 * Parameters:      key                 - Pointer to key key[0] conatins the MSB
 *                  key_len             - key length
 * Parameters:      key                 - Pointer to key key conatins the MSB,
 *                                        must be 128bit
 *                  plain_text          - Pointer to data to be encrypted
 *                                        plain_text[0] conatins the MSB
 *                  pt_len              - plain text length
@@ -189,8 +189,8 @@ extern void SMP_SecureConnectionOobDataReply(uint8_t* p_data);
 *
 *  Returns         Boolean - true: encryption is successful
 ******************************************************************************/
extern bool SMP_Encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
                        uint8_t pt_len, tSMP_ENC* p_out);
extern void SMP_Encrypt(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
                        tSMP_ENC* p_out);

/*******************************************************************************
 *
@@ -234,9 +234,8 @@ extern void smp_link_encrypted(const RawAddress& bda, uint8_t encr_enable);
// |length| - length of the input in byte.
// |tlen| - lenth of mac desired
// |p_signature| - data pointer to where signed data to be stored, tlen long.
// Returns false if out of resources, true in other cases.
//
bool aes_cipher_msg_auth_code(BT_OCTET16 key, uint8_t* input, uint16_t length,
void aes_cipher_msg_auth_code(BT_OCTET16 key, uint8_t* input, uint16_t length,
                              uint16_t tlen, uint8_t* p_signature);

#endif /* SMP_API_H */
+6 −10
Original line number Diff line number Diff line
@@ -489,22 +489,18 @@ void SMP_SecureConnectionOobDataReply(uint8_t* p_data) {
 * Description      This function is called to encrypt the data with the
 *                  specified key
 *
 * Parameters:      key                 - Pointer to key key[0] conatins the MSB
 *                  key_len             - key length
 * Parameters:      key                 - Pointer to key key conatins the MSB,
 *                                        must be 128bit
 *                  plain_text          - Pointer to data to be encrypted
 *                                        plain_text[0] conatins the MSB
 *                  pt_len              - plain text length
 *                  p_out               - output of the encrypted texts
 *
 *  Returns         Boolean - request is successful
 ******************************************************************************/
bool SMP_Encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
                 uint8_t pt_len, tSMP_ENC* p_out)
void SMP_Encrypt(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
                 tSMP_ENC* p_out)

{
  bool status = false;
  status = smp_encrypt_data(key, key_len, plain_text, pt_len, p_out);
  return status;
  smp_encrypt_data(key, plain_text, pt_len, p_out);
}

/*******************************************************************************
Loading