17c478bd9Sstevel@tonic-gate /*
2*8de5c4f4SDan OpenSolaris Anderson * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * 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 /*
137c478bd9Sstevel@tonic-gate * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
147c478bd9Sstevel@tonic-gate */
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
187c478bd9Sstevel@tonic-gate * rights reserved.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * License to copy and use this software is granted provided that it
217c478bd9Sstevel@tonic-gate * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
227c478bd9Sstevel@tonic-gate * Algorithm" in all material mentioning or referencing this software
237c478bd9Sstevel@tonic-gate * or this function.
247c478bd9Sstevel@tonic-gate *
257c478bd9Sstevel@tonic-gate * License is also granted to make and use derivative works provided
267c478bd9Sstevel@tonic-gate * that such works are identified as "derived from the RSA Data
277c478bd9Sstevel@tonic-gate * Security, Inc. MD5 Message-Digest Algorithm" in all material
287c478bd9Sstevel@tonic-gate * mentioning or referencing the derived work.
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * RSA Data Security, Inc. makes no representations concerning either
317c478bd9Sstevel@tonic-gate * the merchantability of this software or the suitability of this
327c478bd9Sstevel@tonic-gate * software for any particular purpose. It is provided "as is"
337c478bd9Sstevel@tonic-gate * without express or implied warranty of any kind.
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * These notices must be retained in any copies of any part of this
367c478bd9Sstevel@tonic-gate * documentation and/or software.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
39*8de5c4f4SDan OpenSolaris Anderson #ifndef _KERNEL
40*8de5c4f4SDan OpenSolaris Anderson #include <stdint.h>
41*8de5c4f4SDan OpenSolaris Anderson #endif /* _KERNEL */
42*8de5c4f4SDan OpenSolaris Anderson
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/md5.h>
457c478bd9Sstevel@tonic-gate #include <sys/md5_consts.h> /* MD5_CONST() optimization */
46afd1ac7bSwesolows #include "md5_byteswap.h"
477c478bd9Sstevel@tonic-gate #if !defined(_KERNEL) || defined(_BOOT)
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate #endif /* !_KERNEL || _BOOT */
507c478bd9Sstevel@tonic-gate
51734b6a94Sdarrenm #ifdef _KERNEL
527c478bd9Sstevel@tonic-gate #include <sys/systm.h>
53734b6a94Sdarrenm #endif /* _KERNEL */
547c478bd9Sstevel@tonic-gate
55734b6a94Sdarrenm static void Encode(uint8_t *, const uint32_t *, size_t);
56160abee0Sda73024
57160abee0Sda73024 #if !defined(__amd64)
587c478bd9Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
597c478bd9Sstevel@tonic-gate const uint8_t [64]);
60160abee0Sda73024 #else
61160abee0Sda73024 void md5_block_asm_host_order(MD5_CTX *ctx, const void *inpp,
62160abee0Sda73024 unsigned int input_length_in_blocks);
63160abee0Sda73024 #endif /* !defined(__amd64) */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * F, G, H and I are the basic MD5 functions.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate #define F(b, c, d) (((b) & (c)) | ((~b) & (d)))
717c478bd9Sstevel@tonic-gate #define G(b, c, d) (((b) & (d)) | ((c) & (~d)))
727c478bd9Sstevel@tonic-gate #define H(b, c, d) ((b) ^ (c) ^ (d))
737c478bd9Sstevel@tonic-gate #define I(b, c, d) ((c) ^ ((b) | (~d)))
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * ROTATE_LEFT rotates x left n bits.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate #define ROTATE_LEFT(x, n) \
797c478bd9Sstevel@tonic-gate (((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
837c478bd9Sstevel@tonic-gate * Rotation is separate from addition to prevent recomputation.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #define FF(a, b, c, d, x, s, ac) { \
87554ff184Skais (a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
887c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
897c478bd9Sstevel@tonic-gate (a) += (b); \
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate #define GG(a, b, c, d, x, s, ac) { \
93554ff184Skais (a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
947c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
957c478bd9Sstevel@tonic-gate (a) += (b); \
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate #define HH(a, b, c, d, x, s, ac) { \
99554ff184Skais (a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
1007c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
1017c478bd9Sstevel@tonic-gate (a) += (b); \
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate #define II(a, b, c, d, x, s, ac) { \
105554ff184Skais (a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
1067c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
1077c478bd9Sstevel@tonic-gate (a) += (b); \
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * Loading 32-bit constants on a RISC is expensive since it involves both a
1127c478bd9Sstevel@tonic-gate * `sethi' and an `or'. thus, we instead have the compiler generate `ld's to
1137c478bd9Sstevel@tonic-gate * load the constants from an array called `md5_consts'. however, on intel
1147c478bd9Sstevel@tonic-gate * (and other CISC processors), it is cheaper to load the constant
1157c478bd9Sstevel@tonic-gate * directly. thus, the c code in MD5Transform() uses the macro MD5_CONST()
1167c478bd9Sstevel@tonic-gate * which either expands to a constant or an array reference, depending on the
1177c478bd9Sstevel@tonic-gate * architecture the code is being compiled for.
1187c478bd9Sstevel@tonic-gate *
1197c478bd9Sstevel@tonic-gate * Right now, i386 and amd64 are the CISC exceptions.
1207c478bd9Sstevel@tonic-gate * If we get another CISC ISA, we'll have to change the ifdef.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate #define MD5_CONST(x) (MD5_CONST_ ## x)
126554ff184Skais #define MD5_CONST_e(x) MD5_CONST(x)
127554ff184Skais #define MD5_CONST_o(x) MD5_CONST(x)
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate #else
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate * sparc/RISC optimization:
1327c478bd9Sstevel@tonic-gate *
1337c478bd9Sstevel@tonic-gate * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
1347c478bd9Sstevel@tonic-gate * machines), it is more efficient to place all the constants used in this
1357c478bd9Sstevel@tonic-gate * function in an array and load the values out of the array than to manually
1367c478bd9Sstevel@tonic-gate * load the constants. this is because setting a register to a 32-bit value
1377c478bd9Sstevel@tonic-gate * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
1387c478bd9Sstevel@tonic-gate * value from memory only takes one `ld' (or `lduw' on v9). while this
1397c478bd9Sstevel@tonic-gate * increases memory usage, the compiler can find enough other things to do
1407c478bd9Sstevel@tonic-gate * while waiting to keep the pipeline does not stall. additionally, it is
1417c478bd9Sstevel@tonic-gate * likely that many of these constants are cached so that later accesses do
1427c478bd9Sstevel@tonic-gate * not even go out to the bus.
1437c478bd9Sstevel@tonic-gate *
1447c478bd9Sstevel@tonic-gate * this array is declared `static' to keep the compiler from having to
1457c478bd9Sstevel@tonic-gate * bcopy() this array onto the stack frame of MD5Transform() each time it is
1467c478bd9Sstevel@tonic-gate * called -- which is unacceptably expensive.
1477c478bd9Sstevel@tonic-gate *
1487c478bd9Sstevel@tonic-gate * the `const' is to ensure that callers are good citizens and do not try to
1497c478bd9Sstevel@tonic-gate * munge the array. since these routines are going to be called from inside
1507c478bd9Sstevel@tonic-gate * multithreaded kernelland, this is a good safety check. -- `constants' will
1517c478bd9Sstevel@tonic-gate * end up in .rodata.
1527c478bd9Sstevel@tonic-gate *
1537c478bd9Sstevel@tonic-gate * unfortunately, loading from an array in this manner hurts performance under
1547c478bd9Sstevel@tonic-gate * intel (and presumably other CISC machines). so, there is a macro,
1557c478bd9Sstevel@tonic-gate * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
1567c478bd9Sstevel@tonic-gate * this array, or to the actual constant, depending on what platform this code
1577c478bd9Sstevel@tonic-gate * is compiled for.
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate
160554ff184Skais #ifdef sun4v
161554ff184Skais
162554ff184Skais /*
163554ff184Skais * Going to load these consts in 8B chunks, so need to enforce 8B alignment
164554ff184Skais */
165554ff184Skais
166554ff184Skais /* CSTYLED */
167554ff184Skais #pragma align 64 (md5_consts)
168b085fdc5Sfr80241 #define _MD5_CHECK_ALIGNMENT
169554ff184Skais
170554ff184Skais #endif /* sun4v */
171554ff184Skais
1727c478bd9Sstevel@tonic-gate static const uint32_t md5_consts[] = {
1737c478bd9Sstevel@tonic-gate MD5_CONST_0, MD5_CONST_1, MD5_CONST_2, MD5_CONST_3,
1747c478bd9Sstevel@tonic-gate MD5_CONST_4, MD5_CONST_5, MD5_CONST_6, MD5_CONST_7,
1757c478bd9Sstevel@tonic-gate MD5_CONST_8, MD5_CONST_9, MD5_CONST_10, MD5_CONST_11,
1767c478bd9Sstevel@tonic-gate MD5_CONST_12, MD5_CONST_13, MD5_CONST_14, MD5_CONST_15,
1777c478bd9Sstevel@tonic-gate MD5_CONST_16, MD5_CONST_17, MD5_CONST_18, MD5_CONST_19,
1787c478bd9Sstevel@tonic-gate MD5_CONST_20, MD5_CONST_21, MD5_CONST_22, MD5_CONST_23,
1797c478bd9Sstevel@tonic-gate MD5_CONST_24, MD5_CONST_25, MD5_CONST_26, MD5_CONST_27,
1807c478bd9Sstevel@tonic-gate MD5_CONST_28, MD5_CONST_29, MD5_CONST_30, MD5_CONST_31,
1817c478bd9Sstevel@tonic-gate MD5_CONST_32, MD5_CONST_33, MD5_CONST_34, MD5_CONST_35,
1827c478bd9Sstevel@tonic-gate MD5_CONST_36, MD5_CONST_37, MD5_CONST_38, MD5_CONST_39,
1837c478bd9Sstevel@tonic-gate MD5_CONST_40, MD5_CONST_41, MD5_CONST_42, MD5_CONST_43,
1847c478bd9Sstevel@tonic-gate MD5_CONST_44, MD5_CONST_45, MD5_CONST_46, MD5_CONST_47,
1857c478bd9Sstevel@tonic-gate MD5_CONST_48, MD5_CONST_49, MD5_CONST_50, MD5_CONST_51,
1867c478bd9Sstevel@tonic-gate MD5_CONST_52, MD5_CONST_53, MD5_CONST_54, MD5_CONST_55,
1877c478bd9Sstevel@tonic-gate MD5_CONST_56, MD5_CONST_57, MD5_CONST_58, MD5_CONST_59,
1887c478bd9Sstevel@tonic-gate MD5_CONST_60, MD5_CONST_61, MD5_CONST_62, MD5_CONST_63
1897c478bd9Sstevel@tonic-gate };
1907c478bd9Sstevel@tonic-gate
191554ff184Skais
192554ff184Skais #ifdef sun4v
193554ff184Skais /*
194554ff184Skais * To reduce the number of loads, load consts in 64-bit
195554ff184Skais * chunks and then split.
196554ff184Skais *
197554ff184Skais * No need to mask upper 32-bits, as just interested in
198554ff184Skais * low 32-bits (saves an & operation and means that this
199554ff184Skais * optimization doesn't increases the icount.
200554ff184Skais */
201554ff184Skais #define MD5_CONST_e(x) (md5_consts64[x/2] >> 32)
202554ff184Skais #define MD5_CONST_o(x) (md5_consts64[x/2])
203554ff184Skais
204554ff184Skais #else
205554ff184Skais
206554ff184Skais #define MD5_CONST_e(x) (md5_consts[x])
207554ff184Skais #define MD5_CONST_o(x) (md5_consts[x])
208554ff184Skais
209554ff184Skais #endif /* sun4v */
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * MD5Init()
2157c478bd9Sstevel@tonic-gate *
2167c478bd9Sstevel@tonic-gate * purpose: initializes the md5 context and begins and md5 digest operation
2177c478bd9Sstevel@tonic-gate * input: MD5_CTX * : the context to initialize.
2187c478bd9Sstevel@tonic-gate * output: void
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate void
MD5Init(MD5_CTX * ctx)2227c478bd9Sstevel@tonic-gate MD5Init(MD5_CTX *ctx)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate ctx->count[0] = ctx->count[1] = 0;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /* load magic initialization constants */
2277c478bd9Sstevel@tonic-gate ctx->state[0] = MD5_INIT_CONST_1;
2287c478bd9Sstevel@tonic-gate ctx->state[1] = MD5_INIT_CONST_2;
2297c478bd9Sstevel@tonic-gate ctx->state[2] = MD5_INIT_CONST_3;
2307c478bd9Sstevel@tonic-gate ctx->state[3] = MD5_INIT_CONST_4;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * MD5Update()
2357c478bd9Sstevel@tonic-gate *
2367c478bd9Sstevel@tonic-gate * purpose: continues an md5 digest operation, using the message block
2377c478bd9Sstevel@tonic-gate * to update the context.
2387c478bd9Sstevel@tonic-gate * input: MD5_CTX * : the context to update
2397c478bd9Sstevel@tonic-gate * uint8_t * : the message block
2407c478bd9Sstevel@tonic-gate * uint32_t : the length of the message block in bytes
2417c478bd9Sstevel@tonic-gate * output: void
2427c478bd9Sstevel@tonic-gate *
2437c478bd9Sstevel@tonic-gate * MD5 crunches in 64-byte blocks. All numeric constants here are related to
2447c478bd9Sstevel@tonic-gate * that property of MD5.
2457c478bd9Sstevel@tonic-gate */
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate void
MD5Update(MD5_CTX * ctx,const void * inpp,unsigned int input_len)2487c478bd9Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate uint32_t i, buf_index, buf_len;
251554ff184Skais #ifdef sun4v
252554ff184Skais uint32_t old_asi;
253554ff184Skais #endif /* sun4v */
254160abee0Sda73024 #if defined(__amd64)
255160abee0Sda73024 uint32_t block_count;
256160abee0Sda73024 #endif /* !defined(__amd64) */
2577c478bd9Sstevel@tonic-gate const unsigned char *input = (const unsigned char *)inpp;
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* compute (number of bytes computed so far) mod 64 */
2607c478bd9Sstevel@tonic-gate buf_index = (ctx->count[0] >> 3) & 0x3F;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /* update number of bits hashed into this MD5 computation so far */
2637c478bd9Sstevel@tonic-gate if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
2647c478bd9Sstevel@tonic-gate ctx->count[1]++;
2657c478bd9Sstevel@tonic-gate ctx->count[1] += (input_len >> 29);
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate buf_len = 64 - buf_index;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /* transform as many times as possible */
2707c478bd9Sstevel@tonic-gate i = 0;
2717c478bd9Sstevel@tonic-gate if (input_len >= buf_len) {
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * general optimization:
2757c478bd9Sstevel@tonic-gate *
2767c478bd9Sstevel@tonic-gate * only do initial bcopy() and MD5Transform() if
2777c478bd9Sstevel@tonic-gate * buf_index != 0. if buf_index == 0, we're just
2787c478bd9Sstevel@tonic-gate * wasting our time doing the bcopy() since there
2797c478bd9Sstevel@tonic-gate * wasn't any data left over from a previous call to
2807c478bd9Sstevel@tonic-gate * MD5Update().
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate
283554ff184Skais #ifdef sun4v
284554ff184Skais /*
285554ff184Skais * For N1 use %asi register. However, costly to repeatedly set
286554ff184Skais * in MD5Transform. Therefore, set once here.
287554ff184Skais * Should probably restore the old value afterwards...
288554ff184Skais */
289554ff184Skais old_asi = get_little();
290554ff184Skais set_little(0x88);
291554ff184Skais #endif /* sun4v */
292554ff184Skais
2937c478bd9Sstevel@tonic-gate if (buf_index) {
2947c478bd9Sstevel@tonic-gate bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
2957c478bd9Sstevel@tonic-gate
296160abee0Sda73024 #if !defined(__amd64)
2977c478bd9Sstevel@tonic-gate MD5Transform(ctx->state[0], ctx->state[1],
2987c478bd9Sstevel@tonic-gate ctx->state[2], ctx->state[3], ctx,
2997c478bd9Sstevel@tonic-gate ctx->buf_un.buf8);
300160abee0Sda73024 #else
301160abee0Sda73024 md5_block_asm_host_order(ctx, ctx->buf_un.buf8, 1);
302160abee0Sda73024 #endif /* !defined(__amd64) */
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate i = buf_len;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
307160abee0Sda73024 #if !defined(__amd64)
3087c478bd9Sstevel@tonic-gate for (; i + 63 < input_len; i += 64)
3097c478bd9Sstevel@tonic-gate MD5Transform(ctx->state[0], ctx->state[1],
3107c478bd9Sstevel@tonic-gate ctx->state[2], ctx->state[3], ctx, &input[i]);
3117c478bd9Sstevel@tonic-gate
312160abee0Sda73024 #else
313160abee0Sda73024 block_count = (input_len - i) >> 6;
314160abee0Sda73024 if (block_count > 0) {
315160abee0Sda73024 md5_block_asm_host_order(ctx, &input[i], block_count);
316160abee0Sda73024 i += block_count << 6;
317160abee0Sda73024 }
318160abee0Sda73024 #endif /* !defined(__amd64) */
319160abee0Sda73024
320554ff184Skais
321554ff184Skais #ifdef sun4v
322554ff184Skais /*
323554ff184Skais * Restore old %ASI value
324554ff184Skais */
325554ff184Skais set_little(old_asi);
326554ff184Skais #endif /* sun4v */
327554ff184Skais
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * general optimization:
3307c478bd9Sstevel@tonic-gate *
3317c478bd9Sstevel@tonic-gate * if i and input_len are the same, return now instead
3327c478bd9Sstevel@tonic-gate * of calling bcopy(), since the bcopy() in this
3337c478bd9Sstevel@tonic-gate * case will be an expensive nop.
3347c478bd9Sstevel@tonic-gate */
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate if (input_len == i)
3377c478bd9Sstevel@tonic-gate return;
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate buf_index = 0;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /* buffer remaining input */
3437c478bd9Sstevel@tonic-gate bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * MD5Final()
3487c478bd9Sstevel@tonic-gate *
3497c478bd9Sstevel@tonic-gate * purpose: ends an md5 digest operation, finalizing the message digest and
3507c478bd9Sstevel@tonic-gate * zeroing the context.
3515151fb12Sdarrenm * input: uchar_t * : a buffer to store the digest in
3525151fb12Sdarrenm * : The function actually uses void* because many
3535151fb12Sdarrenm * : callers pass things other than uchar_t here.
3547c478bd9Sstevel@tonic-gate * MD5_CTX * : the context to finalize, save, and zero
3557c478bd9Sstevel@tonic-gate * output: void
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate void
MD5Final(void * digest,MD5_CTX * ctx)3595151fb12Sdarrenm MD5Final(void *digest, MD5_CTX *ctx)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate uint8_t bitcount_le[sizeof (ctx->count)];
3627c478bd9Sstevel@tonic-gate uint32_t index = (ctx->count[0] >> 3) & 0x3f;
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /* store bit count, little endian */
3657c478bd9Sstevel@tonic-gate Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate /* pad out to 56 mod 64 */
3687c478bd9Sstevel@tonic-gate MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /* append length (before padding) */
3717c478bd9Sstevel@tonic-gate MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /* store state in digest */
3747c478bd9Sstevel@tonic-gate Encode(digest, ctx->state, sizeof (ctx->state));
375673007c6Sdarrenm
376673007c6Sdarrenm /* zeroize sensitive information */
377673007c6Sdarrenm bzero(ctx, sizeof (*ctx));
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate #ifndef _KERNEL
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate void
md5_calc(unsigned char * output,unsigned char * input,unsigned int inlen)3837c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate MD5_CTX context;
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate MD5Init(&context);
3887c478bd9Sstevel@tonic-gate MD5Update(&context, input, inlen);
3897c478bd9Sstevel@tonic-gate MD5Final(output, &context);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate #endif /* !_KERNEL */
3937c478bd9Sstevel@tonic-gate
394160abee0Sda73024 #if !defined(__amd64)
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * sparc register window optimization:
3977c478bd9Sstevel@tonic-gate *
3987c478bd9Sstevel@tonic-gate * `a', `b', `c', and `d' are passed into MD5Transform explicitly
3997c478bd9Sstevel@tonic-gate * since it increases the number of registers available to the
4007c478bd9Sstevel@tonic-gate * compiler. under this scheme, these variables can be held in
4017c478bd9Sstevel@tonic-gate * %i0 - %i3, which leaves more local and out registers available.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate * MD5Transform()
4067c478bd9Sstevel@tonic-gate *
4077c478bd9Sstevel@tonic-gate * purpose: md5 transformation -- updates the digest based on `block'
4087c478bd9Sstevel@tonic-gate * input: uint32_t : bytes 1 - 4 of the digest
4097c478bd9Sstevel@tonic-gate * uint32_t : bytes 5 - 8 of the digest
4107c478bd9Sstevel@tonic-gate * uint32_t : bytes 9 - 12 of the digest
4117c478bd9Sstevel@tonic-gate * uint32_t : bytes 12 - 16 of the digest
4127c478bd9Sstevel@tonic-gate * MD5_CTX * : the context to update
4137c478bd9Sstevel@tonic-gate * uint8_t [64]: the block to use to update the digest
4147c478bd9Sstevel@tonic-gate * output: void
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate static void
MD5Transform(uint32_t a,uint32_t b,uint32_t c,uint32_t d,MD5_CTX * ctx,const uint8_t block[64])4187c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
4197c478bd9Sstevel@tonic-gate MD5_CTX *ctx, const uint8_t block[64])
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate * general optimization:
4237c478bd9Sstevel@tonic-gate *
4247c478bd9Sstevel@tonic-gate * use individual integers instead of using an array. this is a
4257c478bd9Sstevel@tonic-gate * win, although the amount it wins by seems to vary quite a bit.
4267c478bd9Sstevel@tonic-gate */
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate register uint32_t x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7;
4297c478bd9Sstevel@tonic-gate register uint32_t x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
430554ff184Skais #ifdef sun4v
431554ff184Skais unsigned long long *md5_consts64;
432554ff184Skais
433734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
434554ff184Skais md5_consts64 = (unsigned long long *) md5_consts;
435554ff184Skais #endif /* sun4v */
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * general optimization:
4397c478bd9Sstevel@tonic-gate *
4407c478bd9Sstevel@tonic-gate * the compiler (at least SC4.2/5.x) generates better code if
4417c478bd9Sstevel@tonic-gate * variable use is localized. in this case, swapping the integers in
4427c478bd9Sstevel@tonic-gate * this order allows `x_0 'to be swapped nearest to its first use in
4437c478bd9Sstevel@tonic-gate * FF(), and likewise for `x_1' and up. note that the compiler
4447c478bd9Sstevel@tonic-gate * prefers this to doing each swap right before the FF() that
4457c478bd9Sstevel@tonic-gate * uses it.
4467c478bd9Sstevel@tonic-gate */
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate * sparc v9/v8plus optimization:
4507c478bd9Sstevel@tonic-gate *
4517c478bd9Sstevel@tonic-gate * if `block' is already aligned on a 4-byte boundary, use the
4527c478bd9Sstevel@tonic-gate * optimized load_little_32() directly. otherwise, bcopy()
4537c478bd9Sstevel@tonic-gate * into a buffer that *is* aligned on a 4-byte boundary and
4547c478bd9Sstevel@tonic-gate * then do the load_little_32() on that buffer. benchmarks
4557c478bd9Sstevel@tonic-gate * have shown that using the bcopy() is better than loading
4567c478bd9Sstevel@tonic-gate * the bytes individually and doing the endian-swap by hand.
4577c478bd9Sstevel@tonic-gate *
4587c478bd9Sstevel@tonic-gate * even though it's quite tempting to assign to do:
4597c478bd9Sstevel@tonic-gate *
4607c478bd9Sstevel@tonic-gate * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
4617c478bd9Sstevel@tonic-gate *
4627c478bd9Sstevel@tonic-gate * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
4637c478bd9Sstevel@tonic-gate * SC4.2/5.x) *does not* like that, so please resist the urge.
4647c478bd9Sstevel@tonic-gate */
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
4677c478bd9Sstevel@tonic-gate if ((uintptr_t)block & 0x3) { /* not 4-byte aligned? */
4687c478bd9Sstevel@tonic-gate bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
469554ff184Skais
470554ff184Skais #ifdef sun4v
471554ff184Skais x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
472554ff184Skais x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
473554ff184Skais x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
474554ff184Skais x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
475554ff184Skais x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
476554ff184Skais x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
477554ff184Skais x_9 = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
478554ff184Skais x_8 = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
479554ff184Skais x_7 = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
480554ff184Skais x_6 = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
481554ff184Skais x_5 = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
482554ff184Skais x_4 = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
483554ff184Skais x_3 = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
484554ff184Skais x_2 = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
485554ff184Skais x_1 = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
486554ff184Skais x_0 = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
487554ff184Skais #else
4887c478bd9Sstevel@tonic-gate x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
4897c478bd9Sstevel@tonic-gate x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
4907c478bd9Sstevel@tonic-gate x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
4917c478bd9Sstevel@tonic-gate x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
4927c478bd9Sstevel@tonic-gate x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
4937c478bd9Sstevel@tonic-gate x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
4947c478bd9Sstevel@tonic-gate x_9 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 9);
4957c478bd9Sstevel@tonic-gate x_8 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 8);
4967c478bd9Sstevel@tonic-gate x_7 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 7);
4977c478bd9Sstevel@tonic-gate x_6 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 6);
4987c478bd9Sstevel@tonic-gate x_5 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 5);
4997c478bd9Sstevel@tonic-gate x_4 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 4);
5007c478bd9Sstevel@tonic-gate x_3 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 3);
5017c478bd9Sstevel@tonic-gate x_2 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 2);
5027c478bd9Sstevel@tonic-gate x_1 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 1);
5037c478bd9Sstevel@tonic-gate x_0 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 0);
504554ff184Skais #endif /* sun4v */
5057c478bd9Sstevel@tonic-gate } else
5067c478bd9Sstevel@tonic-gate #endif
5077c478bd9Sstevel@tonic-gate {
508554ff184Skais
509554ff184Skais #ifdef sun4v
510734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
511554ff184Skais x_15 = LOAD_LITTLE_32_f(block);
512734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
513554ff184Skais x_14 = LOAD_LITTLE_32_e(block);
514734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
515554ff184Skais x_13 = LOAD_LITTLE_32_d(block);
516734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
517554ff184Skais x_12 = LOAD_LITTLE_32_c(block);
518734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
519554ff184Skais x_11 = LOAD_LITTLE_32_b(block);
520734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
521554ff184Skais x_10 = LOAD_LITTLE_32_a(block);
522734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
523554ff184Skais x_9 = LOAD_LITTLE_32_9(block);
524734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
525554ff184Skais x_8 = LOAD_LITTLE_32_8(block);
526734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
527554ff184Skais x_7 = LOAD_LITTLE_32_7(block);
528734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
529554ff184Skais x_6 = LOAD_LITTLE_32_6(block);
530734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
531554ff184Skais x_5 = LOAD_LITTLE_32_5(block);
532734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
533554ff184Skais x_4 = LOAD_LITTLE_32_4(block);
534734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
535554ff184Skais x_3 = LOAD_LITTLE_32_3(block);
536734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
537554ff184Skais x_2 = LOAD_LITTLE_32_2(block);
538734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
539554ff184Skais x_1 = LOAD_LITTLE_32_1(block);
540734b6a94Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */
541554ff184Skais x_0 = LOAD_LITTLE_32_0(block);
542554ff184Skais #else
5437c478bd9Sstevel@tonic-gate x_15 = LOAD_LITTLE_32(block + 60);
5447c478bd9Sstevel@tonic-gate x_14 = LOAD_LITTLE_32(block + 56);
5457c478bd9Sstevel@tonic-gate x_13 = LOAD_LITTLE_32(block + 52);
5467c478bd9Sstevel@tonic-gate x_12 = LOAD_LITTLE_32(block + 48);
5477c478bd9Sstevel@tonic-gate x_11 = LOAD_LITTLE_32(block + 44);
5487c478bd9Sstevel@tonic-gate x_10 = LOAD_LITTLE_32(block + 40);
5497c478bd9Sstevel@tonic-gate x_9 = LOAD_LITTLE_32(block + 36);
5507c478bd9Sstevel@tonic-gate x_8 = LOAD_LITTLE_32(block + 32);
5517c478bd9Sstevel@tonic-gate x_7 = LOAD_LITTLE_32(block + 28);
5527c478bd9Sstevel@tonic-gate x_6 = LOAD_LITTLE_32(block + 24);
5537c478bd9Sstevel@tonic-gate x_5 = LOAD_LITTLE_32(block + 20);
5547c478bd9Sstevel@tonic-gate x_4 = LOAD_LITTLE_32(block + 16);
5557c478bd9Sstevel@tonic-gate x_3 = LOAD_LITTLE_32(block + 12);
5567c478bd9Sstevel@tonic-gate x_2 = LOAD_LITTLE_32(block + 8);
5577c478bd9Sstevel@tonic-gate x_1 = LOAD_LITTLE_32(block + 4);
5587c478bd9Sstevel@tonic-gate x_0 = LOAD_LITTLE_32(block + 0);
559554ff184Skais #endif /* sun4v */
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /* round 1 */
563554ff184Skais FF(a, b, c, d, x_0, MD5_SHIFT_11, MD5_CONST_e(0)); /* 1 */
564554ff184Skais FF(d, a, b, c, x_1, MD5_SHIFT_12, MD5_CONST_o(1)); /* 2 */
565554ff184Skais FF(c, d, a, b, x_2, MD5_SHIFT_13, MD5_CONST_e(2)); /* 3 */
566554ff184Skais FF(b, c, d, a, x_3, MD5_SHIFT_14, MD5_CONST_o(3)); /* 4 */
567554ff184Skais FF(a, b, c, d, x_4, MD5_SHIFT_11, MD5_CONST_e(4)); /* 5 */
568554ff184Skais FF(d, a, b, c, x_5, MD5_SHIFT_12, MD5_CONST_o(5)); /* 6 */
569554ff184Skais FF(c, d, a, b, x_6, MD5_SHIFT_13, MD5_CONST_e(6)); /* 7 */
570554ff184Skais FF(b, c, d, a, x_7, MD5_SHIFT_14, MD5_CONST_o(7)); /* 8 */
571554ff184Skais FF(a, b, c, d, x_8, MD5_SHIFT_11, MD5_CONST_e(8)); /* 9 */
572554ff184Skais FF(d, a, b, c, x_9, MD5_SHIFT_12, MD5_CONST_o(9)); /* 10 */
573554ff184Skais FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
574554ff184Skais FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
575554ff184Skais FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
576554ff184Skais FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
577554ff184Skais FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
578554ff184Skais FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /* round 2 */
581554ff184Skais GG(a, b, c, d, x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
582554ff184Skais GG(d, a, b, c, x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
583554ff184Skais GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
584554ff184Skais GG(b, c, d, a, x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
585554ff184Skais GG(a, b, c, d, x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
586554ff184Skais GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
587554ff184Skais GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
588554ff184Skais GG(b, c, d, a, x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
589554ff184Skais GG(a, b, c, d, x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
590554ff184Skais GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
591554ff184Skais GG(c, d, a, b, x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
592554ff184Skais GG(b, c, d, a, x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
593554ff184Skais GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
594554ff184Skais GG(d, a, b, c, x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
595554ff184Skais GG(c, d, a, b, x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
596554ff184Skais GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate /* round 3 */
599554ff184Skais HH(a, b, c, d, x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
600554ff184Skais HH(d, a, b, c, x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
601554ff184Skais HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
602554ff184Skais HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
603554ff184Skais HH(a, b, c, d, x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
604554ff184Skais HH(d, a, b, c, x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
605554ff184Skais HH(c, d, a, b, x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
606554ff184Skais HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
607554ff184Skais HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
608554ff184Skais HH(d, a, b, c, x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
609554ff184Skais HH(c, d, a, b, x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
610554ff184Skais HH(b, c, d, a, x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
611554ff184Skais HH(a, b, c, d, x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
612554ff184Skais HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
613554ff184Skais HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
614554ff184Skais HH(b, c, d, a, x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate /* round 4 */
617554ff184Skais II(a, b, c, d, x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
618554ff184Skais II(d, a, b, c, x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
619554ff184Skais II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
620554ff184Skais II(b, c, d, a, x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
621554ff184Skais II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
622554ff184Skais II(d, a, b, c, x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
623554ff184Skais II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
624554ff184Skais II(b, c, d, a, x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
625554ff184Skais II(a, b, c, d, x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
626554ff184Skais II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
627554ff184Skais II(c, d, a, b, x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
628554ff184Skais II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
629554ff184Skais II(a, b, c, d, x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
630554ff184Skais II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
631554ff184Skais II(c, d, a, b, x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
632554ff184Skais II(b, c, d, a, x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate ctx->state[0] += a;
6357c478bd9Sstevel@tonic-gate ctx->state[1] += b;
6367c478bd9Sstevel@tonic-gate ctx->state[2] += c;
6377c478bd9Sstevel@tonic-gate ctx->state[3] += d;
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate * zeroize sensitive information -- compiler will optimize
6417c478bd9Sstevel@tonic-gate * this out if everything is kept in registers
6427c478bd9Sstevel@tonic-gate */
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate x_0 = x_1 = x_2 = x_3 = x_4 = x_5 = x_6 = x_7 = x_8 = 0;
6457c478bd9Sstevel@tonic-gate x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
6467c478bd9Sstevel@tonic-gate }
647160abee0Sda73024 #endif /* !defined(__amd64) */
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate * Encode()
6517c478bd9Sstevel@tonic-gate *
6527c478bd9Sstevel@tonic-gate * purpose: to convert a list of numbers from big endian to little endian
6537c478bd9Sstevel@tonic-gate * input: uint8_t * : place to store the converted little endian numbers
6547c478bd9Sstevel@tonic-gate * uint32_t * : place to get numbers to convert from
6557c478bd9Sstevel@tonic-gate * size_t : the length of the input in bytes
6567c478bd9Sstevel@tonic-gate * output: void
6577c478bd9Sstevel@tonic-gate */
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate static void
Encode(uint8_t * _RESTRICT_KYWD output,const uint32_t * _RESTRICT_KYWD input,size_t input_len)660734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input,
661734b6a94Sdarrenm size_t input_len)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate size_t i, j;
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
6707c478bd9Sstevel@tonic-gate if ((uintptr_t)output & 0x3) /* Not 4-byte aligned */
6717c478bd9Sstevel@tonic-gate bcopy(input + i, output + j, 4);
6727c478bd9Sstevel@tonic-gate else *(uint32_t *)(output + j) = input[i];
6737c478bd9Sstevel@tonic-gate #else
674734b6a94Sdarrenm /*LINTED E_BAD_PTR_CAST_ALIGN*/
6757c478bd9Sstevel@tonic-gate *(uint32_t *)(output + j) = input[i];
6767c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate #else /* big endian -- will work on little endian, but slowly */
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate output[j] = input[i] & 0xff;
6817c478bd9Sstevel@tonic-gate output[j + 1] = (input[i] >> 8) & 0xff;
6827c478bd9Sstevel@tonic-gate output[j + 2] = (input[i] >> 16) & 0xff;
6837c478bd9Sstevel@tonic-gate output[j + 3] = (input[i] >> 24) & 0xff;
6847c478bd9Sstevel@tonic-gate #endif
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate }
687