Skip to content

Commit 04932dd

Browse files
Merge pull request wolfSSL#8206 from dgarske/rx_tsip
Fixes for RSA TSIP RSA Sign/Verify
2 parents 2710b57 + c5e4328 commit 04932dd

4 files changed

Lines changed: 83 additions & 51 deletions

File tree

IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,10 @@ static int tsip_aesgcm256_test(int prnt, tsip_aes_key_index_t* aes256_key)
408408
printf(" tsip_aes256_gcm_test() ");
409409
}
410410

411-
ForceZero(resultT, sizeof(resultT));
412-
ForceZero(resultC, sizeof(resultC));
413-
ForceZero(resultP, sizeof(resultP));
414-
ForceZero(&userContext, sizeof(TsipUserCtx));
411+
XMEMSET(resultT, 0, sizeof(resultT));
412+
XMEMSET(resultC, 0, sizeof(resultC));
413+
XMEMSET(resultP, 0, sizeof(resultP));
414+
XMEMSET(&userContext, 0, sizeof(TsipUserCtx));
415415

416416
if (wc_AesInit(enc, NULL, INVALID_DEVID) != 0) {
417417
ret = -1;
@@ -434,10 +434,11 @@ static int tsip_aesgcm256_test(int prnt, tsip_aes_key_index_t* aes256_key)
434434
}
435435

436436
/* AES-GCM encrypt and decrypt both use AES encrypt internally */
437-
result = wc_tsip_AesGcmEncrypt(enc, resultC, p, sizeof(p),
438-
(byte*)iv1, sizeof(iv1), resultT, sizeof(resultT),
439-
a, sizeof(a), &userContext);
440-
437+
result = wc_tsip_AesGcmEncrypt(enc,
438+
resultC, p, sizeof(p),
439+
(byte*)iv1, sizeof(iv1), resultT, sizeof(resultT),
440+
a, sizeof(a), &userContext
441+
);
441442
if (result != 0) {
442443
ret = -4;
443444
goto out;
@@ -451,9 +452,11 @@ static int tsip_aesgcm256_test(int prnt, tsip_aes_key_index_t* aes256_key)
451452
dec->ctx.keySize = enc->keylen;
452453
}
453454

454-
result = wc_tsip_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
455-
iv1, sizeof(iv1), resultT, sizeof(resultT),
456-
a, sizeof(a), &userContext);
455+
result = wc_tsip_AesGcmDecrypt(dec,
456+
resultP, resultC, sizeof(c1),
457+
iv1, sizeof(iv1), resultT, sizeof(resultT),
458+
a, sizeof(a), &userContext
459+
);
457460
if (result != 0){
458461
ret = -8;
459462
goto out;
@@ -469,18 +472,21 @@ static int tsip_aesgcm256_test(int prnt, tsip_aes_key_index_t* aes256_key)
469472

470473
wc_AesGcmSetKey(enc, k1, sizeof(k1));
471474
/* AES-GCM encrypt and decrypt both use AES encrypt internally */
472-
result = wc_tsip_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
473-
resultT + 1, sizeof(resultT) - 1,
474-
a, sizeof(a), &userContext);
475+
result = wc_tsip_AesGcmEncrypt(enc,
476+
resultC, p, sizeof(p), iv1, sizeof(iv1),
477+
resultT + 1, sizeof(resultT) - 1,
478+
a, sizeof(a), &userContext
479+
);
475480
if (result != 0) {
476481
ret = -10;
477482
goto out;
478483
}
479484

480-
result = wc_tsip_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
481-
iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1,
482-
a, sizeof(a), &userContext);
483-
485+
result = wc_tsip_AesGcmDecrypt(enc,
486+
resultP, resultC, sizeof(p),
487+
iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1,
488+
a, sizeof(a), &userContext
489+
);
484490
if (result != 0) {
485491
ret = -11;
486492
goto out;
@@ -523,7 +529,7 @@ static void tskAes256_Gcm_Test(void *pvParam)
523529
#endif /* FREERTOS */
524530
#endif
525531

526-
#if defined(WOLFSSL_AES_128)
532+
#if defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM)
527533

528534
static int tsip_aesgcm128_test(int prnt, tsip_aes_key_index_t* aes128_key)
529535
{
@@ -568,9 +574,9 @@ static int tsip_aesgcm128_test(int prnt, tsip_aes_key_index_t* aes128_key)
568574
0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
569575
};
570576

571-
byte resultT[16];
572-
byte resultP[60 + AES_BLOCK_SIZE];
573-
byte resultC[60 + AES_BLOCK_SIZE];
577+
byte resultT[sizeof(t3)];
578+
byte resultP[sizeof(p3) + AES_BLOCK_SIZE];
579+
byte resultC[sizeof(p3) + AES_BLOCK_SIZE];
574580
int result = 0;
575581
int ret;
576582

@@ -581,10 +587,10 @@ static int tsip_aesgcm128_test(int prnt, tsip_aes_key_index_t* aes128_key)
581587
printf(" tsip_aes128_gcm_test() ");
582588
}
583589

