Skip to content

Commit

Permalink
Merge pull request #8122 from miyazakh/tsip_rsa_private_enc
Browse files Browse the repository at this point in the history
Implement TSIP RSA Public Enc/Private Dec
  • Loading branch information
douzzer authored Nov 16, 2024
2 parents ff68099 + 2831eb3 commit ae0d73d
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,17 @@
#if defined(WOLFSSL_RENESAS_TSIP)
/*-- TSIP TLS and/or CRYPTONLY Definition --------------------------------*/
/* Enable TSIP TLS (default)
* TSIP CRYPTONLY is also enabled.
* TSIP CRYPT is also enabled.
* Disable TSIP TLS
* TSIP CRYPT is also disabled
* TSIP CRYPTONLY is only enabled.
*/
#define WOLFSSL_RENESAS_TSIP_TLS
/* #define WOLFSSL_RENESAS_TSIP_CRYPTONLY */
/* #define WOLFSSL_KEY_GEN */
/* #define RSA_MIN_SIZE 1024 */

#if !defined(NO_RENESAS_TSIP_CRYPT)
#define HAVE_PK_CALLBACKS
#define WOLF_CRYPTO_CB
Expand All @@ -267,13 +272,13 @@
* directly. Comment out the macro will generate random number by
* wolfSSL Hash DRBG by using a seed which is generated by TSIP API.
*-----------------------------------------------------------------------*/
#define CUSTOM_RAND_GENERATE_BLOCK wc_tsip_GenerateRandBlock
#define CUSTOM_RAND_GENERATE_BLOCK wc_tsip_GenerateRandBlock
#else
#define OPENSSL_EXTRA
#define WOLFSSL_GENSEED_FORTEST /* Warning: define your own seed gen */
#if !defined(min)
#define min(data1, data2) _builtin_min(data1, data2)
#endif
#if !defined(min)
#define min(data1, data2) _builtin_min(data1, data2)
#endif
#endif


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
#endif

#ifndef NO_SHA
int sha_test();
int sha_test(void);
#endif

#ifndef NO_SHA256
int sha256_test();
int sha256_test(void);
#endif

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

static int tsip_rsa_test(int prnt, int keySize)
{
int ret = 0;

RsaKey *key = NULL;
WC_RNG rng;
const char inStr [] = TEST_STRING;
const word32 inLen = (word32)TEST_STRING_SZ;
const word32 outSz = RSA_TEST_BYTES;
word32 out_actual_len = 0;
byte *in = NULL;
byte *out= NULL;
byte *outplain = NULL;
int initRsa = 0;
int devId = 7890; /* fixed devid for TSIP/SCE */

XMEMSET(&rng, 0, sizeof(rng));

key = (RsaKey *)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
outplain = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);

if (key == NULL || in == NULL || out == NULL || outplain == NULL) {
ret = -1;
goto out;
}

XMEMSET(key, 0, sizeof(*key));
XMEMCPY(in, inStr, inLen);
XMEMSET(out, 0, outSz);
XMEMSET(outplain, 0, outSz);

ret = wc_InitRsaKey_ex(key, NULL, devId);
if (ret != 0) {
goto out;
}
initRsa = 1;

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

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

/* Generate a new RSA key to use with TSIP/SCE */
if ((ret = wc_MakeRsaKey(key, keySize, 65537, &rng)) != 0) {
goto out;
}

ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
if (ret < 0) {
goto out;
}

ret = wc_RsaPrivateDecrypt(out, (word32)(keySize/8), outplain, outSz, key);
if (ret < 0) {
ret = -1;
goto out;
}

if (XMEMCMP(in, outplain, inLen) != 0) {
ret = -2;
goto out;
}

ret = 0;
out:

wc_FreeRng(&rng);
if (key != NULL) {
if (initRsa)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(outplain, NULL, DYNAMIC_TYPE_TMP_BUFFER);

(void)prnt;
return ret;
}


static int tsip_rsa_SignVerify_test(int prnt, int keySize)
{
int ret = 0;

RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
RsaKey *key = NULL;
WC_RNG rng;
const char inStr [] = TEST_STRING;
const char inStr2[] = TEST_STRING2;
const word32 inLen = (word32)TEST_STRING_SZ;
const word32 outSz = RSA_TEST_BYTES;

byte *in = NULL;
byte *in2 = NULL;
byte *out= NULL;
int initRsa = 0;
int devId = 7890; /* fixed devid for TSIP/SCE */

XMEMSET(&rng, 0, sizeof(rng));

key = (RsaKey *)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
in = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
in2 = (byte*)XMALLOC(inLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
out= (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);

(void) prnt;
out = (byte*)XMALLOC(outSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);

if (key == NULL || in == NULL || out == NULL) {
ret = -1;
goto out;
}

XMEMSET(&rng, 0, sizeof(rng));
XMEMSET(key, 0, sizeof *key);
XMEMSET(key, 0, sizeof(*key));
XMEMCPY(in, inStr, inLen);
XMEMCPY(in2, inStr2, inLen);

ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/);
ret = wc_InitRsaKey_ex(key, NULL, devId);
if (ret != 0) {
goto out;
}
initRsa = 1;

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

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

/* make rsa key by SCE */
/* Generate a new RSA key to use with TSIP/SCE */
if ((ret = wc_MakeRsaKey(key, keySize, 65537, &rng)) != 0) {
goto out;
}
Expand All @@ -776,22 +862,27 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
goto out;
}
ret = 0;

out:

wc_FreeRng(&rng);
if (key != NULL) {
wc_FreeRsaKey(key);
if (initRsa)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);

(void)prnt;
return ret;
}
#endif /* NO_RSA */


