mirror of
https://github.com/espressif/esp-nimble.git
synced 2026-09-18 15:09:55 +00:00
feat(bt): migrate mbedTLS to PSA APIs
This commit is contained in:
committed by
Mahavir Jain
parent
686728c09e
commit
0288e3987a
@@ -12,7 +12,7 @@
|
||||
#include "host/ble_hs.h"
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
#include "mbedtls/aes.h"
|
||||
// #include "mbedtls/aes.h"
|
||||
#else
|
||||
#include "tinycrypt/aes.h"
|
||||
#endif
|
||||
|
||||
@@ -38,13 +38,13 @@
|
||||
#include "../src/ble_hs_hci_priv.h"
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
#include "mbedtls/aes.h"
|
||||
#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"
|
||||
// #include "mbedtls/aes.h"
|
||||
// #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"
|
||||
|
||||
#else
|
||||
#include "tinycrypt/aes.h"
|
||||
|
||||
@@ -15,13 +15,7 @@
|
||||
#include "syscfg/syscfg.h"
|
||||
|
||||
#if (MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS))
|
||||
#include "mbedtls/aes.h"
|
||||
#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"
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#else
|
||||
#include <tinycrypt/constants.h>
|
||||
@@ -41,36 +35,56 @@ int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
|
||||
size_t sg_len, uint8_t mac[16])
|
||||
{
|
||||
int rc = BLE_HS_EUNKNOWN;
|
||||
mbedtls_cipher_context_t ctx = {0};
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_algorithm_t alg = PSA_ALG_CMAC;
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
||||
|
||||
mbedtls_cipher_init(&ctx);
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
|
||||
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
|
||||
if (cipher_info == NULL) {
|
||||
psa_status_t status = psa_import_key(&attributes, key, 16, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
BT_ERR("Failed to import key: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = mbedtls_cipher_cmac_starts(&ctx, key, 128);
|
||||
if (rc != 0) {
|
||||
status = psa_mac_sign_setup(&operation, key_id, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
BT_ERR("Failed to setup MAC sign operation: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (; sg_len; sg_len--, sg++) {
|
||||
if (sg->len != 0 && sg->data != NULL) {
|
||||
if ((rc = mbedtls_cipher_cmac_update(&ctx, sg->data, sg->len)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
status = psa_mac_update(&operation, sg->data, sg->len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
BT_ERR("Failed to update MAC operation: %d", status);
|
||||
psa_mac_abort(&operation);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
rc = mbedtls_cipher_cmac_finish(&ctx, mac);
|
||||
status = psa_mac_sign_finish(&operation, mac, 16, &sg_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
BT_ERR("Failed to finish MAC sign operation: %d", status);
|
||||
psa_mac_abort(&operation);
|
||||
goto exit;
|
||||
}
|
||||
if (sg_len != 16) {
|
||||
BT_ERR("psa_mac_sign_finish returned unexpected length %zu", sg_len);
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto exit;
|
||||
}
|
||||
rc = 0;
|
||||
|
||||
exit:
|
||||
mbedtls_cipher_free(&ctx);
|
||||
if (key_id != 0) {
|
||||
psa_destroy_key(key_id);
|
||||
}
|
||||
if (rc != 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
#include "base64/base64.h"
|
||||
#endif
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
extern uint8_t g_mesh_addr_type;
|
||||
|
||||
#if MYNEWT_VAL(BLE_EXT_ADV)
|
||||
@@ -136,20 +138,32 @@ void net_buf_simple_clone(const struct os_mbuf *original,
|
||||
int
|
||||
bt_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data)
|
||||
{
|
||||
mbedtls_aes_context s = {0};
|
||||
mbedtls_aes_init(&s);
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_algorithm_t alg = PSA_ALG_ECB_NO_PADDING;
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
|
||||
if (mbedtls_aes_setkey_enc(&s, key, 128) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
status = psa_import_key(&attributes, key, 16, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
BT_ERR("psa_import_key failed with status %d", status);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, plaintext, enc_data) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
// For ECB_NO_PADDING with exactly one block, use psa_cipher_encrypt() directly
|
||||
// This is simpler and avoids the psa_cipher_finish() buffer issue
|
||||
size_t output_len = 0;
|
||||
status = psa_cipher_encrypt(key_id, alg, plaintext, 16, enc_data, 16, &output_len);
|
||||
if (status != PSA_SUCCESS || output_len != 16) {
|
||||
BT_ERR("psa_cipher_encrypt failed with status %d", status);
|
||||
psa_destroy_key(key_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
mbedtls_aes_free(&s);
|
||||
psa_destroy_key(key_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ int ble_svc_gap_device_key_material_set(uint8_t *session_key, uint8_t *iv);
|
||||
#endif
|
||||
|
||||
#else
|
||||
#include "ble_svc_gap_stub.h"
|
||||
// #include "ble_svc_gap_stub.h"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
#include "host/ble_aes_ccm.h"
|
||||
#include "../src/ble_hs_conn_priv.h"
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
#include "psa/crypto.h"
|
||||
#else
|
||||
#include "tinycrypt/constants.h"
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(ENC_ADV_DATA)
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -54,20 +60,34 @@ ble_aes_ccm_hex(const void *buf, size_t len)
|
||||
int
|
||||
ble_aes_ccm_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data)
|
||||
{
|
||||
mbedtls_aes_context s = {0};
|
||||
mbedtls_aes_init(&s);
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_algorithm_t alg = PSA_ALG_ECB_NO_PADDING;
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
|
||||
if (mbedtls_aes_setkey_enc(&s, key, 128) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
ESP_LOGI("ble_aes_ccm_encrypt_be", "encrypting data");
|
||||
|
||||
status = psa_import_key(&attributes, key, 16, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE("ble_aes_ccm_encrypt_be", "psa_import_key failed with status %d", status);
|
||||
return -BLE_HS_EINVAL;
|
||||
}
|
||||
|
||||
if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, plaintext, enc_data) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
// For ECB_NO_PADDING with exactly one block, use psa_cipher_encrypt() directly
|
||||
// This is simpler and avoids the psa_cipher_finish() buffer issue
|
||||
size_t output_len = 0;
|
||||
status = psa_cipher_encrypt(key_id, alg, plaintext, 16, enc_data, 16, &output_len);
|
||||
if (status != PSA_SUCCESS || output_len != 16) {
|
||||
ESP_LOGE("ble_aes_ccm_encrypt_be", "psa_cipher_encrypt failed with status %d", status);
|
||||
psa_destroy_key(key_id);
|
||||
return -BLE_HS_EINVAL;
|
||||
}
|
||||
|
||||
mbedtls_aes_free(&s);
|
||||
psa_destroy_key(key_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+123
-139
@@ -28,17 +28,8 @@
|
||||
|
||||
#include "nimble/ble.h"
|
||||
#include "ble_hs_priv.h"
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
#include "mbedtls/aes.h"
|
||||
|
||||
#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"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
#else
|
||||
#include "tinycrypt/aes.h"
|
||||
#include "tinycrypt/constants.h"
|
||||
@@ -56,7 +47,6 @@
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
#if MYNEWT_VAL(BLE_SM_SC)
|
||||
static mbedtls_ecp_keypair keypair;
|
||||
#endif
|
||||
#else
|
||||
#if MYNEWT_VAL(BLE_SM_SC) && MYNEWT_VAL(TRNG)
|
||||
@@ -64,6 +54,9 @@ static struct trng_dev *g_trng;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define BLE_PUB_KEY_LEN 65
|
||||
const char *TAG = "ble_sm_alg";
|
||||
|
||||
/**
|
||||
* Keep forward-compatibility with Mbed TLS 3.x.
|
||||
*
|
||||
@@ -97,22 +90,31 @@ ble_sm_alg_encrypt(const uint8_t *key, const uint8_t *plaintext,
|
||||
swap_buf(tmp, key, 16);
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
mbedtls_aes_context s = {0};
|
||||
|
||||
mbedtls_aes_init(&s);
|
||||
if (mbedtls_aes_setkey_enc(&s, tmp, 128) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
psa_set_key_algorithm(&key_attributes, PSA_ALG_ECB_NO_PADDING);
|
||||
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&key_attributes, 128);
|
||||
status = psa_import_key(&key_attributes, tmp, 16, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to import AES key: %d", status);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
|
||||
swap_buf(tmp, plaintext, 16);
|
||||
|
||||
if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, tmp, enc_data) != 0) {
|
||||
mbedtls_aes_free(&s);
|
||||
size_t output_len = 0;
|
||||
status = psa_cipher_encrypt(key_id, PSA_ALG_ECB_NO_PADDING, tmp,
|
||||
16, enc_data, 16, &output_len);
|
||||
if (status != PSA_SUCCESS || output_len != 16) {
|
||||
ESP_LOGE(TAG, "Encryption failed: %d", status);
|
||||
psa_destroy_key(key_id);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
mbedtls_aes_free(&s);
|
||||
psa_destroy_key(key_id);
|
||||
#else
|
||||
struct tc_aes_key_sched_struct s;
|
||||
|
||||
@@ -252,37 +254,48 @@ int
|
||||
ble_sm_alg_aes_cmac(const uint8_t *key, const uint8_t *in, size_t len,
|
||||
uint8_t *out)
|
||||
{
|
||||
int rc = BLE_HS_EUNKNOWN;
|
||||
mbedtls_cipher_context_t ctx = {0};
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_algorithm_t alg = PSA_ALG_CMAC;
|
||||
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
||||
psa_set_key_algorithm(&key_attributes, PSA_ALG_CMAC);
|
||||
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&key_attributes, 128);
|
||||
status = psa_import_key(&key_attributes, key, 16, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to import key: %d", status);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
|
||||
mbedtls_cipher_init(&ctx);
|
||||
|
||||
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
|
||||
|
||||
if (cipher_info == NULL) {
|
||||
goto exit;
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
status = psa_mac_sign_setup(&operation, key_id, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to setup MAC sign operation: %d", status);
|
||||
psa_destroy_key(key_id);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) {
|
||||
goto exit;
|
||||
size_t output_len = 0;
|
||||
status = psa_mac_update(&operation, in, len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to update MAC operation: %d", status);
|
||||
psa_mac_abort(&operation);
|
||||
psa_destroy_key(key_id);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
rc = mbedtls_cipher_cmac_starts(&ctx, key, 128);
|
||||
if (rc != 0) {
|
||||
goto exit;
|
||||
status = psa_mac_sign_finish(&operation, out, 16, &output_len);
|
||||
if (status != PSA_SUCCESS || output_len != 16) {
|
||||
ESP_LOGE(TAG, "Failed to finish MAC sign operation: %d", status);
|
||||
psa_mac_abort(&operation);
|
||||
psa_destroy_key(key_id);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
rc = mbedtls_cipher_cmac_update(&ctx, in, len);
|
||||
if (rc != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = mbedtls_cipher_cmac_finish(&ctx, out);
|
||||
|
||||
exit:
|
||||
mbedtls_cipher_free(&ctx);
|
||||
return rc;
|
||||
psa_destroy_key(key_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -518,84 +531,55 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_
|
||||
const uint8_t *our_priv_key, uint8_t *out_dhkey)
|
||||
{
|
||||
uint8_t dh[32];
|
||||
uint8_t pk[64];
|
||||
uint8_t pk[BLE_PUB_KEY_LEN];
|
||||
uint8_t priv[32];
|
||||
int rc = BLE_HS_EUNKNOWN;
|
||||
|
||||
swap_buf(pk, peer_pub_key_x, 32);
|
||||
swap_buf(&pk[32], peer_pub_key_y, 32);
|
||||
swap_buf(priv, our_priv_key, 32);
|
||||
|
||||
#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
|
||||
struct mbedtls_ecp_point pt = {0}, Q = {0};
|
||||
mbedtls_mpi z = {0}, d = {0};
|
||||
mbedtls_ctr_drbg_context ctr_drbg = {0};
|
||||
mbedtls_entropy_context entropy = {0};
|
||||
// PSA/mbedTLS expects 65 bytes: 0x04 prefix + X (32 bytes) + Y (32 bytes)
|
||||
pk[0] = 0x04; // Uncompressed format for public key
|
||||
swap_buf(&pk[1], peer_pub_key_x, 32);
|
||||
swap_buf(&pk[33], peer_pub_key_y, 32);
|
||||
|
||||
uint8_t pub[65] = {0};
|
||||
/* Hardcoded first byte of pub key for MBEDTLS_ECP_PF_UNCOMPRESSED */
|
||||
pub[0] = 0x04;
|
||||
memcpy(&pub[1], pk, 64);
|
||||
|
||||
/* Initialize the required structures here */
|
||||
mbedtls_ecp_point_init(&pt);
|
||||
mbedtls_ecp_point_init(&Q);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_mpi_init(&d);
|
||||
mbedtls_mpi_init(&z);
|
||||
|
||||
/* Below 3 steps are to validate public key on curve secp256r1 */
|
||||
if (mbedtls_ecp_group_load(&keypair.MBEDTLS_PRIVATE(grp), MBEDTLS_ECP_DP_SECP256R1) != 0) {
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&key_attributes, 256);
|
||||
psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
|
||||
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
|
||||
status = psa_import_key(&key_attributes, priv, 32, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to import key: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
size_t output_len = 0;
|
||||
status = psa_raw_key_agreement(PSA_ALG_ECDH, key_id, pk, BLE_PUB_KEY_LEN, dh, sizeof(dh), &output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to perform raw key agreement: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mbedtls_ecp_point_read_binary(&keypair.MBEDTLS_PRIVATE(grp), &pt, pub, 65) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mbedtls_ecp_check_pubkey(&keypair.MBEDTLS_PRIVATE(grp), &pt) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Set PRNG */
|
||||
if ( ( rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
NULL, 0) ) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Prepare point Q from pub key */
|
||||
if (mbedtls_ecp_point_read_binary(&keypair.MBEDTLS_PRIVATE(grp), &Q, pub, 65) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mbedtls_mpi_read_binary(&d, priv, 32) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = mbedtls_ecdh_compute_shared(&keypair.MBEDTLS_PRIVATE(grp), &z, &Q, &d,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
if (rc != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = mbedtls_mpi_write_binary(&z, dh, 32);
|
||||
if (rc != 0) {
|
||||
if (output_len != 32) {
|
||||
ESP_LOGE(TAG, "Unexpected output length: %zu", output_len);
|
||||
goto exit;
|
||||
}
|
||||
rc = 0;
|
||||
|
||||
exit:
|
||||
mbedtls_ecp_point_free(&pt);
|
||||
mbedtls_mpi_free(&z);
|
||||
mbedtls_mpi_free(&d);
|
||||
mbedtls_ecp_point_free(&Q);
|
||||
mbedtls_entropy_free(&entropy);
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
psa_destroy_key(key_id);
|
||||
if (rc != 0) {
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
#else
|
||||
// TinyCrypt/uECC expects 64 bytes: X (32 bytes) + Y (32 bytes), no prefix
|
||||
swap_buf(pk, peer_pub_key_x, 32);
|
||||
swap_buf(&pk[32], peer_pub_key_y, 32);
|
||||
|
||||
if (uECC_valid_public_key(pk, uECC_secp256r1()) < 0) {
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
@@ -635,47 +619,44 @@ static int
|
||||
mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)
|
||||
{
|
||||
int rc = BLE_HS_EUNKNOWN;
|
||||
mbedtls_entropy_context entropy = {0};
|
||||
mbedtls_ctr_drbg_context ctr_drbg = {0};
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_algorithm_t alg = PSA_ALG_ECDH;
|
||||
psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1);
|
||||
psa_key_usage_t key_usage = PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT;
|
||||
|
||||
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
/* Free the previously allocate keypair */
|
||||
mbedtls_ecp_keypair_free(&keypair);
|
||||
|
||||
mbedtls_ecp_keypair_init(&keypair);
|
||||
|
||||
if (( rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
NULL, 0)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if ((rc = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, &keypair,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (( rc = mbedtls_mpi_write_binary(&keypair.MBEDTLS_PRIVATE(d), private_key, 32)) != 0) {
|
||||
psa_set_key_type(&key_attributes, key_type);
|
||||
psa_set_key_bits(&key_attributes, 256);
|
||||
psa_set_key_algorithm(&key_attributes, alg);
|
||||
psa_set_key_usage_flags(&key_attributes, key_usage);
|
||||
status = psa_generate_key(&key_attributes, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to generate key: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
|
||||
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) {
|
||||
status = psa_export_public_key(key_id, public_key, BLE_PUB_KEY_LEN, &olen);
|
||||
if (status != PSA_SUCCESS || olen != BLE_PUB_KEY_LEN) {
|
||||
ESP_LOGE(TAG, "Failed to export public key: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy(public_key, &pub[1], 64);
|
||||
status = psa_export_key(key_id, private_key, 32, &olen);
|
||||
if (status != PSA_SUCCESS || olen != 32) {
|
||||
ESP_LOGE(TAG, "Failed to export private key: %d", status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
if (key_id != 0) {
|
||||
psa_destroy_key(key_id);
|
||||
}
|
||||
if (rc != 0) {
|
||||
mbedtls_ecp_keypair_free(&keypair);
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
|
||||
@@ -684,7 +665,7 @@ exit:
|
||||
|
||||
void mbedtls_free_keypair(void)
|
||||
{
|
||||
mbedtls_ecp_keypair_free(&keypair);
|
||||
// mbedtls_ecp_keypair_free(&keypair);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -700,7 +681,7 @@ ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
|
||||
swap_buf(&pub[32], &ble_sm_alg_dbg_pub_key[32], 32);
|
||||
swap_buf(priv, ble_sm_alg_dbg_priv_key, 32);
|
||||
#else
|
||||
uint8_t pk[64];
|
||||
uint8_t pk[65];
|
||||
|
||||
do {
|
||||
|
||||
@@ -708,17 +689,20 @@ ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
|
||||
if (mbedtls_gen_keypair(pk, priv) != 0) {
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
// PSA/mbedTLS: pk[0]=0x04, pk[1..32]=X, pk[33..64]=Y
|
||||
swap_buf(pub, &pk[1], 32); // Extract X (skip 0x04 prefix)
|
||||
swap_buf(&pub[32], &pk[33], 32); // Extract Y
|
||||
#else
|
||||
if (uECC_make_key(pk, priv, uECC_secp256r1()) != TC_CRYPTO_SUCCESS) {
|
||||
return BLE_HS_EUNKNOWN;
|
||||
}
|
||||
// TinyCrypt/uECC: pk[0..31]=X, pk[32..63]=Y (no prefix)
|
||||
swap_buf(pub, pk, 32); // Extract X (from start)
|
||||
swap_buf(&pub[32], &pk[32], 32); // Extract Y
|
||||
#endif
|
||||
|
||||
/* 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user