From 9b10ee5a3522c2a26538f3439b333fb5ba5b2678 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Tue, 6 Jan 2026 19:37:45 +0800 Subject: [PATCH] fix(ble/bluedroid): Fixed smp unit test failed if mbedTLS or TinyCrypt is enabled --- .../basic_unit_test/main/CMakeLists.txt | 7 +++ .../test_apps/basic_unit_test/main/test_smp.c | 59 ++++++++----------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/components/bt/test_apps/basic_unit_test/main/CMakeLists.txt b/components/bt/test_apps/basic_unit_test/main/CMakeLists.txt index 6aecb24e3d..d873380d2b 100644 --- a/components/bt/test_apps/basic_unit_test/main/CMakeLists.txt +++ b/components/bt/test_apps/basic_unit_test/main/CMakeLists.txt @@ -4,3 +4,10 @@ idf_component_register(SRCS "test_bt_main.c" INCLUDE_DIRS "." PRIV_REQUIRES unity bt WHOLE_ARCHIVE) + +# Add private include directories from bt component to access internal headers +idf_component_get_property(bt_dir bt COMPONENT_DIR) +target_include_directories(${COMPONENT_LIB} PRIVATE ${bt_dir}/host/bluedroid/stack/smp/include) +target_include_directories(${COMPONENT_LIB} PRIVATE ${bt_dir}/host/bluedroid/common/include) +target_include_directories(${COMPONENT_LIB} PRIVATE ${bt_dir}/host/bluedroid/stack/include) +target_include_directories(${COMPONENT_LIB} PRIVATE ${bt_dir}/common/include) diff --git a/components/bt/test_apps/basic_unit_test/main/test_smp.c b/components/bt/test_apps/basic_unit_test/main/test_smp.c index ea89ec261b..01bbc5b64c 100644 --- a/components/bt/test_apps/basic_unit_test/main/test_smp.c +++ b/components/bt/test_apps/basic_unit_test/main/test_smp.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -18,42 +18,14 @@ #include "esp_bt_main.h" #include "esp_bt_device.h" #include "esp_gap_ble_api.h" +#include "sdkconfig.h" + +#if defined(CONFIG_BT_SMP_CRYPTO_STACK_NATIVE) && CONFIG_BT_SMP_CRYPTO_STACK_NATIVE +/* Native Bluedroid crypto implementation tests */ +#include "p_256_ecc_pp.h" #define KEY_LENGTH_DWORDS_P256 8 -typedef unsigned long DWORD; -typedef uint32_t UINT32; - -typedef struct { - DWORD x[KEY_LENGTH_DWORDS_P256]; - DWORD y[KEY_LENGTH_DWORDS_P256]; - DWORD z[KEY_LENGTH_DWORDS_P256]; -} Point; - -typedef struct { - // curve's coefficients - DWORD a[KEY_LENGTH_DWORDS_P256]; - DWORD b[KEY_LENGTH_DWORDS_P256]; - - //whether a is -3 - int a_minus3; - - // prime modulus - DWORD p[KEY_LENGTH_DWORDS_P256]; - - // Omega, p = 2^m -omega - DWORD omega[KEY_LENGTH_DWORDS_P256]; - - // base point, a point on E of order r - Point G; - -} elliptic_curve_t; - -extern void ECC_PointMult_Bin_NAF(Point *q, Point *p, DWORD *n, uint32_t keyLength); -extern bool ECC_CheckPointIsInElliCur_P256(Point *p); -extern void p_256_init_curve(UINT32 keyLength); -extern elliptic_curve_t curve_p256; - static void bt_rand(void *buf, size_t len) { if (!len) { @@ -70,7 +42,17 @@ static void bt_rand(void *buf, size_t len) return; } -TEST_CASE("ble_smp_public_key_check", "[ble_smp]") +/** + * @brief Test ECC public key validation for native Bluedroid crypto implementation + * + * This test is only compiled when SMP_CRYPTO_STACK_NATIVE is enabled. + * It tests the native Bluedroid ECC implementation including: + * - Public key generation from private key + * - Public key validation on elliptic curve + * - Attack simulation (invalid public key detection) + * - Random key generation and validation + */ +TEST_CASE("ble_smp_public_key_check", "[ble_smp][native_crypto]") { /* We wait init finish 200ms here */ vTaskDelay(200 / portTICK_PERIOD_MS); @@ -96,7 +78,14 @@ TEST_CASE("ble_smp_public_key_check", "[ble_smp]") TEST_ASSERT(ECC_CheckPointIsInElliCur_P256(&public_key)); } } +#endif /* CONFIG_BT_SMP_CRYPTO_STACK_NATIVE */ +/** + * @brief Test static passkey set/clear functionality + * + * This test is applicable to all crypto stack implementations (Native, TinyCrypt, mbedTLS). + * It tests the SMP security parameter API for setting and clearing static passkeys. + */ TEST_CASE("ble_smp_set_clear_static_passkey", "[ble_smp]") { /* We wait init finish 200ms here */