OSDN Git Service

SMP_Encrypt refactor
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 14 Jun 2018 16:03:47 +0000 (09:03 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 14 Jun 2018 20:17:43 +0000 (13:17 -0700)
* 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

stack/btm/btm_ble.cc
stack/btm/btm_ble_addr.cc
stack/btm/btm_ble_multi_adv.cc
stack/include/smp_api.h
stack/smp/smp_api.cc
stack/smp/smp_cmac.cc
stack/smp/smp_int.h
stack/smp/smp_keys.cc
stack/smp/smp_utils.cc
stack/test/ble_advertiser_test.cc
stack/test/stack_smp_test.cc

index ca93e1a..ae98dc3 100644 (file)
@@ -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,45 +2174,42 @@ 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 {
-    uint8_t* p_mac = (uint8_t*)signature;
-    uint8_t* pp;
-    uint8_t* p_buf = (uint8_t*)osi_malloc(len + 4);
-
-    BTM_TRACE_DEBUG("%s-Start to generate Local CSRK", __func__);
-    pp = p_buf;
-    /* prepare plain text */
-    if (p_text) {
-      memcpy(p_buf, p_text, len);
-      pp = (p_buf + len);
-    }
+    return false;
+  }
 
-    UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
-    UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
+  uint8_t* p_mac = (uint8_t*)signature;
+  uint8_t* pp;
+  uint8_t* p_buf = (uint8_t*)osi_malloc(len + 4);
 
-    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) {
-      btm_ble_increment_sign_ctr(bd_addr, true);
-    }
-
-    BTM_TRACE_DEBUG("%s p_mac = %d", __func__, p_mac);
-    BTM_TRACE_DEBUG(
-        "p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = "
-        "0x%02x",
-        *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
-    BTM_TRACE_DEBUG(
-        "p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = "
-        "0x%02x",
-        *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
-    osi_free(p_buf);
+  BTM_TRACE_DEBUG("%s-Start to generate Local CSRK", __func__);
+  pp = p_buf;
+  /* prepare plain text */
+  if (p_text) {
+    memcpy(p_buf, p_text, len);
+    pp = (p_buf + len);
   }
-  return ret;
+
+  UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
+  UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
+
+  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(
+      "p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = "
+      "0x%02x",
+      *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
+  BTM_TRACE_DEBUG(
+      "p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = "
+      "0x%02x",
+      *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
+  osi_free(p_buf);
+  return true;
 }
 
 /*******************************************************************************
@@ -2249,12 +2243,11 @@ 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)) {
-      if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0) {
-        btm_ble_increment_sign_ctr(bd_addr, false);
-        verified = true;
-      }
+    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 {
-      btm_ble_process_irk(&output);
-    }
+    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.");
index 2d0acc6..59d6ec7 100644 (file)
@@ -94,12 +94,8 @@ 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 {
-    btm_gen_resolve_paddr_cmpl(&output);
-  }
+  SMP_Encrypt(btm_cb.devcb.id_keys.irk, rand, 3, &output);
+  btm_gen_resolve_paddr_cmpl(&output);
 }
 /*******************************************************************************
  *
@@ -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);
 }
index f34e9de..d68e3b7 100644 (file)
@@ -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];
index 816407d..85aa174 100644 (file)
@@ -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 */
index d096da5..7bb5570 100644 (file)
@@ -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
+ *                  p_out               - output of the encrypted texts
  ******************************************************************************/
-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);
 }
 
 /*******************************************************************************
index 42f91a0..2d652eb 100644 (file)
@@ -121,10 +121,10 @@ static void cmac_aes_cleanup(void) {
  * Returns          void
  *
  ******************************************************************************/
-static bool cmac_aes_k_calculate(BT_OCTET16 key, uint8_t* p_signature,
+static void cmac_aes_k_calculate(BT_OCTET16 key, uint8_t* p_signature,
                                  uint16_t tlen) {
   tSMP_ENC output;
-  uint8_t i = 1, err = 0;
+  uint8_t i = 1;
   uint8_t x[16] = {0};
   uint8_t* p_mac;
 
@@ -134,35 +134,25 @@ static bool cmac_aes_k_calculate(BT_OCTET16 key, uint8_t* p_signature,
     smp_xor_128(&cmac_cb.text[(cmac_cb.round - i) * BT_OCTET16_LEN],
                 x); /* Mi' := Mi (+) X  */
 
-    if (!SMP_Encrypt(key, BT_OCTET16_LEN,
-                     &cmac_cb.text[(cmac_cb.round - i) * BT_OCTET16_LEN],
-                     BT_OCTET16_LEN, &output)) {
-      err = 1;
-      break;
-    }
+    SMP_Encrypt(key, &cmac_cb.text[(cmac_cb.round - i) * BT_OCTET16_LEN],
+                BT_OCTET16_LEN, &output);
 
     memcpy(x, output.param_buf, BT_OCTET16_LEN);
     i++;
   }
 
-  if (!err) {
-    p_mac = output.param_buf + (BT_OCTET16_LEN - tlen);
-    memcpy(p_signature, p_mac, tlen);
-
-    SMP_TRACE_DEBUG("tlen = %d p_mac = %d", tlen, p_mac);
-    SMP_TRACE_DEBUG(
-        "p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = "
-        "0x%02x",
-        *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
-    SMP_TRACE_DEBUG(
-        "p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = "
-        "0x%02x",
-        *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
-
-    return true;
-
-  } else
-    return false;
+  p_mac = output.param_buf + (BT_OCTET16_LEN - tlen);
+  memcpy(p_signature, p_mac, tlen);
+
+  SMP_TRACE_DEBUG("tlen = %d p_mac = %d", tlen, p_mac);
+  SMP_TRACE_DEBUG(
+      "p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = "
+      "0x%02x",
+      *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
+  SMP_TRACE_DEBUG(
+      "p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = "
+      "0x%02x",
+      *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
 }
 /*******************************************************************************
  *
@@ -241,22 +231,14 @@ static void cmac_subkey_cont(tSMP_ENC* p) {
  *
  * Parameters       key - CMAC key, expect SRK when used by SMP.
  *
- * Returns          void
- *
  ******************************************************************************/
-static bool cmac_generate_subkey(BT_OCTET16 key) {
+static void cmac_generate_subkey(BT_OCTET16 key) {
   BT_OCTET16 z = {0};
-  bool ret = true;
   tSMP_ENC output;
   SMP_TRACE_EVENT(" cmac_generate_subkey");
 
-  if (SMP_Encrypt(key, BT_OCTET16_LEN, z, BT_OCTET16_LEN, &output)) {
-    cmac_subkey_cont(&output);
-    ;
-  } else
-    ret = false;
-
-  return ret;
+  SMP_Encrypt(key, z, BT_OCTET16_LEN, &output);
+  cmac_subkey_cont(&output);
 }
 /*******************************************************************************
  *
@@ -273,15 +255,12 @@ static bool cmac_generate_subkey(BT_OCTET16 key) {
  *                  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) {
   uint16_t len, diff;
   uint16_t n = (length + BT_OCTET16_LEN - 1) /
                BT_OCTET16_LEN; /* n is number of rounds */
-  bool ret = false;
 
   SMP_TRACE_EVENT("%s", __func__);
 
@@ -302,12 +281,10 @@ bool aes_cipher_msg_auth_code(BT_OCTET16 key, uint8_t* input, uint16_t length,
   }
 
   /* prepare calculation for subkey s and last block of data */
-  if (cmac_generate_subkey(key)) {
-    /* start calculation */
-    ret = cmac_aes_k_calculate(key, p_signature, tlen);
-  }
+  cmac_generate_subkey(key);
+  /* start calculation */
+  cmac_aes_k_calculate(key, p_signature, tlen);
+
   /* clean up */
   cmac_aes_cleanup();
-
-  return ret;
 }
index c3ad35d..24b0347 100644 (file)
@@ -475,8 +475,8 @@ extern void smp_mask_enc_key(uint8_t loc_enc_size, uint8_t* p_data);
 extern void smp_rsp_timeout(void* data);
 extern void smp_delayed_auth_complete_timeout(void* data);
 extern void smp_xor_128(BT_OCTET16 a, BT_OCTET16 b);
-extern bool smp_encrypt_data(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
-                             uint8_t pt_len, tSMP_ENC* p_out);
+extern void smp_encrypt_data(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
+                             tSMP_ENC* p_out);
 extern bool smp_command_has_invalid_parameters(tSMP_CB* p_cb);
 extern void smp_reject_unexpected_pairing_command(const RawAddress& bd_addr);
 extern tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB* p_cb);
@@ -488,7 +488,7 @@ extern void smp_collect_local_ble_address(uint8_t* le_addr, tSMP_CB* p_cb);
 extern void smp_collect_peer_ble_address(uint8_t* le_addr, tSMP_CB* p_cb);
 extern bool smp_check_commitment(tSMP_CB* p_cb);
 extern void smp_save_secure_connections_long_term_key(tSMP_CB* p_cb);
-extern bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb);
+extern void smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb);
 extern void smp_remove_fixed_channel(tSMP_CB* p_cb);
 extern bool smp_request_oob_data(tSMP_CB* p_cb);
 
@@ -518,27 +518,23 @@ extern void smp_calculate_f4(uint8_t* u, uint8_t* v, uint8_t* x, uint8_t z,
                              uint8_t* c);
 extern uint32_t smp_calculate_g2(uint8_t* u, uint8_t* v, uint8_t* x,
                                  uint8_t* y);
-extern bool smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
+extern void smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
                              uint8_t* a2, uint8_t* mac_key, uint8_t* ltk);
-extern bool smp_calculate_f5_mackey_or_long_term_key(
+extern void smp_calculate_f5_mackey_or_long_term_key(
     uint8_t* t, uint8_t* counter, uint8_t* key_id, uint8_t* n1, uint8_t* n2,
     uint8_t* a1, uint8_t* a2, uint8_t* length, uint8_t* mac);
-extern bool smp_calculate_f5_key(uint8_t* w, uint8_t* t);
-extern bool smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
+extern void smp_calculate_f5_key(uint8_t* w, uint8_t* t);
+extern void smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
                              uint8_t* iocap, uint8_t* a1, uint8_t* a2,
                              uint8_t* f3);
-extern bool smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* h2);
-extern bool smp_calculate_h7(uint8_t* salt, uint8_t* w, uint8_t* h2);
+extern void smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* h2);
+extern void smp_calculate_h7(uint8_t* salt, uint8_t* w, uint8_t* h2);
 #if (SMP_DEBUG == TRUE)
 extern void smp_debug_print_nbyte_little_endian(uint8_t* p,
                                                 const char* key_name,
                                                 uint8_t len);
 #endif
 
