10
10
11
11
#ifndef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_NONE
12
12
#include <mbedtls/version.h>
13
- #include <mbedtls/entropy.h>
14
- #include <mbedtls/ctr_drbg.h>
15
13
#include <mbedtls/platform_util.h> /* mbedtls_platform_zeroize() */
16
14
#include <mbedtls/asn1.h>
17
15
#include <mbedtls/asn1write.h>
146
144
#if defined(CRYPTO_RSA_OAEP_SHA256 )
147
145
#define CRYPTO_MBEDTLS_CRYPTO_RSA
148
146
#endif
147
+
149
148
#endif /* crypto_rsa_*() */
150
149
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>
151
161
static int ctr_drbg_init_state ;
152
162
static mbedtls_ctr_drbg_context ctr_drbg ;
153
163
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
154
167
155
168
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
156
169
#include <mbedtls/bignum.h>
157
170
static mbedtls_mpi mpi_sw_A ;
158
171
#endif
159
172
173
+ #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG )
160
174
static int wm_wrap_entropy_poll (void * data , unsigned char * output , size_t len , size_t * olen )
161
175
{
162
176
((void )data );
@@ -186,20 +200,24 @@ __attribute_cold__ __attribute_noinline__ static mbedtls_ctr_drbg_context *ctr_d
186
200
187
201
return & ctr_drbg ;
188
202
}
203
+ #endif
189
204
190
205
__attribute_cold__ void crypto_unload (void )
191
206
{
192
207
if (ctr_drbg_init_state )
193
208
{
209
+ #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG )
194
210
mbedtls_ctr_drbg_free (& ctr_drbg );
195
211
mbedtls_entropy_free (& entropy );
212
+ #endif
196
213
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
197
214
mbedtls_mpi_free (& mpi_sw_A );
198
215
#endif
199
216
ctr_drbg_init_state = 0 ;
200
217
}
201
218
}
202
219
220
+ #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG )
203
221
/* init ctr_drbg on first use
204
222
* crypto_global_init() and crypto_global_deinit() are not available here
205
223
* (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)
208
226
{
209
227
return ctr_drbg_init_state ? & ctr_drbg : ctr_drbg_init ();
210
228
}
229
+ #endif
211
230
212
231
/* tradeoff: slightly smaller code size here at cost of slight increase
213
232
* 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)
1194
1213
1195
1214
/*assert(r != m);*/ /* r must not be same as m for mbedtls_mpi_random()*/
1196
1215
#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 ) ?
1199
1217
-1 :
1200
1218
0 ;
1201
1219
#else
@@ -1423,8 +1441,7 @@ __attribute_noinline__ static int crypto_mbedtls_dh_init_public(
1423
1441
mbedtls_dhm_context * ctx , u8 generator , const u8 * prime , size_t prime_len , u8 * privkey , u8 * pubkey )
1424
1442
{
1425
1443
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 ))
1428
1445
return -1 ;
1429
1446
1430
1447
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,
1489
1506
int ret =
1490
1507
mbedtls_dhm_read_params (& ctx , & p , p + 2 + prime_len + 5 + pubkey_len ) ||
1491
1508
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 ) ?
1493
1510
-1 :
1494
1511
0 ;
1495
1512
mbedtls_dhm_free (& ctx );
@@ -1581,8 +1598,7 @@ struct wpabuf *dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, co
1581
1598
if (buf == NULL )
1582
1599
return NULL ;
1583
1600
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 )
1586
1602
{
1587
1603
wpabuf_put (buf , olen );
1588
1604
return buf ;
@@ -1732,7 +1748,7 @@ static int crypto_mbedtls_keypair_gen(int group, mbedtls_pk_context *pk)
1732
1748
if (pk_info == NULL )
1733
1749
return -1 ;
1734
1750
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 ) ?
1736
1752
-1 :
1737
1753
0 ;
1738
1754
}
@@ -1940,8 +1956,7 @@ struct wpabuf *crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, cons
1940
1956
if (buf == NULL )
1941
1957
return NULL ;
1942
1958
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 )
1945
1960
{
1946
1961
wpabuf_put (buf , len );
1947
1962
return buf ;
@@ -2240,7 +2255,7 @@ int crypto_ec_point_mul(struct crypto_ec *e,
2240
2255
return -1 ;
2241
2256
2242
2257
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 ) ?
2244
2259
-1 :
2245
2260
0 ;
2246
2261
}
@@ -2361,7 +2376,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
2361
2376
#if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.0.0 */
2362
2377
if (mbedtls_pk_parse_key (ctx , der , der_len , NULL , 0 ) == 0 )
2363
2378
#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 )
2365
2380
#endif
2366
2381
return (struct crypto_ec_key * )ctx ;
2367
2382
@@ -2521,9 +2536,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
2521
2536
{
2522
2537
/* (Is private key generation necessary for callers?)
2523
2538
* 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
2527
2540
*/
2528
2541
mbedtls_ecp_keypair * ecp_kp = mbedtls_pk_ec (* ctx );
2529
2542
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
2532
2545
if (mbedtls_ecp_group_load (ecp_kp_grp , grp_id ) == 0 &&
2533
2546
(pub ? mbedtls_ecp_copy (ecp_kp_Q , pub ) == 0 :
2534
2547
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 )
2536
2549
{
2537
2550
return (struct crypto_ec_key * )ctx ;
2538
2551
}
@@ -2802,7 +2815,7 @@ struct wpabuf *crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data, siz
2802
2815
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
2803
2816
sig_len ,
2804
2817
#endif
2805
- & sig_len , mbedtls_ctr_drbg_random , crypto_mbedtls_ctr_drbg () ) == 0 )
2818
+ & sig_len , hostap_rng_fn , hostap_rng_ctx ) == 0 )
2806
2819
{
2807
2820
wpabuf_put (buf , sig_len );
2808
2821
return buf ;
@@ -2825,7 +2838,7 @@ struct wpabuf *crypto_ec_key_sign_r_s(struct crypto_ec_key *key, const u8 *data,
2825
2838
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
2826
2839
sig_len ,
2827
2840
#endif
2828
- & sig_len , mbedtls_ctr_drbg_random , crypto_mbedtls_ctr_drbg () ))
2841
+ & sig_len , hostap_rng_fn , hostap_rng_ctx ))
2829
2842
{
2830
2843
return NULL ;
2831
2844
}
@@ -3191,8 +3204,7 @@ struct wpabuf *crypto_csr_sign(struct crypto_csr *csr, struct crypto_ec_key *key
3191
3204
mbedtls_x509write_csr_set_md_alg ((mbedtls_x509write_csr * )csr , sig_md );
3192
3205
3193
3206
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 );
3196
3208
if (len < 0 )
3197
3209
return NULL ;
3198
3210
/* Note: data is written at the end of the buffer! Use the
0 commit comments