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

Commit a5a4e276 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Move crypto related functions into crypto_toolbox

Also clean up the code:
* remove spurious logging, or replace with DVLOG
* use std::copy and std::reverse_copy instead of ARRAY_TO_STREAM and
  REVERSE_ARRAY_TO_STREAM

Test: CryptoToolboxTest already cover all those functions
Change-Id: I3221d0966d2dd6ca229adf441ceb75dd37a54d7c
Merged-In: I3221d0966d2dd6ca229adf441ceb75dd37a54d7c
parent f72bb8b1
Loading
Loading
Loading
Loading
+60 −58
Original line number Diff line number Diff line
crypto_toolbox_srcs = [
    "crypto_toolbox/aes.cc",
    "crypto_toolbox/aes_cmac.cc",
    "crypto_toolbox/crypto_toolbox.cc",
]

// Bluetooth stack static library for target
// ========================================================
cc_library_static {
@@ -40,7 +46,7 @@ cc_library_static {
        "packages/modules/Bluetooth/system/bta/sys",
        "packages/modules/Bluetooth/system/utils/include",
    ],
    srcs: [
    srcs: crypto_toolbox_srcs + [
        "a2dp/a2dp_aac.cc",
        "a2dp/a2dp_aac_decoder.cc",
        "a2dp/a2dp_aac_encoder.cc",
@@ -155,15 +161,12 @@ cc_library_static {
        "sdp/sdp_main.cc",
        "sdp/sdp_server.cc",
        "sdp/sdp_utils.cc",
        "smp/crypto_toolbox.cc",
        "smp/aes.cc",
        "smp/p_256_curvepara.cc",
        "smp/p_256_ecc_pp.cc",
        "smp/p_256_multprecision.cc",
        "smp/smp_act.cc",
        "smp/smp_api.cc",
        "smp/smp_br_main.cc",
        "smp/smp_cmac.cc",
        "smp/smp_keys.cc",
        "smp/smp_l2c.cc",
        "smp/smp_main.cc",
@@ -183,7 +186,7 @@ cc_library_static {
    required: [
        "libldacBT_enc",
        "libldacBT_abr",
    ]
    ],
}

// Bluetooth stack unit tests for target
@@ -292,16 +295,16 @@ cc_test {
        "packages/modules/Bluetooth/system/hci/include",
        "packages/modules/Bluetooth/system/utils/include",
    ],
    srcs: [
    srcs: crypto_toolbox_srcs + [
        "smp/smp_keys.cc",
        "smp/crypto_toolbox.cc",
        "smp/aes.cc",
        "smp/p_256_curvepara.cc",
        "smp/p_256_ecc_pp.cc",
        "smp/p_256_multprecision.cc",
        "smp/smp_api.cc",
        "smp/smp_cmac.cc",
        "smp/smp_main.cc",
        "smp/smp_utils.cc",
        "test/stack_smp_test.cc",
        "test/crypto_toolbox_test.cc",
        "test/stack_smp_test.cc",
    ],
    shared_libs: [
        "libcutils",
@@ -313,7 +316,6 @@ cc_test {
    ],
}


// Bluetooth stack multi-advertising unit tests for target
// ========================================================
cc_test {
+84 −2
Original line number Diff line number Diff line
@@ -14,6 +14,22 @@
#  limitations under the License.
#

static_library("crypto_toolbox") {
  sources = [
    "crypto_toolbox/crypto_toolbox.cc",
    "crypto_toolbox/aes.cc",
    "crypto_toolbox/aes_cmac.cc",
  ]

  include_dirs = [
    "//",
  ]

  deps = [
    "//third_party/libchrome:base",
  ]
}

static_library("stack") {
  sources = [
    "a2dp/a2dp_aac.cc",
@@ -130,14 +146,12 @@ static_library("stack") {
    "sdp/sdp_main.cc",
    "sdp/sdp_server.cc",
    "sdp/sdp_utils.cc",
    "smp/aes.cc",
    "smp/p_256_curvepara.cc",
    "smp/p_256_ecc_pp.cc",
    "smp/p_256_multprecision.cc",
    "smp/smp_act.cc",
    "smp/smp_api.cc",
    "smp/smp_br_main.cc",
    "smp/smp_cmac.cc",
    "smp/smp_keys.cc",
    "smp/smp_l2c.cc",
    "smp/smp_main.cc",
@@ -180,6 +194,7 @@ static_library("stack") {
  ]

  deps = [
    ":crypto_toolbox",
    "//types",
    "//third_party/libchrome:base",
    "//third_party/libldac:libldacBT_enc",
@@ -236,6 +251,73 @@ executable("stack_unittests") {
  ]
}

executable("net_test_stack_crypto_toolbox") {
  testonly = true
  sources = [
    "test/crypto_toolbox_test.cc",
  ]

  include_dirs = [
    "//",
  ]

  deps = [
    ":crypto_toolbox",
    "//third_party/googletest:gmock_main",
    "//third_party/libchrome:base",
  ]
}

executable("net_test_stack_smp") {
  testonly = true
  sources = [
        "smp/smp_keys.cc",
        "smp/smp_api.cc",
        "smp/smp_main.cc",
        "smp/smp_utils.cc",
        "test/stack_smp_test.cc",
  ]

  include_dirs = [
    "//",
    "//internal_include",
    "//btcore/include",
    "//hci/include",
    "//utils/include",
    "//bta/include",
    "//bta/sys",
    "//btcore/include",
    "//embdrv/sbc/encoder/include",
    "//hci/include",
    "//internal_include",
    "//stack/a2dp",
    "//stack/l2cap",
    "//stack/btm",
    "//stack/include",
    "//third_party/tinyxml2",
    "//udrv/include",
    "//utils/include",
    "//vnd/include"
  ]

  libs = [
    "-ldl",
    "-lpthread",
    "-lresolv",
    "-lrt",
    "-lz",
    "-latomic",
  ]

  deps = [
    ":crypto_toolbox",
    "//osi",
    "//types",
    "//third_party/googletest:gmock_main",
    "//third_party/libchrome:base",
  ]
}

executable("net_test_stack_multi_adv") {
  testonly = true
  sources = [
+5 −5
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@
#include "l2c_int.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "smp_api.h"
#include "stack/crypto_toolbox/crypto_toolbox.h"

extern void gatt_notify_phy_updated(uint8_t status, uint16_t handle,
                                    uint8_t tx_phy, uint8_t rx_phy);
@@ -2143,7 +2143,7 @@ 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);

  aes_cipher_msg_auth_code(p_rec->ble.keys.lcsrk, p_buf, (uint16_t)(len + 4),
  crypto_toolbox::aes_cmac(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);

@@ -2191,7 +2191,7 @@ 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);

    aes_cipher_msg_auth_code(p_rec->ble.keys.pcsrk, p_orig, len,
    crypto_toolbox::aes_cmac(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);
@@ -2365,12 +2365,12 @@ static void btm_ble_reset_id_impl(const Octet16& rand1, const Octet16& rand2) {

  /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
  btm_cb.devcb.id_keys.dhk =
      SMP_Encrypt(btm_cb.devcb.id_keys.ir, &btm_ble_dhk_pt, 1);
      crypto_toolbox::aes_128(btm_cb.devcb.id_keys.ir, &btm_ble_dhk_pt, 1);

  uint8_t btm_ble_irk_pt = 0x01;
  /* IRK = D1(IR, 1) */
  btm_cb.devcb.id_keys.irk =
      SMP_Encrypt(btm_cb.devcb.id_keys.ir, &btm_ble_irk_pt, 1);
      crypto_toolbox::aes_128(btm_cb.devcb.id_keys.ir, &btm_ble_irk_pt, 1);

  btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);

+3 −3
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@
#include "hcimsgs.h"

#include "btm_ble_int.h"
#include "smp_api.h"
#include "stack/crypto_toolbox/crypto_toolbox.h"

/* This function generates Resolvable Private Address (RPA) from Identity
 * Resolving Key |irk| and |random|*/
@@ -48,7 +48,7 @@ RawAddress generate_rpa_from_irk_and_rand(const Octet16& irk,
  address.address[0] = random[2];

  /* encrypt with IRK */
  Octet16 p = SMP_Encrypt(irk, random, 3);
  Octet16 p = crypto_toolbox::aes_128(irk, random, 3);

  /* set hash to be LSB of rpAddress */
  address.address[5] = p[0];
@@ -178,7 +178,7 @@ static bool rpa_matches_irk(const RawAddress& rpa, const Octet16& irk) {
  rand[2] = rpa.address[0];

  /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
  Octet16 x = SMP_Encrypt(irk, &rand[0], 3);
  Octet16 x = crypto_toolbox::aes_128(irk, &rand[0], 3);

  rand[0] = rpa.address[5];
  rand[1] = rpa.address[4];
+1 −4
Original line number Diff line number Diff line
@@ -43,12 +43,9 @@
#endif
#endif

#include <stdint.h>
#include <stdlib.h>

/* add the target configuration to allow using internal data types and
 * compilation options */
#include "bt_target.h"

/* define if you have fast 32-bit types on your system */
#if 1
#define HAVE_UINT_32T
Loading