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