Skip to content

Commit 219b1f7

Browse files
frkvjukkar
authored andcommitted
[noup] zephyr: Update to support MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
-This commit add support for MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG which adds support for PRNG (seeded by TRNG) without using the legacy Mbed TLS APIs in ctr_drbg.c and entropy.c. When the configuration MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled there is a new function available in the system that is used to override this behavior. This function is called mbedtls_psa_external_get_random but for compatibility with the f_rng/p_rng signature the function mbedtls_psa_get_random (found in mbedtls/psa_util.h) is used directly in code. -Added a function pointer called hostap_rng_fn which is set to mbedtls_psa_get_random if MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, otherwise it is set to the legacy API mbedtls_ctr_drbg_random. -Added a context pointer called hostap_rng_ctx that is set to MBEDTLS_PSA_RANDOM_STATE (NULL) if MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is set, otherwise it points to the existing ctr_drbg context. -Updated all calls using legacy APIs making use of the f_rng/p_rng pattern to use hostap_rng_fn and hostap_rng_ctx in crypto_mbedtls_alt -Added forward declaration of hostap_rng_fn and hostap_rng_ctx in tls_mbedtls_alt.c and changed every API-call making use of the f_rng/p_rng pattern. Signed-off-by: Frank Audun Kvamtrø <frank.kvamtro@nordicsemi.no>
1 parent 4957416 commit 219b1f7

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

src/crypto/crypto_mbedtls_alt.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#ifndef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_NONE
1212
#include <mbedtls/version.h>
13-
#include <mbedtls/entropy.h>
14-
#include <mbedtls/ctr_drbg.h>
1513
#include <mbedtls/platform_util.h> /* mbedtls_platform_zeroize() */
1614
#include <mbedtls/asn1.h>
1715
#include <mbedtls/asn1write.h>
@@ -146,17 +144,33 @@
146144
#if defined(CRYPTO_RSA_OAEP_SHA256)
147145
#define CRYPTO_MBEDTLS_CRYPTO_RSA
148146
#endif
147+
149148
#endif /* crypto_rsa_*() */
150149

150+
151+
152+
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
153+
#include <mbedtls/psa_util.h>
154+
/* Setting ctr_drbg_init_state to 1 to allow unload_crypto to run */
155+
static int ctr_drbg_init_state = 1;
156+
int (*hostap_rng_fn)(void*, unsigned char*, size_t) = mbedtls_psa_get_random;
157+
void *hostap_rng_ctx = MBEDTLS_PSA_RANDOM_STATE;
158+
#else
159+
#include <mbedtls/entropy.h>
160+
#include <mbedtls/ctr_drbg.h>
151161
static int ctr_drbg_init_state;
152162
static mbedtls_ctr_drbg_context ctr_drbg;
153163
static mbedtls_entropy_context entropy;
164+
int(*hostap_rng_fn)(void*, unsigned char*, size_t) = mbedtls_ctr_drbg_random;
165+
void *hostap_rng_ctx = &ctr_drbg;
166+
#endif
154167

155168
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
156169
#include <mbedtls/bignum.h>
157170
static mbedtls_mpi mpi_sw_A;
158171
#endif
159172

173+
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
160174
static int wm_wrap_entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen)
161175
{
162176
((void)data);
@@ -186,20 +200,24 @@ __attribute_cold__ __attribute_noinline__ static mbedtls_ctr_drbg_context *ctr_d
186200

187201
return &ctr_drbg;
188202
}
203+
#endif
189204

190205
__attribute_cold__ void crypto_unload(void)
191206
{
192207
if (ctr_drbg_init_state)
193208
{
209+
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
194210
mbedtls_ctr_drbg_free(&ctr_drbg);
195211
mbedtls_entropy_free(&entropy);
212+
#endif
196213
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
197214
mbedtls_mpi_free(&mpi_sw_A);
198215
#endif
199216
ctr_drbg_init_state = 0;
200217
}
201218
}
202219

