blob: 87744729bab7cb04852f674bbb9e6a89be43f443 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* sha256.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/sha256.h
24*/
25
26
27
28#ifndef WOLF_CRYPT_SHA256_H
29#define WOLF_CRYPT_SHA256_H
30
31#include <wolfssl/wolfcrypt/types.h>
32
33#ifndef NO_SHA256
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 #define wc_Sha256 Sha256
43 #define WC_SHA256 SHA256
44 #define WC_SHA256_BLOCK_SIZE SHA256_BLOCK_SIZE
45 #define WC_SHA256_DIGEST_SIZE SHA256_DIGEST_SIZE
46 #define WC_SHA256_PAD_SIZE SHA256_PAD_SIZE
47
48 #ifdef WOLFSSL_SHA224
49 #define wc_Sha224 Sha224
50 #define WC_SHA224 SHA224
51 #define WC_SHA224_BLOCK_SIZE SHA224_BLOCK_SIZE
52 #define WC_SHA224_DIGEST_SIZE SHA224_DIGEST_SIZE
53 #define WC_SHA224_PAD_SIZE SHA224_PAD_SIZE
54 #endif
55
56 /* for fips @wc_fips */
57 #include <cyassl/ctaocrypt/sha256.h>
58#endif
59
60#ifdef FREESCALE_LTC_SHA
61 #include "fsl_ltc.h"
62#endif
63
64#ifdef WOLFSSL_IMXRT_DCP
65 #include "fsl_dcp.h"
66#endif
67
68#if defined(WOLFSSL_PSOC6_CRYPTO)
69#include "cy_crypto_core_sha.h"
70#include "cy_device_headers.h"
71#include "cy_crypto_common.h"
72#include "cy_crypto_core.h"
73#endif
74
75#ifdef __cplusplus
76 extern "C" {
77#endif
78
79/* avoid redefinition of structs */
80#if !defined(HAVE_FIPS) || \
81 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
82
83#ifdef WOLFSSL_MICROCHIP_PIC32MZ
84 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
85#endif
86#ifdef STM32_HASH
87 #include <wolfssl/wolfcrypt/port/st/stm32.h>
88#endif
89#ifdef WOLFSSL_ASYNC_CRYPT
90 #include <wolfssl/wolfcrypt/async.h>
91#endif
92#if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HASH)
93 #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
94#endif
95#if defined(WOLFSSL_ESP32WROOM32_CRYPT)
96 #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
97#endif
98#if defined(WOLFSSL_CRYPTOCELL)
99 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
100#endif
101#if defined(WOLFSSL_SILABS_SE_ACCEL)
102 #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
103#endif
104
105#if defined(_MSC_VER)
106 #define SHA256_NOINLINE __declspec(noinline)
107#elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
108 #define SHA256_NOINLINE __attribute__((noinline))
109#else
110 #define SHA256_NOINLINE
111#endif
112
113#if !defined(NO_OLD_SHA_NAMES)
114 #define SHA256 WC_SHA256
115#endif
116
117#ifndef NO_OLD_WC_NAMES
118 #define Sha256 wc_Sha256
119 #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
120 #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
121 #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE
122#endif
123
124/* in bytes */
125enum {
126 WC_SHA256 = WC_HASH_TYPE_SHA256,
127 WC_SHA256_BLOCK_SIZE = 64,
128 WC_SHA256_DIGEST_SIZE = 32,
129 WC_SHA256_PAD_SIZE = 56
130};
131
132
133#ifdef WOLFSSL_TI_HASH
134 #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
135#elif defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
136 #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
137#elif defined(WOLFSSL_AFALG_HASH)
138 #include "wolfssl/wolfcrypt/port/af_alg/afalg_hash.h"
139#elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
140 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
141 #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
142#else
143
144/* wc_Sha256 digest */
145struct wc_Sha256 {
146#ifdef FREESCALE_LTC_SHA
147 ltc_hash_ctx_t ctx;
148#elif defined(STM32_HASH_SHA2)
149 STM32_HASH_Context stmCtx;
150#elif defined(WOLFSSL_SILABS_SE_ACCEL)
151 wc_silabs_sha_t silabsCtx;
152#elif defined(WOLFSSL_IMXRT_DCP)
153 dcp_handle_t handle;
154 dcp_hash_ctx_t ctx;
155#elif defined(WOLFSSL_PSOC6_CRYPTO)
156 cy_stc_crypto_sha_state_t hash_state;
157 cy_en_crypto_sha_mode_t sha_mode;
158 cy_stc_crypto_v2_sha256_buffers_t sha_buffers;
159#else
160 /* alignment on digest and buffer speeds up ARMv8 crypto operations */
161 ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
162 ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
163 word32 buffLen; /* in bytes */
164 word32 loLen; /* length in bytes */
165 word32 hiLen; /* length in bytes */
166 void* heap;
167#endif
168#ifdef WOLFSSL_PIC32MZ_HASH
169 hashUpdCache cache; /* cache for updates */
170#endif
171#ifdef WOLFSSL_ASYNC_CRYPT
172 WC_ASYNC_DEV asyncDev;
173#endif /* WOLFSSL_ASYNC_CRYPT */
174#ifdef WOLFSSL_SMALL_STACK_CACHE
175 word32* W;
176#endif /* !FREESCALE_LTC_SHA && !STM32_HASH_SHA2 */
177#ifdef WOLFSSL_DEVCRYPTO_HASH
178 WC_CRYPTODEV ctx;
179 byte* msg;
180 word32 used;
181 word32 len;
182#endif
183#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
184 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
185 WC_ESP32SHA ctx;
186#endif
187#ifdef WOLFSSL_CRYPTOCELL
188 CRYS_HASHUserContext_t ctx;
189#endif
190#ifdef WOLF_CRYPTO_CB
191 int devId;
192 void* devCtx; /* generic crypto callback context */
193#endif
194#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
195 word32 flags; /* enum wc_HashFlags in hash.h */
196#endif
197};
198
199#ifndef WC_SHA256_TYPE_DEFINED
200 typedef struct wc_Sha256 wc_Sha256;
201 #define WC_SHA256_TYPE_DEFINED
202#endif
203
204#endif
205
206#endif /* HAVE_FIPS */
207
208WOLFSSL_API int wc_InitSha256(wc_Sha256*);
209WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int);
210WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32);
211WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*);
212WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*);
213WOLFSSL_API void wc_Sha256Free(wc_Sha256*);
214#if defined(OPENSSL_EXTRA)
215WOLFSSL_API int wc_Sha256Transform(wc_Sha256*, const byte*);
216#endif
217WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*);
218WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
219
220#ifdef WOLFSSL_PIC32MZ_HASH
221WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
222#endif
223
224#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
225 WOLFSSL_API int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags);
226 WOLFSSL_API int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags);
227#endif
228
229#ifdef WOLFSSL_SHA224
230/* avoid redefinition of structs */
231#if !defined(HAVE_FIPS) || \
232 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
233
234#if !defined(NO_OLD_SHA_NAMES)
235 #define SHA224 WC_SHA224
236#endif
237
238#ifndef NO_OLD_WC_NAMES
239 #define Sha224 wc_Sha224
240 #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
241 #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
242 #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
243#endif
244
245/* in bytes */
246enum {
247 WC_SHA224 = WC_HASH_TYPE_SHA224,
248 WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
249 WC_SHA224_DIGEST_SIZE = 28,
250 WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
251};
252
253
254#ifndef WC_SHA224_TYPE_DEFINED
255 typedef struct wc_Sha256 wc_Sha224;
256 #define WC_SHA224_TYPE_DEFINED
257#endif
258#endif /* HAVE_FIPS */
259
260WOLFSSL_API int wc_InitSha224(wc_Sha224*);
261WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int);
262WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32);
263WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*);
264WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
265
266WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
267WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
268
269#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
270 WOLFSSL_API int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags);
271 WOLFSSL_API int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags);
272#endif
273
274#endif /* WOLFSSL_SHA224 */
275
276#ifdef __cplusplus
277 } /* extern "C" */
278#endif
279
280#endif /* NO_SHA256 */
281#endif /* WOLF_CRYPT_SHA256_H */
282