sha2.c revision aa520ab1cb5d6b9a72d186ded1bff22e31ae1e88
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2005, 2006 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
207845805eb591b77ffbd99735617cab7e2ed804Evan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
cf786a52ce85fd069c764a7de3d036b63a741153Automatic Updater * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * PERFORMANCE OF THIS SOFTWARE.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* $Id: sha2.c,v 1.6 2006/01/31 01:58:12 marka Exp $ */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* $FreeBSD: src/sys/crypto/sha2/sha2.c,v 1.2.2.2 2002/03/05 08:36:47 ume Exp $ */
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont/* $KAME: sha2.c,v 1.8 2001/11/08 01:07:52 itojun Exp $ */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Version 1.0.0beta1
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Written by Aaron D. Gifford <me@aarongifford.com>
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Copyright 2000 Aaron D. Gifford. All rights reserved.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Redistribution and use in source and binary forms, with or without
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * modification, are permitted provided that the following conditions
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * 1. Redistributions of source code must retain the above copyright
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * notice, this list of conditions and the following disclaimer.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * notice, this list of conditions and the following disclaimer in the
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * documentation and/or other materials provided with the distribution.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * 3. Neither the name of the copyright holder nor the names of contributors
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * may be used to endorse or promote products derived from this software
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * without specific prior written permission.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * SUCH DAMAGE.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * UNROLLED TRANSFORM LOOP NOTE:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * loop version for the hash transform rounds (defined using macros
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * later in this file). Either define on the command line, for example:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * cc -DISC_SHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * or define below:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * #define ISC_SHA2_UNROLL_TRANSFORM
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** SHA-256/384/512 Machine Architecture Definitions *****************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * BYTE_ORDER NOTE:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Please make sure that your system defines BYTE_ORDER. If your
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * architecture is little-endian, make sure it also defines
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * equivilent.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * If your system does not define the above, then you can do so by
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * hand like this:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * #define LITTLE_ENDIAN 1234
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * #define BIG_ENDIAN 4321
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * And for little-endian machines, add:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * #define BYTE_ORDER LITTLE_ENDIAN
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Or for big-endian machines:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * #define BYTE_ORDER BIG_ENDIAN
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * The FreeBSD machine this was written on defines BYTE_ORDER
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * appropriately by including <sys/types.h> (which in turn includes
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * <machine/endian.h> where the appropriate definitions are actually
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** SHA-256/384/512 Various Length Definitions ***********************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* NOTE: Most of these are in sha2.h */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ISC_SHA256_SHORT_BLOCK_LENGTH (ISC_SHA256_BLOCK_LENGTH - 8)
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ISC_SHA384_SHORT_BLOCK_LENGTH (ISC_SHA384_BLOCK_LENGTH - 16)
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ISC_SHA512_SHORT_BLOCK_LENGTH (ISC_SHA512_BLOCK_LENGTH - 16)
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** ENDIAN REVERSAL MACROS *******************************************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define REVERSE32(w,x) { \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define REVERSE64(w,x) { \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#endif /* BYTE_ORDER == LITTLE_ENDIAN */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Macro for incrementally adding the unsigned 64-bit integer n to the
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * unsigned 128-bit integer (represented using a two-element array of
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * 64-bit words):
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ADDINC128(w,n) { \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt (w)[0] += (isc_uint64_t)(n); \
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt if ((w)[0] < (n)) { \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** THE SIX LOGICAL FUNCTIONS ****************************************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt * S is a ROTATION) because the SHA-256/384/512 description document
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * same "backwards" definition.
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define R(b,x) ((x) >> (b))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* 32-bit Rotate-right (used in SHA-256): */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Four of six logical functions used in SHA-256: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Four of six logical functions used in SHA-384 and SHA-512: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** INTERNAL FUNCTION PROTOTYPES *************************************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* NOTE: These should not be accessed directly from outside this
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * library -- they are intended for private internal visibility/use
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntvoid isc_sha256_transform(isc_sha256_t *, const isc_uint32_t*);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntvoid isc_sha512_transform(isc_sha512_t *, const isc_uint64_t*);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Hash constant words K for SHA-224 and SHA-256: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Initial hash value H for SHA-224: */
41bbb34bc20f189af62e7047ce42822615417f15Evan Huntstatic const isc_uint32_t sha224_initial_hash_value[8] = {
41bbb34bc20f189af62e7047ce42822615417f15Evan Hunt 0xc1059ed8UL,
41bbb34bc20f189af62e7047ce42822615417f15Evan Hunt 0x367cd507UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x3070dd17UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xf70e5939UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xffc00b31UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x68581511UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x64f98fa7UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xbefa4fa4UL
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Initial hash value H for SHA-256: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntstatic const isc_uint32_t sha256_initial_hash_value[8] = {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x6a09e667UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xbb67ae85UL,
c1d33c159bf81d6faf9948ac9a6f307ca52284afEvan Hunt 0x3c6ef372UL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xa54ff53aUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x510e527fUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x9b05688cUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x1f83d9abUL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x5be0cd19UL
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Hash constant words K for SHA-384 and SHA-512: */
08df939613d7f20bdac132a93efc537bb457ccfaTinderbox User 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Initial hash value H for SHA-384: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntstatic const isc_uint64_t sha384_initial_hash_value[8] = {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0xcbbb9d5dc1059ed8ULL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x629a292a367cd507ULL,
c1d33c159bf81d6faf9948ac9a6f307ca52284afEvan Hunt 0x9159015a3070dd17ULL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x152fecd8f70e5939ULL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x67332667ffc00b31ULL,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt 0x8eb44a8768581511ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xdb0c2e0d64f98fa7ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x47b5481dbefa4fa4ULL
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont/* Initial hash value H for SHA-512: */
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupontstatic const isc_uint64_t sha512_initial_hash_value[8] = {
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x6a09e667f3bcc908ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xbb67ae8584caa73bULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x3c6ef372fe94f82bULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0xa54ff53a5f1d36f1ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x510e527fade682d1ULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x9b05688c2b3e6c1fULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x1f83d9abfb41bd6bULL,
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont 0x5be0cd19137e2179ULL
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * Constant used by SHA256/384/512_End() functions for converting the
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont * digest to a readable hexadecimal character string:
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupontstatic const char *sha2_hex_digits = "0123456789abcdef";
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** SHA-224: *********************************************************/
cbd1fa092ea66bfa9990c5e515725646295396c5Evan Hunt memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_sha256_update((isc_sha256_t *)context, data, len);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha224_final(isc_uint8_t digest[], isc_sha256_t *context) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_uint8_t sha256_digest[ISC_SHA256_DIGESTLENGTH];
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_sha256_final(sha256_digest, (isc_sha256_t *)context);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt memcpy(digest, sha256_digest, ISC_SHA224_DIGESTLENGTH);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt memset(sha256_digest, 0, ISC_SHA256_DIGESTLENGTH);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha224_end(isc_sha224_t *context, char buffer[]) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_uint8_t digest[ISC_SHA224_DIGESTLENGTH], *d = digest;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt unsigned int i;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Sanity check: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt if (buffer != (char*)0) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt for (i = 0; i < ISC_SHA224_DIGESTLENGTH; i++) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha224_data(const isc_uint8_t *data, size_t len,
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/*** SHA-256: *********************************************************/
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt/* Unrolled SHA-256 round macros: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
50f64cf0e58073f61bea3e1e4a9ad258bca80961Francis Dupont T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#else /* BYTE_ORDER == LITTLE_ENDIAN */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#endif /* BYTE_ORDER == LITTLE_ENDIAN */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#define ROUND256(a,b,c,d,e,f,g,h) \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntvoid isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Initialize registers with the prev. intermediate value */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Rounds 0 to 15 (unrolled): */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
2b8bed6681d1541474f022586cbe728dfce36880Evan Hunt ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
2b8bed6681d1541474f022586cbe728dfce36880Evan Hunt ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
2b8bed6681d1541474f022586cbe728dfce36880Evan Hunt ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt } while (j < 16);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Now for the remaining rounds to 64: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(a,b,c,d,e,f,g,h);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(h,a,b,c,d,e,f,g);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(g,h,a,b,c,d,e,f);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(f,g,h,a,b,c,d,e);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(e,f,g,h,a,b,c,d);
2b8bed6681d1541474f022586cbe728dfce36880Evan Hunt ROUND256(d,e,f,g,h,a,b,c);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(c,d,e,f,g,h,a,b);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt ROUND256(b,c,d,e,f,g,h,a);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt } while (j < 64);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Compute the current intermediate hash value */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Clean up */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt a = b = c = d = e = f = g = h = T1 = 0;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#else /* ISC_SHA2_UNROLL_TRANSFORM */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Initialize registers with the prev. intermediate value */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Copy data while converting to host byte order */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Apply the SHA-256 compression function to update a..h */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#else /* BYTE_ORDER == LITTLE_ENDIAN */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Apply the SHA-256 compression function to update a..h with copy */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#endif /* BYTE_ORDER == LITTLE_ENDIAN */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt } while (j < 16);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Part of the message block expansion: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Apply the SHA-256 compression function to update a..h */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt } while (j < 64);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Compute the current intermediate hash value */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Clean up */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt#endif /* ISC_SHA2_UNROLL_TRANSFORM */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt if (len == 0) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Calling with no data is valid - we do nothing */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Sanity check: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt usedspace = (context->bitcount >> 3) % ISC_SHA256_BLOCK_LENGTH;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Calculate how much free space is available in the buffer */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Fill the buffer completely and process it */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt memcpy(&context->buffer[usedspace], data, freespace);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* The buffer is not yet full */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Clean up: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Process as many complete blocks as we can */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt isc_sha256_transform(context, (const isc_uint32_t*)data);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt context->bitcount += ISC_SHA256_BLOCK_LENGTH << 3;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* There's left-overs, so save 'em */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Clean up: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Huntisc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt unsigned int usedspace;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Sanity check: */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* If no digest buffer is passed, we don't bother doing this: */
if (usedspace > 0) {
usedspace = 0;
if (buffer != (char*)0) {
for (i = 0; i < ISC_SHA256_DIGESTLENGTH; i++) {
*buffer = (char)0;
return buffer;
#ifdef ISC_SHA2_UNROLL_TRANSFORM
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
(d) += T1, \
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
(d) += T1; \
#define ROUND512(a,b,c,d,e,f,g,h) \
(d) += T1; \
ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
ROUND512(a,b,c,d,e,f,g,h);
ROUND512(h,a,b,c,d,e,f,g);
ROUND512(g,h,a,b,c,d,e,f);
ROUND512(f,g,h,a,b,c,d,e);
ROUND512(e,f,g,h,a,b,c,d);
ROUND512(d,e,f,g,h,a,b,c);
ROUND512(c,d,e,f,g,h,a,b);
ROUND512(b,c,d,e,f,g,h,a);
a = b = c = d = e = f = g = h = T1 = 0;
/* Apply the SHA-512 compression function to update a..h */
/* Apply the SHA-512 compression function to update a..h with copy */
e = d + T1;
/* Apply the SHA-512 compression function to update a..h */
e = d + T1;
if (len == 0) {
if (usedspace > 0) {
if (len > 0) {
unsigned int usedspace;
if (usedspace > 0) {
if (buffer != (char*)0) {
for (i = 0; i < ISC_SHA512_DIGESTLENGTH; i++) {
*buffer = (char)0;
return buffer;
if (buffer != (char*)0) {
for (i = 0; i < ISC_SHA384_DIGESTLENGTH; i++) {
*buffer = (char)0;
return buffer;