Debian Codegraph @

ext/digest/sha2.c

raw
1 /*
2 * FILE: sha2.c
3 * AUTHOR: Aaron D. Gifford
4 * http://www.aarongifford.com/computers/sha.html
5 *
6 * Copyright (c) 2000-2003, Aaron D. Gifford
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the copyright holder nor the names of contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: sha2.c,v 1.4 2004/01/07 22:58:18 adg Exp $
34 */
35
36 #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
37 #include <assert.h> /* assert() */
38 #include "sha2.h"
39
40 /*
41 * ASSERT NOTE:
42 * Some sanity checking code is included using assert(). On my FreeBSD
43 * system, this additional code can be removed by compiling with NDEBUG
44 * defined. Check your own systems manpage on assert() to see how to
45 * compile WITHOUT the sanity checking code on your system.
46 *
47 * UNROLLED TRANSFORM LOOP NOTE:
48 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
49 * loop version for the hash transform rounds (defined using macros
50 * later in this file). Either define on the command line, for example:
51 *
52 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
53 *
54 * or define below:
55 *
56 * #define SHA2_UNROLL_TRANSFORM
57 *
58 */
59
60
61 /*** SHA-224/256/384/512 Machine Architecture Definitions *************/
62 /*
63 * BYTE_ORDER NOTE:
64 *
65 * Please make sure that your system defines BYTE_ORDER. If your
66 * architecture is little-endian, make sure it also defines
67 * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
68 * equivalent.
69 *
70 * If your system does not define the above, then you can do so by
71 * hand like this:
72 *
73 * #define LITTLE_ENDIAN 1234
74 * #define BIG_ENDIAN 4321
75 *
76 * And for little-endian machines, add:
77 *
78 * #define BYTE_ORDER LITTLE_ENDIAN
79 *
80 * Or for big-endian machines:
81 *
82 * #define BYTE_ORDER BIG_ENDIAN
83 *
84 * The FreeBSD machine this was written on defines BYTE_ORDER
85 * appropriately by including <sys/types.h> (which in turn includes
86 * <machine/endian.h> where the appropriate definitions are actually
87 * made).
88 */
89 /*
90 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
91 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
92 #endif
93 */
94
95 /*
96 * Define the following sha_* types to types of the correct length on
97 * the native archtecture. Most BSD systems and Linux define u_intXX_t
98 * types. Machines with very recent ANSI C headers, can use the
99 * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
100 * during compile or in the sha.h header file.
101 *
102 * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
103 * will need to define these three typedefs below (and the appropriate
104 * ones in sha.h too) by hand according to their system architecture.
105 *
106 * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
107 * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
108 */
109 #ifdef SHA2_USE_INTTYPES_H
110
111 typedef uint8_t sha_byte; /* Exactly 1 byte */
112 typedef uint32_t sha_word32; /* Exactly 4 bytes */
113 typedef uint64_t sha_word64; /* Exactly 8 bytes */
114
115 #else /* SHA2_USE_INTTYPES_H */
116
117 typedef u_int8_t sha_byte; /* Exactly 1 byte */
118 typedef u_int32_t sha_word32; /* Exactly 4 bytes */
119 typedef u_int64_t sha_word64; /* Exactly 8 bytes */
120
121 #endif /* SHA2_USE_INTTYPES_H */
122
123
124 /*** ENDIAN REVERSAL MACROS *******************************************/
125 #if BYTE_ORDER == LITTLE_ENDIAN
126 #define REVERSE32(w,x) { \
127 sha_word32 tmp = (w); \
128 tmp = (tmp >> 16) | (tmp << 16); \
129 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
130 }
131 #define REVERSE64(w,x) { \
132 sha_word64 tmp = (w); \
133 tmp = (tmp >> 32) | (tmp << 32); \
134 tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
135 ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
136 (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
137 ((tmp & 0x0000ffff0000ffffULL) << 16); \
138 }
139 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
140
141 /*
142 * Macro for incrementally adding the unsigned 64-bit integer n to the
143 * unsigned 128-bit integer (represented using a two-element array of
144 * 64-bit words):
145 */
146 #define ADDINC128(w,n) { \
147 (w)[0] += (sha_word64)(n); \
148 if ((w)[0] < (n)) { \
149 (w)[1]++; \
150 } \
151 }
152
153 /*
154 * Macros for copying blocks of memory and for zeroing out ranges
155 * of memory. Using these macros makes it easy to switch from
156 * using memset()/memcpy() and using bzero()/bcopy().
157 *
158 * Please define either SHA2_USE_MEMSET_MEMCPY or define
159 * SHA2_USE_BZERO_BCOPY depending on which function set you
160 * choose to use:
161 */
162 #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
163 /* Default to memset()/memcpy() if no option is specified */
164 #define SHA2_USE_MEMSET_MEMCPY 1
165 #endif
166 #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
167 /* Abort with an error if BOTH options are defined */
168 #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
169 #endif
170
171 #ifdef SHA2_USE_MEMSET_MEMCPY
172 #define MEMSET_BZERO(p,l) memset((p), 0, (l))
173 #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
174 #endif
175 #ifdef SHA2_USE_BZERO_BCOPY
176 #define MEMSET_BZERO(p,l) bzero((p), (l))
177 #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
178 #endif
179
180
181 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
182 /*
183 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
184 *
185 * NOTE: In the original SHA-256/384/512 document, the shift-right
186 * function was named R and the rotate-right function was called S.
187 * (See: http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf on the
188 * web.)
189 *
190 * The newer NIST FIPS 180-2 document uses a much clearer naming
191 * scheme, SHR for shift-right, ROTR for rotate-right, and ROTL for
192 * rotate-left. (See:
193 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
194 * on the web.)
195 *
196 * WARNING: These macros must be used cautiously, since they reference
197 * supplied parameters sometimes more than once, and thus could have
198 * unexpected side-effects if used without taking this into account.
199 */
200 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
201 #define SHR(b,x) ((x) >> (b))
202 /* 32-bit Rotate-right (used in SHA-256): */
203 #define ROTR32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
204 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
205 #define ROTR64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
206 /* 32-bit Rotate-left (used in SHA-1): */
207 #define ROTL32(b,x) (((x) << (b)) | ((x) >> (32 - (b))))
208
209 /* Two logical functions used in SHA-1, SHA-254, SHA-256, SHA-384, and SHA-512: */
210 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
211 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
212
213 /* Function used in SHA-1: */
214 #define Parity(x,y,z) ((x) ^ (y) ^ (z))
215
216 /* Four logical functions used in SHA-256: */
217 #define Sigma0_256(x) (ROTR32(2, (x)) ^ ROTR32(13, (x)) ^ ROTR32(22, (x)))
218 #define Sigma1_256(x) (ROTR32(6, (x)) ^ ROTR32(11, (x)) ^ ROTR32(25, (x)))
219 #define sigma0_256(x) (ROTR32(7, (x)) ^ ROTR32(18, (x)) ^ SHR( 3 , (x)))
220 #define sigma1_256(x) (ROTR32(17, (x)) ^ ROTR32(19, (x)) ^ SHR( 10, (x)))
221
222 /* Four of six logical functions used in SHA-384 and SHA-512: */
223 #define Sigma0_512(x) (ROTR64(28, (x)) ^ ROTR64(34, (x)) ^ ROTR64(39, (x)))
224 #define Sigma1_512(x) (ROTR64(14, (x)) ^ ROTR64(18, (x)) ^ ROTR64(41, (x)))
225 #define sigma0_512(x) (ROTR64( 1, (x)) ^ ROTR64( 8, (x)) ^ SHR( 7, (x)))
226 #define sigma1_512(x) (ROTR64(19, (x)) ^ ROTR64(61, (x)) ^ SHR( 6, (x)))
227
228 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
229
230 /* SHA-224 and SHA-256: */
231 void SHA256_Internal_Init(SHA_CTX*, const sha_word32*);
232 void SHA256_Internal_Last(SHA_CTX*);
233 void SHA256_Internal_Transform(SHA_CTX*, const sha_word32*);
234
235 /* SHA-384 and SHA-512: */
236 void SHA512_Internal_Init(SHA_CTX*, const sha_word64*);
237 void SHA512_Internal_Last(SHA_CTX*);
238 void SHA512_Internal_Transform(SHA_CTX*, const sha_word64*);
239
240
241 /*** SHA2 INITIAL HASH VALUES AND CONSTANTS ***************************/
242
243 /* Hash constant words K for SHA-1: */
244 #define K1_0_TO_19 0x5a827999UL
245 #define K1_20_TO_39 0x6ed9eba1UL
246 #define K1_40_TO_59 0x8f1bbcdcUL
247 #define K1_60_TO_79 0xca62c1d6UL
248
249 /* Initial hash value H for SHA-1: */
250 static const sha_word32 sha1_initial_hash_value[5] = {
251 0x67452301UL,
252 0xefcdab89UL,
253 0x98badcfeUL,
254 0x10325476UL,
255 0xc3d2e1f0UL
256 };
257
258 /* Hash constant words K for SHA-224 and SHA-256: */
259 static const sha_word32 K256[64] = {
260 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
261 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
262 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
263 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
264 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
265 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
266 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
267 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
268 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
269 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
270 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
271 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
272 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
273 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
274 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
275 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
276 };
277
278 /* Initial hash value H for SHA-224: */
279 static const sha_word32 sha224_initial_hash_value[8] = {
280 0xc1059ed8UL,
281 0x367cd507UL,
282 0x3070dd17UL,
283 0xf70e5939UL,
284 0xffc00b31UL,
285 0x68581511UL,
286 0x64f98fa7UL,
287 0xbefa4fa4UL
288 };
289
290 /* Initial hash value H for SHA-256: */
291 static const sha_word32 sha256_initial_hash_value[8] = {
292 0x6a09e667UL,
293 0xbb67ae85UL,
294 0x3c6ef372UL,
295 0xa54ff53aUL,
296 0x510e527fUL,
297 0x9b05688cUL,
298 0x1f83d9abUL,
299 0x5be0cd19UL
300 };
301
302 /* Hash constant words K for SHA-384 and SHA-512: */
303 static const sha_word64 K512[80] = {
304 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
305 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
306 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
307 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
308 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
309 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
310 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
311 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
312 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
313 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
314 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
315 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
316 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
317 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
318 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
319 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
320 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
321 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
322 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
323 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
324 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
325 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
326 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
327 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
328 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
329 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
330 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
331 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
332 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
333 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
334 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
335 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
336 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
337 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
338 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
339 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
340 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
341 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
342 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
343 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
344 };
345
346 /* Initial hash value H for SHA-384 */
347 static const sha_word64 sha384_initial_hash_value[8] = {
348 0xcbbb9d5dc1059ed8ULL,
349 0x629a292a367cd507ULL,
350 0x9159015a3070dd17ULL,
351 0x152fecd8f70e5939ULL,
352 0x67332667ffc00b31ULL,
353 0x8eb44a8768581511ULL,
354 0xdb0c2e0d64f98fa7ULL,
355 0x47b5481dbefa4fa4ULL
356 };
357
358 /* Initial hash value H for SHA-512 */
359 static const sha_word64 sha512_initial_hash_value[8] = {
360 0x6a09e667f3bcc908ULL,
361 0xbb67ae8584caa73bULL,
362 0x3c6ef372fe94f82bULL,
363 0xa54ff53a5f1d36f1ULL,
364 0x510e527fade682d1ULL,
365 0x9b05688c2b3e6c1fULL,
366 0x1f83d9abfb41bd6bULL,
367 0x5be0cd19137e2179ULL
368 };
369
370 /*
371 * Constant used by SHA224/256/384/512_End() functions for converting the
372 * digest to a readable hexadecimal character string:
373 */
374 static const char *sha_hex_digits = "0123456789abcdef";
375
376
377 /*** SHA-1: ***********************************************************/
378 void SHA1_Init(SHA_CTX* context) {
379 /* Sanity check: */
380 assert(context != (SHA_CTX*)0);
381
382 MEMCPY_BCOPY(context->s1.state, sha1_initial_hash_value, sizeof(sha_word32) * 5);
383 MEMSET_BZERO(context->s1.buffer, 64);
384 context->s1.bitcount = 0;
385 }
386
387 #ifdef SHA2_UNROLL_TRANSFORM
388
389 /* Unrolled SHA-1 round macros: */
390
391 #if BYTE_ORDER == LITTLE_ENDIAN
392
393 #define ROUND1_0_TO_15(a,b,c,d,e) \
394 REVERSE32(*data++, W1[j]); \
395 (e) = ROTL32(5, (a)) + Ch((b), (c), (d)) + (e) + \
396 K1_0_TO_19 + W1[j]; \
397 (b) = ROTL32(30, (b)); \
398 j++;
399
400 #else /* BYTE_ORDER == LITTLE_ENDIAN */
401
402 #define ROUND1_0_TO_15(a,b,c,d,e) \
403 (e) = ROTL32(5, (a)) + Ch((b), (c), (d)) + (e) + \
404 K1_0_TO_19 + ( W1[j] = *data++ ); \
405 (b) = ROTL32(30, (b)); \
406 j++;
407
408 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
409
410 #define ROUND1_16_TO_19(a,b,c,d,e) \
411 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f]; \
412 (e) = ROTL32(5, a) + Ch(b,c,d) + e + K1_0_TO_19 + ( W1[j&0x0f] = ROTL32(1, T1) ); \
413 (b) = ROTL32(30, b); \
414 j++;
415
416 #define ROUND1_20_TO_39(a,b,c,d,e) \
417 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f]; \
418 (e) = ROTL32(5, a) + Parity(b,c,d) + e + K1_20_TO_39 + ( W1[j&0x0f] = ROTL32(1, T1) ); \
419 (b) = ROTL32(30, b); \
420 j++;
421
422 #define ROUND1_40_TO_59(a,b,c,d,e) \
423 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f]; \
424 (e) = ROTL32(5, a) + Maj(b,c,d) + e + K1_40_TO_59 + ( W1[j&0x0f] = ROTL32(1, T1) ); \
425 (b) = ROTL32(30, b); \
426 j++;
427
428 #define ROUND1_60_TO_79(a,b,c,d,e) \
429 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f]; \
430 (e) = ROTL32(5, a) + Parity(b,c,d) + e + K1_60_TO_79 + ( W1[j&0x0f] = ROTL32(1, T1) ); \
431 (b) = ROTL32(30, b); \
432 j++;
433
434 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
435 sha_word32 a, b, c, d, e;
436 sha_word32 T1, *W1;
437 int j;
438
439 W1 = (sha_word32*)context->s1.buffer;
440
441 /* Initialize registers with the prev. intermediate value */
442 a = context->s1.state[0];
443 b = context->s1.state[1];
444 c = context->s1.state[2];
445 d = context->s1.state[3];
446 e = context->s1.state[4];
447
448 j = 0;
449
450 /* Rounds 0 to 15 unrolled: */
451 ROUND1_0_TO_15(a,b,c,d,e);
452 ROUND1_0_TO_15(e,a,b,c,d);
453 ROUND1_0_TO_15(d,e,a,b,c);
454 ROUND1_0_TO_15(c,d,e,a,b);
455 ROUND1_0_TO_15(b,c,d,e,a);
456 ROUND1_0_TO_15(a,b,c,d,e);
457 ROUND1_0_TO_15(e,a,b,c,d);
458 ROUND1_0_TO_15(d,e,a,b,c);
459 ROUND1_0_TO_15(c,d,e,a,b);
460 ROUND1_0_TO_15(b,c,d,e,a);
461 ROUND1_0_TO_15(a,b,c,d,e);
462 ROUND1_0_TO_15(e,a,b,c,d);
463 ROUND1_0_TO_15(d,e,a,b,c);
464 ROUND1_0_TO_15(c,d,e,a,b);
465 ROUND1_0_TO_15(b,c,d,e,a);
466 ROUND1_0_TO_15(a,b,c,d,e);
467
468 /* Rounds 16 to 19 unrolled: */
469 ROUND1_16_TO_19(e,a,b,c,d);
470 ROUND1_16_TO_19(d,e,a,b,c);
471 ROUND1_16_TO_19(c,d,e,a,b);
472 ROUND1_16_TO_19(b,c,d,e,a);
473
474 /* Rounds 20 to 39 unrolled: */
475 ROUND1_20_TO_39(a,b,c,d,e);
476 ROUND1_20_TO_39(e,a,b,c,d);
477 ROUND1_20_TO_39(d,e,a,b,c);
478 ROUND1_20_TO_39(c,d,e,a,b);
479 ROUND1_20_TO_39(b,c,d,e,a);
480 ROUND1_20_TO_39(a,b,c,d,e);
481 ROUND1_20_TO_39(e,a,b,c,d);
482 ROUND1_20_TO_39(d,e,a,b,c);
483 ROUND1_20_TO_39(c,d,e,a,b);
484 ROUND1_20_TO_39(b,c,d,e,a);
485 ROUND1_20_TO_39(a,b,c,d,e);
486 ROUND1_20_TO_39(e,a,b,c,d);
487 ROUND1_20_TO_39(d,e,a,b,c);
488 ROUND1_20_TO_39(c,d,e,a,b);
489 ROUND1_20_TO_39(b,c,d,e,a);
490 ROUND1_20_TO_39(a,b,c,d,e);
491 ROUND1_20_TO_39(e,a,b,c,d);
492 ROUND1_20_TO_39(d,e,a,b,c);
493 ROUND1_20_TO_39(c,d,e,a,b);
494 ROUND1_20_TO_39(b,c,d,e,a);
495
496 /* Rounds 40 to 59 unrolled: */
497 ROUND1_40_TO_59(a,b,c,d,e);
498 ROUND1_40_TO_59(e,a,b,c,d);
499 ROUND1_40_TO_59(d,e,a,b,c);
500 ROUND1_40_TO_59(c,d,e,a,b);
501 ROUND1_40_TO_59(b,c,d,e,a);
502 ROUND1_40_TO_59(a,b,c,d,e);
503 ROUND1_40_TO_59(e,a,b,c,d);
504 ROUND1_40_TO_59(d,e,a,b,c);
505 ROUND1_40_TO_59(c,d,e,a,b);
506 ROUND1_40_TO_59(b,c,d,e,a);
507 ROUND1_40_TO_59(a,b,c,d,e);
508 ROUND1_40_TO_59(e,a,b,c,d);
509 ROUND1_40_TO_59(d,e,a,b,c);
510 ROUND1_40_TO_59(c,d,e,a,b);
511 ROUND1_40_TO_59(b,c,d,e,a);
512 ROUND1_40_TO_59(a,b,c,d,e);
513 ROUND1_40_TO_59(e,a,b,c,d);
514 ROUND1_40_TO_59(d,e,a,b,c);
515 ROUND1_40_TO_59(c,d,e,a,b);
516 ROUND1_40_TO_59(b,c,d,e,a);
517
518 /* Rounds 60 to 79 unrolled: */
519 ROUND1_60_TO_79(a,b,c,d,e);
520 ROUND1_60_TO_79(e,a,b,c,d);
521 ROUND1_60_TO_79(d,e,a,b,c);
522 ROUND1_60_TO_79(c,d,e,a,b);
523 ROUND1_60_TO_79(b,c,d,e,a);
524 ROUND1_60_TO_79(a,b,c,d,e);
525 ROUND1_60_TO_79(e,a,b,c,d);
526 ROUND1_60_TO_79(d,e,a,b,c);
527 ROUND1_60_TO_79(c,d,e,a,b);
528 ROUND1_60_TO_79(b,c,d,e,a);
529 ROUND1_60_TO_79(a,b,c,d,e);
530 ROUND1_60_TO_79(e,a,b,c,d);
531 ROUND1_60_TO_79(d,e,a,b,c);
532 ROUND1_60_TO_79(c,d,e,a,b);
533 ROUND1_60_TO_79(b,c,d,e,a);
534 ROUND1_60_TO_79(a,b,c,d,e);
535 ROUND1_60_TO_79(e,a,b,c,d);
536 ROUND1_60_TO_79(d,e,a,b,c);
537 ROUND1_60_TO_79(c,d,e,a,b);
538 ROUND1_60_TO_79(b,c,d,e,a);
539
540 /* Compute the current intermediate hash value */
541 context->s1.state[0] += a;
542 context->s1.state[1] += b;
543 context->s1.state[2] += c;
544 context->s1.state[3] += d;
545 context->s1.state[4] += e;
546
547 /* Clean up */
548 a = b = c = d = e = T1 = 0;
549 }
550
551 #else /* SHA2_UNROLL_TRANSFORM */
552
553 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
554 sha_word32 a, b, c, d, e;
555 sha_word32 T1, *W1;
556 int j;
557
558 W1 = (sha_word32*)context->s1.buffer;
559
560 /* Initialize registers with the prev. intermediate value */
561 a = context->s1.state[0];
562 b = context->s1.state[1];
563 c = context->s1.state[2];
564 d = context->s1.state[3];
565 e = context->s1.state[4];
566 j = 0;
567 do {
568 #if BYTE_ORDER == LITTLE_ENDIAN
569 T1 = data[j];
570 /* Copy data while converting to host byte order */
571 REVERSE32(*data++, W1[j]);
572 T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + W1[j];
573 #else /* BYTE_ORDER == LITTLE_ENDIAN */
574 T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + (W1[j] = *data++);
575 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
576 e = d;
577 d = c;
578 c = ROTL32(30, b);
579 b = a;
580 a = T1;
581 j++;
582 } while (j < 16);
583
584 do {
585 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
586 T1 = ROTL32(5, a) + Ch(b,c,d) + e + K1_0_TO_19 + (W1[j&0x0f] = ROTL32(1, T1));
587 e = d;
588 d = c;
589 c = ROTL32(30, b);
590 b = a;
591 a = T1;
592 j++;
593 } while (j < 20);
594
595 do {
596 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
597 T1 = ROTL32(5, a) + Parity(b,c,d) + e + K1_20_TO_39 + (W1[j&0x0f] = ROTL32(1, T1));
598 e = d;
599 d = c;
600 c = ROTL32(30, b);
601 b = a;
602 a = T1;
603 j++;
604 } while (j < 40);
605
606 do {
607 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
608 T1 = ROTL32(5, a) + Maj(b,c,d) + e + K1_40_TO_59 + (W1[j&0x0f] = ROTL32(1, T1));
609 e = d;
610 d = c;
611 c = ROTL32(30, b);
612 b = a;
613 a = T1;
614 j++;
615 } while (j < 60);
616
617 do {
618 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
619 T1 = ROTL32(5, a) + Parity(b,c,d) + e + K1_60_TO_79 + (W1[j&0x0f] = ROTL32(1, T1));
620 e = d;
621 d = c;
622 c = ROTL32(30, b);
623 b = a;
624 a = T1;
625 j++;
626 } while (j < 80);
627
628
629 /* Compute the current intermediate hash value */
630 context->s1.state[0] += a;
631 context->s1.state[1] += b;
632 context->s1.state[2] += c;
633 context->s1.state[3] += d;
634 context->s1.state[4] += e;
635
636 /* Clean up */
637 a = b = c = d = e = T1 = 0;
638 }
639
640 #endif /* SHA2_UNROLL_TRANSFORM */
641
642 void SHA1_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
643 unsigned int freespace, usedspace;
644 if (len == 0) {
645 /* Calling with no data is valid - we do nothing */
646 return;
647 }
648
649 /* Sanity check: */
650 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
651
652 usedspace = (context->s1.bitcount >> 3) % 64;
653 if (usedspace > 0) {
654 /* Calculate how much free space is available in the buffer */
655 freespace = 64 - usedspace;
656
657 if (len >= freespace) {
658 /* Fill the buffer completely and process it */
659 MEMCPY_BCOPY(&context->s1.buffer[usedspace], data, freespace);
660 context->s1.bitcount += freespace << 3;
661 len -= freespace;
662 data += freespace;
663 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
664 } else {
665 /* The buffer is not yet full */
666 MEMCPY_BCOPY(&context->s1.buffer[usedspace], data, len);
667 context->s1.bitcount += len << 3;
668 /* Clean up: */
669 usedspace = freespace = 0;
670 return;
671 }
672 }
673 while (len >= 64) {
674 /* Process as many complete blocks as we can */
675 SHA1_Internal_Transform(context, (sha_word32*)data);
676 context->s1.bitcount += 512;
677 len -= 64;
678 data += 64;
679 }
680 if (len > 0) {
681 /* There's left-overs, so save 'em */
682 MEMCPY_BCOPY(context->s1.buffer, data, len);
683 context->s1.bitcount += len << 3;
684 }
685 /* Clean up: */
686 usedspace = freespace = 0;
687 }
688
689 void SHA1_Final(sha_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX* context) {
690 sha_word32 *d = (sha_word32*)digest;
691 unsigned int usedspace;
692
693 /* Sanity check: */
694 assert(context != (SHA_CTX*)0);
695
696 if (digest == (sha_byte*)0) {
697 /*
698 * No digest buffer, so we can do nothing
699 * except clean up and go home
700 */
701 MEMSET_BZERO(context, sizeof(*context));
702 return;
703 }
704
705 usedspace = (context->s1.bitcount >> 3) % 64;
706 if (usedspace == 0) {
707 /* Set-up for the last transform: */
708 MEMSET_BZERO(context->s1.buffer, 56);
709
710 /* Begin padding with a 1 bit: */
711 *context->s1.buffer = 0x80;
712 } else {
713 /* Begin padding with a 1 bit: */
714 context->s1.buffer[usedspace++] = 0x80;
715
716 if (usedspace <= 56) {
717 /* Set-up for the last transform: */
718 MEMSET_BZERO(&context->s1.buffer[usedspace], 56 - usedspace);
719 } else {
720 if (usedspace < 64) {
721 MEMSET_BZERO(&context->s1.buffer[usedspace], 64 - usedspace);
722 }
723 /* Do second-to-last transform: */
724 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
725
726 /* And set-up for the last transform: */
727 MEMSET_BZERO(context->s1.buffer, 56);
728 }
729 /* Clean up: */
730 usedspace = 0;
731 }
732 /* Set the bit count: */
733 /* [SK] Original code used type punning to set 64bit bitcount into
734 buffer, but that violates the strict aliasing rule and caused
735 unintended code with gcc 11.2. */
736 sha_byte *buf56 = &context->s1.buffer[56];
737 buf56[7] = context->s1.bitcount & 0xff;
738 buf56[6] = (context->s1.bitcount >> 8) & 0xff;
739 buf56[5] = (context->s1.bitcount >> 16) & 0xff;
740 buf56[4] = (context->s1.bitcount >> 24) & 0xff;
741 buf56[3] = (context->s1.bitcount >> 32) & 0xff;
742 buf56[2] = (context->s1.bitcount >> 40) & 0xff;
743 buf56[1] = (context->s1.bitcount >> 48) & 0xff;
744 buf56[0] = (context->s1.bitcount >> 56) & 0xff;
745
746 /* Final transform: */
747 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
748
749 /* Save the hash data for output: */
750 #if BYTE_ORDER == LITTLE_ENDIAN
751 {
752 /* Convert TO host byte order */
753 int j;
754 for (j = 0; j < (SHA1_DIGEST_LENGTH >> 2); j++) {
755 REVERSE32(context->s1.state[j],context->s1.state[j]);
756 *d++ = context->s1.state[j];
757 }
758 }
759 #else
760 MEMCPY_BCOPY(d, context->s1.state, SHA1_DIGEST_LENGTH);
761 #endif
762
763 /* Clean up: */
764 MEMSET_BZERO(context, sizeof(*context));
765 }
766
767 char *SHA1_End(SHA_CTX* context, char buffer[SHA1_DIGEST_STRING_LENGTH]) {
768 sha_byte digest[SHA1_DIGEST_LENGTH], *d = digest;
769 int i;
770
771 /* Sanity check: */
772 assert(context != (SHA_CTX*)0);
773
774 if (buffer != (char*)0) {
775 SHA1_Final(digest, context);
776
777 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
778 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
779 *buffer++ = sha_hex_digits[*d & 0x0f];
780 d++;
781 }
782 *buffer = (char)0;
783 } else {
784 MEMSET_BZERO(context, sizeof(*context));
785 }
786 MEMSET_BZERO(digest, SHA1_DIGEST_LENGTH);
787 return buffer;
788 }
789
790 char* SHA1_Data(const sha_byte* data, size_t len, char digest[SHA1_DIGEST_STRING_LENGTH]) {
791 SHA_CTX context;
792
793 SHA1_Init(&context);
794 SHA1_Update(&context, data, len);
795 return SHA1_End(&context, digest);
796 }
797
798
799 /*** SHA-256: *********************************************************/
800 void SHA256_Internal_Init(SHA_CTX* context, const sha_word32* ihv) {
801 /* Sanity check: */
802 assert(context != (SHA_CTX*)0);
803
804 MEMCPY_BCOPY(context->s256.state, ihv, sizeof(sha_word32) * 8);
805 MEMSET_BZERO(context->s256.buffer, 64);
806 context->s256.bitcount = 0;
807 }
808
809 void SHA256_Init(SHA_CTX* context) {
810 SHA256_Internal_Init(context, sha256_initial_hash_value);
811 }
812
813 #ifdef SHA2_UNROLL_TRANSFORM
814
815 /* Unrolled SHA-256 round macros: */
816
817 #if BYTE_ORDER == LITTLE_ENDIAN
818
819 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
820 REVERSE32(*data++, W256[j]); \
821 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
822 K256[j] + W256[j]; \
823 (d) += T1; \
824 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
825 j++
826
827
828 #else /* BYTE_ORDER == LITTLE_ENDIAN */
829
830 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
831 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
832 K256[j] + (W256[j] = *data++); \
833 (d) += T1; \
834 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
835 j++
836
837 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
838
839 #define ROUND256(a,b,c,d,e,f,g,h) \
840 s0 = W256[(j+1)&0x0f]; \
841 s0 = sigma0_256(s0); \
842 s1 = W256[(j+14)&0x0f]; \
843 s1 = sigma1_256(s1); \
844 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
845 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
846 (d) += T1; \
847 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
848 j++
849
850 void SHA256_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
851 sha_word32 a, b, c, d, e, f, g, h, s0, s1;
852 sha_word32 T1, *W256;
853 int j;
854
855 W256 = (sha_word32*)context->s256.buffer;
856
857 /* Initialize registers with the prev. intermediate value */
858 a = context->s256.state[0];
859 b = context->s256.state[1];
860 c = context->s256.state[2];
861 d = context->s256.state[3];
862 e = context->s256.state[4];
863 f = context->s256.state[5];
864 g = context->s256.state[6];
865 h = context->s256.state[7];
866
867 j = 0;
868 do {
869 /* Rounds 0 to 15 (unrolled): */
870 ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
871 ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
872 ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
873 ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
874 ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
875 ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
876 ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
877 ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
878 } while (j < 16);
879
880 /* Now for the remaining rounds to 64: */
881 do {
882 ROUND256(a,b,c,d,e,f,g,h);
883 ROUND256(h,a,b,c,d,e,f,g);
884 ROUND256(g,h,a,b,c,d,e,f);
885 ROUND256(f,g,h,a,b,c,d,e);
886 ROUND256(e,f,g,h,a,b,c,d);
887 ROUND256(d,e,f,g,h,a,b,c);
888 ROUND256(c,d,e,f,g,h,a,b);
889 ROUND256(b,c,d,e,f,g,h,a);
890 } while (j < 64);
891
892 /* Compute the current intermediate hash value */
893 context->s256.state[0] += a;
894 context->s256.state[1] += b;
895 context->s256.state[2] += c;
896 context->s256.state[3] += d;
897 context->s256.state[4] += e;
898 context->s256.state[5] += f;
899 context->s256.state[6] += g;
900 context->s256.state[7] += h;
901
902 /* Clean up */
903 a = b = c = d = e = f = g = h = T1 = 0;
904 }
905
906 #else /* SHA2_UNROLL_TRANSFORM */
907
908 void SHA256_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
909 sha_word32 a, b, c, d, e, f, g, h, s0, s1;
910 sha_word32 T1, T2, *W256;
911 int j;
912
913 W256 = (sha_word32*)context->s256.buffer;
914
915 /* Initialize registers with the prev. intermediate value */
916 a = context->s256.state[0];
917 b = context->s256.state[1];
918 c = context->s256.state[2];
919 d = context->s256.state[3];
920 e = context->s256.state[4];
921 f = context->s256.state[5];
922 g = context->s256.state[6];
923 h = context->s256.state[7];
924
925 j = 0;
926 do {
927 #if BYTE_ORDER == LITTLE_ENDIAN
928 /* Copy data while converting to host byte order */
929 REVERSE32(*data++,W256[j]);
930 /* Apply the SHA-256 compression function to update a..h */
931 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
932 #else /* BYTE_ORDER == LITTLE_ENDIAN */
933 /* Apply the SHA-256 compression function to update a..h with copy */
934 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
935 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
936 T2 = Sigma0_256(a) + Maj(a, b, c);
937 h = g;
938 g = f;
939 f = e;
940 e = d + T1;
941 d = c;
942 c = b;
943 b = a;
944 a = T1 + T2;
945
946 j++;
947 } while (j < 16);
948
949 do {
950 /* Part of the message block expansion: */
951 s0 = W256[(j+1)&0x0f];
952 s0 = sigma0_256(s0);
953 s1 = W256[(j+14)&0x0f];
954 s1 = sigma1_256(s1);
955
956 /* Apply the SHA-256 compression function to update a..h */
957 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
958 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
959 T2 = Sigma0_256(a) + Maj(a, b, c);
960 h = g;
961 g = f;
962 f = e;
963 e = d + T1;
964 d = c;
965 c = b;
966 b = a;
967 a = T1 + T2;
968
969 j++;
970 } while (j < 64);
971
972 /* Compute the current intermediate hash value */
973 context->s256.state[0] += a;
974 context->s256.state[1] += b;
975 context->s256.state[2] += c;
976 context->s256.state[3] += d;
977 context->s256.state[4] += e;
978 context->s256.state[5] += f;
979 context->s256.state[6] += g;
980 context->s256.state[7] += h;
981
982 /* Clean up */
983 a = b = c = d = e = f = g = h = T1 = T2 = 0;
984 }
985
986 #endif /* SHA2_UNROLL_TRANSFORM */
987
988 void SHA256_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
989 unsigned int freespace, usedspace;
990
991 if (len == 0) {
992 /* Calling with no data is valid - we do nothing */
993 return;
994 }
995
996 /* Sanity check: */
997 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
998
999 usedspace = (context->s256.bitcount >> 3) % 64;
1000 if (usedspace > 0) {
1001 /* Calculate how much free space is available in the buffer */
1002 freespace = 64 - usedspace;
1003
1004 if (len >= freespace) {
1005 /* Fill the buffer completely and process it */
1006 MEMCPY_BCOPY(&context->s256.buffer[usedspace], data, freespace);
1007 context->s256.bitcount += freespace << 3;
1008 len -= freespace;
1009 data += freespace;
1010 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1011 } else {
1012 /* The buffer is not yet full */
1013 MEMCPY_BCOPY(&context->s256.buffer[usedspace], data, len);
1014 context->s256.bitcount += len << 3;
1015 /* Clean up: */
1016 usedspace = freespace = 0;
1017 return;
1018 }
1019 }
1020 while (len >= 64) {
1021 /* Process as many complete blocks as we can */
1022 SHA256_Internal_Transform(context, (sha_word32*)data);
1023 context->s256.bitcount += 512;
1024 len -= 64;
1025 data += 64;
1026 }
1027 if (len > 0) {
1028 /* There's left-overs, so save 'em */
1029 MEMCPY_BCOPY(context->s256.buffer, data, len);
1030 context->s256.bitcount += len << 3;
1031 }
1032 /* Clean up: */
1033 usedspace = freespace = 0;
1034 }
1035
1036 void SHA256_Internal_Last(SHA_CTX* context) {
1037 unsigned int usedspace;
1038
1039 usedspace = (context->s256.bitcount >> 3) % 64;
1040 if (usedspace > 0) {
1041 /* Begin padding with a 1 bit: */
1042 context->s256.buffer[usedspace++] = 0x80;
1043
1044 if (usedspace <= 56) {
1045 /* Set-up for the last transform: */
1046 MEMSET_BZERO(&context->s256.buffer[usedspace], 56 - usedspace);
1047 } else {
1048 if (usedspace < 64) {
1049 MEMSET_BZERO(&context->s256.buffer[usedspace], 64 - usedspace);
1050 }
1051 /* Do second-to-last transform: */
1052 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1053
1054 /* And set-up for the last transform: */
1055 MEMSET_BZERO(context->s256.buffer, 56);
1056 }
1057 /* Clean up: */
1058 usedspace = 0;
1059 } else {
1060 /* Set-up for the last transform: */
1061 MEMSET_BZERO(context->s256.buffer, 56);
1062
1063 /* Begin padding with a 1 bit: */
1064 *context->s256.buffer = 0x80;
1065 }
1066 /* Set the bit count: */
1067 /* [SK] see the above comment in SHA1_Final */
1068 sha_byte *buf56 = &context->s256.buffer[56];
1069 buf56[7] = context->s256.bitcount & 0xff;
1070 buf56[6] = (context->s256.bitcount >> 8) & 0xff;
1071 buf56[5] = (context->s256.bitcount >> 16) & 0xff;
1072 buf56[4] = (context->s256.bitcount >> 24) & 0xff;
1073 buf56[3] = (context->s256.bitcount >> 32) & 0xff;
1074 buf56[2] = (context->s256.bitcount >> 40) & 0xff;
1075 buf56[1] = (context->s256.bitcount >> 48) & 0xff;
1076 buf56[0] = (context->s256.bitcount >> 56) & 0xff;
1077
1078 /* Final transform: */
1079 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1080 }
1081
1082 void SHA256_Final(sha_byte digest[SHA256_DIGEST_LENGTH], SHA_CTX* context) {
1083 sha_word32 *d = (sha_word32*)digest;
1084
1085 /* Sanity check: */
1086 assert(context != (SHA_CTX*)0);
1087
1088 /* If no digest buffer is passed, we don't bother doing this: */
1089 if (digest != (sha_byte*)0) {
1090 SHA256_Internal_Last(context);
1091
1092 /* Save the hash data for output: */
1093 #if BYTE_ORDER == LITTLE_ENDIAN
1094 {
1095 /* Convert TO host byte order */
1096 int j;
1097 for (j = 0; j < (SHA256_DIGEST_LENGTH >> 2); j++) {
1098 REVERSE32(context->s256.state[j],context->s256.state[j]);
1099 *d++ = context->s256.state[j];
1100 }
1101 }
1102 #else
1103 MEMCPY_BCOPY(d, context->s256.state, SHA256_DIGEST_LENGTH);
1104 #endif
1105 }
1106
1107 /* Clean up state data: */
1108 MEMSET_BZERO(context, sizeof(*context));
1109 }
1110
1111 char *SHA256_End(SHA_CTX* context, char buffer[SHA256_DIGEST_STRING_LENGTH]) {
1112 sha_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
1113 int i;
1114
1115 /* Sanity check: */
1116 assert(context != (SHA_CTX*)0);
1117
1118 if (buffer != (char*)0) {
1119 SHA256_Final(digest, context);
1120
1121 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1122 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1123 *buffer++ = sha_hex_digits[*d & 0x0f];
1124 d++;
1125 }
1126 *buffer = (char)0;
1127 } else {
1128 MEMSET_BZERO(context, sizeof(*context));
1129 }
1130 MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
1131 return buffer;
1132 }
1133
1134 char* SHA256_Data(const sha_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
1135 SHA_CTX context;
1136
1137 SHA256_Init(&context);
1138 SHA256_Update(&context, data, len);
1139 return SHA256_End(&context, digest);
1140 }
1141
1142
1143 /*** SHA-224: *********************************************************/
1144 void SHA224_Init(SHA_CTX* context) {
1145 SHA256_Internal_Init(context, sha224_initial_hash_value);
1146 }
1147
1148 void SHA224_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
1149 SHA256_Internal_Transform(context, data);
1150 }
1151
1152 void SHA224_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1153 SHA256_Update(context, data, len);
1154 }
1155
1156 void SHA224_Final(sha_byte digest[SHA224_DIGEST_LENGTH], SHA_CTX* context) {
1157 sha_word32 *d = (sha_word32*)digest;
1158
1159 /* Sanity check: */
1160 assert(context != (SHA_CTX*)0);
1161
1162 /* If no digest buffer is passed, we don't bother doing this: */
1163 if (digest != (sha_byte*)0) {
1164 SHA256_Internal_Last(context);
1165
1166 /* Save the hash data for output: */
1167 #if BYTE_ORDER == LITTLE_ENDIAN
1168 {
1169 /* Convert TO host byte order */
1170 int j;
1171 for (j = 0; j < (SHA224_DIGEST_LENGTH >> 2); j++) {
1172 REVERSE32(context->s256.state[j],context->s256.state[j]);
1173 *d++ = context->s256.state[j];
1174 }
1175 }
1176 #else
1177 MEMCPY_BCOPY(d, context->s256.state, SHA224_DIGEST_LENGTH);
1178 #endif
1179 }
1180
1181 /* Clean up state data: */
1182 MEMSET_BZERO(context, sizeof(*context));
1183 }
1184
1185 char *SHA224_End(SHA_CTX* context, char buffer[SHA224_DIGEST_STRING_LENGTH]) {
1186 sha_byte digest[SHA224_DIGEST_LENGTH], *d = digest;
1187 int i;
1188
1189 /* Sanity check: */
1190 assert(context != (SHA_CTX*)0);
1191
1192 if (buffer != (char*)0) {
1193 SHA224_Final(digest, context);
1194
1195 for (i = 0; i < SHA224_DIGEST_LENGTH; i++) {
1196 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1197 *buffer++ = sha_hex_digits[*d & 0x0f];
1198 d++;
1199 }
1200 *buffer = (char)0;
1201 } else {
1202 MEMSET_BZERO(context, sizeof(*context));
1203 }
1204 MEMSET_BZERO(digest, SHA224_DIGEST_LENGTH);
1205 return buffer;
1206 }
1207
1208 char* SHA224_Data(const sha_byte* data, size_t len, char digest[SHA224_DIGEST_STRING_LENGTH]) {
1209 SHA_CTX context;
1210
1211 SHA224_Init(&context);
1212 SHA224_Update(&context, data, len);
1213 return SHA224_End(&context, digest);
1214 }
1215
1216
1217 /*** SHA-512: *********************************************************/
1218 void SHA512_Internal_Init(SHA_CTX* context, const sha_word64* ihv) {
1219 /* Sanity check: */
1220 assert(context != (SHA_CTX*)0);
1221
1222 MEMCPY_BCOPY(context->s512.state, ihv, sizeof(sha_word64) * 8);
1223 MEMSET_BZERO(context->s512.buffer, 128);
1224 context->s512.bitcount[0] = context->s512.bitcount[1] = 0;
1225 }
1226
1227 void SHA512_Init(SHA_CTX* context) {
1228 SHA512_Internal_Init(context, sha512_initial_hash_value);
1229 }
1230
1231 #ifdef SHA2_UNROLL_TRANSFORM
1232
1233 /* Unrolled SHA-512 round macros: */
1234 #if BYTE_ORDER == LITTLE_ENDIAN
1235
1236 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
1237 REVERSE64(*data++, W512[j]); \
1238 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1239 K512[j] + W512[j]; \
1240 (d) += T1, \
1241 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
1242 j++
1243
1244
1245 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1246
1247 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
1248 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1249 K512[j] + (W512[j] = *data++); \
1250 (d) += T1; \
1251 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1252 j++
1253
1254 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1255
1256 #define ROUND512(a,b,c,d,e,f,g,h) \
1257 s0 = W512[(j+1)&0x0f]; \
1258 s0 = sigma0_512(s0); \
1259 s1 = W512[(j+14)&0x0f]; \
1260 s1 = sigma1_512(s1); \
1261 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
1262 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
1263 (d) += T1; \
1264 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1265 j++
1266
1267 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1268 sha_word64 a, b, c, d, e, f, g, h, s0, s1;
1269 sha_word64 T1, *W512 = (sha_word64*)context->s512.buffer;
1270 int j;
1271
1272 /* Initialize registers with the prev. intermediate value */
1273 a = context->s512.state[0];
1274 b = context->s512.state[1];
1275 c = context->s512.state[2];
1276 d = context->s512.state[3];
1277 e = context->s512.state[4];
1278 f = context->s512.state[5];
1279 g = context->s512.state[6];
1280 h = context->s512.state[7];
1281
1282 j = 0;
1283 do {
1284 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
1285 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
1286 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
1287 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
1288 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
1289 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
1290 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
1291 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
1292 } while (j < 16);
1293
1294 /* Now for the remaining rounds up to 79: */
1295 do {
1296 ROUND512(a,b,c,d,e,f,g,h);
1297 ROUND512(h,a,b,c,d,e,f,g);
1298 ROUND512(g,h,a,b,c,d,e,f);
1299 ROUND512(f,g,h,a,b,c,d,e);
1300 ROUND512(e,f,g,h,a,b,c,d);
1301 ROUND512(d,e,f,g,h,a,b,c);
1302 ROUND512(c,d,e,f,g,h,a,b);
1303 ROUND512(b,c,d,e,f,g,h,a);
1304 } while (j < 80);
1305
1306 /* Compute the current intermediate hash value */
1307 context->s512.state[0] += a;
1308 context->s512.state[1] += b;
1309 context->s512.state[2] += c;
1310 context->s512.state[3] += d;
1311 context->s512.state[4] += e;
1312 context->s512.state[5] += f;
1313 context->s512.state[6] += g;
1314 context->s512.state[7] += h;
1315
1316 /* Clean up */
1317 a = b = c = d = e = f = g = h = T1 = 0;
1318 }
1319
1320 #else /* SHA2_UNROLL_TRANSFORM */
1321
1322 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1323 sha_word64 a, b, c, d, e, f, g, h, s0, s1;
1324 sha_word64 T1, T2, *W512 = (sha_word64*)context->s512.buffer;
1325 int j;
1326
1327 /* Initialize registers with the prev. intermediate value */
1328 a = context->s512.state[0];
1329 b = context->s512.state[1];
1330 c = context->s512.state[2];
1331 d = context->s512.state[3];
1332 e = context->s512.state[4];
1333 f = context->s512.state[5];
1334 g = context->s512.state[6];
1335 h = context->s512.state[7];
1336
1337 j = 0;
1338 do {
1339 #if BYTE_ORDER == LITTLE_ENDIAN
1340 /* Convert TO host byte order */
1341 REVERSE64(*data++, W512[j]);
1342 /* Apply the SHA-512 compression function to update a..h */
1343 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
1344 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1345 /* Apply the SHA-512 compression function to update a..h with copy */
1346 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
1347 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1348 T2 = Sigma0_512(a) + Maj(a, b, c);
1349 h = g;
1350 g = f;
1351 f = e;
1352 e = d + T1;
1353 d = c;
1354 c = b;
1355 b = a;
1356 a = T1 + T2;
1357
1358 j++;
1359 } while (j < 16);
1360
1361 do {
1362 /* Part of the message block expansion: */
1363 s0 = W512[(j+1)&0x0f];
1364 s0 = sigma0_512(s0);
1365 s1 = W512[(j+14)&0x0f];
1366 s1 = sigma1_512(s1);
1367
1368 /* Apply the SHA-512 compression function to update a..h */
1369 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
1370 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
1371 T2 = Sigma0_512(a) + Maj(a, b, c);
1372 h = g;
1373 g = f;
1374 f = e;
1375 e = d + T1;
1376 d = c;
1377 c = b;
1378 b = a;
1379 a = T1 + T2;
1380
1381 j++;
1382 } while (j < 80);
1383
1384 /* Compute the current intermediate hash value */
1385 context->s512.state[0] += a;
1386 context->s512.state[1] += b;
1387 context->s512.state[2] += c;
1388 context->s512.state[3] += d;
1389 context->s512.state[4] += e;
1390 context->s512.state[5] += f;
1391 context->s512.state[6] += g;
1392 context->s512.state[7] += h;
1393
1394 /* Clean up */
1395 a = b = c = d = e = f = g = h = T1 = T2 = 0;
1396 }
1397
1398 #endif /* SHA2_UNROLL_TRANSFORM */
1399
1400 void SHA512_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1401 unsigned int freespace, usedspace;
1402
1403 if (len == 0) {
1404 /* Calling with no data is valid - we do nothing */
1405 return;
1406 }
1407
1408 /* Sanity check: */
1409 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
1410
1411 usedspace = (context->s512.bitcount[0] >> 3) % 128;
1412 if (usedspace > 0) {
1413 /* Calculate how much free space is available in the buffer */
1414 freespace = 128 - usedspace;
1415
1416 if (len >= freespace) {
1417 /* Fill the buffer completely and process it */
1418 MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, freespace);
1419 ADDINC128(context->s512.bitcount, freespace << 3);
1420 len -= freespace;
1421 data += freespace;
1422 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1423 } else {
1424 /* The buffer is not yet full */
1425 MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, len);
1426 ADDINC128(context->s512.bitcount, len << 3);
1427 /* Clean up: */
1428 usedspace = freespace = 0;
1429 return;
1430 }
1431 }
1432 while (len >= 128) {
1433 /* Process as many complete blocks as we can */
1434 SHA512_Internal_Transform(context, (sha_word64*)data);
1435 ADDINC128(context->s512.bitcount, 1024);
1436 len -= 128;
1437 data += 128;
1438 }
1439 if (len > 0) {
1440 /* There's left-overs, so save 'em */
1441 MEMCPY_BCOPY(context->s512.buffer, data, len);
1442 ADDINC128(context->s512.bitcount, len << 3);
1443 }
1444 /* Clean up: */
1445 usedspace = freespace = 0;
1446 }
1447
1448 void SHA512_Internal_Last(SHA_CTX* context) {
1449 unsigned int usedspace;
1450
1451 usedspace = (context->s512.bitcount[0] >> 3) % 128;
1452 if (usedspace > 0) {
1453 /* Begin padding with a 1 bit: */
1454 context->s512.buffer[usedspace++] = 0x80;
1455
1456 if (usedspace <= 112) {
1457 /* Set-up for the last transform: */
1458 MEMSET_BZERO(&context->s512.buffer[usedspace], 112 - usedspace);
1459 } else {
1460 if (usedspace < 128) {
1461 MEMSET_BZERO(&context->s512.buffer[usedspace], 128 - usedspace);
1462 }
1463 /* Do second-to-last transform: */
1464 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1465
1466 /* And set-up for the last transform: */
1467 MEMSET_BZERO(context->s512.buffer, 112);
1468 }
1469 /* Clean up: */
1470 usedspace = 0;
1471 } else {
1472 /* Prepare for final transform: */
1473 MEMSET_BZERO(context->s512.buffer, 112);
1474
1475 /* Begin padding with a 1 bit: */
1476 *context->s512.buffer = 0x80;
1477 }
1478 /* Store the length of input data (in bits): */
1479 /* [SK] see the above comment in SHA1_Final */
1480 sha_byte *buf112 = &context->s512.buffer[112];
1481 buf112[7] = context->s512.bitcount[1] & 0xff;
1482 buf112[6] = (context->s512.bitcount[1] >> 8) & 0xff;
1483 buf112[5] = (context->s512.bitcount[1] >> 16) & 0xff;
1484 buf112[4] = (context->s512.bitcount[1] >> 24) & 0xff;
1485 buf112[3] = (context->s512.bitcount[1] >> 32) & 0xff;
1486 buf112[2] = (context->s512.bitcount[1] >> 40) & 0xff;
1487 buf112[1] = (context->s512.bitcount[1] >> 48) & 0xff;
1488 buf112[0] = (context->s512.bitcount[1] >> 56) & 0xff;
1489 sha_byte *buf120 = &context->s512.buffer[120];
1490 buf120[7] = context->s512.bitcount[0] & 0xff;
1491 buf120[6] = (context->s512.bitcount[0] >> 8) & 0xff;
1492 buf120[5] = (context->s512.bitcount[0] >> 16) & 0xff;
1493 buf120[4] = (context->s512.bitcount[0] >> 24) & 0xff;
1494 buf120[3] = (context->s512.bitcount[0] >> 32) & 0xff;
1495 buf120[2] = (context->s512.bitcount[0] >> 40) & 0xff;
1496 buf120[1] = (context->s512.bitcount[0] >> 48) & 0xff;
1497 buf120[0] = (context->s512.bitcount[0] >> 56) & 0xff;
1498
1499 /* Final transform: */
1500 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1501 }
1502
1503 void SHA512_Final(sha_byte digest[SHA512_DIGEST_LENGTH], SHA_CTX* context) {
1504 sha_word64 *d = (sha_word64*)digest;
1505
1506 /* Sanity check: */
1507 assert(context != (SHA_CTX*)0);
1508
1509 /* If no digest buffer is passed, we don't bother doing this: */
1510 if (digest != (sha_byte*)0) {
1511 SHA512_Internal_Last(context);
1512
1513 /* Save the hash data for output: */
1514 #if BYTE_ORDER == LITTLE_ENDIAN
1515 {
1516 /* Convert TO host byte order */
1517 int j;
1518 for (j = 0; j < (SHA512_DIGEST_LENGTH >> 3); j++) {
1519 REVERSE64(context->s512.state[j],context->s512.state[j]);
1520 *d++ = context->s512.state[j];
1521 }
1522 }
1523 #else
1524 MEMCPY_BCOPY(d, context->s512.state, SHA512_DIGEST_LENGTH);
1525 #endif
1526 }
1527
1528 /* Zero out state data */
1529 MEMSET_BZERO(context, sizeof(*context));
1530 }
1531
1532 char *SHA512_End(SHA_CTX* context, char buffer[SHA512_DIGEST_STRING_LENGTH]) {
1533 sha_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
1534 int i;
1535
1536 /* Sanity check: */
1537 assert(context != (SHA_CTX*)0);
1538
1539 if (buffer != (char*)0) {
1540 SHA512_Final(digest, context);
1541
1542 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
1543 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1544 *buffer++ = sha_hex_digits[*d & 0x0f];
1545 d++;
1546 }
1547 *buffer = (char)0;
1548 } else {
1549 MEMSET_BZERO(context, sizeof(*context));
1550 }
1551 MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
1552 return buffer;
1553 }
1554
1555 char* SHA512_Data(const sha_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
1556 SHA_CTX context;
1557
1558 SHA512_Init(&context);
1559 SHA512_Update(&context, data, len);
1560 return SHA512_End(&context, digest);
1561 }
1562
1563
1564 /*** SHA-384: *********************************************************/
1565 void SHA384_Init(SHA_CTX* context) {
1566 SHA512_Internal_Init(context, sha384_initial_hash_value);
1567 }
1568
1569 void SHA384_Update(SHA_CTX* context, const sha_byte* data, size_t len) {
1570 SHA512_Update(context, data, len);
1571 }
1572
1573 void SHA384_Final(sha_byte digest[SHA384_DIGEST_LENGTH], SHA_CTX* context) {
1574 sha_word64 *d = (sha_word64*)digest;
1575
1576 /* Sanity check: */
1577 assert(context != (SHA_CTX*)0);
1578
1579 /* If no digest buffer is passed, we don't bother doing this: */
1580 if (digest != (sha_byte*)0) {
1581 SHA512_Internal_Last(context);
1582
1583 /* Save the hash data for output: */
1584 #if BYTE_ORDER == LITTLE_ENDIAN
1585 {
1586 /* Convert TO host byte order */
1587 int j;
1588 for (j = 0; j < (SHA384_DIGEST_LENGTH >> 3); j++) {
1589 REVERSE64(context->s512.state[j],context->s512.state[j]);
1590 *d++ = context->s512.state[j];
1591 }
1592 }
1593 #else
1594 MEMCPY_BCOPY(d, context->s512.state, SHA384_DIGEST_LENGTH);
1595 #endif
1596 }
1597
1598 /* Zero out state data */
1599 MEMSET_BZERO(context, sizeof(*context));
1600 }
1601
1602 char *SHA384_End(SHA_CTX* context, char buffer[SHA384_DIGEST_STRING_LENGTH]) {
1603 sha_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
1604 int i;
1605
1606 /* Sanity check: */
1607 assert(context != (SHA_CTX*)0);
1608
1609 if (buffer != (char*)0) {
1610 SHA384_Final(digest, context);
1611
1612 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1613 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1614 *buffer++ = sha_hex_digits[*d & 0x0f];
1615 d++;
1616 }
1617 *buffer = (char)0;
1618 } else {
1619 MEMSET_BZERO(context, sizeof(*context));
1620 }
1621 MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1622 return buffer;
1623 }
1624
1625 char* SHA384_Data(const sha_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1626 SHA_CTX context;
1627
1628 SHA384_Init(&context);
1629 SHA384_Update(&context, data, len);
1630 return SHA384_End(&context, digest);
1631 }