From bc09f4bd30c361b616df04712d6f50899be1759b Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Mon, 5 Nov 2018 01:54:34 +0900 Subject: [PATCH] Porting wolfssl into ESP-IDF development framework --- IDE/Espressif/ESP-IDF/README.md | 33 ++++ .../examples/wolfssl_benchmark/CMakeLists.txt | 6 + .../examples/wolfssl_benchmark/Makefile | 11 ++ .../examples/wolfssl_benchmark/README.md | 14 ++ .../wolfssl_benchmark/main/Kconfig.projbuild | 29 +++ .../wolfssl_benchmark/main/component.mk | 8 + .../examples/wolfssl_benchmark/main/helper.c | 80 +++++++++ .../main/include/user_settings.h | 51 ++++++ .../wolfssl_benchmark/sdkconfig.defaults | 4 + .../examples/wolfssl_client/CMakeLists.txt | 6 + .../ESP-IDF/examples/wolfssl_client/Makefile | 11 ++ .../ESP-IDF/examples/wolfssl_client/README.md | 19 ++ .../wolfssl_client/main/Kconfig.projbuild | 21 +++ .../examples/wolfssl_client/main/client-tls.c | 151 ++++++++++++++++ .../examples/wolfssl_client/main/component.mk | 8 + .../main/include/user_settings.h | 51 ++++++ .../main/include/wifi_connect.h | 38 ++++ .../wolfssl_client/main/wifi_connect.c | 146 +++++++++++++++ .../examples/wolfssl_server/CMakeLists.txt | 7 + .../ESP-IDF/examples/wolfssl_server/Makefile | 11 ++ .../ESP-IDF/examples/wolfssl_server/README.md | 19 ++ .../wolfssl_server/main/Kconfig.projbuild | 15 ++ .../examples/wolfssl_server/main/component.mk | 3 + .../main/include/user_settings.h | 51 ++++++ .../main/include/wifi_connect.h | 37 ++++ .../examples/wolfssl_server/main/server-tls.c | 170 ++++++++++++++++++ .../wolfssl_server/main/wifi_connect.c | 143 +++++++++++++++ .../examples/wolfssl_test/CMakeLists.txt | 6 + .../ESP-IDF/examples/wolfssl_test/Makefile | 11 ++ .../ESP-IDF/examples/wolfssl_test/README.md | 10 ++ .../examples/wolfssl_test/main/component.mk | 3 + .../wolfssl_test/main/include/user_settings.h | 51 ++++++ .../examples/wolfssl_test/sdkconfig.defaults | 2 + IDE/Espressif/ESP-IDF/libs/CMakeLists.txt | 79 ++++++++ IDE/Espressif/ESP-IDF/libs/component.mk | 13 ++ IDE/Espressif/ESP-IDF/setup.sh | 106 +++++++++++ IDE/include.am | 2 +- wolfcrypt/benchmark/benchmark.c | 17 +- wolfcrypt/src/logging.c | 6 + wolfcrypt/src/random.c | 16 ++ wolfcrypt/src/rsa.c | 2 +- wolfcrypt/test/test.c | 25 ++- wolfssl/wolfcrypt/settings.h | 26 ++- 43 files changed, 1508 insertions(+), 10 deletions(-) create mode 100644 IDE/Espressif/ESP-IDF/README.md create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/CMakeLists.txt create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/Makefile create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/README.md create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/Kconfig.projbuild create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/component.mk create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/helper.c create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/include/user_settings.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/sdkconfig.defaults create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/CMakeLists.txt create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/Makefile create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/README.md create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/Kconfig.projbuild create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/client-tls.c create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/component.mk create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/user_settings.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/wifi_connect.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/wifi_connect.c create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/CMakeLists.txt create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/Makefile create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/README.md create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/Kconfig.projbuild create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/component.mk create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/user_settings.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/wifi_connect.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/server-tls.c create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/wifi_connect.c create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/CMakeLists.txt create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/Makefile create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/README.md create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/component.mk create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/include/user_settings.h create mode 100644 IDE/Espressif/ESP-IDF/examples/wolfssl_test/sdkconfig.defaults create mode 100644 IDE/Espressif/ESP-IDF/libs/CMakeLists.txt create mode 100644 IDE/Espressif/ESP-IDF/libs/component.mk create mode 100755 IDE/Espressif/ESP-IDF/setup.sh diff --git a/IDE/Espressif/ESP-IDF/README.md b/IDE/Espressif/ESP-IDF/README.md new file mode 100644 index 000000000..f96fbff10 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/README.md @@ -0,0 +1,33 @@ +# ESP-IDF port +## Overview + ESP-IDF development framework with wolfSSL by setting *WOLFSSL_ESPIDF* definition + + Including the following examples: + simple tls_client/server + crypt test + crypt benchmark + + The *user_settings.h* file enables some of the hardened settings. + +## Requirements + 1. ESP-IDF development framework + [https://docs.espressif.com/projects/esp-idf/en/latest/get-started/] + Note: This expects to use Linux version. + +## Setup + 1. Run *setup.sh* to deploy files into ESP-IDF tree + 2. Find Wolfssl files at /path/to/esp-idf/components/wolfssl/ + 3. Find Example programs under /path/to/esp-idf/examples/protocols/wolfssl_xxx + 4. Uncomment out #define WOLFSSL_ESPIDF in /path/to/wolfssl/wolfssl/wolfcrypt/settings.h + Uncomment out #define WOLFSSL_ESPWROOM32 in /path/to/wolfssl/wolfssl/wolfcrypt/settings.h + +## Configuration + 1. The *user_settings.h* for each example can be found in /path/to/examples/protocols/wolfssl_xxx/main/include/user_settings.h + +## Build examples + 1. See README in each example folder + +## Support + For question please email [support@wolfssl.com] + + Note: This is tested with "Ubuntu 18.04.1 LTS" and ESP32-WROOM-32. diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/CMakeLists.txt b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/CMakeLists.txt new file mode 100644 index 000000000..98c19f5b3 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wolfssl_benchmark) diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/Makefile b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/Makefile new file mode 100644 index 000000000..dbbe9edb4 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/Makefile @@ -0,0 +1,11 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wolfssl_benchmark + +CFLAGS += -DWOLFSSL_USER_SETTINGS + +include $(IDF_PATH)/make/project.mk + diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/README.md b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/README.md new file mode 100644 index 000000000..7581e8bce --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/README.md @@ -0,0 +1,14 @@ +#wolfSSL Example + +The Example contains of wolfSSL benchmark program. + +1. "make menuconfig" to configure the program. + 1-1. Example Configuration -> + BENCH_ARG : argument that you want to use. Default is "-lng 0" + The list of argument can be find in help. + +When you want to run the benchmark program +1. "make flash" to compile and load the firmware +2. "make monitor" to see the message + +See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/Kconfig.projbuild b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/Kconfig.projbuild new file mode 100644 index 000000000..8fd12d389 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/Kconfig.projbuild @@ -0,0 +1,29 @@ +menu "Example Configuration" + +config BENCH_ARGV + string "Arguments for benchmark test" + default "-lng 0" + help + -? Help, print this usage + 0: English, 1: Japanese + -csv Print terminal output in csv format + -base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes) + -no_aad No additional authentication data passed. + -dgst_full Full digest operation performed. + -rsa_sign Measure RSA sign/verify instead of encrypt/decrypt. + - Algorithm to benchmark. Available algorithms include: + cipher aes-cbc aes-gcm chacha20 chacha20-poly1305 + digest md5 poly1305 sha sha2 sha224 sha256 sha384 sha512 sha3 + sha3-224 sha3-256 sha3-384 sha3-512 + mac hmac hmac-md5 hmac-sha hmac-sha224 hmac-sha256 hmac-sha384 + hmac-sha512 + asym rsa rsa-sz dh ecc-kg ecc + other rng + -lng Display benchmark result by specified language. + 0: English, 1: Japanese + Size of block in bytes + + e.g -lng 1 + e.g sha + +endmenu diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/component.mk b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/component.mk new file mode 100644 index 000000000..e19e22a53 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# \ No newline at end of file diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/helper.c b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/helper.c new file mode 100644 index 000000000..94e0d8bfb --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/helper.c @@ -0,0 +1,80 @@ +/* helper.c + * + * Copyright (C) 2006-2018 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 +#include + +#include "sdkconfig.h" + +#define WOLFSSL_BENCH_ARGV CONFIG_BENCH_ARGV + +char* __argv[22]; + +int construct_argv() +{ + int cnt = 0; + int i = 0; + int len = 0; + char *_argv; /* buffer for copying the string */ + char *ch; /* char pointer to trace the string */ + char buff[16] = { 0 }; /* buffer for a argument copy */ + + printf("arg:%s\n", CONFIG_BENCH_ARGV); + len = strlen(CONFIG_BENCH_ARGV); + _argv = (char*)malloc(len + 1); + if (!_argv) { + return -1; + } + memset(_argv, 0, len+1); + memcpy(_argv, CONFIG_BENCH_ARGV, len); + _argv[len] = '\0'; + ch = _argv; + + __argv[cnt] = malloc(10); + sprintf(__argv[cnt], "benchmark"); + __argv[9] = '\0'; + cnt = 1; + + while (*ch != '\0') + { + /* skip white-space */ + while (*ch == ' ') { ++ch; } + + memset(buff, 0, sizeof(buff)); + /* copy each args into buffer */ + i = 0; + while ((*ch != ' ') && (*ch != '\0') && (i < 16)) { + buff[i] = *ch; + ++i; + ++ch; + } + /* copy the string into argv */ + __argv[cnt] = (char*)malloc(i + 1); + memset(__argv[cnt], 0, i + 1); + memcpy(__argv[cnt], buff, i + 1); + /* next args */ + ++cnt; + } + + free(_argv); + + return (cnt); +} diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/include/user_settings.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/include/user_settings.h new file mode 100644 index 000000000..35df8c37e --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/include/user_settings.h @@ -0,0 +1,51 @@ +/* user_settings.h + * + * Copyright (C) 2006-2018 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 + */ + +#define BENCH_EMBEDDED +#define USE_CERT_BUFFERS_2048 + +/* TLS 1.3 */ +#define WOLFSSL_TLS13 +#define HAVE_TLS_EXTENSIONS +#define WC_RSA_PSS +#define HAVE_HKDF +#define HAVE_FFDHE_2048 +#define HAVE_AEAD +#define HAVE_SUPPORTED_CURVES + +#define SINGLE_THREADED /* or define RTOS option */ +#define NO_FILESYSTEM + +#define HAVE_AESGCM +#define WOLFSSL_SHA512 +#define HAVE_ECC +#define HAVE_CURVE25519 +#define CURVE25519_SMALL +#define HAVE_ED25519 + +/* debug options */ +/* #define DEBUG_WOLFSSL */ + +/* date/time */ +/* if it cannot adjust time in the device, */ +/* enable macro below */ +/* #define NO_ASN_TIME */ +/* #define XTIME time */ diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/sdkconfig.defaults b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/sdkconfig.defaults new file mode 100644 index 000000000..29cf15a34 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/sdkconfig.defaults @@ -0,0 +1,4 @@ +CONFIG_BENCH_ARGV="-lng 0" +CONFIG_MAIN_TASK_STACK_SIZE=5000 +CONFIG_FREERTOS_HZ=1000 +CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0= diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/CMakeLists.txt b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/CMakeLists.txt new file mode 100644 index 000000000..bf716c65b --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wolfssl_client) diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/Makefile b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/Makefile new file mode 100644 index 000000000..ac04b5fe5 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/Makefile @@ -0,0 +1,11 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wolfssl_client + +CFLAGS += -DWOLFSSL_USER_SETTINGS + +include $(IDF_PATH)/make/project.mk + diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/README.md b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/README.md new file mode 100644 index 000000000..4edec3eeb --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/README.md @@ -0,0 +1,19 @@ +#wolfssl Example + +The Example contains of wolfSSL tls client demo. + +1. "make menuconfig" to config the project + 1-1. Example Configuration -> + WIFI SSID: your own WIFI, which is connected to the Internet.(default is "myssid") + WIFI Password: WIFI password, and default is "mypassword" + Target host ip address : the host that you want to connect to.(default is 127.0.0.1) + + Note: the example program uses 11111 port. If you want to use different port + , you need to modifiy DEFAULT_PORT definition in the code. + +When you want to test the wolfSSL client +1. "make falsh monitor" to load the firmware and see the context +2. You can use /examples/server/server program for test. + e.g. Launch ./examples/server/server -v 4 -b -i + +See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/Kconfig.projbuild b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/Kconfig.projbuild new file mode 100644 index 000000000..afcf6edc6 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/Kconfig.projbuild @@ -0,0 +1,21 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +config TARGET_HOST + string "Target host" + default "127.0.01.1" + help + host address for the example to connect + +endmenu diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/client-tls.c b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/client-tls.c new file mode 100644 index 000000000..034513e48 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/client-tls.c @@ -0,0 +1,151 @@ +/* client-tls-callback.c + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * 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-1301, USA + */ +/* the usual suspects */ +#include +#include +#include +#include + +/* ESP specific */ +#include "wifi_connect.h" + +/* socket includes */ +#include +#include +#include +#include + +/* wolfSSL */ +#include +#include +#include + +#ifdef WOLFSSL_TRACK_MEMORY + #include +#endif + +const char *TAG = "tls_client"; + +void tls_smp_client_task() +{ + int ret; + int sockfd; + struct sockaddr_in servAddr; + char buff[256]; + size_t len; + + /* declare wolfSSL objects */ + WOLFSSL_CTX *ctx; + WOLFSSL *ssl; + + WOLFSSL_ENTER("tls_smp_client_task"); + +#ifdef DEBUG_WOLFSSL + WOLFSSL_MSG("Debug ON"); + wolfSSL_Debugging_ON(); +#endif + /* Initialize wolfSSL */ + wolfSSL_Init(); + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + printf("ERROR: failed to create the socket\n"); + } + /* Create and initialize WOLFSSL_CTX */ + if ((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())) == NULL) { + printf("ERROR: failed to create WOLFSSL_CTX\n"); + } + WOLFSSL_MSG("Loading...cert"); + /* Load client certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048, + sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1)) != SSL_SUCCESS) { + printf("ERROR: failed to load %d, please check the file.\n",ret); + } + + /* Initialize the server address struct with zeros */ + memset(&servAddr, 0, sizeof(servAddr)); + + /* Fill in the server address */ + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ + + /* Get the server IPv4 address from the command line call */ + WOLFSSL_MSG("inet_pton"); + if ((ret = inet_pton(AF_INET, TLS_SMP_TARGET_HOST, + &servAddr.sin_addr)) != 1) { + printf("ERROR: invalid address ret=%d\n", ret); + } + + /* Connect to the server */ + sprintf(buff, "Connecting to server....%s(port:%d)", TLS_SMP_TARGET_HOST + , DEFAULT_PORT); + WOLFSSL_MSG(buff); + if ((ret = connect(sockfd, (struct sockaddr *)&servAddr, + sizeof(servAddr))) == -1){ + printf("ERROR: failed to connect ret=%d\n", ret); + } + + WOLFSSL_MSG("Create a WOLFSSL object"); + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + printf("ERROR: failed to create WOLFSSL object\n"); + } + + /* Attach wolfSSL to the socket */ + wolfSSL_set_fd(ssl, sockfd); + + WOLFSSL_MSG("Connect to wolfSSL on the server side"); + /* Connect to wolfSSL on the server side */ + if (wolfSSL_connect(ssl) != SSL_SUCCESS) { + printf("ERROR: failed to connect to wolfSSL\n"); + } + + /* Get a message for the server from stdin */ + WOLFSSL_MSG("Message for server: "); + memset(buff, 0, sizeof(buff)); + sprintf(buff, "message from client\n"); + len = strnlen(buff, sizeof(buff)); + /* Send the message to the server */ + if (wolfSSL_write(ssl, buff, len) != len) { + printf("ERROR: failed to write\n"); + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) == -1) { + printf("ERROR: failed to read\n"); + } + + /* Print to stdout any data the server sends */ + WOLFSSL_MSG("Server:"); + WOLFSSL_MSG(buff); + /* Cleanup and return */ + wolfSSL_free(ssl); /* Free the wolfSSL object */ + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ + close(sockfd); /* Close the connection to the server */ + + vTaskDelete(NULL); + + return; /* Return reporting a success */ +} diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/component.mk b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/component.mk new file mode 100644 index 000000000..61f8990c3 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/user_settings.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/user_settings.h new file mode 100644 index 000000000..35df8c37e --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/user_settings.h @@ -0,0 +1,51 @@ +/* user_settings.h + * + * Copyright (C) 2006-2018 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 + */ + +#define BENCH_EMBEDDED +#define USE_CERT_BUFFERS_2048 + +/* TLS 1.3 */ +#define WOLFSSL_TLS13 +#define HAVE_TLS_EXTENSIONS +#define WC_RSA_PSS +#define HAVE_HKDF +#define HAVE_FFDHE_2048 +#define HAVE_AEAD +#define HAVE_SUPPORTED_CURVES + +#define SINGLE_THREADED /* or define RTOS option */ +#define NO_FILESYSTEM + +#define HAVE_AESGCM +#define WOLFSSL_SHA512 +#define HAVE_ECC +#define HAVE_CURVE25519 +#define CURVE25519_SMALL +#define HAVE_ED25519 + +/* debug options */ +/* #define DEBUG_WOLFSSL */ + +/* date/time */ +/* if it cannot adjust time in the device, */ +/* enable macro below */ +/* #define NO_ASN_TIME */ +/* #define XTIME time */ diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/wifi_connect.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/wifi_connect.h new file mode 100644 index 000000000..39345936a --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/include/wifi_connect.h @@ -0,0 +1,38 @@ +/* user_settings.h + * + * Copyright (C) 2006-2018 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 + */ +#ifndef _TLS_WIFI_H_ +#define _TLS_WIFI_H_ + +#include "esp_log.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" + +#define DEFAULT_PORT 11111 + +#define TLS_SMP_CLIENT_TASK_NAME "tls_client_example" +#define TLS_SMP_CLIENT_TASK_WORDS 10240 +#define TLS_SMP_CLIENT_TASK_PRIORITY 8 + +#define TLS_SMP_WIFI_SSID CONFIG_WIFI_SSID +#define TLS_SMP_WIFI_PASS CONFIG_WIFI_PASSWORD +#define TLS_SMP_TARGET_HOST CONFIG_TARGET_HOST + +#endif diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/wifi_connect.c b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/wifi_connect.c new file mode 100644 index 000000000..4735c62eb --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/wifi_connect.c @@ -0,0 +1,146 @@ +/* wifi_connect.c + * + * Copyright (C) 2006-2018 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 + */ +/*ESP specific */ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "wifi_connect.h" +#include "lwip/sockets.h" +#include "lwip/netdb.h" +#include "lwip/apps/sntp.h" +#include "nvs_flash.h" + +const static int CONNECTED_BIT = BIT0; +static EventGroupHandle_t wifi_event_group; +/* proto-type */ +extern void tls_smp_client_task(); +static void tls_smp_client_init(); + +const static char *TAG = "tls_client"; + +static EventGroupHandle_t wifi_event_group; +extern void tls_smp_client_task(); + +static void set_time() +{ + /* set dummy wallclock time. */ + struct timeval utctime; + struct timezone tz; + struct strftime_buf; + time_t now; + struct tm timeinfo; + char strftime_buf[64]; + + utctime.tv_sec = 1542008020; /* dummy time: Mon Nov 12 07:33:40 2018 */ + utctime.tv_usec = 0; + tz.tz_minuteswest = 0; + tz.tz_dsttime = 0; + + settimeofday(&utctime, &tz); + + time(&now); + localtime_r(&now, &timeinfo); + + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + ESP_LOGI(TAG, "The current date/time is: %s", strftime_buf); + + /* wait until wifi connect */ + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, + false, true, portMAX_DELAY); + /* now we start client tasks. */ + tls_smp_client_init(); +} + +/* create task */ +static void tls_smp_client_init(void) +{ + int ret; + xTaskHandle _handle; + /* http://esp32.info/docs/esp_idf/html/dd/d3c/group__xTaskCreate.html */ + ret = xTaskCreate(tls_smp_client_task, + TLS_SMP_CLIENT_TASK_NAME, + TLS_SMP_CLIENT_TASK_WORDS, + NULL, + TLS_SMP_CLIENT_TASK_PRIORITY, + &_handle); + + if (ret != pdPASS) { + ESP_LOGI(TAG, "create thread %s failed", TLS_SMP_CLIENT_TASK_NAME); + } +} +/* event hander for wifi events */ +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) + { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "got ip:%s", + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + /* http://esp32.info/docs/esp_idf/html/dd/d08/group__xEventGroupSetBits.html */ + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} +/* entry point */ +void app_main(void) +{ + ESP_LOGI(TAG, "Start app_main..."); + ESP_ERROR_CHECK(nvs_flash_init()); + + ESP_LOGI(TAG, "Initialize wifi"); + /* TCP/IP adapter initialization */ + tcpip_adapter_init(); + + /* */ + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = TLS_SMP_WIFI_SSID, + .password = TLS_SMP_WIFI_PASS, + }, + }; + /* WiFi station mode */ + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + /* Wifi Set the configuration of the ESP32 STA or AP */ + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + /* Start Wifi */ + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", + TLS_SMP_WIFI_SSID, TLS_SMP_WIFI_PASS); + ESP_LOGI(TAG, "Set dummy time..."); + set_time(); +} diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/CMakeLists.txt b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/CMakeLists.txt new file mode 100644 index 000000000..71455470d --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(tls_server) diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/Makefile b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/Makefile new file mode 100644 index 000000000..5fa6a42bd --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/Makefile @@ -0,0 +1,11 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := tls_server + +CFLAGS += -DWOLFSSL_USER_SETTINGS + +include $(IDF_PATH)/make/project.mk + diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/README.md b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/README.md new file mode 100644 index 000000000..2265618df --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/README.md @@ -0,0 +1,19 @@ +#wolfSSL Example + +The Example contains a wolfSSL simple server. + +1. "make menuconfigure" to configure the project + 1-1. Example Configuration -> + WIFI SSID : your own WIFI, which is connected to the Internet.(default is "myssid") + WIFI Password : WIFI password, and default is "mypassword" + +When you want to test the wolfSSL simple server demo +1. "make flash" to compile the code and load the firmware +2. "make monitor" to see the context. The assigned IP address can be found in output message. +3. Once the server connects to the wifi, it is waiting for client request. + ("Waiting for a connection..." message will be displayed.) +4. You can use /examples/client to test the server + e.g ./example/client/client -h xx.xx.xx + +See the README.md file in the upper level 'examples' directory for more information about examples. + diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/Kconfig.projbuild b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/Kconfig.projbuild new file mode 100644 index 000000000..176d8fb33 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/component.mk b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/component.mk new file mode 100644 index 000000000..d31083f65 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/component.mk @@ -0,0 +1,3 @@ +# +# Main Makefile. This is basically the same as a component makefile. +# \ No newline at end of file diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/user_settings.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/user_settings.h new file mode 100644 index 000000000..35df8c37e --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/user_settings.h @@ -0,0 +1,51 @@ +/* user_settings.h + * + * Copyright (C) 2006-2018 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 + */ + +#define BENCH_EMBEDDED +#define USE_CERT_BUFFERS_2048 + +/* TLS 1.3 */ +#define WOLFSSL_TLS13 +#define HAVE_TLS_EXTENSIONS +#define WC_RSA_PSS +#define HAVE_HKDF +#define HAVE_FFDHE_2048 +#define HAVE_AEAD +#define HAVE_SUPPORTED_CURVES + +#define SINGLE_THREADED /* or define RTOS option */ +#define NO_FILESYSTEM + +#define HAVE_AESGCM +#define WOLFSSL_SHA512 +#define HAVE_ECC +#define HAVE_CURVE25519 +#define CURVE25519_SMALL +#define HAVE_ED25519 + +/* debug options */ +/* #define DEBUG_WOLFSSL */ + +/* date/time */ +/* if it cannot adjust time in the device, */ +/* enable macro below */ +/* #define NO_ASN_TIME */ +/* #define XTIME time */ diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/wifi_connect.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/wifi_connect.h new file mode 100644 index 000000000..f50f578df --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/include/wifi_connect.h @@ -0,0 +1,37 @@ +/* wifi_connect.h + * + * Copyright (C) 2006-2018 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 + */ +#ifndef _TLS_WIFI_H_ +#define _TLS_WIFI_H_ + +#include "esp_log.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" + +#define DEFAULT_PORT 11111 + +#define TLS_SMP_SERVER_TASK_NAME "tls_sever_example" +#define TLS_SMP_SERVER_TASK_WORDS 10240 +#define TLS_SMP_SERVER_TASK_PRIORITY 8 + +#define TLS_SMP_WIFI_SSID CONFIG_WIFI_SSID +#define TLS_SMP_WIFI_PASS CONFIG_WIFI_PASSWORD + +#endif diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/server-tls.c b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/server-tls.c new file mode 100644 index 000000000..3cc1227ce --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/server-tls.c @@ -0,0 +1,170 @@ +/* server-tls-callback.c + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * 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-1301, USA + */ +/* the usual suspects */ +#include +#include +#include +#include + +/* socket includes */ +#include +#include +#include +#include + +/* wolfSSL */ +#include +#include +#include + +/* ESP specific */ +#include "wifi_connect.h" + +#ifdef WOLFSSL_TRACK_MEMORY + #include +#endif + +const char *TAG = "tls_server"; + +void tls_smp_server_task() +{ + int sockfd; + int connd; + struct sockaddr_in servAddr; + struct sockaddr_in clientAddr; + socklen_t size = sizeof(clientAddr); + char buff[256]; + size_t len; + int shutdown = 0; + int ret; + + /* declare wolfSSL objects */ + WOLFSSL_CTX* ctx; + WOLFSSL* ssl; + + WOLFSSL_ENTER("tls_smp_server_task"); + +#ifdef DEBUG_WOLFSSL + WOLFSSL_MSG("Debug ON"); + wolfSSL_Debugging_ON(); +#endif + /* Initialize wolfSSL */ + WOLFSSL_MSG("Start wolfSSL_Init()"); + wolfSSL_Init(); + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + WOLFSSL_MSG( "start socket())"); + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + printf("ERROR: failed to create the socket"); + } + + /* Create and initialize WOLFSSL_CTX */ + WOLFSSL_MSG("Create and initialize WOLFSSL_CTX"); + if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) { + printf("ERROR: failed to create WOLFSSL_CTX"); + } + WOLFSSL_MSG("Loading certificate..."); + /* Load server certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048, + sizeof_server_cert_der_2048, + WOLFSSL_FILETYPE_ASN1)) != SSL_SUCCESS) { + printf("ERROR: failed to load cert"); + } + WOLFSSL_MSG("Loading key info..."); + /* Load server key into WOLFSSL_CTX */ + if((ret=wolfSSL_CTX_use_PrivateKey_buffer(ctx, + server_key_der_2048, sizeof_server_key_der_2048, + WOLFSSL_FILETYPE_ASN1)) != SSL_SUCCESS) { + printf("ERROR: failed to load privatekey"); + } + + /* Initialize the server address struct with zeros */ + memset(&servAddr, 0, sizeof(servAddr)); + /* Fill in the server address */ + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ + servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */ + + /* Bind the server socket to our port */ + if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) { + printf("ERROR: failed to bind"); + } + + /* Listen for a new connection, allow 5 pending connections */ + if (listen(sockfd, 5) == -1) { + printf("ERROR: failed to listen"); + } + /* Continue to accept clients until shutdown is issued */ + while (!shutdown) { + WOLFSSL_MSG("Waiting for a connection..."); + /* Accept client connections */ + if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size)) + == -1) { + printf("ERROR: failed to accept the connection"); + } + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + printf("ERROR: failed to create WOLFSSL object"); + } + /* Attach wolfSSL to the socket */ + wolfSSL_set_fd(ssl, connd); + /* Establish TLS connection */ + ret = wolfSSL_accept(ssl); + if (ret != SSL_SUCCESS) { + printf("wolfSSL_accept error %d", wolfSSL_get_error(ssl, ret)); + } + WOLFSSL_MSG("Client connected successfully"); + /* Read the client data into our buff array */ + memset(buff, 0, sizeof(buff)); + if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) { + printf("ERROR: failed to read"); + } + /* Print to stdout any data the client sends */ + WOLFSSL_MSG("Client sends:"); + WOLFSSL_MSG(buff); + /* Check for server shutdown command */ + if (strncmp(buff, "shutdown", 8) == 0) { + WOLFSSL_MSG("Shutdown command issued!"); + shutdown = 1; + } + /* Write our reply into buff */ + memset(buff, 0, sizeof(buff)); + memcpy(buff, "I hear ya fa shizzle!", sizeof(buff)); + len = strnlen(buff, sizeof(buff)); + /* Reply back to the client */ + if (wolfSSL_write(ssl, buff, len) != len) { + printf("ERROR: failed to write"); + } + /* Cleanup after this connection */ + wolfSSL_free(ssl); /* Free the wolfSSL object */ + close(connd); /* Close the connection to the client */ + } + /* Cleanup and return */ + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ + close(sockfd); /* Close the socket listening for clients */ + + vTaskDelete(NULL); + + return; /* Return reporting a success */ +} diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/wifi_connect.c b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/wifi_connect.c new file mode 100644 index 000000000..8ed2216c1 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_server/main/wifi_connect.c @@ -0,0 +1,143 @@ +/* wifi_connect.c + * + * Copyright (C) 2006-2018 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 + */ +/*ESP specific */ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "wifi_connect.h" +#include "lwip/sockets.h" +#include "lwip/netdb.h" +#include "lwip/apps/sntp.h" +#include "nvs_flash.h" + +const static int CONNECTED_BIT = BIT0; +static EventGroupHandle_t wifi_event_group; +/* prefix for logging */ +const static char *TAG = "tls_server"; +/* proto-type difinition */ +extern void tls_smp_server_task(); +static void tls_smp_server_init(); + +static void set_time() +{ + /* set dummy wallclock time. */ + struct timeval utctime; + struct timezone tz; + struct strftime_buf; + time_t now; + struct tm timeinfo; + char strftime_buf[64]; + + utctime.tv_sec = 1542008020; /* dummy time: Mon Nov 12 07:33:40 2018 */ + utctime.tv_usec = 0; + tz.tz_minuteswest = 0; + tz.tz_dsttime = 0; + + settimeofday(&utctime, &tz); + + time(&now); + localtime_r(&now, &timeinfo); + + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + ESP_LOGI(TAG, "The current date/time is: %s", strftime_buf); + + /* wait until wifi connect */ + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, + false, true, portMAX_DELAY); + /* now we start client tasks. */ + tls_smp_server_init(); +} + +/* create task */ +static void tls_smp_server_init(void) +{ + int ret; + xTaskHandle _handle; + /* http://esp32.info/docs/esp_idf/html/dd/d3c/group__xTaskCreate.html */ + ret = xTaskCreate(tls_smp_server_task, + TLS_SMP_SERVER_TASK_NAME, + TLS_SMP_SERVER_TASK_WORDS, + NULL, + TLS_SMP_SERVER_TASK_PRIORITY, + &_handle); + + if (ret != pdPASS) { + ESP_LOGI(TAG, "create thread %s failed", TLS_SMP_SERVER_TASK_NAME); + } +} +/* event hander for wifi events */ +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) + { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "got ip:%s", + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + /* http://esp32.info/docs/esp_idf/html/dd/d08/group__xEventGroupSetBits.html */ + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} +/* entry point */ +void app_main(void) +{ + ESP_LOGI(TAG, "Start app_main..."); + ESP_ERROR_CHECK(nvs_flash_init()); + + ESP_LOGI(TAG, "Initialize wifi"); + /* TCP/IP adapter initialization */ + tcpip_adapter_init(); + + /* */ + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = TLS_SMP_WIFI_SSID, + .password = TLS_SMP_WIFI_PASS, + }, + }; + /* WiFi station mode */ + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + /* Wifi Set the configuration of the ESP32 STA or AP */ + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + /* Start Wifi */ + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", + TLS_SMP_WIFI_SSID, TLS_SMP_WIFI_PASS); + ESP_LOGI(TAG, "Set Dummy time..."); + set_time(); +} diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/CMakeLists.txt b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/CMakeLists.txt new file mode 100644 index 000000000..26af0fe10 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wolfssl_test) diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/Makefile b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/Makefile new file mode 100644 index 000000000..fd971485a --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/Makefile @@ -0,0 +1,11 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wolfssl_test + +CFLAGS += -DWOLFSSL_USER_SETTINGS + +include $(IDF_PATH)/make/project.mk + diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/README.md b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/README.md new file mode 100644 index 000000000..5b9a952bd --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/README.md @@ -0,0 +1,10 @@ +#wolfSSL Example + +The Example contains of wolfSSL test program. + +When you want to run the benchmark program +1. "make menuconfig" to configure the program,first +1. "make flash" to compile and load the firemware +2. "make monitor" to see the message + +See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/component.mk b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/component.mk new file mode 100644 index 000000000..d31083f65 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/component.mk @@ -0,0 +1,3 @@ +# +# Main Makefile. This is basically the same as a component makefile. +# \ No newline at end of file diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/include/user_settings.h b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/include/user_settings.h new file mode 100644 index 000000000..35df8c37e --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/include/user_settings.h @@ -0,0 +1,51 @@ +/* user_settings.h + * + * Copyright (C) 2006-2018 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 + */ + +#define BENCH_EMBEDDED +#define USE_CERT_BUFFERS_2048 + +/* TLS 1.3 */ +#define WOLFSSL_TLS13 +#define HAVE_TLS_EXTENSIONS +#define WC_RSA_PSS +#define HAVE_HKDF +#define HAVE_FFDHE_2048 +#define HAVE_AEAD +#define HAVE_SUPPORTED_CURVES + +#define SINGLE_THREADED /* or define RTOS option */ +#define NO_FILESYSTEM + +#define HAVE_AESGCM +#define WOLFSSL_SHA512 +#define HAVE_ECC +#define HAVE_CURVE25519 +#define CURVE25519_SMALL +#define HAVE_ED25519 + +/* debug options */ +/* #define DEBUG_WOLFSSL */ + +/* date/time */ +/* if it cannot adjust time in the device, */ +/* enable macro below */ +/* #define NO_ASN_TIME */ +/* #define XTIME time */ diff --git a/IDE/Espressif/ESP-IDF/examples/wolfssl_test/sdkconfig.defaults b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/sdkconfig.defaults new file mode 100644 index 000000000..da8d0aa20 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/examples/wolfssl_test/sdkconfig.defaults @@ -0,0 +1,2 @@ +CONFIG_MAIN_TASK_STACK_SIZE=5000 +CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0= diff --git a/IDE/Espressif/ESP-IDF/libs/CMakeLists.txt b/IDE/Espressif/ESP-IDF/libs/CMakeLists.txt new file mode 100644 index 000000000..78fe8a073 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/libs/CMakeLists.txt @@ -0,0 +1,79 @@ +cmake_minimum_required(VERSION 3.5) + +set(CMAKE_CURRENT_SOURCE_DIR ".") +set(WOLFSSL_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) +set(INCLUDE_PATH ${WOLFSSL_ROOT}) +set(COMPONENT_SRCS + "src/keys.c" + "src/sniffer.c" + "src/tls.c" + "src/wolfio.c" + "src/crl.c" + "src/internal.c" + "src/ocsp.c" + "src/ssl.c" + "src/tls13.c" + "wolfcrypt/src/aes.c" + "wolfcrypt/src/arc4.c" + "wolfcrypt/src/asm.c" + "wolfcrypt/src/asn.c" + "wolfcrypt/src/blake2b.c" + "wolfcrypt/src/camellia.c" + "wolfcrypt/src/chacha.c" + "wolfcrypt/src/chacha20_poly1305.c" + "wolfcrypt/src/cmac.c" + "wolfcrypt/src/coding.c" + "wolfcrypt/src/compress.c" + "wolfcrypt/src/cpuid.c" + "wolfcrypt/src/cryptodev.c" + "wolfcrypt/src/curve25519.c" + "wolfcrypt/src/des3.c" + "wolfcrypt/src/dh.c" + "wolfcrypt/src/dsa.c" + "wolfcrypt/src/ecc.c" + "wolfcrypt/src/ecc_fp.c" + "wolfcrypt/src/ed25519.c" + "wolfcrypt/src/error.c" + "wolfcrypt/src/fe_low_mem.c" + "wolfcrypt/src/fe_operations.c" + "wolfcrypt/src/ge_low_mem.c" + "wolfcrypt/src/ge_operations.c" + "wolfcrypt/src/hash.c" + "wolfcrypt/src/hc128.c" + "wolfcrypt/src/hmac.c" + "wolfcrypt/src/idea.c" + "wolfcrypt/src/integer.c" + "wolfcrypt/src/logging.c" + "wolfcrypt/src/md2.c" + "wolfcrypt/src/md4.c" + "wolfcrypt/src/md5.c" + "wolfcrypt/src/memory.c" + "wolfcrypt/src/pkcs12.c" + "wolfcrypt/src/pkcs7.c" + "wolfcrypt/src/poly1305.c" + "wolfcrypt/src/pwdbased.c" + "wolfcrypt/src/rabbit.c" + "wolfcrypt/src/random.c" + "wolfcrypt/src/ripemd.c" + "wolfcrypt/src/rsa.c" + "wolfcrypt/src/sha.c" + "wolfcrypt/src/sha256.c" + "wolfcrypt/src/sha3.c" + "wolfcrypt/src/sha512.c" + "wolfcrypt/src/signature.c" + "wolfcrypt/src/sp_arm32.c" + "wolfcrypt/src/sp_arm64.c" + "wolfcrypt/src/sp_c32.c" + "wolfcrypt/src/sp_c64.c" + "wolfcrypt/src/sp_int.c" + "wolfcrypt/src/sp_x86_64.c" + "wolfcrypt/src/srp.c" + "wolfcrypt/src/tfm.c" + "wolfcrypt/src/wc_encrypt.c" + "wolfcrypt/src/wc_port.c" + "wolfcrypt/src/wolfevent.c" + "wolfcrypt/src/wolfmath.c" +) +set(COMPONENT_REQUIRES lwip) +set(COMPONENT_ADD_INCLUDEDIRS ../freertos/include/freertos) +register_component() diff --git a/IDE/Espressif/ESP-IDF/libs/component.mk b/IDE/Espressif/ESP-IDF/libs/component.mk new file mode 100644 index 000000000..784209fc8 --- /dev/null +++ b/IDE/Espressif/ESP-IDF/libs/component.mk @@ -0,0 +1,13 @@ +# +# Component Makefile +# + +COMPONENT_ADD_INCLUDEDIRS := . +COMPONENT_ADD_INCLUDEDIRS += ../freertos/include/freertos/ + +COMPONENT_SRCDIRS := src wolfcrypt/src + +COMPONENT_OBJEXCLUDE := wolfcrypt/src/aes_asm.o +COMPONENT_OBJEXCLUDE += wolfcrypt/src/evp.o +COMPONENT_OBJEXCLUDE += wolfcrypt/src/misc.o +COMPONENT_OBJEXCLUDE += src/bio.o diff --git a/IDE/Espressif/ESP-IDF/setup.sh b/IDE/Espressif/ESP-IDF/setup.sh new file mode 100755 index 000000000..40b307bef --- /dev/null +++ b/IDE/Espressif/ESP-IDF/setup.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +# check if IDF_PATH is set +if [ -z "$IDF_PATH" ]; then + echo "Please follows the instruction of ESP-IDF installation and set IDF_PATH." + exit 1 +fi + +RMDCMD='/bin/rm -rf' +MKDCMD='/bin/mkdir' +CPDCMD='/bin/cp' + +SCRIPTDIR=`dirname $0` +SCRIPTDIR=`cd $SCRIPTDIR && pwd -P` +WOLFSSL_ESPIDFDIR=${SCRIPTDIR} +WOLFSSL_ESPIDFDIR=`cd $WOLFSSL_ESPIDFDIR && pwd -P` +BASEDIR=${SCRIPTDIR}/../../../ +BASEDIR=`cd ${BASEDIR} && pwd -P` + +# echo $WOLFSSL_ESPIDFDIR + +WOLFSSLLIB_TRG_DIR=${IDF_PATH}/components/wolfssl +WOLFSSLEXP_TRG_DIR=${IDF_PATH}/examples/protocols + +if [ ! -d $IDF_PATH ]; then + echo "ESP-IDF Development Framework doesn't exist.: $IDF_PATH" + exit 1 +fi + +# Copy files into ESP-IDF development framework +pushd $IDF_PATH > /dev/null + +echo "Copy files into $IDF_PATH" +# Remove/Create directories +${RMDCMD} ${WOLFSSLLIB_TRG_DIR}/ +${MKDCMD} ${WOLFSSLLIB_TRG_DIR}/ + +${MKDCMD} ${WOLFSSLLIB_TRG_DIR}/src +${MKDCMD} ${WOLFSSLLIB_TRG_DIR}/wolfcrypt +${MKDCMD} ${WOLFSSLLIB_TRG_DIR}/wolfssl + +popd > /dev/null # $WOLFSSL_ESPIDFDIR +pushd ${BASEDIR} > /dev/null # WOLFSSL TOP DIR + +# copying ... files in src/ into $WOLFSSLLIB_TRG_DIR/src +${CPDCMD} ./src/*.c ${WOLFSSLLIB_TRG_DIR}/src/ + +${CPDCMD} -r ./wolfcrypt/src/ ${WOLFSSLLIB_TRG_DIR}/wolfcrypt/ +${CPDCMD} -r ./wolfcrypt/test ${WOLFSSLLIB_TRG_DIR}/wolfcrypt/ +${CPDCMD} -r ./wolfcrypt/benchmark ${WOLFSSLLIB_TRG_DIR}/wolfcrypt/ + +${CPDCMD} -r ./wolfssl/*.h ${WOLFSSLLIB_TRG_DIR}/wolfssl/ +${CPDCMD} -r ./wolfssl/wolfcrypt ${WOLFSSLLIB_TRG_DIR}/wolfssl/ + +popd > /dev/null # + +${CPDCMD} ./libs/CMakeLists.txt ${WOLFSSLLIB_TRG_DIR}/ +${CPDCMD} ./libs/component.mk ${WOLFSSLLIB_TRG_DIR}/ + +pushd ${BASEDIR} > /dev/null # WOLFSSL TOP DIR + +# Benchmark program +${RMDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/main/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/main/include + +${CPDCMD} -r ./wolfcrypt/benchmark/benchmark.c ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_benchmark/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_benchmark/main/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_benchmark/main/include/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_benchmark/main/include/ + +# Crypt Test program +${RMDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/main/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/main/include + +${CPDCMD} -r ./wolfcrypt/test/test.c ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_test/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_test/main/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_test/main/include/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_test/main/include/ + +# TLS Client program +${RMDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/main/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/main/include + +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_client/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_client/main/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_client/main/include/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_client/main/include/ + +# TLS Server program +${RMDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/main/ +${MKDCMD} ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/main/include + +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_server/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_server/main/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/main/ +${CPDCMD} -r ${WOLFSSL_ESPIDFDIR}/examples/wolfssl_server/main/include/* ${WOLFSSLEXP_TRG_DIR}/wolfssl_server/main/include/ + +popd > /dev/null # + +exit 1 diff --git a/IDE/include.am b/IDE/include.am index aa4003340..1587d0bcf 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -20,4 +20,4 @@ include IDE/mynewt/include.am include IDE/Renesas/cs+/Projects/include.am include IDE/Renesas/e2studio/Projects/include.am -EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR +EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR IDE/Espressif diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 8e388d3fd..253144e26 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -4986,10 +4986,14 @@ exit_ed_verify: /* declared above at line 239 */ /* extern double current_time(int reset); */ -#elif defined FREERTOS +#elif defined(FREERTOS) #include "task.h" - +#if defined(WOLFSSL_ESPIDF) + /* proto type definition */ + int construct_argv(); + extern char* __argv[22]; +#endif double current_time(int reset) { portTickType tickCount; @@ -5166,11 +5170,18 @@ static int string_matches(const char* arg, const char* str) int len = (int)XSTRLEN(str) + 1; return XSTRNCMP(arg, str, len) == 0; } - +#ifdef WOLFSSL_ESPIDF +int app_main( ) +#else int main(int argc, char** argv) +#endif { int ret = 0; int optMatched; +#ifdef WOLFSSL_ESPIDF + int argc = construct_argv(); + char** argv = (char**)__argv; +#endif #ifndef WOLFSSL_BENCHMARK_ALL int i; #endif diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index c03c797ce..b5da67186 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -213,6 +213,9 @@ void WOLFSSL_TIME(int count) #include #elif defined(WOLFSSL_USER_LOG) /* user includes their own headers */ +#elif defined(WOLFSSL_ESPIDF) + #include "esp_types.h" + #include "esp_log.h" #else #include /* for default printf stuff */ #endif @@ -247,6 +250,9 @@ static void wolfssl_log(const int logLevel, const char *const logMessage) #elif defined(WOLFSSL_APACHE_MYNEWT) LOG_DEBUG(&mynewt_log, LOG_MODULE_DEFAULT, "%s\n", logMessage); +#elif defined(WOLFSSL_ESPIDF) + extern char* TAG; + ESP_LOGI(TAG, "%s", logMessage); #else fprintf(stderr, "%s\n", logMessage); #endif diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index a6a2a77e0..a7d1942be 100755 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2060,6 +2060,22 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } +#elif defined(WOLFSSL_ESPIDF) + #if defined(WOLFSSL_ESPWROOM32) + #include + + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + int i; + + for (i = 0; i< sz; i++) { + output[i] = esp_random( ); + } + + return 0; + } + #endif /* end WOLFSSL_ESPWROOM32 */ + #elif defined(CUSTOM_RAND_GENERATE_BLOCK) /* #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc * extern int myRngFunc(byte* output, word32 sz); diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index a7623c946..aa1a5e34c 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -1474,7 +1474,7 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, #endif #endif int ret = 0; - word32 keyLen, len; + word32 keyLen = 0, len; #endif #ifdef WOLFSSL_HAVE_SP_RSA diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index d01a5e20b..5cbb840f4 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -184,6 +184,9 @@ #include "mcu/mcu_sim.h" #endif #include "os/os_time.h" +#elif defined(WOLFSSL_ESPIDF) + #include + #include #else #include #endif @@ -1064,11 +1067,24 @@ initDefaultName(); #ifndef NO_MAIN_DRIVER /* so overall tests can pull in test function */ +#ifdef WOLFSSL_ESPIDF + void app_main( ) +#else int main(int argc, char** argv) +#endif { int ret; func_args args; - +#ifdef WOLFSSL_ESPIDF + /* set dummy wallclock time. */ + struct timeval utctime; + struct timezone tz; + utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */ + utctime.tv_usec = 0; + tz.tz_minuteswest = 0; + tz.tz_dsttime = 0; + settimeofday(&utctime, &tz); +#endif #ifdef WOLFSSL_APACHE_MYNEWT #ifdef ARCH_sim mcu_sim_parse_args(argc, argv); @@ -1091,10 +1107,10 @@ initDefaultName(); return -1001; } #endif - +#ifndef WOLFSSL_ESPIDF args.argc = argc; args.argv = argv; - +#endif if ((ret = wolfCrypt_Init()) != 0) { printf("wolfCrypt_Init failed %d\n", ret); err_sys("Error with wolfCrypt_Init!\n", -1003); @@ -1115,8 +1131,9 @@ initDefaultName(); if (wc_FreeNetRandom() < 0) err_sys("Failed to free netRandom context", -1005); #endif /* HAVE_WNR */ - +#ifndef WOLFSSL_ESPIDF return args.return_code; +#endif } #endif /* NO_MAIN_DRIVER */ diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 06d010761..5ec5792a0 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -175,6 +175,12 @@ /* Uncomment next line if building for using Apache mynewt */ /* #define WOLFSSL_APACHE_MYNEWT */ +/* Uncomment next line if building for using ESP-IDF */ +/* #define WOLFSSL_ESPIDF */ + +/* Uncomment next line if using Espressif ESP32-WROOM-32 */ +/* #define WOLFSSL_ESPWROOM32 */ + #include #ifdef WOLFSSL_USER_SETTINGS @@ -216,6 +222,22 @@ #include #endif +#if defined(WOLFSSL_ESPIDF) + #define FREERTOS + #define WOLFSSL_LWIP + #define NO_WRITEV + #define SIZEOF_LONG_LONG 8 + #define NO_WOLFSSL_DIR + #define WOLFSSL_NO_CURRDIR + + #define TFM_TIMING_RESISTANT + #define ECC_TIMING_RESISTANT + #define WC_RSA_BLINDING +#if !defined(WOLFSSL_USER_SETTINGS) + #define HAVE_ECC +#endif /* !WOLFSSL_USER_SETTINGS */ +#endif /* WOLFSSL_ESPIDF */ + #if defined(HAVE_LWIP_NATIVE) /* using LwIP native TCP socket */ #define WOLFSSL_LWIP #define NO_WRITEV @@ -609,7 +631,9 @@ extern void uITRON4_free(void *p) ; #define XMALLOC(s, h, type) pvPortMalloc((s)) #define XFREE(p, h, type) vPortFree((p)) #endif - + #if defined(HAVE_ED25519) || defined(WOLFSSL_ESPIDF) + #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n)) + #endif #ifndef NO_WRITEV #define NO_WRITEV #endif