-/* smp_cmac.cc */
-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 print128(BT_OCTET16 x, const uint8_t* key_name);
 
 #endif /* SMP_INT_H */
index 6be1da4..55151e6 100644 (file)
@@ -46,7 +46,7 @@ using base::Bind;
 #endif
 
 static void smp_process_stk(tSMP_CB* p_cb, tSMP_ENC* p);
-static bool smp_calculate_legacy_short_term_key(tSMP_CB* p_cb,
+static void smp_calculate_legacy_short_term_key(tSMP_CB* p_cb,
                                                 tSMP_ENC* output);
 static void smp_process_private_key(tSMP_CB* p_cb);
 
@@ -98,36 +98,23 @@ void smp_debug_print_nbyte_big_endian(uint8_t* p, const char* key_name,
 #endif
 }
 
-/*******************************************************************************
- *
- * Function         smp_encrypt_data
- *
- * Description      This function is called to encrypt data.
- *                  It uses AES-128 encryption algorithm.
- *                  Plain_text is encrypted using key, the result is at p_out.
- *
- * Returns          void
- *
- ******************************************************************************/
-bool smp_encrypt_data(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
-                      uint8_t pt_len, tSMP_ENC* p_out) {
-  aes_context ctx;
-  uint8_t* p_start = NULL;
+/* This function is called to encrypt data. It uses AES-128 encryption
+ * algorithm. |plain_text| is encrypted using |key|, the result is at p_out.
+ */
+void smp_encrypt_data(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
+                      tSMP_ENC* p_out) {
+  CHECK(p_out);
+
   uint8_t* p = NULL;
   uint8_t* p_rev_data = NULL;   /* input data in big endilan format */
   uint8_t* p_rev_key = NULL;    /* input key in big endilan format */
   uint8_t* p_rev_output = NULL; /* encrypted output in big endilan format */
 
   SMP_TRACE_DEBUG("%s", __func__);
-  if ((p_out == NULL) || (key_len != SMP_ENCRYT_KEY_SIZE)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    return false;
-  }
-
-  p_start = (uint8_t*)osi_calloc(SMP_ENCRYT_DATA_SIZE * 4);
 
   if (pt_len > SMP_ENCRYT_DATA_SIZE) pt_len = SMP_ENCRYT_DATA_SIZE;
 
+  uint8_t p_start[SMP_ENCRYT_DATA_SIZE * 4];
   p = p_start;
   ARRAY_TO_STREAM(p, plain_text, pt_len);          /* byte 0 to byte 15 */
   p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE; /* start at byte 16 */
@@ -142,6 +129,8 @@ bool smp_encrypt_data(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
                                       SMP_ENCRYT_DATA_SIZE);
 #endif
   p_rev_output = p;
+
+  aes_context ctx;
   aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
   aes_encrypt(p_rev_data, p, &ctx); /* outputs in byte 48 to byte 63 */
 
@@ -155,10 +144,6 @@ bool smp_encrypt_data(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
   p_out->param_len = SMP_ENCRYT_KEY_SIZE;
   p_out->status = HCI_SUCCESS;
   p_out->opcode = HCI_BLE_ENCRYPT;
-
-  osi_free(p_start);
-
-  return true;
 }
 
 /*******************************************************************************
@@ -244,12 +229,8 @@ void smp_generate_stk(tSMP_CB* p_cb, UNUSED_ATTR tSMP_INT_DATA* p_data) {
     output.status = HCI_SUCCESS;
     output.opcode = HCI_BLE_ENCRYPT;
     memcpy(output.param_buf, p_cb->ltk, SMP_ENCRYT_DATA_SIZE);
-  } else if (!smp_calculate_legacy_short_term_key(p_cb, &output)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
-    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
-    return;
+  } else {
+    smp_calculate_legacy_short_term_key(p_cb, &output);
   }
 
   smp_process_stk(p_cb, &output);
@@ -273,19 +254,9 @@ void smp_compute_csrk(uint16_t div, tSMP_CB* p_cb) {
   UINT16_TO_STREAM(p, p_cb->div);
   UINT16_TO_STREAM(p, r);
 
-  if (!SMP_Encrypt(er, BT_OCTET16_LEN, buffer, 4, &output)) {
-    SMP_TRACE_ERROR("smp_generate_csrk failed");
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
-    if (p_cb->smp_over_br) {
-      smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
-    } else {
-      smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
-    }
-  } else {
-    memcpy((void*)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
-    smp_send_csrk_info(p_cb, NULL);
-  }
+  SMP_Encrypt(er, buffer, 4, &output);
+  memcpy((void*)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
+  smp_send_csrk_info(p_cb, NULL);
 }
 
 /**
@@ -451,10 +422,7 @@ tSMP_STATUS smp_calculate_comfirm(tSMP_CB* p_cb, BT_OCTET16 rand,
   /* calculate e1 = e(k, p1'), where k = TK */
   smp_debug_print_nbyte_little_endian(p_cb->tk, "TK", 16);
   memset(output, 0, sizeof(tSMP_ENC));
-  if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, output)) {
-    SMP_TRACE_ERROR("%s: failed encryption at e1 = e(k, p1')");
-    return SMP_PAIR_FAIL_UNKNOWN;
-  }
+  SMP_Encrypt(p_cb->tk, p1, BT_OCTET16_LEN, output);
   smp_debug_print_nbyte_little_endian(output->param_buf, "e1 = e(k, p1')", 16);
   /* generate p2 = padding || ia || ra */
   BT_OCTET16 p2;
@@ -464,10 +432,7 @@ tSMP_STATUS smp_calculate_comfirm(tSMP_CB* p_cb, BT_OCTET16 rand,
   smp_debug_print_nbyte_little_endian((uint8_t*)p2, "p2' = p2 XOR e1", 16);
   /* calculate: c1 = e(k, p2') */
   memset(output, 0, sizeof(tSMP_ENC));
-  if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p2, BT_OCTET16_LEN, output)) {
-    SMP_TRACE_ERROR("%s: failed encryption at e1 = e(k, p2')");
-    return SMP_PAIR_FAIL_UNKNOWN;
-  }
+  SMP_Encrypt(p_cb->tk, p2, BT_OCTET16_LEN, output);
   return SMP_SUCCESS;
 }
 