220+
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
203221
/* init ctr_drbg on first use
204222
* crypto_global_init() and crypto_global_deinit() are not available here
205223
* (available only when CONFIG_TLS=internal, which is not CONFIG_TLS=mbedtls) */
@@ -208,6 +226,7 @@ inline mbedtls_ctr_drbg_context *crypto_mbedtls_ctr_drbg(void)
208226
{
209227
return ctr_drbg_init_state ? &ctr_drbg : ctr_drbg_init();
210228
}
229+
#endif
211230

212231
/* tradeoff: slightly smaller code size here at cost of slight increase
213232
* in instructions and function calls at runtime versus the expanded
@@ -1194,8 +1213,7 @@ int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
11941213

11951214
/*assert(r != m);*/ /* r must not be same as m for mbedtls_mpi_random()*/
11961215
#if MBEDTLS_VERSION_NUMBER >= 0x021B0000 /* mbedtls 2.27.0 */
1197-
return mbedtls_mpi_random((mbedtls_mpi *)r, 0, (mbedtls_mpi *)m, mbedtls_ctr_drbg_random,
1198-
crypto_mbedtls_ctr_drbg()) ?
1216+
return mbedtls_mpi_random((mbedtls_mpi *)r, 0, (mbedtls_mpi *)m, hostap_rng_fn, hostap_rng_ctx) ?
11991217
-1 :
12001218
0;
12011219
#else
@@ -1423,8 +1441,7 @@ __attribute_noinline__ static int crypto_mbedtls_dh_init_public(
14231441
mbedtls_dhm_context *ctx, u8 generator, const u8 *prime, size_t prime_len, u8 *privkey, u8 *pubkey)
14241442
{
14251443
if (crypto_mbedtls_dh_set_bin_pg(ctx, generator, prime, prime_len) ||
1426-
mbedtls_dhm_make_public(ctx, (int)prime_len, pubkey, prime_len, mbedtls_ctr_drbg_random,
1427-
crypto_mbedtls_ctr_drbg()))
1444+
mbedtls_dhm_make_public(ctx, (int)prime_len, pubkey, prime_len, hostap_rng_fn, hostap_rng_ctx))
14281445
return -1;
14291446

14301447
return mbedtls_mpi_write_binary(&ctx->MBEDTLS_PRIVATE(X), privkey, prime_len) ? -1 : 0;
@@ -1489,7 +1506,7 @@ int crypto_dh_derive_secret(u8 generator,
14891506
int ret =
14901507
mbedtls_dhm_read_params(&ctx, &p, p + 2 + prime_len + 5 + pubkey_len) ||
14911508
mbedtls_mpi_read_binary(&ctx.MBEDTLS_PRIVATE(X), privkey, privkey_len) ||
1492-
mbedtls_dhm_calc_secret(&ctx, secret, *len, len, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) ?
1509+
mbedtls_dhm_calc_secret(&ctx, secret, *len, len, hostap_rng_fn, hostap_rng_ctx) ?
14931510
-1 :
14941511
0;
14951512
mbedtls_dhm_free(&ctx);
@@ -1581,8 +1598,7 @@ struct wpabuf *dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, co
15811598
if (buf == NULL)
15821599
return NULL;
15831600
if (mbedtls_dhm_read_public((mbedtls_dhm_context *)ctx, wpabuf_head(peer_public), wpabuf_len(peer_public)) == 0 &&
1584-
mbedtls_dhm_calc_secret(ctx, wpabuf_mhead(buf), olen, &olen, mbedtls_ctr_drbg_random,
1585-
crypto_mbedtls_ctr_drbg()) == 0)
1601+
mbedtls_dhm_calc_secret(ctx, wpabuf_mhead(buf), olen, &olen, hostap_rng_fn, hostap_rng_ctx) == 0)
15861602
{
15871603
wpabuf_put(buf, olen);
15881604
return buf;
@@ -1732,7 +1748,7 @@ static int crypto_mbedtls_keypair_gen(int group, mbedtls_pk_context *pk)
17321748
if (pk_info == NULL)
17331749
return -1;
17341750
return mbedtls_pk_setup(pk, pk_info) ||
1735-
mbedtls_ecp_gen_key(grp_id, mbedtls_pk_ec(*pk), mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) ?
1751+
mbedtls_ecp_gen_key(grp_id, mbedtls_pk_ec(*pk), hostap_rng_fn, hostap_rng_ctx) ?
17361752
-1 :
17371753
0;
17381754
}
@@ -1940,8 +1956,7 @@ struct wpabuf *crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, cons
19401956
if (buf == NULL)
19411957
return NULL;
19421958

