Skip to content

Commit b789198

Browse files
committed
Merge branch 'bugfix/cleanup_ctr_drbg' into 'master'
wpa_supplicant: Replace use of mbedtls_ctr_drbg with esp_mbedtls_random() Closes IDFGH-14978 See merge request espressif/esp-idf!39221
2 parents fd5b86d + 88d71da commit b789198

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
{
@@ -294,24 +289,14 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
294289
struct crypto_ec_point *res)
295290
{
296291
int ret;
297-
mbedtls_entropy_context entropy;
298-
mbedtls_ctr_drbg_context ctr_drbg;
299-
300-
mbedtls_entropy_init(&entropy);
301-
mbedtls_ctr_drbg_init(&ctr_drbg);
302-
303-
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
304-
NULL, 0));
305-
306292
MBEDTLS_MPI_CHK(mbedtls_ecp_mul((mbedtls_ecp_group *)e,
307293
(mbedtls_ecp_point *) res,
308294
(const mbedtls_mpi *)b,
309295
(const mbedtls_ecp_point *)p,
310-
mbedtls_ctr_drbg_random,
311-
&ctr_drbg));
296+
mbedtls_esp_random,
297+
NULL));
298+
312299
cleanup:
313-
mbedtls_ctr_drbg_free(&ctr_drbg);
314-
mbedtls_entropy_free(&entropy);
315300
return ret ? -1 : 0;
316301
}
317302

@@ -491,23 +476,10 @@ int crypto_ec_point_cmp(const struct crypto_ec *e,
491476

492477
int crypto_ec_key_compare(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
493478
{
494-
int ret = 0;
495-
mbedtls_entropy_context entropy;
496-
mbedtls_ctr_drbg_context ctr_drbg;
497-
498-
mbedtls_entropy_init(&entropy);
499-
mbedtls_ctr_drbg_init(&ctr_drbg);
500-
501-
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0));
502-
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
503-
goto cleanup;
479+
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_esp_random, NULL) < 0) {
480+
return 0;
504481
}
505-
506-
ret = 1;
507-
cleanup:
508-
mbedtls_ctr_drbg_free(&ctr_drbg);
509-
mbedtls_entropy_free(&entropy);
510-
return ret;
482+
return 1;
511483
}
512484

513485
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
707679
wpa_printf(MSG_ERROR, "memory allocation failed");
708680
return NULL;
709681
}
710-
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL);
682+
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, mbedtls_esp_random, NULL);
711683

712684
if (ret < 0) {
713685
//crypto_print_error_string(ret);
@@ -763,17 +735,8 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
763735
mbedtls_ecdh_context *ctx = NULL;
764736
mbedtls_pk_context *own = (mbedtls_pk_context *)key_own;
765737
mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer;
766-
mbedtls_entropy_context entropy;
767-
mbedtls_ctr_drbg_context ctr_drbg;
768738
int ret = -1;
769739

770-
mbedtls_entropy_init(&entropy);
771-
mbedtls_ctr_drbg_init(&ctr_drbg);
772-
773-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) {
774-
goto fail;
775-
}
776-
777740
*secret_len = 0;
778741
ctx = os_malloc(sizeof(*ctx));
779742
if (!ctx) {
@@ -801,7 +764,7 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
801764
}
802765

803766
if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN,
804-
mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
767+
mbedtls_esp_random, NULL) < 0) {
805768
wpa_printf(MSG_ERROR, "failed to calculate secret");
806769
goto fail;
807770
}
@@ -814,8 +777,6 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
814777
ret = 0;
815778

816779
fail:
817-
mbedtls_ctr_drbg_free(&ctr_drbg);
818-
mbedtls_entropy_free(&entropy);
819780
if (ctx) {
820781
mbedtls_ecdh_free(ctx);
821782
os_free(ctx);
@@ -840,7 +801,7 @@ int crypto_ecdsa_get_sign(unsigned char *hash,
840801
goto fail;
841802
}
842803
ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s,
843-
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL);
804+
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, mbedtls_esp_random, NULL);
844805

845806
fail:
846807
mbedtls_ecdsa_free(ctx);
@@ -939,7 +900,7 @@ struct crypto_ec_key * crypto_ec_key_gen(u16 ike_group)
939900
}
940901

941902
mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument
942-
crypto_rng_wrapper, NULL);
903+
mbedtls_esp_random, NULL);
943904

944905
return (struct crypto_ec_key *)kctx;
945906
fail:
@@ -1124,8 +1085,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
11241085

11251086
struct crypto_ecdh * crypto_ecdh_init(int group)
11261087
{
1127-
mbedtls_ctr_drbg_context ctr_drbg;
1128-
mbedtls_entropy_context entropy;
11291088
mbedtls_ecdh_context *ctx;
11301089

11311090
ctx = os_zalloc(sizeof(*ctx));
@@ -1143,33 +1102,19 @@ struct crypto_ecdh * crypto_ecdh_init(int group)
11431102
goto fail;
11441103
}
11451104

1146-
/* Initialize CTR_DRBG context */
1147-
mbedtls_ctr_drbg_init(&ctr_drbg);
1148-
mbedtls_entropy_init(&entropy);
1149-
1150-
/* Seed and setup CTR_DRBG entropy source for future reseeds */
1151-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
1152-
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
1153-
goto fail;
1154-
}
1155-
11561105
/* Generates ECDH keypair on elliptic curve */
1157-
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
1106+
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_esp_random, NULL) != 0) {
11581107
wpa_printf(MSG_ERROR, "ECDH keypair on curve failed");
11591108
goto fail;
11601109
}
11611110

1162-
mbedtls_ctr_drbg_free(&ctr_drbg);
1163-
mbedtls_entropy_free(&entropy);
11641111
return (struct crypto_ecdh *)ctx;
11651112
fail:
11661113
if (ctx) {
11671114
mbedtls_ecdh_free(ctx);
11681115
os_free(ctx);
11691116
ctx = NULL;
11701117
}
1171-
mbedtls_ctr_drbg_free(&ctr_drbg);
1172-
mbedtls_entropy_free(&entropy);
11731118
return NULL;
11741119
}
11751120

@@ -1217,18 +1162,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12171162
return 0;
12181163
}
12191164

1220-
mbedtls_ctr_drbg_context ctr_drbg;
1221-
mbedtls_entropy_context entropy;
1222-
1223-
/* Initialize CTR_DRBG context */
1224-
mbedtls_ctr_drbg_init(&ctr_drbg);
1225-
mbedtls_entropy_init(&entropy);
1226-
1227-
/* Seed and setup CTR_DRBG entropy source for future reseeds */
1228-
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
1229-
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
1230-
goto cleanup;
1231-
}
12321165
len_prime = ACCESS_ECDH(ctx, grp).pbits / 8;
12331166
bn_x = crypto_bignum_init_set(key, len);
12341167

@@ -1287,7 +1220,7 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12871220

12881221
/* Calculate secret
12891222
z = F(DH(x,Y)) */
1290-
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg);
1223+
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_esp_random, NULL);
12911224
if (secret_key != 0) {
12921225
wpa_printf(MSG_ERROR, "Calculation of secret failed");
12931226
goto cleanup;
@@ -1302,8 +1235,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
13021235
crypto_ec_key_deinit(pkey);
13031236
crypto_bignum_deinit(bn_x, 1);
13041237
crypto_ec_point_deinit(ec_pt, 1);
1305-
mbedtls_ctr_drbg_free(&ctr_drbg);
1306-
mbedtls_entropy_free(&entropy);
13071238
return sh_secret;
13081239
}
13091240

0 commit comments

Comments
 (0)