From f7baf9e3925bf83c873ad84d64fcc40e5318da9e Mon Sep 17 00:00:00 2001 From: lchristina26 Date: Fri, 8 Jan 2016 11:54:46 -0700 Subject: [PATCH] settings for Arduino functionality --- IDE/ARDUINO/README.md | 13 +++ IDE/ARDUINO/include.am | 8 ++ IDE/ARDUINO/sketches/wolfssl_client.ino | 144 ++++++++++++++++++++++++ IDE/ARDUINO/wolfssl-arduino.sh | 15 +++ IDE/include.am | 1 + INSTALL | 17 ++- wolfssl/wolfcrypt/settings.h | 21 ++++ 7 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 IDE/ARDUINO/README.md create mode 100644 IDE/ARDUINO/include.am create mode 100644 IDE/ARDUINO/sketches/wolfssl_client.ino create mode 100755 IDE/ARDUINO/wolfssl-arduino.sh diff --git a/IDE/ARDUINO/README.md b/IDE/ARDUINO/README.md new file mode 100644 index 000000000..254aed531 --- /dev/null +++ b/IDE/ARDUINO/README.md @@ -0,0 +1,13 @@ +### wolfSSL with Arduino + +##### reformat-wolfssl.sh +This is a shell script that will re-organize the wolfSSL library to be +compatible with Arduino projects. The Arduino IDE requires a library's source +files to be in the library's root directory with a header file in the name of +the library. This script moves all src/ files to the root wolfssl directory and +creates a stub header file called wolfssl.h. + +To configure wolfSSL with Arduino, enter the following from within the +wolfssl/IDE/ARDUINO directory: + + sh reformat-wolfssl.sh diff --git a/IDE/ARDUINO/include.am b/IDE/ARDUINO/include.am new file mode 100644 index 000000000..2aef28d91 --- /dev/null +++ b/IDE/ARDUINO/include.am @@ -0,0 +1,8 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +EXTRA_DIST+= IDE/ARDUINO/README.md +EXTRA_DIST+= IDE/ARDUINO/sketches/wolfssl_client.ino +EXTRA_DIST+= IDE/ARDUINO/wolfssl-arduino.sh + diff --git a/IDE/ARDUINO/sketches/wolfssl_client.ino b/IDE/ARDUINO/sketches/wolfssl_client.ino new file mode 100644 index 000000000..f91f85a4e --- /dev/null +++ b/IDE/ARDUINO/sketches/wolfssl_client.ino @@ -0,0 +1,144 @@ +/* wolfssl_client.ino + * + * Copyright (C) 2006-2015 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 + */ + +#include +#include +#include + +const char host[] = "192.168.1.148"; // server to connect to +int port = 11111; // port on server to connect to + +int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx); +int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx); +int reconnect = 10; + +EthernetClient client; + +WOLFSSL_CTX* ctx = 0; +WOLFSSL* ssl = 0; +WOLFSSL_METHOD* method = 0; + +void setup() { + Serial.begin(9600); + + method = wolfTLSv1_2_client_method(); + if (method == NULL) { + Serial.println("unable to get method"); + return; + } + ctx = wolfSSL_CTX_new(method); + if (ctx == NULL) { + Serial.println("unable to get ctx"); + return; + } + // initialize wolfSSL using callback functions + wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); + wolfSSL_SetIOSend(ctx, EthernetSend); + wolfSSL_SetIORecv(ctx, EthernetReceive); + + return; +} + +int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx) { + int sent = 0; + + sent = client.write((byte*)msg, sz); + + return sent; +} + +int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx) { + int ret = 0; + + while (client.available() > 0 && ret < sz) { + reply[ret++] = client.read(); + } + + return ret; +} + +void loop() { + int err = 0; + int input = 0; + int sent = 0; + int total_input = 0; + char msg[32] = "hello wolfssl!"; + int msgSz = (int)strlen(msg); + char errBuf[80]; + char reply[80]; + WOLFSSL_CIPHER* cipher; + + if (reconnect) { + reconnect--; + if (client.connect(host, port)) { + + Serial.print("Connected to "); + Serial.println(host); + ssl = wolfSSL_new(ctx); + if (ssl == NULL) { + err = wolfSSL_get_error(ssl, 0); + wolfSSL_ERR_error_string(err, errBuf); + Serial.print("Unable to get SSL object. Error = "); + Serial.println(errBuf); + } + + Serial.print("SSL version is "); + Serial.println(wolfSSL_get_version(ssl)); + + + + if ((wolfSSL_write(ssl, msg, strlen(msg))) == msgSz) { + cipher = wolfSSL_get_current_cipher(ssl); + Serial.print("SSL cipher suite is "); + Serial.println(wolfSSL_CIPHER_get_name(cipher)); + Serial.print("Server response: "); + while (client.available() || wolfSSL_pending(ssl)) { + input = wolfSSL_read(ssl, reply, sizeof(reply) - 1); + total_input += input; + if ( input > 0 ) { + reply[input] = '\0'; + Serial.print(reply); + } else if (input < 0) { + err = wolfSSL_get_error(ssl, 0); + wolfSSL_ERR_error_string(err, errBuf); + Serial.print("wolfSSL_read failed. Error: "); + Serial.println(errBuf); + } else { + Serial.println(); + } + } + } else { + Serial.println("SSL_write failed"); + } + + if (ssl != NULL) + wolfSSL_free(ssl); + + client.stop(); + Serial.println("Connection complete."); + reconnect = 0; + } else { + Serial.println("Trying to reconnect..."); + } + } + delay(1000); +} + diff --git a/IDE/ARDUINO/wolfssl-arduino.sh b/IDE/ARDUINO/wolfssl-arduino.sh new file mode 100755 index 000000000..7d6b27088 --- /dev/null +++ b/IDE/ARDUINO/wolfssl-arduino.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# this script will reformat the wolfSSL source code to be compatible with +# an Arduino project +# run as bash ./wolfssl-arduino.sh + +DIR=${PWD##*/} + +if [ "$DIR" == "ARDUINO" ]; then + cp ../../src/*.c ../../ + cp ../../wolfcrypt/src/*.c ../../ + echo "/* stub header file for Arduino compatibility */" >> ../../wolfssl.h +else + echo "ERROR: You must be in the IDE/ARDUINO directory to run this script" +fi diff --git a/IDE/include.am b/IDE/include.am index 008e6ddda..0a421feb7 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -6,5 +6,6 @@ include IDE/iOS/include.am include IDE/WIN/include.am include IDE/WORKBENCH/include.am include IDE/ROWLEY-CROSSWORKS-ARM/include.am +include IDE/ARDUINO/include.am EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL diff --git a/INSTALL b/INSTALL index 8a11c3e23..f5fdbb858 100644 --- a/INSTALL +++ b/INSTALL @@ -38,12 +38,19 @@ Please see the README in mqx -8. Porting to a new platform +8. Building with Rowley CrossWorks for ARM + + Use the CrossWorks project in IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp + There is a README.md in IDE/ROWLEY-CROSSWORKS-ARM with more information + +9. Building with Arduino + + Use the script IDE/ARDUINO/wolfssl-arduino.sh to reformat the wolfSSL + library for compatibility with the Arduino IDE. There is a README.md in + IDE/ARDUINO for detailed instructions. + +10. Porting to a new platform Please see section 2.4 in the manual: http://www.wolfssl.com/yaSSL/Docs-cyassl-manual-2-building-cyassl.html -9. Building with Rowley CrossWorks for ARM - - Use the CrossWorks project in IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp - There is a README.md in IDE/ROWLEY-CROSSWORKS-ARM with more information diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index d89b64303..b728c8d34 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -132,6 +132,9 @@ /* Uncomment next line to enable deprecated less secure static RSA suites */ /* #define WOLFSSL_STATIC_RSA */ +/* Uncomment next line if building for ARDUINO */ +/* #define WOLFSSL_ARDUINO */ + #include #ifdef WOLFSSL_USER_SETTINGS @@ -318,6 +321,24 @@ #endif +#ifdef WOLFSSL_ARDUINO + #define NO_WRITEV + #define NO_WOLFSSL_DIR + #define SINGLE_THREADED + #define NO_DEV_RANDOM + #ifndef INTEL_GALILEO /* Galileo has time.h compatibility */ + #define TIME_OVERRIDES /* must define XTIME and XGMTIME externally */ + #endif + #define WOLFSSL_USER_IO + #define HAVE_ECC + #define NO_DH + #define NO_SESSION_CACHE + #define USE_SLOW_SHA + #define NO_WOLFSSL_SERVER + #define NO_ERROR_STRINGS +#endif + + /* Micrium will use Visual Studio for compilation but not the Win32 API */ #if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) && !defined(FREERTOS_TCP)\ && !defined(EBSNET) && !defined(WOLFSSL_EROAD)