From 69b25ee508026e11b40dc2dc164ca10dffb2d118 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 24 Aug 2017 12:13:17 -0700 Subject: [PATCH 1/8] Added simple GCC ARM `gcc-arm-none-eabi` Makefile project example for wolfCrypt test/benchmark and wolfSSL Client. --- IDE/GCC-ARM/Header/user_settings.h | 432 ++++++++++++++++++++++++++++ IDE/GCC-ARM/Makefile.bench | 5 + IDE/GCC-ARM/Makefile.client | 5 + IDE/GCC-ARM/Makefile.common | 82 ++++++ IDE/GCC-ARM/Makefile.test | 5 + IDE/GCC-ARM/README.md | 39 +++ IDE/GCC-ARM/Source/armtarget.c | 266 +++++++++++++++++ IDE/GCC-ARM/Source/benchmark_main.c | 47 +++ IDE/GCC-ARM/Source/test_main.c | 47 +++ IDE/GCC-ARM/Source/tls_client.c | 196 +++++++++++++ IDE/GCC-ARM/include.am | 16 ++ IDE/GCC-ARM/linker.ld | 30 ++ IDE/include.am | 1 + 13 files changed, 1171 insertions(+) create mode 100755 IDE/GCC-ARM/Header/user_settings.h create mode 100755 IDE/GCC-ARM/Makefile.bench create mode 100644 IDE/GCC-ARM/Makefile.client create mode 100755 IDE/GCC-ARM/Makefile.common create mode 100755 IDE/GCC-ARM/Makefile.test create mode 100755 IDE/GCC-ARM/README.md create mode 100755 IDE/GCC-ARM/Source/armtarget.c create mode 100755 IDE/GCC-ARM/Source/benchmark_main.c create mode 100755 IDE/GCC-ARM/Source/test_main.c create mode 100755 IDE/GCC-ARM/Source/tls_client.c create mode 100644 IDE/GCC-ARM/include.am create mode 100755 IDE/GCC-ARM/linker.ld diff --git a/IDE/GCC-ARM/Header/user_settings.h b/IDE/GCC-ARM/Header/user_settings.h new file mode 100755 index 000000000..4629f5dca --- /dev/null +++ b/IDE/GCC-ARM/Header/user_settings.h @@ -0,0 +1,432 @@ +/* user_settings.h + * + * Copyright (C) 2006-2016 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 + */ + +/* Custom wolfSSL user settings for GCC ARM */ + +#ifndef WOLFSSL_USER_SETTINGS_H +#define WOLFSSL_USER_SETTINGS_H + +#ifdef __cplusplus +extern "C" { +#endif + + +/* ------------------------------------------------------------------------- */ +/* Platform */ +/* ------------------------------------------------------------------------- */ +#undef WOLFSSL_GENERAL_ALIGNMENT +#define WOLFSSL_GENERAL_ALIGNMENT 4 + +#undef SINGLE_THREADED +#define SINGLE_THREADED + +#undef WOLFSSL_SMALL_STACK +#define WOLFSSL_SMALL_STACK + +#undef WOLFSSL_USER_IO +#define WOLFSSL_USER_IO + + +/* ------------------------------------------------------------------------- */ +/* Math Configuration */ +/* ------------------------------------------------------------------------- */ +#undef USE_FAST_MATH +#define USE_FAST_MATH + +#undef SIZEOF_LONG_LONG +#define SIZEOF_LONG_LONG 8 + +#ifdef USE_FAST_MATH + #undef TFM_TIMING_RESISTANT + #define TFM_TIMING_RESISTANT + + /* Optimizations (on M0 UMULL is not supported, need another assembly solution) */ + //#define TFM_ARM +#endif + + +/* ------------------------------------------------------------------------- */ +/* Crypto */ +/* ------------------------------------------------------------------------- */ +/* RSA */ +#undef NO_RSA +#if 1 + #ifdef USE_FAST_MATH + /* Maximum math bits (Max RSA key bits * 2) */ + #undef FP_MAX_BITS + #define FP_MAX_BITS 4096 + #endif + + /* half as much memory but twice as slow */ + #undef RSA_LOW_MEM + //#define RSA_LOW_MEM + + /* Enables blinding mode, to prevent timing attacks */ + #undef WC_RSA_BLINDING + #define WC_RSA_BLINDING + +#else + #define NO_RSA +#endif + +/* ECC */ +#if 1 + #undef HAVE_ECC + #define HAVE_ECC + + /* Manually define enabled curves */ + #undef ECC_USER_CURVES + #define ECC_USER_CURVES + + //#define HAVE_ECC192 + //#define HAVE_ECC224 + #undef NO_ECC256 + //#define HAVE_ECC384 + //#define HAVE_ECC521 + + /* Fixed point cache (speeds repeated operations against same private key) */ + #undef FP_ECC + //#define FP_ECC + #ifdef FP_ECC + /* Bits / Entries */ + #undef FP_ENTRIES + #define FP_ENTRIES 2 + #undef FP_LUT + #define FP_LUT 4 + #endif + + /* Optional ECC calculation method */ + /* Note: doubles heap usage, but slightly faster */ + #undef ECC_SHAMIR + #define ECC_SHAMIR + + /* Reduces heap usage, but slower */ + #undef ECC_TIMING_RESISTANT + #define ECC_TIMING_RESISTANT + + /* Use alternate ECC size for ECC math */ + #ifdef USE_FAST_MATH + #ifndef NO_RSA + /* if not using RSA set FP_MAX_BITS to 256*2 */ + #undef FP_MAX_BITS + #define FP_MAX_BITS 512 + #else + #undef ALT_ECC_SIZE + #define ALT_ECC_SIZE + #endif + + #ifndef NO_ECC256 + #undef TFM_ECC256 + #define TFM_ECC256 + #endif + #endif +#endif + +/* AES */ +#undef NO_AES +#if 1 + #undef HAVE_AES_CBC + #define HAVE_AES_CBC + + #undef HAVE_AESGCM + #define HAVE_AESGCM + + /* GCM Method: GCM_SMALL, GCM_WORD32 or GCM_TABLE */ + #undef GCM_SMALL + #define GCM_SMALL +#else + #define NO_AES +#endif + +/* ChaCha20 / Poly1305 */ +#undef HAVE_CHACHA +#undef HAVE_POLY1305 +#if 0 + #define HAVE_CHACHA + #define HAVE_POLY1305 + + /* Needed for Poly1305 */ + #undef HAVE_ONE_TIME_AUTH + #define HAVE_ONE_TIME_AUTH +#endif + +/* Ed25519 / Curve25519 */ +#undef HAVE_CURVE25519 +#undef HAVE_ED25519 +#if 0 + #define HAVE_CURVE25519 + #define HAVE_ED25519 /* ED25519 Requires SHA512 */ + + /* Optionally use small math (less flash usage, but much slower) */ + #if 1 + #define CURVED25519_SMALL + #endif +#endif + + +/* ------------------------------------------------------------------------- */ +/* Hashing */ +/* ------------------------------------------------------------------------- */ +/* Sha */ +#undef NO_SHA +#if 1 + /* 1k smaller, but 25% slower */ + //#define USE_SLOW_SHA +#else + #define NO_SHA +#endif + +/* Sha256 */ +#undef NO_SHA256 +#if 1 +#else + #define NO_SHA256 +#endif + +/* Sha512 */ +#undef WOLFSSL_SHA512 +#if 0 + #define WOLFSSL_SHA512 + + /* Sha384 */ + #undef WOLFSSL_SHA384 + #if 0 + #define WOLFSSL_SHA384 + #endif + + /* over twice as small, but 50% slower */ + #define USE_SLOW_SHA2 +#endif + +/* MD5 */ +#undef NO_MD5 +#if 1 + +#else + #define NO_MD5 +#endif + +#undef WOLFSSL_SHA3 +#if 0 + #define WOLFSSL_SHA3 +#else +#endif + +/* HKDF */ +#undef HAVE_HKDF +#if 0 + #define HAVE_HKDF +#else +#endif + + +/* ------------------------------------------------------------------------- */ +/* Benchmark / Test */ +/* ------------------------------------------------------------------------- */ +/* Use reduced benchmark / test sizes */ +#undef BENCH_EMBEDDED +#define BENCH_EMBEDDED + +#undef USE_CERT_BUFFERS_2048 +#define USE_CERT_BUFFERS_2048 + +//#undef USE_CERT_BUFFERS_1024 +//#define USE_CERT_BUFFERS_1024 + +#undef USE_CERT_BUFFERS_256 +#define USE_CERT_BUFFERS_256 + + +/* ------------------------------------------------------------------------- */ +/* Debugging */ +/* ------------------------------------------------------------------------- */ + +#undef DEBUG_WOLFSSL +#define DEBUG_WOLFSSL + +/* Use this to measure / print heap usage */ +#if 0 + #undef USE_WOLFSSL_MEMORY + #define USE_WOLFSSL_MEMORY + + #undef WOLFSSL_TRACK_MEMORY + //#define WOLFSSL_TRACK_MEMORY + + #undef WOLFSSL_DEBUG_MEMORY + //#define WOLFSSL_DEBUG_MEMORY +#else + #undef NO_WOLFSSL_MEMORY + #define NO_WOLFSSL_MEMORY +#endif + +#ifndef DEBUG_WOLFSSL + #undef NO_ERROR_STRINGS + #define NO_ERROR_STRINGS +#endif + + +/* ------------------------------------------------------------------------- */ +/* Port */ +/* ------------------------------------------------------------------------- */ + +/* Override Current Time */ +/* Allows custom "custom_time()" function to be used for benchmark */ +#define WOLFSSL_USER_CURRTIME +#define USER_TICKS +extern unsigned long my_time(unsigned long* timer); +#define XTIME my_time + + +/* ------------------------------------------------------------------------- */ +/* RNG */ +/* ------------------------------------------------------------------------- */ +/* Size of returned HW RNG value */ +#define CUSTOM_RAND_TYPE unsigned int + +/* Seed source */ +extern unsigned int custom_rand_generate(void); +#undef CUSTOM_RAND_GENERATE +#define CUSTOM_RAND_GENERATE custom_rand_generate + +/* Choose RNG method */ +#if 1 + /* Use built-in P-RNG (SHA256 based) with HW RNG */ + /* P-RNG + HW RNG (P-RNG is ~8K) */ + #undef HAVE_HASHDRBG + #define HAVE_HASHDRBG +#else + #undef WC_NO_HASHDRBG + #define WC_NO_HASHDRBG + + /* Bypass P-RNG and use only HW RNG */ + extern int custom_rand_generate_block(unsigned char* output, unsigned int sz); + #undef CUSTOM_RAND_GENERATE_BLOCK + #define CUSTOM_RAND_GENERATE_BLOCK custom_rand_generate_block +#endif + + +/* ------------------------------------------------------------------------- */ +/* Enable Features */ +/* ------------------------------------------------------------------------- */ +#undef KEEP_PEER_CERT +//#define KEEP_PEER_CERT + +#undef HAVE_COMP_KEY +//#define HAVE_COMP_KEY + +#undef HAVE_TLS_EXTENSIONS +#define HAVE_TLS_EXTENSIONS + +#undef HAVE_SUPPORTED_CURVES +#define HAVE_SUPPORTED_CURVES + +#undef WOLFSSL_BASE64_ENCODE +#define WOLFSSL_BASE64_ENCODE + +/* TLS Session Cache */ +#if 0 + #define SMALL_SESSION_CACHE +#else + #define NO_SESSION_CACHE +#endif + + +/* ------------------------------------------------------------------------- */ +/* Disable Features */ +/* ------------------------------------------------------------------------- */ +#undef NO_WOLFSSL_SERVER +//#define NO_WOLFSSL_SERVER + +#undef NO_WOLFSSL_CLIENT +//#define NO_WOLFSSL_CLIENT + +#undef NO_CRYPT_TEST +//#define NO_CRYPT_TEST + +#undef NO_CRYPT_BENCHMARK +//#define NO_CRYPT_BENCHMARK + +/* In-lining of misc.c functions */ +/* If defined, must include wolfcrypt/src/misc.c in build */ +/* Slower, but about 1k smaller */ +#undef NO_INLINE +//#define NO_INLINE + +#undef NO_FILESYSTEM +#define NO_FILESYSTEM + +#undef NO_WRITEV +#define NO_WRITEV + +#undef NO_MAIN_DRIVER +#define NO_MAIN_DRIVER + +#undef NO_DEV_RANDOM +#define NO_DEV_RANDOM + +#undef NO_DSA +#define NO_DSA + +#undef NO_DH +#define NO_DH + +#undef NO_DES3 +#define NO_DES3 + +#undef NO_RC4 +#define NO_RC4 + +#undef NO_OLD_TLS +#define NO_OLD_TLS + +#undef NO_HC128 +#define NO_HC128 + +#undef NO_RABBIT +#define NO_RABBIT + +#undef NO_PSK +#define NO_PSK + +#undef NO_MD4 +#define NO_MD4 + +#undef NO_PWDBASED +#define NO_PWDBASED + +#undef NO_CODING +//#define NO_CODING + +#undef NO_ASN_TIME +//#define NO_ASN_TIME + +#undef NO_CERTS +//#define NO_CERTS + +#undef NO_SIG_WRAPPER +//#define NO_SIG_WRAPPER + +#ifdef __cplusplus +} +#endif + +#endif /* WOLFSSL_USER_SETTINGS_H */ + diff --git a/IDE/GCC-ARM/Makefile.bench b/IDE/GCC-ARM/Makefile.bench new file mode 100755 index 000000000..70592dc8c --- /dev/null +++ b/IDE/GCC-ARM/Makefile.bench @@ -0,0 +1,5 @@ +# Project name +BIN = WolfCryptBench +SRC_C = ./Source/benchmark_main.c + +include Makefile.common diff --git a/IDE/GCC-ARM/Makefile.client b/IDE/GCC-ARM/Makefile.client new file mode 100644 index 000000000..74d57c17a --- /dev/null +++ b/IDE/GCC-ARM/Makefile.client @@ -0,0 +1,5 @@ +# Project name +BIN = WolfSSLClient +SRC_C = ./Source/tls_client.c + +include Makefile.common diff --git a/IDE/GCC-ARM/Makefile.common b/IDE/GCC-ARM/Makefile.common new file mode 100755 index 000000000..7b0b2d533 --- /dev/null +++ b/IDE/GCC-ARM/Makefile.common @@ -0,0 +1,82 @@ +BUILD_DIR = ./Build + +# Set to @ if you want to suppress command echo +CMD_ECHO = @ + +# Important directories +TOOLCHAIN_DIR = /opt/gcc-arm-none-eabi/bin + +INC = -I./Header \ + -I./Source \ + -I../.. + +SRC_LD = ./linker.ld + +# Defines +DEF = -DWOLFSSL_USER_SETTINGS + +# Compiler and linker flags +ARCHFLAGS = -mcpu=cortex-m0 -mthumb -mabi=aapcs +DBGFLAGS = -ggdb +ASFLAGS = $(ARCHFLAGS) + +# CC: Place functions and data into separate sections to allow dead code removal +# by the linker (-f*-sections). Enable link time optimization (-flto) +CFLAGS = $(ARCHFLAGS) -std=gnu99 -Wall -Wno-cpp \ + -ffunction-sections -fdata-sections \ + -Os -flto $(DBGFLAGS) + +# LD: Remove unused sections, link with newlib-nano implementation, generate map +LDFLAGS = $(ARCHFLAGS) -Wl,--gc-sections --specs=nano.specs --specs=nosys.specs +LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(BIN).map $(DBGFLAGS) +LDFLAGS += -Wl,-ereset_handler + +SRC_C += ./Source/armtarget.c +SRC_C += $(wildcard ../../src/*.c ../../wolfcrypt/src/*.c ../../wolfcrypt/benchmark/*.c ../../wolfcrypt/test/*.c) +FILENAMES_C = $(notdir $(SRC_C)) +FILENAMES_C := $(filter-out evp.c, $(FILENAMES_C)) +OBJS_C = $(addprefix $(BUILD_DIR)/, $(FILENAMES_C:.c=.o)) +vpath %.c $(dir $(SRC_C)) + +# Tools selection +CC = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc +AS = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc +LD = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc +NM = $(TOOLCHAIN_DIR)/arm-none-eabi-nm +OBJCOPY = $(TOOLCHAIN_DIR)/arm-none-eabi-objcopy +OBJDUMP = $(TOOLCHAIN_DIR)/arm-none-eabi-objdump +SIZE = $(TOOLCHAIN_DIR)/arm-none-eabi-size + + +all: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).hex + @echo "" + $(CMD_ECHO) @$(SIZE) $(BUILD_DIR)/$(BIN).elf + +$(BUILD_DIR): + $(CMD_ECHO) mkdir -p $(BUILD_DIR) + +$(BUILD_DIR)/$(BIN).hex: $(BUILD_DIR)/$(BIN).elf + @echo "Generating HEX binary: $(notdir $@)" + $(CMD_ECHO) $(OBJCOPY) -O ihex $< $@ + +$(BUILD_DIR)/%.o: %.s + @echo "Compiling ASM file: $(notdir $<)" + $(CMD_ECHO) $(AS) $(ASFLAGS) $(DEF) $(INC) -c -o $@ $< + +$(BUILD_DIR)/%.o: %.c + @echo "Compiling C file: $(notdir $<)" + $(CMD_ECHO) $(CC) $(CFLAGS) $(DEF) $(INC) -c -o $@ $< + +$(BUILD_DIR)/$(BIN).elf: $(OBJS_ASM) $(OBJS_C) + @echo "Linking ELF binary: $(notdir $@)" + $(CMD_ECHO) $(LD) $(LDFLAGS) -T$(SRC_LD) -o $@ $^ + + @echo "Generating name list: $(BIN).sym" + $(CMD_ECHO) $(NM) -n $@ > $(BUILD_DIR)/$(BIN).sym + + @echo "Generating disassembly: $(BIN).disasm" + $(CMD_ECHO) $(OBJDUMP) -S $@ > $(BUILD_DIR)/$(BIN).disasm + +clean: + rm -f $(BUILD_DIR)/*.elf $(BUILD_DIR)/*.hex $(BUILD_DIR)/*.map + rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.sym $(BUILD_DIR)/*.disasm diff --git a/IDE/GCC-ARM/Makefile.test b/IDE/GCC-ARM/Makefile.test new file mode 100755 index 000000000..78778c6ff --- /dev/null +++ b/IDE/GCC-ARM/Makefile.test @@ -0,0 +1,5 @@ +# Project name +BIN = WolfCryptTest +SRC_C = ./Source/test_main.c + +include Makefile.common diff --git a/IDE/GCC-ARM/README.md b/IDE/GCC-ARM/README.md new file mode 100755 index 000000000..8737332a1 --- /dev/null +++ b/IDE/GCC-ARM/README.md @@ -0,0 +1,39 @@ +# Example Project for GCC ARM + +## Design + +* All library options are defined in `Header/user_settings.h`. +* The memory map is located in the linker file in `linker.ld`. +* Entry point function is `reset_handler` in `retarget.c`. +* The RTC and RNG hardware interface needs implemented for real production applications in `retarget.c` + +## Building + +1. Make sure you have `gcc-arm-none-eabi` installed. +2. Modify the `Makefile` to point to correct `TOOLCHAIN_DIR`. +3. Use `make` and it will build both targets as `.elf` and `.hex` in `/Build`. + +Example: + +``` + text data bss dec hex filename + 50076 2508 44 52628 cd94 ./Build/WolfCryptTest.elf + + text data bss dec hex filename + 39155 2508 60 41723 a2fb ./Build/WolfCryptBench.elf +``` + +## Performace Tuning Options + +* `DEBUG_WOLFSSL`: Undefine this to disable debug logging. +* `NO_INLINE`: Disabling inline function saves about 1KB, but is slower. +* `WOLFSSL_SMALL_STACK`: Enables stack reduction techniques to allocate stack sections over 100 bytes from heap. +* `USE_FAST_MATH`: Uses stack based math, which is faster than the heap based math. +* `ECC_SHAMIR`: Doubles heap usage, but slightly faster +* `RSA_LOW_MEM`: Half as much memory but twice as slow. Uses Non-CRT method for private key. +AES GCM: `GCM_SMALL`, `GCM_WORD32` or `GCM_TABLE`: Tunes performance and flash/memory usage. +* `CURVED25519_SMALL`: Enables small versions of Ed/Curve (FE/GE math). +* `USE_SLOW_SHA`: Enables smaller/slower version of SHA. +* `USE_SLOW_SHA2`: Over twice as small, but 50% slower +* `FP_MAX_BITS`: Is the maximum math size (key size * 2). Used only with `USE_FAST_MATH`. +* `USE_CERT_BUFFERS_1024` or `USE_CERT_BUFFERS_2048`: Size of RSA certs / keys to test with. diff --git a/IDE/GCC-ARM/Source/armtarget.c b/IDE/GCC-ARM/Source/armtarget.c new file mode 100755 index 000000000..4f0d5c45d --- /dev/null +++ b/IDE/GCC-ARM/Source/armtarget.c @@ -0,0 +1,266 @@ +/* armtarget.c + * + * Copyright (C) 2006-2017 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 "user_settings.h" + +#include +#include +#include +#include + +/* Memory initialization */ +extern uint32_t __data_load_start__[]; +extern uint32_t __data_start__[]; +extern uint32_t __data_end__[]; + +extern uint32_t __bss_start__[]; +extern uint32_t __bss_end__[]; + +extern uint32_t __stack_process_end__[]; + +extern uint32_t __heap_start__[]; +extern uint32_t __heap_end__[]; + +/* Copy memory: src=Source, dst_beg=Destination Begin, dst_end=Destination End */ +void memcpy32(uint32_t* src, uint32_t* dst_beg, uint32_t* dst_end) +{ + while (dst_beg < dst_end) { + *dst_beg++ = *src++; + } +} +/* Zero address in range */ +void meminit32(uint32_t* start, uint32_t* end) +{ + while (start < end) { + *start++ = 0; + } +} + +/* Entry Point */ +void reset_handler(void) +{ + /* Init sections */ + memcpy32(__data_load_start__, __data_start__, __data_end__); + meminit32(__bss_start__, __bss_end__); + + /* Init heap */ + __heap_start__[0] = 0; + __heap_start__[1] = ((uint32_t)__heap_end__ - (uint32_t)__heap_start__); + + /* Start main */ + extern int main(void); + main(); + + /* Application has ended, so busy wait */ + while(1); +} + +// Vector Exception/Interrupt Handlers +static void Default_Handler(void) +{ + /* If we get here then need to implement real IRQ handler */ + while(1); +} + +__attribute__((section(".sys"))) __attribute__ ((used)) +void HardFault_HandlerC( uint32_t *hardfault_args ) +{ + /* These are volatile to try and prevent the compiler/linker optimizing them + away as the variables never actually get used. If the debugger won't show the + values of the variables, make them global my moving their declaration outside + of this function. */ + volatile uint32_t stacked_r0; + volatile uint32_t stacked_r1; + volatile uint32_t stacked_r2; + volatile uint32_t stacked_r3; + volatile uint32_t stacked_r12; + volatile uint32_t stacked_lr; + volatile uint32_t stacked_pc; + volatile uint32_t stacked_psr; + volatile uint32_t _CFSR; + volatile uint32_t _HFSR; + volatile uint32_t _DFSR; + volatile uint32_t _AFSR; + volatile uint32_t _BFAR; + volatile uint32_t _MMAR; + + stacked_r0 = ((uint32_t)hardfault_args[0]); + stacked_r1 = ((uint32_t)hardfault_args[1]); + stacked_r2 = ((uint32_t)hardfault_args[2]); + stacked_r3 = ((uint32_t)hardfault_args[3]); + stacked_r12 = ((uint32_t)hardfault_args[4]); + stacked_lr = ((uint32_t)hardfault_args[5]); + stacked_pc = ((uint32_t)hardfault_args[6]); + stacked_psr = ((uint32_t)hardfault_args[7]); + + // Configurable Fault Status Register + // Consists of MMSR, BFSR and UFSR + _CFSR = (*((volatile uint32_t *)(0xE000ED28))); + + // Hard Fault Status Register + _HFSR = (*((volatile uint32_t *)(0xE000ED2C))); + + // Debug Fault Status Register + _DFSR = (*((volatile uint32_t *)(0xE000ED30))); + + // Auxiliary Fault Status Register + _AFSR = (*((volatile uint32_t *)(0xE000ED3C))); + + // Read the Fault Address Registers. These may not contain valid values. + // Check BFARVALID/MMARVALID to see if they are valid values + // MemManage Fault Address Register + _MMAR = (*((volatile uint32_t *)(0xE000ED34))); + // Bus Fault Address Register + _BFAR = (*((volatile uint32_t *)(0xE000ED38))); + + printf ("\n\nHard fault handler (all numbers in hex):\n"); + printf ("R0 = %lx\n", stacked_r0); + printf ("R1 = %lx\n", stacked_r1); + printf ("R2 = %lx\n", stacked_r2); + printf ("R3 = %lx\n", stacked_r3); + printf ("R12 = %lx\n", stacked_r12); + printf ("LR [R14] = %lx subroutine call return address\n", stacked_lr); + printf ("PC [R15] = %lx program counter\n", stacked_pc); + printf ("PSR = %lx\n", stacked_psr); + printf ("CFSR = %lx\n", _CFSR); + printf ("HFSR = %lx\n", _HFSR); + printf ("DFSR = %lx\n", _DFSR); + printf ("AFSR = %lx\n", _AFSR); + printf ("MMAR = %lx\n", _MMAR); + printf ("BFAR = %lx\n", _BFAR); + + // Break into the debugger + __asm("BKPT #0\n"); +} + +__attribute__((section(".sys"))) __attribute__( ( naked ) ) +void HardFault_Handler(void) +{ + __asm volatile + ( + " movs r0,#4 \n" /* load bit mask into R0 */ + " mov r1, lr \n" /* load link register into R1 */ + " tst r0, r1 \n" /* compare with bitmask */ + " beq _MSP \n" /* if bitmask is set: stack pointer is in PSP. Otherwise in MSP */ + " mrs r0, psp \n" /* otherwise: stack pointer is in PSP */ + " b _GetPC \n" /* go to part which loads the PC */ + "_MSP: \n" /* stack pointer is in MSP register */ + " mrs r0, msp \n" /* load stack pointer into R0 */ + "_GetPC: \n" /* find out where the hard fault happened */ + " ldr r1,[r0,#20] \n" /* load program counter into R1. R1 contains address of the next instruction where the hard fault happened */ + " ldr r2, =HardFault_HandlerC \n" + " bx r2 \n" + " bx lr \n" /* decode more information. R0 contains pointer to stack frame */ + ); +} + +// Vectors +typedef void (*vector_entry)(void); +const vector_entry vectors[] __attribute__ ((section(".vectors"),used)) = +{ + /* Interrupt Vector Table Function Pointers */ + // Address Vector IRQ Source module Source description + (vector_entry)__stack_process_end__, // ARM core Initial Supervisor SP + reset_handler, // 0x0000_0004 1 - ARM core Initial Program Counter + Default_Handler, // 0x0000_0008 2 - ARM core Non-maskable Interrupt (NMI) + HardFault_Handler, // 0x0000_000C 3 - ARM core Hard Fault + Default_Handler, // 0x0000_0010 4 - + HardFault_Handler, // 0x0000_0014 5 - ARM core Bus Fault + HardFault_Handler, // 0x0000_0018 6 - ARM core Usage Fault + Default_Handler, // 0x0000_001C 7 - + Default_Handler, // 0x0000_0020 8 - + Default_Handler, // 0x0000_0024 9 - + Default_Handler, // 0x0000_0028 10 - + Default_Handler, // 0x0000_002C 11 - ARM core Supervisor call (SVCall) + Default_Handler, // 0x0000_0030 12 - ARM core Debug Monitor + Default_Handler, // 0x0000_0034 13 - + Default_Handler, // 0x0000_0038 14 - ARM core Pendable request for system service (PendableSrvReq) + Default_Handler, // 0x0000_003C 15 - ARM core System tick timer (SysTick) + + /* remainder go below */ +}; + + +/* TIME CODE */ +/* TODO: Implement real RTC */ +static int gTimeMs; +static int hw_get_time_sec(void) +{ + return ++gTimeMs; +} + +unsigned long my_time(unsigned long* timer) +{ + (void)timer; + return hw_get_time_sec(); +} + +unsigned int LowResTimer(void) +{ + return hw_get_time_sec(); +} + +double current_time(int reset) +{ + double time; + int timeMs = gTimeMs; + (void)reset; + time = (timeMs / 1000); // sec + time += (double)(timeMs % 1000) / 1000; // ms + return time; +} + + +/* RNG CODE */ +/* TODO: Implement real RNG */ +static int gCounter; +int hw_rand(void) +{ + return ++gCounter; +} + +unsigned int custom_rand_generate(void) +{ + return hw_rand(); +} + +int custom_rand_generate_block(unsigned char* output, unsigned int sz) +{ + uint32_t i = 0; + + while (i < sz) + { + /* If not aligned or there is odd/remainder */ + if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz || + ((uint32_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0 + ) { + /* Single byte at a time */ + output[i++] = (unsigned char)custom_rand_generate(); + } + else { + /* Use native 8, 16, 32 or 64 copy instruction */ + *((CUSTOM_RAND_TYPE*)&output[i]) = custom_rand_generate(); + i += sizeof(CUSTOM_RAND_TYPE); + } + } + + return 0; +} diff --git a/IDE/GCC-ARM/Source/benchmark_main.c b/IDE/GCC-ARM/Source/benchmark_main.c new file mode 100755 index 000000000..ffc62fd89 --- /dev/null +++ b/IDE/GCC-ARM/Source/benchmark_main.c @@ -0,0 +1,47 @@ +/* benchmark_main.c + * + * Copyright (C) 2006-2016 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 + +typedef struct func_args { + int argc; + char** argv; + int return_code; +} func_args; + +static func_args args = { 0 } ; + + +int main(void) +{ + wolfCrypt_Init(); + + printf("\nBenchmark Test\n"); + benchmark_test(&args); + printf("Benchmark Test: Return code %d\n", args.return_code); + + wolfCrypt_Cleanup(); + + return 0; +} diff --git a/IDE/GCC-ARM/Source/test_main.c b/IDE/GCC-ARM/Source/test_main.c new file mode 100755 index 000000000..137f13f47 --- /dev/null +++ b/IDE/GCC-ARM/Source/test_main.c @@ -0,0 +1,47 @@ +/* test_main.c + * + * Copyright (C) 2006-2017 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 + +typedef struct func_args { + int argc; + char** argv; + int return_code; +} func_args; + +static func_args args = { 0 } ; + +int main(void) +{ + wolfCrypt_Init(); + + printf("\nCrypt Test\n"); + wolfcrypt_test(&args); + printf("Crypt Test: Return code %d\n", args.return_code); + + wolfCrypt_Cleanup(); + + return 0; +} diff --git a/IDE/GCC-ARM/Source/tls_client.c b/IDE/GCC-ARM/Source/tls_client.c new file mode 100755 index 000000000..994c14589 --- /dev/null +++ b/IDE/GCC-ARM/Source/tls_client.c @@ -0,0 +1,196 @@ +/* tls_client.c + * + * Copyright (C) 2006-2017 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 + +#define MAXSZ 1024 + +/*------------------------------------------------------------------------*/ +/* TLS CLIENT */ +/*------------------------------------------------------------------------*/ +static int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + int ret = sz; + + (void)ssl; + (void)ctx; + + /* TODO: Exchange data over your own transport */ +#if 0 + ret = usart_read_buffer_wait(&cdc_uart_module, buf, sz); + if (ret == STATUS_ERR_TIMEOUT) + return WOLFSSL_CBIO_ERR_WANT_READ; + + return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL; +#else + return ret; +#endif +} + +static int CbIOSend(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + int ret = sz; + + (void)ssl; + (void)ctx; + + /* TODO: Exchange data over your own transport */ +#if 0 + ret = usart_write_buffer_wait(&cdc_uart_module, buf, sz); + if (ret == STATUS_ERR_TIMEOUT) + return WOLFSSL_CBIO_ERR_WANT_WRITE; + + return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL; +#else + return ret; +#endif +} + +static int tls_client(void) +{ + char msg[] = "Hello WolfSSL!\r\n"; + char reply[MAXSZ]; + int ret, msgSz, error; + WOLFSSL* ssl = NULL; + WOLFSSL_CTX* ctx = NULL; + + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { + printf("CTXnew failed.\n"); + goto fail; + } + + /*------------------------------------------------------------------------*/ + /* ECDHE-ECDSA */ + /*------------------------------------------------------------------------*/ + /*--------------------*/ + /* for peer auth use: */ + /*--------------------*/ + // wolfSSL_CTX_load_verify_buffer(ctx, rsa_key_der_1024, + // sizeof_rsa_key_der_1024, SSL_FILETYPE_ASN1); + // wolfSSL_CTX_load_verify_buffer(ctx, server_cert_der_1024, + // sizeof_server_cert_der_1024, SSL_FILETYPE_ASN1); + /*---------------------*/ + /* for no peer auth: */ + /*---------------------*/ + wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); + /*---------------------*/ + /* end peer auth option*/ + /*---------------------*/ + if ((ret = wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-ECDSA-AES128-SHA256")) != SSL_SUCCESS) { + wolfSSL_CTX_free(ctx); + printf("CTXset_cipher_list failed, error: %d\n", ret); + goto fail; + } + /*------------------------------------------------------------------------*/ + /* END CIPHER SUITE OPTIONS */ + /*------------------------------------------------------------------------*/ + wolfSSL_SetIORecv(ctx, CbIORecv); + wolfSSL_SetIOSend(ctx, CbIOSend); + + if ((ssl = wolfSSL_new(ctx)) == NULL) { + error = wolfSSL_get_error(ssl, 0); + printf("wolfSSL_new failed %d\n", error); + wolfSSL_CTX_free(ctx); + return -1; + } + + /* non blocking accept and connect */ + ret = SSL_FAILURE; + + while (ret != SSL_SUCCESS) { + /* client connect */ + ret = wolfSSL_connect(ssl); + error = wolfSSL_get_error(ssl, 0); + if (ret != SSL_SUCCESS) { + if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) { + /* Fail */ + printf("wolfSSL connect failed with return code %d\n", error); + goto fail; + } + } + /* Success */ + } + + /* read and write */ + while (1) { + /* client send/read */ + msgSz = sizeof(msg); + ret = wolfSSL_write(ssl, msg, msgSz); + error = wolfSSL_get_error(ssl, 0); + if (ret != msgSz) { + if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) { + /* Write failed */ + goto fail; + } + } + /* Write succeeded */ + break; + } + + while (1) { + ret = wolfSSL_read(ssl, reply, sizeof(reply) - 1); + error = wolfSSL_get_error(ssl, 0); + if (ret < 0) { + if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) { + /* Can put print here, the server enters a loop waiting to read + * a confirmation message at this point */ + // printf("client read failed\n"); + goto fail; + } + continue; + } + else { + /* Can put print here, the server enters a loop waiting to read + * a confirmation message at this point */ + reply[ret] = '\0'; + // printf("Client Received Reply: %s\n", reply); + break; + } + + } + + return 0; + +fail: + wolfSSL_shutdown(ssl); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + + return -1; +} + + +int main(void) +{ + int ret; + + wolfSSL_Init(); + + ret = tls_client(); + + wolfSSL_Cleanup(); + + return ret; +} diff --git a/IDE/GCC-ARM/include.am b/IDE/GCC-ARM/include.am new file mode 100644 index 000000000..2b5c927ca --- /dev/null +++ b/IDE/GCC-ARM/include.am @@ -0,0 +1,16 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +EXTRA_DIST+= IDE/GCC-ARM/Header/user_settings.h +EXTRA_DIST+= IDE/GCC-ARM/Source/armtarget.c +EXTRA_DIST+= IDE/GCC-ARM/Source/benchmark_main.c +EXTRA_DIST+= IDE/GCC-ARM/Source/test_main.c +EXTRA_DIST+= IDE/GCC-ARM/Source/tls_client.c +EXTRA_DIST+= IDE/GCC-ARM/linker.ld +EXTRA_DIST+= IDE/GCC-ARM/Makefile +EXTRA_DIST+= IDE/GCC-ARM/Makefile.bench +EXTRA_DIST+= IDE/GCC-ARM/Makefile.client +EXTRA_DIST+= IDE/GCC-ARM/Makefile.common +EXTRA_DIST+= IDE/GCC-ARM/Makefile.test +EXTRA_DIST+= IDE/GCC-ARM/README.md diff --git a/IDE/GCC-ARM/linker.ld b/IDE/GCC-ARM/linker.ld new file mode 100755 index 000000000..a43790386 --- /dev/null +++ b/IDE/GCC-ARM/linker.ld @@ -0,0 +1,30 @@ +MEMORY +{ + FLASH (wx) : ORIGIN = 0x00000000, LENGTH = 256K + RAM (wx) : ORIGIN = 0x20000000, LENGTH = 64K +} + +SECTIONS +{ + __vectors_start__ = .; + .vectors : { *(.vectors) } > FLASH + __vectors_end__ = __vectors_start__ + 0x400; + .sys : { *(.sys*) } > FLASH + .text : { *(.text*) } > FLASH + .rodata : { *(.text*) } > FLASH + + __data_load_start__ = .; + __data_start__ = .; + .data : { *(.data*) } > RAM + __data_end__ = __data_start__ + SIZEOF(.data); + + __bss_start__ = .; + .bss : { *(.bss*) } > RAM + __bss_end__ = __bss_start__ + SIZEOF(.bss); + + __heap_start__ = .; + .heap : { *(.heap*) } > RAM + __heap_end__ = __heap_start__ + SIZEOF(.heap); + + end = .; +} diff --git a/IDE/include.am b/IDE/include.am index aeb8b43d0..b6e575bec 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -12,5 +12,6 @@ include IDE/ARDUINO/include.am include IDE/INTIME-RTOS/include.am include IDE/OPENSTM32/include.am include IDE/VS-ARM/include.am +include IDE/GCC-ARM/include.am EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR From 2d67f462471cccfdacb5b4d525caddd068c6b180 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 25 Aug 2017 12:27:07 -0700 Subject: [PATCH 2/8] Minor cleanups for the GCC ARM example. Fixed build with DH enabled. Changed random.h to always make sure `CUSTOM_RAND_TYPE` is defined. Added TLS13 option to reference user_settings.h. --- IDE/GCC-ARM/Header/user_settings.h | 5 +++++ IDE/GCC-ARM/Makefile.common | 5 ++++- IDE/GCC-ARM/README.md | 13 ++++++++++--- IDE/GCC-ARM/Source/armtarget.c | 2 ++ wolfssl/wolfcrypt/random.h | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/IDE/GCC-ARM/Header/user_settings.h b/IDE/GCC-ARM/Header/user_settings.h index 4629f5dca..03cc0371d 100755 --- a/IDE/GCC-ARM/Header/user_settings.h +++ b/IDE/GCC-ARM/Header/user_settings.h @@ -326,6 +326,11 @@ extern unsigned int custom_rand_generate(void); /* ------------------------------------------------------------------------- */ /* Enable Features */ /* ------------------------------------------------------------------------- */ +#if 0 + #undef WOLFSSL_TLS13 + #define WOLFSSL_TLS13 +#endif + #undef KEEP_PEER_CERT //#define KEEP_PEER_CERT diff --git a/IDE/GCC-ARM/Makefile.common b/IDE/GCC-ARM/Makefile.common index 7b0b2d533..fe7d454db 100755 --- a/IDE/GCC-ARM/Makefile.common +++ b/IDE/GCC-ARM/Makefile.common @@ -31,6 +31,9 @@ LDFLAGS = $(ARCHFLAGS) -Wl,--gc-sections --specs=nano.specs --specs=nosys.specs LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(BIN).map $(DBGFLAGS) LDFLAGS += -Wl,-ereset_handler +# Math lib for DH +LIBS = -lm + SRC_C += ./Source/armtarget.c SRC_C += $(wildcard ../../src/*.c ../../wolfcrypt/src/*.c ../../wolfcrypt/benchmark/*.c ../../wolfcrypt/test/*.c) FILENAMES_C = $(notdir $(SRC_C)) @@ -69,7 +72,7 @@ $(BUILD_DIR)/%.o: %.c $(BUILD_DIR)/$(BIN).elf: $(OBJS_ASM) $(OBJS_C) @echo "Linking ELF binary: $(notdir $@)" - $(CMD_ECHO) $(LD) $(LDFLAGS) -T$(SRC_LD) -o $@ $^ + $(CMD_ECHO) $(LD) $(LDFLAGS) -T$(SRC_LD) -o $@ $^ $(LIBS) @echo "Generating name list: $(BIN).sym" $(CMD_ECHO) $(NM) -n $@ > $(BUILD_DIR)/$(BIN).sym diff --git a/IDE/GCC-ARM/README.md b/IDE/GCC-ARM/README.md index 8737332a1..27e109e8a 100755 --- a/IDE/GCC-ARM/README.md +++ b/IDE/GCC-ARM/README.md @@ -4,8 +4,8 @@ * All library options are defined in `Header/user_settings.h`. * The memory map is located in the linker file in `linker.ld`. -* Entry point function is `reset_handler` in `retarget.c`. -* The RTC and RNG hardware interface needs implemented for real production applications in `retarget.c` +* Entry point function is `reset_handler` in `armtarget.c`. +* The RTC and RNG hardware interface needs implemented for real production applications in `armtarget.c` ## Building @@ -21,19 +21,26 @@ Example: text data bss dec hex filename 39155 2508 60 41723 a2fb ./Build/WolfCryptBench.elf + + text data bss dec hex filename + 70368 464 36 70868 114d4 ./Build/WolfSSLClient.elf ``` ## Performace Tuning Options * `DEBUG_WOLFSSL`: Undefine this to disable debug logging. +* `NO_ERROR_STRINGS`: Disables error strings to save code space. * `NO_INLINE`: Disabling inline function saves about 1KB, but is slower. * `WOLFSSL_SMALL_STACK`: Enables stack reduction techniques to allocate stack sections over 100 bytes from heap. * `USE_FAST_MATH`: Uses stack based math, which is faster than the heap based math. +* `ALT_ECC_SIZE`: If using fast math and RSA/DH you can define this to reduce your ECC memory consumption. +* `FP_MAX_BITS`: Is the maximum math size (key size * 2). Used only with `USE_FAST_MATH`. +* `ECC_TIMING_RESISTANT`: Enables timing resistance for ECC and uses slightly less memory. * `ECC_SHAMIR`: Doubles heap usage, but slightly faster * `RSA_LOW_MEM`: Half as much memory but twice as slow. Uses Non-CRT method for private key. AES GCM: `GCM_SMALL`, `GCM_WORD32` or `GCM_TABLE`: Tunes performance and flash/memory usage. * `CURVED25519_SMALL`: Enables small versions of Ed/Curve (FE/GE math). * `USE_SLOW_SHA`: Enables smaller/slower version of SHA. * `USE_SLOW_SHA2`: Over twice as small, but 50% slower -* `FP_MAX_BITS`: Is the maximum math size (key size * 2). Used only with `USE_FAST_MATH`. * `USE_CERT_BUFFERS_1024` or `USE_CERT_BUFFERS_2048`: Size of RSA certs / keys to test with. +* `BENCH_EMBEDDED`: Define this if using the wolfCrypt test/benchmark and using a low memory target. diff --git a/IDE/GCC-ARM/Source/armtarget.c b/IDE/GCC-ARM/Source/armtarget.c index 4f0d5c45d..c090f5a7e 100755 --- a/IDE/GCC-ARM/Source/armtarget.c +++ b/IDE/GCC-ARM/Source/armtarget.c @@ -20,6 +20,8 @@ */ #include "user_settings.h" +#include +#include /* for CUSTOM_RAND_TYPE */ #include #include diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index 13afbafb5..d286f0654 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -50,7 +50,7 @@ #endif -#if defined(CUSTOM_RAND_GENERATE) && !defined(CUSTOM_RAND_TYPE) +#if !defined(CUSTOM_RAND_TYPE) /* To maintain compatibility the default is byte */ #define CUSTOM_RAND_TYPE byte #endif From a02903c43e2552667aafa4af1e8f721695da6aa1 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 11 Sep 2017 16:05:31 -0700 Subject: [PATCH 3/8] Improvements to Makefile templates. Added missing Makefile. Added new static library support. --- IDE/GCC-ARM/Header/user_settings.h | 7 +++++-- IDE/GCC-ARM/Makefile | 16 ++++++++++++++++ IDE/GCC-ARM/Makefile.bench | 2 ++ IDE/GCC-ARM/Makefile.client | 2 ++ IDE/GCC-ARM/Makefile.common | 26 ++++++++++++++++++++------ IDE/GCC-ARM/Makefile.static | 6 ++++++ IDE/GCC-ARM/Makefile.test | 2 ++ IDE/GCC-ARM/include.am | 1 + 8 files changed, 54 insertions(+), 8 deletions(-) create mode 100755 IDE/GCC-ARM/Makefile create mode 100755 IDE/GCC-ARM/Makefile.static diff --git a/IDE/GCC-ARM/Header/user_settings.h b/IDE/GCC-ARM/Header/user_settings.h index 03cc0371d..e2bc1197b 100755 --- a/IDE/GCC-ARM/Header/user_settings.h +++ b/IDE/GCC-ARM/Header/user_settings.h @@ -149,6 +149,9 @@ extern "C" { #undef HAVE_AESGCM #define HAVE_AESGCM + #undef HAVE_AESCCM + #define HAVE_AESCCM + /* GCM Method: GCM_SMALL, GCM_WORD32 or GCM_TABLE */ #undef GCM_SMALL #define GCM_SMALL @@ -260,7 +263,7 @@ extern "C" { /* ------------------------------------------------------------------------- */ #undef DEBUG_WOLFSSL -#define DEBUG_WOLFSSL +//#define DEBUG_WOLFSSL /* Use this to measure / print heap usage */ #if 0 @@ -307,7 +310,7 @@ extern unsigned int custom_rand_generate(void); #define CUSTOM_RAND_GENERATE custom_rand_generate /* Choose RNG method */ -#if 1 +#if 0 /* Use built-in P-RNG (SHA256 based) with HW RNG */ /* P-RNG + HW RNG (P-RNG is ~8K) */ #undef HAVE_HASHDRBG diff --git a/IDE/GCC-ARM/Makefile b/IDE/GCC-ARM/Makefile new file mode 100755 index 000000000..82a12fb0a --- /dev/null +++ b/IDE/GCC-ARM/Makefile @@ -0,0 +1,16 @@ +BUILD_DIR = ./Build + +all: WolfSSLStaticLib WolfCryptTest WolfCryptBench WolfSSLClient + +WolfCryptTest: + $(MAKE) -f Makefile.test +WolfCryptBench: + $(MAKE) -f Makefile.bench +WolfSSLClient: + $(MAKE) -f Makefile.client +WolfSSLStaticLib: + $(MAKE) -f Makefile.static + +clean: + rm -f $(BUILD_DIR)/*.elf $(BUILD_DIR)/*.hex $(BUILD_DIR)/*.map + rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.sym $(BUILD_DIR)/*.disasm diff --git a/IDE/GCC-ARM/Makefile.bench b/IDE/GCC-ARM/Makefile.bench index 70592dc8c..b7149e023 100755 --- a/IDE/GCC-ARM/Makefile.bench +++ b/IDE/GCC-ARM/Makefile.bench @@ -2,4 +2,6 @@ BIN = WolfCryptBench SRC_C = ./Source/benchmark_main.c +all: build_hex + include Makefile.common diff --git a/IDE/GCC-ARM/Makefile.client b/IDE/GCC-ARM/Makefile.client index 74d57c17a..1df58cd66 100644 --- a/IDE/GCC-ARM/Makefile.client +++ b/IDE/GCC-ARM/Makefile.client @@ -2,4 +2,6 @@ BIN = WolfSSLClient SRC_C = ./Source/tls_client.c +all: build_hex + include Makefile.common diff --git a/IDE/GCC-ARM/Makefile.common b/IDE/GCC-ARM/Makefile.common index fe7d454db..b64fc7c41 100755 --- a/IDE/GCC-ARM/Makefile.common +++ b/IDE/GCC-ARM/Makefile.common @@ -1,15 +1,15 @@ -BUILD_DIR = ./Build - # Set to @ if you want to suppress command echo -CMD_ECHO = @ +CMD_ECHO = # Important directories +BUILD_DIR = ./Build TOOLCHAIN_DIR = /opt/gcc-arm-none-eabi/bin INC = -I./Header \ -I./Source \ -I../.. +# Memory Map SRC_LD = ./linker.ld # Defines @@ -31,7 +31,7 @@ LDFLAGS = $(ARCHFLAGS) -Wl,--gc-sections --specs=nano.specs --specs=nosys.specs LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(BIN).map $(DBGFLAGS) LDFLAGS += -Wl,-ereset_handler -# Math lib for DH +# Math lib (for DH) LIBS = -lm SRC_C += ./Source/armtarget.c @@ -45,16 +45,20 @@ vpath %.c $(dir $(SRC_C)) CC = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc AS = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc LD = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc +AR = $(TOOLCHAIN_DIR)/arm-none-eabi-ar NM = $(TOOLCHAIN_DIR)/arm-none-eabi-nm OBJCOPY = $(TOOLCHAIN_DIR)/arm-none-eabi-objcopy OBJDUMP = $(TOOLCHAIN_DIR)/arm-none-eabi-objdump SIZE = $(TOOLCHAIN_DIR)/arm-none-eabi-size - -all: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).hex +build_hex: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).hex @echo "" $(CMD_ECHO) @$(SIZE) $(BUILD_DIR)/$(BIN).elf +build_static: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).a + @echo "" + $(CMD_ECHO) @$(SIZE) $(BUILD_DIR)/$(BIN).a + $(BUILD_DIR): $(CMD_ECHO) mkdir -p $(BUILD_DIR) @@ -80,6 +84,16 @@ $(BUILD_DIR)/$(BIN).elf: $(OBJS_ASM) $(OBJS_C) @echo "Generating disassembly: $(BIN).disasm" $(CMD_ECHO) $(OBJDUMP) -S $@ > $(BUILD_DIR)/$(BIN).disasm +$(BUILD_DIR)/$(BIN).a: $(OBJS_ASM) $(OBJS_C) + @echo "Building static library: $(notdir $@)" + $(CMD_ECHO) $(AR) -r $@ $^ + + @echo "Generating name list: $(BIN).sym" + $(CMD_ECHO) $(NM) -n $@ > $(BUILD_DIR)/$(BIN).sym + + @echo "Generating disassembly: $(BIN).disasm" + $(CMD_ECHO) $(OBJDUMP) -S $@ > $(BUILD_DIR)/$(BIN).disasm + clean: rm -f $(BUILD_DIR)/*.elf $(BUILD_DIR)/*.hex $(BUILD_DIR)/*.map rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.sym $(BUILD_DIR)/*.disasm diff --git a/IDE/GCC-ARM/Makefile.static b/IDE/GCC-ARM/Makefile.static new file mode 100755 index 000000000..66b799bff --- /dev/null +++ b/IDE/GCC-ARM/Makefile.static @@ -0,0 +1,6 @@ +# Project name +BIN = libwolfssl + +all: build_static + +include Makefile.common diff --git a/IDE/GCC-ARM/Makefile.test b/IDE/GCC-ARM/Makefile.test index 78778c6ff..4145f457d 100755 --- a/IDE/GCC-ARM/Makefile.test +++ b/IDE/GCC-ARM/Makefile.test @@ -2,4 +2,6 @@ BIN = WolfCryptTest SRC_C = ./Source/test_main.c +all: build_hex + include Makefile.common diff --git a/IDE/GCC-ARM/include.am b/IDE/GCC-ARM/include.am index 2b5c927ca..634fedf3d 100644 --- a/IDE/GCC-ARM/include.am +++ b/IDE/GCC-ARM/include.am @@ -13,4 +13,5 @@ EXTRA_DIST+= IDE/GCC-ARM/Makefile.bench EXTRA_DIST+= IDE/GCC-ARM/Makefile.client EXTRA_DIST+= IDE/GCC-ARM/Makefile.common EXTRA_DIST+= IDE/GCC-ARM/Makefile.test +EXTRA_DIST+= IDE/GCC-ARM/Makefile.static EXTRA_DIST+= IDE/GCC-ARM/README.md From d4b18a6d3fa31c935f0b6e28ea2ea888c1927a6b Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 12 Sep 2017 08:41:08 -0700 Subject: [PATCH 4/8] Added warning for IO callback stubs to make sure they get implemented. --- IDE/GCC-ARM/Source/tls_client.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/IDE/GCC-ARM/Source/tls_client.c b/IDE/GCC-ARM/Source/tls_client.c index 994c14589..6c0d03de1 100755 --- a/IDE/GCC-ARM/Source/tls_client.c +++ b/IDE/GCC-ARM/Source/tls_client.c @@ -32,12 +32,13 @@ /*------------------------------------------------------------------------*/ static int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx) { - int ret = sz; + int ret = WOLFSSL_CBIO_ERR_GENERAL; (void)ssl; (void)ctx; /* TODO: Exchange data over your own transport */ + #warning TODO: Implement your own recv data transport #if 0 ret = usart_read_buffer_wait(&cdc_uart_module, buf, sz); if (ret == STATUS_ERR_TIMEOUT) @@ -51,12 +52,13 @@ static int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx) static int CbIOSend(WOLFSSL *ssl, char *buf, int sz, void *ctx) { - int ret = sz; + int ret = WOLFSSL_CBIO_ERR_GENERAL; (void)ssl; (void)ctx; /* TODO: Exchange data over your own transport */ + #warning TODO: Implement your own send data transport #if 0 ret = usart_write_buffer_wait(&cdc_uart_module, buf, sz); if (ret == STATUS_ERR_TIMEOUT) From d88d8c3ed8553c71a789999521c330860dc5327f Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 20 Sep 2017 11:43:00 -0700 Subject: [PATCH 5/8] Updates to GCC ARM README.md for the ARCHFLAGS and correct file to update. --- IDE/GCC-ARM/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IDE/GCC-ARM/README.md b/IDE/GCC-ARM/README.md index 27e109e8a..00eb0d26e 100755 --- a/IDE/GCC-ARM/README.md +++ b/IDE/GCC-ARM/README.md @@ -10,8 +10,8 @@ ## Building 1. Make sure you have `gcc-arm-none-eabi` installed. -2. Modify the `Makefile` to point to correct `TOOLCHAIN_DIR`. -3. Use `make` and it will build both targets as `.elf` and `.hex` in `/Build`. +2. Modify the `Makefile.common` to use correct toolchain directory `TOOLCHAIN_DIR` and architecture 'ARCHFLAGS' (default is cortex-m0 / thumb). See [GCC ARM Options](https://gcc.gnu.org/onlinedocs/gcc-4.7.3/gcc/ARM-Options.html) `-mcpu=name`. +3. Use `make` and it will build the static library and wolfCrypt test/benchmark and wolfSSL TLS client targets as `.elf` and `.hex` in `/Build`. Example: From 614736cbb23fd71794d489a22a2a78a2d45e2a1b Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 21 Sep 2017 11:09:44 -0700 Subject: [PATCH 6/8] Add code to detect if Cortex M series and disable architecture specific code in `armtarget.c`. Improved `Makefile.common` to include toolchain prefix. --- IDE/GCC-ARM/Makefile.common | 31 ++++++++++++++++++++----------- IDE/GCC-ARM/README.md | 6 +++++- IDE/GCC-ARM/Source/armtarget.c | 12 ++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/IDE/GCC-ARM/Makefile.common b/IDE/GCC-ARM/Makefile.common index b64fc7c41..beab49806 100755 --- a/IDE/GCC-ARM/Makefile.common +++ b/IDE/GCC-ARM/Makefile.common @@ -3,7 +3,9 @@ CMD_ECHO = # Important directories BUILD_DIR = ./Build -TOOLCHAIN_DIR = /opt/gcc-arm-none-eabi/bin + +# Toolchain location and prefix +TOOLCHAIN = /opt/gcc-arm-none-eabi/bin/arm-none-eabi- INC = -I./Header \ -I./Source \ @@ -26,9 +28,16 @@ CFLAGS = $(ARCHFLAGS) -std=gnu99 -Wall -Wno-cpp \ -ffunction-sections -fdata-sections \ -Os -flto $(DBGFLAGS) -# LD: Remove unused sections, link with newlib-nano implementation, generate map -LDFLAGS = $(ARCHFLAGS) -Wl,--gc-sections --specs=nano.specs --specs=nosys.specs +# LD: Remove unused sections +LDFLAGS = $(ARCHFLAGS) -Wl,--gc-sections + +# LD: Link with newlib-nano implementation +LDFLAGS += --specs=nano.specs --specs=nosys.specs + +# LD: generate map LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(BIN).map $(DBGFLAGS) + +# LD: Entry point LDFLAGS += -Wl,-ereset_handler # Math lib (for DH) @@ -42,14 +51,14 @@ OBJS_C = $(addprefix $(BUILD_DIR)/, $(FILENAMES_C:.c=.o)) vpath %.c $(dir $(SRC_C)) # Tools selection -CC = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc -AS = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc -LD = $(TOOLCHAIN_DIR)/arm-none-eabi-gcc -AR = $(TOOLCHAIN_DIR)/arm-none-eabi-ar -NM = $(TOOLCHAIN_DIR)/arm-none-eabi-nm -OBJCOPY = $(TOOLCHAIN_DIR)/arm-none-eabi-objcopy -OBJDUMP = $(TOOLCHAIN_DIR)/arm-none-eabi-objdump -SIZE = $(TOOLCHAIN_DIR)/arm-none-eabi-size +CC = $(TOOLCHAIN)gcc +AS = $(TOOLCHAIN)gcc +LD = $(TOOLCHAIN)gcc +AR = $(TOOLCHAIN)ar +NM = $(TOOLCHAIN)nm +OBJCOPY = $(TOOLCHAIN)objcopy +OBJDUMP = $(TOOLCHAIN)objdump +SIZE = $(TOOLCHAIN)size build_hex: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).hex @echo "" diff --git a/IDE/GCC-ARM/README.md b/IDE/GCC-ARM/README.md index 00eb0d26e..34119dc43 100755 --- a/IDE/GCC-ARM/README.md +++ b/IDE/GCC-ARM/README.md @@ -1,5 +1,7 @@ # Example Project for GCC ARM +This example is for Cortex M series, but can be adopted for other architectures. + ## Design * All library options are defined in `Header/user_settings.h`. @@ -10,7 +12,9 @@ ## Building 1. Make sure you have `gcc-arm-none-eabi` installed. -2. Modify the `Makefile.common` to use correct toolchain directory `TOOLCHAIN_DIR` and architecture 'ARCHFLAGS' (default is cortex-m0 / thumb). See [GCC ARM Options](https://gcc.gnu.org/onlinedocs/gcc-4.7.3/gcc/ARM-Options.html) `-mcpu=name`. +2. Modify the `Makefile.common`: + * Use correct toolchain path `TOOLCHAIN`. + * Use correct architecture 'ARCHFLAGS' (default is cortex-m0 / thumb). See [GCC ARM Options](https://gcc.gnu.org/onlinedocs/gcc-4.7.3/gcc/ARM-Options.html) `-mcpu=name`. 3. Use `make` and it will build the static library and wolfCrypt test/benchmark and wolfSSL TLS client targets as `.elf` and `.hex` in `/Build`. Example: diff --git a/IDE/GCC-ARM/Source/armtarget.c b/IDE/GCC-ARM/Source/armtarget.c index c090f5a7e..13c830a39 100755 --- a/IDE/GCC-ARM/Source/armtarget.c +++ b/IDE/GCC-ARM/Source/armtarget.c @@ -28,6 +28,13 @@ #include #include +/* Test to determine if ARM Cortex M */ +#if defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH == 6 || __ARM_ARCH == 7) + #define CORTEX_M_SERIES +#endif + + +#ifdef CORTEX_M_SERIES /* Memory initialization */ extern uint32_t __data_load_start__[]; extern uint32_t __data_start__[]; @@ -55,10 +62,12 @@ void meminit32(uint32_t* start, uint32_t* end) *start++ = 0; } } +#endif /* CORTEX_M_SERIES */ /* Entry Point */ void reset_handler(void) { +#ifdef CORTEX_M_SERIES /* Init sections */ memcpy32(__data_load_start__, __data_start__, __data_end__); meminit32(__bss_start__, __bss_end__); @@ -66,6 +75,7 @@ void reset_handler(void) /* Init heap */ __heap_start__[0] = 0; __heap_start__[1] = ((uint32_t)__heap_end__ - (uint32_t)__heap_start__); +#endif /* CORTEX_M_SERIES */ /* Start main */ extern int main(void); @@ -75,6 +85,7 @@ void reset_handler(void) while(1); } +#ifdef CORTEX_M_SERIES // Vector Exception/Interrupt Handlers static void Default_Handler(void) { @@ -199,6 +210,7 @@ const vector_entry vectors[] __attribute__ ((section(".vectors"),used)) = /* remainder go below */ }; +#endif /* CORTEX_M_SERIES */ /* TIME CODE */ From 6058362970694163c3f8689a94f9de674a7c0529 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 21 Sep 2017 14:35:28 -0700 Subject: [PATCH 7/8] Fix bug in `user_settings.h` which was incorrectly setting FP_MAX_BITS to 512 when RSA was enabled. This resulted in `RSA_FUNCTION MP_EXPTMOD_E: memory/config problem`. --- IDE/GCC-ARM/Header/user_settings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IDE/GCC-ARM/Header/user_settings.h b/IDE/GCC-ARM/Header/user_settings.h index e2bc1197b..4b8b7f6af 100755 --- a/IDE/GCC-ARM/Header/user_settings.h +++ b/IDE/GCC-ARM/Header/user_settings.h @@ -124,7 +124,7 @@ extern "C" { /* Use alternate ECC size for ECC math */ #ifdef USE_FAST_MATH - #ifndef NO_RSA + #ifdef NO_RSA /* if not using RSA set FP_MAX_BITS to 256*2 */ #undef FP_MAX_BITS #define FP_MAX_BITS 512 From c4052607d7fd7cb1aefee4fb3a1887aa7cb8c762 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 22 Sep 2017 15:15:48 -0700 Subject: [PATCH 8/8] Updated README.md to include instructions for how to build on a different ARM architecture (like Raspberry Pi CortexA53). Moved the `-T` into `SRC_LD` so its easier to disable. --- IDE/GCC-ARM/Makefile.common | 4 ++-- IDE/GCC-ARM/README.md | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/IDE/GCC-ARM/Makefile.common b/IDE/GCC-ARM/Makefile.common index beab49806..c0a9f852a 100755 --- a/IDE/GCC-ARM/Makefile.common +++ b/IDE/GCC-ARM/Makefile.common @@ -12,7 +12,7 @@ INC = -I./Header \ -I../.. # Memory Map -SRC_LD = ./linker.ld +SRC_LD = -T./linker.ld # Defines DEF = -DWOLFSSL_USER_SETTINGS @@ -85,7 +85,7 @@ $(BUILD_DIR)/%.o: %.c $(BUILD_DIR)/$(BIN).elf: $(OBJS_ASM) $(OBJS_C) @echo "Linking ELF binary: $(notdir $@)" - $(CMD_ECHO) $(LD) $(LDFLAGS) -T$(SRC_LD) -o $@ $^ $(LIBS) + $(CMD_ECHO) $(LD) $(LDFLAGS) $(SRC_LD) -o $@ $^ $(LIBS) @echo "Generating name list: $(BIN).sym" $(CMD_ECHO) $(NM) -n $@ > $(BUILD_DIR)/$(BIN).sym diff --git a/IDE/GCC-ARM/README.md b/IDE/GCC-ARM/README.md index 34119dc43..f14c11fdc 100755 --- a/IDE/GCC-ARM/README.md +++ b/IDE/GCC-ARM/README.md @@ -17,9 +17,22 @@ This example is for Cortex M series, but can be adopted for other architectures. * Use correct architecture 'ARCHFLAGS' (default is cortex-m0 / thumb). See [GCC ARM Options](https://gcc.gnu.org/onlinedocs/gcc-4.7.3/gcc/ARM-Options.html) `-mcpu=name`. 3. Use `make` and it will build the static library and wolfCrypt test/benchmark and wolfSSL TLS client targets as `.elf` and `.hex` in `/Build`. -Example: +### Building for Raspberry Pi + +Example `Makefile.common` changes for Rasperry Pi with Cortex-A53: + +1. Change ARCHFLAGS to `ARCHFLAGS = -mcpu=cortex-a53 -mthumb -mabi=aapcs` to specify Cortex-A53. +2. Comment out `SRC_LD`, since custom memory map is not applicable. +3. Clear `TOOLCHAIN`, so it will use default `gcc`. Set `TOOLCHAIN = ` +4. Comment out `LDFLAGS += --specs=nano.specs --specs=nosys.specs` to disable newlib-nano. + +Note: To comment out a line in a Makefile use place `#` in front of line. + +### Example Build ``` +make clean && make + text data bss dec hex filename 50076 2508 44 52628 cd94 ./Build/WolfCryptTest.elf @@ -32,6 +45,8 @@ Example: ## Performace Tuning Options +These settings are located in `Header/user_settings.h`. + * `DEBUG_WOLFSSL`: Undefine this to disable debug logging. * `NO_ERROR_STRINGS`: Disables error strings to save code space. * `NO_INLINE`: Disabling inline function saves about 1KB, but is slower.