@@ -628,14 +593,8 @@ static void smp_generate_y(tSMP_CB* p_cb, BT_OCTET8 rand) {
 
   memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
   tSMP_ENC output;
-  if (!SMP_Encrypt(dhk, BT_OCTET16_LEN, rand, BT_OCTET8_LEN, &output)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
-    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
-  } else {
-    smp_process_ediv(p_cb, &output);
-  }
+  SMP_Encrypt(dhk, rand, BT_OCTET8_LEN, &output);
+  smp_process_ediv(p_cb, &output);
 }
 
 /**
@@ -650,20 +609,13 @@ static void smp_generate_ltk_cont(uint16_t div, tSMP_CB* p_cb) {
 
   tSMP_ENC output;
   /* LTK = d1(ER, DIV, 0)= e(ER, DIV)*/
-  if (!SMP_Encrypt(er, BT_OCTET16_LEN, (uint8_t*)&p_cb->div, sizeof(uint16_t),
-                   &output)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
-    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
-  } else {
-    /* mask the LTK */
-    smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
-    memcpy((void*)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
+  SMP_Encrypt(er, (uint8_t*)&p_cb->div, sizeof(uint16_t), &output);
+  /* mask the LTK */
+  smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
+  memcpy((void*)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
 
-    /* generate EDIV and rand now */
-    btsnd_hcic_ble_rand(Bind(&smp_generate_y, p_cb));
-  }
+  /* generate EDIV and rand now */
+  btsnd_hcic_ble_rand(Bind(&smp_generate_y, p_cb));
 }
 
 /*******************************************************************************
@@ -710,16 +662,8 @@ void smp_generate_ltk(tSMP_CB* p_cb, UNUSED_ATTR tSMP_INT_DATA* p_data) {
   }
 }
 
-/*******************************************************************************
- *
- * Function         smp_calculate_legacy_short_term_key
- *
- * Description      The function calculates legacy STK.
- *
- * Returns          false if out of resources, true in other cases.
- *
- ******************************************************************************/
-bool smp_calculate_legacy_short_term_key(tSMP_CB* p_cb, tSMP_ENC* output) {
+/* The function calculates legacy STK */
+void smp_calculate_legacy_short_term_key(tSMP_CB* p_cb, tSMP_ENC* output) {
   SMP_TRACE_DEBUG("%s", __func__);
 
   BT_OCTET16 ptext;
@@ -734,12 +678,7 @@ bool smp_calculate_legacy_short_term_key(tSMP_CB* p_cb, tSMP_ENC* output) {
   }
 
   /* generate STK = Etk(rand|rrand)*/
-  bool encrypted =
-      SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, ptext, BT_OCTET16_LEN, output);
-  if (!encrypted) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-  }
-  return encrypted;
+  SMP_Encrypt(p_cb->tk, ptext, BT_OCTET16_LEN, output);
 }
 
 /*******************************************************************************
@@ -1149,10 +1088,7 @@ uint32_t smp_calculate_g2(uint8_t* u, uint8_t* v, uint8_t* x, uint8_t* y) {
   smp_debug_print_nbyte_little_endian(p_prnt, "K", BT_OCTET16_LEN);
 #endif
 
-  if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    return (BTM_MAX_PASSKEY_VAL + 1);
-  }
+  aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
 
 #if (SMP_DEBUG == TRUE)
   p_prnt = cmac;
@@ -1231,13 +1167,11 @@ uint32_t smp_calculate_g2(uint8_t* u, uint8_t* v, uint8_t* x, uint8_t* y) {
  *                          MacKey  is 128 bits;
  *                          LTK     is 128 bits
  *
- * Returns          false if out of resources, true in other cases.
- *
  * Note             The LSB is the first octet, the MSB is the last octet of
  *                  the AES-CMAC input/output stream.
  *
  ******************************************************************************/
