xref: /titanic_50/usr/src/common/crypto/md5/md5.c (revision 0209230bf1261579beab4f55226bb509e6b850cb)
1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Cleaned-up and optimized version of MD5, based on the reference
8  * implementation provided in RFC 1321.  See RSA Copyright information
9  * below.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 /*
15  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
16  */
17 
18 /*
19  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
20  * rights reserved.
21  *
22  * License to copy and use this software is granted provided that it
23  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
24  * Algorithm" in all material mentioning or referencing this software
25  * or this function.
26  *
27  * License is also granted to make and use derivative works provided
28  * that such works are identified as "derived from the RSA Data
29  * Security, Inc. MD5 Message-Digest Algorithm" in all material
30  * mentioning or referencing the derived work.
31  *
32  * RSA Data Security, Inc. makes no representations concerning either
33  * the merchantability of this software or the suitability of this
34  * software for any particular purpose. It is provided "as is"
35  * without express or implied warranty of any kind.
36  *
37  * These notices must be retained in any copies of any part of this
38  * documentation and/or software.
39  */
40 
41 #include <sys/types.h>
42 #include <sys/md5.h>
43 #include <sys/md5_consts.h>	/* MD5_CONST() optimization */
44 #include "md5_byteswap.h"
45 #if	!defined(_KERNEL) || defined(_BOOT)
46 #include <strings.h>
47 #endif /* !_KERNEL || _BOOT */
48 
49 #ifdef _KERNEL
50 #include <sys/systm.h>
51 #endif /* _KERNEL */
52 
53 static void Encode(uint8_t *, const uint32_t *, size_t);
54 static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
55     const uint8_t [64]);
56 
57 static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
58 
59 /*
60  * F, G, H and I are the basic MD5 functions.
61  */
62 #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
63 #define	G(b, c, d)	(((b) & (d)) | ((c) & (~d)))
64 #define	H(b, c, d)	((b) ^ (c) ^ (d))
65 #define	I(b, c, d)	((c) ^ ((b) | (~d)))
66 
67 /*
68  * ROTATE_LEFT rotates x left n bits.
69  */
70 #define	ROTATE_LEFT(x, n)	\
71 	(((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
72 
73 /*
74  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
75  * Rotation is separate from addition to prevent recomputation.
76  */
77 
78 #define	FF(a, b, c, d, x, s, ac) { \
79 	(a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
80 	(a) = ROTATE_LEFT((a), (s)); \
81 	(a) += (b); \
82 	}
83 
84 #define	GG(a, b, c, d, x, s, ac) { \
85 	(a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
86 	(a) = ROTATE_LEFT((a), (s)); \
87 	(a) += (b); \
88 	}
89 
90 #define	HH(a, b, c, d, x, s, ac) { \
91 	(a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
92 	(a) = ROTATE_LEFT((a), (s)); \
93 	(a) += (b); \
94 	}
95 
96 #define	II(a, b, c, d, x, s, ac) { \
97 	(a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
98 	(a) = ROTATE_LEFT((a), (s)); \
99 	(a) += (b); \
100 	}
101 
102 /*
103  * Loading 32-bit constants on a RISC is expensive since it involves both a
104  * `sethi' and an `or'.  thus, we instead have the compiler generate `ld's to
105  * load the constants from an array called `md5_consts'.  however, on intel
106  * (and other CISC processors), it is cheaper to load the constant
107  * directly.  thus, the c code in MD5Transform() uses the macro MD5_CONST()
108  * which either expands to a constant or an array reference, depending on the
109  * architecture the code is being compiled for.
110  *
111  * Right now, i386 and amd64 are the CISC exceptions.
112  * If we get another CISC ISA, we'll have to change the ifdef.
113  */
114 
115 #if defined(__i386) || defined(__amd64)
116 
117 #define	MD5_CONST(x)		(MD5_CONST_ ## x)
118 #define	MD5_CONST_e(x)		MD5_CONST(x)
119 #define	MD5_CONST_o(x)		MD5_CONST(x)
120 
121 #else
122 /*
123  * sparc/RISC optimization:
124  *
125  * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
126  * machines), it is more efficient to place all the constants used in this
127  * function in an array and load the values out of the array than to manually
128  * load the constants.  this is because setting a register to a 32-bit value
129  * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
130  * value from memory only takes one `ld' (or `lduw' on v9).  while this
131  * increases memory usage, the compiler can find enough other things to do
132  * while waiting to keep the pipeline does not stall.  additionally, it is
133  * likely that many of these constants are cached so that later accesses do
134  * not even go out to the bus.
135  *
136  * this array is declared `static' to keep the compiler from having to
137  * bcopy() this array onto the stack frame of MD5Transform() each time it is
138  * called -- which is unacceptably expensive.
139  *
140  * the `const' is to ensure that callers are good citizens and do not try to
141  * munge the array.  since these routines are going to be called from inside
142  * multithreaded kernelland, this is a good safety check. -- `constants' will
143  * end up in .rodata.
144  *
145  * unfortunately, loading from an array in this manner hurts performance under
146  * intel (and presumably other CISC machines).  so, there is a macro,
147  * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
148  * this array, or to the actual constant, depending on what platform this code
149  * is compiled for.
150  */
151 
152 #ifdef sun4v
153 
154 /*
155  * Going to load these consts in 8B chunks, so need to enforce 8B alignment
156  */
157 
158 /* CSTYLED */
159 #pragma align 64 (md5_consts)
160 #define	_MD5_CHECK_ALIGNMENT
161 
162 #endif /* sun4v */
163 
164 static const uint32_t md5_consts[] = {
165 	MD5_CONST_0,	MD5_CONST_1,	MD5_CONST_2,	MD5_CONST_3,
166 	MD5_CONST_4,	MD5_CONST_5,	MD5_CONST_6,	MD5_CONST_7,
167 	MD5_CONST_8,	MD5_CONST_9,	MD5_CONST_10,	MD5_CONST_11,
168 	MD5_CONST_12,	MD5_CONST_13,	MD5_CONST_14,	MD5_CONST_15,
169 	MD5_CONST_16,	MD5_CONST_17,	MD5_CONST_18,	MD5_CONST_19,
170 	MD5_CONST_20,	MD5_CONST_21,	MD5_CONST_22,	MD5_CONST_23,
171 	MD5_CONST_24,	MD5_CONST_25,	MD5_CONST_26,	MD5_CONST_27,
172 	MD5_CONST_28,	MD5_CONST_29,	MD5_CONST_30,	MD5_CONST_31,
173 	MD5_CONST_32,	MD5_CONST_33,	MD5_CONST_34,	MD5_CONST_35,
174 	MD5_CONST_36,	MD5_CONST_37,	MD5_CONST_38,	MD5_CONST_39,
175 	MD5_CONST_40,	MD5_CONST_41,	MD5_CONST_42,	MD5_CONST_43,
176 	MD5_CONST_44,	MD5_CONST_45,	MD5_CONST_46,	MD5_CONST_47,
177 	MD5_CONST_48,	MD5_CONST_49,	MD5_CONST_50,	MD5_CONST_51,
178 	MD5_CONST_52,	MD5_CONST_53,	MD5_CONST_54,	MD5_CONST_55,
179 	MD5_CONST_56,	MD5_CONST_57,	MD5_CONST_58,	MD5_CONST_59,
180 	MD5_CONST_60,	MD5_CONST_61,	MD5_CONST_62,	MD5_CONST_63
181 };
182 
183 
184 #ifdef sun4v
185 /*
186  * To reduce the number of loads, load consts in 64-bit
187  * chunks and then split.
188  *
189  * No need to mask upper 32-bits, as just interested in
190  * low 32-bits (saves an & operation and means that this
191  * optimization doesn't increases the icount.
192  */
193 #define	MD5_CONST_e(x)		(md5_consts64[x/2] >> 32)
194 #define	MD5_CONST_o(x)		(md5_consts64[x/2])
195 
196 #else
197 
198 #define	MD5_CONST_e(x)		(md5_consts[x])
199 #define	MD5_CONST_o(x)		(md5_consts[x])
200 
201 #endif /* sun4v */
202 
203 #endif
204 
205 /*
206  * MD5Init()
207  *
208  * purpose: initializes the md5 context and begins and md5 digest operation
209  *   input: MD5_CTX *	: the context to initialize.
210  *  output: void
211  */
212 
213 void
214 MD5Init(MD5_CTX *ctx)
215 {
216 	ctx->count[0] = ctx->count[1] = 0;
217 
218 	/* load magic initialization constants */
219 	ctx->state[0] = MD5_INIT_CONST_1;
220 	ctx->state[1] = MD5_INIT_CONST_2;
221 	ctx->state[2] = MD5_INIT_CONST_3;
222 	ctx->state[3] = MD5_INIT_CONST_4;
223 }
224 
225 /*
226  * MD5Update()
227  *
228  * purpose: continues an md5 digest operation, using the message block
229  *          to update the context.
230  *   input: MD5_CTX *	: the context to update
231  *          uint8_t *	: the message block
232  *          uint32_t    : the length of the message block in bytes
233  *  output: void
234  *
235  * MD5 crunches in 64-byte blocks.  All numeric constants here are related to
236  * that property of MD5.
237  */
238 
239 void
240 MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
241 {
242 	uint32_t		i, buf_index, buf_len;
243 #ifdef	sun4v
244 	uint32_t		old_asi;
245 #endif	/* sun4v */
246 	const unsigned char 	*input = (const unsigned char *)inpp;
247 
248 	/* compute (number of bytes computed so far) mod 64 */
249 	buf_index = (ctx->count[0] >> 3) & 0x3F;
250 
251 	/* update number of bits hashed into this MD5 computation so far */
252 	if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
253 	    ctx->count[1]++;
254 	ctx->count[1] += (input_len >> 29);
255 
256 	buf_len = 64 - buf_index;
257 
258 	/* transform as many times as possible */
259 	i = 0;
260 	if (input_len >= buf_len) {
261 
262 		/*
263 		 * general optimization:
264 		 *
265 		 * only do initial bcopy() and MD5Transform() if
266 		 * buf_index != 0.  if buf_index == 0, we're just
267 		 * wasting our time doing the bcopy() since there
268 		 * wasn't any data left over from a previous call to
269 		 * MD5Update().
270 		 */
271 
272 #ifdef sun4v
273 		/*
274 		 * For N1 use %asi register. However, costly to repeatedly set
275 		 * in MD5Transform. Therefore, set once here.
276 		 * Should probably restore the old value afterwards...
277 		 */
278 		old_asi = get_little();
279 		set_little(0x88);
280 #endif /* sun4v */
281 
282 		if (buf_index) {
283 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
284 
285 			MD5Transform(ctx->state[0], ctx->state[1],
286 			    ctx->state[2], ctx->state[3], ctx,
287 			    ctx->buf_un.buf8);
288 
289 			i = buf_len;
290 		}
291 
292 		for (; i + 63 < input_len; i += 64)
293 			MD5Transform(ctx->state[0], ctx->state[1],
294 			    ctx->state[2], ctx->state[3], ctx, &input[i]);
295 
296 
297 #ifdef sun4v
298 		/*
299 		 * Restore old %ASI value
300 		 */
301 		set_little(old_asi);
302 #endif /* sun4v */
303 
304 		/*
305 		 * general optimization:
306 		 *
307 		 * if i and input_len are the same, return now instead
308 		 * of calling bcopy(), since the bcopy() in this
309 		 * case will be an expensive nop.
310 		 */
311 
312 		if (input_len == i)
313 			return;
314 
315 		buf_index = 0;
316 	}
317 
318 	/* buffer remaining input */
319 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
320 }
321 
322 /*
323  * MD5Final()
324  *
325  * purpose: ends an md5 digest operation, finalizing the message digest and
326  *          zeroing the context.
327  *   input: uint8_t *	: a buffer to store the digest in
328  *          MD5_CTX *   : the context to finalize, save, and zero
329  *  output: void
330  */
331 
332 void
333 MD5Final(unsigned char *digest, MD5_CTX *ctx)
334 {
335 	uint8_t		bitcount_le[sizeof (ctx->count)];
336 	uint32_t	index = (ctx->count[0] >> 3) & 0x3f;
337 
338 	/* store bit count, little endian */
339 	Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
340 
341 	/* pad out to 56 mod 64 */
342 	MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
343 
344 	/* append length (before padding) */
345 	MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
346 
347 	/* store state in digest */
348 	Encode(digest, ctx->state, sizeof (ctx->state));
349 
350 	/* zeroize sensitive information */
351 	bzero(ctx, sizeof (*ctx));
352 }
353 
354 #ifndef	_KERNEL
355 
356 void
357 md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
358 {
359 	MD5_CTX context;
360 
361 	MD5Init(&context);
362 	MD5Update(&context, input, inlen);
363 	MD5Final(output, &context);
364 }
365 
366 #endif	/* !_KERNEL */
367 
368 /*
369  * sparc register window optimization:
370  *
371  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
372  * since it increases the number of registers available to the
373  * compiler.  under this scheme, these variables can be held in
374  * %i0 - %i3, which leaves more local and out registers available.
375  */
376 
377 /*
378  * MD5Transform()
379  *
380  * purpose: md5 transformation -- updates the digest based on `block'
381  *   input: uint32_t	: bytes  1 -  4 of the digest
382  *          uint32_t	: bytes  5 -  8 of the digest
383  *          uint32_t	: bytes  9 - 12 of the digest
384  *          uint32_t	: bytes 12 - 16 of the digest
385  *          MD5_CTX *   : the context to update
386  *          uint8_t [64]: the block to use to update the digest
387  *  output: void
388  */
389 
390 static void
391 MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
392     MD5_CTX *ctx, const uint8_t block[64])
393 {
394 	/*
395 	 * general optimization:
396 	 *
397 	 * use individual integers instead of using an array.  this is a
398 	 * win, although the amount it wins by seems to vary quite a bit.
399 	 */
400 
401 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
402 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
403 #ifdef sun4v
404 	unsigned long long 	*md5_consts64;
405 
406 		/* LINTED E_BAD_PTR_CAST_ALIGN */
407 	md5_consts64 = (unsigned long long *) md5_consts;
408 #endif	/* sun4v */
409 
410 	/*
411 	 * general optimization:
412 	 *
413 	 * the compiler (at least SC4.2/5.x) generates better code if
414 	 * variable use is localized.  in this case, swapping the integers in
415 	 * this order allows `x_0 'to be swapped nearest to its first use in
416 	 * FF(), and likewise for `x_1' and up.  note that the compiler
417 	 * prefers this to doing each swap right before the FF() that
418 	 * uses it.
419 	 */
420 
421 	/*
422 	 * sparc v9/v8plus optimization:
423 	 *
424 	 * if `block' is already aligned on a 4-byte boundary, use the
425 	 * optimized load_little_32() directly.  otherwise, bcopy()
426 	 * into a buffer that *is* aligned on a 4-byte boundary and
427 	 * then do the load_little_32() on that buffer.  benchmarks
428 	 * have shown that using the bcopy() is better than loading
429 	 * the bytes individually and doing the endian-swap by hand.
430 	 *
431 	 * even though it's quite tempting to assign to do:
432 	 *
433 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
434 	 *
435 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
436 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
437 	 */
438 
439 #ifdef _MD5_CHECK_ALIGNMENT
440 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
441 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
442 
443 #ifdef sun4v
444 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
445 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
446 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
447 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
448 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
449 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
450 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
451 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
452 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
453 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
454 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
455 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
456 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
457 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
458 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
459 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
460 #else
461 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
462 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
463 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
464 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
465 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
466 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
467 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
468 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
469 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
470 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
471 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
472 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
473 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
474 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
475 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
476 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
477 #endif /* sun4v */
478 	} else
479 #endif
480 	{
481 
482 #ifdef sun4v
483 		/* LINTED E_BAD_PTR_CAST_ALIGN */
484 		x_15 = LOAD_LITTLE_32_f(block);
485 		/* LINTED E_BAD_PTR_CAST_ALIGN */
486 		x_14 = LOAD_LITTLE_32_e(block);
487 		/* LINTED E_BAD_PTR_CAST_ALIGN */
488 		x_13 = LOAD_LITTLE_32_d(block);
489 		/* LINTED E_BAD_PTR_CAST_ALIGN */
490 		x_12 = LOAD_LITTLE_32_c(block);
491 		/* LINTED E_BAD_PTR_CAST_ALIGN */
492 		x_11 = LOAD_LITTLE_32_b(block);
493 		/* LINTED E_BAD_PTR_CAST_ALIGN */
494 		x_10 = LOAD_LITTLE_32_a(block);
495 		/* LINTED E_BAD_PTR_CAST_ALIGN */
496 		x_9  = LOAD_LITTLE_32_9(block);
497 		/* LINTED E_BAD_PTR_CAST_ALIGN */
498 		x_8  = LOAD_LITTLE_32_8(block);
499 		/* LINTED E_BAD_PTR_CAST_ALIGN */
500 		x_7  = LOAD_LITTLE_32_7(block);
501 		/* LINTED E_BAD_PTR_CAST_ALIGN */
502 		x_6  = LOAD_LITTLE_32_6(block);
503 		/* LINTED E_BAD_PTR_CAST_ALIGN */
504 		x_5  = LOAD_LITTLE_32_5(block);
505 		/* LINTED E_BAD_PTR_CAST_ALIGN */
506 		x_4  = LOAD_LITTLE_32_4(block);
507 		/* LINTED E_BAD_PTR_CAST_ALIGN */
508 		x_3  = LOAD_LITTLE_32_3(block);
509 		/* LINTED E_BAD_PTR_CAST_ALIGN */
510 		x_2  = LOAD_LITTLE_32_2(block);
511 		/* LINTED E_BAD_PTR_CAST_ALIGN */
512 		x_1  = LOAD_LITTLE_32_1(block);
513 		/* LINTED E_BAD_PTR_CAST_ALIGN */
514 		x_0  = LOAD_LITTLE_32_0(block);
515 #else
516 		/* LINTED E_BAD_PTR_CAST_ALIGN */
517 		x_15 = LOAD_LITTLE_32(block + 60);
518 		/* LINTED E_BAD_PTR_CAST_ALIGN */
519 		x_14 = LOAD_LITTLE_32(block + 56);
520 		/* LINTED E_BAD_PTR_CAST_ALIGN */
521 		x_13 = LOAD_LITTLE_32(block + 52);
522 		/* LINTED E_BAD_PTR_CAST_ALIGN */
523 		x_12 = LOAD_LITTLE_32(block + 48);
524 		/* LINTED E_BAD_PTR_CAST_ALIGN */
525 		x_11 = LOAD_LITTLE_32(block + 44);
526 		/* LINTED E_BAD_PTR_CAST_ALIGN */
527 		x_10 = LOAD_LITTLE_32(block + 40);
528 		/* LINTED E_BAD_PTR_CAST_ALIGN */
529 		x_9  = LOAD_LITTLE_32(block + 36);
530 		/* LINTED E_BAD_PTR_CAST_ALIGN */
531 		x_8  = LOAD_LITTLE_32(block + 32);
532 		/* LINTED E_BAD_PTR_CAST_ALIGN */
533 		x_7  = LOAD_LITTLE_32(block + 28);
534 		/* LINTED E_BAD_PTR_CAST_ALIGN */
535 		x_6  = LOAD_LITTLE_32(block + 24);
536 		/* LINTED E_BAD_PTR_CAST_ALIGN */
537 		x_5  = LOAD_LITTLE_32(block + 20);
538 		/* LINTED E_BAD_PTR_CAST_ALIGN */
539 		x_4  = LOAD_LITTLE_32(block + 16);
540 		/* LINTED E_BAD_PTR_CAST_ALIGN */
541 		x_3  = LOAD_LITTLE_32(block + 12);
542 		/* LINTED E_BAD_PTR_CAST_ALIGN */
543 		x_2  = LOAD_LITTLE_32(block +  8);
544 		/* LINTED E_BAD_PTR_CAST_ALIGN */
545 		x_1  = LOAD_LITTLE_32(block +  4);
546 		/* LINTED E_BAD_PTR_CAST_ALIGN */
547 		x_0  = LOAD_LITTLE_32(block +  0);
548 #endif /* sun4v */
549 	}
550 
551 	/* round 1 */
552 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
553 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
554 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
555 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
556 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
557 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
558 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
559 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
560 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
561 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
562 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
563 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
564 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
565 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
566 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
567 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
568 
569 	/* round 2 */
570 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
571 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
572 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
573 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
574 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
575 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
576 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
577 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
578 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
579 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
580 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
581 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
582 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
583 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
584 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
585 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
586 
587 	/* round 3 */
588 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
589 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
590 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
591 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
592 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
593 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
594 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
595 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
596 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
597 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
598 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
599 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
600 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
601 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
602 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
603 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
604 
605 	/* round 4 */
606 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
607 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
608 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
609 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
610 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
611 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
612 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
613 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
614 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
615 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
616 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
617 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
618 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
619 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
620 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
621 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
622 
623 	ctx->state[0] += a;
624 	ctx->state[1] += b;
625 	ctx->state[2] += c;
626 	ctx->state[3] += d;
627 
628 	/*
629 	 * zeroize sensitive information -- compiler will optimize
630 	 * this out if everything is kept in registers
631 	 */
632 
633 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
634 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
635 }
636 
637 /*
638  * Encode()
639  *
640  * purpose: to convert a list of numbers from big endian to little endian
641  *   input: uint8_t *	: place to store the converted little endian numbers
642  *	    uint32_t *	: place to get numbers to convert from
643  *          size_t	: the length of the input in bytes
644  *  output: void
645  */
646 
647 static void
648 Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
649     size_t input_len)
650 {
651 	size_t		i, j;
652 
653 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
654 
655 #ifdef _LITTLE_ENDIAN
656 
657 #ifdef _MD5_CHECK_ALIGNMENT
658 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
659 			bcopy(input + i, output + j, 4);
660 		else *(uint32_t *)(output + j) = input[i];
661 #else
662 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
663 		*(uint32_t *)(output + j) = input[i];
664 #endif /* _MD5_CHECK_ALIGNMENT */
665 
666 #else	/* big endian -- will work on little endian, but slowly */
667 
668 		output[j] = input[i] & 0xff;
669 		output[j + 1] = (input[i] >> 8)  & 0xff;
670 		output[j + 2] = (input[i] >> 16) & 0xff;
671 		output[j + 3] = (input[i] >> 24) & 0xff;
672 #endif
673 	}
674 }
675