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