packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cm_sha2.c
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 "cm_sha2.h"    /* "sha2.h" -> "cm_sha2.h" renamed for CMake */
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  * equivilent.
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 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
90 /* CMake modification: use byte order from cmIML.  */
91 # include "cmIML/ABI.h"
92 # undef BYTE_ORDER
93 # undef BIG_ENDIAN
94 # undef LITTLE_ENDIAN
95 # define BYTE_ORDER    cmIML_ABI_ENDIAN_ID
96 # define BIG_ENDIAN    cmIML_ABI_ENDIAN_ID_BIG
97 # define LITTLE_ENDIAN cmIML_ABI_ENDIAN_ID_LITTLE
98 #endif
99
100 /* CMake modification: use types computed in header.  */
101 typedef cm_sha2_uint8_t  sha_byte;      /* Exactly 1 byte */
102 typedef cm_sha2_uint32_t sha_word32;    /* Exactly 4 bytes */
103 typedef cm_sha2_uint64_t sha_word64;    /* Exactly 8 bytes */
104 #define SHA_UINT32_C(x) cmIML_INT_UINT32_C(x)
105 #define SHA_UINT64_C(x) cmIML_INT_UINT64_C(x)
106 #if defined(__BORLANDC__)
107 # pragma warn -8004 /* variable assigned value that is never used */
108 #endif
109 #if defined(__clang__)
110 # pragma clang diagnostic ignored "-Wcast-align"
111 #endif
112
113 /*** ENDIAN REVERSAL MACROS *******************************************/
114 #if BYTE_ORDER == LITTLE_ENDIAN
115 #define REVERSE32(w,x)  { \
116         sha_word32 tmp = (w); \
117         tmp = (tmp >> 16) | (tmp << 16); \
118         (x) = ((tmp & SHA_UINT32_C(0xff00ff00)) >> 8) | \
119               ((tmp & SHA_UINT32_C(0x00ff00ff)) << 8); \
120 }
121 #define REVERSE64(w,x)  { \
122         sha_word64 tmp = (w); \
123         tmp = (tmp >> 32) | (tmp << 32); \
124         tmp = ((tmp & SHA_UINT64_C(0xff00ff00ff00ff00)) >> 8) | \
125               ((tmp & SHA_UINT64_C(0x00ff00ff00ff00ff)) << 8); \
126         (x) = ((tmp & SHA_UINT64_C(0xffff0000ffff0000)) >> 16) | \
127               ((tmp & SHA_UINT64_C(0x0000ffff0000ffff)) << 16); \
128 }
129 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
130
131 /*
132  * Macro for incrementally adding the unsigned 64-bit integer n to the
133  * unsigned 128-bit integer (represented using a two-element array of
134  * 64-bit words):
135  */
136 #define ADDINC128(w,n)  { \
137         (w)[0] += (sha_word64)(n); \
138         if ((w)[0] < (n)) { \
139                 (w)[1]++; \
140         } \
141 }
142
143 /*
144  * Macros for copying blocks of memory and for zeroing out ranges
145  * of memory.  Using these macros makes it easy to switch from
146  * using memset()/memcpy() and using bzero()/bcopy().
147  *
148  * Please define either SHA2_USE_MEMSET_MEMCPY or define
149  * SHA2_USE_BZERO_BCOPY depending on which function set you
150  * choose to use:
151  */
152 #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
153 /* Default to memset()/memcpy() if no option is specified */
154 #define SHA2_USE_MEMSET_MEMCPY  1
155 #endif
156 #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
157 /* Abort with an error if BOTH options are defined */
158 #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
159 #endif
160
161 #ifdef SHA2_USE_MEMSET_MEMCPY
162 #define MEMSET_BZERO(p,l)       memset((p), 0, (l))
163 #define MEMCPY_BCOPY(d,s,l)     memcpy((d), (s), (l))
164 #endif
165 #ifdef SHA2_USE_BZERO_BCOPY
166 #define MEMSET_BZERO(p,l)       bzero((p), (l))
167 #define MEMCPY_BCOPY(d,s,l)     bcopy((s), (d), (l))
168 #endif
169
170
171 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
172 /*
173  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
174  *
175  *   NOTE:  In the original SHA-256/384/512 document, the shift-right
176  *   function was named R and the rotate-right function was called S.
177  *   (See: http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf on the
178  *   web.)
179  *
180  *   The newer NIST FIPS 180-2 document uses a much clearer naming
181  *   scheme, SHR for shift-right, ROTR for rotate-right, and ROTL for
182  *   rotate-left.  (See:
183  *   http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
184  *   on the web.)
185  *
186  *   WARNING: These macros must be used cautiously, since they reference
187  *   supplied parameters sometimes more than once, and thus could have
188  *   unexpected side-effects if used without taking this into account.
189  */
190 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
191 #define SHR(b,x)                ((x) >> (b))
192 /* 32-bit Rotate-right (used in SHA-256): */
193 #define ROTR32(b,x)     (((x) >> (b)) | ((x) << (32 - (b))))
194 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
195 #define ROTR64(b,x)     (((x) >> (b)) | ((x) << (64 - (b))))
196 /* 32-bit Rotate-left (used in SHA-1): */
197 #define ROTL32(b,x)     (((x) << (b)) | ((x) >> (32 - (b))))
198
199 /* Two logical functions used in SHA-1, SHA-254, SHA-256, SHA-384, and SHA-512: */
200 #define Ch(x,y,z)       (((x) & (y)) ^ ((~(x)) & (z)))
201 #define Maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
202
203 /* Function used in SHA-1: */
204 #define Parity(x,y,z)   ((x) ^ (y) ^ (z))
205
206 /* Four logical functions used in SHA-256: */
207 #define Sigma0_256(x)   (ROTR32(2,  (x)) ^ ROTR32(13, (x)) ^ ROTR32(22, (x)))
208 #define Sigma1_256(x)   (ROTR32(6,  (x)) ^ ROTR32(11, (x)) ^ ROTR32(25, (x)))
209 #define sigma0_256(x)   (ROTR32(7,  (x)) ^ ROTR32(18, (x)) ^ SHR(   3 , (x)))
210 #define sigma1_256(x)   (ROTR32(17, (x)) ^ ROTR32(19, (x)) ^ SHR(   10, (x)))
211
212 /* Four of six logical functions used in SHA-384 and SHA-512: */
213 #define Sigma0_512(x)   (ROTR64(28, (x)) ^ ROTR64(34, (x)) ^ ROTR64(39, (x)))
214 #define Sigma1_512(x)   (ROTR64(14, (x)) ^ ROTR64(18, (x)) ^ ROTR64(41, (x)))
215 #define sigma0_512(x)   (ROTR64( 1, (x)) ^ ROTR64( 8, (x)) ^ SHR(    7, (x)))
216 #define sigma1_512(x)   (ROTR64(19, (x)) ^ ROTR64(61, (x)) ^ SHR(    6, (x)))
217
218 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
219
220 /* SHA-224 and SHA-256: */
221 void SHA256_Internal_Init(SHA_CTX*, const sha_word32*);
222 void SHA256_Internal_Last(SHA_CTX*);
223 void SHA256_Internal_Transform(SHA_CTX*, const sha_word32*);
224
225 /* SHA-384 and SHA-512: */
226 void SHA512_Internal_Init(SHA_CTX*, const sha_word64*);
227 void SHA512_Internal_Last(SHA_CTX*);
228 void SHA512_Internal_Transform(SHA_CTX*, const sha_word64*);
229
230
231 /*** SHA2 INITIAL HASH VALUES AND CONSTANTS ***************************/
232
233 /* Hash constant words K for SHA-1: */
234 #define K1_0_TO_19      SHA_UINT32_C(0x5a827999)
235 #define K1_20_TO_39     SHA_UINT32_C(0x6ed9eba1)
236 #define K1_40_TO_59     SHA_UINT32_C(0x8f1bbcdc)
237 #define K1_60_TO_79     SHA_UINT32_C(0xca62c1d6)
238
239 /* Initial hash value H for SHA-1: */
240 static const sha_word32 sha1_initial_hash_value[5] = {
241         SHA_UINT32_C(0x67452301),
242         SHA_UINT32_C(0xefcdab89),
243         SHA_UINT32_C(0x98badcfe),
244         SHA_UINT32_C(0x10325476),
245         SHA_UINT32_C(0xc3d2e1f0)
246 };
247
248 /* Hash constant words K for SHA-224 and SHA-256: */
249 static const sha_word32 K256[64] = {
250         SHA_UINT32_C(0x428a2f98), SHA_UINT32_C(0x71374491),
251         SHA_UINT32_C(0xb5c0fbcf), SHA_UINT32_C(0xe9b5dba5),
252         SHA_UINT32_C(0x3956c25b), SHA_UINT32_C(0x59f111f1),
253         SHA_UINT32_C(0x923f82a4), SHA_UINT32_C(0xab1c5ed5),
254         SHA_UINT32_C(0xd807aa98), SHA_UINT32_C(0x12835b01),
255         SHA_UINT32_C(0x243185be), SHA_UINT32_C(0x550c7dc3),
256         SHA_UINT32_C(0x72be5d74), SHA_UINT32_C(0x80deb1fe),
257         SHA_UINT32_C(0x9bdc06a7), SHA_UINT32_C(0xc19bf174),
258         SHA_UINT32_C(0xe49b69c1), SHA_UINT32_C(0xefbe4786),
259         SHA_UINT32_C(0x0fc19dc6), SHA_UINT32_C(0x240ca1cc),
260         SHA_UINT32_C(0x2de92c6f), SHA_UINT32_C(0x4a7484aa),
261         SHA_UINT32_C(0x5cb0a9dc), SHA_UINT32_C(0x76f988da),
262         SHA_UINT32_C(0x983e5152), SHA_UINT32_C(0xa831c66d),
263         SHA_UINT32_C(0xb00327c8), SHA_UINT32_C(0xbf597fc7),
264         SHA_UINT32_C(0xc6e00bf3), SHA_UINT32_C(0xd5a79147),
265         SHA_UINT32_C(0x06ca6351), SHA_UINT32_C(0x14292967),
266         SHA_UINT32_C(0x27b70a85), SHA_UINT32_C(0x2e1b2138),
267         SHA_UINT32_C(0x4d2c6dfc), SHA_UINT32_C(0x53380d13),
268         SHA_UINT32_C(0x650a7354), SHA_UINT32_C(0x766a0abb),
269         SHA_UINT32_C(0x81c2c92e), SHA_UINT32_C(0x92722c85),
270         SHA_UINT32_C(0xa2bfe8a1), SHA_UINT32_C(0xa81a664b),
271         SHA_UINT32_C(0xc24b8b70), SHA_UINT32_C(0xc76c51a3),
272         SHA_UINT32_C(0xd192e819), SHA_UINT32_C(0xd6990624),
273         SHA_UINT32_C(0xf40e3585), SHA_UINT32_C(0x106aa070),
274         SHA_UINT32_C(0x19a4c116), SHA_UINT32_C(0x1e376c08),
275         SHA_UINT32_C(0x2748774c), SHA_UINT32_C(0x34b0bcb5),
276         SHA_UINT32_C(0x391c0cb3), SHA_UINT32_C(0x4ed8aa4a),
277         SHA_UINT32_C(0x5b9cca4f), SHA_UINT32_C(0x682e6ff3),
278         SHA_UINT32_C(0x748f82ee), SHA_UINT32_C(0x78a5636f),
279         SHA_UINT32_C(0x84c87814), SHA_UINT32_C(0x8cc70208),
280         SHA_UINT32_C(0x90befffa), SHA_UINT32_C(0xa4506ceb),
281         SHA_UINT32_C(0xbef9a3f7), SHA_UINT32_C(0xc67178f2)
282 };
283
284 /* Initial hash value H for SHA-224: */
285 static const sha_word32 sha224_initial_hash_value[8] = {
286         SHA_UINT32_C(0xc1059ed8),
287         SHA_UINT32_C(0x367cd507),
288         SHA_UINT32_C(0x3070dd17),
289         SHA_UINT32_C(0xf70e5939),
290         SHA_UINT32_C(0xffc00b31),
291         SHA_UINT32_C(0x68581511),
292         SHA_UINT32_C(0x64f98fa7),
293         SHA_UINT32_C(0xbefa4fa4)
294 };
295
296 /* Initial hash value H for SHA-256: */
297 static const sha_word32 sha256_initial_hash_value[8] = {
298         SHA_UINT32_C(0x6a09e667),
299         SHA_UINT32_C(0xbb67ae85),
300         SHA_UINT32_C(0x3c6ef372),
301         SHA_UINT32_C(0xa54ff53a),
302         SHA_UINT32_C(0x510e527f),
303         SHA_UINT32_C(0x9b05688c),
304         SHA_UINT32_C(0x1f83d9ab),
305         SHA_UINT32_C(0x5be0cd19)
306 };
307
308 /* Hash constant words K for SHA-384 and SHA-512: */
309 static const sha_word64 K512[80] = {
310         SHA_UINT64_C(0x428a2f98d728ae22), SHA_UINT64_C(0x7137449123ef65cd),
311         SHA_UINT64_C(0xb5c0fbcfec4d3b2f), SHA_UINT64_C(0xe9b5dba58189dbbc),
312         SHA_UINT64_C(0x3956c25bf348b538), SHA_UINT64_C(0x59f111f1b605d019),
313         SHA_UINT64_C(0x923f82a4af194f9b), SHA_UINT64_C(0xab1c5ed5da6d8118),
314         SHA_UINT64_C(0xd807aa98a3030242), SHA_UINT64_C(0x12835b0145706fbe),
315         SHA_UINT64_C(0x243185be4ee4b28c), SHA_UINT64_C(0x550c7dc3d5ffb4e2),
316         SHA_UINT64_C(0x72be5d74f27b896f), SHA_UINT64_C(0x80deb1fe3b1696b1),
317         SHA_UINT64_C(0x9bdc06a725c71235), SHA_UINT64_C(0xc19bf174cf692694),
318         SHA_UINT64_C(0xe49b69c19ef14ad2), SHA_UINT64_C(0xefbe4786384f25e3),
319         SHA_UINT64_C(0x0fc19dc68b8cd5b5), SHA_UINT64_C(0x240ca1cc77ac9c65),
320         SHA_UINT64_C(0x2de92c6f592b0275), SHA_UINT64_C(0x4a7484aa6ea6e483),
321         SHA_UINT64_C(0x5cb0a9dcbd41fbd4), SHA_UINT64_C(0x76f988da831153b5),
322         SHA_UINT64_C(0x983e5152ee66dfab), SHA_UINT64_C(0xa831c66d2db43210),
323         SHA_UINT64_C(0xb00327c898fb213f), SHA_UINT64_C(0xbf597fc7beef0ee4),
324         SHA_UINT64_C(0xc6e00bf33da88fc2), SHA_UINT64_C(0xd5a79147930aa725),
325         SHA_UINT64_C(0x06ca6351e003826f), SHA_UINT64_C(0x142929670a0e6e70),
326         SHA_UINT64_C(0x27b70a8546d22ffc), SHA_UINT64_C(0x2e1b21385c26c926),
327         SHA_UINT64_C(0x4d2c6dfc5ac42aed), SHA_UINT64_C(0x53380d139d95b3df),
328         SHA_UINT64_C(0x650a73548baf63de), SHA_UINT64_C(0x766a0abb3c77b2a8),
329         SHA_UINT64_C(0x81c2c92e47edaee6), SHA_UINT64_C(0x92722c851482353b),
330         SHA_UINT64_C(0xa2bfe8a14cf10364), SHA_UINT64_C(0xa81a664bbc423001),
331         SHA_UINT64_C(0xc24b8b70d0f89791), SHA_UINT64_C(0xc76c51a30654be30),
332         SHA_UINT64_C(0xd192e819d6ef5218), SHA_UINT64_C(0xd69906245565a910),
333         SHA_UINT64_C(0xf40e35855771202a), SHA_UINT64_C(0x106aa07032bbd1b8),
334         SHA_UINT64_C(0x19a4c116b8d2d0c8), SHA_UINT64_C(0x1e376c085141ab53),
335         SHA_UINT64_C(0x2748774cdf8eeb99), SHA_UINT64_C(0x34b0bcb5e19b48a8),
336         SHA_UINT64_C(0x391c0cb3c5c95a63), SHA_UINT64_C(0x4ed8aa4ae3418acb),
337         SHA_UINT64_C(0x5b9cca4f7763e373), SHA_UINT64_C(0x682e6ff3d6b2b8a3),
338         SHA_UINT64_C(0x748f82ee5defb2fc), SHA_UINT64_C(0x78a5636f43172f60),
339         SHA_UINT64_C(0x84c87814a1f0ab72), SHA_UINT64_C(0x8cc702081a6439ec),
340         SHA_UINT64_C(0x90befffa23631e28), SHA_UINT64_C(0xa4506cebde82bde9),
341         SHA_UINT64_C(0xbef9a3f7b2c67915), SHA_UINT64_C(0xc67178f2e372532b),
342         SHA_UINT64_C(0xca273eceea26619c), SHA_UINT64_C(0xd186b8c721c0c207),
343         SHA_UINT64_C(0xeada7dd6cde0eb1e), SHA_UINT64_C(0xf57d4f7fee6ed178),
344         SHA_UINT64_C(0x06f067aa72176fba), SHA_UINT64_C(0x0a637dc5a2c898a6),
345         SHA_UINT64_C(0x113f9804bef90dae), SHA_UINT64_C(0x1b710b35131c471b),
346         SHA_UINT64_C(0x28db77f523047d84), SHA_UINT64_C(0x32caab7b40c72493),
347         SHA_UINT64_C(0x3c9ebe0a15c9bebc), SHA_UINT64_C(0x431d67c49c100d4c),
348         SHA_UINT64_C(0x4cc5d4becb3e42b6), SHA_UINT64_C(0x597f299cfc657e2a),
349         SHA_UINT64_C(0x5fcb6fab3ad6faec), SHA_UINT64_C(0x6c44198c4a475817)
350 };
351
352 /* Initial hash value H for SHA-384 */
353 static const sha_word64 sha384_initial_hash_value[8] = {
354         SHA_UINT64_C(0xcbbb9d5dc1059ed8),
355         SHA_UINT64_C(0x629a292a367cd507),
356         SHA_UINT64_C(0x9159015a3070dd17),
357         SHA_UINT64_C(0x152fecd8f70e5939),
358         SHA_UINT64_C(0x67332667ffc00b31),
359         SHA_UINT64_C(0x8eb44a8768581511),
360         SHA_UINT64_C(0xdb0c2e0d64f98fa7),
361         SHA_UINT64_C(0x47b5481dbefa4fa4)
362 };
363
364 /* Initial hash value H for SHA-512 */
365 static const sha_word64 sha512_initial_hash_value[8] = {
366         SHA_UINT64_C(0x6a09e667f3bcc908),
367         SHA_UINT64_C(0xbb67ae8584caa73b),
368         SHA_UINT64_C(0x3c6ef372fe94f82b),
369         SHA_UINT64_C(0xa54ff53a5f1d36f1),
370         SHA_UINT64_C(0x510e527fade682d1),
371         SHA_UINT64_C(0x9b05688c2b3e6c1f),
372         SHA_UINT64_C(0x1f83d9abfb41bd6b),
373         SHA_UINT64_C(0x5be0cd19137e2179)
374 };
375
376 /*
377  * Constant used by SHA224/256/384/512_End() functions for converting the
378  * digest to a readable hexadecimal character string:
379  */
380 static const char *sha_hex_digits = "0123456789abcdef";
381
382
383 /*** SHA-1: ***********************************************************/
384 void SHA1_Init(SHA_CTX* context) {
385         /* Sanity check: */
386         assert(context != (SHA_CTX*)0);
387
388         MEMCPY_BCOPY(context->s1.state, sha1_initial_hash_value, sizeof(sha_word32) * 5);
389         MEMSET_BZERO(context->s1.buffer, 64);
390         context->s1.bitcount = 0;
391 }
392
393 #ifdef SHA2_UNROLL_TRANSFORM
394
395 /* Unrolled SHA-1 round macros: */
396
397 #if BYTE_ORDER == LITTLE_ENDIAN
398
399 #define ROUND1_0_TO_15(a,b,c,d,e)                               \
400         REVERSE32(*data++, W1[j]);                              \
401         (e) = ROTL32(5, (a)) + Ch((b), (c), (d)) + (e) +        \
402              K1_0_TO_19 + W1[j];        \
403         (b) = ROTL32(30, (b));          \
404         j++;
405
406 #else /* BYTE_ORDER == LITTLE_ENDIAN */
407
408 #define ROUND1_0_TO_15(a,b,c,d,e)                               \
409         (e) = ROTL32(5, (a)) + Ch((b), (c), (d)) + (e) +        \
410              K1_0_TO_19 + ( W1[j] = *data++ );          \
411         (b) = ROTL32(30, (b));  \
412         j++;
413
414 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
415
416 #define ROUND1_16_TO_19(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) + Ch(b,c,d) + e + K1_0_TO_19 + ( W1[j&0x0f] = ROTL32(1, T1) );       \
419         (b) = ROTL32(30, b);    \
420         j++;
421
422 #define ROUND1_20_TO_39(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) + Parity(b,c,d) + e + K1_20_TO_39 + ( W1[j&0x0f] = ROTL32(1, T1) );  \
425         (b) = ROTL32(30, b);    \
426         j++;
427
428 #define ROUND1_40_TO_59(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) + Maj(b,c,d) + e + K1_40_TO_59 + ( W1[j&0x0f] = ROTL32(1, T1) );     \
431         (b) = ROTL32(30, b);    \
432         j++;
433
434 #define ROUND1_60_TO_79(a,b,c,d,e)      \
435         T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];    \
436         (e) = ROTL32(5, a) + Parity(b,c,d) + e + K1_60_TO_79 + ( W1[j&0x0f] = ROTL32(1, T1) );  \
437         (b) = ROTL32(30, b);    \
438         j++;
439
440 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
441         sha_word32      a, b, c, d, e;
442         sha_word32      T1, *W1;
443         int             j;
444
445         W1 = (sha_word32*)context->s1.buffer;
446
447         /* Initialize registers with the prev. intermediate value */
448         a = context->s1.state[0];
449         b = context->s1.state[1];
450         c = context->s1.state[2];
451         d = context->s1.state[3];
452         e = context->s1.state[4];
453
454         j = 0;
455
456         /* Rounds 0 to 15 unrolled: */
457         ROUND1_0_TO_15(a,b,c,d,e);
458         ROUND1_0_TO_15(e,a,b,c,d);
459         ROUND1_0_TO_15(d,e,a,b,c);
460         ROUND1_0_TO_15(c,d,e,a,b);
461         ROUND1_0_TO_15(b,c,d,e,a);
462         ROUND1_0_TO_15(a,b,c,d,e);
463         ROUND1_0_TO_15(e,a,b,c,d);
464         ROUND1_0_TO_15(d,e,a,b,c);
465         ROUND1_0_TO_15(c,d,e,a,b);
466         ROUND1_0_TO_15(b,c,d,e,a);
467         ROUND1_0_TO_15(a,b,c,d,e);
468         ROUND1_0_TO_15(e,a,b,c,d);
469         ROUND1_0_TO_15(d,e,a,b,c);
470         ROUND1_0_TO_15(c,d,e,a,b);
471         ROUND1_0_TO_15(b,c,d,e,a);
472         ROUND1_0_TO_15(a,b,c,d,e);
473
474         /* Rounds 16 to 19 unrolled: */
475         ROUND1_16_TO_19(e,a,b,c,d);
476         ROUND1_16_TO_19(d,e,a,b,c);
477         ROUND1_16_TO_19(c,d,e,a,b);
478         ROUND1_16_TO_19(b,c,d,e,a);
479
480         /* Rounds 20 to 39 unrolled: */
481         ROUND1_20_TO_39(a,b,c,d,e);
482         ROUND1_20_TO_39(e,a,b,c,d);
483         ROUND1_20_TO_39(d,e,a,b,c);
484         ROUND1_20_TO_39(c,d,e,a,b);
485         ROUND1_20_TO_39(b,c,d,e,a);
486         ROUND1_20_TO_39(a,b,c,d,e);
487         ROUND1_20_TO_39(e,a,b,c,d);
488         ROUND1_20_TO_39(d,e,a,b,c);
489         ROUND1_20_TO_39(c,d,e,a,b);
490         ROUND1_20_TO_39(b,c,d,e,a);
491         ROUND1_20_TO_39(a,b,c,d,e);
492         ROUND1_20_TO_39(e,a,b,c,d);
493         ROUND1_20_TO_39(d,e,a,b,c);
494         ROUND1_20_TO_39(c,d,e,a,b);
495         ROUND1_20_TO_39(b,c,d,e,a);
496         ROUND1_20_TO_39(a,b,c,d,e);
497         ROUND1_20_TO_39(e,a,b,c,d);
498         ROUND1_20_TO_39(d,e,a,b,c);
499         ROUND1_20_TO_39(c,d,e,a,b);
500         ROUND1_20_TO_39(b,c,d,e,a);
501
502         /* Rounds 40 to 59 unrolled: */
503         ROUND1_40_TO_59(a,b,c,d,e);
504         ROUND1_40_TO_59(e,a,b,c,d);
505         ROUND1_40_TO_59(d,e,a,b,c);
506         ROUND1_40_TO_59(c,d,e,a,b);
507         ROUND1_40_TO_59(b,c,d,e,a);
508         ROUND1_40_TO_59(a,b,c,d,e);
509         ROUND1_40_TO_59(e,a,b,c,d);
510         ROUND1_40_TO_59(d,e,a,b,c);
511         ROUND1_40_TO_59(c,d,e,a,b);
512         ROUND1_40_TO_59(b,c,d,e,a);
513         ROUND1_40_TO_59(a,b,c,d,e);
514         ROUND1_40_TO_59(e,a,b,c,d);
515         ROUND1_40_TO_59(d,e,a,b,c);
516         ROUND1_40_TO_59(c,d,e,a,b);
517         ROUND1_40_TO_59(b,c,d,e,a);
518         ROUND1_40_TO_59(a,b,c,d,e);
519         ROUND1_40_TO_59(e,a,b,c,d);
520         ROUND1_40_TO_59(d,e,a,b,c);
521         ROUND1_40_TO_59(c,d,e,a,b);
522         ROUND1_40_TO_59(b,c,d,e,a);
523
524         /* Rounds 60 to 79 unrolled: */
525         ROUND1_60_TO_79(a,b,c,d,e);
526         ROUND1_60_TO_79(e,a,b,c,d);
527         ROUND1_60_TO_79(d,e,a,b,c);
528         ROUND1_60_TO_79(c,d,e,a,b);
529         ROUND1_60_TO_79(b,c,d,e,a);
530         ROUND1_60_TO_79(a,b,c,d,e);
531         ROUND1_60_TO_79(e,a,b,c,d);
532         ROUND1_60_TO_79(d,e,a,b,c);
533         ROUND1_60_TO_79(c,d,e,a,b);
534         ROUND1_60_TO_79(b,c,d,e,a);
535         ROUND1_60_TO_79(a,b,c,d,e);
536         ROUND1_60_TO_79(e,a,b,c,d);
537         ROUND1_60_TO_79(d,e,a,b,c);
538         ROUND1_60_TO_79(c,d,e,a,b);
539         ROUND1_60_TO_79(b,c,d,e,a);
540         ROUND1_60_TO_79(a,b,c,d,e);
541         ROUND1_60_TO_79(e,a,b,c,d);
542         ROUND1_60_TO_79(d,e,a,b,c);
543         ROUND1_60_TO_79(c,d,e,a,b);
544         ROUND1_60_TO_79(b,c,d,e,a);
545
546         /* Compute the current intermediate hash value */
547         context->s1.state[0] += a;
548         context->s1.state[1] += b;
549         context->s1.state[2] += c;
550         context->s1.state[3] += d;
551         context->s1.state[4] += e;
552
553         /* Clean up */
554         a = b = c = d = e = T1 = 0;
555 }
556
557 #else  /* SHA2_UNROLL_TRANSFORM */
558
559 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
560         sha_word32      a, b, c, d, e;
561         sha_word32      T1, *W1;
562         int             j;
563
564         W1 = (sha_word32*)context->s1.buffer;
565
566         /* Initialize registers with the prev. intermediate value */
567         a = context->s1.state[0];
568         b = context->s1.state[1];
569         c = context->s1.state[2];
570         d = context->s1.state[3];
571         e = context->s1.state[4];
572         j = 0;
573         do {
574 #if BYTE_ORDER == LITTLE_ENDIAN
575                 T1 = data[j];
576                 /* Copy data while converting to host byte order */
577                 REVERSE32(*data++, W1[j]);
578                 T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + W1[j];
579 #else /* BYTE_ORDER == LITTLE_ENDIAN */
580                 T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + (W1[j] = *data++);
581 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
582                 e = d;
583                 d = c;
584                 c = ROTL32(30, b);
585                 b = a;
586                 a = T1;
587                 j++;
588         } while (j < 16);
589
590         do {
591                 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
592                 T1 = ROTL32(5, a) + Ch(b,c,d) + e + K1_0_TO_19 + (W1[j&0x0f] = ROTL32(1, T1));
593                 e = d;
594                 d = c;
595                 c = ROTL32(30, b);
596                 b = a;
597                 a = T1;
598                 j++;
599         } while (j < 20);
600
601         do {
602                 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
603                 T1 = ROTL32(5, a) + Parity(b,c,d) + e + K1_20_TO_39 + (W1[j&0x0f] = ROTL32(1, T1));
604                 e = d;
605                 d = c;
606                 c = ROTL32(30, b);
607                 b = a;
608                 a = T1;
609                 j++;
610         } while (j < 40);
611
612         do {
613                 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
614                 T1 = ROTL32(5, a) + Maj(b,c,d) + e + K1_40_TO_59 + (W1[j&0x0f] = ROTL32(1, T1));
615                 e = d;
616                 d = c;
617                 c = ROTL32(30, b);
618                 b = a;
619                 a = T1;
620                 j++;
621         } while (j < 60);
622
623         do {
624                 T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
625                 T1 = ROTL32(5, a) + Parity(b,c,d) + e + K1_60_TO_79 + (W1[j&0x0f] = ROTL32(1, T1));
626                 e = d;
627                 d = c;
628                 c = ROTL32(30, b);
629                 b = a;
630                 a = T1;
631                 j++;
632         } while (j < 80);
633
634
635         /* Compute the current intermediate hash value */
636         context->s1.state[0] += a;
637         context->s1.state[1] += b;
638         context->s1.state[2] += c;
639         context->s1.state[3] += d;
640         context->s1.state[4] += e;
641
642         /* Clean up */
643         a = b = c = d = e = T1 = 0;
644 }
645
646 #endif /* SHA2_UNROLL_TRANSFORM */
647
648 void SHA1_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
649         unsigned int    freespace, usedspace;
650         if (len == 0) {
651                 /* Calling with no data is valid - we do nothing */
652                 return;
653         }
654
655         /* Sanity check: */
656         assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
657
658         usedspace = (unsigned int)((context->s1.bitcount >> 3) % 64);
659         if (usedspace > 0) {
660                 /* Calculate how much free space is available in the buffer */
661                 freespace = 64 - usedspace;
662
663                 if (len >= freespace) {
664                         /* Fill the buffer completely and process it */
665                         MEMCPY_BCOPY(&context->s1.buffer[usedspace], data, freespace);
666                         context->s1.bitcount += freespace << 3;
667                         len -= freespace;
668                         data += freespace;
669                         SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
670                 } else {
671                         /* The buffer is not yet full */
672                         MEMCPY_BCOPY(&context->s1.buffer[usedspace], data, len);
673                         context->s1.bitcount += len << 3;
674                         /* Clean up: */
675                         usedspace = freespace = 0;
676                         return;
677                 }
678         }
679         while (len >= 64) {
680                 /* Process as many complete blocks as we can */
681                 SHA1_Internal_Transform(context, (sha_word32*)data);
682                 context->s1.bitcount += 512;
683                 len -= 64;
684                 data += 64;
685         }
686         if (len > 0) {
687                 /* There's left-overs, so save 'em */
688                 MEMCPY_BCOPY(context->s1.buffer, data, len);
689                 context->s1.bitcount += len << 3;
690         }
691         /* Clean up: */
692         usedspace = freespace = 0;
693 }
694
695 void SHA1_Final(sha_byte digest[], SHA_CTX* context) {
696         sha_word32      *d = (sha_word32*)digest;
697         unsigned int    usedspace;
698
699         /* Sanity check: */
700         assert(context != (SHA_CTX*)0);
701
702         if (digest == (sha_byte*)0) {
703                 /*
704                  * No digest buffer, so we can do nothing
705                  * except clean up and go home
706                  */
707                 MEMSET_BZERO(context, sizeof(*context));
708                 return;
709         }
710
711         usedspace = (unsigned int)((context->s1.bitcount >> 3) % 64);
712         if (usedspace == 0) {
713                 /* Set-up for the last transform: */
714                 MEMSET_BZERO(context->s1.buffer, 56);
715
716                 /* Begin padding with a 1 bit: */
717                 *context->s1.buffer = 0x80;
718         } else {
719                 /* Begin padding with a 1 bit: */
720                 context->s1.buffer[usedspace++] = 0x80;
721
722                 if (usedspace <= 56) {
723                         /* Set-up for the last transform: */
724                         MEMSET_BZERO(&context->s1.buffer[usedspace], 56 - usedspace);
725                 } else {
726                         if (usedspace < 64) {
727                                 MEMSET_BZERO(&context->s1.buffer[usedspace], 64 - usedspace);
728                         }
729                         /* Do second-to-last transform: */
730                         SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
731
732                         /* And set-up for the last transform: */
733                         MEMSET_BZERO(context->s1.buffer, 56);
734                 }
735                 /* Clean up: */
736                 usedspace = 0;
737         }
738         /* Set the bit count: */
739 #if BYTE_ORDER == LITTLE_ENDIAN
740         /* Convert FROM host byte order */
741         REVERSE64(context->s1.bitcount,context->s1.bitcount);
742 #endif
743         MEMCPY_BCOPY(&context->s1.buffer[56], &context->s1.bitcount,
744                      sizeof(sha_word64));
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[]) {
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 = (unsigned int)((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 = (unsigned int)((context->s256.bitcount >> 3) % 64);
1040 #if BYTE_ORDER == LITTLE_ENDIAN
1041         /* Convert FROM host byte order */
1042         REVERSE64(context->s256.bitcount,context->s256.bitcount);
1043 #endif
1044         if (usedspace > 0) {
1045                 /* Begin padding with a 1 bit: */
1046                 context->s256.buffer[usedspace++] = 0x80;
1047
1048                 if (usedspace <= 56) {
1049                         /* Set-up for the last transform: */
1050                         MEMSET_BZERO(&context->s256.buffer[usedspace], 56 - usedspace);
1051                 } else {
1052                         if (usedspace < 64) {
1053                                 MEMSET_BZERO(&context->s256.buffer[usedspace], 64 - usedspace);
1054                         }
1055                         /* Do second-to-last transform: */
1056                         SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1057
1058                         /* And set-up for the last transform: */
1059                         MEMSET_BZERO(context->s256.buffer, 56);
1060                 }
1061                 /* Clean up: */
1062                 usedspace = 0;
1063         } else {
1064                 /* Set-up for the last transform: */
1065                 MEMSET_BZERO(context->s256.buffer, 56);
1066
1067                 /* Begin padding with a 1 bit: */
1068                 *context->s256.buffer = 0x80;
1069         }
1070         /* Set the bit count: */
1071         MEMCPY_BCOPY(&context->s256.buffer[56], &context->s256.bitcount,
1072                      sizeof(sha_word64));
1073
1074         /* Final transform: */
1075         SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1076 }
1077
1078 void SHA256_Final(sha_byte digest[], SHA_CTX* context) {
1079         sha_word32      *d = (sha_word32*)digest;
1080
1081         /* Sanity check: */
1082         assert(context != (SHA_CTX*)0);
1083
1084         /* If no digest buffer is passed, we don't bother doing this: */
1085         if (digest != (sha_byte*)0) {
1086                 SHA256_Internal_Last(context);
1087
1088                 /* Save the hash data for output: */
1089 #if BYTE_ORDER == LITTLE_ENDIAN
1090                 {
1091                         /* Convert TO host byte order */
1092                         int     j;
1093                         for (j = 0; j < (SHA256_DIGEST_LENGTH >> 2); j++) {
1094                                 REVERSE32(context->s256.state[j],context->s256.state[j]);
1095                                 *d++ = context->s256.state[j];
1096                         }
1097                 }
1098 #else
1099                 MEMCPY_BCOPY(d, context->s256.state, SHA256_DIGEST_LENGTH);
1100 #endif
1101         }
1102
1103         /* Clean up state data: */
1104         MEMSET_BZERO(context, sizeof(*context));
1105 }
1106
1107 char *SHA256_End(SHA_CTX* context, char buffer[]) {
1108         sha_byte        digest[SHA256_DIGEST_LENGTH], *d = digest;
1109         int             i;
1110
1111         /* Sanity check: */
1112         assert(context != (SHA_CTX*)0);
1113
1114         if (buffer != (char*)0) {
1115                 SHA256_Final(digest, context);
1116
1117                 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1118                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1119                         *buffer++ = sha_hex_digits[*d & 0x0f];
1120                         d++;
1121                 }
1122                 *buffer = (char)0;
1123         } else {
1124                 MEMSET_BZERO(context, sizeof(*context));
1125         }
1126         MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
1127         return buffer;
1128 }
1129
1130 char* SHA256_Data(const sha_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
1131         SHA_CTX context;
1132
1133         SHA256_Init(&context);
1134         SHA256_Update(&context, data, len);
1135         return SHA256_End(&context, digest);
1136 }
1137
1138
1139 /*** SHA-224: *********************************************************/
1140 void SHA224_Init(SHA_CTX* context) {
1141         SHA256_Internal_Init(context, sha224_initial_hash_value);
1142 }
1143
1144 void SHA224_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
1145         SHA256_Internal_Transform(context, data);
1146 }
1147
1148 void SHA224_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1149         SHA256_Update(context, data, len);
1150 }
1151
1152 void SHA224_Final(sha_byte digest[], SHA_CTX* context) {
1153         sha_word32      *d = (sha_word32*)digest;
1154
1155         /* Sanity check: */
1156         assert(context != (SHA_CTX*)0);
1157
1158         /* If no digest buffer is passed, we don't bother doing this: */
1159         if (digest != (sha_byte*)0) {
1160                 SHA256_Internal_Last(context);
1161
1162                 /* Save the hash data for output: */
1163 #if BYTE_ORDER == LITTLE_ENDIAN
1164                 {
1165                         /* Convert TO host byte order */
1166                         int     j;
1167                         for (j = 0; j < (SHA224_DIGEST_LENGTH >> 2); j++) {
1168                                 REVERSE32(context->s256.state[j],context->s256.state[j]);
1169                                 *d++ = context->s256.state[j];
1170                         }
1171                 }
1172 #else
1173                 MEMCPY_BCOPY(d, context->s256.state, SHA224_DIGEST_LENGTH);
1174 #endif
1175         }
1176
1177         /* Clean up state data: */
1178         MEMSET_BZERO(context, sizeof(*context));
1179 }
1180
1181 char *SHA224_End(SHA_CTX* context, char buffer[]) {
1182         sha_byte        digest[SHA224_DIGEST_LENGTH], *d = digest;
1183         int             i;
1184
1185         /* Sanity check: */
1186         assert(context != (SHA_CTX*)0);
1187
1188         if (buffer != (char*)0) {
1189                 SHA224_Final(digest, context);
1190
1191                 for (i = 0; i < SHA224_DIGEST_LENGTH; i++) {
1192                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1193                         *buffer++ = sha_hex_digits[*d & 0x0f];
1194                         d++;
1195                 }
1196                 *buffer = (char)0;
1197         } else {
1198                 MEMSET_BZERO(context, sizeof(*context));
1199         }
1200         MEMSET_BZERO(digest, SHA224_DIGEST_LENGTH);
1201         return buffer;
1202 }
1203
1204 char* SHA224_Data(const sha_byte* data, size_t len, char digest[SHA224_DIGEST_STRING_LENGTH]) {
1205         SHA_CTX context;
1206
1207         SHA224_Init(&context);
1208         SHA224_Update(&context, data, len);
1209         return SHA224_End(&context, digest);
1210 }
1211
1212
1213 /*** SHA-512: *********************************************************/
1214 void SHA512_Internal_Init(SHA_CTX* context, const sha_word64* ihv) {
1215         /* Sanity check: */
1216         assert(context != (SHA_CTX*)0);
1217
1218         MEMCPY_BCOPY(context->s512.state, ihv, sizeof(sha_word64) * 8);
1219         MEMSET_BZERO(context->s512.buffer, 128);
1220         context->s512.bitcount[0] = context->s512.bitcount[1] =  0;
1221 }
1222
1223 void SHA512_Init(SHA_CTX* context) {
1224         SHA512_Internal_Init(context, sha512_initial_hash_value);
1225 }
1226
1227 #ifdef SHA2_UNROLL_TRANSFORM
1228
1229 /* Unrolled SHA-512 round macros: */
1230 #if BYTE_ORDER == LITTLE_ENDIAN
1231
1232 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
1233         REVERSE64(*data++, W512[j]); \
1234         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1235              K512[j] + W512[j]; \
1236         (d) += T1, \
1237         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
1238         j++
1239
1240
1241 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1242
1243 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
1244         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1245              K512[j] + (W512[j] = *data++); \
1246         (d) += T1; \
1247         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1248         j++
1249
1250 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1251
1252 #define ROUND512(a,b,c,d,e,f,g,h)       \
1253         s0 = W512[(j+1)&0x0f]; \
1254         s0 = sigma0_512(s0); \
1255         s1 = W512[(j+14)&0x0f]; \
1256         s1 = sigma1_512(s1); \
1257         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
1258              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
1259         (d) += T1; \
1260         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1261         j++
1262
1263 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1264         sha_word64      a, b, c, d, e, f, g, h, s0, s1;
1265         sha_word64      T1, *W512 = (sha_word64*)context->s512.buffer;
1266         int             j;
1267
1268         /* Initialize registers with the prev. intermediate value */
1269         a = context->s512.state[0];
1270         b = context->s512.state[1];
1271         c = context->s512.state[2];
1272         d = context->s512.state[3];
1273         e = context->s512.state[4];
1274         f = context->s512.state[5];
1275         g = context->s512.state[6];
1276         h = context->s512.state[7];
1277
1278         j = 0;
1279         do {
1280                 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
1281                 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
1282                 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
1283                 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
1284                 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
1285                 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
1286                 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
1287                 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
1288         } while (j < 16);
1289
1290         /* Now for the remaining rounds up to 79: */
1291         do {
1292                 ROUND512(a,b,c,d,e,f,g,h);
1293                 ROUND512(h,a,b,c,d,e,f,g);
1294                 ROUND512(g,h,a,b,c,d,e,f);
1295                 ROUND512(f,g,h,a,b,c,d,e);
1296                 ROUND512(e,f,g,h,a,b,c,d);
1297                 ROUND512(d,e,f,g,h,a,b,c);
1298                 ROUND512(c,d,e,f,g,h,a,b);
1299                 ROUND512(b,c,d,e,f,g,h,a);
1300         } while (j < 80);
1301
1302         /* Compute the current intermediate hash value */
1303         context->s512.state[0] += a;
1304         context->s512.state[1] += b;
1305         context->s512.state[2] += c;
1306         context->s512.state[3] += d;
1307         context->s512.state[4] += e;
1308         context->s512.state[5] += f;
1309         context->s512.state[6] += g;
1310         context->s512.state[7] += h;
1311
1312         /* Clean up */
1313         a = b = c = d = e = f = g = h = T1 = 0;
1314 }
1315
1316 #else /* SHA2_UNROLL_TRANSFORM */
1317
1318 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1319         sha_word64      a, b, c, d, e, f, g, h, s0, s1;
1320         sha_word64      T1, T2, *W512 = (sha_word64*)context->s512.buffer;
1321         int             j;
1322
1323         /* Initialize registers with the prev. intermediate value */
1324         a = context->s512.state[0];
1325         b = context->s512.state[1];
1326         c = context->s512.state[2];
1327         d = context->s512.state[3];
1328         e = context->s512.state[4];
1329         f = context->s512.state[5];
1330         g = context->s512.state[6];
1331         h = context->s512.state[7];
1332
1333         j = 0;
1334         do {
1335 #if BYTE_ORDER == LITTLE_ENDIAN
1336                 /* Convert TO host byte order */
1337                 REVERSE64(*data++, W512[j]);
1338                 /* Apply the SHA-512 compression function to update a..h */
1339                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
1340 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1341                 /* Apply the SHA-512 compression function to update a..h with copy */
1342                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
1343 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1344                 T2 = Sigma0_512(a) + Maj(a, b, c);
1345                 h = g;
1346                 g = f;
1347                 f = e;
1348                 e = d + T1;
1349                 d = c;
1350                 c = b;
1351                 b = a;
1352                 a = T1 + T2;
1353
1354                 j++;
1355         } while (j < 16);
1356
1357         do {
1358                 /* Part of the message block expansion: */
1359                 s0 = W512[(j+1)&0x0f];
1360                 s0 = sigma0_512(s0);
1361                 s1 = W512[(j+14)&0x0f];
1362                 s1 =  sigma1_512(s1);
1363
1364                 /* Apply the SHA-512 compression function to update a..h */
1365                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
1366                      (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
1367                 T2 = Sigma0_512(a) + Maj(a, b, c);
1368                 h = g;
1369                 g = f;
1370                 f = e;
1371                 e = d + T1;
1372                 d = c;
1373                 c = b;
1374                 b = a;
1375                 a = T1 + T2;
1376
1377                 j++;
1378         } while (j < 80);
1379
1380         /* Compute the current intermediate hash value */
1381         context->s512.state[0] += a;
1382         context->s512.state[1] += b;
1383         context->s512.state[2] += c;
1384         context->s512.state[3] += d;
1385         context->s512.state[4] += e;
1386         context->s512.state[5] += f;
1387         context->s512.state[6] += g;
1388         context->s512.state[7] += h;
1389
1390         /* Clean up */
1391         a = b = c = d = e = f = g = h = T1 = T2 = 0;
1392 }
1393
1394 #endif /* SHA2_UNROLL_TRANSFORM */
1395
1396 void SHA512_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1397         unsigned int    freespace, usedspace;
1398
1399         if (len == 0) {
1400                 /* Calling with no data is valid - we do nothing */
1401                 return;
1402         }
1403
1404         /* Sanity check: */
1405         assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
1406
1407         usedspace = (unsigned int)((context->s512.bitcount[0] >> 3) % 128);
1408         if (usedspace > 0) {
1409                 /* Calculate how much free space is available in the buffer */
1410                 freespace = 128 - usedspace;
1411
1412                 if (len >= freespace) {
1413                         /* Fill the buffer completely and process it */
1414                         MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, freespace);
1415                         ADDINC128(context->s512.bitcount, freespace << 3);
1416                         len -= freespace;
1417                         data += freespace;
1418                         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1419                 } else {
1420                         /* The buffer is not yet full */
1421                         MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, len);
1422                         ADDINC128(context->s512.bitcount, len << 3);
1423                         /* Clean up: */
1424                         usedspace = freespace = 0;
1425                         return;
1426                 }
1427         }
1428         while (len >= 128) {
1429                 /* Process as many complete blocks as we can */
1430                 SHA512_Internal_Transform(context, (sha_word64*)data);
1431                 ADDINC128(context->s512.bitcount, 1024);
1432                 len -= 128;
1433                 data += 128;
1434         }
1435         if (len > 0) {
1436                 /* There's left-overs, so save 'em */
1437                 MEMCPY_BCOPY(context->s512.buffer, data, len);
1438                 ADDINC128(context->s512.bitcount, len << 3);
1439         }
1440         /* Clean up: */
1441         usedspace = freespace = 0;
1442 }
1443
1444 void SHA512_Internal_Last(SHA_CTX* context) {
1445         unsigned int    usedspace;
1446
1447         usedspace = (unsigned int)((context->s512.bitcount[0] >> 3) % 128);
1448 #if BYTE_ORDER == LITTLE_ENDIAN
1449         /* Convert FROM host byte order */
1450         REVERSE64(context->s512.bitcount[0],context->s512.bitcount[0]);
1451         REVERSE64(context->s512.bitcount[1],context->s512.bitcount[1]);
1452 #endif
1453         if (usedspace > 0) {
1454                 /* Begin padding with a 1 bit: */
1455                 context->s512.buffer[usedspace++] = 0x80;
1456
1457                 if (usedspace <= 112) {
1458                         /* Set-up for the last transform: */
1459                         MEMSET_BZERO(&context->s512.buffer[usedspace], 112 - usedspace);
1460                 } else {
1461                         if (usedspace < 128) {
1462                                 MEMSET_BZERO(&context->s512.buffer[usedspace], 128 - usedspace);
1463                         }
1464                         /* Do second-to-last transform: */
1465                         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1466
1467                         /* And set-up for the last transform: */
1468                         MEMSET_BZERO(context->s512.buffer, 112);
1469                 }
1470                 /* Clean up: */
1471                 usedspace = 0;
1472         } else {
1473                 /* Prepare for final transform: */
1474                 MEMSET_BZERO(context->s512.buffer, 112);
1475
1476                 /* Begin padding with a 1 bit: */
1477                 *context->s512.buffer = 0x80;
1478         }
1479         /* Store the length of input data (in bits): */
1480         MEMCPY_BCOPY(&context->s512.buffer[112], &context->s512.bitcount[1],
1481                      sizeof(sha_word64));
1482         MEMCPY_BCOPY(&context->s512.buffer[120], &context->s512.bitcount[0],
1483                      sizeof(sha_word64));
1484
1485         /* Final transform: */
1486         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1487 }
1488
1489 void SHA512_Final(sha_byte digest[], SHA_CTX* context) {
1490         sha_word64      *d = (sha_word64*)digest;
1491
1492         /* Sanity check: */
1493         assert(context != (SHA_CTX*)0);
1494
1495         /* If no digest buffer is passed, we don't bother doing this: */
1496         if (digest != (sha_byte*)0) {
1497                 SHA512_Internal_Last(context);
1498
1499                 /* Save the hash data for output: */
1500 #if BYTE_ORDER == LITTLE_ENDIAN
1501                 {
1502                         /* Convert TO host byte order */
1503                         int     j;
1504                         for (j = 0; j < (SHA512_DIGEST_LENGTH >> 3); j++) {
1505                                 REVERSE64(context->s512.state[j],context->s512.state[j]);
1506                                 *d++ = context->s512.state[j];
1507                         }
1508                 }
1509 #else
1510                 MEMCPY_BCOPY(d, context->s512.state, SHA512_DIGEST_LENGTH);
1511 #endif
1512         }
1513
1514         /* Zero out state data */
1515         MEMSET_BZERO(context, sizeof(*context));
1516 }
1517
1518 char *SHA512_End(SHA_CTX* context, char buffer[]) {
1519         sha_byte        digest[SHA512_DIGEST_LENGTH], *d = digest;
1520         int             i;
1521
1522         /* Sanity check: */
1523         assert(context != (SHA_CTX*)0);
1524
1525         if (buffer != (char*)0) {
1526                 SHA512_Final(digest, context);
1527
1528                 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
1529                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1530                         *buffer++ = sha_hex_digits[*d & 0x0f];
1531                         d++;
1532                 }
1533                 *buffer = (char)0;
1534         } else {
1535                 MEMSET_BZERO(context, sizeof(*context));
1536         }
1537         MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
1538         return buffer;
1539 }
1540
1541 char* SHA512_Data(const sha_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
1542         SHA_CTX context;
1543
1544         SHA512_Init(&context);
1545         SHA512_Update(&context, data, len);
1546         return SHA512_End(&context, digest);
1547 }
1548
1549
1550 /*** SHA-384: *********************************************************/
1551 void SHA384_Init(SHA_CTX* context) {
1552         SHA512_Internal_Init(context, sha384_initial_hash_value);
1553 }
1554
1555 void SHA384_Update(SHA_CTX* context, const sha_byte* data, size_t len) {
1556         SHA512_Update(context, data, len);
1557 }
1558
1559 void SHA384_Final(sha_byte digest[], SHA_CTX* context) {
1560         sha_word64      *d = (sha_word64*)digest;
1561
1562         /* Sanity check: */
1563         assert(context != (SHA_CTX*)0);
1564
1565         /* If no digest buffer is passed, we don't bother doing this: */
1566         if (digest != (sha_byte*)0) {
1567                 SHA512_Internal_Last(context);
1568
1569                 /* Save the hash data for output: */
1570 #if BYTE_ORDER == LITTLE_ENDIAN
1571                 {
1572                         /* Convert TO host byte order */
1573                         int     j;
1574                         for (j = 0; j < (SHA384_DIGEST_LENGTH >> 3); j++) {
1575                                 REVERSE64(context->s512.state[j],context->s512.state[j]);
1576                                 *d++ = context->s512.state[j];
1577                         }
1578                 }
1579 #else
1580                 MEMCPY_BCOPY(d, context->s512.state, SHA384_DIGEST_LENGTH);
1581 #endif
1582         }
1583
1584         /* Zero out state data */
1585         MEMSET_BZERO(context, sizeof(*context));
1586 }
1587
1588 char *SHA384_End(SHA_CTX* context, char buffer[]) {
1589         sha_byte        digest[SHA384_DIGEST_LENGTH], *d = digest;
1590         int             i;
1591
1592         /* Sanity check: */
1593         assert(context != (SHA_CTX*)0);
1594
1595         if (buffer != (char*)0) {
1596                 SHA384_Final(digest, context);
1597
1598                 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1599                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1600                         *buffer++ = sha_hex_digits[*d & 0x0f];
1601                         d++;
1602                 }
1603                 *buffer = (char)0;
1604         } else {
1605                 MEMSET_BZERO(context, sizeof(*context));
1606         }
1607         MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1608         return buffer;
1609 }
1610
1611 char* SHA384_Data(const sha_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1612         SHA_CTX context;
1613
1614         SHA384_Init(&context);
1615         SHA384_Update(&context, data, len);
1616         return SHA384_End(&context, digest);
1617 }