1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6 #pragma ident "%Z%%M% %I% %E% SMI"
7
8 /*
9 * The basic framework for this code came from the reference
10 * implementation for MD5. That implementation is Copyright (C)
11 * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
12 *
13 * License to copy and use this software is granted provided that it
14 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
15 * Algorithm" in all material mentioning or referencing this software
16 * or this function.
17 *
18 * License is also granted to make and use derivative works provided
19 * that such works are identified as "derived from the RSA Data
20 * Security, Inc. MD5 Message-Digest Algorithm" in all material
21 * mentioning or referencing the derived work.
22 *
23 * RSA Data Security, Inc. makes no representations concerning either
24 * the merchantability of this software or the suitability of this
25 * software for any particular purpose. It is provided "as is"
26 * without express or implied warranty of any kind.
27 *
28 * These notices must be retained in any copies of any part of this
29 * documentation and/or software.
30 *
31 * NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1
32 * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-1.htm
33 * Not as fast as one would like -- further optimizations are encouraged
34 * and appreciated.
35 */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysmacros.h>
41 #include <sys/sha1.h>
42 #include <sys/sha1_consts.h>
43
44 #ifndef _KERNEL
45 #include <strings.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <sys/systeminfo.h>
49 #endif /* !_KERNEL */
50
51 static void Encode(uint8_t *, const uint32_t *, size_t);
52
53 #if defined(__sparc)
54
55 #define SHA1_TRANSFORM(ctx, in) \
56 SHA1Transform((ctx)->state[0], (ctx)->state[1], (ctx)->state[2], \
57 (ctx)->state[3], (ctx)->state[4], (ctx), (in))
58
59 static void SHA1Transform(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
60 SHA1_CTX *, const uint8_t *);
61
62 #elif defined(__amd64)
63
64 #define SHA1_TRANSFORM(ctx, in) sha1_block_data_order((ctx), (in), 1)
65 #define SHA1_TRANSFORM_BLOCKS(ctx, in, num) sha1_block_data_order((ctx), \
66 (in), (num))
67
68 void sha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t num_blocks);
69
70 #else
71
72 #define SHA1_TRANSFORM(ctx, in) SHA1Transform((ctx), (in))
73
74 static void SHA1Transform(SHA1_CTX *, const uint8_t *);
75
76 #endif
77
78
79 static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
80
81 /*
82 * F, G, and H are the basic SHA1 functions.
83 */
84 #define F(b, c, d) (((b) & (c)) | ((~b) & (d)))
85 #define G(b, c, d) ((b) ^ (c) ^ (d))
86 #define H(b, c, d) (((b) & (c)) | (((b)|(c)) & (d)))
87
88 /*
89 * ROTATE_LEFT rotates x left n bits.
90 */
91
92 #if defined(__GNUC__) && defined(_LP64)
93 static __inline__ uint64_t
94 ROTATE_LEFT(uint64_t value, uint32_t n)
95 {
96 uint32_t t32;
97
98 t32 = (uint32_t)value;
99 return ((t32 << n) | (t32 >> (32 - n)));
100 }
101
102 #else
103
104 #define ROTATE_LEFT(x, n) \
105 (((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n))))
106
107 #endif
108
109 #if defined(__GNUC__) && (defined(__i386) || defined(__amd64))
110
111 #define HAVE_BSWAP
112
113 extern __inline__ uint32_t bswap(uint32_t value)
114 {
115 __asm__("bswap %0" : "+r" (value));
116 return (value);
117 }
118
119 #endif
120
121 /*
122 * SHA1Init()
123 *
124 * purpose: initializes the sha1 context and begins and sha1 digest operation
125 * input: SHA1_CTX * : the context to initializes.
126 * output: void
127 */
128
129 void
130 SHA1Init(SHA1_CTX *ctx)
131 {
132 ctx->count[0] = ctx->count[1] = 0;
133
134 /*
135 * load magic initialization constants. Tell lint
136 * that these constants are unsigned by using U.
137 */
138
139 ctx->state[0] = 0x67452301U;
140 ctx->state[1] = 0xefcdab89U;
141 ctx->state[2] = 0x98badcfeU;
142 ctx->state[3] = 0x10325476U;
143 ctx->state[4] = 0xc3d2e1f0U;
144 }
145
146 #ifdef VIS_SHA1
147 #ifdef _KERNEL
148
149 #include <sys/regset.h>
150 #include <sys/vis.h>
151 #include <sys/fpu/fpusystm.h>
152
153 /* the alignment for block stores to save fp registers */
154 #define VIS_ALIGN (64)
155
156 extern int sha1_savefp(kfpu_t *, int);
157 extern void sha1_restorefp(kfpu_t *);
158
159 uint32_t vis_sha1_svfp_threshold = 128;
160
161 #endif /* _KERNEL */
162
163 /*
164 * VIS SHA-1 consts.
165 */
166 static uint64_t VIS[] = {
167 0x8000000080000000ULL,
168 0x0002000200020002ULL,
169 0x5a8279996ed9eba1ULL,
170 0x8f1bbcdcca62c1d6ULL,
171 0x012389ab456789abULL};
172
173 extern void SHA1TransformVIS(uint64_t *, uint32_t *, uint32_t *, uint64_t *);
174
175
176 /*
177 * SHA1Update()
178 *
179 * purpose: continues an sha1 digest operation, using the message block
180 * to update the context.
181 * input: SHA1_CTX * : the context to update
182 * void * : the message block
183 * size_t : the length of the message block in bytes
184 * output: void
185 */
186
187 void
188 SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len)
189 {
190 uint32_t i, buf_index, buf_len;
191 uint64_t X0[40], input64[8];
192 const uint8_t *input = inptr;
193 #ifdef _KERNEL
194 int usevis = 0;
195 #else
196 int usevis = 1;
197 #endif /* _KERNEL */
198
199 /* check for noop */
200 if (input_len == 0)
201 return;
202
203 /* compute number of bytes mod 64 */
204 buf_index = (ctx->count[1] >> 3) & 0x3F;
205
206 /* update number of bits */
207 if ((ctx->count[1] += (input_len << 3)) < (input_len << 3))
208 ctx->count[0]++;
209
210 ctx->count[0] += (input_len >> 29);
211
212 buf_len = 64 - buf_index;
213
214 /* transform as many times as possible */
215 i = 0;
216 if (input_len >= buf_len) {
217 #ifdef _KERNEL
218 kfpu_t *fpu;
219 if (fpu_exists) {
220 uint8_t fpua[sizeof (kfpu_t) + GSR_SIZE + VIS_ALIGN];
221 uint32_t len = (input_len + buf_index) & ~0x3f;
222 int svfp_ok;
223
224 fpu = (kfpu_t *)P2ROUNDUP((uintptr_t)fpua, 64);
225 svfp_ok = ((len >= vis_sha1_svfp_threshold) ? 1 : 0);
226 usevis = fpu_exists && sha1_savefp(fpu, svfp_ok);
227 } else {
228 usevis = 0;
229 }
230 #endif /* _KERNEL */
231
232 /*
233 * general optimization:
234 *
235 * only do initial bcopy() and SHA1Transform() if
236 * buf_index != 0. if buf_index == 0, we're just
237 * wasting our time doing the bcopy() since there
238 * wasn't any data left over from a previous call to
239 * SHA1Update().
240 */
241
242 if (buf_index) {
243 bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
244 if (usevis) {
245 SHA1TransformVIS(X0,
246 ctx->buf_un.buf32,
247 &ctx->state[0], VIS);
248 } else {
249 SHA1_TRANSFORM(ctx, ctx->buf_un.buf8);
250 }
251 i = buf_len;
252 }
253
254 /*
255 * VIS SHA-1: uses the VIS 1.0 instructions to accelerate
256 * SHA-1 processing. This is achieved by "offloading" the
257 * computation of the message schedule (MS) to the VIS units.
258 * This allows the VIS computation of the message schedule
259 * to be performed in parallel with the standard integer
260 * processing of the remainder of the SHA-1 computation.
261 * performance by up to around 1.37X, compared to an optimized
262 * integer-only implementation.
263 *
264 * The VIS implementation of SHA1Transform has a different API
265 * to the standard integer version:
266 *
267 * void SHA1TransformVIS(
268 * uint64_t *, // Pointer to MS for ith block
269 * uint32_t *, // Pointer to ith block of message data
270 * uint32_t *, // Pointer to SHA state i.e ctx->state
271 * uint64_t *, // Pointer to various VIS constants
272 * )
273 *
274 * Note: the message data must by 4-byte aligned.
275 *
276 * Function requires VIS 1.0 support.
277 *
278 * Handling is provided to deal with arbitrary byte alingment
279 * of the input data but the performance gains are reduced
280 * for alignments other than 4-bytes.
281 */
282 if (usevis) {
283 if (!IS_P2ALIGNED(&input[i], sizeof (uint32_t))) {
284 /*
285 * Main processing loop - input misaligned
286 */
287 for (; i + 63 < input_len; i += 64) {
288 bcopy(&input[i], input64, 64);
289 SHA1TransformVIS(X0,
290 (uint32_t *)input64,
291 &ctx->state[0], VIS);
292 }
293 } else {
294 /*
295 * Main processing loop - input 8-byte aligned
296 */
297 for (; i + 63 < input_len; i += 64) {
298 SHA1TransformVIS(X0,
299 /* LINTED E_BAD_PTR_CAST_ALIGN */
300 (uint32_t *)&input[i],
301 &ctx->state[0], VIS);
302 }
303
304 }
305 #ifdef _KERNEL
306 sha1_restorefp(fpu);
307 #endif /* _KERNEL */
308 } else {
309 for (; i + 63 < input_len; i += 64) {
310 SHA1_TRANSFORM(ctx, &input[i]);
311 }
312 }
313
314 /*
315 * general optimization:
316 *
317 * if i and input_len are the same, return now instead
318 * of calling bcopy(), since the bcopy() in this case
319 * will be an expensive nop.
320 */
321
322 if (input_len == i)
323 return;
324
325 buf_index = 0;
326 }
327
328 /* buffer remaining input */
329 bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
330 }
331
332 #else /* VIS_SHA1 */
333
334 void
335 SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len)
336 {
337 uint32_t i, buf_index, buf_len;
338 const uint8_t *input = inptr;
339 #if defined(__amd64)
340 uint32_t block_count;
341 #endif /* __amd64 */
342
343 /* check for noop */
344 if (input_len == 0)
345 return;
346
347 /* compute number of bytes mod 64 */
348 buf_index = (ctx->count[1] >> 3) & 0x3F;
349
350 /* update number of bits */
351 if ((ctx->count[1] += (input_len << 3)) < (input_len << 3))
352 ctx->count[0]++;
353
354 ctx->count[0] += (input_len >> 29);
355
356 buf_len = 64 - buf_index;
357
358 /* transform as many times as possible */
359 i = 0;
360 if (input_len >= buf_len) {
361
362 /*
363 * general optimization:
364 *
365 * only do initial bcopy() and SHA1Transform() if
366 * buf_index != 0. if buf_index == 0, we're just
367 * wasting our time doing the bcopy() since there
368 * wasn't any data left over from a previous call to
369 * SHA1Update().
370 */
371
372 if (buf_index) {
373 bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
374 SHA1_TRANSFORM(ctx, ctx->buf_un.buf8);
375 i = buf_len;
376 }
377
378 #if !defined(__amd64)
379 for (; i + 63 < input_len; i += 64)
380 SHA1_TRANSFORM(ctx, &input[i]);
381 #else
382 block_count = (input_len - i) >> 6;
383 if (block_count > 0) {
384 SHA1_TRANSFORM_BLOCKS(ctx, &input[i], block_count);
385 i += block_count << 6;
386 }
387 #endif /* !__amd64 */
388
389 /*
390 * general optimization:
391 *
392 * if i and input_len are the same, return now instead
393 * of calling bcopy(), since the bcopy() in this case
394 * will be an expensive nop.
395 */
396
397 if (input_len == i)
398 return;
399
400 buf_index = 0;
401 }
402
403 /* buffer remaining input */
404 bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
405 }
406
407 #endif /* VIS_SHA1 */
408
409 /*
410 * SHA1Final()
411 *
412 * purpose: ends an sha1 digest operation, finalizing the message digest and
413 * zeroing the context.
414 * input: uchar_t * : A buffer to store the digest.
415 * : The function actually uses void* because many
416 * : callers pass things other than uchar_t here.
417 * SHA1_CTX * : the context to finalize, save, and zero
418 * output: void
419 */
420
421 void
422 SHA1Final(void *digest, SHA1_CTX *ctx)
423 {
424 uint8_t bitcount_be[sizeof (ctx->count)];
425 uint32_t index = (ctx->count[1] >> 3) & 0x3f;
426
427 /* store bit count, big endian */
428 Encode(bitcount_be, ctx->count, sizeof (bitcount_be));
429
430 /* pad out to 56 mod 64 */
431 SHA1Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
432
433 /* append length (before padding) */
434 SHA1Update(ctx, bitcount_be, sizeof (bitcount_be));
435
436 /* store state in digest */
437 Encode(digest, ctx->state, sizeof (ctx->state));
438
439 /* zeroize sensitive information */
440 bzero(ctx, sizeof (*ctx));
441 }
442
443
444 #if !defined(__amd64)
445
446 typedef uint32_t sha1word;
447
448 /*
449 * sparc optimization:
450 *
451 * on the sparc, we can load big endian 32-bit data easily. note that
452 * special care must be taken to ensure the address is 32-bit aligned.
453 * in the interest of speed, we don't check to make sure, since
454 * careful programming can guarantee this for us.
455 */
456
457 #if defined(_BIG_ENDIAN)
458
459 #define LOAD_BIG_32(addr) (*(uint32_t *)(addr))
460
461 #else /* !defined(_BIG_ENDIAN) */
462
463 #if defined(HAVE_BSWAP)
464
465 #define LOAD_BIG_32(addr) bswap(*((uint32_t *)(addr)))
466
467 #else /* !defined(HAVE_BSWAP) */
468
469 /* little endian -- will work on big endian, but slowly */
470 #define LOAD_BIG_32(addr) \
471 (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
472
473 #endif /* !defined(HAVE_BSWAP) */
474
475 #endif /* !defined(_BIG_ENDIAN) */
476
477 /*
478 * SHA1Transform()
479 */
480 #if defined(W_ARRAY)
481 #define W(n) w[n]
482 #else /* !defined(W_ARRAY) */
483 #define W(n) w_ ## n
484 #endif /* !defined(W_ARRAY) */
485
486
487 #if defined(__sparc)
488
489 /*
490 * sparc register window optimization:
491 *
492 * `a', `b', `c', `d', and `e' are passed into SHA1Transform
493 * explicitly since it increases the number of registers available to
494 * the compiler. under this scheme, these variables can be held in
495 * %i0 - %i4, which leaves more local and out registers available.
496 *
497 * purpose: sha1 transformation -- updates the digest based on `block'
498 * input: uint32_t : bytes 1 - 4 of the digest
499 * uint32_t : bytes 5 - 8 of the digest
500 * uint32_t : bytes 9 - 12 of the digest
501 * uint32_t : bytes 12 - 16 of the digest
502 * uint32_t : bytes 16 - 20 of the digest
503 * SHA1_CTX * : the context to update
504 * uint8_t [64]: the block to use to update the digest
505 * output: void
506 */
507
508 void
509 SHA1Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e,
510 SHA1_CTX *ctx, const uint8_t blk[64])
511 {
512 /*
513 * sparc optimization:
514 *
515 * while it is somewhat counter-intuitive, on sparc, it is
516 * more efficient to place all the constants used in this
517 * function in an array and load the values out of the array
518 * than to manually load the constants. this is because
519 * setting a register to a 32-bit value takes two ops in most
520 * cases: a `sethi' and an `or', but loading a 32-bit value
521 * from memory only takes one `ld' (or `lduw' on v9). while
522 * this increases memory usage, the compiler can find enough
523 * other things to do while waiting to keep the pipeline does
524 * not stall. additionally, it is likely that many of these
525 * constants are cached so that later accesses do not even go
526 * out to the bus.
527 *
528 * this array is declared `static' to keep the compiler from
529 * having to bcopy() this array onto the stack frame of
530 * SHA1Transform() each time it is called -- which is
531 * unacceptably expensive.
532 *
533 * the `const' is to ensure that callers are good citizens and
534 * do not try to munge the array. since these routines are
535 * going to be called from inside multithreaded kernelland,
536 * this is a good safety check. -- `sha1_consts' will end up in
537 * .rodata.
538 *
539 * unfortunately, loading from an array in this manner hurts
540 * performance under intel. so, there is a macro,
541 * SHA1_CONST(), used in SHA1Transform(), that either expands to
542 * a reference to this array, or to the actual constant,
543 * depending on what platform this code is compiled for.
544 */
545
546 static const uint32_t sha1_consts[] = {
547 SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3,
548 };
549
550 /*
551 * general optimization:
552 *
553 * use individual integers instead of using an array. this is a
554 * win, although the amount it wins by seems to vary quite a bit.
555 */
556
557 uint32_t w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7;
558 uint32_t w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15;
559
560 /*
561 * sparc optimization:
562 *
563 * if `block' is already aligned on a 4-byte boundary, use
564 * LOAD_BIG_32() directly. otherwise, bcopy() into a
565 * buffer that *is* aligned on a 4-byte boundary and then do
566 * the LOAD_BIG_32() on that buffer. benchmarks have shown
567 * that using the bcopy() is better than loading the bytes
568 * individually and doing the endian-swap by hand.
569 *
570 * even though it's quite tempting to assign to do:
571 *
572 * blk = bcopy(ctx->buf_un.buf32, blk, sizeof (ctx->buf_un.buf32));
573 *
574 * and only have one set of LOAD_BIG_32()'s, the compiler
575 * *does not* like that, so please resist the urge.
576 */
577
578 if ((uintptr_t)blk & 0x3) { /* not 4-byte aligned? */
579 bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
580 w_15 = LOAD_BIG_32(ctx->buf_un.buf32 + 15);
581 w_14 = LOAD_BIG_32(ctx->buf_un.buf32 + 14);
582 w_13 = LOAD_BIG_32(ctx->buf_un.buf32 + 13);
583 w_12 = LOAD_BIG_32(ctx->buf_un.buf32 + 12);
584 w_11 = LOAD_BIG_32(ctx->buf_un.buf32 + 11);
585 w_10 = LOAD_BIG_32(ctx->buf_un.buf32 + 10);
586 w_9 = LOAD_BIG_32(ctx->buf_un.buf32 + 9);
587 w_8 = LOAD_BIG_32(ctx->buf_un.buf32 + 8);
588 w_7 = LOAD_BIG_32(ctx->buf_un.buf32 + 7);
589 w_6 = LOAD_BIG_32(ctx->buf_un.buf32 + 6);
590 w_5 = LOAD_BIG_32(ctx->buf_un.buf32 + 5);
591 w_4 = LOAD_BIG_32(ctx->buf_un.buf32 + 4);
592 w_3 = LOAD_BIG_32(ctx->buf_un.buf32 + 3);
593 w_2 = LOAD_BIG_32(ctx->buf_un.buf32 + 2);
594 w_1 = LOAD_BIG_32(ctx->buf_un.buf32 + 1);
595 w_0 = LOAD_BIG_32(ctx->buf_un.buf32 + 0);
596 } else {
597 /*LINTED*/
598 w_15 = LOAD_BIG_32(blk + 60);
599 /*LINTED*/
600 w_14 = LOAD_BIG_32(blk + 56);
601 /*LINTED*/
602 w_13 = LOAD_BIG_32(blk + 52);
603 /*LINTED*/
604 w_12 = LOAD_BIG_32(blk + 48);
605 /*LINTED*/
606 w_11 = LOAD_BIG_32(blk + 44);
607 /*LINTED*/
608 w_10 = LOAD_BIG_32(blk + 40);
609 /*LINTED*/
610 w_9 = LOAD_BIG_32(blk + 36);
611 /*LINTED*/
612 w_8 = LOAD_BIG_32(blk + 32);
613 /*LINTED*/
614 w_7 = LOAD_BIG_32(blk + 28);
615 /*LINTED*/
616 w_6 = LOAD_BIG_32(blk + 24);
617 /*LINTED*/
618 w_5 = LOAD_BIG_32(blk + 20);
619 /*LINTED*/
620 w_4 = LOAD_BIG_32(blk + 16);
621 /*LINTED*/
622 w_3 = LOAD_BIG_32(blk + 12);
623 /*LINTED*/
624 w_2 = LOAD_BIG_32(blk + 8);
625 /*LINTED*/
626 w_1 = LOAD_BIG_32(blk + 4);
627 /*LINTED*/
628 w_0 = LOAD_BIG_32(blk + 0);
629 }
630 #else /* !defined(__sparc) */
631
632 void
633 SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64])
634 {
635 sha1word a = ctx->state[0];
636 sha1word b = ctx->state[1];
637 sha1word c = ctx->state[2];
638 sha1word d = ctx->state[3];
639 sha1word e = ctx->state[4];
640
641 #if defined(W_ARRAY)
642 sha1word w[16];
643 #else /* !defined(W_ARRAY) */
644 sha1word w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7;
645 sha1word w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15;
646 #endif /* !defined(W_ARRAY) */
647
648 W(0) = LOAD_BIG_32(blk + 0);
649 W(1) = LOAD_BIG_32(blk + 4);
650 W(2) = LOAD_BIG_32(blk + 8);
651 W(3) = LOAD_BIG_32(blk + 12);
652 W(4) = LOAD_BIG_32(blk + 16);
653 W(5) = LOAD_BIG_32(blk + 20);
654 W(6) = LOAD_BIG_32(blk + 24);
655 W(7) = LOAD_BIG_32(blk + 28);
656 W(8) = LOAD_BIG_32(blk + 32);
657 W(9) = LOAD_BIG_32(blk + 36);
658 W(10) = LOAD_BIG_32(blk + 40);
659 W(11) = LOAD_BIG_32(blk + 44);
660 W(12) = LOAD_BIG_32(blk + 48);
661 W(13) = LOAD_BIG_32(blk + 52);
662 W(14) = LOAD_BIG_32(blk + 56);
663 W(15) = LOAD_BIG_32(blk + 60);
664
665 #endif /* !defined(__sparc) */
666
667 /*
668 * general optimization:
669 *
670 * even though this approach is described in the standard as
671 * being slower algorithmically, it is 30-40% faster than the
672 * "faster" version under SPARC, because this version has more
673 * of the constraints specified at compile-time and uses fewer
674 * variables (and therefore has better register utilization)
675 * than its "speedier" brother. (i've tried both, trust me)
676 *
677 * for either method given in the spec, there is an "assignment"
678 * phase where the following takes place:
679 *
680 * tmp = (main_computation);
681 * e = d; d = c; c = rotate_left(b, 30); b = a; a = tmp;
682 *
683 * we can make the algorithm go faster by not doing this work,
684 * but just pretending that `d' is now `e', etc. this works
685 * really well and obviates the need for a temporary variable.
686 * however, we still explicitly perform the rotate action,
687 * since it is cheaper on SPARC to do it once than to have to
688 * do it over and over again.
689 */
690
691 /* round 1 */
692 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(0) + SHA1_CONST(0); /* 0 */
693 b = ROTATE_LEFT(b, 30);
694
695 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(1) + SHA1_CONST(0); /* 1 */
696 a = ROTATE_LEFT(a, 30);
697
698 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(2) + SHA1_CONST(0); /* 2 */
699 e = ROTATE_LEFT(e, 30);
700
701 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(3) + SHA1_CONST(0); /* 3 */
702 d = ROTATE_LEFT(d, 30);
703
704 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(4) + SHA1_CONST(0); /* 4 */
705 c = ROTATE_LEFT(c, 30);
706
707 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(5) + SHA1_CONST(0); /* 5 */
708 b = ROTATE_LEFT(b, 30);
709
710 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(6) + SHA1_CONST(0); /* 6 */
711 a = ROTATE_LEFT(a, 30);
712
713 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(7) + SHA1_CONST(0); /* 7 */
714 e = ROTATE_LEFT(e, 30);
715
716 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(8) + SHA1_CONST(0); /* 8 */
717 d = ROTATE_LEFT(d, 30);
718
719 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(9) + SHA1_CONST(0); /* 9 */
720 c = ROTATE_LEFT(c, 30);
721
722 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(10) + SHA1_CONST(0); /* 10 */
723 b = ROTATE_LEFT(b, 30);
724
725 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(11) + SHA1_CONST(0); /* 11 */
726 a = ROTATE_LEFT(a, 30);
727
728 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(12) + SHA1_CONST(0); /* 12 */
729 e = ROTATE_LEFT(e, 30);
730
731 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(13) + SHA1_CONST(0); /* 13 */
732 d = ROTATE_LEFT(d, 30);
733
734 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(14) + SHA1_CONST(0); /* 14 */
735 c = ROTATE_LEFT(c, 30);
736
737 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(15) + SHA1_CONST(0); /* 15 */
738 b = ROTATE_LEFT(b, 30);
739
740 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 16 */
741 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(0) + SHA1_CONST(0);
742 a = ROTATE_LEFT(a, 30);
743
744 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 17 */
745 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(1) + SHA1_CONST(0);
746 e = ROTATE_LEFT(e, 30);
747
748 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 18 */
749 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(2) + SHA1_CONST(0);
750 d = ROTATE_LEFT(d, 30);
751
752 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 19 */
753 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(3) + SHA1_CONST(0);
754 c = ROTATE_LEFT(c, 30);
755
756 /* round 2 */
757 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 20 */
758 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(4) + SHA1_CONST(1);
759 b = ROTATE_LEFT(b, 30);
760
761 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 21 */
762 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(5) + SHA1_CONST(1);
763 a = ROTATE_LEFT(a, 30);
764
765 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 22 */
766 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(6) + SHA1_CONST(1);
767 e = ROTATE_LEFT(e, 30);
768
769 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 23 */
770 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(7) + SHA1_CONST(1);
771 d = ROTATE_LEFT(d, 30);
772
773 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 24 */
774 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(8) + SHA1_CONST(1);
775 c = ROTATE_LEFT(c, 30);
776
777 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 25 */
778 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(9) + SHA1_CONST(1);
779 b = ROTATE_LEFT(b, 30);
780
781 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 26 */
782 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(10) + SHA1_CONST(1);
783 a = ROTATE_LEFT(a, 30);
784
785 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 27 */
786 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(11) + SHA1_CONST(1);
787 e = ROTATE_LEFT(e, 30);
788
789 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 28 */
790 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(12) + SHA1_CONST(1);
791 d = ROTATE_LEFT(d, 30);
792
793 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 29 */
794 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(13) + SHA1_CONST(1);
795 c = ROTATE_LEFT(c, 30);
796
797 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 30 */
798 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(14) + SHA1_CONST(1);
799 b = ROTATE_LEFT(b, 30);
800
801 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 31 */
802 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(15) + SHA1_CONST(1);
803 a = ROTATE_LEFT(a, 30);
804
805 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 32 */
806 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(0) + SHA1_CONST(1);
807 e = ROTATE_LEFT(e, 30);
808
809 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 33 */
810 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(1) + SHA1_CONST(1);
811 d = ROTATE_LEFT(d, 30);
812
813 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 34 */
814 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(2) + SHA1_CONST(1);
815 c = ROTATE_LEFT(c, 30);
816
817 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 35 */
818 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(3) + SHA1_CONST(1);
819 b = ROTATE_LEFT(b, 30);
820
821 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 36 */
822 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(4) + SHA1_CONST(1);
823 a = ROTATE_LEFT(a, 30);
824
825 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 37 */
826 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(5) + SHA1_CONST(1);
827 e = ROTATE_LEFT(e, 30);
828
829 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 38 */
830 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(6) + SHA1_CONST(1);
831 d = ROTATE_LEFT(d, 30);
832
833 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 39 */
834 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(7) + SHA1_CONST(1);
835 c = ROTATE_LEFT(c, 30);
836
837 /* round 3 */
838 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 40 */
839 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(8) + SHA1_CONST(2);
840 b = ROTATE_LEFT(b, 30);
841
842 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 41 */
843 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(9) + SHA1_CONST(2);
844 a = ROTATE_LEFT(a, 30);
845
846 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 42 */
847 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(10) + SHA1_CONST(2);
848 e = ROTATE_LEFT(e, 30);
849
850 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 43 */
851 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(11) + SHA1_CONST(2);
852 d = ROTATE_LEFT(d, 30);
853
854 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 44 */
855 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(12) + SHA1_CONST(2);
856 c = ROTATE_LEFT(c, 30);
857
858 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 45 */
859 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(13) + SHA1_CONST(2);
860 b = ROTATE_LEFT(b, 30);
861
862 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 46 */
863 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(14) + SHA1_CONST(2);
864 a = ROTATE_LEFT(a, 30);
865
866 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 47 */
867 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(15) + SHA1_CONST(2);
868 e = ROTATE_LEFT(e, 30);
869
870 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 48 */
871 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(0) + SHA1_CONST(2);
872 d = ROTATE_LEFT(d, 30);
873
874 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 49 */
875 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(1) + SHA1_CONST(2);
876 c = ROTATE_LEFT(c, 30);
877
878 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 50 */
879 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(2) + SHA1_CONST(2);
880 b = ROTATE_LEFT(b, 30);
881
882 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 51 */
883 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(3) + SHA1_CONST(2);
884 a = ROTATE_LEFT(a, 30);
885
886 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 52 */
887 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(4) + SHA1_CONST(2);
888 e = ROTATE_LEFT(e, 30);
889
890 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 53 */
891 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(5) + SHA1_CONST(2);
892 d = ROTATE_LEFT(d, 30);
893
894 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 54 */
895 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(6) + SHA1_CONST(2);
896 c = ROTATE_LEFT(c, 30);
897
898 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 55 */
899 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(7) + SHA1_CONST(2);
900 b = ROTATE_LEFT(b, 30);
901
902 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 56 */
903 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(8) + SHA1_CONST(2);
904 a = ROTATE_LEFT(a, 30);
905
906 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 57 */
907 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(9) + SHA1_CONST(2);
908 e = ROTATE_LEFT(e, 30);
909
910 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 58 */
911 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(10) + SHA1_CONST(2);
912 d = ROTATE_LEFT(d, 30);
913
914 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 59 */
915 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(11) + SHA1_CONST(2);
916 c = ROTATE_LEFT(c, 30);
917
918 /* round 4 */
919 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 60 */
920 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(12) + SHA1_CONST(3);
921 b = ROTATE_LEFT(b, 30);
922
923 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 61 */
924 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(13) + SHA1_CONST(3);
925 a = ROTATE_LEFT(a, 30);
926
927 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 62 */
928 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(14) + SHA1_CONST(3);
929 e = ROTATE_LEFT(e, 30);
930
931 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 63 */
932 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(15) + SHA1_CONST(3);
933 d = ROTATE_LEFT(d, 30);
934
935 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 64 */
936 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(0) + SHA1_CONST(3);
937 c = ROTATE_LEFT(c, 30);
938
939 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 65 */
940 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(1) + SHA1_CONST(3);
941 b = ROTATE_LEFT(b, 30);
942
943 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 66 */
944 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(2) + SHA1_CONST(3);
945 a = ROTATE_LEFT(a, 30);
946
947 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 67 */
948 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(3) + SHA1_CONST(3);
949 e = ROTATE_LEFT(e, 30);
950
951 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 68 */
952 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(4) + SHA1_CONST(3);
953 d = ROTATE_LEFT(d, 30);
954
955 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 69 */
956 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(5) + SHA1_CONST(3);
957 c = ROTATE_LEFT(c, 30);
958
959 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 70 */
960 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(6) + SHA1_CONST(3);
961 b = ROTATE_LEFT(b, 30);
962
963 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 71 */
964 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(7) + SHA1_CONST(3);
965 a = ROTATE_LEFT(a, 30);
966
967 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 72 */
968 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(8) + SHA1_CONST(3);
969 e = ROTATE_LEFT(e, 30);
970
971 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 73 */
972 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(9) + SHA1_CONST(3);
973 d = ROTATE_LEFT(d, 30);
974
975 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 74 */
976 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(10) + SHA1_CONST(3);
977 c = ROTATE_LEFT(c, 30);
978
979 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 75 */
980 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(11) + SHA1_CONST(3);
981 b = ROTATE_LEFT(b, 30);
982
983 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 76 */
984 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(12) + SHA1_CONST(3);
985 a = ROTATE_LEFT(a, 30);
986
987 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 77 */
988 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(13) + SHA1_CONST(3);
989 e = ROTATE_LEFT(e, 30);
990
991 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 78 */
992 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(14) + SHA1_CONST(3);
993 d = ROTATE_LEFT(d, 30);
994
995 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 79 */
996
997 ctx->state[0] += ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(15) +
998 SHA1_CONST(3);
999 ctx->state[1] += b;
1000 ctx->state[2] += ROTATE_LEFT(c, 30);
1001 ctx->state[3] += d;
1002 ctx->state[4] += e;
1003
1004 /* zeroize sensitive information */
1005 W(0) = W(1) = W(2) = W(3) = W(4) = W(5) = W(6) = W(7) = W(8) = 0;
1006 W(9) = W(10) = W(11) = W(12) = W(13) = W(14) = W(15) = 0;
1007 }
1008 #endif /* !__amd64 */
1009
1010
1011 /*
1012 * Encode()
1013 *
1014 * purpose: to convert a list of numbers from little endian to big endian
1015 * input: uint8_t * : place to store the converted big endian numbers
1016 * uint32_t * : place to get numbers to convert from
1017 * size_t : the length of the input in bytes
1018 * output: void
1019 */
1020
1021 static void
1022 Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
1023 size_t len)
1024 {
1025 size_t i, j;
1026
1027 #if defined(__sparc)
1028 if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
1029 for (i = 0, j = 0; j < len; i++, j += 4) {
1030 /* LINTED: pointer alignment */
1031 *((uint32_t *)(output + j)) = input[i];
1032 }
1033 } else {
1034 #endif /* little endian -- will work on big endian, but slowly */
1035 for (i = 0, j = 0; j < len; i++, j += 4) {
1036 output[j] = (input[i] >> 24) & 0xff;
1037 output[j + 1] = (input[i] >> 16) & 0xff;
1038 output[j + 2] = (input[i] >> 8) & 0xff;
1039 output[j + 3] = input[i] & 0xff;
1040 }
1041 #if defined(__sparc)
1042 }
1043 #endif
1044 }