17c478bd9Sstevel@tonic-gate /* 255553f71Sda73024 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* 77c478bd9Sstevel@tonic-gate * The basic framework for this code came from the reference 87c478bd9Sstevel@tonic-gate * implementation for MD5. That implementation is Copyright (C) 97c478bd9Sstevel@tonic-gate * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. 107c478bd9Sstevel@tonic-gate * 117c478bd9Sstevel@tonic-gate * License to copy and use this software is granted provided that it 127c478bd9Sstevel@tonic-gate * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 137c478bd9Sstevel@tonic-gate * Algorithm" in all material mentioning or referencing this software 147c478bd9Sstevel@tonic-gate * or this function. 157c478bd9Sstevel@tonic-gate * 167c478bd9Sstevel@tonic-gate * License is also granted to make and use derivative works provided 177c478bd9Sstevel@tonic-gate * that such works are identified as "derived from the RSA Data 187c478bd9Sstevel@tonic-gate * Security, Inc. MD5 Message-Digest Algorithm" in all material 197c478bd9Sstevel@tonic-gate * mentioning or referencing the derived work. 207c478bd9Sstevel@tonic-gate * 217c478bd9Sstevel@tonic-gate * RSA Data Security, Inc. makes no representations concerning either 227c478bd9Sstevel@tonic-gate * the merchantability of this software or the suitability of this 237c478bd9Sstevel@tonic-gate * software for any particular purpose. It is provided "as is" 247c478bd9Sstevel@tonic-gate * without express or implied warranty of any kind. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * These notices must be retained in any copies of any part of this 277c478bd9Sstevel@tonic-gate * documentation and/or software. 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2 30*4b56a003SDaniel Anderson * standard, available at 31*4b56a003SDaniel Anderson * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 327c478bd9Sstevel@tonic-gate * Not as fast as one would like -- further optimizations are encouraged 337c478bd9Sstevel@tonic-gate * and appreciated. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/param.h> 387c478bd9Sstevel@tonic-gate #include <sys/systm.h> 397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 40734b6a94Sdarrenm #define _SHA2_IMPL 417c478bd9Sstevel@tonic-gate #include <sys/sha2.h> 427c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h> 437c478bd9Sstevel@tonic-gate 4455553f71Sda73024 #ifdef _KERNEL 4555553f71Sda73024 #include <sys/cmn_err.h> 467c478bd9Sstevel@tonic-gate 4755553f71Sda73024 #else 487c478bd9Sstevel@tonic-gate #include <strings.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <errno.h> 517c478bd9Sstevel@tonic-gate 52734b6a94Sdarrenm #pragma weak SHA256Update = SHA2Update 53734b6a94Sdarrenm #pragma weak SHA384Update = SHA2Update 54734b6a94Sdarrenm #pragma weak SHA512Update = SHA2Update 55734b6a94Sdarrenm 56734b6a94Sdarrenm #pragma weak SHA256Final = SHA2Final 57734b6a94Sdarrenm #pragma weak SHA384Final = SHA2Final 58734b6a94Sdarrenm #pragma weak SHA512Final = SHA2Final 59734b6a94Sdarrenm 60734b6a94Sdarrenm #endif /* _KERNEL */ 61734b6a94Sdarrenm 62*4b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN 63*4b56a003SDaniel Anderson #include <sys/byteorder.h> 64*4b56a003SDaniel Anderson #define HAVE_HTONL 65*4b56a003SDaniel Anderson #endif 66*4b56a003SDaniel Anderson 677c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t); 687c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t); 6955553f71Sda73024 7055553f71Sda73024 #if defined(__amd64) 7155553f71Sda73024 #define SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1) 7255553f71Sda73024 #define SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1) 7355553f71Sda73024 7455553f71Sda73024 void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num); 7555553f71Sda73024 void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num); 7655553f71Sda73024 7755553f71Sda73024 #else 787c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *); 797c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *); 8055553f71Sda73024 #endif /* __amd64 */ 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ }; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */ 857c478bd9Sstevel@tonic-gate #define Ch(b, c, d) (((b) & (c)) ^ ((~b) & (d))) 867c478bd9Sstevel@tonic-gate #define Maj(b, c, d) (((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d))) 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */ 897c478bd9Sstevel@tonic-gate #define ROTR(x, n) \ 907c478bd9Sstevel@tonic-gate (((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n)))) 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate /* Shift x right n bits */ 937c478bd9Sstevel@tonic-gate #define SHR(x, n) ((x) >> (n)) 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* SHA256 Functions */ 967c478bd9Sstevel@tonic-gate #define BIGSIGMA0_256(x) (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22)) 977c478bd9Sstevel@tonic-gate #define BIGSIGMA1_256(x) (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25)) 987c478bd9Sstevel@tonic-gate #define SIGMA0_256(x) (ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3)) 997c478bd9Sstevel@tonic-gate #define SIGMA1_256(x) (ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10)) 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate #define SHA256ROUND(a, b, c, d, e, f, g, h, i, w) \ 1027c478bd9Sstevel@tonic-gate T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w; \ 1037c478bd9Sstevel@tonic-gate d += T1; \ 1047c478bd9Sstevel@tonic-gate T2 = BIGSIGMA0_256(a) + Maj(a, b, c); \ 1057c478bd9Sstevel@tonic-gate h = T1 + T2 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */ 1087c478bd9Sstevel@tonic-gate #define BIGSIGMA0(x) (ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39)) 1097c478bd9Sstevel@tonic-gate #define BIGSIGMA1(x) (ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41)) 1107c478bd9Sstevel@tonic-gate #define SIGMA0(x) (ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7)) 1117c478bd9Sstevel@tonic-gate #define SIGMA1(x) (ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6)) 1127c478bd9Sstevel@tonic-gate #define SHA512ROUND(a, b, c, d, e, f, g, h, i, w) \ 1137c478bd9Sstevel@tonic-gate T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w; \ 1147c478bd9Sstevel@tonic-gate d += T1; \ 1157c478bd9Sstevel@tonic-gate T2 = BIGSIGMA0(a) + Maj(a, b, c); \ 1167c478bd9Sstevel@tonic-gate h = T1 + T2 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * sparc optimization: 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * on the sparc, we can load big endian 32-bit data easily. note that 1227c478bd9Sstevel@tonic-gate * special care must be taken to ensure the address is 32-bit aligned. 1237c478bd9Sstevel@tonic-gate * in the interest of speed, we don't check to make sure, since 1247c478bd9Sstevel@tonic-gate * careful programming can guarantee this for us. 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN) 1287c478bd9Sstevel@tonic-gate #define LOAD_BIG_32(addr) (*(uint32_t *)(addr)) 1297c478bd9Sstevel@tonic-gate #define LOAD_BIG_64(addr) (*(uint64_t *)(addr)) 1307c478bd9Sstevel@tonic-gate 131*4b56a003SDaniel Anderson #elif defined(HAVE_HTONL) 132*4b56a003SDaniel Anderson #define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr))) 133*4b56a003SDaniel Anderson #define LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr))) 1347c478bd9Sstevel@tonic-gate 135*4b56a003SDaniel Anderson #else 136*4b56a003SDaniel Anderson /* little endian -- will work on big endian, but slowly */ 137*4b56a003SDaniel Anderson #define LOAD_BIG_32(addr) \ 138*4b56a003SDaniel Anderson (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3]) 1397c478bd9Sstevel@tonic-gate #define LOAD_BIG_64(addr) \ 1407c478bd9Sstevel@tonic-gate (((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) | \ 1417c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) | \ 1427c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) | \ 1437c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7]) 144*4b56a003SDaniel Anderson #endif /* _BIG_ENDIAN */ 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate 14755553f71Sda73024 #if !defined(__amd64) 1487c478bd9Sstevel@tonic-gate /* SHA256 Transform */ 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate static void 1517c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate uint32_t a = ctx->state.s32[0]; 1547c478bd9Sstevel@tonic-gate uint32_t b = ctx->state.s32[1]; 1557c478bd9Sstevel@tonic-gate uint32_t c = ctx->state.s32[2]; 1567c478bd9Sstevel@tonic-gate uint32_t d = ctx->state.s32[3]; 1577c478bd9Sstevel@tonic-gate uint32_t e = ctx->state.s32[4]; 1587c478bd9Sstevel@tonic-gate uint32_t f = ctx->state.s32[5]; 1597c478bd9Sstevel@tonic-gate uint32_t g = ctx->state.s32[6]; 1607c478bd9Sstevel@tonic-gate uint32_t h = ctx->state.s32[7]; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate uint32_t w0, w1, w2, w3, w4, w5, w6, w7; 1637c478bd9Sstevel@tonic-gate uint32_t w8, w9, w10, w11, w12, w13, w14, w15; 1647c478bd9Sstevel@tonic-gate uint32_t T1, T2; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate #if defined(__sparc) 1677c478bd9Sstevel@tonic-gate static const uint32_t sha256_consts[] = { 1687c478bd9Sstevel@tonic-gate SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2, 1697c478bd9Sstevel@tonic-gate SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5, 1707c478bd9Sstevel@tonic-gate SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8, 1717c478bd9Sstevel@tonic-gate SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11, 1727c478bd9Sstevel@tonic-gate SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14, 1737c478bd9Sstevel@tonic-gate SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17, 1747c478bd9Sstevel@tonic-gate SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20, 1757c478bd9Sstevel@tonic-gate SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23, 1767c478bd9Sstevel@tonic-gate SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26, 1777c478bd9Sstevel@tonic-gate SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29, 1787c478bd9Sstevel@tonic-gate SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32, 1797c478bd9Sstevel@tonic-gate SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35, 1807c478bd9Sstevel@tonic-gate SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38, 1817c478bd9Sstevel@tonic-gate SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41, 1827c478bd9Sstevel@tonic-gate SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44, 1837c478bd9Sstevel@tonic-gate SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47, 1847c478bd9Sstevel@tonic-gate SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50, 1857c478bd9Sstevel@tonic-gate SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53, 1867c478bd9Sstevel@tonic-gate SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56, 1877c478bd9Sstevel@tonic-gate SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59, 1887c478bd9Sstevel@tonic-gate SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62, 1897c478bd9Sstevel@tonic-gate SHA256_CONST_63 1907c478bd9Sstevel@tonic-gate }; 19155553f71Sda73024 #endif /* __sparc */ 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if ((uintptr_t)blk & 0x3) { /* not 4-byte aligned? */ 1947c478bd9Sstevel@tonic-gate bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32)); 1957c478bd9Sstevel@tonic-gate blk = (uint8_t *)ctx->buf_un.buf32; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 198734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 199f66d273dSizick w0 = LOAD_BIG_32(blk + 4 * 0); 200f66d273dSizick SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0); 201734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 202f66d273dSizick w1 = LOAD_BIG_32(blk + 4 * 1); 203f66d273dSizick SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1); 204734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 205f66d273dSizick w2 = LOAD_BIG_32(blk + 4 * 2); 206f66d273dSizick SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2); 207734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 208f66d273dSizick w3 = LOAD_BIG_32(blk + 4 * 3); 209f66d273dSizick SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3); 210734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 211f66d273dSizick w4 = LOAD_BIG_32(blk + 4 * 4); 212f66d273dSizick SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4); 213734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 214f66d273dSizick w5 = LOAD_BIG_32(blk + 4 * 5); 215f66d273dSizick SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5); 216734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 217f66d273dSizick w6 = LOAD_BIG_32(blk + 4 * 6); 218f66d273dSizick SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6); 219734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 220f66d273dSizick w7 = LOAD_BIG_32(blk + 4 * 7); 221f66d273dSizick SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7); 222734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 223f66d273dSizick w8 = LOAD_BIG_32(blk + 4 * 8); 224f66d273dSizick SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8); 225734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 226f66d273dSizick w9 = LOAD_BIG_32(blk + 4 * 9); 227f66d273dSizick SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9); 228734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 229f66d273dSizick w10 = LOAD_BIG_32(blk + 4 * 10); 230f66d273dSizick SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10); 231734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 232f66d273dSizick w11 = LOAD_BIG_32(blk + 4 * 11); 233f66d273dSizick SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11); 234734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 235f66d273dSizick w12 = LOAD_BIG_32(blk + 4 * 12); 236f66d273dSizick SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12); 237734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 238f66d273dSizick w13 = LOAD_BIG_32(blk + 4 * 13); 239f66d273dSizick SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13); 240734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 241f66d273dSizick w14 = LOAD_BIG_32(blk + 4 * 14); 242f66d273dSizick SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14); 243734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 244f66d273dSizick w15 = LOAD_BIG_32(blk + 4 * 15); 245f66d273dSizick SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15); 246f66d273dSizick 2477c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0; 2487c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0); 2497c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1; 2507c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1); 2517c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2; 2527c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2); 2537c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3; 2547c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3); 2557c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4; 2567c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4); 2577c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5; 2587c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5); 2597c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6; 2607c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6); 2617c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7; 2627c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7); 2637c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8; 2647c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8); 2657c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9; 2667c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9); 2677c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10; 2687c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10); 2697c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11; 2707c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11); 2717c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12; 2727c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12); 2737c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13; 2747c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13); 2757c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14; 2767c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14); 2777c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15; 2787c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0; 2817c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0); 2827c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1; 2837c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1); 2847c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2; 2857c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2); 2867c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3; 2877c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3); 2887c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4; 2897c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4); 2907c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5; 2917c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5); 2927c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6; 2937c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6); 2947c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7; 2957c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7); 2967c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8; 2977c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8); 2987c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9; 2997c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9); 3007c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10; 3017c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10); 3027c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11; 3037c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11); 3047c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12; 3057c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12); 3067c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13; 3077c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13); 3087c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14; 3097c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14); 3107c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15; 3117c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0; 3147c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0); 3157c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1; 3167c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1); 3177c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2; 3187c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2); 3197c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3; 3207c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3); 3217c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4; 3227c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4); 3237c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5; 3247c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5); 3257c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6; 3267c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6); 3277c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7; 3287c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7); 3297c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8; 3307c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8); 3317c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9; 3327c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9); 3337c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10; 3347c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10); 3357c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11; 3367c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11); 3377c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12; 3387c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12); 3397c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13; 3407c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13); 3417c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14; 3427c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14); 3437c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15; 3447c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate ctx->state.s32[0] += a; 3477c478bd9Sstevel@tonic-gate ctx->state.s32[1] += b; 3487c478bd9Sstevel@tonic-gate ctx->state.s32[2] += c; 3497c478bd9Sstevel@tonic-gate ctx->state.s32[3] += d; 3507c478bd9Sstevel@tonic-gate ctx->state.s32[4] += e; 3517c478bd9Sstevel@tonic-gate ctx->state.s32[5] += f; 3527c478bd9Sstevel@tonic-gate ctx->state.s32[6] += g; 3537c478bd9Sstevel@tonic-gate ctx->state.s32[7] += h; 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */ 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate static void 3607c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk) 3617c478bd9Sstevel@tonic-gate { 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate uint64_t a = ctx->state.s64[0]; 3647c478bd9Sstevel@tonic-gate uint64_t b = ctx->state.s64[1]; 3657c478bd9Sstevel@tonic-gate uint64_t c = ctx->state.s64[2]; 3667c478bd9Sstevel@tonic-gate uint64_t d = ctx->state.s64[3]; 3677c478bd9Sstevel@tonic-gate uint64_t e = ctx->state.s64[4]; 3687c478bd9Sstevel@tonic-gate uint64_t f = ctx->state.s64[5]; 3697c478bd9Sstevel@tonic-gate uint64_t g = ctx->state.s64[6]; 3707c478bd9Sstevel@tonic-gate uint64_t h = ctx->state.s64[7]; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate uint64_t w0, w1, w2, w3, w4, w5, w6, w7; 3737c478bd9Sstevel@tonic-gate uint64_t w8, w9, w10, w11, w12, w13, w14, w15; 3747c478bd9Sstevel@tonic-gate uint64_t T1, T2; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate #if defined(__sparc) 3777c478bd9Sstevel@tonic-gate static const uint64_t sha512_consts[] = { 3787c478bd9Sstevel@tonic-gate SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2, 3797c478bd9Sstevel@tonic-gate SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5, 3807c478bd9Sstevel@tonic-gate SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8, 3817c478bd9Sstevel@tonic-gate SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11, 3827c478bd9Sstevel@tonic-gate SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14, 3837c478bd9Sstevel@tonic-gate SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17, 3847c478bd9Sstevel@tonic-gate SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20, 3857c478bd9Sstevel@tonic-gate SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23, 3867c478bd9Sstevel@tonic-gate SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26, 3877c478bd9Sstevel@tonic-gate SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29, 3887c478bd9Sstevel@tonic-gate SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32, 3897c478bd9Sstevel@tonic-gate SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35, 3907c478bd9Sstevel@tonic-gate SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38, 3917c478bd9Sstevel@tonic-gate SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41, 3927c478bd9Sstevel@tonic-gate SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44, 3937c478bd9Sstevel@tonic-gate SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47, 3947c478bd9Sstevel@tonic-gate SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50, 3957c478bd9Sstevel@tonic-gate SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53, 3967c478bd9Sstevel@tonic-gate SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56, 3977c478bd9Sstevel@tonic-gate SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59, 3987c478bd9Sstevel@tonic-gate SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62, 3997c478bd9Sstevel@tonic-gate SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65, 4007c478bd9Sstevel@tonic-gate SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68, 4017c478bd9Sstevel@tonic-gate SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71, 4027c478bd9Sstevel@tonic-gate SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74, 4037c478bd9Sstevel@tonic-gate SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77, 4047c478bd9Sstevel@tonic-gate SHA512_CONST_78, SHA512_CONST_79 4057c478bd9Sstevel@tonic-gate }; 40655553f71Sda73024 #endif /* __sparc */ 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate if ((uintptr_t)blk & 0x7) { /* not 8-byte aligned? */ 4107c478bd9Sstevel@tonic-gate bcopy(blk, ctx->buf_un.buf64, sizeof (ctx->buf_un.buf64)); 4117c478bd9Sstevel@tonic-gate blk = (uint8_t *)ctx->buf_un.buf64; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 414734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 415f66d273dSizick w0 = LOAD_BIG_64(blk + 8 * 0); 416f66d273dSizick SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0); 417734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 418f66d273dSizick w1 = LOAD_BIG_64(blk + 8 * 1); 419f66d273dSizick SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1); 420734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 421f66d273dSizick w2 = LOAD_BIG_64(blk + 8 * 2); 422f66d273dSizick SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2); 423734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 424f66d273dSizick w3 = LOAD_BIG_64(blk + 8 * 3); 425f66d273dSizick SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3); 426734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 427f66d273dSizick w4 = LOAD_BIG_64(blk + 8 * 4); 428f66d273dSizick SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4); 429734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 430f66d273dSizick w5 = LOAD_BIG_64(blk + 8 * 5); 431f66d273dSizick SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5); 432734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 433f66d273dSizick w6 = LOAD_BIG_64(blk + 8 * 6); 434f66d273dSizick SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6); 435734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 436f66d273dSizick w7 = LOAD_BIG_64(blk + 8 * 7); 437f66d273dSizick SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7); 438734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 439f66d273dSizick w8 = LOAD_BIG_64(blk + 8 * 8); 440f66d273dSizick SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8); 441734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 442f66d273dSizick w9 = LOAD_BIG_64(blk + 8 * 9); 443f66d273dSizick SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9); 444734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 445f66d273dSizick w10 = LOAD_BIG_64(blk + 8 * 10); 446f66d273dSizick SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10); 447734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 448f66d273dSizick w11 = LOAD_BIG_64(blk + 8 * 11); 449f66d273dSizick SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11); 450734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 451f66d273dSizick w12 = LOAD_BIG_64(blk + 8 * 12); 452f66d273dSizick SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12); 453734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 454f66d273dSizick w13 = LOAD_BIG_64(blk + 8 * 13); 455f66d273dSizick SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13); 456734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 457f66d273dSizick w14 = LOAD_BIG_64(blk + 8 * 14); 458f66d273dSizick SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14); 459734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 460f66d273dSizick w15 = LOAD_BIG_64(blk + 8 * 15); 461f66d273dSizick SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15); 462f66d273dSizick 4637c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0; 4647c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0); 4657c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1; 4667c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1); 4677c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2; 4687c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2); 4697c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3; 4707c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3); 4717c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4; 4727c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4); 4737c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5; 4747c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5); 4757c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6; 4767c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6); 4777c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7; 4787c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7); 4797c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8; 4807c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8); 4817c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9; 4827c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9); 4837c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10; 4847c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10); 4857c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11; 4867c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11); 4877c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12; 4887c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12); 4897c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13; 4907c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13); 4917c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14; 4927c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14); 4937c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15; 4947c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0; 4977c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0); 4987c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1; 4997c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1); 5007c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2; 5017c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2); 5027c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3; 5037c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3); 5047c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4; 5057c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4); 5067c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5; 5077c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5); 5087c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6; 5097c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6); 5107c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7; 5117c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7); 5127c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8; 5137c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8); 5147c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9; 5157c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9); 5167c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10; 5177c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10); 5187c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11; 5197c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11); 5207c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12; 5217c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12); 5227c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13; 5237c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13); 5247c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14; 5257c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14); 5267c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15; 5277c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15); 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0; 5307c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0); 5317c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1; 5327c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1); 5337c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2; 5347c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2); 5357c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3; 5367c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3); 5377c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4; 5387c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4); 5397c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5; 5407c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5); 5417c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6; 5427c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6); 5437c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7; 5447c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7); 5457c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8; 5467c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8); 5477c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9; 5487c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9); 5497c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10; 5507c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10); 5517c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11; 5527c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11); 5537c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12; 5547c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12); 5557c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13; 5567c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13); 5577c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14; 5587c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14); 5597c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15; 5607c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15); 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0; 5637c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0); 5647c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1; 5657c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1); 5667c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2; 5677c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2); 5687c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3; 5697c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3); 5707c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4; 5717c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4); 5727c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5; 5737c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5); 5747c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6; 5757c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6); 5767c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7; 5777c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7); 5787c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8; 5797c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8); 5807c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9; 5817c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9); 5827c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10; 5837c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10); 5847c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11; 5857c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11); 5867c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12; 5877c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12); 5887c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13; 5897c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13); 5907c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14; 5917c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14); 5927c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15; 5937c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15); 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate ctx->state.s64[0] += a; 5967c478bd9Sstevel@tonic-gate ctx->state.s64[1] += b; 5977c478bd9Sstevel@tonic-gate ctx->state.s64[2] += c; 5987c478bd9Sstevel@tonic-gate ctx->state.s64[3] += d; 5997c478bd9Sstevel@tonic-gate ctx->state.s64[4] += e; 6007c478bd9Sstevel@tonic-gate ctx->state.s64[5] += f; 6017c478bd9Sstevel@tonic-gate ctx->state.s64[6] += g; 6027c478bd9Sstevel@tonic-gate ctx->state.s64[7] += h; 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate } 60555553f71Sda73024 #endif /* !__amd64 */ 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate /* 6097c478bd9Sstevel@tonic-gate * Encode() 6107c478bd9Sstevel@tonic-gate * 6117c478bd9Sstevel@tonic-gate * purpose: to convert a list of numbers from little endian to big endian 6127c478bd9Sstevel@tonic-gate * input: uint8_t * : place to store the converted big endian numbers 6137c478bd9Sstevel@tonic-gate * uint32_t * : place to get numbers to convert from 6147c478bd9Sstevel@tonic-gate * size_t : the length of the input in bytes 6157c478bd9Sstevel@tonic-gate * output: void 6167c478bd9Sstevel@tonic-gate */ 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate static void 619734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input, 620734b6a94Sdarrenm size_t len) 6217c478bd9Sstevel@tonic-gate { 6227c478bd9Sstevel@tonic-gate size_t i, j; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate #if defined(__sparc) 6257c478bd9Sstevel@tonic-gate if (IS_P2ALIGNED(output, sizeof (uint32_t))) { 6267c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 4) { 6277c478bd9Sstevel@tonic-gate /* LINTED: pointer alignment */ 6287c478bd9Sstevel@tonic-gate *((uint32_t *)(output + j)) = input[i]; 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate } else { 6317c478bd9Sstevel@tonic-gate #endif /* little endian -- will work on big endian, but slowly */ 6327c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 4) { 6337c478bd9Sstevel@tonic-gate output[j] = (input[i] >> 24) & 0xff; 6347c478bd9Sstevel@tonic-gate output[j + 1] = (input[i] >> 16) & 0xff; 6357c478bd9Sstevel@tonic-gate output[j + 2] = (input[i] >> 8) & 0xff; 6367c478bd9Sstevel@tonic-gate output[j + 3] = input[i] & 0xff; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate #if defined(__sparc) 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate #endif 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate static void 644734b6a94Sdarrenm Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input, 645734b6a94Sdarrenm size_t len) 6467c478bd9Sstevel@tonic-gate { 6477c478bd9Sstevel@tonic-gate size_t i, j; 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate #if defined(__sparc) 6507c478bd9Sstevel@tonic-gate if (IS_P2ALIGNED(output, sizeof (uint64_t))) { 6517c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 8) { 6527c478bd9Sstevel@tonic-gate /* LINTED: pointer alignment */ 6537c478bd9Sstevel@tonic-gate *((uint64_t *)(output + j)) = input[i]; 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate } else { 6567c478bd9Sstevel@tonic-gate #endif /* little endian -- will work on big endian, but slowly */ 6577c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 8) { 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate output[j] = (input[i] >> 56) & 0xff; 6607c478bd9Sstevel@tonic-gate output[j + 1] = (input[i] >> 48) & 0xff; 6617c478bd9Sstevel@tonic-gate output[j + 2] = (input[i] >> 40) & 0xff; 6627c478bd9Sstevel@tonic-gate output[j + 3] = (input[i] >> 32) & 0xff; 6637c478bd9Sstevel@tonic-gate output[j + 4] = (input[i] >> 24) & 0xff; 6647c478bd9Sstevel@tonic-gate output[j + 5] = (input[i] >> 16) & 0xff; 6657c478bd9Sstevel@tonic-gate output[j + 6] = (input[i] >> 8) & 0xff; 6667c478bd9Sstevel@tonic-gate output[j + 7] = input[i] & 0xff; 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate #if defined(__sparc) 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate #endif 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate void 6757c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx) 6767c478bd9Sstevel@tonic-gate { 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate switch (mech) { 6797c478bd9Sstevel@tonic-gate case SHA256_MECH_INFO_TYPE: 6807c478bd9Sstevel@tonic-gate case SHA256_HMAC_MECH_INFO_TYPE: 6817c478bd9Sstevel@tonic-gate case SHA256_HMAC_GEN_MECH_INFO_TYPE: 6827c478bd9Sstevel@tonic-gate ctx->state.s32[0] = 0x6a09e667U; 6837c478bd9Sstevel@tonic-gate ctx->state.s32[1] = 0xbb67ae85U; 6847c478bd9Sstevel@tonic-gate ctx->state.s32[2] = 0x3c6ef372U; 6857c478bd9Sstevel@tonic-gate ctx->state.s32[3] = 0xa54ff53aU; 6867c478bd9Sstevel@tonic-gate ctx->state.s32[4] = 0x510e527fU; 6877c478bd9Sstevel@tonic-gate ctx->state.s32[5] = 0x9b05688cU; 6887c478bd9Sstevel@tonic-gate ctx->state.s32[6] = 0x1f83d9abU; 6897c478bd9Sstevel@tonic-gate ctx->state.s32[7] = 0x5be0cd19U; 6907c478bd9Sstevel@tonic-gate break; 6917c478bd9Sstevel@tonic-gate case SHA384_MECH_INFO_TYPE: 6927c478bd9Sstevel@tonic-gate case SHA384_HMAC_MECH_INFO_TYPE: 6937c478bd9Sstevel@tonic-gate case SHA384_HMAC_GEN_MECH_INFO_TYPE: 6947c478bd9Sstevel@tonic-gate ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL; 6957c478bd9Sstevel@tonic-gate ctx->state.s64[1] = 0x629a292a367cd507ULL; 6967c478bd9Sstevel@tonic-gate ctx->state.s64[2] = 0x9159015a3070dd17ULL; 6977c478bd9Sstevel@tonic-gate ctx->state.s64[3] = 0x152fecd8f70e5939ULL; 6987c478bd9Sstevel@tonic-gate ctx->state.s64[4] = 0x67332667ffc00b31ULL; 6997c478bd9Sstevel@tonic-gate ctx->state.s64[5] = 0x8eb44a8768581511ULL; 7007c478bd9Sstevel@tonic-gate ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL; 7017c478bd9Sstevel@tonic-gate ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL; 7027c478bd9Sstevel@tonic-gate break; 7037c478bd9Sstevel@tonic-gate case SHA512_MECH_INFO_TYPE: 7047c478bd9Sstevel@tonic-gate case SHA512_HMAC_MECH_INFO_TYPE: 7057c478bd9Sstevel@tonic-gate case SHA512_HMAC_GEN_MECH_INFO_TYPE: 7067c478bd9Sstevel@tonic-gate ctx->state.s64[0] = 0x6a09e667f3bcc908ULL; 7077c478bd9Sstevel@tonic-gate ctx->state.s64[1] = 0xbb67ae8584caa73bULL; 7087c478bd9Sstevel@tonic-gate ctx->state.s64[2] = 0x3c6ef372fe94f82bULL; 7097c478bd9Sstevel@tonic-gate ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL; 7107c478bd9Sstevel@tonic-gate ctx->state.s64[4] = 0x510e527fade682d1ULL; 7117c478bd9Sstevel@tonic-gate ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL; 7127c478bd9Sstevel@tonic-gate ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL; 7137c478bd9Sstevel@tonic-gate ctx->state.s64[7] = 0x5be0cd19137e2179ULL; 7147c478bd9Sstevel@tonic-gate break; 7157c478bd9Sstevel@tonic-gate #ifdef _KERNEL 7167c478bd9Sstevel@tonic-gate default: 717*4b56a003SDaniel Anderson cmn_err(CE_PANIC, 718*4b56a003SDaniel Anderson "sha2_init: failed to find a supported algorithm: 0x%x", 7197c478bd9Sstevel@tonic-gate (uint32_t)mech); 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 7227c478bd9Sstevel@tonic-gate } 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate ctx->algotype = mech; 7257c478bd9Sstevel@tonic-gate ctx->count.c64[0] = ctx->count.c64[1] = 0; 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 728734b6a94Sdarrenm #ifndef _KERNEL 729734b6a94Sdarrenm 730734b6a94Sdarrenm #pragma inline(SHA256Init, SHA384Init, SHA512Init) 731734b6a94Sdarrenm void 732734b6a94Sdarrenm SHA256Init(SHA256_CTX *ctx) 733734b6a94Sdarrenm { 734734b6a94Sdarrenm SHA2Init(SHA256, ctx); 735734b6a94Sdarrenm } 736734b6a94Sdarrenm 737734b6a94Sdarrenm void 738734b6a94Sdarrenm SHA384Init(SHA384_CTX *ctx) 739734b6a94Sdarrenm { 740734b6a94Sdarrenm SHA2Init(SHA384, ctx); 741734b6a94Sdarrenm } 742734b6a94Sdarrenm 743734b6a94Sdarrenm void 744734b6a94Sdarrenm SHA512Init(SHA512_CTX *ctx) 745734b6a94Sdarrenm { 746734b6a94Sdarrenm SHA2Init(SHA512, ctx); 747734b6a94Sdarrenm } 748734b6a94Sdarrenm 749734b6a94Sdarrenm #endif /* _KERNEL */ 750734b6a94Sdarrenm 7517c478bd9Sstevel@tonic-gate /* 7527c478bd9Sstevel@tonic-gate * SHA2Update() 7537c478bd9Sstevel@tonic-gate * 7547c478bd9Sstevel@tonic-gate * purpose: continues an sha2 digest operation, using the message block 7557c478bd9Sstevel@tonic-gate * to update the context. 7567c478bd9Sstevel@tonic-gate * input: SHA2_CTX * : the context to update 757734b6a94Sdarrenm * void * : the message block 75855553f71Sda73024 * size_t : the length of the message block, in bytes 7597c478bd9Sstevel@tonic-gate * output: void 7607c478bd9Sstevel@tonic-gate */ 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate void 763734b6a94Sdarrenm SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len) 7647c478bd9Sstevel@tonic-gate { 7657c478bd9Sstevel@tonic-gate uint32_t i, buf_index, buf_len, buf_limit; 766734b6a94Sdarrenm const uint8_t *input = inptr; 76755553f71Sda73024 uint32_t algotype = ctx->algotype; 76855553f71Sda73024 #if defined(__amd64) 76955553f71Sda73024 uint32_t block_count; 77055553f71Sda73024 #endif /* !__amd64 */ 77155553f71Sda73024 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate /* check for noop */ 7747c478bd9Sstevel@tonic-gate if (input_len == 0) 7757c478bd9Sstevel@tonic-gate return; 7767c478bd9Sstevel@tonic-gate 77755553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) { 7787c478bd9Sstevel@tonic-gate buf_limit = 64; 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate /* compute number of bytes mod 64 */ 7817c478bd9Sstevel@tonic-gate buf_index = (ctx->count.c32[1] >> 3) & 0x3F; 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate /* update number of bits */ 7847c478bd9Sstevel@tonic-gate if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3)) 7857c478bd9Sstevel@tonic-gate ctx->count.c32[0]++; 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate ctx->count.c32[0] += (input_len >> 29); 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate } else { 7907c478bd9Sstevel@tonic-gate buf_limit = 128; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate /* compute number of bytes mod 128 */ 7937c478bd9Sstevel@tonic-gate buf_index = (ctx->count.c64[1] >> 3) & 0x7F; 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate /* update number of bits */ 7967c478bd9Sstevel@tonic-gate if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3)) 7977c478bd9Sstevel@tonic-gate ctx->count.c64[0]++; 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate ctx->count.c64[0] += (input_len >> 29); 8007c478bd9Sstevel@tonic-gate } 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate buf_len = buf_limit - buf_index; 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate /* transform as many times as possible */ 8057c478bd9Sstevel@tonic-gate i = 0; 8067c478bd9Sstevel@tonic-gate if (input_len >= buf_len) { 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate /* 8097c478bd9Sstevel@tonic-gate * general optimization: 8107c478bd9Sstevel@tonic-gate * 8117c478bd9Sstevel@tonic-gate * only do initial bcopy() and SHA2Transform() if 8127c478bd9Sstevel@tonic-gate * buf_index != 0. if buf_index == 0, we're just 8137c478bd9Sstevel@tonic-gate * wasting our time doing the bcopy() since there 8147c478bd9Sstevel@tonic-gate * wasn't any data left over from a previous call to 8157c478bd9Sstevel@tonic-gate * SHA2Update(). 8167c478bd9Sstevel@tonic-gate */ 8177c478bd9Sstevel@tonic-gate if (buf_index) { 8187c478bd9Sstevel@tonic-gate bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len); 81955553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) 8207c478bd9Sstevel@tonic-gate SHA256Transform(ctx, ctx->buf_un.buf8); 8217c478bd9Sstevel@tonic-gate else 8227c478bd9Sstevel@tonic-gate SHA512Transform(ctx, ctx->buf_un.buf8); 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate i = buf_len; 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate 82755553f71Sda73024 #if !defined(__amd64) 82855553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) { 8297c478bd9Sstevel@tonic-gate for (; i + buf_limit - 1 < input_len; i += buf_limit) { 8307c478bd9Sstevel@tonic-gate SHA256Transform(ctx, &input[i]); 83155553f71Sda73024 } 83255553f71Sda73024 } else { 83355553f71Sda73024 for (; i + buf_limit - 1 < input_len; i += buf_limit) { 8347c478bd9Sstevel@tonic-gate SHA512Transform(ctx, &input[i]); 8357c478bd9Sstevel@tonic-gate } 83655553f71Sda73024 } 83755553f71Sda73024 83855553f71Sda73024 #else 83955553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) { 84055553f71Sda73024 block_count = (input_len - i) >> 6; 84155553f71Sda73024 if (block_count > 0) { 84255553f71Sda73024 SHA256TransformBlocks(ctx, &input[i], 84355553f71Sda73024 block_count); 84455553f71Sda73024 i += block_count << 6; 84555553f71Sda73024 } 84655553f71Sda73024 } else { 84755553f71Sda73024 block_count = (input_len - i) >> 7; 84855553f71Sda73024 if (block_count > 0) { 84955553f71Sda73024 SHA512TransformBlocks(ctx, &input[i], 85055553f71Sda73024 block_count); 85155553f71Sda73024 i += block_count << 7; 85255553f71Sda73024 } 85355553f71Sda73024 } 85455553f71Sda73024 #endif /* !__amd64 */ 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* 8577c478bd9Sstevel@tonic-gate * general optimization: 8587c478bd9Sstevel@tonic-gate * 8597c478bd9Sstevel@tonic-gate * if i and input_len are the same, return now instead 8607c478bd9Sstevel@tonic-gate * of calling bcopy(), since the bcopy() in this case 86155553f71Sda73024 * will be an expensive noop. 8627c478bd9Sstevel@tonic-gate */ 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate if (input_len == i) 8657c478bd9Sstevel@tonic-gate return; 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate buf_index = 0; 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* buffer remaining input */ 8717c478bd9Sstevel@tonic-gate bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * SHA2Final() 8777c478bd9Sstevel@tonic-gate * 8787c478bd9Sstevel@tonic-gate * purpose: ends an sha2 digest operation, finalizing the message digest and 8797c478bd9Sstevel@tonic-gate * zeroing the context. 88055553f71Sda73024 * input: uchar_t * : a buffer to store the digest 8815151fb12Sdarrenm * : The function actually uses void* because many 8825151fb12Sdarrenm * : callers pass things other than uchar_t here. 8837c478bd9Sstevel@tonic-gate * SHA2_CTX * : the context to finalize, save, and zero 8847c478bd9Sstevel@tonic-gate * output: void 8857c478bd9Sstevel@tonic-gate */ 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate void 888734b6a94Sdarrenm SHA2Final(void *digest, SHA2_CTX *ctx) 8897c478bd9Sstevel@tonic-gate { 8907c478bd9Sstevel@tonic-gate uint8_t bitcount_be[sizeof (ctx->count.c32)]; 8917c478bd9Sstevel@tonic-gate uint8_t bitcount_be64[sizeof (ctx->count.c64)]; 8927c478bd9Sstevel@tonic-gate uint32_t index; 89355553f71Sda73024 uint32_t algotype = ctx->algotype; 8947c478bd9Sstevel@tonic-gate 89555553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) { 8967c478bd9Sstevel@tonic-gate index = (ctx->count.c32[1] >> 3) & 0x3f; 8977c478bd9Sstevel@tonic-gate Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be)); 8987c478bd9Sstevel@tonic-gate SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index); 8997c478bd9Sstevel@tonic-gate SHA2Update(ctx, bitcount_be, sizeof (bitcount_be)); 9007c478bd9Sstevel@tonic-gate Encode(digest, ctx->state.s32, sizeof (ctx->state.s32)); 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate } else { 9037c478bd9Sstevel@tonic-gate index = (ctx->count.c64[1] >> 3) & 0x7f; 9047c478bd9Sstevel@tonic-gate Encode64(bitcount_be64, ctx->count.c64, 9057c478bd9Sstevel@tonic-gate sizeof (bitcount_be64)); 9067c478bd9Sstevel@tonic-gate SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index); 9077c478bd9Sstevel@tonic-gate SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64)); 90855553f71Sda73024 if (algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) { 9097c478bd9Sstevel@tonic-gate ctx->state.s64[6] = ctx->state.s64[7] = 0; 9107c478bd9Sstevel@tonic-gate Encode64(digest, ctx->state.s64, 9117c478bd9Sstevel@tonic-gate sizeof (uint64_t) * 6); 9127c478bd9Sstevel@tonic-gate } else 9137c478bd9Sstevel@tonic-gate Encode64(digest, ctx->state.s64, 9147c478bd9Sstevel@tonic-gate sizeof (ctx->state.s64)); 9157c478bd9Sstevel@tonic-gate } 916673007c6Sdarrenm 917673007c6Sdarrenm /* zeroize sensitive information */ 918673007c6Sdarrenm bzero(ctx, sizeof (*ctx)); 9197c478bd9Sstevel@tonic-gate } 920