Skip to content

Commit 32c1f8b

Browse files
committed
implement TSIP RSA Public Enc/Private Dec
1 parent 3f651a8 commit 32c1f8b

4 files changed

Lines changed: 232 additions & 46 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,16 @@
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
249+
250+
/* #define WOLFSSL_RENESAS_TSIP_CRYPTONLY */
251+
/* #define WOLFSSL_KEY_GEN */
252+
/* #define RSA_MIN_SIZE 1024 */
248253

249254
#if !defined(NO_RENESAS_TSIP_CRYPT)
250255
#define HAVE_PK_CALLBACKS

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,88 @@ 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 = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
719+
WC_RNG rng;
720+
const char inStr [] = TEST_STRING;
721+
const char inStr2[] = TEST_STRING2;
722+
const word32 inLen = (word32)TEST_STRING_SZ;
723+
const word32 outSz = RSA_TEST_BYTES;
724+
word32 out_actual_len = 0;
725+
byte *in = NULL;
726+
byte *in2 = NULL;
727+
byte *out= NULL;
728+
byte *out2 = NULL;
729+
730+
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
731+
in2 = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
732+
out= (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
733+
out2 = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
734+
735+
if (key == NULL || in == NULL || out == NULL ||
736+
in2 == NULL || out2 == NULL) {
737+
ret = -1;
738+
goto out;
739+
}
740+
741+
XMEMSET(&rng, 0, sizeof(rng));
742+
XMEMSET(key, 0, sizeof *key);
743+
XMEMCPY(in, inStr, inLen);
744+
XMEMCPY(in2, inStr2, inLen);
745+
XMEMSET(out, 0, outSz);
746+
XMEMSET(out2, 0, outSz);
747+
748+
ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/);
749+
if (ret != 0) {
750+
goto out;
751+
}
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+
/* Set Rsa Key created by TSIP in Advance */
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), out2, outSz, key);
770+
if (ret < 0) {
771+
ret = -1;
772+
goto out;
773+
}
774+
775+
if (XMEMCMP(in, out2, inLen) != 0) {
776+
ret = -2;
777+
goto out;
778+
}
779+
780+
ret = 0;
781+
out:
782+
if (key != NULL) {
783+
wc_FreeRsaKey(key);
784+
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
785+
}
786+
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
787+
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
788+
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
789+
XFREE(out2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
790+
791+
(void) prnt;
792+
return ret;
793+
}
794+
795+
714796
static int tsip_rsa_SignVerify_test(int prnt, int keySize)
715797
{
716798
int ret = 0;
@@ -1155,6 +1237,22 @@ int tsip_crypt_test()
11551237
ret = 0;
11561238
}
11571239

1240+
#if RSA_MIN_SIZE <= 1024
1241+
if (ret == 0) {
1242+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
1243+
printf(" tsip_rsa_test(1024)");
1244+
ret = tsip_rsa_test(1, 1024);
1245+
RESULT_STR(ret)
1246+
}
1247+
#endif
1248+
if (ret == 0) {
1249+
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
1250+
printf(" tsip_rsa_test(2048)");
1251+
ret = tsip_rsa_test(1, 2048);
1252+
RESULT_STR(ret)
1253+
}
1254+
1255+
11581256
if (ret == 0) {
11591257
printf(" tsip_rsa_SignVerify_test(1024)");
11601258

wolfcrypt/src/port/Renesas/renesas_common.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,27 +251,34 @@ 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)
254+
#if !defined(NO_RSA) && defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
255255
#if defined(WOLFSSL_KEY_GEN)
256256
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN &&
257257
(info->pk.rsakg.size == 1024 || info->pk.rsakg.size == 2048)) {
258258
ret = wc_tsip_MakeRsaKey(info->pk.rsakg.size, (void*)ctx);
259-
}
259+
} else
260260
#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) {
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+
*/
266273
ret = tsip_SignRsaPkcs(info, cbInfo);
267274
}
268-
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
275+
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
269276
/* RSA Verify */
270-
if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
277+
else if (info->pk.type == WC_PK_TYPE_RSA &&
278+
info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
271279
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
272280
}
273-
#endif
274-
#endif /* !NO_RSA */
281+
#endif
275282