-bool smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
+void smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
                       uint8_t* a2, uint8_t* mac_key, uint8_t* ltk) {
   BT_OCTET16 t; /* AES-CMAC output in smp_calculate_f5_key(...), key in */
                 /* smp_calculate_f5_mackey_or_long_term_key(...) */
@@ -1275,36 +1209,26 @@ bool smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
   smp_debug_print_nbyte_little_endian(p_prnt, "A2", 7);
 #endif
 
-  if (!smp_calculate_f5_key(w, t)) {
-    SMP_TRACE_ERROR("%s failed to calc T", __func__);
-    return false;
-  }
+  smp_calculate_f5_key(w, t);
 #if (SMP_DEBUG == TRUE)
   p_prnt = t;
   smp_debug_print_nbyte_little_endian(p_prnt, "T", BT_OCTET16_LEN);
 #endif
 
-  if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_mac_key, key_id, n1,
-                                                n2, a1, a2, length, mac_key)) {
-    SMP_TRACE_ERROR("%s failed to calc MacKey", __func__);
-    return false;
-  }
+  smp_calculate_f5_mackey_or_long_term_key(t, counter_mac_key, key_id, n1, n2,
+                                           a1, a2, length, mac_key);
 #if (SMP_DEBUG == TRUE)
   p_prnt = mac_key;
   smp_debug_print_nbyte_little_endian(p_prnt, "MacKey", BT_OCTET16_LEN);
 #endif
 
