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"
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"
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
4439struct 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+
312299cleanup :
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
492477int 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
513485void 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
816779fail :
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
845806fail :
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 ;
945906fail :
@@ -1124,8 +1085,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
11241085
11251086struct 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 ;
11651112fail :
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