xref: /titanic_41/usr/src/common/crypto/sha1/sha1.c (revision e65d07ee00fa1bfa9e59aee139779af01e5bb432)
17c478bd9Sstevel@tonic-gate /*
28de5c4f4SDan OpenSolaris Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * The basic framework for this code came from the reference
87c478bd9Sstevel@tonic-gate  * implementation for MD5.  That implementation is Copyright (C)
97c478bd9Sstevel@tonic-gate  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
127c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
137c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
147c478bd9Sstevel@tonic-gate  * or this function.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
177c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
187c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
197c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
227c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
237c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
247c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
277c478bd9Sstevel@tonic-gate  * documentation and/or software.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1
304b56a003SDaniel Anderson  * standard, available at http://www.itl.nist.gov/fipspubs/fip180-1.htm
317c478bd9Sstevel@tonic-gate  * Not as fast as one would like -- further optimizations are encouraged
327c478bd9Sstevel@tonic-gate  * and appreciated.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
35*e65d07eeSKeith Wesolowski #if !defined(_KERNEL) && !defined(_BOOT)
368de5c4f4SDan OpenSolaris Anderson #include <stdint.h>
378de5c4f4SDan OpenSolaris Anderson #include <strings.h>
388de5c4f4SDan OpenSolaris Anderson #include <stdlib.h>
398de5c4f4SDan OpenSolaris Anderson #include <errno.h>
408de5c4f4SDan OpenSolaris Anderson #include <sys/systeminfo.h>
41*e65d07eeSKeith Wesolowski #endif  /* !_KERNEL && !_BOOT */
428de5c4f4SDan OpenSolaris Anderson 
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
477c478bd9Sstevel@tonic-gate #include <sys/sha1.h>
487c478bd9Sstevel@tonic-gate #include <sys/sha1_consts.h>
497c478bd9Sstevel@tonic-gate 
504b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN
514b56a003SDaniel Anderson #include <sys/byteorder.h>
524b56a003SDaniel Anderson #define	HAVE_HTONL
534b56a003SDaniel Anderson #endif
544b56a003SDaniel Anderson 
55*e65d07eeSKeith Wesolowski #ifdef	_BOOT
56*e65d07eeSKeith Wesolowski #define	bcopy(_s, _d, _l)	((void) memcpy((_d), (_s), (_l)))
57*e65d07eeSKeith Wesolowski #define	bzero(_m, _l)		((void) memset((_m), 0, (_l)))
58*e65d07eeSKeith Wesolowski #endif
59*e65d07eeSKeith Wesolowski 
60734b6a94Sdarrenm static void Encode(uint8_t *, const uint32_t *, size_t);
61734b6a94Sdarrenm 
62734b6a94Sdarrenm #if	defined(__sparc)
63734b6a94Sdarrenm 
64734b6a94Sdarrenm #define	SHA1_TRANSFORM(ctx, in) \
65734b6a94Sdarrenm 	SHA1Transform((ctx)->state[0], (ctx)->state[1], (ctx)->state[2], \
66734b6a94Sdarrenm 		(ctx)->state[3], (ctx)->state[4], (ctx), (in))
67734b6a94Sdarrenm 
687c478bd9Sstevel@tonic-gate static void SHA1Transform(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
697c478bd9Sstevel@tonic-gate     SHA1_CTX *, const uint8_t *);
707c478bd9Sstevel@tonic-gate 
71321502cdSda73024 #elif	defined(__amd64)
72321502cdSda73024 
73321502cdSda73024 #define	SHA1_TRANSFORM(ctx, in) sha1_block_data_order((ctx), (in), 1)
74321502cdSda73024 #define	SHA1_TRANSFORM_BLOCKS(ctx, in, num) sha1_block_data_order((ctx), \
75321502cdSda73024 		(in), (num))
76321502cdSda73024 
77321502cdSda73024 void sha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t num_blocks);
78321502cdSda73024 
79734b6a94Sdarrenm #else
80734b6a94Sdarrenm 
81734b6a94Sdarrenm #define	SHA1_TRANSFORM(ctx, in) SHA1Transform((ctx), (in))
82734b6a94Sdarrenm 
83734b6a94Sdarrenm static void SHA1Transform(SHA1_CTX *, const uint8_t *);
84734b6a94Sdarrenm 
85734b6a94Sdarrenm #endif
86734b6a94Sdarrenm 
87734b6a94Sdarrenm 
887c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * F, G, and H are the basic SHA1 functions.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
947c478bd9Sstevel@tonic-gate #define	G(b, c, d)	((b) ^ (c) ^ (d))
95734b6a94Sdarrenm #define	H(b, c, d)	(((b) & (c)) | (((b)|(c)) & (d)))
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * ROTATE_LEFT rotates x left n bits.
997c478bd9Sstevel@tonic-gate  */
100734b6a94Sdarrenm 
101734b6a94Sdarrenm #if	defined(__GNUC__) && defined(_LP64)
102734b6a94Sdarrenm static __inline__ uint64_t
ROTATE_LEFT(uint64_t value,uint32_t n)103734b6a94Sdarrenm ROTATE_LEFT(uint64_t value, uint32_t n)
104734b6a94Sdarrenm {
105734b6a94Sdarrenm 	uint32_t t32;
106734b6a94Sdarrenm 
107734b6a94Sdarrenm 	t32 = (uint32_t)value;
108734b6a94Sdarrenm 	return ((t32 << n) | (t32 >> (32 - n)));
109734b6a94Sdarrenm }
110734b6a94Sdarrenm 
111734b6a94Sdarrenm #else
112734b6a94Sdarrenm 
1137c478bd9Sstevel@tonic-gate #define	ROTATE_LEFT(x, n)	\
1147c478bd9Sstevel@tonic-gate 	(((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n))))
1157c478bd9Sstevel@tonic-gate 
116734b6a94Sdarrenm #endif
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * SHA1Init()
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * purpose: initializes the sha1 context and begins and sha1 digest operation
1237c478bd9Sstevel@tonic-gate  *   input: SHA1_CTX *	: the context to initializes.
1247c478bd9Sstevel@tonic-gate  *  output: void
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate void
SHA1Init(SHA1_CTX * ctx)1287c478bd9Sstevel@tonic-gate SHA1Init(SHA1_CTX *ctx)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	ctx->count[0] = ctx->count[1] = 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * load magic initialization constants. Tell lint
1347c478bd9Sstevel@tonic-gate 	 * that these constants are unsigned by using U.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	ctx->state[0] = 0x67452301U;
1387c478bd9Sstevel@tonic-gate 	ctx->state[1] = 0xefcdab89U;
1397c478bd9Sstevel@tonic-gate 	ctx->state[2] = 0x98badcfeU;
1407c478bd9Sstevel@tonic-gate 	ctx->state[3] = 0x10325476U;
1417c478bd9Sstevel@tonic-gate 	ctx->state[4] = 0xc3d2e1f0U;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #ifdef VIS_SHA1
1457c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #include <sys/regset.h>
1487c478bd9Sstevel@tonic-gate #include <sys/vis.h>
14915b9cbbcSkrishna #include <sys/fpu/fpusystm.h>
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* the alignment for block stores to save fp registers */
1527c478bd9Sstevel@tonic-gate #define	VIS_ALIGN	(64)
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate extern int sha1_savefp(kfpu_t *, int);
1557c478bd9Sstevel@tonic-gate extern void sha1_restorefp(kfpu_t *);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate uint32_t	vis_sha1_svfp_threshold = 128;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * VIS SHA-1 consts.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static uint64_t VIS[] = {
1654cc1ac68Skrishna 	0x8000000080000000ULL,
1664cc1ac68Skrishna 	0x0002000200020002ULL,
1674cc1ac68Skrishna 	0x5a8279996ed9eba1ULL,
1684cc1ac68Skrishna 	0x8f1bbcdcca62c1d6ULL,
1694cc1ac68Skrishna 	0x012389ab456789abULL};
1707c478bd9Sstevel@tonic-gate 
171734b6a94Sdarrenm extern void SHA1TransformVIS(uint64_t *, uint32_t *, uint32_t *, uint64_t *);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * SHA1Update()
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * purpose: continues an sha1 digest operation, using the message block
1787c478bd9Sstevel@tonic-gate  *          to update the context.
1797c478bd9Sstevel@tonic-gate  *   input: SHA1_CTX *	: the context to update
180734b6a94Sdarrenm  *          void *	: the message block
181734b6a94Sdarrenm  *          size_t    : the length of the message block in bytes
1827c478bd9Sstevel@tonic-gate  *  output: void
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate void
SHA1Update(SHA1_CTX * ctx,const void * inptr,size_t input_len)186734b6a94Sdarrenm SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	uint32_t i, buf_index, buf_len;
1897c478bd9Sstevel@tonic-gate 	uint64_t X0[40], input64[8];
190734b6a94Sdarrenm 	const uint8_t *input = inptr;
19115b9cbbcSkrishna #ifdef _KERNEL
19215b9cbbcSkrishna 	int usevis = 0;
193734b6a94Sdarrenm #else
194734b6a94Sdarrenm 	int usevis = 1;
19515b9cbbcSkrishna #endif /* _KERNEL */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* check for noop */
1987c478bd9Sstevel@tonic-gate 	if (input_len == 0)
1997c478bd9Sstevel@tonic-gate 		return;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/* compute number of bytes mod 64 */
2027c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[1] >> 3) & 0x3F;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* update number of bits */
2057c478bd9Sstevel@tonic-gate 	if ((ctx->count[1] += (input_len << 3)) < (input_len << 3))
2067c478bd9Sstevel@tonic-gate 		ctx->count[0]++;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	ctx->count[0] += (input_len >> 29);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
2137c478bd9Sstevel@tonic-gate 	i = 0;
2147c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
2157c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2167c478bd9Sstevel@tonic-gate 		kfpu_t *fpu;
217734b6a94Sdarrenm 		if (fpu_exists) {
218734b6a94Sdarrenm 			uint8_t fpua[sizeof (kfpu_t) + GSR_SIZE + VIS_ALIGN];
2197c478bd9Sstevel@tonic-gate 			uint32_t len = (input_len + buf_index) & ~0x3f;
2207c478bd9Sstevel@tonic-gate 			int svfp_ok;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 			fpu = (kfpu_t *)P2ROUNDUP((uintptr_t)fpua, 64);
2237c478bd9Sstevel@tonic-gate 			svfp_ok = ((len >= vis_sha1_svfp_threshold) ? 1 : 0);
22415b9cbbcSkrishna 			usevis = fpu_exists && sha1_savefp(fpu, svfp_ok);
225734b6a94Sdarrenm 		} else {
226734b6a94Sdarrenm 			usevis = 0;
227734b6a94Sdarrenm 		}
2287c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		/*
2317c478bd9Sstevel@tonic-gate 		 * general optimization:
2327c478bd9Sstevel@tonic-gate 		 *
2337c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA1Transform() if
2347c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
2357c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
2367c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
2377c478bd9Sstevel@tonic-gate 		 * SHA1Update().
2387c478bd9Sstevel@tonic-gate 		 */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		if (buf_index) {
2417c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
2427c478bd9Sstevel@tonic-gate 			if (usevis) {
2437c478bd9Sstevel@tonic-gate 				SHA1TransformVIS(X0,
244734b6a94Sdarrenm 				    ctx->buf_un.buf32,
2457c478bd9Sstevel@tonic-gate 				    &ctx->state[0], VIS);
2467c478bd9Sstevel@tonic-gate 			} else {
247734b6a94Sdarrenm 				SHA1_TRANSFORM(ctx, ctx->buf_un.buf8);
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			i = buf_len;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		/*
2537c478bd9Sstevel@tonic-gate 		 * VIS SHA-1: uses the VIS 1.0 instructions to accelerate
2547c478bd9Sstevel@tonic-gate 		 * SHA-1 processing. This is achieved by "offloading" the
2557c478bd9Sstevel@tonic-gate 		 * computation of the message schedule (MS) to the VIS units.
2567c478bd9Sstevel@tonic-gate 		 * This allows the VIS computation of the message schedule
2577c478bd9Sstevel@tonic-gate 		 * to be performed in parallel with the standard integer
2587c478bd9Sstevel@tonic-gate 		 * processing of the remainder of the SHA-1 computation.
2597c478bd9Sstevel@tonic-gate 		 * performance by up to around 1.37X, compared to an optimized
2607c478bd9Sstevel@tonic-gate 		 * integer-only implementation.
2617c478bd9Sstevel@tonic-gate 		 *
2627c478bd9Sstevel@tonic-gate 		 * The VIS implementation of SHA1Transform has a different API
2637c478bd9Sstevel@tonic-gate 		 * to the standard integer version:
2647c478bd9Sstevel@tonic-gate 		 *
2657c478bd9Sstevel@tonic-gate 		 * void SHA1TransformVIS(
2667c478bd9Sstevel@tonic-gate 		 *	 uint64_t *, // Pointer to MS for ith block
267734b6a94Sdarrenm 		 *	 uint32_t *, // Pointer to ith block of message data
2687c478bd9Sstevel@tonic-gate 		 *	 uint32_t *, // Pointer to SHA state i.e ctx->state
2697c478bd9Sstevel@tonic-gate 		 *	 uint64_t *, // Pointer to various VIS constants
2707c478bd9Sstevel@tonic-gate 		 * )
2717c478bd9Sstevel@tonic-gate 		 *
2727c478bd9Sstevel@tonic-gate 		 * Note: the message data must by 4-byte aligned.
2737c478bd9Sstevel@tonic-gate 		 *
2747c478bd9Sstevel@tonic-gate 		 * Function requires VIS 1.0 support.
2757c478bd9Sstevel@tonic-gate 		 *
2767c478bd9Sstevel@tonic-gate 		 * Handling is provided to deal with arbitrary byte alingment
2777c478bd9Sstevel@tonic-gate 		 * of the input data but the performance gains are reduced
2787c478bd9Sstevel@tonic-gate 		 * for alignments other than 4-bytes.
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		if (usevis) {
281734b6a94Sdarrenm 			if (!IS_P2ALIGNED(&input[i], sizeof (uint32_t))) {
2827c478bd9Sstevel@tonic-gate 				/*
2837c478bd9Sstevel@tonic-gate 				 * Main processing loop - input misaligned
2847c478bd9Sstevel@tonic-gate 				 */
2857c478bd9Sstevel@tonic-gate 				for (; i + 63 < input_len; i += 64) {
2867c478bd9Sstevel@tonic-gate 					bcopy(&input[i], input64, 64);
287321502cdSda73024 					SHA1TransformVIS(X0,
288321502cdSda73024 					    (uint32_t *)input64,
2897c478bd9Sstevel@tonic-gate 					    &ctx->state[0], VIS);
2907c478bd9Sstevel@tonic-gate 				}
2917c478bd9Sstevel@tonic-gate 			} else {
2927c478bd9Sstevel@tonic-gate 				/*
2937c478bd9Sstevel@tonic-gate 				 * Main processing loop - input 8-byte aligned
2947c478bd9Sstevel@tonic-gate 				 */
2957c478bd9Sstevel@tonic-gate 				for (; i + 63 < input_len; i += 64) {
2967c478bd9Sstevel@tonic-gate 					SHA1TransformVIS(X0,
297734b6a94Sdarrenm 					    /* LINTED E_BAD_PTR_CAST_ALIGN */
2984b56a003SDaniel Anderson 					    (uint32_t *)&input[i], /* CSTYLED */
2997c478bd9Sstevel@tonic-gate 					    &ctx->state[0], VIS);
3007c478bd9Sstevel@tonic-gate 				}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate #ifdef _KERNEL
3047c478bd9Sstevel@tonic-gate 			sha1_restorefp(fpu);
3057c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
3067c478bd9Sstevel@tonic-gate 		} else {
3077c478bd9Sstevel@tonic-gate 			for (; i + 63 < input_len; i += 64) {
308734b6a94Sdarrenm 				SHA1_TRANSFORM(ctx, &input[i]);
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		/*
3137c478bd9Sstevel@tonic-gate 		 * general optimization:
3147c478bd9Sstevel@tonic-gate 		 *
3157c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
3167c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
3177c478bd9Sstevel@tonic-gate 		 * will be an expensive nop.
3187c478bd9Sstevel@tonic-gate 		 */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		if (input_len == i)
3217c478bd9Sstevel@tonic-gate 			return;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		buf_index = 0;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
3277c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate #else /* VIS_SHA1 */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate void
SHA1Update(SHA1_CTX * ctx,const void * inptr,size_t input_len)333734b6a94Sdarrenm SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	uint32_t i, buf_index, buf_len;
336734b6a94Sdarrenm 	const uint8_t *input = inptr;
337321502cdSda73024 #if defined(__amd64)
338321502cdSda73024 	uint32_t	block_count;
339321502cdSda73024 #endif	/* __amd64 */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* check for noop */
3427c478bd9Sstevel@tonic-gate 	if (input_len == 0)
3437c478bd9Sstevel@tonic-gate 		return;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/* compute number of bytes mod 64 */
3467c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[1] >> 3) & 0x3F;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* update number of bits */
3497c478bd9Sstevel@tonic-gate 	if ((ctx->count[1] += (input_len << 3)) < (input_len << 3))
3507c478bd9Sstevel@tonic-gate 		ctx->count[0]++;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	ctx->count[0] += (input_len >> 29);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
3577c478bd9Sstevel@tonic-gate 	i = 0;
3587c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		/*
3617c478bd9Sstevel@tonic-gate 		 * general optimization:
3627c478bd9Sstevel@tonic-gate 		 *
3637c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA1Transform() if
3647c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
3657c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
3667c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
3677c478bd9Sstevel@tonic-gate 		 * SHA1Update().
3687c478bd9Sstevel@tonic-gate 		 */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		if (buf_index) {
3717c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
372734b6a94Sdarrenm 			SHA1_TRANSFORM(ctx, ctx->buf_un.buf8);
3737c478bd9Sstevel@tonic-gate 			i = buf_len;
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 
376321502cdSda73024 #if !defined(__amd64)
3777c478bd9Sstevel@tonic-gate 		for (; i + 63 < input_len; i += 64)
378734b6a94Sdarrenm 			SHA1_TRANSFORM(ctx, &input[i]);
379321502cdSda73024 #else
380321502cdSda73024 		block_count = (input_len - i) >> 6;
381321502cdSda73024 		if (block_count > 0) {
382321502cdSda73024 			SHA1_TRANSFORM_BLOCKS(ctx, &input[i], block_count);
383321502cdSda73024 			i += block_count << 6;
384321502cdSda73024 		}
385321502cdSda73024 #endif	/* !__amd64 */
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		/*
3887c478bd9Sstevel@tonic-gate 		 * general optimization:
3897c478bd9Sstevel@tonic-gate 		 *
3907c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
3917c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
3927c478bd9Sstevel@tonic-gate 		 * will be an expensive nop.
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		if (input_len == i)
3967c478bd9Sstevel@tonic-gate 			return;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		buf_index = 0;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
4027c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate #endif /* VIS_SHA1 */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * SHA1Final()
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  * purpose: ends an sha1 digest operation, finalizing the message digest and
4117c478bd9Sstevel@tonic-gate  *          zeroing the context.
412321502cdSda73024  *   input: uchar_t *	: A buffer to store the digest.
4135151fb12Sdarrenm  *			: The function actually uses void* because many
4145151fb12Sdarrenm  *			: callers pass things other than uchar_t here.
4157c478bd9Sstevel@tonic-gate  *          SHA1_CTX *  : the context to finalize, save, and zero
4167c478bd9Sstevel@tonic-gate  *  output: void
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate void
SHA1Final(void * digest,SHA1_CTX * ctx)420734b6a94Sdarrenm SHA1Final(void *digest, SHA1_CTX *ctx)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be[sizeof (ctx->count)];
4237c478bd9Sstevel@tonic-gate 	uint32_t	index = (ctx->count[1] >> 3) & 0x3f;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* store bit count, big endian */
4267c478bd9Sstevel@tonic-gate 	Encode(bitcount_be, ctx->count, sizeof (bitcount_be));
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* pad out to 56 mod 64 */
4297c478bd9Sstevel@tonic-gate 	SHA1Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* append length (before padding) */
4327c478bd9Sstevel@tonic-gate 	SHA1Update(ctx, bitcount_be, sizeof (bitcount_be));
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* store state in digest */
4357c478bd9Sstevel@tonic-gate 	Encode(digest, ctx->state, sizeof (ctx->state));
436673007c6Sdarrenm 
437673007c6Sdarrenm 	/* zeroize sensitive information */
438673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
441321502cdSda73024 
442321502cdSda73024 #if !defined(__amd64)
443321502cdSda73024 
444734b6a94Sdarrenm typedef uint32_t sha1word;
445734b6a94Sdarrenm 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * sparc optimization:
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * on the sparc, we can load big endian 32-bit data easily.  note that
4507c478bd9Sstevel@tonic-gate  * special care must be taken to ensure the address is 32-bit aligned.
4517c478bd9Sstevel@tonic-gate  * in the interest of speed, we don't check to make sure, since
4527c478bd9Sstevel@tonic-gate  * careful programming can guarantee this for us.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
4567c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	(*(uint32_t *)(addr))
4577c478bd9Sstevel@tonic-gate 
4584b56a003SDaniel Anderson #elif	defined(HAVE_HTONL)
4594b56a003SDaniel Anderson #define	LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
4607c478bd9Sstevel@tonic-gate 
4614b56a003SDaniel Anderson #else
462734b6a94Sdarrenm /* little endian -- will work on big endian, but slowly */
4637c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	\
4647c478bd9Sstevel@tonic-gate 	(((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
4654b56a003SDaniel Anderson #endif	/* _BIG_ENDIAN */
466734b6a94Sdarrenm 
467734b6a94Sdarrenm /*
468734b6a94Sdarrenm  * SHA1Transform()
469734b6a94Sdarrenm  */
470734b6a94Sdarrenm #if	defined(W_ARRAY)
471734b6a94Sdarrenm #define	W(n) w[n]
472734b6a94Sdarrenm #else	/* !defined(W_ARRAY) */
473734b6a94Sdarrenm #define	W(n) w_ ## n
474734b6a94Sdarrenm #endif	/* !defined(W_ARRAY) */
475734b6a94Sdarrenm 
476734b6a94Sdarrenm 
477734b6a94Sdarrenm #if	defined(__sparc)
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * sparc register window optimization:
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * `a', `b', `c', `d', and `e' are passed into SHA1Transform
4837c478bd9Sstevel@tonic-gate  * explicitly since it increases the number of registers available to
4847c478bd9Sstevel@tonic-gate  * the compiler.  under this scheme, these variables can be held in
4857c478bd9Sstevel@tonic-gate  * %i0 - %i4, which leaves more local and out registers available.
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * purpose: sha1 transformation -- updates the digest based on `block'
4887c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
4897c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
4907c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
4917c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
4927c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 16 - 20 of the digest
4937c478bd9Sstevel@tonic-gate  *          SHA1_CTX *	: the context to update
4947c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
4957c478bd9Sstevel@tonic-gate  *  output: void
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate void
SHA1Transform(uint32_t a,uint32_t b,uint32_t c,uint32_t d,uint32_t e,SHA1_CTX * ctx,const uint8_t blk[64])4997c478bd9Sstevel@tonic-gate SHA1Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e,
5007c478bd9Sstevel@tonic-gate     SHA1_CTX *ctx, const uint8_t blk[64])
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * sparc optimization:
5047c478bd9Sstevel@tonic-gate 	 *
5057c478bd9Sstevel@tonic-gate 	 * while it is somewhat counter-intuitive, on sparc, it is
5067c478bd9Sstevel@tonic-gate 	 * more efficient to place all the constants used in this
5077c478bd9Sstevel@tonic-gate 	 * function in an array and load the values out of the array
5087c478bd9Sstevel@tonic-gate 	 * than to manually load the constants.  this is because
5097c478bd9Sstevel@tonic-gate 	 * setting a register to a 32-bit value takes two ops in most
5107c478bd9Sstevel@tonic-gate 	 * cases: a `sethi' and an `or', but loading a 32-bit value
5117c478bd9Sstevel@tonic-gate 	 * from memory only takes one `ld' (or `lduw' on v9).  while
5127c478bd9Sstevel@tonic-gate 	 * this increases memory usage, the compiler can find enough
5137c478bd9Sstevel@tonic-gate 	 * other things to do while waiting to keep the pipeline does
5147c478bd9Sstevel@tonic-gate 	 * not stall.  additionally, it is likely that many of these
5157c478bd9Sstevel@tonic-gate 	 * constants are cached so that later accesses do not even go
5167c478bd9Sstevel@tonic-gate 	 * out to the bus.
5177c478bd9Sstevel@tonic-gate 	 *
5187c478bd9Sstevel@tonic-gate 	 * this array is declared `static' to keep the compiler from
5197c478bd9Sstevel@tonic-gate 	 * having to bcopy() this array onto the stack frame of
5207c478bd9Sstevel@tonic-gate 	 * SHA1Transform() each time it is called -- which is
5217c478bd9Sstevel@tonic-gate 	 * unacceptably expensive.
5227c478bd9Sstevel@tonic-gate 	 *
5237c478bd9Sstevel@tonic-gate 	 * the `const' is to ensure that callers are good citizens and
5247c478bd9Sstevel@tonic-gate 	 * do not try to munge the array.  since these routines are
5257c478bd9Sstevel@tonic-gate 	 * going to be called from inside multithreaded kernelland,
5267c478bd9Sstevel@tonic-gate 	 * this is a good safety check. -- `sha1_consts' will end up in
5277c478bd9Sstevel@tonic-gate 	 * .rodata.
5287c478bd9Sstevel@tonic-gate 	 *
5297c478bd9Sstevel@tonic-gate 	 * unfortunately, loading from an array in this manner hurts
5304b56a003SDaniel Anderson 	 * performance under Intel.  So, there is a macro,
5317c478bd9Sstevel@tonic-gate 	 * SHA1_CONST(), used in SHA1Transform(), that either expands to
5327c478bd9Sstevel@tonic-gate 	 * a reference to this array, or to the actual constant,
5337c478bd9Sstevel@tonic-gate 	 * depending on what platform this code is compiled for.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	static const uint32_t sha1_consts[] = {
5374b56a003SDaniel Anderson 		SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3
5387c478bd9Sstevel@tonic-gate 	};
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * general optimization:
5427c478bd9Sstevel@tonic-gate 	 *
5437c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
5447c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	uint32_t	w_0, w_1, w_2,  w_3,  w_4,  w_5,  w_6,  w_7;
5487c478bd9Sstevel@tonic-gate 	uint32_t	w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/*
5517c478bd9Sstevel@tonic-gate 	 * sparc optimization:
5527c478bd9Sstevel@tonic-gate 	 *
5537c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use
5547c478bd9Sstevel@tonic-gate 	 * LOAD_BIG_32() directly.  otherwise, bcopy() into a
5557c478bd9Sstevel@tonic-gate 	 * buffer that *is* aligned on a 4-byte boundary and then do
5567c478bd9Sstevel@tonic-gate 	 * the LOAD_BIG_32() on that buffer.  benchmarks have shown
5577c478bd9Sstevel@tonic-gate 	 * that using the bcopy() is better than loading the bytes
5587c478bd9Sstevel@tonic-gate 	 * individually and doing the endian-swap by hand.
5597c478bd9Sstevel@tonic-gate 	 *
5607c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
5617c478bd9Sstevel@tonic-gate 	 *
5627c478bd9Sstevel@tonic-gate 	 * blk = bcopy(ctx->buf_un.buf32, blk, sizeof (ctx->buf_un.buf32));
5637c478bd9Sstevel@tonic-gate 	 *
5647c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_BIG_32()'s, the compiler
5657c478bd9Sstevel@tonic-gate 	 * *does not* like that, so please resist the urge.
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x3) {		/* not 4-byte aligned? */
5697c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
5707c478bd9Sstevel@tonic-gate 		w_15 = LOAD_BIG_32(ctx->buf_un.buf32 + 15);
5717c478bd9Sstevel@tonic-gate 		w_14 = LOAD_BIG_32(ctx->buf_un.buf32 + 14);
5727c478bd9Sstevel@tonic-gate 		w_13 = LOAD_BIG_32(ctx->buf_un.buf32 + 13);
5737c478bd9Sstevel@tonic-gate 		w_12 = LOAD_BIG_32(ctx->buf_un.buf32 + 12);
5747c478bd9Sstevel@tonic-gate 		w_11 = LOAD_BIG_32(ctx->buf_un.buf32 + 11);
5757c478bd9Sstevel@tonic-gate 		w_10 = LOAD_BIG_32(ctx->buf_un.buf32 + 10);
5767c478bd9Sstevel@tonic-gate 		w_9  = LOAD_BIG_32(ctx->buf_un.buf32 +  9);
5777c478bd9Sstevel@tonic-gate 		w_8  = LOAD_BIG_32(ctx->buf_un.buf32 +  8);
5787c478bd9Sstevel@tonic-gate 		w_7  = LOAD_BIG_32(ctx->buf_un.buf32 +  7);
5797c478bd9Sstevel@tonic-gate 		w_6  = LOAD_BIG_32(ctx->buf_un.buf32 +  6);
5807c478bd9Sstevel@tonic-gate 		w_5  = LOAD_BIG_32(ctx->buf_un.buf32 +  5);
5817c478bd9Sstevel@tonic-gate 		w_4  = LOAD_BIG_32(ctx->buf_un.buf32 +  4);
5827c478bd9Sstevel@tonic-gate 		w_3  = LOAD_BIG_32(ctx->buf_un.buf32 +  3);
5837c478bd9Sstevel@tonic-gate 		w_2  = LOAD_BIG_32(ctx->buf_un.buf32 +  2);
5847c478bd9Sstevel@tonic-gate 		w_1  = LOAD_BIG_32(ctx->buf_un.buf32 +  1);
5857c478bd9Sstevel@tonic-gate 		w_0  = LOAD_BIG_32(ctx->buf_un.buf32 +  0);
5867c478bd9Sstevel@tonic-gate 	} else {
5878de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5887c478bd9Sstevel@tonic-gate 		w_15 = LOAD_BIG_32(blk + 60);
5898de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5907c478bd9Sstevel@tonic-gate 		w_14 = LOAD_BIG_32(blk + 56);
5918de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5927c478bd9Sstevel@tonic-gate 		w_13 = LOAD_BIG_32(blk + 52);
5938de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5947c478bd9Sstevel@tonic-gate 		w_12 = LOAD_BIG_32(blk + 48);
5958de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5967c478bd9Sstevel@tonic-gate 		w_11 = LOAD_BIG_32(blk + 44);
5978de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5987c478bd9Sstevel@tonic-gate 		w_10 = LOAD_BIG_32(blk + 40);
5998de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6007c478bd9Sstevel@tonic-gate 		w_9  = LOAD_BIG_32(blk + 36);
6018de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6027c478bd9Sstevel@tonic-gate 		w_8  = LOAD_BIG_32(blk + 32);
6038de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6047c478bd9Sstevel@tonic-gate 		w_7  = LOAD_BIG_32(blk + 28);
6058de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6067c478bd9Sstevel@tonic-gate 		w_6  = LOAD_BIG_32(blk + 24);
6078de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6087c478bd9Sstevel@tonic-gate 		w_5  = LOAD_BIG_32(blk + 20);
6098de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6107c478bd9Sstevel@tonic-gate 		w_4  = LOAD_BIG_32(blk + 16);
6118de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6127c478bd9Sstevel@tonic-gate 		w_3  = LOAD_BIG_32(blk + 12);
6138de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6147c478bd9Sstevel@tonic-gate 		w_2  = LOAD_BIG_32(blk +  8);
6158de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6167c478bd9Sstevel@tonic-gate 		w_1  = LOAD_BIG_32(blk +  4);
6178de5c4f4SDan OpenSolaris Anderson 		/* LINTED E_BAD_PTR_CAST_ALIGN */
6187c478bd9Sstevel@tonic-gate 		w_0  = LOAD_BIG_32(blk +  0);
6197c478bd9Sstevel@tonic-gate 	}
620734b6a94Sdarrenm #else	/* !defined(__sparc) */
621734b6a94Sdarrenm 
6224b56a003SDaniel Anderson void /* CSTYLED */
623734b6a94Sdarrenm SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64])
624734b6a94Sdarrenm {
6254b56a003SDaniel Anderson 	/* CSTYLED */
626734b6a94Sdarrenm 	sha1word a = ctx->state[0];
627734b6a94Sdarrenm 	sha1word b = ctx->state[1];
628734b6a94Sdarrenm 	sha1word c = ctx->state[2];
629734b6a94Sdarrenm 	sha1word d = ctx->state[3];
630734b6a94Sdarrenm 	sha1word e = ctx->state[4];
631734b6a94Sdarrenm 
632734b6a94Sdarrenm #if	defined(W_ARRAY)
633734b6a94Sdarrenm 	sha1word	w[16];
634734b6a94Sdarrenm #else	/* !defined(W_ARRAY) */
635734b6a94Sdarrenm 	sha1word	w_0, w_1, w_2,  w_3,  w_4,  w_5,  w_6,  w_7;
636734b6a94Sdarrenm 	sha1word	w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15;
637734b6a94Sdarrenm #endif	/* !defined(W_ARRAY) */
638734b6a94Sdarrenm 
63925cc6a40SDaniel Anderson 	W(0)  = LOAD_BIG_32((void *)(blk +  0));
64025cc6a40SDaniel Anderson 	W(1)  = LOAD_BIG_32((void *)(blk +  4));
64125cc6a40SDaniel Anderson 	W(2)  = LOAD_BIG_32((void *)(blk +  8));
64225cc6a40SDaniel Anderson 	W(3)  = LOAD_BIG_32((void *)(blk + 12));
64325cc6a40SDaniel Anderson 	W(4)  = LOAD_BIG_32((void *)(blk + 16));
64425cc6a40SDaniel Anderson 	W(5)  = LOAD_BIG_32((void *)(blk + 20));
64525cc6a40SDaniel Anderson 	W(6)  = LOAD_BIG_32((void *)(blk + 24));
64625cc6a40SDaniel Anderson 	W(7)  = LOAD_BIG_32((void *)(blk + 28));
64725cc6a40SDaniel Anderson 	W(8)  = LOAD_BIG_32((void *)(blk + 32));
64825cc6a40SDaniel Anderson 	W(9)  = LOAD_BIG_32((void *)(blk + 36));
64925cc6a40SDaniel Anderson 	W(10) = LOAD_BIG_32((void *)(blk + 40));
65025cc6a40SDaniel Anderson 	W(11) = LOAD_BIG_32((void *)(blk + 44));
65125cc6a40SDaniel Anderson 	W(12) = LOAD_BIG_32((void *)(blk + 48));
65225cc6a40SDaniel Anderson 	W(13) = LOAD_BIG_32((void *)(blk + 52));
65325cc6a40SDaniel Anderson 	W(14) = LOAD_BIG_32((void *)(blk + 56));
65425cc6a40SDaniel Anderson 	W(15) = LOAD_BIG_32((void *)(blk + 60));
655734b6a94Sdarrenm 
656734b6a94Sdarrenm #endif	/* !defined(__sparc) */
657734b6a94Sdarrenm 
6587c478bd9Sstevel@tonic-gate 	/*
6597c478bd9Sstevel@tonic-gate 	 * general optimization:
6607c478bd9Sstevel@tonic-gate 	 *
6617c478bd9Sstevel@tonic-gate 	 * even though this approach is described in the standard as
6627c478bd9Sstevel@tonic-gate 	 * being slower algorithmically, it is 30-40% faster than the
6637c478bd9Sstevel@tonic-gate 	 * "faster" version under SPARC, because this version has more
6647c478bd9Sstevel@tonic-gate 	 * of the constraints specified at compile-time and uses fewer
6657c478bd9Sstevel@tonic-gate 	 * variables (and therefore has better register utilization)
6667c478bd9Sstevel@tonic-gate 	 * than its "speedier" brother.  (i've tried both, trust me)
6677c478bd9Sstevel@tonic-gate 	 *
6687c478bd9Sstevel@tonic-gate 	 * for either method given in the spec, there is an "assignment"
6697c478bd9Sstevel@tonic-gate 	 * phase where the following takes place:
6707c478bd9Sstevel@tonic-gate 	 *
6717c478bd9Sstevel@tonic-gate 	 *	tmp = (main_computation);
6727c478bd9Sstevel@tonic-gate 	 *	e = d; d = c; c = rotate_left(b, 30); b = a; a = tmp;
6737c478bd9Sstevel@tonic-gate 	 *
6747c478bd9Sstevel@tonic-gate 	 * we can make the algorithm go faster by not doing this work,
6757c478bd9Sstevel@tonic-gate 	 * but just pretending that `d' is now `e', etc. this works
6767c478bd9Sstevel@tonic-gate 	 * really well and obviates the need for a temporary variable.
677321502cdSda73024 	 * however, we still explicitly perform the rotate action,
6787c478bd9Sstevel@tonic-gate 	 * since it is cheaper on SPARC to do it once than to have to
6797c478bd9Sstevel@tonic-gate 	 * do it over and over again.
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* round 1 */
683734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(0) + SHA1_CONST(0); /* 0 */
6847c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
6857c478bd9Sstevel@tonic-gate 
686734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(1) + SHA1_CONST(0); /* 1 */
6877c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
6887c478bd9Sstevel@tonic-gate 
689734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(2) + SHA1_CONST(0); /* 2 */
6907c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
6917c478bd9Sstevel@tonic-gate 
692734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(3) + SHA1_CONST(0); /* 3 */
6937c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
6947c478bd9Sstevel@tonic-gate 
695734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(4) + SHA1_CONST(0); /* 4 */
6967c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
6977c478bd9Sstevel@tonic-gate 
698734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(5) + SHA1_CONST(0); /* 5 */
6997c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7007c478bd9Sstevel@tonic-gate 
701734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(6) + SHA1_CONST(0); /* 6 */
7027c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7037c478bd9Sstevel@tonic-gate 
704734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(7) + SHA1_CONST(0); /* 7 */
7057c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7067c478bd9Sstevel@tonic-gate 
707734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(8) + SHA1_CONST(0); /* 8 */
7087c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
7097c478bd9Sstevel@tonic-gate 
710734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(9) + SHA1_CONST(0); /* 9 */
7117c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
7127c478bd9Sstevel@tonic-gate 
713734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(10) + SHA1_CONST(0); /* 10 */
7147c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7157c478bd9Sstevel@tonic-gate 
716734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(11) + SHA1_CONST(0); /* 11 */
7177c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7187c478bd9Sstevel@tonic-gate 
719734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(12) + SHA1_CONST(0); /* 12 */
7207c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7217c478bd9Sstevel@tonic-gate 
722734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(13) + SHA1_CONST(0); /* 13 */
7237c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
7247c478bd9Sstevel@tonic-gate 
725734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(14) + SHA1_CONST(0); /* 14 */
7267c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
7277c478bd9Sstevel@tonic-gate 
728734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(15) + SHA1_CONST(0); /* 15 */
7297c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7307c478bd9Sstevel@tonic-gate 
731734b6a94Sdarrenm 	W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1);		/* 16 */
732734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(0) + SHA1_CONST(0);
7337c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7347c478bd9Sstevel@tonic-gate 
735734b6a94Sdarrenm 	W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1);		/* 17 */
736734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(1) + SHA1_CONST(0);
7377c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7387c478bd9Sstevel@tonic-gate 
739734b6a94Sdarrenm 	W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1);	/* 18 */
740734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(2) + SHA1_CONST(0);
7417c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
7427c478bd9Sstevel@tonic-gate 
743734b6a94Sdarrenm 	W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1);		/* 19 */
744734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(3) + SHA1_CONST(0);
7457c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* round 2 */
748734b6a94Sdarrenm 	W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1);		/* 20 */
749734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(4) + SHA1_CONST(1);
7507c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7517c478bd9Sstevel@tonic-gate 
752734b6a94Sdarrenm 	W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1);		/* 21 */
753734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(5) + SHA1_CONST(1);
7547c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7557c478bd9Sstevel@tonic-gate 
756734b6a94Sdarrenm 	W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1);		/* 22 */
757734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(6) + SHA1_CONST(1);
7587c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7597c478bd9Sstevel@tonic-gate 
760734b6a94Sdarrenm 	W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1);		/* 23 */
761734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(7) + SHA1_CONST(1);
7627c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
7637c478bd9Sstevel@tonic-gate 
764734b6a94Sdarrenm 	W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1);		/* 24 */
765734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(8) + SHA1_CONST(1);
7667c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
7677c478bd9Sstevel@tonic-gate 
768734b6a94Sdarrenm 	W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1);		/* 25 */
769734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(9) + SHA1_CONST(1);
7707c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7717c478bd9Sstevel@tonic-gate 
772734b6a94Sdarrenm 	W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1);	/* 26 */
773734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(10) + SHA1_CONST(1);
7747c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7757c478bd9Sstevel@tonic-gate 
776734b6a94Sdarrenm 	W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1);	/* 27 */
777734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(11) + SHA1_CONST(1);
7787c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7797c478bd9Sstevel@tonic-gate 
780734b6a94Sdarrenm 	W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1);	/* 28 */
781734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(12) + SHA1_CONST(1);
7827c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
7837c478bd9Sstevel@tonic-gate 
784734b6a94Sdarrenm 	W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1);	/* 29 */
785734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(13) + SHA1_CONST(1);
7867c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
7877c478bd9Sstevel@tonic-gate 
788734b6a94Sdarrenm 	W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1);	/* 30 */
789734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(14) + SHA1_CONST(1);
7907c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
7917c478bd9Sstevel@tonic-gate 
792734b6a94Sdarrenm 	W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1);	/* 31 */
793734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(15) + SHA1_CONST(1);
7947c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
7957c478bd9Sstevel@tonic-gate 
796734b6a94Sdarrenm 	W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1);		/* 32 */
797734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(0) + SHA1_CONST(1);
7987c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
7997c478bd9Sstevel@tonic-gate 
800734b6a94Sdarrenm 	W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1);		/* 33 */
801734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(1) + SHA1_CONST(1);
8027c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
8037c478bd9Sstevel@tonic-gate 
804734b6a94Sdarrenm 	W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1);	/* 34 */
805734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(2) + SHA1_CONST(1);
8067c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
8077c478bd9Sstevel@tonic-gate 
808734b6a94Sdarrenm 	W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1);		/* 35 */
809734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(3) + SHA1_CONST(1);
8107c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
8117c478bd9Sstevel@tonic-gate 
812734b6a94Sdarrenm 	W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1);		/* 36 */
813734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(4) + SHA1_CONST(1);
8147c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
8157c478bd9Sstevel@tonic-gate 
816734b6a94Sdarrenm 	W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1);		/* 37 */
817734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(5) + SHA1_CONST(1);
8187c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
8197c478bd9Sstevel@tonic-gate 
820734b6a94Sdarrenm 	W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1);		/* 38 */
821734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(6) + SHA1_CONST(1);
8227c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
8237c478bd9Sstevel@tonic-gate 
824734b6a94Sdarrenm 	W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1);		/* 39 */
825734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(7) + SHA1_CONST(1);
8267c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/* round 3 */
829734b6a94Sdarrenm 	W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1);		/* 40 */
830734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(8) + SHA1_CONST(2);
8317c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
8327c478bd9Sstevel@tonic-gate 
833734b6a94Sdarrenm 	W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1);		/* 41 */
834734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(9) + SHA1_CONST(2);
8357c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
8367c478bd9Sstevel@tonic-gate 
837734b6a94Sdarrenm 	W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1);	/* 42 */
838734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(10) + SHA1_CONST(2);
8397c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
8407c478bd9Sstevel@tonic-gate 
841734b6a94Sdarrenm 	W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1);	/* 43 */
842734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(11) + SHA1_CONST(2);
8437c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
8447c478bd9Sstevel@tonic-gate 
845734b6a94Sdarrenm 	W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1);	/* 44 */
846734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(12) + SHA1_CONST(2);
8477c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
8487c478bd9Sstevel@tonic-gate 
849734b6a94Sdarrenm 	W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1);	/* 45 */
850734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(13) + SHA1_CONST(2);
8517c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
8527c478bd9Sstevel@tonic-gate 
853734b6a94Sdarrenm 	W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1);	/* 46 */
854734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(14) + SHA1_CONST(2);
8557c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
8567c478bd9Sstevel@tonic-gate 
857734b6a94Sdarrenm 	W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1);	/* 47 */
858734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(15) + SHA1_CONST(2);
8597c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
8607c478bd9Sstevel@tonic-gate 
861734b6a94Sdarrenm 	W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1);		/* 48 */
862734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(0) + SHA1_CONST(2);
8637c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
8647c478bd9Sstevel@tonic-gate 
865734b6a94Sdarrenm 	W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1);		/* 49 */
866734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(1) + SHA1_CONST(2);
8677c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
8687c478bd9Sstevel@tonic-gate 
869734b6a94Sdarrenm 	W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1);	/* 50 */
870734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(2) + SHA1_CONST(2);
8717c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
8727c478bd9Sstevel@tonic-gate 
873734b6a94Sdarrenm 	W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1);		/* 51 */
874734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(3) + SHA1_CONST(2);
8757c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
8767c478bd9Sstevel@tonic-gate 
877734b6a94Sdarrenm 	W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1);		/* 52 */
878734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(4) + SHA1_CONST(2);
8797c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
8807c478bd9Sstevel@tonic-gate 
881734b6a94Sdarrenm 	W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1);		/* 53 */
882734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(5) + SHA1_CONST(2);
8837c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
8847c478bd9Sstevel@tonic-gate 
885734b6a94Sdarrenm 	W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1);		/* 54 */
886734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(6) + SHA1_CONST(2);
8877c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
8887c478bd9Sstevel@tonic-gate 
889734b6a94Sdarrenm 	W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1);		/* 55 */
890734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(7) + SHA1_CONST(2);
8917c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
8927c478bd9Sstevel@tonic-gate 
893734b6a94Sdarrenm 	W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1);		/* 56 */
894734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(8) + SHA1_CONST(2);
8957c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
8967c478bd9Sstevel@tonic-gate 
897734b6a94Sdarrenm 	W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1);		/* 57 */
898734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(9) + SHA1_CONST(2);
8997c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
9007c478bd9Sstevel@tonic-gate 
901734b6a94Sdarrenm 	W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1);	/* 58 */
902734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(10) + SHA1_CONST(2);
9037c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
9047c478bd9Sstevel@tonic-gate 
905734b6a94Sdarrenm 	W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1);	/* 59 */
906734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(11) + SHA1_CONST(2);
9077c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/* round 4 */
910734b6a94Sdarrenm 	W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1);	/* 60 */
911734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(12) + SHA1_CONST(3);
9127c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
9137c478bd9Sstevel@tonic-gate 
914734b6a94Sdarrenm 	W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1);	/* 61 */
915734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(13) + SHA1_CONST(3);
9167c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
9177c478bd9Sstevel@tonic-gate 
918734b6a94Sdarrenm 	W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1);	/* 62 */
919734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(14) + SHA1_CONST(3);
9207c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
9217c478bd9Sstevel@tonic-gate 
922734b6a94Sdarrenm 	W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1);	/* 63 */
923734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(15) + SHA1_CONST(3);
9247c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
9257c478bd9Sstevel@tonic-gate 
926734b6a94Sdarrenm 	W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1);		/* 64 */
927734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(0) + SHA1_CONST(3);
9287c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
9297c478bd9Sstevel@tonic-gate 
930734b6a94Sdarrenm 	W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1);		/* 65 */
931734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(1) + SHA1_CONST(3);
9327c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
9337c478bd9Sstevel@tonic-gate 
934734b6a94Sdarrenm 	W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1);	/* 66 */
935734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(2) + SHA1_CONST(3);
9367c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
9377c478bd9Sstevel@tonic-gate 
938734b6a94Sdarrenm 	W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1);		/* 67 */
939734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(3) + SHA1_CONST(3);
9407c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
9417c478bd9Sstevel@tonic-gate 
942734b6a94Sdarrenm 	W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1);		/* 68 */
943734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(4) + SHA1_CONST(3);
9447c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
9457c478bd9Sstevel@tonic-gate 
946734b6a94Sdarrenm 	W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1);		/* 69 */
947734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(5) + SHA1_CONST(3);
9487c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
9497c478bd9Sstevel@tonic-gate 
950734b6a94Sdarrenm 	W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1);		/* 70 */
951734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(6) + SHA1_CONST(3);
9527c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
9537c478bd9Sstevel@tonic-gate 
954734b6a94Sdarrenm 	W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1);		/* 71 */
955734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(7) + SHA1_CONST(3);
9567c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
9577c478bd9Sstevel@tonic-gate 
958734b6a94Sdarrenm 	W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1);		/* 72 */
959734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(8) + SHA1_CONST(3);
9607c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
9617c478bd9Sstevel@tonic-gate 
962734b6a94Sdarrenm 	W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1);		/* 73 */
963734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(9) + SHA1_CONST(3);
9647c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
9657c478bd9Sstevel@tonic-gate 
966734b6a94Sdarrenm 	W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1);	/* 74 */
967734b6a94Sdarrenm 	a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(10) + SHA1_CONST(3);
9687c478bd9Sstevel@tonic-gate 	c = ROTATE_LEFT(c, 30);
9697c478bd9Sstevel@tonic-gate 
970734b6a94Sdarrenm 	W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1);	/* 75 */
971734b6a94Sdarrenm 	e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(11) + SHA1_CONST(3);
9727c478bd9Sstevel@tonic-gate 	b = ROTATE_LEFT(b, 30);
9737c478bd9Sstevel@tonic-gate 
974734b6a94Sdarrenm 	W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1);	/* 76 */
975734b6a94Sdarrenm 	d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(12) + SHA1_CONST(3);
9767c478bd9Sstevel@tonic-gate 	a = ROTATE_LEFT(a, 30);
9777c478bd9Sstevel@tonic-gate 
978734b6a94Sdarrenm 	W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1);	/* 77 */
979734b6a94Sdarrenm 	c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(13) + SHA1_CONST(3);
9807c478bd9Sstevel@tonic-gate 	e = ROTATE_LEFT(e, 30);
9817c478bd9Sstevel@tonic-gate 
982734b6a94Sdarrenm 	W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1);	/* 78 */
983734b6a94Sdarrenm 	b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(14) + SHA1_CONST(3);
9847c478bd9Sstevel@tonic-gate 	d = ROTATE_LEFT(d, 30);
9857c478bd9Sstevel@tonic-gate 
986734b6a94Sdarrenm 	W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1);	/* 79 */
9877c478bd9Sstevel@tonic-gate 
988734b6a94Sdarrenm 	ctx->state[0] += ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(15) +
9897c478bd9Sstevel@tonic-gate 	    SHA1_CONST(3);
9907c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
9917c478bd9Sstevel@tonic-gate 	ctx->state[2] += ROTATE_LEFT(c, 30);
9927c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
9937c478bd9Sstevel@tonic-gate 	ctx->state[4] += e;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	/* zeroize sensitive information */
996734b6a94Sdarrenm 	W(0) = W(1) = W(2) = W(3) = W(4) = W(5) = W(6) = W(7) = W(8) = 0;
997734b6a94Sdarrenm 	W(9) = W(10) = W(11) = W(12) = W(13) = W(14) = W(15) = 0;
9987c478bd9Sstevel@tonic-gate }
999321502cdSda73024 #endif	/* !__amd64 */
1000321502cdSda73024 
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate /*
10037c478bd9Sstevel@tonic-gate  * Encode()
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from little endian to big endian
10067c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted big endian numbers
10077c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
10087c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
10097c478bd9Sstevel@tonic-gate  *  output: void
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate static void
1013734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
1014734b6a94Sdarrenm     size_t len)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	size_t		i, j;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate #if	defined(__sparc)
10197c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
10207c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
10218de5c4f4SDan OpenSolaris Anderson 			/* LINTED E_BAD_PTR_CAST_ALIGN */
10227c478bd9Sstevel@tonic-gate 			*((uint32_t *)(output + j)) = input[i];
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 	} else {
10257c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
10267c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
10277c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 24) & 0xff;
10287c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 16) & 0xff;
10297c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >>  8) & 0xff;
10307c478bd9Sstevel@tonic-gate 			output[j + 3]	= input[i] & 0xff;
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate #if	defined(__sparc)
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate #endif
10357c478bd9Sstevel@tonic-gate }
1036