diff --git a/src/ssl.c b/src/ssl.c index 7ac80bc0a..56e6eb9b2 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -52769,6 +52769,18 @@ int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa) } return wc_RsaEncryptSize((RsaKey*)rsa->internal); } +/* return RSA modulus in bits */ +/* @param rsa a pointer to WOLFSSL_RSA structur */ +/* @return RSA modulus size in bits, 0 if error */ +int wolfSSL_RSA_bits(const WOLFSSL_RSA* rsa) +{ + WOLFSSL_ENTER("wolfSSL_RSA_bits"); + + if (rsa == NULL) + return WOLFSSL_FAILURE; + + return wolfSSL_BN_num_bits(rsa->n); +} #endif #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) && \ diff --git a/tests/api.c b/tests/api.c index 4150f5cf9..c20c1c3fb 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33117,6 +33117,11 @@ static void test_wolfSSL_RSA(void) AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL)); AssertIntEQ(RSA_size(rsa), 256); + + /* sanity check */ + AssertIntEQ(RSA_bits(NULL), 0); + + AssertIntEQ(RSA_bits(rsa), 2048); RSA_get0_key(rsa, &n, &e, &d); AssertPtrEq(rsa->n, n); AssertPtrEq(rsa->e, e); @@ -33128,11 +33133,15 @@ static void test_wolfSSL_RSA(void) AssertPtrEq(rsa->n, n); AssertPtrEq(rsa->e, e); AssertPtrEq(rsa->d, d); + + AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1); + AssertIntEQ(RSA_bits(rsa), 21); RSA_free(rsa); - + #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2)) AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL)); AssertIntEQ(RSA_size(rsa), 384); + AssertIntEQ(RSA_bits(rsa), 3072); RSA_free(rsa); #endif diff --git a/wolfssl/openssl/rsa.h b/wolfssl/openssl/rsa.h index d62ee9337..dd07fd49e 100644 --- a/wolfssl/openssl/rsa.h +++ b/wolfssl/openssl/rsa.h @@ -111,6 +111,7 @@ WOLFSSL_API int wolfSSL_RSA_private_encrypt(int len, unsigned char* in, unsigned char* out, WOLFSSL_RSA* rsa, int padding); WOLFSSL_API int wolfSSL_RSA_size(const WOLFSSL_RSA*); +WOLFSSL_API int wolfSSL_RSA_bits(const WOLFSSL_RSA*); WOLFSSL_API int wolfSSL_RSA_sign(int type, const unsigned char* m, unsigned int mLen, unsigned char* sigRet, unsigned int* sigLen, WOLFSSL_RSA*); diff --git a/wolfssl/openssl/ssl.h b/wolfssl/openssl/ssl.h index e9b201720..70a79aaa0 100644 --- a/wolfssl/openssl/ssl.h +++ b/wolfssl/openssl/ssl.h @@ -806,7 +806,7 @@ wolfSSL_X509_STORE_set_verify_cb((WOLFSSL_X509_STORE *)(s), (WOLFSSL_X509_STORE_ #define RSA_generate_key wolfSSL_RSA_generate_key #define SSL_CTX_set_tmp_rsa_callback wolfSSL_CTX_set_tmp_rsa_callback #define RSA_print wolfSSL_RSA_print -#define RSA_bits wolfSSL_RSA_size +#define RSA_bits wolfSSL_RSA_bits #define RSA_up_ref wolfSSL_RSA_up_ref #define RSA_padding_add_PKCS1_PSS wolfSSL_RSA_padding_add_PKCS1_PSS #define RSA_verify_PKCS1_PSS wolfSSL_RSA_verify_PKCS1_PSS