From 6c65550eab6c66bd24bb7dbfd4496eb48ef741eb Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 27 Mar 2019 20:44:38 -0700 Subject: [PATCH 1/3] Added CryptoCell-310 support - SHA-256 - AES CBC - CryptoCell 310 RNG - RSA sign/verify and RSA key gen - RSA encrypt/decrypt, decrypt inline - ECC sign/verify/shared secret - ECC key import/export and key gen pairs - Hardware RNG and RTC of nRF52840 for benchmark timing source - readme doc --- IDE/CRYPTOCELL/README.md | 110 ++++ IDE/CRYPTOCELL/include.am | 8 + IDE/CRYPTOCELL/main.c | 63 +++ IDE/CRYPTOCELL/user_settings.h | 654 ++++++++++++++++++++++++ IDE/include.am | 1 + wolfcrypt/src/aes.c | 94 +++- wolfcrypt/src/asn.c | 5 +- wolfcrypt/src/ecc.c | 276 +++++++++- wolfcrypt/src/include.am | 4 +- wolfcrypt/src/port/arm/cryptoCell.c | 300 +++++++++++ wolfcrypt/src/port/arm/cryptoCellHash.c | 137 +++++ wolfcrypt/src/rsa.c | 220 +++++++- wolfcrypt/src/sha256.c | 2 + wolfcrypt/src/signature.c | 11 + wolfcrypt/src/wc_port.c | 20 +- wolfcrypt/test/test.c | 44 +- wolfssl/wolfcrypt/aes.h | 6 + wolfssl/wolfcrypt/ecc.h | 18 + wolfssl/wolfcrypt/include.am | 3 +- wolfssl/wolfcrypt/port/arm/cryptoCell.h | 117 +++++ wolfssl/wolfcrypt/rsa.h | 7 + wolfssl/wolfcrypt/settings.h | 3 + wolfssl/wolfcrypt/sha256.h | 6 + 23 files changed, 2059 insertions(+), 50 deletions(-) create mode 100644 IDE/CRYPTOCELL/README.md create mode 100644 IDE/CRYPTOCELL/include.am create mode 100644 IDE/CRYPTOCELL/main.c create mode 100644 IDE/CRYPTOCELL/user_settings.h create mode 100644 wolfcrypt/src/port/arm/cryptoCell.c create mode 100644 wolfcrypt/src/port/arm/cryptoCellHash.c create mode 100644 wolfssl/wolfcrypt/port/arm/cryptoCell.h diff --git a/IDE/CRYPTOCELL/README.md b/IDE/CRYPTOCELL/README.md new file mode 100644 index 000000000..4f50d174e --- /dev/null +++ b/IDE/CRYPTOCELL/README.md @@ -0,0 +1,110 @@ +# ARM® TrustZone® CryptoCell 310 Port +## Overview +ARM® TrustZone® CryptoCell 310 is a security subsystem which provides root of trust (RoT) and cryptographic services for a device. +You can enable the wolfSSL support for ARM CryptoCell using the `#define WOLFSSL_CRYPTOCELL`, The CryptoCell APIs are distributed as part of the Nordic nRF5 SDKs [here](https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.0.0%2Fgroup__cryptocell__api.html) . + +## Prerequisites +1. Follow the Nordic website [here](https://www.nordicsemi.com/Software-and-Tools/Software/nRF5-SDK) to download the Nordic nRF5-SDK and software tools. +2. Install the SEGGER Embedded Studio IDE. +3. Run a simple blinky application on your Nordic nRF52840 (PCA10056) development board to confirm that your board functions as expected and the communication between your computer and the board works. + +## Usage +You can start with a wolfcrypt SEGGER embedded studio (ses) example project to integrate the wolfSSL source code. +wolfSSL supports a compile-time user configurable options in the `IDE/CRYPTOCELL/user_settings.h` file. + +The `IDE/CRYPTOCELL/main.c` example application provides a function to run the selected examples at compile time through the following two #defines in user_settings.h. You can define these macro options to disable the test run. +``` +- #undef NO_CRYPT_TEST +- #undef NO_CRYPT_BENCHMARK +``` +#### Supported features +- SHA-256 +- AES CBC +- CryptoCell 310 RNG +- RSA sign/verify and RSA key gen +- RSA encrypt/decrypt +- ECC sign/verify/shared secret +- ECC key import/export and key gen pairs +- Hardware RNG +- RTC for benchmark timing source + +#### Setting up Nordic SDK with wolfSSL +1. Download the wolfSSL source code or a zip file from GitHub and place it under your SDK `InstallFolder/external/` directory. You can also copy or simlink to the source. +``` + For example, + + $cd nRF5_SDK_15.2.0_9412b96/external + $git submodule add https://github.com/wolfSSL/wolfssl.git + + Or, assuming you have already cloned the wolfSSL source code under ~/wolfssl. + + $cd nRF5_SDK_15.2.0_9412b96/external + $sudo ln -s ~/wolfssl . + +``` +2. Copy the example project from [here](https://github.com/tmael/nRF5_SDK/tree/master/examples/crypto/nrf_cc310/wolfcrypt) into your `nRF5_SDK/examples/crypto/nrf_cc310` directory. +``` + $cd /nRF5_SDK_15.2.0_9412b96/examples/crypto/nrf_cc310 + $cp -rf ~/wolfcrypt . +``` +3. Launch the SEGGER Embedded Studio IDE +4. In the main menu, go to File >Open Solutions to open the example solution. Browse to the location containing the wolfcrypt code `/examples/crypto/nrf_cc310/wolfcrypt/pca10056/blank/ses/wolfcrypt_pca10056.emProject` and choose Open. + +#### Building and Running +In the main menu, go to Build > Rebuild your project, then load and run your image on your nRF52840 target platform. Review the test results on the console output. + +### `wolfcrypt_test()` +wolfcrypt_test() prints a message on the target console similar to the following output: +``` +wolfCrypt Test Started +error test passed! +base64 test passed! +asn test passed! +SHA test passed! +SHA-256 test passed! +Hash test passed! +HMAC-SHA test passed! +HMAC-SHA256 test passed! +AES test passed! +RANDOM test passed! +RSA test passed! +ECC test passed! +ECC buffer test passed! +logging test passed! +mutex test passed! +wolfCrypt Test Completed +``` +### `benchmark_test()` +benchmark_test() prints a message on the target console similar to the following output. +``` +Benchmark Test Started +------------------------------------------------------------------------------ + wolfSSL version 3.15.7 +------------------------------------------------------------------------------ +wolfCrypt Benchmark (block bytes 1024, min 1.0 sec each) +RNG 5 MB took 1.000 seconds, 4.858 MB/s +AES-128-CBC-enc 17 MB took 1.001 seconds, 17.341 MB/s +AES-128-CBC-dec 17 MB took 1.000 seconds, 17.285 MB/s +SHA 425 KB took 1.040 seconds, 408.654 KB/s +SHA-256 26 MB took 1.000 seconds, 25.903 MB/s +HMAC-SHA 425 KB took 1.049 seconds, 405.148 KB/s +HMAC-SHA256 24 MB took 1.000 seconds, 23.877 MB/s +RSA 1024 key gen 2 ops took 1.579 sec, avg 789.500 ms, 1.267 ops/sec +RSA 2048 key gen 1 ops took 9.695 sec, avg 9695.000 ms, 0.103 ops/sec +RSA 2048 public 328 ops took 1.001 sec, avg 3.052 ms, 327.672 ops/sec +RSA 2048 private 4 ops took 1.713 sec, avg 428.250 ms, 2.335 ops/sec +ECC 256 key gen 55 ops took 1.017 sec, avg 18.491 ms, 54.081 ops/sec +ECDHE 256 agree 56 ops took 1.017 sec, avg 18.161 ms, 55.064 ops/sec +ECDSA 256 sign 50 ops took 1.004 sec, avg 20.080 ms, 49.801 ops/sec +ECDSA 256 verify 48 ops took 1.028 sec, avg 21.417 ms, 46.693 ops/sec +Benchmark Test Completed +``` +## References +The test results were collected from an nRF52840 reference platform target with the following software and tool chains: +- Nordic nRF52840 development board (PCA10056 1.0.0 2018.49 683529999). +- nRF5_SDK_15.2.0_9412b96 +- SEGGER Embedded Studio for ARM, Release 4.12 Build 2018112601.37855 Linux x64Segger J-Link software +- gcc-arm-none-eabi-8-2018-q4-major +- wolfssl [latest version](https://github.com/wolfSSL/wolfssl) + +For more information or questions, please email [support@wolfssl.com](mailto:support@wolfssl.com) diff --git a/IDE/CRYPTOCELL/include.am b/IDE/CRYPTOCELL/include.am new file mode 100644 index 000000000..1ec170299 --- /dev/null +++ b/IDE/CRYPTOCELL/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/CRYPTOCELL/README.md \ + IDE/CRYPTOCELL/main.c \ + IDE/CRYPTOCELL/user_settings.h diff --git a/IDE/CRYPTOCELL/main.c b/IDE/CRYPTOCELL/main.c new file mode 100644 index 000000000..89bfaf449 --- /dev/null +++ b/IDE/CRYPTOCELL/main.c @@ -0,0 +1,63 @@ +/* main.c + * + * Copyright (C) 2019 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 + +/* wolfCrypt_Init/wolfCrypt_Cleanup to turn CryptoCell hardware on/off */ +#include + +/* SEGGER_RTT_Init, you can potential replace it with other serial terminal */ +#include "SEGGER_RTT.h" + +int main(void) +{ + int ret; + + SEGGER_RTT_Init(); + + if ((ret = wolfCrypt_Init()) != 0) { + printf("wolfCrypt_Init failed %d\n", ret); + return -1; + } + +#ifndef NO_CRYPT_TEST + printf("\nwolfCrypt Test Started\n"); + wolfcrypt_test(NULL); + printf("\nwolfCrypt Test Completed\n"); +#endif + +#ifndef NO_CRYPT_BENCHMARK + printf("\nBenchmark Test Started\n"); + benchmark_test(NULL); + printf("\nBenchmark Test Completed\n"); +#endif + if ((ret = wolfCrypt_Cleanup()) != 0) { + printf("wolfCrypt_Cleanup failed %d\n", ret); + return -1; + } + while(1) { + __WFI(); + } + + return 0; +} + diff --git a/IDE/CRYPTOCELL/user_settings.h b/IDE/CRYPTOCELL/user_settings.h new file mode 100644 index 000000000..0997a9678 --- /dev/null +++ b/IDE/CRYPTOCELL/user_settings.h @@ -0,0 +1,654 @@ +/* user_settings.h + * + * Copyright (C) 2019 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 based on GCC ARM example /IDE/GCC-ARM/Header/user_settings.h*/ + +#ifndef WOLFSSL_USER_SETTINGS_H +#define WOLFSSL_USER_SETTINGS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* ------------------------------------------------------------------------- */ +/* Platform */ +/* ------------------------------------------------------------------------- */ +#if 1 + #define WOLFSSL_CRYPTOCELL + #define WOLFSSL_nRF5x_SDK_15_2 /* for benchmark timer */ + //#define WOLFSSL_CRYPTOCELL_AES /* only CBC mode is supported */ +#else + /* run without CryptoCell, + include IDE/GCC-ARM/Source/wolf_main.c for current_time(). */ +#endif + +#if defined(WOLFSSL_CRYPTOCELL) + #define AES_MAX_KEY_SIZE 128 +#endif /* WOLFSSL_CRYPTOCELL*/ + +/*END */ + + +#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 SIZEOF_LONG_LONG +#define SIZEOF_LONG_LONG 8 + +#undef USE_FAST_MATH +#if 1 + #define USE_FAST_MATH + + #undef TFM_TIMING_RESISTANT + #define TFM_TIMING_RESISTANT + + /* Optimizations */ + //#define TFM_ARM +#endif + +/* Wolf Single Precision Math */ +#undef WOLFSSL_SP +#if 0 + #define WOLFSSL_SP + #define WOLFSSL_SP_SMALL /* use smaller version of code */ + #define WOLFSSL_HAVE_SP_RSA + #define WOLFSSL_HAVE_SP_DH + #define WOLFSSL_HAVE_SP_ECC + #define WOLFSSL_SP_CACHE_RESISTANT + //#define WOLFSSL_SP_MATH /* only SP math - eliminates fast math code */ + + /* 64 or 32 bit version */ + //#define WOLFSSL_SP_ASM /* required if using the ASM versions */ + //#define WOLFSSL_SP_ARM32_ASM + //#define WOLFSSL_SP_ARM64_ASM +#endif + +/* ------------------------------------------------------------------------- */ +/* FIPS - Requires eval or license from wolfSSL */ +/* ------------------------------------------------------------------------- */ +#undef HAVE_FIPS +#if 0 + #define HAVE_FIPS + + #undef HAVE_FIPS_VERSION + #define HAVE_FIPS_VERSION 2 + + #ifdef SINGLE_THREADED + #undef NO_THREAD_LS + #define NO_THREAD_LS + #endif +#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 */ + #if 1 + #undef WC_RSA_BLINDING + #define WC_RSA_BLINDING + #else + #undef WC_NO_HARDEN + #define WC_NO_HARDEN + #endif + + /* RSA PSS Support */ + #if 0 + #define WC_RSA_PSS + #endif + + #if 0 + #define WC_RSA_NO_PADDING + #endif +#else + #define NO_RSA +#endif + +/* ECC */ +#undef HAVE_ECC +#if 1 + #define HAVE_ECC + + /* Manually define enabled curves */ + #undef ECC_USER_CURVES + #define ECC_USER_CURVES + + #ifdef ECC_USER_CURVES + /* Manual Curve Selection */ + //#define HAVE_ECC192 + //#define HAVE_ECC224 + #undef NO_ECC256 + //#define HAVE_ECC384 + //#define HAVE_ECC521 + #endif + + /* 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 + + /* Enable cofactor support */ + #ifdef HAVE_FIPS + #undef HAVE_ECC_CDH + #define HAVE_ECC_CDH + #endif + + /* Validate import */ + #ifdef HAVE_FIPS + #undef WOLFSSL_VALIDATE_ECC_IMPORT + #define WOLFSSL_VALIDATE_ECC_IMPORT + #endif + + /* Compressed Key Support */ + #undef HAVE_COMP_KEY + //#define HAVE_COMP_KEY + + /* Use alternate ECC size for ECC math */ + #ifdef USE_FAST_MATH + #ifdef NO_RSA + /* Custom fastmath size if not using RSA */ + /* MAX = ROUND32(ECC BITS 256) + SIZE_OF_MP_DIGIT(32) */ + #undef FP_MAX_BITS + #define FP_MAX_BITS (256 + 32) + #else + #undef ALT_ECC_SIZE + #define ALT_ECC_SIZE + #endif + + /* Speedups specific to curve */ + #ifndef NO_ECC256 + #undef TFM_ECC256 + #define TFM_ECC256 + #endif + #endif +#endif + +/* DH */ +#undef NO_DH +#if 0 + /* Use table for DH instead of -lm (math) lib dependency */ + #if 0 + #define WOLFSSL_DH_CONST + #define HAVE_FFDHE_2048 + #define HAVE_FFDHE_4096 + //#define HAVE_FFDHE_6144 + //#define HAVE_FFDHE_8192 + #endif + + #ifdef HAVE_FIPS + #define WOLFSSL_VALIDATE_FFC_IMPORT + #define HAVE_FFDHE_Q + #endif +#else + #define NO_DH +#endif + + +/* AES */ +#undef NO_AES +#if 1 + #undef HAVE_AES_CBC + #define HAVE_AES_CBC + /* If you need other than AES-CBC mode, you must undefine WOLFSSL_CRYPTOCELL_AES */ +#if !defined(WOLFSSL_CRYPTOCELL_AES) + #undef HAVE_AESGCM + #define HAVE_AESGCM + + /* GCM Method: GCM_SMALL, GCM_WORD32 or GCM_TABLE */ + #define GCM_SMALL + + + #undef WOLFSSL_AES_DIRECT + #define WOLFSSL_AES_DIRECT + + #undef HAVE_AES_ECB + #define HAVE_AES_ECB + + #undef WOLFSSL_AES_COUNTER + #define WOLFSSL_AES_COUNTER + + #undef HAVE_AESCCM + #define HAVE_AESCCM +#endif + +#else + #define NO_AES +#endif + + +/* DES3 */ +#undef NO_DES3 +#if 0 +#else + #define NO_DES3 +#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 + /* not unrolled - ~2k smaller and ~25% slower */ + //#define USE_SLOW_SHA256 + + /* Sha224 */ + #if 0 + #define WOLFSSL_SHA224 + #endif +#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_SHA512 +#endif + +/* Sha3 */ +#undef WOLFSSL_SHA3 +#if 0 + #define WOLFSSL_SHA3 +#endif + +/* MD5 */ +#undef NO_MD5 +#if 0 + +#else + #define NO_MD5 +#endif + +/* HKDF */ +#undef HAVE_HKDF +#if 0 + #define HAVE_HKDF +#endif + +/* CMAC */ +#undef WOLFSSL_CMAC +#if 0 + #define WOLFSSL_CMAC +#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 +#undef NO_ERROR_STRINGS +#if 0 + #define DEBUG_WOLFSSL +#else + #if 0 + #define NO_ERROR_STRINGS + #endif +#endif + + +/* ------------------------------------------------------------------------- */ +/* Memory */ +/* ------------------------------------------------------------------------- */ + +/* Override Memory API's */ +#if 0 + #undef XMALLOC_OVERRIDE + #define XMALLOC_OVERRIDE + + /* prototypes for user heap override functions */ + /* Note: Realloc only required for normal math */ + #include /* for size_t */ + extern void *myMalloc(size_t n, void* heap, int type); + extern void myFree(void *p, void* heap, int type); + extern void *myRealloc(void *p, size_t n, void* heap, int type); + + #define XMALLOC(n, h, t) myMalloc(n, h, t) + #define XFREE(p, h, t) myFree(p, h, t) + #define XREALLOC(p, n, h, t) myRealloc(p, n, h, t) +#endif + +#if 0 + /* Static memory requires fast math */ + #define WOLFSSL_STATIC_MEMORY + + /* Disable fallback malloc/free */ + #define WOLFSSL_NO_MALLOC + #if 1 + #define WOLFSSL_MALLOC_CHECK /* trap malloc failure */ + #endif +#endif + +/* Memory callbacks */ +#if 0 + #undef USE_WOLFSSL_MEMORY + #define USE_WOLFSSL_MEMORY + + /* Use this to measure / print heap usage */ + #if 1 + #undef WOLFSSL_TRACK_MEMORY + #define WOLFSSL_TRACK_MEMORY + + #undef WOLFSSL_DEBUG_MEMORY + #define WOLFSSL_DEBUG_MEMORY + #endif +#else + #ifndef WOLFSSL_STATIC_MEMORY + #define NO_WOLFSSL_MEMORY + /* Otherwise we will use stdlib malloc, free and realloc */ + #endif +#endif + + +/* ------------------------------------------------------------------------- */ +/* Port */ +/* ------------------------------------------------------------------------- */ + +/* Override Current Time */ +/* Allows custom "custom_time()" function to be used for benchmark */ +#define WOLFSSL_USER_CURRTIME +#define WOLFSSL_GMTIME +#define USER_TICKS + +#if !defined(WOLFSSL_CRYPTOCELL) +// extern unsigned long my_time(unsigned long* timer); +// #define XTIME my_time +#endif + +/* ------------------------------------------------------------------------- */ +/* RNG */ +/* ------------------------------------------------------------------------- */ + +#if defined(WOLFSSL_CRYPTOCELL) +// /* Override P-RNG with HW RNG */ + #undef CUSTOM_RAND_GENERATE_BLOCK + #define CUSTOM_RAND_GENERATE_BLOCK cc310_random_generate + //#define CUSTOM_RAND_GENERATE_BLOCK nrf_random_generate + //#define WOLFSSL_GENSEED_FORTEST /* for software RNG*/ +#else + #define WOLFSSL_GENSEED_FORTEST +#endif + +/* ------------------------------------------------------------------------- */ +/* Custom Standard Lib */ +/* ------------------------------------------------------------------------- */ +/* Allows override of all standard library functions */ +#undef STRING_USER +#if 0 + #define STRING_USER + + #include + + #undef USE_WOLF_STRSEP + #define USE_WOLF_STRSEP + #define XSTRSEP(s1,d) wc_strsep((s1),(d)) + + #undef USE_WOLF_STRTOK + #define USE_WOLF_STRTOK + #define XSTRTOK(s1,d,ptr) wc_strtok((s1),(d),(ptr)) + + #define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n)) + + #define XMEMCPY(d,s,l) memcpy((d),(s),(l)) + #define XMEMSET(b,c,l) memset((b),(c),(l)) + #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n)) + #define XMEMMOVE(d,s,l) memmove((d),(s),(l)) + + #define XSTRLEN(s1) strlen((s1)) + #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n)) + #define XSTRSTR(s1,s2) strstr((s1),(s2)) + + #define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n)) + #define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n)) + #define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n)) + + #define XSNPRINTF snprintf +#endif + + + +/* ------------------------------------------------------------------------- */ +/* Enable Features */ +/* ------------------------------------------------------------------------- */ +#undef WOLFSSL_TLS13 +#if 0 + #define WOLFSSL_TLS13 +#endif + + +#undef WOLFSSL_KEY_GEN +#if 0 + #define WOLFSSL_KEY_GEN +#endif + +#if defined(WOLFSSL_CRYPTOCELL) + #define WOLFSSL_KEY_GEN + #define WOLFSSL_OLD_PRIME_CHECK /* reduce DH test time */ +#endif + +#if defined(HAVE_FIPS) && !defined(WOLFSSL_KEY_GEN) + #define WOLFSSL_OLD_PRIME_CHECK +#endif + +#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 + +#undef WOLFCRYPT_ONLY +//#define WOLFCRYPT_ONLY + +/* 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_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/include.am b/IDE/include.am index 852b37b6a..205ee6a35 100644 --- a/IDE/include.am +++ b/IDE/include.am @@ -22,5 +22,6 @@ include IDE/mynewt/include.am include IDE/Renesas/cs+/Projects/include.am include IDE/Renesas/e2studio/Projects/include.am include IDE/WICED-STUDIO/include.am +include IDE/CRYPTOCELL/include.am EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MDK5-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR IDE/Espressif IDE/zephyr diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 39518caec..3df5a5729 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -1996,6 +1996,81 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) { return wc_AesSetKey(aes, userKey, keylen, iv, dir); } +#elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES) + + int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv, + int dir) + { + SaSiError_t ret = SASI_OK; + SaSiAesIv_t iv_aes; + + if (aes == NULL || + (keylen != AES_128_KEY_SIZE && + keylen != AES_192_KEY_SIZE && + keylen != AES_256_KEY_SIZE)) { + return BAD_FUNC_ARG; + } + #if defined(AES_MAX_KEY_SIZE) + if (keylen > (AES_MAX_KEY_SIZE/8)) { + return BAD_FUNC_ARG; + } + #endif + if (dir != AES_ENCRYPTION && + dir != AES_DECRYPTION) { + return BAD_FUNC_ARG; + } + + if (dir == AES_ENCRYPTION) { + aes->ctx.mode = SASI_AES_ENCRYPT; + SaSi_AesInit(&aes->ctx.user_ctx, + SASI_AES_ENCRYPT, + SASI_AES_MODE_CBC, + SASI_AES_PADDING_NONE); + } + else { + aes->ctx.mode = SASI_AES_DECRYPT; + SaSi_AesInit(&aes->ctx.user_ctx, + SASI_AES_DECRYPT, + SASI_AES_MODE_CBC, + SASI_AES_PADDING_NONE); + } + + aes->keylen = keylen; + aes->rounds = keylen/4 + 6; + XMEMCPY(aes->key, userKey, keylen); + + aes->ctx.key.pKey = (uint8_t*)aes->key; + aes->ctx.key.keySize= keylen; + + ret = SaSi_AesSetKey(&aes->ctx.user_ctx, + SASI_AES_USER_KEY, + &aes->ctx.key, + sizeof(aes->ctx.key)); + if (ret != SASI_OK) { + return BAD_FUNC_ARG; + } + + ret = wc_AesSetIV(aes, iv); + + if (iv) + XMEMCPY(iv_aes, iv, AES_BLOCK_SIZE); + else + XMEMSET(iv_aes, 0, AES_BLOCK_SIZE); + + + ret = SaSi_AesSetIv(&aes->ctx.user_ctx, iv_aes); + if (ret != SASI_OK) { + return ret; + } + return ret; + } + #if defined(WOLFSSL_AES_DIRECT) + int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen, + const byte* iv, int dir) + { + return wc_AesSetKey(aes, userKey, keylen, iv, dir); + } + #endif #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES) /* implemented in wolfcrypt/src/port/caam/caam_aes.c */ @@ -2890,7 +2965,15 @@ int wc_AesSetIV(Aes* aes, const byte* iv) { return wc_esp32AesCbcDecrypt(aes, out, in, sz); } - +#elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES) + int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) + { + return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out); + } + int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) + { + return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out); + } #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES) /* implemented in wolfcrypt/src/port/caam/caam_aes.c */ @@ -6574,7 +6657,9 @@ int wc_AesInit(Aes* aes, void* heap, int devId) (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)) aes->ctx.cfd = -1; #endif - +#if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES) + XMEMSET(&aes->ctx, 0, sizeof(aes->ctx)); +#endif return ret; } @@ -6634,7 +6719,10 @@ int wc_AesGetKeySize(Aes* aes, word32* keySize) if (aes == NULL || keySize == NULL) { return BAD_FUNC_ARG; } - +#if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES) + *keySize = aes->ctx.key.keySize; + return ret; +#endif switch (aes->rounds) { #ifdef WOLFSSL_AES_128 case 10: diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 497074374..6f7eab85f 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -107,6 +107,9 @@ ASN Options: #ifndef NO_RSA #include +#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL) +extern int wc_InitRsaHw(RsaKey* key); +#endif #endif #ifdef WOLF_CRYPTO_CB @@ -2273,7 +2276,7 @@ int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, SkipInt(input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E; #endif -#ifdef WOLFSSL_XILINX_CRYPT +#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL) if (wc_InitRsaHw(key) != 0) { return BAD_STATE_E; } diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index def11e8a3..09586661c 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3375,7 +3375,9 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, word32* outlen) { int err; - +#if defined(WOLFSSL_CRYPTOCELL) + CRYS_ECDH_TempData_t tempBuff; +#endif if (private_key == NULL || public_key == NULL || out == NULL || outlen == NULL) { return BAD_FUNC_ARG; @@ -3416,9 +3418,21 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, else { err = NOT_COMPILED_IN; } +#elif defined(WOLFSSL_CRYPTOCELL) + + /* generate a secret*/ + err = CRYS_ECDH_SVDP_DH(&public_key->ctx.pubKey, + &private_key->ctx.privKey, + out, + outlen, + &tempBuff); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECDH_SVDP_DH for secret failed"); + return err; + } #else - err = wc_ecc_shared_secret_ex(private_key, &public_key->pubkey, out, outlen); #endif /* WOLFSSL_ATECC508A */ @@ -3426,7 +3440,7 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, } -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point, byte* out, word32* outlen, ecc_curve_spec* curve) @@ -3689,11 +3703,11 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point, return err; } -#endif /* !WOLFSSL_ATECC508A */ +#endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL */ #endif /* HAVE_ECC_DHE */ -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) /* return 1 if point is at infinity, 0 if not, < 0 on error */ int wc_ecc_point_is_at_infinity(ecc_point* p) { @@ -3757,7 +3771,7 @@ static int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order) #endif /* !WC_NO_RNG */ } #endif /* WOLFSSL_SP_MATH */ -#endif /* !WOLFSSL_ATECC508A */ +#endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL */ static WC_INLINE void wc_ecc_reset(ecc_key* key) { @@ -3926,12 +3940,18 @@ int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut) int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) { int err; -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) #ifndef WOLFSSL_SP_MATH DECLARE_CURVE_SPECS(curve, ECC_CURVE_FIELD_COUNT); #endif #endif /* !WOLFSSL_ATECC508A */ - +#if defined(WOLFSSL_CRYPTOCELL) + const CRYS_ECPKI_Domain_t* pDomain; + CRYS_ECPKI_KG_TempData_t tempBuff; + CRYS_ECPKI_KG_FipsContext_t fipsCtx; + byte ucompressed_key[ECC_MAX_CRYPTO_HW_SIZE*2 + 1]; + word32 raw_size = (word32) (key->dp->size)*2 + 1; +#endif if (key == NULL || rng == NULL) { return BAD_FUNC_ARG; } @@ -4000,6 +4020,45 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) else { err = NOT_COMPILED_IN; } +#elif defined(WOLFSSL_CRYPTOCELL) + + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); + + /* generate first key pair */ + err = CRYS_ECPKI_GenKeyPair(&wc_rndState, + wc_rndGenVectFunc, + pDomain, + &key->ctx.privKey, + &key->ctx.pubKey, + &tempBuff, + &fipsCtx); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECPKI_GenKeyPair for key pair failed"); + return err; + } + key->type = ECC_PRIVATEKEY; + + err = CRYS_ECPKI_ExportPublKey(&key->ctx.pubKey, + CRYS_EC_PointUncompressed, + &ucompressed_key[0], + &raw_size); + + if (err == SA_SILIB_RET_OK && key->pubkey.x && key->pubkey.y) { + err = mp_read_unsigned_bin(key->pubkey.x, + &ucompressed_key[1], key->dp->size); + err = mp_read_unsigned_bin(key->pubkey.y, + &ucompressed_key[1+key->dp->size],key->dp->size); + } + raw_size = key->dp->size; + err = CRYS_ECPKI_ExportPrivKey(&key->ctx.privKey, + ucompressed_key, + &raw_size); + + if (err == SA_SILIB_RET_OK) { + err = mp_read_unsigned_bin(&key->k, ucompressed_key, raw_size); + } + #else #ifdef WOLFSSL_HAVE_SP_ECC @@ -4233,15 +4292,21 @@ int wc_ecc_set_flags(ecc_key* key, word32 flags) #ifndef NO_ASN -#if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) +#if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) || \ + defined(WOLFSSL_CRYPTOCELL) static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen, mp_int* r, mp_int* s, byte* out, word32 *outlen, WC_RNG* rng, ecc_key* key) { int err; - #ifdef PLUTON_CRYPTO_ECC if (key->devId != INVALID_DEVID) /* use hardware */ +#endif +#if defined(WOLFSSL_CRYPTOCELL) + CRYS_ECDSA_SignUserContext_t sigCtxTemp; + word32 raw_sig_size = *outlen; + word32 msgLenInBytes = inlen; + CRYS_ECPKI_HASH_OpMode_t hash_mode; #endif { word32 keysize = (word32)key->dp->size; @@ -4286,6 +4351,34 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen, return BAD_COND_E; } } + #elif defined(WOLFSSL_CRYPTOCELL) + + hash_mode = cc310_hashModeECC(msgLenInBytes); + if (hash_mode == CRYS_ECPKI_HASH_OpModeLast) { + hash_mode = cc310_hashModeECC(keysize); + hash_mode = CRYS_ECPKI_HASH_SHA256_mode; + } + + /* truncate if hash is longer than key size */ + if (msgLenInBytes > keysize) { + msgLenInBytes = keysize; + } + + /* create signature from an input buffer using a private key*/ + err = CRYS_ECDSA_Sign(&wc_rndState, + wc_rndGenVectFunc, + &sigCtxTemp, + &key->ctx.privKey, + hash_mode, + (byte*)in, + msgLenInBytes, + out, + &raw_sig_size); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECDSA_Sign failed"); + return err; + } #endif /* Load R and S */ @@ -4312,7 +4405,7 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen, return err; } -#endif /* WOLFSSL_ATECC508A || PLUTON_CRYPTO_ECC */ +#endif /* WOLFSSL_ATECC508A || PLUTON_CRYPTO_ECC || WOLFSSL_CRYPTOCELL */ /** Sign a message digest @@ -4381,7 +4474,7 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen, } /* hardware crypto */ - #if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) + #if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL) err = wc_ecc_sign_hash_hw(in, inlen, r, s, out, outlen, rng, key); #else err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s); @@ -4446,7 +4539,7 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen, } #endif /* !NO_ASN */ -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) /** Sign a message digest in The message digest to sign @@ -4781,7 +4874,7 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng, return err; } -#endif /* WOLFSSL_ATECC508A */ +#endif /* WOLFSSL_ATECC508A && WOLFSSL_CRYPTOCELL*/ #endif /* HAVE_ECC_SIGN */ #ifdef WOLFSSL_CUSTOM_CURVES @@ -4843,7 +4936,7 @@ int wc_ecc_free(ecc_key* key) return 0; } -#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_ATECC508A) +#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) #ifdef ECC_SHAMIR /** Computes kA*A + kB*B = C using Shamir's Trick @@ -5146,7 +5239,7 @@ int ecc_mul2add(ecc_point* A, mp_int* kA, } #endif /* ECC_SHAMIR */ -#endif /* !WOLFSSL_SP_MATH && !WOLFSSL_ATECC508A */ +#endif /* !WOLFSSL_SP_MATH && !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCEL*/ #ifdef HAVE_ECC_VERIFY @@ -5308,6 +5401,11 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, word32 keySz; #ifdef WOLFSSL_ATECC508A byte sigRS[ATECC_KEY_SIZE*2]; +#elif defined(WOLFSSL_CRYPTOCELL) + byte sigRS[ECC_MAX_CRYPTO_HW_SIZE*2]; + CRYS_ECDSA_VerifyUserContext_t sigCtxTemp; + word32 msgLenInBytes = hashlen; + CRYS_ECPKI_HASH_OpMode_t hash_mode; #elif !defined(WOLFSSL_SP_MATH) int did_init = 0; ecc_point *mG = NULL, *mQ = NULL; @@ -5377,7 +5475,44 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, return err; } (void)hashlen; +#elif defined(WOLFSSL_CRYPTOCELL) + /* Extract R and S */ + + err = mp_to_unsigned_bin(r, &sigRS[0]); + if (err != MP_OKAY) { + return err; + } + err = mp_to_unsigned_bin(s, &sigRS[keySz]); + if (err != MP_OKAY) { + return err; + } + + hash_mode = cc310_hashModeECC(msgLenInBytes); + if (hash_mode == CRYS_ECPKI_HASH_OpModeLast) { + hash_mode = cc310_hashModeECC(keySz); + hash_mode = CRYS_ECPKI_HASH_SHA256_mode; + } + /* truncate if hash is longer than key size */ + if (msgLenInBytes > keySz) { + msgLenInBytes = keySz; + } + + /* verify the signature using the public key */ + err = CRYS_ECDSA_Verify(&sigCtxTemp, + &key->ctx.pubKey, + hash_mode, + &sigRS[0], + keySz*2, + (byte*)hash, + msgLenInBytes); + + if (err != SA_SILIB_RET_OK) { + WOLFSSL_MSG("CRYS_ECDSA_Verify failed"); + return err; + } + /* valid signature if we get to this point */ + *res = 1; #else /* checking if private key with no public part */ if (key->type == ECC_PRIVATEKEY_ONLY) { @@ -5954,7 +6089,7 @@ int wc_ecc_export_x963_ex(ecc_key* key, byte* out, word32* outLen, #endif /* HAVE_ECC_KEY_EXPORT */ -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) /* is ecc point on curve described by dp ? */ int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime) @@ -6217,7 +6352,7 @@ static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a, return err; } #endif -#endif /* !WOLFSSL_ATECC508A */ +#endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL*/ /* perform sanity checks on ecc key validity, 0 on success */ @@ -6225,7 +6360,7 @@ int wc_ecc_check_key(ecc_key* key) { int err; #ifndef WOLFSSL_SP_MATH -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) mp_int* b = NULL; #ifdef USE_ECC_B_PARAM DECLARE_CURVE_SPECS(curve, 4); @@ -6240,7 +6375,7 @@ int wc_ecc_check_key(ecc_key* key) if (key == NULL) return BAD_FUNC_ARG; -#ifdef WOLFSSL_ATECC508A +#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_CRYPTOCELL) err = 0; /* consider key check success on ATECC508A */ @@ -6626,7 +6761,10 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, { int ret; word32 idx = 0; - +#if defined(WOLFSSL_CRYPTOCELL) + const CRYS_ECPKI_Domain_t* pDomain; + CRYS_ECPKI_BUILD_TempData_t tempBuff; +#endif if (key == NULL || priv == NULL) return BAD_FUNC_ARG; @@ -6652,6 +6790,38 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, #ifdef WOLFSSL_ATECC508A /* Hardware does not support loading private keys */ return NOT_COMPILED_IN; +#elif defined(WOLFSSL_CRYPTOCELL) + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); + + if (pub != NULL && pub[0] != '\0') { + /* create public key from external key buffer */ + ret = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, + (byte*)pub, + pubSz, + &key->ctx.pubKey, + &tempBuff); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed"); + return ret; + } + } + /* import private key */ + if (priv != NULL && priv[0] != '\0') { + + /* Create private key from external key buffer*/ + ret = CRYS_ECPKI_BuildPrivKey(pDomain, + priv, + privSz, + &key->ctx.privKey); + + if (ret != SA_SILIB_RET_OK) { + WOLFSSL_MSG("CRYS_ECPKI_BuildPrivKey failed"); + return ret; + } + + ret = mp_read_unsigned_bin(&key->k, priv, privSz); + } #else @@ -6897,7 +7067,12 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, const char* qy, const char* d, int curve_id, int encType) { int err = MP_OKAY; - +#if defined(WOLFSSL_CRYPTOCELL) + const CRYS_ECPKI_Domain_t* pDomain; + CRYS_ECPKI_BUILD_TempData_t tempBuff; + byte key_raw[ECC_MAX_CRYPTO_HW_SIZE*2 + 1]; + word32 keySz = key->dp->size; +#endif /* if d is NULL, only import as public key using Qx,Qy */ if (key == NULL || qx == NULL || qy == NULL) { return BAD_FUNC_ARG; @@ -6960,14 +7135,65 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, err = wc_export_int(key->pubkey.y, &key->pubkey_raw[keySz], &keySz, keySz, WC_TYPE_UNSIGNED_BIN); } +#elif defined(WOLFSSL_CRYPTOCELL) + if (err == MP_OKAY) { + key_raw[0] = ECC_POINT_UNCOMP; + keySz = key->dp->size; + err = wc_export_int(key->pubkey.x, &key_raw[1], &keySz, keySz, + WC_TYPE_UNSIGNED_BIN); + if (err == MP_OKAY) + err = wc_export_int(key->pubkey.y, &key_raw[1+keySz], + &keySz, keySz, WC_TYPE_UNSIGNED_BIN); + + + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); + + /* create public key from external key buffer */ + err = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, + key_raw, + keySz*2 + 1, + &key->ctx.pubKey, + &tempBuff); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed"); + return err; + } + } + #endif /* import private key */ if (err == MP_OKAY) { - if (d != NULL) { + if (d != NULL && d[0] != '\0') { #ifdef WOLFSSL_ATECC508A /* Hardware doesn't support loading private key */ err = NOT_COMPILED_IN; + + #elif defined(WOLFSSL_CRYPTOCELL) + + key->type = ECC_PRIVATEKEY; + + if (encType == WC_TYPE_HEX_STR) + err = mp_read_radix(&key->k, d, MP_RADIX_HEX); + else + err = mp_read_unsigned_bin(&key->k, (const byte*)d, + key->dp->size); + + err = wc_export_int(&key->k, &key_raw[0], &keySz, keySz, + WC_TYPE_UNSIGNED_BIN); + + /* Create private key from external key buffer*/ + err = CRYS_ECPKI_BuildPrivKey(pDomain, + key_raw, + keySz, + &key->ctx.privKey); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECPKI_BuildPrivKey failed"); + return err; + } + #else key->type = ECC_PRIVATEKEY; @@ -9148,7 +9374,7 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, #ifdef HAVE_COMP_KEY -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) #ifndef WOLFSSL_SP_MATH int do_mp_jacobi(mp_int* a, mp_int* n, int* c); @@ -9472,7 +9698,7 @@ int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret) #endif } #endif -#endif /* !WOLFSSL_ATECC508A */ +#endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL */ /* export public ECC key in ANSI X9.63 format compressed */ diff --git a/wolfcrypt/src/include.am b/wolfcrypt/src/include.am index 936fb1252..21e4ab315 100644 --- a/wolfcrypt/src/include.am +++ b/wolfcrypt/src/include.am @@ -72,7 +72,9 @@ EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \ wolfcrypt/src/port/Espressif/esp32_sha.c \ wolfcrypt/src/port/Espressif/esp32_util.c \ wolfcrypt/src/port/Espressif/esp32_mp.c \ - wolfcrypt/src/port/Espressif/README.md + wolfcrypt/src/port/Espressif/README.md \ + wolfcrypt/src/port/arm/cryptoCell.c \ + wolfcrypt/src/port/arm/cryptoCellHash.c if BUILD_CRYPTOCB diff --git a/wolfcrypt/src/port/arm/cryptoCell.c b/wolfcrypt/src/port/arm/cryptoCell.c new file mode 100644 index 000000000..0edb194a3 --- /dev/null +++ b/wolfcrypt/src/port/arm/cryptoCell.c @@ -0,0 +1,300 @@ +/* cryptoCell.c + * + * Copyright (C) 2019 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 + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include +#include + +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif + +#ifndef WOLFSSL_CRYPTOCELL_C +#define WOLFSSL_CRYPTOCELL_C + +#if defined(WOLFSSL_CRYPTOCELL) +#include + +CRYS_RND_State_t wc_rndState; +CRYS_RND_WorkBuff_t wc_rndWorkBuff; +SaSiRndGenerateVectWorkFunc_t wc_rndGenVectFunc = CRYS_RND_GenerateVector; + +static word32 cc310_enableCount = 0; + +static void cc310_enable(void) +{ + cc310_enableCount++; + + /* Enable the CC310 HW/IQ once*/ + if (cc310_enableCount == 1) { + NRF_CRYPTOCELL->ENABLE = 1; + NVIC_EnableIRQ(CRYPTOCELL_IRQn); + } +} + +static void cc310_disable(void) +{ + cc310_enableCount--; + + /* Disable HW/IRQ if no more users */ + if (cc310_enableCount == 0) { + NRF_CRYPTOCELL->ENABLE = 0; + NVIC_DisableIRQ(CRYPTOCELL_IRQn); + } +} +int cc310_Init(void) +{ + int ret = 0; + static bool initialized = false; + + if (!initialized) { + /* Enable the CC310 HW. */ + cc310_enable(); + + /*Initialize the CC310 run-time library*/ + ret = SaSi_LibInit(); + + if (ret != SA_SILIB_RET_OK) { + WOLFSSL_MSG("Error SaSi_LibInit"); + return ret; + } + + /* RNG CryptoCell CC310 */ + ret = CRYS_RndInit(&wc_rndState, &wc_rndWorkBuff); + if (ret != CRYS_OK) { + WOLFSSL_MSG("Error CRYS_RndInit"); + return ret; + } + initialized = true; + } + return ret; +} +void cc310_Free(void) +{ + CRYSError_t crys_result; + + SaSi_LibFini(); + + crys_result = CRYS_RND_UnInstantiation(&wc_rndState); + + if (crys_result != CRYS_OK) { + WOLFSSL_MSG("Error RYS_RND_UnInstantiation"); + } + cc310_disable(); +} + +int cc310_random_generate(byte* output, word32 size) +{ + CRYSError_t crys_result; + + crys_result = CRYS_RND_GenerateVector(&wc_rndState, size, output); + + return (crys_result == CRYS_OK) ? 0 : -1; +} +#ifdef HAVE_ECC +CRYS_ECPKI_DomainID_t cc310_mapCurve(int curve_id) +{ + switch(curve_id) + { + case ECC_CURVE_DEF: return CRYS_ECPKI_DomainID_secp256r1; /* default */ + case ECC_SECP160K1: return CRYS_ECPKI_DomainID_secp160k1; + case ECC_SECP160R1: return CRYS_ECPKI_DomainID_secp160r1; + case ECC_SECP160R2: return CRYS_ECPKI_DomainID_secp160r2; + case ECC_SECP192K1: return CRYS_ECPKI_DomainID_secp192k1; + case ECC_SECP192R1: return CRYS_ECPKI_DomainID_secp192r1; + case ECC_SECP224K1: return CRYS_ECPKI_DomainID_secp224k1; + case ECC_SECP224R1: return CRYS_ECPKI_DomainID_secp224r1; + case ECC_SECP256K1: return CRYS_ECPKI_DomainID_secp256k1; + case ECC_SECP256R1: return CRYS_ECPKI_DomainID_secp256r1; + case ECC_SECP384R1: return CRYS_ECPKI_DomainID_secp384r1; + case ECC_SECP521R1: return CRYS_ECPKI_DomainID_secp521r1; + default: WOLFSSL_MSG("Curve not identified"); + return CRYS_ECPKI_DomainID_Builded; + } +} +#endif /* HAVE_ECC */ + +#ifndef NO_RSA +CRYS_RSA_HASH_OpMode_t cc310_hashModeRSA(enum wc_HashType hash_type) +{ + switch(hash_type) + { + case WC_HASH_TYPE_MD5: + #ifndef NO_MD5 + return CRYS_RSA_HASH_MD5_mode; + #endif + case WC_HASH_TYPE_SHA: + #ifndef NO_SHA + return CRYS_RSA_HASH_SHA1_mode; + #endif + case WC_HASH_TYPE_SHA224: + #ifdef WOLFSSL_SHA224 + return CRYS_RSA_HASH_SHA224_mode; + #endif + case WC_HASH_TYPE_SHA256: + #ifndef NO_SHA256 + return CRYS_RSA_HASH_SHA256_mode; + #endif + case WC_HASH_TYPE_SHA384: + #ifdef WOLFSSL_SHA384 + return CRYS_RSA_HASH_SHA384_mode; + #endif + case WC_HASH_TYPE_SHA512: + #ifdef WOLFSSL_SHA512 + return CRYS_RSA_HASH_SHA512_mode; + #endif + default: + return CRYS_RSA_After_HASH_NOT_KNOWN_mode; + } +} +#endif /* NO_RSA */ +#ifdef HAVE_ECC +CRYS_ECPKI_HASH_OpMode_t cc310_hashModeECC(int hash_size) +{ + CRYS_ECPKI_HASH_OpMode_t hash_mode; + switch (hash_size) + { + case 20: + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode; + break; + case 28: + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode; + break; + case 32: + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode; + break; + case 48: + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode; + break; + case 64: + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode; + break; + default: + hash_mode = CRYS_ECPKI_HASH_OpModeLast; + break; + } + return hash_mode; +} +#endif /* HAVE_ECC */ +#endif /* WOLFSSL_CRYPTOCELL*/ + +#if !defined(NO_CRYPT_BENCHMARK) && defined(WOLFSSL_nRF5x_SDK_15_2) + +static int mRtcSec = 0; + +const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0); + +static void rtc_handler(nrfx_rtc_int_type_t int_type) +{ + if (int_type == NRFX_RTC_INT_COMPARE0) { + mRtcSec++; + nrfx_rtc_counter_clear(&rtc); + nrfx_rtc_int_enable(&rtc, RTC_CHANNEL_INT_MASK(0)); +#ifdef BSP_LED_1 + nrf_gpio_pin_toggle(BSP_LED_1); +#endif + } + else if (int_type == NRF_DRV_RTC_INT_TICK) { +#ifdef BSP_LED_0 + nrf_gpio_pin_toggle(BSP_LED_0); +#endif + } +} + +static void rtc_config(void) +{ + uint32_t err_code; + nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG; + + /* configure gpio for pin toggling. */ + bsp_board_init(BSP_INIT_LEDS); + + /* start the internal LFCLK XTAL oscillator.*/ + err_code = nrf_drv_clock_init(); + APP_ERROR_CHECK(err_code); + nrf_drv_clock_lfclk_request(NULL); + + /* Initialize RTC instance */ + err_code = nrfx_rtc_init(&rtc, &config, rtc_handler); + APP_ERROR_CHECK(err_code); + + /* Enable tick event */ + nrfx_rtc_tick_enable(&rtc, false); + + /* Set compare channel to trigger interrupt after 1 seconds */ + err_code = nrfx_rtc_cc_set(&rtc, 0, RTC_INPUT_FREQ, true); + APP_ERROR_CHECK(err_code); + + /* Power on RTC instance */ + nrfx_rtc_enable(&rtc); +} + +static int rtc_get_ms(void) +{ + /* Prescaler is 12-bit for COUNTER: frequency = (32768/(PRESCALER+1)) */ + int frequency = (RTC_INPUT_FREQ / (rtc_prescaler_get(rtc.p_reg) + 1)); + uint32_t counter = nrfx_rtc_counter_get(&rtc); + + /* Convert with rounding frequency to milliseconds */ + return ((counter * 1000) + (frequency / 2) ) / frequency; +} + +double current_time(int reset) +{ + double time; + static bool initialized = false; + + if (!initialized) { + rtc_config(); + initialized = true; + } + time = mRtcSec; + time += (double)rtc_get_ms() / 1000; + + return time; +} + +int nrf_random_generate(byte* output, word32 size) +{ + uint32_t err_code; + static bool initialized = false; + + /* RNG must be initialized once */ + if (!initialized) { + err_code = nrf_drv_rng_init(NULL); + if (err_code != NRF_SUCCESS) { + return -1; + } + initialized = true; + } + nrf_drv_rng_block_rand(output, size); + return 0; +} +#endif /* !NO_CRYPT_BENCHMARK && WOLFSSL_nRF5x_SDK_15_2 */ +#endif /* WOLFSSL_CRYPTOCELL_C */ + diff --git a/wolfcrypt/src/port/arm/cryptoCellHash.c b/wolfcrypt/src/port/arm/cryptoCellHash.c new file mode 100644 index 000000000..a5119810d --- /dev/null +++ b/wolfcrypt/src/port/arm/cryptoCellHash.c @@ -0,0 +1,137 @@ +/* cryptoCellHash.c + * + * Copyright (C) 2019 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 + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include +#include + +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif + +#if !defined(NO_SHA) && defined(WOLFSSL_CRYPTOCELL_HASH) +#include + +#if defined(WOLFSSL_CRYPTOCELL) +#include + +int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) +{ + CRYSError_t ret = 0; + + (void)heap; + (void)devId; + + if (sha256 == NULL) + return BAD_FUNC_ARG; + + XMEMSET(sha256->digest, 0, sizeof(sha256->digest)); + + /* initializes the HASH context and machine to the supported mode.*/ + ret = CRYS_HASH_Init(&sha256->ctx, CRYS_HASH_SHA256_mode); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("Error CRYS_HASH_Init failed"); + } + + return ret; + } + +WOLFSSL_API int wc_InitSha256(Sha256* sha256) +{ + return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); +} + +int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len) +{ + CRYSError_t ret = 0; + size_t length; + size_t remaining = len; + byte const * p_cur = data; + + if (sha256 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + + if (data == NULL && len == 0) { + /* valid, but do nothing */ + return 0; + } + + /* If the input is larger than CC310_MAX_LENGTH_DMA, split into smaller */ + do { + length = (remaining > CC310_MAX_LENGTH_DMA) ? + CC310_MAX_LENGTH_DMA : remaining; + + ret = CRYS_HASH_Update(&sha256->ctx, (uint8_t *)p_cur, length); + + remaining -= length; + p_cur += length; + + } while (ret == CRYS_OK && remaining > 0); + + return ret; + } + +int wc_Sha256Final(wc_Sha256* sha256, byte* hash) +{ + CRYSError_t ret = 0; + CRYS_HASH_Result_t hashResult; + + if (sha256 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + + ret = CRYS_HASH_Finish(&sha256->ctx, hashResult); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("Error CRYS_HASH_Finish failed"); + return ret; + } + XMEMCPY(sha256->digest, hashResult, WC_SHA256_DIGEST_SIZE); + + XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE); + + /* reset state */ + return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); +} + +void wc_Sha256Free(wc_Sha256* sha256) +{ + if (sha256 == NULL) + return; + +#ifdef WOLFSSL_SMALL_STACK_CACHE + if (sha256->W != NULL) { + XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST); + sha256->W = NULL; + } +#endif +} +#endif /*WOLFSSL_CRYPTOCELL*/ +#endif /* !NO_SHA256 */ diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 1eb7ee9d3..5441c576d 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -429,10 +429,85 @@ int wc_InitRsaHw(RsaKey* key) } } #endif + return 0; +} /* WOLFSSL_XILINX_CRYPT*/ +#elif defined(WOLFSSL_CRYPTOCELL) + +int wc_InitRsaHw(RsaKey* key) +{ + CRYSError_t ret = 0; + byte e[3]; + word32 eSz = sizeof(e); + byte n[256]; + word32 nSz = sizeof(n); + byte d[256]; + word32 dSz = sizeof(d); + byte p[128]; + word32 pSz = sizeof(p); + byte q[128]; + word32 qSz = sizeof(q); + + if (key == NULL) { + return BAD_FUNC_ARG; + } + + ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz); + if (ret != 0) + return MP_READ_E; + + ret = CRYS_RSA_Build_PubKey(&key->ctx.pubKey, e, eSz, n, nSz); + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_Build_PubKey failed"); + return ret; + } + + ret = CRYS_RSA_Build_PrivKey(&key->ctx.privKey, d, dSz, e, eSz, n, nSz); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_Build_PrivKey failed"); + return ret; + } + key->type = RSA_PRIVATE; return 0; } -#endif /* WOLFSSL_XILINX_CRYPT */ +static int cc310_RSA_GenerateKeyPair(RsaKey* key, int size, long e) +{ + CRYSError_t ret = 0; + CRYS_RSAKGData_t KeyGenData; + CRYS_RSAKGFipsContext_t FipsCtx; + byte ex[3]; + uint16_t eSz = sizeof(ex); + byte n[256]; + uint16_t nSz = sizeof(n); + + ret = CRYS_RSA_KG_GenerateKeyPair(&wc_rndState, + wc_rndGenVectFunc, + (byte*)&e, + 3*sizeof(uint8_t), + size, + &key->ctx.privKey, + &key->ctx.pubKey, + &KeyGenData, + &FipsCtx); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_KG_GenerateKeyPair failed"); + return ret; + } + + ret = CRYS_RSA_Get_PubKey(&key->ctx.pubKey, ex, &eSz, n, &nSz); + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_Get_PubKey failed"); + return ret; + } + ret = wc_RsaPublicKeyDecodeRaw(n, nSz, ex, eSz, key); + + key->type = RSA_PRIVATE; + + return ret; +} +#endif /* WOLFSSL_CRYPTOCELL */ int wc_FreeRsaKey(RsaKey* key) { @@ -500,6 +575,9 @@ int wc_FreeRsaKey(RsaKey* key) * Verify that k = (k^e)^d, for some k: 1 < k < n-1. */ int wc_CheckRsaKey(RsaKey* key) { +#if defined(WOLFSSL_CRYPTOCELL) + return 0; +#endif #ifdef WOLFSSL_SMALL_STACK mp_int *k = NULL, *tmp = NULL; #else @@ -2136,6 +2214,109 @@ int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz, } #endif /* WC_RSA_DIRECT || WC_RSA_NO_PADDING */ +#if defined(WOLFSSL_CRYPTOCELL) +static int cc310_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, + word32 outLen, RsaKey* key) +{ + CRYSError_t ret = 0; + CRYS_RSAPrimeData_t primeData; + int modulusSize = wc_RsaEncryptSize(key); + + /* The out buffer must be at least modulus size bytes long. */ + if (outLen < modulusSize) + return BAD_FUNC_ARG; + + ret = CRYS_RSA_PKCS1v15_Encrypt(&wc_rndState, + wc_rndGenVectFunc, + &key->ctx.pubKey, + &primeData, + (byte*)in, + inLen, + out); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Encrypt failed"); + return -1; + } + + return modulusSize; +} +static int cc310_RsaPublicDecrypt(const byte* in, word32 inLen, byte* out, + word32 outLen, RsaKey* key) +{ + CRYSError_t ret = 0; + CRYS_RSAPrimeData_t primeData; + uint16_t actualOutLen = outLen; + + ret = CRYS_RSA_PKCS1v15_Decrypt(&key->ctx.privKey, + &primeData, + (byte*)in, + inLen, + out, + &actualOutLen); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Decrypt failed"); + return -1; + } + return actualOutLen; +} + +static int cc310_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, + word32 outLen, RsaKey* key, enum wc_HashType hash) +{ + CRYSError_t ret = 0; + uint16_t actualOutLen = outLen*sizeof(byte); + CRYS_RSAPrivUserContext_t contextPrivate; + CRYS_RSA_HASH_OpMode_t mode = cc310_hashModeRSA(hash); + + if (mode == CRYS_RSA_After_HASH_NOT_KNOWN_mode) { + mode = CRYS_RSA_HASH_SHA256_mode; + } + + ret = CRYS_RSA_PKCS1v15_Sign(&wc_rndState, + wc_rndGenVectFunc, + &contextPrivate, + &key->ctx.privKey, + mode, + (byte*)in, + inLen, + out, + &actualOutLen); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Sign failed"); + return -1; + } + return actualOutLen; +} + +static int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig, + RsaKey* key, enum wc_HashType hash) +{ + CRYSError_t ret = 0; + CRYS_RSAPubUserContext_t contextPub; + CRYS_RSA_HASH_OpMode_t mode = cc310_hashModeRSA(hash); + + if (mode == CRYS_RSA_After_HASH_NOT_KNOWN_mode) { + mode = CRYS_RSA_HASH_SHA256_mode; + } + /* verify the signature in the sig pointer */ + ret = CRYS_RSA_PKCS1v15_Verify(&contextPub, + &key->ctx.pubKey, + mode, + (byte*)in, + inLen, + sig); + + if (ret != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Verify failed"); + return -1; + } + + return ret; +} +#endif /* WOLFSSL_CRYPTOCELL */ int wc_RsaFunction(const byte* in, word32 inLen, byte* out, word32* outLen, int type, RsaKey* key, WC_RNG* rng) @@ -2316,7 +2497,17 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out, return NitroxRsaSSL_Sign(in, inLen, out, outLen, key); } } - #endif + #elif defined(WOLFSSL_CRYPTOCELL) + if (rsa_type == RSA_PUBLIC_ENCRYPT && + pad_value == RSA_BLOCK_TYPE_2) { + + return cc310_RsaPublicEncrypt(in, inLen, out, outLen, key); + } + else if (rsa_type == RSA_PRIVATE_ENCRYPT && + pad_value == RSA_BLOCK_TYPE_1) { + return cc310_RsaSSL_Sign(in, inLen, out, outLen, key, hash); + } + #endif /* WOLFSSL_CRYPTOCELL */ key->state = RSA_STATE_ENCRYPT_PAD; ret = wc_RsaPad_ex(in, inLen, out, sz, pad_value, rng, pad_type, hash, @@ -2366,6 +2557,7 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out, return ret; } + #endif /* Gives the option of choosing padding type @@ -2424,7 +2616,20 @@ static int RsaPrivateDecryptEx(byte* in, word32 inLen, byte* out, return NitroxRsaSSL_Verify(in, inLen, out, &key->dataLen, key); } } - #endif + #elif defined(WOLFSSL_CRYPTOCELL) + if (rsa_type == RSA_PRIVATE_DECRYPT && + pad_value == RSA_BLOCK_TYPE_2) { + ret = cc310_RsaPublicDecrypt(in, inLen, out, outLen, key); + if (outPtr != NULL) + *outPtr = out; /* for inline */ + return ret; + } + else if (rsa_type == RSA_PUBLIC_DECRYPT && + pad_value == RSA_BLOCK_TYPE_1) { + return cc310_RsaSSL_Verify(in, inLen, out, key, hash); + } + #endif /* WOLFSSL_CRYPTOCELL */ + #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) /* verify the tmp ptr is NULL, otherwise indicates bad state */ @@ -2668,6 +2873,7 @@ int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen, #ifdef WC_RSA_BLINDING rng = key->rng; #endif + return RsaPrivateDecryptEx((byte*)in, inLen, out, outLen, NULL, key, RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); @@ -3357,6 +3563,12 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) if (e < 3 || (e & 1) == 0) return BAD_FUNC_ARG; +#if defined(WOLFSSL_CRYPTOCELL) + + return cc310_RSA_GenerateKeyPair(key, size, e); + +#endif /*WOLFSSL_CRYPTOCELL*/ + #ifdef WOLF_CRYPTO_CB if (key->devId != INVALID_DEVID) { int ret = wc_CryptoCb_MakeRsaKey(key, size, e, rng); @@ -3549,7 +3761,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) return err; } -#ifdef WOLFSSL_XILINX_CRYPT +#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL) if (wc_InitRsaHw(key) != 0) { return BAD_STATE_E; } diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index db222b72d..286b78721 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -99,6 +99,8 @@ #if defined(WOLFSSL_TI_HASH) /* #include included by wc_port.c */ +#elif defined(WOLFSSL_CRYPTOCELL) + /* wc_port.c includes wolfcrypt/src/port/arm/cryptoCellHash.c */ #else #include diff --git a/wolfcrypt/src/signature.c b/wolfcrypt/src/signature.c index cb30c3b88..57d327570 100644 --- a/wolfcrypt/src/signature.c +++ b/wolfcrypt/src/signature.c @@ -176,6 +176,16 @@ int wc_SignatureVerifyHash( case WC_SIGNATURE_TYPE_RSA: { #ifndef NO_RSA +#if defined(WOLFSSL_CRYPTOCELL) + /* the signature must propagate to the cryptocell to get verfied */ + ret = wc_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, sig_len, (RsaKey*)key); + if (ret != 0) { + WOLFSSL_MSG("RSA Signature Verify difference!"); + ret = SIG_VERIFY_E; + } + +#else /* WOLFSSL_CRYPTOCELL */ + word32 plain_len = hash_len; byte *plain_data; @@ -210,6 +220,7 @@ int wc_SignatureVerifyHash( else { ret = MEMORY_E; } +#endif /* !WOLFSSL_CRYPTOCELL */ #else ret = SIG_TYPE_E; #endif diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 7a72032d6..45b3567ad 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -159,7 +159,14 @@ int wolfCrypt_Init(void) return ret; } #endif - + #if defined(WOLFSSL_CRYPTOCELL) + /* enable and initialize the ARM CryptoCell 3xx runtime library */ + ret = cc310_Init(); + if (ret != 0) { + WOLFSSL_MSG("CRYPTOCELL init failed"); + return ret; + } + #endif #if defined(WOLFSSL_STSAFEA100) stsafe_interface_init(); #endif @@ -240,7 +247,9 @@ int wolfCrypt_Cleanup(void) defined(WOLFSSL_IMX6_CAAM_BLOB) wc_caamFree(); #endif - + #if defined(WOLFSSL_CRYPTOCELL) + cc310_Free(); + #endif initRefCount = 0; /* allow re-init */ } @@ -2186,3 +2195,10 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) #include /* initialize and Mutex for TI Crypt Engine */ #include /* md5, sha1, sha224, sha256 */ #endif +#if defined(WOLFSSL_CRYPTOCELL) + #include /* CC310, RTC and RNG */ +#if !defined(NO_SHA256) + #define WOLFSSL_CRYPTOCELL_HASH + #include /* sha256 */ +#endif +#endif \ No newline at end of file diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 6c5fa2c58..1146ae07b 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -5398,7 +5398,8 @@ static int aes_key_size_test(void) ret = wc_AesSetKey(&aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION); if (ret != BAD_FUNC_ARG) return -4807; -#ifndef HAVE_FIPS +/* CryptoCell handles rounds internally */ +#if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL) /* Force invalid rounds */ aes.rounds = 16; ret = wc_AesGetKeySize(&aes, &keySize); @@ -6922,12 +6923,14 @@ int aesgcm_test(void) 0xba, 0x63, 0x7b, 0x39 }; +#if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_256) const byte a[] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }; +#endif #ifdef WOLFSSL_AES_256 const byte k1[] = @@ -9522,6 +9525,9 @@ static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng) if (ret != 0) #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) if (ret != SIG_TYPE_E) +#elif defined(WOLFSSL_CRYPTOCELL) + /* RNG is handled with the cryptocell */ + if (ret != 0) #else if (ret != MISSING_RNG_E) #endif @@ -9601,7 +9607,6 @@ static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng) if (ret == 0) return -6777; - /* check hash functions */ sigSz = (word32)sizeof(out); ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, @@ -11055,11 +11060,13 @@ static int rsa_keygen_test(WC_RNG* rng) ERROR_OUT(-6967, exit_rsa); } idx = 0; +#if !defined(WOLFSSL_CRYPTOCELL) + /* The private key part of the key gen pairs from cryptocell can't be exported */ ret = wc_RsaPrivateKeyDecode(der, &idx, &genKey, derSz); if (ret != 0) { ERROR_OUT(-6968, exit_rsa); } - +#endif /* WOLFSSL_CRYPTOCELL */ wc_FreeRsaKey(&genKey); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); pem = NULL; @@ -11355,7 +11362,12 @@ int rsa_test(void) #endif if (ret >= 0) { #ifndef WOLFSSL_RSA_VERIFY_INLINE - ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key); + +#if defined(WOLFSSL_CRYPTOCELL) + ret = wc_RsaSSL_Verify(in, inLen, out, outSz, &key); +#else + ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key); +#endif /* WOLFSSL_CRYPTOCELL */ #else byte* dec = NULL; ret = wc_RsaSSL_VerifyInline(out, idx, &dec, &key); @@ -11498,7 +11510,9 @@ int rsa_test(void) #endif /* WOLFSSL_RSA_VERIFY_ONLY */ /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */ -#if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) +#if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_CRYPTOCELL) +/* label is unused in cryptocell so it won't detect decrypt error due to label */ idx = (word32)ret; do { #if defined(WOLFSSL_ASYNC_CRYPT) @@ -11573,8 +11587,9 @@ int rsa_test(void) } TEST_SLEEP(); - /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */ - #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) +/* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */ +#if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_CRYPTOCELL) idx = (word32)ret; do { #if defined(WOLFSSL_ASYNC_CRYPT) @@ -15957,6 +15972,7 @@ static int ecc_test_make_pub(WC_RNG* rng) if (ret != 0) { ERROR_OUT(-8322, done); } + TEST_SLEEP(); #ifdef HAVE_ECC_KEY_EXPORT @@ -15967,7 +15983,10 @@ static int ecc_test_make_pub(WC_RNG* rng) ERROR_OUT(-8323, done); } #endif /* HAVE_ECC_KEY_EXPORT */ - +#if defined(WOLFSSL_CRYPTOCELL) + /* create a new key since building private key from public key is unsupported */ + ret = wc_ecc_make_key(rng, 32, &key); +#endif #ifdef HAVE_ECC_SIGN tmpSz = FOURK_BUF; ret = 0; @@ -16931,7 +16950,7 @@ done: } #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */ -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) static int ecc_mulmod_test(ecc_key* key1) { @@ -17048,7 +17067,7 @@ static int ecc_def_curve_test(WC_RNG *rng) if (ret < 0) goto done; #endif -#ifndef WOLFSSL_ATECC508A +#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_CRYPTOCELL) #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) ret = ecc_mulmod_test(&key); if (ret < 0) @@ -17623,13 +17642,13 @@ int ecc_test(void) goto done; } #endif - +#if !defined(WOLFSSL_ATECC508A) ret = ecc_test_make_pub(&rng); if (ret != 0) { printf("ecc_test_make_pub failed!: %d\n", ret); goto done; } - +#endif #ifdef WOLFSSL_CERT_GEN ret = ecc_test_cert_gen(&rng); if (ret != 0) { @@ -17874,7 +17893,6 @@ int ecc_test_buffers(void) { } #endif - x = sizeof(out); do { #if defined(WOLFSSL_ASYNC_CRYPT) diff --git a/wolfssl/wolfcrypt/aes.h b/wolfssl/wolfcrypt/aes.h index 914bfaaff..71fc7b841 100644 --- a/wolfssl/wolfcrypt/aes.h +++ b/wolfssl/wolfcrypt/aes.h @@ -81,6 +81,9 @@ #include #endif +#if defined(WOLFSSL_CRYPTOCELL) + #include +#endif #ifdef __cplusplus extern "C" { @@ -189,6 +192,9 @@ typedef struct Aes { #if defined(WOLFSSL_DEVCRYPTO) && \ (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)) WC_CRYPTODEV ctx; +#endif +#if defined(WOLFSSL_CRYPTOCELL) + aes_context_t ctx; #endif void* heap; /* memory hint to use */ } Aes; diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 49b6b1acb..908ac4961 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -54,6 +54,9 @@ #include #endif /* WOLFSSL_ATECC508A */ +#if defined(WOLFSSL_CRYPTOCELL) + #include +#endif #ifdef __cplusplus extern "C" { @@ -126,6 +129,11 @@ enum { ECC_MAX_CRYPTO_HW_PUBKEY_SIZE = (ATECC_KEY_SIZE*2), #elif defined(PLUTON_CRYPTO_ECC) ECC_MAX_CRYPTO_HW_SIZE = 32, +#elif defined(WOLFSSL_CRYPTOCELL) + #ifndef CRYPTOCELL_KEY_SIZE + CRYPTOCELL_KEY_SIZE = ECC_MAXSIZE, + #endif + ECC_MAX_CRYPTO_HW_SIZE = CRYPTOCELL_KEY_SIZE, #endif /* point compression type */ @@ -376,6 +384,10 @@ struct ecc_key { byte id[ECC_MAX_ID_LEN]; int idLen; #endif +#if defined(WOLFSSL_CRYPTOCELL) + ecc_context_t ctx; +#endif + #ifdef WOLFSSL_SMALL_STACK_CACHE mp_int* t1; mp_int* t2; @@ -436,7 +448,13 @@ int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point, WOLFSSL_API int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point, byte* out, word32 *outlen); + +#if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL) +#define wc_ecc_shared_secret_ssh wc_ecc_shared_secret +#else #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex /* For backwards compat */ +#endif + #endif /* HAVE_ECC_DHE */ #ifdef HAVE_ECC_SIGN diff --git a/wolfssl/wolfcrypt/include.am b/wolfssl/wolfcrypt/include.am index 1ec24a1d5..98a1487d6 100644 --- a/wolfssl/wolfcrypt/include.am +++ b/wolfssl/wolfcrypt/include.am @@ -76,7 +76,8 @@ noinst_HEADERS+= \ wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h \ wolfssl/wolfcrypt/port/st/stm32.h \ wolfssl/wolfcrypt/port/st/stsafe.h \ - wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h + wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h \ + wolfssl/wolfcrypt/port/arm/cryptoCell.h if BUILD_CRYPTOAUTHLIB nobase_include_HEADERS+= wolfssl/wolfcrypt/port/atmel/atmel.h diff --git a/wolfssl/wolfcrypt/port/arm/cryptoCell.h b/wolfssl/wolfcrypt/port/arm/cryptoCell.h new file mode 100644 index 000000000..a1bd03115 --- /dev/null +++ b/wolfssl/wolfcrypt/port/arm/cryptoCell.h @@ -0,0 +1,117 @@ +/* cryptoCell.h + * + * Copyright (C) 2019 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifndef WOLFSSL_CRYPTOCELL_H +#define WOLFSSL_CRYPTOCELL_H + +#if defined(WOLFSSL_CRYPTOCELL) +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include "sns_silib.h" + +#ifndef NO_SHA256 + #include "crys_hash.h" + #include "crys_hash_error.h" + /* largest possible input data on CC310 DMA. */ + #define CC310_MAX_LENGTH_DMA (0xFFFF) +#endif + +#ifndef NO_AES + #include "ssi_aes.h" + + typedef struct aes_context_t { + SaSiAesUserContext_t user_ctx; /* CC310 AES User context */ + SaSiAesUserKeyData_t key; /* CC310 AES key structure */ + SaSiAesEncryptMode_t mode; /* encrypt or decrypt */ + } aes_context_t; + #define CC310_MAX_LENGTH_DMA_AES (0xFFF0) +#endif /* NO_AES */ + +#if !defined(WC_NO_RNG) + #if defined(WOLFSSL_nRF5x_SDK_15_2) + +/* To fix warning. MIN/MAX are defined in tfm.h and Nordic (re)defines them */ + #undef MIN + #undef MAX +/* includes to use RNG on the nRF52 */ + #include "nrf_drv_rng.h" + #include "nrf_assert.h" + #endif + + /*RNG Global variables*/ + extern CRYS_RND_State_t wc_rndState; + extern CRYS_RND_WorkBuff_t wc_rndWorkBuff; + extern SaSiRndGenerateVectWorkFunc_t wc_rndGenVectFunc; + int cc310_random_generate(byte* output, word32 size); +#endif + +#ifndef NO_RSA + #include "crys_rsa_types.h" + #include "crys_rnd.h" + #include "crys_rsa_schemes.h" + #include "crys_rsa_kg.h" + #include "crys_rsa_build.h" + + typedef struct rsa_context_t { + CRYS_RSAUserPrivKey_t privKey; + CRYS_RSAUserPubKey_t pubKey; + } rsa_context_t; +CRYS_RSA_HASH_OpMode_t cc310_hashModeRSA(enum wc_HashType hash_type); +#endif + +#ifdef HAVE_ECC + #include "crys_ecpki_kg.h" + #include "crys_ecpki_dh.h" + #include "crys_ecpki_build.h" + #include "crys_ecpki_domain.h" + #include "crys_ecpki_ecdsa.h" + + typedef struct ecc_context_t { + CRYS_ECPKI_UserPrivKey_t privKey; + CRYS_ECPKI_UserPublKey_t pubKey; + } ecc_context_t; + +CRYS_ECPKI_DomainID_t cc310_mapCurve(int curve_id); +CRYS_ECPKI_HASH_OpMode_t cc310_hashModeECC(int hash_size); +#endif /* HAVE_ECC */ + +#if !defined(NO_CRYPT_BENCHMARK) && defined(WOLFSSL_nRF5x_SDK_15_2) + /* includes to use Real-time counter (RTC) on nRF52840 */ + #include "nrf_gpio.h" + #include "nrf_drv_rtc.h" + #include "nrf_drv_clock.h" + #include "boards.h" + #include + #include +#endif /* NO_CRYPT_BENCHMARK && WOLFSSL_nRF5x_SDK_15_2*/ + +int cc310_Init(void); +void cc310_Free(void); + +#ifdef __cplusplus +} +#endif +#endif /* WOLFSSL_CRYPTOCELL */ +#endif /* WOLFSSL_CRYPTOCELL_H */ diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index d63c4b00a..4c83f8018 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -80,6 +80,10 @@ #include "xsecure_rsa.h" #endif +#if defined(WOLFSSL_CRYPTOCELL) + #include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -183,6 +187,9 @@ struct RsaKey { int alFd; int rdFd; #endif +#if defined(WOLFSSL_CRYPTOCELL) + rsa_context_t ctx; +#endif }; #ifndef WC_RSAKEY_TYPE_DEFINED diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index e54d3196b..8bf9bb521 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -190,6 +190,9 @@ /* Uncomment next line if using Espressif ESP32-WROOM-32SE */ /* #define WOLFSSL_ESPWROOM32SE */ +/* Uncomment next line if using ARM CRYPTOCELL*/ +/* #define WOLFSSL_CRYPTOCELL */ + #include #ifdef WOLFSSL_USER_SETTINGS diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index c451a585e..5bfcc7cb4 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -85,6 +85,9 @@ #if defined(WOLFSSL_ESP32WROOM32_CRYPT) #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h" #endif +#if defined(WOLFSSL_CRYPTOCELL) + #include +#endif #if defined(_MSC_VER) #define SHA256_NOINLINE __declspec(noinline) @@ -154,6 +157,9 @@ typedef struct wc_Sha256 { !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) WC_ESP32SHA ctx; #endif +#ifdef WOLFSSL_CRYPTOCELL + CRYS_HASHUserContext_t ctx; +#endif #ifdef WOLF_CRYPTO_CB int devId; void* devCtx; /* generic crypto callback context */ From 34c1d5cba0d7b5f19e284793289d0c3a759f2f12 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 4 Apr 2019 12:27:01 -0700 Subject: [PATCH 2/3] Cleanups and fixes for CryptoCell port: * Protection for .c includes. * Improved make pub memory issue reporting. * README.md updates. * Cleanup of user_settings.h. --- IDE/CRYPTOCELL/README.md | 30 +++-- IDE/CRYPTOCELL/main.c | 1 + IDE/CRYPTOCELL/user_settings.h | 151 +++++++----------------- wolfcrypt/src/ecc.c | 3 + wolfcrypt/src/port/arm/cryptoCell.c | 27 +++-- wolfcrypt/src/port/arm/cryptoCellHash.c | 33 +++--- wolfcrypt/src/wc_port.c | 12 +- 7 files changed, 104 insertions(+), 153 deletions(-) diff --git a/IDE/CRYPTOCELL/README.md b/IDE/CRYPTOCELL/README.md index 4f50d174e..5786f1ccf 100644 --- a/IDE/CRYPTOCELL/README.md +++ b/IDE/CRYPTOCELL/README.md @@ -17,7 +17,8 @@ The `IDE/CRYPTOCELL/main.c` example application provides a function to run the s - #undef NO_CRYPT_TEST - #undef NO_CRYPT_BENCHMARK ``` -#### Supported features + +## Supported features - SHA-256 - AES CBC - CryptoCell 310 RNG @@ -28,29 +29,33 @@ The `IDE/CRYPTOCELL/main.c` example application provides a function to run the s - Hardware RNG - RTC for benchmark timing source -#### Setting up Nordic SDK with wolfSSL +## Setup +### Setting up Nordic SDK with wolfSSL 1. Download the wolfSSL source code or a zip file from GitHub and place it under your SDK `InstallFolder/external/` directory. You can also copy or simlink to the source. ``` For example, - $cd nRF5_SDK_15.2.0_9412b96/external - $git submodule add https://github.com/wolfSSL/wolfssl.git + $cd ~/nRF5_SDK_15.2.0_9412b96/external + $git clone --depth=1 https://github.com/wolfSSL/wolfssl.git Or, assuming you have already cloned the wolfSSL source code under ~/wolfssl. - $cd nRF5_SDK_15.2.0_9412b96/external - $sudo ln -s ~/wolfssl . + $cd ~/nRF5_SDK_15.2.0_9412b96/external + $ln -s ~/wolfssl wolfssl +``` +2. Copy the example project from [here](https://github.com/tmael/nRF5_SDK/tree/master/examples/crypto/nrf_cc310/wolfcrypt) into your `nRF5_SDK_15.2.0_9412b96/examples/crypto/nrf_cc310/` directory. +``` + $git clone https://github.com/tmael/nRF5_SDK.git + $cd ~/nRF5_SDK_15.2.0_9412b96/examples/crypto/nrf_cc310 -``` -2. Copy the example project from [here](https://github.com/tmael/nRF5_SDK/tree/master/examples/crypto/nrf_cc310/wolfcrypt) into your `nRF5_SDK/examples/crypto/nrf_cc310` directory. -``` - $cd /nRF5_SDK_15.2.0_9412b96/examples/crypto/nrf_cc310 - $cp -rf ~/wolfcrypt . + $cp -rf ~/nRF5_SDK/examples/crypto/nrf_cc310/wolfcrypt . + OR + $ln -s ~/nRF5_SDK/examples/crypto/nrf_cc310/wolfcrypt wolfcrypt ``` 3. Launch the SEGGER Embedded Studio IDE 4. In the main menu, go to File >Open Solutions to open the example solution. Browse to the location containing the wolfcrypt code `/examples/crypto/nrf_cc310/wolfcrypt/pca10056/blank/ses/wolfcrypt_pca10056.emProject` and choose Open. -#### Building and Running +## Building and Running In the main menu, go to Build > Rebuild your project, then load and run your image on your nRF52840 target platform. Review the test results on the console output. ### `wolfcrypt_test()` @@ -99,6 +104,7 @@ ECDSA 256 sign 50 ops took 1.004 sec, avg 20.080 ms, 49.801 ops/sec ECDSA 256 verify 48 ops took 1.028 sec, avg 21.417 ms, 46.693 ops/sec Benchmark Test Completed ``` + ## References The test results were collected from an nRF52840 reference platform target with the following software and tool chains: - Nordic nRF52840 development board (PCA10056 1.0.0 2018.49 683529999). diff --git a/IDE/CRYPTOCELL/main.c b/IDE/CRYPTOCELL/main.c index 89bfaf449..61863cfcf 100644 --- a/IDE/CRYPTOCELL/main.c +++ b/IDE/CRYPTOCELL/main.c @@ -50,6 +50,7 @@ int main(void) benchmark_test(NULL); printf("\nBenchmark Test Completed\n"); #endif + if ((ret = wolfCrypt_Cleanup()) != 0) { printf("wolfCrypt_Cleanup failed %d\n", ret); return -1; diff --git a/IDE/CRYPTOCELL/user_settings.h b/IDE/CRYPTOCELL/user_settings.h index 0997a9678..3d21d840b 100644 --- a/IDE/CRYPTOCELL/user_settings.h +++ b/IDE/CRYPTOCELL/user_settings.h @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -/* Custom wolfSSL based on GCC ARM example /IDE/GCC-ARM/Header/user_settings.h*/ +/* Example Settings for CryptoCell */ #ifndef WOLFSSL_USER_SETTINGS_H #define WOLFSSL_USER_SETTINGS_H @@ -29,12 +29,12 @@ extern "C" { #endif /* ------------------------------------------------------------------------- */ -/* Platform */ +/* CryptoCell */ /* ------------------------------------------------------------------------- */ #if 1 #define WOLFSSL_CRYPTOCELL #define WOLFSSL_nRF5x_SDK_15_2 /* for benchmark timer */ - //#define WOLFSSL_CRYPTOCELL_AES /* only CBC mode is supported */ + #define WOLFSSL_CRYPTOCELL_AES /* only CBC mode is supported */ #else /* run without CryptoCell, include IDE/GCC-ARM/Source/wolf_main.c for current_time(). */ @@ -44,8 +44,10 @@ extern "C" { #define AES_MAX_KEY_SIZE 128 #endif /* WOLFSSL_CRYPTOCELL*/ -/*END */ +/* ------------------------------------------------------------------------- */ +/* Platform */ +/* ------------------------------------------------------------------------- */ #undef WOLFSSL_GENERAL_ALIGNMENT #define WOLFSSL_GENERAL_ALIGNMENT 4 @@ -88,29 +90,11 @@ extern "C" { #define WOLFSSL_SP_CACHE_RESISTANT //#define WOLFSSL_SP_MATH /* only SP math - eliminates fast math code */ - /* 64 or 32 bit version */ + /* Assembly */ //#define WOLFSSL_SP_ASM /* required if using the ASM versions */ - //#define WOLFSSL_SP_ARM32_ASM - //#define WOLFSSL_SP_ARM64_ASM + //#define WOLFSSL_SP_ARM_CORTEX_M_ASM #endif -/* ------------------------------------------------------------------------- */ -/* FIPS - Requires eval or license from wolfSSL */ -/* ------------------------------------------------------------------------- */ -#undef HAVE_FIPS -#if 0 - #define HAVE_FIPS - - #undef HAVE_FIPS_VERSION - #define HAVE_FIPS_VERSION 2 - - #ifdef SINGLE_THREADED - #undef NO_THREAD_LS - #define NO_THREAD_LS - #endif -#endif - - /* ------------------------------------------------------------------------- */ /* Crypto */ /* ------------------------------------------------------------------------- */ @@ -162,7 +146,7 @@ extern "C" { //#define HAVE_ECC192 //#define HAVE_ECC224 #undef NO_ECC256 - //#define HAVE_ECC384 + #define HAVE_ECC384 //#define HAVE_ECC521 #endif @@ -187,16 +171,12 @@ extern "C" { #define ECC_TIMING_RESISTANT /* Enable cofactor support */ - #ifdef HAVE_FIPS - #undef HAVE_ECC_CDH - #define HAVE_ECC_CDH - #endif + #undef HAVE_ECC_CDH + //#define HAVE_ECC_CDH /* Validate import */ - #ifdef HAVE_FIPS - #undef WOLFSSL_VALIDATE_ECC_IMPORT - #define WOLFSSL_VALIDATE_ECC_IMPORT - #endif + #undef WOLFSSL_VALIDATE_ECC_IMPORT + //#define WOLFSSL_VALIDATE_ECC_IMPORT /* Compressed Key Support */ #undef HAVE_COMP_KEY @@ -206,9 +186,9 @@ extern "C" { #ifdef USE_FAST_MATH #ifdef NO_RSA /* Custom fastmath size if not using RSA */ - /* MAX = ROUND32(ECC BITS 256) + SIZE_OF_MP_DIGIT(32) */ + /* MAX = ROUND32(ECC BITS 384) + SIZE_OF_MP_DIGIT(32) */ #undef FP_MAX_BITS - #define FP_MAX_BITS (256 + 32) + #define FP_MAX_BITS (384 + 32) #else #undef ALT_ECC_SIZE #define ALT_ECC_SIZE @@ -219,6 +199,10 @@ extern "C" { #undef TFM_ECC256 #define TFM_ECC256 #endif + #ifndef HAVE_ECC384 + #undef TFM_ECC384 + #define TFM_ECC384 + #endif #endif #endif @@ -228,16 +212,12 @@ extern "C" { /* Use table for DH instead of -lm (math) lib dependency */ #if 0 #define WOLFSSL_DH_CONST - #define HAVE_FFDHE_2048 - #define HAVE_FFDHE_4096 - //#define HAVE_FFDHE_6144 - //#define HAVE_FFDHE_8192 #endif - #ifdef HAVE_FIPS - #define WOLFSSL_VALIDATE_FFC_IMPORT - #define HAVE_FFDHE_Q - #endif + #define HAVE_FFDHE_2048 + //#define HAVE_FFDHE_4096 + //#define HAVE_FFDHE_6144 + //#define HAVE_FFDHE_8192 #else #define NO_DH #endif @@ -246,30 +226,29 @@ extern "C" { /* AES */ #undef NO_AES #if 1 - #undef HAVE_AES_CBC - #define HAVE_AES_CBC + #undef HAVE_AES_CBC + #define HAVE_AES_CBC + /* If you need other than AES-CBC mode, you must undefine WOLFSSL_CRYPTOCELL_AES */ -#if !defined(WOLFSSL_CRYPTOCELL_AES) - #undef HAVE_AESGCM - #define HAVE_AESGCM + #if !defined(WOLFSSL_CRYPTOCELL_AES) + #undef HAVE_AESGCM + #define HAVE_AESGCM - /* GCM Method: GCM_SMALL, GCM_WORD32 or GCM_TABLE */ - #define GCM_SMALL + /* GCM Method: GCM_SMALL, GCM_WORD32 or GCM_TABLE */ + #define GCM_SMALL + #undef WOLFSSL_AES_DIRECT + //#define WOLFSSL_AES_DIRECT - #undef WOLFSSL_AES_DIRECT - #define WOLFSSL_AES_DIRECT + #undef HAVE_AES_ECB + //#define HAVE_AES_ECB - #undef HAVE_AES_ECB - #define HAVE_AES_ECB - - #undef WOLFSSL_AES_COUNTER - #define WOLFSSL_AES_COUNTER - - #undef HAVE_AESCCM - #define HAVE_AESCCM -#endif + #undef WOLFSSL_AES_COUNTER + //#define WOLFSSL_AES_COUNTER + #undef HAVE_AESCCM + //#define HAVE_AESCCM + #endif #else #define NO_AES #endif @@ -490,43 +469,6 @@ extern "C" { #define WOLFSSL_GENSEED_FORTEST #endif -/* ------------------------------------------------------------------------- */ -/* Custom Standard Lib */ -/* ------------------------------------------------------------------------- */ -/* Allows override of all standard library functions */ -#undef STRING_USER -#if 0 - #define STRING_USER - - #include - - #undef USE_WOLF_STRSEP - #define USE_WOLF_STRSEP - #define XSTRSEP(s1,d) wc_strsep((s1),(d)) - - #undef USE_WOLF_STRTOK - #define USE_WOLF_STRTOK - #define XSTRTOK(s1,d,ptr) wc_strtok((s1),(d),(ptr)) - - #define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n)) - - #define XMEMCPY(d,s,l) memcpy((d),(s),(l)) - #define XMEMSET(b,c,l) memset((b),(c),(l)) - #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n)) - #define XMEMMOVE(d,s,l) memmove((d),(s),(l)) - - #define XSTRLEN(s1) strlen((s1)) - #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n)) - #define XSTRSTR(s1,s2) strstr((s1),(s2)) - - #define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n)) - #define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n)) - #define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n)) - - #define XSNPRINTF snprintf -#endif - - /* ------------------------------------------------------------------------- */ /* Enable Features */ @@ -536,20 +478,13 @@ extern "C" { #define WOLFSSL_TLS13 #endif - #undef WOLFSSL_KEY_GEN -#if 0 +#if 1 #define WOLFSSL_KEY_GEN #endif -#if defined(WOLFSSL_CRYPTOCELL) - #define WOLFSSL_KEY_GEN - #define WOLFSSL_OLD_PRIME_CHECK /* reduce DH test time */ -#endif - -#if defined(HAVE_FIPS) && !defined(WOLFSSL_KEY_GEN) - #define WOLFSSL_OLD_PRIME_CHECK -#endif +/* reduce DH test time */ +#define WOLFSSL_OLD_PRIME_CHECK #undef KEEP_PEER_CERT //#define KEEP_PEER_CERT diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 09586661c..e41d3e98c 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3878,6 +3878,9 @@ static int wc_ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curveIn, if (err == MP_OKAY) { err = wc_ecc_mulmod_ex(&key->k, base, pub, curve->Af, curve->prime, 1, key->heap); + if (err == MP_MEM) { + err = MEMORY_E; + } } wc_ecc_del_point_h(base, key->heap); diff --git a/wolfcrypt/src/port/arm/cryptoCell.c b/wolfcrypt/src/port/arm/cryptoCell.c index 0edb194a3..600b8961f 100644 --- a/wolfcrypt/src/port/arm/cryptoCell.c +++ b/wolfcrypt/src/port/arm/cryptoCell.c @@ -24,8 +24,18 @@ #endif #include + +/* This source is included in wc_port.c */ +/* WOLFSSL_CRYPTOCELL_C is defined by wc_port.c in case compile tries to + include this .c directly */ +#ifdef WOLFSSL_CRYPTOCELL_C + +#ifdef WOLFSSL_CRYPTOCELL + #include #include +#include +#include #ifdef NO_INLINE #include @@ -34,12 +44,7 @@ #include #endif -#ifndef WOLFSSL_CRYPTOCELL_C -#define WOLFSSL_CRYPTOCELL_C - -#if defined(WOLFSSL_CRYPTOCELL) -#include - +/* Global Variables (extern) */ CRYS_RND_State_t wc_rndState; CRYS_RND_WorkBuff_t wc_rndWorkBuff; SaSiRndGenerateVectWorkFunc_t wc_rndGenVectFunc = CRYS_RND_GenerateVector; @@ -67,6 +72,7 @@ static void cc310_disable(void) NVIC_DisableIRQ(CRYPTOCELL_IRQn); } } + int cc310_Init(void) { int ret = 0; @@ -94,6 +100,7 @@ int cc310_Init(void) } return ret; } + void cc310_Free(void) { CRYSError_t crys_result; @@ -172,7 +179,8 @@ CRYS_RSA_HASH_OpMode_t cc310_hashModeRSA(enum wc_HashType hash_type) return CRYS_RSA_After_HASH_NOT_KNOWN_mode; } } -#endif /* NO_RSA */ +#endif /* !NO_RSA */ + #ifdef HAVE_ECC CRYS_ECPKI_HASH_OpMode_t cc310_hashModeECC(int hash_size) { @@ -206,8 +214,7 @@ CRYS_ECPKI_HASH_OpMode_t cc310_hashModeECC(int hash_size) #if !defined(NO_CRYPT_BENCHMARK) && defined(WOLFSSL_nRF5x_SDK_15_2) static int mRtcSec = 0; - -const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0); +static const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0); static void rtc_handler(nrfx_rtc_int_type_t int_type) { @@ -296,5 +303,5 @@ int nrf_random_generate(byte* output, word32 size) return 0; } #endif /* !NO_CRYPT_BENCHMARK && WOLFSSL_nRF5x_SDK_15_2 */ -#endif /* WOLFSSL_CRYPTOCELL_C */ +#endif /* WOLFSSL_CRYPTOCELL_C */ diff --git a/wolfcrypt/src/port/arm/cryptoCellHash.c b/wolfcrypt/src/port/arm/cryptoCellHash.c index a5119810d..8796e1eeb 100644 --- a/wolfcrypt/src/port/arm/cryptoCellHash.c +++ b/wolfcrypt/src/port/arm/cryptoCellHash.c @@ -24,8 +24,17 @@ #endif #include + +/* This source is included in wc_port.c */ +/* WOLFSSL_CRYPTOCELL_HASH_C is defined by wc_port.c in case compile tries + to include this .c directly */ +#ifdef WOLFSSL_CRYPTOCELL_HASH_C +#if !defined(NO_SHA256) && defined(WOLFSSL_CRYPTOCELL) + #include #include +#include +#include #ifdef NO_INLINE #include @@ -34,12 +43,6 @@ #include #endif -#if !defined(NO_SHA) && defined(WOLFSSL_CRYPTOCELL_HASH) -#include - -#if defined(WOLFSSL_CRYPTOCELL) -#include - int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) { CRYSError_t ret = 0; @@ -60,9 +63,9 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) } return ret; - } +} -WOLFSSL_API int wc_InitSha256(Sha256* sha256) +int wc_InitSha256(Sha256* sha256) { return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); } @@ -96,7 +99,7 @@ int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len) } while (ret == CRYS_OK && remaining > 0); return ret; - } +} int wc_Sha256Final(wc_Sha256* sha256, byte* hash) { @@ -125,13 +128,7 @@ void wc_Sha256Free(wc_Sha256* sha256) { if (sha256 == NULL) return; - -#ifdef WOLFSSL_SMALL_STACK_CACHE - if (sha256->W != NULL) { - XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST); - sha256->W = NULL; - } -#endif } -#endif /*WOLFSSL_CRYPTOCELL*/ -#endif /* !NO_SHA256 */ + +#endif /* !NO_SHA256 && WOLFSSL_CRYPTOCELL */ +#endif /* WOLFSSL_CRYPTOCELL_HASH_C */ diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 45b3567ad..d61c25c98 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -1492,7 +1492,7 @@ int wolfSSL_CryptHwMutexUnLock(void) { osMutexRelease (*m); return 0; } - + #elif defined(WOLFSSL_CMSIS_RTOSv2) int wc_InitMutex(wolfSSL_Mutex *m) { @@ -2195,10 +2195,12 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) #include /* initialize and Mutex for TI Crypt Engine */ #include /* md5, sha1, sha224, sha256 */ #endif + #if defined(WOLFSSL_CRYPTOCELL) + #define WOLFSSL_CRYPTOCELL_C #include /* CC310, RTC and RNG */ -#if !defined(NO_SHA256) - #define WOLFSSL_CRYPTOCELL_HASH - #include /* sha256 */ + #if !defined(NO_SHA256) + #define WOLFSSL_CRYPTOCELL_HASH_C + #include /* sha256 */ + #endif #endif -#endif \ No newline at end of file From 5d2f34aa6d1741aacd0f3ed2c0d19bcd92e35b9f Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Tue, 23 Apr 2019 08:59:15 -0700 Subject: [PATCH 3/3] Addressed review comments --- wolfcrypt/src/ecc.c | 12 ++++++++---- wolfcrypt/src/port/arm/cryptoCell.c | 19 +++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index e41d3e98c..d646e3aea 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4050,13 +4050,17 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) if (err == SA_SILIB_RET_OK && key->pubkey.x && key->pubkey.y) { err = mp_read_unsigned_bin(key->pubkey.x, &ucompressed_key[1], key->dp->size); - err = mp_read_unsigned_bin(key->pubkey.y, + if (err == MP_OKAY) { + err = mp_read_unsigned_bin(key->pubkey.y, &ucompressed_key[1+key->dp->size],key->dp->size); + } } raw_size = key->dp->size; - err = CRYS_ECPKI_ExportPrivKey(&key->ctx.privKey, - ucompressed_key, - &raw_size); + if (err == MP_OKAY) { + err = CRYS_ECPKI_ExportPrivKey(&key->ctx.privKey, + ucompressed_key, + &raw_size); + } if (err == SA_SILIB_RET_OK) { err = mp_read_unsigned_bin(&key->k, ucompressed_key, raw_size); diff --git a/wolfcrypt/src/port/arm/cryptoCell.c b/wolfcrypt/src/port/arm/cryptoCell.c index 600b8961f..d6dd17b00 100644 --- a/wolfcrypt/src/port/arm/cryptoCell.c +++ b/wolfcrypt/src/port/arm/cryptoCell.c @@ -56,10 +56,9 @@ static void cc310_enable(void) cc310_enableCount++; /* Enable the CC310 HW/IQ once*/ - if (cc310_enableCount == 1) { - NRF_CRYPTOCELL->ENABLE = 1; - NVIC_EnableIRQ(CRYPTOCELL_IRQn); - } + + NRF_CRYPTOCELL->ENABLE = 1; + NVIC_EnableIRQ(CRYPTOCELL_IRQn); } static void cc310_disable(void) @@ -76,7 +75,7 @@ static void cc310_disable(void) int cc310_Init(void) { int ret = 0; - static bool initialized = false; + static int initialized = 0; if (!initialized) { /* Enable the CC310 HW. */ @@ -96,7 +95,7 @@ int cc310_Init(void) WOLFSSL_MSG("Error CRYS_RndInit"); return ret; } - initialized = true; + initialized = 1; } return ret; } @@ -274,11 +273,11 @@ static int rtc_get_ms(void) double current_time(int reset) { double time; - static bool initialized = false; + static int initialized = 0; if (!initialized) { rtc_config(); - initialized = true; + initialized = 1; } time = mRtcSec; time += (double)rtc_get_ms() / 1000; @@ -289,7 +288,7 @@ double current_time(int reset) int nrf_random_generate(byte* output, word32 size) { uint32_t err_code; - static bool initialized = false; + static int initialized = 0; /* RNG must be initialized once */ if (!initialized) { @@ -297,7 +296,7 @@ int nrf_random_generate(byte* output, word32 size) if (err_code != NRF_SUCCESS) { return -1; } - initialized = true; + initialized = 1; } nrf_drv_rng_block_rand(output, size); return 0;