Skip to content

Commit 4495803

Browse files
committed
Merge branch 'bugfix/cleanup_ctr_drbg_v5.4' into 'release/v5.4'
wpa_supplicant: Replace use of mbedtls_ctr_drbg with esp_mbedtls_random() (v5.4) See merge request espressif/esp-idf!42376
2 parents 5d1bfef + 3cf1f74 commit 4495803

File tree

8 files changed

+73
-195
lines changed

8 files changed

+73
-195
lines changed

components/mbedtls/port/esp_hardware.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdlib.h>
1010
#include <stdio.h>
1111
#include "esp_random.h"
12+
#include "mbedtls/esp_mbedtls_random.h"
1213

1314
#include <entropy_poll.h>
1415

@@ -23,3 +24,10 @@ int mbedtls_hardware_poll( void *data,
2324
*olen = len;
2425
return 0;
2526
}
27+
28+
int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len)
29+
{
30+
(void) ctx; // unused
31+
esp_fill_random(buf, len);
32+
return 0;
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include <stddef.h>
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
/**
15+
* @brief MbedTLS-compatible RNG function
16+
*
17+
* @note Suitable for passing as f_rng to various MbedTLS APIs that require it.
18+
* It uses esp_fill_random internally, and the caller must ensure that the
19+
* entropy sources of the RNG peripheral are enabled correctly. See the RNG
20+
* chapter in the TRM for more details.
21+
*
22+
* @param ctx User-supplied context
23+
* @param buf Pointer to a buffer to fill with random numbers
24+
* @param len Length of the buffer in bytes
25+
*
26+
* @return 0 on success
27+
*/
28+
int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif

components/mbedtls/test_apps/main/test_ds_sign_and_decrypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in
2121
#include "esp_ds.h"
2222
#include "esp_ds/esp_ds_rsa.h"
2323

24-
int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len)
24+
static int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len)
2525
{
2626
if (len == 0 || output == NULL) {
2727
return -1;

components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifdef ESP_PLATFORM
88
#include "esp_system.h"
99
#include "mbedtls/bignum.h"
10+
#include "mbedtls/esp_mbedtls_random.h"
1011
#endif
1112

1213
#include "utils/includes.h"
@@ -16,11 +17,6 @@
1617
#include "sha256.h"
1718
#include "mbedtls/pk.h"
1819

19-
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
20-
{
21-
return random_get_bytes(buf, len);
22-
}
23-
2420
struct crypto_bignum *crypto_bignum_init(void)
2521
{
2622
mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi));
@@ -220,7 +216,7 @@ int crypto_bignum_is_odd(const struct crypto_bignum *a)
220216
int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
221217
{
222218
return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m,
223-
crypto_rng_wrapper, NULL) != 0) ? -1 : 0);
219+
mbedtls_esp_random, NULL) != 0) ? -1 : 0);
224220
}
225221

226222
int crypto_bignum_legendre(const struct crypto_bignum *a,

components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-ec.c

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifdef ESP_PLATFORM
88
#include "esp_system.h"
99
#include "mbedtls/bignum.h"
10+
#include "mbedtls/esp_mbedtls_random.h"
1011
#endif
1112

1213
#include "utils/includes.h"
@@ -16,8 +17,6 @@
1617
#include "random.h"
1718

1819
#include "mbedtls/ecp.h"
19-
#include "mbedtls/entropy.h"
20-
#include "mbedtls/ctr_drbg.h"
2120

2221
#include "mbedtls/pk.h"
2322
#include "mbedtls/ecdh.h"
@@ -36,10 +35,6 @@
3635
#endif
3736

3837
#ifdef CONFIG_ECC
39-
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
40-
{
41-
return random_get_bytes(buf, len);
42-
}
4338

4439
struct crypto_ec *crypto_ec_init(int group)
4540
{
@@ -258,24 +253,14 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
258253
struct crypto_ec_point *res)
259254
{
260255
int ret;
261-
mbedtls_entropy_context entropy;
262-
mbedtls_ctr_drbg_context ctr_drbg;
263-
264-
mbedtls_entropy_init(&entropy);
265-
mbedtls_ctr_drbg_init(&ctr_drbg);
266-
267-
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
268-
NULL, 0));
269-
270256
MBEDTLS_MPI_CHK(mbedtls_ecp_mul((mbedtls_ecp_group *)e,
271257
(mbedtls_ecp_point *) res,
272258
(const mbedtls_mpi *)b,
273259
(const mbedtls_ecp_point *)p,
274-
mbedtls_ctr_drbg_random,
275-
&ctr_drbg));
260+
mbedtls_esp_random,
261+
NULL));
262+
276263
cleanup:
277-
mbedtls_ctr_drbg_free(&ctr_drbg);
278-
mbedtls_entropy_free(&entropy);
279264
return ret ? -1 : 0;
280265
}
281266

@@ -455,23 +440,10 @@ int crypto_ec_point_cmp(const struct crypto_ec *e,
455440

456441
int crypto_ec_key_compare(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
457442
{
458-
int ret = 0;
459-
mbedtls_entropy_context entropy;
460-
mbedtls_ctr_drbg_context ctr_drbg;
461-
462-
mbedtls_entropy_init(&entropy);
463-
mbedtls_ctr_drbg_init(&ctr_drbg);
464-
465-
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0));
466-
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
467-
goto cleanup;
443+
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_esp_random, NULL) < 0) {
444+
return 0;
468445
}
469-
470-
ret = 1;
471-
cleanup:
472-
mbedtls_ctr_drbg_free(&ctr_drbg);
473-
mbedtls_entropy_free(&entropy);
474-
return ret;
446+
return 1;
475447
}
476448

