xref: /titanic_44/usr/src/common/crypto/md5/md5.c (revision 160abee025ef30c34521b981edd40ffcaab560aa)
17c478bd9Sstevel@tonic-gate /*
2*160abee0Sda73024  * Copyright 2008 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);
54*160abee0Sda73024 
55*160abee0Sda73024 #if !defined(__amd64)
567c478bd9Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
577c478bd9Sstevel@tonic-gate     const uint8_t [64]);
58*160abee0Sda73024 #else
59*160abee0Sda73024 void md5_block_asm_host_order(MD5_CTX *ctx, const void *inpp,
60*160abee0Sda73024     unsigned int input_length_in_blocks);
61*160abee0Sda73024 #endif /* !defined(__amd64) */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * F, G, H and I are the basic MD5 functions.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
697c478bd9Sstevel@tonic-gate #define	G(b, c, d)	(((b) & (d)) | ((c) & (~d)))
707c478bd9Sstevel@tonic-gate #define	H(b, c, d)	((b) ^ (c) ^ (d))
717c478bd9Sstevel@tonic-gate #define	I(b, c, d)	((c) ^ ((b) | (~d)))
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * ROTATE_LEFT rotates x left n bits.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #define	ROTATE_LEFT(x, n)	\
777c478bd9Sstevel@tonic-gate 	(((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
817c478bd9Sstevel@tonic-gate  * Rotation is separate from addition to prevent recomputation.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	FF(a, b, c, d, x, s, ac) { \
85554ff184Skais 	(a) += F((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	GG(a, b, c, d, x, s, ac) { \
91554ff184Skais 	(a) += G((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	HH(a, b, c, d, x, s, ac) { \
97554ff184Skais 	(a) += H((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 #define	II(a, b, c, d, x, s, ac) { \
103554ff184Skais 	(a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
1047c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
1057c478bd9Sstevel@tonic-gate 	(a) += (b); \
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Loading 32-bit constants on a RISC is expensive since it involves both a
1107c478bd9Sstevel@tonic-gate  * `sethi' and an `or'.  thus, we instead have the compiler generate `ld's to
1117c478bd9Sstevel@tonic-gate  * load the constants from an array called `md5_consts'.  however, on intel
1127c478bd9Sstevel@tonic-gate  * (and other CISC processors), it is cheaper to load the constant
1137c478bd9Sstevel@tonic-gate  * directly.  thus, the c code in MD5Transform() uses the macro MD5_CONST()
1147c478bd9Sstevel@tonic-gate  * which either expands to a constant or an array reference, depending on the
1157c478bd9Sstevel@tonic-gate  * architecture the code is being compiled for.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * Right now, i386 and amd64 are the CISC exceptions.
1187c478bd9Sstevel@tonic-gate  * If we get another CISC ISA, we'll have to change the ifdef.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	MD5_CONST(x)		(MD5_CONST_ ## x)
124554ff184Skais #define	MD5_CONST_e(x)		MD5_CONST(x)
125554ff184Skais #define	MD5_CONST_o(x)		MD5_CONST(x)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #else
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * sparc/RISC optimization:
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
1327c478bd9Sstevel@tonic-gate  * machines), it is more efficient to place all the constants used in this
1337c478bd9Sstevel@tonic-gate  * function in an array and load the values out of the array than to manually
1347c478bd9Sstevel@tonic-gate  * load the constants.  this is because setting a register to a 32-bit value
1357c478bd9Sstevel@tonic-gate  * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
1367c478bd9Sstevel@tonic-gate  * value from memory only takes one `ld' (or `lduw' on v9).  while this
1377c478bd9Sstevel@tonic-gate  * increases memory usage, the compiler can find enough other things to do
1387c478bd9Sstevel@tonic-gate  * while waiting to keep the pipeline does not stall.  additionally, it is
1397c478bd9Sstevel@tonic-gate  * likely that many of these constants are cached so that later accesses do
1407c478bd9Sstevel@tonic-gate  * not even go out to the bus.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * this array is declared `static' to keep the compiler from having to
1437c478bd9Sstevel@tonic-gate  * bcopy() this array onto the stack frame of MD5Transform() each time it is
1447c478bd9Sstevel@tonic-gate  * called -- which is unacceptably expensive.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * the `const' is to ensure that callers are good citizens and do not try to
1477c478bd9Sstevel@tonic-gate  * munge the array.  since these routines are going to be called from inside
1487c478bd9Sstevel@tonic-gate  * multithreaded kernelland, this is a good safety check. -- `constants' will
1497c478bd9Sstevel@tonic-gate  * end up in .rodata.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * unfortunately, loading from an array in this manner hurts performance under
1527c478bd9Sstevel@tonic-gate  * intel (and presumably other CISC machines).  so, there is a macro,
1537c478bd9Sstevel@tonic-gate  * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
1547c478bd9Sstevel@tonic-gate  * this array, or to the actual constant, depending on what platform this code
1557c478bd9Sstevel@tonic-gate  * is compiled for.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 
158554ff184Skais #ifdef sun4v
159554ff184Skais 
160554ff184Skais /*
161554ff184Skais  * Going to load these consts in 8B chunks, so need to enforce 8B alignment
162554ff184Skais  */
163554ff184Skais 
164554ff184Skais /* CSTYLED */
165554ff184Skais #pragma align 64 (md5_consts)
166b085fdc5Sfr80241 #define	_MD5_CHECK_ALIGNMENT
167554ff184Skais 
168554ff184Skais #endif /* sun4v */
169554ff184Skais 
1707c478bd9Sstevel@tonic-gate static const uint32_t md5_consts[] = {
1717c478bd9Sstevel@tonic-gate 	MD5_CONST_0,	MD5_CONST_1,	MD5_CONST_2,	MD5_CONST_3,
1727c478bd9Sstevel@tonic-gate 	MD5_CONST_4,	MD5_CONST_5,	MD5_CONST_6,	MD5_CONST_7,
1737c478bd9Sstevel@tonic-gate 	MD5_CONST_8,	MD5_CONST_9,	MD5_CONST_10,	MD5_CONST_11,
1747c478bd9Sstevel@tonic-gate 	MD5_CONST_12,	MD5_CONST_13,	MD5_CONST_14,	MD5_CONST_15,
1757c478bd9Sstevel@tonic-gate 	MD5_CONST_16,	MD5_CONST_17,	MD5_CONST_18,	MD5_CONST_19,
1767c478bd9Sstevel@tonic-gate 	MD5_CONST_20,	MD5_CONST_21,	MD5_CONST_22,	MD5_CONST_23,
1777c478bd9Sstevel@tonic-gate 	MD5_CONST_24,	MD5_CONST_25,	MD5_CONST_26,	MD5_CONST_27,
1787c478bd9Sstevel@tonic-gate 	MD5_CONST_28,	MD5_CONST_29,	MD5_CONST_30,	MD5_CONST_31,
1797c478bd9Sstevel@tonic-gate 	MD5_CONST_32,	MD5_CONST_33,	MD5_CONST_34,	MD5_CONST_35,
1807c478bd9Sstevel@tonic-gate 	MD5_CONST_36,	MD5_CONST_37,	MD5_CONST_38,	MD5_CONST_39,
1817c478bd9Sstevel@tonic-gate 	MD5_CONST_40,	MD5_CONST_41,	MD5_CONST_42,	MD5_CONST_43,
1827c478bd9Sstevel@tonic-gate 	MD5_CONST_44,	MD5_CONST_45,	MD5_CONST_46,	MD5_CONST_47,
1837c478bd9Sstevel@tonic-gate 	MD5_CONST_48,	MD5_CONST_49,	MD5_CONST_50,	MD5_CONST_51,
1847c478bd9Sstevel@tonic-gate 	MD5_CONST_52,	MD5_CONST_53,	MD5_CONST_54,	MD5_CONST_55,
1857c478bd9Sstevel@tonic-gate 	MD5_CONST_56,	MD5_CONST_57,	MD5_CONST_58,	MD5_CONST_59,
1867c478bd9Sstevel@tonic-gate 	MD5_CONST_60,	MD5_CONST_61,	MD5_CONST_62,	MD5_CONST_63
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate 
189554ff184Skais 
190554ff184Skais #ifdef sun4v
191554ff184Skais /*
192554ff184Skais  * To reduce the number of loads, load consts in 64-bit
193554ff184Skais  * chunks and then split.
194554ff184Skais  *
195554ff184Skais  * No need to mask upper 32-bits, as just interested in
196554ff184Skais  * low 32-bits (saves an & operation and means that this
197554ff184Skais  * optimization doesn't increases the icount.
198554ff184Skais  */
199554ff184Skais #define	MD5_CONST_e(x)		(md5_consts64[x/2] >> 32)
200554ff184Skais #define	MD5_CONST_o(x)		(md5_consts64[x/2])
201554ff184Skais 
202554ff184Skais #else
203554ff184Skais 
204554ff184Skais #define	MD5_CONST_e(x)		(md5_consts[x])
205554ff184Skais #define	MD5_CONST_o(x)		(md5_consts[x])
206554ff184Skais 
207554ff184Skais #endif /* sun4v */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * MD5Init()
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  * purpose: initializes the md5 context and begins and md5 digest operation
2157c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to initialize.
2167c478bd9Sstevel@tonic-gate  *  output: void
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate void
2207c478bd9Sstevel@tonic-gate MD5Init(MD5_CTX *ctx)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	ctx->count[0] = ctx->count[1] = 0;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* load magic initialization constants */
2257c478bd9Sstevel@tonic-gate 	ctx->state[0] = MD5_INIT_CONST_1;
2267c478bd9Sstevel@tonic-gate 	ctx->state[1] = MD5_INIT_CONST_2;
2277c478bd9Sstevel@tonic-gate 	ctx->state[2] = MD5_INIT_CONST_3;
2287c478bd9Sstevel@tonic-gate 	ctx->state[3] = MD5_INIT_CONST_4;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * MD5Update()
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * purpose: continues an md5 digest operation, using the message block
2357c478bd9Sstevel@tonic-gate  *          to update the context.
2367c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to update
2377c478bd9Sstevel@tonic-gate  *          uint8_t *	: the message block
2387c478bd9Sstevel@tonic-gate  *          uint32_t    : the length of the message block in bytes
2397c478bd9Sstevel@tonic-gate  *  output: void
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  * MD5 crunches in 64-byte blocks.  All numeric constants here are related to
2427c478bd9Sstevel@tonic-gate  * that property of MD5.
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate void
2467c478bd9Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	uint32_t		i, buf_index, buf_len;
249554ff184Skais #ifdef	sun4v
250554ff184Skais 	uint32_t		old_asi;
251554ff184Skais #endif	/* sun4v */
252*160abee0Sda73024 #if defined(__amd64)
253*160abee0Sda73024 	uint32_t		block_count;
254*160abee0Sda73024 #endif /* !defined(__amd64) */
2557c478bd9Sstevel@tonic-gate 	const unsigned char 	*input = (const unsigned char *)inpp;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* compute (number of bytes computed so far) mod 64 */
2587c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[0] >> 3) & 0x3F;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/* update number of bits hashed into this MD5 computation so far */
2617c478bd9Sstevel@tonic-gate 	if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
2627c478bd9Sstevel@tonic-gate 		ctx->count[1]++;
2637c478bd9Sstevel@tonic-gate 	ctx->count[1] += (input_len >> 29);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
2687c478bd9Sstevel@tonic-gate 	i = 0;
2697c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		/*
2727c478bd9Sstevel@tonic-gate 		 * general optimization:
2737c478bd9Sstevel@tonic-gate 		 *
2747c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and MD5Transform() if
2757c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
2767c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
2777c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
2787c478bd9Sstevel@tonic-gate 		 * MD5Update().
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 
281554ff184Skais #ifdef sun4v
282554ff184Skais 		/*
283554ff184Skais 		 * For N1 use %asi register. However, costly to repeatedly set
284554ff184Skais 		 * in MD5Transform. Therefore, set once here.
285554ff184Skais 		 * Should probably restore the old value afterwards...
286554ff184Skais 		 */
287554ff184Skais 		old_asi = get_little();
288554ff184Skais 		set_little(0x88);
289554ff184Skais #endif /* sun4v */
290554ff184Skais 
2917c478bd9Sstevel@tonic-gate 		if (buf_index) {
2927c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
2937c478bd9Sstevel@tonic-gate 
294*160abee0Sda73024 #if !defined(__amd64)
2957c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
2967c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx,
2977c478bd9Sstevel@tonic-gate 			    ctx->buf_un.buf8);
298*160abee0Sda73024 #else
299*160abee0Sda73024 			md5_block_asm_host_order(ctx, ctx->buf_un.buf8, 1);
300*160abee0Sda73024 #endif /* !defined(__amd64) */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			i = buf_len;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 
305*160abee0Sda73024 #if !defined(__amd64)
3067c478bd9Sstevel@tonic-gate 		for (; i + 63 < input_len; i += 64)
3077c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
3087c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx, &input[i]);
3097c478bd9Sstevel@tonic-gate 
310*160abee0Sda73024 #else
311*160abee0Sda73024 		block_count = (input_len - i) >> 6;
312*160abee0Sda73024 		if (block_count > 0) {
313*160abee0Sda73024 			md5_block_asm_host_order(ctx, &input[i], block_count);
314*160abee0Sda73024 			i += block_count << 6;
315*160abee0Sda73024 		}
316*160abee0Sda73024 #endif /* !defined(__amd64) */
317*160abee0Sda73024 
318554ff184Skais 
319554ff184Skais #ifdef sun4v
320554ff184Skais 		/*
321554ff184Skais 		 * Restore old %ASI value
322554ff184Skais 		 */
323554ff184Skais 		set_little(old_asi);
324554ff184Skais #endif /* sun4v */
325554ff184Skais 
3267c478bd9Sstevel@tonic-gate 		/*
3277c478bd9Sstevel@tonic-gate 		 * general optimization:
3287c478bd9Sstevel@tonic-gate 		 *
3297c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
3307c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this
3317c478bd9Sstevel@tonic-gate 		 * case will be an expensive nop.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (input_len == i)
3357c478bd9Sstevel@tonic-gate 			return;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		buf_index = 0;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
3417c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * MD5Final()
3467c478bd9Sstevel@tonic-gate  *
3477c478bd9Sstevel@tonic-gate  * purpose: ends an md5 digest operation, finalizing the message digest and
3487c478bd9Sstevel@tonic-gate  *          zeroing the context.
3495151fb12Sdarrenm  *   input: uchar_t *	: a buffer to store the digest in
3505151fb12Sdarrenm  *			: The function actually uses void* because many
3515151fb12Sdarrenm  *			: callers pass things other than uchar_t here.
3527c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to finalize, save, and zero
3537c478bd9Sstevel@tonic-gate  *  output: void
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate void
3575151fb12Sdarrenm MD5Final(void *digest, MD5_CTX *ctx)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_le[sizeof (ctx->count)];
3607c478bd9Sstevel@tonic-gate 	uint32_t	index = (ctx->count[0] >> 3) & 0x3f;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* store bit count, little endian */
3637c478bd9Sstevel@tonic-gate 	Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* pad out to 56 mod 64 */
3667c478bd9Sstevel@tonic-gate 	MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* append length (before padding) */
3697c478bd9Sstevel@tonic-gate 	MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/* store state in digest */
3727c478bd9Sstevel@tonic-gate 	Encode(digest, ctx->state, sizeof (ctx->state));
373673007c6Sdarrenm 
374673007c6Sdarrenm 	/* zeroize sensitive information */
375673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate void
3817c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	MD5_CTX context;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	MD5Init(&context);
3867c478bd9Sstevel@tonic-gate 	MD5Update(&context, input, inlen);
3877c478bd9Sstevel@tonic-gate 	MD5Final(output, &context);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
3917c478bd9Sstevel@tonic-gate 
392*160abee0Sda73024 #if !defined(__amd64)
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * sparc register window optimization:
3957c478bd9Sstevel@tonic-gate  *
3967c478bd9Sstevel@tonic-gate  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
3977c478bd9Sstevel@tonic-gate  * since it increases the number of registers available to the
3987c478bd9Sstevel@tonic-gate  * compiler.  under this scheme, these variables can be held in
3997c478bd9Sstevel@tonic-gate  * %i0 - %i3, which leaves more local and out registers available.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * MD5Transform()
4047c478bd9Sstevel@tonic-gate  *
4057c478bd9Sstevel@tonic-gate  * purpose: md5 transformation -- updates the digest based on `block'
4067c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
4077c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
4087c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
4097c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
4107c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to update
4117c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
4127c478bd9Sstevel@tonic-gate  *  output: void
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate static void
4167c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
4177c478bd9Sstevel@tonic-gate     MD5_CTX *ctx, const uint8_t block[64])
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * general optimization:
4217c478bd9Sstevel@tonic-gate 	 *
4227c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
4237c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
4277c478bd9Sstevel@tonic-gate 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
428554ff184Skais #ifdef sun4v
429554ff184Skais 	unsigned long long 	*md5_consts64;
430554ff184Skais 
431734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
432554ff184Skais 	md5_consts64 = (unsigned long long *) md5_consts;
433554ff184Skais #endif	/* sun4v */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * general optimization:
4377c478bd9Sstevel@tonic-gate 	 *
4387c478bd9Sstevel@tonic-gate 	 * the compiler (at least SC4.2/5.x) generates better code if
4397c478bd9Sstevel@tonic-gate 	 * variable use is localized.  in this case, swapping the integers in
4407c478bd9Sstevel@tonic-gate 	 * this order allows `x_0 'to be swapped nearest to its first use in
4417c478bd9Sstevel@tonic-gate 	 * FF(), and likewise for `x_1' and up.  note that the compiler
4427c478bd9Sstevel@tonic-gate 	 * prefers this to doing each swap right before the FF() that
4437c478bd9Sstevel@tonic-gate 	 * uses it.
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * sparc v9/v8plus optimization:
4487c478bd9Sstevel@tonic-gate 	 *
4497c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use the
4507c478bd9Sstevel@tonic-gate 	 * optimized load_little_32() directly.  otherwise, bcopy()
4517c478bd9Sstevel@tonic-gate 	 * into a buffer that *is* aligned on a 4-byte boundary and
4527c478bd9Sstevel@tonic-gate 	 * then do the load_little_32() on that buffer.  benchmarks
4537c478bd9Sstevel@tonic-gate 	 * have shown that using the bcopy() is better than loading
4547c478bd9Sstevel@tonic-gate 	 * the bytes individually and doing the endian-swap by hand.
4557c478bd9Sstevel@tonic-gate 	 *
4567c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
4577c478bd9Sstevel@tonic-gate 	 *
4587c478bd9Sstevel@tonic-gate 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
4597c478bd9Sstevel@tonic-gate 	 *
4607c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
4617c478bd9Sstevel@tonic-gate 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
4657c478bd9Sstevel@tonic-gate 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
4667c478bd9Sstevel@tonic-gate 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
467554ff184Skais 
468554ff184Skais #ifdef sun4v
469554ff184Skais 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
470554ff184Skais 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
471554ff184Skais 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
472554ff184Skais 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
473554ff184Skais 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
474554ff184Skais 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
475554ff184Skais 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
476554ff184Skais 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
477554ff184Skais 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
478554ff184Skais 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
479554ff184Skais 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
480554ff184Skais 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
481554ff184Skais 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
482554ff184Skais 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
483554ff184Skais 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
484554ff184Skais 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
485554ff184Skais #else
4867c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
4877c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
4887c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
4897c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
4907c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
4917c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
4927c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
4937c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
4947c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
4957c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
4967c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
4977c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
4987c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
4997c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
5007c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
5017c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
502554ff184Skais #endif /* sun4v */
5037c478bd9Sstevel@tonic-gate 	} else
5047c478bd9Sstevel@tonic-gate #endif
5057c478bd9Sstevel@tonic-gate 	{
506554ff184Skais 
507554ff184Skais #ifdef sun4v
508734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
509554ff184Skais 		x_15 = LOAD_LITTLE_32_f(block);
510734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
511554ff184Skais 		x_14 = LOAD_LITTLE_32_e(block);
512734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
513554ff184Skais 		x_13 = LOAD_LITTLE_32_d(block);
514734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
515554ff184Skais 		x_12 = LOAD_LITTLE_32_c(block);
516734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
517554ff184Skais 		x_11 = LOAD_LITTLE_32_b(block);
518734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
519554ff184Skais 		x_10 = LOAD_LITTLE_32_a(block);
520734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
521554ff184Skais 		x_9  = LOAD_LITTLE_32_9(block);
522734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
523554ff184Skais 		x_8  = LOAD_LITTLE_32_8(block);
524734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
525554ff184Skais 		x_7  = LOAD_LITTLE_32_7(block);
526734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
527554ff184Skais 		x_6  = LOAD_LITTLE_32_6(block);
528734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
529554ff184Skais 		x_5  = LOAD_LITTLE_32_5(block);
530734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
531554ff184Skais 		x_4  = LOAD_LITTLE_32_4(block);
532734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
533554ff184Skais 		x_3  = LOAD_LITTLE_32_3(block);
534734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
535554ff184Skais 		x_2  = LOAD_LITTLE_32_2(block);
536734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
537554ff184Skais 		x_1  = LOAD_LITTLE_32_1(block);
538734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
539554ff184Skais 		x_0  = LOAD_LITTLE_32_0(block);
540554ff184Skais #else
541734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5427c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(block + 60);
543734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5447c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(block + 56);
545734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5467c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(block + 52);
547734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5487c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(block + 48);
549734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5507c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(block + 44);
551734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5527c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(block + 40);
553734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5547c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(block + 36);
555734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5567c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(block + 32);
557734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5587c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(block + 28);
559734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5607c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(block + 24);
561734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5627c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(block + 20);
563734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5647c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(block + 16);
565734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5667c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(block + 12);
567734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5687c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(block +  8);
569734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5707c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(block +  4);
571734b6a94Sdarrenm 		/* LINTED E_BAD_PTR_CAST_ALIGN */
5727c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(block +  0);
573554ff184Skais #endif /* sun4v */
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/* round 1 */
577554ff184Skais 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
578554ff184Skais 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
579554ff184Skais 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
580554ff184Skais 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
581554ff184Skais 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
582554ff184Skais 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
583554ff184Skais 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
584554ff184Skais 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
585554ff184Skais 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
586554ff184Skais 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
587554ff184Skais 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
588554ff184Skais 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
589554ff184Skais 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
590554ff184Skais 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
591554ff184Skais 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
592554ff184Skais 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/* round 2 */
595554ff184Skais 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
596554ff184Skais 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
597554ff184Skais 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
598554ff184Skais 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
599554ff184Skais 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
600554ff184Skais 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
601554ff184Skais 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
602554ff184Skais 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
603554ff184Skais 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
604554ff184Skais 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
605554ff184Skais 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
606554ff184Skais 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
607554ff184Skais 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
608554ff184Skais 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
609554ff184Skais 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
610554ff184Skais 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/* round 3 */
613554ff184Skais 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
614554ff184Skais 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
615554ff184Skais 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
616554ff184Skais 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
617554ff184Skais 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
618554ff184Skais 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
619554ff184Skais 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
620554ff184Skais 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
621554ff184Skais 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
622554ff184Skais 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
623554ff184Skais 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
624554ff184Skais 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
625554ff184Skais 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
626554ff184Skais 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
627554ff184Skais 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
628554ff184Skais 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* round 4 */
631554ff184Skais 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
632554ff184Skais 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
633554ff184Skais 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
634554ff184Skais 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
635554ff184Skais 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
636554ff184Skais 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
637554ff184Skais 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
638554ff184Skais 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
639554ff184Skais 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
640554ff184Skais 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
641554ff184Skais 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
642554ff184Skais 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
643554ff184Skais 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
644554ff184Skais 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
645554ff184Skais 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
646554ff184Skais 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	ctx->state[0] += a;
6497c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
6507c478bd9Sstevel@tonic-gate 	ctx->state[2] += c;
6517c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * zeroize sensitive information -- compiler will optimize
6557c478bd9Sstevel@tonic-gate 	 * this out if everything is kept in registers
6567c478bd9Sstevel@tonic-gate 	 */
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
6597c478bd9Sstevel@tonic-gate 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
6607c478bd9Sstevel@tonic-gate }
661*160abee0Sda73024 #endif /* !defined(__amd64) */
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Encode()
6657c478bd9Sstevel@tonic-gate  *
6667c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from big endian to little endian
6677c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted little endian numbers
6687c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
6697c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
6707c478bd9Sstevel@tonic-gate  *  output: void
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static void
674734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
675734b6a94Sdarrenm     size_t input_len)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	size_t		i, j;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
6847c478bd9Sstevel@tonic-gate 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
6857c478bd9Sstevel@tonic-gate 			bcopy(input + i, output + j, 4);
6867c478bd9Sstevel@tonic-gate 		else *(uint32_t *)(output + j) = input[i];
6877c478bd9Sstevel@tonic-gate #else
688734b6a94Sdarrenm 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
6897c478bd9Sstevel@tonic-gate 		*(uint32_t *)(output + j) = input[i];
6907c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		output[j] = input[i] & 0xff;
6957c478bd9Sstevel@tonic-gate 		output[j + 1] = (input[i] >> 8)  & 0xff;
6967c478bd9Sstevel@tonic-gate 		output[j + 2] = (input[i] >> 16) & 0xff;
6977c478bd9Sstevel@tonic-gate 		output[j + 3] = (input[i] >> 24) & 0xff;
6987c478bd9Sstevel@tonic-gate #endif
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate }
701