mbed port of tinydtls

Committer:
ashleymills
Date:
Fri Oct 11 08:46:21 2013 +0000
Revision:
1:bc8a649bad13
Parent:
0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.

Who changed what in which revision?

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