10Sstevel@tonic-gate /* 2*5764Sda73024 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Cleaned-up and optimized version of MD5, based on the reference 80Sstevel@tonic-gate * implementation provided in RFC 1321. See RSA Copyright information 90Sstevel@tonic-gate * below. 100Sstevel@tonic-gate */ 110Sstevel@tonic-gate 120Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 130Sstevel@tonic-gate 140Sstevel@tonic-gate /* 150Sstevel@tonic-gate * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate /* 190Sstevel@tonic-gate * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 200Sstevel@tonic-gate * rights reserved. 210Sstevel@tonic-gate * 220Sstevel@tonic-gate * License to copy and use this software is granted provided that it 230Sstevel@tonic-gate * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 240Sstevel@tonic-gate * Algorithm" in all material mentioning or referencing this software 250Sstevel@tonic-gate * or this function. 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * License is also granted to make and use derivative works provided 280Sstevel@tonic-gate * that such works are identified as "derived from the RSA Data 290Sstevel@tonic-gate * Security, Inc. MD5 Message-Digest Algorithm" in all material 300Sstevel@tonic-gate * mentioning or referencing the derived work. 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * RSA Data Security, Inc. makes no representations concerning either 330Sstevel@tonic-gate * the merchantability of this software or the suitability of this 340Sstevel@tonic-gate * software for any particular purpose. It is provided "as is" 350Sstevel@tonic-gate * without express or implied warranty of any kind. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * These notices must be retained in any copies of any part of this 380Sstevel@tonic-gate * documentation and/or software. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/md5.h> 430Sstevel@tonic-gate #include <sys/md5_consts.h> /* MD5_CONST() optimization */ 441015Swesolows #include "md5_byteswap.h" 450Sstevel@tonic-gate #if !defined(_KERNEL) || defined(_BOOT) 460Sstevel@tonic-gate #include <strings.h> 470Sstevel@tonic-gate #endif /* !_KERNEL || _BOOT */ 480Sstevel@tonic-gate 491694Sdarrenm #ifdef _KERNEL 500Sstevel@tonic-gate #include <sys/systm.h> 511694Sdarrenm #endif /* _KERNEL */ 520Sstevel@tonic-gate 531694Sdarrenm static void Encode(uint8_t *, const uint32_t *, size_t); 54*5764Sda73024 55*5764Sda73024 #if !defined(__amd64) 560Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *, 570Sstevel@tonic-gate const uint8_t [64]); 58*5764Sda73024 #else 59*5764Sda73024 void md5_block_asm_host_order(MD5_CTX *ctx, const void *inpp, 60*5764Sda73024 unsigned int input_length_in_blocks); 61*5764Sda73024 #endif /* !defined(__amd64) */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ }; 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * F, G, H and I are the basic MD5 functions. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate #define F(b, c, d) (((b) & (c)) | ((~b) & (d))) 690Sstevel@tonic-gate #define G(b, c, d) (((b) & (d)) | ((c) & (~d))) 700Sstevel@tonic-gate #define H(b, c, d) ((b) ^ (c) ^ (d)) 710Sstevel@tonic-gate #define I(b, c, d) ((c) ^ ((b) | (~d))) 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * ROTATE_LEFT rotates x left n bits. 750Sstevel@tonic-gate */ 760Sstevel@tonic-gate #define ROTATE_LEFT(x, n) \ 770Sstevel@tonic-gate (((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n)))) 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 810Sstevel@tonic-gate * Rotation is separate from addition to prevent recomputation. 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define FF(a, b, c, d, x, s, ac) { \ 85227Skais (a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \ 860Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \ 870Sstevel@tonic-gate (a) += (b); \ 880Sstevel@tonic-gate } 890Sstevel@tonic-gate 900Sstevel@tonic-gate #define GG(a, b, c, d, x, s, ac) { \ 91227Skais (a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \ 920Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \ 930Sstevel@tonic-gate (a) += (b); \ 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define HH(a, b, c, d, x, s, ac) { \ 97227Skais (a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \ 980Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \ 990Sstevel@tonic-gate (a) += (b); \ 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #define II(a, b, c, d, x, s, ac) { \ 103227Skais (a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \ 1040Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \ 1050Sstevel@tonic-gate (a) += (b); \ 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * Loading 32-bit constants on a RISC is expensive since it involves both a 1100Sstevel@tonic-gate * `sethi' and an `or'. thus, we instead have the compiler generate `ld's to 1110Sstevel@tonic-gate * load the constants from an array called `md5_consts'. however, on intel 1120Sstevel@tonic-gate * (and other CISC processors), it is cheaper to load the constant 1130Sstevel@tonic-gate * directly. thus, the c code in MD5Transform() uses the macro MD5_CONST() 1140Sstevel@tonic-gate * which either expands to a constant or an array reference, depending on the 1150Sstevel@tonic-gate * architecture the code is being compiled for. 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * Right now, i386 and amd64 are the CISC exceptions. 1180Sstevel@tonic-gate * If we get another CISC ISA, we'll have to change the ifdef. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #define MD5_CONST(x) (MD5_CONST_ ## x) 124227Skais #define MD5_CONST_e(x) MD5_CONST(x) 125227Skais #define MD5_CONST_o(x) MD5_CONST(x) 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #else 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * sparc/RISC optimization: 1300Sstevel@tonic-gate * 1310Sstevel@tonic-gate * while it is somewhat counter-intuitive, on sparc (and presumably other RISC 1320Sstevel@tonic-gate * machines), it is more efficient to place all the constants used in this 1330Sstevel@tonic-gate * function in an array and load the values out of the array than to manually 1340Sstevel@tonic-gate * load the constants. this is because setting a register to a 32-bit value 1350Sstevel@tonic-gate * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit 1360Sstevel@tonic-gate * value from memory only takes one `ld' (or `lduw' on v9). while this 1370Sstevel@tonic-gate * increases memory usage, the compiler can find enough other things to do 1380Sstevel@tonic-gate * while waiting to keep the pipeline does not stall. additionally, it is 1390Sstevel@tonic-gate * likely that many of these constants are cached so that later accesses do 1400Sstevel@tonic-gate * not even go out to the bus. 1410Sstevel@tonic-gate * 1420Sstevel@tonic-gate * this array is declared `static' to keep the compiler from having to 1430Sstevel@tonic-gate * bcopy() this array onto the stack frame of MD5Transform() each time it is 1440Sstevel@tonic-gate * called -- which is unacceptably expensive. 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * the `const' is to ensure that callers are good citizens and do not try to 1470Sstevel@tonic-gate * munge the array. since these routines are going to be called from inside 1480Sstevel@tonic-gate * multithreaded kernelland, this is a good safety check. -- `constants' will 1490Sstevel@tonic-gate * end up in .rodata. 1500Sstevel@tonic-gate * 1510Sstevel@tonic-gate * unfortunately, loading from an array in this manner hurts performance under 1520Sstevel@tonic-gate * intel (and presumably other CISC machines). so, there is a macro, 1530Sstevel@tonic-gate * MD5_CONST(), used in MD5Transform(), that either expands to a reference to 1540Sstevel@tonic-gate * this array, or to the actual constant, depending on what platform this code 1550Sstevel@tonic-gate * is compiled for. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate 158227Skais #ifdef sun4v 159227Skais 160227Skais /* 161227Skais * Going to load these consts in 8B chunks, so need to enforce 8B alignment 162227Skais */ 163227Skais 164227Skais /* CSTYLED */ 165227Skais #pragma align 64 (md5_consts) 1662782Sfr80241 #define _MD5_CHECK_ALIGNMENT 167227Skais 168227Skais #endif /* sun4v */ 169227Skais 1700Sstevel@tonic-gate static const uint32_t md5_consts[] = { 1710Sstevel@tonic-gate MD5_CONST_0, MD5_CONST_1, MD5_CONST_2, MD5_CONST_3, 1720Sstevel@tonic-gate MD5_CONST_4, MD5_CONST_5, MD5_CONST_6, MD5_CONST_7, 1730Sstevel@tonic-gate MD5_CONST_8, MD5_CONST_9, MD5_CONST_10, MD5_CONST_11, 1740Sstevel@tonic-gate MD5_CONST_12, MD5_CONST_13, MD5_CONST_14, MD5_CONST_15, 1750Sstevel@tonic-gate MD5_CONST_16, MD5_CONST_17, MD5_CONST_18, MD5_CONST_19, 1760Sstevel@tonic-gate MD5_CONST_20, MD5_CONST_21, MD5_CONST_22, MD5_CONST_23, 1770Sstevel@tonic-gate MD5_CONST_24, MD5_CONST_25, MD5_CONST_26, MD5_CONST_27, 1780Sstevel@tonic-gate MD5_CONST_28, MD5_CONST_29, MD5_CONST_30, MD5_CONST_31, 1790Sstevel@tonic-gate MD5_CONST_32, MD5_CONST_33, MD5_CONST_34, MD5_CONST_35, 1800Sstevel@tonic-gate MD5_CONST_36, MD5_CONST_37, MD5_CONST_38, MD5_CONST_39, 1810Sstevel@tonic-gate MD5_CONST_40, MD5_CONST_41, MD5_CONST_42, MD5_CONST_43, 1820Sstevel@tonic-gate MD5_CONST_44, MD5_CONST_45, MD5_CONST_46, MD5_CONST_47, 1830Sstevel@tonic-gate MD5_CONST_48, MD5_CONST_49, MD5_CONST_50, MD5_CONST_51, 1840Sstevel@tonic-gate MD5_CONST_52, MD5_CONST_53, MD5_CONST_54, MD5_CONST_55, 1850Sstevel@tonic-gate MD5_CONST_56, MD5_CONST_57, MD5_CONST_58, MD5_CONST_59, 1860Sstevel@tonic-gate MD5_CONST_60, MD5_CONST_61, MD5_CONST_62, MD5_CONST_63 1870Sstevel@tonic-gate }; 1880Sstevel@tonic-gate 189227Skais 190227Skais #ifdef sun4v 191227Skais /* 192227Skais * To reduce the number of loads, load consts in 64-bit 193227Skais * chunks and then split. 194227Skais * 195227Skais * No need to mask upper 32-bits, as just interested in 196227Skais * low 32-bits (saves an & operation and means that this 197227Skais * optimization doesn't increases the icount. 198227Skais */ 199227Skais #define MD5_CONST_e(x) (md5_consts64[x/2] >> 32) 200227Skais #define MD5_CONST_o(x) (md5_consts64[x/2]) 201227Skais 202227Skais #else 203227Skais 204227Skais #define MD5_CONST_e(x) (md5_consts[x]) 205227Skais #define MD5_CONST_o(x) (md5_consts[x]) 206227Skais 207227Skais #endif /* sun4v */ 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate #endif 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * MD5Init() 2130Sstevel@tonic-gate * 2140Sstevel@tonic-gate * purpose: initializes the md5 context and begins and md5 digest operation 2150Sstevel@tonic-gate * input: MD5_CTX * : the context to initialize. 2160Sstevel@tonic-gate * output: void 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate void 2200Sstevel@tonic-gate MD5Init(MD5_CTX *ctx) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate ctx->count[0] = ctx->count[1] = 0; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* load magic initialization constants */ 2250Sstevel@tonic-gate ctx->state[0] = MD5_INIT_CONST_1; 2260Sstevel@tonic-gate ctx->state[1] = MD5_INIT_CONST_2; 2270Sstevel@tonic-gate ctx->state[2] = MD5_INIT_CONST_3; 2280Sstevel@tonic-gate ctx->state[3] = MD5_INIT_CONST_4; 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * MD5Update() 2330Sstevel@tonic-gate * 2340Sstevel@tonic-gate * purpose: continues an md5 digest operation, using the message block 2350Sstevel@tonic-gate * to update the context. 2360Sstevel@tonic-gate * input: MD5_CTX * : the context to update 2370Sstevel@tonic-gate * uint8_t * : the message block 2380Sstevel@tonic-gate * uint32_t : the length of the message block in bytes 2390Sstevel@tonic-gate * output: void 2400Sstevel@tonic-gate * 2410Sstevel@tonic-gate * MD5 crunches in 64-byte blocks. All numeric constants here are related to 2420Sstevel@tonic-gate * that property of MD5. 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate void 2460Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate uint32_t i, buf_index, buf_len; 249227Skais #ifdef sun4v 250227Skais uint32_t old_asi; 251227Skais #endif /* sun4v */ 252*5764Sda73024 #if defined(__amd64) 253*5764Sda73024 uint32_t block_count; 254*5764Sda73024 #endif /* !defined(__amd64) */ 2550Sstevel@tonic-gate const unsigned char *input = (const unsigned char *)inpp; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* compute (number of bytes computed so far) mod 64 */ 2580Sstevel@tonic-gate buf_index = (ctx->count[0] >> 3) & 0x3F; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* update number of bits hashed into this MD5 computation so far */ 2610Sstevel@tonic-gate if ((ctx->count[0] += (input_len << 3)) < (input_len << 3)) 262*5764Sda73024 ctx->count[1]++; 2630Sstevel@tonic-gate ctx->count[1] += (input_len >> 29); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate buf_len = 64 - buf_index; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* transform as many times as possible */ 2680Sstevel@tonic-gate i = 0; 2690Sstevel@tonic-gate if (input_len >= buf_len) { 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * general optimization: 2730Sstevel@tonic-gate * 2740Sstevel@tonic-gate * only do initial bcopy() and MD5Transform() if 2750Sstevel@tonic-gate * buf_index != 0. if buf_index == 0, we're just 2760Sstevel@tonic-gate * wasting our time doing the bcopy() since there 2770Sstevel@tonic-gate * wasn't any data left over from a previous call to 2780Sstevel@tonic-gate * MD5Update(). 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate 281227Skais #ifdef sun4v 282227Skais /* 283227Skais * For N1 use %asi register. However, costly to repeatedly set 284227Skais * in MD5Transform. Therefore, set once here. 285227Skais * Should probably restore the old value afterwards... 286227Skais */ 287227Skais old_asi = get_little(); 288227Skais set_little(0x88); 289227Skais #endif /* sun4v */ 290227Skais 2910Sstevel@tonic-gate if (buf_index) { 2920Sstevel@tonic-gate bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len); 2930Sstevel@tonic-gate 294*5764Sda73024 #if !defined(__amd64) 2950Sstevel@tonic-gate MD5Transform(ctx->state[0], ctx->state[1], 2960Sstevel@tonic-gate ctx->state[2], ctx->state[3], ctx, 2970Sstevel@tonic-gate ctx->buf_un.buf8); 298*5764Sda73024 #else 299*5764Sda73024 md5_block_asm_host_order(ctx, ctx->buf_un.buf8, 1); 300*5764Sda73024 #endif /* !defined(__amd64) */ 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate i = buf_len; 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 305*5764Sda73024 #if !defined(__amd64) 3060Sstevel@tonic-gate for (; i + 63 < input_len; i += 64) 3070Sstevel@tonic-gate MD5Transform(ctx->state[0], ctx->state[1], 3080Sstevel@tonic-gate ctx->state[2], ctx->state[3], ctx, &input[i]); 3090Sstevel@tonic-gate 310*5764Sda73024 #else 311*5764Sda73024 block_count = (input_len - i) >> 6; 312*5764Sda73024 if (block_count > 0) { 313*5764Sda73024 md5_block_asm_host_order(ctx, &input[i], block_count); 314*5764Sda73024 i += block_count << 6; 315*5764Sda73024 } 316*5764Sda73024 #endif /* !defined(__amd64) */ 317*5764Sda73024 318227Skais 319227Skais #ifdef sun4v 320227Skais /* 321227Skais * Restore old %ASI value 322227Skais */ 323227Skais set_little(old_asi); 324227Skais #endif /* sun4v */ 325227Skais 3260Sstevel@tonic-gate /* 3270Sstevel@tonic-gate * general optimization: 3280Sstevel@tonic-gate * 3290Sstevel@tonic-gate * if i and input_len are the same, return now instead 3300Sstevel@tonic-gate * of calling bcopy(), since the bcopy() in this 3310Sstevel@tonic-gate * case will be an expensive nop. 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate if (input_len == i) 3350Sstevel@tonic-gate return; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate buf_index = 0; 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate /* buffer remaining input */ 3410Sstevel@tonic-gate bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * MD5Final() 3460Sstevel@tonic-gate * 3470Sstevel@tonic-gate * purpose: ends an md5 digest operation, finalizing the message digest and 3480Sstevel@tonic-gate * zeroing the context. 3494002Sdarrenm * input: uchar_t * : a buffer to store the digest in 3504002Sdarrenm * : The function actually uses void* because many 3514002Sdarrenm * : callers pass things other than uchar_t here. 3520Sstevel@tonic-gate * MD5_CTX * : the context to finalize, save, and zero 3530Sstevel@tonic-gate * output: void 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate void 3574002Sdarrenm MD5Final(void *digest, MD5_CTX *ctx) 3580Sstevel@tonic-gate { 3590Sstevel@tonic-gate uint8_t bitcount_le[sizeof (ctx->count)]; 3600Sstevel@tonic-gate uint32_t index = (ctx->count[0] >> 3) & 0x3f; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate /* store bit count, little endian */ 3630Sstevel@tonic-gate Encode(bitcount_le, ctx->count, sizeof (bitcount_le)); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* pad out to 56 mod 64 */ 3660Sstevel@tonic-gate MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index); 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* append length (before padding) */ 3690Sstevel@tonic-gate MD5Update(ctx, bitcount_le, sizeof (bitcount_le)); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* store state in digest */ 3720Sstevel@tonic-gate Encode(digest, ctx->state, sizeof (ctx->state)); 3731551Sdarrenm 3741551Sdarrenm /* zeroize sensitive information */ 3751551Sdarrenm bzero(ctx, sizeof (*ctx)); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate #ifndef _KERNEL 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate void 3810Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen) 3820Sstevel@tonic-gate { 3830Sstevel@tonic-gate MD5_CTX context; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate MD5Init(&context); 3860Sstevel@tonic-gate MD5Update(&context, input, inlen); 3870Sstevel@tonic-gate MD5Final(output, &context); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate #endif /* !_KERNEL */ 3910Sstevel@tonic-gate 392*5764Sda73024 #if !defined(__amd64) 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * sparc register window optimization: 3950Sstevel@tonic-gate * 3960Sstevel@tonic-gate * `a', `b', `c', and `d' are passed into MD5Transform explicitly 3970Sstevel@tonic-gate * since it increases the number of registers available to the 3980Sstevel@tonic-gate * compiler. under this scheme, these variables can be held in 3990Sstevel@tonic-gate * %i0 - %i3, which leaves more local and out registers available. 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* 4030Sstevel@tonic-gate * MD5Transform() 4040Sstevel@tonic-gate * 4050Sstevel@tonic-gate * purpose: md5 transformation -- updates the digest based on `block' 4060Sstevel@tonic-gate * input: uint32_t : bytes 1 - 4 of the digest 4070Sstevel@tonic-gate * uint32_t : bytes 5 - 8 of the digest 4080Sstevel@tonic-gate * uint32_t : bytes 9 - 12 of the digest 4090Sstevel@tonic-gate * uint32_t : bytes 12 - 16 of the digest 4100Sstevel@tonic-gate * MD5_CTX * : the context to update 4110Sstevel@tonic-gate * uint8_t [64]: the block to use to update the digest 4120Sstevel@tonic-gate * output: void 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate static void 4160Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, 4170Sstevel@tonic-gate MD5_CTX *ctx, const uint8_t block[64]) 4180Sstevel@tonic-gate { 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * general optimization: 4210Sstevel@tonic-gate * 4220Sstevel@tonic-gate * use individual integers instead of using an array. this is a 4230Sstevel@tonic-gate * win, although the amount it wins by seems to vary quite a bit. 4240Sstevel@tonic-gate */ 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate register uint32_t x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7; 4270Sstevel@tonic-gate register uint32_t x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15; 428227Skais #ifdef sun4v 429227Skais unsigned long long *md5_consts64; 430227Skais 4311694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 432227Skais md5_consts64 = (unsigned long long *) md5_consts; 433227Skais #endif /* sun4v */ 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * general optimization: 4370Sstevel@tonic-gate * 4380Sstevel@tonic-gate * the compiler (at least SC4.2/5.x) generates better code if 4390Sstevel@tonic-gate * variable use is localized. in this case, swapping the integers in 4400Sstevel@tonic-gate * this order allows `x_0 'to be swapped nearest to its first use in 4410Sstevel@tonic-gate * FF(), and likewise for `x_1' and up. note that the compiler 4420Sstevel@tonic-gate * prefers this to doing each swap right before the FF() that 4430Sstevel@tonic-gate * uses it. 4440Sstevel@tonic-gate */ 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * sparc v9/v8plus optimization: 4480Sstevel@tonic-gate * 4490Sstevel@tonic-gate * if `block' is already aligned on a 4-byte boundary, use the 4500Sstevel@tonic-gate * optimized load_little_32() directly. otherwise, bcopy() 4510Sstevel@tonic-gate * into a buffer that *is* aligned on a 4-byte boundary and 4520Sstevel@tonic-gate * then do the load_little_32() on that buffer. benchmarks 4530Sstevel@tonic-gate * have shown that using the bcopy() is better than loading 4540Sstevel@tonic-gate * the bytes individually and doing the endian-swap by hand. 4550Sstevel@tonic-gate * 4560Sstevel@tonic-gate * even though it's quite tempting to assign to do: 4570Sstevel@tonic-gate * 4580Sstevel@tonic-gate * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32)); 4590Sstevel@tonic-gate * 4600Sstevel@tonic-gate * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least 4610Sstevel@tonic-gate * SC4.2/5.x) *does not* like that, so please resist the urge. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT 4650Sstevel@tonic-gate if ((uintptr_t)block & 0x3) { /* not 4-byte aligned? */ 4660Sstevel@tonic-gate bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32)); 467227Skais 468227Skais #ifdef sun4v 469227Skais x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32); 470227Skais x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32); 471227Skais x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32); 472227Skais x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32); 473227Skais x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32); 474227Skais x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32); 475227Skais x_9 = LOAD_LITTLE_32_9(ctx->buf_un.buf32); 476227Skais x_8 = LOAD_LITTLE_32_8(ctx->buf_un.buf32); 477227Skais x_7 = LOAD_LITTLE_32_7(ctx->buf_un.buf32); 478227Skais x_6 = LOAD_LITTLE_32_6(ctx->buf_un.buf32); 479227Skais x_5 = LOAD_LITTLE_32_5(ctx->buf_un.buf32); 480227Skais x_4 = LOAD_LITTLE_32_4(ctx->buf_un.buf32); 481227Skais x_3 = LOAD_LITTLE_32_3(ctx->buf_un.buf32); 482227Skais x_2 = LOAD_LITTLE_32_2(ctx->buf_un.buf32); 483227Skais x_1 = LOAD_LITTLE_32_1(ctx->buf_un.buf32); 484227Skais x_0 = LOAD_LITTLE_32_0(ctx->buf_un.buf32); 485227Skais #else 4860Sstevel@tonic-gate x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15); 4870Sstevel@tonic-gate x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14); 4880Sstevel@tonic-gate x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13); 4890Sstevel@tonic-gate x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12); 4900Sstevel@tonic-gate x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11); 4910Sstevel@tonic-gate x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10); 4920Sstevel@tonic-gate x_9 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 9); 4930Sstevel@tonic-gate x_8 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 8); 4940Sstevel@tonic-gate x_7 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 7); 4950Sstevel@tonic-gate x_6 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 6); 4960Sstevel@tonic-gate x_5 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 5); 4970Sstevel@tonic-gate x_4 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 4); 4980Sstevel@tonic-gate x_3 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 3); 4990Sstevel@tonic-gate x_2 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 2); 5000Sstevel@tonic-gate x_1 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 1); 5010Sstevel@tonic-gate x_0 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 0); 502227Skais #endif /* sun4v */ 5030Sstevel@tonic-gate } else 5040Sstevel@tonic-gate #endif 5050Sstevel@tonic-gate { 506227Skais 507227Skais #ifdef sun4v 5081694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 509227Skais x_15 = LOAD_LITTLE_32_f(block); 5101694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 511227Skais x_14 = LOAD_LITTLE_32_e(block); 5121694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 513227Skais x_13 = LOAD_LITTLE_32_d(block); 5141694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 515227Skais x_12 = LOAD_LITTLE_32_c(block); 5161694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 517227Skais x_11 = LOAD_LITTLE_32_b(block); 5181694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 519227Skais x_10 = LOAD_LITTLE_32_a(block); 5201694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 521227Skais x_9 = LOAD_LITTLE_32_9(block); 5221694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 523227Skais x_8 = LOAD_LITTLE_32_8(block); 5241694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 525227Skais x_7 = LOAD_LITTLE_32_7(block); 5261694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 527227Skais x_6 = LOAD_LITTLE_32_6(block); 5281694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 529227Skais x_5 = LOAD_LITTLE_32_5(block); 5301694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 531227Skais x_4 = LOAD_LITTLE_32_4(block); 5321694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 533227Skais x_3 = LOAD_LITTLE_32_3(block); 5341694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 535227Skais x_2 = LOAD_LITTLE_32_2(block); 5361694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 537227Skais x_1 = LOAD_LITTLE_32_1(block); 5381694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 539227Skais x_0 = LOAD_LITTLE_32_0(block); 540227Skais #else 5411694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5420Sstevel@tonic-gate x_15 = LOAD_LITTLE_32(block + 60); 5431694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5440Sstevel@tonic-gate x_14 = LOAD_LITTLE_32(block + 56); 5451694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5460Sstevel@tonic-gate x_13 = LOAD_LITTLE_32(block + 52); 5471694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5480Sstevel@tonic-gate x_12 = LOAD_LITTLE_32(block + 48); 5491694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5500Sstevel@tonic-gate x_11 = LOAD_LITTLE_32(block + 44); 5511694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5520Sstevel@tonic-gate x_10 = LOAD_LITTLE_32(block + 40); 5531694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5540Sstevel@tonic-gate x_9 = LOAD_LITTLE_32(block + 36); 5551694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5560Sstevel@tonic-gate x_8 = LOAD_LITTLE_32(block + 32); 5571694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5580Sstevel@tonic-gate x_7 = LOAD_LITTLE_32(block + 28); 5591694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5600Sstevel@tonic-gate x_6 = LOAD_LITTLE_32(block + 24); 5611694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5620Sstevel@tonic-gate x_5 = LOAD_LITTLE_32(block + 20); 5631694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5640Sstevel@tonic-gate x_4 = LOAD_LITTLE_32(block + 16); 5651694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5660Sstevel@tonic-gate x_3 = LOAD_LITTLE_32(block + 12); 5671694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5680Sstevel@tonic-gate x_2 = LOAD_LITTLE_32(block + 8); 5691694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5700Sstevel@tonic-gate x_1 = LOAD_LITTLE_32(block + 4); 5711694Sdarrenm /* LINTED E_BAD_PTR_CAST_ALIGN */ 5720Sstevel@tonic-gate x_0 = LOAD_LITTLE_32(block + 0); 573227Skais #endif /* sun4v */ 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate /* round 1 */ 577227Skais FF(a, b, c, d, x_0, MD5_SHIFT_11, MD5_CONST_e(0)); /* 1 */ 578227Skais FF(d, a, b, c, x_1, MD5_SHIFT_12, MD5_CONST_o(1)); /* 2 */ 579227Skais FF(c, d, a, b, x_2, MD5_SHIFT_13, MD5_CONST_e(2)); /* 3 */ 580227Skais FF(b, c, d, a, x_3, MD5_SHIFT_14, MD5_CONST_o(3)); /* 4 */ 581227Skais FF(a, b, c, d, x_4, MD5_SHIFT_11, MD5_CONST_e(4)); /* 5 */ 582227Skais FF(d, a, b, c, x_5, MD5_SHIFT_12, MD5_CONST_o(5)); /* 6 */ 583227Skais FF(c, d, a, b, x_6, MD5_SHIFT_13, MD5_CONST_e(6)); /* 7 */ 584227Skais FF(b, c, d, a, x_7, MD5_SHIFT_14, MD5_CONST_o(7)); /* 8 */ 585227Skais FF(a, b, c, d, x_8, MD5_SHIFT_11, MD5_CONST_e(8)); /* 9 */ 586227Skais FF(d, a, b, c, x_9, MD5_SHIFT_12, MD5_CONST_o(9)); /* 10 */ 587227Skais FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */ 588227Skais FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */ 589227Skais FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */ 590227Skais FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */ 591227Skais FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */ 592227Skais FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */ 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* round 2 */ 595227Skais GG(a, b, c, d, x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */ 596227Skais GG(d, a, b, c, x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */ 597227Skais GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */ 598227Skais GG(b, c, d, a, x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */ 599227Skais GG(a, b, c, d, x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */ 600227Skais GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */ 601227Skais GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */ 602227Skais GG(b, c, d, a, x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */ 603227Skais GG(a, b, c, d, x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */ 604227Skais GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */ 605227Skais GG(c, d, a, b, x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */ 606227Skais GG(b, c, d, a, x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */ 607227Skais GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */ 608227Skais GG(d, a, b, c, x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */ 609227Skais GG(c, d, a, b, x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */ 610227Skais GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */ 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* round 3 */ 613227Skais HH(a, b, c, d, x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */ 614227Skais HH(d, a, b, c, x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */ 615227Skais HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */ 616227Skais HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */ 617227Skais HH(a, b, c, d, x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */ 618227Skais HH(d, a, b, c, x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */ 619227Skais HH(c, d, a, b, x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */ 620227Skais HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */ 621227Skais HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */ 622227Skais HH(d, a, b, c, x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */ 623227Skais HH(c, d, a, b, x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */ 624227Skais HH(b, c, d, a, x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */ 625227Skais HH(a, b, c, d, x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */ 626227Skais HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */ 627227Skais HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */ 628227Skais HH(b, c, d, a, x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */ 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* round 4 */ 631227Skais II(a, b, c, d, x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */ 632227Skais II(d, a, b, c, x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */ 633227Skais II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */ 634227Skais II(b, c, d, a, x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */ 635227Skais II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */ 636227Skais II(d, a, b, c, x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */ 637227Skais II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */ 638227Skais II(b, c, d, a, x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */ 639227Skais II(a, b, c, d, x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */ 640227Skais II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */ 641227Skais II(c, d, a, b, x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */ 642227Skais II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */ 643227Skais II(a, b, c, d, x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */ 644227Skais II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */ 645227Skais II(c, d, a, b, x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */ 646227Skais II(b, c, d, a, x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */ 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate ctx->state[0] += a; 6490Sstevel@tonic-gate ctx->state[1] += b; 6500Sstevel@tonic-gate ctx->state[2] += c; 6510Sstevel@tonic-gate ctx->state[3] += d; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * zeroize sensitive information -- compiler will optimize 6550Sstevel@tonic-gate * this out if everything is kept in registers 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate x_0 = x_1 = x_2 = x_3 = x_4 = x_5 = x_6 = x_7 = x_8 = 0; 6590Sstevel@tonic-gate x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0; 6600Sstevel@tonic-gate } 661*5764Sda73024 #endif /* !defined(__amd64) */ 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * Encode() 6650Sstevel@tonic-gate * 6660Sstevel@tonic-gate * purpose: to convert a list of numbers from big endian to little endian 6670Sstevel@tonic-gate * input: uint8_t * : place to store the converted little endian numbers 6680Sstevel@tonic-gate * uint32_t * : place to get numbers to convert from 6690Sstevel@tonic-gate * size_t : the length of the input in bytes 6700Sstevel@tonic-gate * output: void 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate static void 6741694Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input, 6751694Sdarrenm size_t input_len) 6760Sstevel@tonic-gate { 6770Sstevel@tonic-gate size_t i, j; 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) { 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT 6840Sstevel@tonic-gate if ((uintptr_t)output & 0x3) /* Not 4-byte aligned */ 6850Sstevel@tonic-gate bcopy(input + i, output + j, 4); 6860Sstevel@tonic-gate else *(uint32_t *)(output + j) = input[i]; 6870Sstevel@tonic-gate #else 6881694Sdarrenm /*LINTED E_BAD_PTR_CAST_ALIGN*/ 6890Sstevel@tonic-gate *(uint32_t *)(output + j) = input[i]; 6900Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */ 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate #else /* big endian -- will work on little endian, but slowly */ 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate output[j] = input[i] & 0xff; 6950Sstevel@tonic-gate output[j + 1] = (input[i] >> 8) & 0xff; 6960Sstevel@tonic-gate output[j + 2] = (input[i] >> 16) & 0xff; 6970Sstevel@tonic-gate output[j + 3] = (input[i] >> 24) & 0xff; 6980Sstevel@tonic-gate #endif 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate } 701