Skip to content

Commit 43879f9

Browse files
committed
Fix RSA TSIP to return the actual cipher or plain length on success. Fix crypto callback to properly support PKCSv1.5 sign/verify and encrypt/decrypt based on padding info.
1 parent 3179a2f commit 43879f9

8 files changed

Lines changed: 53 additions & 77 deletions

File tree

wolfcrypt/src/cryptocb.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
445445

446446
#ifdef WOLF_CRYPTO_CB_RSA_PAD
447447
int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
448-
word32* outLen, int type, RsaKey* key, WC_RNG* rng,
449-
RsaPadding *padding)
448+
word32* outLen, int type, RsaKey* key, WC_RNG* rng,
449+
RsaPadding *padding)
450450
{
451451
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
452452
CryptoCb* dev;
@@ -458,9 +458,8 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
458458
/* locate registered callback */
459459
dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
460460

461-
if (padding) {
462-
switch(padding->pad_type) {
463-
#ifndef NO_PKCS11_RSA_PKCS
461+
if (padding != NULL) {
462+
switch (padding->pad_type) {
464463
case WC_RSA_PKCSV15_PAD:
465464
pk_type = WC_PK_TYPE_RSA_PKCS;
466465
break;
@@ -470,7 +469,6 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
470469
case WC_RSA_OAEP_PAD:
471470
pk_type = WC_PK_TYPE_RSA_OAEP;
472471
break;
473-
#endif /* NO_PKCS11_RSA_PKCS */
474472
default:
475473
pk_type = WC_PK_TYPE_RSA;
476474
}
@@ -497,7 +495,7 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
497495

498496
return wc_CryptoCb_TranslateErrorCode(ret);
499497
}
500-
#endif
498+
#endif /* WOLF_CRYPTO_CB_RSA_PAD */
501499

502500
#ifdef WOLFSSL_KEY_GEN
503501
int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)

wolfcrypt/src/port/Renesas/renesas_common.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,35 +251,33 @@ static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
251251
}
252252

253253
if (info->algo_type == WC_ALGO_TYPE_PK) {
254-
#if !defined(NO_RSA) && defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
255-
#if defined(WOLFSSL_KEY_GEN)
256-
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN &&
257-
(info->pk.rsakg.size == 1024 || info->pk.rsakg.size == 2048)) {
254+
#if !defined(NO_RSA)
255+
#if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
256+
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
258257
ret = wc_tsip_MakeRsaKey(info->pk.rsakg.size, (void*)ctx);
259-
} else
260-
#endif
261-
if (info->pk.type == WC_PK_TYPE_RSA &&
262-
(info->pk.rsa.type == RSA_PRIVATE_DECRYPT ||
263-
info->pk.rsa.type == RSA_PUBLIC_ENCRYPT)) {
264-
/* rsa public encrypt/private decrypt */
265-
ret = wc_tsip_RsaFunction(info, cbInfo);
266-
} else
267-
#endif
268-
if (info->pk.type == WC_PK_TYPE_RSA &&
269-
info->pk.rsa.type == RSA_PRIVATE_ENCRYPT) {
270-
/* RSA Signing
271-
* Can handle only RSA PkCS#1v1.5 padding scheme here.
272-
*/
273-
ret = tsip_SignRsaPkcs(info, cbInfo);
274-
}
275-
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
276-
/* RSA Verify */
277-
else if (info->pk.type == WC_PK_TYPE_RSA &&
278-
info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
279-
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
280258
}
281259
#endif
282-
260+
/* tsip only supports PKCSV15 padding scheme */
261+
if (info->pk.type == WC_PK_TYPE_RSA_PKCS) {
262+
RsaPadding* pad = info->pk.rsa.padding;
263+
if (pad && pad->pad_value == RSA_BLOCK_TYPE_1) {
264+
/* sign / verify */
265+
if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT ||
266+
info->pk.rsa.type == RSA_PRIVATE_DECRYPT) {
267+
ret = tsip_SignRsaPkcs(info, cbInfo);
268+
}
269+
else {
270+
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
271+
}
272+
}
273+
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
274+
else if (pad && pad->pad_value == RSA_BLOCK_TYPE_2) {
275+
/* encrypt/decrypt */
276+
ret = wc_tsip_RsaFunction(info, cbInfo);
277+
}
278+
#endif
279+
}
280+
#endif /* !NO_RSA */
283281
#if defined(HAVE_ECC)
284282
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
285283
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
@@ -468,7 +466,7 @@ int Renesas_cmn_usable(const struct WOLFSSL* ssl, byte session_key_generated)
468466
* Get Callback ctx by devId
469467
*
470468
* devId : devId to get its CTX
471-
* return asocciated CTX when the method is successfully called.
469+
* return associated CTX when the method is successfully called.
472470
* otherwise, NULL
473471
*/
474472
WOLFSSL_LOCAL void *Renesas_cmn_GetCbCtxBydevId(int devId)

