1 /* $KAME: sha2.c,v 1.11 2004/06/02 09:52:45 itojun Exp $ */
8 * Written by Aaron D. Gifford <me@aarongifford.com>
10 * Copyright 2000 Aaron D. Gifford. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the copyright holder nor the names of contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
44 #include <sys/systm.h>
48 #include <machine/endian.h>
49 #include <crypto/sha2/sha2.h>
53 * Some sanity checking code is included using assert(). On my FreeBSD
54 * system, this additional code can be removed by compiling with NDEBUG
55 * defined. Check your own systems manpage on assert() to see how to
56 * compile WITHOUT the sanity checking code on your system.
58 * UNROLLED TRANSFORM LOOP NOTE:
59 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
60 * loop version for the hash transform rounds (defined using macros
61 * later in this file). Either define on the command line, for example:
63 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
67 * #define SHA2_UNROLL_TRANSFORM
71 #if defined(_KERNEL) && defined(__FreeBSD__)
78 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
82 * Please make sure that your system defines BYTE_ORDER. If your
83 * architecture is little-endian, make sure it also defines
84 * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
87 * If your system does not define the above, then you can do so by
90 * #define LITTLE_ENDIAN 1234
91 * #define BIG_ENDIAN 4321
93 * And for little-endian machines, add:
95 * #define BYTE_ORDER LITTLE_ENDIAN
97 * Or for big-endian machines:
99 * #define BYTE_ORDER BIG_ENDIAN
101 * The FreeBSD machine this was written on defines BYTE_ORDER
102 * appropriately by including <sys/types.h> (which in turn includes
103 * <machine/endian.h> where the appropriate definitions are actually
106 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
107 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
111 * Define the followingsha2_* types to types of the correct length on
112 * the native archtecture. Most BSD systems and Linux define u_intXX_t
113 * types. Machines with very recent ANSI C headers, can use the
114 * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
115 * during compile or in the sha.h header file.
117 * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
118 * will need to define these three typedefs below (and the appropriate
119 * ones in sha.h too) by hand according to their system architecture.
121 * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
122 * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
124 typedef uint8_t sha2_byte; /* Exactly 1 byte */
125 typedef uint32_t sha2_word32; /* Exactly 4 bytes */
126 typedef uint64_t sha2_word64; /* Exactly 8 bytes */
129 /*** SHA-256/384/512 Various Length Definitions ***********************/
130 /* NOTE: Most of these are in sha2.h */
131 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
132 #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
133 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
136 /*** ENDIAN REVERSAL MACROS *******************************************/
137 #if BYTE_ORDER == LITTLE_ENDIAN
138 #define REVERSE32(w,x) { \
139 sha2_word32 tmp = (w); \
140 tmp = (tmp >> 16) | (tmp << 16); \
141 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
143 #define REVERSE64(w,x) { \
144 sha2_word64 tmp = (w); \
145 tmp = (tmp >> 32) | (tmp << 32); \
146 tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
147 ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
148 (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
149 ((tmp & 0x0000ffff0000ffffULL) << 16); \
151 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
154 * Macro for incrementally adding the unsigned 64-bit integer n to the
155 * unsigned 128-bit integer (represented using a two-element array of
158 #define ADDINC128(w,n) { \
159 (w)[0] += (sha2_word64)(n); \
160 if ((w)[0] < (n)) { \
165 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
167 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
169 * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
170 * S is a ROTATION) because the SHA-256/384/512 description document
171 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
172 * same "backwards" definition.
174 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
175 #define R(b,x) ((x) >> (b))
176 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
177 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
179 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
180 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
181 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
183 /* Four of six logical functions used in SHA-384 and SHA-512: */
184 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
185 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
186 #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
187 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
189 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
190 /* NOTE: These should not be accessed directly from outside this
191 * library -- they are intended for private internal visibility/use
194 static void SHA512_Last(SHA512_CTX*);
195 static void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
198 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
199 /* Hash constant words K for SHA-384 and SHA-512: */
200 static const sha2_word64 K512[80] = {
201 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
202 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
203 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
204 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
205 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
206 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
207 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
208 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
209 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
210 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
211 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
212 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
213 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
214 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
215 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
216 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
217 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
218 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
219 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
220 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
221 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
222 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
223 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
224 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
225 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
226 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
227 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
228 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
229 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
230 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
231 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
232 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
233 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
234 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
235 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
236 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
237 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
238 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
239 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
240 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
243 /* Initial hash value H for SHA-384 */
244 static const sha2_word64 sha384_initial_hash_value[8] = {
245 0xcbbb9d5dc1059ed8ULL,
246 0x629a292a367cd507ULL,
247 0x9159015a3070dd17ULL,
248 0x152fecd8f70e5939ULL,
249 0x67332667ffc00b31ULL,
250 0x8eb44a8768581511ULL,
251 0xdb0c2e0d64f98fa7ULL,
252 0x47b5481dbefa4fa4ULL
255 /* Initial hash value H for SHA-512 */
256 static const sha2_word64 sha512_initial_hash_value[8] = {
257 0x6a09e667f3bcc908ULL,
258 0xbb67ae8584caa73bULL,
259 0x3c6ef372fe94f82bULL,
260 0xa54ff53a5f1d36f1ULL,
261 0x510e527fade682d1ULL,
262 0x9b05688c2b3e6c1fULL,
263 0x1f83d9abfb41bd6bULL,
264 0x5be0cd19137e2179ULL
268 * Constant used by SHA256/384/512_End() functions for converting the
269 * digest to a readable hexadecimal character string:
271 static const char *sha2_hex_digits = "0123456789abcdef";
274 /*** SHA-256: *********************************************************/
275 char *SHA256_End(SHA256_CTX* context, char buffer[]) {
276 sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
280 assert(context != (SHA256_CTX*)0);
282 if (buffer != (char*)0) {
283 SHA256_Final(digest, context);
285 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
286 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
287 *buffer++ = sha2_hex_digits[*d & 0x0f];
292 bzero(context, sizeof(*context));
294 bzero(digest, SHA256_DIGEST_LENGTH);
298 char* SHA256_Data(const void *data, unsigned int len, char *digest) {
301 SHA256_Init(&context);
302 SHA256_Update(&context, data, len);
303 return SHA256_End(&context, digest);
307 /*** SHA-512: *********************************************************/
308 void SHA512_Init(SHA512_CTX* context) {
309 if (context == (SHA512_CTX*)0) {
312 bcopy(sha512_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
313 bzero(context->buffer, SHA512_BLOCK_LENGTH);
314 context->bitcount[0] = context->bitcount[1] = 0;
317 #ifdef SHA2_UNROLL_TRANSFORM
319 /* Unrolled SHA-512 round macros: */
320 #if BYTE_ORDER == LITTLE_ENDIAN
322 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
323 REVERSE64(*data++, W512[j]); \
324 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
327 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
331 #else /* BYTE_ORDER == LITTLE_ENDIAN */
333 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
334 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
335 K512[j] + (W512[j] = *data++); \
337 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
340 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
342 #define ROUND512(a,b,c,d,e,f,g,h) \
343 s0 = W512[(j+1)&0x0f]; \
344 s0 = sigma0_512(s0); \
345 s1 = W512[(j+14)&0x0f]; \
346 s1 = sigma1_512(s1); \
347 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
348 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
350 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
353 static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
354 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
355 sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
358 /* Initialize registers with the prev. intermediate value */
359 a = context->state[0];
360 b = context->state[1];
361 c = context->state[2];
362 d = context->state[3];
363 e = context->state[4];
364 f = context->state[5];
365 g = context->state[6];
366 h = context->state[7];
370 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
371 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
372 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
373 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
374 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
375 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
376 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
377 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
380 /* Now for the remaining rounds up to 79: */
382 ROUND512(a,b,c,d,e,f,g,h);
383 ROUND512(h,a,b,c,d,e,f,g);
384 ROUND512(g,h,a,b,c,d,e,f);
385 ROUND512(f,g,h,a,b,c,d,e);
386 ROUND512(e,f,g,h,a,b,c,d);
387 ROUND512(d,e,f,g,h,a,b,c);
388 ROUND512(c,d,e,f,g,h,a,b);
389 ROUND512(b,c,d,e,f,g,h,a);
392 /* Compute the current intermediate hash value */
393 context->state[0] += a;
394 context->state[1] += b;
395 context->state[2] += c;
396 context->state[3] += d;
397 context->state[4] += e;
398 context->state[5] += f;
399 context->state[6] += g;
400 context->state[7] += h;
403 a = b = c = d = e = f = g = h = T1 = 0;
406 #else /* SHA2_UNROLL_TRANSFORM */
408 static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
409 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
410 sha2_word64 T1 = 0, T2 = 0, *W512 = (sha2_word64*)context->buffer;
413 /* Initialize registers with the prev. intermediate value */
414 a = context->state[0];
415 b = context->state[1];
416 c = context->state[2];
417 d = context->state[3];
418 e = context->state[4];
419 f = context->state[5];
420 g = context->state[6];
421 h = context->state[7];
425 #if BYTE_ORDER == LITTLE_ENDIAN
426 /* Convert TO host byte order */
427 REVERSE64(*data++, W512[j]);
428 /* Apply the SHA-512 compression function to update a..h */
429 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
430 #else /* BYTE_ORDER == LITTLE_ENDIAN */
431 /* Apply the SHA-512 compression function to update a..h with copy */
432 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
433 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
434 T2 = Sigma0_512(a) + Maj(a, b, c);
448 /* Part of the message block expansion: */
449 s0 = W512[(j+1)&0x0f];
451 s1 = W512[(j+14)&0x0f];
454 /* Apply the SHA-512 compression function to update a..h */
455 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
456 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
457 T2 = Sigma0_512(a) + Maj(a, b, c);
470 /* Compute the current intermediate hash value */
471 context->state[0] += a;
472 context->state[1] += b;
473 context->state[2] += c;
474 context->state[3] += d;
475 context->state[4] += e;
476 context->state[5] += f;
477 context->state[6] += g;
478 context->state[7] += h;
481 a = b = c = d = e = f = g = h = T1 = T2 = 0;
484 #endif /* SHA2_UNROLL_TRANSFORM */
486 void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
487 unsigned int freespace, usedspace;
490 /* Calling with no data is valid - we do nothing */
495 assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
497 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
499 /* Calculate how much free space is available in the buffer */
500 freespace = SHA512_BLOCK_LENGTH - usedspace;
502 if (len >= freespace) {
503 /* Fill the buffer completely and process it */
504 bcopy(data, &context->buffer[usedspace], freespace);
505 ADDINC128(context->bitcount, freespace << 3);
508 SHA512_Transform(context, (sha2_word64*)context->buffer);
510 /* The buffer is not yet full */
511 bcopy(data, &context->buffer[usedspace], len);
512 ADDINC128(context->bitcount, len << 3);
514 usedspace = freespace = 0;
518 while (len >= SHA512_BLOCK_LENGTH) {
519 /* Process as many complete blocks as we can */
520 SHA512_Transform(context, (const sha2_word64*)data);
521 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
522 len -= SHA512_BLOCK_LENGTH;
523 data += SHA512_BLOCK_LENGTH;
526 /* There's left-overs, so save 'em */
527 bcopy(data, context->buffer, len);
528 ADDINC128(context->bitcount, len << 3);
531 usedspace = freespace = 0;
534 static void SHA512_Last(SHA512_CTX* context) {
535 unsigned int usedspace;
537 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
538 #if BYTE_ORDER == LITTLE_ENDIAN
539 /* Convert FROM host byte order */
540 REVERSE64(context->bitcount[0],context->bitcount[0]);
541 REVERSE64(context->bitcount[1],context->bitcount[1]);
544 /* Begin padding with a 1 bit: */
545 context->buffer[usedspace++] = 0x80;
547 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
548 /* Set-up for the last transform: */
549 bzero(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
551 if (usedspace < SHA512_BLOCK_LENGTH) {
552 bzero(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
554 /* Do second-to-last transform: */
555 SHA512_Transform(context, (sha2_word64*)context->buffer);
557 /* And set-up for the last transform: */
558 bzero(context->buffer, SHA512_BLOCK_LENGTH - 2);
561 /* Prepare for final transform: */
562 bzero(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
564 /* Begin padding with a 1 bit: */
565 *context->buffer = 0x80;
567 /* Store the length of input data (in bits): */
568 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
569 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
571 /* Final transform: */
572 SHA512_Transform(context, (sha2_word64*)context->buffer);
575 void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
576 sha2_word64 *d = (sha2_word64*)digest;
579 assert(context != (SHA512_CTX*)0);
581 /* If no digest buffer is passed, we don't bother doing this: */
582 if (digest != (sha2_byte*)0) {
583 SHA512_Last(context);
585 /* Save the hash data for output: */
586 #if BYTE_ORDER == LITTLE_ENDIAN
588 /* Convert TO host byte order */
590 for (j = 0; j < 8; j++) {
591 REVERSE64(context->state[j],context->state[j]);
592 *d++ = context->state[j];
596 bcopy(context->state, d, SHA512_DIGEST_LENGTH);
600 /* Zero out state data */
601 bzero(context, sizeof(*context));
604 char *SHA512_End(SHA512_CTX* context, char buffer[]) {
605 sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
609 assert(context != (SHA512_CTX*)0);
611 if (buffer != (char*)0) {
612 SHA512_Final(digest, context);
614 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
615 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
616 *buffer++ = sha2_hex_digits[*d & 0x0f];
621 bzero(context, sizeof(*context));
623 bzero(digest, SHA512_DIGEST_LENGTH);
627 char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
630 SHA512_Init(&context);
631 SHA512_Update(&context, data, len);
632 return SHA512_End(&context, digest);
636 /*** SHA-384: *********************************************************/
637 void SHA384_Init(SHA384_CTX* context) {
638 if (context == (SHA384_CTX*)0) {
641 bcopy(sha384_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
642 bzero(context->buffer, SHA384_BLOCK_LENGTH);
643 context->bitcount[0] = context->bitcount[1] = 0;
646 void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
647 SHA512_Update((SHA512_CTX*)context, data, len);
650 void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
651 sha2_word64 *d = (sha2_word64*)digest;
654 assert(context != (SHA384_CTX*)0);
656 /* If no digest buffer is passed, we don't bother doing this: */
657 if (digest != (sha2_byte*)0) {
658 SHA512_Last((SHA512_CTX*)context);
660 /* Save the hash data for output: */
661 #if BYTE_ORDER == LITTLE_ENDIAN
663 /* Convert TO host byte order */
665 for (j = 0; j < 6; j++) {
666 REVERSE64(context->state[j],context->state[j]);
667 *d++ = context->state[j];
671 bcopy(context->state, d, SHA384_DIGEST_LENGTH);
675 /* Zero out state data */
676 bzero(context, sizeof(*context));
679 char *SHA384_End(SHA384_CTX* context, char buffer[]) {
680 sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
684 assert(context != (SHA384_CTX*)0);
686 if (buffer != (char*)0) {
687 SHA384_Final(digest, context);
689 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
690 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
691 *buffer++ = sha2_hex_digits[*d & 0x0f];
696 bzero(context, sizeof(*context));
698 bzero(digest, SHA384_DIGEST_LENGTH);
702 char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
705 SHA384_Init(&context);
706 SHA384_Update(&context, data, len);
707 return SHA384_End(&context, digest);