Skip to content

Commit

Permalink
Merge branch 'bugfix/ble_update_lib_20240125_v5.2' into 'release/v5.2'
Browse files Browse the repository at this point in the history
Bugfix/ble update lib 20240125 v5.2

See merge request espressif/esp-idf!28710
  • Loading branch information
Isl2017 committed Feb 19, 2024
2 parents d4dd0d4 + fdcd678 commit 1c2a671
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 128 deletions.
51 changes: 21 additions & 30 deletions components/bt/controller/esp32c2/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,21 +1024,21 @@ void esp_ble_controller_log_dump_all(bool output)
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED

#if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true)

#if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)
#if CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
#define BLE_SM_KEY_ERR 0x17

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#include "mbedtls/aes.h"

#if CONFIG_BT_LE_SM_SC
#include "mbedtls/cipher.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/cmac.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/ecp.h"
#endif

static mbedtls_ecp_keypair keypair;
#endif // CONFIG_BT_LE_SM_SC

#else
#include "tinycrypt/aes.h"
Expand All @@ -1048,15 +1048,15 @@ void esp_ble_controller_log_dump_all(bool output)
#if CONFIG_BT_LE_SM_SC
#include "tinycrypt/cmac_mode.h"
#include "tinycrypt/ecc_dh.h"
#endif

#endif
#endif // CONFIG_BT_LE_SM_SC
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#if CONFIG_BT_LE_SM_SC
static mbedtls_ecp_keypair keypair;
#endif
#endif
/* Based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
static const uint8_t ble_sm_alg_dbg_priv_key[32] = {
0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3,
0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
};

int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y,
const uint8_t *our_priv_key, uint8_t *out_dhkey)
Expand Down Expand Up @@ -1103,8 +1103,7 @@ int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_
}

/* Set PRNG */
if ((rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
NULL, 0)) != 0) {
if ((rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)) != 0) {
goto exit;
}

Expand Down Expand Up @@ -1148,19 +1147,12 @@ int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_
if (rc == TC_CRYPTO_FAIL) {
return BLE_SM_KEY_ERR;
}
#endif
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS

swap_buf(out_dhkey, dh, 32);
return 0;
}

/* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
static const uint8_t ble_sm_alg_dbg_priv_key[32] = {
0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3,
0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
};

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
static int mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)
{
Expand Down Expand Up @@ -1206,7 +1198,7 @@ static int mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)

return 0;
}
#endif
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS

/**
* pub: 64 bytes
Expand All @@ -1222,7 +1214,6 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
uint8_t pk[64];

do {

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
if (mbedtls_gen_keypair(pk, priv) != 0) {
return BLE_SM_KEY_ERR;
Expand All @@ -1231,17 +1222,17 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) {
return BLE_SM_KEY_ERR;
}
#endif

#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
/* Make sure generated key isn't debug key. */
} while (memcmp(priv, ble_sm_alg_dbg_priv_key, 32) == 0);

swap_buf(pub, pk, 32);
swap_buf(&pub[32], &pk[32], 32);
swap_in_place(priv, 32);
#endif

#endif // CONFIG_BT_LE_SM_SC_DEBUG_KEYS
return 0;
}

#endif
#endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
#endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)