-  if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_ltk, key_id, n1, n2,
-                                                a1, a2, length, ltk)) {
-    SMP_TRACE_ERROR("%s failed to calc LTK", __func__);
-    return false;
-  }
+  smp_calculate_f5_mackey_or_long_term_key(t, counter_ltk, key_id, n1, n2, a1,
+                                           a2, length, ltk);
 #if (SMP_DEBUG == TRUE)
   p_prnt = ltk;
   smp_debug_print_nbyte_little_endian(p_prnt, "LTK", BT_OCTET16_LEN);
 #endif
 
-  return true;
 }
 
 /*******************************************************************************
@@ -1333,13 +1257,11 @@ bool smp_calculate_f5(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* a1,
  *                              Length  is 16 bits, its value is 0x0100
  *                  output:     LTK     is 128 bit.
  *
- * Returns          false if out of resources, true in other cases.
- *
  * Note             The LSB is the first octet, the MSB is the last octet of
  *                  the AES-CMAC input/output stream.
  *
  ******************************************************************************/
-bool smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
+void smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
                                               uint8_t* key_id, uint8_t* n1,
                                               uint8_t* n2, uint8_t* a1,
                                               uint8_t* a2, uint8_t* length,
@@ -1352,7 +1274,6 @@ bool smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
                     BT_OCTET16_LEN /* N2 size */ + 7 /* A1 size*/ +
                     7 /* A2 size*/ + 2 /* Length size */;
   uint8_t msg[1 + 4 + BT_OCTET16_LEN + BT_OCTET16_LEN + 7 + 7 + 2];
-  bool ret = true;
 #if (SMP_DEBUG == TRUE)
   uint8_t* p_prnt = NULL;
 #endif
@@ -1396,10 +1317,7 @@ bool smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
   smp_debug_print_nbyte_little_endian(p_prnt, "M", msg_len);
 #endif
 
-  if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    ret = false;
-  }
+  aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
 
 #if (SMP_DEBUG == TRUE)
   p_prnt = cmac;
@@ -1408,7 +1326,6 @@ bool smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
 
   p = mac;
   ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
-  return ret;
 }
 
 /*******************************************************************************
@@ -1430,7 +1347,7 @@ bool smp_calculate_f5_mackey_or_long_term_key(uint8_t* t, uint8_t* counter,
  *                  the AES-CMAC input/output stream.
  *
  ******************************************************************************/
