From 55c2bc527134a1c160d5e84f1e8442aa835699ca Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Fri, 26 Jun 2026 06:15:28 +0900 Subject: [PATCH] fix tsip hashinit return value(f6162) - Add GCM_TABLE_4BIT - Add build.bat for command line build - f5419 mutex - f6163 AES aadBuf - Add debug_run.bat to write and run the example from CL - Add UART message - Messages are routed sci2_uart_charput(). To get back to Virtual Consol, it takes BSP_CFG_USER_CHARPUT_ENABLED 0. f1535 error messaages typo --- .../RX72N/EnvisionKit/Simple/build.bat | 88 ++++ .../EnvisionKit/Simple/common/wolfssl_dummy.c | 63 +++ .../RX72N/EnvisionKit/Simple/debug_run.bat | 65 +++ .../EnvisionKit/Simple/set_demo_mode.ps1 | 75 +++ .../EnvisionKit/Simple/test/src/test_main.c | 5 + .../RX72N/EnvisionKit/Simple/test/test.scfg | 472 +++++++++--------- .../e2studio/RX72N/EnvisionKit/include.am | 1 + .../EnvisionKit/wolfssl_demo/user_settings.h | 1 + .../wolfssl_demo/wolfssl_tsip_unit_test.c | 197 +++++++- wolfcrypt/src/port/Renesas/renesas_tsip_aes.c | 6 +- wolfcrypt/src/port/Renesas/renesas_tsip_sha.c | 20 +- .../src/port/Renesas/renesas_tsip_util.c | 8 +- 12 files changed, 754 insertions(+), 247 deletions(-) create mode 100644 IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/build.bat create mode 100644 IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/debug_run.bat create mode 100644 IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/set_demo_mode.ps1 diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/build.bat b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/build.bat new file mode 100644 index 0000000000..6a33142e33 --- /dev/null +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/build.bat @@ -0,0 +1,88 @@ +@echo off +setlocal + +set MAKE=C:\Renesas\e2_studio\eclipse\plugins\com.renesas.ide.exttools.gnumake.win32.x86_64_4.3.1.v20240909-0854\mk\make.exe +set CCRX_BIN=C:\PROGRA~2\Renesas\RX\3_6_0\bin +set E2_UTILS=%USERPROFILE%\.eclipse\com.renesas.platform_1435879475\Utilities\ccrx +set PATH=%CCRX_BIN%;%E2_UTILS%;%PATH% +set BASEDIR=%~dp0 + +set TARGET=all +set MODE= +if /i "%1"=="clean" ( + set TARGET=clean +) else if /i "%1"=="crypt" ( + set MODE=crypt +) else if /i "%1"=="bench" ( + set MODE=bench +) else if /i "%1"=="TLSClient" ( + set MODE=TLSClient +) else if not "%1"=="" ( + echo [ERROR] Unknown argument "%1". + echo Usage: build.bat [clean^|crypt^|bench^|TLSClient] + echo crypt -^> enables #define CRYPT_TEST in wolfssl_simple_demo.h + echo bench -^> enables #define BENCHMARK + echo TLSClient -^> enables #define SIMPLE_TLS_TSIP_CLIENT + exit /b 1 +) + +if not "%MODE%"=="" ( + echo ============================================================ + echo Setting demo mode: %MODE% + echo ============================================================ + powershell -NoProfile -ExecutionPolicy Bypass -File "%BASEDIR%set_demo_mode.ps1" -Mode %MODE% + if %ERRORLEVEL% neq 0 ( + echo [ERROR] Failed to set demo mode. + exit /b %ERRORLEVEL% + ) + echo. +) + +REM --- Both makefile(s) below are generated by e2studio's managed-build system (from +REM .cproject) the first time each project is built there; build.bat only does incremental +REM rebuilds on top of that, it cannot bootstrap them from a truly clean checkout. --- +if not exist "%BASEDIR%wolfssl\Debug\makefile" ( + echo [ERROR] "%BASEDIR%wolfssl\Debug\makefile" not found. + echo Open the "wolfssl" project in e2studio and build the "Debug" configuration once + echo ^(Project ^> Build Project^) to generate it, then re-run build.bat. + exit /b 1 +) +if not exist "%BASEDIR%test\HardwareDebug\makefile" ( + echo [ERROR] "%BASEDIR%test\HardwareDebug\makefile" not found. + echo Open the "test" project in e2studio and build the "HardwareDebug" configuration once + echo ^(Project ^> Build Project^) to generate it, then re-run build.bat. + exit /b 1 +) +if not exist "%BASEDIR%test\src\smc_gen" ( + echo [ERROR] "%BASEDIR%test\src\smc_gen" not found. + echo This is generated by e2studio's Smart Configurator from test.scfg ^(not tracked in + echo git^). Open test.scfg in e2studio and click "Generate Code" once, then re-run build.bat. + exit /b 1 +) + +echo ============================================================ +echo wolfssl library [%TARGET%] +echo ============================================================ +cd /d "%BASEDIR%wolfssl\Debug" +"%MAKE%" %TARGET% +if %ERRORLEVEL% neq 0 ( + echo [ERROR] wolfssl build failed. + exit /b %ERRORLEVEL% +) + +echo. +echo ============================================================ +echo test application [%TARGET%] +echo ============================================================ +cd /d "%BASEDIR%test\HardwareDebug" +"%MAKE%" %TARGET% +if %ERRORLEVEL% neq 0 ( + echo [ERROR] test build failed. + exit /b %ERRORLEVEL% +) + +echo. +echo ============================================================ +echo Done. +echo ============================================================ +endlocal diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/common/wolfssl_dummy.c b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/common/wolfssl_dummy.c index 08cbc0ec58..791cf64762 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/common/wolfssl_dummy.c +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/common/wolfssl_dummy.c @@ -20,6 +20,69 @@ */ #include +#include "platform.h" + +/* + * printf() is routed here via the r_bsp charput() hook + * (BSP_CFG_USER_CHARPUT_ENABLED / BSP_CFG_USER_CHARPUT_FUNCTION in + * r_bsp_config.h, sourced from test.scfg). Set BSP_CFG_USER_CHARPUT_ENABLED + * to 0 there to switch printf() back to the E1/E2 emulator's Virtual + * Console. */ + +static void sci2_uart_putraw(unsigned char c) +{ + volatile int timeout = 200000; + while (SCI2.SSR.BIT.TDRE == 0 && --timeout > 0) {} + if (timeout > 0) + SCI2.TDR = c; +} + +/* Translate LF to CR+LF so terminals (Tera Term) return to column 0 on + * newline, matching printf()'s "\n"-only line endings. */ +void sci2_uart_charput(char c) +{ + if (c == '\n') + sci2_uart_putraw('\r'); + sci2_uart_putraw((unsigned char)c); +} + +void sci2_uart_init(void) +{ + unsigned char ick, pckb; + unsigned long iclk_hz, src_hz, pclkb_hz; + unsigned long brr_val; + + SYSTEM.PRCR.WORD = 0xA503; //PRC1,0 Write Enable + SYSTEM.MSTPCRB.BIT.MSTPB29 = 0; //SCI2 + + MPC.PWPR.BYTE = 0x00; //B0WI=0 + MPC.PWPR.BYTE = 0x40; //PFS Write Enable + PORT1.PMR.BYTE = 0x0C; //P12,13:peripheral + MPC.P12PFS.BYTE = 0x0A; //P12:RXD2 + MPC.P13PFS.BYTE = 0x0A; //P13:TXD2 + + SCI2.SCR.BYTE = 0x00; /* TE=0,RE=0 */ + SCI2.SMR.BYTE = 0x00; /* async,8bit,no parity,1stop,Phi/1 */ + SCI2.SCMR.BYTE = 0xF2; /* LSB first,no invert,no smart card I/F */ + SCI2.SEMR.BYTE = 0x00; + + /* Derive PCLKB from R_BSP_GetIClkFreqHz() (which already resolves the + * *current* clock source -- PLL/HOCO/MOSC/etc -- and ICK divisor) + * instead of assuming a fixed PLL frequency, so this stays correct + * even if wolfCrypt_Init() (TSIP) changes the clock dividers, and the + * ICLK/CMT tick rate used by current_time() is left alone. */ + iclk_hz = R_BSP_GetIClkFreqHz(); + ick = (unsigned char)SYSTEM.SCKCR.BIT.ICK; /* 0-6: divisor=2^ick */ + pckb = (unsigned char)SYSTEM.SCKCR.BIT.PCKB; /* 0-6: divisor=2^pckb */ + src_hz = iclk_hz << ick; /* undo ICK divisor -> raw source clock */ + pclkb_hz = src_hz >> pckb; /* apply PCKB divisor */ + /* N = PCLKB / (64 * 2^-1 * baud) - 1 = PCLKB / (32 * baud) - 1 (CKS=0, + * SMR.BIT.CKS=00 selects the 2^-1 baud-generator divisor). */ + brr_val = (pclkb_hz / (32UL * 19200UL)) - 1UL; /* BRR for 19200 baud */ + SCI2.BRR = (unsigned char)(brr_val & 0xFFUL); + SCI2.SSR.BYTE = 0x00; /* status clear */ + SCI2.SCR.BYTE = 0x30; /* TIE=0,RIE=0,TE=1,RE=1 */ +} static int tick = 0; diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/debug_run.bat b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/debug_run.bat new file mode 100644 index 0000000000..2f848a4d1e --- /dev/null +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/debug_run.bat @@ -0,0 +1,65 @@ +@echo off +setlocal + +REM --- Flash test.x and let it run, using Renesas Flash Programmer (RFP) instead of a raw +REM GDB session. e2studio's GUI debug launch sends >150 "monitor set_io_access_width" +REM commands (device-specific I/O bus-width setup, captured from the Debugger Console) before +REM anything else; a hand-written GDB script can't feasibly replicate that, and without it the +REM CPU runs but peripherals (SCI2/TSIP) don't come up correctly -- no UART output, even though +REM GDB reports the thread as running. RFP's own flash-and-run path sidesteps all of that: it's +REM closer to a real power-on boot (erase/program/verify, then release reset via -run) and does +REM not go through a GDB debug session at all. Confirmed working: UART output appears correctly +REM after "rfp-cli ... -auto -run". + +set DEBUG_DIR=%USERPROFILE%\.eclipse\com.renesas.platform_1435879475\DebugComp\RX +set OBJCOPY=%DEBUG_DIR%\rx-elf-objcopy.exe +set BASEDIR=%~dp0 +set TARGET_X=%BASEDIR%test\HardwareDebug\test.x +set TARGET_MOT=%BASEDIR%test\HardwareDebug\test.mot +set RFP_LOG=%BASEDIR%test_result.log + +REM --- Find the installed Renesas Flash Programmer CLI (version-independent) --- +for /d %%d in ("C:\Program Files (x86)\Renesas Electronics\Programming Tools\Renesas Flash Programmer V*") do set RFP_DIR=%%d +set RFP_CLI=%RFP_DIR%\rfp-cli.exe + +if not exist "%TARGET_X%" ( + echo [ERROR] %TARGET_X% not found. Run build.bat first. + exit /b 1 +) +if not exist "%RFP_CLI%" ( + echo [ERROR] rfp-cli.exe not found under "C:\Program Files (x86)\Renesas Electronics\Programming Tools\". + exit /b 1 +) + +echo [1/2] Converting ELF ^(test.x^) to Motorola S-record... +"%OBJCOPY%" -O srec "%TARGET_X%" "%TARGET_MOT%" +if %ERRORLEVEL% neq 0 ( + echo [ERROR] rx-elf-objcopy failed with code %ERRORLEVEL% + exit /b 1 +) + +echo [2/2] Erasing, programming, verifying via E2 Lite, then releasing reset to run... +echo ============================================================ +del "%RFP_LOG%" > nul 2>&1 +REM -device RX72x: RFP groups devices by family, not exact part number (R5F572NN falls under RX72x). +REM -auth id FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: matches the all-FF (disabled) ID code used by this project. +REM -run: release reset and let the target run after RFP disconnects (default is to leave it in reset). +"%RFP_CLI%" ^ + -device RX72x ^ + -tool e2l ^ + -if fine ^ + -auth id FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ + -noquery ^ + -auto -run ^ + -log "%RFP_LOG%" ^ + "%TARGET_MOT%" +set RFP_EXIT=%ERRORLEVEL% +echo ============================================================ + +if %RFP_EXIT% neq 0 ( + echo [ERROR] rfp-cli exited with code %RFP_EXIT% +) else ( + echo [DONE] Target is running -- check Tera Term for UART output. +) + +exit /b %RFP_EXIT% diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/set_demo_mode.ps1 b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/set_demo_mode.ps1 new file mode 100644 index 0000000000..c3138f1014 --- /dev/null +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/set_demo_mode.ps1 @@ -0,0 +1,75 @@ +# set_demo_mode.ps1 +# +# Copyright (C) 2006-2026 wolfSSL Inc. +# +# This file is part of wolfSSL. +# +# wolfSSL is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# wolfSSL is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + +param( + [Parameter(Mandatory=$true)] + [ValidateSet('crypt', 'bench', 'TLSClient')] + [string]$Mode +) + +$macroMap = [ordered]@{ + 'crypt' = 'CRYPT_TEST' + 'bench' = 'BENCHMARK' + 'TLSClient' = 'SIMPLE_TLS_TSIP_CLIENT' +} +$selected = $macroMap[$Mode] + +$file = Join-Path $PSScriptRoot 'test\src\wolfssl_simple_demo.h' +if (-not (Test-Path $file)) { + Write-Error "File not found: $file" + exit 1 +} + +$content = Get-Content -Raw -Path $file + +# Pass 1: normalize all three macros to the uncommented ("#define X") form, +# regardless of their current state, so pass 2's substring replace can't +# double-wrap an already-commented line (e.g. match "#define X" inside +# "/*#define X*/"). +foreach ($m in $macroMap.Values) { + $content = $content.Replace("/*#define $m*/", "#define $m") +} + +# Pass 2: comment out every macro except the selected one. +foreach ($m in $macroMap.Values) { + if ($m -ne $selected) { + $content = $content.Replace("#define $m", "/*#define $m*/") + } +} + +Set-Content -NoNewline -Path $file -Value $content +Write-Host "[demo mode] $Mode -> #define $selected enabled (others disabled)" + +# make's generated dependency (.d) files don't reliably list wolfssl_simple_demo.h as a +# prerequisite for every .c that includes it, so an incremental build can silently relink a +# stale .obj (compiled under the old macro) into a fresh-looking test.x. Force a rebuild of +# exactly the affected files by touching their mtime, rather than doing a full clean (slow) +# or trusting make's dependency tracking (unreliable here). +$srcDir = Join-Path $PSScriptRoot 'test\src' +$dependents = Get-ChildItem -Path $srcDir -Recurse -Include '*.c','*.h' | + Where-Object { $_.FullName -ne $file } | + Select-String -Pattern 'wolfssl_simple_demo\.h' -List | + Select-Object -ExpandProperty Path + +foreach ($dep in $dependents) { + (Get-Item $dep).LastWriteTime = Get-Date +} +Write-Host "[demo mode] Touched $($dependents.Count) dependent source file(s) to force recompilation:" +$dependents | ForEach-Object { Write-Host " $_" } diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/test_main.c b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/test_main.c index f75913e78d..6d549bb834 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/test_main.c +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/test_main.c @@ -172,6 +172,8 @@ int SetTsiptlsKey() #endif +extern void sci2_uart_init(void); + void main(void) { int i = 0; @@ -179,6 +181,8 @@ void main(void) int doClientCheck = 0; uint32_t channel; + sci2_uart_init(); /* route printf → SCI2 UART (P13/TXD2, 19200 baud) */ + #if defined(SIMPLE_TCP_CLIENT) int cipherlist_sz = 1; const char* cipherlist[] = {NULL}; @@ -234,6 +238,7 @@ void main(void) printf("wolfCrypt_Init failed %d\n", ret); } + sci2_uart_init(); /* re-init: TSIP may change PCLKB during wolfCrypt_Init */ printf("Start wolfCrypt Test\n"); wolfcrypt_test(args); printf("End wolfCrypt Test\n"); diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg index 020dd9ef31..86bdffac45 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg @@ -361,230 +361,230 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -843,8 +843,8 @@ - - + + - + - + - + - + @@ -951,14 +951,14 @@ - + - + @@ -1069,7 +1069,7 @@ - + @@ -1130,7 +1130,7 @@ - + @@ -1138,8 +1138,8 @@ - - + + diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/include.am b/IDE/Renesas/e2studio/RX72N/EnvisionKit/include.am index cd39f4b25a..52d1fd00cf 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/include.am +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/include.am @@ -37,3 +37,4 @@ EXTRA_DIST+= IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.rcpc EXTRA_DIST+= IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/test.scfg EXTRA_DIST+= IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/wolfssl/.cproject EXTRA_DIST+= IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/wolfssl/.project +EXTRA_DIST+= IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/build.bat diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h b/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h index a7be4be7bc..95515f4c5c 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h @@ -101,6 +101,7 @@ #define HAVE_TLS_EXTENSIONS #define HAVE_AESGCM + #define GCM_TABLE_4BIT #define HAVE_AESCCM #define HAVE_AES_CBC #define WOLFSSL_AES_DIRECT diff --git a/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c b/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c index 840dd83427..3f3e6dc4ee 100644 --- a/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c +++ b/IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c @@ -1506,12 +1506,197 @@ int tsip_crypt_Sha_AesCbcGcm_multitest(void) #endif +#if !defined(NO_SHA256) && !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) +static int tsip_sha256_hash_test(int prnt) +{ + wc_Sha256 sha; + byte hash1[WC_SHA256_DIGEST_SIZE]; + byte hash2[WC_SHA256_DIGEST_SIZE]; + int ret = 0; + int shaInited = 0; + + /* SHA-256("abc") */ + static const byte msg[] = { 0x61, 0x62, 0x63 }; + static const byte expected[WC_SHA256_DIGEST_SIZE] = { + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, + 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad + }; + + if (prnt) + printf(" tsip_sha256_hash_test() "); + + /* wc_Sha256Final: correct digest */ + ret = wc_InitSha256_ex(&sha, NULL, 0); + if (ret != 0) { + ret = -1; + goto out; + } + shaInited = 1; + ret = wc_Sha256Update(&sha, msg, sizeof(msg)); + if (ret != 0) { + ret = -2; + goto out; + } + XMEMSET(hash1, 0, sizeof(hash1)); + ret = wc_Sha256Final(&sha, hash1); + if (ret != 0) { + ret = -3; + goto out; + } + if (XMEMCMP(hash1, expected, WC_SHA256_DIGEST_SIZE) != 0) { + ret = -4; goto out; + } + + /* wc_Sha256GetHash: non-destructive, same digest on repeated calls */ + ret = wc_Sha256Update(&sha, msg, sizeof(msg)); + if (ret != 0) { + ret = -5; + goto out; + } + XMEMSET(hash1, 0, sizeof(hash1)); + ret = wc_Sha256GetHash(&sha, hash1); + if (ret != 0) { + ret = -6; + goto out; + } + if (XMEMCMP(hash1, expected, WC_SHA256_DIGEST_SIZE) != 0) { + ret = -7; + goto out; + } + XMEMSET(hash2, 0, sizeof(hash2)); + ret = wc_Sha256GetHash(&sha, hash2); + if (ret != 0) { + ret = -8; + goto out; + } + if (XMEMCMP(hash1, hash2, WC_SHA256_DIGEST_SIZE) != 0) { + ret = -9; goto out; + } + + /* Final after GetHash must also match */ + XMEMSET(hash2, 0, sizeof(hash2)); + ret = wc_Sha256Final(&sha, hash2); + if (ret != 0) { + ret = -10; + goto out; + } + if (XMEMCMP(hash1, hash2, WC_SHA256_DIGEST_SIZE) != 0) { + ret = -11; goto out; + } + +out: + if (shaInited) + wc_Sha256Free(&sha); + if (prnt) { + if (ret != 0) + printf("(code=%d) ", ret); + RESULT_STR(ret) + } + return ret; +} +#endif /* !NO_SHA256 && !NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH */ + +#if !defined(NO_SHA) && !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) +static int tsip_sha1_hash_test(int prnt) +{ + wc_Sha sha; + byte hash1[WC_SHA_DIGEST_SIZE]; + byte hash2[WC_SHA_DIGEST_SIZE]; + int ret = 0; + int shaInited = 0; + + /* NIST FIPS 180-4: SHA-1("abc") */ + static const byte msg[] = { 0x61, 0x62, 0x63 }; + static const byte expected[WC_SHA_DIGEST_SIZE] = { + 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, + 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, + 0x9c, 0xd0, 0xd8, 0x9d + }; + + if (prnt) + printf(" tsip_sha1_hash_test() "); + + /* wc_ShaFinal: correct digest */ + ret = wc_InitSha_ex(&sha, NULL, 0); + if (ret != 0) { + ret = -1; + goto out; + } + shaInited = 1; + ret = wc_ShaUpdate(&sha, msg, sizeof(msg)); + if (ret != 0) { + ret = -2; + goto out; + } + XMEMSET(hash1, 0, sizeof(hash1)); + ret = wc_ShaFinal(&sha, hash1); + if (ret != 0) { + ret = -3; + goto out; + } + if (XMEMCMP(hash1, expected, WC_SHA_DIGEST_SIZE) != 0) { + ret = -4; + goto out; + } + + /* wc_ShaGetHash: non-destructive, same digest on repeated calls */ + ret = wc_ShaUpdate(&sha, msg, sizeof(msg)); + if (ret != 0) { + ret = -5; + goto out; + } + XMEMSET(hash1, 0, sizeof(hash1)); + ret = wc_ShaGetHash(&sha, hash1); + if (ret != 0) { + ret = -6; + goto out; + } + if (XMEMCMP(hash1, expected, WC_SHA_DIGEST_SIZE) != 0) { + ret = -7; + goto out; + } + XMEMSET(hash2, 0, sizeof(hash2)); + ret = wc_ShaGetHash(&sha, hash2); + if (ret != 0) { + ret = -8; + goto out; + } + + if (XMEMCMP(hash1, hash2, WC_SHA_DIGEST_SIZE) != 0) { + ret = -9; + goto out; + } + + /* Final after GetHash must also match */ + XMEMSET(hash2, 0, sizeof(hash2)); + ret = wc_ShaFinal(&sha, hash2); + if (ret != 0) { + ret = -10; + goto out; + } + if (XMEMCMP(hash1, hash2, WC_SHA_DIGEST_SIZE) != 0) { + ret = -11; goto out; + } + +out: + if (shaInited) + wc_ShaFree(&sha); + if (prnt) { + if (ret != 0) + printf("(code=%d) ", ret); + RESULT_STR(ret) + } + return ret; +} +#endif /* !NO_SHA && !NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH */ + int tsip_crypt_test(void) { int ret = 0; int devId; - Clr_CallbackCtx(&userContext); if (ret != 0) { printf("TSIP Key Generation failed\n"); return -1; @@ -1537,6 +1722,16 @@ int tsip_crypt_test(void) } #endif + #if !defined(NO_SHA256) && !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) + if (ret == 0) + ret = tsip_sha256_hash_test(1); + #endif + + #if !defined(NO_SHA) && !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) + if (ret == 0) + ret = tsip_sha1_hash_test(1); + #endif + #ifdef HAVE_AES_CBC ret = TSIP_AesKeyGeneration(&userContext, 16); if (ret == 0) diff --git a/wolfcrypt/src/port/Renesas/renesas_tsip_aes.c b/wolfcrypt/src/port/Renesas/renesas_tsip_aes.c index 477fab7ffd..9d3774bbfa 100644 --- a/wolfcrypt/src/port/Renesas/renesas_tsip_aes.c +++ b/wolfcrypt/src/port/Renesas/renesas_tsip_aes.c @@ -171,7 +171,7 @@ WOLFSSL_LOCAL int tsip_Tls13AesEncrypt( sz); if (err != TSIP_SUCCESS) { - WOLFSSL_MSG("R_TSIP_Tls13DecryptUpdate error"); + WOLFSSL_MSG("R_TSIP_Tls13EncryptInit error"); ret = WC_HW_E; } @@ -201,7 +201,7 @@ WOLFSSL_LOCAL int tsip_Tls13AesEncrypt( remain -= dataSz; } else { - WOLFSSL_MSG("R_TSIP_Tls13DecryptUpdate error"); + WOLFSSL_MSG("R_TSIP_Tls13EncryptUpdate error"); ret = WC_HW_E; } } @@ -1123,7 +1123,7 @@ int wc_tsip_AesGcmDecrypt( if (err == TSIP_SUCCESS) { /* pass only AAD and it's size before passing cipher text */ - err = updateFn(&hdl, NULL, NULL, 0UL, (uint8_t*)authIn, + err = updateFn(&hdl, NULL, NULL, 0UL, (uint8_t*)aadBuf, authInSz); } if (err == TSIP_SUCCESS) { diff --git a/wolfcrypt/src/port/Renesas/renesas_tsip_sha.c b/wolfcrypt/src/port/Renesas/renesas_tsip_sha.c index d040723977..d79fca096f 100644 --- a/wolfcrypt/src/port/Renesas/renesas_tsip_sha.c +++ b/wolfcrypt/src/port/Renesas/renesas_tsip_sha.c @@ -364,7 +364,7 @@ static int TSIPHashUpdate(wolfssl_TSIP_Hash* hash, const byte* data, word32 sz) static int TSIPHashFinal(wolfssl_TSIP_Hash* hash, byte* out, word32 outSz) { - int ret; + int ret = WC_HW_E; void* heap; tsip_sha_md5_handle_t handle; uint32_t sz; @@ -400,19 +400,26 @@ static int TSIPHashFinal(wolfssl_TSIP_Hash* hash, byte* out, word32 outSz) ret = Final(&handle, out, (uint32_t*)&sz); if (ret != TSIP_SUCCESS || sz != outSz) { tsip_hw_unlock(); - return ret; + return (ret == TSIP_SUCCESS) ? WC_HW_E : ret; } } + else { + ret = WC_HW_E; + } } tsip_hw_unlock(); + if (ret != 0) { + return ret; + } + TSIPHashFree(hash); return TSIPHashInit(hash, heap, 0, hash->sha_type); } static int TSIPHashGet(wolfssl_TSIP_Hash* hash, byte* out, word32 outSz) { - int ret; + int ret = WC_HW_E; tsip_sha_md5_handle_t handle; uint32_t sz; @@ -445,14 +452,17 @@ static int TSIPHashGet(wolfssl_TSIP_Hash* hash, byte* out, word32 outSz) ret = Final(&handle, out, &sz); if (ret != TSIP_SUCCESS || sz != outSz) { tsip_hw_unlock(); - return ret; + return (ret == TSIP_SUCCESS) ? WC_HW_E : ret; } } + else { + ret = WC_HW_E; + } } tsip_hw_unlock(); - return 0; + return ret; } static int TSIPHashCopy(wolfssl_TSIP_Hash* src, wolfssl_TSIP_Hash* dst) diff --git a/wolfcrypt/src/port/Renesas/renesas_tsip_util.c b/wolfcrypt/src/port/Renesas/renesas_tsip_util.c index 7a5ed543f0..ef4425b6b5 100644 --- a/wolfcrypt/src/port/Renesas/renesas_tsip_util.c +++ b/wolfcrypt/src/port/Renesas/renesas_tsip_util.c @@ -2459,7 +2459,8 @@ int tsip_ImportPublicKey(TsipUserCtx* tuc, int keyType) sizeof(tsip_rsa2048_public_key_index_t), NULL, DYNAMIC_TYPE_RSA_BUFFER); if (tuc->rsa2048pub_keyIdx == NULL) { - return MEMORY_E; + ret = MEMORY_E; + break; } #endif err = R_TSIP_GenerateRsa2048PublicKeyIndex( @@ -3220,8 +3221,10 @@ int wc_tsip_generateSessionKey( if (enc->aes == NULL) { enc->aes = (Aes*)XMALLOC(sizeof(Aes), ssl->heap, DYNAMIC_TYPE_CIPHER); - if (enc->aes == NULL) + if (enc->aes == NULL) { + tsip_hw_unlock(); return MEMORY_E; + } } ForceZero(enc->aes, sizeof(Aes)); @@ -3234,6 +3237,7 @@ int wc_tsip_generateSessionKey( if (enc) { XFREE(enc->aes, NULL, DYNAMIC_TYPE_CIPHER); } + tsip_hw_unlock(); return MEMORY_E; } }