wolfcrypt/src/port/Renesas/renesas_fspsm_rsa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ WOLFSSL_LOCAL void wc_fspsm_RsaKeyFree(RsaKey *key)
6767
/* Set Rsa key by pre-created wrapped user key
6868
*
6969
* key RsaKey object
70-
* size desired keylenth, in bits. supports 1024 or 2048 bits
70+
* size desired key length, in bits. supports 1024 or 2048 bits
7171
* ctx Callback context including pointer to hold generated key
7272
* return FSP_SUCCESS(0) on Success, otherwise negative value
7373
*/

wolfcrypt/src/port/Renesas/renesas_tsip_rsa.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
/* Make RSA key for TSIP and set it to callback ctx
4343
* Assumes to be called by Crypt Callback
4444
*
45-
* size desired keylenth, in bits. supports 1024 or 2048 bits
45+
* size desired key length, in bits. supports 1024 or 2048 bits
4646
* ctx Callback context including pointer to hold generated key
4747
* return TSIP_SUCCESS(0) on Success, otherwise negative value
4848
*/
49-
WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
49+
int wc_tsip_MakeRsaKey(int size, void* ctx)
5050
{
5151
e_tsip_err_t ret;
5252
TsipUserCtx *info = (TsipUserCtx*)ctx;
@@ -249,6 +249,9 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
249249
"1024 or 2048 bits.");
250250
return BAD_FUNC_ARG;
251251
}
252+
if (ret == 0) {
253+
info->pk.rsa.outLen = cipher.data_length;
254+
}
252255
}
253256
else if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT)
254257
{
@@ -270,6 +273,9 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
270273
"1024 or 2048 bits.");
271274
return BAD_FUNC_ARG;
272275
}
276+
if (ret == 0) {
277+
info->pk.rsa.outLen = plain.data_length;
278+
}
273279
}
274280
tsip_hw_unlock();
275281
}

