sha256.h
Go to the documentation of this file.
1 /* sha256.h
2  *
3  * Copyright (C) 2006-2020 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 
28 #ifndef WOLF_CRYPT_SHA256_H
29 #define WOLF_CRYPT_SHA256_H
30 
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 
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_MICROCHIP_PIC32MZ
74  #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
75 #endif
76 #ifdef STM32_HASH
77  #include <wolfssl/wolfcrypt/port/st/stm32.h>
78 #endif
79 #ifdef WOLFSSL_ASYNC_CRYPT
80  #include <wolfssl/wolfcrypt/async.h>
81 #endif
82 #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HASH)
83  #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
84 #endif
85 #if defined(WOLFSSL_ESP32WROOM32_CRYPT)
86  #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
87 #endif
88 #if defined(WOLFSSL_CRYPTOCELL)
89  #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
90 #endif
91 
92 #if defined(_MSC_VER)
93  #define SHA256_NOINLINE __declspec(noinline)
94 #elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
95  #define SHA256_NOINLINE __attribute__((noinline))
96 #else
97  #define SHA256_NOINLINE
98 #endif
99 
100 #if !defined(NO_OLD_SHA_NAMES)
101  #define SHA256 WC_SHA256
102 #endif
103 
104 #ifndef NO_OLD_WC_NAMES
105  #define Sha256 wc_Sha256
106  #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
107  #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
108  #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE
109 #endif
110 
111 /* in bytes */
112 enum {
113  WC_SHA256 = WC_HASH_TYPE_SHA256,
114  WC_SHA256_BLOCK_SIZE = 64,
115  WC_SHA256_DIGEST_SIZE = 32,
116  WC_SHA256_PAD_SIZE = 56
117 };
118 
119 
120 #ifdef WOLFSSL_TI_HASH
121  #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
122 #elif defined(WOLFSSL_IMX6_CAAM)
123  #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
124 #elif defined(WOLFSSL_AFALG_HASH)
125  #include "wolfssl/wolfcrypt/port/af_alg/afalg_hash.h"
126 #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
127  !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
128  #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
129 #else
130 
131 /* wc_Sha256 digest */
132 struct wc_Sha256 {
133 #ifdef FREESCALE_LTC_SHA
134  ltc_hash_ctx_t ctx;
135 #elif defined(STM32_HASH_SHA2)
136  STM32_HASH_Context stmCtx;
137 #else
138  /* alignment on digest and buffer speeds up ARMv8 crypto operations */
139  ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
140  ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
141  word32 buffLen; /* in bytes */
142  word32 loLen; /* length in bytes */
143  word32 hiLen; /* length in bytes */
144  void* heap;
145 #ifdef WOLFSSL_PIC32MZ_HASH
146  hashUpdCache cache; /* cache for updates */
147 #endif
148 #ifdef WOLFSSL_ASYNC_CRYPT
149  WC_ASYNC_DEV asyncDev;
150 #endif /* WOLFSSL_ASYNC_CRYPT */
151 #ifdef WOLFSSL_SMALL_STACK_CACHE
152  word32* W;
153 #endif
154 #ifdef WOLFSSL_DEVCRYPTO_HASH
155  WC_CRYPTODEV ctx;
156  byte* msg;
157  word32 used;
158  word32 len;
159 #endif
160 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
161  !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
162  WC_ESP32SHA ctx;
163 #endif
164 #ifdef WOLFSSL_CRYPTOCELL
165  CRYS_HASHUserContext_t ctx;
166 #endif
167 #ifdef WOLF_CRYPTO_CB
168  int devId;
169  void* devCtx; /* generic crypto callback context */
170 #endif
171 #endif
172 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
173  word32 flags; /* enum wc_HashFlags in hash.h */
174 #endif
175 };
176 
177 #ifndef WC_SHA256_TYPE_DEFINED
178  typedef struct wc_Sha256 wc_Sha256;
179  #define WC_SHA256_TYPE_DEFINED
180 #endif
181 
182 #endif
183 
184 #endif /* HAVE_FIPS */
185 
186 WOLFSSL_API int wc_InitSha256(wc_Sha256*);
187 WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int);
188 WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32);
189 WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*);
190 WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*);
191 WOLFSSL_API void wc_Sha256Free(wc_Sha256*);
192 
193 WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*);
194 WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
195 
196 #ifdef WOLFSSL_PIC32MZ_HASH
197 WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
198 #endif
199 
200 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
201  WOLFSSL_API int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags);
202  WOLFSSL_API int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags);
203 #endif
204 
205 #ifdef WOLFSSL_SHA224
206 /* avoid redefinition of structs */
207 #if !defined(HAVE_FIPS) || \
208  (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
209 
210 #ifndef NO_OLD_WC_NAMES
211  #define Sha224 wc_Sha224
212  #define SHA224 WC_SHA224
213  #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
214  #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
215  #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
216 #endif
217 
218 /* in bytes */
219 enum {
220  WC_SHA224 = WC_HASH_TYPE_SHA224,
221  WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
222  WC_SHA224_DIGEST_SIZE = 28,
223  WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
224 };
225 
226 
227 #ifndef WC_SHA224_TYPE_DEFINED
228  typedef struct wc_Sha256 wc_Sha224;
229  #define WC_SHA224_TYPE_DEFINED
230 #endif
231 #endif /* HAVE_FIPS */
232 
233 WOLFSSL_API int wc_InitSha224(wc_Sha224*);
234 WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int);
235 WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32);
236 WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*);
237 WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
238 
239 WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
240 WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
241 
242 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
243  WOLFSSL_API int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags);
244  WOLFSSL_API int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags);
245 #endif
246 
247 #endif /* WOLFSSL_SHA224 */
248 
249 #ifdef __cplusplus
250  } /* extern "C" */
251 #endif
252 
253 #endif /* NO_SHA256 */
254 #endif /* WOLF_CRYPT_SHA256_H */
255 
Definition: sha256.h:132
Definition: wc_devcrypto.h:35
WOLFSSL_API void wc_Sha256Free(wc_Sha256 *)
Resets the Sha256 structure. Note: this is only supported if you have WOLFSSL_TI_HASH defined...
Definition: sha256.c:1448
Definition: ti-hash.h:38
WOLFSSL_API int wc_InitSha256(wc_Sha256 *)
This function initializes SHA256. This is automatically called by wc_Sha256Hash.
Definition: sha256.c:1443
WOLFSSL_API int wc_Sha256Update(wc_Sha256 *, const byte *, word32)
Can be called to continually hash the provided byte array of length len.
Definition: sha256.c:501
Definition: pic32mz-crypt.h:115
Definition: stm32.h:67
WOLFSSL_API int wc_InitSha224(wc_Sha224 *)
Used to initialize a Sha224 struct.
Definition: armv8-sha256.c:1419
Definition: esp32-crypt.h:104
WOLFSSL_API int wc_Sha256GetHash(wc_Sha256 *, byte *)
Gets hash data. Result is placed into hash. Does not reset state of sha256 struct.
Definition: afalg_hash.c:255
WOLFSSL_API int wc_Sha224Final(wc_Sha224 *, byte *)
Finalizes hashing of data. Result is placed into hash. Resets state of sha224 struct.
Definition: sha256.c:1385
WOLFSSL_API int wc_Sha256Final(wc_Sha256 *, byte *)
Finalizes hashing of data. Result is placed into hash. Resets state of sha256 struct.
Definition: sha256.c:518
WOLFSSL_API int wc_Sha224Update(wc_Sha224 *, const byte *, word32)
Can be called to continually hash the provided byte array of length len.
Definition: sha256.c:1364