From c01cd808da3a41ce7f60e88241c10f224f304cf2 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 10 Jan 2020 09:42:25 -0800 Subject: [PATCH 1/7] changes for build with s7g2 add project info add project info adjust wolfssl library template change TRNG collection fix include.am fix argument for TRNG rename example templates comment out DEBUG_WOLFSSL change include.am update license --- IDE/Renesas/e2studio/DK-S7G2/README.md | 27 ++ .../DK-S7G2/benchmark-template/.cproject | 400 ++++++++++++++++++ .../DK-S7G2/benchmark-template/.project | 34 ++ .../benchmark-template/src/app_entry.c | 298 +++++++++++++ .../DK-S7G2/example_server-template/.cproject | 391 +++++++++++++++++ .../DK-S7G2/example_server-template/.project | 27 ++ .../example_server-template/src/app_entry.c | 181 ++++++++ IDE/Renesas/e2studio/DK-S7G2/include.am | 22 + IDE/Renesas/e2studio/DK-S7G2/user_settings.h | 64 +++ .../DK-S7G2/wolfcrypttest-template/.cproject | 392 +++++++++++++++++ .../DK-S7G2/wolfcrypttest-template/.project | 34 ++ .../wolfcrypttest-template/src/app_entry.c | 76 ++++ .../DK-S7G2/wolfssl-template-project/.project | 49 +++ .../configuration.xml | 352 +++++++++++++++ IDE/include.am | 1 + wolfcrypt/benchmark/benchmark.c | 26 ++ wolfcrypt/src/random.c | 49 ++- wolfcrypt/src/wc_port.c | 15 +- wolfssl/wolfio.h | 13 + 19 files changed, 2448 insertions(+), 3 deletions(-) create mode 100644 IDE/Renesas/e2studio/DK-S7G2/README.md create mode 100644 IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.cproject create mode 100644 IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.project create mode 100644 IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c create mode 100644 IDE/Renesas/e2studio/DK-S7G2/example_server-template/.cproject create mode 100644 IDE/Renesas/e2studio/DK-S7G2/example_server-template/.project create mode 100644 IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c create mode 100644 IDE/Renesas/e2studio/DK-S7G2/include.am create mode 100644 IDE/Renesas/e2studio/DK-S7G2/user_settings.h create mode 100644 IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.cproject create mode 100644 IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.project create mode 100644 IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/src/app_entry.c create mode 100644 IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/.project create mode 100644 IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/configuration.xml diff --git a/IDE/Renesas/e2studio/DK-S7G2/README.md b/IDE/Renesas/e2studio/DK-S7G2/README.md new file mode 100644 index 000000000..4c999418e --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/README.md @@ -0,0 +1,27 @@ + +## Building wolfSSL For DK-S7G2 + +- First physically toggle the ENET1 and JTAG switch to on with the DK-S7G2 board. +- Open e2studio and set the workspace to be wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/ +- Create a Synergy library project named wolfssl "File->New->Synergy C/C++ Project", "Renesas Synergy C Library Project" then "Next", set wolfssl as the "Project Name" then "Next", set Board to "S7G2 DK" then "Next", finally select the BSP radius and click "Finish" +- Copy configuration.xml and .project from wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/ into the wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl directory +- (optional but necessary for production) Add TRNG support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > TRNG Driver on r_sce_trng". Then uncomment WOLFSSL_SCE define in wolfssl project src/user_settings.h +- Generate the changes by clicking on "Generate Project Content" +- Exclude src/wolfcrypt/port and all src/wolfcrypt/*.S and src/wolfcrypt/*.asm files from the build +- Exclude src/wolfcrypt/evp.c, src/wolfcrypt/misc.c and src/wolfssl/bio.c +- Set the Preprocessor define in wolfssl proejct to have WOLFSSL_USER_SETTINGS. Right click on wolfssl project "Properties -> C/C++ Build -> GNU ARM Cross C Compiler -> Preprocessor" add WOLFSSL_USER_SETTINGS under "Defined symbols" +- Set include to wolfssl directory. Right click on project "Properties -> C/C++Build -> GNU ARM Cross Compiler -> Includes". Add "${ProjDirPath}/../../../../.." and "${ProjDirPath}/../" +- Build wolfssl by right clicking on wolfssl project and selecting "Build Project" + +## Example Projects and Building + +- Create a new Synergy project "Renesas Synergy C Project Using Synergy Library" +- Set it to use the wolfssl library +- Copy in the .cproject, .project and source file from the template desired i.e. wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/ +- Right click on the created project and select "Build Project" + +The example_server loops looking to accept connections and closes immediatly after a successful connection was made. + +The benchmark example tries to do a TCP connection to SERVER_IP on port 11112 and a TLS connection to SERVER_IP on port 11111 then does wolfCrypt benchmark collection. + +The wolfcryptest runs through all of the unit tests from wolfcrypt/test/test.c \ No newline at end of file diff --git a/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.cproject b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.cproject new file mode 100644 index 000000000..09d87045d --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.cproject @@ -0,0 +1,400 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.project b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.project new file mode 100644 index 000000000..1b60e8e4f --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.project @@ -0,0 +1,34 @@ + + + benchmark + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + com.renesas.cdt.synergy.contentgen.synergyExecutableNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + src/benchmark + 2 + PARENT-5-PROJECT_LOC/wolfcrypt/benchmark + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c new file mode 100644 index 000000000..1bcbc55ee --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c @@ -0,0 +1,298 @@ +/* app_entry.c + * + * Copyright (C) 2006-2020 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 2 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 + */ + +#include "app.h" +#include "stdio.h" +extern void initialise_monitor_handles(void); + +#include + +#ifndef WOLFCRYPT_ONLY + +#include + +#define USE_CERT_BUFFERS_2048 +#define USE_CERT_BUFFERS_256 +#include + +#include "nx_api.h" +#define CONNECTION_TIMES 100 +#define SERVER_IP IP_ADDRESS(10,22,73,128) +#define TLS_PORT 11111 +#define TCP_PORT 11112 + +static double miliseconds = 0; +void timer_callback(timer_callback_args_t * args) +{ + miliseconds++; + (void)args; +} + +/* version is the type of TLS version to use. For example TLS1.2 = version 2 + * and TLS1.3 = version 3 + * + * suites is a null terminated string containing the cipher suites to us, or + * can be NULL for default*/ +static void benchmark_TLS(int version, char* suites, int group) +{ + UINT TEST_PORT = TLS_PORT; + ULONG TEST_IP = SERVER_IP; + int i; + NX_TCP_SOCKET sockfd; + int ret; + int groups[1]; + double start; + WOLFSSL_METHOD* method = NULL; + + WOLFSSL_CTX* ctx; + WOLFSSL* ssl; + + switch (version) { + case 2: method = wolfTLSv1_2_client_method(); break; +#ifdef WOLFSSL_TLS13 + case 3: method = wolfTLSv1_3_client_method(); break; +#endif + default: + printf("Unknown TLS version (Check if wolfSSL was built with it supported)\n"); + return; + } + + ctx = wolfSSL_CTX_new(method); + if (ctx == NULL) { + printf("unable to create ctx\n"); + return; + } + +#ifndef NO_RSA + /* add default RSA CA */ + ret = wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048, + sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1); + if (ret != WOLFSSL_SUCCESS) { + printf("error %d loading CA\n", ret); + return; + } +#endif + +#ifdef HAVE_ECC + /* add default ECDSA CA */ + ret = wolfSSL_CTX_load_verify_buffer(ctx, ca_ecc_cert_der_256, + sizeof_ca_ecc_cert_der_256, SSL_FILETYPE_ASN1); + if (ret != WOLFSSL_SUCCESS) { + printf("error %d loading CA\n", ret); + return; + } + +#endif + + if (suites != NULL) { + ret = wolfSSL_CTX_set_cipher_list(ctx, suites); + if (ret != WOLFSSL_SUCCESS) { + printf("error %d setting cipher suites %s\n", ret, suites); + return; + } + } + +#ifdef WOLFSSL_TLS13 + if (version == 3) { + groups[0] = group; + ret = wolfSSL_CTX_set_groups(ctx, groups, 1); + if (ret != WOLFSSL_SUCCESS) { + printf("error setting group\n"); + return; + } + } +#endif + + printf("Trying to connect to 0x%lX on port %d\n", TEST_IP, TEST_PORT); + + miliseconds = 0; + g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg); + g_timer0.p_api->start(g_timer0.p_ctrl); + + start = (double)tx_time_get(); // TX_TIMER_TICKS_PER_SECOND = 100 + ret = (int)nx_tcp_socket_create(&g_ip0, &sockfd, "TLS_CLIENT", NX_IP_NORMAL, + NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1500, NX_NULL, NX_NULL); + if (ret != NX_SUCCESS) { + printf("failed to create socket err = 0x%X\n", ret); + return; + } + for (i = 0; i < CONNECTION_TIMES; i++) { + + ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, NX_WAIT_FOREVER); + if (ret != NX_SUCCESS) { + printf("failed to bind socket\n"); + return; + } + + ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, NX_WAIT_FOREVER); + if (ret != NX_SUCCESS) { + printf("failed to connect with error 0x%X\n", ret); + return; + } + + ssl = wolfSSL_new(ctx); + if (ssl == NULL) { + printf("Error creating ssl\n"); + return; + } + +#ifdef WOLFSSL_TLS13 + if (version == 3) { + ret = wolfSSL_UseKeyShare(ssl, group); + if (ret != WOLFSSL_SUCCESS) { + printf("Error %d with set key share\n", ret); + return; + } + } +#endif + + wolfSSL_SetIO_NetX(ssl, &sockfd, NX_WAIT_FOREVER); + + ret = wolfSSL_connect(ssl); + if (ret != WOLFSSL_SUCCESS) { + printf("Error %d with wolfssl connect\n", ret); + return; + } + wolfSSL_free(ssl); + + nx_tcp_socket_disconnect(&sockfd, NX_WAIT_FOREVER); + + ret = nx_tcp_client_socket_unbind(&sockfd); + if (ret != NX_SUCCESS) { + printf("failed to unbind with error 0x%X\n", ret); + return; + } + + } + nx_tcp_socket_delete(&sockfd); + + g_timer0.p_api->stop(g_timer0.p_ctrl); + start = (double)tx_time_get() - start; + g_timer0.p_api->close(g_timer0.p_ctrl); + + printf("%d TLS connections took %f seconds and %f tx_time ticks\n", + CONNECTION_TIMES, (miliseconds / 10), start); + wolfSSL_CTX_free(ctx); +} + + +static void benchmark_TCP() +{ + UINT TEST_PORT = TCP_PORT; + ULONG TEST_IP = SERVER_IP; + int i; + NX_TCP_SOCKET sockfd; + int ret; + double start; + + + { + NX_PACKET* response; + printf("Pinging server to see if up .. "); + fflush(stdout); + ret = (int)nx_icmp_ping(&g_ip0, TEST_IP, "Hello", strlen("Hello"), &response, 2000); + if (ret != NX_SUCCESS) { + printf("Unable to ping server, error = 0x%X\n", ret); + return; + } + printf("got response from server\n"); + nx_packet_release(response); + } + + printf("Benchmarking client TCP connection\n"); + printf("Trying to connect to 0x%lX on port %d\n", TEST_IP, TEST_PORT); + start = (double)tx_time_get() / TX_TIMER_TICKS_PER_SECOND; + ret = (int)nx_tcp_socket_create(&g_ip0, &sockfd, "TCP_CLIENT", NX_IP_NORMAL, + NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 256, NX_NULL, NX_NULL); + if (ret != NX_SUCCESS) { + printf("failed to create socket err = 0x%X\n", ret); + return; + } + + for (i = 0; i < CONNECTION_TIMES; i++) { + ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, NX_WAIT_FOREVER); + if (ret != NX_SUCCESS) { + printf("failed to bind socket\n"); + return; + } + + ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, NX_WAIT_FOREVER); + if (ret != NX_SUCCESS) { + printf("failed to connect with error 0x%X\n", ret); + return; + } + + nx_tcp_socket_disconnect(&sockfd, NX_WAIT_FOREVER); + + ret = (int)nx_tcp_client_socket_unbind(&sockfd); + if (ret != NX_SUCCESS) { + printf("failed to unbind with error 0x%X\n", ret); + return; + } + } + nx_tcp_socket_delete(&sockfd); + + start = ((double)tx_time_get() / TX_TIMER_TICKS_PER_SECOND) - start; + printf("%d TCP connections took %f seconds\n", CONNECTION_TIMES, start); +} +#endif /* WOLFCRYPT_ONLY */ + +/* Benchmark entry function */ +void app_entry(void) +{ + initialise_monitor_handles(); + +#ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); +#endif + + wolfSSL_Init(); + +#ifndef WOLFCRYPT_ONLY + benchmark_TCP(); + + printf("\nBenchmarking client TLSv1.2 connection using ECDHE-RSA-AES128-GCM-SHA256\n"); + benchmark_TLS(2, "ECDHE-RSA-AES128-GCM-SHA256", 0); + #ifdef WOLFSSL_TLS13 + #ifdef HAVE_CURVE25519 + printf("\nBenchmarking client TLSv1.3 WOLFSSL_ECC_X25519 connection using TLS13_AES128_GCM_SHA256\n"); + benchmark_TLS(3, "TLS13-AES128-GCM-SHA256", (int)WOLFSSL_ECC_X25519); + #endif + #ifdef HAVE_ECC + printf("\nBenchmarking client TLSv1.3 WOLFSSL_ECC_SECP256R1 connection using TLS13_AES128_GCM_SHA256\n"); + benchmark_TLS(3, "TLS13-AES128-GCM-SHA256", (int)WOLFSSL_ECC_SECP256R1); + #endif + printf("\nBenchmarking client TLSv1.3 WOLFSSL_FFDHE_2048 connection using TLS13_AES128_GCM_SHA256\n"); + benchmark_TLS(3, "TLS13-AES128-GCM-SHA256", (int)WOLFSSL_FFDHE_2048); + #endif +#endif + +#if 1 + /* run wolfcrypt benchmarks */ + benchmark_test(NULL); +#endif + + wolfSSL_Cleanup(); + while (1) + { + tx_thread_sleep (100); + } +} diff --git a/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.cproject b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.cproject new file mode 100644 index 000000000..3b026ad59 --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.cproject @@ -0,0 +1,391 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.project b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.project new file mode 100644 index 000000000..aaff6e3d7 --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/.project @@ -0,0 +1,27 @@ + + + example_server + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + com.renesas.cdt.synergy.contentgen.synergyExecutableNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c new file mode 100644 index 000000000..92a32d8d7 --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c @@ -0,0 +1,181 @@ +/* app_entry.c + * + * Copyright (C) 2006-2020 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 2 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 + */ + + +#include "app.h" +#include "nx_api.h" +#include "stdio.h" +extern void initialise_monitor_handles(void); + +#include +#define USE_CERT_BUFFERS_2048 +#define USE_CERT_BUFFERS_256 +#include +#define TLS_PORT 11111 + +static void server() +{ + UINT TEST_PORT = TLS_PORT; + NX_TCP_SOCKET sockfd; + int ret; + unsigned char* cert; + int certSz; + + unsigned char* key; + int keySz; + + WOLFSSL_CTX* ctx; + WOLFSSL* ssl; + + ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()); + if (ctx == NULL) { + printf("Unable to create ctx\n"); + return; + } + +#ifndef NO_RSA + cert = server_cert_der_2048; + certSz = sizeof_server_cert_der_2048; + key = server_key_der_2048; + keySz = sizeof_server_key_der_2048; +#endif + +#if 0 + /* Use ECDSA */ + cert = serv_ecc_der_256; + certSz = sizeof_serv_ecc_der_256; + key = ecc_key_der_256; + keySz = sizeof_ecc_key_der_256; +#endif + + ret = wolfSSL_CTX_use_certificate_buffer(ctx, cert, + certSz, SSL_FILETYPE_ASN1); + if (ret != WOLFSSL_SUCCESS) { + printf("Unable to load certificate ret = %d\n", ret); + wolfSSL_CTX_free(ctx); + return; + } + + ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, key, + keySz, SSL_FILETYPE_ASN1); + if (ret != WOLFSSL_SUCCESS) { + printf("Unable to load key ret = %d\n", ret); + wolfSSL_CTX_free(ctx); + return; + } + +#ifdef WOLFSSL_TLS13 + { + int groups[3]; + int idx = 0; + + #ifdef HAVE_CURVE25519 + groups[idx++] = WOLFSSL_ECC_X25519; + #endif + #ifdef HAVE_ECC + groups[idx++] = WOLFSSL_ECC_SECP256R1; + #endif + groups[idx++] = WOLFSSL_FFDHE_2048; + + ret = wolfSSL_CTX_set_groups(ctx, groups, idx); + if (ret != WOLFSSL_SUCCESS) { + printf("Unable to set groups\n"); + } + } +#endif + printf("Waiting for connections on port %d\n", TEST_PORT); + + ret = (int)nx_tcp_socket_create(&g_ip0, &sockfd, "TLS_SERVER", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1500, NX_NULL, NX_NULL); + if (ret != NX_SUCCESS) { + printf("failed to create socket err = 0x%X\n", ret); + } + + ret = (int)nx_tcp_server_socket_listen(&g_ip0, TEST_PORT, &sockfd, + NX_MAX_LISTEN_REQUESTS, NULL); + if (ret != NX_SUCCESS) { + printf("failed to listen\n"); + } + + while (1) { + ret = (int)nx_tcp_server_socket_accept(&sockfd, NX_WAIT_FOREVER); + if (ret != NX_SUCCESS) { + printf("failed to accept with error 0x%X\n", ret); + break; + } + + ssl = wolfSSL_new(ctx); + if (ssl == NULL) { + printf("Error creating ssl\n"); + break; + } + + wolfSSL_SetIO_NetX(ssl, &sockfd, NX_WAIT_FOREVER); + + ret = wolfSSL_accept(ssl); + if (ret != WOLFSSL_SUCCESS) { + printf("Error %d with wolfssl accept\n", wolfSSL_get_error(ssl, ret)); + wolfSSL_free(ssl); + break; + } + wolfSSL_free(ssl); + + nx_tcp_socket_disconnect(&sockfd, NX_WAIT_FOREVER); + + ret = (int)nx_tcp_server_socket_unaccept(&sockfd); + if (ret != NX_SUCCESS) { + printf("failed to unaccept with error 0x%X\n", ret); + break; + } + + ret = (int)nx_tcp_server_socket_relisten(&g_ip0, TEST_PORT, &sockfd); + if (ret != NX_SUCCESS && ret != NX_CONNECTION_PENDING) { + printf("failed to relisten 0x%X\n", ret); + break; + } + } + ret = (int)nx_tcp_server_socket_unlisten(&g_ip0, TEST_PORT); + if (ret != NX_SUCCESS) { + printf("failed to unlisten\n"); + return; + } + + nx_tcp_socket_delete(&sockfd); + + wolfSSL_CTX_free(ctx); +} + +/* app entry function */ +void app_entry(void) +{ + initialise_monitor_handles(); + wolfSSL_Init(); + +#ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); +#endif + server(); + wolfSSL_Cleanup(); + printf("Server closed down\n"); + while (1) + { + tx_thread_sleep (1); + } +} diff --git a/IDE/Renesas/e2studio/DK-S7G2/include.am b/IDE/Renesas/e2studio/DK-S7G2/include.am new file mode 100644 index 000000000..739be2cab --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/include.am @@ -0,0 +1,22 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/README.md +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/user_settings.h + +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/.project +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/configuration.xml + +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.cproject +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.project +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/src/app_entry.c + +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/example_server-template/.cproject +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/example_server-template/.project +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c + +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.cproject +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/benchmark-template/.project +EXTRA_DIST+= IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c + diff --git a/IDE/Renesas/e2studio/DK-S7G2/user_settings.h b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h new file mode 100644 index 000000000..ad58cd67f --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h @@ -0,0 +1,64 @@ + +#ifndef USER_SETTINGS_H +#define USER_SETTINGS_H + +//#define DEBUG_WOLFSSL + +#define NO_MAIN_DRIVER +#define USE_CERT_BUFFERS_2048 +#define USE_CERT_BUFFERS_256 + +/* print out cycles per byte with benchmark when component r_wdt WDT is enabled */ +#define SYNERGY_CYCLE_COUNT +#define BENCH_EMBEDDED + +/* Use TRNG */ +//#define WOLFSSL_SCE +#ifndef WOLFSSL_SCE + /* use unsafe test seed if TRNG not used (not for production) */ + #define WOLFSSL_GENSEED_FORTEST +#endif + +#define HAVE_ECC +#define ALT_ECC_SIZE + +#define HAVE_CHACHA +#define HAVE_POLY1305 +#define HAVE_ONE_TIME_AUTH +#define HAVE_AESGCM + +#define USE_FAST_MATH + +#define TFM_TIMING_RESISTANT +#define WC_RSA_BLINDING +#define ECC_TIMING_RESISTANT + +#define NO_WOLFSSL_DIR + +#define HAVE_NETX +#define THREADX +#define THREADX_NO_DC_PRINTF +#define NO_WRITEV +#define SIZEOF_LONG 4 +#define SIZEOF_LONG_LONG 8 + +#if 1 + /* Optimizations */ + #define WOLFSSL_HAVE_SP_RSA + #define WOLFSSL_HAVE_SP_ECC + #define WOLFSSL_SP_ARM_CORTEX_M_ASM +#endif + +/* TLS 1.3 */ +#define WOLFSSL_TLS13 +#define HAVE_TLS_EXTENSIONS +#define HAVE_SUPPORTED_CURVES +#define HAVE_FFDHE_2048 +#define HAVE_HKDF +#define WC_RSA_PSS + +#define HAVE_CURVE25519 +#define HAVE_ED25519 +#define WOLFSSL_SHA512 + +#endif diff --git a/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.cproject b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.cproject new file mode 100644 index 000000000..67ebabc0d --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.cproject @@ -0,0 +1,392 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.project b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.project new file mode 100644 index 000000000..24fa740ee --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/.project @@ -0,0 +1,34 @@ + + + wolfcrypttest + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + com.renesas.cdt.synergy.contentgen.synergyExecutableNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + src/test + 2 + PARENT-5-PROJECT_LOC/wolfcrypt/test + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/src/app_entry.c b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/src/app_entry.c new file mode 100644 index 000000000..c9eee735e --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/src/app_entry.c @@ -0,0 +1,76 @@ +/* app_entry.c + * + * Copyright (C) 2006-2020 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 2 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 + */ + + +#include +#include "stdio.h" +extern void initialise_monitor_handles(void); + +#include +#include +#include "wolfcrypt/test/test.h" + +typedef struct func_args { + int argc; + char** argv; + int return_code; +} func_args; + +void app_entry(void) +{ + func_args args; + + args.argc = 0; + args.argv = NULL; + args.return_code = 0; + + initialise_monitor_handles(); + wolfCrypt_Init(); + +#if 1 + /* sanity check on RNG */ + printf("Doing quick sanity check on RNG\n"); + { + int i; + for (i = 0; i < 10; i++) { + int j, ret; + WC_RNG rng; + unsigned char buffer[20] = {0}; + + wc_InitRng(&rng); + ret = wc_RNG_GenerateBlock(&rng, buffer, 20); + if (ret != 0) { + printf("Error generating random block\n"); + } + for (j = 0; j < 20; j++) { + printf("%02X", buffer[j]); + } + printf("\n"); + wc_FreeRng(&rng); + } + } + printf("\n"); +#endif + + wolfcrypt_test(&args); + wolfCrypt_Cleanup(); + printf("done with wolfcrypt test, ret = %d\n", args.return_code); +} diff --git a/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/.project b/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/.project new file mode 100644 index 000000000..11745f883 --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/.project @@ -0,0 +1,49 @@ + + + wolfssl + + + + + + com.renesas.cdt.synergy.contentgen.synergyBuilder + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + com.renesas.cdt.synergy.contentgen.synergyNature + + + + src/user_settings.h + 1 + PARENT-1-PROJECT_LOC/user_settings.h + + + src/wolfcrypt + 2 + PARENT-5-PROJECT_LOC/wolfcrypt/src + + + src/wolfssl + 2 + PARENT-5-PROJECT_LOC/src + + + diff --git a/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/configuration.xml b/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/configuration.xml new file mode 100644 index 000000000..6622d9f67 --- /dev/null +++ b/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/configuration.xml @@ -0,0 +1,352 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + SSP Common Code + Renesas.Synergy.1.7.0.pack + + + Clock Generation Circuit: Provides=[CGC] + Renesas.Synergy.1.7.0.pack + + + Event Link Controller: Provides=[ELC] + Renesas.Synergy.1.7.0.pack + + + Factory MCU Information Module: Provides=[FMI] + Renesas.Synergy.1.7.0.pack + + + I/O Port: Provides=[IO Port] + Renesas.Synergy.1.7.0.pack + + + Board support package for R7FS7G27H2A01CBD + Renesas.Synergy_mcu_s7g2.1.7.0.pack + + + Board support package for S7G2 + Renesas.Synergy_mcu_s7g2.1.7.0.pack + + + Board support package for S7G2 + Renesas.Synergy_mcu_s7g2.1.7.0.pack + + + S7G2_DK Board Support Files + Renesas.Synergy_board_s7g2_dk.1.7.0.pack + + + Real Time Clock: Provides=[RTC] + Renesas.Synergy.1.7.0.pack + + + General Purpose Timer: Provides=[Timer ,GPT] + Renesas.Synergy.1.7.0.pack + + + Watchdog Timer: Provides=[WDT] + Renesas.Synergy.1.7.0.pack + + + Express Logic NetX: Provides=[NetX] , Requires=[ThreadX ,NetX Driver] + Renesas.Synergy.1.7.0.pack + + + Express Logic NetX Synergy Port: Provides=[NetX Driver] , Requires=[NetX] + Renesas.Synergy.1.7.0.pack + + + Express Logic ThreadX: Provides=[ThreadX] + Renesas.Synergy.1.7.0.pack + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IDE/include.am b/IDE/include.am index 2ea00f2de..fe260fb6a 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -21,6 +21,7 @@ include IDE/ECLIPSE/DEOS/include.am include IDE/ECLIPSE/MICRIUM/include.am include IDE/ECLIPSE/SIFIVE/include.am include IDE/mynewt/include.am +include IDE/Renesas/e2studio/DK-S7G2/include.am include IDE/Renesas/cs+/Projects/include.am include IDE/Renesas/e2studio/Projects/include.am include IDE/WICED-STUDIO/include.am diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 7a905e59c..d48395296 100755 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -618,6 +618,24 @@ static const char* bench_desc_words[][9] = { XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.2f,\n", \ (float)total_cycles / (count*s)) +#elif defined(SYNERGY_CYCLE_COUNT) + #include "hal_data.h" + static THREAD_LS_T word64 begin_cycles; + static THREAD_LS_T word64 total_cycles; + + #define INIT_CYCLE_COUNTER + #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0; + #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles; + + /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ + #define SHOW_INTEL_CYCLES(b, n, s) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \ + bench_result_words1[lng_index][2], \ + (float)total_cycles / (count*s)) + #define SHOW_INTEL_CYCLES_CSV(b, n, s) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.2f,\n", \ + (float)total_cycles / (count*s)) + #else #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES @@ -5694,6 +5712,14 @@ exit_ed_verify: return (double) ticks/TICKS_PER_SECOND; } + +#elif defined(THREADX) + #include "tx_api.h" + double current_time(int reset) + { + (void)reset; + return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND; + } #else #include diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index a9dcac362..50cfab564 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -155,6 +155,7 @@ int wc_RNG_GenerateByte(WC_RNG* rng, byte* b) #elif defined(WOLFSSL_PB) #elif defined(WOLFSSL_ZEPHYR) #elif defined(WOLFSSL_TELIT_M2MB) +#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG) #else /* include headers that may be needed to get good seed */ #include @@ -2312,8 +2313,52 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) } return ret; } - - + +#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG) + #include "hal_data.h" + + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + uint32_t ret; + uint32_t blocks; + word32 len = sz; + + ret = g_sce_trng.p_api->open(g_sce_trng.p_ctrl, g_sce_trng.p_cfg); + if (ret != SSP_SUCCESS) { + /* error opening TRNG driver */ + return -1; + } + + blocks = sz / sizeof(uint32_t); + if (blocks > 0) { + ret = g_sce_trng.p_api->read(g_sce_trng.p_ctrl, (uint32_t*)output, + blocks); + if (ret != SSP_SUCCESS) { + return -1; + } + } + + len = len - (blocks * sizeof(uint32_t)); + if (len > 0) { + uint32_t tmp; + + if (len > sizeof(uint32_t)) { + return -1; + } + ret = g_sce_trng.p_api->read(g_sce_trng.p_ctrl, (uint32_t*)tmp, 1); + if (ret != SSP_SUCCESS) { + return -1; + } + XMEMCPY(output + (blocks * sizeof(uint32_t)), (byte*)&tmp, len); + } + + ret = g_sce_trng.p_api->close(g_sce_trng.p_ctrl); + if (ret != SSP_SUCCESS) { + /* error opening TRNG driver */ + return -1; + } + return 0; + } #elif defined(CUSTOM_RAND_GENERATE_BLOCK) /* #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc * extern int myRngFunc(byte* output, word32 sz); diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index a23d4449b..2265416c8 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -82,6 +82,10 @@ #include #endif +#ifdef WOLFSSL_SCE + #include "hal_data.h" +#endif + #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) #include "rpcmem.h" #endif @@ -224,6 +228,13 @@ int wolfCrypt_Init(void) #endif #endif +#ifdef WOLFSSL_SCE + if ((ret = g_sce.p_api->open(g_sce.p_ctrl, g_sce.p_cfg)) != SSP_SUCCESS) { + WOLFSSL_MSG("Error opening SCE\n"); + return -1; /* FATAL_ERROR */ + } +#endif + #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ defined(WOLFSSL_IMX6_CAAM_BLOB) if ((ret = wc_caamInit()) != 0) { @@ -276,7 +287,9 @@ int wolfCrypt_Cleanup(void) #ifdef WOLFSSL_ASYNC_CRYPT wolfAsync_HardwareStop(); #endif - + #ifdef WOLFSSL_SCE + g_sce.p_api->close(g_sce.p_ctrl); + #endif #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ defined(WOLFSSL_IMX6_CAAM_BLOB) wc_caamFree(); diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index 9ea0a0322..d0d17a093 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -82,6 +82,8 @@ #include "FreeRTOS_Sockets.h" #elif defined(WOLFSSL_IAR_ARM) /* nothing */ + #elif defined(HAVE_NETX_BSD) + #include "nx_bsd.h" #elif defined(WOLFSSL_VXWORKS) #include #include @@ -120,6 +122,9 @@ #include #elif defined(WOLFSSL_ZEPHYR) #include + #elif defined(HAVE_NETX) + #include "nx_api.h" + #include "errno.h" #elif !defined(WOLFSSL_NO_SOCK) #include #include @@ -233,6 +238,14 @@ #define SOCKET_EPIPE EPIPE #define SOCKET_ECONNREFUSED SOCKET_ERROR #define SOCKET_ECONNABORTED SOCKET_ERROR +#elif defined(HAVE_NETX) + #define SOCKET_EWOULDBLOCK NX_NOT_CONNECTED + #define SOCKET_EAGAIN NX_NOT_CONNECTED + #define SOCKET_ECONNRESET NX_NOT_CONNECTED + #define SOCKET_EINTR NX_NOT_CONNECTED + #define SOCKET_EPIPE NX_NOT_CONNECTED + #define SOCKET_ECONNREFUSED NX_NOT_CONNECTED + #define SOCKET_ECONNABORTED NX_NOT_CONNECTED #else #define SOCKET_EWOULDBLOCK EWOULDBLOCK #define SOCKET_EAGAIN EAGAIN From 3508579f4c100d3dcd6a606fd75816027ab624ef Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 20 Jan 2020 09:33:14 -0700 Subject: [PATCH 2/7] add check on NETX duo build and return value of opening driver --- wolfcrypt/src/random.c | 2 +- wolfcrypt/src/wc_port.c | 3 ++- wolfssl/wolfio.h | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 50cfab564..056baa536 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2324,7 +2324,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) word32 len = sz; ret = g_sce_trng.p_api->open(g_sce_trng.p_ctrl, g_sce_trng.p_cfg); - if (ret != SSP_SUCCESS) { + if (ret != SSP_SUCCESS && ret != SSP_ERR_CRYPTO_ALREADY_OPEN) { /* error opening TRNG driver */ return -1; } diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 2265416c8..860422fcb 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -229,7 +229,8 @@ int wolfCrypt_Init(void) #endif #ifdef WOLFSSL_SCE - if ((ret = g_sce.p_api->open(g_sce.p_ctrl, g_sce.p_cfg)) != SSP_SUCCESS) { + ret = (int)g_sce.p_api->open(g_sce.p_ctrl, g_sce.p_cfg); + if (ret != SSP_SUCCESS && ret != SSP_ERR_CRYPTO_ALREADY_OPEN) { WOLFSSL_MSG("Error opening SCE\n"); return -1; /* FATAL_ERROR */ } diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index d0d17a093..b6487c20b 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -83,7 +83,11 @@ #elif defined(WOLFSSL_IAR_ARM) /* nothing */ #elif defined(HAVE_NETX_BSD) - #include "nx_bsd.h" + #ifdef NETX_DUO + #include "nxd_bsd.h" + #else + #include "nx_bsd.h" + #endif #elif defined(WOLFSSL_VXWORKS) #include #include From 6ec136208cdbd2d1abe4342a844e574dc426a03a Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 31 Jan 2020 14:26:04 -0800 Subject: [PATCH 3/7] add sha256 hardware acceleration --- wolfcrypt/src/sha256.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index de49e3283..31a56ed36 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -544,6 +544,42 @@ static int InitSha256(wc_Sha256* sha256) #elif defined(WOLFSSL_DEVCRYPTO_HASH) /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */ +#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH) + #include "hal_data.h" + + #ifndef WOLFSSL_SCE_SHA256_HANDLE + #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0 + #endif + + #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D)) + static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data) + { + if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, + (word32*)data, 8, sha256->digest) != SSP_SUCCESS) { + WOLFSSL_MSG("Unexpected hardware return value"); + return WC_HW_E; + } + return 0; + } + + + int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) + { + int ret = 0; + if (sha256 == NULL) + return BAD_FUNC_ARG; + + sha256->heap = heap; + + ret = InitSha256(sha256); + if (ret != 0) + return ret; + + (void)devId; + + return ret; + } + #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) From b7d772700a416efa16b690b519387cbb86fef2c6 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 4 Feb 2020 16:03:45 -0800 Subject: [PATCH 4/7] update sha256 support for endian --- wolfcrypt/src/sha256.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 31a56ed36..cc2a1a280 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -551,14 +551,29 @@ static int InitSha256(wc_Sha256* sha256) #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0 #endif + #define WC_SHA256_DIGEST_WORD_SIZE 16 #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D)) static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data) { + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + { + ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); + ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); + } + if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, - (word32*)data, 8, sha256->digest) != SSP_SUCCESS) { + (word32*)data, WC_SHA256_DIGEST_WORD_SIZE, + sha256->digest) != SSP_SUCCESS) { WOLFSSL_MSG("Unexpected hardware return value"); return WC_HW_E; } + + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + { + ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); + ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); + } + return 0; } From e75b1b5cb938e68dbce8f9104c50d485d7e70a1b Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 4 Feb 2020 16:10:20 -0800 Subject: [PATCH 5/7] add suport for AES acceleration --- IDE/Renesas/e2studio/DK-S7G2/README.md | 54 +++--- IDE/Renesas/e2studio/DK-S7G2/user_settings.h | 19 ++- wolfcrypt/src/aes.c | 165 ++++++++++++++++++- wolfcrypt/src/wc_port.c | 8 +- 4 files changed, 213 insertions(+), 33 deletions(-) diff --git a/IDE/Renesas/e2studio/DK-S7G2/README.md b/IDE/Renesas/e2studio/DK-S7G2/README.md index 4c999418e..211a054e0 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/README.md +++ b/IDE/Renesas/e2studio/DK-S7G2/README.md @@ -1,27 +1,29 @@ - -## Building wolfSSL For DK-S7G2 - -- First physically toggle the ENET1 and JTAG switch to on with the DK-S7G2 board. -- Open e2studio and set the workspace to be wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/ -- Create a Synergy library project named wolfssl "File->New->Synergy C/C++ Project", "Renesas Synergy C Library Project" then "Next", set wolfssl as the "Project Name" then "Next", set Board to "S7G2 DK" then "Next", finally select the BSP radius and click "Finish" -- Copy configuration.xml and .project from wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/ into the wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl directory -- (optional but necessary for production) Add TRNG support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > TRNG Driver on r_sce_trng". Then uncomment WOLFSSL_SCE define in wolfssl project src/user_settings.h -- Generate the changes by clicking on "Generate Project Content" -- Exclude src/wolfcrypt/port and all src/wolfcrypt/*.S and src/wolfcrypt/*.asm files from the build -- Exclude src/wolfcrypt/evp.c, src/wolfcrypt/misc.c and src/wolfssl/bio.c -- Set the Preprocessor define in wolfssl proejct to have WOLFSSL_USER_SETTINGS. Right click on wolfssl project "Properties -> C/C++ Build -> GNU ARM Cross C Compiler -> Preprocessor" add WOLFSSL_USER_SETTINGS under "Defined symbols" -- Set include to wolfssl directory. Right click on project "Properties -> C/C++Build -> GNU ARM Cross Compiler -> Includes". Add "${ProjDirPath}/../../../../.." and "${ProjDirPath}/../" -- Build wolfssl by right clicking on wolfssl project and selecting "Build Project" - -## Example Projects and Building - -- Create a new Synergy project "Renesas Synergy C Project Using Synergy Library" -- Set it to use the wolfssl library -- Copy in the .cproject, .project and source file from the template desired i.e. wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/ -- Right click on the created project and select "Build Project" - -The example_server loops looking to accept connections and closes immediatly after a successful connection was made. - -The benchmark example tries to do a TCP connection to SERVER_IP on port 11112 and a TLS connection to SERVER_IP on port 11111 then does wolfCrypt benchmark collection. - + +## Building wolfSSL For DK-S7G2 + +- First physically toggle the ENET1 and JTAG switch to on with the DK-S7G2 board. +- Open e2studio and set the workspace to be wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/ +- Create a Synergy library project named wolfssl "File->New->Synergy C/C++ Project", "Renesas Synergy C Library Project" then "Next", set wolfssl as the "Project Name" then "Next", set Board to "S7G2 DK" then "Next", finally select the BSP radius and click "Finish" +- Copy configuration.xml and .project from wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/ into the wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl directory +- (optional but necessary for production) Add TRNG support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > TRNG Driver on r_sce_trng". Then comment out WOLFSSL_SCE_NO_TRNG define in wolfssl project src/user_settings.h +- (optional SHA acceleration) Add HASH support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > HASH Driver on r_sce_hash". Then uncomment WOLFSSL_SCE_NO_HASH define in wolfssl project src/user_settings.h +- (optional AES acceleration) Add the stacks for AES128, AES192, and AES256. Click on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > AES Driver on r_sce_aes". Add three one for each key size and rename them to g_sce_aes_256, g_sce_aes_192, and g_sce_aes_128. Changing each to ECB chaining mode and the key length that matches the name. +- Generate the changes by clicking on "Generate Project Content" +- Exclude src/wolfcrypt/port and all src/wolfcrypt/*.S and src/wolfcrypt/*.asm files from the build +- Exclude src/wolfcrypt/evp.c, src/wolfcrypt/misc.c and src/wolfssl/bio.c +- Set the Preprocessor define in wolfssl proejct to have WOLFSSL_USER_SETTINGS. Right click on wolfssl project "Properties -> C/C++ Build -> GNU ARM Cross C Compiler -> Preprocessor" add WOLFSSL_USER_SETTINGS under "Defined symbols" +- Set include to wolfssl directory. Right click on project "Properties -> C/C++Build -> GNU ARM Cross Compiler -> Includes". Add "${ProjDirPath}/../../../../.." and "${ProjDirPath}/../" +- Build wolfssl by right clicking on wolfssl project and selecting "Build Project" + +## Example Projects and Building + +- Create a new Synergy project "Renesas Synergy C Project Using Synergy Library" +- Set it to use the wolfssl library +- Copy in the .cproject, .project and source file from the template desired i.e. wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/ +- Right click on the created project and select "Build Project" + +The example_server loops looking to accept connections and closes immediatly after a successful connection was made. + +The benchmark example tries to do a TCP connection to SERVER_IP on port 11112 and a TLS connection to SERVER_IP on port 11111 then does wolfCrypt benchmark collection. + The wolfcryptest runs through all of the unit tests from wolfcrypt/test/test.c \ No newline at end of file diff --git a/IDE/Renesas/e2studio/DK-S7G2/user_settings.h b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h index ad58cd67f..cdebbd445 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/user_settings.h +++ b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h @@ -12,9 +12,19 @@ #define SYNERGY_CYCLE_COUNT #define BENCH_EMBEDDED -/* Use TRNG */ -//#define WOLFSSL_SCE -#ifndef WOLFSSL_SCE +/* Use turn on all SCE acceleration */ +#define WOLFSSL_SCE + +/* Used to turn off TRNG */ +#define WOLFSSL_SCE_NO_TRNG + +/* Used to turn off AES hardware acc. */ +#define WOLFSSL_SCE_NO_AES + +/* Used to turn off HASH hardware acc. */ +#define WOLFSSL_SCE_NO_HASH + +#if defined(WOLFSSL_SCE_NO_TRNG) /* use unsafe test seed if TRNG not used (not for production) */ #define WOLFSSL_GENSEED_FORTEST #endif @@ -27,6 +37,9 @@ #define HAVE_ONE_TIME_AUTH #define HAVE_AESGCM +#define HAVE_AES_ECB +#define WOLFSSL_AES_DIRECT + #define USE_FAST_MATH #define TFM_TIMING_RESISTANT diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 946fae4fe..10a46ce12 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -772,6 +772,132 @@ #elif defined(WOLFSSL_AFALG) #elif defined(WOLFSSL_DEVCRYPTO_AES) + +#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) + #include "hal_data.h" + + #ifndef WOLFSSL_SCE_AES256_HANDLE + #define WOLFSSL_SCE_AES256_HANDLE g_sce_aes_256 + #endif + + #ifndef WOLFSSL_SCE_AES192_HANDLE + #define WOLFSSL_SCE_AES192_HANDLE g_sce_aes_192 + #endif + + #ifndef WOLFSSL_SCE_AES128_HANDLE + #define WOLFSSL_SCE_AES128_HANDLE g_sce_aes_128 + #endif + + static int AES_ECB_encrypt(Aes* aes, const byte* inBlock, byte* outBlock, int sz) + { + uint32_t ret; + + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); + } + + switch (aes->keylen) { + #ifdef WOLFSSL_AES_128 + case AES_128_KEY_SIZE: + ret = WOLFSSL_SCE_AES128_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), + (word32*)inBlock, (word32*)outBlock); + break; + #endif + #ifdef WOLFSSL_AES_192 + case AES_192_KEY_SIZE: + ret = WOLFSSL_SCE_AES192_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), + (word32*)inBlock, (word32*)outBlock); + break; + #endif + #ifdef WOLFSSL_AES_256 + case AES_256_KEY_SIZE: + ret = WOLFSSL_SCE_AES256_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), + (word32*)inBlock, (word32*)outBlock); + break; + #endif + default: + WOLFSSL_MSG("Unknown key size"); + return BAD_FUNC_ARG; + } + + if (ret != SSP_SUCCESS) { + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + return WC_HW_E; + } + + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); + if (inBlock != outBlock) { + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + } + } + return 0; + } + + #if defined(HAVE_AES_DECRYPT) + static int AES_ECB_decrypt(Aes* aes, const byte* inBlock, byte* outBlock, int sz) + { + uint32_t ret; + + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); + } + + switch (aes->keylen) { + #ifdef WOLFSSL_AES_128 + case AES_128_KEY_SIZE: + ret = WOLFSSL_SCE_AES128_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + break; + #endif + #ifdef WOLFSSL_AES_192 + case AES_192_KEY_SIZE: + ret = WOLFSSL_SCE_AES192_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + break; + #endif + #ifdef WOLFSSL_AES_256 + case AES_256_KEY_SIZE: + ret = WOLFSSL_SCE_AES256_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + break; + #endif + default: + WOLFSSL_MSG("Unknown key size"); + return BAD_FUNC_ARG; + } + if (ret != SSP_SUCCESS) { + return WC_HW_E; + } + + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); + if (inBlock != outBlock) { + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + } + } + + return 0; + } + + #endif + + #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT) + static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock) + { + return AES_ECB_encrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE); + } + #endif + + #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT) + static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) + { + return AES_ECB_decrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE); + } + #endif #else /* using wolfCrypt software implementation */ @@ -1455,6 +1581,10 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock) #endif } #endif +#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) + AES_ECB_encrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE); + return; +#endif /* * map byte array block to cipher state @@ -1653,6 +1783,9 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) #endif } #endif /* WOLFSSL_AESNI */ +#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) + return AES_ECB_decrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE); +#endif /* * map byte array block to cipher state @@ -2280,6 +2413,13 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) #endif /* HAVE_AES_DECRYPT */ #endif /* NEED_AES_TABLES */ +#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) + XMEMCPY((byte*)aes->key, userKey, keylen); + if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + ByteReverseWords(aes->key, aes->key, 32); + } +#endif + return wc_AesSetIV(aes, iv); } @@ -5705,7 +5845,7 @@ int AES_GCM_encrypt_C(Aes* aes, byte* out, const byte* in, word32 sz, #if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT) /* some hardware acceleration can gain performance from doing AES encryption * of the whole buffer at once */ - if (c != p) { /* can not handle inline encryption */ + if (c != p && blocks > 0) { /* can not handle inline encryption */ while (blocks--) { IncrementGcmCounter(ctr); XMEMCPY(c, ctr, AES_BLOCK_SIZE); @@ -6157,7 +6297,7 @@ int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz, #if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT) /* some hardware acceleration can gain performance from doing AES encryption * of the whole buffer at once */ - if (c != p) { /* can not handle inline decryption */ + if (c != p && blocks > 0) { /* can not handle inline decryption */ while (blocks--) { IncrementGcmCounter(ctr); XMEMCPY(p, ctr, AES_BLOCK_SIZE); @@ -6166,6 +6306,7 @@ int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz, /* reset number of blocks and then do encryption */ blocks = sz / AES_BLOCK_SIZE; + wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks); xorbuf(out, c, AES_BLOCK_SIZE * blocks); c += AES_BLOCK_SIZE * blocks; @@ -7034,6 +7175,26 @@ int wc_AesGetKeySize(Aes* aes, word32* keySize) #elif defined(WOLFSSL_DEVCRYPTO_AES) /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */ +#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) + +/* Software AES - ECB */ +int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) +{ + if ((in == NULL) || (out == NULL) || (aes == NULL)) + return BAD_FUNC_ARG; + + return AES_ECB_encrypt(aes, in, out, sz); +} + + +int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) +{ + if ((in == NULL) || (out == NULL) || (aes == NULL)) + return BAD_FUNC_ARG; + + return AES_ECB_decrypt(aes, in, out, sz); +} + #else /* Software AES - ECB */ diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 860422fcb..d8279886f 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -230,8 +230,12 @@ int wolfCrypt_Init(void) #ifdef WOLFSSL_SCE ret = (int)g_sce.p_api->open(g_sce.p_ctrl, g_sce.p_cfg); - if (ret != SSP_SUCCESS && ret != SSP_ERR_CRYPTO_ALREADY_OPEN) { - WOLFSSL_MSG("Error opening SCE\n"); + if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) { + WOLFSSL_MSG("SCE already open"); + ret = 0; + } + if (ret != SSP_SUCCESS) { + WOLFSSL_MSG("Error opening SCE"); return -1; /* FATAL_ERROR */ } #endif From 61a5fe31088c80b39e925ad9e1c67b011a705759 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Thu, 6 Feb 2020 09:20:07 -0800 Subject: [PATCH 6/7] add macro for trng and gce driver names --- IDE/Renesas/e2studio/DK-S7G2/README.md | 80 ++++++++++++++++---------- wolfcrypt/src/aes.c | 10 ++-- wolfcrypt/src/random.c | 16 ++++-- wolfcrypt/src/sha256.c | 4 +- wolfcrypt/src/wc_port.c | 5 +- wolfssl/wolfcrypt/wc_port.h | 6 ++ 6 files changed, 78 insertions(+), 43 deletions(-) diff --git a/IDE/Renesas/e2studio/DK-S7G2/README.md b/IDE/Renesas/e2studio/DK-S7G2/README.md index 211a054e0..a95883c31 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/README.md +++ b/IDE/Renesas/e2studio/DK-S7G2/README.md @@ -1,29 +1,51 @@ - -## Building wolfSSL For DK-S7G2 - -- First physically toggle the ENET1 and JTAG switch to on with the DK-S7G2 board. -- Open e2studio and set the workspace to be wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/ -- Create a Synergy library project named wolfssl "File->New->Synergy C/C++ Project", "Renesas Synergy C Library Project" then "Next", set wolfssl as the "Project Name" then "Next", set Board to "S7G2 DK" then "Next", finally select the BSP radius and click "Finish" -- Copy configuration.xml and .project from wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/ into the wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl directory -- (optional but necessary for production) Add TRNG support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > TRNG Driver on r_sce_trng". Then comment out WOLFSSL_SCE_NO_TRNG define in wolfssl project src/user_settings.h -- (optional SHA acceleration) Add HASH support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > HASH Driver on r_sce_hash". Then uncomment WOLFSSL_SCE_NO_HASH define in wolfssl project src/user_settings.h -- (optional AES acceleration) Add the stacks for AES128, AES192, and AES256. Click on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > AES Driver on r_sce_aes". Add three one for each key size and rename them to g_sce_aes_256, g_sce_aes_192, and g_sce_aes_128. Changing each to ECB chaining mode and the key length that matches the name. -- Generate the changes by clicking on "Generate Project Content" -- Exclude src/wolfcrypt/port and all src/wolfcrypt/*.S and src/wolfcrypt/*.asm files from the build -- Exclude src/wolfcrypt/evp.c, src/wolfcrypt/misc.c and src/wolfssl/bio.c -- Set the Preprocessor define in wolfssl proejct to have WOLFSSL_USER_SETTINGS. Right click on wolfssl project "Properties -> C/C++ Build -> GNU ARM Cross C Compiler -> Preprocessor" add WOLFSSL_USER_SETTINGS under "Defined symbols" -- Set include to wolfssl directory. Right click on project "Properties -> C/C++Build -> GNU ARM Cross Compiler -> Includes". Add "${ProjDirPath}/../../../../.." and "${ProjDirPath}/../" -- Build wolfssl by right clicking on wolfssl project and selecting "Build Project" - -## Example Projects and Building - -- Create a new Synergy project "Renesas Synergy C Project Using Synergy Library" -- Set it to use the wolfssl library -- Copy in the .cproject, .project and source file from the template desired i.e. wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/ -- Right click on the created project and select "Build Project" - -The example_server loops looking to accept connections and closes immediatly after a successful connection was made. - -The benchmark example tries to do a TCP connection to SERVER_IP on port 11112 and a TLS connection to SERVER_IP on port 11111 then does wolfCrypt benchmark collection. - -The wolfcryptest runs through all of the unit tests from wolfcrypt/test/test.c \ No newline at end of file + +## Building wolfSSL For DK-S7G2 + +- First physically toggle the ENET1 and JTAG switch to on with the DK-S7G2 board. +- Open e2studio and set the workspace to be wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/ +- Create a Synergy library project named wolfssl "File->New->Synergy C/C++ Project", "Renesas Synergy C Library Project" then "Next", set wolfssl as the "Project Name" then "Next", set Board to "S7G2 DK" then "Next", finally select the BSP radius and click "Finish" +- Copy configuration.xml and .project from wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl-template-project/ into the wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfssl directory +- (optional but necessary for production) Add TRNG support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > TRNG Driver on r_sce_trng". Then comment out WOLFSSL_SCE_NO_TRNG define in wolfssl project src/user_settings.h +- (optional SHA acceleration) Add HASH support by clicking on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > HASH Driver on r_sce_hash". Then uncomment WOLFSSL_SCE_NO_HASH define in wolfssl project src/user_settings.h +- (optional AES acceleration) Add the stacks for AES128, AES192, and AES256. Click on Threads tab and highlight HAL/Common click "New Stack > Driver > Crypto > AES Driver on r_sce_aes". Add three one for each key size and rename them to g_sce_aes_256, g_sce_aes_192, and g_sce_aes_128. Changing each to ECB chaining mode and the key length that matches the name. +- Generate the changes by clicking on "Generate Project Content" +- Exclude src/wolfcrypt/port and all src/wolfcrypt/*.S and src/wolfcrypt/*.asm files from the build +- Exclude src/wolfcrypt/evp.c, src/wolfcrypt/misc.c and src/wolfssl/bio.c +- Set the Preprocessor define in wolfssl proejct to have WOLFSSL_USER_SETTINGS. Right click on wolfssl project "Properties -> C/C++ Build -> GNU ARM Cross C Compiler -> Preprocessor" add WOLFSSL_USER_SETTINGS under "Defined symbols" +- Set include to wolfssl directory. Right click on project "Properties -> C/C++Build -> GNU ARM Cross Compiler -> Includes". Add "${ProjDirPath}/../../../../.." and "${ProjDirPath}/../" +- Build wolfssl by right clicking on wolfssl project and selecting "Build Project" + +## Example Projects and Building + +- Create a new Synergy project "Renesas Synergy C Project Using Synergy Library" +- Set it to use the wolfssl library +- Copy in the .cproject, .project and source file from the template desired i.e. wolfssl-X.X.X/IDE/Renesas/e2studio/DK-S7G2/wolfcrypttest-template/ +- Right click on the created project and select "Build Project" + +The example_server loops looking to accept connections and closes immediatly after a successful connection was made. + +The benchmark example tries to do a TCP connection to SERVER_IP on port 11112 and a TLS connection to SERVER_IP on port 11111 then does wolfCrypt benchmark collection. + +The wolfcryptest runs through all of the unit tests from wolfcrypt/test/test.c + +## Advanced Overriding Driver Name +Defaults are set for when accessing the driver but the default names may not always work for an existing project. These are the macros and their defaults that could be mapped to other driver names: + +/* For main SCE open and close */ +WOLFSSL_SCE_GSCE_HANDLE g_sce + +/* For AES operations */ +WOLFSSL_SCE_AES256_HANDLE g_sce_aes_256 +WOLFSSL_SCE_AES192_HANDLE g_sce_aes_192 +WOLFSSL_SCE_AES128_HANDLE g_sce_aes_128 + +/* HASH operations */ +WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0 + +/* TRNG access */ +WOLFSSL_SCE_TRNG_HANDLE g_sce_trng + + + +An example of remapping a driver name would be the following added to a wolfSSL user_settings.h file: +#define WOFSSL_SCE_SHA256_HANDLE my_sce_hash_driver diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 10a46ce12..7017a0bb3 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -792,7 +792,7 @@ { uint32_t ret; - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } @@ -828,7 +828,7 @@ return WC_HW_E; } - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); if (inBlock != outBlock) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ @@ -842,7 +842,7 @@ { uint32_t ret; - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } @@ -873,7 +873,7 @@ return WC_HW_E; } - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); if (inBlock != outBlock) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ @@ -2415,7 +2415,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) #if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES) XMEMCPY((byte*)aes->key, userKey, keylen); - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords(aes->key, aes->key, 32); } #endif diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 056baa536..3e014df76 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2317,13 +2317,18 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG) #include "hal_data.h" + #ifndef WOLFSSL_SCE_TRNG_HANDLE + #define WOLFSSL_SCE_TRNG_HANDLE g_sce_trng + #endif + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { uint32_t ret; uint32_t blocks; word32 len = sz; - ret = g_sce_trng.p_api->open(g_sce_trng.p_ctrl, g_sce_trng.p_cfg); + ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->open(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, + WOLFSSL_SCE_TRNG_HANDLE.p_cfg); if (ret != SSP_SUCCESS && ret != SSP_ERR_CRYPTO_ALREADY_OPEN) { /* error opening TRNG driver */ return -1; @@ -2331,8 +2336,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) blocks = sz / sizeof(uint32_t); if (blocks > 0) { - ret = g_sce_trng.p_api->read(g_sce_trng.p_ctrl, (uint32_t*)output, - blocks); + ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, + (uint32_t*)output, blocks); if (ret != SSP_SUCCESS) { return -1; } @@ -2345,14 +2350,15 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) if (len > sizeof(uint32_t)) { return -1; } - ret = g_sce_trng.p_api->read(g_sce_trng.p_ctrl, (uint32_t*)tmp, 1); + ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, + (uint32_t*)tmp, 1); if (ret != SSP_SUCCESS) { return -1; } XMEMCPY(output + (blocks * sizeof(uint32_t)), (byte*)&tmp, len); } - ret = g_sce_trng.p_api->close(g_sce_trng.p_ctrl); + ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->close(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl); if (ret != SSP_SUCCESS) { /* error opening TRNG driver */ return -1; diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index cc2a1a280..7fe3b4f40 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -555,7 +555,7 @@ static int InitSha256(wc_Sha256* sha256) #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D)) static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data) { - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) { ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); @@ -568,7 +568,7 @@ static int InitSha256(wc_Sha256* sha256) return WC_HW_E; } - if (g_sce.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) { ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index d8279886f..f32ed9076 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -229,7 +229,8 @@ int wolfCrypt_Init(void) #endif #ifdef WOLFSSL_SCE - ret = (int)g_sce.p_api->open(g_sce.p_ctrl, g_sce.p_cfg); + ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, + WOLFSSL_SCE_GSCE_HANDLE.p_cfg); if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) { WOLFSSL_MSG("SCE already open"); ret = 0; @@ -293,7 +294,7 @@ int wolfCrypt_Cleanup(void) wolfAsync_HardwareStop(); #endif #ifdef WOLFSSL_SCE - g_sce.p_api->close(g_sce.p_ctrl); + WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl); #endif #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ defined(WOLFSSL_IMX6_CAAM_BLOB) diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 2d3f8900b..f2dc702b2 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -767,6 +767,12 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); WOLFSSL_LOCAL void wolfSSL_CleanupHandle(); #endif +#ifdef WOLFSSL_SCE + #ifndef WOLFSSL_SCE_GSCE_HANDLE + #define WOLFSSL_SCE_GSCE_HANDLE g_sce + #endif +#endif + #ifdef __cplusplus } /* extern "C" */ #endif From 0814f61b11f3e493df744b59e826d37e0c898359 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 12 Feb 2020 10:31:34 -0700 Subject: [PATCH 7/7] fix code formating and turn on HW acc. by default --- IDE/Renesas/e2studio/DK-S7G2/README.md | 3 +- .../benchmark-template/src/app_entry.c | 17 +++-- .../example_server-template/src/app_entry.c | 3 +- IDE/Renesas/e2studio/DK-S7G2/user_settings.h | 6 +- wolfcrypt/src/aes.c | 70 ++++++++++++------- wolfcrypt/src/sha256.c | 24 ++++--- wolfcrypt/src/wc_port.c | 4 +- 7 files changed, 79 insertions(+), 48 deletions(-) diff --git a/IDE/Renesas/e2studio/DK-S7G2/README.md b/IDE/Renesas/e2studio/DK-S7G2/README.md index a95883c31..1945d7b94 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/README.md +++ b/IDE/Renesas/e2studio/DK-S7G2/README.md @@ -31,6 +31,7 @@ The wolfcryptest runs through all of the unit tests from wolfcrypt/test/test.c ## Advanced Overriding Driver Name Defaults are set for when accessing the driver but the default names may not always work for an existing project. These are the macros and their defaults that could be mapped to other driver names: +``` /* For main SCE open and close */ WOLFSSL_SCE_GSCE_HANDLE g_sce @@ -44,7 +45,7 @@ WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0 /* TRNG access */ WOLFSSL_SCE_TRNG_HANDLE g_sce_trng - +``` An example of remapping a driver name would be the following added to a wolfSSL user_settings.h file: diff --git a/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c index 1bcbc55ee..23389a396 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c +++ b/IDE/Renesas/e2studio/DK-S7G2/benchmark-template/src/app_entry.c @@ -71,7 +71,7 @@ static void benchmark_TLS(int version, char* suites, int group) case 3: method = wolfTLSv1_3_client_method(); break; #endif default: - printf("Unknown TLS version (Check if wolfSSL was built with it supported)\n"); + printf("Unknown TLS version (Check if built with it supported)\n"); return; } @@ -136,13 +136,15 @@ static void benchmark_TLS(int version, char* suites, int group) } for (i = 0; i < CONNECTION_TIMES; i++) { - ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, NX_WAIT_FOREVER); + ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, + NX_WAIT_FOREVER); if (ret != NX_SUCCESS) { printf("failed to bind socket\n"); return; } - ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, NX_WAIT_FOREVER); + ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, + NX_WAIT_FOREVER); if (ret != NX_SUCCESS) { printf("failed to connect with error 0x%X\n", ret); return; @@ -208,7 +210,8 @@ static void benchmark_TCP() NX_PACKET* response; printf("Pinging server to see if up .. "); fflush(stdout); - ret = (int)nx_icmp_ping(&g_ip0, TEST_IP, "Hello", strlen("Hello"), &response, 2000); + ret = (int)nx_icmp_ping(&g_ip0, TEST_IP, "Hello", strlen("Hello"), + &response, 2000); if (ret != NX_SUCCESS) { printf("Unable to ping server, error = 0x%X\n", ret); return; @@ -228,13 +231,15 @@ static void benchmark_TCP() } for (i = 0; i < CONNECTION_TIMES; i++) { - ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, NX_WAIT_FOREVER); + ret = (int)nx_tcp_client_socket_bind(&sockfd, NX_ANY_PORT, + NX_WAIT_FOREVER); if (ret != NX_SUCCESS) { printf("failed to bind socket\n"); return; } - ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, NX_WAIT_FOREVER); + ret = (int)nx_tcp_client_socket_connect(&sockfd, TEST_IP, TEST_PORT, + NX_WAIT_FOREVER); if (ret != NX_SUCCESS) { printf("failed to connect with error 0x%X\n", ret); return; diff --git a/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c index 92a32d8d7..a83322322 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c +++ b/IDE/Renesas/e2studio/DK-S7G2/example_server-template/src/app_entry.c @@ -103,7 +103,8 @@ static void server() #endif printf("Waiting for connections on port %d\n", TEST_PORT); - ret = (int)nx_tcp_socket_create(&g_ip0, &sockfd, "TLS_SERVER", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1500, NX_NULL, NX_NULL); + ret = (int)nx_tcp_socket_create(&g_ip0, &sockfd, "TLS_SERVER", NX_IP_NORMAL, + NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1500, NX_NULL, NX_NULL); if (ret != NX_SUCCESS) { printf("failed to create socket err = 0x%X\n", ret); } diff --git a/IDE/Renesas/e2studio/DK-S7G2/user_settings.h b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h index cdebbd445..146f7020b 100644 --- a/IDE/Renesas/e2studio/DK-S7G2/user_settings.h +++ b/IDE/Renesas/e2studio/DK-S7G2/user_settings.h @@ -16,13 +16,13 @@ #define WOLFSSL_SCE /* Used to turn off TRNG */ -#define WOLFSSL_SCE_NO_TRNG +/* #define WOLFSSL_SCE_NO_TRNG */ /* Used to turn off AES hardware acc. */ -#define WOLFSSL_SCE_NO_AES +/* #define WOLFSSL_SCE_NO_AES */ /* Used to turn off HASH hardware acc. */ -#define WOLFSSL_SCE_NO_HASH +/* #define WOLFSSL_SCE_NO_HASH */ #if defined(WOLFSSL_SCE_NO_TRNG) /* use unsafe test seed if TRNG not used (not for production) */ diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 7017a0bb3..caeb2309f 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -788,34 +788,39 @@ #define WOLFSSL_SCE_AES128_HANDLE g_sce_aes_128 #endif - static int AES_ECB_encrypt(Aes* aes, const byte* inBlock, byte* outBlock, int sz) + static int AES_ECB_encrypt(Aes* aes, const byte* inBlock, byte* outBlock, + int sz) { uint32_t ret; - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } switch (aes->keylen) { #ifdef WOLFSSL_AES_128 case AES_128_KEY_SIZE: - ret = WOLFSSL_SCE_AES128_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, - NULL, (sz / sizeof(word32)), - (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES128_HANDLE.p_api->encrypt( + WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif #ifdef WOLFSSL_AES_192 case AES_192_KEY_SIZE: - ret = WOLFSSL_SCE_AES192_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, - NULL, (sz / sizeof(word32)), - (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES192_HANDLE.p_api->encrypt( + WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif #ifdef WOLFSSL_AES_256 case AES_256_KEY_SIZE: - ret = WOLFSSL_SCE_AES256_HANDLE.p_api->encrypt(WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, - NULL, (sz / sizeof(word32)), - (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES256_HANDLE.p_api->encrypt( + WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, + NULL, (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif default: @@ -824,45 +829,56 @@ } if (ret != SSP_SUCCESS) { - ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + /* revert input */ + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); return WC_HW_E; } - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); if (inBlock != outBlock) { - ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + /* revert input */ + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } } return 0; } #if defined(HAVE_AES_DECRYPT) - static int AES_ECB_decrypt(Aes* aes, const byte* inBlock, byte* outBlock, int sz) + static int AES_ECB_decrypt(Aes* aes, const byte* inBlock, byte* outBlock, + int sz) { uint32_t ret; - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } switch (aes->keylen) { #ifdef WOLFSSL_AES_128 case AES_128_KEY_SIZE: - ret = WOLFSSL_SCE_AES128_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, aes->reg, - (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES128_HANDLE.p_api->decrypt( + WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif #ifdef WOLFSSL_AES_192 case AES_192_KEY_SIZE: - ret = WOLFSSL_SCE_AES192_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, aes->reg, - (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES192_HANDLE.p_api->decrypt( + WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif #ifdef WOLFSSL_AES_256 case AES_256_KEY_SIZE: - ret = WOLFSSL_SCE_AES256_HANDLE.p_api->decrypt(WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, aes->reg, - (sz / sizeof(word32)), (word32*)inBlock, (word32*)outBlock); + ret = WOLFSSL_SCE_AES256_HANDLE.p_api->decrypt( + WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, aes->reg, + (sz / sizeof(word32)), (word32*)inBlock, + (word32*)outBlock); break; #endif default: @@ -873,10 +889,12 @@ return WC_HW_E; } - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) { + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_BIG) { ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz); if (inBlock != outBlock) { - ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); /* revert input*/ + /* revert input */ + ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz); } } @@ -1559,8 +1577,8 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock) tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN)); XMEMCPY(tmp_align, inBlock, AES_BLOCK_SIZE); - AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE, (byte*)aes->key, - aes->rounds); + AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE, + (byte*)aes->key, aes->rounds); XMEMCPY(outBlock, tmp_align, AES_BLOCK_SIZE); XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER); return; diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 7fe3b4f40..eb0911b01 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -555,23 +555,29 @@ static int InitSha256(wc_Sha256* sha256) #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D)) static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data) { - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_LITTLE) { - ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); - ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); + ByteReverseWords((word32*)data, (word32*)data, + WC_SHA256_BLOCK_SIZE); + ByteReverseWords(sha256->digest, sha256->digest, + WC_SHA256_DIGEST_SIZE); } - if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, - (word32*)data, WC_SHA256_DIGEST_WORD_SIZE, - sha256->digest) != SSP_SUCCESS) { + if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate( + WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data, + WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){ WOLFSSL_MSG("Unexpected hardware return value"); return WC_HW_E; } - if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_LITTLE) + if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == + CRYPTO_WORD_ENDIAN_LITTLE) { - ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE); - ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE); + ByteReverseWords((word32*)data, (word32*)data, + WC_SHA256_BLOCK_SIZE); + ByteReverseWords(sha256->digest, sha256->digest, + WC_SHA256_DIGEST_SIZE); } return 0; diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index f32ed9076..d0d6bdf16 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -229,8 +229,8 @@ int wolfCrypt_Init(void) #endif #ifdef WOLFSSL_SCE - ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, - WOLFSSL_SCE_GSCE_HANDLE.p_cfg); + ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open( + WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg); if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) { WOLFSSL_MSG("SCE already open"); ret = 0;