1 | /* sha256.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 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 | /* code submitted by raphael.huck@efixo.com */
|
---|
28 |
|
---|
29 | #ifndef WOLF_CRYPT_SHA256_H
|
---|
30 | #define WOLF_CRYPT_SHA256_H
|
---|
31 |
|
---|
32 | #include <wolfssl/wolfcrypt/types.h>
|
---|
33 |
|
---|
34 | #ifndef NO_SHA256
|
---|
35 |
|
---|
36 | #if defined(HAVE_FIPS) && \
|
---|
37 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
|
---|
38 | #include <wolfssl/wolfcrypt/fips.h>
|
---|
39 | #endif /* HAVE_FIPS_VERSION >= 2 */
|
---|
40 |
|
---|
41 | #if defined(HAVE_FIPS) && \
|
---|
42 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
|
---|
43 | #define wc_Sha256 Sha256
|
---|
44 | #define WC_SHA256 SHA256
|
---|
45 | #define WC_SHA256_BLOCK_SIZE SHA256_BLOCK_SIZE
|
---|
46 | #define WC_SHA256_DIGEST_SIZE SHA256_DIGEST_SIZE
|
---|
47 | #define WC_SHA256_PAD_SIZE SHA256_PAD_SIZE
|
---|
48 |
|
---|
49 | #ifdef WOLFSSL_SHA224
|
---|
50 | #define wc_Sha224 Sha224
|
---|
51 | #define WC_SHA224 SHA224
|
---|
52 | #define WC_SHA224_BLOCK_SIZE SHA224_BLOCK_SIZE
|
---|
53 | #define WC_SHA224_DIGEST_SIZE SHA224_DIGEST_SIZE
|
---|
54 | #define WC_SHA224_PAD_SIZE SHA224_PAD_SIZE
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | /* for fips @wc_fips */
|
---|
58 | #include <cyassl/ctaocrypt/sha256.h>
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #ifdef FREESCALE_LTC_SHA
|
---|
62 | #include "fsl_ltc.h"
|
---|
63 | #endif
|
---|
64 |
|
---|
65 |
|
---|
66 | #ifdef __cplusplus
|
---|
67 | extern "C" {
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | /* avoid redefinition of structs */
|
---|
71 | #if !defined(HAVE_FIPS) || \
|
---|
72 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
|
---|
73 |
|
---|
74 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ
|
---|
75 | #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
|
---|
76 | #endif
|
---|
77 | #ifdef STM32_HASH
|
---|
78 | #include <wolfssl/wolfcrypt/port/st/stm32.h>
|
---|
79 | #endif
|
---|
80 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
81 | #include <wolfssl/wolfcrypt/async.h>
|
---|
82 | #endif
|
---|
83 | #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HASH)
|
---|
84 | #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #if defined(_MSC_VER)
|
---|
88 | #define SHA256_NOINLINE __declspec(noinline)
|
---|
89 | #elif defined(__GNUC__)
|
---|
90 | #define SHA256_NOINLINE __attribute__((noinline))
|
---|
91 | #else
|
---|
92 | #define SHA256_NOINLINE
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | #if !defined(NO_OLD_SHA_NAMES)
|
---|
96 | #define SHA256 WC_SHA256
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | #ifndef NO_OLD_WC_NAMES
|
---|
100 | #define Sha256 wc_Sha256
|
---|
101 | #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
|
---|
102 | #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
|
---|
103 | #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /* in bytes */
|
---|
107 | enum {
|
---|
108 | WC_SHA256 = WC_HASH_TYPE_SHA256,
|
---|
109 | WC_SHA256_BLOCK_SIZE = 64,
|
---|
110 | WC_SHA256_DIGEST_SIZE = 32,
|
---|
111 | WC_SHA256_PAD_SIZE = 56
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | #ifdef WOLFSSL_TI_HASH
|
---|
116 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
---|
117 | #elif defined(WOLFSSL_IMX6_CAAM)
|
---|
118 | #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
|
---|
119 | #elif defined(WOLFSSL_AFALG_HASH)
|
---|
120 | #include "wolfssl/wolfcrypt/port/af_alg/afalg_hash.h"
|
---|
121 | #else
|
---|
122 | /* wc_Sha256 digest */
|
---|
123 | typedef struct wc_Sha256 {
|
---|
124 | #ifdef FREESCALE_LTC_SHA
|
---|
125 | ltc_hash_ctx_t ctx;
|
---|
126 | #elif defined(STM32_HASH)
|
---|
127 | STM32_HASH_Context stmCtx;
|
---|
128 | #else
|
---|
129 | /* alignment on digest and buffer speeds up ARMv8 crypto operations */
|
---|
130 | ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
|
---|
131 | ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
|
---|
132 | word32 buffLen; /* in bytes */
|
---|
133 | word32 loLen; /* length in bytes */
|
---|
134 | word32 hiLen; /* length in bytes */
|
---|
135 | void* heap;
|
---|
136 | #ifdef USE_INTEL_SPEEDUP
|
---|
137 | const byte* data;
|
---|
138 | #endif
|
---|
139 | #ifdef WOLFSSL_PIC32MZ_HASH
|
---|
140 | hashUpdCache cache; /* cache for updates */
|
---|
141 | #endif
|
---|
142 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
143 | WC_ASYNC_DEV asyncDev;
|
---|
144 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
145 | #ifdef WOLFSSL_SMALL_STACK_CACHE
|
---|
146 | word32* W;
|
---|
147 | #endif
|
---|
148 | #ifdef WOLFSSL_DEVCRYPTO_HASH
|
---|
149 | WC_CRYPTODEV ctx;
|
---|
150 | byte* msg;
|
---|
151 | word32 used;
|
---|
152 | word32 len;
|
---|
153 | #endif
|
---|
154 | #endif
|
---|
155 | } wc_Sha256;
|
---|
156 |
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | #endif /* HAVE_FIPS */
|
---|
160 |
|
---|
161 | WOLFSSL_API int wc_InitSha256(wc_Sha256*);
|
---|
162 | WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int);
|
---|
163 | WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32);
|
---|
164 | WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*);
|
---|
165 | WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*);
|
---|
166 | WOLFSSL_API void wc_Sha256Free(wc_Sha256*);
|
---|
167 |
|
---|
168 | WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*);
|
---|
169 | WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
|
---|
170 |
|
---|
171 | #ifdef WOLFSSL_PIC32MZ_HASH
|
---|
172 | WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | #ifdef WOLFSSL_SHA224
|
---|
176 | /* avoid redefinition of structs */
|
---|
177 | #if !defined(HAVE_FIPS) || \
|
---|
178 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
|
---|
179 |
|
---|
180 | #ifndef NO_OLD_WC_NAMES
|
---|
181 | #define Sha224 wc_Sha224
|
---|
182 | #define SHA224 WC_SHA224
|
---|
183 | #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
|
---|
184 | #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
|
---|
185 | #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | /* in bytes */
|
---|
189 | enum {
|
---|
190 | WC_SHA224 = WC_HASH_TYPE_SHA224,
|
---|
191 | WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
|
---|
192 | WC_SHA224_DIGEST_SIZE = 28,
|
---|
193 | WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
|
---|
194 | };
|
---|
195 |
|
---|
196 |
|
---|
197 | typedef wc_Sha256 wc_Sha224;
|
---|
198 | #endif /* HAVE_FIPS */
|
---|
199 |
|
---|
200 | WOLFSSL_API int wc_InitSha224(wc_Sha224*);
|
---|
201 | WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int);
|
---|
202 | WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32);
|
---|
203 | WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*);
|
---|
204 | WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
|
---|
205 |
|
---|
206 | WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
|
---|
207 | WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
|
---|
208 |
|
---|
209 | #endif /* WOLFSSL_SHA224 */
|
---|
210 |
|
---|
211 | #ifdef __cplusplus
|
---|
212 | } /* extern "C" */
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | #endif /* NO_SHA256 */
|
---|
216 | #endif /* WOLF_CRYPT_SHA256_H */
|
---|
217 |
|
---|