-bool smp_calculate_f5_key(uint8_t* w, uint8_t* t) {
+void smp_calculate_f5_key(uint8_t* w, uint8_t* t) {
   uint8_t* p = NULL;
   /* Please see 2.2.7 LE Secure Connections Key Generation Function f5 */
   /*
@@ -1465,12 +1382,7 @@ bool smp_calculate_f5_key(uint8_t* w, uint8_t* t) {
 #endif
 
   BT_OCTET16 cmac;
-  bool ret = true;
-  if (!aes_cipher_msg_auth_code(key, msg, BT_OCTET32_LEN, BT_OCTET16_LEN,
-                                cmac)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    ret = false;
-  }
+  aes_cipher_msg_auth_code(key, msg, BT_OCTET32_LEN, BT_OCTET16_LEN, cmac);
 
 #if (SMP_DEBUG == TRUE)
   p_prnt = cmac;
@@ -1479,7 +1391,6 @@ bool smp_calculate_f5_key(uint8_t* w, uint8_t* t) {
 
   p = t;
   ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
-  return ret;
 }
 
 /*******************************************************************************
@@ -1525,7 +1436,6 @@ void smp_calculate_local_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
 void smp_calculate_peer_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
   uint8_t iocap[3], a[7], b[7];
   BT_OCTET16 param_buf;
-  bool ret;
   tSMP_KEY key;
 
   SMP_TRACE_DEBUG("%s", __func__);
@@ -1534,26 +1444,19 @@ void smp_calculate_peer_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
 
   smp_collect_local_ble_address(a, p_cb);
   smp_collect_peer_ble_address(b, p_cb);
-  ret = smp_calculate_f6(p_cb->mac_key, p_cb->rrand, p_cb->rand,
-                         p_cb->local_random, iocap, b, a, param_buf);
+  smp_calculate_f6(p_cb->mac_key, p_cb->rrand, p_cb->rand, p_cb->local_random,
+                   iocap, b, a, param_buf);
 
-  if (ret) {
-    SMP_TRACE_EVENT("peer DHKey check calculation is completed");
+  SMP_TRACE_EVENT("peer DHKey check calculation is completed");
 #if (SMP_DEBUG == TRUE)
-    smp_debug_print_nbyte_little_endian(param_buf, "peer DHKey check",
-                                        BT_OCTET16_LEN);
+  smp_debug_print_nbyte_little_endian(param_buf, "peer DHKey check",
+                                      BT_OCTET16_LEN);
 #endif
-    key.key_type = SMP_KEY_TYPE_PEER_DHK_CHCK;
-    key.p_data = param_buf;
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.key = key;
-    smp_sm_event(p_cb, SMP_SC_KEY_READY_EVT, &smp_int_data);
-  } else {
-    SMP_TRACE_EVENT("peer DHKey check calculation failed");
-    tSMP_INT_DATA smp_int_data;
-    smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
-    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
-  }
+  key.key_type = SMP_KEY_TYPE_PEER_DHK_CHCK;
+  key.p_data = param_buf;
+  tSMP_INT_DATA smp_int_data;
+  smp_int_data.key = key;
+  smp_sm_event(p_cb, SMP_SC_KEY_READY_EVT, &smp_int_data);
 }
 
 /*******************************************************************************
@@ -1574,13 +1477,11 @@ void smp_calculate_peer_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  *                          A2 is 56 bit,
  *                  output: C is 128 bit.
  *
- * Returns          false if out of resources, true in other cases.
- *
  * Note             The LSB is the first octet, the MSB is the last octet of
  *                  the AES-CMAC input/output stream.
  *
  ******************************************************************************/
-bool smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
+void smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
                       uint8_t* iocap, uint8_t* a1, uint8_t* a2, uint8_t* c) {
   uint8_t* p = NULL;
   uint8_t msg_len = BT_OCTET16_LEN /* N1 size */ +
@@ -1632,11 +1533,7 @@ bool smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
   smp_debug_print_nbyte_little_endian(p_print, "M", msg_len);
 #endif
 
-  bool ret = true;
-  if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    ret = false;
-  }
+  aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
 
 #if (SMP_DEBUG == TRUE)
   p_print = cmac;
@@ -1645,7 +1542,6 @@ bool smp_calculate_f6(uint8_t* w, uint8_t* n1, uint8_t* n2, uint8_t* r,
 
   p = c;
   ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
-  return ret;
 }
 
 /*******************************************************************************
@@ -1687,74 +1583,54 @@ bool smp_calculate_link_key_from_long_term_key(tSMP_CB* p_cb) {
   }
 
   BT_OCTET16 intermediate_link_key;
-  bool ret = true;
 
   if (p_cb->key_derivation_h7_used)
-    ret = smp_calculate_h7((uint8_t*)salt, p_cb->ltk, intermediate_link_key);
+    smp_calculate_h7((uint8_t*)salt, p_cb->ltk, intermediate_link_key);
   else
-    ret = smp_calculate_h6(p_cb->ltk, (uint8_t*)"1pmt" /* reversed "tmp1" */,
-                           intermediate_link_key);
-  if (!ret) {
-    SMP_TRACE_ERROR("%s failed to derive intermediate_link_key", __func__);
-    return ret;
-  }
+    smp_calculate_h6(p_cb->ltk, (uint8_t*)"1pmt" /* reversed "tmp1" */,
+                     intermediate_link_key);
 
   BT_OCTET16 link_key;
