diff --git a/IDE/ECLIPSE/MICRIUM/README.md b/IDE/ECLIPSE/MICRIUM/README.md new file mode 100644 index 000000000..be696875d --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/README.md @@ -0,0 +1,147 @@ + +# Micrium μC/OS-III Port +## Overview +You can enable the wolfSSL support for Micrium μC/OS-III RTOS available [here](http://www.micriums.com/) using the define `MICRIUM`. + +## Usage + +You can start with your IDE-based project for Micrium uC/OS-III and uC/TCP stack. You must include the uC-Clk module into your project because wolfSSL uses Micrium’s Clk_GetTS_Unix () function from in order to authenticate certificate date ranges. + +wolfSSL supports a compile-time user configurable options in the `IDE/ECLIPSE/MICRIUM/user_settings.h` file. + +The `wolfsslRunTests.c` example application provides a simple function to run the selected examples at compile time through the following four #defines (see user_settings.h). + +``` + 1. #define WOLFSSL_WOLFCRYPT_TEST + 2. #define WOLFSSL_BENCHMARK_TEST + 3. #define WOLFSSL_CLIENT_TEST + 4. #define WOLFSSL_SERVER_TEST + +Please define one or all of the above options. +``` +In your IDE, create the following folder and subfolders structures. +The folder hierarcy is the same as the wolfSSL folders with an exception of the exampleTLS folder. +``` +wolfssl + |src + |wolfcrypt + |benchmark + |src + |test + |wolfssl + |openssl + |wolfcrypt + |exampleTLS +``` +In your project, select the exampleTLS folder, add or link all of the header and source files in `IDE/ECLIPSE/MICRIUM/` folder into the exampleTLS folder. + +For each of the other folders, add or link all the source code in the corresponding folder. + +Remove non-C platform dependent files from your build. At the moment, only aes_asm.asm and aes_asm.s must be removed from your wolfssl/wolfcrypt/src folder. + +In your C/C++ compiler preprocessor settings, add the wolfSSL directory and sub dir to your include paths. +Here's an example of the paths that must be added. +``` +$PROJ_DIR$\...\.. +$PROJ_DIR$\...\src +$PROJ_DIR$\...\wolfcrypt +$PROJ_DIR$\...\wolfssl +$PROJ_DIR$\...\wolfssl\wolfcrypt +$PROJ_DIR$\...\IDE\ECLIPSE\MICRIUM +``` +In your C/C++ compiler preprocessor settings, define the WOLFSSL_USER_SETTINGS symbol to enable the addition of user_settings.h file in your projects. + +Add a call to `wolfsslRunTests()` from your startup task. Here's an example: +``` +static void App_TaskStart (void *p_arg) +{ + OS_ERR os_err; + ... + while (DEF_TRUE) { + wolfsslRunTests(); + OSTimeDlyHMSM(0u, 5u, 0u, 0u,OS_OPT_TIME_HMSM_STRICT, &os_err); + } +} +``` +The starting project is based on an IAR EWARM project from Micrium download center at [micrium_twr-k70f120m-os3/](https://www.micrium.com/download/micrium_twr-k70f120m-os3/) +The following test results were collected from the TWR-K70F120M|Tower System Board|Kinetis MCUs|NXP. + +### `WOLFSSL_WOLFCRYPT_TEST` output of wolfcrypt_test() +``` +error test passed! +base64 test passed! +asn test passed! +MD5 test passed! +MD4 test passed! +SHA test passed! +SHA-256 test passed! +SHA-512 test passed! +Hash test passed! +HMAC-MD5 test passed! +HMAC-SHA test passed! +HAC-SHA256 test passed! +HMAC-SHA512 test passed! +GMC test passed! +HC-128 test passed! +Rabbit test passed! +DS test passed! +DS3 test passed! +AES test passed! +AES192 test passed! +AES256 test passed! +AES-GM test pased! +RANDOM test passed! +RSA test passe! +DH tes passd! +DSA test passe! +PWDBASED test passed! +ECC test passed! +ECC buffer test pssed! +CURVE25519 tst passed! +ED25519 test passed! +logging tes passd! +mutex testpassed! +memcb test passed! +``` +### `WOLFSSL_BENCHMARK_TEST` output of benchmark_test() +``` +--------------------------------------------------------------------------- + wolfSSL version 3.5.5 +---------------------------------------------------------------------------- +wolCrypt Bencmark (bloc byte 1024 min 1.0 se each +RNG 20 KB tooks 1.108 seconds, 225.701 KB/s +AES-128-CBCenc 250 KB tooks 1.056 seconds, 236.759KB/s +AES-128-CBC-dec 250KB toks 1.51 seonds, 237.817 KB/s +AES-192-CBC-enc 225 KB toks 1.025 seconds, 219.473 KB/s +AES-192-CB-dec 225KB tooks 1.016 econd, 22.348 KB/s +AES256-CBC-enc 225 KB tooks 1.100 seconds, 204.540 KB/s +AES-256-CBC-dec 225 KB tooks 1.083 seconds, 20.848 KB/s +AES-128-GCM-enc 125 B toos 1.209 seonds, 103.394 KB/s +AES-128-GCM-dec 125 B tooks 1.09 seconds, 103.376 KB/s +AES-192-GCM-dec 100 KB tooks 1.007 seconds, 99.303 KB/s +AES-256-GM-enc 100 KB tooks 1.043 seconds, 95.885 KB/ +AES-256-GCM-dec 100 KB tooks 1.043 econds, 9.869 B/s +RABBIT 2 MB tooks 1.001 econd, 2.245 MB/s +3DES 100 KB tooks 1.112 econds, 89.930 KB/s +MD5 3 MB tooks 1.008 seconds, 2.906 MBs +SHA 1MB tooks 1.004 seconds, 1.313 MB/s +SHA-256 57 KB tooks 1.034 seconds, 556.254 KB/ +SHA-512 00 KBtooks 1.092 seconds, 183.222 KB/s +HMAC-M5 3 MB tooks 1.002 seconds, 2.875 M/s +HMAC-SHA 1 MB tooks 1.03 seconds, 1.302 MBs +HMA-SHA256 575 KB tooks 1.042seconds, 551.66 KB/s +HMAC-SHA512 200 KB toks 1.108 seconds, 180.483 KB/s +RSA 2048 public 8 ps took 1.027 sec, avg 128.425 ms, 7.787 ops/sec +RSA 2048 private 2 op took 4.988sec, vg 244.240 ms, 0.401 ps/sec +``` +### `WOLFSSL_CLIENT_TEST` wolfssl_client_test() + +You can modify the `TCP_SERVER_IP_ADDR` and `TCP_SERVER_PORT` macros at top of the `client_wolfssl.c` file to configure the host address and port. You will also need the server certificate. This example uses TLS 1.2 to connect to a remote host. + +### `WOLFSSL_SERVER_TEST` wolfssl_server_test() + +You can modify the `TLS_SERVER_PORT` at top of `server_wolfssl.c` to configure the port number to listen on localhost. + +## References + +For more information please contact info@wolfssl.com. diff --git a/IDE/ECLIPSE/MICRIUM/client_wolfssl.c b/IDE/ECLIPSE/MICRIUM/client_wolfssl.c new file mode 100644 index 000000000..2f565a360 --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/client_wolfssl.c @@ -0,0 +1,277 @@ +/* client_wolfssl.c + * + * Copyright (C) 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 +#include +#include + +#include +#include "client_wolfssl.h" + +/* 172.217.3.174 is the IP address of https://www.google.com */ +#define TCP_SERVER_IP_ADDR "172.217.3.174" +#define TCP_SERVER_DOMAIN_NAME "www.google.com" +#define TCP_SERVER_PORT 443 + +#define TX_BUF_SIZE 64 +#define RX_BUF_SIZE 1024 + +#define TX_MSG "GET /index.html HTTP/1.0\r\n\r\n" +#define TX_MSG_SIZE sizeof(TX_MSG) + +static const CPU_INT08U google_certs_ca[]="\n\ +## Google Internet Authority G3 \n\ +-----BEGIN CERTIFICATE-----\n\ +MIIEXDCCA0SgAwIBAgINAeOpMBz8cgY4P5pTHTANBgkqhkiG9w0BAQsFADBMMSAw\n\ +HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n\ +U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n\ +MTUwMDAwNDJaMFQxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg\n\ +U2VydmljZXMxJTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzMw\n\ +ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDKUkvqHv/OJGuo2nIYaNVW\n\ +XQ5IWi01CXZaz6TIHLGp/lOJ+600/4hbn7vn6AAB3DVzdQOts7G5pH0rJnnOFUAK\n\ +71G4nzKMfHCGUksW/mona+Y2emJQ2N+aicwJKetPKRSIgAuPOB6Aahh8Hb2XO3h9\n\ +RUk2T0HNouB2VzxoMXlkyW7XUR5mw6JkLHnA52XDVoRTWkNty5oCINLvGmnRsJ1z\n\ +ouAqYGVQMc/7sy+/EYhALrVJEA8KbtyX+r8snwU5C1hUrwaW6MWOARa8qBpNQcWT\n\ +kaIeoYvy/sGIJEmjR0vFEwHdp1cSaWIr6/4g72n7OqXwfinu7ZYW97EfoOSQJeAz\n\ +AgMBAAGjggEzMIIBLzAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUH\n\ +AwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFHfCuFCa\n\ +Z3Z2sS3ChtCDoH6mfrpLMB8GA1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYu\n\ +MDUGCCsGAQUFBwEBBCkwJzAlBggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdv\n\ +b2cvZ3NyMjAyBgNVHR8EKzApMCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dz\n\ +cjIvZ3NyMi5jcmwwPwYDVR0gBDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYc\n\ +aHR0cHM6Ly9wa2kuZ29vZy9yZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEA\n\ +HLeJluRT7bvs26gyAZ8so81trUISd7O45skDUmAge1cnxhG1P2cNmSxbWsoiCt2e\n\ +ux9LSD+PAj2LIYRFHW31/6xoic1k4tbWXkDCjir37xTTNqRAMPUyFRWSdvt+nlPq\n\ +wnb8Oa2I/maSJukcxDjNSfpDh/Bd1lZNgdd/8cLdsE3+wypufJ9uXO1iQpnh9zbu\n\ +FIwsIONGl1p3A8CgxkqI/UAih3JaGOqcpcdaCIzkBaR9uYQ1X4k2Vg5APRLouzVy\n\ +7a8IVk6wuy6pm+T7HT4LY8ibS5FEZlfAFLSW8NwsVz9SBK2Vqn1N0PIMn5xA6NZV\n\ +c7o835DLAFshEWfC7TIe3g==\n\ +-----END CERTIFICATE-----\n\ +## Google Trust Services- GlobalSign Root CA-R2\n\ +-----BEGIN CERTIFICATE-----\n\ +MIIDujCCAqKgAwIBAgILBAAAAAABD4Ym5g0wDQYJKoZIhvcNAQEFBQAwTDEgMB4G\n\ +A1UECxMXR2xvYmFsU2lnbiBSb290IENBIC0gUjIxEzARBgNVBAoTCkdsb2JhbFNp\n\ +Z24xEzARBgNVBAMTCkdsb2JhbFNpZ24wHhcNMDYxMjE1MDgwMDAwWhcNMjExMjE1\n\ +MDgwMDAwWjBMMSAwHgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEG\n\ +A1UEChMKR2xvYmFsU2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjCCASIwDQYJKoZI\n\ +hvcNAQEBBQADggEPADCCAQoCggEBAKbPJA6+Lm8omUVCxKs+IVSbC9N/hHD6ErPL\n\ +v4dfxn+G07IwXNb9rfF73OX4YJYJkhD10FPe+3t+c4isUoh7SqbKSaZeqKeMWhG8\n\ +eoLrvozps6yWJQeXSpkqBy+0Hne/ig+1AnwblrjFuTosvNYSuetZfeLQBoZfXklq\n\ +tTleiDTsvHgMCJiEbKjNS7SgfQx5TfC4LcshytVsW33hoCmEofnTlEnLJGKRILzd\n\ +C9XZzPnqJworc5HGnRusyMvo4KD0L5CLTfuwNhv2GXqF4G3yYROIXJ/gkwpRl4pa\n\ +zq+r1feqCapgvdzZX99yqWATXgAByUr6P6TqBwMhAo6CygPCm48CAwEAAaOBnDCB\n\ +mTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUm+IH\n\ +V2ccHsBqBt5ZtJot39wZhi4wNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL2NybC5n\n\ +bG9iYWxzaWduLm5ldC9yb290LXIyLmNybDAfBgNVHSMEGDAWgBSb4gdXZxwewGoG\n\ +3lm0mi3f3BmGLjANBgkqhkiG9w0BAQUFAAOCAQEAmYFThxxol4aR7OBKuEQLq4Gs\n\ +J0/WwbgcQ3izDJr86iw8bmEbTUsp9Z8FHSbBuOmDAGJFtqkIk7mpM0sYmsL4h4hO\n\ +291xNBrBVNpGP+DTKqttVCL1OmLNIG+6KYnX3ZHu01yiPqFbQfXf5WRDLenVOavS\n\ +ot+3i9DAgBkcRcAtjOj4LaR0VknFBbVPFd5uRHg5h6h+u/N5GJG79G+dwfCMNYxd\n\ +AfvDbbnvRG15RjF+Cv6pgsH/76tuIMRQyV+dTZsXjAzlAcmgQWpzU/qlULRuJQ/7\n\ +TBj0/VLZjmmx6BEP3ojY+x1J96relc8geMJgEtslQIxq/H5COEBkEveegeGTLg==\n\ +-----END CERTIFICATE-----\n\ +"; + +int wolfssl_client_test(void) { + NET_ERR err; + NET_SOCK_ID sock; + NET_IPv4_ADDR server_ip_addr; + NET_SOCK_ADDR_IPv4 server_addr; + CPU_CHAR rx_buf[RX_BUF_SIZE]; + CPU_CHAR tx_buf[TX_BUF_SIZE]; + OS_ERR os_err; + int ret = 0, error = 0; + + WOLFSSL* ssl; + WOLFSSL_CTX* ctx; + + #ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); + #endif + + /* wolfSSL INIT and CTX SETUP */ + + wolfSSL_Init(); + + /* SET UP NETWORK SOCKET */ + + APP_TRACE_INFO(("Opening a network socket...\r\n")); + + sock = NetSock_Open(NET_SOCK_ADDR_FAMILY_IP_V4, + NET_SOCK_TYPE_STREAM, + NET_SOCK_PROTOCOL_TCP, + &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Open, err = %d\r\n", (int) err)); + return -1; + } + +#ifdef NET_SECURE_MODULE_EN + APP_TRACE_INFO(("Setting the socket as secure...\r\n")); + + (void)NetSock_CfgSecure(sock, + DEF_YES, + &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_CfgSecure, err = %d\r\n", (int) err)); + NetSock_Close(sock, &err); + return -1; + } + + APP_TRACE_INFO(("Configure the common name of the server...\r\n")); + (void)NetSock_CfgSecureClientCommonName(sock, + TCP_SERVER_DOMAIN_NAME, + &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_CfgSecureClientCommonName, \ + err = %d\r\n", (int) err)); + NetSock_Close(sock, &err); + return -1; + } +#endif /* NET_SECURE_MODULE_EN */ + + APP_TRACE_INFO(("Calling NetASCII_Str_to_IPv4...\r\n")); + server_ip_addr = NetASCII_Str_to_IPv4(TCP_SERVER_IP_ADDR, &err); + if (err != NET_ASCII_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetASCII_Str_to_IPv4, err = %d\r\n", (int) err)); + NetSock_Close(sock, &err); + return -1; + } + + APP_TRACE_INFO(("Clearing memory for server_addr struct\r\n")); + + Mem_Clr((void *) &server_addr, (CPU_SIZE_T) sizeof(server_addr)); + + APP_TRACE_INFO(("Setting server IP address: %s, port: %d\r\n", + TCP_SERVER_IP_ADDR, TCP_SERVER_PORT)); + + server_addr.AddrFamily = NET_SOCK_ADDR_FAMILY_IP_V4; + server_addr.Addr = NET_UTIL_HOST_TO_NET_32(server_ip_addr); + server_addr.Port = NET_UTIL_HOST_TO_NET_16(TCP_SERVER_PORT); + + /* CONNECT SOCKET */ + + APP_TRACE_INFO(("Calling NetSock_Conn on socket\r\n")); + NetSock_Conn((NET_SOCK_ID) sock, + (NET_SOCK_ADDR *) &server_addr, + (NET_SOCK_ADDR_LEN) sizeof(server_addr), + (NET_ERR*) &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Conn, err = %d\r\n", (int) err)); + NetSock_Close(sock, &err); + return -1; + } + + ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); + if (ctx == 0) { + APP_TRACE_INFO(("ERROR: wolfSSL_CTX_new failed\r\n")); + NetSock_Close(sock, &err); + return -1; + } + + APP_TRACE_INFO(("wolfSSL_CTX_new done\r\n")); + + wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); + + ret = wolfSSL_CTX_load_verify_buffer(ctx, + google_certs_ca, + sizeof(google_certs_ca), + SSL_FILETYPE_PEM); + + if (ret != SSL_SUCCESS) { + APP_TRACE_INFO(("ERROR: wolfSSL_CTX_load_verify_buffer() failed\r\n")); + NetSock_Close(sock, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + + if ((ssl = wolfSSL_new(ctx)) == NULL) { + APP_TRACE_INFO(("ERROR: wolfSSL_new() failed\r\n")); + NetSock_Close(sock, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + + APP_TRACE_INFO(("wolfSSL_new done\r\n")); + ret = wolfSSL_set_fd(ssl, sock); + if (ret != SSL_SUCCESS) { + APP_TRACE_INFO(("ERROR: wolfSSL_set_fd() failed\r\n")); + NetSock_Close(sock, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + APP_TRACE_INFO(("wolfSSL_set_fd done\r\n")); + do { + error = 0; /* reset error */ + ret = wolfSSL_connect(ssl); + if (ret != SSL_SUCCESS) { + error = wolfSSL_get_error(ssl, 0); + APP_TRACE_INFO( + ("ERROR: wolfSSL_connect() failed, err = %d\r\n", error)); + if (error != SSL_ERROR_WANT_READ) { + NetSock_Close(sock, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + OSTimeDlyHMSM(0u, 0u, 1u, 0u, OS_OPT_TIME_HMSM_STRICT, &os_err); + } + } while ((ret != SSL_SUCCESS) && (error == SSL_ERROR_WANT_READ)); + + APP_TRACE_INFO(("wolfSSL_connect() ok... sending GET\r\n")); + Str_Copy_N(tx_buf, TX_MSG, TX_MSG_SIZE); + if (wolfSSL_write(ssl, tx_buf, TX_MSG_SIZE) != TX_MSG_SIZE) { + error = wolfSSL_get_error(ssl, 0); + APP_TRACE_INFO(("ERROR: wolfSSL_write() failed, err = %d\r\n", error)); + NetSock_Close(sock, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + do { + error = 0; /* reset error */ + ret = wolfSSL_read(ssl, rx_buf, RX_BUF_SIZE - 1); + if (ret < 0) { + error = wolfSSL_get_error(ssl, 0); + if (error != SSL_ERROR_WANT_READ) { + APP_TRACE_INFO(("wolfSSL_read failed, error = %d\r\n", error)); + NetSock_Close(sock, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + OSTimeDlyHMSM(0u, 0u, 1u, 0u, OS_OPT_TIME_HMSM_STRICT, &os_err); + } else if (ret > 0) { + rx_buf[ret] = 0; + APP_TRACE_INFO(("%s\r\n", rx_buf)); + } + } while (error == SSL_ERROR_WANT_READ); + wolfSSL_shutdown(ssl); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + wolfSSL_Cleanup(); + NetSock_Close(sock, &err); + return 0; +} diff --git a/IDE/ECLIPSE/MICRIUM/client_wolfssl.h b/IDE/ECLIPSE/MICRIUM/client_wolfssl.h new file mode 100644 index 000000000..d7231ef19 --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/client_wolfssl.h @@ -0,0 +1,35 @@ +/* client_wolfssl.h + * + * Copyright (C) 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 __CLIENT_WOLFSSL_H__ +#define __CLIENT_WOLFSSL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int wolfssl_client_test(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* CLIENT_WOLFSSL_H */ diff --git a/IDE/ECLIPSE/MICRIUM/include.am b/IDE/ECLIPSE/MICRIUM/include.am new file mode 100644 index 000000000..be7b1dfa5 --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/include.am @@ -0,0 +1,12 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +EXTRA_DIST += \ + IDE/ECLIPSE/MICRIUM/README.md \ + IDE/ECLIPSE/MICRIUM/user_settings.h \ + IDE/ECLIPSE/MICRIUM/client_wolfssl.h \ + IDE/ECLIPSE/MICRIUM/server_wolfssl.h \ + IDE/ECLIPSE/MICRIUM/client_wolfssl.c \ + IDE/ECLIPSE/MICRIUM/server_wolfssl.c \ + IDE/ECLIPSE/MICRIUM/wolfsslRunTests.c diff --git a/IDE/ECLIPSE/MICRIUM/server_wolfssl.c b/IDE/ECLIPSE/MICRIUM/server_wolfssl.c new file mode 100644 index 000000000..66d1a9774 --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/server_wolfssl.c @@ -0,0 +1,335 @@ +/* server_wolfssl.c + * + * Copyright (C) 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 +#include + +#include "wolfssl/ssl.h" +#include "server_wolfssl.h" + +#define TLS_SERVER_PORT 11111 +#define TX_BUF_SIZE 64 +#define RX_BUF_SIZE 1024 +#define TCP_SERVER_CONN_Q_SIZE 1 + +/* derived from wolfSSL/certs/server-ecc.der */ + +static const CPU_INT08U server_ecc_der_256[] = { 0x30, 0x82, 0x03, 0x10, + 0x30, 0x82, 0x02, 0xB5, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, + 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30, 0x0A, 0x06, 0x08, + 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x81, 0x8F, 0x31, + 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, + 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57, + 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30, + 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74, + 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A, + 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30, + 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31, + 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, + 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, + 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, + 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, + 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, + 0x17, 0x0D, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31, 0x32, 0x30, 0x30, 0x37, + 0x33, 0x38, 0x5A, 0x17, 0x0D, 0x31, 0x39, 0x30, 0x35, 0x30, 0x38, 0x32, + 0x30, 0x30, 0x37, 0x33, 0x38, 0x5A, 0x30, 0x81, 0x8F, 0x31, 0x0B, 0x30, + 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, + 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57, 0x61, 0x73, + 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30, 0x0E, 0x06, + 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6C, + 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x07, + 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30, 0x0A, 0x06, + 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31, 0x18, 0x30, + 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, + 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, + 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, + 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, + 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, + 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, + 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xBB, + 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6, 0x4A, 0xA5, 0x04, 0xC3, 0x3C, + 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE, 0x94, 0xEA, 0x2B, 0xFA, 0xCB, + 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61, 0x02, 0xE9, 0xAF, 0x4D, 0xD3, + 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92, 0x21, 0x7F, 0xF0, 0xCF, 0x18, + 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8, 0x20, 0x58, 0x33, 0x0B, 0x80, + 0x34, 0x89, 0xD8, 0xA3, 0x81, 0xF7, 0x30, 0x81, 0xF4, 0x30, 0x1D, 0x06, + 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x5D, 0x5D, 0x26, 0xEF, + 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23, + 0xEF, 0xB2, 0x89, 0x30, 0x30, 0x81, 0xC4, 0x06, 0x03, 0x55, 0x1D, 0x23, + 0x04, 0x81, 0xBC, 0x30, 0x81, 0xB9, 0x80, 0x14, 0x5D, 0x5D, 0x26, 0xEF, + 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23, + 0xEF, 0xB2, 0x89, 0x30, 0xA1, 0x81, 0x95, 0xA4, 0x81, 0x92, 0x30, 0x81, + 0x8F, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, + 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, + 0x0A, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, + 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, + 0x61, 0x74, 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, + 0x04, 0x0A, 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, + 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, + 0x43, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, + 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, + 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, + 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, + 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, + 0x82, 0x09, 0x00, 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30, + 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, + 0xFF, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, + 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xF1, 0xD0, 0xA6, + 0x3E, 0x83, 0x33, 0x24, 0xD1, 0x7A, 0x05, 0x5F, 0x1E, 0x0E, 0xBD, 0x7D, + 0x6B, 0x33, 0xE9, 0xF2, 0x86, 0xF3, 0xF3, 0x3D, 0xA9, 0xEF, 0x6A, 0x87, + 0x31, 0xB3, 0xB7, 0x7E, 0x50, 0x02, 0x21, 0x00, 0xF0, 0x60, 0xDD, 0xCE, + 0xA2, 0xDB, 0x56, 0xEC, 0xD9, 0xF4, 0xE4, 0xE3, 0x25, 0xD4, 0xB0, 0xC9, + 0x25, 0x7D, 0xCA, 0x7A, 0x5D, 0xBA, 0xC4, 0xB2, 0xF6, 0x7D, 0x04, 0xC7, + 0xBD, 0x62, 0xC9, 0x20 }; + +/* derived from wolfSSL/certs/ecc-key.der */ + +static const CPU_INT08U ecc_key_der_256[] = { 0x30, 0x77, 0x02, 0x01, 0x01, + 0x04, 0x20, 0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85, 0xA1, 0x38, + 0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC, 0xC4, 0x03, 0x8D, 0x53, 0x35, 0x04, + 0xFA, 0x6C, 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8, 0x09, 0x8C, 0xA0, 0x0A, + 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0xA1, 0x44, + 0x03, 0x42, 0x00, 0x04, 0xBB, 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6, + 0x4A, 0xA5, 0x04, 0xC3, 0x3C, 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE, + 0x94, 0xEA, 0x2B, 0xFA, 0xCB, 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61, + 0x02, 0xE9, 0xAF, 0x4D, 0xD3, 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92, + 0x21, 0x7F, 0xF0, 0xCF, 0x18, 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8, + 0x20, 0x58, 0x33, 0x0B, 0x80, 0x34, 0x89, 0xD8 }; + + +int wolfssl_server_test(void) +{ + NET_ERR err; + NET_SOCK_ID sock_listen; + NET_SOCK_ID sock_req; + NET_SOCK_ADDR_IPv4 server_addr; + NET_SOCK_ADDR_LEN server_addr_len; + NET_SOCK_ADDR_IPv4 client_sock_addr_ip; + NET_SOCK_ADDR_LEN client_sock_addr_ip_size; + CPU_CHAR rx_buf[RX_BUF_SIZE]; + CPU_CHAR tx_buf[TX_BUF_SIZE]; + CPU_BOOLEAN attempt_conn; + OS_ERR os_err; + WOLFSSL * ssl; + WOLFSSL_CTX * ctx; + int tx_buf_sz = 0, ret = 0, error = 0; + + #ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); + #endif + + /* wolfSSL INIT and CTX SETUP */ + + wolfSSL_Init(); + + /* SET UP NETWORK SOCKET */ + + APP_TRACE_INFO(("Opening network socket...\r\n")); + sock_listen = NetSock_Open(NET_SOCK_ADDR_FAMILY_IP_V4, + NET_SOCK_TYPE_STREAM, + NET_SOCK_PROTOCOL_TCP, + &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Open, err = %d\r\n", (int) err)); + return -1; + } + + APP_TRACE_INFO(("Clearing memory for server_addr struct\r\n")); + server_addr_len = sizeof(server_addr); + Mem_Clr((void *) &server_addr, (CPU_SIZE_T) server_addr_len); + + APP_TRACE_INFO(("Setting up server_addr struct\r\n")); + server_addr.AddrFamily = NET_SOCK_ADDR_FAMILY_IP_V4; + server_addr.Addr = NET_UTIL_HOST_TO_NET_32(NET_SOCK_ADDR_IP_V4_WILDCARD); + server_addr.Port = NET_UTIL_HOST_TO_NET_16(TLS_SERVER_PORT); + + NetSock_Bind((NET_SOCK_ID) sock_listen, + (NET_SOCK_ADDR*) &server_addr, + (NET_SOCK_ADDR_LEN) NET_SOCK_ADDR_SIZE, + (NET_ERR*) &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Bind, err = %d\r\n", (int) err)); + NetSock_Close(sock_listen, &err); + return -1; + } + + ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()); + if (ctx == 0) { + APP_TRACE_INFO(("ERROR: wolfSSL_CTX_new failed\r\n")); + NetSock_Close(sock_listen, &err); + return -1; + } + APP_TRACE_INFO(("wolfSSL_CTX_new done\r\n")); + + ret = wolfSSL_CTX_use_certificate_buffer(ctx, + server_ecc_der_256, + sizeof(server_ecc_der_256), + SSL_FILETYPE_ASN1); + if (ret != SSL_SUCCESS) { + APP_TRACE_INFO( + ("ERROR: wolfSSL_CTX_use_certificate_buffer() failed\r\n")); + NetSock_Close(sock_listen, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, + ecc_key_der_256, + sizeof(ecc_key_der_256), + SSL_FILETYPE_ASN1); + if (ret != SSL_SUCCESS) { + APP_TRACE_INFO( + ("ERROR: wolfSSL_CTX_use_PrivateKey_buffer() failed\r\n")); + NetSock_Close(sock_listen, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + /* accept client socket connections */ + + APP_TRACE_INFO(("Listening for client connection\r\n")); + + NetSock_Listen(sock_listen, TCP_SERVER_CONN_Q_SIZE, &err); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Listen, err = %d\r\n", (int) err)); + NetSock_Close(sock_listen, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + do { + client_sock_addr_ip_size = sizeof(client_sock_addr_ip); + sock_req = NetSock_Accept((NET_SOCK_ID) sock_listen, + (NET_SOCK_ADDR*) &client_sock_addr_ip, + (NET_SOCK_ADDR_LEN*) &client_sock_addr_ip_size, + (NET_ERR*) &err); + switch (err) { + case NET_SOCK_ERR_NONE: + attempt_conn = DEF_NO; + break; + case NET_ERR_INIT_INCOMPLETE: + case NET_SOCK_ERR_NULL_PTR: + case NET_SOCK_ERR_NONE_AVAIL: + case NET_SOCK_ERR_CONN_ACCEPT_Q_NONE_AVAIL: + attempt_conn = DEF_YES; + break; + case NET_SOCK_ERR_CONN_SIGNAL_TIMEOUT: + APP_TRACE_INFO( + ("NetSockAccept err = NET_SOCK_ERR_CONN_SIGNAL_TIMEOUT\r\n")); + attempt_conn = DEF_YES; + break; + default: + attempt_conn = DEF_NO; + break; + } + } while (attempt_conn == DEF_YES); + if (err != NET_SOCK_ERR_NONE) { + APP_TRACE_INFO(("ERROR: NetSock_Accept, err = %d\r\n", (int) err)); + NetSock_Close(sock_listen, &err); + return -1; + } + + APP_TRACE_INFO(("Got client connection! Starting TLS negotiation\r\n")); + /* set up wolfSSL session */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + APP_TRACE_INFO(("ERROR: wolfSSL_new() failed\r\n")); + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + wolfSSL_CTX_free(ctx); + return -1; + } + + APP_TRACE_INFO(("wolfSSL_new done\r\n")); + ret = wolfSSL_set_fd(ssl, sock_req); + if (ret != SSL_SUCCESS) { + APP_TRACE_INFO(("ERROR: wolfSSL_set_fd() failed\r\n")); + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + + APP_TRACE_INFO(("wolfSSL_set_fd done\r\n")); + do { + error = 0; /* reset error */ + if (ret != SSL_SUCCESS) { + error = wolfSSL_get_error(ssl, 0); + APP_TRACE_INFO( + ("ERROR: wolfSSL_accept() failed, err = %d\r\n", error)); + if (error != SSL_ERROR_WANT_READ) { + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + OSTimeDlyHMSM(0u, 0u, 0u, 500u, OS_OPT_TIME_HMSM_STRICT, &os_err); + } + } while ((ret != SSL_SUCCESS) && (error == SSL_ERROR_WANT_READ)); + + APP_TRACE_INFO(("wolfSSL_accept() ok...\r\n")); + + /* read client data */ + + error = 0; + Mem_Set(rx_buf, 0, RX_BUF_SIZE); + ret = wolfSSL_read(ssl, rx_buf, RX_BUF_SIZE - 1); + if (ret < 0) { + error = wolfSSL_get_error(ssl, 0); + if (error != SSL_ERROR_WANT_READ) { + APP_TRACE_INFO(("wolfSSL_read failed, error = %d\r\n", error)); + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + } + + APP_TRACE_INFO(("AFTER wolfSSL_read() call, ret = %d\r\n", ret)); + if (ret > 0) { + rx_buf[ret] = 0; + APP_TRACE_INFO(("Client sent: %s\r\n", rx_buf)); + } + /* write response to client */ + Mem_Set(tx_buf, 0, TX_BUF_SIZE); + tx_buf_sz = 22; + Str_Copy_N(tx_buf, "I hear ya fa shizzle!\n", tx_buf_sz); + if (wolfSSL_write(ssl, tx_buf, tx_buf_sz) != tx_buf_sz) { + error = wolfSSL_get_error(ssl, 0); + APP_TRACE_INFO(("ERROR: wolfSSL_write() failed, err = %d\r\n", error)); + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return -1; + } + ret = wolfSSL_shutdown(ssl); + if (ret == SSL_SHUTDOWN_NOT_DONE) + wolfSSL_shutdown(ssl); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + wolfSSL_Cleanup(); + NetSock_Close(sock_req, &err); + NetSock_Close(sock_listen, &err); + return 0; +} diff --git a/IDE/ECLIPSE/MICRIUM/server_wolfssl.h b/IDE/ECLIPSE/MICRIUM/server_wolfssl.h new file mode 100644 index 000000000..538ae2f75 --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/server_wolfssl.h @@ -0,0 +1,35 @@ +/* server_wolfssl.h + * + * Copyright (C) 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 __SERVER_WOLFSSL_H__ +#define __SERVER_WOLFSSL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int wolfssl_server_test(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SERVER_WOLFSSL_H */ diff --git a/IDE/ECLIPSE/MICRIUM/user_settings.h b/IDE/ECLIPSE/MICRIUM/user_settings.h new file mode 100644 index 000000000..bb30372ca --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/user_settings.h @@ -0,0 +1,77 @@ +/* user_setting.h + * + * Copyright (C) 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 MICRIUM_USER_SETTINGS_H_ +#define MICRIUM_USER_SETTINGS_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +#define MICRIUM + +/* You can select one or all of the following tests */ +#define WOLFSSL_WOLFCRYPT_TEST +#define WOLFSSL_BENCHMARK_TEST +#define WOLFSSL_CLIENT_TEST +#define WOLFSSL_SERVER_TEST + +/* adjust x to seconds since Jan 01 1970. (UTC) +https://www.unixtimestamp.com/ +*/ +#define CURRENT_UNIX_TS 1542605837 + +/* When using Windows simulator, you must define USE_WINDOWS_API for test.h to build */ +#ifdef _WIN32 +#define USE_WINDOWS_API +#endif + +#define NO_FILESYSTEM +#define SIZEOF_LONG_LONG 8 + +/* prevents from including multiple definition of main() */ +#define NO_MAIN_DRIVER +#define NO_TESTSUITE_MAIN_DRIVER + +/* wolfSSL_dtls_get_current_timeout is called from MicriumReceiveFrom */ +#define WOLFSSL_DTLS + +/* includes certificate test buffers via header files */ +#define USE_CERT_BUFFERS_2048 +/*use kB instead of mB for embedded benchmarking*/ +#define BENCH_EMBEDDED +#define NO_ECC_VECTOR_TEST +#define NO_WRITE_TEMP_FILES + +#define XSNPRINTF snprintf + +#define HAVE_AESGCM +#define WOLFSSL_SHA512 +#define HAVE_ECC +#define HAVE_CURVE25519 +#define CURVE25519_SMALL +#define HAVE_ED25519 + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif diff --git a/IDE/ECLIPSE/MICRIUM/wolfsslRunTests.c b/IDE/ECLIPSE/MICRIUM/wolfsslRunTests.c new file mode 100644 index 000000000..581e6256c --- /dev/null +++ b/IDE/ECLIPSE/MICRIUM/wolfsslRunTests.c @@ -0,0 +1,80 @@ +/* wolfsslRunTests.c + * + * Copyright (C) 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 /* master includes from Micrium Freescale Kinetis K70*/ + +#include + +/* +* Description : This function runs wolfssl tests. +* Caller(s) : main() in app.c +* Note(s) : none. +*/ + +int wolfsslRunTests (void) +{ + CLK_ERR err; + CLK_TS_SEC ts_unix_sec; + CPU_BOOLEAN valid; + static int initialized = 0; + + if(!initialized) { + Clk_Init(&err); + + if (err == CLK_ERR_NONE) { + APP_TRACE_INFO(("Clock module successfully initialized\n")); + } else { + APP_TRACE_INFO(("Clock module initialization failed\n")); + return -1; + } + + valid = Clk_GetTS_Unix(&ts_unix_sec); + + if (valid == DEF_OK) { + APP_TRACE_INFO(("Timestamp Unix = %u\n", ts_unix_sec)); + } else { + APP_TRACE_INFO(("Get TS Unix error\n")); + } + #if defined(CURRENT_UNIX_TS) + valid = Clk_SetTS_Unix(CURRENT_UNIX_TS); + if (valid != DEF_OK) { + APP_TRACE_INFO(("Clk_SetTS_Unix error\n")); + return -1; + } + #endif + initialized = 1; + } + + #if defined(WOLFSSL_WOLFCRYPT_TEST) + wolfcrypt_test(NULL); + #endif + #if defined(WOLFSSL_BENCHMARK_TEST) + benchmark_test(NULL); + #endif + #if defined(WOLFSSL_CLIENT_TEST) + wolfssl_client_test(); + #endif + #if defined(WOLFSSL_SERVER_TEST) + wolfssl_server_test(); + #endif + + return 0; +} diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 253144e26..7e786f5aa 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -60,6 +60,11 @@ #undef printf #define printf PRINTF +#elif defined(MICRIUM) + #include + void BSP_Ser_Printf (CPU_CHAR* format, ...); + #undef printf + #define printf BSP_Ser_Printf #else #include #endif @@ -439,7 +444,7 @@ static const char* bench_Usage_msg1[][10] = { "- アルゴリズムのベンチマークを実施します。\n 利用可能なアルゴリズムは下記を含みます:\n", "-lng 指定された言語でベンチマーク結果を表示します。\n 0: 英語、 1: 日本語\n", " ブロックサイズをバイト単位で指定します。\n", - }, + }, }; static const char* bench_result_words1[][4] = { @@ -455,7 +460,7 @@ static const char* bench_result_words1[][4] = { static const char* bench_desc_words[][9] = { /* 0 1 2 3 4 5 6 7 8 */ {"public", "private", "key gen", "agree" , "sign", "verify", "encryption", "decryption", NULL}, /* 0 English */ - {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , NULL}, /* 1 Japanese */ + {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , NULL}, /* 1 Japanese */ }; #endif @@ -977,7 +982,7 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), countSz); } else { XSNPRINTF(msg, sizeof(msg), "%-16s%s %5.0f %s %s %5.3f %s, %8.3f %s/s", - desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, word[0], total, word[1], + desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, word[0], total, word[1], persec, blockType); SHOW_INTEL_CYCLES(msg, sizeof(msg), countSz); } @@ -1637,11 +1642,9 @@ int benchmark_test(void *args) (void)args; - printf( -"------------------------------------------------------------------------------" -"\n wolfSSL version %s\n" -"------------------------------------------------------------------------------" -"\n", LIBWOLFSSL_VERSION_STRING); + printf("------------------------------------------------------------------------------\n"); + printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING); + printf("------------------------------------------------------------------------------\n"); ret = benchmark_init(); if (ret != 0) @@ -3854,10 +3857,20 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING], const int len = (int)XSTRLEN((char*)messageStr); double start = 0.0f; const char**desc = bench_desc_words[lng_index]; - DECLARE_VAR_INIT(message, byte, len, messageStr, HEAP_HINT); - DECLARE_ARRAY(enc, byte, BENCH_MAX_PENDING, rsaKeySz/8, HEAP_HINT); - DECLARE_ARRAY(out, byte, BENCH_MAX_PENDING, rsaKeySz/8, HEAP_HINT); + + #ifdef USE_CERT_BUFFERS_1024 + DECLARE_ARRAY(enc, byte, BENCH_MAX_PENDING, 128, HEAP_HINT); + DECLARE_ARRAY(out, byte, BENCH_MAX_PENDING, 128, HEAP_HINT); + #elif defined(USE_CERT_BUFFERS_2048) + DECLARE_ARRAY(enc, byte, BENCH_MAX_PENDING, 256, HEAP_HINT); + DECLARE_ARRAY(out, byte, BENCH_MAX_PENDING, 256, HEAP_HINT); + #elif defined(USE_CERT_BUFFERS_3072) + DECLARE_ARRAY(enc, byte, BENCH_MAX_PENDING, 384, HEAP_HINT); + DECLARE_ARRAY(out, byte, BENCH_MAX_PENDING, 384, HEAP_HINT); + #else + #error "need a cert buffer size" + #endif /* USE_CERT_BUFFERS */ if (!rsa_sign_verify) { /* begin public RSA */ @@ -5044,6 +5057,14 @@ exit_ed_verify: #elif defined(WOLFSSL_SGX) double current_time(int reset); +#elif defined(MICRIUM) + double current_time(int reset) + { + CPU_ERR err; + + (void)reset; + return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err); + } #else #include diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index b5da67186..f091d9f01 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -210,7 +210,9 @@ void WOLFSSL_TIME(int count) /* Declare sprintf for ocall */ int sprintf(char* buf, const char *fmt, ...); #elif defined(MICRIUM) - #include + #if (BSP_SER_COMM_EN == DEF_ENABLED) + #include + #endif #elif defined(WOLFSSL_USER_LOG) /* user includes their own headers */ #elif defined(WOLFSSL_ESPIDF) diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index c3ece65cf..c5b8ddd95 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -1517,6 +1517,9 @@ time_t micrium_time(time_t* timer) Clk_GetTS_Unix(&sec); + if (timer != NULL) + *timer = sec; + return (time_t) sec; } diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 5ec5792a0..8ad27739f 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -1046,7 +1046,7 @@ extern void uITRON4_free(void *p) ; #define HAVE_ECC224 #undef NO_ECC256 #define HAVE_ECC384 - #endif + #endif #endif #endif #endif @@ -1185,12 +1185,6 @@ extern void uITRON4_free(void *p) ; #define CUSTOM_RAND_TYPE RAND_NBR #define CUSTOM_RAND_GENERATE Math_Rand #endif - - #define WOLFSSL_TYPES - typedef CPU_INT08U byte; - typedef CPU_INT16U word16; - typedef CPU_INT32U word32; - #define STRING_USER #define XSTRLEN(pstr) ((CPU_SIZE_T)Str_Len((CPU_CHAR *)(pstr))) #define XSTRNCPY(pstr_dest, pstr_src, len_max) \ @@ -1398,7 +1392,7 @@ extern void uITRON4_free(void *p) ; #if !defined(HAVE_FIPS) && !defined(NO_RSA) #define WC_RSA_BLINDING #endif - + #define NO_FILESYSTEM #define ECC_TIMING_RESISTANT #define TFM_TIMING_RESISTANT