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