From 82c1ae72daea4cef4481a6fb3928f7b8868395dd Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 10 Feb 2022 10:04:32 +0530 Subject: [PATCH] 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 --- nimble/host/src/ble_sm_alg.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c index e0bd5f504..76b3baea9 100644 --- a/nimble/host/src/ble_sm_alg.c +++ b/nimble/host/src/ble_sm_alg.c @@ -66,6 +66,20 @@ static struct trng_dev *g_trng; #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 ble_sm_alg_xor_128(const uint8_t *p, const uint8_t *q, uint8_t *r) { @@ -534,15 +548,15 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_ mbedtls_mpi_init(&z); /* 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; } - 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; } - if (mbedtls_ecp_check_pubkey(&keypair.grp, &pt) != 0) { + if (mbedtls_ecp_check_pubkey(&keypair.MBEDTLS_PRIVATE(grp), &pt) != 0) { goto exit; } @@ -553,7 +567,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 */ - 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; } @@ -561,7 +575,7 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_ 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); if (rc != 0) { goto exit; @@ -641,14 +655,14 @@ mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key) 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; } size_t olen = 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) { goto exit; }