94 changes: 46 additions & 48 deletions components/bt/controller/esp32c6/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end);
extern int ble_osi_coex_funcs_register(struct osi_coex_funcs_t *coex_funcs);
extern int ble_controller_init(esp_bt_controller_config_t *cfg);
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
extern int ble_log_init_async(interface_func_t bt_controller_log_interface, bool task_create, uint8_t buffers, uint32_t *bufs_size);
extern int ble_log_deinit_async(void);
extern void ble_log_async_select_dump_buffers(uint8_t buffers);
extern void ble_log_async_output_dump_all(bool output);
extern int r_ble_log_init_async(interface_func_t bt_controller_log_interface, bool task_create, uint8_t buffers, uint32_t *bufs_size);
extern int r_ble_log_deinit_async(void);
extern void r_ble_log_async_select_dump_buffers(uint8_t buffers);
extern void r_ble_log_async_output_dump_all(bool output);
extern void esp_panic_handler_reconfigure_wdts(uint32_t timeout_ms);
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
extern int ble_controller_deinit(void);
Expand All @@ -140,8 +140,8 @@ extern void esp_unregister_npl_funcs (void);
extern void npl_freertos_mempool_deinit(void);
extern uint32_t r_os_cputime_get32(void);
extern uint32_t r_os_cputime_ticks_to_usecs(uint32_t ticks);
extern void ble_lll_rfmgmt_set_sleep_cb(void *s_cb, void *w_cb, void *s_arg,
void *w_arg, uint32_t us_to_enabled);
extern void r_ble_lll_rfmgmt_set_sleep_cb(void *s_cb, void *w_cb, void *s_arg,
void *w_arg, uint32_t us_to_enabled);
extern void r_ble_rtc_wake_up_state_clr(void);
extern int os_msys_init(void);
extern void os_msys_deinit(void);
Expand All @@ -157,6 +157,7 @@ extern int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv);
extern int ble_txpwr_set(esp_ble_enhanced_power_type_t power_type, uint16_t handle, int power_level);
extern int ble_txpwr_get(esp_ble_enhanced_power_type_t power_type, uint16_t handle);
extern int ble_get_npl_element_info(esp_bt_controller_config_t *cfg, ble_npl_count_info_t * npl_info);
extern char *ble_controller_get_compile_version(void);
extern uint32_t _bt_bss_start;
extern uint32_t _bt_bss_end;
extern uint32_t _bt_controller_bss_start;
Expand Down Expand Up @@ -541,10 +542,10 @@ esp_err_t controller_sleep_init(void)
#ifdef CONFIG_BT_LE_SLEEP_ENABLE
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "BLE modem sleep is enabled");
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
ble_lll_rfmgmt_set_sleep_cb(controller_sleep_cb, controller_wakeup_cb, 0, 0,
r_ble_lll_rfmgmt_set_sleep_cb(controller_sleep_cb, controller_wakeup_cb, 0, 0,
BLE_RTC_DELAY_US_LIGHT_SLEEP);
#else
ble_lll_rfmgmt_set_sleep_cb(controller_sleep_cb, controller_wakeup_cb, 0, 0,
r_ble_lll_rfmgmt_set_sleep_cb(controller_sleep_cb, controller_wakeup_cb, 0, 0,
BLE_RTC_DELAY_US_MODEM_SLEEP);
#endif /* FREERTOS_USE_TICKLESS_IDLE */
#endif // CONFIG_BT_LE_SLEEP_ENABLE
Expand Down Expand Up @@ -624,36 +625,36 @@ typedef enum {
}disc_duplicate_mode_t;


extern void filter_duplicate_mode_enable(disc_duplicate_mode_t mode);
extern void filter_duplicate_mode_disable(disc_duplicate_mode_t mode);
extern void filter_duplicate_set_ring_list_max_num(uint32_t max_num);
extern void scan_duplicate_cache_refresh_set_time(uint32_t period_time);
extern void r_filter_duplicate_mode_enable(disc_duplicate_mode_t mode);
extern void r_filter_duplicate_mode_disable(disc_duplicate_mode_t mode);
extern void r_filter_duplicate_set_ring_list_max_num(uint32_t max_num);
extern void r_scan_duplicate_cache_refresh_set_time(uint32_t period_time);

int
ble_vhci_disc_duplicate_mode_enable(int mode)
{
// TODO: use vendor hci to update
filter_duplicate_mode_enable(mode);
r_filter_duplicate_mode_enable(mode);
return true;
}

int
ble_vhci_disc_duplicate_mode_disable(int mode)
{
// TODO: use vendor hci to update
filter_duplicate_mode_disable(mode);
r_filter_duplicate_mode_disable(mode);
return true;
}

int ble_vhci_disc_duplicate_set_max_cache_size(int max_cache_size){
// TODO: use vendor hci to update
filter_duplicate_set_ring_list_max_num(max_cache_size);
r_filter_duplicate_set_ring_list_max_num(max_cache_size);
return true;
}

int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time){
// TODO: use vendor hci to update
scan_duplicate_cache_refresh_set_time(refresh_period_time);
r_scan_duplicate_cache_refresh_set_time(refresh_period_time);
return true;
}

Expand Down Expand Up @@ -785,6 +786,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
goto modem_deint;
}

ESP_LOGI(NIMBLE_PORT_LOG_TAG, "ble controller commit:[%s]", ble_controller_get_compile_version());
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
interface_func_t bt_controller_log_interface;
bt_controller_log_interface = esp_bt_controller_log_interface;
Expand All @@ -796,9 +798,9 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
buffers |= ESP_BLE_LOG_BUF_HCI;
#endif // CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
#if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
ret = ble_log_init_async(bt_controller_log_interface, false, buffers, (uint32_t *)log_bufs_size);
ret = r_ble_log_init_async(bt_controller_log_interface, false, buffers, (uint32_t *)log_bufs_size);
#else
ret = ble_log_init_async(bt_controller_log_interface, true, buffers, (uint32_t *)log_bufs_size);
ret = r_ble_log_init_async(bt_controller_log_interface, true, buffers, (uint32_t *)log_bufs_size);
#endif // CONFIG_BT_CONTROLLER_LOG_DUMP
if (ret != ESP_OK) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_log_init failed %d", ret);
Expand Down Expand Up @@ -835,7 +837,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
controller_sleep_deinit();
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
controller_init_err:
ble_log_deinit_async();
r_ble_log_deinit_async();
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
os_msys_deinit();
ble_controller_deinit();
Expand Down Expand Up @@ -871,7 +873,7 @@ esp_err_t esp_bt_controller_deinit(void)
modem_clock_module_disable(PERIPH_BT_MODULE);

