From 93510339065904e43bb77c8983fe73bacc6a8b46 Mon Sep 17 00:00:00 2001 From: Thomas Cook Date: Thu, 26 Mar 2026 16:55:32 -0400 Subject: [PATCH] Add hw support for SHA1 and SHA256 --- wolfcrypt/src/port/nxp/casper_port.c | 48 +++++++ wolfcrypt/src/port/nxp/hashcrypt_port.c | 149 ++++++++++++++++++++ wolfcrypt/src/sha.c | 3 + wolfcrypt/src/sha256.c | 4 + wolfcrypt/src/wc_port.c | 18 +++ wolfssl/wolfcrypt/port/nxp/casper_port.h | 28 ++++ wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h | 28 ++++ 7 files changed, 278 insertions(+) create mode 100644 wolfcrypt/src/port/nxp/casper_port.c create mode 100644 wolfcrypt/src/port/nxp/hashcrypt_port.c create mode 100644 wolfssl/wolfcrypt/port/nxp/casper_port.h create mode 100644 wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h diff --git a/wolfcrypt/src/port/nxp/casper_port.c b/wolfcrypt/src/port/nxp/casper_port.c new file mode 100644 index 0000000000..9b4a2560ff --- /dev/null +++ b/wolfcrypt/src/port/nxp/casper_port.c @@ -0,0 +1,48 @@ +/* casper_port.c + * + * Copyright (C) 2006-2026 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 3 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 WOLFSSL_NXP_CASPER + +#if defined(WOLFSSL_CRYPT_HW_MUTEX) && WOLFSSL_CRYPT_HW_MUTEX > 0 + #error WOLFSSL_CRYPT_HW_MUTEX=1 not supported yet +#endif + +//#include +#include +#include +#include +#include "fsl_casper.h" + +int wc_casper_init(void) +{ + CASPER_Init(CASPER); + return 0; +} + + +#endif /* WOLFSSL_NXP_CASPER */ diff --git a/wolfcrypt/src/port/nxp/hashcrypt_port.c b/wolfcrypt/src/port/nxp/hashcrypt_port.c new file mode 100644 index 0000000000..1e799dd569 --- /dev/null +++ b/wolfcrypt/src/port/nxp/hashcrypt_port.c @@ -0,0 +1,149 @@ +/* hashcrypt_port.c + * + * Copyright (C) 2006-2026 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 3 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 WOLFSSL_NXP_HASHCRYPT + +#if defined(WOLFSSL_CRYPT_HW_MUTEX) && WOLFSSL_CRYPT_HW_MUTEX > 0 + #error WOLFSSL_CRYPT_HW_MUTEX=1 not supported yet +#endif + +//#include +#include +#include +#include +#include "fsl_hashcrypt.h" + +#if !(defined(NO_SHA256) && defined(NO_SHA)) +static hashcrypt_hash_ctx_t hash_ctx; +#endif + +int wc_hashcrypt_init(void) +{ +#if !(defined(NO_SHA256) && defined(NO_SHA)) + HASHCRYPT_Init(HASHCRYPT); +#endif + return 0; +} + +#ifndef NO_SHA256 +int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) +{ + (void)heap; + (void)devId; + + if (sha256 == NULL) + return BAD_FUNC_ARG; + + // if (wolfSSL_CryptHwMutexLock() != 0) + // return BAD_MUTEX_E; + + XMEMSET(sha256, 0, sizeof(wc_Sha256)); + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha256) != kStatus_Success) + return WC_HW_E; + + return 0; +} + +int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len) +{ + if (sha256 == NULL || (data == NULL && len != 0)) + return BAD_FUNC_ARG; + + if (HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) != kStatus_Success) + return WC_HW_E; + + return 0; +} + +int wc_Sha256Final(wc_Sha256* sha256, byte* hash) +{ + size_t outlen = WC_SHA256_DIGEST_SIZE; + + if (sha256 == NULL || hash == NULL) + return BAD_FUNC_ARG; + + if ( + HASHCRYPT_SHA_Finish(HASHCRYPT, &hash_ctx, hash, &outlen) != kStatus_Success || + outlen != WC_SHA256_DIGEST_SIZE + ) + { + return WC_HW_E; + } + + return 0; +} +#endif /* !NO_SHA256 */ + + +#ifndef NO_SHA +int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) +{ + (void)heap; + (void)devId; + + if (sha == NULL) + return BAD_FUNC_ARG; + + XMEMSET(sha, 0, sizeof(wc_Sha)); + if (HASHCRYPT_SHA_Init(HASHCRYPT, &hash_ctx, kHASHCRYPT_Sha1) != kStatus_Success) + return WC_HW_E; + + return 0; +} + +int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) +{ + if (sha == NULL || (data == NULL && len != 0)) + return BAD_FUNC_ARG; + + if (HASHCRYPT_SHA_Update(HASHCRYPT, &hash_ctx, data, len) != kStatus_Success) + return WC_HW_E; + + return 0; +} + +int wc_ShaFinal(wc_Sha* sha, byte* hash) +{ + size_t outlen = WC_SHA_DIGEST_SIZE; + + if (sha == NULL || hash == NULL) + return BAD_FUNC_ARG; + + if ( + HASHCRYPT_SHA_Finish(HASHCRYPT, &hash_ctx, hash, &outlen) != kStatus_Success || + outlen != WC_SHA_DIGEST_SIZE + ) + { + return WC_HW_E; + } + + return 0; +} +#endif /* !NO_SHA */ + +#endif /* WOLFSSL_NXP_HASHCRYPT */ diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index 8b5fb5e1a8..efdf7fcefe 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -348,6 +348,9 @@ #include /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */ +#elif defined(WOLFSSL_NXP_HASHCRYPT) + /* implemented in wolfcrypt/src/port/nxp/hashcrypt_port.c */ + #elif defined(WOLFSSL_SILABS_SE_ACCEL) /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */ diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 9e44c59975..7acb3ef1a2 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -222,6 +222,7 @@ on the specific device platform. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \ defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \ !defined(PSOC6_HASH_SHA2) && !defined(WOLFSSL_IMXRT_DCP) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \ + !defined(WOLFSSL_NXP_HASHCRYPT) && \ !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_SE050_HASH) && \ ((!defined(WOLFSSL_RENESAS_SCEPROTECT) && \ !defined(WOLFSSL_RENESAS_RSIP)) \ @@ -1051,6 +1052,9 @@ static int InitSha256(wc_Sha256* sha256) #include /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */ +#elif defined(WOLFSSL_NXP_HASHCRYPT) + /* implemented in wolfcrypt/src/port/nxp/hashcrypt_port.c */ + #elif defined(WOLFSSL_SILABS_SE_ACCEL) /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */ diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index b0f8c07aad..70fb1dc680 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -99,6 +99,13 @@ #include #endif +#ifdef WOLFSSL_NXP_CASPER + #include +#endif +#ifdef WOLFSSL_NXP_HASHCRYPT + #include +#endif + #ifdef WOLF_CRYPTO_CB #include #endif @@ -441,6 +448,17 @@ int wolfCrypt_Init(void) } #endif +#ifdef WOLFSSL_NXP_CASPER + if ((ret = wc_casper_init()) != 0) { + return ret; + } +#endif +#ifdef WOLFSSL_NXP_HASHCRYPT + if ((ret = wc_hashcrypt_init()) != 0) { + return ret; + } +#endif + #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) if ((ret = wolfSSL_InitHandle()) != 0) { return ret; diff --git a/wolfssl/wolfcrypt/port/nxp/casper_port.h b/wolfssl/wolfcrypt/port/nxp/casper_port.h new file mode 100644 index 0000000000..e700b1f26f --- /dev/null +++ b/wolfssl/wolfcrypt/port/nxp/casper_port.h @@ -0,0 +1,28 @@ +/* casper_port.h + * + * Copyright (C) 2006-2026 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 3 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 _CASPER_PORT_H_ +#define _CASPER_PORT_H_ + +#include + +int wc_casper_init(void); + +#endif diff --git a/wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h b/wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h new file mode 100644 index 0000000000..8001606705 --- /dev/null +++ b/wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h @@ -0,0 +1,28 @@ +/* hashcrypt_port.h + * + * Copyright (C) 2006-2026 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 3 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 _HASHCRYPT_PORT_H_ +#define _HASHCRYPT_PORT_H_ + +#include + +int wc_hashcrypt_init(void); + +#endif