mirror of
https://github.com/espressif/esp-nimble.git
synced 2026-09-10 19:20:01 +00:00
ble_sm_alg.c: Added changes for mbedTLS v3.1.0
- Added MBEDTLS_PRIVATE for accessing private struct members wherever necessary - Added macros for version check for backward compatibility
This commit is contained in:
committed by
Mahavir Jain
parent
1dc1ec6e76
commit
51f4b84aca
@@ -65,6 +65,20 @@ static struct trng_dev *g_trng;
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep forward-compatibility with Mbed TLS 3.x.
|
||||||
|
*
|
||||||
|
* Direct access to fields of structures declared in public headers is no longer
|
||||||
|
* supported. In Mbed TLS 3, the layout of structures is not considered part of
|
||||||
|
* the stable API, and minor versions (3.1, 3.2, etc.) may add, remove, rename,
|
||||||
|
* reorder or change the type of structure fields.
|
||||||
|
*/
|
||||||
|
#if (MBEDTLS_VERSION_NUMBER < 0x03000000)
|
||||||
|
#ifndef MBEDTLS_PRIVATE
|
||||||
|
#define MBEDTLS_PRIVATE(member) member
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ble_sm_alg_xor_128(const uint8_t *p, const uint8_t *q, uint8_t *r)
|
ble_sm_alg_xor_128(const uint8_t *p, const uint8_t *q, uint8_t *r)
|
||||||
{
|
{
|
||||||
@@ -533,15 +547,15 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_
|
|||||||
mbedtls_mpi_init(&z);
|
mbedtls_mpi_init(&z);
|
||||||
|
|
||||||
/* Below 3 steps are to validate public key on curve secp256r1 */
|
/* Below 3 steps are to validate public key on curve secp256r1 */
|
||||||
if (mbedtls_ecp_group_load(&keypair.grp, MBEDTLS_ECP_DP_SECP256R1) != 0) {
|
if (mbedtls_ecp_group_load(&keypair.MBEDTLS_PRIVATE(grp), MBEDTLS_ECP_DP_SECP256R1) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mbedtls_ecp_point_read_binary(&keypair.grp, &pt, pub, 65) != 0) {
|
if (mbedtls_ecp_point_read_binary(&keypair.MBEDTLS_PRIVATE(grp), &pt, pub, 65) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mbedtls_ecp_check_pubkey(&keypair.grp, &pt) != 0) {
|
if (mbedtls_ecp_check_pubkey(&keypair.MBEDTLS_PRIVATE(grp), &pt) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -552,7 +566,7 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare point Q from pub key */
|
/* Prepare point Q from pub key */
|
||||||
if (mbedtls_ecp_point_read_binary(&keypair.grp, &Q, pub, 65) != 0) {
|
if (mbedtls_ecp_point_read_binary(&keypair.MBEDTLS_PRIVATE(grp), &Q, pub, 65) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -560,7 +574,7 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mbedtls_ecdh_compute_shared(&keypair.grp, &z, &Q, &d,
|
rc = mbedtls_ecdh_compute_shared(&keypair.MBEDTLS_PRIVATE(grp), &z, &Q, &d,
|
||||||
mbedtls_ctr_drbg_random, &ctr_drbg);
|
mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
@@ -640,14 +654,14 @@ mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key)
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (( rc = mbedtls_mpi_write_binary(&keypair.d, private_key, 32)) != 0) {
|
if (( rc = mbedtls_mpi_write_binary(&keypair.MBEDTLS_PRIVATE(d), private_key, 32)) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t olen = 0;
|
size_t olen = 0;
|
||||||
uint8_t pub[65] = {0};
|
uint8_t pub[65] = {0};
|
||||||
|
|
||||||
if ((rc = mbedtls_ecp_point_write_binary(&keypair.grp, &keypair.Q, MBEDTLS_ECP_PF_UNCOMPRESSED,
|
if ((rc = mbedtls_ecp_point_write_binary(&keypair.MBEDTLS_PRIVATE(grp), &keypair.MBEDTLS_PRIVATE(Q), MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||||
&olen, pub, 65)) != 0) {
|
&olen, pub, 65)) != 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user