1240075Sdes/* from OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp */ 2162852Sdes 3162852Sdes/* 4162852Sdes * FILE: sha2.c 5162852Sdes * AUTHOR: Aaron D. Gifford <me@aarongifford.com> 6162852Sdes * 7162852Sdes * Copyright (c) 2000-2001, Aaron D. Gifford 8162852Sdes * All rights reserved. 9162852Sdes * 10162852Sdes * Redistribution and use in source and binary forms, with or without 11162852Sdes * modification, are permitted provided that the following conditions 12162852Sdes * are met: 13162852Sdes * 1. Redistributions of source code must retain the above copyright 14162852Sdes * notice, this list of conditions and the following disclaimer. 15162852Sdes * 2. Redistributions in binary form must reproduce the above copyright 16162852Sdes * notice, this list of conditions and the following disclaimer in the 17162852Sdes * documentation and/or other materials provided with the distribution. 18162852Sdes * 3. Neither the name of the copyright holder nor the names of contributors 19162852Sdes * may be used to endorse or promote products derived from this software 20162852Sdes * without specific prior written permission. 21162852Sdes * 22162852Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND 23162852Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24162852Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25162852Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 26162852Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27162852Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28162852Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29162852Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32162852Sdes * SUCH DAMAGE. 33162852Sdes * 34162852Sdes * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ 35162852Sdes */ 36162852Sdes 37162852Sdes/* OPENBSD ORIGINAL: lib/libc/hash/sha2.c */ 38162852Sdes 39162852Sdes#include "includes.h" 40162852Sdes 41295367Sdes#ifdef WITH_OPENSSL 42295367Sdes# include <openssl/opensslv.h> 43295367Sdes# if !defined(HAVE_EVP_SHA256) && (OPENSSL_VERSION_NUMBER >= 0x00907000L) 44295367Sdes# define _NEED_SHA2 1 45295367Sdes# endif 46295367Sdes#else 47295367Sdes# define _NEED_SHA2 1 48295367Sdes#endif 49162852Sdes 50295367Sdes#if defined(_NEED_SHA2) && !defined(HAVE_SHA256_UPDATE) 51295367Sdes 52162852Sdes#include <string.h> 53162852Sdes 54162852Sdes/* 55162852Sdes * UNROLLED TRANSFORM LOOP NOTE: 56162852Sdes * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform 57162852Sdes * loop version for the hash transform rounds (defined using macros 58162852Sdes * later in this file). Either define on the command line, for example: 59162852Sdes * 60162852Sdes * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c 61162852Sdes * 62162852Sdes * or define below: 63162852Sdes * 64162852Sdes * #define SHA2_UNROLL_TRANSFORM 65162852Sdes * 66162852Sdes */ 67162852Sdes 68162852Sdes/*** SHA-256/384/512 Machine Architecture Definitions *****************/ 69162852Sdes/* 70162852Sdes * BYTE_ORDER NOTE: 71162852Sdes * 72162852Sdes * Please make sure that your system defines BYTE_ORDER. If your 73162852Sdes * architecture is little-endian, make sure it also defines 74162852Sdes * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are 75162852Sdes * equivilent. 76162852Sdes * 77162852Sdes * If your system does not define the above, then you can do so by 78162852Sdes * hand like this: 79162852Sdes * 80162852Sdes * #define LITTLE_ENDIAN 1234 81162852Sdes * #define BIG_ENDIAN 4321 82162852Sdes * 83162852Sdes * And for little-endian machines, add: 84162852Sdes * 85162852Sdes * #define BYTE_ORDER LITTLE_ENDIAN 86162852Sdes * 87162852Sdes * Or for big-endian machines: 88162852Sdes * 89162852Sdes * #define BYTE_ORDER BIG_ENDIAN 90162852Sdes * 91162852Sdes * The FreeBSD machine this was written on defines BYTE_ORDER 92162852Sdes * appropriately by including <sys/types.h> (which in turn includes 93162852Sdes * <machine/endian.h> where the appropriate definitions are actually 94162852Sdes * made). 95162852Sdes */ 96162852Sdes#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) 97162852Sdes#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN 98162852Sdes#endif 99162852Sdes 100162852Sdes 101162852Sdes/*** SHA-256/384/512 Various Length Definitions ***********************/ 102162852Sdes/* NOTE: Most of these are in sha2.h */ 103162852Sdes#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) 104162852Sdes#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) 105162852Sdes#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) 106162852Sdes 107162852Sdes/*** ENDIAN SPECIFIC COPY MACROS **************************************/ 108162852Sdes#define BE_8_TO_32(dst, cp) do { \ 109162852Sdes (dst) = (u_int32_t)(cp)[3] | ((u_int32_t)(cp)[2] << 8) | \ 110162852Sdes ((u_int32_t)(cp)[1] << 16) | ((u_int32_t)(cp)[0] << 24); \ 111162852Sdes} while(0) 112162852Sdes 113162852Sdes#define BE_8_TO_64(dst, cp) do { \ 114162852Sdes (dst) = (u_int64_t)(cp)[7] | ((u_int64_t)(cp)[6] << 8) | \ 115162852Sdes ((u_int64_t)(cp)[5] << 16) | ((u_int64_t)(cp)[4] << 24) | \ 116162852Sdes ((u_int64_t)(cp)[3] << 32) | ((u_int64_t)(cp)[2] << 40) | \ 117162852Sdes ((u_int64_t)(cp)[1] << 48) | ((u_int64_t)(cp)[0] << 56); \ 118162852Sdes} while (0) 119162852Sdes 120162852Sdes#define BE_64_TO_8(cp, src) do { \ 121162852Sdes (cp)[0] = (src) >> 56; \ 122162852Sdes (cp)[1] = (src) >> 48; \ 123162852Sdes (cp)[2] = (src) >> 40; \ 124162852Sdes (cp)[3] = (src) >> 32; \ 125162852Sdes (cp)[4] = (src) >> 24; \ 126162852Sdes (cp)[5] = (src) >> 16; \ 127162852Sdes (cp)[6] = (src) >> 8; \ 128162852Sdes (cp)[7] = (src); \ 129162852Sdes} while (0) 130162852Sdes 131162852Sdes#define BE_32_TO_8(cp, src) do { \ 132162852Sdes (cp)[0] = (src) >> 24; \ 133162852Sdes (cp)[1] = (src) >> 16; \ 134162852Sdes (cp)[2] = (src) >> 8; \ 135162852Sdes (cp)[3] = (src); \ 136162852Sdes} while (0) 137162852Sdes 138162852Sdes/* 139162852Sdes * Macro for incrementally adding the unsigned 64-bit integer n to the 140162852Sdes * unsigned 128-bit integer (represented using a two-element array of 141162852Sdes * 64-bit words): 142162852Sdes */ 143162852Sdes#define ADDINC128(w,n) do { \ 144162852Sdes (w)[0] += (u_int64_t)(n); \ 145162852Sdes if ((w)[0] < (n)) { \ 146162852Sdes (w)[1]++; \ 147162852Sdes } \ 148162852Sdes} while (0) 149162852Sdes 150162852Sdes/*** THE SIX LOGICAL FUNCTIONS ****************************************/ 151162852Sdes/* 152162852Sdes * Bit shifting and rotation (used by the six SHA-XYZ logical functions: 153162852Sdes * 154162852Sdes * NOTE: The naming of R and S appears backwards here (R is a SHIFT and 155162852Sdes * S is a ROTATION) because the SHA-256/384/512 description document 156162852Sdes * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this 157162852Sdes * same "backwards" definition. 158162852Sdes */ 159162852Sdes/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ 160162852Sdes#define R(b,x) ((x) >> (b)) 161162852Sdes/* 32-bit Rotate-right (used in SHA-256): */ 162162852Sdes#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) 163162852Sdes/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ 164162852Sdes#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) 165162852Sdes 166162852Sdes/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ 167162852Sdes#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) 168162852Sdes#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 169162852Sdes 170162852Sdes/* Four of six logical functions used in SHA-256: */ 171162852Sdes#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) 172162852Sdes#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) 173162852Sdes#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) 174162852Sdes#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) 175162852Sdes 176162852Sdes/* Four of six logical functions used in SHA-384 and SHA-512: */ 177162852Sdes#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) 178162852Sdes#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) 179162852Sdes#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) 180162852Sdes#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) 181162852Sdes 182162852Sdes 183162852Sdes/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ 184162852Sdes/* Hash constant words K for SHA-256: */ 185162852Sdesconst static u_int32_t K256[64] = { 186162852Sdes 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 187162852Sdes 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 188162852Sdes 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 189162852Sdes 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 190162852Sdes 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 191162852Sdes 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 192162852Sdes 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 193162852Sdes 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 194162852Sdes 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 195162852Sdes 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 196162852Sdes 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 197162852Sdes 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 198162852Sdes 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 199162852Sdes 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 200162852Sdes 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 201162852Sdes 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL 202162852Sdes}; 203162852Sdes 204162852Sdes/* Initial hash value H for SHA-256: */ 205162852Sdesconst static u_int32_t sha256_initial_hash_value[8] = { 206162852Sdes 0x6a09e667UL, 207162852Sdes 0xbb67ae85UL, 208162852Sdes 0x3c6ef372UL, 209162852Sdes 0xa54ff53aUL, 210162852Sdes 0x510e527fUL, 211162852Sdes 0x9b05688cUL, 212162852Sdes 0x1f83d9abUL, 213162852Sdes 0x5be0cd19UL 214162852Sdes}; 215162852Sdes 216162852Sdes/* Hash constant words K for SHA-384 and SHA-512: */ 217162852Sdesconst static u_int64_t K512[80] = { 218162852Sdes 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 219162852Sdes 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 220162852Sdes 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 221162852Sdes 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 222162852Sdes 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 223162852Sdes 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 224162852Sdes 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 225162852Sdes 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, 226162852Sdes 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 227162852Sdes 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 228162852Sdes 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 229162852Sdes 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 230162852Sdes 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 231162852Sdes 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 232162852Sdes 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 233162852Sdes 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 234162852Sdes 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 235162852Sdes 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 236162852Sdes 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 237162852Sdes 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, 238162852Sdes 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 239162852Sdes 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 240162852Sdes 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 241162852Sdes 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 242162852Sdes 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 243162852Sdes 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 244162852Sdes 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 245162852Sdes 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 246162852Sdes 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 247162852Sdes 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 248162852Sdes 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 249162852Sdes 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, 250162852Sdes 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 251162852Sdes 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 252162852Sdes 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 253162852Sdes 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 254162852Sdes 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 255162852Sdes 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 256162852Sdes 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 257162852Sdes 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL 258162852Sdes}; 259162852Sdes 260162852Sdes/* Initial hash value H for SHA-384 */ 261162852Sdesconst static u_int64_t sha384_initial_hash_value[8] = { 262162852Sdes 0xcbbb9d5dc1059ed8ULL, 263162852Sdes 0x629a292a367cd507ULL, 264162852Sdes 0x9159015a3070dd17ULL, 265162852Sdes 0x152fecd8f70e5939ULL, 266162852Sdes 0x67332667ffc00b31ULL, 267162852Sdes 0x8eb44a8768581511ULL, 268162852Sdes 0xdb0c2e0d64f98fa7ULL, 269162852Sdes 0x47b5481dbefa4fa4ULL 270162852Sdes}; 271162852Sdes 272162852Sdes/* Initial hash value H for SHA-512 */ 273162852Sdesconst static u_int64_t sha512_initial_hash_value[8] = { 274162852Sdes 0x6a09e667f3bcc908ULL, 275162852Sdes 0xbb67ae8584caa73bULL, 276162852Sdes 0x3c6ef372fe94f82bULL, 277162852Sdes 0xa54ff53a5f1d36f1ULL, 278162852Sdes 0x510e527fade682d1ULL, 279162852Sdes 0x9b05688c2b3e6c1fULL, 280162852Sdes 0x1f83d9abfb41bd6bULL, 281162852Sdes 0x5be0cd19137e2179ULL 282162852Sdes}; 283162852Sdes 284162852Sdes 285162852Sdes/*** SHA-256: *********************************************************/ 286162852Sdesvoid 287162852SdesSHA256_Init(SHA256_CTX *context) 288162852Sdes{ 289162852Sdes if (context == NULL) 290162852Sdes return; 291162852Sdes memcpy(context->state, sha256_initial_hash_value, 292162852Sdes sizeof(sha256_initial_hash_value)); 293162852Sdes memset(context->buffer, 0, sizeof(context->buffer)); 294162852Sdes context->bitcount = 0; 295162852Sdes} 296162852Sdes 297162852Sdes#ifdef SHA2_UNROLL_TRANSFORM 298162852Sdes 299162852Sdes/* Unrolled SHA-256 round macros: */ 300162852Sdes 301162852Sdes#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \ 302162852Sdes BE_8_TO_32(W256[j], data); \ 303162852Sdes data += 4; \ 304162852Sdes T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \ 305162852Sdes (d) += T1; \ 306162852Sdes (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ 307162852Sdes j++; \ 308162852Sdes} while(0) 309162852Sdes 310162852Sdes#define ROUND256(a,b,c,d,e,f,g,h) do { \ 311162852Sdes s0 = W256[(j+1)&0x0f]; \ 312162852Sdes s0 = sigma0_256(s0); \ 313162852Sdes s1 = W256[(j+14)&0x0f]; \ 314162852Sdes s1 = sigma1_256(s1); \ 315162852Sdes T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \ 316162852Sdes (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ 317162852Sdes (d) += T1; \ 318162852Sdes (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ 319162852Sdes j++; \ 320162852Sdes} while(0) 321162852Sdes 322162852Sdesvoid 323162852SdesSHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH]) 324162852Sdes{ 325162852Sdes u_int32_t a, b, c, d, e, f, g, h, s0, s1; 326162852Sdes u_int32_t T1, W256[16]; 327162852Sdes int j; 328162852Sdes 329162852Sdes /* Initialize registers with the prev. intermediate value */ 330162852Sdes a = state[0]; 331162852Sdes b = state[1]; 332162852Sdes c = state[2]; 333162852Sdes d = state[3]; 334162852Sdes e = state[4]; 335162852Sdes f = state[5]; 336162852Sdes g = state[6]; 337162852Sdes h = state[7]; 338162852Sdes 339162852Sdes j = 0; 340162852Sdes do { 341162852Sdes /* Rounds 0 to 15 (unrolled): */ 342162852Sdes ROUND256_0_TO_15(a,b,c,d,e,f,g,h); 343162852Sdes ROUND256_0_TO_15(h,a,b,c,d,e,f,g); 344162852Sdes ROUND256_0_TO_15(g,h,a,b,c,d,e,f); 345162852Sdes ROUND256_0_TO_15(f,g,h,a,b,c,d,e); 346162852Sdes ROUND256_0_TO_15(e,f,g,h,a,b,c,d); 347162852Sdes ROUND256_0_TO_15(d,e,f,g,h,a,b,c); 348162852Sdes ROUND256_0_TO_15(c,d,e,f,g,h,a,b); 349162852Sdes ROUND256_0_TO_15(b,c,d,e,f,g,h,a); 350162852Sdes } while (j < 16); 351162852Sdes 352162852Sdes /* Now for the remaining rounds up to 63: */ 353162852Sdes do { 354162852Sdes ROUND256(a,b,c,d,e,f,g,h); 355162852Sdes ROUND256(h,a,b,c,d,e,f,g); 356162852Sdes ROUND256(g,h,a,b,c,d,e,f); 357162852Sdes ROUND256(f,g,h,a,b,c,d,e); 358162852Sdes ROUND256(e,f,g,h,a,b,c,d); 359162852Sdes ROUND256(d,e,f,g,h,a,b,c); 360162852Sdes ROUND256(c,d,e,f,g,h,a,b); 361162852Sdes ROUND256(b,c,d,e,f,g,h,a); 362162852Sdes } while (j < 64); 363162852Sdes 364162852Sdes /* Compute the current intermediate hash value */ 365162852Sdes state[0] += a; 366162852Sdes state[1] += b; 367162852Sdes state[2] += c; 368162852Sdes state[3] += d; 369162852Sdes state[4] += e; 370162852Sdes state[5] += f; 371162852Sdes state[6] += g; 372162852Sdes state[7] += h; 373162852Sdes 374162852Sdes /* Clean up */ 375162852Sdes a = b = c = d = e = f = g = h = T1 = 0; 376162852Sdes} 377162852Sdes 378162852Sdes#else /* SHA2_UNROLL_TRANSFORM */ 379162852Sdes 380162852Sdesvoid 381162852SdesSHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH]) 382162852Sdes{ 383162852Sdes u_int32_t a, b, c, d, e, f, g, h, s0, s1; 384162852Sdes u_int32_t T1, T2, W256[16]; 385162852Sdes int j; 386162852Sdes 387162852Sdes /* Initialize registers with the prev. intermediate value */ 388162852Sdes a = state[0]; 389162852Sdes b = state[1]; 390162852Sdes c = state[2]; 391162852Sdes d = state[3]; 392162852Sdes e = state[4]; 393162852Sdes f = state[5]; 394162852Sdes g = state[6]; 395162852Sdes h = state[7]; 396162852Sdes 397162852Sdes j = 0; 398162852Sdes do { 399162852Sdes BE_8_TO_32(W256[j], data); 400162852Sdes data += 4; 401162852Sdes /* Apply the SHA-256 compression function to update a..h */ 402162852Sdes T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; 403162852Sdes T2 = Sigma0_256(a) + Maj(a, b, c); 404162852Sdes h = g; 405162852Sdes g = f; 406162852Sdes f = e; 407162852Sdes e = d + T1; 408162852Sdes d = c; 409162852Sdes c = b; 410162852Sdes b = a; 411162852Sdes a = T1 + T2; 412162852Sdes 413162852Sdes j++; 414162852Sdes } while (j < 16); 415162852Sdes 416162852Sdes do { 417162852Sdes /* Part of the message block expansion: */ 418162852Sdes s0 = W256[(j+1)&0x0f]; 419162852Sdes s0 = sigma0_256(s0); 420162852Sdes s1 = W256[(j+14)&0x0f]; 421162852Sdes s1 = sigma1_256(s1); 422162852Sdes 423162852Sdes /* Apply the SHA-256 compression function to update a..h */ 424162852Sdes T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 425162852Sdes (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); 426162852Sdes T2 = Sigma0_256(a) + Maj(a, b, c); 427162852Sdes h = g; 428162852Sdes g = f; 429162852Sdes f = e; 430162852Sdes e = d + T1; 431162852Sdes d = c; 432162852Sdes c = b; 433162852Sdes b = a; 434162852Sdes a = T1 + T2; 435162852Sdes 436162852Sdes j++; 437162852Sdes } while (j < 64); 438162852Sdes 439162852Sdes /* Compute the current intermediate hash value */ 440162852Sdes state[0] += a; 441162852Sdes state[1] += b; 442162852Sdes state[2] += c; 443162852Sdes state[3] += d; 444162852Sdes state[4] += e; 445162852Sdes state[5] += f; 446162852Sdes state[6] += g; 447162852Sdes state[7] += h; 448162852Sdes 449162852Sdes /* Clean up */ 450162852Sdes a = b = c = d = e = f = g = h = T1 = T2 = 0; 451162852Sdes} 452162852Sdes 453162852Sdes#endif /* SHA2_UNROLL_TRANSFORM */ 454162852Sdes 455162852Sdesvoid 456162852SdesSHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len) 457162852Sdes{ 458162852Sdes size_t freespace, usedspace; 459162852Sdes 460162852Sdes /* Calling with no data is valid (we do nothing) */ 461162852Sdes if (len == 0) 462162852Sdes return; 463162852Sdes 464162852Sdes usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; 465162852Sdes if (usedspace > 0) { 466162852Sdes /* Calculate how much free space is available in the buffer */ 467162852Sdes freespace = SHA256_BLOCK_LENGTH - usedspace; 468162852Sdes 469162852Sdes if (len >= freespace) { 470162852Sdes /* Fill the buffer completely and process it */ 471162852Sdes memcpy(&context->buffer[usedspace], data, freespace); 472162852Sdes context->bitcount += freespace << 3; 473162852Sdes len -= freespace; 474162852Sdes data += freespace; 475162852Sdes SHA256_Transform(context->state, context->buffer); 476162852Sdes } else { 477162852Sdes /* The buffer is not yet full */ 478162852Sdes memcpy(&context->buffer[usedspace], data, len); 479162852Sdes context->bitcount += len << 3; 480162852Sdes /* Clean up: */ 481162852Sdes usedspace = freespace = 0; 482162852Sdes return; 483162852Sdes } 484162852Sdes } 485162852Sdes while (len >= SHA256_BLOCK_LENGTH) { 486162852Sdes /* Process as many complete blocks as we can */ 487162852Sdes SHA256_Transform(context->state, data); 488162852Sdes context->bitcount += SHA256_BLOCK_LENGTH << 3; 489162852Sdes len -= SHA256_BLOCK_LENGTH; 490162852Sdes data += SHA256_BLOCK_LENGTH; 491162852Sdes } 492162852Sdes if (len > 0) { 493162852Sdes /* There's left-overs, so save 'em */ 494162852Sdes memcpy(context->buffer, data, len); 495162852Sdes context->bitcount += len << 3; 496162852Sdes } 497162852Sdes /* Clean up: */ 498162852Sdes usedspace = freespace = 0; 499162852Sdes} 500162852Sdes 501162852Sdesvoid 502162852SdesSHA256_Pad(SHA256_CTX *context) 503162852Sdes{ 504162852Sdes unsigned int usedspace; 505162852Sdes 506162852Sdes usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; 507162852Sdes if (usedspace > 0) { 508162852Sdes /* Begin padding with a 1 bit: */ 509162852Sdes context->buffer[usedspace++] = 0x80; 510162852Sdes 511162852Sdes if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) { 512162852Sdes /* Set-up for the last transform: */ 513162852Sdes memset(&context->buffer[usedspace], 0, 514162852Sdes SHA256_SHORT_BLOCK_LENGTH - usedspace); 515162852Sdes } else { 516162852Sdes if (usedspace < SHA256_BLOCK_LENGTH) { 517162852Sdes memset(&context->buffer[usedspace], 0, 518162852Sdes SHA256_BLOCK_LENGTH - usedspace); 519162852Sdes } 520162852Sdes /* Do second-to-last transform: */ 521162852Sdes SHA256_Transform(context->state, context->buffer); 522162852Sdes 523162852Sdes /* Prepare for last transform: */ 524162852Sdes memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH); 525162852Sdes } 526162852Sdes } else { 527162852Sdes /* Set-up for the last transform: */ 528162852Sdes memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH); 529162852Sdes 530162852Sdes /* Begin padding with a 1 bit: */ 531162852Sdes *context->buffer = 0x80; 532162852Sdes } 533162852Sdes /* Store the length of input data (in bits) in big endian format: */ 534162852Sdes BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], 535162852Sdes context->bitcount); 536162852Sdes 537162852Sdes /* Final transform: */ 538162852Sdes SHA256_Transform(context->state, context->buffer); 539162852Sdes 540162852Sdes /* Clean up: */ 541162852Sdes usedspace = 0; 542162852Sdes} 543162852Sdes 544162852Sdesvoid 545162852SdesSHA256_Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA256_CTX *context) 546162852Sdes{ 547162852Sdes SHA256_Pad(context); 548162852Sdes 549162852Sdes /* If no digest buffer is passed, we don't bother doing this: */ 550162852Sdes if (digest != NULL) { 551162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN 552162852Sdes int i; 553162852Sdes 554162852Sdes /* Convert TO host byte order */ 555162852Sdes for (i = 0; i < 8; i++) 556162852Sdes BE_32_TO_8(digest + i * 4, context->state[i]); 557162852Sdes#else 558162852Sdes memcpy(digest, context->state, SHA256_DIGEST_LENGTH); 559162852Sdes#endif 560162852Sdes memset(context, 0, sizeof(*context)); 561162852Sdes } 562162852Sdes} 563162852Sdes 564162852Sdes 565162852Sdes/*** SHA-512: *********************************************************/ 566162852Sdesvoid 567162852SdesSHA512_Init(SHA512_CTX *context) 568162852Sdes{ 569162852Sdes if (context == NULL) 570162852Sdes return; 571162852Sdes memcpy(context->state, sha512_initial_hash_value, 572162852Sdes sizeof(sha512_initial_hash_value)); 573162852Sdes memset(context->buffer, 0, sizeof(context->buffer)); 574162852Sdes context->bitcount[0] = context->bitcount[1] = 0; 575162852Sdes} 576162852Sdes 577162852Sdes#ifdef SHA2_UNROLL_TRANSFORM 578162852Sdes 579162852Sdes/* Unrolled SHA-512 round macros: */ 580162852Sdes 581162852Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do { \ 582162852Sdes BE_8_TO_64(W512[j], data); \ 583162852Sdes data += 8; \ 584162852Sdes T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \ 585162852Sdes (d) += T1; \ 586162852Sdes (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \ 587162852Sdes j++; \ 588162852Sdes} while(0) 589162852Sdes 590162852Sdes 591162852Sdes#define ROUND512(a,b,c,d,e,f,g,h) do { \ 592162852Sdes s0 = W512[(j+1)&0x0f]; \ 593162852Sdes s0 = sigma0_512(s0); \ 594162852Sdes s1 = W512[(j+14)&0x0f]; \ 595162852Sdes s1 = sigma1_512(s1); \ 596162852Sdes T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + \ 597162852Sdes (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \ 598162852Sdes (d) += T1; \ 599162852Sdes (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \ 600162852Sdes j++; \ 601162852Sdes} while(0) 602162852Sdes 603162852Sdesvoid 604162852SdesSHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 605162852Sdes{ 606162852Sdes u_int64_t a, b, c, d, e, f, g, h, s0, s1; 607162852Sdes u_int64_t T1, W512[16]; 608162852Sdes int j; 609162852Sdes 610162852Sdes /* Initialize registers with the prev. intermediate value */ 611162852Sdes a = state[0]; 612162852Sdes b = state[1]; 613162852Sdes c = state[2]; 614162852Sdes d = state[3]; 615162852Sdes e = state[4]; 616162852Sdes f = state[5]; 617162852Sdes g = state[6]; 618162852Sdes h = state[7]; 619162852Sdes 620162852Sdes j = 0; 621162852Sdes do { 622162852Sdes /* Rounds 0 to 15 (unrolled): */ 623162852Sdes ROUND512_0_TO_15(a,b,c,d,e,f,g,h); 624162852Sdes ROUND512_0_TO_15(h,a,b,c,d,e,f,g); 625162852Sdes ROUND512_0_TO_15(g,h,a,b,c,d,e,f); 626162852Sdes ROUND512_0_TO_15(f,g,h,a,b,c,d,e); 627162852Sdes ROUND512_0_TO_15(e,f,g,h,a,b,c,d); 628162852Sdes ROUND512_0_TO_15(d,e,f,g,h,a,b,c); 629162852Sdes ROUND512_0_TO_15(c,d,e,f,g,h,a,b); 630162852Sdes ROUND512_0_TO_15(b,c,d,e,f,g,h,a); 631162852Sdes } while (j < 16); 632162852Sdes 633162852Sdes /* Now for the remaining rounds up to 79: */ 634162852Sdes do { 635162852Sdes ROUND512(a,b,c,d,e,f,g,h); 636162852Sdes ROUND512(h,a,b,c,d,e,f,g); 637162852Sdes ROUND512(g,h,a,b,c,d,e,f); 638162852Sdes ROUND512(f,g,h,a,b,c,d,e); 639162852Sdes ROUND512(e,f,g,h,a,b,c,d); 640162852Sdes ROUND512(d,e,f,g,h,a,b,c); 641162852Sdes ROUND512(c,d,e,f,g,h,a,b); 642162852Sdes ROUND512(b,c,d,e,f,g,h,a); 643162852Sdes } while (j < 80); 644162852Sdes 645162852Sdes /* Compute the current intermediate hash value */ 646162852Sdes state[0] += a; 647162852Sdes state[1] += b; 648162852Sdes state[2] += c; 649162852Sdes state[3] += d; 650162852Sdes state[4] += e; 651162852Sdes state[5] += f; 652162852Sdes state[6] += g; 653162852Sdes state[7] += h; 654162852Sdes 655162852Sdes /* Clean up */ 656162852Sdes a = b = c = d = e = f = g = h = T1 = 0; 657162852Sdes} 658162852Sdes 659162852Sdes#else /* SHA2_UNROLL_TRANSFORM */ 660162852Sdes 661162852Sdesvoid 662162852SdesSHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 663162852Sdes{ 664162852Sdes u_int64_t a, b, c, d, e, f, g, h, s0, s1; 665162852Sdes u_int64_t T1, T2, W512[16]; 666162852Sdes int j; 667162852Sdes 668162852Sdes /* Initialize registers with the prev. intermediate value */ 669162852Sdes a = state[0]; 670162852Sdes b = state[1]; 671162852Sdes c = state[2]; 672162852Sdes d = state[3]; 673162852Sdes e = state[4]; 674162852Sdes f = state[5]; 675162852Sdes g = state[6]; 676162852Sdes h = state[7]; 677162852Sdes 678162852Sdes j = 0; 679162852Sdes do { 680162852Sdes BE_8_TO_64(W512[j], data); 681162852Sdes data += 8; 682162852Sdes /* Apply the SHA-512 compression function to update a..h */ 683162852Sdes T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; 684162852Sdes T2 = Sigma0_512(a) + Maj(a, b, c); 685162852Sdes h = g; 686162852Sdes g = f; 687162852Sdes f = e; 688162852Sdes e = d + T1; 689162852Sdes d = c; 690162852Sdes c = b; 691162852Sdes b = a; 692162852Sdes a = T1 + T2; 693162852Sdes 694162852Sdes j++; 695162852Sdes } while (j < 16); 696162852Sdes 697162852Sdes do { 698162852Sdes /* Part of the message block expansion: */ 699162852Sdes s0 = W512[(j+1)&0x0f]; 700162852Sdes s0 = sigma0_512(s0); 701162852Sdes s1 = W512[(j+14)&0x0f]; 702162852Sdes s1 = sigma1_512(s1); 703162852Sdes 704162852Sdes /* Apply the SHA-512 compression function to update a..h */ 705162852Sdes T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + 706162852Sdes (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); 707162852Sdes T2 = Sigma0_512(a) + Maj(a, b, c); 708162852Sdes h = g; 709162852Sdes g = f; 710162852Sdes f = e; 711162852Sdes e = d + T1; 712162852Sdes d = c; 713162852Sdes c = b; 714162852Sdes b = a; 715162852Sdes a = T1 + T2; 716162852Sdes 717162852Sdes j++; 718162852Sdes } while (j < 80); 719162852Sdes 720162852Sdes /* Compute the current intermediate hash value */ 721162852Sdes state[0] += a; 722162852Sdes state[1] += b; 723162852Sdes state[2] += c; 724162852Sdes state[3] += d; 725162852Sdes state[4] += e; 726162852Sdes state[5] += f; 727162852Sdes state[6] += g; 728162852Sdes state[7] += h; 729162852Sdes 730162852Sdes /* Clean up */ 731162852Sdes a = b = c = d = e = f = g = h = T1 = T2 = 0; 732162852Sdes} 733162852Sdes 734162852Sdes#endif /* SHA2_UNROLL_TRANSFORM */ 735162852Sdes 736162852Sdesvoid 737162852SdesSHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) 738162852Sdes{ 739162852Sdes size_t freespace, usedspace; 740162852Sdes 741162852Sdes /* Calling with no data is valid (we do nothing) */ 742162852Sdes if (len == 0) 743162852Sdes return; 744162852Sdes 745162852Sdes usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; 746162852Sdes if (usedspace > 0) { 747162852Sdes /* Calculate how much free space is available in the buffer */ 748162852Sdes freespace = SHA512_BLOCK_LENGTH - usedspace; 749162852Sdes 750162852Sdes if (len >= freespace) { 751162852Sdes /* Fill the buffer completely and process it */ 752162852Sdes memcpy(&context->buffer[usedspace], data, freespace); 753162852Sdes ADDINC128(context->bitcount, freespace << 3); 754162852Sdes len -= freespace; 755162852Sdes data += freespace; 756162852Sdes SHA512_Transform(context->state, context->buffer); 757162852Sdes } else { 758162852Sdes /* The buffer is not yet full */ 759162852Sdes memcpy(&context->buffer[usedspace], data, len); 760162852Sdes ADDINC128(context->bitcount, len << 3); 761162852Sdes /* Clean up: */ 762162852Sdes usedspace = freespace = 0; 763162852Sdes return; 764162852Sdes } 765162852Sdes } 766162852Sdes while (len >= SHA512_BLOCK_LENGTH) { 767162852Sdes /* Process as many complete blocks as we can */ 768162852Sdes SHA512_Transform(context->state, data); 769162852Sdes ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); 770162852Sdes len -= SHA512_BLOCK_LENGTH; 771162852Sdes data += SHA512_BLOCK_LENGTH; 772162852Sdes } 773162852Sdes if (len > 0) { 774162852Sdes /* There's left-overs, so save 'em */ 775162852Sdes memcpy(context->buffer, data, len); 776162852Sdes ADDINC128(context->bitcount, len << 3); 777162852Sdes } 778162852Sdes /* Clean up: */ 779162852Sdes usedspace = freespace = 0; 780162852Sdes} 781162852Sdes 782162852Sdesvoid 783162852SdesSHA512_Pad(SHA512_CTX *context) 784162852Sdes{ 785162852Sdes unsigned int usedspace; 786162852Sdes 787162852Sdes usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; 788162852Sdes if (usedspace > 0) { 789162852Sdes /* Begin padding with a 1 bit: */ 790162852Sdes context->buffer[usedspace++] = 0x80; 791162852Sdes 792162852Sdes if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { 793162852Sdes /* Set-up for the last transform: */ 794162852Sdes memset(&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace); 795162852Sdes } else { 796162852Sdes if (usedspace < SHA512_BLOCK_LENGTH) { 797162852Sdes memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace); 798162852Sdes } 799162852Sdes /* Do second-to-last transform: */ 800162852Sdes SHA512_Transform(context->state, context->buffer); 801162852Sdes 802162852Sdes /* And set-up for the last transform: */ 803162852Sdes memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2); 804162852Sdes } 805162852Sdes } else { 806162852Sdes /* Prepare for final transform: */ 807162852Sdes memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH); 808162852Sdes 809162852Sdes /* Begin padding with a 1 bit: */ 810162852Sdes *context->buffer = 0x80; 811162852Sdes } 812162852Sdes /* Store the length of input data (in bits) in big endian format: */ 813162852Sdes BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], 814162852Sdes context->bitcount[1]); 815162852Sdes BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], 816162852Sdes context->bitcount[0]); 817162852Sdes 818162852Sdes /* Final transform: */ 819162852Sdes SHA512_Transform(context->state, context->buffer); 820162852Sdes 821162852Sdes /* Clean up: */ 822162852Sdes usedspace = 0; 823162852Sdes} 824162852Sdes 825162852Sdesvoid 826162852SdesSHA512_Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context) 827162852Sdes{ 828162852Sdes SHA512_Pad(context); 829162852Sdes 830162852Sdes /* If no digest buffer is passed, we don't bother doing this: */ 831162852Sdes if (digest != NULL) { 832162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN 833162852Sdes int i; 834162852Sdes 835162852Sdes /* Convert TO host byte order */ 836162852Sdes for (i = 0; i < 8; i++) 837162852Sdes BE_64_TO_8(digest + i * 8, context->state[i]); 838162852Sdes#else 839162852Sdes memcpy(digest, context->state, SHA512_DIGEST_LENGTH); 840162852Sdes#endif 841162852Sdes memset(context, 0, sizeof(*context)); 842162852Sdes } 843162852Sdes} 844162852Sdes 845162852Sdes 846162852Sdes/*** SHA-384: *********************************************************/ 847162852Sdesvoid 848162852SdesSHA384_Init(SHA384_CTX *context) 849162852Sdes{ 850162852Sdes if (context == NULL) 851162852Sdes return; 852162852Sdes memcpy(context->state, sha384_initial_hash_value, 853162852Sdes sizeof(sha384_initial_hash_value)); 854162852Sdes memset(context->buffer, 0, sizeof(context->buffer)); 855162852Sdes context->bitcount[0] = context->bitcount[1] = 0; 856162852Sdes} 857162852Sdes 858295367Sdes#if 0 859162852Sdes__weak_alias(SHA384_Transform, SHA512_Transform); 860162852Sdes__weak_alias(SHA384_Update, SHA512_Update); 861162852Sdes__weak_alias(SHA384_Pad, SHA512_Pad); 862295367Sdes#endif 863162852Sdes 864162852Sdesvoid 865295367SdesSHA384_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 866295367Sdes{ 867295367Sdes return SHA512_Transform(state, data); 868295367Sdes} 869295367Sdes 870295367Sdesvoid 871295367SdesSHA384_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) 872295367Sdes{ 873295367Sdes SHA512_Update(context, data, len); 874295367Sdes} 875295367Sdes 876295367Sdesvoid 877295367SdesSHA384_Pad(SHA512_CTX *context) 878295367Sdes{ 879295367Sdes SHA512_Pad(context); 880295367Sdes} 881295367Sdes 882295367Sdesvoid 883162852SdesSHA384_Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA384_CTX *context) 884162852Sdes{ 885162852Sdes SHA384_Pad(context); 886162852Sdes 887162852Sdes /* If no digest buffer is passed, we don't bother doing this: */ 888162852Sdes if (digest != NULL) { 889162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN 890162852Sdes int i; 891162852Sdes 892162852Sdes /* Convert TO host byte order */ 893162852Sdes for (i = 0; i < 6; i++) 894162852Sdes BE_64_TO_8(digest + i * 8, context->state[i]); 895162852Sdes#else 896162852Sdes memcpy(digest, context->state, SHA384_DIGEST_LENGTH); 897162852Sdes#endif 898162852Sdes } 899162852Sdes 900162852Sdes /* Zero out state data */ 901162852Sdes memset(context, 0, sizeof(*context)); 902162852Sdes} 903162852Sdes 904295367Sdes#endif /* defined(_NEED_SHA2) && !defined(HAVE_SHA256_UPDATE) */ 905