5 * AUTHOR: Aaron D. Gifford <me@aarongifford.com>
7 * Copyright (c) 2000-2001, Aaron D. Gifford
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the copyright holder nor the names of contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
41 #include <sys/param.h>
46 * UNROLLED TRANSFORM LOOP NOTE:
47 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
48 * loop version for the hash transform rounds (defined using macros
49 * later in this file). Either define on the command line, for example:
51 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
55 * #define SHA2_UNROLL_TRANSFORM
59 /*** SHA-256/384/512 Various Length Definitions ***********************/
60 /* NOTE: Most of these are in sha2.h */
61 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
62 #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
63 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
66 /*** ENDIAN REVERSAL MACROS *******************************************/
67 #ifndef WORDS_BIGENDIAN
68 #define REVERSE32(w,x) { \
70 tmp = (tmp >> 16) | (tmp << 16); \
71 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
73 #define REVERSE64(w,x) { \
75 tmp = (tmp >> 32) | (tmp << 32); \
76 tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
77 ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
78 (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
79 ((tmp & 0x0000ffff0000ffffULL) << 16); \
81 #endif /* not bigendian */
84 * Macro for incrementally adding the unsigned 64-bit integer n to the
85 * unsigned 128-bit integer (represented using a two-element array of
88 #define ADDINC128(w,n) { \
89 (w)[0] += (uint64)(n); \
95 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
97 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
99 * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
100 * S is a ROTATION) because the SHA-256/384/512 description document
101 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
102 * same "backwards" definition.
104 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
105 #define R(b,x) ((x) >> (b))
106 /* 32-bit Rotate-right (used in SHA-256): */
107 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
108 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
109 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
111 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
112 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
113 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
115 /* Four of six logical functions used in SHA-256: */
116 #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
117 #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
118 #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
119 #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
121 /* Four of six logical functions used in SHA-384 and SHA-512: */
122 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
123 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
124 #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
125 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
127 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
128 /* NOTE: These should not be accessed directly from outside this
129 * library -- they are intended for private internal visibility/use
132 static void SHA512_Last(SHA512_CTX
*);
133 static void SHA256_Transform(SHA256_CTX
*, const uint8
*);
134 static void SHA512_Transform(SHA512_CTX
*, const uint8
*);
137 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
138 /* Hash constant words K for SHA-256: */
139 static const uint32 K256
[64] = {
140 0x428a2f98UL
, 0x71374491UL
, 0xb5c0fbcfUL
, 0xe9b5dba5UL
,
141 0x3956c25bUL
, 0x59f111f1UL
, 0x923f82a4UL
, 0xab1c5ed5UL
,
142 0xd807aa98UL
, 0x12835b01UL
, 0x243185beUL
, 0x550c7dc3UL
,
143 0x72be5d74UL
, 0x80deb1feUL
, 0x9bdc06a7UL
, 0xc19bf174UL
,
144 0xe49b69c1UL
, 0xefbe4786UL
, 0x0fc19dc6UL
, 0x240ca1ccUL
,
145 0x2de92c6fUL
, 0x4a7484aaUL
, 0x5cb0a9dcUL
, 0x76f988daUL
,
146 0x983e5152UL
, 0xa831c66dUL
, 0xb00327c8UL
, 0xbf597fc7UL
,
147 0xc6e00bf3UL
, 0xd5a79147UL
, 0x06ca6351UL
, 0x14292967UL
,
148 0x27b70a85UL
, 0x2e1b2138UL
, 0x4d2c6dfcUL
, 0x53380d13UL
,
149 0x650a7354UL
, 0x766a0abbUL
, 0x81c2c92eUL
, 0x92722c85UL
,
150 0xa2bfe8a1UL
, 0xa81a664bUL
, 0xc24b8b70UL
, 0xc76c51a3UL
,
151 0xd192e819UL
, 0xd6990624UL
, 0xf40e3585UL
, 0x106aa070UL
,
152 0x19a4c116UL
, 0x1e376c08UL
, 0x2748774cUL
, 0x34b0bcb5UL
,
153 0x391c0cb3UL
, 0x4ed8aa4aUL
, 0x5b9cca4fUL
, 0x682e6ff3UL
,
154 0x748f82eeUL
, 0x78a5636fUL
, 0x84c87814UL
, 0x8cc70208UL
,
155 0x90befffaUL
, 0xa4506cebUL
, 0xbef9a3f7UL
, 0xc67178f2UL
158 /* Initial hash value H for SHA-224: */
159 static const uint32 sha224_initial_hash_value
[8] = {
170 /* Initial hash value H for SHA-256: */
171 static const uint32 sha256_initial_hash_value
[8] = {
182 /* Hash constant words K for SHA-384 and SHA-512: */
183 static const uint64 K512
[80] = {
184 0x428a2f98d728ae22ULL
, 0x7137449123ef65cdULL
,
185 0xb5c0fbcfec4d3b2fULL
, 0xe9b5dba58189dbbcULL
,
186 0x3956c25bf348b538ULL
, 0x59f111f1b605d019ULL
,
187 0x923f82a4af194f9bULL
, 0xab1c5ed5da6d8118ULL
,
188 0xd807aa98a3030242ULL
, 0x12835b0145706fbeULL
,
189 0x243185be4ee4b28cULL
, 0x550c7dc3d5ffb4e2ULL
,
190 0x72be5d74f27b896fULL
, 0x80deb1fe3b1696b1ULL
,
191 0x9bdc06a725c71235ULL
, 0xc19bf174cf692694ULL
,
192 0xe49b69c19ef14ad2ULL
, 0xefbe4786384f25e3ULL
,
193 0x0fc19dc68b8cd5b5ULL
, 0x240ca1cc77ac9c65ULL
,
194 0x2de92c6f592b0275ULL
, 0x4a7484aa6ea6e483ULL
,
195 0x5cb0a9dcbd41fbd4ULL
, 0x76f988da831153b5ULL
,
196 0x983e5152ee66dfabULL
, 0xa831c66d2db43210ULL
,
197 0xb00327c898fb213fULL
, 0xbf597fc7beef0ee4ULL
,
198 0xc6e00bf33da88fc2ULL
, 0xd5a79147930aa725ULL
,
199 0x06ca6351e003826fULL
, 0x142929670a0e6e70ULL
,
200 0x27b70a8546d22ffcULL
, 0x2e1b21385c26c926ULL
,
201 0x4d2c6dfc5ac42aedULL
, 0x53380d139d95b3dfULL
,
202 0x650a73548baf63deULL
, 0x766a0abb3c77b2a8ULL
,
203 0x81c2c92e47edaee6ULL
, 0x92722c851482353bULL
,
204 0xa2bfe8a14cf10364ULL
, 0xa81a664bbc423001ULL
,
205 0xc24b8b70d0f89791ULL
, 0xc76c51a30654be30ULL
,
206 0xd192e819d6ef5218ULL
, 0xd69906245565a910ULL
,
207 0xf40e35855771202aULL
, 0x106aa07032bbd1b8ULL
,
208 0x19a4c116b8d2d0c8ULL
, 0x1e376c085141ab53ULL
,
209 0x2748774cdf8eeb99ULL
, 0x34b0bcb5e19b48a8ULL
,
210 0x391c0cb3c5c95a63ULL
, 0x4ed8aa4ae3418acbULL
,
211 0x5b9cca4f7763e373ULL
, 0x682e6ff3d6b2b8a3ULL
,
212 0x748f82ee5defb2fcULL
, 0x78a5636f43172f60ULL
,
213 0x84c87814a1f0ab72ULL
, 0x8cc702081a6439ecULL
,
214 0x90befffa23631e28ULL
, 0xa4506cebde82bde9ULL
,
215 0xbef9a3f7b2c67915ULL
, 0xc67178f2e372532bULL
,
216 0xca273eceea26619cULL
, 0xd186b8c721c0c207ULL
,
217 0xeada7dd6cde0eb1eULL
, 0xf57d4f7fee6ed178ULL
,
218 0x06f067aa72176fbaULL
, 0x0a637dc5a2c898a6ULL
,
219 0x113f9804bef90daeULL
, 0x1b710b35131c471bULL
,
220 0x28db77f523047d84ULL
, 0x32caab7b40c72493ULL
,
221 0x3c9ebe0a15c9bebcULL
, 0x431d67c49c100d4cULL
,
222 0x4cc5d4becb3e42b6ULL
, 0x597f299cfc657e2aULL
,
223 0x5fcb6fab3ad6faecULL
, 0x6c44198c4a475817ULL
226 /* Initial hash value H for SHA-384 */
227 static const uint64 sha384_initial_hash_value
[8] = {
228 0xcbbb9d5dc1059ed8ULL
,
229 0x629a292a367cd507ULL
,
230 0x9159015a3070dd17ULL
,
231 0x152fecd8f70e5939ULL
,
232 0x67332667ffc00b31ULL
,
233 0x8eb44a8768581511ULL
,
234 0xdb0c2e0d64f98fa7ULL
,
235 0x47b5481dbefa4fa4ULL
238 /* Initial hash value H for SHA-512 */
239 static const uint64 sha512_initial_hash_value
[8] = {
240 0x6a09e667f3bcc908ULL
,
241 0xbb67ae8584caa73bULL
,
242 0x3c6ef372fe94f82bULL
,
243 0xa54ff53a5f1d36f1ULL
,
244 0x510e527fade682d1ULL
,
245 0x9b05688c2b3e6c1fULL
,
246 0x1f83d9abfb41bd6bULL
,
247 0x5be0cd19137e2179ULL
251 /*** SHA-256: *********************************************************/
253 SHA256_Init(SHA256_CTX
*context
)
257 memcpy(context
->state
, sha256_initial_hash_value
, SHA256_DIGEST_LENGTH
);
258 memset(context
->buffer
, 0, SHA256_BLOCK_LENGTH
);
259 context
->bitcount
= 0;
262 #ifdef SHA2_UNROLL_TRANSFORM
264 /* Unrolled SHA-256 round macros: */
266 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \
267 W256[j] = (uint32)data[3] | ((uint32)data[2] << 8) | \
268 ((uint32)data[1] << 16) | ((uint32)data[0] << 24); \
270 T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
272 (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
276 #define ROUND256(a,b,c,d,e,f,g,h) do { \
277 s0 = W256[(j+1)&0x0f]; \
278 s0 = sigma0_256(s0); \
279 s1 = W256[(j+14)&0x0f]; \
280 s1 = sigma1_256(s1); \
281 T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \
282 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
284 (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
289 SHA256_Transform(SHA256_CTX
*context
, const uint8
*data
)
305 W256
= (uint32
*) context
->buffer
;
307 /* Initialize registers with the prev. intermediate value */
308 a
= context
->state
[0];
309 b
= context
->state
[1];
310 c
= context
->state
[2];
311 d
= context
->state
[3];
312 e
= context
->state
[4];
313 f
= context
->state
[5];
314 g
= context
->state
[6];
315 h
= context
->state
[7];
320 /* Rounds 0 to 15 (unrolled): */
321 ROUND256_0_TO_15(a
, b
, c
, d
, e
, f
, g
, h
);
322 ROUND256_0_TO_15(h
, a
, b
, c
, d
, e
, f
, g
);
323 ROUND256_0_TO_15(g
, h
, a
, b
, c
, d
, e
, f
);
324 ROUND256_0_TO_15(f
, g
, h
, a
, b
, c
, d
, e
);
325 ROUND256_0_TO_15(e
, f
, g
, h
, a
, b
, c
, d
);
326 ROUND256_0_TO_15(d
, e
, f
, g
, h
, a
, b
, c
);
327 ROUND256_0_TO_15(c
, d
, e
, f
, g
, h
, a
, b
);
328 ROUND256_0_TO_15(b
, c
, d
, e
, f
, g
, h
, a
);
331 /* Now for the remaining rounds to 64: */
334 ROUND256(a
, b
, c
, d
, e
, f
, g
, h
);
335 ROUND256(h
, a
, b
, c
, d
, e
, f
, g
);
336 ROUND256(g
, h
, a
, b
, c
, d
, e
, f
);
337 ROUND256(f
, g
, h
, a
, b
, c
, d
, e
);
338 ROUND256(e
, f
, g
, h
, a
, b
, c
, d
);
339 ROUND256(d
, e
, f
, g
, h
, a
, b
, c
);
340 ROUND256(c
, d
, e
, f
, g
, h
, a
, b
);
341 ROUND256(b
, c
, d
, e
, f
, g
, h
, a
);
344 /* Compute the current intermediate hash value */
345 context
->state
[0] += a
;
346 context
->state
[1] += b
;
347 context
->state
[2] += c
;
348 context
->state
[3] += d
;
349 context
->state
[4] += e
;
350 context
->state
[5] += f
;
351 context
->state
[6] += g
;
352 context
->state
[7] += h
;
355 a
= b
= c
= d
= e
= f
= g
= h
= T1
= 0;
357 #else /* SHA2_UNROLL_TRANSFORM */
360 SHA256_Transform(SHA256_CTX
*context
, const uint8
*data
)
377 W256
= (uint32
*) context
->buffer
;
379 /* Initialize registers with the prev. intermediate value */
380 a
= context
->state
[0];
381 b
= context
->state
[1];
382 c
= context
->state
[2];
383 d
= context
->state
[3];
384 e
= context
->state
[4];
385 f
= context
->state
[5];
386 g
= context
->state
[6];
387 h
= context
->state
[7];
392 W256
[j
] = (uint32
) data
[3] | ((uint32
) data
[2] << 8) |
393 ((uint32
) data
[1] << 16) | ((uint32
) data
[0] << 24);
395 /* Apply the SHA-256 compression function to update a..h */
396 T1
= h
+ Sigma1_256(e
) + Ch(e
, f
, g
) + K256
[j
] + W256
[j
];
397 T2
= Sigma0_256(a
) + Maj(a
, b
, c
);
412 /* Part of the message block expansion: */
413 s0
= W256
[(j
+ 1) & 0x0f];
415 s1
= W256
[(j
+ 14) & 0x0f];
418 /* Apply the SHA-256 compression function to update a..h */
419 T1
= h
+ Sigma1_256(e
) + Ch(e
, f
, g
) + K256
[j
] +
420 (W256
[j
& 0x0f] += s1
+ W256
[(j
+ 9) & 0x0f] + s0
);
421 T2
= Sigma0_256(a
) + Maj(a
, b
, c
);
434 /* Compute the current intermediate hash value */
435 context
->state
[0] += a
;
436 context
->state
[1] += b
;
437 context
->state
[2] += c
;
438 context
->state
[3] += d
;
439 context
->state
[4] += e
;
440 context
->state
[5] += f
;
441 context
->state
[6] += g
;
442 context
->state
[7] += h
;
445 a
= b
= c
= d
= e
= f
= g
= h
= T1
= T2
= 0;
447 #endif /* SHA2_UNROLL_TRANSFORM */
450 SHA256_Update(SHA256_CTX
*context
, const uint8
*data
, size_t len
)
455 /* Calling with no data is valid (we do nothing) */
459 usedspace
= (context
->bitcount
>> 3) % SHA256_BLOCK_LENGTH
;
462 /* Calculate how much free space is available in the buffer */
463 freespace
= SHA256_BLOCK_LENGTH
- usedspace
;
465 if (len
>= freespace
)
467 /* Fill the buffer completely and process it */
468 memcpy(&context
->buffer
[usedspace
], data
, freespace
);
469 context
->bitcount
+= freespace
<< 3;
472 SHA256_Transform(context
, context
->buffer
);
476 /* The buffer is not yet full */
477 memcpy(&context
->buffer
[usedspace
], data
, len
);
478 context
->bitcount
+= len
<< 3;
480 usedspace
= freespace
= 0;
484 while (len
>= SHA256_BLOCK_LENGTH
)
486 /* Process as many complete blocks as we can */
487 SHA256_Transform(context
, data
);
488 context
->bitcount
+= SHA256_BLOCK_LENGTH
<< 3;
489 len
-= SHA256_BLOCK_LENGTH
;
490 data
+= SHA256_BLOCK_LENGTH
;
494 /* There's left-overs, so save 'em */
495 memcpy(context
->buffer
, data
, len
);
496 context
->bitcount
+= len
<< 3;
499 usedspace
= freespace
= 0;
503 SHA256_Last(SHA256_CTX
*context
)
505 unsigned int usedspace
;
507 usedspace
= (context
->bitcount
>> 3) % SHA256_BLOCK_LENGTH
;
508 #ifndef WORDS_BIGENDIAN
509 /* Convert FROM host byte order */
510 REVERSE64(context
->bitcount
, context
->bitcount
);
514 /* Begin padding with a 1 bit: */
515 context
->buffer
[usedspace
++] = 0x80;
517 if (usedspace
<= SHA256_SHORT_BLOCK_LENGTH
)
519 /* Set-up for the last transform: */
520 memset(&context
->buffer
[usedspace
], 0, SHA256_SHORT_BLOCK_LENGTH
- usedspace
);
524 if (usedspace
< SHA256_BLOCK_LENGTH
)
526 memset(&context
->buffer
[usedspace
], 0, SHA256_BLOCK_LENGTH
- usedspace
);
528 /* Do second-to-last transform: */
529 SHA256_Transform(context
, context
->buffer
);
531 /* And set-up for the last transform: */
532 memset(context
->buffer
, 0, SHA256_SHORT_BLOCK_LENGTH
);
537 /* Set-up for the last transform: */
538 memset(context
->buffer
, 0, SHA256_SHORT_BLOCK_LENGTH
);
540 /* Begin padding with a 1 bit: */
541 *context
->buffer
= 0x80;
543 /* Set the bit count: */
544 *(uint64
*) &context
->buffer
[SHA256_SHORT_BLOCK_LENGTH
] = context
->bitcount
;
546 /* Final transform: */
547 SHA256_Transform(context
, context
->buffer
);
551 SHA256_Final(uint8 digest
[], SHA256_CTX
*context
)
553 /* If no digest buffer is passed, we don't bother doing this: */
556 SHA256_Last(context
);
558 #ifndef WORDS_BIGENDIAN
560 /* Convert TO host byte order */
563 for (j
= 0; j
< 8; j
++)
565 REVERSE32(context
->state
[j
], context
->state
[j
]);
569 memcpy(digest
, context
->state
, SHA256_DIGEST_LENGTH
);
572 /* Clean up state data: */
573 memset(context
, 0, sizeof(*context
));
577 /*** SHA-512: *********************************************************/
579 SHA512_Init(SHA512_CTX
*context
)
583 memcpy(context
->state
, sha512_initial_hash_value
, SHA512_DIGEST_LENGTH
);
584 memset(context
->buffer
, 0, SHA512_BLOCK_LENGTH
);
585 context
->bitcount
[0] = context
->bitcount
[1] = 0;
588 #ifdef SHA2_UNROLL_TRANSFORM
590 /* Unrolled SHA-512 round macros: */
592 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do { \
593 W512[j] = (uint64)data[7] | ((uint64)data[6] << 8) | \
594 ((uint64)data[5] << 16) | ((uint64)data[4] << 24) | \
595 ((uint64)data[3] << 32) | ((uint64)data[2] << 40) | \
596 ((uint64)data[1] << 48) | ((uint64)data[0] << 56); \
598 T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
600 (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
605 #define ROUND512(a,b,c,d,e,f,g,h) do { \
606 s0 = W512[(j+1)&0x0f]; \
607 s0 = sigma0_512(s0); \
608 s1 = W512[(j+14)&0x0f]; \
609 s1 = sigma1_512(s1); \
610 T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + \
611 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
613 (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
618 SHA512_Transform(SHA512_CTX
*context
, const uint8
*data
)
631 *W512
= (uint64
*) context
->buffer
;
634 /* Initialize registers with the prev. intermediate value */
635 a
= context
->state
[0];
636 b
= context
->state
[1];
637 c
= context
->state
[2];
638 d
= context
->state
[3];
639 e
= context
->state
[4];
640 f
= context
->state
[5];
641 g
= context
->state
[6];
642 h
= context
->state
[7];
647 ROUND512_0_TO_15(a
, b
, c
, d
, e
, f
, g
, h
);
648 ROUND512_0_TO_15(h
, a
, b
, c
, d
, e
, f
, g
);
649 ROUND512_0_TO_15(g
, h
, a
, b
, c
, d
, e
, f
);
650 ROUND512_0_TO_15(f
, g
, h
, a
, b
, c
, d
, e
);
651 ROUND512_0_TO_15(e
, f
, g
, h
, a
, b
, c
, d
);
652 ROUND512_0_TO_15(d
, e
, f
, g
, h
, a
, b
, c
);
653 ROUND512_0_TO_15(c
, d
, e
, f
, g
, h
, a
, b
);
654 ROUND512_0_TO_15(b
, c
, d
, e
, f
, g
, h
, a
);
657 /* Now for the remaining rounds up to 79: */
660 ROUND512(a
, b
, c
, d
, e
, f
, g
, h
);
661 ROUND512(h
, a
, b
, c
, d
, e
, f
, g
);
662 ROUND512(g
, h
, a
, b
, c
, d
, e
, f
);
663 ROUND512(f
, g
, h
, a
, b
, c
, d
, e
);
664 ROUND512(e
, f
, g
, h
, a
, b
, c
, d
);
665 ROUND512(d
, e
, f
, g
, h
, a
, b
, c
);
666 ROUND512(c
, d
, e
, f
, g
, h
, a
, b
);
667 ROUND512(b
, c
, d
, e
, f
, g
, h
, a
);
670 /* Compute the current intermediate hash value */
671 context
->state
[0] += a
;
672 context
->state
[1] += b
;
673 context
->state
[2] += c
;
674 context
->state
[3] += d
;
675 context
->state
[4] += e
;
676 context
->state
[5] += f
;
677 context
->state
[6] += g
;
678 context
->state
[7] += h
;
681 a
= b
= c
= d
= e
= f
= g
= h
= T1
= 0;
683 #else /* SHA2_UNROLL_TRANSFORM */
686 SHA512_Transform(SHA512_CTX
*context
, const uint8
*data
)
700 *W512
= (uint64
*) context
->buffer
;
703 /* Initialize registers with the prev. intermediate value */
704 a
= context
->state
[0];
705 b
= context
->state
[1];
706 c
= context
->state
[2];
707 d
= context
->state
[3];
708 e
= context
->state
[4];
709 f
= context
->state
[5];
710 g
= context
->state
[6];
711 h
= context
->state
[7];
716 W512
[j
] = (uint64
) data
[7] | ((uint64
) data
[6] << 8) |
717 ((uint64
) data
[5] << 16) | ((uint64
) data
[4] << 24) |
718 ((uint64
) data
[3] << 32) | ((uint64
) data
[2] << 40) |
719 ((uint64
) data
[1] << 48) | ((uint64
) data
[0] << 56);
721 /* Apply the SHA-512 compression function to update a..h */
722 T1
= h
+ Sigma1_512(e
) + Ch(e
, f
, g
) + K512
[j
] + W512
[j
];
723 T2
= Sigma0_512(a
) + Maj(a
, b
, c
);
738 /* Part of the message block expansion: */
739 s0
= W512
[(j
+ 1) & 0x0f];
741 s1
= W512
[(j
+ 14) & 0x0f];
744 /* Apply the SHA-512 compression function to update a..h */
745 T1
= h
+ Sigma1_512(e
) + Ch(e
, f
, g
) + K512
[j
] +
746 (W512
[j
& 0x0f] += s1
+ W512
[(j
+ 9) & 0x0f] + s0
);
747 T2
= Sigma0_512(a
) + Maj(a
, b
, c
);
760 /* Compute the current intermediate hash value */
761 context
->state
[0] += a
;
762 context
->state
[1] += b
;
763 context
->state
[2] += c
;
764 context
->state
[3] += d
;
765 context
->state
[4] += e
;
766 context
->state
[5] += f
;
767 context
->state
[6] += g
;
768 context
->state
[7] += h
;
771 a
= b
= c
= d
= e
= f
= g
= h
= T1
= T2
= 0;
773 #endif /* SHA2_UNROLL_TRANSFORM */
776 SHA512_Update(SHA512_CTX
*context
, const uint8
*data
, size_t len
)
781 /* Calling with no data is valid (we do nothing) */
785 usedspace
= (context
->bitcount
[0] >> 3) % SHA512_BLOCK_LENGTH
;
788 /* Calculate how much free space is available in the buffer */
789 freespace
= SHA512_BLOCK_LENGTH
- usedspace
;
791 if (len
>= freespace
)
793 /* Fill the buffer completely and process it */
794 memcpy(&context
->buffer
[usedspace
], data
, freespace
);
795 ADDINC128(context
->bitcount
, freespace
<< 3);
798 SHA512_Transform(context
, context
->buffer
);
802 /* The buffer is not yet full */
803 memcpy(&context
->buffer
[usedspace
], data
, len
);
804 ADDINC128(context
->bitcount
, len
<< 3);
806 usedspace
= freespace
= 0;
810 while (len
>= SHA512_BLOCK_LENGTH
)
812 /* Process as many complete blocks as we can */
813 SHA512_Transform(context
, data
);
814 ADDINC128(context
->bitcount
, SHA512_BLOCK_LENGTH
<< 3);
815 len
-= SHA512_BLOCK_LENGTH
;
816 data
+= SHA512_BLOCK_LENGTH
;
820 /* There's left-overs, so save 'em */
821 memcpy(context
->buffer
, data
, len
);
822 ADDINC128(context
->bitcount
, len
<< 3);
825 usedspace
= freespace
= 0;
829 SHA512_Last(SHA512_CTX
*context
)
831 unsigned int usedspace
;
833 usedspace
= (context
->bitcount
[0] >> 3) % SHA512_BLOCK_LENGTH
;
834 #ifndef WORDS_BIGENDIAN
835 /* Convert FROM host byte order */
836 REVERSE64(context
->bitcount
[0], context
->bitcount
[0]);
837 REVERSE64(context
->bitcount
[1], context
->bitcount
[1]);
841 /* Begin padding with a 1 bit: */
842 context
->buffer
[usedspace
++] = 0x80;
844 if (usedspace
<= SHA512_SHORT_BLOCK_LENGTH
)
846 /* Set-up for the last transform: */
847 memset(&context
->buffer
[usedspace
], 0, SHA512_SHORT_BLOCK_LENGTH
- usedspace
);
851 if (usedspace
< SHA512_BLOCK_LENGTH
)
853 memset(&context
->buffer
[usedspace
], 0, SHA512_BLOCK_LENGTH
- usedspace
);
855 /* Do second-to-last transform: */
856 SHA512_Transform(context
, context
->buffer
);
858 /* And set-up for the last transform: */
859 memset(context
->buffer
, 0, SHA512_BLOCK_LENGTH
- 2);
864 /* Prepare for final transform: */
865 memset(context
->buffer
, 0, SHA512_SHORT_BLOCK_LENGTH
);
867 /* Begin padding with a 1 bit: */
868 *context
->buffer
= 0x80;
870 /* Store the length of input data (in bits): */
871 *(uint64
*) &context
->buffer
[SHA512_SHORT_BLOCK_LENGTH
] = context
->bitcount
[1];
872 *(uint64
*) &context
->buffer
[SHA512_SHORT_BLOCK_LENGTH
+ 8] = context
->bitcount
[0];
874 /* Final transform: */
875 SHA512_Transform(context
, context
->buffer
);
879 SHA512_Final(uint8 digest
[], SHA512_CTX
*context
)
881 /* If no digest buffer is passed, we don't bother doing this: */
884 SHA512_Last(context
);
886 /* Save the hash data for output: */
887 #ifndef WORDS_BIGENDIAN
889 /* Convert TO host byte order */
892 for (j
= 0; j
< 8; j
++)
894 REVERSE64(context
->state
[j
], context
->state
[j
]);
898 memcpy(digest
, context
->state
, SHA512_DIGEST_LENGTH
);
901 /* Zero out state data */
902 memset(context
, 0, sizeof(*context
));
906 /*** SHA-384: *********************************************************/
908 SHA384_Init(SHA384_CTX
*context
)
912 memcpy(context
->state
, sha384_initial_hash_value
, SHA512_DIGEST_LENGTH
);
913 memset(context
->buffer
, 0, SHA384_BLOCK_LENGTH
);
914 context
->bitcount
[0] = context
->bitcount
[1] = 0;
918 SHA384_Update(SHA384_CTX
*context
, const uint8
*data
, size_t len
)
920 SHA512_Update((SHA512_CTX
*) context
, data
, len
);
924 SHA384_Final(uint8 digest
[], SHA384_CTX
*context
)
926 /* If no digest buffer is passed, we don't bother doing this: */
929 SHA512_Last((SHA512_CTX
*) context
);
931 /* Save the hash data for output: */
932 #ifndef WORDS_BIGENDIAN
934 /* Convert TO host byte order */
937 for (j
= 0; j
< 6; j
++)
939 REVERSE64(context
->state
[j
], context
->state
[j
]);
943 memcpy(digest
, context
->state
, SHA384_DIGEST_LENGTH
);
946 /* Zero out state data */
947 memset(context
, 0, sizeof(*context
));
950 /*** SHA-224: *********************************************************/
952 SHA224_Init(SHA224_CTX
*context
)
956 memcpy(context
->state
, sha224_initial_hash_value
, SHA256_DIGEST_LENGTH
);
957 memset(context
->buffer
, 0, SHA256_BLOCK_LENGTH
);
958 context
->bitcount
= 0;
962 SHA224_Update(SHA224_CTX
*context
, const uint8
*data
, size_t len
)
964 SHA256_Update((SHA256_CTX
*) context
, data
, len
);
968 SHA224_Final(uint8 digest
[], SHA224_CTX
*context
)
970 /* If no digest buffer is passed, we don't bother doing this: */
973 SHA256_Last(context
);
975 #ifndef WORDS_BIGENDIAN
977 /* Convert TO host byte order */
980 for (j
= 0; j
< 8; j
++)
982 REVERSE32(context
->state
[j
], context
->state
[j
]);
986 memcpy(digest
, context
->state
, SHA224_DIGEST_LENGTH
);
989 /* Clean up state data: */
990 memset(context
, 0, sizeof(*context
));