Home | History | Annotate | Download | only in sha2
      1 /*	$NetBSD: sha2.c,v 1.4 2006/09/09 16:22:36 manu Exp $	*/
      2 
      3 /* Id: sha2.c,v 1.6 2004/09/21 14:35:25 ludvigm Exp */
      4 
      5 /*
      6  * sha2.c
      7  *
      8  * Version 1.0.0beta1
      9  *
     10  * Written by Aaron D. Gifford <me (at) aarongifford.com>
     11  *
     12  * Copyright 2000 Aaron D. Gifford.  All rights reserved.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. Neither the name of the copyright holder nor the names of contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  */
     39 
     40 #include "config.h"
     41 
     42 #include <sys/types.h>
     43 #include <sys/time.h>
     44 #ifndef __linux__
     45 #include <machine/endian.h>
     46 #endif
     47 #include <crypto/sha2/sha2.h>
     48 #include <openssl/evp.h>
     49 
     50 /* get openssl/ssleay version number */
     51 #include <openssl/opensslv.h>
     52 
     53 #include <err.h>
     54 #include <string.h>
     55 #define bcopy(a, b, c) memcpy((b), (a), (c))
     56 #define bzero(a, b) memset((a), 0, (b))
     57 #define panic(a) err(1, (a))
     58 
     59 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
     60 #define HAVE_EVP_097
     61 #endif
     62 
     63 /*
     64  * ASSERT NOTE:
     65  * Some sanity checking code is included using assert().  On my FreeBSD
     66  * system, this additional code can be removed by compiling with NDEBUG
     67  * defined.  Check your own systems manpage on assert() to see how to
     68  * compile WITHOUT the sanity checking code on your system.
     69  *
     70  * UNROLLED TRANSFORM LOOP NOTE:
     71  * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
     72  * loop version for the hash transform rounds (defined using macros
     73  * later in this file).  Either define on the command line, for example:
     74  *
     75  *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
     76  *
     77  * or define below:
     78  *
     79  *   #define SHA2_UNROLL_TRANSFORM
     80  *
     81  */
     82 
     83 #define assert(x)
     84 
     85 
     86 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
     87 /*
     88  * BYTE_ORDER NOTE:
     89  *
     90  * Please make sure that your system defines BYTE_ORDER.  If your
     91  * architecture is little-endian, make sure it also defines
     92  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
     93  * equivilent.
     94  *
     95  * If your system does not define the above, then you can do so by
     96  * hand like this:
     97  *
     98  *   #define LITTLE_ENDIAN 1234
     99  *   #define BIG_ENDIAN    4321
    100  *
    101  * And for little-endian machines, add:
    102  *
    103  *   #define BYTE_ORDER LITTLE_ENDIAN
    104  *
    105  * Or for big-endian machines:
    106  *
    107  *   #define BYTE_ORDER BIG_ENDIAN
    108  *
    109  * The FreeBSD machine this was written on defines BYTE_ORDER
    110  * appropriately by including <sys/types.h> (which in turn includes
    111  * <machine/endian.h> where the appropriate definitions are actually
    112  * made).
    113  */
    114 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
    115 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
    116 #endif
    117 
    118 /*
    119  * Define the followingsha2_* types to types of the correct length on
    120  * the native archtecture.   Most BSD systems and Linux define u_intXX_t
    121  * types.  Machines with very recent ANSI C headers, can use the
    122  * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
    123  * during compile or in the sha.h header file.
    124  *
    125  * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
    126  * will need to define these three typedefs below (and the appropriate
    127  * ones in sha.h too) by hand according to their system architecture.
    128  *
    129  * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
    130  * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
    131  */
    132 #if 0 /*def SHA2_USE_INTTYPES_H*/
    133 
    134 typedef uint8_t  sha2_byte;	/* Exactly 1 byte */
    135 typedef uint32_t sha2_word32;	/* Exactly 4 bytes */
    136 typedef uint64_t sha2_word64;	/* Exactly 8 bytes */
    137 
    138 #else /* SHA2_USE_INTTYPES_H */
    139 
    140 typedef u_int8_t  sha2_byte;	/* Exactly 1 byte */
    141 typedef u_int32_t sha2_word32;	/* Exactly 4 bytes */
    142 typedef u_int64_t sha2_word64;	/* Exactly 8 bytes */
    143 
    144 #endif /* SHA2_USE_INTTYPES_H */
    145 
    146 
    147 /*** SHA-256/384/512 Various Length Definitions ***********************/
    148 /* NOTE: Most of these are in sha2.h */
    149 #define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
    150 #define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
    151 #define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
    152 
    153 
    154 /*** ENDIAN REVERSAL MACROS *******************************************/
    155 #if BYTE_ORDER == LITTLE_ENDIAN
    156 #define REVERSE32(w,x)	{ \
    157 	sha2_word32 tmp = (w); \
    158 	tmp = (tmp >> 16) | (tmp << 16); \
    159 	(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
    160 }
    161 #define REVERSE64(w,x)	{ \
    162 	sha2_word64 tmp = (w); \
    163 	tmp = (tmp >> 32) | (tmp << 32); \
    164 	tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
    165 	      ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
    166 	(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
    167 	      ((tmp & 0x0000ffff0000ffffULL) << 16); \
    168 }
    169 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    170 
    171 /*
    172  * Macro for incrementally adding the unsigned 64-bit integer n to the
    173  * unsigned 128-bit integer (represented using a two-element array of
    174  * 64-bit words):
    175  */
    176 #define ADDINC128(w,n)	{ \
    177 	(w)[0] += (sha2_word64)(n); \
    178 	if ((w)[0] < (n)) { \
    179 		(w)[1]++; \
    180 	} \
    181 }
    182 
    183 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
    184 /*
    185  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
    186  *
    187  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
    188  *   S is a ROTATION) because the SHA-256/384/512 description document
    189  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
    190  *   same "backwards" definition.
    191  */
    192 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
    193 #define R(b,x) 		((x) >> (b))
    194 /* 32-bit Rotate-right (used in SHA-256): */
    195 #define S32(b,x)	(((x) >> (b)) | ((x) << (32 - (b))))
    196 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
    197 #define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
    198 
    199 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
    200 #define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
    201 #define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
    202 
    203 /* Four of six logical functions used in SHA-256: */
    204 #define Sigma0_256(x)	(S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
    205 #define Sigma1_256(x)	(S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
    206 #define sigma0_256(x)	(S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
    207 #define sigma1_256(x)	(S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
    208 
    209 /* Four of six logical functions used in SHA-384 and SHA-512: */
    210 #define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
    211 #define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
    212 #define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
    213 #define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
    214 
    215 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
    216 /* NOTE: These should not be accessed directly from outside this
    217  * library -- they are intended for private internal visibility/use
    218  * only.
    219  */
    220 void SHA512_Last(SHA512_CTX*);
    221 void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
    222 void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
    223 
    224 
    225 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
    226 /* Hash constant words K for SHA-256: */
    227 const static sha2_word32 K256[64] = {
    228 	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
    229 	0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
    230 	0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
    231 	0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
    232 	0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
    233 	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
    234 	0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
    235 	0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
    236 	0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
    237 	0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
    238 	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
    239 	0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
    240 	0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
    241 	0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
    242 	0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
    243 	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
    244 };
    245 
    246 /* Initial hash value H for SHA-256: */
    247 const static sha2_word32 sha256_initial_hash_value[8] = {
    248 	0x6a09e667UL,
    249 	0xbb67ae85UL,
    250 	0x3c6ef372UL,
    251 	0xa54ff53aUL,
    252 	0x510e527fUL,
    253 	0x9b05688cUL,
    254 	0x1f83d9abUL,
    255 	0x5be0cd19UL
    256 };
    257 
    258 /* Hash constant words K for SHA-384 and SHA-512: */
    259 const static sha2_word64 K512[80] = {
    260 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
    261 	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
    262 	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
    263 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
    264 	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
    265 	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
    266 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
    267 	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
    268 	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
    269 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
    270 	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
    271 	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
    272 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
    273 	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
    274 	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
    275 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
    276 	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
    277 	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
    278 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
    279 	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
    280 	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
    281 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
    282 	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
    283 	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
    284 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
    285 	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
    286 	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
    287 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
    288 	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
    289 	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
    290 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
    291 	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
    292 	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
    293 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
    294 	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
    295 	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
    296 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
    297 	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
    298 	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
    299 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
    300 };
    301 
    302 /* Initial hash value H for SHA-384 */
    303 const static sha2_word64 sha384_initial_hash_value[8] = {
    304 	0xcbbb9d5dc1059ed8ULL,
    305 	0x629a292a367cd507ULL,
    306 	0x9159015a3070dd17ULL,
    307 	0x152fecd8f70e5939ULL,
    308 	0x67332667ffc00b31ULL,
    309 	0x8eb44a8768581511ULL,
    310 	0xdb0c2e0d64f98fa7ULL,
    311 	0x47b5481dbefa4fa4ULL
    312 };
    313 
    314 /* Initial hash value H for SHA-512 */
    315 const static sha2_word64 sha512_initial_hash_value[8] = {
    316 	0x6a09e667f3bcc908ULL,
    317 	0xbb67ae8584caa73bULL,
    318 	0x3c6ef372fe94f82bULL,
    319 	0xa54ff53a5f1d36f1ULL,
    320 	0x510e527fade682d1ULL,
    321 	0x9b05688c2b3e6c1fULL,
    322 	0x1f83d9abfb41bd6bULL,
    323 	0x5be0cd19137e2179ULL
    324 };
    325 
    326 /*
    327  * Constant used by SHA256/384/512_End() functions for converting the
    328  * digest to a readable hexadecimal character string:
    329  */
    330 static const char *sha2_hex_digits = "0123456789abcdef";
    331 
    332 
    333 /*** SHA-256: *********************************************************/
    334 void SHA256_Init(SHA256_CTX* context) {
    335 	if (context == (SHA256_CTX*)0) {
    336 		return;
    337 	}
    338 	bcopy(sha256_initial_hash_value, context->state, SHA256_DIGEST_LENGTH);
    339 	bzero(context->buffer, SHA256_BLOCK_LENGTH);
    340 	context->bitcount = 0;
    341 }
    342 
    343 #ifdef SHA2_UNROLL_TRANSFORM
    344 
    345 /* Unrolled SHA-256 round macros: */
    346 
    347 #if BYTE_ORDER == LITTLE_ENDIAN
    348 
    349 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
    350 	REVERSE32(*data++, W256[j]); \
    351 	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
    352              K256[j] + W256[j]; \
    353 	(d) += T1; \
    354 	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
    355 	j++
    356 
    357 
    358 #else /* BYTE_ORDER == LITTLE_ENDIAN */
    359 
    360 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
    361 	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
    362 	     K256[j] + (W256[j] = *data++); \
    363 	(d) += T1; \
    364 	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
    365 	j++
    366 
    367 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    368 
    369 #define ROUND256(a,b,c,d,e,f,g,h)	\
    370 	s0 = W256[(j+1)&0x0f]; \
    371 	s0 = sigma0_256(s0); \
    372 	s1 = W256[(j+14)&0x0f]; \
    373 	s1 = sigma1_256(s1); \
    374 	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
    375 	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
    376 	(d) += T1; \
    377 	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
    378 	j++
    379 
    380 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
    381 	sha2_word32	a, b, c, d, e, f, g, h, s0, s1;
    382 	sha2_word32	T1, *W256;
    383 	int		j;
    384 
    385 	W256 = (sha2_word32*)context->buffer;
    386 
    387 	/* Initialize registers with the prev. intermediate value */
    388 	a = context->state[0];
    389 	b = context->state[1];
    390 	c = context->state[2];
    391 	d = context->state[3];
    392 	e = context->state[4];
    393 	f = context->state[5];
    394 	g = context->state[6];
    395 	h = context->state[7];
    396 
    397 	j = 0;
    398 	do {
    399 		/* Rounds 0 to 15 (unrolled): */
    400 		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
    401 		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
    402 		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
    403 		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
    404 		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
    405 		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
    406 		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
    407 		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
    408 	} while (j < 16);
    409 
    410 	/* Now for the remaining rounds to 64: */
    411 	do {
    412 		ROUND256(a,b,c,d,e,f,g,h);
    413 		ROUND256(h,a,b,c,d,e,f,g);
    414 		ROUND256(g,h,a,b,c,d,e,f);
    415 		ROUND256(f,g,h,a,b,c,d,e);
    416 		ROUND256(e,f,g,h,a,b,c,d);
    417 		ROUND256(d,e,f,g,h,a,b,c);
    418 		ROUND256(c,d,e,f,g,h,a,b);
    419 		ROUND256(b,c,d,e,f,g,h,a);
    420 	} while (j < 64);
    421 
    422 	/* Compute the current intermediate hash value */
    423 	context->state[0] += a;
    424 	context->state[1] += b;
    425 	context->state[2] += c;
    426 	context->state[3] += d;
    427 	context->state[4] += e;
    428 	context->state[5] += f;
    429 	context->state[6] += g;
    430 	context->state[7] += h;
    431 
    432 	/* Clean up */
    433 	a = b = c = d = e = f = g = h = T1 = 0;
    434 }
    435 
    436 #else /* SHA2_UNROLL_TRANSFORM */
    437 
    438 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
    439 	sha2_word32	a, b, c, d, e, f, g, h, s0, s1;
    440 	sha2_word32	T1, T2, *W256;
    441 	int		j;
    442 
    443 	W256 = (sha2_word32*)context->buffer;
    444 
    445 	/* Initialize registers with the prev. intermediate value */
    446 	a = context->state[0];
    447 	b = context->state[1];
    448 	c = context->state[2];
    449 	d = context->state[3];
    450 	e = context->state[4];
    451 	f = context->state[5];
    452 	g = context->state[6];
    453 	h = context->state[7];
    454 
    455 	j = 0;
    456 	do {
    457 #if BYTE_ORDER == LITTLE_ENDIAN
    458 		/* Copy data while converting to host byte order */
    459 		REVERSE32(*data++,W256[j]);
    460 		/* Apply the SHA-256 compression function to update a..h */
    461 		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
    462 #else /* BYTE_ORDER == LITTLE_ENDIAN */
    463 		/* Apply the SHA-256 compression function to update a..h with copy */
    464 		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
    465 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    466 		T2 = Sigma0_256(a) + Maj(a, b, c);
    467 		h = g;
    468 		g = f;
    469 		f = e;
    470 		e = d + T1;
    471 		d = c;
    472 		c = b;
    473 		b = a;
    474 		a = T1 + T2;
    475 
    476 		j++;
    477 	} while (j < 16);
    478 
    479 	do {
    480 		/* Part of the message block expansion: */
    481 		s0 = W256[(j+1)&0x0f];
    482 		s0 = sigma0_256(s0);
    483 		s1 = W256[(j+14)&0x0f];
    484 		s1 = sigma1_256(s1);
    485 
    486 		/* Apply the SHA-256 compression function to update a..h */
    487 		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
    488 		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
    489 		T2 = Sigma0_256(a) + Maj(a, b, c);
    490 		h = g;
    491 		g = f;
    492 		f = e;
    493 		e = d + T1;
    494 		d = c;
    495 		c = b;
    496 		b = a;
    497 		a = T1 + T2;
    498 
    499 		j++;
    500 	} while (j < 64);
    501 
    502 	/* Compute the current intermediate hash value */
    503 	context->state[0] += a;
    504 	context->state[1] += b;
    505 	context->state[2] += c;
    506 	context->state[3] += d;
    507 	context->state[4] += e;
    508 	context->state[5] += f;
    509 	context->state[6] += g;
    510 	context->state[7] += h;
    511 
    512 	/* Clean up */
    513 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
    514 }
    515 
    516 #endif /* SHA2_UNROLL_TRANSFORM */
    517 
    518 void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
    519 	unsigned int	freespace, usedspace;
    520 
    521 	if (len == 0) {
    522 		/* Calling with no data is valid - we do nothing */
    523 		return;
    524 	}
    525 
    526 	/* Sanity check: */
    527 	assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
    528 
    529 	usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
    530 	if (usedspace > 0) {
    531 		/* Calculate how much free space is available in the buffer */
    532 		freespace = SHA256_BLOCK_LENGTH - usedspace;
    533 
    534 		if (len >= freespace) {
    535 			/* Fill the buffer completely and process it */
    536 			bcopy(data, &context->buffer[usedspace], freespace);
    537 			context->bitcount += freespace << 3;
    538 			len -= freespace;
    539 			data += freespace;
    540 			SHA256_Transform(context, (sha2_word32*)context->buffer);
    541 		} else {
    542 			/* The buffer is not yet full */
    543 			bcopy(data, &context->buffer[usedspace], len);
    544 			context->bitcount += len << 3;
    545 			/* Clean up: */
    546 			usedspace = freespace = 0;
    547 			return;
    548 		}
    549 	}
    550 	while (len >= SHA256_BLOCK_LENGTH) {
    551 		/* Process as many complete blocks as we can */
    552 		SHA256_Transform(context, (const sha2_word32*)data);
    553 		context->bitcount += SHA256_BLOCK_LENGTH << 3;
    554 		len -= SHA256_BLOCK_LENGTH;
    555 		data += SHA256_BLOCK_LENGTH;
    556 	}
    557 	if (len > 0) {
    558 		/* There's left-overs, so save 'em */
    559 		bcopy(data, context->buffer, len);
    560 		context->bitcount += len << 3;
    561 	}
    562 	/* Clean up: */
    563 	usedspace = freespace = 0;
    564 }
    565 
    566 void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
    567 	sha2_word32	*d = (sha2_word32*)digest;
    568 	unsigned int	usedspace;
    569 
    570 	/* Sanity check: */
    571 	assert(context != (SHA256_CTX*)0);
    572 
    573 	/* If no digest buffer is passed, we don't bother doing this: */
    574 	if (digest != (sha2_byte*)0) {
    575 		usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
    576 #if BYTE_ORDER == LITTLE_ENDIAN
    577 		/* Convert FROM host byte order */
    578 		REVERSE64(context->bitcount,context->bitcount);
    579 #endif
    580 		if (usedspace > 0) {
    581 			/* Begin padding with a 1 bit: */
    582 			context->buffer[usedspace++] = 0x80;
    583 
    584 			if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
    585 				/* Set-up for the last transform: */
    586 				bzero(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
    587 			} else {
    588 				if (usedspace < SHA256_BLOCK_LENGTH) {
    589 					bzero(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
    590 				}
    591 				/* Do second-to-last transform: */
    592 				SHA256_Transform(context, (sha2_word32*)context->buffer);
    593 
    594 				/* And set-up for the last transform: */
    595 				bzero(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
    596 			}
    597 		} else {
    598 			/* Set-up for the last transform: */
    599 			bzero(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
    600 
    601 			/* Begin padding with a 1 bit: */
    602 			*context->buffer = 0x80;
    603 		}
    604 		/* Set the bit count: */
    605 		*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
    606 
    607 		/* Final transform: */
    608 		SHA256_Transform(context, (sha2_word32*)context->buffer);
    609 
    610 #if BYTE_ORDER == LITTLE_ENDIAN
    611 		{
    612 			/* Convert TO host byte order */
    613 			int	j;
    614 			for (j = 0; j < 8; j++) {
    615 				REVERSE32(context->state[j],context->state[j]);
    616 				*d++ = context->state[j];
    617 			}
    618 		}
    619 #else
    620 		bcopy(context->state, d, SHA256_DIGEST_LENGTH);
    621 #endif
    622 	}
    623 
    624 	/* Clean up state data: */
    625 	bzero(context, sizeof(*context));
    626 	usedspace = 0;
    627 }
    628 
    629 char *SHA256_End(SHA256_CTX* context, char buffer[]) {
    630 	sha2_byte	digest[SHA256_DIGEST_LENGTH], *d = digest;
    631 	int		i;
    632 
    633 	/* Sanity check: */
    634 	assert(context != (SHA256_CTX*)0);
    635 
    636 	if (buffer != (char*)0) {
    637 		SHA256_Final(digest, context);
    638 
    639 		for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
    640 			*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
    641 			*buffer++ = sha2_hex_digits[*d & 0x0f];
    642 			d++;
    643 		}
    644 		*buffer = (char)0;
    645 	} else {
    646 		bzero(context, sizeof(*context));
    647 	}
    648 	bzero(digest, SHA256_DIGEST_LENGTH);
    649 	return buffer;
    650 }
    651 
    652 char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
    653 	SHA256_CTX	context;
    654 
    655 	SHA256_Init(&context);
    656 	SHA256_Update(&context, data, len);
    657 	return SHA256_End(&context, digest);
    658 }
    659 
    660 
    661 /*** SHA-512: *********************************************************/
    662 void SHA512_Init(SHA512_CTX* context) {
    663 	if (context == (SHA512_CTX*)0) {
    664 		return;
    665 	}
    666 	bcopy(sha512_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
    667 	bzero(context->buffer, SHA512_BLOCK_LENGTH);
    668 	context->bitcount[0] = context->bitcount[1] =  0;
    669 }
    670 
    671 #ifdef SHA2_UNROLL_TRANSFORM
    672 
    673 /* Unrolled SHA-512 round macros: */
    674 #if BYTE_ORDER == LITTLE_ENDIAN
    675 
    676 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
    677 	REVERSE64(*data++, W512[j]); \
    678 	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
    679              K512[j] + W512[j]; \
    680 	(d) += T1, \
    681 	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
    682 	j++
    683 
    684 
    685 #else /* BYTE_ORDER == LITTLE_ENDIAN */
    686 
    687 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
    688 	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
    689              K512[j] + (W512[j] = *data++); \
    690 	(d) += T1; \
    691 	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
    692 	j++
    693 
    694 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    695 
    696 #define ROUND512(a,b,c,d,e,f,g,h)	\
    697 	s0 = W512[(j+1)&0x0f]; \
    698 	s0 = sigma0_512(s0); \
    699 	s1 = W512[(j+14)&0x0f]; \
    700 	s1 = sigma1_512(s1); \
    701 	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
    702              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
    703 	(d) += T1; \
    704 	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
    705 	j++
    706 
    707 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
    708 	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
    709 	sha2_word64	T1, *W512 = (sha2_word64*)context->buffer;
    710 	int		j;
    711 
    712 	/* Initialize registers with the prev. intermediate value */
    713 	a = context->state[0];
    714 	b = context->state[1];
    715 	c = context->state[2];
    716 	d = context->state[3];
    717 	e = context->state[4];
    718 	f = context->state[5];
    719 	g = context->state[6];
    720 	h = context->state[7];
    721 
    722 	j = 0;
    723 	do {
    724 		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
    725 		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
    726 		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
    727 		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
    728 		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
    729 		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
    730 		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
    731 		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
    732 	} while (j < 16);
    733 
    734 	/* Now for the remaining rounds up to 79: */
    735 	do {
    736 		ROUND512(a,b,c,d,e,f,g,h);
    737 		ROUND512(h,a,b,c,d,e,f,g);
    738 		ROUND512(g,h,a,b,c,d,e,f);
    739 		ROUND512(f,g,h,a,b,c,d,e);
    740 		ROUND512(e,f,g,h,a,b,c,d);
    741 		ROUND512(d,e,f,g,h,a,b,c);
    742 		ROUND512(c,d,e,f,g,h,a,b);
    743 		ROUND512(b,c,d,e,f,g,h,a);
    744 	} while (j < 80);
    745 
    746 	/* Compute the current intermediate hash value */
    747 	context->state[0] += a;
    748 	context->state[1] += b;
    749 	context->state[2] += c;
    750 	context->state[3] += d;
    751 	context->state[4] += e;
    752 	context->state[5] += f;
    753 	context->state[6] += g;
    754 	context->state[7] += h;
    755 
    756 	/* Clean up */
    757 	a = b = c = d = e = f = g = h = T1 = 0;
    758 }
    759 
    760 #else /* SHA2_UNROLL_TRANSFORM */
    761 
    762 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
    763 	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
    764 	sha2_word64	T1, T2, *W512 = (sha2_word64*)context->buffer;
    765 	int		j;
    766 
    767 	/* Initialize registers with the prev. intermediate value */
    768 	a = context->state[0];
    769 	b = context->state[1];
    770 	c = context->state[2];
    771 	d = context->state[3];
    772 	e = context->state[4];
    773 	f = context->state[5];
    774 	g = context->state[6];
    775 	h = context->state[7];
    776 
    777 	j = 0;
    778 	do {
    779 #if BYTE_ORDER == LITTLE_ENDIAN
    780 		/* Convert TO host byte order */
    781 		REVERSE64(*data++, W512[j]);
    782 		/* Apply the SHA-512 compression function to update a..h */
    783 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
    784 #else /* BYTE_ORDER == LITTLE_ENDIAN */
    785 		/* Apply the SHA-512 compression function to update a..h with copy */
    786 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
    787 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    788 		T2 = Sigma0_512(a) + Maj(a, b, c);
    789 		h = g;
    790 		g = f;
    791 		f = e;
    792 		e = d + T1;
    793 		d = c;
    794 		c = b;
    795 		b = a;
    796 		a = T1 + T2;
    797 
    798 		j++;
    799 	} while (j < 16);
    800 
    801 	do {
    802 		/* Part of the message block expansion: */
    803 		s0 = W512[(j+1)&0x0f];
    804 		s0 = sigma0_512(s0);
    805 		s1 = W512[(j+14)&0x0f];
    806 		s1 =  sigma1_512(s1);
    807 
    808 		/* Apply the SHA-512 compression function to update a..h */
    809 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
    810 		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
    811 		T2 = Sigma0_512(a) + Maj(a, b, c);
    812 		h = g;
    813 		g = f;
    814 		f = e;
    815 		e = d + T1;
    816 		d = c;
    817 		c = b;
    818 		b = a;
    819 		a = T1 + T2;
    820 
    821 		j++;
    822 	} while (j < 80);
    823 
    824 	/* Compute the current intermediate hash value */
    825 	context->state[0] += a;
    826 	context->state[1] += b;
    827 	context->state[2] += c;
    828 	context->state[3] += d;
    829 	context->state[4] += e;
    830 	context->state[5] += f;
    831 	context->state[6] += g;
    832 	context->state[7] += h;
    833 
    834 	/* Clean up */
    835 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
    836 }
    837 
    838 #endif /* SHA2_UNROLL_TRANSFORM */
    839 
    840 void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
    841 	unsigned int	freespace, usedspace;
    842 
    843 	if (len == 0) {
    844 		/* Calling with no data is valid - we do nothing */
    845 		return;
    846 	}
    847 
    848 	/* Sanity check: */
    849 	assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
    850 
    851 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
    852 	if (usedspace > 0) {
    853 		/* Calculate how much free space is available in the buffer */
    854 		freespace = SHA512_BLOCK_LENGTH - usedspace;
    855 
    856 		if (len >= freespace) {
    857 			/* Fill the buffer completely and process it */
    858 			bcopy(data, &context->buffer[usedspace], freespace);
    859 			ADDINC128(context->bitcount, freespace << 3);
    860 			len -= freespace;
    861 			data += freespace;
    862 			SHA512_Transform(context, (sha2_word64*)context->buffer);
    863 		} else {
    864 			/* The buffer is not yet full */
    865 			bcopy(data, &context->buffer[usedspace], len);
    866 			ADDINC128(context->bitcount, len << 3);
    867 			/* Clean up: */
    868 			usedspace = freespace = 0;
    869 			return;
    870 		}
    871 	}
    872 	while (len >= SHA512_BLOCK_LENGTH) {
    873 		/* Process as many complete blocks as we can */
    874 		SHA512_Transform(context, (const sha2_word64*)data);
    875 		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
    876 		len -= SHA512_BLOCK_LENGTH;
    877 		data += SHA512_BLOCK_LENGTH;
    878 	}
    879 	if (len > 0) {
    880 		/* There's left-overs, so save 'em */
    881 		bcopy(data, context->buffer, len);
    882 		ADDINC128(context->bitcount, len << 3);
    883 	}
    884 	/* Clean up: */
    885 	usedspace = freespace = 0;
    886 }
    887 
    888 void SHA512_Last(SHA512_CTX* context) {
    889 	unsigned int	usedspace;
    890 
    891 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
    892 #if BYTE_ORDER == LITTLE_ENDIAN
    893 	/* Convert FROM host byte order */
    894 	REVERSE64(context->bitcount[0],context->bitcount[0]);
    895 	REVERSE64(context->bitcount[1],context->bitcount[1]);
    896 #endif
    897 	if (usedspace > 0) {
    898 		/* Begin padding with a 1 bit: */
    899 		context->buffer[usedspace++] = 0x80;
    900 
    901 		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
    902 			/* Set-up for the last transform: */
    903 			bzero(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
    904 		} else {
    905 			if (usedspace < SHA512_BLOCK_LENGTH) {
    906 				bzero(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
    907 			}
    908 			/* Do second-to-last transform: */
    909 			SHA512_Transform(context, (sha2_word64*)context->buffer);
    910 
    911 			/* And set-up for the last transform: */
    912 			bzero(context->buffer, SHA512_BLOCK_LENGTH - 2);
    913 		}
    914 	} else {
    915 		/* Prepare for final transform: */
    916 		bzero(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
    917 
    918 		/* Begin padding with a 1 bit: */
    919 		*context->buffer = 0x80;
    920 	}
    921 	/* Store the length of input data (in bits): */
    922 	*(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
    923 	*(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
    924 
    925 	/* Final transform: */
    926 	SHA512_Transform(context, (sha2_word64*)context->buffer);
    927 }
    928 
    929 void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
    930 	sha2_word64	*d = (sha2_word64*)digest;
    931 
    932 	/* Sanity check: */
    933 	assert(context != (SHA512_CTX*)0);
    934 
    935 	/* If no digest buffer is passed, we don't bother doing this: */
    936 	if (digest != (sha2_byte*)0) {
    937 		SHA512_Last(context);
    938 
    939 		/* Save the hash data for output: */
    940 #if BYTE_ORDER == LITTLE_ENDIAN
    941 		{
    942 			/* Convert TO host byte order */
    943 			int	j;
    944 			for (j = 0; j < 8; j++) {
    945 				REVERSE64(context->state[j],context->state[j]);
    946 				*d++ = context->state[j];
    947 			}
    948 		}
    949 #else
    950 		bcopy(context->state, d, SHA512_DIGEST_LENGTH);
    951 #endif
    952 	}
    953 
    954 	/* Zero out state data */
    955 	bzero(context, sizeof(*context));
    956 }
    957 
    958 char *SHA512_End(SHA512_CTX* context, char buffer[]) {
    959 	sha2_byte	digest[SHA512_DIGEST_LENGTH], *d = digest;
    960 	int		i;
    961 
    962 	/* Sanity check: */
    963 	assert(context != (SHA512_CTX*)0);
    964 
    965 	if (buffer != (char*)0) {
    966 		SHA512_Final(digest, context);
    967 
    968 		for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
    969 			*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
    970 			*buffer++ = sha2_hex_digits[*d & 0x0f];
    971 			d++;
    972 		}
    973 		*buffer = (char)0;
    974 	} else {
    975 		bzero(context, sizeof(*context));
    976 	}
    977 	bzero(digest, SHA512_DIGEST_LENGTH);
    978 	return buffer;
    979 }
    980 
    981 char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
    982 	SHA512_CTX	context;
    983 
    984 	SHA512_Init(&context);
    985 	SHA512_Update(&context, data, len);
    986 	return SHA512_End(&context, digest);
    987 }
    988 
    989 
    990 /*** SHA-384: *********************************************************/
    991 void SHA384_Init(SHA384_CTX* context) {
    992 	if (context == (SHA384_CTX*)0) {
    993 		return;
    994 	}
    995 	bcopy(sha384_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
    996 	bzero(context->buffer, SHA384_BLOCK_LENGTH);
    997 	context->bitcount[0] = context->bitcount[1] = 0;
    998 }
    999 
   1000 void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
   1001 	SHA512_Update((SHA512_CTX*)context, data, len);
   1002 }
   1003 
   1004 void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
   1005 	sha2_word64	*d = (sha2_word64*)digest;
   1006 
   1007 	/* Sanity check: */
   1008 	assert(context != (SHA384_CTX*)0);
   1009 
   1010 	/* If no digest buffer is passed, we don't bother doing this: */
   1011 	if (digest != (sha2_byte*)0) {
   1012 		SHA512_Last((SHA512_CTX*)context);
   1013 
   1014 		/* Save the hash data for output: */
   1015 #if BYTE_ORDER == LITTLE_ENDIAN
   1016 		{
   1017 			/* Convert TO host byte order */
   1018 			int	j;
   1019 			for (j = 0; j < 6; j++) {
   1020 				REVERSE64(context->state[j],context->state[j]);
   1021 				*d++ = context->state[j];
   1022 			}
   1023 		}
   1024 #else
   1025 		bcopy(context->state, d, SHA384_DIGEST_LENGTH);
   1026 #endif
   1027 	}
   1028 
   1029 	/* Zero out state data */
   1030 	bzero(context, sizeof(*context));
   1031 }
   1032 
   1033 char *SHA384_End(SHA384_CTX* context, char buffer[]) {
   1034 	sha2_byte	digest[SHA384_DIGEST_LENGTH], *d = digest;
   1035 	int		i;
   1036 
   1037 	/* Sanity check: */
   1038 	assert(context != (SHA384_CTX*)0);
   1039 
   1040 	if (buffer != (char*)0) {
   1041 		SHA384_Final(digest, context);
   1042 
   1043 		for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
   1044 			*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
   1045 			*buffer++ = sha2_hex_digits[*d & 0x0f];
   1046 			d++;
   1047 		}
   1048 		*buffer = (char)0;
   1049 	} else {
   1050 		bzero(context, sizeof(*context));
   1051 	}
   1052 	bzero(digest, SHA384_DIGEST_LENGTH);
   1053 	return buffer;
   1054 }
   1055 
   1056 char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
   1057 	SHA384_CTX	context;
   1058 
   1059 	SHA384_Init(&context);
   1060 	SHA384_Update(&context, data, len);
   1061 	return SHA384_End(&context, digest);
   1062 }
   1063 
   1064 /*glue*/
   1065 #ifdef HAVE_EVP_097
   1066 
   1067 /* SHA256 */
   1068 #define data(ctx) ((SHA256_CTX *)(ctx)->md_data)
   1069 static int sha256_init(EVP_MD_CTX *ctx)
   1070 {
   1071   SHA256_Init(data(ctx));
   1072   return 1;
   1073 }
   1074 static int sha256_update(EVP_MD_CTX *ctx, const void *data, unsigned long count)
   1075 {
   1076   SHA256_Update(data(ctx), data, count);
   1077   return 1;
   1078 }
   1079 static int sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
   1080 {
   1081   SHA256_Final(md, data(ctx));
   1082   return 1;
   1083 }
   1084 #undef data
   1085 
   1086 /* SHA384 */
   1087 #define data(ctx) ((SHA384_CTX *)(ctx)->md_data)
   1088 static int sha384_init(EVP_MD_CTX *ctx)
   1089 {
   1090   SHA384_Init(data(ctx));
   1091   return 1;
   1092 }
   1093 static int sha384_update(EVP_MD_CTX *ctx, const void *data, unsigned long count)
   1094 {
   1095   SHA384_Update(data(ctx), data, count);
   1096   return 1;
   1097 }
   1098 static int sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
   1099 {
   1100   SHA384_Final(md, data(ctx));
   1101   return 1;
   1102 }
   1103 #undef data
   1104 
   1105 /* SHA512 */
   1106 #define data(ctx) ((SHA512_CTX *)(ctx)->md_data)
   1107 static int sha512_init(EVP_MD_CTX *ctx)
   1108 {
   1109   SHA512_Init(data(ctx));
   1110   return 1;
   1111 }
   1112 static int sha512_update(EVP_MD_CTX *ctx, const void *data, unsigned long count)
   1113 {
   1114   SHA512_Update(data(ctx), data, count);
   1115   return 1;
   1116 }
   1117 static int sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
   1118 {
   1119   SHA512_Final(md, data(ctx));
   1120   return 1;
   1121 }
   1122 #undef data
   1123 #endif
   1124 
   1125 static struct env_md_st sha2_256_md = {
   1126 	0, /*NID_sha1*/
   1127 	0, /*NID_sha1WithRSAEncryption*/
   1128 	SHA256_DIGEST_LENGTH,
   1129 #ifdef HAVE_EVP_097
   1130 	0,			/* flags */
   1131 	sha256_init,
   1132 	sha256_update,
   1133 	sha256_final,
   1134 	NULL,			/* copy */
   1135 	NULL,			/* cleanup */
   1136 #else
   1137 	SHA256_Init,
   1138 	SHA256_Update,
   1139 	SHA256_Final,
   1140 #endif
   1141 	NULL, NULL, {0, 0, 0, 0},
   1142 	SHA256_BLOCK_LENGTH,
   1143 	sizeof(struct env_md_st *) + sizeof(SHA256_CTX),
   1144 };
   1145 
   1146 struct env_md_st *EVP_sha2_256(void)
   1147 {
   1148 	return(&sha2_256_md);
   1149 }
   1150 
   1151 static struct env_md_st sha2_384_md = {
   1152 	0, /*NID_sha1*/
   1153 	0, /*NID_sha1WithRSAEncryption*/
   1154 	SHA384_DIGEST_LENGTH,
   1155 #ifdef HAVE_EVP_097
   1156 	0,			/* flags */
   1157 	sha384_init,
   1158 	sha384_update,
   1159 	sha384_final,
   1160 	NULL,			/* copy */
   1161 	NULL,			/* cleanup */
   1162 #else
   1163 	SHA384_Init,
   1164 	SHA384_Update,
   1165 	SHA384_Final,
   1166 #endif
   1167 	NULL, NULL, {0, 0, 0, 0},
   1168 	SHA384_BLOCK_LENGTH,
   1169 	sizeof(struct env_md_st *) + sizeof(SHA384_CTX),
   1170 };
   1171 
   1172 struct env_md_st *EVP_sha2_384(void)
   1173 {
   1174 	return(&sha2_384_md);
   1175 }
   1176 
   1177 static struct env_md_st sha2_512_md = {
   1178 	0, /*NID_sha1*/
   1179 	0, /*NID_sha1WithRSAEncryption*/
   1180 	SHA512_DIGEST_LENGTH,
   1181 #ifdef HAVE_EVP_097
   1182 	0,			/* flags */
   1183 	sha512_init,
   1184 	sha512_update,
   1185 	sha512_final,
   1186 	NULL,			/* copy */
   1187 	NULL,			/* cleanup */
   1188 #else
   1189 	SHA512_Init,
   1190 	SHA512_Update,
   1191 	SHA512_Final,
   1192 #endif
   1193 	NULL, NULL, {0, 0, 0, 0}, /*EVP_PKEY_RSA_method*/
   1194 	SHA512_BLOCK_LENGTH,
   1195 	sizeof(struct env_md_st *) + sizeof(SHA512_CTX),
   1196 };
   1197 
   1198 struct env_md_st *EVP_sha2_512(void)
   1199 {
   1200 	return(&sha2_512_md);
   1201 }
   1202