276283
#if defined(HAVE_ECC)
277284
#if defined(WOLFSSL_RENESAS_TSIP_TLS)

wolfcrypt/src/port/Renesas/renesas_tsip_rsa.c

Lines changed: 110 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include <wolfssl/wolfcrypt/settings.h>
2323

2424
#if !defined(NO_RSA) && \
25-
(defined(WOLFSSL_RENESAS_TSIP_TLS) || \
26-
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY))
25+
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
2726

2827
#include <string.h>
2928
#include <stdio.h>
@@ -121,6 +120,7 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
121120

122121
info->keyflgs_crypt.bits.rsapri1024_key_set = 1;
123122
info->keyflgs_crypt.bits.rsapub1024_key_set = 1;
123+
info->wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
124124
}
125125
else if (size == 2048) {
126126
XFREE(info->rsa2048pri_keyIdx, NULL, DYNAMIC_TYPE_RSA_BUFFER);
@@ -158,6 +158,7 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
158158

159159
info->keyflgs_crypt.bits.rsapri2048_key_set = 1;
160160
info->keyflgs_crypt.bits.rsapub2048_key_set = 1;
161+
info->wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
161162
}
162163
}
163164

@@ -167,42 +168,14 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
167168

168169
return 0;
169170
}
170-
171-
172-
/* Perform Rsa verify by TSIP
173-
* Assumes to be called by Crypt Callback
171+
/* Generate TSIP key index if needed
174172
*
175-
* in Buffer to hold plaintext
176-
* inLen Length of plaintext in bytes
177-
* out Buffer to hold generated signature
178-
* outLen Length of signature in bytes
179-
* key rsa key object
180-
* ctx The callback context
181-
* return FSP_SUCCESS(0) on Success, otherwise negative value
173+
* tuc struct pointer of TsipUserCtx
174+
* return FSP_SUCCESS(0) on Success, otherwise CRYPTOCB_UNAVAILABLE
182175
*/
183-
184-
WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
176+
static int tsip_RsakeyImport(TsipUserCtx* tuc)
185177
{
186178
int ret = 0;
187-
e_tsip_err_t err = TSIP_SUCCESS;
188-
tsip_rsa_byte_data_t hashData, sigData;
189-
uint8_t tsip_hash_type;
190-
191-
/* sanity check */
192-
if (info == NULL || tuc == NULL){
193-
return BAD_FUNC_ARG;
194-
}
195-
196-
if (ret == 0) {
197-
if (tuc->sign_hash_type == md5_mac)
198-
tsip_hash_type = R_TSIP_RSA_HASH_MD5;
199-
else if (tuc->sign_hash_type == sha_mac)
200-
tsip_hash_type = R_TSIP_RSA_HASH_SHA1;
201-
else if (tuc->sign_hash_type == sha256_mac)
202-
tsip_hash_type = R_TSIP_RSA_HASH_SHA256;
203-
else
204-
ret = CRYPTOCB_UNAVAILABLE;
205-
}
206179

207180
switch (tuc->wrappedKeyType) {
208181
case TSIP_KEY_TYPE_RSA1024:
@@ -230,7 +203,110 @@ WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
230203
break;
231204
}
232205

206+
return ret;
207+
}
208+
209+
/* Perform rsa encryption/decryption by TSIP
210+
* Assumes to be called by Crypt Callback
211+
*
212+
* info struct pointer of wc_CryptoInfo including necessary info
213+
* tuc struct pointer of TsipUserCtx including TSIP key info
214+
* return FSP_SUCCESS(0) on Success, otherwise negative value
215+
*/
216+
WOLFSSL_LOCAL int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
217+
{
218+
int ret;
219+
int keySize;
220+
int type;
221+
tsip_rsa_byte_data_t plain, cipher;
222+
223+
224+
if (info == NULL || tuc == NULL) {
225+
return BAD_FUNC_ARG;
226+
}
227+
228+
if(tsip_RsakeyImport(tuc) == 0) {
229+
type = info->pk.rsa.type;
230+
keySize = (int)tuc->wrappedKeyType;
231+
232+
if ((ret = tsip_hw_lock()) == 0) {
233+
if (type == RSA_PUBLIC_ENCRYPT) {
234+
plain.pdata = (uint8_t*)info->pk.rsa.in;
235+
plain.data_length = info->pk.rsa.inLen;
236+
cipher.pdata = (uint8_t*)info->pk.rsa.out;
237+
cipher.data_length = info->pk.rsa.outLen;
238+
239+
if (keySize == TSIP_KEY_TYPE_RSA1024) {
240+
ret = R_TSIP_RsaesPkcs1024Encrypt(&plain, &cipher,
241+
tuc->rsa1024pub_keyIdx);
242+
}
243+
else if (keySize == TSIP_KEY_TYPE_RSA2048) {
244+
ret = R_TSIP_RsaesPkcs2048Encrypt(&plain, &cipher,
245+
tuc->rsa2048pub_keyIdx);
246+
}
247+
else {
248+
WOLFSSL_MSG("keySize is invalid, neither 128 or 256 bytes, "
249+
"1024 or 2048 bits.");
250+
return BAD_FUNC_ARG;
251+
}
252+
}
253+
else if (type == RSA_PRIVATE_DECRYPT) {
254+
plain.pdata = (uint8_t*)info->pk.rsa.out;
255+
plain.data_length = info->pk.rsa.outLen;
256+
cipher.pdata = (uint8_t*)info->pk.rsa.in;
257+
cipher.data_length = info->pk.rsa.inLen;
258+
259+
if (keySize == TSIP_KEY_TYPE_RSA1024) {
260+
ret = R_TSIP_RsaesPkcs1024Decrypt(&cipher, &plain,
261+
tuc->rsa1024pri_keyIdx);
262+
}
263+
else if (keySize == TSIP_KEY_TYPE_RSA2048) {
264+
ret = R_TSIP_RsaesPkcs2048Decrypt(&cipher, &plain,
265+
tuc->rsa2048pri_keyIdx);
266+
}
267+
else {
268+
WOLFSSL_MSG("keySize is invalid, neither 128 or 256 bytes, "
269+
"1024 or 2048 bits.");
270+
return BAD_FUNC_ARG;
271+
}
272+
}
273+
tsip_hw_unlock();
274+
}
275+
}
276+
return ret;
277+
}
278+
/* Perform Rsa verify by TSIP
279+
* Assumes to be called by Crypt Callback
280+
*
281+
* info struct pointer of wc_CryptoInfo including necessary info
282+
* tuc struct pointer of TsipUserCtx including TSIP key info
283+
* return FSP_SUCCESS(0) on Success, otherwise negative value
284+
*/
285+
286+
WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
287+
{
288+
int ret = 0;
289+
e_tsip_err_t err = TSIP_SUCCESS;
290+
tsip_rsa_byte_data_t hashData, sigData;
291+
uint8_t tsip_hash_type;
292+
293+
/* sanity check */
294+
if (info == NULL || tuc == NULL){
295+
return BAD_FUNC_ARG;
296+
}
297+
233298
if (ret == 0) {
299+
if (tuc->sign_hash_type == md5_mac)
300+
tsip_hash_type = R_TSIP_RSA_HASH_MD5;
301+
else if (tuc->sign_hash_type == sha_mac)
302+
tsip_hash_type = R_TSIP_RSA_HASH_SHA1;
303+
else if (tuc->sign_hash_type == sha256_mac)
304+
tsip_hash_type = R_TSIP_RSA_HASH_SHA256;
305+
else
306+
ret = CRYPTOCB_UNAVAILABLE;
307+
}
308+
309+
if (tsip_RsakeyImport(tuc) == 0) {
234310
hashData.pdata = (uint8_t*)info->pk.rsa.in;
235311
hashData.data_length = info->pk.rsa.inLen;
236312
hashData.data_type =

0 commit comments

Comments
 (0)