1 /*
   2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
   3  * Use is subject to license terms.
   4  */
   5 
   6 /*
   7  * The basic framework for this code came from the reference
   8  * implementation for MD5.  That implementation is Copyright (C)
   9  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
  10  *
  11  * License to copy and use this software is granted provided that it
  12  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  13  * Algorithm" in all material mentioning or referencing this software
  14  * or this function.
  15  *
  16  * License is also granted to make and use derivative works provided
  17  * that such works are identified as "derived from the RSA Data
  18  * Security, Inc. MD5 Message-Digest Algorithm" in all material
  19  * mentioning or referencing the derived work.
  20  *
  21  * RSA Data Security, Inc. makes no representations concerning either
  22  * the merchantability of this software or the suitability of this
  23  * software for any particular purpose. It is provided "as is"
  24  * without express or implied warranty of any kind.
  25  *
  26  * These notices must be retained in any copies of any part of this
  27  * documentation and/or software.
  28  *
  29  * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2
  30  * standard, available at
  31  * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  32  * Not as fast as one would like -- further optimizations are encouraged
  33  * and appreciated.
  34  */
  35 
  36 #include <sys/types.h>
  37 #include <sys/param.h>
  38 #include <sys/systm.h>
  39 #include <sys/sysmacros.h>
  40 #define _SHA2_IMPL
  41 #include <sys/sha2.h>
  42 #include <sys/sha2_consts.h>
  43 
  44 #ifdef _KERNEL
  45 #include <sys/cmn_err.h>
  46 
  47 #else
  48 #include <strings.h>
  49 #include <stdlib.h>
  50 #include <errno.h>
  51 
  52 #pragma weak SHA256Update = SHA2Update
  53 #pragma weak SHA384Update = SHA2Update
  54 #pragma weak SHA512Update = SHA2Update
  55 
  56 #pragma weak SHA256Final = SHA2Final
  57 #pragma weak SHA384Final = SHA2Final
  58 #pragma weak SHA512Final = SHA2Final
  59 
  60 #endif  /* _KERNEL */
  61 
  62 #ifdef _LITTLE_ENDIAN
  63 #include <sys/byteorder.h>
  64 #define HAVE_HTONL
  65 #endif
  66 
  67 static void Encode(uint8_t *, uint32_t *, size_t);
  68 static void Encode64(uint8_t *, uint64_t *, size_t);
  69 
  70 #if     defined(__amd64)
  71 #define SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1)
  72 #define SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1)
  73 
  74 void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
  75 void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
  76 
  77 #else
  78 static void SHA256Transform(SHA2_CTX *, const uint8_t *);
  79 static void SHA512Transform(SHA2_CTX *, const uint8_t *);
  80 #endif  /* __amd64 */
  81 
  82 static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
  83 
  84 /* Ch and Maj are the basic SHA2 functions. */
  85 #define Ch(b, c, d)     (((b) & (c)) ^ ((~b) & (d)))
  86 #define Maj(b, c, d)    (((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
  87 
  88 /* Rotates x right n bits. */
  89 #define ROTR(x, n)      \
  90         (((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
  91 
  92 /* Shift x right n bits */
  93 #define SHR(x, n)       ((x) >> (n))
  94 
  95 /* SHA256 Functions */
  96 #define BIGSIGMA0_256(x)        (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
  97 #define BIGSIGMA1_256(x)        (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
  98 #define SIGMA0_256(x)           (ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
  99 #define SIGMA1_256(x)           (ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
 100 
 101 #define SHA256ROUND(a, b, c, d, e, f, g, h, i, w)                       \
 102         T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w;  \
 103         d += T1;                                                        \
 104         T2 = BIGSIGMA0_256(a) + Maj(a, b, c);                           \
 105         h = T1 + T2
 106 
 107 /* SHA384/512 Functions */
 108 #define BIGSIGMA0(x)    (ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
 109 #define BIGSIGMA1(x)    (ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
 110 #define SIGMA0(x)       (ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
 111 #define SIGMA1(x)       (ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
 112 #define SHA512ROUND(a, b, c, d, e, f, g, h, i, w)                       \
 113         T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w;      \
 114         d += T1;                                                        \
 115         T2 = BIGSIGMA0(a) + Maj(a, b, c);                               \
 116         h = T1 + T2
 117 
 118 /*
 119  * sparc optimization:
 120  *
 121  * on the sparc, we can load big endian 32-bit data easily.  note that
 122  * special care must be taken to ensure the address is 32-bit aligned.
 123  * in the interest of speed, we don't check to make sure, since
 124  * careful programming can guarantee this for us.
 125  */
 126 
 127 #if     defined(_BIG_ENDIAN)
 128 #define LOAD_BIG_32(addr)       (*(uint32_t *)(addr))
 129 #define LOAD_BIG_64(addr)       (*(uint64_t *)(addr))
 130 
 131 #elif   defined(HAVE_HTONL)
 132 #define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
 133 #define LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr)))
 134 
 135 #else
 136 /* little endian -- will work on big endian, but slowly */
 137 #define LOAD_BIG_32(addr)       \
 138         (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
 139 #define LOAD_BIG_64(addr)       \
 140         (((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |        \
 141             ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) |     \
 142             ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) |     \
 143             ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
 144 #endif  /* _BIG_ENDIAN */
 145 
 146 
 147 #if     !defined(__amd64)
 148 /* SHA256 Transform */
 149 
 150 static void
 151 SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
 152 {
 153         uint32_t a = ctx->state.s32[0];
 154         uint32_t b = ctx->state.s32[1];
 155         uint32_t c = ctx->state.s32[2];
 156         uint32_t d = ctx->state.s32[3];
 157         uint32_t e = ctx->state.s32[4];
 158         uint32_t f = ctx->state.s32[5];
 159         uint32_t g = ctx->state.s32[6];
 160         uint32_t h = ctx->state.s32[7];
 161 
 162         uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
 163         uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
 164         uint32_t T1, T2;
 165 
 166 #if     defined(__sparc)
 167         static const uint32_t sha256_consts[] = {
 168                 SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
 169                 SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
 170                 SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
 171                 SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
 172                 SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
 173                 SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
 174                 SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
 175                 SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
 176                 SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
 177                 SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
 178                 SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
 179                 SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
 180                 SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
 181                 SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
 182                 SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
 183                 SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
 184                 SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
 185                 SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
 186                 SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
 187                 SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
 188                 SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
 189                 SHA256_CONST_63
 190         };
 191 #endif  /* __sparc */
 192 
 193         if ((uintptr_t)blk & 0x3) {         /* not 4-byte aligned? */
 194                 bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
 195                 blk = (uint8_t *)ctx->buf_un.buf32;
 196         }
 197 
 198         /* LINTED E_BAD_PTR_CAST_ALIGN */
 199         w0 =  LOAD_BIG_32(blk + 4 * 0);
 200         SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
 201         /* LINTED E_BAD_PTR_CAST_ALIGN */
 202         w1 =  LOAD_BIG_32(blk + 4 * 1);
 203         SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
 204         /* LINTED E_BAD_PTR_CAST_ALIGN */
 205         w2 =  LOAD_BIG_32(blk + 4 * 2);
 206         SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
 207         /* LINTED E_BAD_PTR_CAST_ALIGN */
 208         w3 =  LOAD_BIG_32(blk + 4 * 3);
 209         SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
 210         /* LINTED E_BAD_PTR_CAST_ALIGN */
 211         w4 =  LOAD_BIG_32(blk + 4 * 4);
 212         SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
 213         /* LINTED E_BAD_PTR_CAST_ALIGN */
 214         w5 =  LOAD_BIG_32(blk + 4 * 5);
 215         SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
 216         /* LINTED E_BAD_PTR_CAST_ALIGN */
 217         w6 =  LOAD_BIG_32(blk + 4 * 6);
 218         SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
 219         /* LINTED E_BAD_PTR_CAST_ALIGN */
 220         w7 =  LOAD_BIG_32(blk + 4 * 7);
 221         SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
 222         /* LINTED E_BAD_PTR_CAST_ALIGN */
 223         w8 =  LOAD_BIG_32(blk + 4 * 8);
 224         SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
 225         /* LINTED E_BAD_PTR_CAST_ALIGN */
 226         w9 =  LOAD_BIG_32(blk + 4 * 9);
 227         SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
 228         /* LINTED E_BAD_PTR_CAST_ALIGN */
 229         w10 =  LOAD_BIG_32(blk + 4 * 10);
 230         SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
 231         /* LINTED E_BAD_PTR_CAST_ALIGN */
 232         w11 =  LOAD_BIG_32(blk + 4 * 11);
 233         SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
 234         /* LINTED E_BAD_PTR_CAST_ALIGN */
 235         w12 =  LOAD_BIG_32(blk + 4 * 12);
 236         SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
 237         /* LINTED E_BAD_PTR_CAST_ALIGN */
 238         w13 =  LOAD_BIG_32(blk + 4 * 13);
 239         SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
 240         /* LINTED E_BAD_PTR_CAST_ALIGN */
 241         w14 =  LOAD_BIG_32(blk + 4 * 14);
 242         SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
 243         /* LINTED E_BAD_PTR_CAST_ALIGN */
 244         w15 =  LOAD_BIG_32(blk + 4 * 15);
 245         SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
 246 
 247         w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
 248         SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
 249         w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
 250         SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
 251         w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
 252         SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
 253         w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
 254         SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
 255         w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
 256         SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
 257         w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
 258         SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
 259         w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
 260         SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
 261         w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
 262         SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
 263         w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
 264         SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
 265         w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
 266         SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
 267         w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
 268         SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
 269         w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
 270         SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
 271         w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
 272         SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
 273         w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
 274         SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
 275         w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
 276         SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
 277         w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
 278         SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
 279 
 280         w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
 281         SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
 282         w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
 283         SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
 284         w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
 285         SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
 286         w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
 287         SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
 288         w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
 289         SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
 290         w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
 291         SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
 292         w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
 293         SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
 294         w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
 295         SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
 296         w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
 297         SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
 298         w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
 299         SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
 300         w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
 301         SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
 302         w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
 303         SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
 304         w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
 305         SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
 306         w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
 307         SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
 308         w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
 309         SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
 310         w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
 311         SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
 312 
 313         w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
 314         SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
 315         w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
 316         SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
 317         w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
 318         SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
 319         w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
 320         SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
 321         w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
 322         SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
 323         w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
 324         SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
 325         w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
 326         SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
 327         w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
 328         SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
 329         w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
 330         SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
 331         w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
 332         SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
 333         w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
 334         SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
 335         w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
 336         SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
 337         w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
 338         SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
 339         w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
 340         SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
 341         w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
 342         SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
 343         w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
 344         SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
 345 
 346         ctx->state.s32[0] += a;
 347         ctx->state.s32[1] += b;
 348         ctx->state.s32[2] += c;
 349         ctx->state.s32[3] += d;
 350         ctx->state.s32[4] += e;
 351         ctx->state.s32[5] += f;
 352         ctx->state.s32[6] += g;
 353         ctx->state.s32[7] += h;
 354 }
 355 
 356 
 357 /* SHA384 and SHA512 Transform */
 358 
 359 static void
 360 SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
 361 {
 362 
 363         uint64_t a = ctx->state.s64[0];
 364         uint64_t b = ctx->state.s64[1];
 365         uint64_t c = ctx->state.s64[2];
 366         uint64_t d = ctx->state.s64[3];
 367         uint64_t e = ctx->state.s64[4];
 368         uint64_t f = ctx->state.s64[5];
 369         uint64_t g = ctx->state.s64[6];
 370         uint64_t h = ctx->state.s64[7];
 371 
 372         uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
 373         uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
 374         uint64_t T1, T2;
 375 
 376 #if     defined(__sparc)
 377         static const uint64_t sha512_consts[] = {
 378                 SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
 379                 SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
 380                 SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
 381                 SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
 382                 SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
 383                 SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
 384                 SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
 385                 SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
 386                 SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
 387                 SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
 388                 SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
 389                 SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
 390                 SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
 391                 SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
 392                 SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
 393                 SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
 394                 SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
 395                 SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
 396                 SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
 397                 SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
 398                 SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
 399                 SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
 400                 SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
 401                 SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
 402                 SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
 403                 SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
 404                 SHA512_CONST_78, SHA512_CONST_79
 405         };
 406 #endif  /* __sparc */
 407 
 408 
 409         if ((uintptr_t)blk & 0x7) {         /* not 8-byte aligned? */
 410                 bcopy(blk, ctx->buf_un.buf64,  sizeof (ctx->buf_un.buf64));
 411                 blk = (uint8_t *)ctx->buf_un.buf64;
 412         }
 413 
 414         /* LINTED E_BAD_PTR_CAST_ALIGN */
 415         w0 =  LOAD_BIG_64(blk + 8 * 0);
 416         SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
 417         /* LINTED E_BAD_PTR_CAST_ALIGN */
 418         w1 =  LOAD_BIG_64(blk + 8 * 1);
 419         SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
 420         /* LINTED E_BAD_PTR_CAST_ALIGN */
 421         w2 =  LOAD_BIG_64(blk + 8 * 2);
 422         SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
 423         /* LINTED E_BAD_PTR_CAST_ALIGN */
 424         w3 =  LOAD_BIG_64(blk + 8 * 3);
 425         SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
 426         /* LINTED E_BAD_PTR_CAST_ALIGN */
 427         w4 =  LOAD_BIG_64(blk + 8 * 4);
 428         SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
 429         /* LINTED E_BAD_PTR_CAST_ALIGN */
 430         w5 =  LOAD_BIG_64(blk + 8 * 5);
 431         SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
 432         /* LINTED E_BAD_PTR_CAST_ALIGN */
 433         w6 =  LOAD_BIG_64(blk + 8 * 6);
 434         SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
 435         /* LINTED E_BAD_PTR_CAST_ALIGN */
 436         w7 =  LOAD_BIG_64(blk + 8 * 7);
 437         SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
 438         /* LINTED E_BAD_PTR_CAST_ALIGN */
 439         w8 =  LOAD_BIG_64(blk + 8 * 8);
 440         SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
 441         /* LINTED E_BAD_PTR_CAST_ALIGN */
 442         w9 =  LOAD_BIG_64(blk + 8 * 9);
 443         SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
 444         /* LINTED E_BAD_PTR_CAST_ALIGN */
 445         w10 =  LOAD_BIG_64(blk + 8 * 10);
 446         SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
 447         /* LINTED E_BAD_PTR_CAST_ALIGN */
 448         w11 =  LOAD_BIG_64(blk + 8 * 11);
 449         SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
 450         /* LINTED E_BAD_PTR_CAST_ALIGN */
 451         w12 =  LOAD_BIG_64(blk + 8 * 12);
 452         SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
 453         /* LINTED E_BAD_PTR_CAST_ALIGN */
 454         w13 =  LOAD_BIG_64(blk + 8 * 13);
 455         SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
 456         /* LINTED E_BAD_PTR_CAST_ALIGN */
 457         w14 =  LOAD_BIG_64(blk + 8 * 14);
 458         SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
 459         /* LINTED E_BAD_PTR_CAST_ALIGN */
 460         w15 =  LOAD_BIG_64(blk + 8 * 15);
 461         SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
 462 
 463         w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
 464         SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
 465         w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
 466         SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
 467         w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
 468         SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
 469         w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
 470         SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
 471         w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
 472         SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
 473         w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
 474         SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
 475         w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
 476         SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
 477         w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
 478         SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
 479         w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
 480         SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
 481         w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
 482         SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
 483         w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
 484         SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
 485         w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
 486         SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
 487         w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
 488         SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
 489         w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
 490         SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
 491         w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
 492         SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
 493         w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
 494         SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
 495 
 496         w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
 497         SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
 498         w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
 499         SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
 500         w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
 501         SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
 502         w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
 503         SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
 504         w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
 505         SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
 506         w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
 507         SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
 508         w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
 509         SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
 510         w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
 511         SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
 512         w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
 513         SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
 514         w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
 515         SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
 516         w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
 517         SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
 518         w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
 519         SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
 520         w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
 521         SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
 522         w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
 523         SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
 524         w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
 525         SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
 526         w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
 527         SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
 528 
 529         w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
 530         SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
 531         w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
 532         SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
 533         w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
 534         SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
 535         w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
 536         SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
 537         w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
 538         SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
 539         w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
 540         SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
 541         w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
 542         SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
 543         w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
 544         SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
 545         w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
 546         SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
 547         w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
 548         SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
 549         w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
 550         SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
 551         w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
 552         SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
 553         w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
 554         SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
 555         w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
 556         SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
 557         w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
 558         SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
 559         w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
 560         SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
 561 
 562         w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
 563         SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
 564         w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
 565         SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
 566         w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
 567         SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
 568         w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
 569         SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
 570         w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
 571         SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
 572         w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
 573         SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
 574         w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
 575         SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
 576         w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
 577         SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
 578         w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
 579         SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
 580         w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
 581         SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
 582         w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
 583         SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
 584         w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
 585         SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
 586         w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
 587         SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
 588         w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
 589         SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
 590         w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
 591         SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
 592         w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
 593         SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
 594 
 595         ctx->state.s64[0] += a;
 596         ctx->state.s64[1] += b;
 597         ctx->state.s64[2] += c;
 598         ctx->state.s64[3] += d;
 599         ctx->state.s64[4] += e;
 600         ctx->state.s64[5] += f;
 601         ctx->state.s64[6] += g;
 602         ctx->state.s64[7] += h;
 603 
 604 }
 605 #endif  /* !__amd64 */
 606 
 607 
 608 /*
 609  * Encode()
 610  *
 611  * purpose: to convert a list of numbers from little endian to big endian
 612  *   input: uint8_t *   : place to store the converted big endian numbers
 613  *          uint32_t *  : place to get numbers to convert from
 614  *          size_t      : the length of the input in bytes
 615  *  output: void
 616  */
 617 
 618 static void
 619 Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input,
 620     size_t len)
 621 {
 622         size_t          i, j;
 623 
 624 #if     defined(__sparc)
 625         if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
 626                 for (i = 0, j = 0; j < len; i++, j += 4) {
 627                         /* LINTED: pointer alignment */
 628                         *((uint32_t *)(output + j)) = input[i];
 629                 }
 630         } else {
 631 #endif  /* little endian -- will work on big endian, but slowly */
 632                 for (i = 0, j = 0; j < len; i++, j += 4) {
 633                         output[j]       = (input[i] >> 24) & 0xff;
 634                         output[j + 1]   = (input[i] >> 16) & 0xff;
 635                         output[j + 2]   = (input[i] >>  8) & 0xff;
 636                         output[j + 3]   = input[i] & 0xff;
 637                 }
 638 #if     defined(__sparc)
 639         }
 640 #endif
 641 }
 642 
 643 static void
 644 Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input,
 645     size_t len)
 646 {
 647         size_t          i, j;
 648 
 649 #if     defined(__sparc)
 650         if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
 651                 for (i = 0, j = 0; j < len; i++, j += 8) {
 652                         /* LINTED: pointer alignment */
 653                         *((uint64_t *)(output + j)) = input[i];
 654                 }
 655         } else {
 656 #endif  /* little endian -- will work on big endian, but slowly */
 657                 for (i = 0, j = 0; j < len; i++, j += 8) {
 658 
 659                         output[j]       = (input[i] >> 56) & 0xff;
 660                         output[j + 1]   = (input[i] >> 48) & 0xff;
 661                         output[j + 2]   = (input[i] >> 40) & 0xff;
 662                         output[j + 3]   = (input[i] >> 32) & 0xff;
 663                         output[j + 4]   = (input[i] >> 24) & 0xff;
 664                         output[j + 5]   = (input[i] >> 16) & 0xff;
 665                         output[j + 6]   = (input[i] >>  8) & 0xff;
 666                         output[j + 7]   = input[i] & 0xff;
 667                 }
 668 #if     defined(__sparc)
 669         }
 670 #endif
 671 }
 672 
 673 
 674 void
 675 SHA2Init(uint64_t mech, SHA2_CTX *ctx)
 676 {
 677 
 678         switch (mech) {
 679         case SHA256_MECH_INFO_TYPE:
 680         case SHA256_HMAC_MECH_INFO_TYPE:
 681         case SHA256_HMAC_GEN_MECH_INFO_TYPE:
 682                 ctx->state.s32[0] = 0x6a09e667U;
 683                 ctx->state.s32[1] = 0xbb67ae85U;
 684                 ctx->state.s32[2] = 0x3c6ef372U;
 685                 ctx->state.s32[3] = 0xa54ff53aU;
 686                 ctx->state.s32[4] = 0x510e527fU;
 687                 ctx->state.s32[5] = 0x9b05688cU;
 688                 ctx->state.s32[6] = 0x1f83d9abU;
 689                 ctx->state.s32[7] = 0x5be0cd19U;
 690                 break;
 691         case SHA384_MECH_INFO_TYPE:
 692         case SHA384_HMAC_MECH_INFO_TYPE:
 693         case SHA384_HMAC_GEN_MECH_INFO_TYPE:
 694                 ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
 695                 ctx->state.s64[1] = 0x629a292a367cd507ULL;
 696                 ctx->state.s64[2] = 0x9159015a3070dd17ULL;
 697                 ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
 698                 ctx->state.s64[4] = 0x67332667ffc00b31ULL;
 699                 ctx->state.s64[5] = 0x8eb44a8768581511ULL;
 700                 ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
 701                 ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
 702                 break;
 703         case SHA512_MECH_INFO_TYPE:
 704         case SHA512_HMAC_MECH_INFO_TYPE:
 705         case SHA512_HMAC_GEN_MECH_INFO_TYPE:
 706                 ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
 707                 ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
 708                 ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
 709                 ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
 710                 ctx->state.s64[4] = 0x510e527fade682d1ULL;
 711                 ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
 712                 ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
 713                 ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
 714                 break;
 715 #ifdef _KERNEL
 716         default:
 717                 cmn_err(CE_PANIC,
 718                     "sha2_init: failed to find a supported algorithm: 0x%x",
 719                     (uint32_t)mech);
 720 
 721 #endif /* _KERNEL */
 722         }
 723 
 724         ctx->algotype = mech;
 725         ctx->count.c64[0] = ctx->count.c64[1] = 0;
 726 }
 727 
 728 #ifndef _KERNEL
 729 
 730 #pragma inline(SHA256Init, SHA384Init, SHA512Init)
 731 void
 732 SHA256Init(SHA256_CTX *ctx)
 733 {
 734         SHA2Init(SHA256, ctx);
 735 }
 736 
 737 void
 738 SHA384Init(SHA384_CTX *ctx)
 739 {
 740         SHA2Init(SHA384, ctx);
 741 }
 742 
 743 void
 744 SHA512Init(SHA512_CTX *ctx)
 745 {
 746         SHA2Init(SHA512, ctx);
 747 }
 748 
 749 #endif /* _KERNEL */
 750 
 751 /*
 752  * SHA2Update()
 753  *
 754  * purpose: continues an sha2 digest operation, using the message block
 755  *          to update the context.
 756  *   input: SHA2_CTX *  : the context to update
 757  *          void *      : the message block
 758  *          size_t      : the length of the message block, in bytes
 759  *  output: void
 760  */
 761 
 762 void
 763 SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len)
 764 {
 765         uint32_t        i, buf_index, buf_len, buf_limit;
 766         const uint8_t   *input = inptr;
 767         uint32_t        algotype = ctx->algotype;
 768 #if defined(__amd64)
 769         uint32_t        block_count;
 770 #endif  /* !__amd64 */
 771 
 772 
 773         /* check for noop */
 774         if (input_len == 0)
 775                 return;
 776 
 777         if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
 778                 buf_limit = 64;
 779 
 780                 /* compute number of bytes mod 64 */
 781                 buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
 782 
 783                 /* update number of bits */
 784                 if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
 785                         ctx->count.c32[0]++;
 786 
 787                 ctx->count.c32[0] += (input_len >> 29);
 788 
 789         } else {
 790                 buf_limit = 128;
 791 
 792                 /* compute number of bytes mod 128 */
 793                 buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
 794 
 795                 /* update number of bits */
 796                 if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
 797                         ctx->count.c64[0]++;
 798 
 799                 ctx->count.c64[0] += (input_len >> 29);
 800         }
 801 
 802         buf_len = buf_limit - buf_index;
 803 
 804         /* transform as many times as possible */
 805         i = 0;
 806         if (input_len >= buf_len) {
 807 
 808                 /*
 809                  * general optimization:
 810                  *
 811                  * only do initial bcopy() and SHA2Transform() if
 812                  * buf_index != 0.  if buf_index == 0, we're just
 813                  * wasting our time doing the bcopy() since there
 814                  * wasn't any data left over from a previous call to
 815                  * SHA2Update().
 816                  */
 817                 if (buf_index) {
 818                         bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
 819                         if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
 820                                 SHA256Transform(ctx, ctx->buf_un.buf8);
 821                         else
 822                                 SHA512Transform(ctx, ctx->buf_un.buf8);
 823 
 824                         i = buf_len;
 825                 }
 826 
 827 #if !defined(__amd64)
 828                 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
 829                         for (; i + buf_limit - 1 < input_len; i += buf_limit) {
 830                                 SHA256Transform(ctx, &input[i]);
 831                         }
 832                 } else {
 833                         for (; i + buf_limit - 1 < input_len; i += buf_limit) {
 834                                 SHA512Transform(ctx, &input[i]);
 835                         }
 836                 }
 837 
 838 #else
 839                 if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
 840                         block_count = (input_len - i) >> 6;
 841                         if (block_count > 0) {
 842                                 SHA256TransformBlocks(ctx, &input[i],
 843                                     block_count);
 844                                 i += block_count << 6;
 845                         }
 846                 } else {
 847                         block_count = (input_len - i) >> 7;
 848                         if (block_count > 0) {
 849                                 SHA512TransformBlocks(ctx, &input[i],
 850                                     block_count);
 851                                 i += block_count << 7;
 852                         }
 853                 }
 854 #endif  /* !__amd64 */
 855 
 856                 /*
 857                  * general optimization:
 858                  *
 859                  * if i and input_len are the same, return now instead
 860                  * of calling bcopy(), since the bcopy() in this case
 861                  * will be an expensive noop.
 862                  */
 863 
 864                 if (input_len == i)
 865                         return;
 866 
 867                 buf_index = 0;
 868         }
 869 
 870         /* buffer remaining input */
 871         bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
 872 }
 873 
 874 
 875 /*
 876  * SHA2Final()
 877  *
 878  * purpose: ends an sha2 digest operation, finalizing the message digest and
 879  *          zeroing the context.
 880  *   input: uchar_t *   : a buffer to store the digest
 881  *                      : The function actually uses void* because many
 882  *                      : callers pass things other than uchar_t here.
 883  *          SHA2_CTX *  : the context to finalize, save, and zero
 884  *  output: void
 885  */
 886 
 887 void
 888 SHA2Final(void *digest, SHA2_CTX *ctx)
 889 {
 890         uint8_t         bitcount_be[sizeof (ctx->count.c32)];
 891         uint8_t         bitcount_be64[sizeof (ctx->count.c64)];
 892         uint32_t        index;
 893         uint32_t        algotype = ctx->algotype;
 894 
 895         if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
 896                 index  = (ctx->count.c32[1] >> 3) & 0x3f;
 897                 Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
 898                 SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
 899                 SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
 900                 Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
 901 
 902         } else {
 903                 index  = (ctx->count.c64[1] >> 3) & 0x7f;
 904                 Encode64(bitcount_be64, ctx->count.c64,
 905                     sizeof (bitcount_be64));
 906                 SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
 907                 SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
 908                 if (algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
 909                         ctx->state.s64[6] = ctx->state.s64[7] = 0;
 910                         Encode64(digest, ctx->state.s64,
 911                             sizeof (uint64_t) * 6);
 912                 } else
 913                         Encode64(digest, ctx->state.s64,
 914                             sizeof (ctx->state.s64));
 915         }
 916 
 917         /* zeroize sensitive information */
 918         bzero(ctx, sizeof (*ctx));
 919 }