From aa581523c97e44b40e164c169f4b58823b584e5c Mon Sep 17 00:00:00 2001 From: Deomid rojer Ryabkov Date: Sun, 30 Mar 2025 00:49:08 +0200 Subject: [PATCH 1/3] feat(mbedtls): Add mbedtls_esp_random() Suitable for passing as f_rng to various Mbed-TLS APIs that require it --- components/mbedtls/port/esp_hardware.c | 7 +++++ .../mbedtls/port/include/mbedtls/esp_random.h | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 components/mbedtls/port/include/mbedtls/esp_random.h diff --git a/components/mbedtls/port/esp_hardware.c b/components/mbedtls/port/esp_hardware.c index 6376ca8f8c..4168b3c9d3 100644 --- a/components/mbedtls/port/esp_hardware.c +++ b/components/mbedtls/port/esp_hardware.c @@ -9,6 +9,7 @@ #include #include #include "esp_random.h" +#include "mbedtls/esp_random.h" #include @@ -23,3 +24,9 @@ int mbedtls_hardware_poll( void *data, *olen = len; return 0; } + +int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len) +{ + esp_fill_random(buf, len); + return 0; +} diff --git a/components/mbedtls/port/include/mbedtls/esp_random.h b/components/mbedtls/port/include/mbedtls/esp_random.h new file mode 100644 index 0000000000..7e5e7a1d4c --- /dev/null +++ b/components/mbedtls/port/include/mbedtls/esp_random.h @@ -0,0 +1,29 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief MbedTLS-compatible RNG function + * + * @note Suitable for passing as f_rng to various Mbed-TLS APIs that require it. + * + * @param ctx User-supplied context + * @param buf Pointer to buffer to fill with random numbers. + * @param len Length of buffer in bytes + * + * @return 0 (success) + */ +int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len); + +#ifdef __cplusplus +} +#endif From f933e51ad9fb752f7aca6d42e556f70f538eadf0 Mon Sep 17 00:00:00 2001 From: Deomid rojer Ryabkov Date: Sun, 30 Mar 2025 00:55:15 +0200 Subject: [PATCH 2/3] change(wpa_supplicant): Replace use of mbedtls_ctr_drbg with esp_mbedtls_random() This way wpa_supplicant does not require ctr_drbg that saves some space --- .../src/crypto/crypto_mbedtls-bignum.c | 8 +- .../src/crypto/crypto_mbedtls-ec.c | 95 +++-------------- .../src/crypto/crypto_mbedtls-rsa.c | 100 +++--------------- .../src/crypto/crypto_mbedtls.c | 2 - .../esp_supplicant/src/crypto/tls_mbedtls.c | 21 +--- 5 files changed, 32 insertions(+), 194 deletions(-) diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c index 8ef5e37c57..895bdfd803 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c @@ -7,6 +7,7 @@ #ifdef ESP_PLATFORM #include "esp_system.h" #include "mbedtls/bignum.h" +#include "mbedtls/esp_random.h" #endif #include "utils/includes.h" @@ -16,11 +17,6 @@ #include "sha256.h" #include "mbedtls/pk.h" -static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) -{ - return random_get_bytes(buf, len); -} - struct crypto_bignum *crypto_bignum_init(void) { mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi)); @@ -220,7 +216,7 @@ int crypto_bignum_is_odd(const struct crypto_bignum *a) int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m) { return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m, - crypto_rng_wrapper, NULL) != 0) ? -1 : 0); + mbedtls_esp_random, NULL) != 0) ? -1 : 0); } int crypto_bignum_legendre(const struct crypto_bignum *a, diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c index a08624fbc9..32ec4bb76c 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c @@ -7,6 +7,7 @@ #ifdef ESP_PLATFORM #include "esp_system.h" #include "mbedtls/bignum.h" +#include "mbedtls/esp_random.h" #endif #include "utils/includes.h" @@ -16,8 +17,6 @@ #include "random.h" #include "mbedtls/ecp.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" #include "mbedtls/pk.h" #include "mbedtls/ecdh.h" @@ -36,10 +35,6 @@ #endif #ifdef CONFIG_ECC -static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) -{ - return random_get_bytes(buf, len); -} struct crypto_ec *crypto_ec_init(int group) { @@ -294,24 +289,14 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, struct crypto_ec_point *res) { int ret; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); - - MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - NULL, 0)); - MBEDTLS_MPI_CHK(mbedtls_ecp_mul((mbedtls_ecp_group *)e, (mbedtls_ecp_point *) res, (const mbedtls_mpi *)b, (const mbedtls_ecp_point *)p, - mbedtls_ctr_drbg_random, - &ctr_drbg)); + mbedtls_esp_random, + NULL)); + cleanup: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); return ret ? -1 : 0; } @@ -491,23 +476,10 @@ int crypto_ec_point_cmp(const struct crypto_ec *e, int crypto_ec_key_compare(struct crypto_ec_key *key1, struct crypto_ec_key *key2) { - int ret = 0; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); - - MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)); - if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { - goto cleanup; + if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_esp_random, NULL) < 0) { + return 0; } - - ret = 1; -cleanup: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - return ret; + return 1; } void crypto_debug_print_point(const char *title, struct crypto_ec *e, @@ -707,7 +679,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *privkey, size_t privkey wpa_printf(MSG_ERROR, "memory allocation failed"); return NULL; } - ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL); + ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, mbedtls_esp_random, NULL); if (ret < 0) { //crypto_print_error_string(ret); @@ -763,17 +735,8 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer, mbedtls_ecdh_context *ctx = NULL; mbedtls_pk_context *own = (mbedtls_pk_context *)key_own; mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; int ret = -1; - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&ctr_drbg); - - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) { - goto fail; - } - *secret_len = 0; ctx = os_malloc(sizeof(*ctx)); if (!ctx) { @@ -801,7 +764,7 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer, } if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN, - mbedtls_ctr_drbg_random, &ctr_drbg) < 0) { + mbedtls_esp_random, NULL) < 0) { wpa_printf(MSG_ERROR, "failed to calculate secret"); goto fail; } @@ -814,8 +777,6 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer, ret = 0; fail: - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); if (ctx) { mbedtls_ecdh_free(ctx); os_free(ctx); @@ -840,7 +801,7 @@ int crypto_ecdsa_get_sign(unsigned char *hash, goto fail; } ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s, - &ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL); + &ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, mbedtls_esp_random, NULL); fail: mbedtls_ecdsa_free(ctx); @@ -939,7 +900,7 @@ struct crypto_ec_key * crypto_ec_key_gen(u16 ike_group) } mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument - crypto_rng_wrapper, NULL); + mbedtls_esp_random, NULL); return (struct crypto_ec_key *)kctx; fail: @@ -1124,8 +1085,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh) struct crypto_ecdh * crypto_ecdh_init(int group) { - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_context entropy; mbedtls_ecdh_context *ctx; ctx = os_zalloc(sizeof(*ctx)); @@ -1143,24 +1102,12 @@ struct crypto_ecdh * crypto_ecdh_init(int group) goto fail; } - /* Initialize CTR_DRBG context */ - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_entropy_init(&entropy); - - /* Seed and setup CTR_DRBG entropy source for future reseeds */ - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { - wpa_printf(MSG_ERROR, "Seeding entropy source failed"); - goto fail; - } - /* Generates ECDH keypair on elliptic curve */ - if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg) != 0) { + if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_esp_random, NULL) != 0) { wpa_printf(MSG_ERROR, "ECDH keypair on curve failed"); goto fail; } - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); return (struct crypto_ecdh *)ctx; fail: if (ctx) { @@ -1168,8 +1115,6 @@ fail: os_free(ctx); ctx = NULL; } - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); return NULL; } @@ -1217,18 +1162,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, return 0; } - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_entropy_context entropy; - - /* Initialize CTR_DRBG context */ - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_entropy_init(&entropy); - - /* Seed and setup CTR_DRBG entropy source for future reseeds */ - if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { - wpa_printf(MSG_ERROR, "Seeding entropy source failed"); - goto cleanup; - } len_prime = ACCESS_ECDH(ctx, grp).pbits / 8; bn_x = crypto_bignum_init_set(key, len); @@ -1287,7 +1220,7 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, /* Calculate secret z = F(DH(x,Y)) */ - secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg); + secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_esp_random, NULL); if (secret_key != 0) { wpa_printf(MSG_ERROR, "Calculation of secret failed"); goto cleanup; @@ -1302,8 +1235,6 @@ cleanup: crypto_ec_key_deinit(pkey); crypto_bignum_deinit(bn_x, 1); crypto_ec_point_deinit(ec_pt, 1); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); return sh_secret; } diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c index 5eb0220fd0..6de2e81a1c 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c @@ -6,6 +6,7 @@ #ifdef ESP_PLATFORM #include "mbedtls/bignum.h" +#include "mbedtls/esp_random.h" #endif #include "utils/includes.h" @@ -14,9 +15,6 @@ #include "common/defs.h" #ifdef CONFIG_CRYPTO_MBEDTLS -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - #include #include #include @@ -38,11 +36,6 @@ static void crypto_dump_verify_info(u32 flags) static void crypto_dump_verify_info(u32 flags) { } #endif -static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) -{ - return os_get_random(buf, len); -} - int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen) { int ret; @@ -125,7 +118,7 @@ struct crypto_private_key * crypto_private_key_import(const u8 *key, mbedtls_pk_init(pkey); ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd, - passwd ? os_strlen(passwd) : 0, crypto_rng_wrapper, NULL); + passwd ? os_strlen(passwd) : 0, mbedtls_esp_random, NULL); if (ret < 0) { wpa_printf(MSG_ERROR, "failed to parse private key"); @@ -190,35 +183,13 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, { int ret; mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - const char *pers = "rsa_encrypt"; - mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) { - os_free(entropy); - } - if (ctr_drbg) { - os_free(ctr_drbg); - } - wpa_printf(MSG_ERROR, "failed to allocate memory"); + if (!pkey) { return -1; } - mbedtls_entropy_init(entropy); - mbedtls_ctr_drbg_init(ctr_drbg); - - ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen(pers)); - if (ret != 0) { - wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d", - ret); - goto cleanup; - } - - ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, - ctr_drbg, inlen, in, out); + ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_esp_random, + NULL, inlen, in, out); if (ret != 0) { wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret); @@ -227,11 +198,6 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); cleanup: - mbedtls_ctr_drbg_free(ctr_drbg); - mbedtls_entropy_free(entropy); - os_free(entropy); - os_free(ctr_drbg); - return ret; } @@ -242,40 +208,18 @@ int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key, int ret; size_t i; mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - const char *pers = "rsa_decrypt"; - mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) { - os_free(entropy); - } - if (ctr_drbg) { - os_free(ctr_drbg); - } + if (!pkey) { return -1; } - mbedtls_ctr_drbg_init(ctr_drbg); - mbedtls_entropy_init(entropy); - ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen(pers)); - - if (ret < 0) { - goto cleanup; - } i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); - ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, - ctr_drbg, &i, in, out, *outlen); + ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_esp_random, + NULL, &i, in, out, *outlen); - *outlen = i; - -cleanup: - mbedtls_ctr_drbg_free(ctr_drbg); - mbedtls_entropy_free(entropy); - os_free(entropy); - os_free(ctr_drbg); + if (ret == 0) { + *outlen = i; + } return ret; } @@ -285,27 +229,13 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, u8 *out, size_t *outlen) { int ret; - const char *pers = "rsa_encrypt"; mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; - mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy)); - mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg)); - if (!pkey || !entropy || !ctr_drbg) { - if (entropy) { - os_free(entropy); - } - if (ctr_drbg) { - os_free(ctr_drbg); - } + if (!pkey) { return -1; } - mbedtls_ctr_drbg_init(ctr_drbg); - mbedtls_entropy_init(entropy); - ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, - entropy, (const unsigned char *) pers, - strlen(pers)); - if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, ctr_drbg, + if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_esp_random, NULL, (mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id), inlen, in, out)) != 0) { wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret); @@ -314,10 +244,6 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, *outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey)); cleanup: - mbedtls_ctr_drbg_free(ctr_drbg); - mbedtls_entropy_free(entropy); - os_free(entropy); - os_free(ctr_drbg); return ret; } diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c index b5f3e65061..89653b789f 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c @@ -15,8 +15,6 @@ #include "sha256.h" #include "mbedtls/ecp.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" #include "mbedtls/md.h" #include "mbedtls/aes.h" #include "mbedtls/bignum.h" diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c index 803bcc74af..df828e0160 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c @@ -13,6 +13,8 @@ #include "crypto/sha256.h" #include "crypto/sha384.h" +#include "mbedtls/esp_random.h" + /* TODO: Remove this once the appropriate solution is found * * ssl_misc.h header uses private elements from @@ -24,8 +26,6 @@ // located at mbedtls/library/ssl_misc.h #include "ssl_misc.h" -#include "mbedtls/ctr_drbg.h" -#include "mbedtls/entropy.h" #include "mbedtls/debug.h" #include "mbedtls/oid.h" #ifdef ESPRESSIF_USE @@ -75,8 +75,6 @@ struct tls_data { typedef struct tls_context { mbedtls_ssl_context ssl; /*!< TLS/SSL context */ - mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */ - mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure */ mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */ mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */ mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */ @@ -105,9 +103,7 @@ static void tls_mbedtls_cleanup(tls_context_t *tls) mbedtls_x509_crt_free(&tls->cacert); mbedtls_x509_crt_free(&tls->clientcert); mbedtls_pk_free(&tls->clientkey); - mbedtls_entropy_free(&tls->entropy); mbedtls_ssl_config_free(&tls->conf); - mbedtls_ctr_drbg_free(&tls->ctr_drbg); mbedtls_ssl_free(&tls->ssl); } @@ -181,7 +177,7 @@ static int set_pki_context(tls_context_t *tls, const struct tls_connection_param ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len, (const unsigned char *)cfg->private_key_passwd, - cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg); + cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_esp_random, NULL); if (ret < 0) { wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret); return ret; @@ -599,9 +595,7 @@ static int tls_create_mbedtls_handle(struct tls_connection *conn, assert(tls != NULL); mbedtls_ssl_init(&tls->ssl); - mbedtls_ctr_drbg_init(&tls->ctr_drbg); mbedtls_ssl_config_init(&tls->conf); - mbedtls_entropy_init(&tls->entropy); ret = set_client_config(params, tls); if (ret != 0) { @@ -609,14 +603,7 @@ static int tls_create_mbedtls_handle(struct tls_connection *conn, goto exit; } - ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func, - &tls->entropy, NULL, 0); - if (ret != 0) { - wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret); - goto exit; - } - - mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg); + mbedtls_ssl_conf_rng(&tls->conf, mbedtls_esp_random, NULL); #if defined(CONFIG_MBEDTLS_SSL_PROTO_TLS1_3) && !defined(CONFIG_TLSV13) /* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config. From 88d71da23f500cd19d2dabdd50224afd46e30cab Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Wed, 28 May 2025 12:03:48 +0530 Subject: [PATCH 3/3] fix(mbedtls): Addressed comments on PR15679 --- components/mbedtls/port/esp_hardware.c | 3 +- .../port/include/mbedtls/esp_mbedtls_random.h | 32 +++++++++++++++++++ .../mbedtls/port/include/mbedtls/esp_random.h | 29 ----------------- .../test_apps/main/test_ds_sign_and_decrypt.c | 2 +- .../src/crypto/crypto_mbedtls-bignum.c | 2 +- .../src/crypto/crypto_mbedtls-ec.c | 2 +- .../src/crypto/crypto_mbedtls-rsa.c | 2 +- .../esp_supplicant/src/crypto/tls_mbedtls.c | 2 +- 8 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h delete mode 100644 components/mbedtls/port/include/mbedtls/esp_random.h diff --git a/components/mbedtls/port/esp_hardware.c b/components/mbedtls/port/esp_hardware.c index 4168b3c9d3..5633cccf3a 100644 --- a/components/mbedtls/port/esp_hardware.c +++ b/components/mbedtls/port/esp_hardware.c @@ -9,7 +9,7 @@ #include #include #include "esp_random.h" -#include "mbedtls/esp_random.h" +#include "mbedtls/esp_mbedtls_random.h" #include @@ -27,6 +27,7 @@ int mbedtls_hardware_poll( void *data, int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len) { + (void) ctx; // unused esp_fill_random(buf, len); return 0; } diff --git a/components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h b/components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h new file mode 100644 index 0000000000..a89575c2dc --- /dev/null +++ b/components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief MbedTLS-compatible RNG function + * + * @note Suitable for passing as f_rng to various MbedTLS APIs that require it. + * It uses esp_fill_random internally, and the caller must ensure that the + * entropy sources of the RNG peripheral are enabled correctly. See the RNG + * chapter in the TRM for more details. + * + * @param ctx User-supplied context + * @param buf Pointer to a buffer to fill with random numbers + * @param len Length of the buffer in bytes + * + * @return 0 on success + */ +int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len); + +#ifdef __cplusplus +} +#endif diff --git a/components/mbedtls/port/include/mbedtls/esp_random.h b/components/mbedtls/port/include/mbedtls/esp_random.h deleted file mode 100644 index 7e5e7a1d4c..0000000000 --- a/components/mbedtls/port/include/mbedtls/esp_random.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief MbedTLS-compatible RNG function - * - * @note Suitable for passing as f_rng to various Mbed-TLS APIs that require it. - * - * @param ctx User-supplied context - * @param buf Pointer to buffer to fill with random numbers. - * @param len Length of buffer in bytes - * - * @return 0 (success) - */ -int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len); - -#ifdef __cplusplus -} -#endif diff --git a/components/mbedtls/test_apps/main/test_ds_sign_and_decrypt.c b/components/mbedtls/test_apps/main/test_ds_sign_and_decrypt.c index 0f1ce9b1c2..2ea61fa5fc 100644 --- a/components/mbedtls/test_apps/main/test_ds_sign_and_decrypt.c +++ b/components/mbedtls/test_apps/main/test_ds_sign_and_decrypt.c @@ -21,7 +21,7 @@ static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in #include "esp_ds.h" #include "esp_ds/esp_ds_rsa.h" -int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len) +static int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len) { if (len == 0 || output == NULL) { return -1; diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c index 895bdfd803..6328136a91 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c @@ -7,7 +7,7 @@ #ifdef ESP_PLATFORM #include "esp_system.h" #include "mbedtls/bignum.h" -#include "mbedtls/esp_random.h" +#include "mbedtls/esp_mbedtls_random.h" #endif #include "utils/includes.h" diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c index 32ec4bb76c..b7c9fab697 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c @@ -7,7 +7,7 @@ #ifdef ESP_PLATFORM #include "esp_system.h" #include "mbedtls/bignum.h" -#include "mbedtls/esp_random.h" +#include "mbedtls/esp_mbedtls_random.h" #endif #include "utils/includes.h" diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c index 6de2e81a1c..649e860a3c 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-rsa.c @@ -6,7 +6,7 @@ #ifdef ESP_PLATFORM #include "mbedtls/bignum.h" -#include "mbedtls/esp_random.h" +#include "mbedtls/esp_mbedtls_random.h" #endif #include "utils/includes.h" diff --git a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c index df828e0160..4eef749d73 100644 --- a/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c +++ b/components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c @@ -13,7 +13,7 @@ #include "crypto/sha256.h" #include "crypto/sha384.h" -#include "mbedtls/esp_random.h" +#include "mbedtls/esp_mbedtls_random.h" /* TODO: Remove this once the appropriate solution is found *