wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* sha256.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 23 #include <config.h>
wolfSSL 15:117db924cf7c 24 #endif
wolfSSL 15:117db924cf7c 25
wolfSSL 15:117db924cf7c 26 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 27
wolfSSL 16:8e0d178b1d1e 28 /*
wolfSSL 16:8e0d178b1d1e 29 * SHA256 Build Options:
wolfSSL 16:8e0d178b1d1e 30 * USE_SLOW_SHA256: Reduces code size by not partially unrolling
wolfSSL 16:8e0d178b1d1e 31 (~2KB smaller and ~25% slower) (default OFF)
wolfSSL 16:8e0d178b1d1e 32 * WOLFSSL_SHA256_BY_SPEC: Uses the Ch/Maj based on SHA256 specification
wolfSSL 16:8e0d178b1d1e 33 (default ON)
wolfSSL 16:8e0d178b1d1e 34 * WOLFSSL_SHA256_ALT_CH_MAJ: Alternate Ch/Maj that is easier for compilers to
wolfSSL 16:8e0d178b1d1e 35 optimize and recognize as SHA256 (default OFF)
wolfSSL 16:8e0d178b1d1e 36 * SHA256_MANY_REGISTERS: A SHA256 version that keeps all data in registers
wolfSSL 16:8e0d178b1d1e 37 and partial unrolled (default OFF)
wolfSSL 16:8e0d178b1d1e 38 */
wolfSSL 16:8e0d178b1d1e 39
wolfSSL 16:8e0d178b1d1e 40 /* Default SHA256 to use Ch/Maj based on specification */
wolfSSL 16:8e0d178b1d1e 41 #if !defined(WOLFSSL_SHA256_BY_SPEC) && !defined(WOLFSSL_SHA256_ALT_CH_MAJ)
wolfSSL 16:8e0d178b1d1e 42 #define WOLFSSL_SHA256_BY_SPEC
wolfSSL 16:8e0d178b1d1e 43 #endif
wolfSSL 16:8e0d178b1d1e 44
wolfSSL 16:8e0d178b1d1e 45
wolfSSL 15:117db924cf7c 46 #if !defined(NO_SHA256) && !defined(WOLFSSL_ARMASM)
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 49 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 50
wolfSSL 15:117db924cf7c 51 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
wolfSSL 15:117db924cf7c 52 #define FIPS_NO_WRAPPERS
wolfSSL 15:117db924cf7c 53
wolfSSL 15:117db924cf7c 54 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 55 #pragma code_seg(".fipsA$d")
wolfSSL 15:117db924cf7c 56 #pragma const_seg(".fipsB$d")
wolfSSL 15:117db924cf7c 57 #endif
wolfSSL 15:117db924cf7c 58 #endif
wolfSSL 15:117db924cf7c 59
wolfSSL 15:117db924cf7c 60 #include <wolfssl/wolfcrypt/sha256.h>
wolfSSL 15:117db924cf7c 61 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 62 #include <wolfssl/wolfcrypt/cpuid.h>
wolfSSL 16:8e0d178b1d1e 63 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 16:8e0d178b1d1e 64
wolfSSL 16:8e0d178b1d1e 65 #ifdef WOLF_CRYPTO_CB
wolfSSL 16:8e0d178b1d1e 66 #include <wolfssl/wolfcrypt/cryptocb.h>
wolfSSL 16:8e0d178b1d1e 67 #endif
wolfSSL 15:117db924cf7c 68
wolfSSL 15:117db924cf7c 69 /* fips wrapper calls, user can call direct */
wolfSSL 15:117db924cf7c 70 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 71 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 72
wolfSSL 15:117db924cf7c 73 int wc_InitSha256(wc_Sha256* sha)
wolfSSL 15:117db924cf7c 74 {
wolfSSL 15:117db924cf7c 75 if (sha == NULL) {
wolfSSL 15:117db924cf7c 76 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 77 }
wolfSSL 15:117db924cf7c 78 return InitSha256_fips(sha);
wolfSSL 15:117db924cf7c 79 }
wolfSSL 15:117db924cf7c 80 int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId)
wolfSSL 15:117db924cf7c 81 {
wolfSSL 15:117db924cf7c 82 (void)heap;
wolfSSL 15:117db924cf7c 83 (void)devId;
wolfSSL 15:117db924cf7c 84 if (sha == NULL) {
wolfSSL 15:117db924cf7c 85 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 86 }
wolfSSL 15:117db924cf7c 87 return InitSha256_fips(sha);
wolfSSL 15:117db924cf7c 88 }
wolfSSL 15:117db924cf7c 89 int wc_Sha256Update(wc_Sha256* sha, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 90 {
wolfSSL 15:117db924cf7c 91 if (sha == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 92 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 93 }
wolfSSL 15:117db924cf7c 94
wolfSSL 15:117db924cf7c 95 if (data == NULL && len == 0) {
wolfSSL 15:117db924cf7c 96 /* valid, but do nothing */
wolfSSL 15:117db924cf7c 97 return 0;
wolfSSL 15:117db924cf7c 98 }
wolfSSL 15:117db924cf7c 99
wolfSSL 15:117db924cf7c 100 return Sha256Update_fips(sha, data, len);
wolfSSL 15:117db924cf7c 101 }
wolfSSL 15:117db924cf7c 102 int wc_Sha256Final(wc_Sha256* sha, byte* out)
wolfSSL 15:117db924cf7c 103 {
wolfSSL 15:117db924cf7c 104 if (sha == NULL || out == NULL) {
wolfSSL 15:117db924cf7c 105 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 106 }
wolfSSL 15:117db924cf7c 107 return Sha256Final_fips(sha, out);
wolfSSL 15:117db924cf7c 108 }
wolfSSL 15:117db924cf7c 109 void wc_Sha256Free(wc_Sha256* sha)
wolfSSL 15:117db924cf7c 110 {
wolfSSL 15:117db924cf7c 111 (void)sha;
wolfSSL 15:117db924cf7c 112 /* Not supported in FIPS */
wolfSSL 15:117db924cf7c 113 }
wolfSSL 15:117db924cf7c 114
wolfSSL 15:117db924cf7c 115 #else /* else build without fips, or for FIPS v2 */
wolfSSL 15:117db924cf7c 116
wolfSSL 15:117db924cf7c 117
wolfSSL 15:117db924cf7c 118 #if defined(WOLFSSL_TI_HASH)
wolfSSL 15:117db924cf7c 119 /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
wolfSSL 16:8e0d178b1d1e 120 #elif defined(WOLFSSL_CRYPTOCELL)
wolfSSL 16:8e0d178b1d1e 121 /* wc_port.c includes wolfcrypt/src/port/arm/cryptoCellHash.c */
wolfSSL 15:117db924cf7c 122 #else
wolfSSL 15:117db924cf7c 123
wolfSSL 15:117db924cf7c 124 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 125
wolfSSL 15:117db924cf7c 126 #ifdef NO_INLINE
wolfSSL 15:117db924cf7c 127 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 15:117db924cf7c 128 #else
wolfSSL 15:117db924cf7c 129 #define WOLFSSL_MISC_INCLUDED
wolfSSL 15:117db924cf7c 130 #include <wolfcrypt/src/misc.c>
wolfSSL 15:117db924cf7c 131 #endif
wolfSSL 15:117db924cf7c 132
wolfSSL 16:8e0d178b1d1e 133 #ifdef WOLFSSL_DEVCRYPTO_HASH
wolfSSL 16:8e0d178b1d1e 134 #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
wolfSSL 16:8e0d178b1d1e 135 #endif
wolfSSL 16:8e0d178b1d1e 136
wolfSSL 16:8e0d178b1d1e 137
wolfSSL 15:117db924cf7c 138
wolfSSL 15:117db924cf7c 139 #if defined(USE_INTEL_SPEEDUP)
wolfSSL 15:117db924cf7c 140 #if defined(__GNUC__) && ((__GNUC__ < 4) || \
wolfSSL 15:117db924cf7c 141 (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
wolfSSL 16:8e0d178b1d1e 142 #undef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 143 #define NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 144 #endif
wolfSSL 15:117db924cf7c 145 #if defined(__clang__) && ((__clang_major__ < 3) || \
wolfSSL 15:117db924cf7c 146 (__clang_major__ == 3 && __clang_minor__ <= 5))
wolfSSL 15:117db924cf7c 147 #define NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 148 #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
wolfSSL 15:117db924cf7c 149 #undef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 150 #endif
wolfSSL 15:117db924cf7c 151
wolfSSL 15:117db924cf7c 152 #define HAVE_INTEL_AVX1
wolfSSL 15:117db924cf7c 153 #ifndef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 154 #define HAVE_INTEL_AVX2
wolfSSL 15:117db924cf7c 155 #endif
wolfSSL 15:117db924cf7c 156 #endif /* USE_INTEL_SPEEDUP */
wolfSSL 15:117db924cf7c 157
wolfSSL 15:117db924cf7c 158 #if defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 159 #define HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 160 #endif
wolfSSL 15:117db924cf7c 161
wolfSSL 15:117db924cf7c 162
wolfSSL 15:117db924cf7c 163 #if !defined(WOLFSSL_PIC32MZ_HASH) && !defined(STM32_HASH_SHA2) && \
wolfSSL 16:8e0d178b1d1e 164 (!defined(WOLFSSL_IMX6_CAAM) || defined(NO_IMX6_CAAM_HASH)) && \
wolfSSL 16:8e0d178b1d1e 165 !defined(WOLFSSL_AFALG_HASH) && !defined(WOLFSSL_DEVCRYPTO_HASH) && \
wolfSSL 16:8e0d178b1d1e 166 (!defined(WOLFSSL_ESP32WROOM32_CRYPT) || defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)) && \
wolfSSL 16:8e0d178b1d1e 167 (!defined(WOLFSSL_RENESAS_TSIP_CRYPT) || defined(NO_WOLFSSL_RENESAS_TSIP_HASH))
wolfSSL 16:8e0d178b1d1e 168
wolfSSL 15:117db924cf7c 169 static int InitSha256(wc_Sha256* sha256)
wolfSSL 15:117db924cf7c 170 {
wolfSSL 15:117db924cf7c 171 int ret = 0;
wolfSSL 15:117db924cf7c 172
wolfSSL 15:117db924cf7c 173 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 174 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 175
wolfSSL 15:117db924cf7c 176 XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
wolfSSL 15:117db924cf7c 177 sha256->digest[0] = 0x6A09E667L;
wolfSSL 15:117db924cf7c 178 sha256->digest[1] = 0xBB67AE85L;
wolfSSL 15:117db924cf7c 179 sha256->digest[2] = 0x3C6EF372L;
wolfSSL 15:117db924cf7c 180 sha256->digest[3] = 0xA54FF53AL;
wolfSSL 15:117db924cf7c 181 sha256->digest[4] = 0x510E527FL;
wolfSSL 15:117db924cf7c 182 sha256->digest[5] = 0x9B05688CL;
wolfSSL 15:117db924cf7c 183 sha256->digest[6] = 0x1F83D9ABL;
wolfSSL 15:117db924cf7c 184 sha256->digest[7] = 0x5BE0CD19L;
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186 sha256->buffLen = 0;
wolfSSL 15:117db924cf7c 187 sha256->loLen = 0;
wolfSSL 15:117db924cf7c 188 sha256->hiLen = 0;
wolfSSL 16:8e0d178b1d1e 189 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 190 sha256->flags = 0;
wolfSSL 16:8e0d178b1d1e 191 #endif
wolfSSL 15:117db924cf7c 192
wolfSSL 15:117db924cf7c 193 return ret;
wolfSSL 15:117db924cf7c 194 }
wolfSSL 15:117db924cf7c 195 #endif
wolfSSL 15:117db924cf7c 196
wolfSSL 15:117db924cf7c 197
wolfSSL 15:117db924cf7c 198 /* Hardware Acceleration */
wolfSSL 15:117db924cf7c 199 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 200
wolfSSL 15:117db924cf7c 201 /* in case intel instructions aren't available, plus we need the K[] global */
wolfSSL 15:117db924cf7c 202 #define NEED_SOFT_SHA256
wolfSSL 15:117db924cf7c 203
wolfSSL 15:117db924cf7c 204 /*****
wolfSSL 15:117db924cf7c 205 Intel AVX1/AVX2 Macro Control Structure
wolfSSL 15:117db924cf7c 206
wolfSSL 15:117db924cf7c 207 #define HAVE_INTEL_AVX1
wolfSSL 15:117db924cf7c 208 #define HAVE_INTEL_AVX2
wolfSSL 15:117db924cf7c 209
wolfSSL 15:117db924cf7c 210 #define HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 211
wolfSSL 15:117db924cf7c 212
wolfSSL 15:117db924cf7c 213 int InitSha256(wc_Sha256* sha256) {
wolfSSL 15:117db924cf7c 214 Save/Recover XMM, YMM
wolfSSL 15:117db924cf7c 215 ...
wolfSSL 15:117db924cf7c 216 }
wolfSSL 15:117db924cf7c 217
wolfSSL 15:117db924cf7c 218 #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 219 Transform_Sha256(); Function prototype
wolfSSL 15:117db924cf7c 220 #else
wolfSSL 15:117db924cf7c 221 Transform_Sha256() { }
wolfSSL 15:117db924cf7c 222 int Sha256Final() {
wolfSSL 15:117db924cf7c 223 Save/Recover XMM, YMM
wolfSSL 15:117db924cf7c 224 ...
wolfSSL 15:117db924cf7c 225 }
wolfSSL 15:117db924cf7c 226 #endif
wolfSSL 15:117db924cf7c 227
wolfSSL 15:117db924cf7c 228 #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 229 #if defined(HAVE_INTEL_RORX
wolfSSL 16:8e0d178b1d1e 230 #define RND with rorx instruction
wolfSSL 15:117db924cf7c 231 #else
wolfSSL 15:117db924cf7c 232 #define RND
wolfSSL 15:117db924cf7c 233 #endif
wolfSSL 15:117db924cf7c 234 #endif
wolfSSL 15:117db924cf7c 235
wolfSSL 15:117db924cf7c 236 #if defined(HAVE_INTEL_AVX1)
wolfSSL 15:117db924cf7c 237
wolfSSL 15:117db924cf7c 238 #define XMM Instructions/inline asm
wolfSSL 15:117db924cf7c 239
wolfSSL 15:117db924cf7c 240 int Transform_Sha256() {
wolfSSL 15:117db924cf7c 241 Stitched Message Sched/Round
wolfSSL 15:117db924cf7c 242 }
wolfSSL 15:117db924cf7c 243
wolfSSL 15:117db924cf7c 244 #elif defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 245
wolfSSL 15:117db924cf7c 246 #define YMM Instructions/inline asm
wolfSSL 15:117db924cf7c 247
wolfSSL 15:117db924cf7c 248 int Transform_Sha256() {
wolfSSL 16:8e0d178b1d1e 249 More granular Stitched Message Sched/Round
wolfSSL 15:117db924cf7c 250 }
wolfSSL 15:117db924cf7c 251
wolfSSL 15:117db924cf7c 252 #endif
wolfSSL 15:117db924cf7c 253
wolfSSL 15:117db924cf7c 254 */
wolfSSL 15:117db924cf7c 255
wolfSSL 15:117db924cf7c 256 /* Each platform needs to query info type 1 from cpuid to see if aesni is
wolfSSL 15:117db924cf7c 257 * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
wolfSSL 15:117db924cf7c 258 */
wolfSSL 15:117db924cf7c 259
wolfSSL 15:117db924cf7c 260 /* #if defined(HAVE_INTEL_AVX1/2) at the tail of sha256 */
wolfSSL 16:8e0d178b1d1e 261 static int Transform_Sha256(wc_Sha256* sha256, const byte* data);
wolfSSL 16:8e0d178b1d1e 262
wolfSSL 16:8e0d178b1d1e 263 #ifdef __cplusplus
wolfSSL 16:8e0d178b1d1e 264 extern "C" {
wolfSSL 16:8e0d178b1d1e 265 #endif
wolfSSL 16:8e0d178b1d1e 266
wolfSSL 15:117db924cf7c 267 #if defined(HAVE_INTEL_AVX1)
wolfSSL 16:8e0d178b1d1e 268 extern int Transform_Sha256_AVX1(wc_Sha256 *sha256, const byte* data);
wolfSSL 16:8e0d178b1d1e 269 extern int Transform_Sha256_AVX1_Len(wc_Sha256* sha256,
wolfSSL 16:8e0d178b1d1e 270 const byte* data, word32 len);
wolfSSL 15:117db924cf7c 271 #endif
wolfSSL 15:117db924cf7c 272 #if defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 273 extern int Transform_Sha256_AVX2(wc_Sha256 *sha256, const byte* data);
wolfSSL 16:8e0d178b1d1e 274 extern int Transform_Sha256_AVX2_Len(wc_Sha256* sha256,
wolfSSL 16:8e0d178b1d1e 275 const byte* data, word32 len);
wolfSSL 15:117db924cf7c 276 #ifdef HAVE_INTEL_RORX
wolfSSL 16:8e0d178b1d1e 277 extern int Transform_Sha256_AVX1_RORX(wc_Sha256 *sha256, const byte* data);
wolfSSL 16:8e0d178b1d1e 278 extern int Transform_Sha256_AVX1_RORX_Len(wc_Sha256* sha256,
wolfSSL 16:8e0d178b1d1e 279 const byte* data, word32 len);
wolfSSL 16:8e0d178b1d1e 280 extern int Transform_Sha256_AVX2_RORX(wc_Sha256 *sha256, const byte* data);
wolfSSL 16:8e0d178b1d1e 281 extern int Transform_Sha256_AVX2_RORX_Len(wc_Sha256* sha256,
wolfSSL 16:8e0d178b1d1e 282 const byte* data, word32 len);
wolfSSL 16:8e0d178b1d1e 283 #endif /* HAVE_INTEL_RORX */
wolfSSL 16:8e0d178b1d1e 284 #endif /* HAVE_INTEL_AVX2 */
wolfSSL 16:8e0d178b1d1e 285
wolfSSL 16:8e0d178b1d1e 286 #ifdef __cplusplus
wolfSSL 16:8e0d178b1d1e 287 } /* extern "C" */
wolfSSL 16:8e0d178b1d1e 288 #endif
wolfSSL 16:8e0d178b1d1e 289
wolfSSL 16:8e0d178b1d1e 290 static int (*Transform_Sha256_p)(wc_Sha256* sha256, const byte* data);
wolfSSL 15:117db924cf7c 291 /* = _Transform_Sha256 */
wolfSSL 16:8e0d178b1d1e 292 static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
wolfSSL 16:8e0d178b1d1e 293 word32 len);
wolfSSL 15:117db924cf7c 294 /* = NULL */
wolfSSL 15:117db924cf7c 295 static int transform_check = 0;
wolfSSL 15:117db924cf7c 296 static word32 intel_flags;
wolfSSL 16:8e0d178b1d1e 297
wolfSSL 16:8e0d178b1d1e 298 #define XTRANSFORM(S, D) (*Transform_Sha256_p)((S),(D))
wolfSSL 16:8e0d178b1d1e 299 #define XTRANSFORM_LEN(S, D, L) (*Transform_Sha256_Len_p)((S),(D),(L))
wolfSSL 15:117db924cf7c 300
wolfSSL 15:117db924cf7c 301 static void Sha256_SetTransform(void)
wolfSSL 15:117db924cf7c 302 {
wolfSSL 15:117db924cf7c 303
wolfSSL 15:117db924cf7c 304 if (transform_check)
wolfSSL 15:117db924cf7c 305 return;
wolfSSL 15:117db924cf7c 306
wolfSSL 15:117db924cf7c 307 intel_flags = cpuid_get_flags();
wolfSSL 15:117db924cf7c 308
wolfSSL 15:117db924cf7c 309 #ifdef HAVE_INTEL_AVX2
wolfSSL 16:8e0d178b1d1e 310 if (1 && IS_INTEL_AVX2(intel_flags)) {
wolfSSL 15:117db924cf7c 311 #ifdef HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 312 if (IS_INTEL_BMI2(intel_flags)) {
wolfSSL 15:117db924cf7c 313 Transform_Sha256_p = Transform_Sha256_AVX2_RORX;
wolfSSL 15:117db924cf7c 314 Transform_Sha256_Len_p = Transform_Sha256_AVX2_RORX_Len;
wolfSSL 15:117db924cf7c 315 }
wolfSSL 15:117db924cf7c 316 else
wolfSSL 15:117db924cf7c 317 #endif
wolfSSL 15:117db924cf7c 318 if (1)
wolfSSL 15:117db924cf7c 319 {
wolfSSL 15:117db924cf7c 320 Transform_Sha256_p = Transform_Sha256_AVX2;
wolfSSL 15:117db924cf7c 321 Transform_Sha256_Len_p = Transform_Sha256_AVX2_Len;
wolfSSL 15:117db924cf7c 322 }
wolfSSL 15:117db924cf7c 323 #ifdef HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 324 else {
wolfSSL 15:117db924cf7c 325 Transform_Sha256_p = Transform_Sha256_AVX1_RORX;
wolfSSL 15:117db924cf7c 326 Transform_Sha256_Len_p = Transform_Sha256_AVX1_RORX_Len;
wolfSSL 15:117db924cf7c 327 }
wolfSSL 15:117db924cf7c 328 #endif
wolfSSL 15:117db924cf7c 329 }
wolfSSL 15:117db924cf7c 330 else
wolfSSL 15:117db924cf7c 331 #endif
wolfSSL 15:117db924cf7c 332 #ifdef HAVE_INTEL_AVX1
wolfSSL 15:117db924cf7c 333 if (IS_INTEL_AVX1(intel_flags)) {
wolfSSL 15:117db924cf7c 334 Transform_Sha256_p = Transform_Sha256_AVX1;
wolfSSL 15:117db924cf7c 335 Transform_Sha256_Len_p = Transform_Sha256_AVX1_Len;
wolfSSL 15:117db924cf7c 336 }
wolfSSL 15:117db924cf7c 337 else
wolfSSL 15:117db924cf7c 338 #endif
wolfSSL 15:117db924cf7c 339 {
wolfSSL 15:117db924cf7c 340 Transform_Sha256_p = Transform_Sha256;
wolfSSL 15:117db924cf7c 341 Transform_Sha256_Len_p = NULL;
wolfSSL 15:117db924cf7c 342 }
wolfSSL 15:117db924cf7c 343
wolfSSL 15:117db924cf7c 344 transform_check = 1;
wolfSSL 15:117db924cf7c 345 }
wolfSSL 15:117db924cf7c 346
wolfSSL 15:117db924cf7c 347 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 15:117db924cf7c 348 {
wolfSSL 15:117db924cf7c 349 int ret = 0;
wolfSSL 15:117db924cf7c 350 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 351 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 352
wolfSSL 15:117db924cf7c 353 sha256->heap = heap;
wolfSSL 16:8e0d178b1d1e 354 #ifdef WOLF_CRYPTO_CB
wolfSSL 16:8e0d178b1d1e 355 sha256->devId = devId;
wolfSSL 16:8e0d178b1d1e 356 #endif
wolfSSL 15:117db924cf7c 357
wolfSSL 15:117db924cf7c 358 ret = InitSha256(sha256);
wolfSSL 15:117db924cf7c 359 if (ret != 0)
wolfSSL 15:117db924cf7c 360 return ret;
wolfSSL 15:117db924cf7c 361
wolfSSL 15:117db924cf7c 362 /* choose best Transform function under this runtime environment */
wolfSSL 15:117db924cf7c 363 Sha256_SetTransform();
wolfSSL 15:117db924cf7c 364
wolfSSL 15:117db924cf7c 365 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
wolfSSL 15:117db924cf7c 366 ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
wolfSSL 15:117db924cf7c 367 WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
wolfSSL 15:117db924cf7c 368 #else
wolfSSL 15:117db924cf7c 369 (void)devId;
wolfSSL 15:117db924cf7c 370 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 371
wolfSSL 15:117db924cf7c 372 return ret;
wolfSSL 15:117db924cf7c 373 }
wolfSSL 15:117db924cf7c 374
wolfSSL 15:117db924cf7c 375 #elif defined(FREESCALE_LTC_SHA)
wolfSSL 15:117db924cf7c 376 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 15:117db924cf7c 377 {
wolfSSL 15:117db924cf7c 378 (void)heap;
wolfSSL 15:117db924cf7c 379 (void)devId;
wolfSSL 15:117db924cf7c 380
wolfSSL 15:117db924cf7c 381 LTC_HASH_Init(LTC_BASE, &sha256->ctx, kLTC_Sha256, NULL, 0);
wolfSSL 15:117db924cf7c 382
wolfSSL 15:117db924cf7c 383 return 0;
wolfSSL 15:117db924cf7c 384 }
wolfSSL 15:117db924cf7c 385
wolfSSL 15:117db924cf7c 386 #elif defined(FREESCALE_MMCAU_SHA)
wolfSSL 15:117db924cf7c 387
wolfSSL 15:117db924cf7c 388 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
wolfSSL 15:117db924cf7c 389 #include "cau_api.h"
wolfSSL 15:117db924cf7c 390 #else
wolfSSL 15:117db924cf7c 391 #include "fsl_mmcau.h"
wolfSSL 15:117db924cf7c 392 #endif
wolfSSL 15:117db924cf7c 393
wolfSSL 16:8e0d178b1d1e 394 #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
wolfSSL 16:8e0d178b1d1e 395 #define XTRANSFORM_LEN(S, D, L) Transform_Sha256_Len((S),(D),(L))
wolfSSL 16:8e0d178b1d1e 396
wolfSSL 16:8e0d178b1d1e 397 #ifndef WC_HASH_DATA_ALIGNMENT
wolfSSL 16:8e0d178b1d1e 398 /* these hardware API's require 4 byte (word32) alignment */
wolfSSL 16:8e0d178b1d1e 399 #define WC_HASH_DATA_ALIGNMENT 4
wolfSSL 16:8e0d178b1d1e 400 #endif
wolfSSL 15:117db924cf7c 401
wolfSSL 15:117db924cf7c 402 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 15:117db924cf7c 403 {
wolfSSL 15:117db924cf7c 404 int ret = 0;
wolfSSL 15:117db924cf7c 405
wolfSSL 15:117db924cf7c 406 (void)heap;
wolfSSL 15:117db924cf7c 407 (void)devId;
wolfSSL 15:117db924cf7c 408
wolfSSL 15:117db924cf7c 409 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 410 if (ret != 0) {
wolfSSL 15:117db924cf7c 411 return ret;
wolfSSL 15:117db924cf7c 412 }
wolfSSL 16:8e0d178b1d1e 413
wolfSSL 15:117db924cf7c 414 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
wolfSSL 15:117db924cf7c 415 cau_sha256_initialize_output(sha256->digest);
wolfSSL 15:117db924cf7c 416 #else
wolfSSL 15:117db924cf7c 417 MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
wolfSSL 15:117db924cf7c 418 #endif
wolfSSL 15:117db924cf7c 419 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 420
wolfSSL 15:117db924cf7c 421 sha256->buffLen = 0;
wolfSSL 15:117db924cf7c 422 sha256->loLen = 0;
wolfSSL 15:117db924cf7c 423 sha256->hiLen = 0;
wolfSSL 16:8e0d178b1d1e 424 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 16:8e0d178b1d1e 425 sha256->W = NULL;
wolfSSL 16:8e0d178b1d1e 426 #endif
wolfSSL 15:117db924cf7c 427
wolfSSL 15:117db924cf7c 428 return ret;
wolfSSL 15:117db924cf7c 429 }
wolfSSL 15:117db924cf7c 430
wolfSSL 16:8e0d178b1d1e 431 static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
wolfSSL 15:117db924cf7c 432 {
wolfSSL 15:117db924cf7c 433 int ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 434 if (ret == 0) {
wolfSSL 15:117db924cf7c 435 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
wolfSSL 16:8e0d178b1d1e 436 cau_sha256_hash_n((byte*)data, 1, sha256->digest);
wolfSSL 15:117db924cf7c 437 #else
wolfSSL 16:8e0d178b1d1e 438 MMCAU_SHA256_HashN((byte*)data, 1, sha256->digest);
wolfSSL 15:117db924cf7c 439 #endif
wolfSSL 15:117db924cf7c 440 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 441 }
wolfSSL 15:117db924cf7c 442 return ret;
wolfSSL 15:117db924cf7c 443 }
wolfSSL 15:117db924cf7c 444
wolfSSL 16:8e0d178b1d1e 445 static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
wolfSSL 16:8e0d178b1d1e 446 word32 len)
wolfSSL 16:8e0d178b1d1e 447 {
wolfSSL 16:8e0d178b1d1e 448 int ret = wolfSSL_CryptHwMutexLock();
wolfSSL 16:8e0d178b1d1e 449 if (ret == 0) {
wolfSSL 16:8e0d178b1d1e 450 #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
wolfSSL 16:8e0d178b1d1e 451 if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
wolfSSL 16:8e0d178b1d1e 452 /* data pointer is NOT aligned,
wolfSSL 16:8e0d178b1d1e 453 * so copy and perform one block at a time */
wolfSSL 16:8e0d178b1d1e 454 byte* local = (byte*)sha256->buffer;
wolfSSL 16:8e0d178b1d1e 455 while (len >= WC_SHA256_BLOCK_SIZE) {
wolfSSL 16:8e0d178b1d1e 456 XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 457 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
wolfSSL 16:8e0d178b1d1e 458 cau_sha256_hash_n(local, 1, sha256->digest);
wolfSSL 16:8e0d178b1d1e 459 #else
wolfSSL 16:8e0d178b1d1e 460 MMCAU_SHA256_HashN(local, 1, sha256->digest);
wolfSSL 16:8e0d178b1d1e 461 #endif
wolfSSL 16:8e0d178b1d1e 462 data += WC_SHA256_BLOCK_SIZE;
wolfSSL 16:8e0d178b1d1e 463 len -= WC_SHA256_BLOCK_SIZE;
wolfSSL 16:8e0d178b1d1e 464 }
wolfSSL 16:8e0d178b1d1e 465 }
wolfSSL 16:8e0d178b1d1e 466 else
wolfSSL 16:8e0d178b1d1e 467 #endif
wolfSSL 16:8e0d178b1d1e 468 {
wolfSSL 16:8e0d178b1d1e 469 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
wolfSSL 16:8e0d178b1d1e 470 cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
wolfSSL 16:8e0d178b1d1e 471 sha256->digest);
wolfSSL 16:8e0d178b1d1e 472 #else
wolfSSL 16:8e0d178b1d1e 473 MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
wolfSSL 16:8e0d178b1d1e 474 sha256->digest);
wolfSSL 16:8e0d178b1d1e 475 #endif
wolfSSL 16:8e0d178b1d1e 476 }
wolfSSL 16:8e0d178b1d1e 477 wolfSSL_CryptHwMutexUnLock();
wolfSSL 16:8e0d178b1d1e 478 }
wolfSSL 16:8e0d178b1d1e 479 return ret;
wolfSSL 16:8e0d178b1d1e 480 }
wolfSSL 16:8e0d178b1d1e 481
wolfSSL 15:117db924cf7c 482 #elif defined(WOLFSSL_PIC32MZ_HASH)
wolfSSL 15:117db924cf7c 483 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
wolfSSL 15:117db924cf7c 484
wolfSSL 15:117db924cf7c 485 #elif defined(STM32_HASH_SHA2)
wolfSSL 15:117db924cf7c 486
wolfSSL 15:117db924cf7c 487 /* Supports CubeMX HAL or Standard Peripheral Library */
wolfSSL 15:117db924cf7c 488
wolfSSL 15:117db924cf7c 489 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 15:117db924cf7c 490 {
wolfSSL 15:117db924cf7c 491 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 492 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 493
wolfSSL 15:117db924cf7c 494 (void)devId;
wolfSSL 15:117db924cf7c 495 (void)heap;
wolfSSL 15:117db924cf7c 496
wolfSSL 15:117db924cf7c 497 wc_Stm32_Hash_Init(&sha256->stmCtx);
wolfSSL 15:117db924cf7c 498 return 0;
wolfSSL 15:117db924cf7c 499 }
wolfSSL 15:117db924cf7c 500
wolfSSL 15:117db924cf7c 501 int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 502 {
wolfSSL 15:117db924cf7c 503 int ret = 0;
wolfSSL 15:117db924cf7c 504
wolfSSL 15:117db924cf7c 505 if (sha256 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 506 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 507 }
wolfSSL 15:117db924cf7c 508
wolfSSL 15:117db924cf7c 509 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 510 if (ret == 0) {
wolfSSL 15:117db924cf7c 511 ret = wc_Stm32_Hash_Update(&sha256->stmCtx,
wolfSSL 15:117db924cf7c 512 HASH_AlgoSelection_SHA256, data, len);
wolfSSL 15:117db924cf7c 513 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 514 }
wolfSSL 15:117db924cf7c 515 return ret;
wolfSSL 15:117db924cf7c 516 }
wolfSSL 15:117db924cf7c 517
wolfSSL 15:117db924cf7c 518 int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
wolfSSL 15:117db924cf7c 519 {
wolfSSL 15:117db924cf7c 520 int ret = 0;
wolfSSL 15:117db924cf7c 521
wolfSSL 15:117db924cf7c 522 if (sha256 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 523 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 524 }
wolfSSL 15:117db924cf7c 525
wolfSSL 15:117db924cf7c 526 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 527 if (ret == 0) {
wolfSSL 15:117db924cf7c 528 ret = wc_Stm32_Hash_Final(&sha256->stmCtx,
wolfSSL 15:117db924cf7c 529 HASH_AlgoSelection_SHA256, hash, WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 530 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 531 }
wolfSSL 15:117db924cf7c 532
wolfSSL 15:117db924cf7c 533 (void)wc_InitSha256(sha256); /* reset state */
wolfSSL 15:117db924cf7c 534
wolfSSL 15:117db924cf7c 535 return ret;
wolfSSL 15:117db924cf7c 536 }
wolfSSL 15:117db924cf7c 537
wolfSSL 15:117db924cf7c 538 #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
wolfSSL 15:117db924cf7c 539 /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
wolfSSL 16:8e0d178b1d1e 540
wolfSSL 16:8e0d178b1d1e 541 #elif defined(WOLFSSL_AFALG_HASH)
wolfSSL 16:8e0d178b1d1e 542 /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
wolfSSL 16:8e0d178b1d1e 543
wolfSSL 16:8e0d178b1d1e 544 #elif defined(WOLFSSL_DEVCRYPTO_HASH)
wolfSSL 16:8e0d178b1d1e 545 /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
wolfSSL 16:8e0d178b1d1e 546
wolfSSL 16:8e0d178b1d1e 547 #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)
wolfSSL 16:8e0d178b1d1e 548 #include "hal_data.h"
wolfSSL 16:8e0d178b1d1e 549
wolfSSL 16:8e0d178b1d1e 550 #ifndef WOLFSSL_SCE_SHA256_HANDLE
wolfSSL 16:8e0d178b1d1e 551 #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0
wolfSSL 16:8e0d178b1d1e 552 #endif
wolfSSL 16:8e0d178b1d1e 553
wolfSSL 16:8e0d178b1d1e 554 #define WC_SHA256_DIGEST_WORD_SIZE 16
wolfSSL 16:8e0d178b1d1e 555 #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D))
wolfSSL 16:8e0d178b1d1e 556 static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data)
wolfSSL 16:8e0d178b1d1e 557 {
wolfSSL 16:8e0d178b1d1e 558 if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
wolfSSL 16:8e0d178b1d1e 559 CRYPTO_WORD_ENDIAN_LITTLE)
wolfSSL 16:8e0d178b1d1e 560 {
wolfSSL 16:8e0d178b1d1e 561 ByteReverseWords((word32*)data, (word32*)data,
wolfSSL 16:8e0d178b1d1e 562 WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 563 ByteReverseWords(sha256->digest, sha256->digest,
wolfSSL 16:8e0d178b1d1e 564 WC_SHA256_DIGEST_SIZE);
wolfSSL 16:8e0d178b1d1e 565 }
wolfSSL 16:8e0d178b1d1e 566
wolfSSL 16:8e0d178b1d1e 567 if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(
wolfSSL 16:8e0d178b1d1e 568 WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data,
wolfSSL 16:8e0d178b1d1e 569 WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){
wolfSSL 16:8e0d178b1d1e 570 WOLFSSL_MSG("Unexpected hardware return value");
wolfSSL 16:8e0d178b1d1e 571 return WC_HW_E;
wolfSSL 16:8e0d178b1d1e 572 }
wolfSSL 16:8e0d178b1d1e 573
wolfSSL 16:8e0d178b1d1e 574 if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
wolfSSL 16:8e0d178b1d1e 575 CRYPTO_WORD_ENDIAN_LITTLE)
wolfSSL 16:8e0d178b1d1e 576 {
wolfSSL 16:8e0d178b1d1e 577 ByteReverseWords((word32*)data, (word32*)data,
wolfSSL 16:8e0d178b1d1e 578 WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 579 ByteReverseWords(sha256->digest, sha256->digest,
wolfSSL 16:8e0d178b1d1e 580 WC_SHA256_DIGEST_SIZE);
wolfSSL 16:8e0d178b1d1e 581 }
wolfSSL 16:8e0d178b1d1e 582
wolfSSL 16:8e0d178b1d1e 583 return 0;
wolfSSL 16:8e0d178b1d1e 584 }
wolfSSL 16:8e0d178b1d1e 585
wolfSSL 16:8e0d178b1d1e 586
wolfSSL 16:8e0d178b1d1e 587 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 588 {
wolfSSL 16:8e0d178b1d1e 589 int ret = 0;
wolfSSL 16:8e0d178b1d1e 590 if (sha256 == NULL)
wolfSSL 16:8e0d178b1d1e 591 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 592
wolfSSL 16:8e0d178b1d1e 593 sha256->heap = heap;
wolfSSL 16:8e0d178b1d1e 594
wolfSSL 16:8e0d178b1d1e 595 ret = InitSha256(sha256);
wolfSSL 16:8e0d178b1d1e 596 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 597 return ret;
wolfSSL 16:8e0d178b1d1e 598
wolfSSL 16:8e0d178b1d1e 599 (void)devId;
wolfSSL 16:8e0d178b1d1e 600
wolfSSL 16:8e0d178b1d1e 601 return ret;
wolfSSL 16:8e0d178b1d1e 602 }
wolfSSL 16:8e0d178b1d1e 603
wolfSSL 16:8e0d178b1d1e 604 #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 605 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 606
wolfSSL 16:8e0d178b1d1e 607 #define NEED_SOFT_SHA256
wolfSSL 16:8e0d178b1d1e 608
wolfSSL 16:8e0d178b1d1e 609 static int InitSha256(wc_Sha256* sha256)
wolfSSL 16:8e0d178b1d1e 610 {
wolfSSL 16:8e0d178b1d1e 611 int ret = 0;
wolfSSL 16:8e0d178b1d1e 612
wolfSSL 16:8e0d178b1d1e 613 if (sha256 == NULL)
wolfSSL 16:8e0d178b1d1e 614 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 615
wolfSSL 16:8e0d178b1d1e 616 XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
wolfSSL 16:8e0d178b1d1e 617 sha256->digest[0] = 0x6A09E667L;
wolfSSL 16:8e0d178b1d1e 618 sha256->digest[1] = 0xBB67AE85L;
wolfSSL 16:8e0d178b1d1e 619 sha256->digest[2] = 0x3C6EF372L;
wolfSSL 16:8e0d178b1d1e 620 sha256->digest[3] = 0xA54FF53AL;
wolfSSL 16:8e0d178b1d1e 621 sha256->digest[4] = 0x510E527FL;
wolfSSL 16:8e0d178b1d1e 622 sha256->digest[5] = 0x9B05688CL;
wolfSSL 16:8e0d178b1d1e 623 sha256->digest[6] = 0x1F83D9ABL;
wolfSSL 16:8e0d178b1d1e 624 sha256->digest[7] = 0x5BE0CD19L;
wolfSSL 16:8e0d178b1d1e 625
wolfSSL 16:8e0d178b1d1e 626 sha256->buffLen = 0;
wolfSSL 16:8e0d178b1d1e 627 sha256->loLen = 0;
wolfSSL 16:8e0d178b1d1e 628 sha256->hiLen = 0;
wolfSSL 16:8e0d178b1d1e 629
wolfSSL 16:8e0d178b1d1e 630 /* always start firstblock = 1 when using hw engine */
wolfSSL 16:8e0d178b1d1e 631 sha256->ctx.isfirstblock = 1;
wolfSSL 16:8e0d178b1d1e 632 sha256->ctx.sha_type = SHA2_256;
wolfSSL 16:8e0d178b1d1e 633 if(sha256->ctx.mode == ESP32_SHA_HW) {
wolfSSL 16:8e0d178b1d1e 634 /* release hw */
wolfSSL 16:8e0d178b1d1e 635 esp_sha_hw_unlock();
wolfSSL 16:8e0d178b1d1e 636 }
wolfSSL 16:8e0d178b1d1e 637 /* always set mode as INIT
wolfSSL 16:8e0d178b1d1e 638 * whether using HW or SW is determined at first call of update()
wolfSSL 16:8e0d178b1d1e 639 */
wolfSSL 16:8e0d178b1d1e 640 sha256->ctx.mode = ESP32_SHA_INIT;
wolfSSL 16:8e0d178b1d1e 641
wolfSSL 16:8e0d178b1d1e 642 return ret;
wolfSSL 16:8e0d178b1d1e 643 }
wolfSSL 16:8e0d178b1d1e 644 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 645 {
wolfSSL 16:8e0d178b1d1e 646 int ret = 0;
wolfSSL 16:8e0d178b1d1e 647
wolfSSL 16:8e0d178b1d1e 648 if (sha256 == NULL)
wolfSSL 16:8e0d178b1d1e 649 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 650
wolfSSL 16:8e0d178b1d1e 651 XMEMSET(sha256, 0, sizeof(wc_Sha256));
wolfSSL 16:8e0d178b1d1e 652 sha256->ctx.mode = ESP32_SHA_INIT;
wolfSSL 16:8e0d178b1d1e 653 sha256->ctx.isfirstblock = 1;
wolfSSL 16:8e0d178b1d1e 654 (void)devId;
wolfSSL 16:8e0d178b1d1e 655
wolfSSL 16:8e0d178b1d1e 656 ret = InitSha256(sha256);
wolfSSL 16:8e0d178b1d1e 657
wolfSSL 16:8e0d178b1d1e 658 return ret;
wolfSSL 16:8e0d178b1d1e 659 }
wolfSSL 16:8e0d178b1d1e 660
wolfSSL 16:8e0d178b1d1e 661 #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 662 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 663
wolfSSL 16:8e0d178b1d1e 664 /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
wolfSSL 16:8e0d178b1d1e 665
wolfSSL 15:117db924cf7c 666 #else
wolfSSL 15:117db924cf7c 667 #define NEED_SOFT_SHA256
wolfSSL 15:117db924cf7c 668
wolfSSL 15:117db924cf7c 669 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
wolfSSL 15:117db924cf7c 670 {
wolfSSL 15:117db924cf7c 671 int ret = 0;
wolfSSL 15:117db924cf7c 672 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 673 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 674
wolfSSL 15:117db924cf7c 675 sha256->heap = heap;
wolfSSL 16:8e0d178b1d1e 676 #ifdef WOLF_CRYPTO_CB
wolfSSL 16:8e0d178b1d1e 677 sha256->devId = devId;
wolfSSL 16:8e0d178b1d1e 678 sha256->devCtx = NULL;
wolfSSL 16:8e0d178b1d1e 679 #endif
wolfSSL 15:117db924cf7c 680
wolfSSL 15:117db924cf7c 681 ret = InitSha256(sha256);
wolfSSL 15:117db924cf7c 682 if (ret != 0)
wolfSSL 15:117db924cf7c 683 return ret;
wolfSSL 15:117db924cf7c 684
wolfSSL 15:117db924cf7c 685 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 686 sha256->W = NULL;
wolfSSL 15:117db924cf7c 687 #endif
wolfSSL 15:117db924cf7c 688
wolfSSL 15:117db924cf7c 689 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
wolfSSL 15:117db924cf7c 690 ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
wolfSSL 15:117db924cf7c 691 WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
wolfSSL 15:117db924cf7c 692 #else
wolfSSL 15:117db924cf7c 693 (void)devId;
wolfSSL 15:117db924cf7c 694 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 695
wolfSSL 15:117db924cf7c 696 return ret;
wolfSSL 15:117db924cf7c 697 }
wolfSSL 15:117db924cf7c 698 #endif /* End Hardware Acceleration */
wolfSSL 15:117db924cf7c 699
wolfSSL 15:117db924cf7c 700 #ifdef NEED_SOFT_SHA256
wolfSSL 15:117db924cf7c 701
wolfSSL 15:117db924cf7c 702 static const ALIGN32 word32 K[64] = {
wolfSSL 15:117db924cf7c 703 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
wolfSSL 15:117db924cf7c 704 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
wolfSSL 15:117db924cf7c 705 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
wolfSSL 15:117db924cf7c 706 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
wolfSSL 15:117db924cf7c 707 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
wolfSSL 15:117db924cf7c 708 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
wolfSSL 15:117db924cf7c 709 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
wolfSSL 15:117db924cf7c 710 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
wolfSSL 15:117db924cf7c 711 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
wolfSSL 15:117db924cf7c 712 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
wolfSSL 15:117db924cf7c 713 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
wolfSSL 15:117db924cf7c 714 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
wolfSSL 15:117db924cf7c 715 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
wolfSSL 15:117db924cf7c 716 };
wolfSSL 15:117db924cf7c 717
wolfSSL 16:8e0d178b1d1e 718 /* Both versions of Ch and Maj are logically the same, but with the second set
wolfSSL 16:8e0d178b1d1e 719 the compilers can recognize them better for optimization */
wolfSSL 16:8e0d178b1d1e 720 #ifdef WOLFSSL_SHA256_BY_SPEC
wolfSSL 16:8e0d178b1d1e 721 /* SHA256 math based on specification */
wolfSSL 15:117db924cf7c 722 #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
wolfSSL 15:117db924cf7c 723 #define Maj(x,y,z) ((((x) | (y)) & (z)) | ((x) & (y)))
wolfSSL 16:8e0d178b1d1e 724 #else
wolfSSL 16:8e0d178b1d1e 725 /* SHA256 math reworked for easier compiler optimization */
wolfSSL 16:8e0d178b1d1e 726 #define Ch(x,y,z) ((((y) ^ (z)) & (x)) ^ (z))
wolfSSL 16:8e0d178b1d1e 727 #define Maj(x,y,z) ((((x) ^ (y)) & ((y) ^ (z))) ^ (y))
wolfSSL 16:8e0d178b1d1e 728 #endif
wolfSSL 15:117db924cf7c 729 #define R(x, n) (((x) & 0xFFFFFFFFU) >> (n))
wolfSSL 15:117db924cf7c 730
wolfSSL 15:117db924cf7c 731 #define S(x, n) rotrFixed(x, n)
wolfSSL 15:117db924cf7c 732 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
wolfSSL 15:117db924cf7c 733 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
wolfSSL 15:117db924cf7c 734 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
wolfSSL 15:117db924cf7c 735 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
wolfSSL 15:117db924cf7c 736
wolfSSL 15:117db924cf7c 737 #define a(i) S[(0-i) & 7]
wolfSSL 15:117db924cf7c 738 #define b(i) S[(1-i) & 7]
wolfSSL 15:117db924cf7c 739 #define c(i) S[(2-i) & 7]
wolfSSL 15:117db924cf7c 740 #define d(i) S[(3-i) & 7]
wolfSSL 15:117db924cf7c 741 #define e(i) S[(4-i) & 7]
wolfSSL 15:117db924cf7c 742 #define f(i) S[(5-i) & 7]
wolfSSL 15:117db924cf7c 743 #define g(i) S[(6-i) & 7]
wolfSSL 15:117db924cf7c 744 #define h(i) S[(7-i) & 7]
wolfSSL 15:117db924cf7c 745
wolfSSL 16:8e0d178b1d1e 746 #ifndef XTRANSFORM
wolfSSL 16:8e0d178b1d1e 747 #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
wolfSSL 16:8e0d178b1d1e 748 #endif
wolfSSL 16:8e0d178b1d1e 749
wolfSSL 16:8e0d178b1d1e 750 #ifndef SHA256_MANY_REGISTERS
wolfSSL 15:117db924cf7c 751 #define RND(j) \
wolfSSL 15:117db924cf7c 752 t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + W[i+j]; \
wolfSSL 15:117db924cf7c 753 t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
wolfSSL 15:117db924cf7c 754 d(j) += t0; \
wolfSSL 15:117db924cf7c 755 h(j) = t0 + t1
wolfSSL 15:117db924cf7c 756
wolfSSL 16:8e0d178b1d1e 757 static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
wolfSSL 15:117db924cf7c 758 {
wolfSSL 15:117db924cf7c 759 word32 S[8], t0, t1;
wolfSSL 15:117db924cf7c 760 int i;
wolfSSL 15:117db924cf7c 761
wolfSSL 15:117db924cf7c 762 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 763 word32* W = sha256->W;
wolfSSL 15:117db924cf7c 764 if (W == NULL) {
wolfSSL 15:117db924cf7c 765 W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
wolfSSL 16:8e0d178b1d1e 766 DYNAMIC_TYPE_DIGEST);
wolfSSL 15:117db924cf7c 767 if (W == NULL)
wolfSSL 15:117db924cf7c 768 return MEMORY_E;
wolfSSL 15:117db924cf7c 769 sha256->W = W;
wolfSSL 15:117db924cf7c 770 }
wolfSSL 15:117db924cf7c 771 #elif defined(WOLFSSL_SMALL_STACK)
wolfSSL 15:117db924cf7c 772 word32* W;
wolfSSL 15:117db924cf7c 773 W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
wolfSSL 15:117db924cf7c 774 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 775 if (W == NULL)
wolfSSL 15:117db924cf7c 776 return MEMORY_E;
wolfSSL 15:117db924cf7c 777 #else
wolfSSL 15:117db924cf7c 778 word32 W[WC_SHA256_BLOCK_SIZE];
wolfSSL 15:117db924cf7c 779 #endif
wolfSSL 15:117db924cf7c 780
wolfSSL 15:117db924cf7c 781 /* Copy context->state[] to working vars */
wolfSSL 15:117db924cf7c 782 for (i = 0; i < 8; i++)
wolfSSL 15:117db924cf7c 783 S[i] = sha256->digest[i];
wolfSSL 15:117db924cf7c 784
wolfSSL 15:117db924cf7c 785 for (i = 0; i < 16; i++)
wolfSSL 16:8e0d178b1d1e 786 W[i] = *((word32*)&data[i*sizeof(word32)]);
wolfSSL 15:117db924cf7c 787
wolfSSL 15:117db924cf7c 788 for (i = 16; i < WC_SHA256_BLOCK_SIZE; i++)
wolfSSL 15:117db924cf7c 789 W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
wolfSSL 15:117db924cf7c 790
wolfSSL 15:117db924cf7c 791 #ifdef USE_SLOW_SHA256
wolfSSL 15:117db924cf7c 792 /* not unrolled - ~2k smaller and ~25% slower */
wolfSSL 15:117db924cf7c 793 for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
wolfSSL 15:117db924cf7c 794 int j;
wolfSSL 15:117db924cf7c 795 for (j = 0; j < 8; j++) { /* braces needed here for macros {} */
wolfSSL 15:117db924cf7c 796 RND(j);
wolfSSL 15:117db924cf7c 797 }
wolfSSL 15:117db924cf7c 798 }
wolfSSL 15:117db924cf7c 799 #else
wolfSSL 15:117db924cf7c 800 /* partially loop unrolled */
wolfSSL 15:117db924cf7c 801 for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
wolfSSL 15:117db924cf7c 802 RND(0); RND(1); RND(2); RND(3);
wolfSSL 15:117db924cf7c 803 RND(4); RND(5); RND(6); RND(7);
wolfSSL 15:117db924cf7c 804 }
wolfSSL 15:117db924cf7c 805 #endif /* USE_SLOW_SHA256 */
wolfSSL 15:117db924cf7c 806
wolfSSL 15:117db924cf7c 807 /* Add the working vars back into digest state[] */
wolfSSL 15:117db924cf7c 808 for (i = 0; i < 8; i++) {
wolfSSL 15:117db924cf7c 809 sha256->digest[i] += S[i];
wolfSSL 15:117db924cf7c 810 }
wolfSSL 15:117db924cf7c 811
wolfSSL 15:117db924cf7c 812 #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
wolfSSL 15:117db924cf7c 813 XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 814 #endif
wolfSSL 15:117db924cf7c 815 return 0;
wolfSSL 15:117db924cf7c 816 }
wolfSSL 16:8e0d178b1d1e 817 #else
wolfSSL 16:8e0d178b1d1e 818 /* SHA256 version that keeps all data in registers */
wolfSSL 16:8e0d178b1d1e 819 #define SCHED1(j) (W[j] = *((word32*)&data[j*sizeof(word32)]))
wolfSSL 16:8e0d178b1d1e 820 #define SCHED(j) ( \
wolfSSL 16:8e0d178b1d1e 821 W[ j & 15] += \
wolfSSL 16:8e0d178b1d1e 822 Gamma1(W[(j-2) & 15])+ \
wolfSSL 16:8e0d178b1d1e 823 W[(j-7) & 15] + \
wolfSSL 16:8e0d178b1d1e 824 Gamma0(W[(j-15) & 15]) \
wolfSSL 16:8e0d178b1d1e 825 )
wolfSSL 16:8e0d178b1d1e 826
wolfSSL 16:8e0d178b1d1e 827 #define RND1(j) \
wolfSSL 16:8e0d178b1d1e 828 t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED1(j); \
wolfSSL 16:8e0d178b1d1e 829 t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
wolfSSL 16:8e0d178b1d1e 830 d(j) += t0; \
wolfSSL 16:8e0d178b1d1e 831 h(j) = t0 + t1
wolfSSL 16:8e0d178b1d1e 832 #define RNDN(j) \
wolfSSL 16:8e0d178b1d1e 833 t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED(j); \
wolfSSL 16:8e0d178b1d1e 834 t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
wolfSSL 16:8e0d178b1d1e 835 d(j) += t0; \
wolfSSL 16:8e0d178b1d1e 836 h(j) = t0 + t1
wolfSSL 16:8e0d178b1d1e 837
wolfSSL 16:8e0d178b1d1e 838 static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
wolfSSL 16:8e0d178b1d1e 839 {
wolfSSL 16:8e0d178b1d1e 840 word32 S[8], t0, t1;
wolfSSL 16:8e0d178b1d1e 841 int i;
wolfSSL 16:8e0d178b1d1e 842 word32 W[WC_SHA256_BLOCK_SIZE/sizeof(word32)];
wolfSSL 16:8e0d178b1d1e 843
wolfSSL 16:8e0d178b1d1e 844 /* Copy digest to working vars */
wolfSSL 16:8e0d178b1d1e 845 S[0] = sha256->digest[0];
wolfSSL 16:8e0d178b1d1e 846 S[1] = sha256->digest[1];
wolfSSL 16:8e0d178b1d1e 847 S[2] = sha256->digest[2];
wolfSSL 16:8e0d178b1d1e 848 S[3] = sha256->digest[3];
wolfSSL 16:8e0d178b1d1e 849 S[4] = sha256->digest[4];
wolfSSL 16:8e0d178b1d1e 850 S[5] = sha256->digest[5];
wolfSSL 16:8e0d178b1d1e 851 S[6] = sha256->digest[6];
wolfSSL 16:8e0d178b1d1e 852 S[7] = sha256->digest[7];
wolfSSL 16:8e0d178b1d1e 853
wolfSSL 16:8e0d178b1d1e 854 i = 0;
wolfSSL 16:8e0d178b1d1e 855 RND1( 0); RND1( 1); RND1( 2); RND1( 3);
wolfSSL 16:8e0d178b1d1e 856 RND1( 4); RND1( 5); RND1( 6); RND1( 7);
wolfSSL 16:8e0d178b1d1e 857 RND1( 8); RND1( 9); RND1(10); RND1(11);
wolfSSL 16:8e0d178b1d1e 858 RND1(12); RND1(13); RND1(14); RND1(15);
wolfSSL 16:8e0d178b1d1e 859 /* 64 operations, partially loop unrolled */
wolfSSL 16:8e0d178b1d1e 860 for (i = 16; i < 64; i += 16) {
wolfSSL 16:8e0d178b1d1e 861 RNDN( 0); RNDN( 1); RNDN( 2); RNDN( 3);
wolfSSL 16:8e0d178b1d1e 862 RNDN( 4); RNDN( 5); RNDN( 6); RNDN( 7);
wolfSSL 16:8e0d178b1d1e 863 RNDN( 8); RNDN( 9); RNDN(10); RNDN(11);
wolfSSL 16:8e0d178b1d1e 864 RNDN(12); RNDN(13); RNDN(14); RNDN(15);
wolfSSL 16:8e0d178b1d1e 865 }
wolfSSL 16:8e0d178b1d1e 866
wolfSSL 16:8e0d178b1d1e 867 /* Add the working vars back into digest */
wolfSSL 16:8e0d178b1d1e 868 sha256->digest[0] += S[0];
wolfSSL 16:8e0d178b1d1e 869 sha256->digest[1] += S[1];
wolfSSL 16:8e0d178b1d1e 870 sha256->digest[2] += S[2];
wolfSSL 16:8e0d178b1d1e 871 sha256->digest[3] += S[3];
wolfSSL 16:8e0d178b1d1e 872 sha256->digest[4] += S[4];
wolfSSL 16:8e0d178b1d1e 873 sha256->digest[5] += S[5];
wolfSSL 16:8e0d178b1d1e 874 sha256->digest[6] += S[6];
wolfSSL 16:8e0d178b1d1e 875 sha256->digest[7] += S[7];
wolfSSL 16:8e0d178b1d1e 876
wolfSSL 16:8e0d178b1d1e 877 return 0;
wolfSSL 16:8e0d178b1d1e 878 }
wolfSSL 16:8e0d178b1d1e 879 #endif /* SHA256_MANY_REGISTERS */
wolfSSL 15:117db924cf7c 880 #endif
wolfSSL 15:117db924cf7c 881 /* End wc_ software implementation */
wolfSSL 15:117db924cf7c 882
wolfSSL 15:117db924cf7c 883
wolfSSL 15:117db924cf7c 884 #ifdef XTRANSFORM
wolfSSL 15:117db924cf7c 885
wolfSSL 15:117db924cf7c 886 static WC_INLINE void AddLength(wc_Sha256* sha256, word32 len)
wolfSSL 15:117db924cf7c 887 {
wolfSSL 15:117db924cf7c 888 word32 tmp = sha256->loLen;
wolfSSL 16:8e0d178b1d1e 889 if ((sha256->loLen += len) < tmp) {
wolfSSL 15:117db924cf7c 890 sha256->hiLen++; /* carry low to high */
wolfSSL 16:8e0d178b1d1e 891 }
wolfSSL 15:117db924cf7c 892 }
wolfSSL 15:117db924cf7c 893
wolfSSL 16:8e0d178b1d1e 894 /* do block size increments/updates */
wolfSSL 15:117db924cf7c 895 static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 896 {
wolfSSL 15:117db924cf7c 897 int ret = 0;
wolfSSL 16:8e0d178b1d1e 898 word32 blocksLen;
wolfSSL 15:117db924cf7c 899 byte* local;
wolfSSL 15:117db924cf7c 900
wolfSSL 15:117db924cf7c 901 if (sha256 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 902 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 903 }
wolfSSL 15:117db924cf7c 904
wolfSSL 15:117db924cf7c 905 if (data == NULL && len == 0) {
wolfSSL 15:117db924cf7c 906 /* valid, but do nothing */
wolfSSL 15:117db924cf7c 907 return 0;
wolfSSL 15:117db924cf7c 908 }
wolfSSL 15:117db924cf7c 909
wolfSSL 16:8e0d178b1d1e 910 /* check that internal buffLen is valid */
wolfSSL 16:8e0d178b1d1e 911 if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) {
wolfSSL 16:8e0d178b1d1e 912 return BUFFER_E;
wolfSSL 15:117db924cf7c 913 }
wolfSSL 15:117db924cf7c 914
wolfSSL 16:8e0d178b1d1e 915 /* add length for final */
wolfSSL 16:8e0d178b1d1e 916 AddLength(sha256, len);
wolfSSL 16:8e0d178b1d1e 917
wolfSSL 15:117db924cf7c 918 local = (byte*)sha256->buffer;
wolfSSL 15:117db924cf7c 919
wolfSSL 16:8e0d178b1d1e 920 /* process any remainder from previous operation */
wolfSSL 15:117db924cf7c 921 if (sha256->buffLen > 0) {
wolfSSL 16:8e0d178b1d1e 922 blocksLen = min(len, WC_SHA256_BLOCK_SIZE - sha256->buffLen);
wolfSSL 16:8e0d178b1d1e 923 XMEMCPY(&local[sha256->buffLen], data, blocksLen);
wolfSSL 15:117db924cf7c 924
wolfSSL 16:8e0d178b1d1e 925 sha256->buffLen += blocksLen;
wolfSSL 16:8e0d178b1d1e 926 data += blocksLen;
wolfSSL 16:8e0d178b1d1e 927 len -= blocksLen;
wolfSSL 15:117db924cf7c 928
wolfSSL 15:117db924cf7c 929 if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) {
wolfSSL 16:8e0d178b1d1e 930 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
wolfSSL 16:8e0d178b1d1e 931 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 932 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 16:8e0d178b1d1e 933 #endif
wolfSSL 15:117db924cf7c 934 {
wolfSSL 15:117db924cf7c 935 ByteReverseWords(sha256->buffer, sha256->buffer,
wolfSSL 16:8e0d178b1d1e 936 WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 937 }
wolfSSL 16:8e0d178b1d1e 938 #endif
wolfSSL 16:8e0d178b1d1e 939
wolfSSL 16:8e0d178b1d1e 940 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 941 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 942 if (sha256->ctx.mode == ESP32_SHA_INIT){
wolfSSL 16:8e0d178b1d1e 943 esp_sha_try_hw_lock(&sha256->ctx);
wolfSSL 15:117db924cf7c 944 }
wolfSSL 16:8e0d178b1d1e 945 if (sha256->ctx.mode == ESP32_SHA_SW){
wolfSSL 16:8e0d178b1d1e 946 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 947 } else {
wolfSSL 16:8e0d178b1d1e 948 esp_sha256_process(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 949 }
wolfSSL 16:8e0d178b1d1e 950 #else
wolfSSL 16:8e0d178b1d1e 951 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 952 #endif
wolfSSL 16:8e0d178b1d1e 953
wolfSSL 16:8e0d178b1d1e 954 if (ret == 0)
wolfSSL 15:117db924cf7c 955 sha256->buffLen = 0;
wolfSSL 15:117db924cf7c 956 else
wolfSSL 16:8e0d178b1d1e 957 len = 0; /* error */
wolfSSL 15:117db924cf7c 958 }
wolfSSL 15:117db924cf7c 959 }
wolfSSL 15:117db924cf7c 960
wolfSSL 16:8e0d178b1d1e 961 /* process blocks */
wolfSSL 16:8e0d178b1d1e 962 #ifdef XTRANSFORM_LEN
wolfSSL 16:8e0d178b1d1e 963 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 964 if (Transform_Sha256_Len_p != NULL)
wolfSSL 16:8e0d178b1d1e 965 #endif
wolfSSL 16:8e0d178b1d1e 966 {
wolfSSL 16:8e0d178b1d1e 967 /* get number of blocks */
wolfSSL 16:8e0d178b1d1e 968 /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
wolfSSL 16:8e0d178b1d1e 969 /* len (masked by 0xFFFFFFC0) returns block aligned length */
wolfSSL 16:8e0d178b1d1e 970 blocksLen = len & ~(WC_SHA256_BLOCK_SIZE-1);
wolfSSL 15:117db924cf7c 971 if (blocksLen > 0) {
wolfSSL 16:8e0d178b1d1e 972 /* Byte reversal and alignment handled in function if required */
wolfSSL 16:8e0d178b1d1e 973 XTRANSFORM_LEN(sha256, data, blocksLen);
wolfSSL 15:117db924cf7c 974 data += blocksLen;
wolfSSL 15:117db924cf7c 975 len -= blocksLen;
wolfSSL 15:117db924cf7c 976 }
wolfSSL 15:117db924cf7c 977 }
wolfSSL 16:8e0d178b1d1e 978 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 979 else
wolfSSL 16:8e0d178b1d1e 980 #endif
wolfSSL 16:8e0d178b1d1e 981 #endif /* XTRANSFORM_LEN */
wolfSSL 16:8e0d178b1d1e 982 #if !defined(XTRANSFORM_LEN) || defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 983 {
wolfSSL 15:117db924cf7c 984 while (len >= WC_SHA256_BLOCK_SIZE) {
wolfSSL 16:8e0d178b1d1e 985 word32* local32 = sha256->buffer;
wolfSSL 16:8e0d178b1d1e 986 /* optimization to avoid memcpy if data pointer is properly aligned */
wolfSSL 16:8e0d178b1d1e 987 /* Intel transform function requires use of sha256->buffer */
wolfSSL 16:8e0d178b1d1e 988 /* Little Endian requires byte swap, so can't use data directly */
wolfSSL 16:8e0d178b1d1e 989 #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) && \
wolfSSL 16:8e0d178b1d1e 990 !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 991 if (((size_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
wolfSSL 16:8e0d178b1d1e 992 local32 = (word32*)data;
wolfSSL 16:8e0d178b1d1e 993 }
wolfSSL 16:8e0d178b1d1e 994 else
wolfSSL 16:8e0d178b1d1e 995 #endif
wolfSSL 16:8e0d178b1d1e 996 {
wolfSSL 16:8e0d178b1d1e 997 XMEMCPY(local32, data, WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 998 }
wolfSSL 15:117db924cf7c 999
wolfSSL 15:117db924cf7c 1000 data += WC_SHA256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1001 len -= WC_SHA256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1002
wolfSSL 16:8e0d178b1d1e 1003 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
wolfSSL 16:8e0d178b1d1e 1004 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 1005 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 16:8e0d178b1d1e 1006 #endif
wolfSSL 16:8e0d178b1d1e 1007 {
wolfSSL 16:8e0d178b1d1e 1008 ByteReverseWords(local32, local32, WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 1009 }
wolfSSL 16:8e0d178b1d1e 1010 #endif
wolfSSL 15:117db924cf7c 1011
wolfSSL 16:8e0d178b1d1e 1012 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1013 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1014 if (sha256->ctx.mode == ESP32_SHA_INIT){
wolfSSL 16:8e0d178b1d1e 1015 esp_sha_try_hw_lock(&sha256->ctx);
wolfSSL 16:8e0d178b1d1e 1016 }
wolfSSL 16:8e0d178b1d1e 1017 if (sha256->ctx.mode == ESP32_SHA_SW){
wolfSSL 16:8e0d178b1d1e 1018 ret = XTRANSFORM(sha256, (const byte*)local32);
wolfSSL 16:8e0d178b1d1e 1019 } else {
wolfSSL 16:8e0d178b1d1e 1020 esp_sha256_process(sha256, (const byte*)local32);
wolfSSL 16:8e0d178b1d1e 1021 }
wolfSSL 16:8e0d178b1d1e 1022 #else
wolfSSL 16:8e0d178b1d1e 1023 ret = XTRANSFORM(sha256, (const byte*)local32);
wolfSSL 16:8e0d178b1d1e 1024 #endif
wolfSSL 15:117db924cf7c 1025
wolfSSL 15:117db924cf7c 1026 if (ret != 0)
wolfSSL 15:117db924cf7c 1027 break;
wolfSSL 15:117db924cf7c 1028 }
wolfSSL 15:117db924cf7c 1029 }
wolfSSL 15:117db924cf7c 1030 #endif
wolfSSL 15:117db924cf7c 1031
wolfSSL 16:8e0d178b1d1e 1032 /* save remainder */
wolfSSL 15:117db924cf7c 1033 if (len > 0) {
wolfSSL 15:117db924cf7c 1034 XMEMCPY(local, data, len);
wolfSSL 15:117db924cf7c 1035 sha256->buffLen = len;
wolfSSL 15:117db924cf7c 1036 }
wolfSSL 15:117db924cf7c 1037
wolfSSL 15:117db924cf7c 1038 return ret;
wolfSSL 15:117db924cf7c 1039 }
wolfSSL 15:117db924cf7c 1040
wolfSSL 15:117db924cf7c 1041 int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 1042 {
wolfSSL 16:8e0d178b1d1e 1043 if (sha256 == NULL || (data == NULL && len > 0)) {
wolfSSL 16:8e0d178b1d1e 1044 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 1045 }
wolfSSL 16:8e0d178b1d1e 1046
wolfSSL 16:8e0d178b1d1e 1047 if (data == NULL && len == 0) {
wolfSSL 16:8e0d178b1d1e 1048 /* valid, but do nothing */
wolfSSL 16:8e0d178b1d1e 1049 return 0;
wolfSSL 16:8e0d178b1d1e 1050 }
wolfSSL 16:8e0d178b1d1e 1051
wolfSSL 16:8e0d178b1d1e 1052 #ifdef WOLF_CRYPTO_CB
wolfSSL 16:8e0d178b1d1e 1053 if (sha256->devId != INVALID_DEVID) {
wolfSSL 16:8e0d178b1d1e 1054 int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
wolfSSL 16:8e0d178b1d1e 1055 if (ret != CRYPTOCB_UNAVAILABLE)
wolfSSL 16:8e0d178b1d1e 1056 return ret;
wolfSSL 16:8e0d178b1d1e 1057 /* fall-through when unavailable */
wolfSSL 16:8e0d178b1d1e 1058 }
wolfSSL 16:8e0d178b1d1e 1059 #endif
wolfSSL 16:8e0d178b1d1e 1060 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
wolfSSL 16:8e0d178b1d1e 1061 if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
wolfSSL 16:8e0d178b1d1e 1062 #if defined(HAVE_INTEL_QA)
wolfSSL 16:8e0d178b1d1e 1063 return IntelQaSymSha256(&sha256->asyncDev, NULL, data, len);
wolfSSL 16:8e0d178b1d1e 1064 #endif
wolfSSL 16:8e0d178b1d1e 1065 }
wolfSSL 16:8e0d178b1d1e 1066 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 16:8e0d178b1d1e 1067
wolfSSL 15:117db924cf7c 1068 return Sha256Update(sha256, data, len);
wolfSSL 15:117db924cf7c 1069 }
wolfSSL 15:117db924cf7c 1070
wolfSSL 15:117db924cf7c 1071 static WC_INLINE int Sha256Final(wc_Sha256* sha256)
wolfSSL 15:117db924cf7c 1072 {
wolfSSL 15:117db924cf7c 1073
wolfSSL 15:117db924cf7c 1074 int ret;
wolfSSL 16:8e0d178b1d1e 1075 byte* local;
wolfSSL 15:117db924cf7c 1076
wolfSSL 15:117db924cf7c 1077 if (sha256 == NULL) {
wolfSSL 15:117db924cf7c 1078 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1079 }
wolfSSL 15:117db924cf7c 1080
wolfSSL 16:8e0d178b1d1e 1081 local = (byte*)sha256->buffer;
wolfSSL 16:8e0d178b1d1e 1082 local[sha256->buffLen++] = 0x80; /* add 1 */
wolfSSL 15:117db924cf7c 1083
wolfSSL 15:117db924cf7c 1084 /* pad with zeros */
wolfSSL 15:117db924cf7c 1085 if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
wolfSSL 15:117db924cf7c 1086 XMEMSET(&local[sha256->buffLen], 0,
wolfSSL 15:117db924cf7c 1087 WC_SHA256_BLOCK_SIZE - sha256->buffLen);
wolfSSL 15:117db924cf7c 1088 sha256->buffLen += WC_SHA256_BLOCK_SIZE - sha256->buffLen;
wolfSSL 15:117db924cf7c 1089
wolfSSL 15:117db924cf7c 1090 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
wolfSSL 15:117db924cf7c 1091 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 1092 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 1093 #endif
wolfSSL 16:8e0d178b1d1e 1094 {
wolfSSL 16:8e0d178b1d1e 1095 ByteReverseWords(sha256->buffer, sha256->buffer,
wolfSSL 16:8e0d178b1d1e 1096 WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 1097 }
wolfSSL 15:117db924cf7c 1098 #endif
wolfSSL 16:8e0d178b1d1e 1099
wolfSSL 16:8e0d178b1d1e 1100 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1101 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1102 if (sha256->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 1103 esp_sha_try_hw_lock(&sha256->ctx);
wolfSSL 15:117db924cf7c 1104 }
wolfSSL 16:8e0d178b1d1e 1105 if (sha256->ctx.mode == ESP32_SHA_SW) {
wolfSSL 16:8e0d178b1d1e 1106 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 1107 } else {
wolfSSL 16:8e0d178b1d1e 1108 ret = esp_sha256_process(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 1109 }
wolfSSL 16:8e0d178b1d1e 1110 #else
wolfSSL 16:8e0d178b1d1e 1111 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 1112 #endif
wolfSSL 15:117db924cf7c 1113 if (ret != 0)
wolfSSL 15:117db924cf7c 1114 return ret;
wolfSSL 15:117db924cf7c 1115
wolfSSL 15:117db924cf7c 1116 sha256->buffLen = 0;
wolfSSL 15:117db924cf7c 1117 }
wolfSSL 16:8e0d178b1d1e 1118 XMEMSET(&local[sha256->buffLen], 0,
wolfSSL 16:8e0d178b1d1e 1119 WC_SHA256_PAD_SIZE - sha256->buffLen);
wolfSSL 15:117db924cf7c 1120
wolfSSL 15:117db924cf7c 1121 /* put lengths in bits */
wolfSSL 15:117db924cf7c 1122 sha256->hiLen = (sha256->loLen >> (8 * sizeof(sha256->loLen) - 3)) +
wolfSSL 15:117db924cf7c 1123 (sha256->hiLen << 3);
wolfSSL 15:117db924cf7c 1124 sha256->loLen = sha256->loLen << 3;
wolfSSL 15:117db924cf7c 1125
wolfSSL 15:117db924cf7c 1126 /* store lengths */
wolfSSL 15:117db924cf7c 1127 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
wolfSSL 15:117db924cf7c 1128 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 1129 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 1130 #endif
wolfSSL 16:8e0d178b1d1e 1131 {
wolfSSL 16:8e0d178b1d1e 1132 ByteReverseWords(sha256->buffer, sha256->buffer,
wolfSSL 16:8e0d178b1d1e 1133 WC_SHA256_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 1134 }
wolfSSL 15:117db924cf7c 1135 #endif
wolfSSL 15:117db924cf7c 1136 /* ! length ordering dependent on digest endian type ! */
wolfSSL 15:117db924cf7c 1137 XMEMCPY(&local[WC_SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
wolfSSL 15:117db924cf7c 1138 XMEMCPY(&local[WC_SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
wolfSSL 15:117db924cf7c 1139 sizeof(word32));
wolfSSL 15:117db924cf7c 1140
wolfSSL 15:117db924cf7c 1141 #if defined(FREESCALE_MMCAU_SHA) || defined(HAVE_INTEL_AVX1) || \
wolfSSL 15:117db924cf7c 1142 defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 1143 /* Kinetis requires only these bytes reversed */
wolfSSL 15:117db924cf7c 1144 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 1145 if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 1146 #endif
wolfSSL 16:8e0d178b1d1e 1147 {
wolfSSL 16:8e0d178b1d1e 1148 ByteReverseWords(
wolfSSL 16:8e0d178b1d1e 1149 &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
wolfSSL 16:8e0d178b1d1e 1150 &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
wolfSSL 16:8e0d178b1d1e 1151 2 * sizeof(word32));
wolfSSL 16:8e0d178b1d1e 1152 }
wolfSSL 15:117db924cf7c 1153 #endif
wolfSSL 15:117db924cf7c 1154
wolfSSL 16:8e0d178b1d1e 1155 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1156 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1157 if (sha256->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 1158 esp_sha_try_hw_lock(&sha256->ctx);
wolfSSL 16:8e0d178b1d1e 1159 }
wolfSSL 16:8e0d178b1d1e 1160 if (sha256->ctx.mode == ESP32_SHA_SW) {
wolfSSL 16:8e0d178b1d1e 1161 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 1162 } else {
wolfSSL 16:8e0d178b1d1e 1163 ret = esp_sha256_digest_process(sha256, 1);
wolfSSL 16:8e0d178b1d1e 1164 }
wolfSSL 16:8e0d178b1d1e 1165 #else
wolfSSL 16:8e0d178b1d1e 1166 ret = XTRANSFORM(sha256, (const byte*)local);
wolfSSL 16:8e0d178b1d1e 1167 #endif
wolfSSL 16:8e0d178b1d1e 1168
wolfSSL 16:8e0d178b1d1e 1169 return ret;
wolfSSL 15:117db924cf7c 1170 }
wolfSSL 15:117db924cf7c 1171
wolfSSL 15:117db924cf7c 1172 int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
wolfSSL 15:117db924cf7c 1173 {
wolfSSL 15:117db924cf7c 1174 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1175 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
wolfSSL 15:117db924cf7c 1176 #endif
wolfSSL 15:117db924cf7c 1177
wolfSSL 15:117db924cf7c 1178 if (sha256 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 1179 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1180 }
wolfSSL 15:117db924cf7c 1181
wolfSSL 15:117db924cf7c 1182 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1183 ByteReverseWords((word32*)digest, (word32*)sha256->digest,
wolfSSL 15:117db924cf7c 1184 WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1185 XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1186 #else
wolfSSL 15:117db924cf7c 1187 XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1188 #endif
wolfSSL 15:117db924cf7c 1189
wolfSSL 15:117db924cf7c 1190 return 0;
wolfSSL 15:117db924cf7c 1191 }
wolfSSL 15:117db924cf7c 1192
wolfSSL 15:117db924cf7c 1193 int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
wolfSSL 15:117db924cf7c 1194 {
wolfSSL 15:117db924cf7c 1195 int ret;
wolfSSL 15:117db924cf7c 1196
wolfSSL 15:117db924cf7c 1197 if (sha256 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 1198 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1199 }
wolfSSL 15:117db924cf7c 1200
wolfSSL 16:8e0d178b1d1e 1201 #ifdef WOLF_CRYPTO_CB
wolfSSL 16:8e0d178b1d1e 1202 if (sha256->devId != INVALID_DEVID) {
wolfSSL 16:8e0d178b1d1e 1203 ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
wolfSSL 16:8e0d178b1d1e 1204 if (ret != CRYPTOCB_UNAVAILABLE)
wolfSSL 16:8e0d178b1d1e 1205 return ret;
wolfSSL 16:8e0d178b1d1e 1206 /* fall-through when unavailable */
wolfSSL 16:8e0d178b1d1e 1207 }
wolfSSL 16:8e0d178b1d1e 1208 #endif
wolfSSL 16:8e0d178b1d1e 1209
wolfSSL 15:117db924cf7c 1210 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
wolfSSL 15:117db924cf7c 1211 if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
wolfSSL 15:117db924cf7c 1212 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 1213 return IntelQaSymSha256(&sha256->asyncDev, hash, NULL,
wolfSSL 15:117db924cf7c 1214 WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1215 #endif
wolfSSL 15:117db924cf7c 1216 }
wolfSSL 15:117db924cf7c 1217 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1218
wolfSSL 15:117db924cf7c 1219 ret = Sha256Final(sha256);
wolfSSL 15:117db924cf7c 1220 if (ret != 0)
wolfSSL 15:117db924cf7c 1221 return ret;
wolfSSL 15:117db924cf7c 1222
wolfSSL 15:117db924cf7c 1223 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 15:117db924cf7c 1224 ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1225 #endif
wolfSSL 15:117db924cf7c 1226 XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1227
wolfSSL 15:117db924cf7c 1228 return InitSha256(sha256); /* reset state */
wolfSSL 15:117db924cf7c 1229 }
wolfSSL 15:117db924cf7c 1230
wolfSSL 15:117db924cf7c 1231 #endif /* XTRANSFORM */
wolfSSL 15:117db924cf7c 1232
wolfSSL 15:117db924cf7c 1233 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 1234
wolfSSL 15:117db924cf7c 1235 #ifdef STM32_HASH_SHA2
wolfSSL 15:117db924cf7c 1236
wolfSSL 15:117db924cf7c 1237 /* Supports CubeMX HAL or Standard Peripheral Library */
wolfSSL 15:117db924cf7c 1238
wolfSSL 15:117db924cf7c 1239 int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
wolfSSL 15:117db924cf7c 1240 {
wolfSSL 15:117db924cf7c 1241 if (sha224 == NULL)
wolfSSL 15:117db924cf7c 1242 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1243
wolfSSL 15:117db924cf7c 1244 (void)devId;
wolfSSL 15:117db924cf7c 1245 (void)heap;
wolfSSL 15:117db924cf7c 1246
wolfSSL 15:117db924cf7c 1247 wc_Stm32_Hash_Init(&sha224->stmCtx);
wolfSSL 15:117db924cf7c 1248 return 0;
wolfSSL 15:117db924cf7c 1249 }
wolfSSL 15:117db924cf7c 1250
wolfSSL 15:117db924cf7c 1251 int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 1252 {
wolfSSL 15:117db924cf7c 1253 int ret = 0;
wolfSSL 15:117db924cf7c 1254
wolfSSL 15:117db924cf7c 1255 if (sha224 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 1256 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1257 }
wolfSSL 15:117db924cf7c 1258
wolfSSL 15:117db924cf7c 1259 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 1260 if (ret == 0) {
wolfSSL 15:117db924cf7c 1261 ret = wc_Stm32_Hash_Update(&sha224->stmCtx,
wolfSSL 15:117db924cf7c 1262 HASH_AlgoSelection_SHA224, data, len);
wolfSSL 15:117db924cf7c 1263 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 1264 }
wolfSSL 15:117db924cf7c 1265 return ret;
wolfSSL 15:117db924cf7c 1266 }
wolfSSL 15:117db924cf7c 1267
wolfSSL 15:117db924cf7c 1268 int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
wolfSSL 15:117db924cf7c 1269 {
wolfSSL 15:117db924cf7c 1270 int ret = 0;
wolfSSL 15:117db924cf7c 1271
wolfSSL 15:117db924cf7c 1272 if (sha224 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 1273 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1274 }
wolfSSL 15:117db924cf7c 1275
wolfSSL 15:117db924cf7c 1276 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 1277 if (ret == 0) {
wolfSSL 15:117db924cf7c 1278 ret = wc_Stm32_Hash_Final(&sha224->stmCtx,
wolfSSL 15:117db924cf7c 1279 HASH_AlgoSelection_SHA224, hash, WC_SHA224_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1280 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 1281 }
wolfSSL 15:117db924cf7c 1282
wolfSSL 15:117db924cf7c 1283 (void)wc_InitSha224(sha224); /* reset state */
wolfSSL 15:117db924cf7c 1284
wolfSSL 15:117db924cf7c 1285 return ret;
wolfSSL 15:117db924cf7c 1286 }
wolfSSL 15:117db924cf7c 1287
wolfSSL 15:117db924cf7c 1288 #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
wolfSSL 15:117db924cf7c 1289 /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
wolfSSL 16:8e0d178b1d1e 1290
wolfSSL 16:8e0d178b1d1e 1291 #elif defined(WOLFSSL_AFALG_HASH)
wolfSSL 16:8e0d178b1d1e 1292 #error SHA224 currently not supported with AF_ALG enabled
wolfSSL 16:8e0d178b1d1e 1293
wolfSSL 16:8e0d178b1d1e 1294 #elif defined(WOLFSSL_DEVCRYPTO_HASH)
wolfSSL 16:8e0d178b1d1e 1295 /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
wolfSSL 16:8e0d178b1d1e 1296
wolfSSL 15:117db924cf7c 1297 #else
wolfSSL 15:117db924cf7c 1298
wolfSSL 15:117db924cf7c 1299 #define NEED_SOFT_SHA224
wolfSSL 15:117db924cf7c 1300
wolfSSL 15:117db924cf7c 1301
wolfSSL 15:117db924cf7c 1302 static int InitSha224(wc_Sha224* sha224)
wolfSSL 15:117db924cf7c 1303 {
wolfSSL 15:117db924cf7c 1304 int ret = 0;
wolfSSL 15:117db924cf7c 1305
wolfSSL 15:117db924cf7c 1306 if (sha224 == NULL) {
wolfSSL 15:117db924cf7c 1307 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1308 }
wolfSSL 15:117db924cf7c 1309
wolfSSL 15:117db924cf7c 1310 sha224->digest[0] = 0xc1059ed8;
wolfSSL 15:117db924cf7c 1311 sha224->digest[1] = 0x367cd507;
wolfSSL 15:117db924cf7c 1312 sha224->digest[2] = 0x3070dd17;
wolfSSL 15:117db924cf7c 1313 sha224->digest[3] = 0xf70e5939;
wolfSSL 15:117db924cf7c 1314 sha224->digest[4] = 0xffc00b31;
wolfSSL 15:117db924cf7c 1315 sha224->digest[5] = 0x68581511;
wolfSSL 15:117db924cf7c 1316 sha224->digest[6] = 0x64f98fa7;
wolfSSL 15:117db924cf7c 1317 sha224->digest[7] = 0xbefa4fa4;
wolfSSL 15:117db924cf7c 1318
wolfSSL 15:117db924cf7c 1319 sha224->buffLen = 0;
wolfSSL 15:117db924cf7c 1320 sha224->loLen = 0;
wolfSSL 15:117db924cf7c 1321 sha224->hiLen = 0;
wolfSSL 15:117db924cf7c 1322
wolfSSL 15:117db924cf7c 1323 #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 1324 /* choose best Transform function under this runtime environment */
wolfSSL 15:117db924cf7c 1325 Sha256_SetTransform();
wolfSSL 15:117db924cf7c 1326 #endif
wolfSSL 16:8e0d178b1d1e 1327 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1328 sha224->flags = 0;
wolfSSL 16:8e0d178b1d1e 1329 #endif
wolfSSL 15:117db924cf7c 1330
wolfSSL 15:117db924cf7c 1331 return ret;
wolfSSL 15:117db924cf7c 1332 }
wolfSSL 15:117db924cf7c 1333
wolfSSL 15:117db924cf7c 1334 #endif
wolfSSL 15:117db924cf7c 1335
wolfSSL 15:117db924cf7c 1336 #ifdef NEED_SOFT_SHA224
wolfSSL 15:117db924cf7c 1337 int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
wolfSSL 15:117db924cf7c 1338 {
wolfSSL 15:117db924cf7c 1339 int ret = 0;
wolfSSL 15:117db924cf7c 1340
wolfSSL 15:117db924cf7c 1341 if (sha224 == NULL)
wolfSSL 15:117db924cf7c 1342 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1343
wolfSSL 15:117db924cf7c 1344 sha224->heap = heap;
wolfSSL 15:117db924cf7c 1345
wolfSSL 15:117db924cf7c 1346 ret = InitSha224(sha224);
wolfSSL 15:117db924cf7c 1347 if (ret != 0)
wolfSSL 15:117db924cf7c 1348 return ret;
wolfSSL 15:117db924cf7c 1349
wolfSSL 15:117db924cf7c 1350 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1351 sha224->W = NULL;
wolfSSL 15:117db924cf7c 1352 #endif
wolfSSL 15:117db924cf7c 1353
wolfSSL 15:117db924cf7c 1354 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
wolfSSL 15:117db924cf7c 1355 ret = wolfAsync_DevCtxInit(&sha224->asyncDev,
wolfSSL 15:117db924cf7c 1356 WOLFSSL_ASYNC_MARKER_SHA224, sha224->heap, devId);
wolfSSL 15:117db924cf7c 1357 #else
wolfSSL 15:117db924cf7c 1358 (void)devId;
wolfSSL 15:117db924cf7c 1359 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1360
wolfSSL 15:117db924cf7c 1361 return ret;
wolfSSL 15:117db924cf7c 1362 }
wolfSSL 15:117db924cf7c 1363
wolfSSL 15:117db924cf7c 1364 int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 1365 {
wolfSSL 15:117db924cf7c 1366 int ret;
wolfSSL 15:117db924cf7c 1367
wolfSSL 15:117db924cf7c 1368 if (sha224 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 1369 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1370 }
wolfSSL 15:117db924cf7c 1371
wolfSSL 15:117db924cf7c 1372 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
wolfSSL 15:117db924cf7c 1373 if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
wolfSSL 15:117db924cf7c 1374 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 1375 return IntelQaSymSha224(&sha224->asyncDev, NULL, data, len);
wolfSSL 15:117db924cf7c 1376 #endif
wolfSSL 15:117db924cf7c 1377 }
wolfSSL 15:117db924cf7c 1378 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1379
wolfSSL 15:117db924cf7c 1380 ret = Sha256Update((wc_Sha256*)sha224, data, len);
wolfSSL 15:117db924cf7c 1381
wolfSSL 15:117db924cf7c 1382 return ret;
wolfSSL 15:117db924cf7c 1383 }
wolfSSL 15:117db924cf7c 1384
wolfSSL 15:117db924cf7c 1385 int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
wolfSSL 15:117db924cf7c 1386 {
wolfSSL 15:117db924cf7c 1387 int ret;
wolfSSL 15:117db924cf7c 1388
wolfSSL 15:117db924cf7c 1389 if (sha224 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 1390 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1391 }
wolfSSL 15:117db924cf7c 1392
wolfSSL 15:117db924cf7c 1393 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
wolfSSL 15:117db924cf7c 1394 if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
wolfSSL 15:117db924cf7c 1395 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 1396 return IntelQaSymSha224(&sha224->asyncDev, hash, NULL,
wolfSSL 15:117db924cf7c 1397 WC_SHA224_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1398 #endif
wolfSSL 15:117db924cf7c 1399 }
wolfSSL 15:117db924cf7c 1400 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1401
wolfSSL 15:117db924cf7c 1402 ret = Sha256Final((wc_Sha256*)sha224);
wolfSSL 15:117db924cf7c 1403 if (ret != 0)
wolfSSL 15:117db924cf7c 1404 return ret;
wolfSSL 15:117db924cf7c 1405
wolfSSL 15:117db924cf7c 1406 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 15:117db924cf7c 1407 ByteReverseWords(sha224->digest, sha224->digest, WC_SHA224_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1408 #endif
wolfSSL 15:117db924cf7c 1409 XMEMCPY(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1410
wolfSSL 15:117db924cf7c 1411 return InitSha224(sha224); /* reset state */
wolfSSL 15:117db924cf7c 1412 }
wolfSSL 15:117db924cf7c 1413 #endif /* end of SHA224 software implementation */
wolfSSL 15:117db924cf7c 1414
wolfSSL 15:117db924cf7c 1415 int wc_InitSha224(wc_Sha224* sha224)
wolfSSL 15:117db924cf7c 1416 {
wolfSSL 15:117db924cf7c 1417 return wc_InitSha224_ex(sha224, NULL, INVALID_DEVID);
wolfSSL 15:117db924cf7c 1418 }
wolfSSL 15:117db924cf7c 1419
wolfSSL 15:117db924cf7c 1420 void wc_Sha224Free(wc_Sha224* sha224)
wolfSSL 15:117db924cf7c 1421 {
wolfSSL 15:117db924cf7c 1422 if (sha224 == NULL)
wolfSSL 15:117db924cf7c 1423 return;
wolfSSL 15:117db924cf7c 1424
wolfSSL 15:117db924cf7c 1425 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1426 if (sha224->W != NULL) {
wolfSSL 16:8e0d178b1d1e 1427 XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
wolfSSL 15:117db924cf7c 1428 sha224->W = NULL;
wolfSSL 15:117db924cf7c 1429 }
wolfSSL 15:117db924cf7c 1430 #endif
wolfSSL 15:117db924cf7c 1431
wolfSSL 15:117db924cf7c 1432 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
wolfSSL 15:117db924cf7c 1433 wolfAsync_DevCtxFree(&sha224->asyncDev, WOLFSSL_ASYNC_MARKER_SHA224);
wolfSSL 15:117db924cf7c 1434 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 16:8e0d178b1d1e 1435
wolfSSL 16:8e0d178b1d1e 1436 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 16:8e0d178b1d1e 1437 wc_Sha256Pic32Free(sha224);
wolfSSL 16:8e0d178b1d1e 1438 #endif
wolfSSL 15:117db924cf7c 1439 }
wolfSSL 15:117db924cf7c 1440 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 1441
wolfSSL 15:117db924cf7c 1442
wolfSSL 15:117db924cf7c 1443 int wc_InitSha256(wc_Sha256* sha256)
wolfSSL 15:117db924cf7c 1444 {
wolfSSL 15:117db924cf7c 1445 return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
wolfSSL 15:117db924cf7c 1446 }
wolfSSL 15:117db924cf7c 1447
wolfSSL 15:117db924cf7c 1448 void wc_Sha256Free(wc_Sha256* sha256)
wolfSSL 15:117db924cf7c 1449 {
wolfSSL 15:117db924cf7c 1450 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 1451 return;
wolfSSL 15:117db924cf7c 1452
wolfSSL 15:117db924cf7c 1453 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1454 if (sha256->W != NULL) {
wolfSSL 16:8e0d178b1d1e 1455 XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
wolfSSL 15:117db924cf7c 1456 sha256->W = NULL;
wolfSSL 15:117db924cf7c 1457 }
wolfSSL 15:117db924cf7c 1458 #endif
wolfSSL 15:117db924cf7c 1459
wolfSSL 15:117db924cf7c 1460 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
wolfSSL 15:117db924cf7c 1461 wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
wolfSSL 15:117db924cf7c 1462 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 16:8e0d178b1d1e 1463 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 16:8e0d178b1d1e 1464 wc_Sha256Pic32Free(sha256);
wolfSSL 16:8e0d178b1d1e 1465 #endif
wolfSSL 16:8e0d178b1d1e 1466 #if defined(WOLFSSL_AFALG_HASH)
wolfSSL 16:8e0d178b1d1e 1467 if (sha256->alFd > 0) {
wolfSSL 16:8e0d178b1d1e 1468 close(sha256->alFd);
wolfSSL 16:8e0d178b1d1e 1469 sha256->alFd = -1; /* avoid possible double close on socket */
wolfSSL 16:8e0d178b1d1e 1470 }
wolfSSL 16:8e0d178b1d1e 1471 if (sha256->rdFd > 0) {
wolfSSL 16:8e0d178b1d1e 1472 close(sha256->rdFd);
wolfSSL 16:8e0d178b1d1e 1473 sha256->rdFd = -1; /* avoid possible double close on socket */
wolfSSL 16:8e0d178b1d1e 1474 }
wolfSSL 16:8e0d178b1d1e 1475 #endif /* WOLFSSL_AFALG_HASH */
wolfSSL 16:8e0d178b1d1e 1476 #ifdef WOLFSSL_DEVCRYPTO_HASH
wolfSSL 16:8e0d178b1d1e 1477 wc_DevCryptoFree(&sha256->ctx);
wolfSSL 16:8e0d178b1d1e 1478 #endif /* WOLFSSL_DEVCRYPTO */
wolfSSL 16:8e0d178b1d1e 1479 #if (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
wolfSSL 16:8e0d178b1d1e 1480 (defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
wolfSSL 16:8e0d178b1d1e 1481 (defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1482 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH))
wolfSSL 16:8e0d178b1d1e 1483 if (sha256->msg != NULL) {
wolfSSL 16:8e0d178b1d1e 1484 XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1485 sha256->msg = NULL;
wolfSSL 16:8e0d178b1d1e 1486 }
wolfSSL 16:8e0d178b1d1e 1487 #endif
wolfSSL 15:117db924cf7c 1488 }
wolfSSL 15:117db924cf7c 1489
wolfSSL 15:117db924cf7c 1490 #endif /* !WOLFSSL_TI_HASH */
wolfSSL 15:117db924cf7c 1491 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 1492
wolfSSL 15:117db924cf7c 1493
wolfSSL 15:117db924cf7c 1494 #ifndef WOLFSSL_TI_HASH
wolfSSL 15:117db924cf7c 1495 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 1496 int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
wolfSSL 15:117db924cf7c 1497 {
wolfSSL 15:117db924cf7c 1498 int ret;
wolfSSL 15:117db924cf7c 1499 wc_Sha224 tmpSha224;
wolfSSL 15:117db924cf7c 1500
wolfSSL 15:117db924cf7c 1501 if (sha224 == NULL || hash == NULL)
wolfSSL 15:117db924cf7c 1502 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1503
wolfSSL 15:117db924cf7c 1504 ret = wc_Sha224Copy(sha224, &tmpSha224);
wolfSSL 15:117db924cf7c 1505 if (ret == 0) {
wolfSSL 15:117db924cf7c 1506 ret = wc_Sha224Final(&tmpSha224, hash);
wolfSSL 15:117db924cf7c 1507 wc_Sha224Free(&tmpSha224);
wolfSSL 15:117db924cf7c 1508 }
wolfSSL 15:117db924cf7c 1509 return ret;
wolfSSL 15:117db924cf7c 1510 }
wolfSSL 15:117db924cf7c 1511 int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
wolfSSL 15:117db924cf7c 1512 {
wolfSSL 15:117db924cf7c 1513 int ret = 0;
wolfSSL 15:117db924cf7c 1514
wolfSSL 15:117db924cf7c 1515 if (src == NULL || dst == NULL)
wolfSSL 15:117db924cf7c 1516 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1517
wolfSSL 15:117db924cf7c 1518 XMEMCPY(dst, src, sizeof(wc_Sha224));
wolfSSL 15:117db924cf7c 1519 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1520 dst->W = NULL;
wolfSSL 15:117db924cf7c 1521 #endif
wolfSSL 15:117db924cf7c 1522
wolfSSL 15:117db924cf7c 1523 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 1524 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 15:117db924cf7c 1525 #endif
wolfSSL 16:8e0d178b1d1e 1526 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1527 dst->flags |= WC_HASH_FLAG_ISCOPY;
wolfSSL 16:8e0d178b1d1e 1528 #endif
wolfSSL 15:117db924cf7c 1529
wolfSSL 15:117db924cf7c 1530 return ret;
wolfSSL 15:117db924cf7c 1531 }
wolfSSL 16:8e0d178b1d1e 1532
wolfSSL 16:8e0d178b1d1e 1533 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1534 int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags)
wolfSSL 16:8e0d178b1d1e 1535 {
wolfSSL 16:8e0d178b1d1e 1536 if (sha224) {
wolfSSL 16:8e0d178b1d1e 1537 sha224->flags = flags;
wolfSSL 16:8e0d178b1d1e 1538 }
wolfSSL 16:8e0d178b1d1e 1539 return 0;
wolfSSL 16:8e0d178b1d1e 1540 }
wolfSSL 16:8e0d178b1d1e 1541 int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags)
wolfSSL 16:8e0d178b1d1e 1542 {
wolfSSL 16:8e0d178b1d1e 1543 if (sha224 && flags) {
wolfSSL 16:8e0d178b1d1e 1544 *flags = sha224->flags;
wolfSSL 16:8e0d178b1d1e 1545 }
wolfSSL 16:8e0d178b1d1e 1546 return 0;
wolfSSL 16:8e0d178b1d1e 1547 }
wolfSSL 16:8e0d178b1d1e 1548 #endif
wolfSSL 16:8e0d178b1d1e 1549
wolfSSL 15:117db924cf7c 1550 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 1551
wolfSSL 16:8e0d178b1d1e 1552 #ifdef WOLFSSL_AFALG_HASH
wolfSSL 16:8e0d178b1d1e 1553 /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
wolfSSL 16:8e0d178b1d1e 1554
wolfSSL 16:8e0d178b1d1e 1555 #elif defined(WOLFSSL_DEVCRYPTO_HASH)
wolfSSL 16:8e0d178b1d1e 1556 /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
wolfSSL 16:8e0d178b1d1e 1557
wolfSSL 16:8e0d178b1d1e 1558 #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1559 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1560
wolfSSL 16:8e0d178b1d1e 1561 /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
wolfSSL 16:8e0d178b1d1e 1562 #else
wolfSSL 16:8e0d178b1d1e 1563
wolfSSL 15:117db924cf7c 1564 int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
wolfSSL 15:117db924cf7c 1565 {
wolfSSL 15:117db924cf7c 1566 int ret;
wolfSSL 15:117db924cf7c 1567 wc_Sha256 tmpSha256;
wolfSSL 15:117db924cf7c 1568
wolfSSL 15:117db924cf7c 1569 if (sha256 == NULL || hash == NULL)
wolfSSL 15:117db924cf7c 1570 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1571
wolfSSL 16:8e0d178b1d1e 1572 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1573 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1574 if(sha256->ctx.mode == ESP32_SHA_INIT){
wolfSSL 16:8e0d178b1d1e 1575 esp_sha_try_hw_lock(&sha256->ctx);
wolfSSL 16:8e0d178b1d1e 1576 }
wolfSSL 16:8e0d178b1d1e 1577 if(sha256->ctx.mode == ESP32_SHA_HW)
wolfSSL 16:8e0d178b1d1e 1578 {
wolfSSL 16:8e0d178b1d1e 1579 esp_sha256_digest_process(sha256, 0);
wolfSSL 16:8e0d178b1d1e 1580 }
wolfSSL 16:8e0d178b1d1e 1581 #endif
wolfSSL 15:117db924cf7c 1582 ret = wc_Sha256Copy(sha256, &tmpSha256);
wolfSSL 15:117db924cf7c 1583 if (ret == 0) {
wolfSSL 15:117db924cf7c 1584 ret = wc_Sha256Final(&tmpSha256, hash);
wolfSSL 16:8e0d178b1d1e 1585 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1586 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1587 sha256->ctx.mode = ESP32_SHA_SW;
wolfSSL 16:8e0d178b1d1e 1588 #endif
wolfSSL 16:8e0d178b1d1e 1589
wolfSSL 15:117db924cf7c 1590 wc_Sha256Free(&tmpSha256);
wolfSSL 15:117db924cf7c 1591 }
wolfSSL 15:117db924cf7c 1592 return ret;
wolfSSL 15:117db924cf7c 1593 }
wolfSSL 15:117db924cf7c 1594 int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
wolfSSL 15:117db924cf7c 1595 {
wolfSSL 15:117db924cf7c 1596 int ret = 0;
wolfSSL 15:117db924cf7c 1597
wolfSSL 15:117db924cf7c 1598 if (src == NULL || dst == NULL)
wolfSSL 15:117db924cf7c 1599 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1600
wolfSSL 15:117db924cf7c 1601 XMEMCPY(dst, src, sizeof(wc_Sha256));
wolfSSL 15:117db924cf7c 1602 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1603 dst->W = NULL;
wolfSSL 15:117db924cf7c 1604 #endif
wolfSSL 15:117db924cf7c 1605
wolfSSL 15:117db924cf7c 1606 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 1607 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 15:117db924cf7c 1608 #endif
wolfSSL 15:117db924cf7c 1609 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 15:117db924cf7c 1610 ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
wolfSSL 15:117db924cf7c 1611 #endif
wolfSSL 16:8e0d178b1d1e 1612 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1613 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1614 dst->ctx.mode = src->ctx.mode;
wolfSSL 16:8e0d178b1d1e 1615 dst->ctx.isfirstblock = src->ctx.isfirstblock;
wolfSSL 16:8e0d178b1d1e 1616 dst->ctx.sha_type = src->ctx.sha_type;
wolfSSL 16:8e0d178b1d1e 1617 #endif
wolfSSL 16:8e0d178b1d1e 1618 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1619 dst->flags |= WC_HASH_FLAG_ISCOPY;
wolfSSL 16:8e0d178b1d1e 1620 #endif
wolfSSL 15:117db924cf7c 1621
wolfSSL 15:117db924cf7c 1622 return ret;
wolfSSL 15:117db924cf7c 1623 }
wolfSSL 16:8e0d178b1d1e 1624 #endif
wolfSSL 16:8e0d178b1d1e 1625
wolfSSL 16:8e0d178b1d1e 1626 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1627 int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags)
wolfSSL 16:8e0d178b1d1e 1628 {
wolfSSL 16:8e0d178b1d1e 1629 if (sha256) {
wolfSSL 16:8e0d178b1d1e 1630 sha256->flags = flags;
wolfSSL 16:8e0d178b1d1e 1631 }
wolfSSL 16:8e0d178b1d1e 1632 return 0;
wolfSSL 16:8e0d178b1d1e 1633 }
wolfSSL 16:8e0d178b1d1e 1634 int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags)
wolfSSL 16:8e0d178b1d1e 1635 {
wolfSSL 16:8e0d178b1d1e 1636 if (sha256 && flags) {
wolfSSL 16:8e0d178b1d1e 1637 *flags = sha256->flags;
wolfSSL 16:8e0d178b1d1e 1638 }
wolfSSL 16:8e0d178b1d1e 1639 return 0;
wolfSSL 16:8e0d178b1d1e 1640 }
wolfSSL 16:8e0d178b1d1e 1641 #endif
wolfSSL 15:117db924cf7c 1642 #endif /* !WOLFSSL_TI_HASH */
wolfSSL 15:117db924cf7c 1643
wolfSSL 15:117db924cf7c 1644 #endif /* NO_SHA256 */
wolfSSL 15:117db924cf7c 1645