1943-
if (mbedtls_ecdh_calc_secret(&ecdh->ctx, &len, wpabuf_mhead(buf), len, mbedtls_ctr_drbg_random,
1944-
crypto_mbedtls_ctr_drbg()) == 0)
1959+
if (mbedtls_ecdh_calc_secret(&ecdh->ctx, &len, wpabuf_mhead(buf), len, hostap_rng_fn, hostap_rng_ctx) == 0)
19451960
{
19461961
wpabuf_put(buf, len);
19471962
return buf;
@@ -2240,7 +2255,7 @@ int crypto_ec_point_mul(struct crypto_ec *e,
22402255
return -1;
22412256

22422257
return mbedtls_ecp_mul((mbedtls_ecp_group *)e, (mbedtls_ecp_point *)res, (const mbedtls_mpi *)b,
2243-
(const mbedtls_ecp_point *)p, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) ?
2258+
(const mbedtls_ecp_point *)p, hostap_rng_fn, hostap_rng_ctx) ?
22442259
-1 :
22452260
0;
22462261
}
@@ -2361,7 +2376,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
23612376
#if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.0.0 */
23622377
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0) == 0)
23632378
#else
2364-
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) == 0)
2379+
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0, hostap_rng_fn, hostap_rng_ctx) == 0)
23652380
#endif
23662381
return (struct crypto_ec_key *)ctx;
23672382

