xref: /titanic_44/usr/src/common/crypto/md5/md5.c (revision b085fdc5a221b961f6fae80f83a14bae7f09fe67)
17c478bd9Sstevel@tonic-gate /*
2673007c6Sdarrenm  * Copyright 2006 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  * Cleaned-up and optimized version of MD5, based on the reference
87c478bd9Sstevel@tonic-gate  * implementation provided in RFC 1321.  See RSA Copyright information
97c478bd9Sstevel@tonic-gate  * below.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate /*
157c478bd9Sstevel@tonic-gate  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
207c478bd9Sstevel@tonic-gate  * rights reserved.
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
237c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
247c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
257c478bd9Sstevel@tonic-gate  * or this function.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
287c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
297c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
307c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
337c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
347c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
357c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
387c478bd9Sstevel@tonic-gate  * documentation and/or software.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/md5.h>
437c478bd9Sstevel@tonic-gate #include <sys/md5_consts.h>	/* MD5_CONST() optimization */
44afd1ac7bSwesolows #include "md5_byteswap.h"
457c478bd9Sstevel@tonic-gate #if	!defined(_KERNEL) || defined(_BOOT)
467c478bd9Sstevel@tonic-gate #include <strings.h>
477c478bd9Sstevel@tonic-gate #endif /* !_KERNEL || _BOOT */
487c478bd9Sstevel@tonic-gate 
49734b6a94Sdarrenm #ifdef _KERNEL
507c478bd9Sstevel@tonic-gate #include <sys/systm.h>
51734b6a94Sdarrenm #endif /* _KERNEL */
527c478bd9Sstevel@tonic-gate 
53734b6a94Sdarrenm static void Encode(uint8_t *, const uint32_t *, size_t);
547c478bd9Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
557c478bd9Sstevel@tonic-gate     const uint8_t [64]);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * F, G, H and I are the basic MD5 functions.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
637c478bd9Sstevel@tonic-gate #define	G(b, c, d)	(((b) & (d)) | ((c) & (~d)))
647c478bd9Sstevel@tonic-gate #define	H(b, c, d)	((b) ^ (c) ^ (d))
657c478bd9Sstevel@tonic-gate #define	I(b, c, d)	((c) ^ ((b) | (~d)))
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * ROTATE_LEFT rotates x left n bits.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #define	ROTATE_LEFT(x, n)	\
717c478bd9Sstevel@tonic-gate 	(((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
757c478bd9Sstevel@tonic-gate  * Rotation is separate from addition to prevent recomputation.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	FF(a, b, c, d, x, s, ac) { \
79554ff184Skais 	(a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
807c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
817c478bd9Sstevel@tonic-gate 	(a) += (b); \
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	GG(a, b, c, d, x, s, ac) { \
85554ff184Skais 	(a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
867c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
877c478bd9Sstevel@tonic-gate 	(a) += (b); \
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	HH(a, b, c, d, x, s, ac) { \
91554ff184Skais 	(a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
927c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
937c478bd9Sstevel@tonic-gate 	(a) += (b); \
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	II(a, b, c, d, x, s, ac) { \
97554ff184Skais 	(a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
987c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
997c478bd9Sstevel@tonic-gate 	(a) += (b); \
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Loading 32-bit constants on a RISC is expensive since it involves both a
1047c478bd9Sstevel@tonic-gate  * `sethi' and an `or'.  thus, we instead have the compiler generate `ld's to
1057c478bd9Sstevel@tonic-gate  * load the constants from an array called `md5_consts'.  however, on intel
1067c478bd9Sstevel@tonic-gate  * (and other CISC processors), it is cheaper to load the constant
1077c478bd9Sstevel@tonic-gate  * directly.  thus, the c code in MD5Transform() uses the macro MD5_CONST()
1087c478bd9Sstevel@tonic-gate  * which either expands to a constant or an array reference, depending on the
1097c478bd9Sstevel@tonic-gate  * architecture the code is being compiled for.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * Right now, i386 and amd64 are the CISC exceptions.
1127c478bd9Sstevel@tonic-gate  * If we get another CISC ISA, we'll have to change the ifdef.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	MD5_CONST(x)		(MD5_CONST_ ## x)
118554ff184Skais #define	MD5_CONST_e(x)		MD5_CONST(x)
119554ff184Skais #define	MD5_CONST_o(x)		MD5_CONST(x)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #else
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * sparc/RISC optimization:
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
1267c478bd9Sstevel@tonic-gate  * machines), it is more efficient to place all the constants used in this
1277c478bd9Sstevel@tonic-gate  * function in an array and load the values out of the array than to manually
1287c478bd9Sstevel@tonic-gate  * load the constants.  this is because setting a register to a 32-bit value
1297c478bd9Sstevel@tonic-gate  * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
1307c478bd9Sstevel@tonic-gate  * value from memory only takes one `ld' (or `lduw' on v9).  while this
1317c478bd9Sstevel@tonic-gate  * increases memory usage, the compiler can find enough other things to do
1327c478bd9Sstevel@tonic-gate  * while waiting to keep the pipeline does not stall.  additionally, it is
1337c478bd9Sstevel@tonic-gate  * likely that many of these constants are cached so that later accesses do
1347c478bd9Sstevel@tonic-gate  * not even go out to the bus.
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * this array is declared `static' to keep the compiler from having to
1377c478bd9Sstevel@tonic-gate  * bcopy() this array onto the stack frame of MD5Transform() each time it is
1387c478bd9Sstevel@tonic-gate  * called -- which is unacceptably expensive.
1397c478bd9Sstevel@tonic-gate  *
1407c478bd9Sstevel@tonic-gate  * the `const' is to ensure that callers are good citizens and do not try to
1417c478bd9Sstevel@tonic-gate  * munge the array.  since these routines are going to be called from inside
1427c478bd9Sstevel@tonic-gate  * multithreaded kernelland, this is a good safety check. -- `constants' will
1437c478bd9Sstevel@tonic-gate  * end up in .rodata.
1447c478bd9Sstevel@tonic-gate  *
1457c478bd9Sstevel@tonic-gate  * unfortunately, loading from an array in this manner hurts performance under
1467c478bd9Sstevel@tonic-gate  * intel (and presumably other CISC machines).  so, there is a macro,
1477c478bd9Sstevel@tonic-gate  * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
1487c478bd9Sstevel@tonic-gate  * this array, or to the actual constant, depending on what platform this code
1497c478bd9Sstevel@tonic-gate  * is compiled for.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate 
152554ff184Skais #ifdef sun4v
153554ff184Skais 
154554ff184Skais /*
155554ff184Skais  * Going to load these consts in 8B chunks, so need to enforce 8B alignment
156554ff184Skais  */
157554ff184Skais 
158554ff184Skais /* CSTYLED */
159554ff184Skais #pragma align 64 (md5_consts)
160*b085fdc5Sfr80241 #define	_MD5_CHECK_ALIGNMENT
161554ff184Skais 
162554ff184Skais #endif /* sun4v */
163554ff184Skais 
1647c478bd9Sstevel@tonic-gate static const uint32_t md5_consts[] = {
1657c478bd9Sstevel@tonic-gate 	MD5_CONST_0,	MD5_CONST_1,	MD5_CONST_2,	MD5_CONST_3,
1667c478bd9Sstevel@tonic-gate 	MD5_CONST_4,	MD5_CONST_5,	MD5_CONST_6,	MD5_CONST_7,
1677c478bd9Sstevel@tonic-gate 	MD5_CONST_8,	MD5_CONST_9,	MD5_CONST_10,	MD5_CONST_11,
1687c478bd9Sstevel@tonic-gate 	MD5_CONST_12,	MD5_CONST_13,	MD5_CONST_14,	MD5_CONST_15,
1697c478bd9Sstevel@tonic-gate 	MD5_CONST_16,	MD5_CONST_17,	MD5_CONST_18,	MD5_CONST_19,
1707c478bd9Sstevel@tonic-gate 	MD5_CONST_20,	MD5_CONST_21,	MD5_CONST_22,	MD5_CONST_23,
1717c478bd9Sstevel@tonic-gate 	MD5_CONST_24,	MD5_CONST_25,	MD5_CONST_26,	MD5_CONST_27,
1727c478bd9Sstevel@tonic-gate 	MD5_CONST_28,	MD5_CONST_29,	MD5_CONST_30,	MD5_CONST_31,
1737c478bd9Sstevel@tonic-gate 	MD5_CONST_32,	MD5_CONST_33,	MD5_CONST_34,	MD5_CONST_35,
1747c478bd9Sstevel@tonic-gate 	MD5_CONST_36,	MD5_CONST_37,	MD5_CONST_38,	MD5_CONST_39,
1757c478bd9Sstevel@tonic-gate 	MD5_CONST_40,	MD5_CONST_41,	MD5_CONST_42,	MD5_CONST_43,
1767c478bd9Sstevel@tonic-gate 	MD5_CONST_44,	MD5_CONST_45,	MD5_CONST_46,	MD5_CONST_47,
1777c478bd9Sstevel@tonic-gate 	MD5_CONST_48,	MD5_CONST_49,	MD5_CONST_50,	MD5_CONST_51,
1787c478bd9Sstevel@tonic-gate 	MD5_CONST_52,	MD5_CONST_53,	MD5_CONST_54,	MD5_CONST_55,
1797c478bd9Sstevel@tonic-gate 	MD5_CONST_56,	MD5_CONST_57,	MD5_CONST_58,	MD5_CONST_59,
1807c478bd9Sstevel@tonic-gate 	MD5_CONST_60,	MD5_CONST_61,	MD5_CONST_62,	MD5_CONST_63
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
183554ff184Skais 
184554ff184Skais #ifdef sun4v
185554ff184Skais /*
186554ff184Skais  * To reduce the number of loads, load consts in 64-bit
187554ff184Skais  * chunks and then split.
188554ff184Skais  *
189554ff184Skais  * No need to mask upper 32-bits, as just interested in
190554ff184Skais  * low 32-bits (saves an & operation and means that this
191554ff184Skais  * optimization doesn't increases the icount.
192554ff184Skais  */
193554ff184Skais #define	MD5_CONST_e(x)		(md5_consts64[x/2] >> 32)
194554ff184Skais #define	MD5_CONST_o(x)		(md5_consts64[x/2])
195554ff184Skais 
196554ff184Skais #else
197554ff184Skais 
198554ff184Skais #define	MD5_CONST_e(x)		(md5_consts[x])
199554ff184Skais #define	MD5_CONST_o(x)		(md5_consts[x])
200554ff184Skais 
201554ff184Skais #endif /* sun4v */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * MD5Init()
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  * purpose: initializes the md5 context and begins and md5 digest operation
2097c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to initialize.
2107c478bd9Sstevel@tonic-gate  *  output: void
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate void
2147c478bd9Sstevel@tonic-gate MD5Init(MD5_CTX *ctx)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	ctx->count[0] = ctx->count[1] = 0;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* load magic initialization constants */
2197c478bd9Sstevel@tonic-gate 	ctx->state[0] = MD5_INIT_CONST_1;
2207c478bd9Sstevel@tonic-gate 	ctx->state[1] = MD5_INIT_CONST_2;
2217c478bd9Sstevel@tonic-gate 	ctx->state[2] = MD5_INIT_CONST_3;
2227c478bd9Sstevel@tonic-gate 	ctx->state[3] = MD5_INIT_CONST_4;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * MD5Update()
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * purpose: continues an md5 digest operation, using the message block
2297c478bd9Sstevel@tonic-gate  *          to update the context.
2307c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to update
2317c478bd9Sstevel@tonic-gate  *          uint8_t *	: the message block
2327c478bd9Sstevel@tonic-gate  *          uint32_t    : the length of the message block in bytes
2337c478bd9Sstevel@tonic-gate  *  output: void
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * MD5 crunches in 64-byte blocks.  All numeric constants here are related to
2367c478bd9Sstevel@tonic-gate  * that property of MD5.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate void
2407c478bd9Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	uint32_t		i, buf_index, buf_len;
243554ff184Skais #ifdef	sun4v
244554ff184Skais 	uint32_t		old_asi;
245554ff184Skais #endif	/* sun4v */
2467c478bd9Sstevel@tonic-gate 	const unsigned char 	*input = (const unsigned char *)inpp;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* compute (number of bytes computed so far) mod 64 */
2497c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[0] >> 3) & 0x3F;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* update number of bits hashed into this MD5 computation so far */
2527c478bd9Sstevel@tonic-gate 	if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
2537c478bd9Sstevel@tonic-gate 	    ctx->count[1]++;
2547c478bd9Sstevel@tonic-gate 	ctx->count[1] += (input_len >> 29);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
2597c478bd9Sstevel@tonic-gate 	i = 0;
2607c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		/*
2637c478bd9Sstevel@tonic-gate 		 * general optimization:
2647c478bd9Sstevel@tonic-gate 		 *
2657c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and MD5Transform() if
2667c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
2677c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
2687c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
2697c478bd9Sstevel@tonic-gate 		 * MD5Update().
2707c478bd9Sstevel@tonic-gate 		 */
2717c478bd9Sstevel@tonic-gate 
272554ff184Skais #ifdef sun4v
273554ff184Skais 		/*
274554ff184Skais 		 * For N1 use %asi register. However, costly to repeatedly set
275554ff184Skais 		 * in MD5Transform. Therefore, set once here.
276554ff184Skais 		 * Should probably restore the old value afterwards...
277554ff184Skais 		 */
278554ff184Skais 		old_asi = get_little();
279554ff184Skais 		set_little(0x88);
280554ff184Skais #endif /* sun4v */
281554ff184Skais 
2827c478bd9Sstevel@tonic-gate 		if (buf_index) {
2837c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
2867c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx,
2877c478bd9Sstevel@tonic-gate 			    ctx->buf_un.buf8);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			i = buf_len;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		for (; i + 63 < input_len; i += 64)
2937c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
2947c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx, &input[i]);
2957c478bd9Sstevel@tonic-gate 
296554ff184Skais 
297554ff184Skais #ifdef sun4v
298554ff184Skais 		/*
299554ff184Skais 		 * Restore old %ASI value
300554ff184Skais 		 */
301554ff184Skais 		set_little(old_asi);
302554ff184Skais #endif /* sun4v */
303554ff184Skais 
3047c478bd9Sstevel@tonic-gate 		/*
3057c478bd9Sstevel@tonic-gate 		 * general optimization:
3067c478bd9Sstevel@tonic-gate 		 *
3077c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
3087c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this
3097c478bd9Sstevel@tonic-gate 		 * case will be an expensive nop.
3107c478bd9Sstevel@tonic-gate 		 */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		if (input_len == i)
3137c478bd9Sstevel@tonic-gate 			return;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		buf_index = 0;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
3197c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * MD5Final()
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  * purpose: ends an md5 digest operation, finalizing the message digest and
3267c478bd9Sstevel@tonic-gate  *          zeroing the context.
3277c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: a buffer to store the digest in
3287c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to finalize, save, and zero
3297c478bd9Sstevel@tonic-gate  *  output: void
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate void
3337c478bd9Sstevel@tonic-gate MD5Final(unsigned char *digest, MD5_CTX *ctx)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_le[sizeof (ctx->count)];
3367c478bd9Sstevel@tonic-gate 	uint32_t	index = (ctx->count[0] >> 3) & 0x3f;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* store bit count, little endian */
3397c478bd9Sstevel@tonic-gate 	Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* pad out to 56 mod 64 */
3427c478bd9Sstevel@tonic-gate 	MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* append length (before padding) */
3457c478bd9Sstevel@tonic-gate 	MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/* store state in digest */
3487c478bd9Sstevel@tonic-gate 	Encode(digest, ctx->state, sizeof (ctx->state));
349673007c6Sdarrenm 
350673007c6Sdarrenm 	/* zeroize sensitive information */
351673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate void
3577c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	MD5_CTX context;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	MD5Init(&context);
3627c478bd9Sstevel@tonic-gate 	MD5Update(&context, input, inlen);
3637c478bd9Sstevel@tonic-gate 	MD5Final(output, &context);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * sparc register window optimization:
3707c478bd9Sstevel@tonic-gate  *
3717c478bd9Sstevel@tonic-gate  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
3727c478bd9Sstevel@tonic-gate  * since it increases the number of registers available to the
3737c478bd9Sstevel@tonic-gate  * compiler.  under this scheme, these variables can be held in
3747c478bd9Sstevel@tonic-gate  * %i0 - %i3, which leaves more local and out registers available.
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * MD5Transform()
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  * purpose: md5 transformation -- updates the digest based on `block'
3817c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
3827c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
3837c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
3847c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
3857c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to update
3867c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
3877c478bd9Sstevel@tonic-gate  *  output: void
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static void
3917c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
3927c478bd9Sstevel@tonic-gate     MD5_CTX *ctx, const uint8_t block[64])
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * general optimization:
3967c478bd9Sstevel@tonic-gate 	 *
3977c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
3987c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
4027c478bd9Sstevel@tonic-gate 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
403554ff184Skais #ifdef sun4v
404554ff184Skais 	unsigned long long 	*md5_consts64;
405554ff184Skais 
406734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
407554ff184Skais 	md5_consts64 = (unsigned long long *) md5_consts;
408554ff184Skais #endif	/* sun4v */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * general optimization:
4127c478bd9Sstevel@tonic-gate 	 *
4137c478bd9Sstevel@tonic-gate 	 * the compiler (at least SC4.2/5.x) generates better code if
4147c478bd9Sstevel@tonic-gate 	 * variable use is localized.  in this case, swapping the integers in
4157c478bd9Sstevel@tonic-gate 	 * this order allows `x_0 'to be swapped nearest to its first use in
4167c478bd9Sstevel@tonic-gate 	 * FF(), and likewise for `x_1' and up.  note that the compiler
4177c478bd9Sstevel@tonic-gate 	 * prefers this to doing each swap right before the FF() that
4187c478bd9Sstevel@tonic-gate 	 * uses it.
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * sparc v9/v8plus optimization:
4237c478bd9Sstevel@tonic-gate 	 *
4247c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use the
4257c478bd9Sstevel@tonic-gate 	 * optimized load_little_32() directly.  otherwise, bcopy()
4267c478bd9Sstevel@tonic-gate 	 * into a buffer that *is* aligned on a 4-byte boundary and
4277c478bd9Sstevel@tonic-gate 	 * then do the load_little_32() on that buffer.  benchmarks
4287c478bd9Sstevel@tonic-gate 	 * have shown that using the bcopy() is better than loading
4297c478bd9Sstevel@tonic-gate 	 * the bytes individually and doing the endian-swap by hand.
4307c478bd9Sstevel@tonic-gate 	 *
4317c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
4327c478bd9Sstevel@tonic-gate 	 *
4337c478bd9Sstevel@tonic-gate 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
4347c478bd9Sstevel@tonic-gate 	 *
4357c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
4367c478bd9Sstevel@tonic-gate 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
4407c478bd9Sstevel@tonic-gate 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
4417c478bd9Sstevel@tonic-gate 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
442554ff184Skais 
443554ff184Skais #ifdef sun4v
444554ff184Skais 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
445554ff184Skais 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
446554ff184Skais 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
447554ff184Skais 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
448554ff184Skais 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
449554ff184Skais 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
450554ff184Skais 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
451554ff184Skais 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
452554ff184Skais 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
453554ff184Skais 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
454554ff184Skais 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
455554ff184Skais 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
456554ff184Skais 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
457554ff184Skais 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
458554ff184Skais 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
459554ff184Skais 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
460554ff184Skais #else
4617c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
4627c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
4637c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
4647c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
4657c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
4667c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
4677c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
4687c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
4697c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
4707c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
4717c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
4727c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
4737c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
4747c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
4757c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
4767c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
477554ff184Skais #endif /* sun4v */
4787c478bd9Sstevel@tonic-gate 	} else
4797c478bd9Sstevel@tonic-gate #endif
4807c478bd9Sstevel@tonic-gate 	{
481554ff184Skais 
482554ff184Skais #ifdef sun4v
483734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
484554ff184Skais 		x_15 = LOAD_LITTLE_32_f(block);
485734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
486554ff184Skais 		x_14 = LOAD_LITTLE_32_e(block);
487734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
488554ff184Skais 		x_13 = LOAD_LITTLE_32_d(block);
489734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
490554ff184Skais 		x_12 = LOAD_LITTLE_32_c(block);
491734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
492554ff184Skais 		x_11 = LOAD_LITTLE_32_b(block);
493734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
494554ff184Skais 		x_10 = LOAD_LITTLE_32_a(block);
495734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
496554ff184Skais 		x_9  = LOAD_LITTLE_32_9(block);
497734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
498554ff184Skais 		x_8  = LOAD_LITTLE_32_8(block);
499734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
500554ff184Skais 		x_7  = LOAD_LITTLE_32_7(block);
501734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
502554ff184Skais 		x_6  = LOAD_LITTLE_32_6(block);
503734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
504554ff184Skais 		x_5  = LOAD_LITTLE_32_5(block);
505734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
506554ff184Skais 		x_4  = LOAD_LITTLE_32_4(block);
507734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
508554ff184Skais 		x_3  = LOAD_LITTLE_32_3(block);
509734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
510554ff184Skais 		x_2  = LOAD_LITTLE_32_2(block);
511734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
512554ff184Skais 		x_1  = LOAD_LITTLE_32_1(block);
513734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
514554ff184Skais 		x_0  = LOAD_LITTLE_32_0(block);
515554ff184Skais #else
516734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5177c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(block + 60);
518734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5197c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(block + 56);
520734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5217c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(block + 52);
522734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5237c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(block + 48);
524734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5257c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(block + 44);
526734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5277c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(block + 40);
528734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5297c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(block + 36);
530734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5317c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(block + 32);
532734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5337c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(block + 28);
534734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5357c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(block + 24);
536734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5377c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(block + 20);
538734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5397c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(block + 16);
540734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5417c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(block + 12);
542734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5437c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(block +  8);
544734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5457c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(block +  4);
546734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5477c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(block +  0);
548554ff184Skais #endif /* sun4v */
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	/* round 1 */
552554ff184Skais 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
553554ff184Skais 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
554554ff184Skais 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
555554ff184Skais 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
556554ff184Skais 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
557554ff184Skais 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
558554ff184Skais 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
559554ff184Skais 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
560554ff184Skais 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
561554ff184Skais 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
562554ff184Skais 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
563554ff184Skais 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
564554ff184Skais 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
565554ff184Skais 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
566554ff184Skais 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
567554ff184Skais 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/* round 2 */
570554ff184Skais 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
571554ff184Skais 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
572554ff184Skais 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
573554ff184Skais 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
574554ff184Skais 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
575554ff184Skais 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
576554ff184Skais 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
577554ff184Skais 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
578554ff184Skais 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
579554ff184Skais 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
580554ff184Skais 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
581554ff184Skais 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
582554ff184Skais 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
583554ff184Skais 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
584554ff184Skais 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
585554ff184Skais 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/* round 3 */
588554ff184Skais 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
589554ff184Skais 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
590554ff184Skais 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
591554ff184Skais 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
592554ff184Skais 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
593554ff184Skais 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
594554ff184Skais 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
595554ff184Skais 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
596554ff184Skais 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
597554ff184Skais 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
598554ff184Skais 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
599554ff184Skais 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
600554ff184Skais 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
601554ff184Skais 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
602554ff184Skais 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
603554ff184Skais 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/* round 4 */
606554ff184Skais 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
607554ff184Skais 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
608554ff184Skais 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
609554ff184Skais 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
610554ff184Skais 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
611554ff184Skais 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
612554ff184Skais 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
613554ff184Skais 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
614554ff184Skais 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
615554ff184Skais 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
616554ff184Skais 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
617554ff184Skais 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
618554ff184Skais 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
619554ff184Skais 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
620554ff184Skais 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
621554ff184Skais 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	ctx->state[0] += a;
6247c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
6257c478bd9Sstevel@tonic-gate 	ctx->state[2] += c;
6267c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * zeroize sensitive information -- compiler will optimize
6307c478bd9Sstevel@tonic-gate 	 * this out if everything is kept in registers
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
6347c478bd9Sstevel@tonic-gate 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Encode()
6397c478bd9Sstevel@tonic-gate  *
6407c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from big endian to little endian
6417c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted little endian numbers
6427c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
6437c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
6447c478bd9Sstevel@tonic-gate  *  output: void
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static void
648734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
649734b6a94Sdarrenm     size_t input_len)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	size_t		i, j;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
6587c478bd9Sstevel@tonic-gate 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
6597c478bd9Sstevel@tonic-gate 			bcopy(input + i, output + j, 4);
6607c478bd9Sstevel@tonic-gate 		else *(uint32_t *)(output + j) = input[i];
6617c478bd9Sstevel@tonic-gate #else
662734b6a94Sdarrenm 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
6637c478bd9Sstevel@tonic-gate 		*(uint32_t *)(output + j) = input[i];
6647c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		output[j] = input[i] & 0xff;
6697c478bd9Sstevel@tonic-gate 		output[j + 1] = (input[i] >> 8)  & 0xff;
6707c478bd9Sstevel@tonic-gate 		output[j + 2] = (input[i] >> 16) & 0xff;
6717c478bd9Sstevel@tonic-gate 		output[j + 3] = (input[i] >> 24) & 0xff;
6727c478bd9Sstevel@tonic-gate #endif
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate }
675