sha512.h (11530B)
1 /* sha512.h 2 * 3 * Copyright (C) 2006-2025 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 /*! 23 \file wolfssl/wolfcrypt/sha512.h 24 */ 25 26 27 #ifndef WOLF_CRYPT_SHA512_H 28 #define WOLF_CRYPT_SHA512_H 29 30 #include <wolfssl/wolfcrypt/types.h> 31 32 #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) 33 34 35 #if FIPS_VERSION3_GE(2,0,0) 36 #include <wolfssl/wolfcrypt/fips.h> 37 #endif /* HAVE_FIPS_VERSION >= 2 */ 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 #if FIPS_VERSION3_GE(6,0,0) 44 extern const unsigned int wolfCrypt_FIPS_sha512_ro_sanity[2]; 45 WOLFSSL_LOCAL int wolfCrypt_FIPS_SHA512_sanity(void); 46 #endif 47 48 /* avoid redefinition of structs */ 49 #if !defined(HAVE_FIPS) || \ 50 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 51 52 #ifdef WOLFSSL_ASYNC_CRYPT 53 #include <wolfssl/wolfcrypt/async.h> 54 #endif 55 #ifdef WOLFSSL_ESP32_CRYPT 56 #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h> 57 #endif 58 #if defined(WOLFSSL_SILABS_SE_ACCEL) 59 #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h> 60 #endif 61 #if defined(WOLFSSL_PSOC6_CRYPTO) 62 #include "cy_crypto_core_sha.h" 63 #include "cy_device_headers.h" 64 #include "cy_crypto_common.h" 65 #include "cy_crypto_core.h" 66 #endif 67 #if defined(WOLFSSL_KCAPI_HASH) 68 #include <wolfssl/wolfcrypt/port/kcapi/kcapi_hash.h> 69 #endif 70 71 #if defined(WOLFSSL_IMXRT1170_CAAM) 72 #include "fsl_device_registers.h" 73 #include "fsl_caam.h" 74 #endif 75 76 #ifdef STM32_HASH 77 #include <wolfssl/wolfcrypt/port/st/stm32.h> 78 #endif 79 80 #if defined(_MSC_VER) 81 #define SHA512_NOINLINE __declspec(noinline) 82 #elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__) 83 #define SHA512_NOINLINE __attribute__((noinline)) 84 #else 85 #define SHA512_NOINLINE 86 #endif 87 88 #ifdef WOLFSSL_SHA512 89 90 #if !defined(NO_OLD_SHA_NAMES) 91 #define SHA512 WC_SHA512 92 #endif 93 94 #if !defined(NO_OLD_WC_NAMES) 95 #define Sha512 wc_Sha512 96 #define SHA512_BLOCK_SIZE WC_SHA512_BLOCK_SIZE 97 #define SHA512_DIGEST_SIZE WC_SHA512_DIGEST_SIZE 98 #define SHA512_PAD_SIZE WC_SHA512_PAD_SIZE 99 #endif 100 101 #endif /* WOLFSSL_SHA512 */ 102 103 /* in bytes */ 104 enum { 105 #ifdef WOLFSSL_SHA512 106 WC_SHA512 = WC_HASH_TYPE_SHA512, 107 #ifndef WOLFSSL_NOSHA512_224 108 WC_SHA512_224 = WC_HASH_TYPE_SHA512_224, 109 #endif 110 #ifndef WOLFSSL_NOSHA512_256 111 WC_SHA512_256 = WC_HASH_TYPE_SHA512_256, 112 #endif 113 #endif 114 WC_SHA512_BLOCK_SIZE = 128, 115 WC_SHA512_DIGEST_SIZE = 64, 116 WC_SHA512_PAD_SIZE = 112, 117 118 WC_SHA512_224_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE, 119 WC_SHA512_224_DIGEST_SIZE = 28, 120 WC_SHA512_224_PAD_SIZE = WC_SHA512_PAD_SIZE, 121 122 WC_SHA512_256_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE, 123 WC_SHA512_256_DIGEST_SIZE = 32, 124 WC_SHA512_256_PAD_SIZE = WC_SHA512_PAD_SIZE 125 }; 126 127 128 #if defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM) 129 #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h" 130 #elif defined(WOLFSSL_RENESAS_RSIP) && \ 131 !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH) 132 #include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h" 133 134 #else 135 #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) 136 #include "wolfssl/wolfcrypt/port/nxp/se050_port.h" 137 #endif 138 #ifdef HAVE_ARIA 139 #include "mcapi.h" 140 #include "mcapi_error.h" 141 #endif 142 #if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) 143 #include "wolfssl/wolfcrypt/port/maxim/max3266x.h" 144 #endif 145 /* wc_Sha512 digest */ 146 struct wc_Sha512 { 147 #ifdef WOLFSSL_PSOC6_CRYPTO 148 cy_stc_crypto_sha_state_t hash_state; 149 cy_en_crypto_sha_mode_t sha_mode; 150 cy_stc_crypto_v2_sha512_buffers_t sha_buffers; 151 void* heap; 152 #else 153 word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)]; 154 word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)]; 155 word32 buffLen; /* in bytes */ 156 word64 loLen; /* length in bytes */ 157 word64 hiLen; /* length in bytes */ 158 void* heap; 159 #ifdef USE_INTEL_SPEEDUP 160 const byte* data; 161 #endif 162 #ifdef WC_C_DYNAMIC_FALLBACK 163 int sha_method; 164 #endif 165 #ifdef WOLFSSL_ASYNC_CRYPT 166 WC_ASYNC_DEV asyncDev; 167 #endif /* WOLFSSL_ASYNC_CRYPT */ 168 #ifdef WOLFSSL_SMALL_STACK_CACHE 169 word64* W; 170 #endif 171 172 #if defined(WOLFSSL_ESP32_CRYPT) && \ 173 !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \ 174 (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) || \ 175 !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)) 176 WC_ESP32SHA ctx; 177 #endif 178 #if defined(WOLFSSL_SILABS_SE_ACCEL) 179 wc_silabs_sha_t silabsCtx; 180 #endif 181 #ifdef WOLFSSL_KCAPI_HASH 182 wolfssl_KCAPI_Hash kcapi; 183 #endif 184 #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) 185 SE050_HASH_Context se050Ctx; 186 #endif 187 #if defined(WOLFSSL_HASH_KEEP) 188 byte* msg; 189 word32 used; 190 word32 len; 191 #endif 192 #ifdef WOLF_CRYPTO_CB 193 int devId; 194 void* devCtx; /* generic crypto callback context */ 195 #endif 196 #if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA) 197 wc_MXC_Sha mxcCtx; 198 #endif 199 #ifdef WOLFSSL_HASH_FLAGS 200 word32 flags; /* enum wc_HashFlags in hash.h */ 201 #endif 202 #ifdef WOLFSSL_IMXRT1170_CAAM 203 caam_hash_ctx_t ctx; 204 caam_handle_t hndl; 205 #endif 206 #ifdef HAVE_ARIA 207 MC_HSESSION hSession; 208 #endif 209 #if defined(STM32_HASH_SHA512) 210 STM32_HASH_Context stmCtx; 211 #endif 212 #endif /* WOLFSSL_PSOC6_CRYPTO */ 213 }; 214 215 216 #ifndef WC_SHA512_TYPE_DEFINED 217 typedef struct wc_Sha512 wc_Sha512; 218 typedef struct wc_Sha512 wc_Sha512_224; 219 typedef struct wc_Sha512 wc_Sha512_256; 220 221 #define WC_SHA512_TYPE_DEFINED 222 #endif 223 #endif /* WOLFSSL_IMX6_CAAM && !WOLFSSL_QNX_CAAM */ 224 225 #endif /* HAVE_FIPS */ 226 227 #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) 228 229 #ifdef WOLFSSL_ARMASM 230 #ifdef __aarch64__ 231 void Transform_Sha512_Len_neon(wc_Sha512* sha512, const byte* data, 232 word32 len); 233 #ifdef WOLFSSL_ARMASM_CRYPTO_SHA512 234 void Transform_Sha512_Len_crypto(wc_Sha512* sha512, const byte* data, 235 word32 len); 236 #endif 237 #else 238 extern void Transform_Sha512_Len(wc_Sha512* sha512, const byte* data, 239 word32 len); 240 #endif 241 #endif 242 243 WOLFSSL_API int wc_InitSha512(wc_Sha512* sha); 244 WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha, void* heap, int devId); 245 WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha, const byte* data, word32 len); 246 WOLFSSL_API int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash); 247 WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, byte* hash); 248 WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha); 249 250 WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash); 251 WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst); 252 253 #if defined(WOLFSSL_HASH_KEEP) 254 WOLFSSL_API int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz); 255 #endif 256 #ifdef WOLFSSL_HASH_FLAGS 257 WOLFSSL_API int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags); 258 WOLFSSL_API int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags); 259 #endif 260 261 #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) 262 WOLFSSL_API int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data); 263 #endif 264 265 #if !defined(WOLFSSL_NOSHA512_224) && \ 266 (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST) 267 WOLFSSL_API int wc_InitSha512_224(wc_Sha512* sha); 268 WOLFSSL_API int wc_InitSha512_224_ex(wc_Sha512* sha, void* heap, int devId); 269 WOLFSSL_API int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len); 270 WOLFSSL_API int wc_Sha512_224FinalRaw(wc_Sha512* sha512, byte* hash); 271 WOLFSSL_API int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash); 272 WOLFSSL_API void wc_Sha512_224Free(wc_Sha512* sha); 273 WOLFSSL_API int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash); 274 WOLFSSL_API int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst); 275 #ifdef WOLFSSL_HASH_FLAGS 276 WOLFSSL_API int wc_Sha512_224SetFlags(wc_Sha512* sha512, word32 flags); 277 WOLFSSL_API int wc_Sha512_224GetFlags(wc_Sha512* sha512, word32* flags); 278 #endif 279 280 #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) 281 WOLFSSL_API int wc_Sha512_224Transform(wc_Sha512* sha, 282 const unsigned char* data); 283 #endif /* OPENSSL_EXTRA */ 284 #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */ 285 286 #if !defined(WOLFSSL_NOSHA512_256) && \ 287 (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST) 288 WOLFSSL_API int wc_InitSha512_256(wc_Sha512* sha); 289 WOLFSSL_API int wc_InitSha512_256_ex(wc_Sha512* sha, void* heap, int devId); 290 WOLFSSL_API int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len); 291 WOLFSSL_API int wc_Sha512_256FinalRaw(wc_Sha512* sha512, byte* hash); 292 WOLFSSL_API int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash); 293 WOLFSSL_API void wc_Sha512_256Free(wc_Sha512* sha); 294 WOLFSSL_API int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash); 295 WOLFSSL_API int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst); 296 #ifdef WOLFSSL_HASH_FLAGS 297 WOLFSSL_API int wc_Sha512_256SetFlags(wc_Sha512* sha512, word32 flags); 298 WOLFSSL_API int wc_Sha512_256GetFlags(wc_Sha512* sha512, word32* flags); 299 #endif 300 301 #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) 302 WOLFSSL_API int wc_Sha512_256Transform(wc_Sha512* sha, 303 const unsigned char* data); 304 #endif /* OPENSSL_EXTRA */ 305 #endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */ 306 307 #endif /* WOLFSSL_SHA512 */ 308 309 #if defined(WOLFSSL_SHA384) 310 311 /* avoid redefinition of structs */ 312 #if !defined(HAVE_FIPS) || \ 313 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 314 315 #if !defined(NO_OLD_SHA_NAMES) 316 #define SHA384 WC_SHA384 317 #endif 318 319 #if !defined(NO_OLD_WC_NAMES) 320 #define Sha384 wc_Sha384 321 #define SHA384_BLOCK_SIZE WC_SHA384_BLOCK_SIZE 322 #define SHA384_DIGEST_SIZE WC_SHA384_DIGEST_SIZE 323 #define SHA384_PAD_SIZE WC_SHA384_PAD_SIZE 324 #endif 325 326 /* in bytes */ 327 enum { 328 WC_SHA384 = WC_HASH_TYPE_SHA384, 329 WC_SHA384_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE, 330 WC_SHA384_DIGEST_SIZE = 48, 331 WC_SHA384_PAD_SIZE = WC_SHA512_PAD_SIZE 332 }; 333 334 335 #ifndef WC_SHA384_TYPE_DEFINED 336 typedef struct wc_Sha512 wc_Sha384; 337 #define WC_SHA384_TYPE_DEFINED 338 #endif 339 #endif /* HAVE_FIPS */ 340 341 WOLFSSL_API int wc_InitSha384(wc_Sha384* sha); 342 WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha, void* heap, int devId); 343 WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha, const byte* data, word32 len); 344 WOLFSSL_API int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash); 345 WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, byte* hash); 346 WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha); 347 348 WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash); 349 WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst); 350 351 #if defined(WOLFSSL_HASH_KEEP) 352 WOLFSSL_API int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz); 353 #endif 354 #ifdef WOLFSSL_HASH_FLAGS 355 WOLFSSL_API int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags); 356 WOLFSSL_API int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags); 357 #endif 358 359 #endif /* WOLFSSL_SHA384 */ 360 361 #ifdef __cplusplus 362 } /* extern "C" */ 363 #endif 364 365 #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */ 366 #endif /* WOLF_CRYPT_SHA512_H */