584-
ForceZero(resultT, sizeof(resultT));
585-
ForceZero(resultC, sizeof(resultC));
586-
ForceZero(resultP, sizeof(resultP));
587-
ForceZero(&userContext, sizeof(TsipUserCtx));
590+
XMEMSET(resultT, 0, sizeof(resultT));
591+
XMEMSET(resultC, 0, sizeof(resultC));
592+
XMEMSET(resultP, 0, sizeof(resultP));
593+
XMEMSET(&userContext, 0, sizeof(TsipUserCtx));
588594

589595
if (wc_AesInit(enc, NULL, INVALID_DEVID) != 0) {
590596
ret = -1;
@@ -607,21 +613,27 @@ static int tsip_aesgcm128_test(int prnt, tsip_aes_key_index_t* aes128_key)
607613
enc->ctx.keySize = enc->keylen;
608614
}
609615
/* AES-GCM encrypt and decrypt both use AES encrypt internally */
610-
result = wc_tsip_AesGcmEncrypt(enc, resultC, p3, sizeof(p3),
611-
iv3, sizeof(iv3),
612-
resultT, sizeof(t3),
613-
a3, sizeof(a3), &userContext);
616+
result = wc_tsip_AesGcmEncrypt(enc,
617+
resultC, p3, sizeof(p3),
618+
iv3, sizeof(iv3),
619+
resultT, sizeof(t3),
620+
a3, sizeof(a3), &userContext
621+
);
614622
if (result != 0) {
615623
ret = -4;
616624
goto out;
617625
}
618-
result = wc_tsip_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
619-
iv3, sizeof(iv3), resultT, sizeof(resultT),
620-
a3, sizeof(a3), &userContext);
626+
627+
result = wc_tsip_AesGcmDecrypt(enc,
628+
resultP, resultC, sizeof(c3),
629+
iv3, sizeof(iv3), resultT, sizeof(resultT),
630+
a3, sizeof(a3), &userContext
631+
);
621632
if (result != 0) {
622633
ret = -5;
623634
goto out;
624635
}
636+
625637
if (XMEMCMP(p3, resultP, sizeof(p3))) {
626638
ret = -6;
627639
goto out;
@@ -805,6 +817,7 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
805817
const char inStr2[] = TEST_STRING2;
806818
const word32 inLen = (word32)TEST_STRING_SZ;
807819
const word32 outSz = RSA_TEST_BYTES;
820+
word32 signSz = 0;
808821
byte *in = NULL;
809822
byte *in2 = NULL;
810823
byte *out= NULL;
@@ -848,15 +861,16 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
848861
if (ret < 0) {
849862
goto out;
850863
}
864+
signSz = ret;
851865

852866
/* this should fail */
853-
ret = wc_RsaSSL_Verify(in2, inLen, out, keySize/8, key);
867+
ret = wc_RsaSSL_Verify(out, signSz, in2, inLen, key);
854868
if (ret != SIG_VERIFY_E) {
855869
ret = -1;
856870
goto out;
857871
}
858872
/* this should succeed */
859-
ret = wc_RsaSSL_Verify(in, inLen, out, keySize/8, key);
873+
ret = wc_RsaSSL_Verify(out, signSz, in, inLen, key);
860874
if (ret < 0) {
861875
ret = -1;
862876
goto out;
@@ -1223,6 +1237,7 @@ int tsip_crypt_test(void)
12231237

12241238
}
12251239

1240+
#ifdef HAVE_AESGCM
12261241
if (ret == 0) {
12271242

12281243
ret = tsip_aesgcm128_test(1, &g_user_aes128_key_index1);
@@ -1234,8 +1249,10 @@ int tsip_crypt_test(void)
12341249
ret = tsip_aesgcm256_test(1, &g_user_aes256_key_index1);
12351250

12361251
}
1237-
#if defined(WOLFSSL_KEY_GEN) && \
1238-
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
1252+
#endif
1253+
1254+
#if defined(WOLFSSL_KEY_GEN) && \
1255+
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
12391256

12401257
if (ret == 0) {
12411258
Clr_CallbackCtx(&userContext);
@@ -1248,20 +1265,21 @@ int tsip_crypt_test(void)
12481265

12491266
#if RSA_MIN_SIZE <= 1024
12501267
if (ret == 0) {
1251-
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
1268+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
12521269
printf(" tsip_rsa_test(1024)");
12531270
ret = tsip_rsa_test(1, 1024);
12541271
RESULT_STR(ret)
12551272
}
12561273
#endif
12571274
if (ret == 0) {
1258-
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
1275+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
12591276
printf(" tsip_rsa_test(2048)");
12601277
ret = tsip_rsa_test(1, 2048);
12611278
RESULT_STR(ret)
12621279
}
12631280

12641281

1282+
#if RSA_MIN_SIZE <= 1024
12651283
if (ret == 0) {
12661284
printf(" tsip_rsa_SignVerify_test(1024)");
12671285

@@ -1274,6 +1292,7 @@ int tsip_crypt_test(void)
12741292
}
12751293

12761294
Clr_CallbackCtx(&userContext);
1295+
#endif
12771296

12781297
if (ret == 0) {
12791298
printf(" tsip_rsa_SignVerify_test(2048)");
@@ -1287,12 +1306,11 @@ int tsip_crypt_test(void)
12871306
}
12881307

12891308
Clr_CallbackCtx(&userContext);
1290-
#endif
1309+
#endif /* WOLFSSL_KEY_GEN && WOLFSSL_RENESAS_TSIP_CRYPTONLY */
12911310
}
1292-
else
1311+
else {
12931312
ret = -1;
1294-
1295-
1313+
}
12961314
return ret;
12971315
}
12981316

