17c478bd9Sstevel@tonic-gate /*
2*8de5c4f4SDan OpenSolaris Anderson * Copyright 2009 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
304b56a003SDaniel Anderson * standard, available at
314b56a003SDaniel 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
36*8de5c4f4SDan OpenSolaris Anderson #ifndef _KERNEL
37*8de5c4f4SDan OpenSolaris Anderson #include <stdint.h>
38*8de5c4f4SDan OpenSolaris Anderson #include <strings.h>
39*8de5c4f4SDan OpenSolaris Anderson #include <stdlib.h>
40*8de5c4f4SDan OpenSolaris Anderson #include <errno.h>
41*8de5c4f4SDan OpenSolaris Anderson #endif /* _KERNEL */
42*8de5c4f4SDan OpenSolaris Anderson
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
47734b6a94Sdarrenm #define _SHA2_IMPL
487c478bd9Sstevel@tonic-gate #include <sys/sha2.h>
497c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h>
507c478bd9Sstevel@tonic-gate
5155553f71Sda73024 #ifdef _KERNEL
5255553f71Sda73024 #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate
5455553f71Sda73024 #else
55734b6a94Sdarrenm #pragma weak SHA256Update = SHA2Update
56734b6a94Sdarrenm #pragma weak SHA384Update = SHA2Update
57734b6a94Sdarrenm #pragma weak SHA512Update = SHA2Update
58734b6a94Sdarrenm
59734b6a94Sdarrenm #pragma weak SHA256Final = SHA2Final
60734b6a94Sdarrenm #pragma weak SHA384Final = SHA2Final
61734b6a94Sdarrenm #pragma weak SHA512Final = SHA2Final
62734b6a94Sdarrenm
63734b6a94Sdarrenm #endif /* _KERNEL */
64734b6a94Sdarrenm
654b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN
664b56a003SDaniel Anderson #include <sys/byteorder.h>
674b56a003SDaniel Anderson #define HAVE_HTONL
684b56a003SDaniel Anderson #endif
694b56a003SDaniel Anderson
707c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
717c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t);
7255553f71Sda73024
7355553f71Sda73024 #if defined(__amd64)
7455553f71Sda73024 #define SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1)
7555553f71Sda73024 #define SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1)
7655553f71Sda73024
7755553f71Sda73024 void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
7855553f71Sda73024 void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
7955553f71Sda73024
8055553f71Sda73024 #else
817c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *);
827c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *);
8355553f71Sda73024 #endif /* __amd64 */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */
887c478bd9Sstevel@tonic-gate #define Ch(b, c, d) (((b) & (c)) ^ ((~b) & (d)))
897c478bd9Sstevel@tonic-gate #define Maj(b, c, d) (((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */
927c478bd9Sstevel@tonic-gate #define ROTR(x, n) \
937c478bd9Sstevel@tonic-gate (((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /* Shift x right n bits */
967c478bd9Sstevel@tonic-gate #define SHR(x, n) ((x) >> (n))
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* SHA256 Functions */
997c478bd9Sstevel@tonic-gate #define BIGSIGMA0_256(x) (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
1007c478bd9Sstevel@tonic-gate #define BIGSIGMA1_256(x) (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
1017c478bd9Sstevel@tonic-gate #define SIGMA0_256(x) (ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
1027c478bd9Sstevel@tonic-gate #define SIGMA1_256(x) (ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate #define SHA256ROUND(a, b, c, d, e, f, g, h, i, w) \
1057c478bd9Sstevel@tonic-gate T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w; \
1067c478bd9Sstevel@tonic-gate d += T1; \
1077c478bd9Sstevel@tonic-gate T2 = BIGSIGMA0_256(a) + Maj(a, b, c); \
1087c478bd9Sstevel@tonic-gate h = T1 + T2
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */
1117c478bd9Sstevel@tonic-gate #define BIGSIGMA0(x) (ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
1127c478bd9Sstevel@tonic-gate #define BIGSIGMA1(x) (ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
1137c478bd9Sstevel@tonic-gate #define SIGMA0(x) (ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
1147c478bd9Sstevel@tonic-gate #define SIGMA1(x) (ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
1157c478bd9Sstevel@tonic-gate #define SHA512ROUND(a, b, c, d, e, f, g, h, i, w) \
1167c478bd9Sstevel@tonic-gate T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w; \
1177c478bd9Sstevel@tonic-gate d += T1; \
1187c478bd9Sstevel@tonic-gate T2 = BIGSIGMA0(a) + Maj(a, b, c); \
1197c478bd9Sstevel@tonic-gate h = T1 + T2
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * sparc optimization:
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * on the sparc, we can load big endian 32-bit data easily. note that
1257c478bd9Sstevel@tonic-gate * special care must be taken to ensure the address is 32-bit aligned.
1267c478bd9Sstevel@tonic-gate * in the interest of speed, we don't check to make sure, since
1277c478bd9Sstevel@tonic-gate * careful programming can guarantee this for us.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN)
1317c478bd9Sstevel@tonic-gate #define LOAD_BIG_32(addr) (*(uint32_t *)(addr))
1327c478bd9Sstevel@tonic-gate #define LOAD_BIG_64(addr) (*(uint64_t *)(addr))
1337c478bd9Sstevel@tonic-gate
1344b56a003SDaniel Anderson #elif defined(HAVE_HTONL)
1354b56a003SDaniel Anderson #define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
1364b56a003SDaniel Anderson #define LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr)))
1377c478bd9Sstevel@tonic-gate
1384b56a003SDaniel Anderson #else
1394b56a003SDaniel Anderson /* little endian -- will work on big endian, but slowly */
1404b56a003SDaniel Anderson #define LOAD_BIG_32(addr) \
1414b56a003SDaniel Anderson (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
1427c478bd9Sstevel@tonic-gate #define LOAD_BIG_64(addr) \
1437c478bd9Sstevel@tonic-gate (((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) | \
1447c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) | \
1457c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) | \
1467c478bd9Sstevel@tonic-gate ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
1474b56a003SDaniel Anderson #endif /* _BIG_ENDIAN */
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate
15055553f71Sda73024 #if !defined(__amd64)
1517c478bd9Sstevel@tonic-gate /* SHA256 Transform */
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate static void
SHA256Transform(SHA2_CTX * ctx,const uint8_t * blk)1547c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate uint32_t a = ctx->state.s32[0];
1577c478bd9Sstevel@tonic-gate uint32_t b = ctx->state.s32[1];
1587c478bd9Sstevel@tonic-gate uint32_t c = ctx->state.s32[2];
1597c478bd9Sstevel@tonic-gate uint32_t d = ctx->state.s32[3];
1607c478bd9Sstevel@tonic-gate uint32_t e = ctx->state.s32[4];
1617c478bd9Sstevel@tonic-gate uint32_t f = ctx->state.s32[5];
1627c478bd9Sstevel@tonic-gate uint32_t g = ctx->state.s32[6];
1637c478bd9Sstevel@tonic-gate uint32_t h = ctx->state.s32[7];
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
1667c478bd9Sstevel@tonic-gate uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
1677c478bd9Sstevel@tonic-gate uint32_t T1, T2;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate #if defined(__sparc)
1707c478bd9Sstevel@tonic-gate static const uint32_t sha256_consts[] = {
1717c478bd9Sstevel@tonic-gate SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
1727c478bd9Sstevel@tonic-gate SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
1737c478bd9Sstevel@tonic-gate SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
1747c478bd9Sstevel@tonic-gate SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
1757c478bd9Sstevel@tonic-gate SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
1767c478bd9Sstevel@tonic-gate SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
1777c478bd9Sstevel@tonic-gate SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
1787c478bd9Sstevel@tonic-gate SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
1797c478bd9Sstevel@tonic-gate SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
1807c478bd9Sstevel@tonic-gate SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
1817c478bd9Sstevel@tonic-gate SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
1827c478bd9Sstevel@tonic-gate SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
1837c478bd9Sstevel@tonic-gate SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
1847c478bd9Sstevel@tonic-gate SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
1857c478bd9Sstevel@tonic-gate SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
1867c478bd9Sstevel@tonic-gate SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
1877c478bd9Sstevel@tonic-gate SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
1887c478bd9Sstevel@tonic-gate SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
1897c478bd9Sstevel@tonic-gate SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
1907c478bd9Sstevel@tonic-gate SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
1917c478bd9Sstevel@tonic-gate SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
1927c478bd9Sstevel@tonic-gate SHA256_CONST_63
1937c478bd9Sstevel@tonic-gate };
19455553f71Sda73024 #endif /* __sparc */
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate if ((uintptr_t)blk & 0x3) { /* not 4-byte aligned? */
1977c478bd9Sstevel@tonic-gate bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
1987c478bd9Sstevel@tonic-gate blk = (uint8_t *)ctx->buf_un.buf32;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
201734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
202f66d273dSizick w0 = LOAD_BIG_32(blk + 4 * 0);
203f66d273dSizick SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
204734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
205f66d273dSizick w1 = LOAD_BIG_32(blk + 4 * 1);
206f66d273dSizick SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
207734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
208f66d273dSizick w2 = LOAD_BIG_32(blk + 4 * 2);
209f66d273dSizick SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
210734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
211f66d273dSizick w3 = LOAD_BIG_32(blk + 4 * 3);
212f66d273dSizick SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
213734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
214f66d273dSizick w4 = LOAD_BIG_32(blk + 4 * 4);
215f66d273dSizick SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
216734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
217f66d273dSizick w5 = LOAD_BIG_32(blk + 4 * 5);
218f66d273dSizick SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
219734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
220f66d273dSizick w6 = LOAD_BIG_32(blk + 4 * 6);
221f66d273dSizick SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
222734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
223f66d273dSizick w7 = LOAD_BIG_32(blk + 4 * 7);
224f66d273dSizick SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
225734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
226f66d273dSizick w8 = LOAD_BIG_32(blk + 4 * 8);
227f66d273dSizick SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
228734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
229f66d273dSizick w9 = LOAD_BIG_32(blk + 4 * 9);
230f66d273dSizick SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
231734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
232f66d273dSizick w10 = LOAD_BIG_32(blk + 4 * 10);
233f66d273dSizick SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
234734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
235f66d273dSizick w11 = LOAD_BIG_32(blk + 4 * 11);
236f66d273dSizick SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
237734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
238f66d273dSizick w12 = LOAD_BIG_32(blk + 4 * 12);
239f66d273dSizick SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
240734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
241f66d273dSizick w13 = LOAD_BIG_32(blk + 4 * 13);
242f66d273dSizick SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
243734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
244f66d273dSizick w14 = LOAD_BIG_32(blk + 4 * 14);
245f66d273dSizick SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
246734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
247f66d273dSizick w15 = LOAD_BIG_32(blk + 4 * 15);
248f66d273dSizick SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
249f66d273dSizick
2507c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2517c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
2527c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2537c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
2547c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2557c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
2567c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2577c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
2587c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2597c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
2607c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2617c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
2627c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2637c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
2647c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2657c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
2667c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2677c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
2687c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
2697c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
2707c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
2717c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
2727c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
2737c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
2747c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
2757c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
2767c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
2777c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
2787c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
2797c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
2807c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
2817c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2847c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
2857c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2867c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
2877c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2887c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
2897c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2907c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
2917c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2927c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
2937c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2947c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
2957c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2967c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
2977c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2987c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
2997c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3007c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
3017c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3027c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
3037c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3047c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
3057c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3067c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
3077c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3087c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
3097c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3107c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
3117c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3127c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
3137c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3147c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
3177c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
3187c478bd9Sstevel@tonic-gate w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
3197c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
3207c478bd9Sstevel@tonic-gate w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
3217c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
3227c478bd9Sstevel@tonic-gate w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
3237c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
3247c478bd9Sstevel@tonic-gate w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
3257c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
3267c478bd9Sstevel@tonic-gate w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
3277c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
3287c478bd9Sstevel@tonic-gate w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
3297c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
3307c478bd9Sstevel@tonic-gate w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
3317c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
3327c478bd9Sstevel@tonic-gate w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3337c478bd9Sstevel@tonic-gate SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
3347c478bd9Sstevel@tonic-gate w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3357c478bd9Sstevel@tonic-gate SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
3367c478bd9Sstevel@tonic-gate w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3377c478bd9Sstevel@tonic-gate SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
3387c478bd9Sstevel@tonic-gate w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3397c478bd9Sstevel@tonic-gate SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
3407c478bd9Sstevel@tonic-gate w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3417c478bd9Sstevel@tonic-gate SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
3427c478bd9Sstevel@tonic-gate w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3437c478bd9Sstevel@tonic-gate SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
3447c478bd9Sstevel@tonic-gate w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3457c478bd9Sstevel@tonic-gate SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
3467c478bd9Sstevel@tonic-gate w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3477c478bd9Sstevel@tonic-gate SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate ctx->state.s32[0] += a;
3507c478bd9Sstevel@tonic-gate ctx->state.s32[1] += b;
3517c478bd9Sstevel@tonic-gate ctx->state.s32[2] += c;
3527c478bd9Sstevel@tonic-gate ctx->state.s32[3] += d;
3537c478bd9Sstevel@tonic-gate ctx->state.s32[4] += e;
3547c478bd9Sstevel@tonic-gate ctx->state.s32[5] += f;
3557c478bd9Sstevel@tonic-gate ctx->state.s32[6] += g;
3567c478bd9Sstevel@tonic-gate ctx->state.s32[7] += h;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate static void
SHA512Transform(SHA2_CTX * ctx,const uint8_t * blk)3637c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate uint64_t a = ctx->state.s64[0];
3677c478bd9Sstevel@tonic-gate uint64_t b = ctx->state.s64[1];
3687c478bd9Sstevel@tonic-gate uint64_t c = ctx->state.s64[2];
3697c478bd9Sstevel@tonic-gate uint64_t d = ctx->state.s64[3];
3707c478bd9Sstevel@tonic-gate uint64_t e = ctx->state.s64[4];
3717c478bd9Sstevel@tonic-gate uint64_t f = ctx->state.s64[5];
3727c478bd9Sstevel@tonic-gate uint64_t g = ctx->state.s64[6];
3737c478bd9Sstevel@tonic-gate uint64_t h = ctx->state.s64[7];
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
3767c478bd9Sstevel@tonic-gate uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
3777c478bd9Sstevel@tonic-gate uint64_t T1, T2;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate #if defined(__sparc)
3807c478bd9Sstevel@tonic-gate static const uint64_t sha512_consts[] = {
3817c478bd9Sstevel@tonic-gate SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
3827c478bd9Sstevel@tonic-gate SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
3837c478bd9Sstevel@tonic-gate SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
3847c478bd9Sstevel@tonic-gate SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
3857c478bd9Sstevel@tonic-gate SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
3867c478bd9Sstevel@tonic-gate SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
3877c478bd9Sstevel@tonic-gate SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
3887c478bd9Sstevel@tonic-gate SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
3897c478bd9Sstevel@tonic-gate SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
3907c478bd9Sstevel@tonic-gate SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
3917c478bd9Sstevel@tonic-gate SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
3927c478bd9Sstevel@tonic-gate SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
3937c478bd9Sstevel@tonic-gate SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
3947c478bd9Sstevel@tonic-gate SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
3957c478bd9Sstevel@tonic-gate SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
3967c478bd9Sstevel@tonic-gate SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
3977c478bd9Sstevel@tonic-gate SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
3987c478bd9Sstevel@tonic-gate SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
3997c478bd9Sstevel@tonic-gate SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
4007c478bd9Sstevel@tonic-gate SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
4017c478bd9Sstevel@tonic-gate SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
4027c478bd9Sstevel@tonic-gate SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
4037c478bd9Sstevel@tonic-gate SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
4047c478bd9Sstevel@tonic-gate SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
4057c478bd9Sstevel@tonic-gate SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
4067c478bd9Sstevel@tonic-gate SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
4077c478bd9Sstevel@tonic-gate SHA512_CONST_78, SHA512_CONST_79
4087c478bd9Sstevel@tonic-gate };
40955553f71Sda73024 #endif /* __sparc */
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate if ((uintptr_t)blk & 0x7) { /* not 8-byte aligned? */
4137c478bd9Sstevel@tonic-gate bcopy(blk, ctx->buf_un.buf64, sizeof (ctx->buf_un.buf64));
4147c478bd9Sstevel@tonic-gate blk = (uint8_t *)ctx->buf_un.buf64;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
417734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
418f66d273dSizick w0 = LOAD_BIG_64(blk + 8 * 0);
419f66d273dSizick SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
420734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
421f66d273dSizick w1 = LOAD_BIG_64(blk + 8 * 1);
422f66d273dSizick SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
423734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
424f66d273dSizick w2 = LOAD_BIG_64(blk + 8 * 2);
425f66d273dSizick SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
426734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
427f66d273dSizick w3 = LOAD_BIG_64(blk + 8 * 3);
428f66d273dSizick SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
429734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
430f66d273dSizick w4 = LOAD_BIG_64(blk + 8 * 4);
431f66d273dSizick SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
432734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
433f66d273dSizick w5 = LOAD_BIG_64(blk + 8 * 5);
434f66d273dSizick SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
435734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
436f66d273dSizick w6 = LOAD_BIG_64(blk + 8 * 6);
437f66d273dSizick SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
438734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
439f66d273dSizick w7 = LOAD_BIG_64(blk + 8 * 7);
440f66d273dSizick SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
441734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
442f66d273dSizick w8 = LOAD_BIG_64(blk + 8 * 8);
443f66d273dSizick SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
444734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
445f66d273dSizick w9 = LOAD_BIG_64(blk + 8 * 9);
446f66d273dSizick SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
447734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
448f66d273dSizick w10 = LOAD_BIG_64(blk + 8 * 10);
449f66d273dSizick SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
450734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
451f66d273dSizick w11 = LOAD_BIG_64(blk + 8 * 11);
452f66d273dSizick SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
453734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
454f66d273dSizick w12 = LOAD_BIG_64(blk + 8 * 12);
455f66d273dSizick SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
456734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
457f66d273dSizick w13 = LOAD_BIG_64(blk + 8 * 13);
458f66d273dSizick SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
459734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
460f66d273dSizick w14 = LOAD_BIG_64(blk + 8 * 14);
461f66d273dSizick SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
462734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
463f66d273dSizick w15 = LOAD_BIG_64(blk + 8 * 15);
464f66d273dSizick SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
465f66d273dSizick
4667c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4677c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
4687c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
4697c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
4707c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
4717c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
4727c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
4737c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
4747c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
4757c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
4767c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
4777c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
4787c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
4797c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
4807c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
4817c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
4827c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
4837c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
4847c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
4857c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
4867c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
4877c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
4887c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
4897c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
4907c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
4917c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
4927c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
4937c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
4947c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
4957c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
4967c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
4977c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5007c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
5017c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5027c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
5037c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5047c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
5057c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5067c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
5077c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5087c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
5097c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5107c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
5117c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5127c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
5137c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5147c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
5157c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5167c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
5177c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5187c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
5197c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5207c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
5217c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5227c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
5237c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5247c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
5257c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5267c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
5277c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5287c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
5297c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5307c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5337c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
5347c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5357c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
5367c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5377c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
5387c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5397c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
5407c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5417c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
5427c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5437c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
5447c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5457c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
5467c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5477c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
5487c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5497c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
5507c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5517c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
5527c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5537c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
5547c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5557c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
5567c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5577c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
5587c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5597c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
5607c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5617c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
5627c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5637c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5667c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
5677c478bd9Sstevel@tonic-gate w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5687c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
5697c478bd9Sstevel@tonic-gate w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5707c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
5717c478bd9Sstevel@tonic-gate w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5727c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
5737c478bd9Sstevel@tonic-gate w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5747c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
5757c478bd9Sstevel@tonic-gate w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5767c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
5777c478bd9Sstevel@tonic-gate w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5787c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
5797c478bd9Sstevel@tonic-gate w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5807c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
5817c478bd9Sstevel@tonic-gate w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5827c478bd9Sstevel@tonic-gate SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
5837c478bd9Sstevel@tonic-gate w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5847c478bd9Sstevel@tonic-gate SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
5857c478bd9Sstevel@tonic-gate w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5867c478bd9Sstevel@tonic-gate SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
5877c478bd9Sstevel@tonic-gate w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5887c478bd9Sstevel@tonic-gate SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
5897c478bd9Sstevel@tonic-gate w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5907c478bd9Sstevel@tonic-gate SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
5917c478bd9Sstevel@tonic-gate w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5927c478bd9Sstevel@tonic-gate SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
5937c478bd9Sstevel@tonic-gate w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5947c478bd9Sstevel@tonic-gate SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
5957c478bd9Sstevel@tonic-gate w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5967c478bd9Sstevel@tonic-gate SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate ctx->state.s64[0] += a;
5997c478bd9Sstevel@tonic-gate ctx->state.s64[1] += b;
6007c478bd9Sstevel@tonic-gate ctx->state.s64[2] += c;
6017c478bd9Sstevel@tonic-gate ctx->state.s64[3] += d;
6027c478bd9Sstevel@tonic-gate ctx->state.s64[4] += e;
6037c478bd9Sstevel@tonic-gate ctx->state.s64[5] += f;
6047c478bd9Sstevel@tonic-gate ctx->state.s64[6] += g;
6057c478bd9Sstevel@tonic-gate ctx->state.s64[7] += h;
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate }
60855553f71Sda73024 #endif /* !__amd64 */
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate * Encode()
6137c478bd9Sstevel@tonic-gate *
6147c478bd9Sstevel@tonic-gate * purpose: to convert a list of numbers from little endian to big endian
6157c478bd9Sstevel@tonic-gate * input: uint8_t * : place to store the converted big endian numbers
6167c478bd9Sstevel@tonic-gate * uint32_t * : place to get numbers to convert from
6177c478bd9Sstevel@tonic-gate * size_t : the length of the input in bytes
6187c478bd9Sstevel@tonic-gate * output: void
6197c478bd9Sstevel@tonic-gate */
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate static void
Encode(uint8_t * _RESTRICT_KYWD output,uint32_t * _RESTRICT_KYWD input,size_t len)622734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input,
623734b6a94Sdarrenm size_t len)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate size_t i, j;
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate #if defined(__sparc)
6287c478bd9Sstevel@tonic-gate if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
6297c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 4) {
630*8de5c4f4SDan OpenSolaris Anderson /* LINTED E_BAD_PTR_CAST_ALIGN */
6317c478bd9Sstevel@tonic-gate *((uint32_t *)(output + j)) = input[i];
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate } else {
6347c478bd9Sstevel@tonic-gate #endif /* little endian -- will work on big endian, but slowly */
6357c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 4) {
6367c478bd9Sstevel@tonic-gate output[j] = (input[i] >> 24) & 0xff;
6377c478bd9Sstevel@tonic-gate output[j + 1] = (input[i] >> 16) & 0xff;
6387c478bd9Sstevel@tonic-gate output[j + 2] = (input[i] >> 8) & 0xff;
6397c478bd9Sstevel@tonic-gate output[j + 3] = input[i] & 0xff;
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate #if defined(__sparc)
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate #endif
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate static void
Encode64(uint8_t * _RESTRICT_KYWD output,uint64_t * _RESTRICT_KYWD input,size_t len)647734b6a94Sdarrenm Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input,
648734b6a94Sdarrenm size_t len)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate size_t i, j;
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate #if defined(__sparc)
6537c478bd9Sstevel@tonic-gate if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
6547c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 8) {
655*8de5c4f4SDan OpenSolaris Anderson /* LINTED E_BAD_PTR_CAST_ALIGN */
6567c478bd9Sstevel@tonic-gate *((uint64_t *)(output + j)) = input[i];
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate } else {
6597c478bd9Sstevel@tonic-gate #endif /* little endian -- will work on big endian, but slowly */
6607c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 8) {
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate output[j] = (input[i] >> 56) & 0xff;
6637c478bd9Sstevel@tonic-gate output[j + 1] = (input[i] >> 48) & 0xff;
6647c478bd9Sstevel@tonic-gate output[j + 2] = (input[i] >> 40) & 0xff;
6657c478bd9Sstevel@tonic-gate output[j + 3] = (input[i] >> 32) & 0xff;
6667c478bd9Sstevel@tonic-gate output[j + 4] = (input[i] >> 24) & 0xff;
6677c478bd9Sstevel@tonic-gate output[j + 5] = (input[i] >> 16) & 0xff;
6687c478bd9Sstevel@tonic-gate output[j + 6] = (input[i] >> 8) & 0xff;
6697c478bd9Sstevel@tonic-gate output[j + 7] = input[i] & 0xff;
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate #if defined(__sparc)
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate #endif
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate void
SHA2Init(uint64_t mech,SHA2_CTX * ctx)6787c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate switch (mech) {
6827c478bd9Sstevel@tonic-gate case SHA256_MECH_INFO_TYPE:
6837c478bd9Sstevel@tonic-gate case SHA256_HMAC_MECH_INFO_TYPE:
6847c478bd9Sstevel@tonic-gate case SHA256_HMAC_GEN_MECH_INFO_TYPE:
6857c478bd9Sstevel@tonic-gate ctx->state.s32[0] = 0x6a09e667U;
6867c478bd9Sstevel@tonic-gate ctx->state.s32[1] = 0xbb67ae85U;
6877c478bd9Sstevel@tonic-gate ctx->state.s32[2] = 0x3c6ef372U;
6887c478bd9Sstevel@tonic-gate ctx->state.s32[3] = 0xa54ff53aU;
6897c478bd9Sstevel@tonic-gate ctx->state.s32[4] = 0x510e527fU;
6907c478bd9Sstevel@tonic-gate ctx->state.s32[5] = 0x9b05688cU;
6917c478bd9Sstevel@tonic-gate ctx->state.s32[6] = 0x1f83d9abU;
6927c478bd9Sstevel@tonic-gate ctx->state.s32[7] = 0x5be0cd19U;
6937c478bd9Sstevel@tonic-gate break;
6947c478bd9Sstevel@tonic-gate case SHA384_MECH_INFO_TYPE:
6957c478bd9Sstevel@tonic-gate case SHA384_HMAC_MECH_INFO_TYPE:
6967c478bd9Sstevel@tonic-gate case SHA384_HMAC_GEN_MECH_INFO_TYPE:
6977c478bd9Sstevel@tonic-gate ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
6987c478bd9Sstevel@tonic-gate ctx->state.s64[1] = 0x629a292a367cd507ULL;
6997c478bd9Sstevel@tonic-gate ctx->state.s64[2] = 0x9159015a3070dd17ULL;
7007c478bd9Sstevel@tonic-gate ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
7017c478bd9Sstevel@tonic-gate ctx->state.s64[4] = 0x67332667ffc00b31ULL;
7027c478bd9Sstevel@tonic-gate ctx->state.s64[5] = 0x8eb44a8768581511ULL;
7037c478bd9Sstevel@tonic-gate ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
7047c478bd9Sstevel@tonic-gate ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
7057c478bd9Sstevel@tonic-gate break;
7067c478bd9Sstevel@tonic-gate case SHA512_MECH_INFO_TYPE:
7077c478bd9Sstevel@tonic-gate case SHA512_HMAC_MECH_INFO_TYPE:
7087c478bd9Sstevel@tonic-gate case SHA512_HMAC_GEN_MECH_INFO_TYPE:
7097c478bd9Sstevel@tonic-gate ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
7107c478bd9Sstevel@tonic-gate ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
7117c478bd9Sstevel@tonic-gate ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
7127c478bd9Sstevel@tonic-gate ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
7137c478bd9Sstevel@tonic-gate ctx->state.s64[4] = 0x510e527fade682d1ULL;
7147c478bd9Sstevel@tonic-gate ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
7157c478bd9Sstevel@tonic-gate ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
7167c478bd9Sstevel@tonic-gate ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
7177c478bd9Sstevel@tonic-gate break;
7187c478bd9Sstevel@tonic-gate #ifdef _KERNEL
7197c478bd9Sstevel@tonic-gate default:
7204b56a003SDaniel Anderson cmn_err(CE_PANIC,
7214b56a003SDaniel Anderson "sha2_init: failed to find a supported algorithm: 0x%x",
7227c478bd9Sstevel@tonic-gate (uint32_t)mech);
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate
727*8de5c4f4SDan OpenSolaris Anderson ctx->algotype = (uint32_t)mech;
7287c478bd9Sstevel@tonic-gate ctx->count.c64[0] = ctx->count.c64[1] = 0;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate
731734b6a94Sdarrenm #ifndef _KERNEL
732734b6a94Sdarrenm
733734b6a94Sdarrenm #pragma inline(SHA256Init, SHA384Init, SHA512Init)
734734b6a94Sdarrenm void
SHA256Init(SHA256_CTX * ctx)735734b6a94Sdarrenm SHA256Init(SHA256_CTX *ctx)
736734b6a94Sdarrenm {
737734b6a94Sdarrenm SHA2Init(SHA256, ctx);
738734b6a94Sdarrenm }
739734b6a94Sdarrenm
740734b6a94Sdarrenm void
SHA384Init(SHA384_CTX * ctx)741734b6a94Sdarrenm SHA384Init(SHA384_CTX *ctx)
742734b6a94Sdarrenm {
743734b6a94Sdarrenm SHA2Init(SHA384, ctx);
744734b6a94Sdarrenm }
745734b6a94Sdarrenm
746734b6a94Sdarrenm void
SHA512Init(SHA512_CTX * ctx)747734b6a94Sdarrenm SHA512Init(SHA512_CTX *ctx)
748734b6a94Sdarrenm {
749734b6a94Sdarrenm SHA2Init(SHA512, ctx);
750734b6a94Sdarrenm }
751734b6a94Sdarrenm
752734b6a94Sdarrenm #endif /* _KERNEL */
753734b6a94Sdarrenm
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate * SHA2Update()
7567c478bd9Sstevel@tonic-gate *
7577c478bd9Sstevel@tonic-gate * purpose: continues an sha2 digest operation, using the message block
7587c478bd9Sstevel@tonic-gate * to update the context.
7597c478bd9Sstevel@tonic-gate * input: SHA2_CTX * : the context to update
760734b6a94Sdarrenm * void * : the message block
76155553f71Sda73024 * size_t : the length of the message block, in bytes
7627c478bd9Sstevel@tonic-gate * output: void
7637c478bd9Sstevel@tonic-gate */
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate void
SHA2Update(SHA2_CTX * ctx,const void * inptr,size_t input_len)766734b6a94Sdarrenm SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate uint32_t i, buf_index, buf_len, buf_limit;
769734b6a94Sdarrenm const uint8_t *input = inptr;
77055553f71Sda73024 uint32_t algotype = ctx->algotype;
77155553f71Sda73024 #if defined(__amd64)
77255553f71Sda73024 uint32_t block_count;
77355553f71Sda73024 #endif /* !__amd64 */
77455553f71Sda73024
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /* check for noop */
7777c478bd9Sstevel@tonic-gate if (input_len == 0)
7787c478bd9Sstevel@tonic-gate return;
7797c478bd9Sstevel@tonic-gate
78055553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
7817c478bd9Sstevel@tonic-gate buf_limit = 64;
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate /* compute number of bytes mod 64 */
7847c478bd9Sstevel@tonic-gate buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate /* update number of bits */
7877c478bd9Sstevel@tonic-gate if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
7887c478bd9Sstevel@tonic-gate ctx->count.c32[0]++;
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate ctx->count.c32[0] += (input_len >> 29);
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate } else {
7937c478bd9Sstevel@tonic-gate buf_limit = 128;
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate /* compute number of bytes mod 128 */
7967c478bd9Sstevel@tonic-gate buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate /* update number of bits */
7997c478bd9Sstevel@tonic-gate if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
8007c478bd9Sstevel@tonic-gate ctx->count.c64[0]++;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate ctx->count.c64[0] += (input_len >> 29);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate
8057c478bd9Sstevel@tonic-gate buf_len = buf_limit - buf_index;
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /* transform as many times as possible */
8087c478bd9Sstevel@tonic-gate i = 0;
8097c478bd9Sstevel@tonic-gate if (input_len >= buf_len) {
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate * general optimization:
8137c478bd9Sstevel@tonic-gate *
8147c478bd9Sstevel@tonic-gate * only do initial bcopy() and SHA2Transform() if
8157c478bd9Sstevel@tonic-gate * buf_index != 0. if buf_index == 0, we're just
8167c478bd9Sstevel@tonic-gate * wasting our time doing the bcopy() since there
8177c478bd9Sstevel@tonic-gate * wasn't any data left over from a previous call to
8187c478bd9Sstevel@tonic-gate * SHA2Update().
8197c478bd9Sstevel@tonic-gate */
8207c478bd9Sstevel@tonic-gate if (buf_index) {
8217c478bd9Sstevel@tonic-gate bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
82255553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
8237c478bd9Sstevel@tonic-gate SHA256Transform(ctx, ctx->buf_un.buf8);
8247c478bd9Sstevel@tonic-gate else
8257c478bd9Sstevel@tonic-gate SHA512Transform(ctx, ctx->buf_un.buf8);
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate i = buf_len;
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
83055553f71Sda73024 #if !defined(__amd64)
83155553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8327c478bd9Sstevel@tonic-gate for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8337c478bd9Sstevel@tonic-gate SHA256Transform(ctx, &input[i]);
83455553f71Sda73024 }
83555553f71Sda73024 } else {
83655553f71Sda73024 for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8377c478bd9Sstevel@tonic-gate SHA512Transform(ctx, &input[i]);
8387c478bd9Sstevel@tonic-gate }
83955553f71Sda73024 }
84055553f71Sda73024
84155553f71Sda73024 #else
84255553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
84355553f71Sda73024 block_count = (input_len - i) >> 6;
84455553f71Sda73024 if (block_count > 0) {
84555553f71Sda73024 SHA256TransformBlocks(ctx, &input[i],
84655553f71Sda73024 block_count);
84755553f71Sda73024 i += block_count << 6;
84855553f71Sda73024 }
84955553f71Sda73024 } else {
85055553f71Sda73024 block_count = (input_len - i) >> 7;
85155553f71Sda73024 if (block_count > 0) {
85255553f71Sda73024 SHA512TransformBlocks(ctx, &input[i],
85355553f71Sda73024 block_count);
85455553f71Sda73024 i += block_count << 7;
85555553f71Sda73024 }
85655553f71Sda73024 }
85755553f71Sda73024 #endif /* !__amd64 */
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate * general optimization:
8617c478bd9Sstevel@tonic-gate *
8627c478bd9Sstevel@tonic-gate * if i and input_len are the same, return now instead
8637c478bd9Sstevel@tonic-gate * of calling bcopy(), since the bcopy() in this case
86455553f71Sda73024 * will be an expensive noop.
8657c478bd9Sstevel@tonic-gate */
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate if (input_len == i)
8687c478bd9Sstevel@tonic-gate return;
8697c478bd9Sstevel@tonic-gate
8707c478bd9Sstevel@tonic-gate buf_index = 0;
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate /* buffer remaining input */
8747c478bd9Sstevel@tonic-gate bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate * SHA2Final()
8807c478bd9Sstevel@tonic-gate *
8817c478bd9Sstevel@tonic-gate * purpose: ends an sha2 digest operation, finalizing the message digest and
8827c478bd9Sstevel@tonic-gate * zeroing the context.
88355553f71Sda73024 * input: uchar_t * : a buffer to store the digest
8845151fb12Sdarrenm * : The function actually uses void* because many
8855151fb12Sdarrenm * : callers pass things other than uchar_t here.
8867c478bd9Sstevel@tonic-gate * SHA2_CTX * : the context to finalize, save, and zero
8877c478bd9Sstevel@tonic-gate * output: void
8887c478bd9Sstevel@tonic-gate */
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate void
SHA2Final(void * digest,SHA2_CTX * ctx)891734b6a94Sdarrenm SHA2Final(void *digest, SHA2_CTX *ctx)
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate uint8_t bitcount_be[sizeof (ctx->count.c32)];
8947c478bd9Sstevel@tonic-gate uint8_t bitcount_be64[sizeof (ctx->count.c64)];
8957c478bd9Sstevel@tonic-gate uint32_t index;
89655553f71Sda73024 uint32_t algotype = ctx->algotype;
8977c478bd9Sstevel@tonic-gate
89855553f71Sda73024 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8997c478bd9Sstevel@tonic-gate index = (ctx->count.c32[1] >> 3) & 0x3f;
9007c478bd9Sstevel@tonic-gate Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
9017c478bd9Sstevel@tonic-gate SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
9027c478bd9Sstevel@tonic-gate SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
9037c478bd9Sstevel@tonic-gate Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
9047c478bd9Sstevel@tonic-gate
9057c478bd9Sstevel@tonic-gate } else {
9067c478bd9Sstevel@tonic-gate index = (ctx->count.c64[1] >> 3) & 0x7f;
9077c478bd9Sstevel@tonic-gate Encode64(bitcount_be64, ctx->count.c64,
9087c478bd9Sstevel@tonic-gate sizeof (bitcount_be64));
9097c478bd9Sstevel@tonic-gate SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
9107c478bd9Sstevel@tonic-gate SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
91155553f71Sda73024 if (algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
9127c478bd9Sstevel@tonic-gate ctx->state.s64[6] = ctx->state.s64[7] = 0;
9137c478bd9Sstevel@tonic-gate Encode64(digest, ctx->state.s64,
9147c478bd9Sstevel@tonic-gate sizeof (uint64_t) * 6);
9157c478bd9Sstevel@tonic-gate } else
9167c478bd9Sstevel@tonic-gate Encode64(digest, ctx->state.s64,
9177c478bd9Sstevel@tonic-gate sizeof (ctx->state.s64));
9187c478bd9Sstevel@tonic-gate }
919673007c6Sdarrenm
920673007c6Sdarrenm /* zeroize sensitive information */
921673007c6Sdarrenm bzero(ctx, sizeof (*ctx));
9227c478bd9Sstevel@tonic-gate }
923