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