blob: cc55fa00db7a32767f4b9887877500c0d2f4ec88 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* sha.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/sha.h
24*/
25
26
27#ifndef WOLF_CRYPT_SHA_H
28#define WOLF_CRYPT_SHA_H
29
30#include <wolfssl/wolfcrypt/types.h>
31
32#ifndef NO_SHA
33
34#if defined(HAVE_FIPS) && \
35 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
36 #include <wolfssl/wolfcrypt/fips.h>
37#endif /* HAVE_FIPS_VERSION >= 2 */
38
39#if defined(HAVE_FIPS) && \
40 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
41#define wc_Sha Sha
42#define WC_SHA SHA
43#define WC_SHA_BLOCK_SIZE SHA_BLOCK_SIZE
44#define WC_SHA_DIGEST_SIZE SHA_DIGEST_SIZE
45#define WC_SHA_PAD_SIZE SHA_PAD_SIZE
46
47/* for fips @wc_fips */
48#include <cyassl/ctaocrypt/sha.h>
49#endif
50
51#ifdef FREESCALE_LTC_SHA
52 #include "fsl_ltc.h"
53#endif
54
55#ifdef WOLFSSL_IMXRT_DCP
56 #include "fsl_dcp.h"
57#endif
58
59#ifdef __cplusplus
60 extern "C" {
61#endif
62
63/* avoid redefinition of structs */
64#if !defined(HAVE_FIPS) || \
65 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
66
67#ifdef WOLFSSL_MICROCHIP_PIC32MZ
68 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
69#endif
70#ifdef STM32_HASH
71 #include <wolfssl/wolfcrypt/port/st/stm32.h>
72#endif
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
83#if !defined(NO_OLD_SHA_NAMES)
84 #define SHA WC_SHA
85#endif
86
87#ifndef NO_OLD_WC_NAMES
88 #define Sha wc_Sha
89 #define SHA_BLOCK_SIZE WC_SHA_BLOCK_SIZE
90 #define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE
91 #define SHA_PAD_SIZE WC_SHA_PAD_SIZE
92#endif
93
94/* in bytes */
95enum {
96 WC_SHA = WC_HASH_TYPE_SHA,
97 WC_SHA_BLOCK_SIZE = 64,
98 WC_SHA_DIGEST_SIZE = 20,
99 WC_SHA_PAD_SIZE = 56
100};
101
102
103#if defined(WOLFSSL_TI_HASH)
104 #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
105
106#elif defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
107 #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
108#elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
109 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
110 #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
111#else
112
113/* Sha digest */
114struct wc_Sha {
115#ifdef FREESCALE_LTC_SHA
116 ltc_hash_ctx_t ctx;
117#elif defined(STM32_HASH)
118 STM32_HASH_Context stmCtx;
119#elif defined(WOLFSSL_SILABS_SE_ACCEL)
120 wc_silabs_sha_t silabsCtx;
121#elif defined(WOLFSSL_IMXRT_DCP)
122 dcp_handle_t handle;
123 dcp_hash_ctx_t ctx;
124#else
125 word32 buffLen; /* in bytes */
126 word32 loLen; /* length in bytes */
127 word32 hiLen; /* length in bytes */
128 word32 buffer[WC_SHA_BLOCK_SIZE / sizeof(word32)];
129 #ifdef WOLFSSL_PIC32MZ_HASH
130 word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)];
131 #else
132 word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
133 #endif
134 void* heap;
135 #ifdef WOLFSSL_PIC32MZ_HASH
136 hashUpdCache cache; /* cache for updates */
137 #endif
138 #ifdef WOLFSSL_ASYNC_CRYPT
139 WC_ASYNC_DEV asyncDev;
140 #endif /* WOLFSSL_ASYNC_CRYPT */
141 #ifdef WOLF_CRYPTO_CB
142 int devId;
143 void* devCtx; /* generic crypto callback context */
144 #endif
145#endif
146#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
147 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
148 WC_ESP32SHA ctx;
149#endif
150#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
151 word32 flags; /* enum wc_HashFlags in hash.h */
152#endif
153};
154
155#ifndef WC_SHA_TYPE_DEFINED
156 typedef struct wc_Sha wc_Sha;
157 #define WC_SHA_TYPE_DEFINED
158#endif
159
160#endif /* WOLFSSL_TI_HASH */
161
162
163#endif /* HAVE_FIPS */
164
165WOLFSSL_API int wc_InitSha(wc_Sha*);
166WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId);
167WOLFSSL_API int wc_ShaUpdate(wc_Sha*, const byte*, word32);
168WOLFSSL_API int wc_ShaFinalRaw(wc_Sha*, byte*);
169WOLFSSL_API int wc_ShaFinal(wc_Sha*, byte*);
170WOLFSSL_API void wc_ShaFree(wc_Sha*);
171
172WOLFSSL_API int wc_ShaGetHash(wc_Sha*, byte*);
173WOLFSSL_API int wc_ShaCopy(wc_Sha*, wc_Sha*);
174#if defined(OPENSSL_EXTRA)
175WOLFSSL_API int wc_ShaTransform(wc_Sha*, const byte*);
176#endif
177
178#ifdef WOLFSSL_PIC32MZ_HASH
179WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len);
180#endif
181
182#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
183 WOLFSSL_API int wc_ShaSetFlags(wc_Sha* sha, word32 flags);
184 WOLFSSL_API int wc_ShaGetFlags(wc_Sha* sha, word32* flags);
185#endif
186
187#ifdef __cplusplus
188 } /* extern "C" */
189#endif
190
191#endif /* NO_SHA */
192#endif /* WOLF_CRYPT_SHA_H */
193