@@ -2521,9 +2536,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
25212536
{
25222537
/* (Is private key generation necessary for callers?)
25232538
* alt: gen key then overwrite Q
2524-
* mbedtls_ecp_gen_key(grp_id, ecp_kp,
2525-
* mbedtls_ctr_drbg_random,
2526-
* crypto_mbedtls_ctr_drbg()) == 0
2539+
* mbedtls_ecp_gen_key(grp_id, ecp_kp, hostap_rng_fn, hostap_rng_ctx) == 0
25272540
*/
25282541
mbedtls_ecp_keypair *ecp_kp = mbedtls_pk_ec(*ctx);
25292542
mbedtls_ecp_group *ecp_kp_grp = &ecp_kp->MBEDTLS_PRIVATE(grp);
@@ -2532,7 +2545,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
25322545
if (mbedtls_ecp_group_load(ecp_kp_grp, grp_id) == 0 &&
25332546
(pub ? mbedtls_ecp_copy(ecp_kp_Q, pub) == 0 :
25342547
mbedtls_ecp_point_read_binary(ecp_kp_grp, ecp_kp_Q, buf, len) == 0) &&
2535-
mbedtls_ecp_gen_privkey(ecp_kp_grp, ecp_kp_d, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) == 0)
2548+
mbedtls_ecp_gen_privkey(ecp_kp_grp, ecp_kp_d, hostap_rng_fn, hostap_rng_ctx) == 0)
25362549
{
25372550
return (struct crypto_ec_key *)ctx;
25382551
}
@@ -2802,7 +2815,7 @@ struct wpabuf *crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data, siz
28022815
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
28032816
sig_len,
28042817
#endif
2805-
&sig_len, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()) == 0)
2818+
&sig_len, hostap_rng_fn, hostap_rng_ctx) == 0)
28062819
{
28072820
wpabuf_put(buf, sig_len);
28082821
return buf;
@@ -2825,7 +2838,7 @@ struct wpabuf *crypto_ec_key_sign_r_s(struct crypto_ec_key *key, const u8 *data,
28252838
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
28262839
sig_len,
28272840
#endif
2828-
&sig_len, mbedtls_ctr_drbg_random, crypto_mbedtls_ctr_drbg()))
2841+
&sig_len, hostap_rng_fn, hostap_rng_ctx))
28292842
{
28302843
return NULL;
28312844
}
@@ -3191,8 +3204,7 @@ struct wpabuf *crypto_csr_sign(struct crypto_csr *csr, struct crypto_ec_key *key
31913204
mbedtls_x509write_csr_set_md_alg((mbedtls_x509write_csr *)csr, sig_md);
31923205

31933206
unsigned char buf[4096]; /* XXX: large enough? too large? */
3194-
int len = mbedtls_x509write_csr_der((mbedtls_x509write_csr *)csr, buf, sizeof(buf), mbedtls_ctr_drbg_random,
3195-
crypto_mbedtls_ctr_drbg());
3207+
int len = mbedtls_x509write_csr_der((mbedtls_x509write_csr *)csr, buf, sizeof(buf), hostap_rng_fn, hostap_rng_ctx);
31963208
if (len < 0)
31973209
return NULL;
31983210
/* Note: data is written at the end of the buffer! Use the

src/crypto/tls_mbedtls_alt.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#include <mbedtls/x509.h>
6060
#include <mbedtls/x509_crt.h>
6161

62+
extern int (*hostap_rng_fn)(void*, unsigned char*, size_t);
63+
extern void* hostap_rng_ctx;
64+
6265
#ifdef MBEDTLS_DEBUG_C
6366
#define DEBUG_THRESHOLD 4
6467
#include <mbedtls/debug.h>
@@ -345,7 +348,7 @@ struct tls_conf *tls_conf_init(void *tls_ctx)
345348
tls_conf->refcnt = 1;
346349

347350
mbedtls_ssl_config_init(&tls_conf->conf);
348-
mbedtls_ssl_conf_rng(&tls_conf->conf, mbedtls_ctr_drbg_random, tls_ctx_global.ctr_drbg);
351+
mbedtls_ssl_conf_rng(&tls_conf->conf, hostap_rng_fn, hostap_rng_ctx);
349352
mbedtls_x509_crt_init(&tls_conf->ca_cert);
350353
mbedtls_x509_crt_init(&tls_conf->client_cert);
351354
mbedtls_pk_init(&tls_conf->private_key);
@@ -383,7 +386,9 @@ struct tls_conf *tls_conf_deinit(struct tls_conf *tls_conf)
383386
return NULL;
384387
}
385388

389+
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
386390
mbedtls_ctr_drbg_context *crypto_mbedtls_ctr_drbg(void); /*(not in header)*/
391+
#endif
387392

388393
__attribute_cold__ void *tls_init(const struct tls_config *conf)
389394
{
@@ -392,10 +397,12 @@ __attribute_cold__ void *tls_init(const struct tls_config *conf)
392397
if (++tls_ctx_global.refcnt > 1)
393398
return &tls_ctx_global;
394399

400+
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
395401
tls_ctx_global.ctr_drbg = crypto_mbedtls_ctr_drbg();
402+
#endif
396403
#ifdef MBEDTLS_SSL_SESSION_TICKETS
397404
mbedtls_ssl_ticket_init(&tls_ctx_global.ticket_ctx);
398-
mbedtls_ssl_ticket_setup(&tls_ctx_global.ticket_ctx, mbedtls_ctr_drbg_random, tls_ctx_global.ctr_drbg,
405+
mbedtls_ssl_ticket_setup(&tls_ctx_global.ticket_ctx, hostap_rng_fn, hostap_rng_ctx,
399406
MBEDTLS_CIPHER_AES_256_GCM, 43200); /* ticket timeout: 12 hours */
400407
#endif
401408
/* copy struct for future use */
@@ -1608,7 +1615,7 @@ static int tls_mbedtls_set_certs(struct tls_conf *tls_conf, const struct tls_con
16081615
const char *pwd = params->private_key_passwd;
16091616
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
16101617
ret = mbedtls_pk_parse_key(&tls_conf->private_key, data, len, (const unsigned char *)pwd,
1611-
pwd ? os_strlen(pwd) : 0, mbedtls_ctr_drbg_random, tls_ctx_global.ctr_drbg);
1618+
pwd ? os_strlen(pwd) : 0, hostap_rng_fn, hostap_rng_ctx);
16121619
#else
16131620
ret = mbedtls_pk_parse_key(&tls_conf->private_key, data, len, (const unsigned char *)pwd,
16141621
pwd ? os_strlen(pwd) : 0);

0 commit comments

Comments
 (0)