sha.h
Go to the documentation of this file.
1 /* sha.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 
27 #ifndef WOLF_CRYPT_SHA_H
28 #define WOLF_CRYPT_SHA_H
29 
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 __cplusplus
56  extern "C" {
57 #endif
58 
59 /* avoid redefinition of structs */
60 #if !defined(HAVE_FIPS) || \
61  (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
62 
63 #ifdef WOLFSSL_MICROCHIP_PIC32MZ
64  #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
65 #endif
66 #ifdef STM32_HASH
67  #include <wolfssl/wolfcrypt/port/st/stm32.h>
68 #endif
69 #ifdef WOLFSSL_ASYNC_CRYPT
70  #include <wolfssl/wolfcrypt/async.h>
71 #endif
72 #ifdef WOLFSSL_ESP32WROOM32_CRYPT
73  #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
74 #endif
75 
76 #if !defined(NO_OLD_SHA_NAMES)
77  #define SHA WC_SHA
78 #endif
79 
80 #ifndef NO_OLD_WC_NAMES
81  #define Sha wc_Sha
82  #define SHA_BLOCK_SIZE WC_SHA_BLOCK_SIZE
83  #define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE
84  #define SHA_PAD_SIZE WC_SHA_PAD_SIZE
85 #endif
86 
87 /* in bytes */
88 enum {
89  WC_SHA = WC_HASH_TYPE_SHA,
90  WC_SHA_BLOCK_SIZE = 64,
91  WC_SHA_DIGEST_SIZE = 20,
92  WC_SHA_PAD_SIZE = 56
93 };
94 
95 
96 #if defined(WOLFSSL_TI_HASH)
97  #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
98 
99 #elif defined(WOLFSSL_IMX6_CAAM)
100  #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
101 #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
102  !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
103  #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
104 #else
105 
106 /* Sha digest */
107 struct wc_Sha {
108 #ifdef FREESCALE_LTC_SHA
109  ltc_hash_ctx_t ctx;
110 #elif defined(STM32_HASH)
111  STM32_HASH_Context stmCtx;
112 #else
113  word32 buffLen; /* in bytes */
114  word32 loLen; /* length in bytes */
115  word32 hiLen; /* length in bytes */
116  word32 buffer[WC_SHA_BLOCK_SIZE / sizeof(word32)];
117  #ifdef WOLFSSL_PIC32MZ_HASH
118  word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)];
119  #else
120  word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
121  #endif
122  void* heap;
123  #ifdef WOLFSSL_PIC32MZ_HASH
124  hashUpdCache cache; /* cache for updates */
125  #endif
126  #ifdef WOLFSSL_ASYNC_CRYPT
127  WC_ASYNC_DEV asyncDev;
128  #endif /* WOLFSSL_ASYNC_CRYPT */
129  #ifdef WOLF_CRYPTO_CB
130  int devId;
131  void* devCtx; /* generic crypto callback context */
132  #endif
133 #endif
134 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
135  !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
136  WC_ESP32SHA ctx;
137 #endif
138 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
139  word32 flags; /* enum wc_HashFlags in hash.h */
140 #endif
141 };
142 
143 #ifndef WC_SHA_TYPE_DEFINED
144  typedef struct wc_Sha wc_Sha;
145  #define WC_SHA_TYPE_DEFINED
146 #endif
147 
148 #endif /* WOLFSSL_TI_HASH */
149 
150 
151 #endif /* HAVE_FIPS */
152 
153 WOLFSSL_API int wc_InitSha(wc_Sha*);
154 WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId);
155 WOLFSSL_API int wc_ShaUpdate(wc_Sha*, const byte*, word32);
156 WOLFSSL_API int wc_ShaFinalRaw(wc_Sha*, byte*);
157 WOLFSSL_API int wc_ShaFinal(wc_Sha*, byte*);
158 WOLFSSL_API void wc_ShaFree(wc_Sha*);
159 
160 WOLFSSL_API int wc_ShaGetHash(wc_Sha*, byte*);
161 WOLFSSL_API int wc_ShaCopy(wc_Sha*, wc_Sha*);
162 
163 #ifdef WOLFSSL_PIC32MZ_HASH
164 WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len);
165 #endif
166 
167 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
168  WOLFSSL_API int wc_ShaSetFlags(wc_Sha* sha, word32 flags);
169  WOLFSSL_API int wc_ShaGetFlags(wc_Sha* sha, word32* flags);
170 #endif
171 
172 #ifdef __cplusplus
173  } /* extern "C" */
174 #endif
175 
176 #endif /* NO_SHA */
177 #endif /* WOLF_CRYPT_SHA_H */
178 
WOLFSSL_API int wc_ShaUpdate(wc_Sha *, const byte *, word32)
Can be called to continually hash the provided byte array of length len.
Definition: sha.c:131
WOLFSSL_API int wc_ShaFinal(wc_Sha *, byte *)
Finalizes hashing of data. Result is placed into hash. Resets state of sha struct.
Definition: sha.c:148
Definition: pic32mz-crypt.h:115
Definition: stm32.h:67
WOLFSSL_API void wc_ShaFree(wc_Sha *)
Used to clean up memory used by an initialized Sha struct. Note: this is only supported if you have W...
Definition: sha.c:778
Definition: esp32-crypt.h:104
Definition: wolfcaam_sha.h:68
WOLFSSL_API int wc_InitSha(wc_Sha *)
This function initializes SHA. This is automatically called by wc_ShaHash.
Definition: sha.c:773
WOLFSSL_API int wc_ShaGetHash(wc_Sha *, byte *)
Gets hash data. Result is placed into hash. Does not reset state of sha struct.
Definition: renesas_tsip_sha.c:233