154
154
/* Setting ctr_drbg_init_state to 1 to allow unload_crypto to run */
155
155
static int ctr_drbg_init_state = 1 ;
156
156
int (* hostap_rng_fn )(void * , unsigned char * , size_t ) = mbedtls_psa_get_random ;
157
- void * hostap_rng_ctx = MBEDTLS_PSA_RANDOM_STATE ;
158
157
#else
159
158
#include <mbedtls/entropy.h>
160
159
#include <mbedtls/ctr_drbg.h>
161
160
static int ctr_drbg_init_state ;
162
161
static mbedtls_ctr_drbg_context ctr_drbg ;
163
162
static mbedtls_entropy_context entropy ;
164
163
int (* hostap_rng_fn )(void * , unsigned char * , size_t ) = mbedtls_ctr_drbg_random ;
165
- void * hostap_rng_ctx = & ctr_drbg ;
166
164
#endif
167
165
168
166
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
@@ -228,6 +226,15 @@ inline mbedtls_ctr_drbg_context *crypto_mbedtls_ctr_drbg(void)
228
226
}
229
227
#endif
230
228
229
+ void * hostap_rng_ctx (void )
230
+ {
231
+ #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG )
232
+ return MBEDTLS_PSA_RANDOM_STATE ;
233
+ #else
234
+ return (mbedtls_ctr_drbg_context * ) crypto_mbedtls_ctr_drbg ();
235
+ #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
236
+ }
237
+
231
238
/* tradeoff: slightly smaller code size here at cost of slight increase
232
239
* in instructions and function calls at runtime versus the expanded
233
240
* per-message-digest code that follows in #else (~0.5 kib .text larger) */
@@ -1213,7 +1220,7 @@ int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1213
1220
1214
1221
/*assert(r != m);*/ /* r must not be same as m for mbedtls_mpi_random()*/
1215
1222
#if MBEDTLS_VERSION_NUMBER >= 0x021B0000 /* mbedtls 2.27.0 */
1216
- return mbedtls_mpi_random ((mbedtls_mpi * )r , 0 , (mbedtls_mpi * )m , hostap_rng_fn , hostap_rng_ctx ) ?
1223
+ return mbedtls_mpi_random ((mbedtls_mpi * )r , 0 , (mbedtls_mpi * )m , hostap_rng_fn , hostap_rng_ctx () ) ?
1217
1224
-1 :
1218
1225
0 ;
1219
1226
#else
@@ -1441,7 +1448,7 @@ __attribute_noinline__ static int crypto_mbedtls_dh_init_public(
1441
1448
mbedtls_dhm_context * ctx , u8 generator , const u8 * prime , size_t prime_len , u8 * privkey , u8 * pubkey )
1442
1449
{
1443
1450
if (crypto_mbedtls_dh_set_bin_pg (ctx , generator , prime , prime_len ) ||
1444
- mbedtls_dhm_make_public (ctx , (int )prime_len , pubkey , prime_len , hostap_rng_fn , hostap_rng_ctx ))
1451
+ mbedtls_dhm_make_public (ctx , (int )prime_len , pubkey , prime_len , hostap_rng_fn , hostap_rng_ctx () ))
1445
1452
return -1 ;
1446
1453
1447
1454
return mbedtls_mpi_write_binary (& ctx -> MBEDTLS_PRIVATE (X ), privkey , prime_len ) ? -1 : 0 ;
@@ -1506,7 +1513,7 @@ int crypto_dh_derive_secret(u8 generator,
1506
1513
int ret =
1507
1514
mbedtls_dhm_read_params (& ctx , & p , p + 2 + prime_len + 5 + pubkey_len ) ||
1508
1515
mbedtls_mpi_read_binary (& ctx .MBEDTLS_PRIVATE (X ), privkey , privkey_len ) ||
1509
- mbedtls_dhm_calc_secret (& ctx , secret , * len , len , hostap_rng_fn , hostap_rng_ctx ) ?
1516
+ mbedtls_dhm_calc_secret (& ctx , secret , * len , len , hostap_rng_fn , hostap_rng_ctx () ) ?
1510
1517
-1 :
1511
1518
0 ;
1512
1519
mbedtls_dhm_free (& ctx );
@@ -1598,7 +1605,7 @@ struct wpabuf *dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, co
1598
1605
if (buf == NULL )
1599
1606
return NULL ;
1600
1607
if (mbedtls_dhm_read_public ((mbedtls_dhm_context * )ctx , wpabuf_head (peer_public ), wpabuf_len (peer_public )) == 0 &&
1601
- mbedtls_dhm_calc_secret (ctx , wpabuf_mhead (buf ), olen , & olen , hostap_rng_fn , hostap_rng_ctx ) == 0 )
1608
+ mbedtls_dhm_calc_secret (ctx , wpabuf_mhead (buf ), olen , & olen , hostap_rng_fn , hostap_rng_ctx () ) == 0 )
1602
1609
{
1603
1610
wpabuf_put (buf , olen );
1604
1611
return buf ;
@@ -1748,7 +1755,7 @@ static int crypto_mbedtls_keypair_gen(int group, mbedtls_pk_context *pk)
1748
1755
if (pk_info == NULL )
1749
1756
return -1 ;
1750
1757
return mbedtls_pk_setup (pk , pk_info ) ||
1751
- mbedtls_ecp_gen_key (grp_id , mbedtls_pk_ec (* pk ), hostap_rng_fn , hostap_rng_ctx ) ?
1758
+ mbedtls_ecp_gen_key (grp_id , mbedtls_pk_ec (* pk ), hostap_rng_fn , hostap_rng_ctx () ) ?
1752
1759
-1 :
1753
1760
0 ;
1754
1761
}
@@ -1956,7 +1963,7 @@ struct wpabuf *crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, cons
1956
1963
if (buf == NULL )
1957
1964
return NULL ;
1958
1965
1959
- if (mbedtls_ecdh_calc_secret (& ecdh -> ctx , & len , wpabuf_mhead (buf ), len , hostap_rng_fn , hostap_rng_ctx ) == 0 )
1966
+ if (mbedtls_ecdh_calc_secret (& ecdh -> ctx , & len , wpabuf_mhead (buf ), len , hostap_rng_fn , hostap_rng_ctx () ) == 0 )
1960
1967
{
1961
1968
wpabuf_put (buf , len );
1962
1969
return buf ;
@@ -2255,7 +2262,7 @@ int crypto_ec_point_mul(struct crypto_ec *e,
2255
2262
return -1 ;
2256
2263
2257
2264
return mbedtls_ecp_mul ((mbedtls_ecp_group * )e , (mbedtls_ecp_point * )res , (const mbedtls_mpi * )b ,
2258
- (const mbedtls_ecp_point * )p , hostap_rng_fn , hostap_rng_ctx ) ?
2265
+ (const mbedtls_ecp_point * )p , hostap_rng_fn , hostap_rng_ctx () ) ?
2259
2266
-1 :
2260
2267
0 ;
2261
2268
}
@@ -2376,7 +2383,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
2376
2383
#if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.0.0 */
2377
2384
if (mbedtls_pk_parse_key (ctx , der , der_len , NULL , 0 ) == 0 )
2378
2385
#else
2379
- if (mbedtls_pk_parse_key (ctx , der , der_len , NULL , 0 , hostap_rng_fn , hostap_rng_ctx ) == 0 )
2386
+ if (mbedtls_pk_parse_key (ctx , der , der_len , NULL , 0 , hostap_rng_fn , hostap_rng_ctx () ) == 0 )
2380
2387
#endif
2381
2388
return (struct crypto_ec_key * )ctx ;
2382
2389
@@ -2536,7 +2543,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
2536
2543
{
2537
2544
/* (Is private key generation necessary for callers?)
2538
2545
* alt: gen key then overwrite Q
2539
- * mbedtls_ecp_gen_key(grp_id, ecp_kp, hostap_rng_fn, hostap_rng_ctx) == 0
2546
+ * mbedtls_ecp_gen_key(grp_id, ecp_kp, hostap_rng_fn, hostap_rng_ctx() ) == 0
2540
2547
*/
2541
2548
mbedtls_ecp_keypair * ecp_kp = mbedtls_pk_ec (* ctx );
2542
2549
mbedtls_ecp_group * ecp_kp_grp = & ecp_kp -> MBEDTLS_PRIVATE (grp );
@@ -2545,7 +2552,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
2545
2552
if (mbedtls_ecp_group_load (ecp_kp_grp , grp_id ) == 0 &&
2546
2553
(pub ? mbedtls_ecp_copy (ecp_kp_Q , pub ) == 0 :
2547
2554
mbedtls_ecp_point_read_binary (ecp_kp_grp , ecp_kp_Q , buf , len ) == 0 ) &&
2548
- mbedtls_ecp_gen_privkey (ecp_kp_grp , ecp_kp_d , hostap_rng_fn , hostap_rng_ctx ) == 0 )
2555
+ mbedtls_ecp_gen_privkey (ecp_kp_grp , ecp_kp_d , hostap_rng_fn , hostap_rng_ctx () ) == 0 )
2549
2556
{
2550
2557
return (struct crypto_ec_key * )ctx ;
2551
2558
}
@@ -2815,7 +2822,7 @@ struct wpabuf *crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data, siz
2815
2822
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
2816
2823
sig_len ,
2817
2824
#endif
2818
- & sig_len , hostap_rng_fn , hostap_rng_ctx ) == 0 )
2825
+ & sig_len , hostap_rng_fn , hostap_rng_ctx () ) == 0 )
2819
2826
{
2820
2827
wpabuf_put (buf , sig_len );
2821
2828
return buf ;
@@ -2838,7 +2845,7 @@ struct wpabuf *crypto_ec_key_sign_r_s(struct crypto_ec_key *key, const u8 *data,
2838
2845
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
2839
2846
sig_len ,
2840
2847
#endif
2841
- & sig_len , hostap_rng_fn , hostap_rng_ctx ))
2848
+ & sig_len , hostap_rng_fn , hostap_rng_ctx () ))
2842
2849
{
2843
2850
return NULL ;
2844
2851
}
@@ -3204,7 +3211,7 @@ struct wpabuf *crypto_csr_sign(struct crypto_csr *csr, struct crypto_ec_key *key
3204
3211
mbedtls_x509write_csr_set_md_alg ((mbedtls_x509write_csr * )csr , sig_md );
3205
3212
3206
3213
unsigned char buf [4096 ]; /* XXX: large enough? too large? */
3207
- int len = mbedtls_x509write_csr_der ((mbedtls_x509write_csr * )csr , buf , sizeof (buf ), hostap_rng_fn , hostap_rng_ctx );
3214
+ int len = mbedtls_x509write_csr_der ((mbedtls_x509write_csr * )csr , buf , sizeof (buf ), hostap_rng_fn , hostap_rng_ctx () );
3208
3215
if (len < 0 )
3209
3216
return NULL ;
3210
3217
/* Note: data is written at the end of the buffer! Use the
0 commit comments