-  ret = smp_calculate_h6(intermediate_link_key,
-                         (uint8_t*)"rbel" /* reversed "lebr" */, link_key);
-  if (!ret) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-  } else {
-    uint8_t link_key_type;
-    if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
-      /* Secure Connections Only Mode */
+  smp_calculate_h6(intermediate_link_key,
+                   (uint8_t*)"rbel" /* reversed "lebr" */, link_key);
+  uint8_t link_key_type;
+  if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
+    /* Secure Connections Only Mode */
+    link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
+  } else if (controller_get_interface()->supports_secure_connections()) {
+    /* both transports are SC capable */
+    if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
       link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
-    } else if (controller_get_interface()->supports_secure_connections()) {
-      /* both transports are SC capable */
-      if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
-        link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
-      else
-        link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB_P_256;
-    } else if (btm_cb.security_mode == BTM_SEC_MODE_SP) {
-      /* BR/EDR transport is SSP capable */
-      if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
-        link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
-      else
-        link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
-    } else {
-      SMP_TRACE_ERROR(
-          "%s failed to update link_key. Sec Mode = %d, sm4 = 0x%02x", __func__,
-          btm_cb.security_mode, p_dev_rec->sm4);
-      return false;
-    }
+    else
+      link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB_P_256;
+  } else if (btm_cb.security_mode == BTM_SEC_MODE_SP) {
+    /* BR/EDR transport is SSP capable */
+    if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
+      link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
+    else
+      link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
+  } else {
+    SMP_TRACE_ERROR("%s failed to update link_key. Sec Mode = %d, sm4 = 0x%02x",
+                    __func__, btm_cb.security_mode, p_dev_rec->sm4);
+    return false;
+  }
 
-    link_key_type += BTM_LTK_DERIVED_LKEY_OFFSET;
+  link_key_type += BTM_LTK_DERIVED_LKEY_OFFSET;
 
-    uint8_t* p;
-    BT_OCTET16 notif_link_key;
-    p = notif_link_key;
-    ARRAY16_TO_STREAM(p, link_key);
+  uint8_t* p;
+  BT_OCTET16 notif_link_key;
+  p = notif_link_key;
+  ARRAY16_TO_STREAM(p, link_key);
 
-    btm_sec_link_key_notification(bda_for_lk, notif_link_key, link_key_type);
+  btm_sec_link_key_notification(bda_for_lk, notif_link_key, link_key_type);
 
-    SMP_TRACE_EVENT("%s is completed", __func__);
-  }
+  SMP_TRACE_EVENT("%s is completed", __func__);
 
-  return ret;
+  return true;
 }
 
-/*******************************************************************************
- *
- * Function         smp_calculate_long_term_key_from_link_key
- *
- * Description      The function calculates and saves SC LTK derived from BR/EDR
- *                  link key.
- *
- * Returns          false if out of resources, true in other cases.
- *
- ******************************************************************************/
+/** The function calculates and saves SC LTK derived from BR/EDR link key. */
 bool smp_calculate_long_term_key_from_link_key(tSMP_CB* p_cb) {
-  bool ret = true;
   tBTM_SEC_DEV_REC* p_dev_rec;
   uint8_t rev_link_key[16];
   BT_OCTET16 salt = {0x32, 0x70, 0x6D, 0x74, 0x00, 0x00, 0x00, 0x00,
@@ -1790,33 +1666,22 @@ bool smp_calculate_long_term_key_from_link_key(tSMP_CB* p_cb) {
 
   BT_OCTET16 intermediate_long_term_key;
   if (p_cb->key_derivation_h7_used) {
-    ret = smp_calculate_h7((uint8_t*)salt, rev_link_key,
-                           intermediate_long_term_key);
+    smp_calculate_h7((uint8_t*)salt, rev_link_key, intermediate_long_term_key);
   } else {
     /* "tmp2" obtained from the spec */
-    ret = smp_calculate_h6(rev_link_key, (uint8_t*)"2pmt" /* reversed "tmp2" */,
-                           intermediate_long_term_key);
-  }
-
-  if (!ret) {
-    SMP_TRACE_ERROR("%s failed to derive intermediate_long_term_key", __func__);
-    return ret;
+    smp_calculate_h6(rev_link_key, (uint8_t*)"2pmt" /* reversed "tmp2" */,
+                     intermediate_long_term_key);
   }
 
   /* "brle" obtained from the spec */
-  ret = smp_calculate_h6(intermediate_long_term_key,
-                         (uint8_t*)"elrb" /* reversed "brle" */, p_cb->ltk);
+  smp_calculate_h6(intermediate_long_term_key,
+                   (uint8_t*)"elrb" /* reversed "brle" */, p_cb->ltk);
 
-  if (!ret) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-  } else {
-    p_cb->sec_level = (br_link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)
-                          ? SMP_SEC_AUTHENTICATED
-                          : SMP_SEC_UNAUTHENTICATE;
-    SMP_TRACE_EVENT("%s is completed", __func__);
-  }
-
-  return ret;
+  p_cb->sec_level = (br_link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)
+                        ? SMP_SEC_AUTHENTICATED
+                        : SMP_SEC_UNAUTHENTICATE;
+  SMP_TRACE_EVENT("%s is completed", __func__);
+  return true;
 }
 
 /*******************************************************************************
@@ -1831,13 +1696,11 @@ bool smp_calculate_long_term_key_from_link_key(tSMP_CB* p_cb) {
  *                          KeyId is 32 bit,
  *                  output: C is 128 bit.
  *
- * Returns          false if out of resources, true in other cases.
- *
  * Note             The LSB is the first octet, the MSB is the last octet of
  *                  the AES-CMAC input/output stream.
  *
  ******************************************************************************/
-bool smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* c) {
+void smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* c) {
 #if (SMP_DEBUG == TRUE)
   uint8_t* p_print = NULL;
 #endif
@@ -1872,12 +1735,8 @@ bool smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* c) {
   smp_debug_print_nbyte_little_endian(p_print, "M", msg_len);
 #endif
 
-  bool ret = true;
   uint8_t cmac[BT_OCTET16_LEN];
-  if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    ret = false;
-  }
+  aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
 
 #if (SMP_DEBUG == TRUE)
   p_print = cmac;
