From a63bb12f6b3b99f261482dccdb308f75e6619e49 Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Fri, 1 Aug 2025 16:58:16 +0900 Subject: [PATCH] Move devId from internal to public - addressed code review by devin --- .../e2studio/RA6M4/test/src/test_main.c | 27 +++-- .../RA6M4/test/src/wolfssl_sce_unit_test.c | 103 ++++++++++-------- .../test/src/client/simple_tls_tsip_client.c | 6 +- .../RX72N/EnvisionKit/Simple/test/test.scfg | 4 +- IDE/Renesas/e2studio/RZN2L/README.md | 2 +- .../RZN2L/test/src/rzn2l_tst_thread_entry.c | 5 +- .../test/src/test/wolfssl_rsip_unit_test.c | 101 +++++++++-------- wolfcrypt/src/port/Renesas/renesas_common.c | 34 +++--- .../src/port/Renesas/renesas_tsip_util.c | 2 +- wolfcrypt/test/test.c | 3 + .../port/Renesas/renesas-fspsm-crypt.h | 3 + .../port/Renesas/renesas-tsip-crypt.h | 2 + .../port/Renesas/renesas_fspsm_internal.h | 2 - .../port/Renesas/renesas_tsip_internal.h | 4 - 14 files changed, 164 insertions(+), 134 deletions(-) diff --git a/IDE/Renesas/e2studio/RA6M4/test/src/test_main.c b/IDE/Renesas/e2studio/RA6M4/test/src/test_main.c index b804ffcdc..20ea72d26 100644 --- a/IDE/Renesas/e2studio/RA6M4/test/src/test_main.c +++ b/IDE/Renesas/e2studio/RA6M4/test/src/test_main.c @@ -150,18 +150,28 @@ void Clr_CallbackCtx(FSPSM_ST *g) { (void) g; - #if defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY) - XFREE(g->wrapped_key_rsapri2048, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (g == NULL) return; - XFREE(g->wrapped_key_rsapub2048, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (g->wrapped_key_aes256 != NULL) + g->wrapped_key_aes256 = NULL; - XFREE(g->wrapped_key_rsapri1024, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (g->wrapped_key_aes128 != NULL) + g->wrapped_key_aes128 = NULL; + + #if defined(WOLFSSL_RENESAS_RSIP_CRYPTONLY) + if (g->wrapped_key_rsapri2048 != NULL) + g->wrapped_key_rsapri2048 = NULL; if (g->wrapped_key_rsapub2048 != NULL) - XFREE(g->wrapped_key_rsapub1024, - NULL, DYNAMIC_TYPE_TMP_BUFFER); + g->wrapped_key_rsapub2048 = NULL; + + if (g->wrapped_key_rsapri1024 != NULL) + g->wrapped_key_rsapri1024 = NULL; + + if (g->wrapped_key_rsapub2048 != NULL) + g->wrapped_key_rsapub2048 = NULL; #endif - XMEMSET(g, 0, sizeof(FSPSM_ST)); + } #endif @@ -262,9 +272,6 @@ void sce_test(void) benchmark_test(NULL); printf("End wolfCrypt Benchmark\n"); - /* free */ - Clr_CallbackCtx(&guser_PKCbInfo); - #elif defined(TLS_CLIENT) #include "hal_data.h" #include "r_sce.h" diff --git a/IDE/Renesas/e2studio/RA6M4/test/src/wolfssl_sce_unit_test.c b/IDE/Renesas/e2studio/RA6M4/test/src/wolfssl_sce_unit_test.c index ac359efb9..c3718e117 100644 --- a/IDE/Renesas/e2studio/RA6M4/test/src/wolfssl_sce_unit_test.c +++ b/IDE/Renesas/e2studio/RA6M4/test/src/wolfssl_sce_unit_test.c @@ -37,8 +37,6 @@ #include "FreeRTOS.h" -static const int devId = 7890; - #ifndef NO_SHA int sha_test(); #endif @@ -63,10 +61,9 @@ static byte Aes256_Cbc_multTst_rslt = 0; static byte Aes128_Gcm_multTst_rslt = 0; static byte Aes256_Gcm_multTst_rslt = 0; -int sce_crypt_AesCbc_multitest(); -int sce_crypt_AesGcm_multitest(); -int sce_crypt_Sha_AesCbcGcm_multitest(); -int sce_crypt_sha_multitest(); +int sce_crypt_AesCbc_multitest(int devId); +int sce_crypt_AesGcm_multitest(int devId); +int sce_crypt_Sha_AesCbcGcm_multitest(int devId); int sce_crypt_test(); int sce_crypt_sha256_multitest(); void tskSha256_Test1(void *pvParam); @@ -97,12 +94,13 @@ FSPSM_ST gCbInfo_a; /* for multi testing */ #endif typedef struct tagInfo { + int devId; sce_aes_wrapped_key_t aes_key; } Info; #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) -static int sce_aes_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) +static int sce_aes_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key, int devId) { Aes aes[1]; @@ -171,7 +169,7 @@ static void tskAes128_Cbc_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = sce_aes_cbc_test(0, &p->aes_key); + ret = sce_aes_cbc_test(0, &p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). sce_aes_cbc_test\n", ret); @@ -186,7 +184,7 @@ static void tskAes128_Cbc_Test(void *pvParam) #endif #ifdef WOLFSSL_AES_256 -static int sce_aes256_test(int prnt, FSPSM_AES_PWKEY aes_key) +static int sce_aes256_test(int prnt, FSPSM_AES_PWKEY aes_key, int devId) { Aes enc[1]; byte cipher[WC_AES_BLOCK_SIZE]; @@ -269,7 +267,7 @@ static void tskAes256_Cbc_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = sce_aes256_test(0, &p->aes_key); + ret = sce_aes256_test(0, &p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). sce_aes256_test\n", ret); @@ -284,7 +282,7 @@ static void tskAes256_Cbc_Test(void *pvParam) #endif /* WOLFSSL_AES_256 */ #if defined(WOLFSSL_AES_256) -static int sce_aesgcm256_test(int prnt, FSPSM_AES_PWKEY aes256_key) +static int sce_aesgcm256_test(int prnt, FSPSM_AES_PWKEY aes256_key, int devId) { Aes enc[1]; Aes dec[1]; @@ -451,7 +449,7 @@ static void tskAes256_Gcm_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = sce_aesgcm256_test(0, &p->aes_key); + ret = sce_aesgcm256_test(0, &p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). sce_aesgcm256_test\n", ret); @@ -466,7 +464,7 @@ static void tskAes256_Gcm_Test(void *pvParam) #if defined(WOLFSSL_AES_128) -static int sce_aesgcm128_test(int prnt, FSPSM_AES_PWKEY aes128_key) +static int sce_aesgcm128_test(int prnt, FSPSM_AES_PWKEY aes128_key, int devId) { Aes enc[1]; Aes dec[1]; @@ -597,7 +595,7 @@ static void tskAes128_Gcm_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = sce_aesgcm128_test(0, &p->aes_key); + ret = sce_aesgcm128_test(0, &p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). sce_aesgcm128_test\n", ret); @@ -619,7 +617,7 @@ static void tskAes128_Gcm_Test(void *pvParam) #define TEST_STRING_SZ 25 #define RSA_TEST_BYTES 256 /* up to 2048-bit key */ -static int sce_rsa_test(int prnt, int keySize) +static int sce_rsa_test(int prnt, int keySize, int devId) { int ret = 0; @@ -654,7 +652,7 @@ static int sce_rsa_test(int prnt, int keySize) XMEMSET(out, 0, outSz); XMEMSET(out2, 0, outSz); - ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/); + ret = wc_InitRsaKey_ex(key, NULL, devId); if (ret != 0) { goto out; } @@ -699,7 +697,7 @@ out: return ret; } -static int sce_rsa_SignVerify_test(int prnt, int keySize) +static int sce_rsa_SignVerify_test(int prnt, int keySize, int devId) { int ret = 0; @@ -730,7 +728,7 @@ static int sce_rsa_SignVerify_test(int prnt, int keySize) 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; } @@ -781,6 +779,7 @@ static int sce_rsa_SignVerify_test(int prnt, int keySize) int sce_crypt_test() { int ret = 0; + int devId = INVALID_DEVID; fsp_err_t err; Clr_CallbackCtx(&gCbInfo); @@ -817,13 +816,14 @@ int sce_crypt_test() SCE_KeyGeneration(&gCbInfo_a); ret = wc_CryptoCb_CryptInitRenesasCmn(NULL, &gCbInfo); - if ( ret > 0) - ret = 0; - + if ( ret > 0) { + devId = ret; + ret = 0; + } if (ret == 0) { printf(" sce_rsa_test(512)(this will be done" " by SW because SCE doesn't support 512 bits key size.)"); - ret = sce_rsa_test(1, 512); + ret = sce_rsa_test(1, 512, devId); RESULT_STR(ret) } @@ -833,13 +833,13 @@ int sce_crypt_test() gCbInfo.keyflgs_crypt.bits.rsapub1024_installedkey_set = 1; gCbInfo.keyflgs_crypt.bits.rsapri2048_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 0; - ret = sce_rsa_test(1, 1024); + ret = sce_rsa_test(1, 1024, devId); RESULT_STR(ret) } if (ret == 0) { printf(" sce_rsa_SignVerify_test(1024)"); - ret = sce_rsa_SignVerify_test(1, 1024); + ret = sce_rsa_SignVerify_test(1, 1024, devId); RESULT_STR(ret) } @@ -849,13 +849,13 @@ int sce_crypt_test() gCbInfo.keyflgs_crypt.bits.rsapub1024_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapri2048_installedkey_set = 1; gCbInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 1; - ret = sce_rsa_test(1, 2048); + ret = sce_rsa_test(1, 2048, devId); RESULT_STR(ret) } if (ret == 0 && err == FSP_SUCCESS) { printf(" sce_rsa_SignVerify_test(2048)"); - ret = sce_rsa_SignVerify_test(1, 2048); + ret = sce_rsa_SignVerify_test(1, 2048, devId); RESULT_STR(ret) } @@ -864,16 +864,16 @@ int sce_crypt_test() ret = sha256_test(); RESULT_STR(ret) #endif - ret = sce_aes_cbc_test(1, &g_user_aes128_key_index1); + ret = sce_aes_cbc_test(1, &g_user_aes128_key_index1, devId); if (ret == 0) { - ret = sce_aes256_test(1, &g_user_aes256_key_index1); + ret = sce_aes256_test(1, &g_user_aes256_key_index1, devId); } if (ret == 0) { - ret = sce_aesgcm128_test(1, &g_user_aes128_key_index1); + ret = sce_aesgcm128_test(1, &g_user_aes128_key_index1, devId); } if (ret == 0) { - ret = sce_aesgcm256_test(1, &g_user_aes256_key_index1); + ret = sce_aesgcm256_test(1, &g_user_aes256_key_index1, devId); } printf(" \n"); if (ret == 0) { @@ -882,22 +882,21 @@ int sce_crypt_test() } if (ret == 0) { printf(" multi Aes cbc thread test\n"); - ret = sce_crypt_AesCbc_multitest(); + ret = sce_crypt_AesCbc_multitest(devId); } if (ret == 0) { printf(" multi Aes Gcm thread test\n"); - ret = sce_crypt_AesGcm_multitest(); + ret = sce_crypt_AesGcm_multitest(devId); } if (ret == 0) { printf(" multi sha aescbc aesgcm thread test\n"); - sce_crypt_Sha_AesCbcGcm_multitest(); + sce_crypt_Sha_AesCbcGcm_multitest(devId); } else ret = -1; - #if defined(WOLFSSL_RENESAS_RSIP_CRYPTONLY) - Clr_CallbackCtx(&gCbInfo); - Clr_CallbackCtx(&gCbInfo_a); - #endif + wc_CryptoCb_CleanupRenesasCmn(&devId); + Clr_CallbackCtx(&gCbInfo); + Clr_CallbackCtx(&gCbInfo_a); return ret; } @@ -955,7 +954,7 @@ int sce_crypt_sha256_multitest() sha256_multTst_rslt1 = 0; sha256_multTst_rslt2 = 0; - exit_semaph = xSemaphoreCreateCounting(num, 0); + exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; #ifndef NO_SHA256 @@ -994,7 +993,7 @@ int sce_crypt_sha256_multitest() } -int sce_crypt_AesCbc_multitest() +int sce_crypt_AesCbc_multitest(int devId) { int ret = 0; int num = 0; @@ -1015,9 +1014,13 @@ int sce_crypt_AesCbc_multitest() Aes128_Cbc_multTst_rslt = 0; Aes256_Cbc_multTst_rslt = 0; - exit_semaph = xSemaphoreCreateCounting(num, 0); + exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes1.devId = devId; + info_aes2.devId = devId; + info_aes256_1.devId = devId; + info_aes256_2.devId = devId; + #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) XMEMCPY(&info_aes1.aes_key, &g_user_aes128_key_index1, sizeof(sce_aes_wrapped_key_t)); @@ -1075,7 +1078,7 @@ int sce_crypt_AesCbc_multitest() } -int sce_crypt_AesGcm_multitest() +int sce_crypt_AesGcm_multitest(int devId) { int ret = 0; int num = 0; @@ -1097,9 +1100,12 @@ int sce_crypt_AesGcm_multitest() Aes128_Gcm_multTst_rslt = 0; Aes256_Gcm_multTst_rslt = 0; - exit_semaph = xSemaphoreCreateCounting(num, 0); + exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes1.devId = devId; + info_aes2.devId = devId; + info_aes256_1.devId = devId; + info_aes256_2.devId = devId; #if defined(WOLFSSL_AES_128) XMEMCPY(&info_aes1.aes_key, &g_user_aes128_key_index1, sizeof(sce_aes_wrapped_key_t)); @@ -1158,7 +1164,7 @@ int sce_crypt_AesGcm_multitest() return ret; } -int sce_crypt_Sha_AesCbcGcm_multitest() +int sce_crypt_Sha_AesCbcGcm_multitest(int devId) { int ret = 0; int num = 0; @@ -1188,9 +1194,12 @@ int sce_crypt_Sha_AesCbcGcm_multitest() Aes128_Gcm_multTst_rslt = 0; Aes256_Gcm_multTst_rslt = 0; - exit_semaph = xSemaphoreCreateCounting(num, 0); + exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes128cbc.devId = devId; + info_aes128gcm.devId = devId; + info_aes256cbc.devId = devId; + info_aes256gcm.devId = devId; #ifndef NO_SHA256 xRet = xTaskCreate(tskSha256_Test1, "sha256_test1", STACK_SIZE, NULL, 3, NULL); diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/client/simple_tls_tsip_client.c b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/client/simple_tls_tsip_client.c index fb2f81c11..c5c19e549 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/client/simple_tls_tsip_client.c +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/client/simple_tls_tsip_client.c @@ -32,7 +32,7 @@ #endif #if defined(SIMPLE_TLS_TSIP_CLIENT) || defined(SIMPLE_TLS_CLIENT) -#define SIMPLE_TLSSEVER_IP "192.168.11.11" +#define SIMPLE_TLSSEVER_IP "192.168.11.6" #define SIMPLE_TLSSERVER_PORT "11111" ER t4_tcp_callback(ID cepid, FN fncd , VP p_parblk); @@ -308,10 +308,10 @@ void wolfSSL_TLS_client( ) #else if (ret == 0) { - err = wolfSSL_use_PrivateKey_buffer(ssl, client_key_der_2048, + ret = wolfSSL_use_PrivateKey_buffer(ssl, client_key_der_2048, sizeof_client_key_der_2048, WOLFSSL_FILETYPE_ASN1); - if (err != SSL_SUCCESS) { + if (ret != SSL_SUCCESS) { printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n", wolfSSL_get_error(ssl, 0)); ret = -1; diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg index c3b4c35b7..020dd9ef3 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg @@ -1132,9 +1132,9 @@ - + - + diff --git a/IDE/Renesas/e2studio/RZN2L/README.md b/IDE/Renesas/e2studio/RZN2L/README.md index 40fc58522..d3f417548 100644 --- a/IDE/Renesas/e2studio/RZN2L/README.md +++ b/IDE/Renesas/e2studio/RZN2L/README.md @@ -218,7 +218,7 @@ MODIFIED ``` .incbin "../../test/Debug/test_RZN2L.bin" ``` -+ Modify `Load Image and Symbol`. Open `Debug Configuration` -> Open `Statup` tab -> Replace `RZN2L_bsp_xspi0bootx1_app.elf` to `test_RZN2L.elf` ++ Modify `Load Image and Symbol`. Open `Debug Configuration` -> Open `Startup` tab -> Replace `RZN2L_bsp_xspi0bootx1_app.elf` to `test_RZN2L.elf` ## Run loader and `test_RZN2L` + Run the loader project diff --git a/IDE/Renesas/e2studio/RZN2L/test/src/rzn2l_tst_thread_entry.c b/IDE/Renesas/e2studio/RZN2L/test/src/rzn2l_tst_thread_entry.c index f2531dabe..c96569b94 100644 --- a/IDE/Renesas/e2studio/RZN2L/test/src/rzn2l_tst_thread_entry.c +++ b/IDE/Renesas/e2studio/RZN2L/test/src/rzn2l_tst_thread_entry.c @@ -237,13 +237,10 @@ void wolfSSL_TLS_cleanup() void rzn2l_tst_thread_entry(void *pvParameters) { FSP_PARAMETER_NOT_USED (pvParameters); - fsp_err_t err; - (void)err; /* Open the transfer instance with initial configuration. */ - err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg); + R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg); #if defined(UNIT_TEST) - int ret; printf("\n"); diff --git a/IDE/Renesas/e2studio/RZN2L/test/src/test/wolfssl_rsip_unit_test.c b/IDE/Renesas/e2studio/RZN2L/test/src/test/wolfssl_rsip_unit_test.c index b632b6224..442c4391c 100644 --- a/IDE/Renesas/e2studio/RZN2L/test/src/test/wolfssl_rsip_unit_test.c +++ b/IDE/Renesas/e2studio/RZN2L/test/src/test/wolfssl_rsip_unit_test.c @@ -37,7 +37,6 @@ #include "FreeRTOS.h" extern FSPSM_INSTANCE gFSPSM_ctrl; -int devId1 = INVALID_DEVID; #ifndef NO_SHA int sha_test(); @@ -74,9 +73,9 @@ static byte Aes256_Cbc_multTst_rslt = 0; static byte Aes128_Gcm_multTst_rslt = 0; static byte Aes256_Gcm_multTst_rslt = 0; -int rsip_crypt_AesCbc_multitest(); -int rsip_crypt_AesGcm_multitest(); -int rsip_crypt_Sha_AesCbcGcm_multitest(); +int rsip_crypt_AesCbc_multitest(int devId); +int rsip_crypt_AesGcm_multitest(int devId); +int rsip_crypt_Sha_AesCbcGcm_multitest(int devId); int rsip_crypt_sha_multitest(); int rsip_crypt_test(); @@ -124,12 +123,13 @@ FSPSM_ST gCbInfo_a; /* for multi testing */ #endif typedef struct tagInfo { + int devId; FSPSM_AES_PWKEY aes_key; } Info; #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) -static int rsip_aes128_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) +static int rsip_aes128_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key, int devId) { Aes aes[1]; @@ -154,7 +154,7 @@ static int rsip_aes128_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) printf(" rsip_aes_cbc_test() "); } - ret = wc_AesInit(aes, NULL, devId1); + ret = wc_AesInit(aes, NULL, devId); if (ret == 0) { ret = wc_AesSetKey(aes, (byte*)aes_key, keySz, iv, AES_ENCRYPTION); @@ -169,7 +169,7 @@ static int rsip_aes128_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) ret = -1; #ifdef HAVE_AES_DECRYPT - ret = wc_AesInit(aes, NULL, devId1); + ret = wc_AesInit(aes, NULL, devId); if (ret == 0) { ret = wc_AesSetKey(aes, (byte*)aes_key, keySz, iv, AES_DECRYPTION); @@ -199,7 +199,7 @@ static void tskAes128_Cbc_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = rsip_aes128_cbc_test(0, p->aes_key); + ret = rsip_aes128_cbc_test(0, p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). rsip_aes_cbc_test\n", ret); @@ -214,7 +214,7 @@ static void tskAes128_Cbc_Test(void *pvParam) #endif #ifdef WOLFSSL_AES_256 -static int rsip_aes256_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) +static int rsip_aes256_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key, int devId) { Aes enc[1]; byte cipher[WC_AES_BLOCK_SIZE]; @@ -237,12 +237,12 @@ static int rsip_aes256_cbc_test(int prnt, FSPSM_AES_PWKEY aes_key) if (prnt) printf(" rsip_aes256_test() "); - if (wc_AesInit(enc, NULL, devId1) != 0) { + if (wc_AesInit(enc, NULL, devId) != 0) { ret = -1; goto out; } - if (wc_AesInit(dec, NULL, devId1) != 0){ + if (wc_AesInit(dec, NULL, devId) != 0){ ret = -2; goto out; } @@ -298,7 +298,7 @@ static void tskAes256_Cbc_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = rsip_aes256_cbc_test(0, p->aes_key); + ret = rsip_aes256_cbc_test(0, p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). rsip_aes256_test\n", ret); @@ -313,7 +313,7 @@ static void tskAes256_Cbc_Test(void *pvParam) #endif /* WOLFSSL_AES_256 */ #if defined(WOLFSSL_AES_256) -static int rsip_aesgcm256_test(int prnt, FSPSM_AES_PWKEY aes256_key) +static int rsip_aesgcm256_test(int prnt, FSPSM_AES_PWKEY aes256_key, int devId) { Aes enc[1]; Aes dec[1]; @@ -384,11 +384,11 @@ static int rsip_aesgcm256_test(int prnt, FSPSM_AES_PWKEY aes256_key) XMEMSET(resultP, 0, sizeof(resultP)); XMEMSET(&userContext, 0, sizeof(FSPSM_ST)); - if (wc_AesInit(enc, NULL, devId1) != 0) { + if (wc_AesInit(enc, NULL, devId) != 0) { ret = -1; goto out; } - if (wc_AesInit(dec, NULL, devId1) != 0) { + if (wc_AesInit(dec, NULL, devId) != 0) { ret = -2; goto out; } @@ -478,7 +478,7 @@ static void tskAes256_Gcm_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = rsip_aesgcm256_test(0, p->aes_key); + ret = rsip_aesgcm256_test(0, p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). rsip_aesgcm256_test\n", ret); @@ -493,7 +493,7 @@ static void tskAes256_Gcm_Test(void *pvParam) #if defined(WOLFSSL_AES_128) -static int rsip_aesgcm128_test(int prnt, FSPSM_AES_PWKEY aes128_key) +static int rsip_aesgcm128_test(int prnt, FSPSM_AES_PWKEY aes128_key, int devId) { Aes enc[1]; Aes dec[1]; @@ -570,12 +570,12 @@ static int rsip_aesgcm128_test(int prnt, FSPSM_AES_PWKEY aes128_key) XMEMSET(resultP, 0, sizeof(resultP)); XMEMSET(&userContext, 0, sizeof(FSPSM_ST)); - if (wc_AesInit(enc, NULL, devId1) != 0) { + if (wc_AesInit(enc, NULL, devId) != 0) { ret = -1; goto out; } - if (wc_AesInit(dec, NULL, devId1) != 0) { + if (wc_AesInit(dec, NULL, devId) != 0) { ret = -2; goto out; } @@ -624,7 +624,7 @@ static void tskAes128_Gcm_Test(void *pvParam) Info *p = (Info*)pvParam; while (exit_loop == 0) { - ret = rsip_aesgcm128_test(0, p->aes_key); + ret = rsip_aesgcm128_test(0, p->aes_key, p->devId); vTaskDelay(10/portTICK_PERIOD_MS); if (ret != 0) { printf(" result was not good(%d). rsip_aesgcm128_test\n", ret); @@ -646,7 +646,7 @@ static void tskAes128_Gcm_Test(void *pvParam) #define TEST_STRING_SZ 25 #define RSA_TEST_BYTES 256 /* up to 2048-bit key */ -static int rsip_rsa_test(int prnt, int keySize) +static int rsip_rsa_test(int prnt, int keySize, int devId) { int ret = 0; @@ -679,7 +679,7 @@ static int rsip_rsa_test(int prnt, int keySize) XMEMSET(out, 0, outSz); XMEMSET(out2, 0, outSz); - ret = wc_InitRsaKey_ex(key, NULL, 7890/* fixed devid for TSIP/SCE*/); + ret = wc_InitRsaKey_ex(key, NULL, devId); if (ret != 0) { goto out; } @@ -726,7 +726,7 @@ out: return ret; } -static int rsip_rsa_SignVerify_test(int prnt, int keySize) +static int rsip_rsa_SignVerify_test(int prnt, int keySize, int devId) { int ret = 0; @@ -760,7 +760,7 @@ static int rsip_rsa_SignVerify_test(int prnt, int keySize) 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; } @@ -902,7 +902,7 @@ int rsip_crypt_sha256_multitest() } -int rsip_crypt_AesCbc_multitest() +int rsip_crypt_AesCbc_multitest(int devId) { int ret = 0; int num = 0; @@ -925,7 +925,10 @@ int rsip_crypt_AesCbc_multitest() exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes1.devId = devId; + info_aes2.devId = devId; + info_aes256_1.devId = devId; + info_aes256_2.devId = devId; #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) XMEMCPY(&info_aes1.aes_key, &g_user_aes128_key_index1, sizeof(FSPSM_AES_PWKEY)); @@ -983,7 +986,7 @@ int rsip_crypt_AesCbc_multitest() } -int rsip_crypt_AesGcm_multitest() +int rsip_crypt_AesGcm_multitest(int devId) { int ret = 0; int num = 0; @@ -1007,7 +1010,10 @@ int rsip_crypt_AesGcm_multitest() exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes1.devId = devId; + info_aes2.devId = devId; + info_aes256_1.devId = devId; + info_aes256_2.devId = devId; #if defined(WOLFSSL_AES_128) XMEMCPY(&info_aes1.aes_key, &g_user_aes128_key_index1, sizeof(FSPSM_AES_PWKEY)); @@ -1066,7 +1072,7 @@ int rsip_crypt_AesGcm_multitest() return ret; } -int rsip_crypt_Sha_AesCbcGcm_multitest() +int rsip_crypt_Sha_AesCbcGcm_multitest(int devId) { int ret = 0; int num = 0; @@ -1098,7 +1104,10 @@ int rsip_crypt_Sha_AesCbcGcm_multitest() exit_semaph = xSemaphoreCreateCounting((UBaseType_t)num, 0); xRet = pdPASS; - + info_aes128cbc.devId = devId; + info_aes128gcm.devId = devId; + info_aes256cbc.devId = devId; + info_aes256gcm.devId = devId; #ifndef NO_SHA256 xRet = xTaskCreate(tskSha256_Test1, "sha256_test1", STACK_SIZE, NULL, 3, NULL); @@ -1174,6 +1183,7 @@ int rsip_crypt_Sha_AesCbcGcm_multitest() int rsip_crypt_test() { int ret = 0; + int devId = INVALID_DEVID; fsp_err_t rsip_error_code = FSP_SUCCESS; /* Generate AES sce Key */ @@ -1204,7 +1214,7 @@ int rsip_crypt_test() ret = wc_CryptoCb_CryptInitRenesasCmn(NULL, &gCbInfo); if ( ret > 0) { - devId1 = ret; + devId = ret; ret = 0; } #if RSA_MIN_SIZE < 1024 @@ -1215,7 +1225,7 @@ int rsip_crypt_test() gCbInfo.keyflgs_crypt.bits.rsapub1024_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapri2048_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 0; - ret = rsip_rsa_test(1, 512); + ret = rsip_rsa_test(1, 512, devId); RESULT_STR(ret) } #endif @@ -1226,13 +1236,13 @@ int rsip_crypt_test() gCbInfo.keyflgs_crypt.bits.rsapub1024_installedkey_set = 1; gCbInfo.keyflgs_crypt.bits.rsapri2048_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 0; - ret = rsip_rsa_test(1, 1024); + ret = rsip_rsa_test(1, 1024, devId); RESULT_STR(ret) } if (ret == 0) { gCbInfo.hash_type = RSIP_HASH_TYPE_SHA256 ; printf(" rsip_rsa_SignVerify_test(1024)"); - ret = rsip_rsa_SignVerify_test(1, 1024); + ret = rsip_rsa_SignVerify_test(1, 1024, devId); RESULT_STR(ret) } #endif @@ -1242,12 +1252,12 @@ int rsip_crypt_test() gCbInfo.keyflgs_crypt.bits.rsapub1024_installedkey_set = 0; gCbInfo.keyflgs_crypt.bits.rsapri2048_installedkey_set = 1; gCbInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 1; - ret = rsip_rsa_test(1, 2048); + ret = rsip_rsa_test(1, 2048, devId); RESULT_STR(ret) } if (ret == 0 && rsip_error_code == FSP_SUCCESS) { printf(" rsip_rsa_SignVerify_test(2048)"); - ret = rsip_rsa_SignVerify_test(1, 2048); + ret = rsip_rsa_SignVerify_test(1, 2048, devId); RESULT_STR(ret) } @@ -1274,18 +1284,18 @@ int rsip_crypt_test() RESULT_STR(ret) #endif - ret = rsip_aes128_cbc_test(1, g_user_aes128_key_index1); + ret = rsip_aes128_cbc_test(1, g_user_aes128_key_index1, devId); if (ret == 0) { - ret = rsip_aes256_cbc_test(1, g_user_aes256_key_index1); + ret = rsip_aes256_cbc_test(1, g_user_aes256_key_index1, devId); } if (ret == 0) { - ret = rsip_aesgcm128_test(1, g_user_aes128_key_index1); + ret = rsip_aesgcm128_test(1, g_user_aes128_key_index1, devId); } if (ret == 0) { - ret = rsip_aesgcm256_test(1, g_user_aes256_key_index1); + ret = rsip_aesgcm256_test(1, g_user_aes256_key_index1, devId); } if (ret == 0) { @@ -1295,20 +1305,25 @@ int rsip_crypt_test() if (ret == 0) { printf(" multi Aes cbc thread test\n"); - ret = rsip_crypt_AesCbc_multitest(); + ret = rsip_crypt_AesCbc_multitest(devId); } if (ret == 0) { printf(" multi Aes Gcm thread test\n"); - ret = rsip_crypt_AesGcm_multitest(); + ret = rsip_crypt_AesGcm_multitest(devId); } if (ret == 0) { printf(" multi Sha AesCbcGcm thread test\n"); - ret = rsip_crypt_Sha_AesCbcGcm_multitest(); + ret = rsip_crypt_Sha_AesCbcGcm_multitest(devId); } #if defined(WOLFSSL_RENESAS_RSIP_CRYPTONLY) + /* + * Need to be cleaned up before context clear + * for internal instance + */ + wc_CryptoCb_CleanupRenesasCmn(&devId); Clr_CallbackCtx(&gCbInfo); Clr_CallbackCtx(&gCbInfo_a); #endif diff --git a/wolfcrypt/src/port/Renesas/renesas_common.c b/wolfcrypt/src/port/Renesas/renesas_common.c index 8c8b52dca..a351261ce 100644 --- a/wolfcrypt/src/port/Renesas/renesas_common.c +++ b/wolfcrypt/src/port/Renesas/renesas_common.c @@ -44,7 +44,7 @@ #define cmn_hw_lock tsip_hw_lock #define cmn_hw_unlock tsip_hw_unlock - #define FSPSM_ST TsipUserCtx; + #define FSPSM_ST TsipUserCtx #define FSPSM_ST_Internal TsipUserCtx_Internal #endif @@ -58,9 +58,7 @@ #define WOLFSSL_MISC_INCLUDED #include #endif -#ifndef WOLFSSL_RENESAS_TSIP_CRYPTONLY - #include -#endif +#include #include #include #include @@ -431,7 +429,7 @@ int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx) /* On Crypt Only mode, it is possible to call this method * first. On that time, internal instance has not yet been allocated. */ - if (!cbInfo->internal) { + if (cbInfo->internal == NULL) { if (!ssl) cbInfo->internal = (FSPSM_ST_Internal*)XMALLOC(internal_sz, ssl->heap, @@ -439,7 +437,7 @@ int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx) else cbInfo->internal = (FSPSM_ST_Internal*)XMALLOC(internal_sz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (!cbInfo->internal) { + if (cbInfo->internal == NULL) { return MEMORY_E; } #if defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\ @@ -455,7 +453,7 @@ int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx) if (gdevId < 0) { gdevId = INITIAL_DEVID; } - cbInfo->internal->devId = gdevId++; + cbInfo->devId = gdevId++; cmn_hw_unlock(); } else { @@ -463,7 +461,7 @@ int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx) return INVALID_DEVID; } - if (wc_CryptoCb_RegisterDevice(cbInfo->internal->devId, + if (wc_CryptoCb_RegisterDevice(cbInfo->devId, Renesas_cmn_CryptoDevCb, cbInfo) < 0) { /* undo devId number */ gdevId--; @@ -474,12 +472,12 @@ int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx) !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) && \ !defined(HAVE_RENESAS_SYNC) if (ssl) - wolfSSL_SetDevId(ssl, cbInfo->internal->devId); + wolfSSL_SetDevId(ssl, cbInfo->devId); #endif - gCbCtx[cbInfo->internal->devId - INITIAL_DEVID] = (void*)cbInfo; + gCbCtx[cbInfo->devId - INITIAL_DEVID] = (void*)cbInfo; - return cbInfo->internal->devId; + return cbInfo->devId; } /* Renesas Security Library Common Method @@ -493,11 +491,13 @@ void wc_CryptoCb_CleanupRenesasCmn(int* id) FSPSM_ST* cbInfo = NULL; - wc_CryptoCb_UnRegisterDevice(*id); + if (*id < INITIAL_DEVID || + (*id - INITIAL_DEVID) > MAX_FSPSM_CBINDEX) + return; /* retrieve internal instance */ - cbInfo = (FSPSM_ST*)gCbCtx[cbInfo->internal->devId - INITIAL_DEVID]; + cbInfo = (FSPSM_ST*)gCbCtx[*id - INITIAL_DEVID]; - if (!cbInfo->internal) { + if (cbInfo != NULL && cbInfo->internal != NULL) { #if defined(WOLFSSL_RENESAS_FSPSM_TLS) && \ !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) XFREE(cbInfo->internal, cbInfo->internal->heap, @@ -507,7 +507,7 @@ void wc_CryptoCb_CleanupRenesasCmn(int* id) #endif cbInfo->internal = NULL; } - *id = INVALID_DEVID; + wc_CryptoCb_UnRegisterDevice(*id); } #endif /* WOLF_CRYPTO_CB */ @@ -799,9 +799,9 @@ WOLFSSL_LOCAL int Renesas_cmn_generateSessionKey(WOLFSSL* ssl, void* ctx) if (Renesas_cmn_usable(ssl, 0)) { #if defined(WOLFSSL_RENESAS_TSIP_TLS) ret = wc_tsip_generateSessionKey(ssl, cbInfo, - cbInfo->internal->devId); + cbInfo->devId); #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) - ret = wc_fspsm_generateSessionKey(ssl, ctx, cbInfo->internal->devId); + ret = wc_fspsm_generateSessionKey(ssl, ctx, cbInfo->devId); #endif } else { diff --git a/wolfcrypt/src/port/Renesas/renesas_tsip_util.c b/wolfcrypt/src/port/Renesas/renesas_tsip_util.c index 8bc02ebc8..33fa22a9b 100644 --- a/wolfcrypt/src/port/Renesas/renesas_tsip_util.c +++ b/wolfcrypt/src/port/Renesas/renesas_tsip_util.c @@ -174,7 +174,7 @@ WOLFSSL_API int tsip_use_PrivateKey_buffer_TLS(struct WOLFSSL* ssl, /* store keyType as Id since Id capacity is 32 bytes */ ret = wolfSSL_use_PrivateKey_Id(ssl, (const unsigned char*)keyBuf, 32, - tuc->internal->devId); + tuc->devId); if (ret == WOLFSSL_SUCCESS) { ret = 0; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 39a993046..3e3488a41 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -2568,6 +2568,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ #ifdef HAVE_CAVIUM_OCTEON_SYNC wc_CryptoCb_CleanupOcteon(&devId); #endif +#ifdef HAVE_RENESAS_SYNC + wc_CryptoCb_CleanupRenesasCmn(&devId); +#endif #endif #ifdef WOLFSSL_ASYNC_CRYPT diff --git a/wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h b/wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h index ce59134fe..d8652adbc 100644 --- a/wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h +++ b/wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h @@ -45,6 +45,9 @@ struct FSPSM_key_flg_ST { typedef struct FSPSM_ST_Internal FSPSM_ST_Internal; typedef struct FSPSM_tag_ST { + /* unique number for each session */ + int devId; + /* installed key handling */ /* aes */ FSPSM_W_KEYVAR wrapped_key_aes256; diff --git a/wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h b/wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h index 1a2ecf52b..a0e3f7de2 100644 --- a/wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h +++ b/wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h @@ -153,6 +153,8 @@ typedef struct TsipUserCtx_Internal TsipUserCtx_Internal; * TsipUserCtx holds mainly keys used for TLS handshake in TSIP specific format. */ typedef struct TsipUserCtx { + /* unique number for each session */ + int devId; int wrappedKeyType; /* for tsip crypt only mode */ diff --git a/wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h b/wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h index 5a7203937..95b5f5982 100644 --- a/wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h +++ b/wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h @@ -32,8 +32,6 @@ struct FSPSM_tls_flg_ST { }; struct FSPSM_ST_Internal { - /* unique number for each session */ - int devId; #if defined(WOLFSSL_RENESAS_FSPSM_TLS) && \ !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) diff --git a/wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h b/wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h index 1459cd8ea..be306b0db 100644 --- a/wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h +++ b/wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h @@ -24,10 +24,6 @@ #include "renesas-tsip-crypt.h" struct TsipUserCtx_Internal { - - /* unique number for each session */ - int devId; - /* client key pair wrapped by provisioning key */ byte* wrappedPrivateKey; byte* wrappedPublicKey;