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

Commit 5cd984db authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "SMP_Encrypt - convert p_out into raw array"

parents 0942ee49 0b04ae6a
Loading
Loading
Loading
Loading
+23 −50
Original line number Diff line number Diff line
@@ -2418,20 +2418,11 @@ static void btm_notify_new_key(uint8_t key_type) {
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_process_irk
 *
 * Description      This function is called when IRK is generated, store it in
 *                  local control block.
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_ble_process_irk(tSMP_ENC* p) {
/** This function is called when IRK is generated, store it in local control
 * block */
static void btm_ble_process_irk(BT_OCTET16 p) {
  BTM_TRACE_DEBUG("btm_ble_process_irk");
  if (p && p->opcode == HCI_BLE_ENCRYPT) {
    memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
  memcpy(btm_cb.devcb.id_keys.irk, p, BT_OCTET16_LEN);
  btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);

#if (BLE_PRIVACY_SPT == TRUE)
@@ -2440,9 +2431,6 @@ static void btm_ble_process_irk(tSMP_ENC* p) {
    btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
  }
#endif
  } else {
    BTM_TRACE_ERROR("Generating IRK exception.");
  }

  /* proceed generate ER */
  btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand1) {
@@ -2456,34 +2444,19 @@ static void btm_ble_process_irk(tSMP_ENC* p) {
  }));
}

/*******************************************************************************
 *
 * Function         btm_ble_process_dhk
 *
 * Description      This function is called when DHK is calculated, store it in
 *                  local control block, and proceed to generate ER, a 128-bits
 *                  random number.
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_ble_process_dhk(tSMP_ENC* p) {
  uint8_t btm_ble_irk_pt = 0x01;

  BTM_TRACE_DEBUG("btm_ble_process_dhk");
/** This function is called when DHK is calculated, store it in local control
 * block, and proceed to generate ER, a 128-bits random number. */
static void btm_ble_process_dhk(BT_OCTET16 p) {
  BTM_TRACE_DEBUG("%s", __func__);

  if (p && p->opcode == HCI_BLE_ENCRYPT) {
    memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
  memcpy(btm_cb.devcb.id_keys.dhk, p, BT_OCTET16_LEN);
  BTM_TRACE_DEBUG("BLE DHK generated.");

    tSMP_ENC output;
  uint8_t btm_ble_irk_pt = 0x01;
  /* IRK = D1(IR, 1) */
    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));
  }
  BT_OCTET16 output;
  SMP_Encrypt(btm_cb.devcb.id_keys.ir, &btm_ble_irk_pt, 1, output);
  btm_ble_process_irk(output);
}

/*******************************************************************************
@@ -2505,7 +2478,6 @@ void btm_ble_reset_id(void) {

    btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand) {
      uint8_t btm_ble_dhk_pt = 0x03;
      tSMP_ENC output;

      BTM_TRACE_DEBUG("btm_ble_process_ir2");

@@ -2513,8 +2485,9 @@ 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, &btm_ble_dhk_pt, 1, &output);
      btm_ble_process_dhk(&output);
      BT_OCTET16 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.");
    }));
+27 −49
Original line number Diff line number Diff line
@@ -35,25 +35,15 @@
#include "btm_ble_int.h"
#include "smp_api.h"

/*******************************************************************************
 *
 * Function         btm_gen_resolve_paddr_cmpl
 *
 * Description      This is callback functioin when resolvable private address
 *                  generation is complete.
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_gen_resolve_paddr_cmpl(tSMP_ENC* p) {
/** callback - resolvable private address generation is complete */
static void btm_gen_resolve_paddr_cmpl(BT_OCTET16 p) {
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  BTM_TRACE_EVENT("btm_gen_resolve_paddr_cmpl");
  BTM_TRACE_EVENT("%s", __func__);

  if (p) {
  /* set hash to be LSB of rpAddress */
    p_cb->private_addr.address[5] = p->param_buf[0];
    p_cb->private_addr.address[4] = p->param_buf[1];
    p_cb->private_addr.address[3] = p->param_buf[2];
  p_cb->private_addr.address[5] = p[0];
  p_cb->private_addr.address[4] = p[1];
  p_cb->private_addr.address[3] = p[2];
  /* set it to controller */
  btm_ble_set_random_address(p_cb->private_addr);

@@ -66,10 +56,6 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC* p) {
#endif
  alarm_set_on_mloop(p_cb->refresh_raddr_timer, interval_ms,
                     btm_ble_refresh_raddr_timer_timeout, NULL);
  } else {
    /* random address set failure */
    BTM_TRACE_DEBUG("set random address failed");
  }
}
/*******************************************************************************
 *
@@ -83,7 +69,6 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC* p) {
 ******************************************************************************/
