Skip to content

Commit ae0d73d

Browse files
authored
Merge pull request wolfSSL#8122 from miyazakh/tsip_rsa_private_enc
Implement TSIP RSA Public Enc/Private Dec
2 parents ff68099 + 2831eb3 commit ae0d73d

11 files changed

Lines changed: 326 additions & 140 deletions

File tree

IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,17 @@
240240
#if defined(WOLFSSL_RENESAS_TSIP)
241241
/*-- TSIP TLS and/or CRYPTONLY Definition --------------------------------*/
242242
/* Enable TSIP TLS (default)
243-
* TSIP CRYPTONLY is also enabled.
243+
* TSIP CRYPT is also enabled.
244244
* Disable TSIP TLS
245+
* TSIP CRYPT is also disabled
245246
* TSIP CRYPTONLY is only enabled.
246247
*/
247248
#define WOLFSSL_RENESAS_TSIP_TLS
248249

250+
/* #define WOLFSSL_RENESAS_TSIP_CRYPTONLY */
251+
/* #define WOLFSSL_KEY_GEN */
252+
/* #define RSA_MIN_SIZE 1024 */
253+
249254
#if !defined(NO_RENESAS_TSIP_CRYPT)
250255
#define HAVE_PK_CALLBACKS
251256
#define WOLF_CRYPTO_CB
@@ -267,13 +272,13 @@
267272
* directly. Comment out the macro will generate random number by
268273
* wolfSSL Hash DRBG by using a seed which is generated by TSIP API.
269274
*-----------------------------------------------------------------------*/
270-
#define CUSTOM_RAND_GENERATE_BLOCK wc_tsip_GenerateRandBlock
275+
#define CUSTOM_RAND_GENERATE_BLOCK wc_tsip_GenerateRandBlock
271276
#else
272277
#define OPENSSL_EXTRA
273278
#define WOLFSSL_GENSEED_FORTEST /* Warning: define your own seed gen */
274-
#if !defined(min)
275-
#define min(data1, data2) _builtin_min(data1, data2)
276-
#endif
279+
#if !defined(min)
280+
#define min(data1, data2) _builtin_min(data1, data2)
281+
#endif
277282
#endif
278283

279284

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

Lines changed: 124 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
#endif
5757

5858
#ifndef NO_SHA
59-
int sha_test();
59+
int sha_test(void);
6060
#endif
6161

6262
#ifndef NO_SHA256
63-
int sha256_test();
63+
int sha256_test(void);
6464
#endif
6565

6666
#define SMALL_STACK_SIZE (1 * 1024)
@@ -711,49 +711,135 @@ static void tskSha256_Test(void *pvParam)
711711
#define TEST_STRING_SZ 25
712712
#define RSA_TEST_BYTES 256 /* up to 2048-bit key */
713713

