@@ -1435,6 +1435,10 @@ static wp_RsaGenCtx* wp_rsa_base_gen_init(WOLFPROV_CTX* provCtx,
14351435 return ctx ;
14361436}
14371437
1438+ #ifndef WP_RSA_KEYGEN_MAX_RETRY_CNT
1439+ #define WP_RSA_KEYGEN_MAX_RETRY_CNT 10
1440+ #endif
1441+
14381442/**
14391443 * Generate RSA key pair using wolfSSL.
14401444 *
@@ -1447,29 +1451,47 @@ static wp_RsaGenCtx* wp_rsa_base_gen_init(WOLFPROV_CTX* provCtx,
14471451static wp_Rsa * wp_rsa_gen (wp_RsaGenCtx * ctx , OSSL_CALLBACK * cb , void * cbArg )
14481452{
14491453 wp_Rsa * rsa = NULL ;
1454+ int rc = -1 ;
1455+ int i = 0 ;
14501456
14511457 (void )cb ;
14521458 (void )cbArg ;
14531459
14541460 if (wolfssl_prov_is_running () && wp_rsagen_check_key_size (ctx )) {
14551461 rsa = wp_rsa_base_new (ctx -> provCtx , ctx -> type );
14561462 if (rsa != NULL ) {
1457- int rc = wc_MakeRsaKey (& rsa -> key , (int )ctx -> bits , ctx -> e ,
1458- & ctx -> rng );
1459- if (rc != 0 ) {
1460- wp_rsa_free (rsa );
1461- rsa = NULL ;
1462- }
1463- else {
1464- rsa -> type = ctx -> type ;
1465- rsa -> bits = (int )ctx -> bits ;
1466- rsa -> hasPub = 1 ;
1467- rsa -> hasPriv = 1 ;
1468- rsa -> pssParams = ctx -> pssParams ;
1463+ /* wolfCrypt FIPS RSA keygen has a small chance it simply will not
1464+ * find RSA primes within the failCount. Account for this by
1465+ * retrying here. For simplicity we will always use this flow
1466+ * even for non-FIPS case. */
1467+ for (i = 0 ; i < WP_RSA_KEYGEN_MAX_RETRY_CNT ; i ++ ) {
1468+ rc = wc_MakeRsaKey (& rsa -> key , (int )ctx -> bits , ctx -> e ,
1469+ & ctx -> rng );
1470+ if (rc == PRIME_GEN_E ) {
1471+ /* retry */
1472+ }
1473+ else if (rc != 0 ) {
1474+ wp_rsa_free (rsa );
1475+ rsa = NULL ;
1476+ break ;
1477+ }
1478+ else {
1479+ rsa -> type = ctx -> type ;
1480+ rsa -> bits = (int )ctx -> bits ;
1481+ rsa -> hasPub = 1 ;
1482+ rsa -> hasPriv = 1 ;
1483+ rsa -> pssParams = ctx -> pssParams ;
1484+ break ;
1485+ }
14691486 }
14701487 }
14711488 }
14721489
1490+ if (i == WP_RSA_KEYGEN_MAX_RETRY_CNT ) {
1491+ wp_rsa_free (rsa );
1492+ rsa = NULL ;
1493+ }
1494+
14731495 return rsa ;
14741496}
14751497
0 commit comments