blob: 81da52ebb9c9ac6eeaabfb84ee3411665f639d7a [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* sha512.h
2 *
3 * Copyright (C) 2006-2021 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 2 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 defined(HAVE_FIPS) && \
36 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
37 #include <wolfssl/wolfcrypt/fips.h>
38#endif /* HAVE_FIPS_VERSION >= 2 */
39
40#if defined(HAVE_FIPS) && \
41 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
42 #ifdef WOLFSSL_SHA512
43 #define wc_Sha512 Sha512
44 #define WC_SHA512 SHA512
45 #define WC_SHA512_BLOCK_SIZE SHA512_BLOCK_SIZE
46 #define WC_SHA512_DIGEST_SIZE SHA512_DIGEST_SIZE
47 #define WC_SHA512_PAD_SIZE SHA512_PAD_SIZE
48 #endif /* WOLFSSL_SHA512 */
49 #ifdef WOLFSSL_SHA384
50 #define wc_Sha384 Sha384
51 #define WC_SHA384 SHA384
52 #define WC_SHA384_BLOCK_SIZE SHA384_BLOCK_SIZE
53 #define WC_SHA384_DIGEST_SIZE SHA384_DIGEST_SIZE
54 #define WC_SHA384_PAD_SIZE SHA384_PAD_SIZE
55 #endif /* WOLFSSL_SHA384 */
56
57 #define CYASSL_SHA512
58 #if defined(WOLFSSL_SHA384)
59 #define CYASSL_SHA384
60 #endif
61 /* for fips @wc_fips */
62 #include <cyassl/ctaocrypt/sha512.h>
63#endif
64
65#ifdef __cplusplus
66 extern "C" {
67#endif
68
69/* avoid redefinition of structs */
70#if !defined(HAVE_FIPS) || \
71 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
72
73#ifdef WOLFSSL_ASYNC_CRYPT
74 #include <wolfssl/wolfcrypt/async.h>
75#endif
76#ifdef WOLFSSL_ESP32WROOM32_CRYPT
77 #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
78#endif
79#if defined(WOLFSSL_SILABS_SE_ACCEL)
80 #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
81#endif
82#if defined(WOLFSSL_PSOC6_CRYPTO)
83 #include "cy_crypto_core_sha.h"
84 #include "cy_device_headers.h"
85 #include "cy_crypto_common.h"
86 #include "cy_crypto_core.h"
87#endif
88
89#if defined(_MSC_VER)
90 #define SHA512_NOINLINE __declspec(noinline)
91#elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
92 #define SHA512_NOINLINE __attribute__((noinline))
93#else
94 #define SHA512_NOINLINE
95#endif
96
97#ifdef WOLFSSL_SHA512
98
99#if !defined(NO_OLD_SHA_NAMES)
100 #define SHA512 WC_SHA512
101#endif
102
103#if !defined(NO_OLD_WC_NAMES)
104 #define Sha512 wc_Sha512
105 #define SHA512_BLOCK_SIZE WC_SHA512_BLOCK_SIZE
106 #define SHA512_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
107 #define SHA512_PAD_SIZE WC_SHA512_PAD_SIZE
108#endif
109
110#endif /* WOLFSSL_SHA512 */
111
112/* in bytes */
113enum {
114#ifdef WOLFSSL_SHA512
115 WC_SHA512 = WC_HASH_TYPE_SHA512,
116#endif
117 WC_SHA512_BLOCK_SIZE = 128,
118 WC_SHA512_DIGEST_SIZE = 64,
119 WC_SHA512_PAD_SIZE = 112
120};
121
122
123#if defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
124 #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
125#else
126/* wc_Sha512 digest */
127struct wc_Sha512 {
128#ifdef WOLFSSL_PSOC6_CRYPTO
129 cy_stc_crypto_sha_state_t hash_state;
130 cy_en_crypto_sha_mode_t sha_mode;
131 cy_stc_crypto_v2_sha512_buffers_t sha_buffers;
132#else
133 word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
134 word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];
135 word32 buffLen; /* in bytes */
136 word64 loLen; /* length in bytes */
137 word64 hiLen; /* length in bytes */
138 void* heap;
139#ifdef USE_INTEL_SPEEDUP
140 const byte* data;
141#endif
142#ifdef WOLFSSL_ASYNC_CRYPT
143 WC_ASYNC_DEV asyncDev;
144#endif /* WOLFSSL_ASYNC_CRYPT */
145#ifdef WOLFSSL_SMALL_STACK_CACHE
146 word64* W;
147#endif
148#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
149 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
150 WC_ESP32SHA ctx;
151#endif
152#if defined(WOLFSSL_SILABS_SE_ACCEL)
153 wc_silabs_sha_t silabsCtx;
154#endif
155#ifdef WOLF_CRYPTO_CB
156 int devId;
157 void* devCtx; /* generic crypto callback context */
158#endif
159#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
160 word32 flags; /* enum wc_HashFlags in hash.h */
161#endif
162#endif /* WOLFSSL_PSOC6_CRYPTO */
163};
164
165#ifndef WC_SHA512_TYPE_DEFINED
166 typedef struct wc_Sha512 wc_Sha512;
167 #define WC_SHA512_TYPE_DEFINED
168#endif
169#endif
170
171#endif /* HAVE_FIPS */
172
173#ifdef WOLFSSL_ARMASM
174WOLFSSL_LOCAL void Transform_Sha512_Len(wc_Sha512* sha512, const byte* data,
175 word32 len);
176#endif
177
178#ifdef WOLFSSL_SHA512
179
180
181WOLFSSL_API int wc_InitSha512(wc_Sha512*);
182WOLFSSL_API int wc_InitSha512_ex(wc_Sha512*, void*, int);
183WOLFSSL_API int wc_Sha512Update(wc_Sha512*, const byte*, word32);
184WOLFSSL_API int wc_Sha512FinalRaw(wc_Sha512*, byte*);
185WOLFSSL_API int wc_Sha512Final(wc_Sha512*, byte*);
186WOLFSSL_API void wc_Sha512Free(wc_Sha512*);
187
188WOLFSSL_API int wc_Sha512GetHash(wc_Sha512*, byte*);
189WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst);
190
191#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
192 WOLFSSL_API int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags);
193 WOLFSSL_API int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags);
194#endif
195
196#if defined(OPENSSL_EXTRA)
197WOLFSSL_API int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data);
198#endif
199#endif /* WOLFSSL_SHA512 */
200
201#if defined(WOLFSSL_SHA384)
202
203/* avoid redefinition of structs */
204#if !defined(HAVE_FIPS) || \
205 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
206
207#if !defined(NO_OLD_SHA_NAMES)
208 #define SHA384 WC_SHA384
209#endif
210
211#if !defined(NO_OLD_WC_NAMES)
212 #define Sha384 wc_Sha384
213 #define SHA384_BLOCK_SIZE WC_SHA384_BLOCK_SIZE
214 #define SHA384_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
215 #define SHA384_PAD_SIZE WC_SHA384_PAD_SIZE
216#endif
217
218/* in bytes */
219enum {
220 WC_SHA384 = WC_HASH_TYPE_SHA384,
221 WC_SHA384_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
222 WC_SHA384_DIGEST_SIZE = 48,
223 WC_SHA384_PAD_SIZE = WC_SHA512_PAD_SIZE
224};
225
226
227#ifndef WC_SHA384_TYPE_DEFINED
228 typedef struct wc_Sha512 wc_Sha384;
229 #define WC_SHA384_TYPE_DEFINED
230#endif
231#endif /* HAVE_FIPS */
232
233WOLFSSL_API int wc_InitSha384(wc_Sha384*);
234WOLFSSL_API int wc_InitSha384_ex(wc_Sha384*, void*, int);
235WOLFSSL_API int wc_Sha384Update(wc_Sha384*, const byte*, word32);
236WOLFSSL_API int wc_Sha384FinalRaw(wc_Sha384*, byte*);
237WOLFSSL_API int wc_Sha384Final(wc_Sha384*, byte*);
238WOLFSSL_API void wc_Sha384Free(wc_Sha384*);
239
240WOLFSSL_API int wc_Sha384GetHash(wc_Sha384*, byte*);
241WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst);
242
243#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
244 WOLFSSL_API int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags);
245 WOLFSSL_API int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags);
246#endif
247
248#endif /* WOLFSSL_SHA384 */
249
250#ifdef __cplusplus
251 } /* extern "C" */
252#endif
253
254#endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
255#endif /* WOLF_CRYPT_SHA512_H */
256