@@ -1886,7 +1745,6 @@ bool smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* c) {
 
   p = c;
   ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
-  return ret;
 }
 
 /*******************************************************************************
@@ -1901,13 +1759,11 @@ bool smp_calculate_h6(uint8_t* w, uint8_t* keyid, uint8_t* c) {
 **                          SALT is 128 bit,
 **                  output: C is 128 bit.
 **
-** Returns          FALSE if out of resources, TRUE in other cases.
-**
 ** Note             The LSB is the first octet, the MSB is the last octet of
 **                  the AES-CMAC input/output stream.
 **
 *******************************************************************************/
-bool smp_calculate_h7(uint8_t* salt, uint8_t* w, uint8_t* c) {
+void smp_calculate_h7(uint8_t* salt, uint8_t* w, uint8_t* c) {
   SMP_TRACE_DEBUG("%s", __FUNCTION__);
 
   uint8_t key[BT_OCTET16_LEN];
@@ -1919,16 +1775,11 @@ bool smp_calculate_h7(uint8_t* salt, uint8_t* w, uint8_t* c) {
   p = msg;
   ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
 
-  bool ret = true;
   uint8_t cmac[BT_OCTET16_LEN];
-  if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
-    SMP_TRACE_ERROR("%s failed", __FUNCTION__);
-    ret = false;
-  }
+  aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
 
   p = c;
   ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
-  return ret;
 }
 
 /**
index bff5645..7a96ddd 100644 (file)
@@ -1467,10 +1467,8 @@ void smp_save_secure_connections_long_term_key(tSMP_CB* p_cb) {
  *                  MacKey is used in dhkey calculation, LTK is used to encrypt
  *                  the link.
  *
- * Returns          false if out of resources, true otherwise.
- *
  ******************************************************************************/
-bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb) {
+void smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb) {
   uint8_t a[7];
   uint8_t b[7];
   uint8_t* p_na;
@@ -1490,14 +1488,9 @@ bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb) {
     p_nb = p_cb->rand;
   }
 
-  if (!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key,
-                        p_cb->ltk)) {
-    SMP_TRACE_ERROR("%s failed", __func__);
-    return false;
-  }
+  smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key, p_cb->ltk);
 
   SMP_TRACE_EVENT("%s is completed", __func__);
-  return true;
 }
 
 /*******************************************************************************
index 65a30d5..3ed65d4 100644 (file)
@@ -47,10 +47,8 @@ bool BTM_BleLocalPrivacyEnabled() { return true; }
 uint16_t BTM_ReadDiscoverability(uint16_t* p_window, uint16_t* p_interval) {
   return true;
 }
-bool SMP_Encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
-                 uint8_t pt_len, tSMP_ENC* p_out) {
-  return true;
-}
+void SMP_Encrypt(uint8_t* key, uint8_t* plain_text, uint8_t pt_len,
+                 tSMP_ENC* p_out) {}
 void BTM_GetDeviceIDRoot(BT_OCTET16 irk) {}
 void btm_ble_update_dmt_flag_bits(uint8_t* flag_value,
                                   const uint16_t connect_mode,
index 9070764..de78898 100644 (file)
@@ -205,8 +205,7 @@ TEST_F(SmpCalculateConfirmTest, test_SMP_Encrypt_as_master) {
   ASSERT_THAT(p1_xor_r_str, StrEq(expected_p1_xor_r_str));
   tSMP_ENC output;
   memset(&output, 0, sizeof(tSMP_ENC));
-  ASSERT_TRUE(
-      SMP_Encrypt(p_cb_.tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output));
+  SMP_Encrypt(p_cb_.tk, p1, BT_OCTET16_LEN, &output);
   const char expected_p1_prime_str[] = "02c7aa2a9857ac866ff91232df0e3c95";
   char p1_prime_str[2 * sizeof(BT_OCTET16) + 1];
   dump_uint128_reverse(output.param_buf, p1_prime_str);