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{
@@ -258,24 +253,14 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
258253 struct crypto_ec_point * res )
259254{
260255 int ret ;
261- mbedtls_entropy_context entropy ;
262- mbedtls_ctr_drbg_context ctr_drbg ;
263-
264- mbedtls_entropy_init (& entropy );
265- mbedtls_ctr_drbg_init (& ctr_drbg );
266-
267- MBEDTLS_MPI_CHK (mbedtls_ctr_drbg_seed (& ctr_drbg , mbedtls_entropy_func , & entropy ,
268- NULL , 0 ));
269-
270256 MBEDTLS_MPI_CHK (mbedtls_ecp_mul ((mbedtls_ecp_group * )e ,
271257 (mbedtls_ecp_point * ) res ,
272258 (const mbedtls_mpi * )b ,
273259 (const mbedtls_ecp_point * )p ,
274- mbedtls_ctr_drbg_random ,
275- & ctr_drbg ));
260+ mbedtls_esp_random ,
261+ NULL ));
262+
276263cleanup :
277- mbedtls_ctr_drbg_free (& ctr_drbg );
278- mbedtls_entropy_free (& entropy );
279264 return ret ? -1 : 0 ;
280265}
281266
@@ -455,23 +440,10 @@ int crypto_ec_point_cmp(const struct crypto_ec *e,
455440
456441int crypto_ec_key_compare (struct crypto_ec_key * key1 , struct crypto_ec_key * key2 )
457442{
458- int ret = 0 ;
459- mbedtls_entropy_context entropy ;
460- mbedtls_ctr_drbg_context ctr_drbg ;
461-
462- mbedtls_entropy_init (& entropy );
463- mbedtls_ctr_drbg_init (& ctr_drbg );
464-
465- MBEDTLS_MPI_CHK (mbedtls_ctr_drbg_seed (& ctr_drbg , mbedtls_entropy_func , & entropy , NULL , 0 ));
466- if (mbedtls_pk_check_pair ((mbedtls_pk_context * )key1 , (mbedtls_pk_context * )key2 , mbedtls_ctr_drbg_random , & ctr_drbg ) < 0 ) {
467- goto cleanup ;
443+ if (mbedtls_pk_check_pair ((mbedtls_pk_context * )key1 , (mbedtls_pk_context * )key2 , mbedtls_esp_random , NULL ) < 0 ) {
444+ return 0 ;
468445 }
469-
470- ret = 1 ;
471- cleanup :
472- mbedtls_ctr_drbg_free (& ctr_drbg );
473- mbedtls_entropy_free (& entropy );
474- return ret ;
446+ return 1 ;
475447}
476448
477449void crypto_debug_print_point (const char * title , struct crypto_ec * e ,
@@ -671,7 +643,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *privkey, size_t privkey
671643 wpa_printf (MSG_ERROR , "memory allocation failed" );
672644 return NULL ;
673645 }
674- ret = mbedtls_pk_parse_key (kctx , privkey , privkey_len , NULL , 0 , crypto_rng_wrapper , NULL );
646+ ret = mbedtls_pk_parse_key (kctx , privkey , privkey_len , NULL , 0 , mbedtls_esp_random , NULL );
675647
676648 if (ret < 0 ) {
677649 //crypto_print_error_string(ret);
@@ -727,17 +699,8 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
727699 mbedtls_ecdh_context * ctx = NULL ;
728700 mbedtls_pk_context * own = (mbedtls_pk_context * )key_own ;
729701 mbedtls_pk_context * peer = (mbedtls_pk_context * )key_peer ;
730- mbedtls_entropy_context entropy ;
731- mbedtls_ctr_drbg_context ctr_drbg ;
732702 int ret = -1 ;
733703
734- mbedtls_entropy_init (& entropy );
735- mbedtls_ctr_drbg_init (& ctr_drbg );
736-
737- if (mbedtls_ctr_drbg_seed (& ctr_drbg , mbedtls_entropy_func , & entropy , NULL , 0 ) < 0 ) {
738- goto fail ;
739- }
740-
741704 * secret_len = 0 ;
742705 ctx = os_malloc (sizeof (* ctx ));
743706 if (!ctx ) {
@@ -765,7 +728,7 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
765728 }
766729
767730 if (mbedtls_ecdh_calc_secret (ctx , secret_len , secret , DPP_MAX_SHARED_SECRET_LEN ,
768- mbedtls_ctr_drbg_random , & ctr_drbg ) < 0 ) {
731+ mbedtls_esp_random , NULL ) < 0 ) {
769732 wpa_printf (MSG_ERROR , "failed to calculate secret" );
770733 goto fail ;
771734 }
@@ -778,8 +741,6 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
778741 ret = 0 ;
779742
780743fail :
781- mbedtls_ctr_drbg_free (& ctr_drbg );
782- mbedtls_entropy_free (& entropy );
783744 if (ctx ) {
784745 mbedtls_ecdh_free (ctx );
785746 os_free (ctx );
@@ -804,7 +765,7 @@ int crypto_ecdsa_get_sign(unsigned char *hash,
804765 goto fail ;
805766 }
806767 ret = mbedtls_ecdsa_sign (& ctx -> MBEDTLS_PRIVATE (grp ), (mbedtls_mpi * )r , (mbedtls_mpi * )s ,
807- & ctx -> MBEDTLS_PRIVATE (d ), hash , SHA256_MAC_LEN , crypto_rng_wrapper , NULL );
768+ & ctx -> MBEDTLS_PRIVATE (d ), hash , SHA256_MAC_LEN , mbedtls_esp_random , NULL );
808769
809770fail :
810771 mbedtls_ecdsa_free (ctx );
@@ -901,7 +862,7 @@ struct crypto_ec_key * crypto_ec_key_gen(u16 ike_group)
901862 }
902863
903864 mbedtls_ecp_gen_key (MBEDTLS_ECP_DP_SECP256R1 , mbedtls_pk_ec (* kctx ), //get this from argument
904- crypto_rng_wrapper , NULL );
865+ mbedtls_esp_random , NULL );
905866
906867 return (struct crypto_ec_key * )kctx ;
907868fail :
@@ -1081,8 +1042,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
10811042
10821043struct crypto_ecdh * crypto_ecdh_init (int group )
10831044{
1084- mbedtls_ctr_drbg_context ctr_drbg ;
1085- mbedtls_entropy_context entropy ;
10861045 mbedtls_ecdh_context * ctx ;
10871046
10881047 ctx = os_zalloc (sizeof (* ctx ));
@@ -1100,33 +1059,19 @@ struct crypto_ecdh * crypto_ecdh_init(int group)
11001059 goto fail ;
11011060 }
11021061
1103- /* Initialize CTR_DRBG context */
1104- mbedtls_ctr_drbg_init (& ctr_drbg );
1105- mbedtls_entropy_init (& entropy );
1106-
1107- /* Seed and setup CTR_DRBG entropy source for future reseeds */
1108- if (mbedtls_ctr_drbg_seed (& ctr_drbg , mbedtls_entropy_func , & entropy , NULL , 0 ) != 0 ) {
1109- wpa_printf (MSG_ERROR , "Seeding entropy source failed" );
1110- goto fail ;
1111- }
1112-
11131062 /* Generates ECDH keypair on elliptic curve */
1114- if (mbedtls_ecdh_gen_public (ACCESS_ECDH (& ctx , grp ), ACCESS_ECDH (& ctx , d ), ACCESS_ECDH (& ctx , Q ), mbedtls_ctr_drbg_random , & ctr_drbg ) != 0 ) {
1063+ if (mbedtls_ecdh_gen_public (ACCESS_ECDH (& ctx , grp ), ACCESS_ECDH (& ctx , d ), ACCESS_ECDH (& ctx , Q ), mbedtls_esp_random , NULL ) != 0 ) {
11151064 wpa_printf (MSG_ERROR , "ECDH keypair on curve failed" );
11161065 goto fail ;
11171066 }
11181067
1119- mbedtls_ctr_drbg_free (& ctr_drbg );
1120- mbedtls_entropy_free (& entropy );
11211068 return (struct crypto_ecdh * )ctx ;
11221069fail :
11231070 if (ctx ) {
11241071 mbedtls_ecdh_free (ctx );
11251072 os_free (ctx );
11261073 ctx = NULL ;
11271074 }
1128- mbedtls_ctr_drbg_free (& ctr_drbg );
1129- mbedtls_entropy_free (& entropy );
11301075 return NULL ;
11311076}
11321077
@@ -1174,18 +1119,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
11741119 return 0 ;
11751120 }
11761121
1177- mbedtls_ctr_drbg_context ctr_drbg ;
1178- mbedtls_entropy_context entropy ;
1179-
1180- /* Initialize CTR_DRBG context */
1181- mbedtls_ctr_drbg_init (& ctr_drbg );
1182- mbedtls_entropy_init (& entropy );
1183-
1184- /* Seed and setup CTR_DRBG entropy source for future reseeds */
1185- if (mbedtls_ctr_drbg_seed (& ctr_drbg , mbedtls_entropy_func , & entropy , NULL , 0 ) != 0 ) {
1186- wpa_printf (MSG_ERROR , "Seeding entropy source failed" );
1187- goto cleanup ;
1188- }
11891122 len_prime = ACCESS_ECDH (ctx , grp ).pbits / 8 ;
11901123 bn_x = crypto_bignum_init_set (key , len );
11911124
@@ -1244,7 +1177,7 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12441177
12451178 /* Calculate secret
12461179 z = F(DH(x,Y)) */
1247- secret_key = mbedtls_ecdh_calc_secret (ctx , & olen , secret , len_prime , mbedtls_ctr_drbg_random , & ctr_drbg );
1180+ secret_key = mbedtls_ecdh_calc_secret (ctx , & olen , secret , len_prime , mbedtls_esp_random , NULL );
12481181 if (secret_key != 0 ) {
12491182 wpa_printf (MSG_ERROR , "Calculation of secret failed" );
12501183 goto cleanup ;
@@ -1259,8 +1192,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
12591192 crypto_ec_key_deinit (pkey );
12601193 crypto_bignum_deinit (bn_x , 1 );
12611194 crypto_ec_point_deinit (ec_pt , 1 );
1262- mbedtls_ctr_drbg_free (& ctr_drbg );
1263- mbedtls_entropy_free (& entropy );
12641195 return sh_secret ;
12651196}
12661197
0 commit comments