17c478bd9Sstevel@tonic-gate /*
24b56a003SDaniel Anderson * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3a0b85df4Sstevel * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
87c478bd9Sstevel@tonic-gate */
97c478bd9Sstevel@tonic-gate
107c478bd9Sstevel@tonic-gate /*
117c478bd9Sstevel@tonic-gate * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * License to copy and use this software is granted provided that it
147c478bd9Sstevel@tonic-gate * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
157c478bd9Sstevel@tonic-gate * Algorithm" in all material mentioning or referencing this software
167c478bd9Sstevel@tonic-gate * or this function.
177c478bd9Sstevel@tonic-gate *
187c478bd9Sstevel@tonic-gate * License is also granted to make and use derivative works provided
197c478bd9Sstevel@tonic-gate * that such works are identified as "derived from the RSA Data
207c478bd9Sstevel@tonic-gate * Security, Inc. MD4 Message-Digest Algorithm" in all material
217c478bd9Sstevel@tonic-gate * mentioning or referencing the derived work.
227c478bd9Sstevel@tonic-gate *
237c478bd9Sstevel@tonic-gate * RSA Data Security, Inc. makes no representations concerning either
247c478bd9Sstevel@tonic-gate * the merchantability of this software or the suitability of this
257c478bd9Sstevel@tonic-gate * software for any particular purpose. It is provided "as is"
267c478bd9Sstevel@tonic-gate * without express or implied warranty of any kind.
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate * These notices must be retained in any copies of any part of this
297c478bd9Sstevel@tonic-gate * documentation and/or software.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
325151fb12Sdarrenm #include <sys/types.h>
335151fb12Sdarrenm #ifdef _KERNEL
345151fb12Sdarrenm #include <sys/sunddi.h>
355151fb12Sdarrenm #else
365151fb12Sdarrenm #include <strings.h>
375151fb12Sdarrenm #endif /* _KERNEL */
387c478bd9Sstevel@tonic-gate
394b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64)
404b56a003SDaniel Anderson #define UNALIGNED_POINTERS_PERMITTED
414b56a003SDaniel Anderson #endif
424b56a003SDaniel Anderson
435151fb12Sdarrenm #include <sys/md4.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * Constants for MD4Transform routine.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate #define S11 3
497c478bd9Sstevel@tonic-gate #define S12 7
507c478bd9Sstevel@tonic-gate #define S13 11
517c478bd9Sstevel@tonic-gate #define S14 19
527c478bd9Sstevel@tonic-gate #define S21 3
537c478bd9Sstevel@tonic-gate #define S22 5
547c478bd9Sstevel@tonic-gate #define S23 9
557c478bd9Sstevel@tonic-gate #define S24 13
567c478bd9Sstevel@tonic-gate #define S31 3
577c478bd9Sstevel@tonic-gate #define S32 9
587c478bd9Sstevel@tonic-gate #define S33 11
597c478bd9Sstevel@tonic-gate #define S34 15
607c478bd9Sstevel@tonic-gate
615151fb12Sdarrenm static void MD4Transform(uint32_t [4], unsigned char [64]);
625151fb12Sdarrenm static void Encode(unsigned char *, uint32_t *, unsigned int);
635151fb12Sdarrenm static void Decode(uint32_t *, unsigned char *, unsigned int);
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate static unsigned char PADDING[64] = {
667c478bd9Sstevel@tonic-gate 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
677c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
687c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * F, G and H are basic MD4 functions.
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
757c478bd9Sstevel@tonic-gate #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
767c478bd9Sstevel@tonic-gate #define H(x, y, z) ((x) ^ (y) ^ (z))
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * ROTATE_LEFT rotates x left n bits.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
847c478bd9Sstevel@tonic-gate /* Rotation is separate from addition to prevent recomputation */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #define FF(a, b, c, d, x, s) { \
877c478bd9Sstevel@tonic-gate (a) += F((b), (c), (d)) + (x); \
887c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate #define GG(a, b, c, d, x, s) { \
915151fb12Sdarrenm (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5a827999; \
927c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate #define HH(a, b, c, d, x, s) { \
955151fb12Sdarrenm (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ed9eba1; \
967c478bd9Sstevel@tonic-gate (a) = ROTATE_LEFT((a), (s)); \
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * MD4 initialization. Begins an MD4 operation, writing a new context.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate void
MD4Init(MD4_CTX * context)1035151fb12Sdarrenm MD4Init(MD4_CTX *context)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate context->count[0] = context->count[1] = 0;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Load magic initialization constants.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate context->state[0] = 0x67452301UL;
1117c478bd9Sstevel@tonic-gate context->state[1] = 0xefcdab89UL;
1127c478bd9Sstevel@tonic-gate context->state[2] = 0x98badcfeUL;
1137c478bd9Sstevel@tonic-gate context->state[3] = 0x10325476UL;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * MD4 block update operation. Continues an MD4 message-digest
1197c478bd9Sstevel@tonic-gate * operation, processing another message block, and updating the
1207c478bd9Sstevel@tonic-gate * context.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate void
MD4Update(MD4_CTX * context,const void * _RESTRICT_KYWD inptr,size_t inputLen)123734b6a94Sdarrenm MD4Update(MD4_CTX *context, const void *_RESTRICT_KYWD inptr, size_t inputLen)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate unsigned int i, index, partLen;
126734b6a94Sdarrenm uchar_t *input = (uchar_t *)inptr;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /* Compute number of bytes mod 64 */
1297c478bd9Sstevel@tonic-gate index = (unsigned int)((context->count[0] >> 3) & 0x3F);
1307c478bd9Sstevel@tonic-gate /* Update number of bits */
1315151fb12Sdarrenm if ((context->count[0] += ((uint32_t)inputLen << 3))
1325151fb12Sdarrenm < ((uint32_t)inputLen << 3))
1337c478bd9Sstevel@tonic-gate context->count[1]++;
1345151fb12Sdarrenm context->count[1] += ((uint32_t)inputLen >> 29);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate partLen = 64 - index;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Transform as many times as possible.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate if (inputLen >= partLen) {
142734b6a94Sdarrenm bcopy(input, &context->buffer[index], partLen);
143734b6a94Sdarrenm MD4Transform(context->state, (uchar_t *)context->buffer);
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate for (i = partLen; i + 63 < inputLen; i += 64) {
146734b6a94Sdarrenm MD4Transform(context->state, (uchar_t *)&input[i]);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate index = 0;
1507c478bd9Sstevel@tonic-gate } else {
1517c478bd9Sstevel@tonic-gate i = 0;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* Buffer remaining input */
155734b6a94Sdarrenm bcopy(&input[i], &context->buffer[index], inputLen - i);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * MD4 finalization. Ends an MD4 message-digest operation, writing the
1607c478bd9Sstevel@tonic-gate * the message digest and zeroizing the context.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate void
MD4Final(void * digest,MD4_CTX * context)163734b6a94Sdarrenm MD4Final(void *digest, MD4_CTX *context)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate unsigned char bits[8];
1667c478bd9Sstevel@tonic-gate unsigned int index, padLen;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /* Save number of bits */
1697c478bd9Sstevel@tonic-gate Encode(bits, context->count, 8);
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * Pad out to 56 mod 64.
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate index = (unsigned int)((context->count[0] >> 3) & 0x3f);
1757c478bd9Sstevel@tonic-gate padLen = (index < 56) ? (56 - index) : (120 - index);
1767c478bd9Sstevel@tonic-gate MD4Update(context, PADDING, padLen);
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /* Append length (before padding) */
1797c478bd9Sstevel@tonic-gate MD4Update(context, bits, 8);
1807c478bd9Sstevel@tonic-gate /* Store state in digest */
1817c478bd9Sstevel@tonic-gate Encode(digest, context->state, 16);
1827c478bd9Sstevel@tonic-gate
183734b6a94Sdarrenm /* zeroize sensitive information */
184734b6a94Sdarrenm bzero(context, sizeof (*context));
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * MD4 basic transformation. Transforms state based on block.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate static void
MD4Transform(uint32_t state[4],unsigned char block[64])1915151fb12Sdarrenm MD4Transform(uint32_t state[4], unsigned char block[64])
1927c478bd9Sstevel@tonic-gate {
1935151fb12Sdarrenm uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate Decode(x, block, 64);
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /* Round 1 */
1997c478bd9Sstevel@tonic-gate FF(a, b, c, d, x[ 0], S11); /* 1 */
2007c478bd9Sstevel@tonic-gate FF(d, a, b, c, x[ 1], S12); /* 2 */
2017c478bd9Sstevel@tonic-gate FF(c, d, a, b, x[ 2], S13); /* 3 */
2027c478bd9Sstevel@tonic-gate FF(b, c, d, a, x[ 3], S14); /* 4 */
2037c478bd9Sstevel@tonic-gate FF(a, b, c, d, x[ 4], S11); /* 5 */
2047c478bd9Sstevel@tonic-gate FF(d, a, b, c, x[ 5], S12); /* 6 */
2057c478bd9Sstevel@tonic-gate FF(c, d, a, b, x[ 6], S13); /* 7 */
2067c478bd9Sstevel@tonic-gate FF(b, c, d, a, x[ 7], S14); /* 8 */
2077c478bd9Sstevel@tonic-gate FF(a, b, c, d, x[ 8], S11); /* 9 */
2087c478bd9Sstevel@tonic-gate FF(d, a, b, c, x[ 9], S12); /* 10 */
2097c478bd9Sstevel@tonic-gate FF(c, d, a, b, x[10], S13); /* 11 */
2107c478bd9Sstevel@tonic-gate FF(b, c, d, a, x[11], S14); /* 12 */
2117c478bd9Sstevel@tonic-gate FF(a, b, c, d, x[12], S11); /* 13 */
2127c478bd9Sstevel@tonic-gate FF(d, a, b, c, x[13], S12); /* 14 */
2137c478bd9Sstevel@tonic-gate FF(c, d, a, b, x[14], S13); /* 15 */
2147c478bd9Sstevel@tonic-gate FF(b, c, d, a, x[15], S14); /* 16 */
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /* Round 2 */
2177c478bd9Sstevel@tonic-gate GG(a, b, c, d, x[ 0], S21); /* 17 */
2187c478bd9Sstevel@tonic-gate GG(d, a, b, c, x[ 4], S22); /* 18 */
2197c478bd9Sstevel@tonic-gate GG(c, d, a, b, x[ 8], S23); /* 19 */
2207c478bd9Sstevel@tonic-gate GG(b, c, d, a, x[12], S24); /* 20 */
2217c478bd9Sstevel@tonic-gate GG(a, b, c, d, x[ 1], S21); /* 21 */
2227c478bd9Sstevel@tonic-gate GG(d, a, b, c, x[ 5], S22); /* 22 */
2237c478bd9Sstevel@tonic-gate GG(c, d, a, b, x[ 9], S23); /* 23 */
2247c478bd9Sstevel@tonic-gate GG(b, c, d, a, x[13], S24); /* 24 */
2257c478bd9Sstevel@tonic-gate GG(a, b, c, d, x[ 2], S21); /* 25 */
2267c478bd9Sstevel@tonic-gate GG(d, a, b, c, x[ 6], S22); /* 26 */
2277c478bd9Sstevel@tonic-gate GG(c, d, a, b, x[10], S23); /* 27 */
2287c478bd9Sstevel@tonic-gate GG(b, c, d, a, x[14], S24); /* 28 */
2297c478bd9Sstevel@tonic-gate GG(a, b, c, d, x[ 3], S21); /* 29 */
2307c478bd9Sstevel@tonic-gate GG(d, a, b, c, x[ 7], S22); /* 30 */
2317c478bd9Sstevel@tonic-gate GG(c, d, a, b, x[11], S23); /* 31 */
2327c478bd9Sstevel@tonic-gate GG(b, c, d, a, x[15], S24); /* 32 */
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate /* Round 3 */
2367c478bd9Sstevel@tonic-gate HH(a, b, c, d, x[ 0], S31); /* 33 */
2377c478bd9Sstevel@tonic-gate HH(d, a, b, c, x[ 8], S32); /* 34 */
2387c478bd9Sstevel@tonic-gate HH(c, d, a, b, x[ 4], S33); /* 35 */
2397c478bd9Sstevel@tonic-gate HH(b, c, d, a, x[12], S34); /* 36 */
2407c478bd9Sstevel@tonic-gate HH(a, b, c, d, x[ 2], S31); /* 37 */
2417c478bd9Sstevel@tonic-gate HH(d, a, b, c, x[10], S32); /* 38 */
2427c478bd9Sstevel@tonic-gate HH(c, d, a, b, x[ 6], S33); /* 39 */
2437c478bd9Sstevel@tonic-gate HH(b, c, d, a, x[14], S34); /* 40 */
2447c478bd9Sstevel@tonic-gate HH(a, b, c, d, x[ 1], S31); /* 41 */
2457c478bd9Sstevel@tonic-gate HH(d, a, b, c, x[ 9], S32); /* 42 */
2467c478bd9Sstevel@tonic-gate HH(c, d, a, b, x[ 5], S33); /* 43 */
2477c478bd9Sstevel@tonic-gate HH(b, c, d, a, x[13], S34); /* 44 */
2487c478bd9Sstevel@tonic-gate HH(a, b, c, d, x[ 3], S31); /* 45 */
2497c478bd9Sstevel@tonic-gate HH(d, a, b, c, x[11], S32); /* 46 */
2507c478bd9Sstevel@tonic-gate HH(c, d, a, b, x[ 7], S33); /* 47 */
2517c478bd9Sstevel@tonic-gate HH(b, c, d, a, x[15], S34); /* 48 */
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate state[0] += a;
2547c478bd9Sstevel@tonic-gate state[1] += b;
2557c478bd9Sstevel@tonic-gate state[2] += c;
2567c478bd9Sstevel@tonic-gate state[3] += d;
2577c478bd9Sstevel@tonic-gate
258734b6a94Sdarrenm /* zeroize sensitive information */
259734b6a94Sdarrenm bzero(x, sizeof (*x));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2635151fb12Sdarrenm * Encodes input (uint32_t) into output (unsigned char). Assumes len is
2647c478bd9Sstevel@tonic-gate * a multiple of 4.
2657c478bd9Sstevel@tonic-gate */
2667c478bd9Sstevel@tonic-gate static void
Encode(unsigned char * output,uint32_t * input,unsigned int len)2674b56a003SDaniel Anderson Encode(unsigned char *output, uint32_t *input, unsigned int len)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate unsigned int i, j;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate for (i = 0, j = 0; j < len; i++, j += 4) {
2724b56a003SDaniel Anderson #if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
273*25cc6a40SDaniel Anderson *(uint32_t *)(void *)&output[j] = input[i];
2744b56a003SDaniel Anderson #else
2754b56a003SDaniel Anderson /* endian-independent code */
2767c478bd9Sstevel@tonic-gate output[j] = (unsigned char)(input[i] & 0xff);
2777c478bd9Sstevel@tonic-gate output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
2787c478bd9Sstevel@tonic-gate output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
2797c478bd9Sstevel@tonic-gate output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
2804b56a003SDaniel Anderson #endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate /*
2855151fb12Sdarrenm * Decodes input (unsigned char) into output (uint32_t). Assumes len is
2867c478bd9Sstevel@tonic-gate * a multiple of 4.
2877c478bd9Sstevel@tonic-gate */
2887c478bd9Sstevel@tonic-gate static void
Decode(uint32_t * output,unsigned char * input,unsigned int len)2894b56a003SDaniel Anderson Decode(uint32_t *output, unsigned char *input, unsigned int len)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate unsigned int i, j;
2927c478bd9Sstevel@tonic-gate
2934b56a003SDaniel Anderson for (i = 0, j = 0; j < len; i++, j += 4) {
2944b56a003SDaniel Anderson #if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
295*25cc6a40SDaniel Anderson output[i] = *(uint32_t *)(void *)&input[j];
2964b56a003SDaniel Anderson #else
2974b56a003SDaniel Anderson /* endian-independent code */
2985151fb12Sdarrenm output[i] = ((uint32_t)input[j]) |
2994b56a003SDaniel Anderson (((uint32_t)input[j+1]) << 8) |
3004b56a003SDaniel Anderson (((uint32_t)input[j+2]) << 16) |
3014b56a003SDaniel Anderson (((uint32_t)input[j+3]) << 24);
3024b56a003SDaniel Anderson #endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
3034b56a003SDaniel Anderson }
3044b56a003SDaniel Anderson
3057c478bd9Sstevel@tonic-gate }
306