wolfcrypt/src/port/Renesas/renesas_common.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
279279
}
280280
#endif
281281
}
282+
if (info->pk.type == WC_PK_TYPE_RSA_GET_SIZE) {
283+
if (cbInfo->wrappedKeyType == TSIP_KEY_TYPE_RSA2048) {
284+
*info->pk.rsa_get_size.keySize = 256;
285+
ret = 0;
286+
}
287+
else if (cbInfo->wrappedKeyType == TSIP_KEY_TYPE_RSA1024) {
288+
*info->pk.rsa_get_size.keySize = 128;
289+
ret = 0;
290+
}
291+
}
282292
#endif /* !NO_RSA */
283293
#if defined(HAVE_ECC)
284294
#if defined(WOLFSSL_RENESAS_TSIP_TLS)

wolfcrypt/src/port/Renesas/renesas_tsip_rsa.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
234234
plain.pdata = (uint8_t*)info->pk.rsa.in;
235235
plain.data_length = info->pk.rsa.inLen;
236236
cipher.pdata = (uint8_t*)info->pk.rsa.out;
237-
cipher.data_length = info->pk.rsa.outLen;
237+
cipher.data_length = *(info->pk.rsa.outLen);
238238

239239
if (keySize == TSIP_KEY_TYPE_RSA1024) {
240240
ret = R_TSIP_RsaesPkcs1024Encrypt(&plain, &cipher,
@@ -250,13 +250,13 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
250250
return BAD_FUNC_ARG;
251251
}
252252
if (ret == 0) {
253-
info->pk.rsa.outLen = cipher.data_length;
253+
*(info->pk.rsa.outLen) = cipher.data_length;
254254
}
255255
}
256256
else if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT)
257257
{
258258
plain.pdata = (uint8_t*)info->pk.rsa.out;
259-
plain.data_length = info->pk.rsa.outLen;
259+
plain.data_length = *(info->pk.rsa.outLen);
260260
cipher.pdata = (uint8_t*)info->pk.rsa.in;
261261
cipher.data_length = info->pk.rsa.inLen;
262262

@@ -274,7 +274,7 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
274274
return BAD_FUNC_ARG;
275275
}
276276
if (ret == 0) {
277-
info->pk.rsa.outLen = plain.data_length;
277+
*(info->pk.rsa.outLen) = plain.data_length;
278278
}
279279
}
280280
tsip_hw_unlock();
@@ -314,13 +314,13 @@ int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
314314
}
315315

316316
if (tsip_RsakeyImport(tuc) == 0) {
317-
hashData.pdata = (uint8_t*)info->pk.rsa.in;
318-
hashData.data_length = info->pk.rsa.inLen;
317+
hashData.pdata = (uint8_t*)info->pk.rsa.out;
318+
hashData.data_length = *(info->pk.rsa.outLen);
319319
hashData.data_type =
320320
tuc->keyflgs_crypt.bits.message_type;/* message 0, hash 1 */
321321

322-
sigData.pdata = (uint8_t*)info->pk.rsa.out;
323-
sigData.data_length = info->pk.rsa.outLen;
322+
sigData.pdata = (uint8_t*)info->pk.rsa.in;
323+
sigData.data_length = info->pk.rsa.inLen;
324324

325325
if ((ret = tsip_hw_lock()) == 0) {
326326
switch (tuc->wrappedKeyType) {

wolfssl/wolfcrypt/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ typedef struct w64wrapper {
14971497
#if !defined(__MINGW32__)
14981498
#define WOLFSSL_THREAD_NO_JOIN __cdecl
14991499
#endif
1500+
#elif defined(THREADX)
1501+
typedef unsigned int THREAD_RETURN;
1502+
typedef TX_THREAD THREAD_TYPE;
1503+
#define WOLFSSL_THREAD
15001504
#else
15011505
typedef unsigned int THREAD_RETURN;
15021506
typedef size_t THREAD_TYPE;

0 commit comments

Comments
 (0)