Version 0.5.0 of tinydtls

Dependents:   tinydtls_test_cellular tinydtls_test_ethernet tiny-dtls

Committer:
ashleymills
Date:
Fri Oct 18 13:18:30 2013 +0000
Revision:
0:ff9ebe0cf0e9
Upgraded to tinydtls 0.5.0

Who changed what in which revision?

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