Skip to content

Commit 305a754

Browse files
committed
Improvements to RSA padding. Expose API's to support external pad/unpad.
1 parent 43f4ba9 commit 305a754

3 files changed

Lines changed: 36 additions & 26 deletions

File tree

examples/async/include.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
# All paths should be given relative to the root
33

44
if BUILD_ASYNCCRYPT
5+
56
noinst_HEADERS += examples/async/async_tls.h
67

8+
if BUILD_EXAMPLE_CLIENTS
79
noinst_PROGRAMS += examples/async/async_client
810
examples_async_async_client_SOURCES = examples/async/async_client.c examples/async/async_tls.c
911
examples_async_async_client_LDADD = src/libwolfssl@LIBSUFFIX@.la $(LIB_STATIC_ADD)
1012
examples_async_async_client_DEPENDENCIES = src/libwolfssl@LIBSUFFIX@.la
1113
examples_async_async_client_CFLAGS = $(AM_CFLAGS)
14+
endif
1215

16+
if BUILD_EXAMPLE_SERVERS
1317
noinst_PROGRAMS += examples/async/async_server
1418
examples_async_async_server_SOURCES = examples/async/async_server.c examples/async/async_tls.c
1519
examples_async_async_server_LDADD = src/libwolfssl@LIBSUFFIX@.la $(LIB_STATIC_ADD)
1620
examples_async_async_server_DEPENDENCIES = src/libwolfssl@LIBSUFFIX@.la
1721
examples_async_async_server_CFLAGS = $(AM_CFLAGS)
1822
endif
23+
endif
1924

2025
dist_example_DATA+= examples/async/async_server.c
2126
dist_example_DATA+= examples/async/async_client.c

wolfcrypt/src/rsa.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,23 @@ enum {
129129

130130
static void wc_RsaCleanup(RsaKey* key)
131131
{
132-
#if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_NO_MALLOC)
133-
if (key && key->data) {
132+
#if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \
133+
(!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE)))
134+
if (key != NULL) {
135+
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
136+
/* if private operation zero temp buffer */
137+
if ((key->data != NULL && key->dataLen > 0) &&
138+
(key->type == RSA_PRIVATE_DECRYPT ||
139+
key->type == RSA_PRIVATE_ENCRYPT)) {
140+
ForceZero(key->data, key->dataLen);
141+
}
142+
#endif
134143
/* make sure any allocated memory is free'd */
135144
if (key->dataIsAlloc) {
136-
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
137-
if (key->type == RSA_PRIVATE_DECRYPT ||
138-
key->type == RSA_PRIVATE_ENCRYPT) {
139-
ForceZero(key->data, key->dataLen);
140-
}
141-
#endif
142145
XFREE(key->data, key->heap, DYNAMIC_TYPE_WOLF_BIGINT);
143146
key->dataIsAlloc = 0;
144147
}
148+
145149
key->data = NULL;
146150
key->dataLen = 0;
147151
}
@@ -163,10 +167,11 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
163167
key->type = RSA_TYPE_UNKNOWN;
164168
key->state = RSA_STATE_NONE;
165169
key->heap = heap;
166-
#if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_NO_MALLOC)
170+
#if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \
171+
(!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE)))
167172
key->dataIsAlloc = 0;
168-
key->data = NULL;
169173
#endif
174+
key->data = NULL;
170175
key->dataLen = 0;
171176
#ifdef WC_RSA_BLINDING
172177
key->rng = NULL;
@@ -3504,6 +3509,7 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35043509
break;
35053510
}
35063511
XMEMCPY(key->data, in, inLen);
3512+
key->dataLen = inLen;
35073513
}
35083514
else {
35093515
key->dataIsAlloc = 0;
@@ -3537,13 +3543,13 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35373543
case RSA_STATE_DECRYPT_UNPAD:
35383544
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
35393545
!defined(WOLFSSL_NO_MALLOC)
3540-
ret = wc_RsaUnPad_ex(key->data, key->dataLen, &pad, pad_value, pad_type,
3541-
hash, mgf, label, labelSz, saltLen,
3542-
mp_count_bits(&key->n), key->heap);
3546+
ret = wc_RsaUnPad_ex(key->data,
3547+
key->dataLen, &pad, pad_value, pad_type, hash, mgf,
3548+
label, labelSz, saltLen, mp_count_bits(&key->n), key->heap);
35433549
#else
3544-
ret = wc_RsaUnPad_ex(out, key->dataLen, &pad, pad_value, pad_type, hash,
3545-
mgf, label, labelSz, saltLen,
3546-
mp_count_bits(&key->n), key->heap);
3550+
ret = wc_RsaUnPad_ex(out,
3551+
key->dataLen, &pad, pad_value, pad_type, hash, mgf, label,
3552+
labelSz, saltLen, mp_count_bits(&key->n), key->heap);
35473553
#endif
35483554
if (rsa_type == RSA_PUBLIC_DECRYPT && ret > (int)outLen) {
35493555
ret = RSA_BUFFER_E;

wolfssl/wolfcrypt/rsa.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ struct RsaKey {
242242
char label[RSA_MAX_LABEL_LEN];
243243
int labelLen;
244244
#endif
245-
#if defined(WOLFSSL_ASYNC_CRYPT) || !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
246-
!defined(WOLFSSL_NO_MALLOC)
245+
#if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \
246+
(!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE)))
247247
byte dataIsAlloc;
248248
#endif
249249
#ifdef WC_RSA_NONBLOCK
@@ -441,14 +441,13 @@ WOLFSSL_API int wc_RsaExportKey(RsaKey* key,
441441
int nlen, int* isPrime);
442442
#endif
443443

444-
WOLFSSL_LOCAL int wc_RsaPad_ex(const byte* input, word32 inputLen, byte* pkcsBlock,
445-
word32 pkcsBlockLen, byte padValue, WC_RNG* rng, int padType,
446-
enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen,
447-
int saltLen, int bits, void* heap);
448-
WOLFSSL_LOCAL int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
449-
byte padValue, int padType, enum wc_HashType hType,
450-
int mgf, byte* optLabel, word32 labelLen, int saltLen,
451-
int bits, void* heap);
444+
WOLFSSL_API int wc_RsaPad_ex(const byte* input, word32 inputLen,
445+
byte* pkcsBlock, word32 pkcsBlockLen, byte padValue,
446+
WC_RNG* rng, int padType, enum wc_HashType hType, int mgf,
447+
byte* optLabel, word32 labelLen, int saltLen, int bits, void* heap);
448+
WOLFSSL_API int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen,
449+
byte** out, byte padValue, int padType, enum wc_HashType hType, int mgf,
450+
byte* optLabel, word32 labelLen, int saltLen, int bits, void* heap);
452451

453452
WOLFSSL_LOCAL int wc_hash2mgf(enum wc_HashType hType);
454453
WOLFSSL_LOCAL int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key,

0 commit comments

Comments
 (0)