wolfcrypt/src/port/Renesas/renesas_tsip_util.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,7 @@ int wc_tsip_tls_RootCertVerify(
36323632
return ret;
36333633
}
36343634
#endif /* WOLFSSL_RENESAS_TSIP_TLS */
3635+
36353636
#if !defined(NO_RSA)
36363637
/* Perform signing with the client's RSA private key on hash value of messages
36373638
* exchanged with server.
@@ -3646,7 +3647,7 @@ int wc_tsip_tls_RootCertVerify(
36463647
* 0 on success, CRYPTOCB_UNAVAILABLE on unsupported key type specified.
36473648
*
36483649
*/
3649-
WOLFSSL_LOCAL int tsip_SignRsaPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
3650+
int tsip_SignRsaPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
36503651
{
36513652
int ret = 0;
36523653
e_tsip_err_t err = TSIP_SUCCESS;
@@ -3724,18 +3725,18 @@ WOLFSSL_LOCAL int tsip_SignRsaPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
37243725
#endif
37253726

37263727
if (ret == 0) {
3727-
#ifdef WOLFSSL_RENESAS_TSIP_TLS
3728+
#ifdef WOLFSSL_RENESAS_TSIP_TLS
37283729
hashData.pdata = (uint8_t*)ssl->buffers.digest.buffer;
37293730
hashData.data_type = 1;
37303731
sigData.pdata = (uint8_t*)info->pk.rsa.in;
37313732
sigData.data_length = 0; /* signature size will be returned here */
3732-
#else
3733+
#else
37333734
hashData.pdata = (uint8_t*)info->pk.rsa.in;
37343735
hashData.data_length= info->pk.rsa.inLen;
37353736
hashData.data_type = tuc->keyflgs_crypt.bits.message_type;
37363737
sigData.pdata = (uint8_t*)info->pk.rsa.out;
37373738
sigData.data_length = 0;
3738-
#endif
3739+
#endif
37393740
if ((ret = tsip_hw_lock()) == 0) {
37403741
switch (tuc->wrappedKeyType) {
37413742
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
@@ -3752,7 +3753,6 @@ WOLFSSL_LOCAL int tsip_SignRsaPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
37523753
break;
37533754
#endif
37543755
case TSIP_KEY_TYPE_RSA2048:
3755-
37563756
err = R_TSIP_RsassaPkcs2048SignatureGenerate(
37573757
&hashData, &sigData,
37583758
#ifdef WOLFSSL_RENESAS_TSIP_TLS

wolfcrypt/src/rsa.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
277277
key->handle = NULL;
278278
#endif
279279

280-
281280
#if defined(WOLFSSL_RENESAS_FSPSM)
282281
key->ctx.wrapped_pri1024_key = NULL;
283282
key->ctx.wrapped_pub1024_key = NULL;
284283
key->ctx.wrapped_pri2048_key = NULL;
285284
key->ctx.wrapped_pub2048_key = NULL;
286285
key->ctx.keySz = 0;
287286
#endif
287+
288288
return ret;
289289
}
290290

@@ -3374,24 +3374,7 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33743374
pad_value, pad_type, hash, mgf, label,
33753375
labelSz, sz);
33763376
}
3377-
#elif defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) || \
3378-
(!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
3379-
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY))
3380-
/* SCE needs wrapped key which is passed via
3381-
* user ctx object of crypt-call back.
3382-
*/
3383-
#ifdef WOLF_CRYPTO_CB
3384-
if (key->devId != INVALID_DEVID) {
3385-
/* SCE supports 1024 and 2048 bits */
3386-
ret = wc_CryptoCb_Rsa(in, inLen, out,
3387-
&outLen, rsa_type, key, rng);
3388-
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3389-
return ret;
3390-
/* fall-through when unavailable */
3391-
ret = 0; /* reset error code and try using software */
3392-
}
3393-
#endif
3394-
#endif /* WOLFSSL_SE050 */
3377+
#endif /* RSA CRYPTO HW */
33953378

33963379
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
33973380
if (key->devId != INVALID_DEVID) {
@@ -3561,21 +3544,7 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35613544
}
35623545
return ret;
35633546
}
3564-
#elif defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) || \
3565-
(!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
3566-
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY))
3567-
#ifdef WOLF_CRYPTO_CB
3568-
if (key->devId != INVALID_DEVID) {
3569-
ret = wc_CryptoCb_Rsa(in, inLen, out,
3570-
&outLen, rsa_type, key, rng);
3571-
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3572-
return ret;
3573-
/* fall-through when unavailable */
3574-
ret = 0; /* reset error code and try using software */
3575-
}
3576-
#endif
3577-
3578-
#endif /* WOLFSSL_CRYPTOCELL */
3547+
#endif /* RSA CRYPTO HW */
35793548

35803549

35813550
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \

wolfcrypt/src/wc_pkcs11.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ static int Pkcs11Rsa(Pkcs11Session* session, wc_CryptoInfo* info)
21982198
case WC_PK_TYPE_RSA_OAEP:
21992199
mechanism = CKM_RSA_PKCS_OAEP;
22002200
break;
2201-
#endif /* NO_PKCS11_RSA_PKCS */
2201+
#endif /* !NO_PKCS11_RSA_PKCS */
22022202
case WC_PK_TYPE_RSA:
22032203
mechanism = CKM_RSA_X_509;
22042204
break;

wolfssl/wolfcrypt/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,11 @@
994994
#define TSIP_TLS_HMAC_KEY_INDEX_WORDSIZE 64
995995
#define TSIP_TLS_MASTERSECRET_SIZE 80 /* 20 words */
996996
#define TSIP_TLS_ENCPUBKEY_SZ_BY_CERTVRFY 560 /* in byte */
997+
998+
#ifdef WOLF_CRYPTO_CB
999+
/* make sure RSA padding callbacks are enabled */
1000+
#define WOLF_CRYPTO_CB_RSA_PAD
1001+
#endif
9971002
#endif /* WOLFSSL_RENESAS_TSIP */
9981003

9991004
#if !defined(WOLFSSL_NO_HASH_RAW) && defined(WOLFSSL_RENESAS_RX64_HASH)

0 commit comments

Comments
 (0)