/src/ghostpdl/base/sha2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * FILE: sha2.c |
3 | | * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ |
4 | | * |
5 | | * Copyright (c) 2000-2001, Aaron D. Gifford |
6 | | * All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * 1. Redistributions of source code must retain the above copyright |
12 | | * notice, this list of conditions and the following disclaimer. |
13 | | * 2. Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in the |
15 | | * documentation and/or other materials provided with the distribution. |
16 | | * 3. Neither the name of the copyright holder nor the names of contributors |
17 | | * may be used to endorse or promote products derived from this software |
18 | | * without specific prior written permission. |
19 | | * |
20 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND |
21 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE |
24 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | | * SUCH DAMAGE. |
31 | | * |
32 | | * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ |
33 | | */ |
34 | | |
35 | | #include "std.h" |
36 | | #include "string_.h" |
37 | | #include "sha2.h" |
38 | | |
39 | | /* |
40 | | * Disable asserts for now -- they're all just null pointer checks |
41 | | * anyway, and this way we don't require <assert.h>. |
42 | | */ |
43 | 148k | #define assert(x) (void)0 |
44 | | |
45 | | /* |
46 | | * UNROLLED TRANSFORM LOOP NOTE: |
47 | | * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform |
48 | | * loop version for the hash transform rounds (defined using macros |
49 | | * later in this file). Either define on the command line, for example: |
50 | | * |
51 | | * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c |
52 | | * |
53 | | * or define below: |
54 | | * |
55 | | * #define SHA2_UNROLL_TRANSFORM |
56 | | * |
57 | | */ |
58 | | |
59 | | /*** SHA-256/384/512 Machine Architecture Definitions *****************/ |
60 | | /* |
61 | | * BYTE_ORDER NOTE: |
62 | | * |
63 | | * Please make sure that your system defines BYTE_ORDER. If your |
64 | | * architecture is little-endian, make sure it also defines |
65 | | * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are |
66 | | * equivilent. |
67 | | * |
68 | | * If your system does not define the above, then you can do so by |
69 | | * hand like this: |
70 | | * |
71 | | * #define LITTLE_ENDIAN 1234 |
72 | | * #define BIG_ENDIAN 4321 |
73 | | * |
74 | | * And for little-endian machines, add: |
75 | | * |
76 | | * #define BYTE_ORDER LITTLE_ENDIAN |
77 | | * |
78 | | * Or for big-endian machines: |
79 | | * |
80 | | * #define BYTE_ORDER BIG_ENDIAN |
81 | | * |
82 | | * The FreeBSD machine this was written on defines BYTE_ORDER |
83 | | * appropriately by including <sys/types.h> (which in turn includes |
84 | | * <machine/endian.h> where the appropriate definitions are actually |
85 | | * made). |
86 | | */ |
87 | | |
88 | | /* |
89 | | * Use ghostscript's ARCH_IS_BIG_ENDIAN macro to define BYTE_ORDER |
90 | | * the way this code expects it. |
91 | | */ |
92 | | #undef BYTE_ORDER |
93 | | #undef LITTLE_ENDIAN |
94 | | #undef BIG_ENDIAN |
95 | | |
96 | | #define LITTLE_ENDIAN 1234 |
97 | | #define BIG_ENDIAN 4321 |
98 | | |
99 | | #if ARCH_IS_BIG_ENDIAN |
100 | | # define BYTE_ORDER BIG_ENDIAN |
101 | | #else |
102 | | # define BYTE_ORDER LITTLE_ENDIAN |
103 | | #endif |
104 | | |
105 | | #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) |
106 | | #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN |
107 | | #endif |
108 | | |
109 | | /* |
110 | | * Define the followingsha2_* types to types of the correct length on |
111 | | * the native archtecture. Most BSD systems and Linux define u_intXX_t |
112 | | * types. Machines with very recent ANSI C headers, can use the |
113 | | * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H |
114 | | * during compile or in the sha.h header file. |
115 | | * |
116 | | * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t |
117 | | * will need to define these three typedefs below (and the appropriate |
118 | | * ones in sha.h too) by hand according to their system architecture. |
119 | | * |
120 | | * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t |
121 | | * types and pointing out recent ANSI C support for uintXX_t in inttypes.h. |
122 | | */ |
123 | | #ifdef SHA2_USE_INTTYPES_H |
124 | | |
125 | | typedef uint8_t sha2_byte; /* Exactly 1 byte */ |
126 | | typedef uint32_t sha2_word32; /* Exactly 4 bytes */ |
127 | | typedef uint64_t sha2_word64; /* Exactly 8 bytes */ |
128 | | |
129 | | #else /* SHA2_USE_INTTYPES_H */ |
130 | | |
131 | | typedef u_int8_t sha2_byte; /* Exactly 1 byte */ |
132 | | typedef u_int32_t sha2_word32; /* Exactly 4 bytes */ |
133 | | typedef u_int64_t sha2_word64; /* Exactly 8 bytes */ |
134 | | |
135 | | #endif /* SHA2_USE_INTTYPES_H */ |
136 | | |
137 | | /* Microsoft/Borland/Intel have their own 64-bit siffix */ |
138 | | #if defined(_MSC_VER) || defined( __BORLANDC__) |
139 | | # define ULL(x) x##ui64 |
140 | | #else |
141 | 100M | # define ULL(x) x##ULL |
142 | | #endif |
143 | | |
144 | | /*** SHA-256/384/512 Various Length Definitions ***********************/ |
145 | | /* NOTE: Most of these are in sha2.h */ |
146 | 27.6k | #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) |
147 | | #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) |
148 | 94.4k | #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) |
149 | | |
150 | | /*** ENDIAN REVERSAL MACROS *******************************************/ |
151 | | #if BYTE_ORDER == LITTLE_ENDIAN |
152 | 26.4M | #define REVERSE32(w,x) { \ |
153 | 26.4M | sha2_word32 tmp = (w); \ |
154 | 26.4M | tmp = (tmp >> 16) | (tmp << 16); \ |
155 | 26.4M | (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ |
156 | 26.4M | } |
157 | 25.2M | #define REVERSE64(w,x) { \ |
158 | 25.2M | sha2_word64 tmp = (w); \ |
159 | 25.2M | tmp = (tmp >> 32) | (tmp << 32); \ |
160 | 25.2M | tmp = ((tmp & ULL(0xff00ff00ff00ff00)) >> 8) | \ |
161 | 25.2M | ((tmp & ULL(0x00ff00ff00ff00ff)) << 8); \ |
162 | 25.2M | (x) = ((tmp & ULL(0xffff0000ffff0000)) >> 16) | \ |
163 | 25.2M | ((tmp & ULL(0x0000ffff0000ffff)) << 16); \ |
164 | 25.2M | } |
165 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
166 | | |
167 | | /* |
168 | | * Macro for incrementally adding the unsigned 64-bit integer n to the |
169 | | * unsigned 128-bit integer (represented using a two-element array of |
170 | | * 64-bit words): |
171 | | */ |
172 | 1.50M | #define ADDINC128(w,n) { \ |
173 | 1.50M | (w)[0] += (sha2_word64)(n); \ |
174 | 1.50M | if ((w)[0] < (n)) { \ |
175 | 0 | (w)[1]++; \ |
176 | 0 | } \ |
177 | 1.50M | } |
178 | | |
179 | | /* |
180 | | * Macros for copying blocks of memory and for zeroing out ranges |
181 | | * of memory. Using these macros makes it easy to switch from |
182 | | * using memset()/memcpy() and using bzero()/bcopy(). |
183 | | * |
184 | | * Please define either SHA2_USE_MEMSET_MEMCPY or define |
185 | | * SHA2_USE_BZERO_BCOPY depending on which function set you |
186 | | * choose to use: |
187 | | */ |
188 | | #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY) |
189 | | /* Default to memset()/memcpy() if no option is specified */ |
190 | | #define SHA2_USE_MEMSET_MEMCPY 1 |
191 | | #endif |
192 | | #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY) |
193 | | /* Abort with an error if BOTH options are defined */ |
194 | | #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both! |
195 | | #endif |
196 | | |
197 | | #ifdef SHA2_USE_MEMSET_MEMCPY |
198 | 221k | #define MEMSET_BZERO(p,l) memset((p), 0, (l)) |
199 | 75.2k | #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l)) |
200 | | #endif |
201 | | #ifdef SHA2_USE_BZERO_BCOPY |
202 | | #define MEMSET_BZERO(p,l) bzero((p), (l)) |
203 | | #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l)) |
204 | | #endif |
205 | | |
206 | | /*** THE SIX LOGICAL FUNCTIONS ****************************************/ |
207 | | /* |
208 | | * Bit shifting and rotation (used by the six SHA-XYZ logical functions: |
209 | | * |
210 | | * NOTE: The naming of R and S appears backwards here (R is a SHIFT and |
211 | | * S is a ROTATION) because the SHA-256/384/512 description document |
212 | | * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this |
213 | | * same "backwards" definition. |
214 | | */ |
215 | | /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ |
216 | 355M | #define R(b,x) ((x) >> (b)) |
217 | | /* 32-bit Rotate-right (used in SHA-256): */ |
218 | 945M | #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) |
219 | | /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ |
220 | 1.14G | #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) |
221 | | |
222 | | /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ |
223 | 228M | #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) |
224 | 228M | #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
225 | | |
226 | | /* Four of six logical functions used in SHA-256: */ |
227 | 105M | #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) |
228 | 105M | #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) |
229 | 78.7M | #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) |
230 | 78.7M | #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) |
231 | | |
232 | | /* Four of six logical functions used in SHA-384 and SHA-512: */ |
233 | 123M | #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) |
234 | 123M | #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) |
235 | 99.1M | #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) |
236 | 99.1M | #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) |
237 | | |
238 | | /*** INTERNAL FUNCTION PROTOTYPES *************************************/ |
239 | | /* NOTE: These should not be accessed directly from outside this |
240 | | * library -- they are intended for private internal visibility/use |
241 | | * only. |
242 | | */ |
243 | | void pSHA512_Last(SHA512_CTX*); |
244 | | void pSHA256_Transform(SHA256_CTX*, const sha2_word32*); |
245 | | void pSHA512_Transform(SHA512_CTX*, const sha2_word64*); |
246 | | |
247 | | /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ |
248 | | /* Hash constant words K for SHA-256: */ |
249 | | const static sha2_word32 K256[64] = { |
250 | | 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, |
251 | | 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, |
252 | | 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, |
253 | | 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, |
254 | | 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, |
255 | | 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, |
256 | | 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, |
257 | | 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, |
258 | | 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, |
259 | | 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, |
260 | | 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, |
261 | | 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, |
262 | | 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, |
263 | | 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, |
264 | | 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, |
265 | | 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL |
266 | | }; |
267 | | |
268 | | /* Initial hash value H for SHA-256: */ |
269 | | const static sha2_word32 sha256_initial_hash_value[8] = { |
270 | | 0x6a09e667UL, |
271 | | 0xbb67ae85UL, |
272 | | 0x3c6ef372UL, |
273 | | 0xa54ff53aUL, |
274 | | 0x510e527fUL, |
275 | | 0x9b05688cUL, |
276 | | 0x1f83d9abUL, |
277 | | 0x5be0cd19UL |
278 | | }; |
279 | | |
280 | | /* Hash constant words K for SHA-384 and SHA-512: */ |
281 | | const static sha2_word64 K512[80] = { |
282 | | ULL(0x428a2f98d728ae22), ULL(0x7137449123ef65cd), |
283 | | ULL(0xb5c0fbcfec4d3b2f), ULL(0xe9b5dba58189dbbc), |
284 | | ULL(0x3956c25bf348b538), ULL(0x59f111f1b605d019), |
285 | | ULL(0x923f82a4af194f9b), ULL(0xab1c5ed5da6d8118), |
286 | | ULL(0xd807aa98a3030242), ULL(0x12835b0145706fbe), |
287 | | ULL(0x243185be4ee4b28c), ULL(0x550c7dc3d5ffb4e2), |
288 | | ULL(0x72be5d74f27b896f), ULL(0x80deb1fe3b1696b1), |
289 | | ULL(0x9bdc06a725c71235), ULL(0xc19bf174cf692694), |
290 | | ULL(0xe49b69c19ef14ad2), ULL(0xefbe4786384f25e3), |
291 | | ULL(0x0fc19dc68b8cd5b5), ULL(0x240ca1cc77ac9c65), |
292 | | ULL(0x2de92c6f592b0275), ULL(0x4a7484aa6ea6e483), |
293 | | ULL(0x5cb0a9dcbd41fbd4), ULL(0x76f988da831153b5), |
294 | | ULL(0x983e5152ee66dfab), ULL(0xa831c66d2db43210), |
295 | | ULL(0xb00327c898fb213f), ULL(0xbf597fc7beef0ee4), |
296 | | ULL(0xc6e00bf33da88fc2), ULL(0xd5a79147930aa725), |
297 | | ULL(0x06ca6351e003826f), ULL(0x142929670a0e6e70), |
298 | | ULL(0x27b70a8546d22ffc), ULL(0x2e1b21385c26c926), |
299 | | ULL(0x4d2c6dfc5ac42aed), ULL(0x53380d139d95b3df), |
300 | | ULL(0x650a73548baf63de), ULL(0x766a0abb3c77b2a8), |
301 | | ULL(0x81c2c92e47edaee6), ULL(0x92722c851482353b), |
302 | | ULL(0xa2bfe8a14cf10364), ULL(0xa81a664bbc423001), |
303 | | ULL(0xc24b8b70d0f89791), ULL(0xc76c51a30654be30), |
304 | | ULL(0xd192e819d6ef5218), ULL(0xd69906245565a910), |
305 | | ULL(0xf40e35855771202a), ULL(0x106aa07032bbd1b8), |
306 | | ULL(0x19a4c116b8d2d0c8), ULL(0x1e376c085141ab53), |
307 | | ULL(0x2748774cdf8eeb99), ULL(0x34b0bcb5e19b48a8), |
308 | | ULL(0x391c0cb3c5c95a63), ULL(0x4ed8aa4ae3418acb), |
309 | | ULL(0x5b9cca4f7763e373), ULL(0x682e6ff3d6b2b8a3), |
310 | | ULL(0x748f82ee5defb2fc), ULL(0x78a5636f43172f60), |
311 | | ULL(0x84c87814a1f0ab72), ULL(0x8cc702081a6439ec), |
312 | | ULL(0x90befffa23631e28), ULL(0xa4506cebde82bde9), |
313 | | ULL(0xbef9a3f7b2c67915), ULL(0xc67178f2e372532b), |
314 | | ULL(0xca273eceea26619c), ULL(0xd186b8c721c0c207), |
315 | | ULL(0xeada7dd6cde0eb1e), ULL(0xf57d4f7fee6ed178), |
316 | | ULL(0x06f067aa72176fba), ULL(0x0a637dc5a2c898a6), |
317 | | ULL(0x113f9804bef90dae), ULL(0x1b710b35131c471b), |
318 | | ULL(0x28db77f523047d84), ULL(0x32caab7b40c72493), |
319 | | ULL(0x3c9ebe0a15c9bebc), ULL(0x431d67c49c100d4c), |
320 | | ULL(0x4cc5d4becb3e42b6), ULL(0x597f299cfc657e2a), |
321 | | ULL(0x5fcb6fab3ad6faec), ULL(0x6c44198c4a475817) |
322 | | }; |
323 | | |
324 | | /* Initial hash value H for SHA-384 */ |
325 | | const static sha2_word64 sha384_initial_hash_value[8] = { |
326 | | ULL(0xcbbb9d5dc1059ed8), |
327 | | ULL(0x629a292a367cd507), |
328 | | ULL(0x9159015a3070dd17), |
329 | | ULL(0x152fecd8f70e5939), |
330 | | ULL(0x67332667ffc00b31), |
331 | | ULL(0x8eb44a8768581511), |
332 | | ULL(0xdb0c2e0d64f98fa7), |
333 | | ULL(0x47b5481dbefa4fa4) |
334 | | }; |
335 | | |
336 | | /* Initial hash value H for SHA-512 */ |
337 | | const static sha2_word64 sha512_initial_hash_value[8] = { |
338 | | ULL(0x6a09e667f3bcc908), |
339 | | ULL(0xbb67ae8584caa73b), |
340 | | ULL(0x3c6ef372fe94f82b), |
341 | | ULL(0xa54ff53a5f1d36f1), |
342 | | ULL(0x510e527fade682d1), |
343 | | ULL(0x9b05688c2b3e6c1f), |
344 | | ULL(0x1f83d9abfb41bd6b), |
345 | | ULL(0x5be0cd19137e2179) |
346 | | }; |
347 | | |
348 | | /* |
349 | | * Constant used by SHA256/384/512_End() functions for converting the |
350 | | * digest to a readable hexadecimal character string: |
351 | | */ |
352 | | static const char *sha2_hex_digits = "0123456789abcdef"; |
353 | | |
354 | | /*** SHA-256: *********************************************************/ |
355 | 26.5k | void pSHA256_Init(SHA256_CTX* context) { |
356 | 26.5k | if (context == (SHA256_CTX*)0) { |
357 | 0 | return; |
358 | 0 | } |
359 | 26.5k | MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH); |
360 | 26.5k | MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH); |
361 | 26.5k | context->bitcount = 0; |
362 | 26.5k | } |
363 | | |
364 | | #ifdef SHA2_UNROLL_TRANSFORM |
365 | | |
366 | | /* Unrolled SHA-256 round macros: */ |
367 | | |
368 | | #if BYTE_ORDER == LITTLE_ENDIAN |
369 | | |
370 | | #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ |
371 | | REVERSE32(*data++, W256[j]); \ |
372 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \ |
373 | | K256[j] + W256[j]; \ |
374 | | (d) += T1; \ |
375 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
376 | | j++ |
377 | | |
378 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
379 | | |
380 | | #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ |
381 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \ |
382 | | K256[j] + (W256[j] = *data++); \ |
383 | | (d) += T1; \ |
384 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
385 | | j++ |
386 | | |
387 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
388 | | |
389 | | #define ROUND256(a,b,c,d,e,f,g,h) \ |
390 | | s0 = W256[(j+1)&0x0f]; \ |
391 | | s0 = sigma0_256(s0); \ |
392 | | s1 = W256[(j+14)&0x0f]; \ |
393 | | s1 = sigma1_256(s1); \ |
394 | | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \ |
395 | | (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ |
396 | | (d) += T1; \ |
397 | | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ |
398 | | j++ |
399 | | |
400 | | void pSHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { |
401 | | sha2_word32 a, b, c, d, e, f, g, h, s0, s1; |
402 | | sha2_word32 T1, *W256; |
403 | | int j; |
404 | | |
405 | | W256 = (sha2_word32*)context->buffer; |
406 | | |
407 | | /* Initialize registers with the prev. intermediate value */ |
408 | | a = context->state[0]; |
409 | | b = context->state[1]; |
410 | | c = context->state[2]; |
411 | | d = context->state[3]; |
412 | | e = context->state[4]; |
413 | | f = context->state[5]; |
414 | | g = context->state[6]; |
415 | | h = context->state[7]; |
416 | | |
417 | | j = 0; |
418 | | do { |
419 | | /* Rounds 0 to 15 (unrolled): */ |
420 | | ROUND256_0_TO_15(a,b,c,d,e,f,g,h); |
421 | | ROUND256_0_TO_15(h,a,b,c,d,e,f,g); |
422 | | ROUND256_0_TO_15(g,h,a,b,c,d,e,f); |
423 | | ROUND256_0_TO_15(f,g,h,a,b,c,d,e); |
424 | | ROUND256_0_TO_15(e,f,g,h,a,b,c,d); |
425 | | ROUND256_0_TO_15(d,e,f,g,h,a,b,c); |
426 | | ROUND256_0_TO_15(c,d,e,f,g,h,a,b); |
427 | | ROUND256_0_TO_15(b,c,d,e,f,g,h,a); |
428 | | } while (j < 16); |
429 | | |
430 | | /* Now for the remaining rounds to 64: */ |
431 | | do { |
432 | | ROUND256(a,b,c,d,e,f,g,h); |
433 | | ROUND256(h,a,b,c,d,e,f,g); |
434 | | ROUND256(g,h,a,b,c,d,e,f); |
435 | | ROUND256(f,g,h,a,b,c,d,e); |
436 | | ROUND256(e,f,g,h,a,b,c,d); |
437 | | ROUND256(d,e,f,g,h,a,b,c); |
438 | | ROUND256(c,d,e,f,g,h,a,b); |
439 | | ROUND256(b,c,d,e,f,g,h,a); |
440 | | } while (j < 64); |
441 | | |
442 | | /* Compute the current intermediate hash value */ |
443 | | context->state[0] += a; |
444 | | context->state[1] += b; |
445 | | context->state[2] += c; |
446 | | context->state[3] += d; |
447 | | context->state[4] += e; |
448 | | context->state[5] += f; |
449 | | context->state[6] += g; |
450 | | context->state[7] += h; |
451 | | |
452 | | /* Clean up */ |
453 | | a = b = c = d = e = f = g = h = T1 = 0; |
454 | | } |
455 | | |
456 | | #else /* SHA2_UNROLL_TRANSFORM */ |
457 | | |
458 | 1.64M | void pSHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { |
459 | 1.64M | sha2_word32 a, b, c, d, e, f, g, h, s0, s1; |
460 | 1.64M | sha2_word32 T1, T2, *W256; |
461 | 1.64M | int j; |
462 | | |
463 | 1.64M | W256 = (sha2_word32*)context->buffer; |
464 | | |
465 | | /* Initialize registers with the prev. intermediate value */ |
466 | 1.64M | a = context->state[0]; |
467 | 1.64M | b = context->state[1]; |
468 | 1.64M | c = context->state[2]; |
469 | 1.64M | d = context->state[3]; |
470 | 1.64M | e = context->state[4]; |
471 | 1.64M | f = context->state[5]; |
472 | 1.64M | g = context->state[6]; |
473 | 1.64M | h = context->state[7]; |
474 | | |
475 | 1.64M | j = 0; |
476 | 26.2M | do { |
477 | 26.2M | #if BYTE_ORDER == LITTLE_ENDIAN |
478 | | /* Copy data while converting to host byte order */ |
479 | 26.2M | REVERSE32(*data++,W256[j]); |
480 | | /* Apply the SHA-256 compression function to update a..h */ |
481 | 26.2M | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; |
482 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
483 | | /* Apply the SHA-256 compression function to update a..h with copy */ |
484 | | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++); |
485 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
486 | 26.2M | T2 = Sigma0_256(a) + Maj(a, b, c); |
487 | 26.2M | h = g; |
488 | 26.2M | g = f; |
489 | 26.2M | f = e; |
490 | 26.2M | e = d + T1; |
491 | 26.2M | d = c; |
492 | 26.2M | c = b; |
493 | 26.2M | b = a; |
494 | 26.2M | a = T1 + T2; |
495 | | |
496 | 26.2M | j++; |
497 | 26.2M | } while (j < 16); |
498 | | |
499 | 78.7M | do { |
500 | | /* Part of the message block expansion: */ |
501 | 78.7M | s0 = W256[(j+1)&0x0f]; |
502 | 78.7M | s0 = sigma0_256(s0); |
503 | 78.7M | s1 = W256[(j+14)&0x0f]; |
504 | 78.7M | s1 = sigma1_256(s1); |
505 | | |
506 | | /* Apply the SHA-256 compression function to update a..h */ |
507 | 78.7M | T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + |
508 | 78.7M | (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); |
509 | 78.7M | T2 = Sigma0_256(a) + Maj(a, b, c); |
510 | 78.7M | h = g; |
511 | 78.7M | g = f; |
512 | 78.7M | f = e; |
513 | 78.7M | e = d + T1; |
514 | 78.7M | d = c; |
515 | 78.7M | c = b; |
516 | 78.7M | b = a; |
517 | 78.7M | a = T1 + T2; |
518 | | |
519 | 78.7M | j++; |
520 | 78.7M | } while (j < 64); |
521 | | |
522 | | /* Compute the current intermediate hash value */ |
523 | 1.64M | context->state[0] += a; |
524 | 1.64M | context->state[1] += b; |
525 | 1.64M | context->state[2] += c; |
526 | 1.64M | context->state[3] += d; |
527 | 1.64M | context->state[4] += e; |
528 | 1.64M | context->state[5] += f; |
529 | 1.64M | context->state[6] += g; |
530 | 1.64M | context->state[7] += h; |
531 | | |
532 | | /* Clean up */ |
533 | 1.64M | a = b = c = d = e = f = g = h = T1 = T2 = 0; |
534 | 1.64M | } |
535 | | |
536 | | #endif /* SHA2_UNROLL_TRANSFORM */ |
537 | | |
538 | 28.0k | void pSHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) { |
539 | 28.0k | unsigned int freespace, usedspace; |
540 | | |
541 | 28.0k | if (len == 0) { |
542 | | /* Calling with no data is valid - we do nothing */ |
543 | 1.07k | return; |
544 | 1.07k | } |
545 | | |
546 | | /* Sanity check: */ |
547 | 26.9k | assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0); |
548 | | |
549 | 26.9k | usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
550 | 26.9k | if (usedspace > 0) { |
551 | | /* Calculate how much free space is available in the buffer */ |
552 | 358 | freespace = SHA256_BLOCK_LENGTH - usedspace; |
553 | | |
554 | 358 | if (len >= freespace) { |
555 | | /* Fill the buffer completely and process it */ |
556 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); |
557 | 0 | context->bitcount += freespace << 3; |
558 | 0 | len -= freespace; |
559 | 0 | data += freespace; |
560 | 0 | pSHA256_Transform(context, (sha2_word32*)context->buffer); |
561 | 358 | } else { |
562 | | /* The buffer is not yet full */ |
563 | 358 | MEMCPY_BCOPY(&context->buffer[usedspace], data, len); |
564 | 358 | context->bitcount += len << 3; |
565 | | /* Clean up: */ |
566 | 358 | usedspace = freespace = 0; |
567 | 358 | return; |
568 | 358 | } |
569 | 358 | } |
570 | 1.64M | while (len >= SHA256_BLOCK_LENGTH) { |
571 | | /* Process as many complete blocks as we can */ |
572 | 1.61M | pSHA256_Transform(context, (sha2_word32*)data); |
573 | 1.61M | context->bitcount += SHA256_BLOCK_LENGTH << 3; |
574 | 1.61M | len -= SHA256_BLOCK_LENGTH; |
575 | 1.61M | data += SHA256_BLOCK_LENGTH; |
576 | 1.61M | } |
577 | 26.5k | if (len > 0) { |
578 | | /* There's left-overs, so save 'em */ |
579 | 1.07k | MEMCPY_BCOPY(context->buffer, data, len); |
580 | 1.07k | context->bitcount += len << 3; |
581 | 1.07k | } |
582 | | /* Clean up: */ |
583 | 26.5k | usedspace = freespace = 0; |
584 | 26.5k | } |
585 | | |
586 | 26.5k | void pSHA256_Final(sha2_byte digest[SHA256_DIGEST_LENGTH], SHA256_CTX* context) { |
587 | 26.5k | sha2_word32 *d = (sha2_word32*)digest; |
588 | 26.5k | unsigned int usedspace; |
589 | | |
590 | | /* Sanity check: */ |
591 | 26.5k | assert(context != (SHA256_CTX*)0); |
592 | | |
593 | | /* If no digest buffer is passed, we don't bother doing this: */ |
594 | 26.5k | if (digest != (sha2_byte*)0) { |
595 | 26.5k | usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
596 | 26.5k | #if BYTE_ORDER == LITTLE_ENDIAN |
597 | | /* Convert FROM host byte order */ |
598 | 26.5k | REVERSE64(context->bitcount,context->bitcount); |
599 | 26.5k | #endif |
600 | 26.5k | if (usedspace > 0) { |
601 | | /* Begin padding with a 1 bit: */ |
602 | 1.07k | context->buffer[usedspace++] = 0x80; |
603 | | |
604 | 1.07k | if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) { |
605 | | /* Set-up for the last transform: */ |
606 | 712 | MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace); |
607 | 712 | } else { |
608 | 358 | if (usedspace < SHA256_BLOCK_LENGTH) { |
609 | 358 | MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace); |
610 | 358 | } |
611 | | /* Do second-to-last transform: */ |
612 | 358 | pSHA256_Transform(context, (sha2_word32*)context->buffer); |
613 | | |
614 | | /* And set-up for the last transform: */ |
615 | 358 | MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); |
616 | 358 | } |
617 | 25.5k | } else { |
618 | | /* Set-up for the last transform: */ |
619 | 25.5k | MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); |
620 | | |
621 | | /* Begin padding with a 1 bit: */ |
622 | 25.5k | *context->buffer = 0x80; |
623 | 25.5k | } |
624 | | /* Set the bit count: */ |
625 | 26.5k | *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount; |
626 | | |
627 | | /* Final transform: */ |
628 | 26.5k | pSHA256_Transform(context, (sha2_word32*)context->buffer); |
629 | | |
630 | 26.5k | #if BYTE_ORDER == LITTLE_ENDIAN |
631 | 26.5k | { |
632 | | /* Convert TO host byte order */ |
633 | 26.5k | int j; |
634 | 239k | for (j = 0; j < 8; j++) { |
635 | 212k | REVERSE32(context->state[j],context->state[j]); |
636 | 212k | *d++ = context->state[j]; |
637 | 212k | } |
638 | 26.5k | } |
639 | | #else |
640 | | MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH); |
641 | | #endif |
642 | 26.5k | } |
643 | | |
644 | | /* Clean up state data: */ |
645 | 26.5k | MEMSET_BZERO(context, sizeof(*context)); |
646 | 26.5k | usedspace = 0; |
647 | 26.5k | } |
648 | | |
649 | 0 | char *pSHA256_End(SHA256_CTX* context, char buffer[SHA256_DIGEST_STRING_LENGTH]) { |
650 | 0 | sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest; |
651 | 0 | int i; |
652 | | |
653 | | /* Sanity check: */ |
654 | 0 | assert(context != (SHA256_CTX*)0); |
655 | |
|
656 | 0 | if (buffer != (char*)0) { |
657 | 0 | pSHA256_Final(digest, context); |
658 | |
|
659 | 0 | for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { |
660 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
661 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
662 | 0 | d++; |
663 | 0 | } |
664 | 0 | *buffer = (char)0; |
665 | 0 | } else { |
666 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
667 | 0 | } |
668 | 0 | MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH); |
669 | 0 | return buffer; |
670 | 0 | } |
671 | | |
672 | 0 | char* pSHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) { |
673 | 0 | SHA256_CTX context; |
674 | |
|
675 | 0 | pSHA256_Init(&context); |
676 | 0 | pSHA256_Update(&context, data, len); |
677 | 0 | return pSHA256_End(&context, digest); |
678 | 0 | } |
679 | | |
680 | | /*** SHA-512: *********************************************************/ |
681 | 24.4k | void pSHA512_Init(SHA512_CTX* context) { |
682 | 24.4k | if (context == (SHA512_CTX*)0) { |
683 | 0 | return; |
684 | 0 | } |
685 | 24.4k | MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); |
686 | 24.4k | MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH); |
687 | 24.4k | context->bitcount[0] = context->bitcount[1] = 0; |
688 | 24.4k | } |
689 | | |
690 | | #ifdef SHA2_UNROLL_TRANSFORM |
691 | | |
692 | | /* Unrolled SHA-512 round macros: */ |
693 | | #if BYTE_ORDER == LITTLE_ENDIAN |
694 | | |
695 | | #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ |
696 | | REVERSE64(*data++, W512[j]); \ |
697 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \ |
698 | | K512[j] + W512[j]; \ |
699 | | (d) += T1, \ |
700 | | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \ |
701 | | j++ |
702 | | |
703 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
704 | | |
705 | | #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ |
706 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \ |
707 | | K512[j] + (W512[j] = *data++); \ |
708 | | (d) += T1; \ |
709 | | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ |
710 | | j++ |
711 | | |
712 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
713 | | |
714 | | #define ROUND512(a,b,c,d,e,f,g,h) \ |
715 | | s0 = W512[(j+1)&0x0f]; \ |
716 | | s0 = sigma0_512(s0); \ |
717 | | s1 = W512[(j+14)&0x0f]; \ |
718 | | s1 = sigma1_512(s1); \ |
719 | | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \ |
720 | | (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \ |
721 | | (d) += T1; \ |
722 | | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ |
723 | | j++ |
724 | | |
725 | | void pSHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { |
726 | | sha2_word64 a, b, c, d, e, f, g, h, s0, s1; |
727 | | sha2_word64 T1, *W512 = (sha2_word64*)context->buffer; |
728 | | int j; |
729 | | |
730 | | /* Initialize registers with the prev. intermediate value */ |
731 | | a = context->state[0]; |
732 | | b = context->state[1]; |
733 | | c = context->state[2]; |
734 | | d = context->state[3]; |
735 | | e = context->state[4]; |
736 | | f = context->state[5]; |
737 | | g = context->state[6]; |
738 | | h = context->state[7]; |
739 | | |
740 | | j = 0; |
741 | | do { |
742 | | ROUND512_0_TO_15(a,b,c,d,e,f,g,h); |
743 | | ROUND512_0_TO_15(h,a,b,c,d,e,f,g); |
744 | | ROUND512_0_TO_15(g,h,a,b,c,d,e,f); |
745 | | ROUND512_0_TO_15(f,g,h,a,b,c,d,e); |
746 | | ROUND512_0_TO_15(e,f,g,h,a,b,c,d); |
747 | | ROUND512_0_TO_15(d,e,f,g,h,a,b,c); |
748 | | ROUND512_0_TO_15(c,d,e,f,g,h,a,b); |
749 | | ROUND512_0_TO_15(b,c,d,e,f,g,h,a); |
750 | | } while (j < 16); |
751 | | |
752 | | /* Now for the remaining rounds up to 79: */ |
753 | | do { |
754 | | ROUND512(a,b,c,d,e,f,g,h); |
755 | | ROUND512(h,a,b,c,d,e,f,g); |
756 | | ROUND512(g,h,a,b,c,d,e,f); |
757 | | ROUND512(f,g,h,a,b,c,d,e); |
758 | | ROUND512(e,f,g,h,a,b,c,d); |
759 | | ROUND512(d,e,f,g,h,a,b,c); |
760 | | ROUND512(c,d,e,f,g,h,a,b); |
761 | | ROUND512(b,c,d,e,f,g,h,a); |
762 | | } while (j < 80); |
763 | | |
764 | | /* Compute the current intermediate hash value */ |
765 | | context->state[0] += a; |
766 | | context->state[1] += b; |
767 | | context->state[2] += c; |
768 | | context->state[3] += d; |
769 | | context->state[4] += e; |
770 | | context->state[5] += f; |
771 | | context->state[6] += g; |
772 | | context->state[7] += h; |
773 | | |
774 | | /* Clean up */ |
775 | | a = b = c = d = e = f = g = h = T1 = 0; |
776 | | } |
777 | | |
778 | | #else /* SHA2_UNROLL_TRANSFORM */ |
779 | | |
780 | 1.54M | void pSHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { |
781 | 1.54M | sha2_word64 a, b, c, d, e, f, g, h, s0, s1; |
782 | 1.54M | sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer; |
783 | 1.54M | int j; |
784 | | |
785 | | /* Initialize registers with the prev. intermediate value */ |
786 | 1.54M | a = context->state[0]; |
787 | 1.54M | b = context->state[1]; |
788 | 1.54M | c = context->state[2]; |
789 | 1.54M | d = context->state[3]; |
790 | 1.54M | e = context->state[4]; |
791 | 1.54M | f = context->state[5]; |
792 | 1.54M | g = context->state[6]; |
793 | 1.54M | h = context->state[7]; |
794 | | |
795 | 1.54M | j = 0; |
796 | 24.7M | do { |
797 | 24.7M | #if BYTE_ORDER == LITTLE_ENDIAN |
798 | | /* Convert TO host byte order */ |
799 | 24.7M | REVERSE64(*data++, W512[j]); |
800 | | /* Apply the SHA-512 compression function to update a..h */ |
801 | 24.7M | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; |
802 | | #else /* BYTE_ORDER == LITTLE_ENDIAN */ |
803 | | /* Apply the SHA-512 compression function to update a..h with copy */ |
804 | | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++); |
805 | | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |
806 | 24.7M | T2 = Sigma0_512(a) + Maj(a, b, c); |
807 | 24.7M | h = g; |
808 | 24.7M | g = f; |
809 | 24.7M | f = e; |
810 | 24.7M | e = d + T1; |
811 | 24.7M | d = c; |
812 | 24.7M | c = b; |
813 | 24.7M | b = a; |
814 | 24.7M | a = T1 + T2; |
815 | | |
816 | 24.7M | j++; |
817 | 24.7M | } while (j < 16); |
818 | | |
819 | 99.1M | do { |
820 | | /* Part of the message block expansion: */ |
821 | 99.1M | s0 = W512[(j+1)&0x0f]; |
822 | 99.1M | s0 = sigma0_512(s0); |
823 | 99.1M | s1 = W512[(j+14)&0x0f]; |
824 | 99.1M | s1 = sigma1_512(s1); |
825 | | |
826 | | /* Apply the SHA-512 compression function to update a..h */ |
827 | 99.1M | T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + |
828 | 99.1M | (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); |
829 | 99.1M | T2 = Sigma0_512(a) + Maj(a, b, c); |
830 | 99.1M | h = g; |
831 | 99.1M | g = f; |
832 | 99.1M | f = e; |
833 | 99.1M | e = d + T1; |
834 | 99.1M | d = c; |
835 | 99.1M | c = b; |
836 | 99.1M | b = a; |
837 | 99.1M | a = T1 + T2; |
838 | | |
839 | 99.1M | j++; |
840 | 99.1M | } while (j < 80); |
841 | | |
842 | | /* Compute the current intermediate hash value */ |
843 | 1.54M | context->state[0] += a; |
844 | 1.54M | context->state[1] += b; |
845 | 1.54M | context->state[2] += c; |
846 | 1.54M | context->state[3] += d; |
847 | 1.54M | context->state[4] += e; |
848 | 1.54M | context->state[5] += f; |
849 | 1.54M | context->state[6] += g; |
850 | 1.54M | context->state[7] += h; |
851 | | |
852 | | /* Clean up */ |
853 | 1.54M | a = b = c = d = e = f = g = h = T1 = T2 = 0; |
854 | 1.54M | } |
855 | | |
856 | | #endif /* SHA2_UNROLL_TRANSFORM */ |
857 | | |
858 | 47.2k | void pSHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) { |
859 | 47.2k | unsigned int freespace, usedspace; |
860 | | |
861 | 47.2k | if (len == 0) { |
862 | | /* Calling with no data is valid - we do nothing */ |
863 | 0 | return; |
864 | 0 | } |
865 | | |
866 | | /* Sanity check: */ |
867 | 47.2k | assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0); |
868 | | |
869 | 47.2k | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
870 | 47.2k | if (usedspace > 0) { |
871 | | /* Calculate how much free space is available in the buffer */ |
872 | 0 | freespace = SHA512_BLOCK_LENGTH - usedspace; |
873 | |
|
874 | 0 | if (len >= freespace) { |
875 | | /* Fill the buffer completely and process it */ |
876 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); |
877 | 0 | ADDINC128(context->bitcount, freespace << 3); |
878 | 0 | len -= freespace; |
879 | 0 | data += freespace; |
880 | 0 | pSHA512_Transform(context, (sha2_word64*)context->buffer); |
881 | 0 | } else { |
882 | | /* The buffer is not yet full */ |
883 | 0 | MEMCPY_BCOPY(&context->buffer[usedspace], data, len); |
884 | 0 | ADDINC128(context->bitcount, len << 3); |
885 | | /* Clean up: */ |
886 | 0 | usedspace = freespace = 0; |
887 | 0 | return; |
888 | 0 | } |
889 | 0 | } |
890 | 1.54M | while (len >= SHA512_BLOCK_LENGTH) { |
891 | | /* Process as many complete blocks as we can */ |
892 | 1.50M | pSHA512_Transform(context, (sha2_word64*)data); |
893 | 1.50M | ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); |
894 | 1.50M | len -= SHA512_BLOCK_LENGTH; |
895 | 1.50M | data += SHA512_BLOCK_LENGTH; |
896 | 1.50M | } |
897 | 47.2k | if (len > 0) { |
898 | | /* There's left-overs, so save 'em */ |
899 | 0 | MEMCPY_BCOPY(context->buffer, data, len); |
900 | 0 | ADDINC128(context->bitcount, len << 3); |
901 | 0 | } |
902 | | /* Clean up: */ |
903 | 47.2k | usedspace = freespace = 0; |
904 | 47.2k | } |
905 | | |
906 | 47.2k | void pSHA512_Last(SHA512_CTX* context) { |
907 | 47.2k | unsigned int usedspace; |
908 | | |
909 | 47.2k | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
910 | 47.2k | #if BYTE_ORDER == LITTLE_ENDIAN |
911 | | /* Convert FROM host byte order */ |
912 | 47.2k | REVERSE64(context->bitcount[0],context->bitcount[0]); |
913 | 47.2k | REVERSE64(context->bitcount[1],context->bitcount[1]); |
914 | 47.2k | #endif |
915 | 47.2k | if (usedspace > 0) { |
916 | | /* Begin padding with a 1 bit: */ |
917 | 0 | context->buffer[usedspace++] = 0x80; |
918 | |
|
919 | 0 | if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { |
920 | | /* Set-up for the last transform: */ |
921 | 0 | MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace); |
922 | 0 | } else { |
923 | 0 | if (usedspace < SHA512_BLOCK_LENGTH) { |
924 | 0 | MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace); |
925 | 0 | } |
926 | | /* Do second-to-last transform: */ |
927 | 0 | pSHA512_Transform(context, (sha2_word64*)context->buffer); |
928 | | |
929 | | /* And set-up for the last transform: */ |
930 | 0 | MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2); |
931 | 0 | } |
932 | 47.2k | } else { |
933 | | /* Prepare for final transform: */ |
934 | 47.2k | MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH); |
935 | | |
936 | | /* Begin padding with a 1 bit: */ |
937 | 47.2k | *context->buffer = 0x80; |
938 | 47.2k | } |
939 | | /* Store the length of input data (in bits): */ |
940 | 47.2k | *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1]; |
941 | 47.2k | *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0]; |
942 | | |
943 | | /* Final transform: */ |
944 | 47.2k | pSHA512_Transform(context, (sha2_word64*)context->buffer); |
945 | 47.2k | } |
946 | | |
947 | 24.4k | void pSHA512_Final(sha2_byte digest[SHA512_DIGEST_LENGTH], SHA512_CTX* context) { |
948 | 24.4k | sha2_word64 *d = (sha2_word64*)digest; |
949 | | |
950 | | /* Sanity check: */ |
951 | 24.4k | assert(context != (SHA512_CTX*)0); |
952 | | |
953 | | /* If no digest buffer is passed, we don't bother doing this: */ |
954 | 24.4k | if (digest != (sha2_byte*)0) { |
955 | 24.4k | pSHA512_Last(context); |
956 | | |
957 | | /* Save the hash data for output: */ |
958 | 24.4k | #if BYTE_ORDER == LITTLE_ENDIAN |
959 | 24.4k | { |
960 | | /* Convert TO host byte order */ |
961 | 24.4k | int j; |
962 | 219k | for (j = 0; j < 8; j++) { |
963 | 195k | REVERSE64(context->state[j],context->state[j]); |
964 | 195k | *d++ = context->state[j]; |
965 | 195k | } |
966 | 24.4k | } |
967 | | #else |
968 | | MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH); |
969 | | #endif |
970 | 24.4k | } |
971 | | |
972 | | /* Zero out state data */ |
973 | 24.4k | MEMSET_BZERO(context, sizeof(*context)); |
974 | 24.4k | } |
975 | | |
976 | 0 | char *pSHA512_End(SHA512_CTX* context, char buffer[SHA512_DIGEST_STRING_LENGTH]) { |
977 | 0 | sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest; |
978 | 0 | int i; |
979 | | |
980 | | /* Sanity check: */ |
981 | 0 | assert(context != (SHA512_CTX*)0); |
982 | |
|
983 | 0 | if (buffer != (char*)0) { |
984 | 0 | pSHA512_Final(digest, context); |
985 | |
|
986 | 0 | for (i = 0; i < SHA512_DIGEST_LENGTH; i++) { |
987 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
988 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
989 | 0 | d++; |
990 | 0 | } |
991 | 0 | *buffer = (char)0; |
992 | 0 | } else { |
993 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
994 | 0 | } |
995 | 0 | MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH); |
996 | 0 | return buffer; |
997 | 0 | } |
998 | | |
999 | 0 | char* pSHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) { |
1000 | 0 | SHA512_CTX context; |
1001 | |
|
1002 | 0 | pSHA512_Init(&context); |
1003 | 0 | pSHA512_Update(&context, data, len); |
1004 | 0 | return pSHA512_End(&context, digest); |
1005 | 0 | } |
1006 | | |
1007 | | /*** SHA-384: *********************************************************/ |
1008 | 22.7k | void pSHA384_Init(SHA384_CTX* context) { |
1009 | 22.7k | if (context == (SHA384_CTX*)0) { |
1010 | 0 | return; |
1011 | 0 | } |
1012 | 22.7k | MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH); |
1013 | 22.7k | MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH); |
1014 | 22.7k | context->bitcount[0] = context->bitcount[1] = 0; |
1015 | 22.7k | } |
1016 | | |
1017 | 22.7k | void pSHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) { |
1018 | 22.7k | pSHA512_Update((SHA512_CTX*)context, data, len); |
1019 | 22.7k | } |
1020 | | |
1021 | 22.7k | void pSHA384_Final(sha2_byte digest[SHA384_DIGEST_LENGTH], SHA384_CTX* context) { |
1022 | 22.7k | sha2_word64 *d = (sha2_word64*)digest; |
1023 | | |
1024 | | /* Sanity check: */ |
1025 | 22.7k | assert(context != (SHA384_CTX*)0); |
1026 | | |
1027 | | /* If no digest buffer is passed, we don't bother doing this: */ |
1028 | 22.7k | if (digest != (sha2_byte*)0) { |
1029 | 22.7k | pSHA512_Last((SHA512_CTX*)context); |
1030 | | |
1031 | | /* Save the hash data for output: */ |
1032 | 22.7k | #if BYTE_ORDER == LITTLE_ENDIAN |
1033 | 22.7k | { |
1034 | | /* Convert TO host byte order */ |
1035 | 22.7k | int j; |
1036 | 159k | for (j = 0; j < 6; j++) { |
1037 | 136k | REVERSE64(context->state[j],context->state[j]); |
1038 | 136k | *d++ = context->state[j]; |
1039 | 136k | } |
1040 | 22.7k | } |
1041 | | #else |
1042 | | MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH); |
1043 | | #endif |
1044 | 22.7k | } |
1045 | | |
1046 | | /* Zero out state data */ |
1047 | 22.7k | MEMSET_BZERO(context, sizeof(*context)); |
1048 | 22.7k | } |
1049 | | |
1050 | 0 | char *pSHA384_End(SHA384_CTX* context, char buffer[SHA384_DIGEST_STRING_LENGTH]) { |
1051 | 0 | sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest; |
1052 | 0 | int i; |
1053 | | |
1054 | | /* Sanity check: */ |
1055 | 0 | assert(context != (SHA384_CTX*)0); |
1056 | |
|
1057 | 0 | if (buffer != (char*)0) { |
1058 | 0 | pSHA384_Final(digest, context); |
1059 | |
|
1060 | 0 | for (i = 0; i < SHA384_DIGEST_LENGTH; i++) { |
1061 | 0 | *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; |
1062 | 0 | *buffer++ = sha2_hex_digits[*d & 0x0f]; |
1063 | 0 | d++; |
1064 | 0 | } |
1065 | 0 | *buffer = (char)0; |
1066 | 0 | } else { |
1067 | 0 | MEMSET_BZERO(context, sizeof(*context)); |
1068 | 0 | } |
1069 | 0 | MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH); |
1070 | 0 | return buffer; |
1071 | 0 | } |
1072 | | |
1073 | 0 | char* pSHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) { |
1074 | 0 | SHA384_CTX context; |
1075 | |
|
1076 | 0 | pSHA384_Init(&context); |
1077 | 0 | pSHA384_Update(&context, data, len); |
1078 | 0 | return pSHA384_End(&context, digest); |
1079 | 0 | } |