3 * AUTHOR: Aaron D. Gifford
4 * http://www.aarongifford.com/computers/sha.html
6 * Copyright (c) 2000-2003, Aaron D. Gifford
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 * $Id: sha2.c,v 1.4 2004/01/07 22:58:18 adg Exp $
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 */
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.
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:
52 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
56 * #define SHA2_UNROLL_TRANSFORM
61 /*** SHA-224/256/384/512 Machine Architecture Definitions *************/
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
70 * If your system does not define the above, then you can do so by
73 * #define LITTLE_ENDIAN 1234
74 * #define BIG_ENDIAN 4321
76 * And for little-endian machines, add:
78 * #define BYTE_ORDER LITTLE_ENDIAN
80 * Or for big-endian machines:
82 * #define BYTE_ORDER BIG_ENDIAN
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
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"
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
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 */
109 #if defined(__clang__)
110 # pragma clang diagnostic ignored "-Wcast-align"
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); \
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); \
129 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
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
136 #define ADDINC128(w,n) { \
137 (w)[0] += (sha_word64)(n); \
138 if ((w)[0] < (n)) { \
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().
148 * Please define either SHA2_USE_MEMSET_MEMCPY or define
149 * SHA2_USE_BZERO_BCOPY depending on which function set you
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
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!
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))
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))
171 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
173 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
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
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
183 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
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.
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))))
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)))
203 /* Function used in SHA-1: */
204 #define Parity(x,y,z) ((x) ^ (y) ^ (z))
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)))
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)))
218 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
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*);
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*);
231 /*** SHA2 INITIAL HASH VALUES AND CONSTANTS ***************************/
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)
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)
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)
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)
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)
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)
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)
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)
377 * Constant used by SHA224/256/384/512_End() functions for converting the
378 * digest to a readable hexadecimal character string:
380 static const char *sha_hex_digits = "0123456789abcdef";
383 /*** SHA-1: ***********************************************************/
384 void SHA1_Init(SHA_CTX* context) {
386 assert(context != (SHA_CTX*)0);
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;
393 #ifdef SHA2_UNROLL_TRANSFORM
395 /* Unrolled SHA-1 round macros: */
397 #if BYTE_ORDER == LITTLE_ENDIAN
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)); \
406 #else /* BYTE_ORDER == LITTLE_ENDIAN */
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)); \
414 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
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); \
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); \
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); \
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); \
440 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
441 sha_word32 a, b, c, d, e;
445 W1 = (sha_word32*)context->s1.buffer;
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];
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);
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);
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);
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);
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);
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;
554 a = b = c = d = e = T1 = 0;
557 #else /* SHA2_UNROLL_TRANSFORM */
559 void SHA1_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
560 sha_word32 a, b, c, d, e;
564 W1 = (sha_word32*)context->s1.buffer;
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];
574 #if BYTE_ORDER == LITTLE_ENDIAN
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 */
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));
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));
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));
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));
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;
643 a = b = c = d = e = T1 = 0;
646 #endif /* SHA2_UNROLL_TRANSFORM */
648 void SHA1_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
649 unsigned int freespace, usedspace;
651 /* Calling with no data is valid - we do nothing */
656 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
658 usedspace = (unsigned int)((context->s1.bitcount >> 3) % 64);
660 /* Calculate how much free space is available in the buffer */
661 freespace = 64 - usedspace;
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;
669 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
671 /* The buffer is not yet full */
672 MEMCPY_BCOPY(&context->s1.buffer[usedspace], data, len);
673 context->s1.bitcount += len << 3;
675 usedspace = freespace = 0;
680 /* Process as many complete blocks as we can */
681 SHA1_Internal_Transform(context, (sha_word32*)data);
682 context->s1.bitcount += 512;
687 /* There's left-overs, so save 'em */
688 MEMCPY_BCOPY(context->s1.buffer, data, len);
689 context->s1.bitcount += len << 3;
692 usedspace = freespace = 0;
695 void SHA1_Final(sha_byte digest[], SHA_CTX* context) {
696 sha_word32 *d = (sha_word32*)digest;
697 unsigned int usedspace;
700 assert(context != (SHA_CTX*)0);
702 if (digest == (sha_byte*)0) {
704 * No digest buffer, so we can do nothing
705 * except clean up and go home
707 MEMSET_BZERO(context, sizeof(*context));
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);
716 /* Begin padding with a 1 bit: */
717 *context->s1.buffer = 0x80;
719 /* Begin padding with a 1 bit: */
720 context->s1.buffer[usedspace++] = 0x80;
722 if (usedspace <= 56) {
723 /* Set-up for the last transform: */
724 MEMSET_BZERO(&context->s1.buffer[usedspace], 56 - usedspace);
726 if (usedspace < 64) {
727 MEMSET_BZERO(&context->s1.buffer[usedspace], 64 - usedspace);
729 /* Do second-to-last transform: */
730 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
732 /* And set-up for the last transform: */
733 MEMSET_BZERO(context->s1.buffer, 56);
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);
743 *(sha_word64*)&context->s1.buffer[56] = context->s1.bitcount;
745 /* Final transform: */
746 SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
748 /* Save the hash data for output: */
749 #if BYTE_ORDER == LITTLE_ENDIAN
751 /* Convert TO host byte order */
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];
759 MEMCPY_BCOPY(d, context->s1.state, SHA1_DIGEST_LENGTH);
763 MEMSET_BZERO(context, sizeof(*context));
766 char *SHA1_End(SHA_CTX* context, char buffer[]) {
767 sha_byte digest[SHA1_DIGEST_LENGTH], *d = digest;
771 assert(context != (SHA_CTX*)0);
773 if (buffer != (char*)0) {
774 SHA1_Final(digest, context);
776 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
777 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
778 *buffer++ = sha_hex_digits[*d & 0x0f];
783 MEMSET_BZERO(context, sizeof(*context));
785 MEMSET_BZERO(digest, SHA1_DIGEST_LENGTH);
789 char* SHA1_Data(const sha_byte* data, size_t len, char digest[SHA1_DIGEST_STRING_LENGTH]) {
793 SHA1_Update(&context, data, len);
794 return SHA1_End(&context, digest);
798 /*** SHA-256: *********************************************************/
799 void SHA256_Internal_Init(SHA_CTX* context, const sha_word32* ihv) {
801 assert(context != (SHA_CTX*)0);
803 MEMCPY_BCOPY(context->s256.state, ihv, sizeof(sha_word32) * 8);
804 MEMSET_BZERO(context->s256.buffer, 64);
805 context->s256.bitcount = 0;
808 void SHA256_Init(SHA_CTX* context) {
809 SHA256_Internal_Init(context, sha256_initial_hash_value);
812 #ifdef SHA2_UNROLL_TRANSFORM
814 /* Unrolled SHA-256 round macros: */
816 #if BYTE_ORDER == LITTLE_ENDIAN
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)) + \
823 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
827 #else /* BYTE_ORDER == LITTLE_ENDIAN */
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++); \
833 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
836 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
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); \
846 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
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;
854 W256 = (sha_word32*)context->s256.buffer;
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];
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);
879 /* Now for the remaining rounds to 64: */
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);
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;
902 a = b = c = d = e = f = g = h = T1 = 0;
905 #else /* SHA2_UNROLL_TRANSFORM */
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;
912 W256 = (sha_word32*)context->s256.buffer;
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];
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);
949 /* Part of the message block expansion: */
950 s0 = W256[(j+1)&0x0f];
952 s1 = W256[(j+14)&0x0f];
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);
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;
982 a = b = c = d = e = f = g = h = T1 = T2 = 0;
985 #endif /* SHA2_UNROLL_TRANSFORM */
987 void SHA256_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
988 unsigned int freespace, usedspace;
991 /* Calling with no data is valid - we do nothing */
996 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
998 usedspace = (unsigned int)((context->s256.bitcount >> 3) % 64);
1000 /* Calculate how much free space is available in the buffer */
1001 freespace = 64 - usedspace;
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;
1009 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1011 /* The buffer is not yet full */
1012 MEMCPY_BCOPY(&context->s256.buffer[usedspace], data, len);
1013 context->s256.bitcount += len << 3;
1015 usedspace = freespace = 0;
1020 /* Process as many complete blocks as we can */
1021 SHA256_Internal_Transform(context, (sha_word32*)data);
1022 context->s256.bitcount += 512;
1027 /* There's left-overs, so save 'em */
1028 MEMCPY_BCOPY(context->s256.buffer, data, len);
1029 context->s256.bitcount += len << 3;
1032 usedspace = freespace = 0;
1035 void SHA256_Internal_Last(SHA_CTX* context) {
1036 unsigned int usedspace;
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);
1043 if (usedspace > 0) {
1044 /* Begin padding with a 1 bit: */
1045 context->s256.buffer[usedspace++] = 0x80;
1047 if (usedspace <= 56) {
1048 /* Set-up for the last transform: */
1049 MEMSET_BZERO(&context->s256.buffer[usedspace], 56 - usedspace);
1051 if (usedspace < 64) {
1052 MEMSET_BZERO(&context->s256.buffer[usedspace], 64 - usedspace);
1054 /* Do second-to-last transform: */
1055 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1057 /* And set-up for the last transform: */
1058 MEMSET_BZERO(context->s256.buffer, 56);
1063 /* Set-up for the last transform: */
1064 MEMSET_BZERO(context->s256.buffer, 56);
1066 /* Begin padding with a 1 bit: */
1067 *context->s256.buffer = 0x80;
1069 /* Set the bit count: */
1070 *(sha_word64*)&context->s256.buffer[56] = context->s256.bitcount;
1072 /* Final transform: */
1073 SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
1076 void SHA256_Final(sha_byte digest[], SHA_CTX* context) {
1077 sha_word32 *d = (sha_word32*)digest;
1080 assert(context != (SHA_CTX*)0);
1082 /* If no digest buffer is passed, we don't bother doing this: */
1083 if (digest != (sha_byte*)0) {
1084 SHA256_Internal_Last(context);
1086 /* Save the hash data for output: */
1087 #if BYTE_ORDER == LITTLE_ENDIAN
1089 /* Convert TO host byte order */
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];
1097 MEMCPY_BCOPY(d, context->s256.state, SHA256_DIGEST_LENGTH);
1101 /* Clean up state data: */
1102 MEMSET_BZERO(context, sizeof(*context));
1105 char *SHA256_End(SHA_CTX* context, char buffer[]) {
1106 sha_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
1110 assert(context != (SHA_CTX*)0);
1112 if (buffer != (char*)0) {
1113 SHA256_Final(digest, context);
1115 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
1116 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1117 *buffer++ = sha_hex_digits[*d & 0x0f];
1122 MEMSET_BZERO(context, sizeof(*context));
1124 MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
1128 char* SHA256_Data(const sha_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
1131 SHA256_Init(&context);
1132 SHA256_Update(&context, data, len);
1133 return SHA256_End(&context, digest);
1137 /*** SHA-224: *********************************************************/
1138 void SHA224_Init(SHA_CTX* context) {
1139 SHA256_Internal_Init(context, sha224_initial_hash_value);
1142 void SHA224_Internal_Transform(SHA_CTX* context, const sha_word32* data) {
1143 SHA256_Internal_Transform(context, data);
1146 void SHA224_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1147 SHA256_Update(context, data, len);
1150 void SHA224_Final(sha_byte digest[], SHA_CTX* context) {
1151 sha_word32 *d = (sha_word32*)digest;
1154 assert(context != (SHA_CTX*)0);
1156 /* If no digest buffer is passed, we don't bother doing this: */
1157 if (digest != (sha_byte*)0) {
1158 SHA256_Internal_Last(context);
1160 /* Save the hash data for output: */
1161 #if BYTE_ORDER == LITTLE_ENDIAN
1163 /* Convert TO host byte order */
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];
1171 MEMCPY_BCOPY(d, context->s256.state, SHA224_DIGEST_LENGTH);
1175 /* Clean up state data: */
1176 MEMSET_BZERO(context, sizeof(*context));
1179 char *SHA224_End(SHA_CTX* context, char buffer[]) {
1180 sha_byte digest[SHA224_DIGEST_LENGTH], *d = digest;
1184 assert(context != (SHA_CTX*)0);
1186 if (buffer != (char*)0) {
1187 SHA224_Final(digest, context);
1189 for (i = 0; i < SHA224_DIGEST_LENGTH; i++) {
1190 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1191 *buffer++ = sha_hex_digits[*d & 0x0f];
1196 MEMSET_BZERO(context, sizeof(*context));
1198 MEMSET_BZERO(digest, SHA224_DIGEST_LENGTH);
1202 char* SHA224_Data(const sha_byte* data, size_t len, char digest[SHA224_DIGEST_STRING_LENGTH]) {
1205 SHA224_Init(&context);
1206 SHA224_Update(&context, data, len);
1207 return SHA224_End(&context, digest);
1211 /*** SHA-512: *********************************************************/
1212 void SHA512_Internal_Init(SHA_CTX* context, const sha_word64* ihv) {
1214 assert(context != (SHA_CTX*)0);
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;
1221 void SHA512_Init(SHA_CTX* context) {
1222 SHA512_Internal_Init(context, sha512_initial_hash_value);
1225 #ifdef SHA2_UNROLL_TRANSFORM
1227 /* Unrolled SHA-512 round macros: */
1228 #if BYTE_ORDER == LITTLE_ENDIAN
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]; \
1235 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
1239 #else /* BYTE_ORDER == LITTLE_ENDIAN */
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++); \
1245 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
1248 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
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); \
1258 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
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;
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];
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);
1288 /* Now for the remaining rounds up to 79: */
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);
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;
1311 a = b = c = d = e = f = g = h = T1 = 0;
1314 #else /* SHA2_UNROLL_TRANSFORM */
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;
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];
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);
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);
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);
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;
1389 a = b = c = d = e = f = g = h = T1 = T2 = 0;
1392 #endif /* SHA2_UNROLL_TRANSFORM */
1394 void SHA512_Update(SHA_CTX* context, const sha_byte *data, size_t len) {
1395 unsigned int freespace, usedspace;
1398 /* Calling with no data is valid - we do nothing */
1403 assert(context != (SHA_CTX*)0 && data != (sha_byte*)0);
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;
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);
1416 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1418 /* The buffer is not yet full */
1419 MEMCPY_BCOPY(&context->s512.buffer[usedspace], data, len);
1420 ADDINC128(context->s512.bitcount, len << 3);
1422 usedspace = freespace = 0;
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);
1434 /* There's left-overs, so save 'em */
1435 MEMCPY_BCOPY(context->s512.buffer, data, len);
1436 ADDINC128(context->s512.bitcount, len << 3);
1439 usedspace = freespace = 0;
1442 void SHA512_Internal_Last(SHA_CTX* context) {
1443 unsigned int usedspace;
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]);
1451 if (usedspace > 0) {
1452 /* Begin padding with a 1 bit: */
1453 context->s512.buffer[usedspace++] = 0x80;
1455 if (usedspace <= 112) {
1456 /* Set-up for the last transform: */
1457 MEMSET_BZERO(&context->s512.buffer[usedspace], 112 - usedspace);
1459 if (usedspace < 128) {
1460 MEMSET_BZERO(&context->s512.buffer[usedspace], 128 - usedspace);
1462 /* Do second-to-last transform: */
1463 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1465 /* And set-up for the last transform: */
1466 MEMSET_BZERO(context->s512.buffer, 112);
1471 /* Prepare for final transform: */
1472 MEMSET_BZERO(context->s512.buffer, 112);
1474 /* Begin padding with a 1 bit: */
1475 *context->s512.buffer = 0x80;
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];
1481 /* Final transform: */
1482 SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
1485 void SHA512_Final(sha_byte digest[], SHA_CTX* context) {
1486 sha_word64 *d = (sha_word64*)digest;
1489 assert(context != (SHA_CTX*)0);
1491 /* If no digest buffer is passed, we don't bother doing this: */
1492 if (digest != (sha_byte*)0) {
1493 SHA512_Internal_Last(context);
1495 /* Save the hash data for output: */
1496 #if BYTE_ORDER == LITTLE_ENDIAN
1498 /* Convert TO host byte order */
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];
1506 MEMCPY_BCOPY(d, context->s512.state, SHA512_DIGEST_LENGTH);
1510 /* Zero out state data */
1511 MEMSET_BZERO(context, sizeof(*context));
1514 char *SHA512_End(SHA_CTX* context, char buffer[]) {
1515 sha_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
1519 assert(context != (SHA_CTX*)0);
1521 if (buffer != (char*)0) {
1522 SHA512_Final(digest, context);
1524 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
1525 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1526 *buffer++ = sha_hex_digits[*d & 0x0f];
1531 MEMSET_BZERO(context, sizeof(*context));
1533 MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
1537 char* SHA512_Data(const sha_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
1540 SHA512_Init(&context);
1541 SHA512_Update(&context, data, len);
1542 return SHA512_End(&context, digest);
1546 /*** SHA-384: *********************************************************/
1547 void SHA384_Init(SHA_CTX* context) {
1548 SHA512_Internal_Init(context, sha384_initial_hash_value);
1551 void SHA384_Update(SHA_CTX* context, const sha_byte* data, size_t len) {
1552 SHA512_Update(context, data, len);
1555 void SHA384_Final(sha_byte digest[], SHA_CTX* context) {
1556 sha_word64 *d = (sha_word64*)digest;
1559 assert(context != (SHA_CTX*)0);
1561 /* If no digest buffer is passed, we don't bother doing this: */
1562 if (digest != (sha_byte*)0) {
1563 SHA512_Internal_Last(context);
1565 /* Save the hash data for output: */
1566 #if BYTE_ORDER == LITTLE_ENDIAN
1568 /* Convert TO host byte order */
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];
1576 MEMCPY_BCOPY(d, context->s512.state, SHA384_DIGEST_LENGTH);
1580 /* Zero out state data */
1581 MEMSET_BZERO(context, sizeof(*context));
1584 char *SHA384_End(SHA_CTX* context, char buffer[]) {
1585 sha_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
1589 assert(context != (SHA_CTX*)0);
1591 if (buffer != (char*)0) {
1592 SHA384_Final(digest, context);
1594 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1595 *buffer++ = sha_hex_digits[(*d & 0xf0) >> 4];
1596 *buffer++ = sha_hex_digits[*d & 0x0f];
1601 MEMSET_BZERO(context, sizeof(*context));
1603 MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1607 char* SHA384_Data(const sha_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1610 SHA384_Init(&context);
1611 SHA384_Update(&context, data, len);
1612 return SHA384_End(&context, digest);