Skip to content

Commit 6b02d78

Browse files
committed
Add public decrypt and private encrypt. Cleanups.
1 parent b409967 commit 6b02d78

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static int tsip_rsa_test(int prnt, int keySize)
715715
{
716716
int ret = 0;
717717

718-
RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
718+
RsaKey *key = NULL;
719719
WC_RNG rng;
720720
const char inStr [] = TEST_STRING;
721721
const char inStr2[] = TEST_STRING2;
@@ -726,10 +726,15 @@ static int tsip_rsa_test(int prnt, int keySize)
726726
byte *in2 = NULL;
727727
byte *out= NULL;
728728
byte *out2 = NULL;
729+
int initRsa = 0;
730+
int devId = 7890; /* fixed devid for TSIP/SCE */
729731

732+
XMEMSET(&rng, 0, sizeof(rng));
733+
734+
key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
730735
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
731736
in2 = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
732-
out= (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
737+
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
733738
out2 = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
734739

735740
if (key == NULL || in == NULL || out == NULL ||
@@ -738,17 +743,17 @@ static int tsip_rsa_test(int prnt, int keySize)
738743
goto out;
739744
}
740745

741-
XMEMSET(&rng, 0, sizeof(rng));
742746
XMEMSET(key, 0, sizeof *key);
743747
XMEMCPY(in, inStr, inLen);
744748
XMEMCPY(in2, inStr2, inLen);
745749
XMEMSET(out, 0, outSz);
746750
XMEMSET(out2, 0, outSz);
747751

748-
ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/);
752+
ret = wc_InitRsaKey_ex(key, NULL, devId);
749753
if (ret != 0) {
750754
goto out;
751755
}
756+
initRsa = 1;
752757

753758
if ((ret = wc_InitRng(&rng)) != 0)
754759
goto out;
@@ -779,8 +784,11 @@ static int tsip_rsa_test(int prnt, int keySize)
779784

780785
ret = 0;
781786
out:
787+
788+
wc_FreeRng(&rng);
782789
if (key != NULL) {
783-
wc_FreeRsaKey(key);
790+
if (initRsa)
791+
wc_FreeRsaKey(key);
784792
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
785793
}
786794
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -797,37 +805,41 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
797805
{
798806
int ret = 0;
799807

800-
RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
808+
RsaKey *key = NULL;
801809
WC_RNG rng;
802810
const char inStr [] = TEST_STRING;
803811
const char inStr2[] = TEST_STRING2;
804812
const word32 inLen = (word32)TEST_STRING_SZ;
805813
const word32 outSz = RSA_TEST_BYTES;
806-
807814
byte *in = NULL;
808815
byte *in2 = NULL;
809816
byte *out= NULL;
817+
int initRsa = 0;
818+
int devId = 7890; /* fixed devid for TSIP/SCE */
810819

820+
XMEMSET(&rng, 0, sizeof(rng));
821+
822+
key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
811823
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
812824
in2 = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
813-
out= (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
825+
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
814826

815-
(void) prnt;
827+
(void)prnt;
816828

817829
if (key == NULL || in == NULL || out == NULL) {
818830
ret = -1;
819831
goto out;
820832
}
821833

822-
XMEMSET(&rng, 0, sizeof(rng));
823834
XMEMSET(key, 0, sizeof *key);
824835
XMEMCPY(in, inStr, inLen);
825836
XMEMCPY(in2, inStr2, inLen);
826837

827-
ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/);
838+
ret = wc_InitRsaKey_ex(key, NULL, devId);
828839
if (ret != 0) {
829840
goto out;
830841
}
842+
initRsa = 1;
831843

832844
if ((ret = wc_InitRng(&rng)) != 0)
833845
goto out;
@@ -858,9 +870,13 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
858870
goto out;
859871
}
860872
ret = 0;
873+
861874
out:
875+
876+
wc_FreeRng(&rng);
862877
if (key != NULL) {
863-
wc_FreeRsaKey(key);
878+
if (initRsa)
879+
wc_FreeRsaKey(key);
864880
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
865881
}
866882
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);

wolfcrypt/src/port/Renesas/renesas_tsip_rsa.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static int tsip_RsakeyImport(TsipUserCtx* tuc)
213213
* tuc struct pointer of TsipUserCtx including TSIP key info
214214
* return FSP_SUCCESS(0) on Success, otherwise negative value
215215
*/
216-
WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
216+
int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
217217
{
218218
int ret;
219219
int keySize;
@@ -225,12 +225,12 @@ WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
225225
return BAD_FUNC_ARG;
226226
}
227227

228-
if(tsip_RsakeyImport(tuc) == 0) {
228+
if (tsip_RsakeyImport(tuc) == 0) {
229229
type = info->pk.rsa.type;
230230
keySize = (int)tuc->wrappedKeyType;
231231

232232
if ((ret = tsip_hw_lock()) == 0) {
233-
if (type == RSA_PUBLIC_ENCRYPT) {
233+
if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
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;
@@ -250,7 +250,8 @@ WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
250250
return BAD_FUNC_ARG;
251251
}
252252
}
253-
else if (type == RSA_PRIVATE_DECRYPT) {
253+
else if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT)
254+
{
254255
plain.pdata = (uint8_t*)info->pk.rsa.out;
255256
plain.data_length = info->pk.rsa.outLen;
256257
cipher.pdata = (uint8_t*)info->pk.rsa.in;
@@ -283,7 +284,7 @@ WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
283284
* return FSP_SUCCESS(0) on Success, otherwise negative value
284285
*/
285286

286-
WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
287+
int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
287288
{
288289
int ret = 0;
289290
e_tsip_err_t err = TSIP_SUCCESS;

wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,12 @@ WOLFSSL_LOCAL int tsip_SignRsaPkcs(struct wc_CryptoInfo* info,
437437
WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(struct wc_CryptoInfo* info,
438438
TsipUserCtx* tuc);
439439

440+
WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc);
441+
440442
WOLFSSL_LOCAL int tsip_SignEcdsa(struct wc_CryptoInfo* info, TsipUserCtx* tuc);
441443

442-
WOLFSSL_LOCAL int tsip_VerifyEcdsa(struct wc_CryptoInfo* info, TsipUserCtx* tuc);
444+
WOLFSSL_LOCAL int tsip_VerifyEcdsa(struct wc_CryptoInfo* info,
445+
TsipUserCtx* tuc);
443446

444447
WOLFSSL_LOCAL int tsip_TlsCleanup(struct WOLFSSL* ssl);
445448

0 commit comments

Comments
 (0)