void btm_gen_resolve_paddr_low(BT_OCTET8 rand) {
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  tSMP_ENC output;

  BTM_TRACE_EVENT("btm_gen_resolve_paddr_low");
  rand[2] &= (~BLE_RESOLVE_ADDR_MASK);
@@ -94,8 +79,9 @@ void btm_gen_resolve_paddr_low(BT_OCTET8 rand) {
  p_cb->private_addr.address[0] = rand[2];

  /* encrypt with ur IRK */
  SMP_Encrypt(btm_cb.devcb.id_keys.irk, rand, 3, &output);
  btm_gen_resolve_paddr_cmpl(&output);
  BT_OCTET16 output;
  SMP_Encrypt(btm_cb.devcb.id_keys.irk, rand, 3, output);
  btm_gen_resolve_paddr_cmpl(output);
}
/*******************************************************************************
 *
@@ -165,17 +151,9 @@ void btm_gen_non_resolvable_private_addr(tBTM_BLE_ADDR_CBACK* p_cback,
/*******************************************************************************
 *  Utility functions for Random address resolving
 ******************************************************************************/
/*******************************************************************************
 *
 * Function         btm_ble_proc_resolve_x
 *
 * Description      This function compares the X with random address 3 MSO bytes
 *                  to find a match.
 *
 * Returns          true on match, false otherwise
 *
 ******************************************************************************/
static bool btm_ble_proc_resolve_x(const tSMP_ENC& encrypt_output,
/* This function compares the X with random address 3 MSO bytes to find a match
 */
static bool btm_ble_proc_resolve_x(const BT_OCTET16 encrypt_output,
                                   const RawAddress& random_bda) {
  BTM_TRACE_EVENT("btm_ble_proc_resolve_x");

@@ -185,7 +163,7 @@ static bool btm_ble_proc_resolve_x(const tSMP_ENC& encrypt_output,
  comp[1] = random_bda.address[4];
  comp[2] = random_bda.address[3];

  if (!memcmp(encrypt_output.param_buf, comp, 3)) {
  if (!memcmp(encrypt_output, comp, 3)) {
    BTM_TRACE_EVENT("match is found");
    return true;
  }
@@ -230,7 +208,6 @@ bool btm_ble_addr_resolvable(const RawAddress& rpa,
  if (!BTM_BLE_IS_RESOLVE_BDA(rpa)) return rt;

  uint8_t rand[3];
  tSMP_ENC output;
  if ((p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) &&
      (p_dev_rec->ble.key_type & BTM_LE_KEY_PID)) {
    BTM_TRACE_DEBUG("%s try to resolve", __func__);
@@ -240,13 +217,14 @@ 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, &rand[0], 3, &output);
    BT_OCTET16 output;
    SMP_Encrypt(p_dev_rec->ble.keys.irk, &rand[0], 3, output);

    rand[0] = rpa.address[5];
    rand[1] = rpa.address[4];
    rand[2] = rpa.address[3];

    if (!memcmp(output.param_buf, &rand[0], 3)) {
    if (!memcmp(output, &rand[0], 3)) {
      btm_ble_init_pseudo_addr(p_dev_rec, rpa);
      rt = true;
    }
@@ -277,7 +255,6 @@ static bool btm_ble_match_random_bda(void* data, void* context) {

  BTM_TRACE_EVENT("%s next iteration", __func__);

  tSMP_ENC output;
  tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);

  BTM_TRACE_DEBUG("sec_flags = %02x device_type = %d", p_dev_rec->sec_flags,
@@ -288,7 +265,8 @@ 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, &rand[0], 3, &output);
  BT_OCTET16 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);
}
+5 −5
Original line number Diff line number Diff line
@@ -202,14 +202,14 @@ class BleAdvertisingManagerImpl

    BT_OCTET16 irk;
    BTM_GetDeviceIDRoot(irk);
    tSMP_ENC output;

    SMP_Encrypt(irk, rand, 3, &output);
    BT_OCTET16 output;
    SMP_Encrypt(irk, rand, 3, output);

    /* set hash to be LSB of rpAddress */
    bda.address[5] = output.param_buf[0];
    bda.address[4] = output.param_buf[1];
    bda.address[3] = output.param_buf[2];
    bda.address[5] = output[0];
    bda.address[4] = output[1];
    bda.address[3] = output[2];

    cb.Run(bda);
  }
+5 −17
Original line number Diff line number Diff line
@@ -174,23 +174,11 @@ extern void SMP_OobDataReply(const RawAddress& bd_addr, tSMP_STATUS res,
 ******************************************************************************/
extern void SMP_SecureConnectionOobDataReply(uint8_t* p_data);

/*******************************************************************************
 *
 * Function         SMP_Encrypt
 *
 * Description      Encrypt the data with the specified key.
 *
 * 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               - pointer to the encrypted outputs
 *
 *  Returns         Boolean - true: encryption is successful
 ******************************************************************************/
extern void SMP_Encrypt(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
                        tSMP_ENC* p_out);
/* This function computes AES_128(key, message). |key| must be 128bit.
 * |message| can be at most 16 bytes long, it's length in bytes is given in
 * |length| */
extern void SMP_Encrypt(BT_OCTET16 key, uint8_t* message, uint8_t length,
                        BT_OCTET16 p_out);

/*******************************************************************************
 *
+6 −19
Original line number Diff line number Diff line
@@ -482,25 +482,12 @@ void SMP_SecureConnectionOobDataReply(uint8_t* p_data) {
  smp_sm_event(&smp_cb, SMP_SC_OOB_DATA_EVT, &smp_int_data);
}

/*******************************************************************************
 *
 * Function         SMP_Encrypt
 *
 * Description      This function is called to encrypt the data with the
 *                  specified key
 *
 * 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
 ******************************************************************************/
void SMP_Encrypt(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
                 tSMP_ENC* p_out)

{
  smp_encrypt_data(key, plain_text, pt_len, p_out);
/* This function computes AES_128(key, message). |key| must be 128bit.
 * |message| can be at most 16 bytes long, it's length in bytes is given in
 * |length| */
void SMP_Encrypt(BT_OCTET16 key, uint8_t* message, uint8_t length,
                 BT_OCTET16 p_out) {
  smp_encrypt_data(key, message, length, p_out);
}

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