714+
static int tsip_rsa_test(int prnt, int keySize)
715+
{
716+
int ret = 0;
717+
718+
RsaKey *key = NULL;
719+
WC_RNG rng;
720+
const char inStr [] = TEST_STRING;
721+
const word32 inLen = (word32)TEST_STRING_SZ;
722+
const word32 outSz = RSA_TEST_BYTES;
723+
word32 out_actual_len = 0;
724+
byte *in = NULL;
725+
byte *out= NULL;
726+
byte *outplain = NULL;
727+
int initRsa = 0;
728+
int devId = 7890; /* fixed devid for TSIP/SCE */
729+
730+
XMEMSET(&rng, 0, sizeof(rng));
731+
732+
key = (RsaKey *)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
733+
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
734+
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
735+
outplain = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
736+
737+
if (key == NULL || in == NULL || out == NULL || outplain == NULL) {
738+
ret = -1;
739+
goto out;
740+
}
741+
742+
XMEMSET(key, 0, sizeof(*key));
743+
XMEMCPY(in, inStr, inLen);
744+
XMEMSET(out, 0, outSz);
745+
XMEMSET(outplain, 0, outSz);
746+
747+
ret = wc_InitRsaKey_ex(key, NULL, devId);
748+
if (ret != 0) {
749+
goto out;
750+
}
751+
initRsa = 1;
752+
753+
if ((ret = wc_InitRng(&rng)) != 0)
754+
goto out;
755+
756+
if ((ret = wc_RsaSetRNG(key, &rng)) != 0)
757+
goto out;
758+
759+
/* Generate a new RSA key to use with TSIP/SCE */
760+
if ((ret = wc_MakeRsaKey(key, keySize, 65537, &rng)) != 0) {
761+
goto out;
762+
}
763+
764+
ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
765+
if (ret < 0) {
766+
goto out;
767+
}
768+
769+
ret = wc_RsaPrivateDecrypt(out, (word32)(keySize/8), outplain, outSz, key);
770+
if (ret < 0) {
771+
ret = -1;
772+
goto out;
773+
}
774+
775+
if (XMEMCMP(in, outplain, inLen) != 0) {
776+
ret = -2;
777+
goto out;
778+
}
779+
780+
ret = 0;
781+
out:
782+
783+
wc_FreeRng(&rng);
784+
if (key != NULL) {
785+
if (initRsa)
786+
wc_FreeRsaKey(key);
787+
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
788+
}
789+
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
790+
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
791+
XFREE(outplain, NULL, DYNAMIC_TYPE_TMP_BUFFER);
792+
793+
(void)prnt;
794+
return ret;
795+
}
796+
797+
714798
static int tsip_rsa_SignVerify_test(int prnt, int keySize)
715799
{
716800
int ret = 0;
717801

718-
RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
802+
RsaKey *key = NULL;
719803
WC_RNG rng;
720804
const char inStr [] = TEST_STRING;
721805
const char inStr2[] = TEST_STRING2;
722806
const word32 inLen = (word32)TEST_STRING_SZ;
723807
const word32 outSz = RSA_TEST_BYTES;
724-
725808
byte *in = NULL;
726809
byte *in2 = NULL;
727810
byte *out= NULL;
811+
int initRsa = 0;
812+
int devId = 7890; /* fixed devid for TSIP/SCE */
813+
814+
XMEMSET(&rng, 0, sizeof(rng));
728815

816+
key = (RsaKey *)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
729817
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
730818
in2 = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
731-
out= (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
732-
733-
(void) prnt;
819+
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
734820

735821
if (key == NULL || in == NULL || out == NULL) {
736822
ret = -1;
737823
goto out;
738824
}
739825

740-
XMEMSET(&rng, 0, sizeof(rng));
741-
XMEMSET(key, 0, sizeof *key);
826+
XMEMSET(key, 0, sizeof(*key));
742827
XMEMCPY(in, inStr, inLen);
743828
XMEMCPY(in2, inStr2, inLen);
744829

745-
ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/);
830+
ret = wc_InitRsaKey_ex(key, NULL, devId);
746831
if (ret != 0) {
747832
goto out;
748833
}
834+
initRsa = 1;
749835

750836
if ((ret = wc_InitRng(&rng)) != 0)
751837
goto out;
752838

753839
if ((ret = wc_RsaSetRNG(key, &rng)) != 0)
754840
goto out;
755841

756-
/* make rsa key by SCE */
842+
/* Generate a new RSA key to use with TSIP/SCE */
757843
if ((ret = wc_MakeRsaKey(key, keySize, 65537, &rng)) != 0) {
758844
goto out;
759845
}
@@ -776,22 +862,27 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
776862
goto out;
777863
}
778864
ret = 0;
865+
779866
out:
867+
868+
wc_FreeRng(&rng);
780869
if (key != NULL) {
781-
wc_FreeRsaKey(key);
870+
if (initRsa)
871+
wc_FreeRsaKey(key);
782872
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
783873
}
784874
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
785875
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
786876
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
787877

