diff --git a/src/wp_rsa_asym.c b/src/wp_rsa_asym.c index 3611705e..dd070c3a 100644 --- a/src/wp_rsa_asym.c +++ b/src/wp_rsa_asym.c @@ -282,6 +282,7 @@ static int wp_rsaa_encrypt(wp_RsaAsymCtx* ctx, unsigned char* out, size_t* outLen, size_t outSize, const unsigned char* in, size_t inLen) { int ok = 1; + word32 sz; if (!wolfssl_prov_is_running()) { ok = 0; @@ -318,6 +319,14 @@ static int wp_rsaa_encrypt(wp_RsaAsymCtx* ctx, unsigned char* out, ok = 0; } } + else if (ctx->padMode == RSA_NO_PADDING) { + sz = (word32)outSize; + rc = wc_RsaDirect((byte*)in, (word32)inLen, out, &sz, + wp_rsa_get_key(ctx->rsa), RSA_PUBLIC_ENCRYPT, &ctx->rng); + if (rc < 0) { + ok = 0; + } + } else { ok = 0; } @@ -371,6 +380,7 @@ static int wp_rsaa_decrypt(wp_RsaAsymCtx* ctx, unsigned char* out, size_t* outLen, size_t outSize, const unsigned char* in, size_t inLen) { int ok = 1; + word32 sz; if (!wolfssl_prov_is_running()) { ok = 0; @@ -455,6 +465,16 @@ static int wp_rsaa_decrypt(wp_RsaAsymCtx* ctx, unsigned char* out, } } } + else if (ctx->padMode == RSA_NO_PADDING) { + sz = (word32)outSize; + PRIVATE_KEY_UNLOCK(); + rc = wc_RsaDirect((byte*)in, (word32)inLen, out, &sz, + wp_rsa_get_key(ctx->rsa), RSA_PRIVATE_DECRYPT, &ctx->rng); + PRIVATE_KEY_LOCK(); + if (rc < 0) { + ok = 0; + } + } else { ok = 0; } diff --git a/test/test_rsa.c b/test/test_rsa.c index 510ffc87..6d86fe8d 100644 --- a/test/test_rsa.c +++ b/test/test_rsa.c @@ -682,6 +682,26 @@ int test_rsa_enc_dec_pkcs1(void *data) return err; } +int test_rsa_enc_dec_nopad(void *data) +{ + int err = 0; + + (void)data; + + if (!noKeyLimits) { + PRINT_MSG("Check that private decrypt fails with invalid key size."); + err = test_rsa_enc_dec(rsa_key_der_256, sizeof(rsa_key_der_256), + RSA_NO_PADDING, NULL, NULL) != 1; + } + if (err == 0) { + PRINT_MSG("Check that private decrypt works with valid key size."); + err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), + RSA_NO_PADDING, NULL, NULL); + } + + return err; +} + int test_rsa_enc_dec_oaep(void *data) { int err = 0; diff --git a/test/unit.c b/test/unit.c index 3adedbe4..d6ac289e 100644 --- a/test/unit.c +++ b/test/unit.c @@ -159,6 +159,7 @@ TEST_CASE test_case[] = { TEST_DECL(test_rsa_sign_verify_x931, NULL), TEST_DECL(test_rsa_enc_dec_pkcs1, NULL), TEST_DECL(test_rsa_enc_dec_oaep, NULL), + TEST_DECL(test_rsa_enc_dec_nopad, NULL), TEST_DECL(test_rsa_pkey_keygen, NULL), TEST_DECL(test_rsa_pkey_invalid_key_size, NULL), TEST_DECL(test_rsa_get_params, NULL), diff --git a/test/unit.h b/test/unit.h index 53d48be9..30b30c5d 100644 --- a/test/unit.h +++ b/test/unit.h @@ -237,6 +237,7 @@ int test_rsa_sign_verify_pss(void *data); int test_rsa_sign_verify_x931(void *data); int test_rsa_enc_dec_pkcs1(void *data); int test_rsa_enc_dec_oaep(void *data); +int test_rsa_enc_dec_nopad(void *data); int test_rsa_pkey_keygen(void *data); int test_rsa_pkey_invalid_key_size(void *data); int test_rsa_get_params(void *data);