Skip to content

Commit 8b1c601

Browse files
authored
Merge pull request #110 from ColtonWilley/wp_rsa_crypt_nopad
Initial implementation of RSA encrypt/decrypt without padding
2 parents eeab709 + bbd42d7 commit 8b1c601

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/wp_rsa_asym.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ static int wp_rsaa_encrypt(wp_RsaAsymCtx* ctx, unsigned char* out,
282282
size_t* outLen, size_t outSize, const unsigned char* in, size_t inLen)
283283
{
284284
int ok = 1;
285+
word32 sz;
285286

286287
if (!wolfssl_prov_is_running()) {
287288
ok = 0;
@@ -318,6 +319,14 @@ static int wp_rsaa_encrypt(wp_RsaAsymCtx* ctx, unsigned char* out,
318319
ok = 0;
319320
}
320321
}
322+
else if (ctx->padMode == RSA_NO_PADDING) {
323+
sz = (word32)outSize;
324+
rc = wc_RsaDirect((byte*)in, (word32)inLen, out, &sz,
325+
wp_rsa_get_key(ctx->rsa), RSA_PUBLIC_ENCRYPT, &ctx->rng);
326+
if (rc < 0) {
327+
ok = 0;
328+
}
329+
}
321330
else {
322331
ok = 0;
323332
}
@@ -371,6 +380,7 @@ static int wp_rsaa_decrypt(wp_RsaAsymCtx* ctx, unsigned char* out,
371380
size_t* outLen, size_t outSize, const unsigned char* in, size_t inLen)
372381
{
373382
int ok = 1;
383+
word32 sz;
374384

375385
if (!wolfssl_prov_is_running()) {
376386
ok = 0;
@@ -455,6 +465,16 @@ static int wp_rsaa_decrypt(wp_RsaAsymCtx* ctx, unsigned char* out,
455465
}
456466
}
457467
}
468+
else if (ctx->padMode == RSA_NO_PADDING) {
469+
sz = (word32)outSize;
470+
PRIVATE_KEY_UNLOCK();
471+
rc = wc_RsaDirect((byte*)in, (word32)inLen, out, &sz,
472+
wp_rsa_get_key(ctx->rsa), RSA_PRIVATE_DECRYPT, &ctx->rng);
473+
PRIVATE_KEY_LOCK();
474+
if (rc < 0) {
475+
ok = 0;
476+
}
477+
}
458478
else {
459479
ok = 0;
460480
}

test/test_rsa.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,26 @@ int test_rsa_enc_dec_pkcs1(void *data)
682682
return err;
683683
}
684684

685+
int test_rsa_enc_dec_nopad(void *data)
686+
{
687+
int err = 0;
688+
689+
(void)data;
690+
691+
if (!noKeyLimits) {
692+
PRINT_MSG("Check that private decrypt fails with invalid key size.");
693+
err = test_rsa_enc_dec(rsa_key_der_256, sizeof(rsa_key_der_256),
694+
RSA_NO_PADDING, NULL, NULL) != 1;
695+
}
696+
if (err == 0) {
697+
PRINT_MSG("Check that private decrypt works with valid key size.");
698+
err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048),
699+
RSA_NO_PADDING, NULL, NULL);
700+
}
701+
702+
return err;
703+
}
704+
685705
int test_rsa_enc_dec_oaep(void *data)
686706
{
687707
int err = 0;

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ TEST_CASE test_case[] = {
159159
TEST_DECL(test_rsa_sign_verify_x931, NULL),
160160
TEST_DECL(test_rsa_enc_dec_pkcs1, NULL),
161161
TEST_DECL(test_rsa_enc_dec_oaep, NULL),
162+
TEST_DECL(test_rsa_enc_dec_nopad, NULL),
162163
TEST_DECL(test_rsa_pkey_keygen, NULL),
163164
TEST_DECL(test_rsa_pkey_invalid_key_size, NULL),
164165
TEST_DECL(test_rsa_get_params, NULL),

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ int test_rsa_sign_verify_pss(void *data);
237237
int test_rsa_sign_verify_x931(void *data);
238238
int test_rsa_enc_dec_pkcs1(void *data);
239239
int test_rsa_enc_dec_oaep(void *data);
240+
int test_rsa_enc_dec_nopad(void *data);
240241
int test_rsa_pkey_keygen(void *data);
241242
int test_rsa_pkey_invalid_key_size(void *data);
242243
int test_rsa_get_params(void *data);

0 commit comments

Comments
 (0)