#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
ble_log_deinit_async();
r_ble_log_deinit_async();
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
ble_controller_deinit();

Expand Down Expand Up @@ -1191,16 +1193,15 @@ void esp_ble_controller_log_dump_all(bool output)
portENTER_CRITICAL_SAFE(&spinlock);
esp_panic_handler_reconfigure_wdts(5000);
BT_ASSERT_PRINT("\r\n[DUMP_START:");
ble_log_async_output_dump_all(output);
r_ble_log_async_output_dump_all(output);
BT_ASSERT_PRINT(":DUMP_END]\r\n");
portEXIT_CRITICAL_SAFE(&spinlock);
}
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED


#if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true)
#if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)
#if CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
#define BLE_SM_KEY_ERR 0x17

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#include "mbedtls/aes.h"
#if CONFIG_BT_LE_SM_SC
Expand All @@ -1210,22 +1211,27 @@ void esp_ble_controller_log_dump_all(bool output)
#include "mbedtls/cmac.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/ecp.h"

static mbedtls_ecp_keypair keypair;
#endif // CONFIG_BT_LE_SM_SC

#else
#include "tinycrypt/aes.h"
#include "tinycrypt/constants.h"
#include "tinycrypt/utils.h"

#if CONFIG_BT_LE_SM_SC
#include "tinycrypt/cmac_mode.h"
#include "tinycrypt/ecc_dh.h"
#endif // CONFIG_BT_LE_SM_SC
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#if CONFIG_BT_LE_SM_SC
static mbedtls_ecp_keypair keypair;
#endif // CONFIG_BT_LE_SM_SC
#endif// CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
/* Based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
static const uint8_t ble_sm_alg_dbg_priv_key[32] = {
0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3,
0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
};

int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y,
const uint8_t *our_priv_key, uint8_t *out_dhkey)
Expand Down Expand Up @@ -1272,8 +1278,7 @@ int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_
}

/* Set PRNG */
if ((rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
NULL, 0)) != 0) {
if ((rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)) != 0) {
goto exit;
}

Expand Down Expand Up @@ -1323,19 +1328,10 @@ int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_
return 0;
}

/* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
static const uint8_t ble_sm_alg_dbg_priv_key[32] = {
0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3,
0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
};

#if CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
static int mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)
{
int rc = BLE_SM_KEY_ERR;
size_t olen = 0;
uint8_t pub[65] = {0};
mbedtls_entropy_context entropy = {0};
mbedtls_ctr_drbg_context ctr_drbg = {0};

Expand All @@ -1357,11 +1353,11 @@ static int mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)
goto exit;
}

size_t olen = 0;
uint8_t pub[65] = {0};

if ((rc = mbedtls_ecp_point_write_binary(&keypair.MBEDTLS_PRIVATE(grp),
&keypair.MBEDTLS_PRIVATE(Q),
MBEDTLS_ECP_PF_UNCOMPRESSED,
&olen, pub, 65)) != 0) {
if ((rc = mbedtls_ecp_point_write_binary(&keypair.MBEDTLS_PRIVATE(grp), &keypair.MBEDTLS_PRIVATE(Q), MBEDTLS_ECP_PF_UNCOMPRESSED,
&olen, pub, 65)) != 0) {
goto exit;
}

Expand All @@ -1377,7 +1373,7 @@ static int mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)

return 0;
}
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS

/**
* pub: 64 bytes
Expand All @@ -1401,15 +1397,17 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) {
return BLE_SM_KEY_ERR;
}
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
#endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS
/* Make sure generated key isn't debug key. */
} while (memcmp(priv, ble_sm_alg_dbg_priv_key, 32) == 0);

swap_buf(pub, pk, 32);
swap_buf(&pub[32], &pk[32], 32);
swap_in_place(priv, 32);
#endif // CONFIG_BT_LE_SM_SC_DEBUG_KEYS

return 0;
}
#endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true)

#endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
#endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)

Loading

0 comments on commit 1c2a671

Please sign in to comment.