From 861fec1dc64d5fb4e2e30ee4b78ba13ef2bf1074 Mon Sep 17 00:00:00 2001 From: Naruto TAKAHASHI Date: Tue, 13 Mar 2018 00:58:49 +0900 Subject: [PATCH] porting mynewt --- IDE/include.am | 1 + IDE/mynewt/README.md | 71 ++++++++ IDE/mynewt/apps.wolfcrypttest.pkg.yml | 31 ++++ IDE/mynewt/crypto.wolfssl.pkg.yml | 30 ++++ IDE/mynewt/crypto.wolfssl.syscfg.yml | 5 + IDE/mynewt/include.am | 10 ++ IDE/mynewt/setup.sh | 73 ++++++++ src/internal.c | 27 +++ src/wolfio.c | 210 ++++++++++++++++++++++++ wolfcrypt/src/logging.c | 10 ++ wolfcrypt/src/port/mynewt/mynewt_port.c | 147 +++++++++++++++++ wolfcrypt/src/random.c | 19 +++ wolfcrypt/src/wc_port.c | 16 ++ wolfcrypt/test/test.c | 25 +++ wolfssl/internal.h | 9 +- wolfssl/wolfcrypt/settings.h | 38 +++++ wolfssl/wolfcrypt/wc_port.h | 22 +++ wolfssl/wolfio.h | 13 +- 18 files changed, 754 insertions(+), 3 deletions(-) create mode 100644 IDE/mynewt/README.md create mode 100644 IDE/mynewt/apps.wolfcrypttest.pkg.yml create mode 100644 IDE/mynewt/crypto.wolfssl.pkg.yml create mode 100644 IDE/mynewt/crypto.wolfssl.syscfg.yml create mode 100644 IDE/mynewt/include.am create mode 100755 IDE/mynewt/setup.sh create mode 100644 wolfcrypt/src/port/mynewt/mynewt_port.c diff --git a/IDE/include.am b/IDE/include.am index 5a8b493ab..65d249d3d 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -16,5 +16,6 @@ include IDE/OPENSTM32/include.am include IDE/VS-ARM/include.am include IDE/GCC-ARM/include.am include IDE/CSBENCH/include.am +include IDE/mynewt/include.am EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR diff --git a/IDE/mynewt/README.md b/IDE/mynewt/README.md new file mode 100644 index 000000000..115d984f8 --- /dev/null +++ b/IDE/mynewt/README.md @@ -0,0 +1,71 @@ +Apache Mynewt Port +============= + +## Overview + +This port is for Apache Mynewt available [here](https://mynewt.apache.org/). + +It provides follows mynewt packages. + +- crypto/wolfssl + - wolfssl library +- apps/wolfcrypttest + - wolfcrypt unit test application + +## How to setup + +### delopy wolfssl source to mynewt project +Specify the path of the mynewt project and execute `wolfssl/IDE/mynewt/setup.sh`. + +```bash +./IDE/mynewt/setup.sh /path/to/myproject_path +``` + +This script will deploy wolfssl's mynewt package described in the Overview to the mynewt project. + +## Customization +### logging + +To enable logging, please append `-DDEBUG_WOLFSSL` to `crypto.wolfssl.pkg.yml`(or `/path/to/myproject_path/crypto/wolfssl/pkg.yml`) in `pkg.cflags:` and inject dependency of mynewt log modules. + +mynewt/crypto.wolfssl.pkg.yml +```yaml +pkg.req_apis: + ... + - log + - stats + - console +pkg.cflags: -DWOLFSSL_APACHE_MYNEWT ... -DDEBUG_WOLFSSL +``` + +Please add `wolfSSL_Debugging_ON()` to application code. + +When it executes application, display tty device for output display devices. +please confirm with the `cat /dev/ttysXXX` command or `kermit` or `screen`. + +## example setting example applications to targets + +This section eplains how to set wolfssl example application to target device. + +Please execute follows commands at only once. + +create wolfcrypttest_sim that is simulator device for unit test of wolfcrypt. + +``` +cd /path/to/myproject_path +newt target create wolfcrypttest_sim +newt target set wolfcrypttest_sim app=apps/wolfcrypttest +newt target set wolfcrypttest_sim bsp=@apache-mynewt-core/hw/bsp/native +newt target set wolfcrypttest_sim build_profile=debug +``` + +## build & test + +build and execute wolfcrypttest_sim + +``` +newt clean wolfcrypttest_sim +newt build wolfcrypttest_sim +./bin/targets/wolfcrypttest_sim/app/apps/wolfcrypttest/wolfcrypttest.elf +``` + diff --git a/IDE/mynewt/apps.wolfcrypttest.pkg.yml b/IDE/mynewt/apps.wolfcrypttest.pkg.yml new file mode 100644 index 000000000..3a2e18e9d --- /dev/null +++ b/IDE/mynewt/apps.wolfcrypttest.pkg.yml @@ -0,0 +1,31 @@ +# 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 +# + +pkg.name: "apps/wolfcrypttest" +pkg.type: app +pkg.description: "wolfCrypt Test Application" +pkg.author: "wolfSSL " +pkg.homepage: "https://www.wolfssl.com" +pkg.keywords: +pkg.deps: + - crypto/wolfssl + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/sys/console/full" + +pkg.cflags: -DWOLFSSL_APACHE_MYNEWT -Icrypto/wolfssl/src -Wno-error -DNO_FILESYSTEM -DWOLFSSL_IGNORE_FILE_WARN diff --git a/IDE/mynewt/crypto.wolfssl.pkg.yml b/IDE/mynewt/crypto.wolfssl.pkg.yml new file mode 100644 index 000000000..845040afb --- /dev/null +++ b/IDE/mynewt/crypto.wolfssl.pkg.yml @@ -0,0 +1,30 @@ +# 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 +# + +pkg.name: "crypto/wolfssl" +pkg.description: "wolfSSL Embedded SSL/TLS Library" +pkg.author: "wolfSSL" +pkg.homepage: "https://www.wolfssl.com" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/net/ip/mn_socket" + + +pkg.cflags: -DWOLFSSL_APACHE_MYNEWT -DNO_FILESYSTEM -Wno-error -DHAVE_TLS_EXTENSIONS -DHAVE_SUPPORTED_CURVES # -DDEBUG_WOLFSSL -DWOLFSSL_IGNORE_FILE_WARN diff --git a/IDE/mynewt/crypto.wolfssl.syscfg.yml b/IDE/mynewt/crypto.wolfssl.syscfg.yml new file mode 100644 index 000000000..93a151234 --- /dev/null +++ b/IDE/mynewt/crypto.wolfssl.syscfg.yml @@ -0,0 +1,5 @@ +syscfg.defs: + WOLFSSL_MNSOCK_MEM_BUF_COUNT: + value: 10 + WOLFSSL_MNSOCK_MEM_BUF_SIZE: + value: 2048 diff --git a/IDE/mynewt/include.am b/IDE/mynewt/include.am new file mode 100644 index 000000000..c19bf232b --- /dev/null +++ b/IDE/mynewt/include.am @@ -0,0 +1,10 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +EXTRA_DIST+= IDE/mynewt/README.md +EXTRA_DIST+= IDE/mynewt/apps.wolfcrypttest.pkg.yml +EXTRA_DIST+= IDE/mynewt/crypto.wolfssl.pkg.yml +EXTRA_DIST+= IDE/mynewt/crypto.wolfssl.syscfg.yml +EXTRA_DIST+= IDE/mynewt/setup.sh + diff --git a/IDE/mynewt/setup.sh b/IDE/mynewt/setup.sh new file mode 100755 index 000000000..7cec0a825 --- /dev/null +++ b/IDE/mynewt/setup.sh @@ -0,0 +1,73 @@ +#!/bin/bash -e + +# this scrypt deploy wolfssl and wolfcrypto source code to mynewt project +# run as bash "mynewt project root directory path" + +SCRIPTDIR=`dirname $0` +SCRIPTDIR=`cd $SCRIPTDIR && pwd -P` +WOLFSSL_MYNEWTDIR=${SCRIPTDIR} +WOLFSSL_MYNEWTDIR=`cd $WOLFSSL_MYNEWTDIR && pwd -P` +BASEDIR=${SCRIPTDIR}/../.. +BASEDIR=`cd ${BASEDIR} && pwd -P` + +if [ $# -ne 1 ]; then + echo "Usage: $0 'mynewt project root directory path'" 1>&2 + exit 1 +fi +MYNEWT_PROJECT=$1 + +if [ ! -d $MYNEWT_PROJECT ] || [ ! -f $MYNEWT_PROJECT/project.yml ]; then + echo "target directory is not mynewt project.: $MYNEWT_PROJECT" + exit 1 +fi + +# create wolfssl pkgs to mynewt project +pushd $MYNEWT_PROJECT > /dev/null +echo "create crypto/wolfssl pkg" +/bin/rm -rf crypto/wolfssl +newt pkg new crypto/wolfssl +/bin/rm -rf crypto/wolfssl/include +/bin/mkdir -p crypto/wolfssl/include +/bin/rm -rf crypto/wolfssl/src +/bin/mkdir -p crypto/wolfssl/src + +echo "create apps/wolfcrypttest pkg" +/bin/rm -rf apps/wolfcrypttest +newt pkg new -t app apps/wolfcrypttest +/bin/rm -rf apps/wolfcrypttest/include +/bin/rm -rf apps/wolfcrypttest/src +/bin/mkdir -p apps/wolfcrypttest/src + +popd > /dev/null # $MYNEWT_PROJECT + +# deploy source files and pkg +pushd $BASEDIR > /dev/null + +# deploy to crypto/wolfssl +echo "deploy wolfssl sources to crypto/wolfssl" +/bin/cp $WOLFSSL_MYNEWTDIR/crypto.wolfssl.pkg.yml $MYNEWT_PROJECT/crypto/wolfssl/pkg.yml +/bin/cp $WOLFSSL_MYNEWTDIR/crypto.wolfssl.syscfg.yml $MYNEWT_PROJECT/crypto/wolfssl/syscfg.yml + +/bin/mkdir -p $MYNEWT_PROJECT/crypto/wolfssl/src/src +/bin/cp ./src/*.c $MYNEWT_PROJECT/crypto/wolfssl/src/src + +/bin/mkdir -p $MYNEWT_PROJECT/crypto/wolfssl/src/wolfcrypt/src +/bin/cp ./wolfcrypt/src/*.asm $MYNEWT_PROJECT/crypto/wolfssl/src/wolfcrypt/src +/bin/cp ./wolfcrypt/src/*.c $MYNEWT_PROJECT/crypto/wolfssl/src/wolfcrypt/src +/bin/mkdir -p $MYNEWT_PROJECT/crypto/wolfssl/src/wolfcrypt/src/port/mynewt +/bin/cp ./wolfcrypt/src/port/mynewt/* $MYNEWT_PROJECT/crypto/wolfssl/src/wolfcrypt/src/port/mynewt + +/bin/mkdir -p $MYNEWT_PROJECT/crypto/wolfssl/include/wolfssl +/bin/cp -r wolfssl/* $MYNEWT_PROJECT/crypto/wolfssl/include/wolfssl/ + +# deploy to apps/wolfcrypttest +echo "deploy unit test sources to apps/wolfcrypttest" +/bin/cp $WOLFSSL_MYNEWTDIR/apps.wolfcrypttest.pkg.yml $MYNEWT_PROJECT/apps/wolfcrypttest/pkg.yml + +/bin/mkdir -p $MYNEWT_PROJECT/apps/wolfcrypttest/include/wolfcrypt/test +/bin/cp -r wolfcrypt/test/test.h $MYNEWT_PROJECT/apps/wolfcrypttest/include/wolfcrypt/test/ + +/bin/mkdir -p $MYNEWT_PROJECT/apps/wolfcrypttest/src +/bin/cp wolfcrypt/test/test.c $MYNEWT_PROJECT/apps/wolfcrypttest/src/main.c + +popd > /dev/null # $BASEDIR diff --git a/src/internal.c b/src/internal.c index 521921c29..413bf9281 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1416,6 +1416,9 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap) #ifdef HAVE_NETX ctx->CBIORecv = NetX_Receive; ctx->CBIOSend = NetX_Send; +#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + ctx->CBIORecv = Mynewt_Receive; + ctx->CBIOSend = Mynewt_Send; #endif #ifdef HAVE_NTRU @@ -4540,6 +4543,13 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) #ifdef HAVE_NETX ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */ ssl->IOCB_WriteCtx = &ssl->nxCtx; /* and write */ +#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + ssl->mnCtx = mynewt_ctx_new(); + if(!ssl->mnCtx) { + return MEMORY_E; + } + ssl->IOCB_ReadCtx = ssl->mnCtx; /* default Mynewt IO ctx, same for read */ + ssl->IOCB_WriteCtx = ssl->mnCtx; /* and write */ #endif /* initialize states */ @@ -5125,6 +5135,12 @@ void SSL_ResourceFree(WOLFSSL* ssl) } #endif #endif /* HAVE_TLS_EXTENSIONS */ +#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + if (ssl->mnCtx) { + mynewt_ctx_clear(ssl->mnCtx); + ssl->mnCtx = NULL; + } +#endif #ifdef HAVE_NETX if (ssl->nxCtx.nxPacket) nx_packet_release(ssl->nxCtx.nxPacket); @@ -6132,6 +6148,17 @@ ProtocolVersion MakeDTLSv1_2(void) /* returns number of 10ms ticks, so 100 ticks/sec */ return NU_Retrieve_Clock() / NU_TICKS_PER_SECOND; } +#elif defined(WOLFSSL_APACHE_MYNEWT) + + #include "os/os_time.h" + word32 LowResTimer(void) + { + word32 now; + struct os_timeval tv; + os_gettimeofday(&tv, NULL); + now = (word32)tv.tv_sec; + return now; + } #else /* Posix style time */ diff --git a/src/wolfio.c b/src/wolfio.c index 4b82475d2..72d86fa87 100644 --- a/src/wolfio.c +++ b/src/wolfio.c @@ -1852,4 +1852,214 @@ int MicriumGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx) #endif /* MICRIUM */ +#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + +#include +#include +#include + +#define MB_NAME "wolfssl_mb" + +typedef struct Mynewt_Ctx { + struct mn_socket *mnSocket; /* send/recv socket handler */ + struct mn_sockaddr_in mnSockAddrIn; /* socket address */ + struct os_mbuf *mnPacket; /* incoming packet handle + for short reads */ + int reading; /* reading flag */ + + /* private */ + void *mnMemBuffer; /* memory buffer for mempool */ + struct os_mempool mnMempool; /* mempool */ + struct os_mbuf_pool mnMbufpool; /* mbuf pool */ +} Mynewt_Ctx; + +void mynewt_ctx_clear(void *ctx) { + Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; + if(!mynewt_ctx) return; + + if(mynewt_ctx->mnPacket) { + os_mbuf_free_chain(mynewt_ctx->mnPacket); + mynewt_ctx->mnPacket = NULL; + } + os_mempool_clear(&mynewt_ctx->mnMempool); + XFREE(mynewt_ctx->mnMemBuffer, 0, 0); + XFREE(mynewt_ctx, 0, 0); +} + +/* return Mynewt_Ctx instance */ +void* mynewt_ctx_new() { + int rc = 0; + Mynewt_Ctx *mynewt_ctx = NULL; + int mem_buf_count = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_COUNT);; + int mem_buf_size = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_SIZE); + int mempool_bytes = OS_MEMPOOL_BYTES(mem_buf_count, mem_buf_size); + + mynewt_ctx = (Mynewt_Ctx *)XMALLOC(sizeof(struct Mynewt_Ctx), + NULL, DYNAMIC_TYPE_TMP_BUFFER); + if(!mynewt_ctx) return NULL; + + XMEMSET(mynewt_ctx, 0, sizeof(Mynewt_Ctx)); + mynewt_ctx->mnMemBuffer = XMALLOC(mempool_bytes, 0, 0); + if(!mynewt_ctx->mnMemBuffer) { + mynewt_ctx_clear((void*)mynewt_ctx); + return NULL; + } + + rc = os_mempool_init(&mynewt_ctx->mnMempool, + mem_buf_count, mem_buf_size, + mynewt_ctx->mnMemBuffer, MB_NAME); + if(rc != 0) { + mynewt_ctx_clear((void*)mynewt_ctx); + return NULL; + } + rc = os_mbuf_pool_init(&mynewt_ctx->mnMbufpool, &mynewt_ctx->mnMempool, + mem_buf_count, mem_buf_size); + if(rc != 0) { + mynewt_ctx_clear((void*)mynewt_ctx); + return NULL; + } + + return mynewt_ctx; +} + +static void mynewt_sock_writable(void *arg, int err); +static void mynewt_sock_readable(void *arg, int err); +static const union mn_socket_cb mynewt_sock_cbs = { + .socket.writable = mynewt_sock_writable, + .socket.readable = mynewt_sock_readable, +}; +static void mynewt_sock_writable(void *arg, int err) +{ + /* do nothing */ +} +static void mynewt_sock_readable(void *arg, int err) +{ + Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)arg; + if (err && mynewt_ctx->reading) { + mynewt_ctx->reading = 0; + } +} + +/* The Mynewt receive callback + * return : bytes read, or error + */ +int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; + int rc = 0; + struct mn_sockaddr_in from; + struct os_mbuf *m; + int read_sz = 0; + uint16_t total; + + if (mynewt_ctx == NULL || mynewt_ctx->mnSocket == NULL) { + WOLFSSL_MSG("Mynewt Recv NULL parameters"); + return WOLFSSL_CBIO_ERR_GENERAL; + } + + if(mynewt_ctx->mnPacket == NULL) { + mynewt_ctx->mnPacket = os_mbuf_get_pkthdr(&mynewt_ctx->mnMbufpool, 0); + if(mynewt_ctx->mnPacket == NULL) { + return MEMORY_E; + } + + mynewt_ctx->reading = 1; + while(mynewt_ctx->reading && rc == 0) { + rc = mn_recvfrom(mynewt_ctx->mnSocket, &m, (struct mn_sockaddr *) &from); + if(rc == MN_ECONNABORTED) { + rc = 0; + mynewt_ctx->reading = 0; + break; + } + if (!(rc == 0 || rc == MN_EAGAIN)) { + WOLFSSL_MSG("Mynewt Recv receive error"); + mynewt_ctx->reading = 0; + break; + } + if(rc == 0) { + int len = OS_MBUF_PKTLEN(m); + if(len == 0) { + break; + } + rc = os_mbuf_appendfrom(mynewt_ctx->mnPacket, m, 0, len); + if(rc != 0) { + WOLFSSL_MSG("Mynewt Recv os_mbuf_appendfrom error"); + break; + } + os_mbuf_free_chain(m); + m = NULL; + } else if(rc == MN_EAGAIN) { + /* continue to until reading all of packet data. */ + rc = 0; + break; + } + } + if(rc != 0) { + mynewt_ctx->reading = 0; + os_mbuf_free_chain(mynewt_ctx->mnPacket); + mynewt_ctx->mnPacket = NULL; + return rc; + } + } + + if(mynewt_ctx->mnPacket) { + total = OS_MBUF_PKTLEN(mynewt_ctx->mnPacket); + read_sz = (total >= sz)? sz : total; + + os_mbuf_copydata(mynewt_ctx->mnPacket, 0, read_sz, (void*)buf); + os_mbuf_adj(mynewt_ctx->mnPacket, read_sz); + + if (read_sz == total) { + WOLFSSL_MSG("Mynewt Recv Drained packet"); + os_mbuf_free_chain(mynewt_ctx->mnPacket); + mynewt_ctx->mnPacket = NULL; + } + } + + return read_sz; +} + +/* The Mynewt send callback + * return : bytes sent, or error + */ +int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) +{ + Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; + int rc = 0; + struct os_mbuf *m = NULL; + int write_sz = 0; + m = os_msys_get_pkthdr(sz, 0); + if (!m) { + WOLFSSL_MSG("Mynewt Send os_msys_get_pkthdr error"); + return WOLFSSL_CBIO_ERR_GENERAL; + } + rc = os_mbuf_copyinto(m, 0, buf, sz); + if (rc != 0) { + WOLFSSL_MSG("Mynewt Send os_mbuf_copyinto error"); + os_mbuf_free_chain(m); + return rc; + } + rc = mn_sendto(mynewt_ctx->mnSocket, m, (struct mn_sockaddr *)&mynewt_ctx->mnSockAddrIn); + if(rc != 0) { + WOLFSSL_MSG("Mynewt Send mn_sendto error"); + os_mbuf_free_chain(m); + return rc; + } + write_sz = sz; + return write_sz; +} + +/* like set_fd, but for default NetX context */ +void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_sockaddr_in* mnSockAddrIn) +{ + if (ssl && ssl->mnCtx) { + Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)ssl->mnCtx; + mynewt_ctx->mnSocket = mnSocket; + memcpy(&mynewt_ctx->mnSockAddrIn, mnSockAddrIn, sizeof(struct mn_sockaddr_in)); + mn_socket_set_cbs(mynewt_ctx->mnSocket, mnSocket, &mynewt_sock_cbs); + } +} + +#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ + #endif /* WOLFCRYPT_ONLY */ diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index fb7d46298..57505d165 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -117,6 +117,11 @@ static WC_INLINE double current_time(int reset) static wolfSSL_Logging_cb log_function = NULL; static int loggingEnabled = 0; +#if defined(WOLFSSL_APACHE_MYNEWT) +#include "log/log.h" +static struct log mynewt_log; +#endif /* WOLFSSL_APACHE_MYNEWT */ + #endif /* DEBUG_WOLFSSL */ @@ -137,6 +142,9 @@ int wolfSSL_Debugging_ON(void) { #ifdef DEBUG_WOLFSSL loggingEnabled = 1; +#if defined(WOLFSSL_APACHE_MYNEWT) + log_register("wolfcrypt", &mynewt_log, &log_console_handler, NULL, LOG_SYSLEVEL); +#endif /* WOLFSSL_APACHE_MYNEWT */ return 0; #else return NOT_COMPILED_IN; @@ -237,6 +245,8 @@ static void wolfssl_log(const int logLevel, const char *const logMessage) #elif defined(MQX_USE_IO_OLD) fprintf(_mqxio_stderr, "%s\n", logMessage); +#elif defined(WOLFSSL_APACHE_MYNEWT) + LOG_DEBUG(&mynewt_log, LOG_MODULE_DEFAULT, "%s\n", logMessage); #else fprintf(stderr, "%s\n", logMessage); #endif diff --git a/wolfcrypt/src/port/mynewt/mynewt_port.c b/wolfcrypt/src/port/mynewt/mynewt_port.c new file mode 100644 index 000000000..82940d6d9 --- /dev/null +++ b/wolfcrypt/src/port/mynewt/mynewt_port.c @@ -0,0 +1,147 @@ +/* mynewt_port.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 + */ + +#if defined(WOLFSSL_APACHE_MYNEWT) +#ifndef NO_FILESYSTEM +#include "fs/fs.h" +#define FILE struct fs_file + +FILE* mynewt_fopen(const char * restrict path, const char * restrict mode) +{ + FILE *file; + uint8_t access_flags = 0; + const char *p = mode; + while(*p != '\0') { + switch(*p) { + case 'r': + { + access_flags |= FS_ACCESS_READ; + if(*(p+1) == '+') { + access_flags |= FS_ACCESS_WRITE; + } + } + break; + + case 'w': + { + access_flags |= (FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE); + if(*(p+1) == '+') { + access_flags |= FS_ACCESS_READ; + } + } + break; + + case 'a': + { + access_flags |= (FS_ACCESS_WRITE | FS_ACCESS_APPEND); + if(*(p+1) == '+') { + access_flags |= FS_ACCESS_READ; + } + } + break; + } + p++; + } + + /* Open the file for reading. */ + int rc = fs_open(path, access_flags, &file); + if (rc != 0) { + return NULL; + } + return file; +} + +int mynewt_fseek(FILE *stream, long offset, int whence) +{ + uint32_t fs_offset; + + switch(whence) { + case 0: /* SEEK_SET */ + { + fs_offset += offset; + } + break; + + case 1: /* SEEK_CUR */ + { + fs_offset = fs_getpos(stream); + fs_offset += offset; + } + break; + + case 2: /* SEEK_END */ + { + fs_filelen(stream, &fs_offset); + fs_offset += offset; + } + break; + } + + fs_seek(stream, fs_offset); + + return 0; +} + +long mynewt_ftell(FILE *stream) +{ + uint32_t fs_offset; + fs_filelen(stream, &fs_offset); + fs_seek(stream, fs_offset); + return (long)fs_offset; +} + +void mynewt_rewind(FILE *stream) +{ + fs_seek(stream, 0); +} + +size_t mynewt_fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream) +{ + size_t to_read = size * nitems; + uint32_t read_size; + int rc = fs_read(stream, to_read, ptr, &read_size); + if(rc != 0) { + return 0; + } + + return (size_t)read_size; +} + +size_t mynewt_fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream) +{ + size_t to_write = size * nitems; + size_t write_size; + int rc = fs_write(stream, ptr, to_write); + if(rc != 0) { + return 0; + } + + return to_write; +} + +int mynewt_fclose(FILE *stream) +{ + fs_close(stream); + return 0; +} + +#endif /* NO_FILESYSTEM*/ +#endif /* if defined(WOLFSSL_APACHE_MYNEWT) */ diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index b60abfa91..e9cd4bad7 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -1907,6 +1907,25 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return ret; } +#elif defined(WOLFSSL_APACHE_MYNEWT) + + #include + #include "os/os_time.h" + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + int i; + srand(os_time_get()); + + for (i = 0; i < sz; i++ ) { + output[i] = rand() % 256; + if ((i % 8) == 7) { + srand(os_time_get()); + } + } + + return 0; + } + #elif defined(CUSTOM_RAND_GENERATE_BLOCK) /* #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc * extern int myRngFunc(byte* output, word32 sz); diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index f89ca07ac..90641bf96 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -1324,6 +1324,22 @@ time_t windows_time(time_t* timer) } #endif /* _WIN32_WCE */ +#if defined(WOLFSSL_APACHE_MYNEWT) +#include "os/os_time.h" + +time_t mynewt_time(time_t* timer) +{ + time_t now; + struct os_timeval tv; + os_gettimeofday(&tv, NULL); + now = (time_t)tv.tv_sec; + if(timer != NULL) { + *timer = now; + } + return now; +} +#endif /* WOLFSSL_APACHE_MYNEWT */ + #if defined(WOLFSSL_GMTIME) struct tm* gmtime(const time_t* timer) { diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 5fdeda243..08644ee25 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -175,6 +175,15 @@ #include "fsl_debug_console.h" #undef printf #define printf PRINTF +#elif defined(WOLFSSL_APACHE_MYNEWT) + #include + #include + #include "sysinit/sysinit.h" + #include "os/os.h" + #ifdef ARCH_sim + #include "mcu/mcu_sim.h" + #endif + #include "os/os_time.h" #else #include #endif @@ -1008,6 +1017,22 @@ initDefaultName(); int ret; func_args args; +#ifdef WOLFSSL_APACHE_MYNEWT + #ifdef ARCH_sim + mcu_sim_parse_args(argc, argv); + #endif + sysinit(); + + /* set dummy wallclock time. */ + struct os_timeval utctime; + struct os_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; + os_settimeofday(&utctime, &tz); +#endif + #ifdef HAVE_WNR if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) { err_sys("Whitewood netRandom global config failed", -1002); diff --git a/wolfssl/internal.h b/wolfssl/internal.h index f437714f5..8b00ac7a8 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -161,6 +161,11 @@ #include #elif defined(WOLFSSL_NUCLEUS_1_2) /* do nothing */ +#elif defined(WOLFSSL_APACHE_MYNEWT) + #if !defined(WOLFSSL_LWIP) + void mynewt_ctx_clear(void *ctx); + void* mynewt_ctx_new(); + #endif #else #ifndef SINGLE_THREADED #define WOLFSSL_PTHREADS @@ -3424,7 +3429,6 @@ typedef struct DtlsMsg { #endif - /* Handshake messages received from peer (plus change cipher */ typedef struct MsgsReceived { word16 got_hello_request:1; @@ -3761,6 +3765,9 @@ struct WOLFSSL { #ifdef HAVE_NETX NetX_Ctx nxCtx; /* NetX IO Context */ #endif +#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + void* mnCtx; /* mynewt mn_socket IO Context */ +#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ #ifdef SESSION_INDEX int sessionIndex; /* Session's location in the cache. */ #endif diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 9309c0e54..3ae818a55 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -169,6 +169,9 @@ /* Uncomment next line if building for Nucleus 1.2 */ /* #define WOLFSSL_NUCLEUS_1_2 */ +/* Uncomment next line if building for using Apache mynewt */ +/* #define WOLFSSL_APACHE_MYNEWT */ + #include #ifdef WOLFSSL_USER_SETTINGS @@ -1239,6 +1242,41 @@ extern void uITRON4_free(void *p) ; #endif #endif /*(WOLFSSL_XILINX_CRYPT)*/ +#if defined(WOLFSSL_APACHE_MYNEWT) + #include "os/os_malloc.h" + #if !defined(WOLFSSL_LWIP) + #include + #endif + + #if !defined(SIZEOF_LONG) + #define SIZEOF_LONG 4 + #endif + #if !defined(SIZEOF_LONG_LONG) + #define SIZEOF_LONG_LONG 8 + #endif + #if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + #define BIG_ENDIAN_ORDER + #else + #undef BIG_ENDIAN_ORDER + #define LITTLE_ENDIAN_ORDER + #endif + #define NO_WRITEV + #define WOLFSSL_USER_IO + #define SINGLE_THREADED + #define NO_DEV_RANDOM + #define NO_DH + #define NO_WOLFSSL_DIR + #define NO_ERROR_STRINGS + #define HAVE_ECC + #define NO_SESSION_CACHE + #define NO_ERROR_STRINGS + #define XMALLOC_USER + #define XMALLOC(sz, heap, type) os_malloc(sz) + #define XREALLOC(p, sz, heap, type) os_realloc(p, sz) + #define XFREE(p, heap, type) os_free(p) + +#endif /*(WOLFSSL_APACHE_MYNEWT)*/ + #ifdef WOLFSSL_IMX6 #ifndef SIZEOF_LONG_LONG #define SIZEOF_LONG_LONG 8 diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 0ac0e2514..723dcca78 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -102,6 +102,8 @@ #define NU_DEBUG #include "plus/nucleus.h" #include "nucleus.h" +#elif defined(WOLFSSL_APACHE_MYNEWT) + /* do nothing */ #else #ifndef SINGLE_THREADED #define WOLFSSL_PTHREADS @@ -292,6 +294,20 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #define XFCLOSE fclose #define XSEEK_END PSEEK_END #define XBADFILE NULL +#elif defined(WOLFSSL_APACHE_MYNEWT) + #include + #define XFILE struct fs_file* + + #define XFOPEN mynewt_fopen + #define XFSEEK mynewt_fseek + #define XFTELL mynewt_ftell + #define XREWIND mynewt_rewind + #define XFREAD mynewt_fread + #define XFWRITE mynewt_fwrite + #define XFCLOSE mynewt_fclose + #define XSEEK_END 2 + #define XBADFILE NULL + #define XFGETS(b,s,f) -2 /* Not ported yet */ #else /* stdio, default case */ #include @@ -437,6 +453,12 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #define XTIME(t1) windows_time((t1)) #define WOLFSSL_GMTIME +#elif defined(WOLFSSL_APACHE_MYNEWT) + #include "os/os_time.h" + #define XTIME(t1) mynewt_time((t1)) + #define WOLFSSL_GMTIME + #define USE_WOLF_TM + #define USE_WOLF_TIME_T #else /* default */ /* uses complete facility */ diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index cafaaad5e..2662f943b 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -52,7 +52,7 @@ #endif #ifndef USE_WINDOWS_API - #ifdef WOLFSSL_LWIP + #if defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT) /* lwIP needs to be configured to use sockets API in this mode */ /* LWIP_SOCKET 1 in lwip/opt.h or in build */ #include "lwip/sockets.h" @@ -107,6 +107,8 @@ #include #elif defined(WOLFSSL_SGX) #include + #elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + #include #elif !defined(WOLFSSL_NO_SOCK) #include #include @@ -243,7 +245,7 @@ int net_recv(int, void*, int, unsigned int); #define SEND_FUNCTION net_send #define RECV_FUNCTION net_recv -#elif defined(WOLFSSL_LWIP) +#elif defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT) #define SEND_FUNCTION lwip_send #define RECV_FUNCTION lwip_recv #elif defined(WOLFSSL_PICOTCP) @@ -414,6 +416,13 @@ WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags); WOLFSSL_LOCAL int MicriumSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx); #endif /* MICRIUM */ +#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) + WOLFSSL_LOCAL int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx); + WOLFSSL_LOCAL int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx); + WOLFSSL_API void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, + struct mn_sockaddr_in* mnSockAddrIn); +#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ + #ifdef WOLFSSL_DTLS typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz, void* ctx);