Deleted Added
full compact
sha2.c (251886) sha2.c (253734)
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0

--- 38 unchanged lines hidden (view full) ---

47/*** SHA-256/384/512 Machine Architecture Definitions *****************/
48typedef apr_byte_t sha2_byte; /* Exactly 1 byte */
49typedef apr_uint32_t sha2_word32; /* Exactly 4 bytes */
50typedef apr_uint64_t sha2_word64; /* Exactly 8 bytes */
51
52/*** SHA-256/384/512 Various Length Definitions ***********************/
53/* NOTE: Most of these are in sha2.h */
54#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0

--- 38 unchanged lines hidden (view full) ---

47/*** SHA-256/384/512 Machine Architecture Definitions *****************/
48typedef apr_byte_t sha2_byte; /* Exactly 1 byte */
49typedef apr_uint32_t sha2_word32; /* Exactly 4 bytes */
50typedef apr_uint64_t sha2_word64; /* Exactly 8 bytes */
51
52/*** SHA-256/384/512 Various Length Definitions ***********************/
53/* NOTE: Most of these are in sha2.h */
54#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
55#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
56#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
57
58
59/*** ENDIAN REVERSAL MACROS *******************************************/
60#if !APR_IS_BIGENDIAN
61#define REVERSE32(w,x) { \
62 sha2_word32 tmp = (w); \
63 tmp = (tmp >> 16) | (tmp << 16); \
64 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \

--- 80 unchanged lines hidden (view full) ---

145#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
146#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
147
148/*** INTERNAL FUNCTION PROTOTYPES *************************************/
149/* NOTE: These should not be accessed directly from outside this
150 * library -- they are intended for private internal visibility/use
151 * only.
152 */
55
56
57/*** ENDIAN REVERSAL MACROS *******************************************/
58#if !APR_IS_BIGENDIAN
59#define REVERSE32(w,x) { \
60 sha2_word32 tmp = (w); \
61 tmp = (tmp >> 16) | (tmp << 16); \
62 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \

--- 80 unchanged lines hidden (view full) ---