477449
void crypto_debug_print_point(const char *title, struct crypto_ec *e,
@@ -671,7 +643,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *privkey, size_t privkey
671643
wpa_printf(MSG_ERROR, "memory allocation failed");
672644
return NULL;
673645
}
674-
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL);
646+
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, mbedtls_esp_random, NULL);
675647

676648
if (ret < 0) {
677649
//crypto_print_error_string(ret);
@@ -727,17 +699,8 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
727699
mbedtls_ecdh_context *ctx = NULL;
728700
mbedtls_pk_context *own = (mbedtls_pk_context *)key_own;
729701
mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer;
730-
mbedtls_entropy_context entropy;
731-
mbedtls_ctr_drbg_context ctr_drbg;
732702
int ret = -1;
733703

734-
mbedtls_entropy_init(&entropy);
735-
mbedtls_ctr_drbg_init(&ctr_drbg);
736-
737-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) {
738-
goto fail;
739-
}
740-
741704
*secret_len = 0;
742705
ctx = os_malloc(sizeof(*ctx));
743706
if (!ctx) {
@@ -765,7 +728,7 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
765728
}
766729

767730
if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN,
768-
mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
731+
mbedtls_esp_random, NULL) < 0) {
769732
wpa_printf(MSG_ERROR, "failed to calculate secret");
770733
goto fail;
771734
}
@@ -778,8 +741,6 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
778741
ret = 0;
779742

780743
fail:
781-
mbedtls_ctr_drbg_free(&ctr_drbg);
782-
mbedtls_entropy_free(&entropy);
783744
if (ctx) {
784745
mbedtls_ecdh_free(ctx);
785746
os_free(ctx);
@@ -804,7 +765,7 @@ int crypto_ecdsa_get_sign(unsigned char *hash,
804765
goto fail;
805766
}
806767
ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s,
807-
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL);
768+
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, mbedtls_esp_random, NULL);
808769

809770
fail:
810771
mbedtls_ecdsa_free(ctx);
@@ -901,7 +862,7 @@ struct crypto_ec_key * crypto_ec_key_gen(u16 ike_group)
901862
}
902863

903864
mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument
904-
crypto_rng_wrapper, NULL);
865+
mbedtls_esp_random, NULL);
905866

906867
return (struct crypto_ec_key *)kctx;
907868
fail:
@@ -1081,8 +1042,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
10811042

10821043
struct crypto_ecdh * crypto_ecdh_init(int group)
10831044
{
1084-
mbedtls_ctr_drbg_context ctr_drbg;
1085-
mbedtls_entropy_context entropy;
10861045
mbedtls_ecdh_context *ctx;
10871046

10881047
ctx = os_zalloc(sizeof(*ctx));
@@ -1100,33 +1059,19 @@ struct crypto_ecdh * crypto_ecdh_init(int group)
11001059
goto fail;
11011060
}
11021061

1103-
/* Initialize CTR_DRBG context */
1104-
mbedtls_ctr_drbg_init(&ctr_drbg);
1105-
mbedtls_entropy_init(&entropy);
1106-
1107-
/* Seed and setup CTR_DRBG entropy source for future reseeds */
1108-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
1109-
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
1110-
goto fail;
1111-
}
1112-
11131062
/* Generates ECDH keypair on elliptic curve */
1114-
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
1063+
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_esp_random, NULL) != 0) {
11151064
wpa_printf(MSG_ERROR, "ECDH keypair on curve failed");
11161065
goto fail;
11171066
}
11181067

1119-
mbedtls_ctr_drbg_free(&ctr_drbg);
1120-
mbedtls_entropy_free(&entropy);
11211068
return (struct crypto_ecdh *)ctx;
11221069
fail:
11231070
if (ctx) {
11241071
mbedtls_ecdh_free(ctx);
11251072
os_free(ctx);
11261073
ctx = NULL;
11271074
}
1128-
mbedtls_ctr_drbg_free(&ctr_drbg);
1129-
mbedtls_entropy_free(&entropy);
11301075
return NULL;
11311076
}
11321077

@@ -1174,18 +1119,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
11741119
return 0;
11751120
}
11761121

1177-
mbedtls_ctr_drbg_context ctr_drbg;
1178-
mbedtls_entropy_context entropy;
1179-
1180-
/* Initialize CTR_DRBG context */
1181-
mbedtls_ctr_drbg_init(&ctr_drbg);
1182-
mbedtls_entropy_init(&entropy);
1183-
1184-
/* Seed and setup CTR_DRBG entropy source for future reseeds */
1185-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
1186-
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
1187-
goto cleanup;
1188-
}
11891122
len_prime = ACCESS_ECDH(ctx, grp).pbits / 8;
11901123
bn_x = crypto_bignum_init_set(key, len);
11911124

@@ -1244,7 +1177,7 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12441177

12451178
/* Calculate secret
12461179
z = F(DH(x,Y)) */
1247-
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg);
1180+
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_esp_random, NULL);
12481181
if (secret_key != 0) {
12491182
wpa_printf(MSG_ERROR, "Calculation of secret failed");
12501183
goto cleanup;
@@ -1259,8 +1192,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12591192
crypto_ec_key_deinit(pkey);
12601193
crypto_bignum_deinit(bn_x, 1);
12611194
crypto_ec_point_deinit(ec_pt, 1);
1262-
mbedtls_ctr_drbg_free(&ctr_drbg);
1263-
mbedtls_entropy_free(&entropy);
12641195
return sh_secret;
12651196
}
12661197

0 commit comments

Comments
 (0)