@@ -557,30 +557,19 @@ static int km_rsa_ctx_init(struct km_rsa_ctx * ctx, int hash_oid)
557557 goto out ;
558558 }
559559
560- ret = wc_InitRng (& ctx -> rng );
561- if (ret ) {
562- pr_err ("%s: init rng returned: %d\n" , WOLFKM_RSA_DRIVER , ret );
563- if (ret == WC_NO_ERR_TRACE (MEMORY_E ))
564- ret = - ENOMEM ;
565- else
566- ret = - EINVAL ;
567- goto out ;
568- }
569-
570560 ret = wc_InitRsaKey (ctx -> key , NULL );
571561 if (ret ) {
572562 pr_err ("%s: init rsa key returned: %d\n" , WOLFKM_RSA_DRIVER , ret );
573563 ret = - EINVAL ;
574564 goto out ;
575565 }
576566
577- #ifdef WC_RSA_BLINDING
567+ /* Note the initialization of ctx->rng is deferred unless/until needed. */
578568 ret = wc_RsaSetRNG (ctx -> key , & ctx -> rng );
579569 if (ret ) {
580570 ret = - EINVAL ;
581571 goto out ;
582572 }
583- #endif /* WC_RSA_BLINDING */
584573
585574 ctx -> hash_oid = hash_oid ;
586575
@@ -638,6 +627,31 @@ static int km_rsa_ctx_init(struct km_rsa_ctx * ctx, int hash_oid)
638627 return ret ;
639628}
640629
630+ static inline int km_rsa_ctx_init_rng (struct km_rsa_ctx * ctx ) {
631+ switch (ctx -> rng .status ) {
632+ case WC_DRBG_OK :
633+ return 0 ;
634+ case WC_DRBG_NOT_INIT :
635+ {
636+ int err ;
637+ if (WOLFSSL_ATOMIC_LOAD (linuxkm_lkcapi_registering_now ))
638+ err = LKCAPI_INITRNG_FOR_SELFTEST (& ctx -> rng );
639+ else
640+ err = wc_InitRng (& ctx -> rng );
641+ if (err ) {
642+ pr_err ("%s: init rng returned: %d\n" , WOLFKM_RSA_DRIVER , err );
643+ if (err == WC_NO_ERR_TRACE (MEMORY_E ))
644+ return - ENOMEM ;
645+ else
646+ return - EINVAL ;
647+ }
648+ return 0 ;
649+ }
650+ default :
651+ return - EINVAL ;
652+ }
653+ }
654+
641655#if defined(LINUXKM_DIRECT_RSA )
642656/*
643657 * RSA encrypt with public key.
@@ -704,8 +718,15 @@ static int km_direct_rsa_enc(struct akcipher_request *req)
704718 scatterwalk_map_and_copy (dec , req -> src , 0 , req -> src_len , 0 );
705719
706720 /* note: matching behavior of kernel rsa-generic. */
721+
722+ /* note, currently WOLF_CRYPTO_CB is not supported for linuxkm, and the rng
723+ * are to wc_RsaFunction() is not actually used for low level (no-pad)
724+ * public key ops in the native implementation (it is a pure function of its
725+ * input args).
726+ */
727+
707728 err = wc_RsaFunction (dec , req -> src_len , enc , & out_len ,
708- RSA_PUBLIC_ENCRYPT , ctx -> key , & ctx -> rng );
729+ RSA_PUBLIC_ENCRYPT , ctx -> key , NULL /* rng */ );
709730
710731 if (unlikely (err || (out_len != ctx -> key_len ))) {
711732 #ifdef WOLFKM_DEBUG_RSA
@@ -789,6 +810,10 @@ static int km_direct_rsa_dec(struct akcipher_request *req)
789810 memset (dec , 0 , req -> dst_len );
790811 scatterwalk_map_and_copy (enc , req -> src , 0 , req -> src_len , 0 );
791812
813+ err = km_rsa_ctx_init_rng (ctx );
814+ if (err )
815+ goto rsa_dec_out ;
816+
792817 err = wc_RsaDirect (enc , ctx -> key_len , dec , & out_len ,
793818 ctx -> key , RSA_PRIVATE_DECRYPT , & ctx -> rng );
794819
@@ -848,12 +873,11 @@ static int km_rsa_set_priv(struct crypto_akcipher *tfm, const void *key,
848873 return - ENOMEM ;
849874 }
850875
851- #ifdef WC_RSA_BLINDING
876+ /* Note the initialization of ctx->rng is deferred unless/until needed. */
852877 err = wc_RsaSetRNG (ctx -> key , & ctx -> rng );
853878 if (unlikely (err )) {
854879 return - ENOMEM ;
855880 }
856- #endif /* WC_RSA_BLINDING */
857881 }
858882
859883 err = wc_RsaPrivateKeyDecode (key , & idx , ctx -> key , keylen );
@@ -907,6 +931,12 @@ static int km_rsa_set_pub(struct crypto_akcipher *tfm, const void *key,
907931 if (unlikely (err )) {
908932 return - ENOMEM ;
909933 }
934+
935+ /* Note the initialization of ctx->rng is deferred unless/until needed. */
936+ err = wc_RsaSetRNG (ctx -> key , & ctx -> rng );
937+ if (unlikely (err )) {
938+ return - ENOMEM ;
939+ }
910940 }
911941
912942 err = wc_RsaPublicKeyDecode (key , & idx , ctx -> key , keylen );
@@ -1114,6 +1144,10 @@ static int km_pkcs1pad_sign(struct akcipher_request *req)
11141144 goto pkcs1pad_sign_out ;
11151145 }
11161146
1147+ err = km_rsa_ctx_init_rng (ctx );
1148+ if (err )
1149+ goto pkcs1pad_sign_out ;
1150+
11171151 /* sign encoded message. */
11181152 sig_len = wc_RsaSSL_Sign (msg , enc_len , sig ,
11191153 ctx -> key_len , ctx -> key , & ctx -> rng );
@@ -1355,6 +1389,10 @@ static int km_pkcs1_sign(struct crypto_sig *tfm,
13551389 goto pkcs1_sign_out ;
13561390 }
13571391
1392+ err = km_rsa_ctx_init_rng (ctx );
1393+ if (err )
1394+ goto pkcs1_sign_out ;
1395+
13581396 /* sign encoded message. */
13591397 sig_len = wc_RsaSSL_Sign (msg , enc_msg_len , sig ,
13601398 ctx -> key_len , ctx -> key , & ctx -> rng );
@@ -1522,12 +1560,11 @@ static int km_pkcs1_set_priv(struct crypto_sig *tfm, const void *key,
15221560 return - ENOMEM ;
15231561 }
15241562
1525- #ifdef WC_RSA_BLINDING
1563+ /* Note the initialization of ctx->rng is deferred unless/until needed. */
15261564 err = wc_RsaSetRNG (ctx -> key , & ctx -> rng );
15271565 if (unlikely (err )) {
15281566 return - ENOMEM ;
15291567 }
1530- #endif /* WC_RSA_BLINDING */
15311568 }
15321569
15331570 err = wc_RsaPrivateKeyDecode (key , & idx , ctx -> key , keylen );
@@ -1667,6 +1704,10 @@ static int km_pkcs1pad_enc(struct akcipher_request *req)
16671704 memset (enc , 0 , req -> dst_len );
16681705 scatterwalk_map_and_copy (dec , req -> src , 0 , req -> src_len , 0 );
16691706
1707+ err = km_rsa_ctx_init_rng (ctx );
1708+ if (err )
1709+ goto pkcs1_enc_out ;
1710+
16701711 err = wc_RsaPublicEncrypt (dec , req -> src_len , enc , ctx -> key_len ,
16711712 ctx -> key , & ctx -> rng );
16721713
@@ -1741,6 +1782,12 @@ static int km_pkcs1pad_dec(struct akcipher_request *req)
17411782 memset (dec , 0 , req -> dst_len );
17421783 scatterwalk_map_and_copy (enc , req -> src , 0 , req -> src_len , 0 );
17431784
1785+ #ifdef WC_RSA_BLINDING
1786+ err = km_rsa_ctx_init_rng (ctx );
1787+ if (err )
1788+ goto pkcs1_dec_out ;
1789+ #endif
1790+
17441791 dec_len = wc_RsaPrivateDecrypt (enc , ctx -> key_len , dec , req -> dst_len ,
17451792 ctx -> key );
17461793
@@ -2054,7 +2101,8 @@ static int linuxkm_test_rsa_driver(const char * driver, int nbits)
20542101 memset (& rng , 0 , sizeof (rng ));
20552102 memset (key , 0 , sizeof (RsaKey ));
20562103
2057- ret = wc_InitRng (& rng );
2104+ ret = LKCAPI_INITRNG_FOR_SELFTEST (& rng );
2105+
20582106 if (ret ) {
20592107 pr_err ("error: init rng returned: %d\n" , ret );
20602108 goto test_rsa_end ;
@@ -2068,13 +2116,11 @@ static int linuxkm_test_rsa_driver(const char * driver, int nbits)
20682116 }
20692117 init_key = 1 ;
20702118
2071- #ifdef WC_RSA_BLINDING
20722119 ret = wc_RsaSetRNG (key , & rng );
20732120 if (ret ) {
20742121 pr_err ("error: rsa set rng returned: %d\n" , ret );
20752122 goto test_rsa_end ;
20762123 }
2077- #endif /* WC_RSA_BLINDING */
20782124
20792125 #ifdef HAVE_FIPS
20802126 for (;;) {
@@ -2425,7 +2471,7 @@ static int linuxkm_test_pkcs1pad_driver(const char * driver, int nbits,
24252471 memset (& rng , 0 , sizeof (rng ));
24262472 memset (key , 0 , sizeof (RsaKey ));
24272473
2428- ret = wc_InitRng (& rng );
2474+ ret = LKCAPI_INITRNG_FOR_SELFTEST (& rng );
24292475 if (ret ) {
24302476 pr_err ("error: init rng returned: %d\n" , ret );
24312477 goto test_pkcs1_end ;
@@ -2440,14 +2486,12 @@ static int linuxkm_test_pkcs1pad_driver(const char * driver, int nbits,
24402486 }
24412487 init_key = 1 ;
24422488
2443- #ifdef WC_RSA_BLINDING
24442489 ret = wc_RsaSetRNG (key , & rng );
24452490 if (ret ) {
24462491 pr_err ("error: rsa set rng returned: %d\n" , ret );
24472492 test_rc = ret ;
24482493 goto test_pkcs1_end ;
24492494 }
2450- #endif /* WC_RSA_BLINDING */
24512495
24522496 #ifdef HAVE_FIPS
24532497 for (;;) {
@@ -2935,7 +2979,7 @@ static int linuxkm_test_pkcs1_driver(const char * driver, int nbits,
29352979 memset (& rng , 0 , sizeof (rng ));
29362980 memset (key , 0 , sizeof (RsaKey ));
29372981
2938- ret = wc_InitRng (& rng );
2982+ ret = LKCAPI_INITRNG_FOR_SELFTEST (& rng );
29392983 if (ret ) {
29402984 pr_err ("error: init rng returned: %d\n" , ret );
29412985 goto test_pkcs1_end ;
@@ -2950,14 +2994,12 @@ static int linuxkm_test_pkcs1_driver(const char * driver, int nbits,
29502994 }
29512995 init_key = 1 ;
29522996
2953- #ifdef WC_RSA_BLINDING
29542997 ret = wc_RsaSetRNG (key , & rng );
29552998 if (ret ) {
29562999 pr_err ("error: rsa set rng returned: %d\n" , ret );
29573000 test_rc = ret ;
29583001 goto test_pkcs1_end ;
29593002 }
2960- #endif /* WC_RSA_BLINDING */
29613003
29623004 #ifdef HAVE_FIPS
29633005 for (;;) {
0 commit comments