Imported Upstream version 2.8.9
[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         *(sha_word64*)&context->s1.buffer[56] = context->s1.bitcount;
744
745         /* Final transform: */
746         SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
747
748         /* Save the hash data for output: */
749 #if BYTE_ORDER == LITTLE_ENDIAN
750         {
751                 /* Convert TO host byte order */
752                 int     j;
753                 for (j = 0; j < (SHA1_DIGEST_LENGTH >> 2); j++) {
754                         REVERSE32(context->s1.state[j],context->s1.state[j]);
755                         *d++ = context->s1.state[j];
756                 }
757         }
758 #else
759         MEMCPY_BCOPY(d, context->s1.state, SHA1_DIGEST_LENGTH);
760 #endif
761
762         /* Clean up: */
763         MEMSET_BZERO(context, sizeof(*context));
764 }
765
766 char *SHA1_End(SHA_CTX* context, char buffer[]) {
767         sha_byte        digest[SHA1_DIGEST_LENGTH], *d = digest;
768         int             i;
769
770         /* Sanity check: */
771         assert(context != (SHA_CTX*)0);
772
773         if (buffer != (char*)0) {
774                 SHA1_Final(digest, context);
775
776                 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
777                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
778                         *buffer++ = sha_hex_digits[*d & 0x0f];
779                         d++;
780                 }
781                 *buffer = (char)0;
782         } else {
783                 MEMSET_BZERO(context, sizeof(*context));
784         }
785         MEMSET_BZERO(digest, SHA1_DIGEST_LENGTH);
786         return buffer;
787 }
788
789 char* SHA1_Data(const sha_byte* data, size_t len, char digest[SHA1_DIGEST_STRING_LENGTH]) {
790         SHA_CTX context;
791
792         SHA1_Init(&context);
793         SHA1_Update(&context, data, len);
794         return SHA1_End(&context, digest);
795 }
796
797
798 /*** SHA-256: *********************************************************/
799 void SHA256_Internal_Init(SHA_CTX* context, const sha_word32* ihv) {
800         /* Sanity check: */
801         assert(context != (SHA_CTX*)0);
802
803         MEMCPY_BCOPY(context->s256.state, ihv, sizeof(sha_word32) * 8);
804         MEMSET_BZERO(context->s256.buffer, 64);
805         context->s256.bitcount = 0;
806 }
807
808 void SHA256_Init(SHA_CTX* context) {
809         SHA256_Internal_Init(context, sha256_initial_hash_value);
810 }
811
812 #ifdef SHA2_UNROLL_TRANSFORM
813
814 /* Unrolled SHA-256 round macros: */
815
816 #if BYTE_ORDER == LITTLE_ENDIAN
817
818 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
819         REVERSE32(*data++, W256[j]); \
820         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
821              K256[j] + W256[j]; \
822         (d) += T1; \
823         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
824         j++
825
826
827 #else /* BYTE_ORDER == LITTLE_ENDIAN */
828
829 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
830         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
831              K256[j] + (W256[j] = *data++); \
832         (d) += T1; \
833         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
834         j++
835
836 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
837
838 #define ROUND256(a,b,c,d,e,f,g,h)       \
839         s0 = W256[(j+1)&0x0f]; \
840         s0 = sigma0_256(s0); \
841         s1 = W256[(j+14)&0x0f]; \
842         s1 = sigma1_256(s1); \
843         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
844              (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
845         (d) += T1; \
846         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
847         j++
848
849 void SHA256_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
850         sha_word32      a, b, c, d, e, f, g, h, s0, s1;
851         sha_word32      T1, *W256;
852         int             j;
853
854         W256 = (sha_word32*)context->s256.buffer;
855
856         /* Initialize registers with the prev. intermediate value */
857         a = context->s256.state[0];
858         b = context->s256.state[1];
859         c = context->s256.state[2];
860         d = context->s256.state[3];
861         e = context->s256.state[4];
862         f = context->s256.state[5];
863         g = context->s256.state[6];
864         h = context->s256.state[7];
865
866         j = 0;
867         do {
868                 /* Rounds 0 to 15 (unrolled): */
869                 ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
870                 ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
871                 ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
872                 ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
873                 ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
874                 ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
875                 ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
876                 ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
877         } while (j < 16);
878
879         /* Now for the remaining rounds to 64: */
880         do {
881                 ROUND256(a,b,c,d,e,f,g,h);
882                 ROUND256(h,a,b,c,d,e,f,g);
883                 ROUND256(g,h,a,b,c,d,e,f);
884                 ROUND256(f,g,h,a,b,c,d,e);
885                 ROUND256(e,f,g,h,a,b,c,d);
886                 ROUND256(d,e,f,g,h,a,b,c);
887                 ROUND256(c,d,e,f,g,h,a,b);
888                 ROUND256(b,c,d,e,f,g,h,a);
889         } while (j < 64);
890
891         /* Compute the current intermediate hash value */
892         context->s256.state[0] += a;
893         context->s256.state[1] += b;
894         context->s256.state[2] += c;
895         context->s256.state[3] += d;
896         context->s256.state[4] += e;
897         context->s256.state[5] += f;
898         context->s256.state[6] += g;
899         context->s256.state[7] += h;
900
901         /* Clean up */
902         a = b = c = d = e = f = g = h = T1 = 0;
903 }
904
905 #else /* SHA2_UNROLL_TRANSFORM */
906
907 void SHA256_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
908         sha_word32      a, b, c, d, e, f, g, h, s0, s1;
909         sha_word32      T1, T2, *W256;
910         int             j;
911
912         W256 = (sha_word32*)context->s256.buffer;
913
914         /* Initialize registers with the prev. intermediate value */
915         a = context->s256.state[0];
916         b = context->s256.state[1];
917         c = context->s256.state[2];
918         d = context->s256.state[3];
919         e = context->s256.state[4];
920         f = context->s256.state[5];
921         g = context->s256.state[6];
922         h = context->s256.state[7];
923
924         j = 0;
925         do {
926 #if BYTE_ORDER == LITTLE_ENDIAN
927                 /* Copy data while converting to host byte order */
928                 REVERSE32(*data++,W256[j]);
929                 /* Apply the SHA-256 compression function to update a..h */
930                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
931 #else /* BYTE_ORDER == LITTLE_ENDIAN */
932                 /* Apply the SHA-256 compression function to update a..h with copy */
933                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
934 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
935                 T2 = Sigma0_256(a) + Maj(a, b, c);
936                 h = g;
937                 g = f;
938                 f = e;
939                 e = d + T1;
940                 d = c;
941                 c = b;
942                 b = a;
943                 a = T1 + T2;
944
945                 j++;
946         } while (j < 16);
947
948         do {
949                 /* Part of the message block expansion: */
950                 s0 = W256[(j+1)&0x0f];
951                 s0 = sigma0_256(s0);
952                 s1 = W256[(j+14)&0x0f];
953                 s1 = sigma1_256(s1);
954
955                 /* Apply the SHA-256 compression function to update a..h */
956                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
957                      (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
958                 T2 = Sigma0_256(a) + Maj(a, b, c);
959                 h = g;
960                 g = f;
961                 f = e;
962                 e = d + T1;
963                 d = c;
964                 c = b;
965                 b = a;
966                 a = T1 + T2;
967
968                 j++;
969         } while (j < 64);
970
971         /* Compute the current intermediate hash value */
972         context->s256.state[0] += a;
973         context->s256.state[1] += b;
974         context->s256.state[2] += c;
975         context->s256.state[3] += d;
976         context->s256.state[4] += e;
977         context->s256.state[5] += f;
978         context->s256.state[6] += g;
979         context->s256.state[7] += h;
980
981         /* Clean up */
982         a = b = c = d = e = f = g = h = T1 = T2 = 0;
983 }
984
985 #endif /* SHA2_UNROLL_TRANSFORM */
986
987 void SHA256_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
988         unsigned int    freespace, usedspace;
989
990         if (len == 0) {
991                 /* Calling with no data is valid - we do nothing */
992                 return;
993         }
994
995         /* Sanity check: */
996         assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
997
998         usedspace = (unsigned int)((context->s256.bitcount >> 3) % 64);
999         if (usedspace > 0) {
1000                 /* Calculate how much free space is available in the buffer */
1001                 freespace = 64 - usedspace;
1002
1003                 if (len >= freespace) {
1004                         /* Fill the buffer completely and process it */
1005                         MEMCPY_BCOPY(&context->s256.buffer[usedspace], data, freespace);
1006                         context->s256.bitcount += freespace << 3;
1007                         len -= freespace;
1008                         data += freespace;
1009                         SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1010                 } else {
1011                         /* The buffer is not yet full */
1012                         MEMCPY_BCOPY(&context->s256.buffer[usedspace], data, len);
1013                         context->s256.bitcount += len << 3;
1014                         /* Clean up: */
1015                         usedspace = freespace = 0;
1016                         return;
1017                 }
1018         }
1019         while (len >= 64) {
1020                 /* Process as many complete blocks as we can */
1021                 SHA256_Internal_Transform(context, (sha_word32*)data);
1022                 context->s256.bitcount += 512;
1023                 len -= 64;
1024                 data += 64;
1025         }
1026         if (len > 0) {
1027                 /* There's left-overs, so save 'em */
1028                 MEMCPY_BCOPY(context->s256.buffer, data, len);
1029                 context->s256.bitcount += len << 3;
1030         }
1031         /* Clean up: */
1032         usedspace = freespace = 0;
1033 }
1034
1035 void SHA256_Internal_Last(SHA_CTX* context) {
1036         unsigned int    usedspace;
1037
1038         usedspace = (unsigned int)((context->s256.bitcount >> 3) % 64);
1039 #if BYTE_ORDER == LITTLE_ENDIAN
1040         /* Convert FROM host byte order */
1041         REVERSE64(context->s256.bitcount,context->s256.bitcount);
1042 #endif
1043         if (usedspace > 0) {
1044                 /* Begin padding with a 1 bit: */
1045                 context->s256.buffer[usedspace++] = 0x80;
1046
1047                 if (usedspace <= 56) {
1048                         /* Set-up for the last transform: */
1049                         MEMSET_BZERO(&context->s256.buffer[usedspace], 56 - usedspace);
1050                 } else {
1051                         if (usedspace < 64) {
1052                                 MEMSET_BZERO(&context->s256.buffer[usedspace], 64 - usedspace);
1053                         }
1054                         /* Do second-to-last transform: */
1055                         SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1056
1057                         /* And set-up for the last transform: */
1058                         MEMSET_BZERO(context->s256.buffer, 56);
1059                 }
1060                 /* Clean up: */
1061                 usedspace = 0;
1062         } else {
1063                 /* Set-up for the last transform: */
1064                 MEMSET_BZERO(context->s256.buffer, 56);
1065
1066                 /* Begin padding with a 1 bit: */
1067                 *context->s256.buffer = 0x80;
1068         }
1069         /* Set the bit count: */
1070         *(sha_word64*)&context->s256.buffer[56] = context->s256.bitcount;
1071
1072         /* Final transform: */
1073         SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1074 }
1075
1076 void SHA256_Final(sha_byte digest[], SHA_CTX* context) {
1077         sha_word32      *d = (sha_word32*)digest;
1078
1079         /* Sanity check: */
1080         assert(context != (SHA_CTX*)0);
1081
1082         /* If no digest buffer is passed, we don't bother doing this: */
1083         if (digest != (sha_byte*)0) {
1084                 SHA256_Internal_Last(context);
1085
1086                 /* Save the hash data for output: */
1087 #if BYTE_ORDER == LITTLE_ENDIAN
1088                 {
1089                         /* Convert TO host byte order */
1090                         int     j;
1091                         for (j = 0; j < (SHA256_DIGEST_LENGTH >> 2); j++) {
1092                                 REVERSE32(context->s256.state[j],context->s256.state[j]);
1093                                 *d++ = context->s256.state[j];
1094                         }
1095                 }
1096 #else
1097                 MEMCPY_BCOPY(d, context->s256.state, SHA256_DIGEST_LENGTH);
1098 #endif
1099         }
1100
1101         /* Clean up state data: */
1102         MEMSET_BZERO(context, sizeof(*context));
1103 }
1104
1105 char *SHA256_End(SHA_CTX* context, char buffer[]) {
1106         sha_byte        digest[SHA256_DIGEST_LENGTH], *d = digest;
1107         int             i;
1108
1109         /* Sanity check: */
1110         assert(context != (SHA_CTX*)0);
1111
1112         if (buffer != (char*)0) {
1113                 SHA256_Final(digest, context);
1114
1115                 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1116                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1117                         *buffer++ = sha_hex_digits[*d & 0x0f];
1118                         d++;
1119                 }
1120                 *buffer = (char)0;
1121         } else {
1122                 MEMSET_BZERO(context, sizeof(*context));
1123         }
1124         MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
1125         return buffer;
1126 }
1127
1128 char* SHA256_Data(const sha_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
1129         SHA_CTX context;
1130
1131         SHA256_Init(&context);
1132         SHA256_Update(&context, data, len);
1133         return SHA256_End(&context, digest);
1134 }
1135
1136
1137 /*** SHA-224: *********************************************************/
1138 void SHA224_Init(SHA_CTX* context) {
1139         SHA256_Internal_Init(context, sha224_initial_hash_value);
1140 }
1141
1142 void SHA224_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
1143         SHA256_Internal_Transform(context, data);
1144 }
1145
1146 void SHA224_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1147         SHA256_Update(context, data, len);
1148 }
1149
1150 void SHA224_Final(sha_byte digest[], SHA_CTX* context) {
1151         sha_word32      *d = (sha_word32*)digest;
1152
1153         /* Sanity check: */
1154         assert(context != (SHA_CTX*)0);
1155
1156         /* If no digest buffer is passed, we don't bother doing this: */
1157         if (digest != (sha_byte*)0) {
1158                 SHA256_Internal_Last(context);
1159
1160                 /* Save the hash data for output: */
1161 #if BYTE_ORDER == LITTLE_ENDIAN
1162                 {
1163                         /* Convert TO host byte order */
1164                         int     j;
1165                         for (j = 0; j < (SHA224_DIGEST_LENGTH >> 2); j++) {
1166                                 REVERSE32(context->s256.state[j],context->s256.state[j]);
1167                                 *d++ = context->s256.state[j];
1168                         }
1169                 }
1170 #else
1171                 MEMCPY_BCOPY(d, context->s256.state, SHA224_DIGEST_LENGTH);
1172 #endif
1173         }
1174
1175         /* Clean up state data: */
1176         MEMSET_BZERO(context, sizeof(*context));
1177 }
1178
1179 char *SHA224_End(SHA_CTX* context, char buffer[]) {
1180         sha_byte        digest[SHA224_DIGEST_LENGTH], *d = digest;
1181         int             i;
1182
1183         /* Sanity check: */
1184         assert(context != (SHA_CTX*)0);
1185
1186         if (buffer != (char*)0) {
1187                 SHA224_Final(digest, context);
1188
1189                 for (i = 0; i < SHA224_DIGEST_LENGTH; i++) {
1190                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1191                         *buffer++ = sha_hex_digits[*d & 0x0f];
1192                         d++;
1193                 }
1194                 *buffer = (char)0;
1195         } else {
1196                 MEMSET_BZERO(context, sizeof(*context));
1197         }
1198         MEMSET_BZERO(digest, SHA224_DIGEST_LENGTH);
1199         return buffer;
1200 }
1201
1202 char* SHA224_Data(const sha_byte* data, size_t len, char digest[SHA224_DIGEST_STRING_LENGTH]) {
1203         SHA_CTX context;
1204
1205         SHA224_Init(&context);
1206         SHA224_Update(&context, data, len);
1207         return SHA224_End(&context, digest);
1208 }
1209
1210
1211 /*** SHA-512: *********************************************************/
1212 void SHA512_Internal_Init(SHA_CTX* context, const sha_word64* ihv) {
1213         /* Sanity check: */
1214         assert(context != (SHA_CTX*)0);
1215
1216         MEMCPY_BCOPY(context->s512.state, ihv, sizeof(sha_word64) * 8);
1217         MEMSET_BZERO(context->s512.buffer, 128);
1218         context->s512.bitcount[0] = context->s512.bitcount[1] =  0;
1219 }
1220
1221 void SHA512_Init(SHA_CTX* context) {
1222         SHA512_Internal_Init(context, sha512_initial_hash_value);
1223 }
1224
1225 #ifdef SHA2_UNROLL_TRANSFORM
1226
1227 /* Unrolled SHA-512 round macros: */
1228 #if BYTE_ORDER == LITTLE_ENDIAN
1229
1230 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
1231         REVERSE64(*data++, W512[j]); \
1232         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1233              K512[j] + W512[j]; \
1234         (d) += T1, \
1235         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
1236         j++
1237
1238
1239 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1240
1241 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
1242         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
1243              K512[j] + (W512[j] = *data++); \
1244         (d) += T1; \
1245         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1246         j++
1247
1248 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1249
1250 #define ROUND512(a,b,c,d,e,f,g,h)       \
1251         s0 = W512[(j+1)&0x0f]; \
1252         s0 = sigma0_512(s0); \
1253         s1 = W512[(j+14)&0x0f]; \
1254         s1 = sigma1_512(s1); \
1255         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
1256              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
1257         (d) += T1; \
1258         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1259         j++
1260
1261 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1262         sha_word64      a, b, c, d, e, f, g, h, s0, s1;
1263         sha_word64      T1, *W512 = (sha_word64*)context->s512.buffer;
1264         int             j;
1265
1266         /* Initialize registers with the prev. intermediate value */
1267         a = context->s512.state[0];
1268         b = context->s512.state[1];
1269         c = context->s512.state[2];
1270         d = context->s512.state[3];
1271         e = context->s512.state[4];
1272         f = context->s512.state[5];
1273         g = context->s512.state[6];
1274         h = context->s512.state[7];
1275
1276         j = 0;
1277         do {
1278                 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
1279                 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
1280                 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
1281                 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
1282                 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
1283                 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
1284                 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
1285                 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
1286         } while (j < 16);
1287
1288         /* Now for the remaining rounds up to 79: */
1289         do {
1290                 ROUND512(a,b,c,d,e,f,g,h);
1291                 ROUND512(h,a,b,c,d,e,f,g);
1292                 ROUND512(g,h,a,b,c,d,e,f);
1293                 ROUND512(f,g,h,a,b,c,d,e);
1294                 ROUND512(e,f,g,h,a,b,c,d);
1295                 ROUND512(d,e,f,g,h,a,b,c);
1296                 ROUND512(c,d,e,f,g,h,a,b);
1297                 ROUND512(b,c,d,e,f,g,h,a);
1298         } while (j < 80);
1299
1300         /* Compute the current intermediate hash value */
1301         context->s512.state[0] += a;
1302         context->s512.state[1] += b;
1303         context->s512.state[2] += c;
1304         context->s512.state[3] += d;
1305         context->s512.state[4] += e;
1306         context->s512.state[5] += f;
1307         context->s512.state[6] += g;
1308         context->s512.state[7] += h;
1309
1310         /* Clean up */
1311         a = b = c = d = e = f = g = h = T1 = 0;
1312 }
1313
1314 #else /* SHA2_UNROLL_TRANSFORM */
1315
1316 void SHA512_Internal_Transform(SHA_CTX* context, const sha_word64* data) {
1317         sha_word64      a, b, c, d, e, f, g, h, s0, s1;
1318         sha_word64      T1, T2, *W512 = (sha_word64*)context->s512.buffer;
1319         int             j;
1320
1321         /* Initialize registers with the prev. intermediate value */
1322         a = context->s512.state[0];
1323         b = context->s512.state[1];
1324         c = context->s512.state[2];
1325         d = context->s512.state[3];
1326         e = context->s512.state[4];
1327         f = context->s512.state[5];
1328         g = context->s512.state[6];
1329         h = context->s512.state[7];
1330
1331         j = 0;
1332         do {
1333 #if BYTE_ORDER == LITTLE_ENDIAN
1334                 /* Convert TO host byte order */
1335                 REVERSE64(*data++, W512[j]);
1336                 /* Apply the SHA-512 compression function to update a..h */
1337                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
1338 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1339                 /* Apply the SHA-512 compression function to update a..h with copy */
1340                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
1341 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1342                 T2 = Sigma0_512(a) + Maj(a, b, c);
1343                 h = g;
1344                 g = f;
1345                 f = e;
1346                 e = d + T1;
1347                 d = c;
1348                 c = b;
1349                 b = a;
1350                 a = T1 + T2;
1351
1352                 j++;
1353         } while (j < 16);
1354
1355         do {
1356                 /* Part of the message block expansion: */
1357                 s0 = W512[(j+1)&0x0f];
1358                 s0 = sigma0_512(s0);
1359                 s1 = W512[(j+14)&0x0f];
1360                 s1 =  sigma1_512(s1);
1361
1362                 /* Apply the SHA-512 compression function to update a..h */
1363                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
1364                      (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
1365                 T2 = Sigma0_512(a) + Maj(a, b, c);
1366                 h = g;
1367                 g = f;
1368                 f = e;
1369                 e = d + T1;
1370                 d = c;
1371                 c = b;
1372                 b = a;
1373                 a = T1 + T2;
1374
1375                 j++;
1376         } while (j < 80);
1377
1378         /* Compute the current intermediate hash value */
1379         context->s512.state[0] += a;
1380         context->s512.state[1] += b;
1381         context->s512.state[2] += c;
1382         context->s512.state[3] += d;
1383         context->s512.state[4] += e;
1384         context->s512.state[5] += f;
1385         context->s512.state[6] += g;
1386         context->s512.state[7] += h;
1387
1388         /* Clean up */
1389         a = b = c = d = e = f = g = h = T1 = T2 = 0;
1390 }
1391
1392 #endif /* SHA2_UNROLL_TRANSFORM */
1393
1394 void SHA512_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1395         unsigned int    freespace, usedspace;
1396
1397         if (len == 0) {
1398                 /* Calling with no data is valid - we do nothing */
1399                 return;
1400         }
1401
1402         /* Sanity check: */
1403         assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
1404
1405         usedspace = (unsigned int)((context->s512.bitcount[0] >> 3) % 128);
1406         if (usedspace > 0) {
1407                 /* Calculate how much free space is available in the buffer */
1408                 freespace = 128 - usedspace;
1409
1410                 if (len >= freespace) {
1411                         /* Fill the buffer completely and process it */
1412                         MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, freespace);
1413                         ADDINC128(context->s512.bitcount, freespace << 3);
1414                         len -= freespace;
1415                         data += freespace;
1416                         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1417                 } else {
1418                         /* The buffer is not yet full */
1419                         MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, len);
1420                         ADDINC128(context->s512.bitcount, len << 3);
1421                         /* Clean up: */
1422                         usedspace = freespace = 0;
1423                         return;
1424                 }
1425         }
1426         while (len >= 128) {
1427                 /* Process as many complete blocks as we can */
1428                 SHA512_Internal_Transform(context, (sha_word64*)data);
1429                 ADDINC128(context->s512.bitcount, 1024);
1430                 len -= 128;
1431                 data += 128;
1432         }
1433         if (len > 0) {
1434                 /* There's left-overs, so save 'em */
1435                 MEMCPY_BCOPY(context->s512.buffer, data, len);
1436                 ADDINC128(context->s512.bitcount, len << 3);
1437         }
1438         /* Clean up: */
1439         usedspace = freespace = 0;
1440 }
1441
1442 void SHA512_Internal_Last(SHA_CTX* context) {
1443         unsigned int    usedspace;
1444
1445         usedspace = (unsigned int)((context->s512.bitcount[0] >> 3) % 128);
1446 #if BYTE_ORDER == LITTLE_ENDIAN
1447         /* Convert FROM host byte order */
1448         REVERSE64(context->s512.bitcount[0],context->s512.bitcount[0]);
1449         REVERSE64(context->s512.bitcount[1],context->s512.bitcount[1]);
1450 #endif
1451         if (usedspace > 0) {
1452                 /* Begin padding with a 1 bit: */
1453                 context->s512.buffer[usedspace++] = 0x80;
1454
1455                 if (usedspace <= 112) {
1456                         /* Set-up for the last transform: */
1457                         MEMSET_BZERO(&context->s512.buffer[usedspace], 112 - usedspace);
1458                 } else {
1459                         if (usedspace < 128) {
1460                                 MEMSET_BZERO(&context->s512.buffer[usedspace], 128 - usedspace);
1461                         }
1462                         /* Do second-to-last transform: */
1463                         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1464
1465                         /* And set-up for the last transform: */
1466                         MEMSET_BZERO(context->s512.buffer, 112);
1467                 }
1468                 /* Clean up: */
1469                 usedspace = 0;
1470         } else {
1471                 /* Prepare for final transform: */
1472                 MEMSET_BZERO(context->s512.buffer, 112);
1473
1474                 /* Begin padding with a 1 bit: */
1475                 *context->s512.buffer = 0x80;
1476         }
1477         /* Store the length of input data (in bits): */
1478         *(sha_word64*)&context->s512.buffer[112] = context->s512.bitcount[1];
1479         *(sha_word64*)&context->s512.buffer[120] = context->s512.bitcount[0];
1480
1481         /* Final transform: */
1482         SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1483 }
1484
1485 void SHA512_Final(sha_byte digest[], SHA_CTX* context) {
1486         sha_word64      *d = (sha_word64*)digest;
1487
1488         /* Sanity check: */
1489         assert(context != (SHA_CTX*)0);
1490
1491         /* If no digest buffer is passed, we don't bother doing this: */
1492         if (digest != (sha_byte*)0) {
1493                 SHA512_Internal_Last(context);
1494
1495                 /* Save the hash data for output: */
1496 #if BYTE_ORDER == LITTLE_ENDIAN
1497                 {
1498                         /* Convert TO host byte order */
1499                         int     j;
1500                         for (j = 0; j < (SHA512_DIGEST_LENGTH >> 3); j++) {
1501                                 REVERSE64(context->s512.state[j],context->s512.state[j]);
1502                                 *d++ = context->s512.state[j];
1503                         }
1504                 }
1505 #else
1506                 MEMCPY_BCOPY(d, context->s512.state, SHA512_DIGEST_LENGTH);
1507 #endif
1508         }
1509
1510         /* Zero out state data */
1511         MEMSET_BZERO(context, sizeof(*context));
1512 }
1513
1514 char *SHA512_End(SHA_CTX* context, char buffer[]) {
1515         sha_byte        digest[SHA512_DIGEST_LENGTH], *d = digest;
1516         int             i;
1517
1518         /* Sanity check: */
1519         assert(context != (SHA_CTX*)0);
1520
1521         if (buffer != (char*)0) {
1522                 SHA512_Final(digest, context);
1523
1524                 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
1525                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1526                         *buffer++ = sha_hex_digits[*d & 0x0f];
1527                         d++;
1528                 }
1529                 *buffer = (char)0;
1530         } else {
1531                 MEMSET_BZERO(context, sizeof(*context));
1532         }
1533         MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
1534         return buffer;
1535 }
1536
1537 char* SHA512_Data(const sha_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
1538         SHA_CTX context;
1539
1540         SHA512_Init(&context);
1541         SHA512_Update(&context, data, len);
1542         return SHA512_End(&context, digest);
1543 }
1544
1545
1546 /*** SHA-384: *********************************************************/
1547 void SHA384_Init(SHA_CTX* context) {
1548         SHA512_Internal_Init(context, sha384_initial_hash_value);
1549 }
1550
1551 void SHA384_Update(SHA_CTX* context, const sha_byte* data, size_t len) {
1552         SHA512_Update(context, data, len);
1553 }
1554
1555 void SHA384_Final(sha_byte digest[], SHA_CTX* context) {
1556         sha_word64      *d = (sha_word64*)digest;
1557
1558         /* Sanity check: */
1559         assert(context != (SHA_CTX*)0);
1560
1561         /* If no digest buffer is passed, we don't bother doing this: */
1562         if (digest != (sha_byte*)0) {
1563                 SHA512_Internal_Last(context);
1564
1565                 /* Save the hash data for output: */
1566 #if BYTE_ORDER == LITTLE_ENDIAN
1567                 {
1568                         /* Convert TO host byte order */
1569                         int     j;
1570                         for (j = 0; j < (SHA384_DIGEST_LENGTH >> 3); j++) {
1571                                 REVERSE64(context->s512.state[j],context->s512.state[j]);
1572                                 *d++ = context->s512.state[j];
1573                         }
1574                 }
1575 #else
1576                 MEMCPY_BCOPY(d, context->s512.state, SHA384_DIGEST_LENGTH);
1577 #endif
1578         }
1579
1580         /* Zero out state data */
1581         MEMSET_BZERO(context, sizeof(*context));
1582 }
1583
1584 char *SHA384_End(SHA_CTX* context, char buffer[]) {
1585         sha_byte        digest[SHA384_DIGEST_LENGTH], *d = digest;
1586         int             i;
1587
1588         /* Sanity check: */
1589         assert(context != (SHA_CTX*)0);
1590
1591         if (buffer != (char*)0) {
1592                 SHA384_Final(digest, context);
1593
1594                 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1595                         *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1596                         *buffer++ = sha_hex_digits[*d & 0x0f];
1597                         d++;
1598                 }
1599                 *buffer = (char)0;
1600         } else {
1601                 MEMSET_BZERO(context, sizeof(*context));
1602         }
1603         MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1604         return buffer;
1605 }
1606
1607 char* SHA384_Data(const sha_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1608         SHA_CTX context;
1609
1610         SHA384_Init(&context);
1611         SHA384_Update(&context, data, len);
1612         return SHA384_End(&context, digest);
1613 }