2015-05-21 14:16:09 +09:00
|
|
|
/* sha256.c
|
|
|
|
|
*
|
2024-07-19 13:15:05 -06:00
|
|
|
* Copyright (C) 2006-2024 wolfSSL Inc.
|
2015-05-21 14:16:09 +09:00
|
|
|
*
|
2016-03-17 16:02:13 -06:00
|
|
|
* This file is part of wolfSSL.
|
2015-05-21 14:16:09 +09:00
|
|
|
*
|
|
|
|
|
* 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
|
2016-03-17 16:02:13 -06:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
2015-05-21 14:16:09 +09:00
|
|
|
*/
|
|
|
|
|
|
2022-07-06 09:30:49 -07:00
|
|
|
/* For more info on the algorithm, see https://tools.ietf.org/html/rfc6234
|
|
|
|
|
*
|
|
|
|
|
* For more information on NIST FIPS PUB 180-4, see
|
|
|
|
|
* https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-09 15:13:01 -07:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
|
This library provides the interface to SHA-256 secure hash algorithms.
|
|
|
|
|
SHA-256 performs processing on message blocks to produce a final hash digest
|
|
|
|
|
output. It can be used to hash a message, M, having a length of L bits,
|
|
|
|
|
where 0 <= L < 2^64.
|
|
|
|
|
|
2022-07-06 09:30:49 -07:00
|
|
|
Note that in some cases, hardware acceleration may be enabled, depending
|
|
|
|
|
on the specific device platform.
|
|
|
|
|
|
2020-07-09 15:13:01 -07:00
|
|
|
*/
|
2022-07-06 09:30:49 -07:00
|
|
|
|
2015-05-21 14:16:09 +09:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <wolfssl/wolfcrypt/settings.h>
|
2022-08-24 14:24:17 +10:00
|
|
|
#include <wolfssl/wolfcrypt/types.h>
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-06-03 14:17:47 -07:00
|
|
|
/*
|
|
|
|
|
* SHA256 Build Options:
|
|
|
|
|
* USE_SLOW_SHA256: Reduces code size by not partially unrolling
|
|
|
|
|
(~2KB smaller and ~25% slower) (default OFF)
|
|
|
|
|
* WOLFSSL_SHA256_BY_SPEC: Uses the Ch/Maj based on SHA256 specification
|
|
|
|
|
(default ON)
|
|
|
|
|
* WOLFSSL_SHA256_ALT_CH_MAJ: Alternate Ch/Maj that is easier for compilers to
|
|
|
|
|
optimize and recognize as SHA256 (default OFF)
|
|
|
|
|
* SHA256_MANY_REGISTERS: A SHA256 version that keeps all data in registers
|
|
|
|
|
and partial unrolled (default OFF)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Default SHA256 to use Ch/Maj based on specification */
|
|
|
|
|
#if !defined(WOLFSSL_SHA256_BY_SPEC) && !defined(WOLFSSL_SHA256_ALT_CH_MAJ)
|
|
|
|
|
#define WOLFSSL_SHA256_BY_SPEC
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2024-07-18 11:15:23 +10:00
|
|
|
#if !defined(NO_SHA256) && !(defined(WOLFSSL_ARMASM) || \
|
|
|
|
|
defined(WOLFSSL_ARMASM_NO_NEON)) && !defined(WOLFSSL_RISCV_ASM)
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2021-11-05 18:16:08 -05:00
|
|
|
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
|
2018-01-05 17:00:39 -08:00
|
|
|
/* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
|
|
|
|
|
#define FIPS_NO_WRAPPERS
|
|
|
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
2024-04-04 14:45:20 -04:00
|
|
|
#pragma code_seg(".fipsA$l")
|
|
|
|
|
#pragma const_seg(".fipsB$l")
|
2018-01-05 17:00:39 -08:00
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#include <wolfssl/wolfcrypt/sha256.h>
|
|
|
|
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
2017-07-18 10:14:17 +10:00
|
|
|
#include <wolfssl/wolfcrypt/cpuid.h>
|
2019-02-05 18:28:54 -08:00
|
|
|
#include <wolfssl/wolfcrypt/hash.h>
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-01-17 11:01:14 -08:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
|
|
|
|
#include <wolfssl/wolfcrypt/cryptocb.h>
|
2019-01-02 14:34:26 -08:00
|
|
|
#endif
|
|
|
|
|
|
2022-12-28 13:02:07 -08:00
|
|
|
#ifdef WOLFSSL_IMXRT1170_CAAM
|
|
|
|
|
#include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2022-07-06 09:30:49 -07:00
|
|
|
/* determine if we are using Espressif SHA hardware acceleration */
|
2023-07-07 15:47:00 -07:00
|
|
|
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
|
|
|
|
|
#if defined(WOLFSSL_ESP32_CRYPT) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
|
2022-07-06 09:30:49 -07:00
|
|
|
/* define a single keyword for simplicity & readability
|
|
|
|
|
*
|
|
|
|
|
* by default the HW acceleration is on for ESP32-WROOM32
|
|
|
|
|
* but individual components can be turned off.
|
|
|
|
|
*/
|
2023-07-07 15:47:00 -07:00
|
|
|
#define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
|
2022-07-06 09:30:49 -07:00
|
|
|
#else
|
2023-07-07 15:47:00 -07:00
|
|
|
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
|
2022-07-06 09:30:49 -07:00
|
|
|
#endif
|
|
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
#ifdef WOLFSSL_ESPIDF
|
2024-02-13 03:01:40 -08:00
|
|
|
/* Define the ESP_LOGx(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE value for output messages here.
|
2023-04-19 15:10:22 +02:00
|
|
|
**
|
|
|
|
|
** Beware of possible conflict in test.c (that one now named TEST_TAG)
|
|
|
|
|
*/
|
2024-01-25 15:23:58 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2023-09-19 08:21:13 -07:00
|
|
|
static const char* TAG = "wc_sha256";
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_TI_HASH)
|
2015-06-01 14:32:36 +09:00
|
|
|
/* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
|
2019-03-27 20:44:38 -07:00
|
|
|
#elif defined(WOLFSSL_CRYPTOCELL)
|
|
|
|
|
/* wc_port.c includes wolfcrypt/src/port/arm/cryptoCellHash.c */
|
2020-06-01 13:23:50 +02:00
|
|
|
|
|
|
|
|
#elif defined(WOLFSSL_PSOC6_CRYPTO)
|
|
|
|
|
|
2024-07-08 08:51:24 -06:00
|
|
|
#elif defined(MAX3266X_SHA)
|
|
|
|
|
/* Already brought in by sha256.h */
|
|
|
|
|
/* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */
|
2015-05-28 20:40:53 +09:00
|
|
|
#else
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#include <wolfssl/wolfcrypt/logging.h>
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#ifdef NO_INLINE
|
|
|
|
|
#include <wolfssl/wolfcrypt/misc.h>
|
|
|
|
|
#else
|
|
|
|
|
#define WOLFSSL_MISC_INCLUDED
|
|
|
|
|
#include <wolfcrypt/src/misc.c>
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
|
|
|
|
|
2018-08-17 09:46:16 -06:00
|
|
|
#ifdef WOLFSSL_DEVCRYPTO_HASH
|
|
|
|
|
#include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
|
|
|
|
|
#endif
|
2021-11-23 15:03:53 -08:00
|
|
|
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
|
|
|
|
|
#include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
|
|
|
|
|
#endif
|
2018-08-17 09:46:16 -06:00
|
|
|
|
2024-03-01 19:40:27 -05:00
|
|
|
#if FIPS_VERSION3_GE(6,0,0)
|
|
|
|
|
const unsigned int wolfCrypt_FIPS_sha256_ro_sanity[2] =
|
|
|
|
|
{ 0x1a2b3c4d, 0x00000014 };
|
|
|
|
|
int wolfCrypt_FIPS_SHA256_sanity(void)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
|
2017-11-14 11:36:22 +10:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ < 4) || \
|
|
|
|
|
(__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
|
2018-12-05 13:04:30 -08:00
|
|
|
#undef NO_AVX2_SUPPORT
|
2017-11-14 11:36:22 +10:00
|
|
|
#define NO_AVX2_SUPPORT
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__clang__) && ((__clang_major__ < 3) || \
|
|
|
|
|
(__clang_major__ == 3 && __clang_minor__ <= 5))
|
|
|
|
|
#define NO_AVX2_SUPPORT
|
2017-11-24 08:49:45 +10:00
|
|
|
#elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
|
|
|
|
|
#undef NO_AVX2_SUPPORT
|
2017-11-14 11:36:22 +10:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define HAVE_INTEL_AVX1
|
|
|
|
|
#ifndef NO_AVX2_SUPPORT
|
|
|
|
|
#define HAVE_INTEL_AVX2
|
|
|
|
|
#endif
|
2021-01-12 12:25:52 +10:00
|
|
|
#else
|
|
|
|
|
#undef HAVE_INTEL_AVX1
|
|
|
|
|
#undef HAVE_INTEL_AVX2
|
2022-09-30 16:19:40 +10:00
|
|
|
#endif /* WOLFSSL_X86_64_BUILD && USE_INTEL_SPEEDUP */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
|
|
|
|
#if defined(HAVE_INTEL_AVX2)
|
2016-12-19 12:15:10 -08:00
|
|
|
#define HAVE_INTEL_RORX
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
|
|
|
|
|
2024-04-02 19:32:41 -07:00
|
|
|
#if defined(LITTLE_ENDIAN_ORDER)
|
2024-02-29 08:44:33 +10:00
|
|
|
#if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP8684) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP32C3) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP32C6) \
|
|
|
|
|
) && \
|
|
|
|
|
defined(WOLFSSL_ESP32_CRYPT) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
/* For Espressif RISC-V Targets, we *may* need to reverse bytes
|
|
|
|
|
* depending on if HW is active or not. */
|
|
|
|
|
#define SHA256_REV_BYTES(ctx) \
|
|
|
|
|
(esp_sha_need_byte_reversal(ctx))
|
2024-04-02 19:32:41 -07:00
|
|
|
#elif defined(FREESCALE_MMCAU_SHA)
|
|
|
|
|
#define SHA256_REV_BYTES(ctx) 1 /* reverse needed on final */
|
2024-02-29 08:44:33 +10:00
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef SHA256_REV_BYTES
|
2024-04-02 19:32:41 -07:00
|
|
|
#if defined(LITTLE_ENDIAN_ORDER)
|
2024-02-29 08:44:33 +10:00
|
|
|
#define SHA256_REV_BYTES(ctx) 1
|
|
|
|
|
#else
|
|
|
|
|
#define SHA256_REV_BYTES(ctx) 0
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2024-04-02 19:32:41 -07:00
|
|
|
#if defined(LITTLE_ENDIAN_ORDER) && \
|
2024-02-29 08:44:33 +10:00
|
|
|
defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
|
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2024-04-27 01:02:31 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
#define SHA256_UPDATE_REV_BYTES(ctx) (sha256->sha_method == SHA256_C)
|
|
|
|
|
#else
|
|
|
|
|
#define SHA256_UPDATE_REV_BYTES(ctx) \
|
2024-04-29 14:02:44 -05:00
|
|
|
(!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
|
2024-04-27 01:02:31 -05:00
|
|
|
#endif
|
2024-04-02 19:32:41 -07:00
|
|
|
#elif defined(FREESCALE_MMCAU_SHA)
|
|
|
|
|
#define SHA256_UPDATE_REV_BYTES(ctx) 0 /* reverse not needed on update */
|
2024-02-29 08:44:33 +10:00
|
|
|
#else
|
|
|
|
|
#define SHA256_UPDATE_REV_BYTES(ctx) SHA256_REV_BYTES(ctx)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#if !defined(WOLFSSL_PIC32MZ_HASH) && !defined(STM32_HASH_SHA2) && \
|
2020-08-07 14:40:44 -06:00
|
|
|
(!defined(WOLFSSL_IMX6_CAAM) || defined(NO_IMX6_CAAM_HASH) || \
|
|
|
|
|
defined(WOLFSSL_QNX_CAAM)) && \
|
2018-11-21 18:41:29 +09:00
|
|
|
!defined(WOLFSSL_AFALG_HASH) && !defined(WOLFSSL_DEVCRYPTO_HASH) && \
|
2023-07-07 15:47:00 -07:00
|
|
|
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_HASH)) && \
|
2023-06-17 11:57:11 +09:00
|
|
|
((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
|
|
|
|
|
!defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \
|
|
|
|
|
defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \
|
2021-08-19 11:25:59 +10:00
|
|
|
!defined(WOLFSSL_PSOC6_CRYPTO) && !defined(WOLFSSL_IMXRT_DCP) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
|
2021-10-30 20:21:21 +09:00
|
|
|
!defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_SE050_HASH) && \
|
2023-08-09 09:08:44 +09:00
|
|
|
((!defined(WOLFSSL_RENESAS_SCEPROTECT) && \
|
|
|
|
|
!defined(WOLFSSL_RENESAS_RSIP)) \
|
2023-08-12 17:28:24 +09:00
|
|
|
|| defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) && \
|
2022-09-15 16:18:44 -05:00
|
|
|
(!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \
|
|
|
|
|
!defined(WOLFSSL_RENESAS_RX64_HASH)
|
2020-11-06 12:20:03 -08:00
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
|
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2024-04-27 01:02:31 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
2024-04-29 14:02:44 -05:00
|
|
|
#define SHA256_SETTRANSFORM_ARGS int *sha_method
|
2024-04-27 01:02:31 -05:00
|
|
|
#else
|
2024-04-29 14:02:44 -05:00
|
|
|
#define SHA256_SETTRANSFORM_ARGS void
|
2024-04-27 01:02:31 -05:00
|
|
|
#endif
|
2024-04-29 14:02:44 -05:00
|
|
|
static void Sha256_SetTransform(SHA256_SETTRANSFORM_ARGS);
|
2024-04-27 12:33:20 -05:00
|
|
|
#endif
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
static int InitSha256(wc_Sha256* sha256)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
2017-08-01 12:42:09 -07:00
|
|
|
XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->digest[0] = 0x6A09E667L;
|
|
|
|
|
sha256->digest[1] = 0xBB67AE85L;
|
|
|
|
|
sha256->digest[2] = 0x3C6EF372L;
|
|
|
|
|
sha256->digest[3] = 0xA54FF53AL;
|
|
|
|
|
sha256->digest[4] = 0x510E527FL;
|
|
|
|
|
sha256->digest[5] = 0x9B05688CL;
|
|
|
|
|
sha256->digest[6] = 0x1F83D9ABL;
|
|
|
|
|
sha256->digest[7] = 0x5BE0CD19L;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->buffLen = 0;
|
|
|
|
|
sha256->loLen = 0;
|
|
|
|
|
sha256->hiLen = 0;
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-08-21 06:36:37 -07:00
|
|
|
sha256->flags = 0;
|
|
|
|
|
#endif
|
2021-12-08 23:59:19 +00:00
|
|
|
#ifdef WOLFSSL_HASH_KEEP
|
|
|
|
|
sha256->msg = NULL;
|
|
|
|
|
sha256->len = 0;
|
|
|
|
|
sha256->used = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
|
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2024-04-27 01:02:31 -05:00
|
|
|
/* choose best Transform function under this runtime environment */
|
|
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
sha256->sha_method = 0;
|
|
|
|
|
Sha256_SetTransform(&sha256->sha_method);
|
|
|
|
|
#else
|
|
|
|
|
Sha256_SetTransform();
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-12-08 23:59:19 +00:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
|
|
|
|
sha256->devId = wc_CryptoCb_DefaultDevID();
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2022-09-21 03:21:33 -04:00
|
|
|
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
|
|
|
|
|
XMEMSET(&sha256->maxq_ctx, 0, sizeof(sha256->maxq_ctx));
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-06 13:05:39 -04:00
|
|
|
#ifdef HAVE_ARIA
|
|
|
|
|
sha256->hSession = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
return 0;
|
2015-05-21 14:16:09 +09:00
|
|
|
}
|
2017-08-01 12:42:09 -07:00
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
/* Hardware Acceleration */
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
|
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
/* in case intel instructions aren't available, plus we need the K[] global */
|
|
|
|
|
#define NEED_SOFT_SHA256
|
|
|
|
|
|
|
|
|
|
/*****
|
|
|
|
|
Intel AVX1/AVX2 Macro Control Structure
|
|
|
|
|
|
|
|
|
|
#define HAVE_INTEL_AVX1
|
|
|
|
|
#define HAVE_INTEL_AVX2
|
|
|
|
|
|
|
|
|
|
#define HAVE_INTEL_RORX
|
|
|
|
|
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int InitSha256(wc_Sha256* sha256) {
|
2016-12-19 12:15:10 -08:00
|
|
|
Save/Recover XMM, YMM
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
|
2017-10-31 09:05:52 +10:00
|
|
|
Transform_Sha256(); Function prototype
|
2015-05-21 14:16:09 +09:00
|
|
|
#else
|
2017-10-31 09:05:52 +10:00
|
|
|
Transform_Sha256() { }
|
2016-12-19 12:15:10 -08:00
|
|
|
int Sha256Final() {
|
|
|
|
|
Save/Recover XMM, YMM
|
|
|
|
|
...
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
|
|
|
|
|
#if defined(HAVE_INTEL_RORX
|
2019-12-24 12:29:33 -06:00
|
|
|
#define RND with rorx instruction
|
2016-12-19 12:15:10 -08:00
|
|
|
#else
|
|
|
|
|
#define RND
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(HAVE_INTEL_AVX1)
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#define XMM Instructions/inline asm
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2017-10-31 09:05:52 +10:00
|
|
|
int Transform_Sha256() {
|
2016-12-19 12:15:10 -08:00
|
|
|
Stitched Message Sched/Round
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#elif defined(HAVE_INTEL_AVX2)
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#define YMM Instructions/inline asm
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2017-10-31 09:05:52 +10:00
|
|
|
int Transform_Sha256() {
|
2019-12-24 12:29:33 -06:00
|
|
|
More granular Stitched Message Sched/Round
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
2017-07-18 10:14:17 +10:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Each platform needs to query info type 1 from cpuid to see if aesni is
|
|
|
|
|
* supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
|
|
|
|
|
*/
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
/* #if defined(HAVE_INTEL_AVX1/2) at the tail of sha256 */
|
2019-09-25 12:47:12 -07:00
|
|
|
static int Transform_Sha256(wc_Sha256* sha256, const byte* data);
|
2019-01-25 12:03:08 +10:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-01-30 16:47:38 +10:00
|
|
|
extern int Transform_Sha256_SSE2_Sha(wc_Sha256 *sha256,
|
|
|
|
|
const byte* data);
|
|
|
|
|
extern int Transform_Sha256_SSE2_Sha_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(HAVE_INTEL_AVX1)
|
2024-01-30 16:47:38 +10:00
|
|
|
extern int Transform_Sha256_AVX1_Sha(wc_Sha256 *sha256,
|
|
|
|
|
const byte* data);
|
|
|
|
|
extern int Transform_Sha256_AVX1_Sha_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2019-09-25 12:47:12 -07:00
|
|
|
extern int Transform_Sha256_AVX1(wc_Sha256 *sha256, const byte* data);
|
2019-01-25 12:03:08 +10:00
|
|
|
extern int Transform_Sha256_AVX1_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
|
|
|
|
#if defined(HAVE_INTEL_AVX2)
|
2019-09-25 12:47:12 -07:00
|
|
|
extern int Transform_Sha256_AVX2(wc_Sha256 *sha256, const byte* data);
|
2019-01-25 12:03:08 +10:00
|
|
|
extern int Transform_Sha256_AVX2_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2017-11-15 16:56:19 +10:00
|
|
|
#ifdef HAVE_INTEL_RORX
|
2019-09-25 12:47:12 -07:00
|
|
|
extern int Transform_Sha256_AVX1_RORX(wc_Sha256 *sha256, const byte* data);
|
2019-01-25 12:03:08 +10:00
|
|
|
extern int Transform_Sha256_AVX1_RORX_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2019-09-25 12:47:12 -07:00
|
|
|
extern int Transform_Sha256_AVX2_RORX(wc_Sha256 *sha256, const byte* data);
|
2019-01-25 12:03:08 +10:00
|
|
|
extern int Transform_Sha256_AVX2_RORX_Len(wc_Sha256* sha256,
|
|
|
|
|
const byte* data, word32 len);
|
2019-09-25 12:47:12 -07:00
|
|
|
#endif /* HAVE_INTEL_RORX */
|
|
|
|
|
#endif /* HAVE_INTEL_AVX2 */
|
2019-01-25 12:03:08 +10:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
} /* extern "C" */
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
static word32 intel_flags = 0;
|
|
|
|
|
|
2024-04-27 01:02:31 -05:00
|
|
|
#if defined(WC_C_DYNAMIC_FALLBACK) && !defined(WC_NO_INTERNAL_FUNCTION_POINTERS)
|
|
|
|
|
#define WC_NO_INTERNAL_FUNCTION_POINTERS
|
|
|
|
|
#endif
|
2024-04-23 01:31:43 -05:00
|
|
|
|
|
|
|
|
#ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
|
|
|
|
|
|
2024-05-22 15:39:46 -05:00
|
|
|
enum sha_methods { SHA256_UNSET = 0, SHA256_AVX1_SHA, SHA256_AVX2,
|
|
|
|
|
SHA256_AVX1_RORX, SHA256_AVX1_NOSHA, SHA256_AVX2_RORX,
|
|
|
|
|
SHA256_SSE2, SHA256_C };
|
2024-04-23 01:31:43 -05:00
|
|
|
|
2024-04-27 01:02:31 -05:00
|
|
|
#ifndef WC_C_DYNAMIC_FALLBACK
|
2024-08-14 15:23:48 -05:00
|
|
|
/* note that all write access to this static variable must be idempotent,
|
|
|
|
|
* as arranged by Sha256_SetTransform(), else it will be susceptible to
|
|
|
|
|
* data races.
|
|
|
|
|
*/
|
2024-04-27 01:02:31 -05:00
|
|
|
static enum sha_methods sha_method = SHA256_UNSET;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
static void Sha256_SetTransform(SHA256_SETTRANSFORM_ARGS)
|
2024-04-23 01:31:43 -05:00
|
|
|
{
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
#define SHA_METHOD (*sha_method)
|
|
|
|
|
#else
|
|
|
|
|
#define SHA_METHOD sha_method
|
|
|
|
|
#endif
|
2024-04-27 01:02:31 -05:00
|
|
|
if (SHA_METHOD != SHA256_UNSET)
|
|
|
|
|
return;
|
2024-04-23 01:31:43 -05:00
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
2024-04-27 01:02:31 -05:00
|
|
|
if (! CAN_SAVE_VECTOR_REGISTERS()) {
|
|
|
|
|
SHA_METHOD = SHA256_C;
|
2024-04-23 01:31:43 -05:00
|
|
|
return;
|
2024-04-27 01:02:31 -05:00
|
|
|
}
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2024-04-23 01:31:43 -05:00
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
if (intel_flags == 0)
|
|
|
|
|
intel_flags = cpuid_get_flags();
|
2024-04-23 01:31:43 -05:00
|
|
|
|
|
|
|
|
if (IS_INTEL_SHA(intel_flags)) {
|
|
|
|
|
#ifdef HAVE_INTEL_AVX1
|
|
|
|
|
if (IS_INTEL_AVX1(intel_flags)) {
|
2024-05-22 15:39:46 -05:00
|
|
|
SHA_METHOD = SHA256_AVX1_SHA;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2024-04-27 01:02:31 -05:00
|
|
|
SHA_METHOD = SHA256_SSE2;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#ifdef HAVE_INTEL_AVX2
|
|
|
|
|
if (IS_INTEL_AVX2(intel_flags)) {
|
|
|
|
|
#ifdef HAVE_INTEL_RORX
|
|
|
|
|
if (IS_INTEL_BMI2(intel_flags)) {
|
2024-04-27 01:02:31 -05:00
|
|
|
SHA_METHOD = SHA256_AVX2_RORX;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2024-04-27 01:02:31 -05:00
|
|
|
SHA_METHOD = SHA256_AVX2;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_INTEL_AVX1
|
|
|
|
|
if (IS_INTEL_AVX1(intel_flags)) {
|
|
|
|
|
#ifdef HAVE_INTEL_RORX
|
|
|
|
|
if (IS_INTEL_BMI2(intel_flags)) {
|
2024-04-27 01:02:31 -05:00
|
|
|
SHA_METHOD = SHA256_AVX1_RORX;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2024-05-22 15:39:46 -05:00
|
|
|
SHA_METHOD = SHA256_AVX1_NOSHA;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2024-04-27 01:02:31 -05:00
|
|
|
SHA_METHOD = SHA256_C;
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
2024-04-29 14:02:44 -05:00
|
|
|
#undef SHA_METHOD
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
#define SHA_METHOD (S->sha_method)
|
|
|
|
|
#else
|
|
|
|
|
#define SHA_METHOD sha_method
|
|
|
|
|
#endif
|
2024-04-23 01:31:43 -05:00
|
|
|
int ret;
|
2024-04-27 01:02:31 -05:00
|
|
|
|
|
|
|
|
if (SHA_METHOD == SHA256_C)
|
2024-04-23 01:31:43 -05:00
|
|
|
return Transform_Sha256(S, D);
|
|
|
|
|
SAVE_VECTOR_REGISTERS(return _svr_ret;);
|
2024-04-27 01:02:31 -05:00
|
|
|
switch (SHA_METHOD) {
|
2024-04-23 01:31:43 -05:00
|
|
|
case SHA256_AVX2:
|
|
|
|
|
ret = Transform_Sha256_AVX2(S, D);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_AVX2_RORX:
|
|
|
|
|
ret = Transform_Sha256_AVX2_RORX(S, D);
|
|
|
|
|
break;
|
2024-05-22 15:39:46 -05:00
|
|
|
case SHA256_AVX1_SHA:
|
2024-04-23 01:31:43 -05:00
|
|
|
ret = Transform_Sha256_AVX1_Sha(S, D);
|
|
|
|
|
break;
|
2024-05-22 15:39:46 -05:00
|
|
|
case SHA256_AVX1_NOSHA:
|
|
|
|
|
ret = Transform_Sha256_AVX1(S, D);
|
|
|
|
|
break;
|
2024-04-23 01:31:43 -05:00
|
|
|
case SHA256_AVX1_RORX:
|
|
|
|
|
ret = Transform_Sha256_AVX1_RORX(S, D);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_SSE2:
|
|
|
|
|
ret = Transform_Sha256_SSE2_Sha(S, D);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_C:
|
|
|
|
|
case SHA256_UNSET:
|
|
|
|
|
default:
|
|
|
|
|
ret = Transform_Sha256(S, D);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
RESTORE_VECTOR_REGISTERS();
|
|
|
|
|
return ret;
|
2024-04-29 14:02:44 -05:00
|
|
|
#undef SHA_METHOD
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
#define SHA_METHOD (S->sha_method)
|
|
|
|
|
#else
|
|
|
|
|
#define SHA_METHOD sha_method
|
|
|
|
|
#endif
|
2024-04-23 01:31:43 -05:00
|
|
|
int ret;
|
|
|
|
|
SAVE_VECTOR_REGISTERS(return _svr_ret;);
|
2024-04-27 01:02:31 -05:00
|
|
|
switch (SHA_METHOD) {
|
2024-04-23 01:31:43 -05:00
|
|
|
case SHA256_AVX2:
|
|
|
|
|
ret = Transform_Sha256_AVX2_Len(S, D, L);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_AVX2_RORX:
|
|
|
|
|
ret = Transform_Sha256_AVX2_RORX_Len(S, D, L);
|
|
|
|
|
break;
|
2024-05-22 15:39:46 -05:00
|
|
|
case SHA256_AVX1_SHA:
|
2024-04-23 01:31:43 -05:00
|
|
|
ret = Transform_Sha256_AVX1_Sha_Len(S, D, L);
|
|
|
|
|
break;
|
2024-05-22 15:39:46 -05:00
|
|
|
case SHA256_AVX1_NOSHA:
|
|
|
|
|
ret = Transform_Sha256_AVX1_Len(S, D, L);
|
|
|
|
|
break;
|
2024-04-23 01:31:43 -05:00
|
|
|
case SHA256_AVX1_RORX:
|
|
|
|
|
ret = Transform_Sha256_AVX1_RORX_Len(S, D, L);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_SSE2:
|
|
|
|
|
ret = Transform_Sha256_SSE2_Sha_Len(S, D, L);
|
|
|
|
|
break;
|
|
|
|
|
case SHA256_C:
|
|
|
|
|
case SHA256_UNSET:
|
|
|
|
|
default:
|
|
|
|
|
ret = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
RESTORE_VECTOR_REGISTERS();
|
|
|
|
|
return ret;
|
2024-04-29 14:02:44 -05:00
|
|
|
#undef SHA_METHOD
|
2024-04-23 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
#else /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
static int (*Transform_Sha256_p)(wc_Sha256* sha256, const byte* data);
|
2017-10-31 09:05:52 +10:00
|
|
|
/* = _Transform_Sha256 */
|
2019-01-25 12:03:08 +10:00
|
|
|
static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
|
|
|
|
|
word32 len);
|
2017-10-31 09:05:52 +10:00
|
|
|
/* = NULL */
|
2017-07-18 10:14:17 +10:00
|
|
|
static int transform_check = 0;
|
2024-04-27 01:02:31 -05:00
|
|
|
static int Transform_Sha256_is_vectorized = 0;
|
2020-09-08 23:11:03 -05:00
|
|
|
|
2020-11-11 13:44:26 -06:00
|
|
|
static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
|
|
|
|
|
int ret;
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
2024-04-23 01:31:43 -05:00
|
|
|
if (Transform_Sha256_is_vectorized)
|
|
|
|
|
SAVE_VECTOR_REGISTERS(return _svr_ret;);
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2020-11-11 13:44:26 -06:00
|
|
|
ret = (*Transform_Sha256_p)(S, D);
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
2024-04-23 01:31:43 -05:00
|
|
|
if (Transform_Sha256_is_vectorized)
|
|
|
|
|
RESTORE_VECTOR_REGISTERS();
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2020-11-11 13:44:26 -06:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
|
|
|
|
|
int ret;
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
2024-04-23 01:31:43 -05:00
|
|
|
if (Transform_Sha256_is_vectorized)
|
|
|
|
|
SAVE_VECTOR_REGISTERS(return _svr_ret;);
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2020-11-11 13:44:26 -06:00
|
|
|
ret = (*Transform_Sha256_Len_p)(S, D, L);
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
2024-04-23 01:31:43 -05:00
|
|
|
if (Transform_Sha256_is_vectorized)
|
|
|
|
|
RESTORE_VECTOR_REGISTERS();
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2020-11-11 13:44:26 -06:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2017-07-20 12:24:38 +10:00
|
|
|
static void Sha256_SetTransform(void)
|
2017-07-18 10:14:17 +10:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (transform_check)
|
|
|
|
|
return;
|
2017-07-20 12:24:38 +10:00
|
|
|
|
2017-07-18 10:14:17 +10:00
|
|
|
intel_flags = cpuid_get_flags();
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2024-01-30 16:47:38 +10:00
|
|
|
if (IS_INTEL_SHA(intel_flags)) {
|
|
|
|
|
#ifdef HAVE_INTEL_AVX1
|
|
|
|
|
if (IS_INTEL_AVX1(intel_flags)) {
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256_AVX1_Sha;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_AVX1_Sha_Len;
|
|
|
|
|
Transform_Sha256_is_vectorized = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256_SSE2_Sha;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_SSE2_Sha_Len;
|
|
|
|
|
Transform_Sha256_is_vectorized = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-11-15 16:56:19 +10:00
|
|
|
#ifdef HAVE_INTEL_AVX2
|
2023-12-25 00:23:37 -06:00
|
|
|
if (IS_INTEL_AVX2(intel_flags)) {
|
2017-10-31 09:05:52 +10:00
|
|
|
#ifdef HAVE_INTEL_RORX
|
|
|
|
|
if (IS_INTEL_BMI2(intel_flags)) {
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256_AVX2_RORX;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_AVX2_RORX_Len;
|
2020-09-08 23:11:03 -05:00
|
|
|
Transform_Sha256_is_vectorized = 1;
|
2017-10-31 09:05:52 +10:00
|
|
|
}
|
2017-07-20 12:24:38 +10:00
|
|
|
else
|
2017-10-31 09:05:52 +10:00
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256_AVX2;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_AVX2_Len;
|
2020-09-08 23:11:03 -05:00
|
|
|
Transform_Sha256_is_vectorized = 1;
|
2017-10-31 09:05:52 +10:00
|
|
|
}
|
2023-12-25 00:23:37 -06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_INTEL_AVX1
|
|
|
|
|
if (IS_INTEL_AVX1(intel_flags)) {
|
2017-10-31 09:05:52 +10:00
|
|
|
#ifdef HAVE_INTEL_RORX
|
2023-12-25 00:23:37 -06:00
|
|
|
if (IS_INTEL_BMI2(intel_flags)) {
|
2017-10-31 09:05:52 +10:00
|
|
|
Transform_Sha256_p = Transform_Sha256_AVX1_RORX;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_AVX1_RORX_Len;
|
2020-09-08 23:11:03 -05:00
|
|
|
Transform_Sha256_is_vectorized = 1;
|
2017-10-31 09:05:52 +10:00
|
|
|
}
|
2023-12-25 00:23:37 -06:00
|
|
|
else
|
2017-10-31 09:05:52 +10:00
|
|
|
#endif
|
2023-12-25 00:23:37 -06:00
|
|
|
{
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256_AVX1;
|
|
|
|
|
Transform_Sha256_Len_p = Transform_Sha256_AVX1_Len;
|
|
|
|
|
Transform_Sha256_is_vectorized = 1;
|
|
|
|
|
}
|
2017-07-20 12:24:38 +10:00
|
|
|
}
|
|
|
|
|
else
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
2017-10-31 09:05:52 +10:00
|
|
|
{
|
|
|
|
|
Transform_Sha256_p = Transform_Sha256;
|
|
|
|
|
Transform_Sha256_Len_p = NULL;
|
2020-09-08 23:11:03 -05:00
|
|
|
Transform_Sha256_is_vectorized = 0;
|
2017-10-31 09:05:52 +10:00
|
|
|
}
|
2017-07-20 12:24:38 +10:00
|
|
|
|
|
|
|
|
transform_check = 1;
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2024-04-23 01:31:43 -05:00
|
|
|
#endif /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
|
|
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#if !defined(WOLFSSL_KCAPI_HASH)
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (sha256 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->heap = heap;
|
2019-01-17 11:01:14 -08:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
2019-01-02 14:34:26 -08:00
|
|
|
sha256->devId = devId;
|
2021-03-12 11:49:25 -08:00
|
|
|
sha256->devCtx = NULL;
|
2019-01-02 14:34:26 -08:00
|
|
|
#endif
|
2020-08-26 09:44:32 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
sha256->W = NULL;
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
ret = InitSha256(sha256);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
|
|
|
|
ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
|
|
|
|
|
WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
|
|
|
|
|
#else
|
|
|
|
|
(void)devId;
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2021-08-19 11:25:59 +10:00
|
|
|
#endif /* !WOLFSSL_KCAPI_HASH */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#elif defined(FREESCALE_LTC_SHA)
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
(void)heap;
|
|
|
|
|
(void)devId;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
LTC_HASH_Init(LTC_BASE, &sha256->ctx, kLTC_Sha256, NULL, 0);
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#elif defined(FREESCALE_MMCAU_SHA)
|
2017-06-28 13:42:46 -06:00
|
|
|
|
|
|
|
|
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
|
|
|
|
#include "cau_api.h"
|
|
|
|
|
#else
|
|
|
|
|
#include "fsl_mmcau.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
#define XTRANSFORM(S, D) Transform_Sha256((S),(D))
|
2019-01-25 12:03:08 +10:00
|
|
|
#define XTRANSFORM_LEN(S, D, L) Transform_Sha256_Len((S),(D),(L))
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-10-30 13:34:57 -07:00
|
|
|
#ifndef WC_HASH_DATA_ALIGNMENT
|
|
|
|
|
/* these hardware API's require 4 byte (word32) alignment */
|
|
|
|
|
#define WC_HASH_DATA_ALIGNMENT 4
|
2019-10-29 13:41:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
2015-05-21 14:16:09 +09:00
|
|
|
{
|
2016-12-19 12:15:10 -08:00
|
|
|
int ret = 0;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
(void)heap;
|
|
|
|
|
(void)devId;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2015-10-13 12:36:24 -07:00
|
|
|
ret = wolfSSL_CryptHwMutexLock();
|
2016-12-19 12:15:10 -08:00
|
|
|
if (ret != 0) {
|
2015-10-13 12:36:24 -07:00
|
|
|
return ret;
|
|
|
|
|
}
|
2020-01-06 14:09:41 -08:00
|
|
|
|
2017-06-28 13:42:46 -06:00
|
|
|
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
|
|
|
|
cau_sha256_initialize_output(sha256->digest);
|
|
|
|
|
#else
|
2021-10-12 19:34:12 -05:00
|
|
|
MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
|
2017-06-28 13:42:46 -06:00
|
|
|
#endif
|
2015-10-13 12:36:24 -07:00
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->buffLen = 0;
|
|
|
|
|
sha256->loLen = 0;
|
|
|
|
|
sha256->hiLen = 0;
|
2020-01-06 14:09:41 -08:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
sha256->W = NULL;
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
2017-06-28 13:42:46 -06:00
|
|
|
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
2019-09-25 12:47:12 -07:00
|
|
|
cau_sha256_hash_n((byte*)data, 1, sha256->digest);
|
2017-06-28 13:42:46 -06:00
|
|
|
#else
|
2021-10-12 19:34:12 -05:00
|
|
|
MMCAU_SHA256_HashN((byte*)data, 1, (uint32_t*)sha256->digest);
|
2019-09-25 12:47:12 -07:00
|
|
|
#endif
|
|
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
|
|
|
|
|
word32 len)
|
|
|
|
|
{
|
|
|
|
|
int ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
2019-10-29 13:41:28 -07:00
|
|
|
#if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
|
2021-05-03 11:23:55 -07:00
|
|
|
if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
|
2019-10-29 13:41:28 -07:00
|
|
|
/* data pointer is NOT aligned,
|
|
|
|
|
* so copy and perform one block at a time */
|
|
|
|
|
byte* local = (byte*)sha256->buffer;
|
|
|
|
|
while (len >= WC_SHA256_BLOCK_SIZE) {
|
|
|
|
|
XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
|
|
|
|
cau_sha256_hash_n(local, 1, sha256->digest);
|
|
|
|
|
#else
|
2021-10-12 19:34:12 -05:00
|
|
|
MMCAU_SHA256_HashN(local, 1, (uint32_t*)sha256->digest);
|
2019-10-29 13:41:28 -07:00
|
|
|
#endif
|
|
|
|
|
data += WC_SHA256_BLOCK_SIZE;
|
|
|
|
|
len -= WC_SHA256_BLOCK_SIZE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2019-09-25 12:47:12 -07:00
|
|
|
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
|
|
|
|
cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
|
|
|
|
|
sha256->digest);
|
|
|
|
|
#else
|
|
|
|
|
MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
|
2021-10-12 19:34:12 -05:00
|
|
|
(uint32_t*)sha256->digest);
|
2017-06-28 13:42:46 -06:00
|
|
|
#endif
|
2019-10-29 13:41:28 -07:00
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#elif defined(WOLFSSL_PIC32MZ_HASH)
|
2017-08-01 12:42:09 -07:00
|
|
|
#include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#elif defined(STM32_HASH_SHA2)
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
/* Supports CubeMX HAL or Standard Peripheral Library */
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
if (sha256 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
(void)devId;
|
|
|
|
|
(void)heap;
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2020-05-28 07:57:56 -07:00
|
|
|
XMEMSET(sha256, 0, sizeof(wc_Sha256));
|
2018-01-31 11:25:20 -08:00
|
|
|
wc_Stm32_Hash_Init(&sha256->stmCtx);
|
2017-08-24 12:06:42 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
2024-04-30 11:06:30 -04:00
|
|
|
if (sha256 == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL && len == 0) {
|
|
|
|
|
/* valid, but do nothing */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL) {
|
2017-08-24 12:06:42 -07:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
ret = wc_Stm32_Hash_Update(&sha256->stmCtx,
|
2022-06-06 12:11:06 -07:00
|
|
|
HASH_AlgoSelection_SHA256, data, len, WC_SHA256_BLOCK_SIZE);
|
2018-01-31 11:25:20 -08:00
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
2017-08-24 12:06:42 -07:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
if (sha256 == NULL || hash == NULL) {
|
2017-08-24 12:06:42 -07:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
ret = wc_Stm32_Hash_Final(&sha256->stmCtx,
|
|
|
|
|
HASH_AlgoSelection_SHA256, hash, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
2017-08-24 12:06:42 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
(void)wc_InitSha256(sha256); /* reset state */
|
2017-08-24 12:06:42 -07:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-01-31 11:25:20 -08:00
|
|
|
|
2020-08-07 14:40:44 -06:00
|
|
|
#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
|
|
|
|
|
!defined(WOLFSSL_QNX_CAAM)
|
2017-12-14 11:46:20 -07:00
|
|
|
/* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
|
2018-07-18 17:26:25 -06:00
|
|
|
|
2021-11-01 16:18:59 -07:00
|
|
|
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
|
2021-08-20 12:59:41 -06:00
|
|
|
|
|
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
|
|
|
|
{
|
|
|
|
|
if (sha256 == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
(void)devId;
|
|
|
|
|
|
|
|
|
|
return se050_hash_init(&sha256->se050Ctx, heap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
|
|
|
|
|
{
|
2024-04-30 11:06:30 -04:00
|
|
|
if (sha256 == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL && len == 0) {
|
|
|
|
|
/* valid, but do nothing */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 12:59:41 -06:00
|
|
|
return se050_hash_update(&sha256->se050Ctx, data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
|
2021-11-05 18:16:08 -05:00
|
|
|
{
|
2021-08-20 12:59:41 -06:00
|
|
|
int ret = 0;
|
|
|
|
|
ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
|
|
|
|
|
kAlgorithm_SSS_SHA256);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2021-11-05 18:16:08 -05:00
|
|
|
ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
|
2021-08-20 12:59:41 -06:00
|
|
|
kAlgorithm_SSS_SHA256);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:26:25 -06:00
|
|
|
#elif defined(WOLFSSL_AFALG_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
|
|
|
|
|
|
2018-08-17 09:46:16 -06:00
|
|
|
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
|
|
|
|
|
|
2020-01-31 14:26:04 -08:00
|
|
|
#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)
|
|
|
|
|
#include "hal_data.h"
|
|
|
|
|
|
|
|
|
|
#ifndef WOLFSSL_SCE_SHA256_HANDLE
|
|
|
|
|
#define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-04 16:03:45 -08:00
|
|
|
#define WC_SHA256_DIGEST_WORD_SIZE 16
|
2020-01-31 14:26:04 -08:00
|
|
|
#define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D))
|
|
|
|
|
static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data)
|
|
|
|
|
{
|
2020-02-12 10:31:34 -07:00
|
|
|
if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
|
|
|
|
|
CRYPTO_WORD_ENDIAN_LITTLE)
|
2020-02-04 16:03:45 -08:00
|
|
|
{
|
2020-02-12 10:31:34 -07:00
|
|
|
ByteReverseWords((word32*)data, (word32*)data,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
ByteReverseWords(sha256->digest, sha256->digest,
|
|
|
|
|
WC_SHA256_DIGEST_SIZE);
|
2020-02-04 16:03:45 -08:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 10:31:34 -07:00
|
|
|
if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(
|
|
|
|
|
WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data,
|
|
|
|
|
WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){
|
2020-01-31 14:26:04 -08:00
|
|
|
WOLFSSL_MSG("Unexpected hardware return value");
|
|
|
|
|
return WC_HW_E;
|
|
|
|
|
}
|
2020-02-04 16:03:45 -08:00
|
|
|
|
2020-02-12 10:31:34 -07:00
|
|
|
if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
|
|
|
|
|
CRYPTO_WORD_ENDIAN_LITTLE)
|
2020-02-04 16:03:45 -08:00
|
|
|
{
|
2020-02-12 10:31:34 -07:00
|
|
|
ByteReverseWords((word32*)data, (word32*)data,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
ByteReverseWords(sha256->digest, sha256->digest,
|
|
|
|
|
WC_SHA256_DIGEST_SIZE);
|
2020-02-04 16:03:45 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 14:26:04 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (sha256 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
|
|
|
|
sha256->heap = heap;
|
|
|
|
|
|
|
|
|
|
ret = InitSha256(sha256);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
(void)devId;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 15:47:00 -07:00
|
|
|
#elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
|
2018-11-21 18:41:29 +09:00
|
|
|
|
2022-07-06 09:30:49 -07:00
|
|
|
/* HW may fail since there's only one, so we still need SW */
|
2018-11-21 18:41:29 +09:00
|
|
|
#define NEED_SOFT_SHA256
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
/*
|
|
|
|
|
** An Espressif-specific InitSha256()
|
|
|
|
|
**
|
2023-04-19 15:10:22 +02:00
|
|
|
** soft SHA needs initialization digest, but HW does not.
|
|
|
|
|
*/
|
2018-11-21 18:41:29 +09:00
|
|
|
static int InitSha256(wc_Sha256* sha256)
|
|
|
|
|
{
|
2022-07-06 09:30:49 -07:00
|
|
|
int ret = 0; /* zero = success */
|
2018-11-21 18:41:29 +09:00
|
|
|
|
2023-11-21 16:22:49 -08:00
|
|
|
/* We may or may not need initial digest for HW.
|
|
|
|
|
* Always needed for SW-only. */
|
2018-11-21 18:41:29 +09:00
|
|
|
sha256->digest[0] = 0x6A09E667L;
|
|
|
|
|
sha256->digest[1] = 0xBB67AE85L;
|
|
|
|
|
sha256->digest[2] = 0x3C6EF372L;
|
|
|
|
|
sha256->digest[3] = 0xA54FF53AL;
|
|
|
|
|
sha256->digest[4] = 0x510E527FL;
|
|
|
|
|
sha256->digest[5] = 0x9B05688CL;
|
|
|
|
|
sha256->digest[6] = 0x1F83D9ABL;
|
|
|
|
|
sha256->digest[7] = 0x5BE0CD19L;
|
|
|
|
|
|
|
|
|
|
sha256->buffLen = 0;
|
|
|
|
|
sha256->loLen = 0;
|
|
|
|
|
sha256->hiLen = 0;
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
|
2024-01-25 15:23:58 -08:00
|
|
|
ret = esp_sha_init((WC_ESP32SHA*)&(sha256->ctx), WC_HASH_TYPE_SHA256);
|
2023-11-20 18:05:18 -08:00
|
|
|
#endif
|
2018-11-21 18:41:29 +09:00
|
|
|
return ret;
|
|
|
|
|
}
|
2022-07-06 09:30:49 -07:00
|
|
|
|
|
|
|
|
/*
|
2023-11-20 18:05:18 -08:00
|
|
|
** An Espressif-specific wolfCrypt InitSha256 external wrapper.
|
2023-04-19 15:10:22 +02:00
|
|
|
**
|
|
|
|
|
** we'll assume this is ALWAYS for a new, uninitialized sha256
|
|
|
|
|
*/
|
2018-11-21 18:41:29 +09:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
|
|
|
|
{
|
2023-04-19 15:10:22 +02:00
|
|
|
(void)devId;
|
|
|
|
|
if (sha256 == NULL) {
|
2018-11-21 18:41:29 +09:00
|
|
|
return BAD_FUNC_ARG;
|
2023-04-19 15:10:22 +02:00
|
|
|
}
|
2018-11-21 18:41:29 +09:00
|
|
|
|
2024-01-25 15:23:58 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2023-04-19 15:10:22 +02:00
|
|
|
/* We know this is a fresh, uninitialized item, so set to INIT */
|
|
|
|
|
if (sha256->ctx.mode != ESP32_SHA_INIT) {
|
|
|
|
|
ESP_LOGV(TAG, "Set ctx mode from prior value: "
|
|
|
|
|
"%d", sha256->ctx.mode);
|
|
|
|
|
}
|
2018-11-21 18:41:29 +09:00
|
|
|
sha256->ctx.mode = ESP32_SHA_INIT;
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
2018-11-21 18:41:29 +09:00
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
return InitSha256(sha256);
|
2018-11-21 18:41:29 +09:00
|
|
|
}
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2023-06-17 11:57:11 +09:00
|
|
|
#elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
|
|
|
|
|
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
|
2019-08-19 17:32:22 +09:00
|
|
|
!defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2019-08-19 17:32:22 +09:00
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2023-08-09 09:08:44 +09:00
|
|
|
#elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP)) \
|
|
|
|
|
&& !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
|
2021-10-30 20:21:21 +09:00
|
|
|
|
2023-08-12 17:28:24 +09:00
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
|
2021-11-29 23:58:39 -06:00
|
|
|
|
2020-06-01 13:23:50 +02:00
|
|
|
#elif defined(WOLFSSL_PSOC6_CRYPTO)
|
|
|
|
|
|
|
|
|
|
/* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
|
|
|
|
|
|
2020-09-18 11:46:08 +02:00
|
|
|
#elif defined(WOLFSSL_IMXRT_DCP)
|
|
|
|
|
#include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
|
|
|
|
|
/* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
|
|
|
|
|
|
2020-11-06 12:20:03 -08:00
|
|
|
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
|
|
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#elif defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
|
|
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
|
|
|
|
|
2022-09-15 16:18:44 -05:00
|
|
|
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
|
|
|
|
|
|
|
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#else
|
|
|
|
|
#define NEED_SOFT_SHA256
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (sha256 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
2021-12-08 23:59:19 +00:00
|
|
|
ret = InitSha256(sha256);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->heap = heap;
|
2019-01-17 11:01:14 -08:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
2019-01-09 14:36:40 -08:00
|
|
|
sha256->devId = devId;
|
2019-01-21 15:52:33 -08:00
|
|
|
sha256->devCtx = NULL;
|
2019-01-09 14:36:40 -08:00
|
|
|
#endif
|
2020-08-26 09:44:32 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
sha256->W = NULL;
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
|
|
|
|
ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
|
|
|
|
|
WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
|
|
|
|
|
#else
|
|
|
|
|
(void)devId;
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
2022-12-28 13:02:07 -08:00
|
|
|
#ifdef WOLFSSL_IMXRT1170_CAAM
|
|
|
|
|
ret = wc_CAAM_HashInit(&sha256->hndl, &sha256->ctx, WC_HASH_TYPE_SHA256);
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
2015-05-21 14:16:09 +09:00
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif /* End Hardware Acceleration */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#ifdef NEED_SOFT_SHA256
|
|
|
|
|
|
2020-01-31 23:06:15 +10:00
|
|
|
static const FLASH_QUALIFIER ALIGN32 word32 K[64] = {
|
2016-12-19 12:15:10 -08:00
|
|
|
0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
|
|
|
|
|
0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
|
|
|
|
|
0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
|
|
|
|
|
0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
|
|
|
|
|
0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
|
|
|
|
|
0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
|
|
|
|
|
0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
|
|
|
|
|
0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
|
|
|
|
|
0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
|
|
|
|
|
0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
|
|
|
|
|
0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
|
|
|
|
|
0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
|
|
|
|
|
0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-03 14:17:47 -07:00
|
|
|
/* Both versions of Ch and Maj are logically the same, but with the second set
|
|
|
|
|
the compilers can recognize them better for optimization */
|
2019-05-23 21:53:07 -06:00
|
|
|
#ifdef WOLFSSL_SHA256_BY_SPEC
|
2019-06-03 14:17:47 -07:00
|
|
|
/* SHA256 math based on specification */
|
2016-12-19 12:15:10 -08:00
|
|
|
#define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
|
|
|
|
|
#define Maj(x,y,z) ((((x) | (y)) & (z)) | ((x) & (y)))
|
2019-05-23 21:53:07 -06:00
|
|
|
#else
|
2019-06-03 14:17:47 -07:00
|
|
|
/* SHA256 math reworked for easier compiler optimization */
|
2019-05-23 21:53:07 -06:00
|
|
|
#define Ch(x,y,z) ((((y) ^ (z)) & (x)) ^ (z))
|
|
|
|
|
#define Maj(x,y,z) ((((x) ^ (y)) & ((y) ^ (z))) ^ (y))
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
#define R(x, n) (((x) & 0xFFFFFFFFU) >> (n))
|
|
|
|
|
|
|
|
|
|
#define S(x, n) rotrFixed(x, n)
|
2017-11-09 11:05:28 -08:00
|
|
|
#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
|
|
|
|
|
#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
|
|
|
|
|
#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
|
2016-12-19 12:15:10 -08:00
|
|
|
#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
|
|
|
|
|
|
jumbo patch of fixes for clang-tidy gripes (with some bug fixes).
defect/gripe statistics:
configured --enable-all --enable-sp-math-all --enable-intelasm
with LLVM 13 clang-tidy -checks=readability-*,bugprone-*,misc-no-recursion,misc-misplaced-const,misc-redundant-expression,misc-unused-parameters,misc-unused-using-decls,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-optin.performance.Padding,-readability-braces-around-statements,-readability-function-size,-readability-function-cognitive-complexity,-bugprone-suspicious-include,-bugprone-easily-swappable-parameters,-readability-isolate-declaration,-readability-magic-numbers,-readability-else-after-return,-bugprone-reserved-identifier,-readability-suspicious-call-argument,-bugprone-suspicious-string-compare,-bugprone-branch-clone,-misc-redundant-expression,-readability-non-const-parameter,-readability-redundant-control-flow,-readability-misleading-indentation,-bugprone-narrowing-conversions,-bugprone-implicit-widening-of-multiplication-result
[note these figures don't reflect additional defects fixed in this commit for --enable-smallstack, --enable-fips, --enable-async, --enable-asn=template, and --enable-fastmath, and --disable-fastmath]
pre-patch warning count per file, with suppressions:
clang-analyzer-security.insecureAPI.strcpy 6 wolfssl/tests/suites.c
clang-analyzer-security.insecureAPI.strcpy 2 wolfssl/testsuite/testsuite.c
bugprone-suspicious-missing-comma 3 wolfssl/examples/server/server.c
bugprone-suspicious-missing-comma 3 wolfssl/examples/client/client.c
readability-redundant-preprocessor 2 wolfssl/wolfcrypt/src/asn.c
readability-redundant-preprocessor 1 wolfssl/wolfcrypt/src/rsa.c
readability-redundant-preprocessor 9 wolfssl/src/ssl.c
readability-redundant-preprocessor 2 wolfssl/src/tls13.c
readability-redundant-preprocessor 18 wolfssl/tests/api.c
readability-redundant-preprocessor 3 wolfssl/src/internal.c
readability-redundant-preprocessor 10 wolfssl/wolfcrypt/test/test.c
readability-named-parameter 1 wolfssl/wolfcrypt/benchmark/benchmark.c
readability-named-parameter 7 wolfssl/src/internal.c
readability-named-parameter 1 wolfssl/wolfcrypt/src/ecc.c
readability-named-parameter 1 wolfssl/testsuite/testsuite.c
readability-named-parameter 11 wolfssl/wolfcrypt/src/ge_operations.c
misc-no-recursion 3 wolfssl/src/ssl.c
readability-uppercase-literal-suffix 4 wolfssl/wolfcrypt/src/asn.c
readability-uppercase-literal-suffix 1 wolfssl/src/ssl.c
readability-uppercase-literal-suffix 13 wolfssl/wolfcrypt/benchmark/benchmark.c
bugprone-too-small-loop-variable 1 wolfssl/wolfcrypt/src/rsa.c
bugprone-too-small-loop-variable 2 wolfssl/wolfcrypt/src/sha3.c
bugprone-too-small-loop-variable 4 wolfssl/wolfcrypt/src/idea.c
bugprone-signed-char-misuse 2 wolfssl/src/ssl.c
bugprone-signed-char-misuse 3 wolfssl/wolfcrypt/src/sp_int.c
bugprone-signed-char-misuse 3 wolfssl/examples/client/client.c
bugprone-macro-parentheses 19 wolfssl/wolfcrypt/src/aes.c
bugprone-macro-parentheses 109 wolfssl/wolfcrypt/src/camellia.c
bugprone-macro-parentheses 1 wolfssl/src/tls.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/md4.c
bugprone-macro-parentheses 2 wolfssl/wolfcrypt/src/asn.c
bugprone-macro-parentheses 26 wolfssl/wolfcrypt/src/blake2b.c
bugprone-macro-parentheses 257 wolfssl/wolfcrypt/src/sha3.c
bugprone-macro-parentheses 15 wolfssl/src/ssl.c
bugprone-macro-parentheses 1 wolfssl/wolfcrypt/src/sha.c
bugprone-macro-parentheses 8 wolfssl/tests/api.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/src/sp_int.c
bugprone-macro-parentheses 6 wolfssl/wolfcrypt/benchmark/benchmark.c
bugprone-macro-parentheses 38 wolfssl/wolfcrypt/src/hc128.c
bugprone-macro-parentheses 12 wolfssl/wolfcrypt/src/md5.c
bugprone-macro-parentheses 10 wolfssl/wolfcrypt/src/sha256.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/test/test.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/ecc.c
bugprone-macro-parentheses 2 wolfssl/tests/suites.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/src/cpuid.c
bugprone-macro-parentheses 26 wolfssl/wolfcrypt/src/blake2s.c
bugprone-macro-parentheses 24 wolfssl/wolfcrypt/src/sha512.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/poly1305.c
bugprone-macro-parentheses 24 wolfssl/wolfcrypt/src/ripemd.c
readability-inconsistent-declaration-parameter-name 1 wolfssl/src/internal.c
readability-inconsistent-declaration-parameter-name 1 wolfssl/testsuite/testsuite.c
pre-patch warning count summaries, with suppressions:
clang-analyzer-security.insecureAPI.strcpy 8
bugprone-suspicious-missing-comma 6
readability-redundant-preprocessor 45
readability-named-parameter 21
misc-no-recursion 3
readability-uppercase-literal-suffix 18
bugprone-too-small-loop-variable 7
bugprone-signed-char-misuse 8
bugprone-macro-parentheses 601
readability-inconsistent-declaration-parameter-name 2
pre-patch warning count summaries, without suppressions:
clang-analyzer-security.insecureAPI.strcpy 8
bugprone-branch-clone 152
readability-non-const-parameter 118
bugprone-suspicious-missing-comma 6
bugprone-suspicious-include 52
readability-magic-numbers 22423
readability-redundant-preprocessor 45
readability-named-parameter 21
readability-function-cognitive-complexity 845
readability-else-after-return 398
bugprone-implicit-widening-of-multiplication-result 595
readability-function-size 21
readability-isolate-declaration 1090
misc-redundant-expression 2
bugprone-narrowing-conversions 994
misc-no-recursion 3
readability-uppercase-literal-suffix 18
bugprone-reserved-identifier 56
readability-suspicious-call-argument 74
bugprone-too-small-loop-variable 7
bugprone-easily-swappable-parameters 437
bugprone-signed-char-misuse 8
readability-misleading-indentation 94
bugprone-macro-parentheses 601
readability-inconsistent-declaration-parameter-name 2
bugprone-suspicious-string-compare 495
readability-redundant-control-flow 20
readability-braces-around-statements 11483
clang-analyzer-valist.Uninitialized 1
clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling 3502
2022-01-19 23:09:50 -06:00
|
|
|
#define a(i) S[(0-(i)) & 7]
|
|
|
|
|
#define b(i) S[(1-(i)) & 7]
|
|
|
|
|
#define c(i) S[(2-(i)) & 7]
|
|
|
|
|
#define d(i) S[(3-(i)) & 7]
|
|
|
|
|
#define e(i) S[(4-(i)) & 7]
|
|
|
|
|
#define f(i) S[(5-(i)) & 7]
|
|
|
|
|
#define g(i) S[(6-(i)) & 7]
|
|
|
|
|
#define h(i) S[(7-(i)) & 7]
|
2017-11-09 11:05:28 -08:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
#ifndef XTRANSFORM
|
|
|
|
|
#define XTRANSFORM(S, D) Transform_Sha256((S),(D))
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-23 21:53:07 -06:00
|
|
|
#ifndef SHA256_MANY_REGISTERS
|
2017-11-09 11:05:28 -08:00
|
|
|
#define RND(j) \
|
jumbo patch of fixes for clang-tidy gripes (with some bug fixes).
defect/gripe statistics:
configured --enable-all --enable-sp-math-all --enable-intelasm
with LLVM 13 clang-tidy -checks=readability-*,bugprone-*,misc-no-recursion,misc-misplaced-const,misc-redundant-expression,misc-unused-parameters,misc-unused-using-decls,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-optin.performance.Padding,-readability-braces-around-statements,-readability-function-size,-readability-function-cognitive-complexity,-bugprone-suspicious-include,-bugprone-easily-swappable-parameters,-readability-isolate-declaration,-readability-magic-numbers,-readability-else-after-return,-bugprone-reserved-identifier,-readability-suspicious-call-argument,-bugprone-suspicious-string-compare,-bugprone-branch-clone,-misc-redundant-expression,-readability-non-const-parameter,-readability-redundant-control-flow,-readability-misleading-indentation,-bugprone-narrowing-conversions,-bugprone-implicit-widening-of-multiplication-result
[note these figures don't reflect additional defects fixed in this commit for --enable-smallstack, --enable-fips, --enable-async, --enable-asn=template, and --enable-fastmath, and --disable-fastmath]
pre-patch warning count per file, with suppressions:
clang-analyzer-security.insecureAPI.strcpy 6 wolfssl/tests/suites.c
clang-analyzer-security.insecureAPI.strcpy 2 wolfssl/testsuite/testsuite.c
bugprone-suspicious-missing-comma 3 wolfssl/examples/server/server.c
bugprone-suspicious-missing-comma 3 wolfssl/examples/client/client.c
readability-redundant-preprocessor 2 wolfssl/wolfcrypt/src/asn.c
readability-redundant-preprocessor 1 wolfssl/wolfcrypt/src/rsa.c
readability-redundant-preprocessor 9 wolfssl/src/ssl.c
readability-redundant-preprocessor 2 wolfssl/src/tls13.c
readability-redundant-preprocessor 18 wolfssl/tests/api.c
readability-redundant-preprocessor 3 wolfssl/src/internal.c
readability-redundant-preprocessor 10 wolfssl/wolfcrypt/test/test.c
readability-named-parameter 1 wolfssl/wolfcrypt/benchmark/benchmark.c
readability-named-parameter 7 wolfssl/src/internal.c
readability-named-parameter 1 wolfssl/wolfcrypt/src/ecc.c
readability-named-parameter 1 wolfssl/testsuite/testsuite.c
readability-named-parameter 11 wolfssl/wolfcrypt/src/ge_operations.c
misc-no-recursion 3 wolfssl/src/ssl.c
readability-uppercase-literal-suffix 4 wolfssl/wolfcrypt/src/asn.c
readability-uppercase-literal-suffix 1 wolfssl/src/ssl.c
readability-uppercase-literal-suffix 13 wolfssl/wolfcrypt/benchmark/benchmark.c
bugprone-too-small-loop-variable 1 wolfssl/wolfcrypt/src/rsa.c
bugprone-too-small-loop-variable 2 wolfssl/wolfcrypt/src/sha3.c
bugprone-too-small-loop-variable 4 wolfssl/wolfcrypt/src/idea.c
bugprone-signed-char-misuse 2 wolfssl/src/ssl.c
bugprone-signed-char-misuse 3 wolfssl/wolfcrypt/src/sp_int.c
bugprone-signed-char-misuse 3 wolfssl/examples/client/client.c
bugprone-macro-parentheses 19 wolfssl/wolfcrypt/src/aes.c
bugprone-macro-parentheses 109 wolfssl/wolfcrypt/src/camellia.c
bugprone-macro-parentheses 1 wolfssl/src/tls.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/md4.c
bugprone-macro-parentheses 2 wolfssl/wolfcrypt/src/asn.c
bugprone-macro-parentheses 26 wolfssl/wolfcrypt/src/blake2b.c
bugprone-macro-parentheses 257 wolfssl/wolfcrypt/src/sha3.c
bugprone-macro-parentheses 15 wolfssl/src/ssl.c
bugprone-macro-parentheses 1 wolfssl/wolfcrypt/src/sha.c
bugprone-macro-parentheses 8 wolfssl/tests/api.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/src/sp_int.c
bugprone-macro-parentheses 6 wolfssl/wolfcrypt/benchmark/benchmark.c
bugprone-macro-parentheses 38 wolfssl/wolfcrypt/src/hc128.c
bugprone-macro-parentheses 12 wolfssl/wolfcrypt/src/md5.c
bugprone-macro-parentheses 10 wolfssl/wolfcrypt/src/sha256.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/test/test.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/ecc.c
bugprone-macro-parentheses 2 wolfssl/tests/suites.c
bugprone-macro-parentheses 4 wolfssl/wolfcrypt/src/cpuid.c
bugprone-macro-parentheses 26 wolfssl/wolfcrypt/src/blake2s.c
bugprone-macro-parentheses 24 wolfssl/wolfcrypt/src/sha512.c
bugprone-macro-parentheses 3 wolfssl/wolfcrypt/src/poly1305.c
bugprone-macro-parentheses 24 wolfssl/wolfcrypt/src/ripemd.c
readability-inconsistent-declaration-parameter-name 1 wolfssl/src/internal.c
readability-inconsistent-declaration-parameter-name 1 wolfssl/testsuite/testsuite.c
pre-patch warning count summaries, with suppressions:
clang-analyzer-security.insecureAPI.strcpy 8
bugprone-suspicious-missing-comma 6
readability-redundant-preprocessor 45
readability-named-parameter 21
misc-no-recursion 3
readability-uppercase-literal-suffix 18
bugprone-too-small-loop-variable 7
bugprone-signed-char-misuse 8
bugprone-macro-parentheses 601
readability-inconsistent-declaration-parameter-name 2
pre-patch warning count summaries, without suppressions:
clang-analyzer-security.insecureAPI.strcpy 8
bugprone-branch-clone 152
readability-non-const-parameter 118
bugprone-suspicious-missing-comma 6
bugprone-suspicious-include 52
readability-magic-numbers 22423
readability-redundant-preprocessor 45
readability-named-parameter 21
readability-function-cognitive-complexity 845
readability-else-after-return 398
bugprone-implicit-widening-of-multiplication-result 595
readability-function-size 21
readability-isolate-declaration 1090
misc-redundant-expression 2
bugprone-narrowing-conversions 994
misc-no-recursion 3
readability-uppercase-literal-suffix 18
bugprone-reserved-identifier 56
readability-suspicious-call-argument 74
bugprone-too-small-loop-variable 7
bugprone-easily-swappable-parameters 437
bugprone-signed-char-misuse 8
readability-misleading-indentation 94
bugprone-macro-parentheses 601
readability-inconsistent-declaration-parameter-name 2
bugprone-suspicious-string-compare 495
readability-redundant-control-flow 20
readability-braces-around-statements 11483
clang-analyzer-valist.Uninitialized 1
clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling 3502
2022-01-19 23:09:50 -06:00
|
|
|
t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+(j)] + W[i+(j)]; \
|
2017-11-09 11:05:28 -08:00
|
|
|
t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
|
|
|
|
|
d(j) += t0; \
|
|
|
|
|
h(j) = t0 + t1
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
word32 S[8], t0, t1;
|
|
|
|
|
int i;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
word32* W = sha256->W;
|
|
|
|
|
if (W == NULL) {
|
|
|
|
|
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
|
2018-08-21 09:03:11 +10:00
|
|
|
DYNAMIC_TYPE_DIGEST);
|
2018-07-17 09:17:39 +10:00
|
|
|
if (W == NULL)
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
sha256->W = W;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(WOLFSSL_SMALL_STACK)
|
2016-12-19 12:15:10 -08:00
|
|
|
word32* W;
|
2017-09-25 18:47:36 -07:00
|
|
|
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
|
2018-07-17 09:17:39 +10:00
|
|
|
DYNAMIC_TYPE_TMP_BUFFER);
|
2016-12-19 12:15:10 -08:00
|
|
|
if (W == NULL)
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
#else
|
2017-09-25 18:47:36 -07:00
|
|
|
word32 W[WC_SHA256_BLOCK_SIZE];
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
/* Copy context->state[] to working vars */
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
|
S[i] = sha256->digest[i];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
2023-04-04 13:46:30 -07:00
|
|
|
W[i] = *((const word32*)&data[i*(int)sizeof(word32)]);
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
for (i = 16; i < WC_SHA256_BLOCK_SIZE; i++)
|
2016-12-19 12:15:10 -08:00
|
|
|
W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
|
|
|
|
|
|
2017-11-09 11:05:28 -08:00
|
|
|
#ifdef USE_SLOW_SHA256
|
|
|
|
|
/* not unrolled - ~2k smaller and ~25% slower */
|
2017-09-25 18:47:36 -07:00
|
|
|
for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
|
2017-11-09 11:05:28 -08:00
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < 8; j++) { /* braces needed here for macros {} */
|
|
|
|
|
RND(j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* partially loop unrolled */
|
|
|
|
|
for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
|
|
|
|
|
RND(0); RND(1); RND(2); RND(3);
|
|
|
|
|
RND(4); RND(5); RND(6); RND(7);
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2017-11-09 11:05:28 -08:00
|
|
|
#endif /* USE_SLOW_SHA256 */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
/* Add the working vars back into digest state[] */
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
|
sha256->digest[i] += S[i];
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
ForceZero(W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
|
2016-12-19 12:15:10 -08:00
|
|
|
XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-05-23 21:53:07 -06:00
|
|
|
#else
|
2019-06-03 14:17:47 -07:00
|
|
|
/* SHA256 version that keeps all data in registers */
|
2019-09-25 12:47:12 -07:00
|
|
|
#define SCHED1(j) (W[j] = *((word32*)&data[j*sizeof(word32)]))
|
2019-05-23 21:53:07 -06:00
|
|
|
#define SCHED(j) ( \
|
|
|
|
|
W[ j & 15] += \
|
|
|
|
|
Gamma1(W[(j-2) & 15])+ \
|
|
|
|
|
W[(j-7) & 15] + \
|
|
|
|
|
Gamma0(W[(j-15) & 15]) \
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#define RND1(j) \
|
|
|
|
|
t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED1(j); \
|
|
|
|
|
t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
|
|
|
|
|
d(j) += t0; \
|
|
|
|
|
h(j) = t0 + t1
|
|
|
|
|
#define RNDN(j) \
|
|
|
|
|
t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED(j); \
|
|
|
|
|
t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
|
|
|
|
|
d(j) += t0; \
|
|
|
|
|
h(j) = t0 + t1
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
|
2019-05-23 21:53:07 -06:00
|
|
|
{
|
|
|
|
|
word32 S[8], t0, t1;
|
|
|
|
|
int i;
|
2024-08-26 11:48:37 +10:00
|
|
|
#ifdef USE_SLOW_SHA256
|
|
|
|
|
int j;
|
|
|
|
|
#endif
|
2019-06-03 14:17:47 -07:00
|
|
|
word32 W[WC_SHA256_BLOCK_SIZE/sizeof(word32)];
|
2019-05-23 21:53:07 -06:00
|
|
|
|
|
|
|
|
/* Copy digest to working vars */
|
|
|
|
|
S[0] = sha256->digest[0];
|
|
|
|
|
S[1] = sha256->digest[1];
|
|
|
|
|
S[2] = sha256->digest[2];
|
|
|
|
|
S[3] = sha256->digest[3];
|
|
|
|
|
S[4] = sha256->digest[4];
|
|
|
|
|
S[5] = sha256->digest[5];
|
|
|
|
|
S[6] = sha256->digest[6];
|
|
|
|
|
S[7] = sha256->digest[7];
|
|
|
|
|
|
|
|
|
|
i = 0;
|
2024-08-26 11:48:37 +10:00
|
|
|
#ifdef USE_SLOW_SHA256
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
|
RND1(j);
|
|
|
|
|
}
|
|
|
|
|
for (i = 16; i < 64; i += 16) {
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
|
RNDN(j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
2019-05-23 21:53:07 -06:00
|
|
|
RND1( 0); RND1( 1); RND1( 2); RND1( 3);
|
|
|
|
|
RND1( 4); RND1( 5); RND1( 6); RND1( 7);
|
|
|
|
|
RND1( 8); RND1( 9); RND1(10); RND1(11);
|
|
|
|
|
RND1(12); RND1(13); RND1(14); RND1(15);
|
|
|
|
|
/* 64 operations, partially loop unrolled */
|
|
|
|
|
for (i = 16; i < 64; i += 16) {
|
|
|
|
|
RNDN( 0); RNDN( 1); RNDN( 2); RNDN( 3);
|
|
|
|
|
RNDN( 4); RNDN( 5); RNDN( 6); RNDN( 7);
|
|
|
|
|
RNDN( 8); RNDN( 9); RNDN(10); RNDN(11);
|
|
|
|
|
RNDN(12); RNDN(13); RNDN(14); RNDN(15);
|
|
|
|
|
}
|
2024-08-26 11:48:37 +10:00
|
|
|
#endif
|
2019-05-23 21:53:07 -06:00
|
|
|
|
|
|
|
|
/* Add the working vars back into digest */
|
|
|
|
|
sha256->digest[0] += S[0];
|
|
|
|
|
sha256->digest[1] += S[1];
|
|
|
|
|
sha256->digest[2] += S[2];
|
|
|
|
|
sha256->digest[3] += S[3];
|
|
|
|
|
sha256->digest[4] += S[4];
|
|
|
|
|
sha256->digest[5] += S[5];
|
|
|
|
|
sha256->digest[6] += S[6];
|
|
|
|
|
sha256->digest[7] += S[7];
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-06-03 14:17:47 -07:00
|
|
|
#endif /* SHA256_MANY_REGISTERS */
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
|
|
|
|
/* End wc_ software implementation */
|
2015-05-21 14:16:09 +09:00
|
|
|
|
|
|
|
|
|
2017-08-24 12:06:42 -07:00
|
|
|
#ifdef XTRANSFORM
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2018-06-26 11:40:34 -07:00
|
|
|
static WC_INLINE void AddLength(wc_Sha256* sha256, word32 len)
|
2018-01-31 11:25:20 -08:00
|
|
|
{
|
|
|
|
|
word32 tmp = sha256->loLen;
|
2019-09-25 12:47:12 -07:00
|
|
|
if ((sha256->loLen += len) < tmp) {
|
2018-01-31 11:25:20 -08:00
|
|
|
sha256->hiLen++; /* carry low to high */
|
2019-09-25 12:47:12 -07:00
|
|
|
}
|
2018-01-31 11:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
/* do block size increments/updates */
|
2024-02-29 08:44:33 +10:00
|
|
|
static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data,
|
|
|
|
|
word32 len)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2019-09-25 12:47:12 -07:00
|
|
|
word32 blocksLen;
|
2019-09-27 09:22:18 -07:00
|
|
|
byte* local;
|
2016-12-05 09:15:58 -08:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
/* check that internal buffLen is valid */
|
|
|
|
|
if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) {
|
|
|
|
|
return BUFFER_E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* add length for final */
|
2019-01-25 12:03:08 +10:00
|
|
|
AddLength(sha256, len);
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
local = (byte*)sha256->buffer;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
/* process any remainder from previous operation */
|
2017-10-31 09:05:52 +10:00
|
|
|
if (sha256->buffLen > 0) {
|
2019-09-25 12:47:12 -07:00
|
|
|
blocksLen = min(len, WC_SHA256_BLOCK_SIZE - sha256->buffLen);
|
|
|
|
|
XMEMCPY(&local[sha256->buffLen], data, blocksLen);
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
sha256->buffLen += blocksLen;
|
|
|
|
|
data += blocksLen;
|
|
|
|
|
len -= blocksLen;
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) {
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT) {
|
|
|
|
|
ESP_LOGV(TAG, "Sha256Update try hardware");
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords(sha256->buffer, sha256->buffer,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
}
|
2018-11-21 18:41:29 +09:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2022-07-06 09:30:49 -07:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_SW) {
|
2023-11-21 16:22:49 -08:00
|
|
|
#if defined(WOLFSSL_DEBUG_MUTEX)
|
2023-11-20 18:05:18 -08:00
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Sha256Update process software");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef WOLFSSL_HW_METRICS
|
|
|
|
|
{
|
|
|
|
|
/* Track of # SW during transforms during active HW */
|
|
|
|
|
esp_sw_sha256_count_add();
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_HW_METRICS */
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
2022-07-06 09:30:49 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2023-11-21 16:22:49 -08:00
|
|
|
#if defined(WOLFSSL_DEBUG_MUTEX)
|
2023-11-20 18:05:18 -08:00
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Sha256Update process hardware");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-09-25 12:47:12 -07:00
|
|
|
esp_sha256_process(sha256, (const byte*)local);
|
2018-11-21 18:41:29 +09:00
|
|
|
}
|
2019-09-25 12:47:12 -07:00
|
|
|
#else
|
2023-11-20 18:05:18 -08:00
|
|
|
/* Always SW */
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
|
|
|
|
#endif
|
2019-01-25 12:03:08 +10:00
|
|
|
if (ret == 0)
|
2017-10-31 09:05:52 +10:00
|
|
|
sha256->buffLen = 0;
|
|
|
|
|
else
|
2019-09-25 12:47:12 -07:00
|
|
|
len = 0; /* error */
|
2017-10-31 09:05:52 +10:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
/* process blocks */
|
|
|
|
|
#ifdef XTRANSFORM_LEN
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2024-04-23 01:31:43 -05:00
|
|
|
|
2024-04-27 01:02:31 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
if (sha256->sha_method != SHA256_C)
|
|
|
|
|
#elif defined(WC_NO_INTERNAL_FUNCTION_POINTERS)
|
2024-04-23 01:31:43 -05:00
|
|
|
if (sha_method != SHA256_C)
|
|
|
|
|
#else
|
2019-09-25 12:47:12 -07:00
|
|
|
if (Transform_Sha256_Len_p != NULL)
|
2024-04-23 01:31:43 -05:00
|
|
|
#endif
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
#endif
|
|
|
|
|
{
|
2024-02-29 08:44:33 +10:00
|
|
|
if (len >= WC_SHA256_BLOCK_SIZE) {
|
|
|
|
|
/* get number of blocks */
|
|
|
|
|
/* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
|
|
|
|
|
/* len (masked by 0xFFFFFFC0) returns block aligned length */
|
|
|
|
|
blocksLen = len & ~((word32)WC_SHA256_BLOCK_SIZE-1);
|
|
|
|
|
/* Byte reversal and alignment handled in function if required
|
|
|
|
|
*/
|
2019-01-25 12:03:08 +10:00
|
|
|
XTRANSFORM_LEN(sha256, data, blocksLen);
|
2017-10-31 09:05:52 +10:00
|
|
|
data += blocksLen;
|
|
|
|
|
len -= blocksLen;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2017-10-31 09:05:52 +10:00
|
|
|
else
|
2019-09-25 12:47:12 -07:00
|
|
|
#endif
|
|
|
|
|
#endif /* XTRANSFORM_LEN */
|
2022-09-30 16:19:40 +10:00
|
|
|
#if !defined(XTRANSFORM_LEN) || \
|
|
|
|
|
(defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
|
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
|
2017-10-31 09:05:52 +10:00
|
|
|
{
|
|
|
|
|
while (len >= WC_SHA256_BLOCK_SIZE) {
|
2019-09-27 09:22:18 -07:00
|
|
|
word32* local32 = sha256->buffer;
|
2019-09-25 12:47:12 -07:00
|
|
|
/* optimization to avoid memcpy if data pointer is properly aligned */
|
|
|
|
|
/* Intel transform function requires use of sha256->buffer */
|
|
|
|
|
/* Little Endian requires byte swap, so can't use data directly */
|
2019-09-26 11:53:24 -07:00
|
|
|
#if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) && \
|
2022-09-30 16:19:40 +10:00
|
|
|
!(defined(WOLFSSL_X86_64_BUILD) && \
|
|
|
|
|
defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
|
2021-05-03 11:23:55 -07:00
|
|
|
if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
|
2019-09-25 12:47:12 -07:00
|
|
|
local32 = (word32*)data;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
XMEMCPY(local32, data, WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
}
|
2017-11-16 08:59:21 +10:00
|
|
|
|
|
|
|
|
data += WC_SHA256_BLOCK_SIZE;
|
|
|
|
|
len -= WC_SHA256_BLOCK_SIZE;
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined( NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT){
|
|
|
|
|
ESP_LOGV(TAG, "Sha256Update try hardware loop");
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-11-16 08:59:21 +10:00
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords(local32, local32, WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
}
|
2017-10-31 09:05:52 +10:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2022-07-06 09:30:49 -07:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_SW) {
|
2023-04-19 15:10:22 +02:00
|
|
|
ESP_LOGV(TAG, "Sha256Update process software loop");
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local32);
|
2022-07-06 09:30:49 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2023-04-19 15:10:22 +02:00
|
|
|
ESP_LOGV(TAG, "Sha256Update process hardware");
|
2019-09-25 12:47:12 -07:00
|
|
|
esp_sha256_process(sha256, (const byte*)local32);
|
2018-11-21 18:41:29 +09:00
|
|
|
}
|
2023-04-19 15:10:22 +02:00
|
|
|
#else
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local32);
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-10-31 09:05:52 +10:00
|
|
|
if (ret != 0)
|
|
|
|
|
break;
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-31 09:05:52 +10:00
|
|
|
#endif
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
/* save remainder */
|
2021-02-06 08:15:30 +01:00
|
|
|
if (ret == 0 && len > 0) {
|
2017-10-31 09:05:52 +10:00
|
|
|
XMEMCPY(local, data, len);
|
|
|
|
|
sha256->buffLen = len;
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#if defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
|
|
|
|
|
|
|
|
|
|
#else
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
2024-02-29 08:44:33 +10:00
|
|
|
if (sha256 == NULL) {
|
2019-01-09 14:36:40 -08:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL && len == 0) {
|
|
|
|
|
/* valid, but do nothing */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-02-29 08:44:33 +10:00
|
|
|
if (data == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
2019-01-09 14:36:40 -08:00
|
|
|
|
2019-01-17 11:01:14 -08:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
2023-05-10 15:28:19 -07:00
|
|
|
#ifndef WOLF_CRYPTO_CB_FIND
|
|
|
|
|
if (sha256->devId != INVALID_DEVID)
|
|
|
|
|
#endif
|
2023-05-08 09:49:50 -07:00
|
|
|
{
|
2019-01-17 11:01:14 -08:00
|
|
|
int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
|
2024-06-08 16:39:53 -05:00
|
|
|
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
|
2019-01-09 14:36:40 -08:00
|
|
|
return ret;
|
2019-03-08 16:50:45 -08:00
|
|
|
/* fall-through when unavailable */
|
2019-01-09 14:36:40 -08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
|
|
|
|
if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
|
|
|
|
|
#if defined(HAVE_INTEL_QA)
|
|
|
|
|
return IntelQaSymSha256(&sha256->asyncDev, NULL, data, len);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return Sha256Update(sha256, data, len);
|
2015-05-21 14:16:09 +09:00
|
|
|
}
|
2021-08-19 11:25:59 +10:00
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2018-06-26 11:40:34 -07:00
|
|
|
static WC_INLINE int Sha256Final(wc_Sha256* sha256)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
2019-09-25 12:47:12 -07:00
|
|
|
byte* local;
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
/* we'll add a 0x80 byte at the end,
|
|
|
|
|
** so make sure we have appropriate buffer length. */
|
|
|
|
|
if (sha256->buffLen > WC_SHA256_BLOCK_SIZE - 1) {
|
|
|
|
|
/* exit with error code if there's a bad buffer size in buffLen */
|
|
|
|
|
return BAD_STATE_E;
|
|
|
|
|
} /* buffLen check */
|
|
|
|
|
|
2019-09-25 12:47:12 -07:00
|
|
|
local = (byte*)sha256->buffer;
|
|
|
|
|
local[sha256->buffLen++] = 0x80; /* add 1 */
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
/* pad with zeros */
|
2017-09-25 18:47:36 -07:00
|
|
|
if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
|
2024-02-02 19:50:22 -06:00
|
|
|
if (sha256->buffLen < WC_SHA256_BLOCK_SIZE) {
|
|
|
|
|
XMEMSET(&local[sha256->buffLen], 0,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE - sha256->buffLen);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT) {
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords(sha256->buffer, sha256->buffer,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
}
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2019-09-25 12:47:12 -07:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT) {
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
|
|
|
|
if (sha256->ctx.mode == ESP32_SHA_SW) {
|
|
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
2022-07-06 09:30:49 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = esp_sha256_process(sha256, (const byte*)local);
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2018-11-21 18:41:29 +09:00
|
|
|
#else
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
2018-11-21 18:41:29 +09:00
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
sha256->buffLen = 0;
|
|
|
|
|
}
|
2019-09-25 12:47:12 -07:00
|
|
|
XMEMSET(&local[sha256->buffLen], 0,
|
|
|
|
|
WC_SHA256_PAD_SIZE - sha256->buffLen);
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
/* put 64 bit length in separate 32 bit parts */
|
2016-12-19 12:15:10 -08:00
|
|
|
sha256->hiLen = (sha256->loLen >> (8 * sizeof(sha256->loLen) - 3)) +
|
|
|
|
|
(sha256->hiLen << 3);
|
|
|
|
|
sha256->loLen = sha256->loLen << 3;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT) {
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
/* store lengths */
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
|
2019-09-25 12:47:12 -07:00
|
|
|
ByteReverseWords(sha256->buffer, sha256->buffer,
|
2024-02-29 08:44:33 +10:00
|
|
|
WC_SHA256_PAD_SIZE);
|
2019-09-25 12:47:12 -07:00
|
|
|
}
|
2023-11-20 18:05:18 -08:00
|
|
|
/* ! 64-bit length ordering dependent on digest endian type ! */
|
2017-09-25 18:47:36 -07:00
|
|
|
XMEMCPY(&local[WC_SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
|
|
|
|
|
XMEMCPY(&local[WC_SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
|
2016-12-19 12:15:10 -08:00
|
|
|
sizeof(word32));
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
/* Only the ESP32-C3 with HW enabled may need pad size byte order reversal
|
|
|
|
|
* depending on HW or SW mode */
|
2023-12-18 17:35:43 -08:00
|
|
|
#if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP8684) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP32C3) || \
|
|
|
|
|
defined(CONFIG_IDF_TARGET_ESP32C6) \
|
|
|
|
|
) && \
|
|
|
|
|
defined(WOLFSSL_ESP32_CRYPT) && \
|
2023-11-20 18:05:18 -08:00
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2023-12-18 17:35:43 -08:00
|
|
|
/* For Espressif RISC-V Targets, we *may* need to reverse bytes
|
|
|
|
|
* depending on if HW is active or not. */
|
2023-11-20 18:05:18 -08:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_HW) {
|
|
|
|
|
#if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
|
|
|
|
|
ESP_LOGV(TAG, "Start: Reverse PAD SIZE Endianness.");
|
|
|
|
|
#endif
|
|
|
|
|
ByteReverseWords(
|
|
|
|
|
&sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* out */
|
|
|
|
|
&sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* in */
|
|
|
|
|
2 * sizeof(word32) /* byte count to reverse */
|
|
|
|
|
);
|
|
|
|
|
#if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
|
|
|
|
|
ESP_LOGV(TAG, "End: Reverse PAD SIZE Endianness.");
|
|
|
|
|
#endif
|
|
|
|
|
} /* end if (sha256->ctx.mode == ESP32_SHA_HW) */
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(FREESCALE_MMCAU_SHA) || \
|
|
|
|
|
(defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
|
2015-05-21 14:16:09 +09:00
|
|
|
/* Kinetis requires only these bytes reversed */
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2024-04-27 01:02:31 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
|
|
|
|
if (sha256->sha_method != SHA256_C)
|
|
|
|
|
#else
|
2019-09-25 12:47:12 -07:00
|
|
|
if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
2024-04-27 01:02:31 -05:00
|
|
|
#endif
|
2019-09-25 12:47:12 -07:00
|
|
|
{
|
|
|
|
|
ByteReverseWords(
|
|
|
|
|
&sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
|
|
|
|
|
&sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
|
|
|
|
|
2 * sizeof(word32));
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2019-09-25 12:47:12 -07:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_INIT) {
|
|
|
|
|
esp_sha_try_hw_lock(&sha256->ctx);
|
|
|
|
|
}
|
2023-11-20 18:05:18 -08:00
|
|
|
/* depending on architecture and ctx.mode value
|
|
|
|
|
* we may or may not need default digest */
|
2019-09-25 12:47:12 -07:00
|
|
|
if (sha256->ctx.mode == ESP32_SHA_SW) {
|
|
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
2022-07-06 09:30:49 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = esp_sha256_digest_process(sha256, 1);
|
|
|
|
|
}
|
2018-11-21 18:41:29 +09:00
|
|
|
#else
|
2019-09-25 12:47:12 -07:00
|
|
|
ret = XTRANSFORM(sha256, (const byte*)local);
|
2018-11-21 18:41:29 +09:00
|
|
|
#endif
|
2019-09-25 12:47:12 -07:00
|
|
|
|
|
|
|
|
return ret;
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#if !defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
|
2018-05-11 16:11:01 +10:00
|
|
|
int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
|
|
|
|
|
{
|
2018-05-25 09:01:44 +10:00
|
|
|
#ifdef LITTLE_ENDIAN_ORDER
|
|
|
|
|
word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-05-11 16:11:01 +10:00
|
|
|
if (sha256 == NULL || hash == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 09:01:44 +10:00
|
|
|
#ifdef LITTLE_ENDIAN_ORDER
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords((word32*)digest, (word32*)sha256->digest,
|
|
|
|
|
WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
}
|
2018-05-25 09:01:44 +10:00
|
|
|
XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
#else
|
2018-05-11 16:11:01 +10:00
|
|
|
XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
if (sha256 == NULL || hash == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 11:01:14 -08:00
|
|
|
#ifdef WOLF_CRYPTO_CB
|
2023-05-10 15:28:19 -07:00
|
|
|
#ifndef WOLF_CRYPTO_CB_FIND
|
|
|
|
|
if (sha256->devId != INVALID_DEVID)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
|
2024-06-08 16:39:53 -05:00
|
|
|
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
|
2023-05-10 15:28:19 -07:00
|
|
|
return ret;
|
|
|
|
|
/* fall-through when unavailable */
|
|
|
|
|
}
|
2019-01-02 14:34:26 -08:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
|
|
|
|
if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
|
|
|
|
|
#if defined(HAVE_INTEL_QA)
|
|
|
|
|
return IntelQaSymSha256(&sha256->asyncDev, hash, NULL,
|
2017-09-25 18:47:36 -07:00
|
|
|
WC_SHA256_DIGEST_SIZE);
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
|
|
|
|
|
|
|
|
|
ret = Sha256Final(sha256);
|
2023-04-19 15:10:22 +02:00
|
|
|
if (ret != 0) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
2023-04-19 15:10:22 +02:00
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
|
|
|
|
|
#if defined(LITTLE_ENDIAN_ORDER)
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords(sha256->digest, sha256->digest,
|
|
|
|
|
WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
}
|
2015-05-21 14:16:09 +09:00
|
|
|
#endif
|
2017-09-25 18:47:36 -07:00
|
|
|
XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return InitSha256(sha256); /* reset state */
|
|
|
|
|
}
|
2016-06-10 15:46:35 -07:00
|
|
|
|
2023-04-19 14:51:36 -07:00
|
|
|
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
|
2021-03-06 10:18:31 +09:00
|
|
|
/* Apply SHA256 transformation to the data */
|
|
|
|
|
/* @param sha a pointer to wc_Sha256 structure */
|
|
|
|
|
/* @param data data to be applied SHA256 transformation */
|
|
|
|
|
/* @return 0 on successful, otherwise non-zero on failure */
|
2024-02-29 08:44:33 +10:00
|
|
|
int wc_Sha256Transform(wc_Sha256* sha256, const unsigned char* data)
|
2021-01-28 12:02:35 +09:00
|
|
|
{
|
2024-02-29 08:44:33 +10:00
|
|
|
if (sha256 == NULL || data == NULL) {
|
2021-03-06 10:18:31 +09:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
2024-02-29 08:44:33 +10:00
|
|
|
return Transform_Sha256(sha256, data);
|
2021-01-28 12:02:35 +09:00
|
|
|
}
|
2024-02-29 08:44:33 +10:00
|
|
|
#endif /* OPENSSL_EXTRA || HAVE_CURL */
|
|
|
|
|
|
|
|
|
|
#if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
|
|
|
|
|
/* One block will be used from data.
|
|
|
|
|
* hash must be big enough to hold all of digest output.
|
|
|
|
|
*/
|
|
|
|
|
int wc_Sha256HashBlock(wc_Sha256* sha256, const unsigned char* data,
|
|
|
|
|
unsigned char* hash)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if ((sha256 == NULL) || (data == NULL)) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
2023-01-13 14:59:15 -08:00
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
ByteReverseWords(sha256->buffer, (word32*)data,
|
|
|
|
|
WC_SHA256_BLOCK_SIZE);
|
|
|
|
|
data = (unsigned char*)sha256->buffer;
|
|
|
|
|
}
|
|
|
|
|
ret = XTRANSFORM(sha256, data);
|
|
|
|
|
|
|
|
|
|
if ((ret == 0) && (hash != NULL)) {
|
|
|
|
|
if (!SHA256_REV_BYTES(&sha256->ctx)) {
|
|
|
|
|
XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
|
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
|
"mov 0x00(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x00(%[h])\n\t"
|
|
|
|
|
"mov 0x04(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x04(%[h])\n\t"
|
|
|
|
|
"mov 0x08(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x08(%[h])\n\t"
|
|
|
|
|
"mov 0x0c(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x0c(%[h])\n\t"
|
|
|
|
|
"mov 0x10(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x10(%[h])\n\t"
|
|
|
|
|
"mov 0x14(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x14(%[h])\n\t"
|
|
|
|
|
"mov 0x18(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x18(%[h])\n\t"
|
|
|
|
|
"mov 0x1c(%[d]), %%esi\n\t"
|
|
|
|
|
"movbe %%esi, 0x1c(%[h])\n\t"
|
|
|
|
|
:
|
|
|
|
|
: [d] "r" (sha256->digest), [h] "r" (hash)
|
|
|
|
|
: "memory", "esi"
|
|
|
|
|
);
|
|
|
|
|
#else
|
|
|
|
|
word32* hash32 = (word32*)hash;
|
|
|
|
|
word32* digest = (word32*)sha256->digest;
|
|
|
|
|
#if WOLFSSL_GENERAL_ALIGNMENT < 4
|
|
|
|
|
ALIGN16 word32 buf[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
|
|
|
|
|
|
|
|
|
|
if (((size_t)digest & 0x3) != 0) {
|
|
|
|
|
if (((size_t)hash32 & 0x3) != 0) {
|
|
|
|
|
XMEMCPY(buf, digest, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
hash32 = buf;
|
|
|
|
|
digest = buf;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
digest = hash32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (((size_t)hash32 & 0x3) != 0) {
|
|
|
|
|
hash32 = digest;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
hash32[0] = ByteReverseWord32(digest[0]);
|
|
|
|
|
hash32[1] = ByteReverseWord32(digest[1]);
|
|
|
|
|
hash32[2] = ByteReverseWord32(digest[2]);
|
|
|
|
|
hash32[3] = ByteReverseWord32(digest[3]);
|
|
|
|
|
hash32[4] = ByteReverseWord32(digest[4]);
|
|
|
|
|
hash32[5] = ByteReverseWord32(digest[5]);
|
|
|
|
|
hash32[6] = ByteReverseWord32(digest[6]);
|
|
|
|
|
hash32[7] = ByteReverseWord32(digest[7]);
|
|
|
|
|
#if WOLFSSL_GENERAL_ALIGNMENT < 4
|
|
|
|
|
if (hash != (byte*)hash32) {
|
|
|
|
|
XMEMCPY(hash, hash32, WC_SHA256_DIGEST_SIZE);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif /* WOLFSSL_X86_64_BUILD && USE_INTEL_SPEEDUP */
|
|
|
|
|
}
|
|
|
|
|
sha256->digest[0] = 0x6A09E667L;
|
|
|
|
|
sha256->digest[1] = 0xBB67AE85L;
|
|
|
|
|
sha256->digest[2] = 0x3C6EF372L;
|
|
|
|
|
sha256->digest[3] = 0xA54FF53AL;
|
|
|
|
|
sha256->digest[4] = 0x510E527FL;
|
|
|
|
|
sha256->digest[5] = 0x9B05688CL;
|
|
|
|
|
sha256->digest[6] = 0x1F83D9ABL;
|
|
|
|
|
sha256->digest[7] = 0x5BE0CD19L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_HAVE_LMS && !WOLFSSL_LMS_FULL_HASH */
|
2023-01-13 14:59:15 -08:00
|
|
|
#endif /* !WOLFSSL_KCAPI_HASH */
|
|
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
#endif /* XTRANSFORM */
|
|
|
|
|
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2016-11-10 15:52:26 +10:00
|
|
|
#ifdef WOLFSSL_SHA224
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#ifdef STM32_HASH_SHA2
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
/* Supports CubeMX HAL or Standard Peripheral Library */
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
if (sha224 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
2018-01-31 11:25:20 -08:00
|
|
|
(void)devId;
|
|
|
|
|
(void)heap;
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2020-08-26 09:44:32 -07:00
|
|
|
XMEMSET(sha224, 0, sizeof(wc_Sha224));
|
2018-01-31 11:25:20 -08:00
|
|
|
wc_Stm32_Hash_Init(&sha224->stmCtx);
|
2017-08-24 12:06:42 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
if (sha224 == NULL || (data == NULL && len > 0)) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
ret = wc_Stm32_Hash_Update(&sha224->stmCtx,
|
2022-06-06 12:11:06 -07:00
|
|
|
HASH_AlgoSelection_SHA224, data, len, WC_SHA224_BLOCK_SIZE);
|
2018-01-31 11:25:20 -08:00
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
2017-08-24 12:06:42 -07:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
|
2017-08-24 12:06:42 -07:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
if (sha224 == NULL || hash == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
2017-08-24 12:06:42 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
ret = wolfSSL_CryptHwMutexLock();
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
ret = wc_Stm32_Hash_Final(&sha224->stmCtx,
|
|
|
|
|
HASH_AlgoSelection_SHA224, hash, WC_SHA224_DIGEST_SIZE);
|
|
|
|
|
wolfSSL_CryptHwMutexUnLock();
|
2017-08-24 12:06:42 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
(void)wc_InitSha224(sha224); /* reset state */
|
2017-08-24 12:06:42 -07:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2021-11-01 16:18:59 -07:00
|
|
|
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
|
2021-08-20 12:59:41 -06:00
|
|
|
|
|
|
|
|
int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
|
|
|
|
|
{
|
|
|
|
|
if (sha224 == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
(void)devId;
|
2021-11-05 18:16:08 -05:00
|
|
|
|
2021-08-20 12:59:41 -06:00
|
|
|
return se050_hash_init(&sha224->se050Ctx, heap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
|
|
|
|
|
{
|
|
|
|
|
return se050_hash_update(&sha224->se050Ctx, data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
|
|
|
|
|
{
|
2021-11-05 18:16:08 -05:00
|
|
|
int ret = 0;
|
2021-08-20 12:59:41 -06:00
|
|
|
ret = se050_hash_final(&sha224->se050Ctx, hash, WC_SHA224_DIGEST_SIZE,
|
|
|
|
|
kAlgorithm_SSS_SHA224);
|
|
|
|
|
(void)wc_InitSha224(sha224);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2020-08-07 14:40:44 -06:00
|
|
|
#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
|
|
|
|
|
!defined(WOLFSSL_QNX_CAAM)
|
2017-12-14 11:46:20 -07:00
|
|
|
/* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
|
2018-07-18 17:26:25 -06:00
|
|
|
|
2018-08-03 15:04:37 -06:00
|
|
|
#elif defined(WOLFSSL_AFALG_HASH)
|
|
|
|
|
#error SHA224 currently not supported with AF_ALG enabled
|
2018-07-18 17:26:25 -06:00
|
|
|
|
2018-08-17 09:46:16 -06:00
|
|
|
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
|
|
|
|
|
|
2020-11-06 12:20:03 -08:00
|
|
|
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
|
|
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#elif defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
|
|
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
|
|
|
|
|
2022-09-15 16:18:44 -05:00
|
|
|
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
|
|
|
|
|
|
|
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
|
|
|
|
|
|
2023-08-09 09:08:44 +09:00
|
|
|
#elif defined(WOLFSSL_RENESAS_RSIP) && \
|
|
|
|
|
!defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
|
|
|
|
|
|
|
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
|
|
|
|
|
|
2017-08-24 12:06:42 -07:00
|
|
|
#else
|
|
|
|
|
|
2017-12-14 11:46:20 -07:00
|
|
|
#define NEED_SOFT_SHA224
|
|
|
|
|
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
static int InitSha224(wc_Sha224* sha224)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
sha224->digest[0] = 0xc1059ed8;
|
|
|
|
|
sha224->digest[1] = 0x367cd507;
|
|
|
|
|
sha224->digest[2] = 0x3070dd17;
|
|
|
|
|
sha224->digest[3] = 0xf70e5939;
|
|
|
|
|
sha224->digest[4] = 0xffc00b31;
|
|
|
|
|
sha224->digest[5] = 0x68581511;
|
|
|
|
|
sha224->digest[6] = 0x64f98fa7;
|
|
|
|
|
sha224->digest[7] = 0xbefa4fa4;
|
|
|
|
|
|
|
|
|
|
sha224->buffLen = 0;
|
|
|
|
|
sha224->loLen = 0;
|
|
|
|
|
sha224->hiLen = 0;
|
|
|
|
|
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
2024-04-27 01:02:31 -05:00
|
|
|
sha224->sha_method = 0;
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2024-04-27 01:02:31 -05:00
|
|
|
|
2022-09-30 16:19:40 +10:00
|
|
|
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
|
2020-01-06 15:23:45 +10:00
|
|
|
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
2016-12-19 12:15:10 -08:00
|
|
|
/* choose best Transform function under this runtime environment */
|
2024-04-29 14:02:44 -05:00
|
|
|
#ifdef WC_C_DYNAMIC_FALLBACK
|
2024-04-27 01:02:31 -05:00
|
|
|
Sha256_SetTransform(&sha224->sha_method);
|
2024-04-29 14:02:44 -05:00
|
|
|
#else
|
2017-07-20 12:24:38 +10:00
|
|
|
Sha256_SetTransform();
|
2024-04-29 14:02:44 -05:00
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-08-21 06:36:37 -07:00
|
|
|
sha224->flags = 0;
|
|
|
|
|
#endif
|
2022-04-05 14:45:18 -07:00
|
|
|
#ifdef WOLFSSL_HASH_KEEP
|
|
|
|
|
sha224->msg = NULL;
|
|
|
|
|
sha224->len = 0;
|
|
|
|
|
sha224->used = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
(!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
|
|
|
|
|
/* not to be confused with SHAS512_224 */
|
|
|
|
|
ret = esp_sha_init(&(sha224->ctx), WC_HASH_TYPE_SHA224);
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#endif
|
2017-08-24 12:06:42 -07:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#ifdef NEED_SOFT_SHA224
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
if (sha224 == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
|
|
|
|
sha224->heap = heap;
|
2020-08-26 09:44:32 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
sha224->W = NULL;
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
|
|
|
|
|
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224)
|
2023-04-19 15:10:22 +02:00
|
|
|
/* We know this is a fresh, uninitialized item, so set to INIT */
|
|
|
|
|
if (sha224->ctx.mode != ESP32_SHA_SW) {
|
|
|
|
|
ESP_LOGV(TAG, "Set sha224 ctx mode init to ESP32_SHA_SW. "
|
|
|
|
|
"Prior value: %d", sha224->ctx.mode);
|
|
|
|
|
}
|
|
|
|
|
/* no sha224 HW support is available, set to SW */
|
2023-11-20 18:05:18 -08:00
|
|
|
sha224->ctx.mode = ESP32_SHA_SW;
|
|
|
|
|
#else
|
|
|
|
|
/* We know this is a fresh, uninitialized item, so set to INIT */
|
|
|
|
|
sha224->ctx.mode = ESP32_SHA_INIT;
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
ret = InitSha224(sha224);
|
2023-04-19 15:10:22 +02:00
|
|
|
if (ret != 0) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
2023-04-19 15:10:22 +02:00
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
|
|
|
|
|
ret = wolfAsync_DevCtxInit(&sha224->asyncDev,
|
|
|
|
|
WOLFSSL_ASYNC_MARKER_SHA224, sha224->heap, devId);
|
|
|
|
|
#else
|
|
|
|
|
(void)devId;
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
2022-12-28 13:02:07 -08:00
|
|
|
#ifdef WOLFSSL_IMXRT1170_CAAM
|
|
|
|
|
ret = wc_CAAM_HashInit(&sha224->hndl, &sha224->ctx, WC_HASH_TYPE_SHA224);
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
(!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
|
2023-04-19 15:10:22 +02:00
|
|
|
if (sha224->ctx.mode != ESP32_SHA_INIT) {
|
|
|
|
|
ESP_LOGV("SHA224", "Set ctx mode from prior value: "
|
|
|
|
|
"%d", sha224->ctx.mode);
|
|
|
|
|
}
|
|
|
|
|
/* We know this is a fresh, uninitialized item, so set to INIT */
|
|
|
|
|
sha224->ctx.mode = ESP32_SHA_INIT;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
2024-02-29 08:44:33 +10:00
|
|
|
if (sha224 == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL && len == 0) {
|
|
|
|
|
/* valid, but do nothing */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (data == NULL) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
|
|
|
|
|
if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
|
|
|
|
|
#if defined(HAVE_INTEL_QA)
|
|
|
|
|
return IntelQaSymSha224(&sha224->asyncDev, NULL, data, len);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
|
|
|
|
|
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
|
2023-04-19 15:10:22 +02:00
|
|
|
sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
ret = Sha256Update((wc_Sha256*)sha224, data, len);
|
2016-11-10 15:52:26 +10:00
|
|
|
|
|
|
|
|
return ret;
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (sha224 == NULL || hash == NULL) {
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
|
|
|
|
|
if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
|
|
|
|
|
#if defined(HAVE_INTEL_QA)
|
|
|
|
|
return IntelQaSymSha224(&sha224->asyncDev, hash, NULL,
|
2017-09-25 18:47:36 -07:00
|
|
|
WC_SHA224_DIGEST_SIZE);
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
|
|
|
|
|
2023-12-18 17:35:43 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
( !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) )
|
2023-11-20 18:05:18 -08:00
|
|
|
|
2023-12-18 17:35:43 -08:00
|
|
|
/* nothing enabled here for RISC-V C2/C3/C6 success */
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
ret = Sha256Final((wc_Sha256*)sha224);
|
2016-12-19 12:15:10 -08:00
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2018-01-31 11:25:20 -08:00
|
|
|
#if defined(LITTLE_ENDIAN_ORDER)
|
2024-02-29 08:44:33 +10:00
|
|
|
if (SHA256_REV_BYTES(&sha224->ctx)) {
|
2023-11-20 18:05:18 -08:00
|
|
|
ByteReverseWords(sha224->digest,
|
|
|
|
|
sha224->digest,
|
|
|
|
|
WC_SHA224_DIGEST_SIZE);
|
|
|
|
|
}
|
2016-11-10 15:52:26 +10:00
|
|
|
#endif
|
2017-09-25 18:47:36 -07:00
|
|
|
XMEMCPY(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
return InitSha224(sha224); /* reset state */
|
|
|
|
|
}
|
2017-12-14 11:46:20 -07:00
|
|
|
#endif /* end of SHA224 software implementation */
|
|
|
|
|
|
|
|
|
|
int wc_InitSha224(wc_Sha224* sha224)
|
|
|
|
|
{
|
2021-12-08 23:59:19 +00:00
|
|
|
int devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
#ifdef WOLF_CRYPTO_CB
|
|
|
|
|
devId = wc_CryptoCb_DefaultDevID();
|
|
|
|
|
#endif
|
|
|
|
|
return wc_InitSha224_ex(sha224, NULL, devId);
|
2017-12-14 11:46:20 -07:00
|
|
|
}
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
void wc_Sha224Free(wc_Sha224* sha224)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
if (sha224 == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
if (sha224->W != NULL) {
|
|
|
|
|
ForceZero(sha224->W, sizeof(word32) * WC_SHA224_BLOCK_SIZE);
|
|
|
|
|
XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
|
|
|
|
|
sha224->W = NULL;
|
|
|
|
|
}
|
2018-07-17 09:17:39 +10:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
|
|
|
|
|
wolfAsync_DevCtxFree(&sha224->asyncDev, WOLFSSL_ASYNC_MARKER_SHA224);
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
2018-07-24 11:09:30 -07:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_PIC32MZ_HASH
|
|
|
|
|
wc_Sha256Pic32Free(sha224);
|
|
|
|
|
#endif
|
2021-08-19 11:25:59 +10:00
|
|
|
#if defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
KcapiHashFree(&sha224->kcapi);
|
|
|
|
|
#endif
|
2023-09-29 16:22:16 +09:00
|
|
|
#if defined(WOLFSSL_RENESAS_RX64_HASH)
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
if (sha224->msg != NULL) {
|
|
|
|
|
ForceZero(sha224->msg, sha224->len);
|
|
|
|
|
XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
sha224->msg = NULL;
|
|
|
|
|
}
|
2022-09-15 16:18:44 -05:00
|
|
|
#endif
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
ForceZero(sha224, sizeof(*sha224));
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2023-11-20 18:05:18 -08:00
|
|
|
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
|
|
|
|
|
#endif /* WOLFSSL_SHA224 */
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_InitSha256(wc_Sha256* sha256)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
2021-12-08 23:59:19 +00:00
|
|
|
int devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
#ifdef WOLF_CRYPTO_CB
|
|
|
|
|
devId = wc_CryptoCb_DefaultDevID();
|
|
|
|
|
#endif
|
|
|
|
|
return wc_InitSha256_ex(sha256, NULL, devId);
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
void wc_Sha256Free(wc_Sha256* sha256)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
if (sha256 == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_ESP32) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
|
|
|
|
esp_sha_release_unfinished_lock(&sha256->ctx);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
if (sha256->W != NULL) {
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
ForceZero(sha256->W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
|
2018-08-21 09:03:11 +10:00
|
|
|
XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
|
2018-07-17 09:17:39 +10:00
|
|
|
sha256->W = NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
|
|
|
|
wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
|
|
|
|
|
#endif /* WOLFSSL_ASYNC_CRYPT */
|
2018-07-24 11:09:30 -07:00
|
|
|
#ifdef WOLFSSL_PIC32MZ_HASH
|
|
|
|
|
wc_Sha256Pic32Free(sha256);
|
|
|
|
|
#endif
|
2018-07-18 17:26:25 -06:00
|
|
|
#if defined(WOLFSSL_AFALG_HASH)
|
|
|
|
|
if (sha256->alFd > 0) {
|
|
|
|
|
close(sha256->alFd);
|
2018-08-03 16:46:15 -06:00
|
|
|
sha256->alFd = -1; /* avoid possible double close on socket */
|
2018-07-18 17:26:25 -06:00
|
|
|
}
|
|
|
|
|
if (sha256->rdFd > 0) {
|
|
|
|
|
close(sha256->rdFd);
|
2018-08-03 16:46:15 -06:00
|
|
|
sha256->rdFd = -1; /* avoid possible double close on socket */
|
2018-07-18 17:26:25 -06:00
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_AFALG_HASH */
|
2018-08-17 09:46:16 -06:00
|
|
|
#ifdef WOLFSSL_DEVCRYPTO_HASH
|
|
|
|
|
wc_DevCryptoFree(&sha256->ctx);
|
|
|
|
|
#endif /* WOLFSSL_DEVCRYPTO */
|
2018-08-24 10:24:53 -06:00
|
|
|
#if (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
|
2019-08-29 13:42:18 +09:00
|
|
|
(defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
|
2023-06-17 11:57:11 +09:00
|
|
|
((defined(WOLFSSL_RENESAS_TSIP_TLS) || \
|
|
|
|
|
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
|
2021-12-10 19:01:01 +09:00
|
|
|
!defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) || \
|
2023-09-29 16:22:16 +09:00
|
|
|
(defined(WOLFSSL_RENESAS_SCEPROTECT) && \
|
2023-08-12 17:28:24 +09:00
|
|
|
!defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
|
2023-02-21 15:26:10 -06:00
|
|
|
defined(WOLFSSL_RENESAS_RX64_HASH) || \
|
2021-12-08 23:59:19 +00:00
|
|
|
defined(WOLFSSL_HASH_KEEP)
|
2021-11-29 23:58:39 -06:00
|
|
|
|
2018-09-19 14:29:33 -06:00
|
|
|
if (sha256->msg != NULL) {
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
ForceZero(sha256->msg, sha256->len);
|
2018-09-19 14:29:33 -06:00
|
|
|
XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
sha256->msg = NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-07-07 16:11:54 +01:00
|
|
|
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
|
|
|
|
|
se050_hash_free(&sha256->se050Ctx);
|
|
|
|
|
#endif
|
2021-08-19 11:25:59 +10:00
|
|
|
#if defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
KcapiHashFree(&sha256->kcapi);
|
|
|
|
|
#endif
|
2020-09-22 12:56:09 +02:00
|
|
|
#ifdef WOLFSSL_IMXRT_DCP
|
|
|
|
|
DCPSha256Free(sha256);
|
|
|
|
|
#endif
|
2022-09-21 03:21:33 -04:00
|
|
|
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
|
|
|
|
|
wc_MAXQ10XX_Sha256Free(sha256);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-06 13:05:39 -04:00
|
|
|
#ifdef HAVE_ARIA
|
|
|
|
|
if (sha256->hSession != NULL) {
|
|
|
|
|
MC_CloseSession(sha256->hSession);
|
|
|
|
|
sha256->hSession = NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-07-21 09:08:10 +02:00
|
|
|
/* Espressif embedded hardware acceleration specific: */
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2022-07-21 09:08:10 +02:00
|
|
|
if (sha256->ctx.lockDepth > 0) {
|
|
|
|
|
/* probably due to unclean shutdown, error, or other problem.
|
|
|
|
|
*
|
|
|
|
|
* if you find yourself here, code needs to be cleaned up to
|
|
|
|
|
* properly release hardware. this init is only for handling
|
|
|
|
|
* the unexpected. by the time free is called, the hardware
|
|
|
|
|
* should have already been released (lockDepth = 0)
|
|
|
|
|
*/
|
2023-04-19 15:10:22 +02:00
|
|
|
(void)InitSha256(sha256); /* unlock mutex, set mode to ESP32_SHA_INIT */
|
|
|
|
|
ESP_LOGV(TAG, "Alert: hardware unlock needed in wc_Sha256Free.");
|
2022-07-21 09:08:10 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-04-19 15:10:22 +02:00
|
|
|
ESP_LOGV(TAG, "Hardware unlock not needed in wc_Sha256Free.");
|
2022-07-21 09:08:10 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
2023-09-06 14:53:19 -05:00
|
|
|
ForceZero(sha256, sizeof(*sha256));
|
2023-11-20 18:05:18 -08:00
|
|
|
} /* wc_Sha256Free */
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
|
2021-12-08 23:59:19 +00:00
|
|
|
#ifdef WOLFSSL_HASH_KEEP
|
|
|
|
|
/* Some hardware have issues with update, this function stores the data to be
|
|
|
|
|
* hashed into an array. Once ready, the Final operation is called on all of the
|
|
|
|
|
* data to be hashed at once.
|
|
|
|
|
* returns 0 on success
|
|
|
|
|
*/
|
|
|
|
|
int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz)
|
|
|
|
|
{
|
2022-04-06 15:08:19 -06:00
|
|
|
return _wc_Hash_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in,
|
2021-12-08 23:59:19 +00:00
|
|
|
inSz, sha256->heap);
|
|
|
|
|
}
|
|
|
|
|
#ifdef WOLFSSL_SHA224
|
|
|
|
|
int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
|
|
|
|
|
{
|
2022-04-06 15:08:19 -06:00
|
|
|
return _wc_Hash_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in,
|
2021-12-08 23:59:19 +00:00
|
|
|
inSz, sha224->heap);
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_SHA224 */
|
|
|
|
|
#endif /* WOLFSSL_HASH_KEEP */
|
|
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif /* !WOLFSSL_TI_HASH */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WOLFSSL_TI_HASH
|
2023-08-09 09:08:44 +09:00
|
|
|
#if !defined(WOLFSSL_RENESAS_RX64_HASH) && \
|
|
|
|
|
(!defined(WOLFSSL_RENESAS_RSIP) || \
|
|
|
|
|
defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
|
2016-12-19 12:15:10 -08:00
|
|
|
#ifdef WOLFSSL_SHA224
|
2021-08-19 11:25:59 +10:00
|
|
|
|
|
|
|
|
#if defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
|
2021-12-22 05:20:59 +01:00
|
|
|
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
2021-08-19 11:25:59 +10:00
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
2022-10-03 12:52:11 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
wc_Sha224* tmpSha224;
|
|
|
|
|
#else
|
|
|
|
|
wc_Sha224 tmpSha224[1];
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
if (sha224 == NULL || hash == NULL) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return BAD_FUNC_ARG;
|
2022-10-03 12:52:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
tmpSha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
|
|
|
|
|
DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
if (tmpSha224 == NULL) {
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
ret = wc_Sha224Copy(sha224, tmpSha224);
|
2016-12-19 12:15:10 -08:00
|
|
|
if (ret == 0) {
|
2022-10-03 12:52:11 -07:00
|
|
|
ret = wc_Sha224Final(tmpSha224, hash);
|
|
|
|
|
wc_Sha224Free(tmpSha224);
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2022-10-03 12:52:11 -07:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
XFREE(tmpSha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2022-10-03 12:52:11 -07:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
2023-04-19 15:10:22 +02:00
|
|
|
int ret = 0; /* assume success unless proven otherwise */
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
if (src == NULL || dst == NULL) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return BAD_FUNC_ARG;
|
2023-04-19 15:10:22 +02:00
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
XMEMCPY(dst, src, sizeof(wc_Sha224));
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
dst->W = NULL;
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-06-01 13:54:36 -07:00
|
|
|
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
|
|
|
|
|
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
|
|
|
|
|
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
|
2020-11-06 12:20:03 -08:00
|
|
|
#endif
|
|
|
|
|
|
2022-03-03 16:33:18 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
|
2016-12-19 12:15:10 -08:00
|
|
|
ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
(!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
|
|
|
|
|
/* regardless of any other settings, there's no SHA-224 HW on ESP32 */
|
|
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
ret = esp_sha224_ctx_copy(src, dst);
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-02-05 18:28:54 -08:00
|
|
|
dst->flags |= WC_HASH_FLAG_ISCOPY;
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2021-12-08 23:59:19 +00:00
|
|
|
#if defined(WOLFSSL_HASH_KEEP)
|
|
|
|
|
if (src->msg != NULL) {
|
|
|
|
|
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
|
|
|
|
|
DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
if (dst->msg == NULL)
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
XMEMCPY(dst->msg, src->msg, src->len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-02-03 15:51:57 -08:00
|
|
|
|
2021-08-19 11:25:59 +10:00
|
|
|
#endif /* WOLFSSL_KCAPI_HASH && !WOLFSSL_NO_KCAPI_SHA224 */
|
|
|
|
|
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-02-03 15:51:57 -08:00
|
|
|
int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags)
|
|
|
|
|
{
|
|
|
|
|
if (sha224) {
|
|
|
|
|
sha224->flags = flags;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags)
|
|
|
|
|
{
|
|
|
|
|
if (sha224 && flags) {
|
|
|
|
|
*flags = sha224->flags;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-10 15:52:26 +10:00
|
|
|
#endif /* WOLFSSL_SHA224 */
|
2022-09-15 16:18:44 -05:00
|
|
|
#endif /* WOLFSSL_RENESAS_RX64_HASH */
|
2016-11-10 15:52:26 +10:00
|
|
|
|
2018-07-18 17:26:25 -06:00
|
|
|
#ifdef WOLFSSL_AFALG_HASH
|
|
|
|
|
/* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
|
2018-08-17 09:46:16 -06:00
|
|
|
|
|
|
|
|
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2023-06-17 11:57:11 +09:00
|
|
|
#elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
|
|
|
|
|
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
|
2019-08-19 17:32:22 +09:00
|
|
|
!defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
|
2019-09-25 12:47:12 -07:00
|
|
|
|
2019-08-19 17:32:22 +09:00
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
|
2021-10-30 20:21:21 +09:00
|
|
|
|
2023-08-09 09:08:44 +09:00
|
|
|
#elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP))\
|
|
|
|
|
&& !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
|
2021-10-30 20:21:21 +09:00
|
|
|
|
2023-08-09 09:08:44 +09:00
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
|
2021-10-30 20:21:21 +09:00
|
|
|
|
2020-06-01 13:23:50 +02:00
|
|
|
#elif defined(WOLFSSL_PSOC6_CRYPTO)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
|
2020-09-18 11:46:08 +02:00
|
|
|
#elif defined(WOLFSSL_IMXRT_DCP)
|
2020-09-23 09:26:34 +02:00
|
|
|
/* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
|
2021-08-19 11:25:59 +10:00
|
|
|
#elif defined(WOLFSSL_KCAPI_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
|
|
|
|
|
|
2021-12-22 05:20:59 +01:00
|
|
|
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
|
2022-09-15 16:18:44 -05:00
|
|
|
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
|
|
|
|
|
/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
|
2024-07-08 08:51:24 -06:00
|
|
|
#elif defined(MAX3266X_SHA)
|
|
|
|
|
/* Implemented in wolfcrypt/src/port/maxim/max3266x.c */
|
2018-07-18 17:26:25 -06:00
|
|
|
#else
|
|
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret;
|
2022-10-03 12:52:11 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
wc_Sha256* tmpSha256;
|
|
|
|
|
#else
|
|
|
|
|
wc_Sha256 tmpSha256[1];
|
|
|
|
|
#endif
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
if (sha256 == NULL || hash == NULL) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return BAD_FUNC_ARG;
|
2022-10-03 12:52:11 -07:00
|
|
|
}
|
2015-05-28 20:40:53 +09:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
tmpSha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
|
|
|
|
|
DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
if (tmpSha256 == NULL) {
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-07-06 09:30:49 -07:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
ret = wc_Sha256Copy(sha256, tmpSha256);
|
2016-12-19 12:15:10 -08:00
|
|
|
if (ret == 0) {
|
2022-10-03 12:52:11 -07:00
|
|
|
ret = wc_Sha256Final(tmpSha256, hash);
|
2024-07-24 16:06:04 -07:00
|
|
|
wc_Sha256Free(tmpSha256);
|
2016-12-19 12:15:10 -08:00
|
|
|
}
|
2022-07-06 09:30:49 -07:00
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2022-10-03 12:52:11 -07:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK
|
|
|
|
|
XFREE(tmpSha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
2022-07-06 09:30:49 -07:00
|
|
|
#endif
|
2022-10-03 12:52:11 -07:00
|
|
|
|
2016-12-19 12:15:10 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2017-09-25 18:47:36 -07:00
|
|
|
int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
|
2016-12-19 12:15:10 -08:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2015-05-21 14:16:09 +09:00
|
|
|
|
2023-04-19 15:10:22 +02:00
|
|
|
if (src == NULL || dst == NULL) {
|
2016-12-19 12:15:10 -08:00
|
|
|
return BAD_FUNC_ARG;
|
2023-04-19 15:10:22 +02:00
|
|
|
}
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2017-09-25 18:47:36 -07:00
|
|
|
XMEMCPY(dst, src, sizeof(wc_Sha256));
|
2022-09-21 03:21:33 -04:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
|
|
|
|
|
wc_MAXQ10XX_Sha256Copy(src);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-17 09:17:39 +10:00
|
|
|
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
|
|
|
|
dst->W = NULL;
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
2023-06-01 13:54:36 -07:00
|
|
|
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
|
|
|
|
|
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
|
|
|
|
|
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
|
2020-11-06 12:20:03 -08:00
|
|
|
#endif
|
|
|
|
|
|
2022-03-03 16:33:18 -08:00
|
|
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
|
2016-12-19 12:15:10 -08:00
|
|
|
ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2017-08-02 08:42:04 -07:00
|
|
|
#ifdef WOLFSSL_PIC32MZ_HASH
|
|
|
|
|
ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
|
|
|
|
|
#endif
|
2022-07-06 09:30:49 -07:00
|
|
|
|
2023-11-20 18:05:18 -08:00
|
|
|
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
|
|
|
|
|
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
|
2023-04-19 15:10:22 +02:00
|
|
|
esp_sha256_ctx_copy(src, dst);
|
2018-11-21 18:41:29 +09:00
|
|
|
#endif
|
2022-07-06 09:30:49 -07:00
|
|
|
|
2023-07-06 13:05:39 -04:00
|
|
|
#ifdef HAVE_ARIA
|
|
|
|
|
dst->hSession = NULL;
|
|
|
|
|
if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-02-05 18:28:54 -08:00
|
|
|
dst->flags |= WC_HASH_FLAG_ISCOPY;
|
|
|
|
|
#endif
|
2023-04-19 15:10:22 +02:00
|
|
|
|
2021-12-08 23:59:19 +00:00
|
|
|
#if defined(WOLFSSL_HASH_KEEP)
|
|
|
|
|
if (src->msg != NULL) {
|
|
|
|
|
dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
|
|
|
|
if (dst->msg == NULL)
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
XMEMCPY(dst->msg, src->msg, src->len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-10-18 11:04:45 -06:00
|
|
|
#endif
|
2019-02-03 15:51:57 -08:00
|
|
|
|
2021-10-14 14:38:57 -07:00
|
|
|
#ifdef WOLFSSL_HASH_FLAGS
|
2019-02-03 15:51:57 -08:00
|
|
|
int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags)
|
|
|
|
|
{
|
|
|
|
|
if (sha256) {
|
|
|
|
|
sha256->flags = flags;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags)
|
|
|
|
|
{
|
|
|
|
|
if (sha256 && flags) {
|
|
|
|
|
*flags = sha256->flags;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-07-18 17:26:25 -06:00
|
|
|
#endif
|
2016-12-19 12:15:10 -08:00
|
|
|
#endif /* !WOLFSSL_TI_HASH */
|
|
|
|
|
|
|
|
|
|
#endif /* NO_SHA256 */
|