878+
(void)prnt;
788879
return ret;
789880
}
790881
#endif /* NO_RSA */
791882

792883

793884
#ifdef TSIP_MULTIUNIT_TEST
794-
int tsip_crypt_sha_multitest()
885+
int tsip_crypt_sha_multitest(void)
795886
{
796887
int ret = 0;
797888
int num = 0;
@@ -849,7 +940,7 @@ int tsip_crypt_sha_multitest()
849940
}
850941

851942

852-
int tsip_crypt_AesCbc_multitest()
943+
int tsip_crypt_AesCbc_multitest(void)
853944
{
854945
int ret = 0;
855946
int num = 0;
@@ -930,7 +1021,7 @@ int tsip_crypt_AesCbc_multitest()
9301021
}
9311022

9321023

933-
int tsip_crypt_AesGcm_multitest()
1024+
int tsip_crypt_AesGcm_multitest(void)
9341025
{
9351026
int ret = 0;
9361027
int num = 0;
@@ -1009,7 +1100,7 @@ int tsip_crypt_AesGcm_multitest()
10091100
return ret;
10101101
}
10111102

1012-
int tsip_crypt_Sha_AesCbcGcm_multitest()
1103+
int tsip_crypt_Sha_AesCbcGcm_multitest(void)
10131104
{
10141105
int ret = 0;
10151106
int num = 0;
@@ -1089,7 +1180,7 @@ int tsip_crypt_Sha_AesCbcGcm_multitest()
10891180
#endif
10901181

10911182

1092-
int tsip_crypt_test()
1183+
int tsip_crypt_test(void)
10931184
{
10941185
int ret = 0;
10951186
e_tsip_err_t tsip_error_code;
@@ -1155,6 +1246,22 @@ int tsip_crypt_test()
11551246
ret = 0;
11561247
}
11571248

1249+
#if RSA_MIN_SIZE <= 1024
1250+
if (ret == 0) {
1251+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
1252+
printf(" tsip_rsa_test(1024)");
1253+
ret = tsip_rsa_test(1, 1024);
1254+
RESULT_STR(ret)
1255+
}
1256+
#endif
1257+
if (ret == 0) {
1258+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
1259+
printf(" tsip_rsa_test(2048)");
1260+
ret = tsip_rsa_test(1, 2048);
1261+
RESULT_STR(ret)
1262+
}
1263+
1264+
11581265
if (ret == 0) {
11591266
printf(" tsip_rsa_SignVerify_test(1024)");
11601267

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: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,34 @@ static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
252252

253253
if (info->algo_type == WC_ALGO_TYPE_PK) {
254254
#if !defined(NO_RSA)
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)) {
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);
259258
}
259+
#endif
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+
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
270+
else {
271+
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
272+
}
273+
#endif
274+
}
275+
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
276+
else if (pad && pad->pad_value == RSA_BLOCK_TYPE_2) {
277+
/* encrypt/decrypt */
278+
ret = wc_tsip_RsaFunction(info, cbInfo);
279+
}
260280
#endif
261-
262-
/* RSA Signing
263-
* Can handle only RSA PkCS#1v1.5 padding scheme here.
264-
*/
265-
if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT) {
266-
ret = tsip_SignRsaPkcs(info, cbInfo);
267-
}
268-
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
269-
/* RSA Verify */
270-
if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
271-
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
272281
}
273-
#endif
274282
#endif /* !NO_RSA */
275-
276283
#if defined(HAVE_ECC)
277284
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
278285
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
@@ -461,7 +468,7 @@ int Renesas_cmn_usable(const struct WOLFSSL* ssl, byte session_key_generated)
461468
* Get Callback ctx by devId
462469
*
463470
* devId : devId to get its CTX
464-
* return asocciated CTX when the method is successfully called.
471+
* return associated CTX when the method is successfully called.
465472
* otherwise, NULL
466473
*/
467474
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
*/

0 commit comments

Comments
 (0)