143#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
144#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
145
146/*** INTERNAL FUNCTION PROTOTYPES *************************************/
147/* NOTE: These should not be accessed directly from outside this
148 * library -- they are intended for private internal visibility/use
149 * only.
150 */
153void apr__SHA512_Last(SHA512_CTX*);
154void apr__SHA256_Transform(SHA256_CTX*, const sha2_word32*);
151void apr__SHA256_Transform(SHA256_CTX*, const sha2_word32*);
155void apr__SHA512_Transform(SHA512_CTX*, const sha2_word64*);
156
157
158/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
159/* Hash constant words K for SHA-256: */
160static const sha2_word32 K256[64] = {
161 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
162 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
163 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,

--- 19 unchanged lines hidden (view full) ---

183 0x3c6ef372UL,
184 0xa54ff53aUL,
185 0x510e527fUL,
186 0x9b05688cUL,
187 0x1f83d9abUL,
188 0x5be0cd19UL
189};
190
152
153
154/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
155/* Hash constant words K for SHA-256: */
156static const sha2_word32 K256[64] = {
157 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
158 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
159 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,

--- 19 unchanged lines hidden (view full) ---

179 0x3c6ef372UL,
180 0xa54ff53aUL,
181 0x510e527fUL,
182 0x9b05688cUL,
183 0x1f83d9abUL,
184 0x5be0cd19UL
185};
186
191/* Hash constant words K for SHA-384 and SHA-512: */
192static const sha2_word64 K512[80] = {
193 APR_UINT64_C(0x428a2f98d728ae22), APR_UINT64_C(0x7137449123ef65cd),
194 APR_UINT64_C(0xb5c0fbcfec4d3b2f), APR_UINT64_C(0xe9b5dba58189dbbc),
195 APR_UINT64_C(0x3956c25bf348b538), APR_UINT64_C(0x59f111f1b605d019),
196 APR_UINT64_C(0x923f82a4af194f9b), APR_UINT64_C(0xab1c5ed5da6d8118),
197 APR_UINT64_C(0xd807aa98a3030242), APR_UINT64_C(0x12835b0145706fbe),
198 APR_UINT64_C(0x243185be4ee4b28c), APR_UINT64_C(0x550c7dc3d5ffb4e2),
199 APR_UINT64_C(0x72be5d74f27b896f), APR_UINT64_C(0x80deb1fe3b1696b1),
200 APR_UINT64_C(0x9bdc06a725c71235), APR_UINT64_C(0xc19bf174cf692694),
201 APR_UINT64_C(0xe49b69c19ef14ad2), APR_UINT64_C(0xefbe4786384f25e3),
202 APR_UINT64_C(0x0fc19dc68b8cd5b5), APR_UINT64_C(0x240ca1cc77ac9c65),
203 APR_UINT64_C(0x2de92c6f592b0275), APR_UINT64_C(0x4a7484aa6ea6e483),
204 APR_UINT64_C(0x5cb0a9dcbd41fbd4), APR_UINT64_C(0x76f988da831153b5),
205 APR_UINT64_C(0x983e5152ee66dfab), APR_UINT64_C(0xa831c66d2db43210),
206 APR_UINT64_C(0xb00327c898fb213f), APR_UINT64_C(0xbf597fc7beef0ee4),
207 APR_UINT64_C(0xc6e00bf33da88fc2), APR_UINT64_C(0xd5a79147930aa725),
208 APR_UINT64_C(0x06ca6351e003826f), APR_UINT64_C(0x142929670a0e6e70),
209 APR_UINT64_C(0x27b70a8546d22ffc), APR_UINT64_C(0x2e1b21385c26c926),
210 APR_UINT64_C(0x4d2c6dfc5ac42aed), APR_UINT64_C(0x53380d139d95b3df),
211 APR_UINT64_C(0x650a73548baf63de), APR_UINT64_C(0x766a0abb3c77b2a8),
212 APR_UINT64_C(0x81c2c92e47edaee6), APR_UINT64_C(0x92722c851482353b),
213 APR_UINT64_C(0xa2bfe8a14cf10364), APR_UINT64_C(0xa81a664bbc423001),
214 APR_UINT64_C(0xc24b8b70d0f89791), APR_UINT64_C(0xc76c51a30654be30),
215 APR_UINT64_C(0xd192e819d6ef5218), APR_UINT64_C(0xd69906245565a910),
216 APR_UINT64_C(0xf40e35855771202a), APR_UINT64_C(0x106aa07032bbd1b8),
217 APR_UINT64_C(0x19a4c116b8d2d0c8), APR_UINT64_C(0x1e376c085141ab53),
218 APR_UINT64_C(0x2748774cdf8eeb99), APR_UINT64_C(0x34b0bcb5e19b48a8),
219 APR_UINT64_C(0x391c0cb3c5c95a63), APR_UINT64_C(0x4ed8aa4ae3418acb),
220 APR_UINT64_C(0x5b9cca4f7763e373), APR_UINT64_C(0x682e6ff3d6b2b8a3),
221 APR_UINT64_C(0x748f82ee5defb2fc), APR_UINT64_C(0x78a5636f43172f60),
222 APR_UINT64_C(0x84c87814a1f0ab72), APR_UINT64_C(0x8cc702081a6439ec),
223 APR_UINT64_C(0x90befffa23631e28), APR_UINT64_C(0xa4506cebde82bde9),
224 APR_UINT64_C(0xbef9a3f7b2c67915), APR_UINT64_C(0xc67178f2e372532b),
225 APR_UINT64_C(0xca273eceea26619c), APR_UINT64_C(0xd186b8c721c0c207),
226 APR_UINT64_C(0xeada7dd6cde0eb1e), APR_UINT64_C(0xf57d4f7fee6ed178),
227 APR_UINT64_C(0x06f067aa72176fba), APR_UINT64_C(0x0a637dc5a2c898a6),
228 APR_UINT64_C(0x113f9804bef90dae), APR_UINT64_C(0x1b710b35131c471b),
229 APR_UINT64_C(0x28db77f523047d84), APR_UINT64_C(0x32caab7b40c72493),
230 APR_UINT64_C(0x3c9ebe0a15c9bebc), APR_UINT64_C(0x431d67c49c100d4c),
231 APR_UINT64_C(0x4cc5d4becb3e42b6), APR_UINT64_C(0x597f299cfc657e2a),
232 APR_UINT64_C(0x5fcb6fab3ad6faec), APR_UINT64_C(0x6c44198c4a475817)
233};
234
235/* Initial hash value H for SHA-384 */
236static const sha2_word64 sha384_initial_hash_value[8] = {
237 APR_UINT64_C(0xcbbb9d5dc1059ed8),
238 APR_UINT64_C(0x629a292a367cd507),
239 APR_UINT64_C(0x9159015a3070dd17),
240 APR_UINT64_C(0x152fecd8f70e5939),
241 APR_UINT64_C(0x67332667ffc00b31),
242 APR_UINT64_C(0x8eb44a8768581511),
243 APR_UINT64_C(0xdb0c2e0d64f98fa7),
244 APR_UINT64_C(0x47b5481dbefa4fa4)
245};
246
247/* Initial hash value H for SHA-512 */
248static const sha2_word64 sha512_initial_hash_value[8] = {
249 APR_UINT64_C(0x6a09e667f3bcc908),
250 APR_UINT64_C(0xbb67ae8584caa73b),
251 APR_UINT64_C(0x3c6ef372fe94f82b),
252 APR_UINT64_C(0xa54ff53a5f1d36f1),
253 APR_UINT64_C(0x510e527fade682d1),
254 APR_UINT64_C(0x9b05688c2b3e6c1f),
255 APR_UINT64_C(0x1f83d9abfb41bd6b),
256 APR_UINT64_C(0x5be0cd19137e2179)
257};
258
259/*
260 * Constant used by SHA256/384/512_End() functions for converting the
261 * digest to a readable hexadecimal character string:
262 */
263static const char *sha2_hex_digits = "0123456789abcdef";
264
265
266/*** SHA-256: *********************************************************/