#ifdef TSIP_MULTIUNIT_TEST
int tsip_crypt_sha_multitest()
int tsip_crypt_sha_multitest(void)
{
int ret = 0;
int num = 0;
Expand Down Expand Up @@ -849,7 +940,7 @@ int tsip_crypt_sha_multitest()
}


int tsip_crypt_AesCbc_multitest()
int tsip_crypt_AesCbc_multitest(void)
{
int ret = 0;
int num = 0;
Expand Down Expand Up @@ -930,7 +1021,7 @@ int tsip_crypt_AesCbc_multitest()
}


int tsip_crypt_AesGcm_multitest()
int tsip_crypt_AesGcm_multitest(void)
{
int ret = 0;
int num = 0;
Expand Down Expand Up @@ -1009,7 +1100,7 @@ int tsip_crypt_AesGcm_multitest()
return ret;
}

int tsip_crypt_Sha_AesCbcGcm_multitest()
int tsip_crypt_Sha_AesCbcGcm_multitest(void)
{
int ret = 0;
int num = 0;
Expand Down Expand Up @@ -1089,7 +1180,7 @@ int tsip_crypt_Sha_AesCbcGcm_multitest()
#endif


int tsip_crypt_test()
int tsip_crypt_test(void)
{
int ret = 0;
e_tsip_err_t tsip_error_code;
Expand Down Expand Up @@ -1155,6 +1246,22 @@ int tsip_crypt_test()
ret = 0;
}

#if RSA_MIN_SIZE <= 1024
if (ret == 0) {
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA1024;
printf(" tsip_rsa_test(1024)");
ret = tsip_rsa_test(1, 1024);
RESULT_STR(ret)
}
#endif
if (ret == 0) {
userContext.wrappedKeyType = TSIP_KEY_TYPE_RSA2048;
printf(" tsip_rsa_test(2048)");
ret = tsip_rsa_test(1, 2048);
RESULT_STR(ret)
}


if (ret == 0) {
printf(" tsip_rsa_SignVerify_test(1024)");

Expand Down
12 changes: 5 additions & 7 deletions wolfcrypt/src/cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,

#ifdef WOLF_CRYPTO_CB_RSA_PAD
int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
word32* outLen, int type, RsaKey* key, WC_RNG* rng,
RsaPadding *padding)
word32* outLen, int type, RsaKey* key, WC_RNG* rng,
RsaPadding *padding)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
CryptoCb* dev;
Expand All @@ -458,9 +458,8 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);

if (padding) {
switch(padding->pad_type) {
#ifndef NO_PKCS11_RSA_PKCS
if (padding != NULL) {
switch (padding->pad_type) {
case WC_RSA_PKCSV15_PAD:
pk_type = WC_PK_TYPE_RSA_PKCS;
break;
Expand All @@ -470,7 +469,6 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
case WC_RSA_OAEP_PAD:
pk_type = WC_PK_TYPE_RSA_OAEP;
break;
#endif /* NO_PKCS11_RSA_PKCS */
default:
pk_type = WC_PK_TYPE_RSA;
}
Expand All @@ -497,7 +495,7 @@ int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,

return wc_CryptoCb_TranslateErrorCode(ret);
}
#endif
#endif /* WOLF_CRYPTO_CB_RSA_PAD */

#ifdef WOLFSSL_KEY_GEN
int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
Expand Down
41 changes: 24 additions & 17 deletions wolfcrypt/src/port/Renesas/renesas_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,34 @@ static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)

if (info->algo_type == WC_ALGO_TYPE_PK) {
#if !defined(NO_RSA)
#if defined(WOLFSSL_KEY_GEN)
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN &&
(info->pk.rsakg.size == 1024 || info->pk.rsakg.size == 2048)) {
#if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
ret = wc_tsip_MakeRsaKey(info->pk.rsakg.size, (void*)ctx);
}
#endif
/* tsip only supports PKCSV15 padding scheme */
if (info->pk.type == WC_PK_TYPE_RSA_PKCS) {
RsaPadding* pad = info->pk.rsa.padding;
if (pad && pad->pad_value == RSA_BLOCK_TYPE_1) {
/* sign / verify */
if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT ||
info->pk.rsa.type == RSA_PRIVATE_DECRYPT) {
ret = tsip_SignRsaPkcs(info, cbInfo);
}
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
else {
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
}
#endif
}
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
else if (pad && pad->pad_value == RSA_BLOCK_TYPE_2) {
/* encrypt/decrypt */
ret = wc_tsip_RsaFunction(info, cbInfo);
}
#endif

/* RSA Signing
* Can handle only RSA PkCS#1v1.5 padding scheme here.
*/
if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT) {
ret = tsip_SignRsaPkcs(info, cbInfo);
}
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
/* RSA Verify */
if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
}
#endif
#endif /* !NO_RSA */

#if defined(HAVE_ECC)
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
Expand Down Expand Up @@ -461,7 +468,7 @@ int Renesas_cmn_usable(const struct WOLFSSL* ssl, byte session_key_generated)
* Get Callback ctx by devId
*
* devId : devId to get its CTX
* return asocciated CTX when the method is successfully called.
* return associated CTX when the method is successfully called.
* otherwise, NULL
*/
WOLFSSL_LOCAL void *Renesas_cmn_GetCbCtxBydevId(int devId)
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/port/Renesas/renesas_fspsm_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ WOLFSSL_LOCAL void wc_fspsm_RsaKeyFree(RsaKey *key)
/* Set Rsa key by pre-created wrapped user key
*
* key RsaKey object
* size desired keylenth, in bits. supports 1024 or 2048 bits
* size desired key length, in bits. supports 1024 or 2048 bits
* ctx Callback context including pointer to hold generated key
* return FSP_SUCCESS(0) on Success, otherwise negative value
*/
Expand Down
Loading

0 comments on commit ae0d73d

Please sign in to comment.