wolfSSL SSL/TLS library, support up to TLS1.3
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfcrypt/src/sha.c@17:a5f916481144, 2020-06-05 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 15:117db924cf7c | 1 | /* sha.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 | |
wolfSSL | 15:117db924cf7c | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 15:117db924cf7c | 24 | #include <config.h> |
wolfSSL | 15:117db924cf7c | 25 | #endif |
wolfSSL | 15:117db924cf7c | 26 | |
wolfSSL | 15:117db924cf7c | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 15:117db924cf7c | 28 | |
wolfSSL | 15:117db924cf7c | 29 | #if !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 30 | |
wolfSSL | 15:117db924cf7c | 31 | #if defined(HAVE_FIPS) && \ |
wolfSSL | 15:117db924cf7c | 32 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) |
wolfSSL | 15:117db924cf7c | 33 | |
wolfSSL | 15:117db924cf7c | 34 | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
wolfSSL | 15:117db924cf7c | 35 | #define FIPS_NO_WRAPPERS |
wolfSSL | 15:117db924cf7c | 36 | |
wolfSSL | 15:117db924cf7c | 37 | #ifdef USE_WINDOWS_API |
wolfSSL | 15:117db924cf7c | 38 | #pragma code_seg(".fipsA$j") |
wolfSSL | 15:117db924cf7c | 39 | #pragma const_seg(".fipsB$j") |
wolfSSL | 15:117db924cf7c | 40 | #endif |
wolfSSL | 15:117db924cf7c | 41 | #endif |
wolfSSL | 15:117db924cf7c | 42 | |
wolfSSL | 15:117db924cf7c | 43 | #include <wolfssl/wolfcrypt/sha.h> |
wolfSSL | 15:117db924cf7c | 44 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 16:8e0d178b1d1e | 45 | #include <wolfssl/wolfcrypt/hash.h> |
wolfSSL | 16:8e0d178b1d1e | 46 | |
wolfSSL | 16:8e0d178b1d1e | 47 | #ifdef WOLF_CRYPTO_CB |
wolfSSL | 16:8e0d178b1d1e | 48 | #include <wolfssl/wolfcrypt/cryptocb.h> |
wolfSSL | 16:8e0d178b1d1e | 49 | #endif |
wolfSSL | 15:117db924cf7c | 50 | |
wolfSSL | 15:117db924cf7c | 51 | /* fips wrapper calls, user can call direct */ |
wolfSSL | 15:117db924cf7c | 52 | #if defined(HAVE_FIPS) && \ |
wolfSSL | 15:117db924cf7c | 53 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) |
wolfSSL | 15:117db924cf7c | 54 | |
wolfSSL | 15:117db924cf7c | 55 | int wc_InitSha(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 56 | { |
wolfSSL | 15:117db924cf7c | 57 | if (sha == NULL) { |
wolfSSL | 15:117db924cf7c | 58 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 59 | } |
wolfSSL | 15:117db924cf7c | 60 | return InitSha_fips(sha); |
wolfSSL | 15:117db924cf7c | 61 | } |
wolfSSL | 15:117db924cf7c | 62 | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
wolfSSL | 15:117db924cf7c | 63 | { |
wolfSSL | 15:117db924cf7c | 64 | (void)heap; |
wolfSSL | 15:117db924cf7c | 65 | (void)devId; |
wolfSSL | 15:117db924cf7c | 66 | if (sha == NULL) { |
wolfSSL | 15:117db924cf7c | 67 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 68 | } |
wolfSSL | 15:117db924cf7c | 69 | return InitSha_fips(sha); |
wolfSSL | 15:117db924cf7c | 70 | } |
wolfSSL | 15:117db924cf7c | 71 | |
wolfSSL | 15:117db924cf7c | 72 | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
wolfSSL | 15:117db924cf7c | 73 | { |
wolfSSL | 15:117db924cf7c | 74 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 15:117db924cf7c | 75 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 76 | } |
wolfSSL | 15:117db924cf7c | 77 | return ShaUpdate_fips(sha, data, len); |
wolfSSL | 15:117db924cf7c | 78 | } |
wolfSSL | 15:117db924cf7c | 79 | |
wolfSSL | 15:117db924cf7c | 80 | int wc_ShaFinal(wc_Sha* sha, byte* out) |
wolfSSL | 15:117db924cf7c | 81 | { |
wolfSSL | 15:117db924cf7c | 82 | if (sha == NULL || out == NULL) { |
wolfSSL | 15:117db924cf7c | 83 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 84 | } |
wolfSSL | 15:117db924cf7c | 85 | return ShaFinal_fips(sha,out); |
wolfSSL | 15:117db924cf7c | 86 | } |
wolfSSL | 15:117db924cf7c | 87 | void wc_ShaFree(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 88 | { |
wolfSSL | 15:117db924cf7c | 89 | (void)sha; |
wolfSSL | 15:117db924cf7c | 90 | /* Not supported in FIPS */ |
wolfSSL | 15:117db924cf7c | 91 | } |
wolfSSL | 15:117db924cf7c | 92 | |
wolfSSL | 15:117db924cf7c | 93 | #else /* else build without fips, or for FIPS v2 */ |
wolfSSL | 15:117db924cf7c | 94 | |
wolfSSL | 15:117db924cf7c | 95 | |
wolfSSL | 15:117db924cf7c | 96 | #if defined(WOLFSSL_TI_HASH) |
wolfSSL | 15:117db924cf7c | 97 | /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */ |
wolfSSL | 15:117db924cf7c | 98 | |
wolfSSL | 15:117db924cf7c | 99 | #else |
wolfSSL | 15:117db924cf7c | 100 | |
wolfSSL | 15:117db924cf7c | 101 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 15:117db924cf7c | 102 | #ifdef NO_INLINE |
wolfSSL | 15:117db924cf7c | 103 | #include <wolfssl/wolfcrypt/misc.h> |
wolfSSL | 15:117db924cf7c | 104 | #else |
wolfSSL | 15:117db924cf7c | 105 | #define WOLFSSL_MISC_INCLUDED |
wolfSSL | 15:117db924cf7c | 106 | #include <wolfcrypt/src/misc.c> |
wolfSSL | 15:117db924cf7c | 107 | #endif |
wolfSSL | 15:117db924cf7c | 108 | |
wolfSSL | 15:117db924cf7c | 109 | |
wolfSSL | 15:117db924cf7c | 110 | /* Hardware Acceleration */ |
wolfSSL | 15:117db924cf7c | 111 | #if defined(WOLFSSL_PIC32MZ_HASH) |
wolfSSL | 15:117db924cf7c | 112 | #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h> |
wolfSSL | 15:117db924cf7c | 113 | |
wolfSSL | 15:117db924cf7c | 114 | #elif defined(STM32_HASH) |
wolfSSL | 15:117db924cf7c | 115 | |
wolfSSL | 15:117db924cf7c | 116 | /* Supports CubeMX HAL or Standard Peripheral Library */ |
wolfSSL | 15:117db924cf7c | 117 | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
wolfSSL | 15:117db924cf7c | 118 | { |
wolfSSL | 15:117db924cf7c | 119 | if (sha == NULL) { |
wolfSSL | 15:117db924cf7c | 120 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 121 | } |
wolfSSL | 15:117db924cf7c | 122 | |
wolfSSL | 15:117db924cf7c | 123 | (void)devId; |
wolfSSL | 15:117db924cf7c | 124 | (void)heap; |
wolfSSL | 15:117db924cf7c | 125 | |
wolfSSL | 15:117db924cf7c | 126 | wc_Stm32_Hash_Init(&sha->stmCtx); |
wolfSSL | 15:117db924cf7c | 127 | |
wolfSSL | 15:117db924cf7c | 128 | return 0; |
wolfSSL | 15:117db924cf7c | 129 | } |
wolfSSL | 15:117db924cf7c | 130 | |
wolfSSL | 15:117db924cf7c | 131 | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
wolfSSL | 15:117db924cf7c | 132 | { |
wolfSSL | 15:117db924cf7c | 133 | int ret; |
wolfSSL | 15:117db924cf7c | 134 | |
wolfSSL | 15:117db924cf7c | 135 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 15:117db924cf7c | 136 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 137 | } |
wolfSSL | 15:117db924cf7c | 138 | |
wolfSSL | 15:117db924cf7c | 139 | ret = wolfSSL_CryptHwMutexLock(); |
wolfSSL | 15:117db924cf7c | 140 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 141 | ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1, |
wolfSSL | 15:117db924cf7c | 142 | data, len); |
wolfSSL | 15:117db924cf7c | 143 | wolfSSL_CryptHwMutexUnLock(); |
wolfSSL | 15:117db924cf7c | 144 | } |
wolfSSL | 15:117db924cf7c | 145 | return ret; |
wolfSSL | 15:117db924cf7c | 146 | } |
wolfSSL | 15:117db924cf7c | 147 | |
wolfSSL | 15:117db924cf7c | 148 | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
wolfSSL | 15:117db924cf7c | 149 | { |
wolfSSL | 15:117db924cf7c | 150 | int ret; |
wolfSSL | 15:117db924cf7c | 151 | |
wolfSSL | 15:117db924cf7c | 152 | if (sha == NULL || hash == NULL) { |
wolfSSL | 15:117db924cf7c | 153 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 154 | } |
wolfSSL | 15:117db924cf7c | 155 | |
wolfSSL | 15:117db924cf7c | 156 | ret = wolfSSL_CryptHwMutexLock(); |
wolfSSL | 15:117db924cf7c | 157 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 158 | ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1, |
wolfSSL | 15:117db924cf7c | 159 | hash, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 160 | wolfSSL_CryptHwMutexUnLock(); |
wolfSSL | 15:117db924cf7c | 161 | } |
wolfSSL | 15:117db924cf7c | 162 | |
wolfSSL | 15:117db924cf7c | 163 | (void)wc_InitSha(sha); /* reset state */ |
wolfSSL | 15:117db924cf7c | 164 | |
wolfSSL | 15:117db924cf7c | 165 | return ret; |
wolfSSL | 15:117db924cf7c | 166 | } |
wolfSSL | 15:117db924cf7c | 167 | |
wolfSSL | 15:117db924cf7c | 168 | |
wolfSSL | 15:117db924cf7c | 169 | #elif defined(FREESCALE_LTC_SHA) |
wolfSSL | 15:117db924cf7c | 170 | |
wolfSSL | 15:117db924cf7c | 171 | #include "fsl_ltc.h" |
wolfSSL | 15:117db924cf7c | 172 | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
wolfSSL | 15:117db924cf7c | 173 | { |
wolfSSL | 15:117db924cf7c | 174 | if (sha == NULL) { |
wolfSSL | 15:117db924cf7c | 175 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 176 | } |
wolfSSL | 15:117db924cf7c | 177 | |
wolfSSL | 15:117db924cf7c | 178 | (void)devId; |
wolfSSL | 15:117db924cf7c | 179 | (void)heap; |
wolfSSL | 15:117db924cf7c | 180 | |
wolfSSL | 15:117db924cf7c | 181 | LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0); |
wolfSSL | 15:117db924cf7c | 182 | return 0; |
wolfSSL | 15:117db924cf7c | 183 | } |
wolfSSL | 15:117db924cf7c | 184 | |
wolfSSL | 15:117db924cf7c | 185 | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
wolfSSL | 15:117db924cf7c | 186 | { |
wolfSSL | 15:117db924cf7c | 187 | LTC_HASH_Update(&sha->ctx, data, len); |
wolfSSL | 15:117db924cf7c | 188 | return 0; |
wolfSSL | 15:117db924cf7c | 189 | } |
wolfSSL | 15:117db924cf7c | 190 | |
wolfSSL | 15:117db924cf7c | 191 | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
wolfSSL | 15:117db924cf7c | 192 | { |
wolfSSL | 15:117db924cf7c | 193 | uint32_t hashlen = WC_SHA_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 194 | LTC_HASH_Finish(&sha->ctx, hash, &hashlen); |
wolfSSL | 15:117db924cf7c | 195 | return wc_InitSha(sha); /* reset state */ |
wolfSSL | 15:117db924cf7c | 196 | } |
wolfSSL | 15:117db924cf7c | 197 | |
wolfSSL | 15:117db924cf7c | 198 | |
wolfSSL | 15:117db924cf7c | 199 | #elif defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 15:117db924cf7c | 200 | |
wolfSSL | 15:117db924cf7c | 201 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
wolfSSL | 15:117db924cf7c | 202 | #include "cau_api.h" |
wolfSSL | 15:117db924cf7c | 203 | #else |
wolfSSL | 15:117db924cf7c | 204 | #include "fsl_mmcau.h" |
wolfSSL | 15:117db924cf7c | 205 | #endif |
wolfSSL | 15:117db924cf7c | 206 | |
wolfSSL | 15:117db924cf7c | 207 | #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */ |
wolfSSL | 16:8e0d178b1d1e | 208 | |
wolfSSL | 16:8e0d178b1d1e | 209 | #define XTRANSFORM(S,B) Transform((S),(B)) |
wolfSSL | 16:8e0d178b1d1e | 210 | #define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L)) |
wolfSSL | 16:8e0d178b1d1e | 211 | |
wolfSSL | 16:8e0d178b1d1e | 212 | #ifndef WC_HASH_DATA_ALIGNMENT |
wolfSSL | 16:8e0d178b1d1e | 213 | /* these hardware API's require 4 byte (word32) alignment */ |
wolfSSL | 16:8e0d178b1d1e | 214 | #define WC_HASH_DATA_ALIGNMENT 4 |
wolfSSL | 16:8e0d178b1d1e | 215 | #endif |
wolfSSL | 15:117db924cf7c | 216 | |
wolfSSL | 15:117db924cf7c | 217 | static int InitSha(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 218 | { |
wolfSSL | 15:117db924cf7c | 219 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 220 | ret = wolfSSL_CryptHwMutexLock(); |
wolfSSL | 15:117db924cf7c | 221 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 222 | return ret; |
wolfSSL | 15:117db924cf7c | 223 | } |
wolfSSL | 15:117db924cf7c | 224 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
wolfSSL | 15:117db924cf7c | 225 | cau_sha1_initialize_output(sha->digest); |
wolfSSL | 15:117db924cf7c | 226 | #else |
wolfSSL | 15:117db924cf7c | 227 | MMCAU_SHA1_InitializeOutput((uint32_t*)sha->digest); |
wolfSSL | 15:117db924cf7c | 228 | #endif |
wolfSSL | 15:117db924cf7c | 229 | wolfSSL_CryptHwMutexUnLock(); |
wolfSSL | 15:117db924cf7c | 230 | |
wolfSSL | 15:117db924cf7c | 231 | sha->buffLen = 0; |
wolfSSL | 15:117db924cf7c | 232 | sha->loLen = 0; |
wolfSSL | 15:117db924cf7c | 233 | sha->hiLen = 0; |
wolfSSL | 15:117db924cf7c | 234 | |
wolfSSL | 15:117db924cf7c | 235 | return ret; |
wolfSSL | 15:117db924cf7c | 236 | } |
wolfSSL | 15:117db924cf7c | 237 | |
wolfSSL | 16:8e0d178b1d1e | 238 | static int Transform(wc_Sha* sha, const byte* data) |
wolfSSL | 16:8e0d178b1d1e | 239 | { |
wolfSSL | 16:8e0d178b1d1e | 240 | int ret = wolfSSL_CryptHwMutexLock(); |
wolfSSL | 16:8e0d178b1d1e | 241 | if (ret == 0) { |
wolfSSL | 16:8e0d178b1d1e | 242 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
wolfSSL | 16:8e0d178b1d1e | 243 | cau_sha1_hash_n((byte*)data, 1, sha->digest); |
wolfSSL | 16:8e0d178b1d1e | 244 | #else |
wolfSSL | 16:8e0d178b1d1e | 245 | MMCAU_SHA1_HashN((byte*)data, 1, (uint32_t*)sha->digest); |
wolfSSL | 16:8e0d178b1d1e | 246 | #endif |
wolfSSL | 16:8e0d178b1d1e | 247 | wolfSSL_CryptHwMutexUnLock(); |
wolfSSL | 16:8e0d178b1d1e | 248 | } |
wolfSSL | 16:8e0d178b1d1e | 249 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 250 | } |
wolfSSL | 16:8e0d178b1d1e | 251 | |
wolfSSL | 16:8e0d178b1d1e | 252 | static int Transform_Len(wc_Sha* sha, const byte* data, word32 len) |
wolfSSL | 15:117db924cf7c | 253 | { |
wolfSSL | 15:117db924cf7c | 254 | int ret = wolfSSL_CryptHwMutexLock(); |
wolfSSL | 16:8e0d178b1d1e | 255 | if (ret == 0) { |
wolfSSL | 16:8e0d178b1d1e | 256 | #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0 |
wolfSSL | 16:8e0d178b1d1e | 257 | if ((size_t)data % WC_HASH_DATA_ALIGNMENT) { |
wolfSSL | 16:8e0d178b1d1e | 258 | /* data pointer is NOT aligned, |
wolfSSL | 16:8e0d178b1d1e | 259 | * so copy and perform one block at a time */ |
wolfSSL | 16:8e0d178b1d1e | 260 | byte* local = (byte*)sha->buffer; |
wolfSSL | 16:8e0d178b1d1e | 261 | while (len >= WC_SHA_BLOCK_SIZE) { |
wolfSSL | 16:8e0d178b1d1e | 262 | XMEMCPY(local, data, WC_SHA_BLOCK_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 263 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
wolfSSL | 16:8e0d178b1d1e | 264 | cau_sha1_hash_n(local, 1, sha->digest); |
wolfSSL | 16:8e0d178b1d1e | 265 | #else |
wolfSSL | 16:8e0d178b1d1e | 266 | MMCAU_SHA1_HashN(local, 1, sha->digest); |
wolfSSL | 16:8e0d178b1d1e | 267 | #endif |
wolfSSL | 16:8e0d178b1d1e | 268 | data += WC_SHA_BLOCK_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 269 | len -= WC_SHA_BLOCK_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 270 | } |
wolfSSL | 16:8e0d178b1d1e | 271 | } |
wolfSSL | 16:8e0d178b1d1e | 272 | else |
wolfSSL | 16:8e0d178b1d1e | 273 | #endif |
wolfSSL | 16:8e0d178b1d1e | 274 | { |
wolfSSL | 15:117db924cf7c | 275 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
wolfSSL | 16:8e0d178b1d1e | 276 | cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest); |
wolfSSL | 15:117db924cf7c | 277 | #else |
wolfSSL | 16:8e0d178b1d1e | 278 | MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE, |
wolfSSL | 16:8e0d178b1d1e | 279 | (uint32_t*)sha->digest); |
wolfSSL | 15:117db924cf7c | 280 | #endif |
wolfSSL | 16:8e0d178b1d1e | 281 | } |
wolfSSL | 15:117db924cf7c | 282 | wolfSSL_CryptHwMutexUnLock(); |
wolfSSL | 15:117db924cf7c | 283 | } |
wolfSSL | 15:117db924cf7c | 284 | return ret; |
wolfSSL | 15:117db924cf7c | 285 | } |
wolfSSL | 15:117db924cf7c | 286 | |
wolfSSL | 15:117db924cf7c | 287 | #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) |
wolfSSL | 15:117db924cf7c | 288 | /* wolfcrypt/src/port/caam/caam_sha.c */ |
wolfSSL | 16:8e0d178b1d1e | 289 | |
wolfSSL | 16:8e0d178b1d1e | 290 | #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 291 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 292 | |
wolfSSL | 16:8e0d178b1d1e | 293 | #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h" |
wolfSSL | 16:8e0d178b1d1e | 294 | |
wolfSSL | 16:8e0d178b1d1e | 295 | #define USE_SHA_SOFTWARE_IMPL |
wolfSSL | 16:8e0d178b1d1e | 296 | |
wolfSSL | 16:8e0d178b1d1e | 297 | static int InitSha(wc_Sha* sha) |
wolfSSL | 16:8e0d178b1d1e | 298 | { |
wolfSSL | 16:8e0d178b1d1e | 299 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 300 | |
wolfSSL | 16:8e0d178b1d1e | 301 | sha->digest[0] = 0x67452301L; |
wolfSSL | 16:8e0d178b1d1e | 302 | sha->digest[1] = 0xEFCDAB89L; |
wolfSSL | 16:8e0d178b1d1e | 303 | sha->digest[2] = 0x98BADCFEL; |
wolfSSL | 16:8e0d178b1d1e | 304 | sha->digest[3] = 0x10325476L; |
wolfSSL | 16:8e0d178b1d1e | 305 | sha->digest[4] = 0xC3D2E1F0L; |
wolfSSL | 16:8e0d178b1d1e | 306 | |
wolfSSL | 16:8e0d178b1d1e | 307 | sha->buffLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 308 | sha->loLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 309 | sha->hiLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 310 | |
wolfSSL | 16:8e0d178b1d1e | 311 | /* always start firstblock = 1 when using hw engine */ |
wolfSSL | 16:8e0d178b1d1e | 312 | sha->ctx.isfirstblock = 1; |
wolfSSL | 16:8e0d178b1d1e | 313 | sha->ctx.sha_type = SHA1; |
wolfSSL | 16:8e0d178b1d1e | 314 | if(sha->ctx.mode == ESP32_SHA_HW){ |
wolfSSL | 16:8e0d178b1d1e | 315 | /* release hw engine */ |
wolfSSL | 16:8e0d178b1d1e | 316 | esp_sha_hw_unlock(); |
wolfSSL | 16:8e0d178b1d1e | 317 | } |
wolfSSL | 16:8e0d178b1d1e | 318 | /* always set mode as INIT |
wolfSSL | 16:8e0d178b1d1e | 319 | * whether using HW or SW is determined at first call of update() |
wolfSSL | 16:8e0d178b1d1e | 320 | */ |
wolfSSL | 16:8e0d178b1d1e | 321 | sha->ctx.mode = ESP32_SHA_INIT; |
wolfSSL | 16:8e0d178b1d1e | 322 | |
wolfSSL | 16:8e0d178b1d1e | 323 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 324 | } |
wolfSSL | 16:8e0d178b1d1e | 325 | |
wolfSSL | 16:8e0d178b1d1e | 326 | #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 327 | !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 328 | |
wolfSSL | 16:8e0d178b1d1e | 329 | /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */ |
wolfSSL | 16:8e0d178b1d1e | 330 | |
wolfSSL | 15:117db924cf7c | 331 | #else |
wolfSSL | 15:117db924cf7c | 332 | /* Software implementation */ |
wolfSSL | 15:117db924cf7c | 333 | #define USE_SHA_SOFTWARE_IMPL |
wolfSSL | 15:117db924cf7c | 334 | |
wolfSSL | 15:117db924cf7c | 335 | static int InitSha(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 336 | { |
wolfSSL | 15:117db924cf7c | 337 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 338 | |
wolfSSL | 15:117db924cf7c | 339 | sha->digest[0] = 0x67452301L; |
wolfSSL | 15:117db924cf7c | 340 | sha->digest[1] = 0xEFCDAB89L; |
wolfSSL | 15:117db924cf7c | 341 | sha->digest[2] = 0x98BADCFEL; |
wolfSSL | 15:117db924cf7c | 342 | sha->digest[3] = 0x10325476L; |
wolfSSL | 15:117db924cf7c | 343 | sha->digest[4] = 0xC3D2E1F0L; |
wolfSSL | 15:117db924cf7c | 344 | |
wolfSSL | 15:117db924cf7c | 345 | sha->buffLen = 0; |
wolfSSL | 15:117db924cf7c | 346 | sha->loLen = 0; |
wolfSSL | 15:117db924cf7c | 347 | sha->hiLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 348 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
wolfSSL | 16:8e0d178b1d1e | 349 | sha->flags = 0; |
wolfSSL | 16:8e0d178b1d1e | 350 | #endif |
wolfSSL | 15:117db924cf7c | 351 | |
wolfSSL | 15:117db924cf7c | 352 | return ret; |
wolfSSL | 15:117db924cf7c | 353 | } |
wolfSSL | 15:117db924cf7c | 354 | #endif /* End Hardware Acceleration */ |
wolfSSL | 15:117db924cf7c | 355 | |
wolfSSL | 15:117db924cf7c | 356 | /* Software implementation */ |
wolfSSL | 15:117db924cf7c | 357 | #ifdef USE_SHA_SOFTWARE_IMPL |
wolfSSL | 15:117db924cf7c | 358 | |
wolfSSL | 15:117db924cf7c | 359 | static WC_INLINE void AddLength(wc_Sha* sha, word32 len) |
wolfSSL | 15:117db924cf7c | 360 | { |
wolfSSL | 15:117db924cf7c | 361 | word32 tmp = sha->loLen; |
wolfSSL | 15:117db924cf7c | 362 | if ((sha->loLen += len) < tmp) |
wolfSSL | 15:117db924cf7c | 363 | sha->hiLen++; /* carry low to high */ |
wolfSSL | 15:117db924cf7c | 364 | } |
wolfSSL | 15:117db924cf7c | 365 | |
wolfSSL | 15:117db924cf7c | 366 | /* Check if custom wc_Sha transform is used */ |
wolfSSL | 15:117db924cf7c | 367 | #ifndef XTRANSFORM |
wolfSSL | 15:117db924cf7c | 368 | #define XTRANSFORM(S,B) Transform((S),(B)) |
wolfSSL | 15:117db924cf7c | 369 | |
wolfSSL | 16:8e0d178b1d1e | 370 | #define blk0(i) (W[i] = *((word32*)&data[i*sizeof(word32)])) |
wolfSSL | 15:117db924cf7c | 371 | #define blk1(i) (W[(i)&15] = \ |
wolfSSL | 15:117db924cf7c | 372 | rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1)) |
wolfSSL | 15:117db924cf7c | 373 | |
wolfSSL | 15:117db924cf7c | 374 | #define f1(x,y,z) ((z)^((x) &((y)^(z)))) |
wolfSSL | 15:117db924cf7c | 375 | #define f2(x,y,z) ((x)^(y)^(z)) |
wolfSSL | 15:117db924cf7c | 376 | #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y)))) |
wolfSSL | 15:117db924cf7c | 377 | #define f4(x,y,z) ((x)^(y)^(z)) |
wolfSSL | 15:117db924cf7c | 378 | |
wolfSSL | 15:117db924cf7c | 379 | #ifdef WOLFSSL_NUCLEUS_1_2 |
wolfSSL | 15:117db924cf7c | 380 | /* nucleus.h also defines R1-R4 */ |
wolfSSL | 15:117db924cf7c | 381 | #undef R1 |
wolfSSL | 15:117db924cf7c | 382 | #undef R2 |
wolfSSL | 15:117db924cf7c | 383 | #undef R3 |
wolfSSL | 15:117db924cf7c | 384 | #undef R4 |
wolfSSL | 15:117db924cf7c | 385 | #endif |
wolfSSL | 15:117db924cf7c | 386 | |
wolfSSL | 15:117db924cf7c | 387 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
wolfSSL | 15:117db924cf7c | 388 | #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \ |
wolfSSL | 15:117db924cf7c | 389 | rotlFixed((v),5); (w) = rotlFixed((w),30); |
wolfSSL | 15:117db924cf7c | 390 | #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \ |
wolfSSL | 15:117db924cf7c | 391 | rotlFixed((v),5); (w) = rotlFixed((w),30); |
wolfSSL | 15:117db924cf7c | 392 | #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \ |
wolfSSL | 15:117db924cf7c | 393 | rotlFixed((v),5); (w) = rotlFixed((w),30); |
wolfSSL | 15:117db924cf7c | 394 | #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \ |
wolfSSL | 15:117db924cf7c | 395 | rotlFixed((v),5); (w) = rotlFixed((w),30); |
wolfSSL | 15:117db924cf7c | 396 | #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \ |
wolfSSL | 15:117db924cf7c | 397 | rotlFixed((v),5); (w) = rotlFixed((w),30); |
wolfSSL | 15:117db924cf7c | 398 | |
wolfSSL | 16:8e0d178b1d1e | 399 | static int Transform(wc_Sha* sha, const byte* data) |
wolfSSL | 15:117db924cf7c | 400 | { |
wolfSSL | 15:117db924cf7c | 401 | word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)]; |
wolfSSL | 15:117db924cf7c | 402 | |
wolfSSL | 15:117db924cf7c | 403 | /* Copy context->state[] to working vars */ |
wolfSSL | 15:117db924cf7c | 404 | word32 a = sha->digest[0]; |
wolfSSL | 15:117db924cf7c | 405 | word32 b = sha->digest[1]; |
wolfSSL | 15:117db924cf7c | 406 | word32 c = sha->digest[2]; |
wolfSSL | 15:117db924cf7c | 407 | word32 d = sha->digest[3]; |
wolfSSL | 15:117db924cf7c | 408 | word32 e = sha->digest[4]; |
wolfSSL | 15:117db924cf7c | 409 | |
wolfSSL | 15:117db924cf7c | 410 | #ifdef USE_SLOW_SHA |
wolfSSL | 15:117db924cf7c | 411 | word32 t, i; |
wolfSSL | 15:117db924cf7c | 412 | |
wolfSSL | 15:117db924cf7c | 413 | for (i = 0; i < 16; i++) { |
wolfSSL | 15:117db924cf7c | 414 | R0(a, b, c, d, e, i); |
wolfSSL | 15:117db924cf7c | 415 | t = e; e = d; d = c; c = b; b = a; a = t; |
wolfSSL | 15:117db924cf7c | 416 | } |
wolfSSL | 15:117db924cf7c | 417 | |
wolfSSL | 15:117db924cf7c | 418 | for (; i < 20; i++) { |
wolfSSL | 15:117db924cf7c | 419 | R1(a, b, c, d, e, i); |
wolfSSL | 15:117db924cf7c | 420 | t = e; e = d; d = c; c = b; b = a; a = t; |
wolfSSL | 15:117db924cf7c | 421 | } |
wolfSSL | 15:117db924cf7c | 422 | |
wolfSSL | 15:117db924cf7c | 423 | for (; i < 40; i++) { |
wolfSSL | 15:117db924cf7c | 424 | R2(a, b, c, d, e, i); |
wolfSSL | 15:117db924cf7c | 425 | t = e; e = d; d = c; c = b; b = a; a = t; |
wolfSSL | 15:117db924cf7c | 426 | } |
wolfSSL | 15:117db924cf7c | 427 | |
wolfSSL | 15:117db924cf7c | 428 | for (; i < 60; i++) { |
wolfSSL | 15:117db924cf7c | 429 | R3(a, b, c, d, e, i); |
wolfSSL | 15:117db924cf7c | 430 | t = e; e = d; d = c; c = b; b = a; a = t; |
wolfSSL | 15:117db924cf7c | 431 | } |
wolfSSL | 15:117db924cf7c | 432 | |
wolfSSL | 15:117db924cf7c | 433 | for (; i < 80; i++) { |
wolfSSL | 15:117db924cf7c | 434 | R4(a, b, c, d, e, i); |
wolfSSL | 15:117db924cf7c | 435 | t = e; e = d; d = c; c = b; b = a; a = t; |
wolfSSL | 15:117db924cf7c | 436 | } |
wolfSSL | 15:117db924cf7c | 437 | #else |
wolfSSL | 15:117db924cf7c | 438 | /* nearly 1 K bigger in code size but 25% faster */ |
wolfSSL | 15:117db924cf7c | 439 | /* 4 rounds of 20 operations each. Loop unrolled. */ |
wolfSSL | 15:117db924cf7c | 440 | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); |
wolfSSL | 15:117db924cf7c | 441 | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); |
wolfSSL | 15:117db924cf7c | 442 | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); |
wolfSSL | 15:117db924cf7c | 443 | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); |
wolfSSL | 15:117db924cf7c | 444 | |
wolfSSL | 15:117db924cf7c | 445 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
wolfSSL | 15:117db924cf7c | 446 | |
wolfSSL | 15:117db924cf7c | 447 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
wolfSSL | 15:117db924cf7c | 448 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
wolfSSL | 15:117db924cf7c | 449 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
wolfSSL | 15:117db924cf7c | 450 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
wolfSSL | 15:117db924cf7c | 451 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
wolfSSL | 15:117db924cf7c | 452 | |
wolfSSL | 15:117db924cf7c | 453 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
wolfSSL | 15:117db924cf7c | 454 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
wolfSSL | 15:117db924cf7c | 455 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
wolfSSL | 15:117db924cf7c | 456 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
wolfSSL | 15:117db924cf7c | 457 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
wolfSSL | 15:117db924cf7c | 458 | |
wolfSSL | 15:117db924cf7c | 459 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
wolfSSL | 15:117db924cf7c | 460 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
wolfSSL | 15:117db924cf7c | 461 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
wolfSSL | 15:117db924cf7c | 462 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
wolfSSL | 15:117db924cf7c | 463 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
wolfSSL | 15:117db924cf7c | 464 | #endif |
wolfSSL | 15:117db924cf7c | 465 | |
wolfSSL | 15:117db924cf7c | 466 | /* Add the working vars back into digest state[] */ |
wolfSSL | 15:117db924cf7c | 467 | sha->digest[0] += a; |
wolfSSL | 15:117db924cf7c | 468 | sha->digest[1] += b; |
wolfSSL | 15:117db924cf7c | 469 | sha->digest[2] += c; |
wolfSSL | 15:117db924cf7c | 470 | sha->digest[3] += d; |
wolfSSL | 15:117db924cf7c | 471 | sha->digest[4] += e; |
wolfSSL | 15:117db924cf7c | 472 | |
wolfSSL | 15:117db924cf7c | 473 | (void)data; /* Not used */ |
wolfSSL | 16:8e0d178b1d1e | 474 | |
wolfSSL | 16:8e0d178b1d1e | 475 | return 0; |
wolfSSL | 15:117db924cf7c | 476 | } |
wolfSSL | 15:117db924cf7c | 477 | #endif /* !USE_CUSTOM_SHA_TRANSFORM */ |
wolfSSL | 15:117db924cf7c | 478 | |
wolfSSL | 15:117db924cf7c | 479 | |
wolfSSL | 15:117db924cf7c | 480 | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
wolfSSL | 15:117db924cf7c | 481 | { |
wolfSSL | 15:117db924cf7c | 482 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 483 | |
wolfSSL | 15:117db924cf7c | 484 | if (sha == NULL) |
wolfSSL | 15:117db924cf7c | 485 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 486 | |
wolfSSL | 15:117db924cf7c | 487 | sha->heap = heap; |
wolfSSL | 16:8e0d178b1d1e | 488 | #ifdef WOLF_CRYPTO_CB |
wolfSSL | 16:8e0d178b1d1e | 489 | sha->devId = devId; |
wolfSSL | 16:8e0d178b1d1e | 490 | #endif |
wolfSSL | 15:117db924cf7c | 491 | |
wolfSSL | 16:8e0d178b1d1e | 492 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 493 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 494 | sha->ctx.mode = ESP32_SHA_INIT; |
wolfSSL | 16:8e0d178b1d1e | 495 | sha->ctx.isfirstblock = 1; |
wolfSSL | 16:8e0d178b1d1e | 496 | #endif |
wolfSSL | 15:117db924cf7c | 497 | ret = InitSha(sha); |
wolfSSL | 15:117db924cf7c | 498 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 499 | return ret; |
wolfSSL | 15:117db924cf7c | 500 | |
wolfSSL | 15:117db924cf7c | 501 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
wolfSSL | 15:117db924cf7c | 502 | ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA, |
wolfSSL | 15:117db924cf7c | 503 | sha->heap, devId); |
wolfSSL | 15:117db924cf7c | 504 | #else |
wolfSSL | 15:117db924cf7c | 505 | (void)devId; |
wolfSSL | 15:117db924cf7c | 506 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 15:117db924cf7c | 507 | |
wolfSSL | 15:117db924cf7c | 508 | return ret; |
wolfSSL | 15:117db924cf7c | 509 | } |
wolfSSL | 15:117db924cf7c | 510 | |
wolfSSL | 16:8e0d178b1d1e | 511 | /* do block size increments/updates */ |
wolfSSL | 15:117db924cf7c | 512 | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
wolfSSL | 15:117db924cf7c | 513 | { |
wolfSSL | 16:8e0d178b1d1e | 514 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 515 | word32 blocksLen; |
wolfSSL | 15:117db924cf7c | 516 | byte* local; |
wolfSSL | 15:117db924cf7c | 517 | |
wolfSSL | 16:8e0d178b1d1e | 518 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 15:117db924cf7c | 519 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 520 | } |
wolfSSL | 15:117db924cf7c | 521 | |
wolfSSL | 16:8e0d178b1d1e | 522 | #ifdef WOLF_CRYPTO_CB |
wolfSSL | 16:8e0d178b1d1e | 523 | if (sha->devId != INVALID_DEVID) { |
wolfSSL | 16:8e0d178b1d1e | 524 | ret = wc_CryptoCb_ShaHash(sha, data, len, NULL); |
wolfSSL | 16:8e0d178b1d1e | 525 | if (ret != CRYPTOCB_UNAVAILABLE) |
wolfSSL | 16:8e0d178b1d1e | 526 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 527 | ret = 0; /* reset ret */ |
wolfSSL | 16:8e0d178b1d1e | 528 | /* fall-through when unavailable */ |
wolfSSL | 16:8e0d178b1d1e | 529 | } |
wolfSSL | 16:8e0d178b1d1e | 530 | #endif |
wolfSSL | 15:117db924cf7c | 531 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
wolfSSL | 15:117db924cf7c | 532 | if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) { |
wolfSSL | 15:117db924cf7c | 533 | #if defined(HAVE_INTEL_QA) |
wolfSSL | 15:117db924cf7c | 534 | return IntelQaSymSha(&sha->asyncDev, NULL, data, len); |
wolfSSL | 15:117db924cf7c | 535 | #endif |
wolfSSL | 15:117db924cf7c | 536 | } |
wolfSSL | 15:117db924cf7c | 537 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 15:117db924cf7c | 538 | |
wolfSSL | 15:117db924cf7c | 539 | /* check that internal buffLen is valid */ |
wolfSSL | 15:117db924cf7c | 540 | if (sha->buffLen >= WC_SHA_BLOCK_SIZE) |
wolfSSL | 15:117db924cf7c | 541 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 542 | |
wolfSSL | 16:8e0d178b1d1e | 543 | if (data == NULL && len == 0) { |
wolfSSL | 16:8e0d178b1d1e | 544 | /* valid, but do nothing */ |
wolfSSL | 16:8e0d178b1d1e | 545 | return 0; |
wolfSSL | 16:8e0d178b1d1e | 546 | } |
wolfSSL | 16:8e0d178b1d1e | 547 | |
wolfSSL | 16:8e0d178b1d1e | 548 | /* add length for final */ |
wolfSSL | 16:8e0d178b1d1e | 549 | AddLength(sha, len); |
wolfSSL | 15:117db924cf7c | 550 | |
wolfSSL | 16:8e0d178b1d1e | 551 | local = (byte*)sha->buffer; |
wolfSSL | 16:8e0d178b1d1e | 552 | |
wolfSSL | 16:8e0d178b1d1e | 553 | /* process any remainder from previous operation */ |
wolfSSL | 16:8e0d178b1d1e | 554 | if (sha->buffLen > 0) { |
wolfSSL | 16:8e0d178b1d1e | 555 | blocksLen = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen); |
wolfSSL | 16:8e0d178b1d1e | 556 | XMEMCPY(&local[sha->buffLen], data, blocksLen); |
wolfSSL | 16:8e0d178b1d1e | 557 | |
wolfSSL | 16:8e0d178b1d1e | 558 | sha->buffLen += blocksLen; |
wolfSSL | 16:8e0d178b1d1e | 559 | data += blocksLen; |
wolfSSL | 16:8e0d178b1d1e | 560 | len -= blocksLen; |
wolfSSL | 15:117db924cf7c | 561 | |
wolfSSL | 15:117db924cf7c | 562 | if (sha->buffLen == WC_SHA_BLOCK_SIZE) { |
wolfSSL | 16:8e0d178b1d1e | 563 | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 15:117db924cf7c | 564 | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 565 | #endif |
wolfSSL | 16:8e0d178b1d1e | 566 | |
wolfSSL | 16:8e0d178b1d1e | 567 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 568 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 569 | if (sha->ctx.mode == ESP32_SHA_INIT) { |
wolfSSL | 16:8e0d178b1d1e | 570 | esp_sha_try_hw_lock(&sha->ctx); |
wolfSSL | 16:8e0d178b1d1e | 571 | } |
wolfSSL | 16:8e0d178b1d1e | 572 | if (sha->ctx.mode == ESP32_SHA_SW) { |
wolfSSL | 16:8e0d178b1d1e | 573 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 574 | } else { |
wolfSSL | 16:8e0d178b1d1e | 575 | esp_sha_process(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 576 | } |
wolfSSL | 16:8e0d178b1d1e | 577 | #else |
wolfSSL | 16:8e0d178b1d1e | 578 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 579 | #endif |
wolfSSL | 16:8e0d178b1d1e | 580 | if (ret != 0) |
wolfSSL | 16:8e0d178b1d1e | 581 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 582 | |
wolfSSL | 15:117db924cf7c | 583 | sha->buffLen = 0; |
wolfSSL | 15:117db924cf7c | 584 | } |
wolfSSL | 15:117db924cf7c | 585 | } |
wolfSSL | 15:117db924cf7c | 586 | |
wolfSSL | 16:8e0d178b1d1e | 587 | /* process blocks */ |
wolfSSL | 16:8e0d178b1d1e | 588 | #ifdef XTRANSFORM_LEN |
wolfSSL | 16:8e0d178b1d1e | 589 | /* get number of blocks */ |
wolfSSL | 16:8e0d178b1d1e | 590 | /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */ |
wolfSSL | 16:8e0d178b1d1e | 591 | /* len (masked by 0xFFFFFFC0) returns block aligned length */ |
wolfSSL | 16:8e0d178b1d1e | 592 | blocksLen = len & ~(WC_SHA_BLOCK_SIZE-1); |
wolfSSL | 16:8e0d178b1d1e | 593 | if (blocksLen > 0) { |
wolfSSL | 16:8e0d178b1d1e | 594 | /* Byte reversal performed in function if required. */ |
wolfSSL | 16:8e0d178b1d1e | 595 | XTRANSFORM_LEN(sha, data, blocksLen); |
wolfSSL | 16:8e0d178b1d1e | 596 | data += blocksLen; |
wolfSSL | 16:8e0d178b1d1e | 597 | len -= blocksLen; |
wolfSSL | 16:8e0d178b1d1e | 598 | } |
wolfSSL | 16:8e0d178b1d1e | 599 | #else |
wolfSSL | 16:8e0d178b1d1e | 600 | while (len >= WC_SHA_BLOCK_SIZE) { |
wolfSSL | 16:8e0d178b1d1e | 601 | word32* local32 = sha->buffer; |
wolfSSL | 16:8e0d178b1d1e | 602 | /* optimization to avoid memcpy if data pointer is properly aligned */ |
wolfSSL | 16:8e0d178b1d1e | 603 | /* Little Endian requires byte swap, so can't use data directly */ |
wolfSSL | 16:8e0d178b1d1e | 604 | #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) |
wolfSSL | 16:8e0d178b1d1e | 605 | if (((size_t)data % WC_HASH_DATA_ALIGNMENT) == 0) { |
wolfSSL | 16:8e0d178b1d1e | 606 | local32 = (word32*)data; |
wolfSSL | 16:8e0d178b1d1e | 607 | } |
wolfSSL | 16:8e0d178b1d1e | 608 | else |
wolfSSL | 16:8e0d178b1d1e | 609 | #endif |
wolfSSL | 16:8e0d178b1d1e | 610 | { |
wolfSSL | 16:8e0d178b1d1e | 611 | XMEMCPY(local32, data, WC_SHA_BLOCK_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 612 | } |
wolfSSL | 16:8e0d178b1d1e | 613 | |
wolfSSL | 16:8e0d178b1d1e | 614 | data += WC_SHA_BLOCK_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 615 | len -= WC_SHA_BLOCK_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 616 | |
wolfSSL | 16:8e0d178b1d1e | 617 | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 16:8e0d178b1d1e | 618 | ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 619 | #endif |
wolfSSL | 16:8e0d178b1d1e | 620 | |
wolfSSL | 16:8e0d178b1d1e | 621 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 622 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 623 | if (sha->ctx.mode == ESP32_SHA_INIT){ |
wolfSSL | 16:8e0d178b1d1e | 624 | esp_sha_try_hw_lock(&sha->ctx); |
wolfSSL | 16:8e0d178b1d1e | 625 | } |
wolfSSL | 16:8e0d178b1d1e | 626 | if (sha->ctx.mode == ESP32_SHA_SW){ |
wolfSSL | 16:8e0d178b1d1e | 627 | ret = XTRANSFORM(sha, (const byte*)local32); |
wolfSSL | 16:8e0d178b1d1e | 628 | } else { |
wolfSSL | 16:8e0d178b1d1e | 629 | esp_sha_process(sha, (const byte*)local32); |
wolfSSL | 16:8e0d178b1d1e | 630 | } |
wolfSSL | 16:8e0d178b1d1e | 631 | #else |
wolfSSL | 16:8e0d178b1d1e | 632 | ret = XTRANSFORM(sha, (const byte*)local32); |
wolfSSL | 16:8e0d178b1d1e | 633 | #endif |
wolfSSL | 16:8e0d178b1d1e | 634 | } |
wolfSSL | 16:8e0d178b1d1e | 635 | #endif /* XTRANSFORM_LEN */ |
wolfSSL | 16:8e0d178b1d1e | 636 | |
wolfSSL | 16:8e0d178b1d1e | 637 | /* save remainder */ |
wolfSSL | 16:8e0d178b1d1e | 638 | if (len > 0) { |
wolfSSL | 16:8e0d178b1d1e | 639 | XMEMCPY(local, data, len); |
wolfSSL | 16:8e0d178b1d1e | 640 | sha->buffLen = len; |
wolfSSL | 16:8e0d178b1d1e | 641 | } |
wolfSSL | 16:8e0d178b1d1e | 642 | |
wolfSSL | 16:8e0d178b1d1e | 643 | return ret; |
wolfSSL | 15:117db924cf7c | 644 | } |
wolfSSL | 15:117db924cf7c | 645 | |
wolfSSL | 15:117db924cf7c | 646 | int wc_ShaFinalRaw(wc_Sha* sha, byte* hash) |
wolfSSL | 15:117db924cf7c | 647 | { |
wolfSSL | 15:117db924cf7c | 648 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 15:117db924cf7c | 649 | word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)]; |
wolfSSL | 15:117db924cf7c | 650 | #endif |
wolfSSL | 15:117db924cf7c | 651 | |
wolfSSL | 15:117db924cf7c | 652 | if (sha == NULL || hash == NULL) { |
wolfSSL | 15:117db924cf7c | 653 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 654 | } |
wolfSSL | 15:117db924cf7c | 655 | |
wolfSSL | 15:117db924cf7c | 656 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 15:117db924cf7c | 657 | ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 658 | XMEMCPY(hash, digest, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 659 | #else |
wolfSSL | 15:117db924cf7c | 660 | XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 661 | #endif |
wolfSSL | 15:117db924cf7c | 662 | |
wolfSSL | 15:117db924cf7c | 663 | return 0; |
wolfSSL | 15:117db924cf7c | 664 | } |
wolfSSL | 15:117db924cf7c | 665 | |
wolfSSL | 15:117db924cf7c | 666 | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
wolfSSL | 15:117db924cf7c | 667 | { |
wolfSSL | 16:8e0d178b1d1e | 668 | int ret; |
wolfSSL | 15:117db924cf7c | 669 | byte* local; |
wolfSSL | 15:117db924cf7c | 670 | |
wolfSSL | 15:117db924cf7c | 671 | if (sha == NULL || hash == NULL) { |
wolfSSL | 15:117db924cf7c | 672 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 673 | } |
wolfSSL | 15:117db924cf7c | 674 | |
wolfSSL | 15:117db924cf7c | 675 | local = (byte*)sha->buffer; |
wolfSSL | 15:117db924cf7c | 676 | |
wolfSSL | 16:8e0d178b1d1e | 677 | #ifdef WOLF_CRYPTO_CB |
wolfSSL | 16:8e0d178b1d1e | 678 | if (sha->devId != INVALID_DEVID) { |
wolfSSL | 16:8e0d178b1d1e | 679 | ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash); |
wolfSSL | 16:8e0d178b1d1e | 680 | if (ret != CRYPTOCB_UNAVAILABLE) |
wolfSSL | 16:8e0d178b1d1e | 681 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 682 | ret = 0; /* reset ret */ |
wolfSSL | 16:8e0d178b1d1e | 683 | /* fall-through when unavailable */ |
wolfSSL | 16:8e0d178b1d1e | 684 | } |
wolfSSL | 16:8e0d178b1d1e | 685 | #endif |
wolfSSL | 15:117db924cf7c | 686 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
wolfSSL | 15:117db924cf7c | 687 | if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) { |
wolfSSL | 15:117db924cf7c | 688 | #if defined(HAVE_INTEL_QA) |
wolfSSL | 15:117db924cf7c | 689 | return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 690 | #endif |
wolfSSL | 15:117db924cf7c | 691 | } |
wolfSSL | 15:117db924cf7c | 692 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 15:117db924cf7c | 693 | |
wolfSSL | 15:117db924cf7c | 694 | local[sha->buffLen++] = 0x80; /* add 1 */ |
wolfSSL | 15:117db924cf7c | 695 | |
wolfSSL | 15:117db924cf7c | 696 | /* pad with zeros */ |
wolfSSL | 15:117db924cf7c | 697 | if (sha->buffLen > WC_SHA_PAD_SIZE) { |
wolfSSL | 15:117db924cf7c | 698 | XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen); |
wolfSSL | 15:117db924cf7c | 699 | sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen; |
wolfSSL | 15:117db924cf7c | 700 | |
wolfSSL | 16:8e0d178b1d1e | 701 | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 15:117db924cf7c | 702 | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 703 | #endif |
wolfSSL | 16:8e0d178b1d1e | 704 | |
wolfSSL | 16:8e0d178b1d1e | 705 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 706 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 707 | if (sha->ctx.mode == ESP32_SHA_INIT) { |
wolfSSL | 16:8e0d178b1d1e | 708 | esp_sha_try_hw_lock(&sha->ctx); |
wolfSSL | 16:8e0d178b1d1e | 709 | } |
wolfSSL | 16:8e0d178b1d1e | 710 | if (sha->ctx.mode == ESP32_SHA_SW) { |
wolfSSL | 16:8e0d178b1d1e | 711 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 712 | } else { |
wolfSSL | 16:8e0d178b1d1e | 713 | ret = esp_sha_process(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 714 | } |
wolfSSL | 16:8e0d178b1d1e | 715 | #else |
wolfSSL | 16:8e0d178b1d1e | 716 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 717 | #endif |
wolfSSL | 16:8e0d178b1d1e | 718 | if (ret != 0) |
wolfSSL | 16:8e0d178b1d1e | 719 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 720 | |
wolfSSL | 15:117db924cf7c | 721 | sha->buffLen = 0; |
wolfSSL | 15:117db924cf7c | 722 | } |
wolfSSL | 15:117db924cf7c | 723 | XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen); |
wolfSSL | 15:117db924cf7c | 724 | |
wolfSSL | 15:117db924cf7c | 725 | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 15:117db924cf7c | 726 | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
wolfSSL | 15:117db924cf7c | 727 | #endif |
wolfSSL | 15:117db924cf7c | 728 | |
wolfSSL | 15:117db924cf7c | 729 | /* store lengths */ |
wolfSSL | 15:117db924cf7c | 730 | /* put lengths in bits */ |
wolfSSL | 15:117db924cf7c | 731 | sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3); |
wolfSSL | 15:117db924cf7c | 732 | sha->loLen = sha->loLen << 3; |
wolfSSL | 15:117db924cf7c | 733 | |
wolfSSL | 15:117db924cf7c | 734 | /* ! length ordering dependent on digest endian type ! */ |
wolfSSL | 15:117db924cf7c | 735 | XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32)); |
wolfSSL | 15:117db924cf7c | 736 | XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32)); |
wolfSSL | 15:117db924cf7c | 737 | |
wolfSSL | 15:117db924cf7c | 738 | #if defined(FREESCALE_MMCAU_SHA) |
wolfSSL | 15:117db924cf7c | 739 | /* Kinetis requires only these bytes reversed */ |
wolfSSL | 15:117db924cf7c | 740 | ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], |
wolfSSL | 15:117db924cf7c | 741 | &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], |
wolfSSL | 15:117db924cf7c | 742 | 2 * sizeof(word32)); |
wolfSSL | 15:117db924cf7c | 743 | #endif |
wolfSSL | 15:117db924cf7c | 744 | |
wolfSSL | 16:8e0d178b1d1e | 745 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 746 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 747 | if (sha->ctx.mode == ESP32_SHA_INIT) { |
wolfSSL | 16:8e0d178b1d1e | 748 | esp_sha_try_hw_lock(&sha->ctx); |
wolfSSL | 16:8e0d178b1d1e | 749 | } |
wolfSSL | 16:8e0d178b1d1e | 750 | if (sha->ctx.mode == ESP32_SHA_SW) { |
wolfSSL | 16:8e0d178b1d1e | 751 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 752 | } else { |
wolfSSL | 16:8e0d178b1d1e | 753 | ret = esp_sha_digest_process(sha, 1); |
wolfSSL | 16:8e0d178b1d1e | 754 | } |
wolfSSL | 16:8e0d178b1d1e | 755 | #else |
wolfSSL | 16:8e0d178b1d1e | 756 | ret = XTRANSFORM(sha, (const byte*)local); |
wolfSSL | 16:8e0d178b1d1e | 757 | #endif |
wolfSSL | 16:8e0d178b1d1e | 758 | |
wolfSSL | 15:117db924cf7c | 759 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 15:117db924cf7c | 760 | ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 761 | #endif |
wolfSSL | 16:8e0d178b1d1e | 762 | |
wolfSSL | 15:117db924cf7c | 763 | XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE); |
wolfSSL | 15:117db924cf7c | 764 | |
wolfSSL | 16:8e0d178b1d1e | 765 | (void)InitSha(sha); /* reset state */ |
wolfSSL | 16:8e0d178b1d1e | 766 | |
wolfSSL | 16:8e0d178b1d1e | 767 | return ret; |
wolfSSL | 15:117db924cf7c | 768 | } |
wolfSSL | 15:117db924cf7c | 769 | |
wolfSSL | 15:117db924cf7c | 770 | #endif /* USE_SHA_SOFTWARE_IMPL */ |
wolfSSL | 15:117db924cf7c | 771 | |
wolfSSL | 15:117db924cf7c | 772 | |
wolfSSL | 15:117db924cf7c | 773 | int wc_InitSha(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 774 | { |
wolfSSL | 15:117db924cf7c | 775 | return wc_InitSha_ex(sha, NULL, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 776 | } |
wolfSSL | 15:117db924cf7c | 777 | |
wolfSSL | 15:117db924cf7c | 778 | void wc_ShaFree(wc_Sha* sha) |
wolfSSL | 15:117db924cf7c | 779 | { |
wolfSSL | 15:117db924cf7c | 780 | if (sha == NULL) |
wolfSSL | 15:117db924cf7c | 781 | return; |
wolfSSL | 15:117db924cf7c | 782 | |
wolfSSL | 15:117db924cf7c | 783 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
wolfSSL | 15:117db924cf7c | 784 | wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA); |
wolfSSL | 15:117db924cf7c | 785 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 16:8e0d178b1d1e | 786 | |
wolfSSL | 16:8e0d178b1d1e | 787 | #ifdef WOLFSSL_PIC32MZ_HASH |
wolfSSL | 16:8e0d178b1d1e | 788 | wc_ShaPic32Free(sha); |
wolfSSL | 16:8e0d178b1d1e | 789 | #endif |
wolfSSL | 16:8e0d178b1d1e | 790 | #if (defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 791 | !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) |
wolfSSL | 16:8e0d178b1d1e | 792 | if (sha->msg != NULL) { |
wolfSSL | 16:8e0d178b1d1e | 793 | XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 16:8e0d178b1d1e | 794 | sha->msg = NULL; |
wolfSSL | 16:8e0d178b1d1e | 795 | } |
wolfSSL | 16:8e0d178b1d1e | 796 | #endif |
wolfSSL | 15:117db924cf7c | 797 | } |
wolfSSL | 15:117db924cf7c | 798 | |
wolfSSL | 15:117db924cf7c | 799 | #endif /* !WOLFSSL_TI_HASH */ |
wolfSSL | 15:117db924cf7c | 800 | #endif /* HAVE_FIPS */ |
wolfSSL | 15:117db924cf7c | 801 | |
wolfSSL | 15:117db924cf7c | 802 | #ifndef WOLFSSL_TI_HASH |
wolfSSL | 16:8e0d178b1d1e | 803 | #if !defined(WOLFSSL_RENESAS_TSIP_CRYPT) || \ |
wolfSSL | 16:8e0d178b1d1e | 804 | defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) |
wolfSSL | 15:117db924cf7c | 805 | int wc_ShaGetHash(wc_Sha* sha, byte* hash) |
wolfSSL | 15:117db924cf7c | 806 | { |
wolfSSL | 15:117db924cf7c | 807 | int ret; |
wolfSSL | 15:117db924cf7c | 808 | wc_Sha tmpSha; |
wolfSSL | 15:117db924cf7c | 809 | |
wolfSSL | 15:117db924cf7c | 810 | if (sha == NULL || hash == NULL) |
wolfSSL | 15:117db924cf7c | 811 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 812 | |
wolfSSL | 16:8e0d178b1d1e | 813 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 814 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 815 | if(sha->ctx.mode == ESP32_SHA_INIT){ |
wolfSSL | 16:8e0d178b1d1e | 816 | esp_sha_try_hw_lock(&sha->ctx); |
wolfSSL | 16:8e0d178b1d1e | 817 | } |
wolfSSL | 16:8e0d178b1d1e | 818 | if(sha->ctx.mode != ESP32_SHA_SW) |
wolfSSL | 16:8e0d178b1d1e | 819 | esp_sha_digest_process(sha, 0); |
wolfSSL | 16:8e0d178b1d1e | 820 | #endif |
wolfSSL | 16:8e0d178b1d1e | 821 | |
wolfSSL | 15:117db924cf7c | 822 | ret = wc_ShaCopy(sha, &tmpSha); |
wolfSSL | 15:117db924cf7c | 823 | if (ret == 0) { |
wolfSSL | 16:8e0d178b1d1e | 824 | ret = wc_ShaFinal(&tmpSha, hash); |
wolfSSL | 16:8e0d178b1d1e | 825 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 826 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 827 | sha->ctx.mode = ESP32_SHA_SW; |
wolfSSL | 16:8e0d178b1d1e | 828 | #endif |
wolfSSL | 16:8e0d178b1d1e | 829 | |
wolfSSL | 16:8e0d178b1d1e | 830 | |
wolfSSL | 15:117db924cf7c | 831 | } |
wolfSSL | 15:117db924cf7c | 832 | return ret; |
wolfSSL | 15:117db924cf7c | 833 | } |
wolfSSL | 15:117db924cf7c | 834 | |
wolfSSL | 15:117db924cf7c | 835 | int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) |
wolfSSL | 15:117db924cf7c | 836 | { |
wolfSSL | 15:117db924cf7c | 837 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 838 | |
wolfSSL | 15:117db924cf7c | 839 | if (src == NULL || dst == NULL) |
wolfSSL | 15:117db924cf7c | 840 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 841 | |
wolfSSL | 15:117db924cf7c | 842 | XMEMCPY(dst, src, sizeof(wc_Sha)); |
wolfSSL | 15:117db924cf7c | 843 | |
wolfSSL | 15:117db924cf7c | 844 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 845 | ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev); |
wolfSSL | 15:117db924cf7c | 846 | #endif |
wolfSSL | 15:117db924cf7c | 847 | #ifdef WOLFSSL_PIC32MZ_HASH |
wolfSSL | 15:117db924cf7c | 848 | ret = wc_Pic32HashCopy(&src->cache, &dst->cache); |
wolfSSL | 15:117db924cf7c | 849 | #endif |
wolfSSL | 16:8e0d178b1d1e | 850 | #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ |
wolfSSL | 16:8e0d178b1d1e | 851 | !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) |
wolfSSL | 16:8e0d178b1d1e | 852 | dst->ctx.mode = src->ctx.mode; |
wolfSSL | 16:8e0d178b1d1e | 853 | dst->ctx.isfirstblock = src->ctx.isfirstblock; |
wolfSSL | 16:8e0d178b1d1e | 854 | dst->ctx.sha_type = src->ctx.sha_type; |
wolfSSL | 16:8e0d178b1d1e | 855 | #endif |
wolfSSL | 16:8e0d178b1d1e | 856 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
wolfSSL | 16:8e0d178b1d1e | 857 | dst->flags |= WC_HASH_FLAG_ISCOPY; |
wolfSSL | 16:8e0d178b1d1e | 858 | #endif |
wolfSSL | 15:117db924cf7c | 859 | return ret; |
wolfSSL | 15:117db924cf7c | 860 | } |
wolfSSL | 16:8e0d178b1d1e | 861 | #endif /* defined(WOLFSSL_RENESAS_TSIP_CRYPT) ... */ |
wolfSSL | 15:117db924cf7c | 862 | #endif /* !WOLFSSL_TI_HASH */ |
wolfSSL | 15:117db924cf7c | 863 | |
wolfSSL | 16:8e0d178b1d1e | 864 | |
wolfSSL | 16:8e0d178b1d1e | 865 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
wolfSSL | 16:8e0d178b1d1e | 866 | int wc_ShaSetFlags(wc_Sha* sha, word32 flags) |
wolfSSL | 16:8e0d178b1d1e | 867 | { |
wolfSSL | 16:8e0d178b1d1e | 868 | if (sha) { |
wolfSSL | 16:8e0d178b1d1e | 869 | sha->flags = flags; |
wolfSSL | 16:8e0d178b1d1e | 870 | } |
wolfSSL | 16:8e0d178b1d1e | 871 | return 0; |
wolfSSL | 16:8e0d178b1d1e | 872 | } |
wolfSSL | 16:8e0d178b1d1e | 873 | int wc_ShaGetFlags(wc_Sha* sha, word32* flags) |
wolfSSL | 16:8e0d178b1d1e | 874 | { |
wolfSSL | 16:8e0d178b1d1e | 875 | if (sha && flags) { |
wolfSSL | 16:8e0d178b1d1e | 876 | *flags = sha->flags; |
wolfSSL | 16:8e0d178b1d1e | 877 | } |
wolfSSL | 16:8e0d178b1d1e | 878 | return 0; |
wolfSSL | 16:8e0d178b1d1e | 879 | } |
wolfSSL | 16:8e0d178b1d1e | 880 | #endif |
wolfSSL | 16:8e0d178b1d1e | 881 | |
wolfSSL | 15:117db924cf7c | 882 | #endif /* !NO_SHA */ |
wolfSSL | 15:117db924cf7c | 883 |