--- 265 unchanged lines hidden (view full) ---

532 } else {
533 /* Set-up for the last transform: */
534 MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
535
536 /* Begin padding with a 1 bit: */
537 *context->buffer = 0x80;
538 }
539 /* Set the bit count: */
187/*
188 * Constant used by SHA256/384/512_End() functions for converting the
189 * digest to a readable hexadecimal character string:
190 */
191static const char *sha2_hex_digits = "0123456789abcdef";
192
193
194/*** SHA-256: *********************************************************/

--- 265 unchanged lines hidden (view full) ---

460 } else {
461 /* Set-up for the last transform: */
462 MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
463
464 /* Begin padding with a 1 bit: */
465 *context->buffer = 0x80;
466 }
467 /* Set the bit count: */
540 *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
468 {
469 union dummy {
470 apr_uint64_t bitcount;
471 apr_byte_t bytes[8];
472 } bitcount;
473 bitcount.bitcount = context->bitcount;
474 MEMCPY_BCOPY(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], bitcount.bytes, 8);
475 }
541
542 /* Final transform: */
543 apr__SHA256_Transform(context, (sha2_word32*)context->buffer);
544
545#if !APR_IS_BIGENDIAN
546 {
547 /* Convert TO host byte order */
548 int j;

--- 37 unchanged lines hidden (view full) ---

586
587char* apr__SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
588 SHA256_CTX context;
589
590 apr__SHA256_Init(&context);
591 apr__SHA256_Update(&context, data, len);
592 return apr__SHA256_End(&context, digest);
593}
476
477 /* Final transform: */
478 apr__SHA256_Transform(context, (sha2_word32*)context->buffer);
479
480#if !APR_IS_BIGENDIAN
481 {
482 /* Convert TO host byte order */
483 int j;

--- 37 unchanged lines hidden (view full) ---

521
522char* apr__SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
523 SHA256_CTX context;
524
525 apr__SHA256_Init(&context);
526 apr__SHA256_Update(&context, data, len);
527 return apr__SHA256_End(&context, digest);
528}
594
595
596/*** SHA-512: *********************************************************/
597void apr__SHA512_Init(SHA512_CTX* context) {
598 if (context == (SHA512_CTX*)0) {
599 return;
600 }
601 MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
602 MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
603 context->bitcount[0] = context->bitcount[1] = 0;
604}
605
606#ifdef SHA2_UNROLL_TRANSFORM
607
608/* Unrolled SHA-512 round macros: */
609#if !APR_IS_BIGENDIAN
610
611#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
612 REVERSE64(*data++, W512[j]); \
613 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
614 K512[j] + W512[j]; \
615 (d) += T1, \
616 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
617 j++
618
619
620#else /* APR_IS_BIGENDIAN */
621
622#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
623 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
624 K512[j] + (W512[j] = *data++); \
625 (d) += T1; \
626 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
627 j++
628
629#endif /* APR_IS_BIGENDIAN */
630
631#define ROUND512(a,b,c,d,e,f,g,h) \
632 s0 = W512[(j+1)&0x0f]; \
633 s0 = sigma0_512(s0); \
634 s1 = W512[(j+14)&0x0f]; \
635 s1 = sigma1_512(s1); \
636 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
637 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
638 (d) += T1; \
639 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
640 j++
641
642void apr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
643 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
644 sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
645 int j;
646
647 /* Initialize registers with the prev. intermediate value */
648 a = context->state[0];
649 b = context->state[1];
650 c = context->state[2];
651 d = context->state[3];
652 e = context->state[4];
653 f = context->state[5];
654 g = context->state[6];
655 h = context->state[7];
656
657 j = 0;
658 do {
659 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
660 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
661 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
662 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
663 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
664 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
665 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
666 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
667 } while (j < 16);
668
669 /* Now for the remaining rounds up to 79: */
670 do {
671 ROUND512(a,b,c,d,e,f,g,h);
672 ROUND512(h,a,b,c,d,e,f,g);
673 ROUND512(g,h,a,b,c,d,e,f);
674 ROUND512(f,g,h,a,b,c,d,e);
675 ROUND512(e,f,g,h,a,b,c,d);
676 ROUND512(d,e,f,g,h,a,b,c);
677 ROUND512(c,d,e,f,g,h,a,b);
678 ROUND512(b,c,d,e,f,g,h,a);
679 } while (j < 80);
680
681 /* Compute the current intermediate hash value */
682 context->state[0] += a;
683 context->state[1] += b;
684 context->state[2] += c;
685 context->state[3] += d;
686 context->state[4] += e;
687 context->state[5] += f;
688 context->state[6] += g;
689 context->state[7] += h;
690
691 /* Clean up */
692 a = b = c = d = e = f = g = h = T1 = 0;
693}
694
695#else /* SHA2_UNROLL_TRANSFORM */
696
697void apr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
698 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
699 sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
700 int j;
701
702 /* Initialize registers with the prev. intermediate value */
703 a = context->state[0];
704 b = context->state[1];
705 c = context->state[2];
706 d = context->state[3];
707 e = context->state[4];
708 f = context->state[5];
709 g = context->state[6];
710 h = context->state[7];
711
712 j = 0;
713 do {
714#if !APR_IS_BIGENDIAN
715 /* Convert TO host byte order */
716 REVERSE64(*data++, W512[j]);
717 /* Apply the SHA-512 compression function to update a..h */
718 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
719#else /* APR_IS_BIGENDIAN */
720 /* Apply the SHA-512 compression function to update a..h with copy */
721 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
722#endif /* APR_IS_BIGENDIAN */
723 T2 = Sigma0_512(a) + Maj(a, b, c);
724 h = g;
725 g = f;
726 f = e;
727 e = d + T1;
728 d = c;
729 c = b;
730 b = a;
731 a = T1 + T2;
732
733 j++;
734 } while (j < 16);
735
736 do {
737 /* Part of the message block expansion: */
738 s0 = W512[(j+1)&0x0f];
739 s0 = sigma0_512(s0);
740 s1 = W512[(j+14)&0x0f];
741 s1 = sigma1_512(s1);
742
743 /* Apply the SHA-512 compression function to update a..h */
744 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
745 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
746 T2 = Sigma0_512(a) + Maj(a, b, c);
747 h = g;
748 g = f;
749 f = e;
750 e = d + T1;
751 d = c;
752 c = b;
753 b = a;
754 a = T1 + T2;
755
756 j++;
757 } while (j < 80);
758
759 /* Compute the current intermediate hash value */
760 context->state[0] += a;
761 context->state[1] += b;
762 context->state[2] += c;
763 context->state[3] += d;
764 context->state[4] += e;
765 context->state[5] += f;
766 context->state[6] += g;
767 context->state[7] += h;
768
769 /* Clean up */
770 a = b = c = d = e = f = g = h = T1 = T2 = 0;
771}
772
773#endif /* SHA2_UNROLL_TRANSFORM */
774
775void apr__SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
776 unsigned int freespace, usedspace;
777
778 if (len == 0) {
779 /* Calling with no data is valid - we do nothing */
780 return;
781 }
782
783 /* Sanity check: */
784 assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
785
786 usedspace = (unsigned int)((context->bitcount[0] >> 3)
787 % SHA512_BLOCK_LENGTH);
788 if (usedspace > 0) {
789 /* Calculate how much free space is available in the buffer */
790 freespace = SHA512_BLOCK_LENGTH - usedspace;
791
792 if (len >= freespace) {
793 /* Fill the buffer completely and process it */
794 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
795 ADDINC128(context->bitcount, freespace << 3);
796 len -= freespace;
797 data += freespace;
798 apr__SHA512_Transform(context, (sha2_word64*)context->buffer);
799 } else {
800 /* The buffer is not yet full */
801 MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
802 ADDINC128(context->bitcount, len << 3);
803 /* Clean up: */
804 usedspace = freespace = 0;
805 return;
806 }
807 }
808 while (len >= SHA512_BLOCK_LENGTH) {
809 /* Process as many complete blocks as we can */
810 apr__SHA512_Transform(context, (sha2_word64*)data);
811 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
812 len -= SHA512_BLOCK_LENGTH;
813 data += SHA512_BLOCK_LENGTH;
814 }
815 if (len > 0) {
816 /* There's left-overs, so save 'em */
817 MEMCPY_BCOPY(context->buffer, data, len);
818 ADDINC128(context->bitcount, len << 3);
819 }
820 /* Clean up: */
821 usedspace = freespace = 0;
822}
823
824void apr__SHA512_Last(SHA512_CTX* context) {
825 unsigned int usedspace;
826
827 usedspace = (unsigned int)((context->bitcount[0] >> 3)
828 % SHA512_BLOCK_LENGTH);
829#if !APR_IS_BIGENDIAN
830 /* Convert FROM host byte order */
831 REVERSE64(context->bitcount[0],context->bitcount[0]);
832 REVERSE64(context->bitcount[1],context->bitcount[1]);
833#endif
834 if (usedspace > 0) {
835 /* Begin padding with a 1 bit: */
836 context->buffer[usedspace++] = 0x80;
837
838 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
839 /* Set-up for the last transform: */
840 MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
841 } else {
842 if (usedspace < SHA512_BLOCK_LENGTH) {
843 MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
844 }
845 /* Do second-to-last transform: */
846 apr__SHA512_Transform(context, (sha2_word64*)context->buffer);
847
848 /* And set-up for the last transform: */
849 MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
850 }
851 } else {
852 /* Prepare for final transform: */
853 MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
854
855 /* Begin padding with a 1 bit: */
856 *context->buffer = 0x80;
857 }
858 /* Store the length of input data (in bits): */
859 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
860 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
861
862 /* Final transform: */
863 apr__SHA512_Transform(context, (sha2_word64*)context->buffer);
864}
865
866void apr__SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
867 sha2_word64 *d = (sha2_word64*)digest;
868
869 /* Sanity check: */
870 assert(context != (SHA512_CTX*)0);
871
872 /* If no digest buffer is passed, we don't bother doing this: */
873 if (digest != (sha2_byte*)0) {
874 apr__SHA512_Last(context);
875
876 /* Save the hash data for output: */
877#if !APR_IS_BIGENDIAN
878 {
879 /* Convert TO host byte order */
880 int j;
881 for (j = 0; j < 8; j++) {
882 REVERSE64(context->state[j],context->state[j]);
883 *d++ = context->state[j];
884 }
885 }
886#else /* APR_IS_BIGENDIAN */
887 MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
888#endif /* APR_IS_BIGENDIAN */
889 }
890
891 /* Zero out state data */
892 MEMSET_BZERO(context, sizeof(*context));
893}
894
895char *apr__SHA512_End(SHA512_CTX* context, char buffer[]) {
896 sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
897 int i;
898
899 /* Sanity check: */
900 assert(context != (SHA512_CTX*)0);
901
902 if (buffer != (char*)0) {
903 apr__SHA512_Final(digest, context);
904
905 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
906 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
907 *buffer++ = sha2_hex_digits[*d & 0x0f];
908 d++;
909 }
910 *buffer = (char)0;
911 } else {
912 MEMSET_BZERO(context, sizeof(*context));
913 }
914 MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
915 return buffer;
916}
917
918char* apr__SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
919 SHA512_CTX context;
920
921 apr__SHA512_Init(&context);
922 apr__SHA512_Update(&context, data, len);
923 return apr__SHA512_End(&context, digest);
924}
925
926
927/*** SHA-384: *********************************************************/
928void apr__SHA384_Init(SHA384_CTX* context) {
929 if (context == (SHA384_CTX*)0) {
930 return;
931 }
932 MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
933 MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
934 context->bitcount[0] = context->bitcount[1] = 0;
935}
936
937void apr__SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
938 apr__SHA512_Update((SHA512_CTX*)context, data, len);
939}
940
941void apr__SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
942 sha2_word64 *d = (sha2_word64*)digest;
943
944 /* Sanity check: */
945 assert(context != (SHA384_CTX*)0);
946
947 /* If no digest buffer is passed, we don't bother doing this: */
948 if (digest != (sha2_byte*)0) {
949 apr__SHA512_Last((SHA512_CTX*)context);
950
951 /* Save the hash data for output: */
952#if !APR_IS_BIGENDIAN
953 {
954 /* Convert TO host byte order */
955 int j;
956 for (j = 0; j < 6; j++) {
957 REVERSE64(context->state[j],context->state[j]);
958 *d++ = context->state[j];
959 }
960 }
961#else /* APR_IS_BIGENDIAN */
962 MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
963#endif /* APR_IS_BIGENDIAN */
964 }
965
966 /* Zero out state data */
967 MEMSET_BZERO(context, sizeof(*context));
968}
969
970char *apr__SHA384_End(SHA384_CTX* context, char buffer[]) {
971 sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
972 int i;
973
974 /* Sanity check: */
975 assert(context != (SHA384_CTX*)0);
976
977 if (buffer != (char*)0) {
978 apr__SHA384_Final(digest, context);
979
980 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
981 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
982 *buffer++ = sha2_hex_digits[*d & 0x0f];
983 d++;
984 }
985 *buffer = (char)0;
986 } else {
987 MEMSET_BZERO(context, sizeof(*context));
988 }
989 MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
990 return buffer;
991}
992
993char* apr__SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
994 SHA384_CTX context;
995
996 apr__SHA384_Init(&context);
997 apr__SHA384_Update(&context, data, len);
998 return apr__SHA384_End(&context, digest);
999}
1000