From 82520572b0aab2fdd6b881b64fd533829b2f4220 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 1 Jun 2020 13:23:50 +0200 Subject: [PATCH] Initial support for psoc6_crypto (sha256 only) --- wolfcrypt/src/port/cypress/psoc6_crypto.c | 107 ++++++++++++++++++ wolfcrypt/src/sha256.c | 13 ++- wolfcrypt/src/wc_port.c | 12 ++ wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h | 59 ++++++++++ wolfssl/wolfcrypt/sha.h | 2 + wolfssl/wolfcrypt/sha256.h | 2 + 6 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 wolfcrypt/src/port/cypress/psoc6_crypto.c create mode 100644 wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h diff --git a/wolfcrypt/src/port/cypress/psoc6_crypto.c b/wolfcrypt/src/port/cypress/psoc6_crypto.c new file mode 100644 index 000000000..d67d800ea --- /dev/null +++ b/wolfcrypt/src/port/cypress/psoc6_crypto.c @@ -0,0 +1,107 @@ +/* psoc6_crypto.c + * + * Copyright (C) 2006-2020 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 +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif + +#if defined(WOLFSSL_PSOC6_CRYPTO) + +#include +#include +#include +#include +#include + +static CRYPTO_Type *crypto_base = PSOC6_CRYPTO_BASE; + +/* Hook for device specific initialization */ +int psoc6_crypto_port_init(void) +{ + Cy_Crypto_Core_Enable(crypto_base); + return 0; +} + +#ifndef NO_SHA256 + +int wc_InitSha256(wc_Sha256* sha) +{ + cy_en_crypto_status_t res; + if (!sha) + return BAD_FUNC_ARG; + Cy_Crypto_Core_MemSet(crypto_base, sha, 0, sizeof(sha)); + res = Cy_Crypto_Core_Sha_Init(crypto_base, &sha->hash_state, CY_CRYPTO_MODE_SHA256, &sha->sha_buffers); + if (res != CY_CRYPTO_SUCCESS) + return (int)res; + return (int) Cy_Crypto_Core_Sha_Start(crypto_base, &sha->hash_state); +} + +int wc_Sha256Update(wc_Sha256* sha, const byte* in, word32 sz) +{ + if ((!sha) || (!in)) + return BAD_FUNC_ARG; + if (sz == 0) + return 0; + + return (int)Cy_Crypto_Core_Sha_Update(crypto_base, &sha->hash_state, in, sz); +} + +int wc_Sha256Final(wc_Sha256* sha, byte* hash) +{ + if ((!sha) || (!hash)) + return BAD_FUNC_ARG; + return (int)Cy_Crypto_Core_Sha_Finish(crypto_base, &sha->hash_state, hash); +} + +int wc_Sha256GetHash(wc_Sha256* sha, byte* hash) +{ + if ((!sha) || (!hash)) + return BAD_FUNC_ARG; + Cy_Crypto_Core_MemCpy(crypto_base, hash, sha->hash_state.hash, WC_SHA256_DIGEST_SIZE); + return 0; +} + +int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) +{ + cy_en_crypto_status_t res; + if ((!dst) || (!src)) + return BAD_FUNC_ARG; + Cy_Crypto_Core_MemCpy(crypto_base, dst, src, sizeof(wc_Sha256)); + return (int)Cy_Crypto_Core_Sha_Init(crypto_base, &dst->hash_state, CY_CRYPTO_MODE_SHA256, &dst->sha_buffers); +} + +void wc_Sha256Free(wc_Sha256* sha) +{ + if (sha) + Cy_Crypto_Core_Sha_Free(crypto_base, &sha->hash_state); +} +#endif /* NO_SHA256 */ + +#endif /* defined(WOLFSSL_PSOC6_CRYPTO) */ + diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index eb0911b01..4d0a00ce0 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -119,6 +119,10 @@ /* #include included by wc_port.c */ #elif defined(WOLFSSL_CRYPTOCELL) /* wc_port.c includes wolfcrypt/src/port/arm/cryptoCellHash.c */ + +#elif defined(WOLFSSL_PSOC6_CRYPTO) + + #else #include @@ -164,7 +168,8 @@ (!defined(WOLFSSL_IMX6_CAAM) || defined(NO_IMX6_CAAM_HASH)) && \ !defined(WOLFSSL_AFALG_HASH) && !defined(WOLFSSL_DEVCRYPTO_HASH) && \ (!defined(WOLFSSL_ESP32WROOM32_CRYPT) || defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)) && \ - (!defined(WOLFSSL_RENESAS_TSIP_CRYPT) || defined(NO_WOLFSSL_RENESAS_TSIP_HASH)) + (!defined(WOLFSSL_RENESAS_TSIP_CRYPT) || defined(NO_WOLFSSL_RENESAS_TSIP_HASH)) && \ + !defined(WOLFSSL_PSOC6_CRYPTO) static int InitSha256(wc_Sha256* sha256) { @@ -663,6 +668,10 @@ static int InitSha256(wc_Sha256* sha256) /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */ +#elif defined(WOLFSSL_PSOC6_CRYPTO) + + /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */ + #else #define NEED_SOFT_SHA256 @@ -1559,6 +1568,8 @@ void wc_Sha256Free(wc_Sha256* sha256) !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */ +#elif defined(WOLFSSL_PSOC6_CRYPTO) + /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */ #else int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash) diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index cc2572953..6295cc09b 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -46,6 +46,10 @@ #include #endif +#ifdef WOLFSSL_PSOC6_CRYPTO + #include +#endif + #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ defined(WOLFSSL_ATECC608A) #include @@ -201,6 +205,14 @@ int wolfCrypt_Init(void) stsafe_interface_init(); #endif + #if defined(WOLFSSL_PSOC6_CRYPTO) + ret = psoc6_crypto_port_init(); + if (ret != 0) { + WOLFSSL_MSG("PSoC6 crypto engine init failed"); + return ret; + } + #endif + #ifdef WOLFSSL_ARMASM WOLFSSL_MSG("Using ARM hardware acceleration"); #endif diff --git a/wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h b/wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h new file mode 100644 index 000000000..2732efc9e --- /dev/null +++ b/wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h @@ -0,0 +1,59 @@ +/* psoc6_crypto.h + * + * Copyright (C) 2006-2020 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 _PSOC6_CRYPTO_PORT_H_ +#define _PSOC6_CRYPTO_PORT_H_ + +#include +#ifdef USE_FAST_MATH + #include +#elif defined WOLFSSL_SP_MATH + #include +#else + #include +#endif +#include "cy_crypto_core_sha.h" +#include "cy_device_headers.h" +#include "psoc6_02_config.h" +#include "cy_crypto_common.h" +#include "cy_crypto_core.h" + +#ifndef NO_SHA256 + +#include "cy_crypto_core_sha.h" +typedef struct wc_Sha256 { + cy_stc_crypto_sha_state_t hash_state; + cy_en_crypto_sha_mode_t sha_mode; + cy_stc_crypto_v2_sha256_buffers_t sha_buffers; +} wc_Sha256; + + +#endif /* !def NO_SHA256 */ + +#include +#include + +#define PSOC6_CRYPTO_BASE ((CRYPTO_Type*) CRYPTO_BASE) + +/* Crypto HW engine initialization */ +int psoc6_crypto_port_init(void); + +#endif /* _PSOC6_CRYPTO_PORT_H_ */ diff --git a/wolfssl/wolfcrypt/sha.h b/wolfssl/wolfcrypt/sha.h index 0b7f65404..960d9a03c 100644 --- a/wolfssl/wolfcrypt/sha.h +++ b/wolfssl/wolfcrypt/sha.h @@ -101,6 +101,8 @@ enum { #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \ !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h" +#elif defined(WOLFSSL_PSOC6_CRYPTO) + #include "wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h" #else /* Sha digest */ diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index 798f309d1..77f5fb759 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -126,6 +126,8 @@ enum { #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \ !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h" +#elif defined(WOLFSSL_PSOC6_CRYPTO) + #include "wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h" #else /* wc_Sha256 digest */