10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM * 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 /*
80Sstevel@tonic-gate * lib/crypto/md4/md4.c
90Sstevel@tonic-gate */
100Sstevel@tonic-gate
110Sstevel@tonic-gate /*
120Sstevel@tonic-gate **********************************************************************
130Sstevel@tonic-gate ** md4.c **
140Sstevel@tonic-gate ** RSA Data Security, Inc. MD4 Message Digest Algorithm **
150Sstevel@tonic-gate ** Created: 2/17/90 RLR **
160Sstevel@tonic-gate ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
170Sstevel@tonic-gate **********************************************************************
180Sstevel@tonic-gate */
190Sstevel@tonic-gate
200Sstevel@tonic-gate /*
210Sstevel@tonic-gate **********************************************************************
220Sstevel@tonic-gate ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
230Sstevel@tonic-gate ** **
240Sstevel@tonic-gate ** License to copy and use this software is granted provided that **
250Sstevel@tonic-gate ** it is identified as the "RSA Data Security, Inc. MD4 Message **
260Sstevel@tonic-gate ** Digest Algorithm" in all material mentioning or referencing this **
270Sstevel@tonic-gate ** software or this function. **
280Sstevel@tonic-gate ** **
290Sstevel@tonic-gate ** License is also granted to make and use derivative works **
300Sstevel@tonic-gate ** provided that such works are identified as "derived from the RSA **
310Sstevel@tonic-gate ** Data Security, Inc. MD4 Message Digest Algorithm" in all **
320Sstevel@tonic-gate ** material mentioning or referencing the derived work. **
330Sstevel@tonic-gate ** **
340Sstevel@tonic-gate ** RSA Data Security, Inc. makes no representations concerning **
350Sstevel@tonic-gate ** either the merchantability of this software or the suitability **
360Sstevel@tonic-gate ** of this software for any particular purpose. It is provided "as **
370Sstevel@tonic-gate ** is" without express or implied warranty of any kind. **
380Sstevel@tonic-gate ** **
390Sstevel@tonic-gate ** These notices must be retained in any copies of any part of this **
400Sstevel@tonic-gate ** documentation and/or software. **
410Sstevel@tonic-gate **********************************************************************
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
44*7934SMark.Phalan@Sun.COM #include "k5-int.h"
45*7934SMark.Phalan@Sun.COM #include "rsa-md4.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate /* forward declaration */
480Sstevel@tonic-gate static void Transform (krb5_ui_4 *, krb5_ui_4 *);
490Sstevel@tonic-gate
500Sstevel@tonic-gate static const unsigned char PADDING[64] = {
510Sstevel@tonic-gate 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
520Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
530Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
540Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
550Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
560Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
570Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
580Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
590Sstevel@tonic-gate };
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* F, G and H are basic MD4 functions: selection, majority, parity */
620Sstevel@tonic-gate #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
630Sstevel@tonic-gate #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
640Sstevel@tonic-gate #define H(x, y, z) ((x) ^ (y) ^ (z))
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* ROTATE_LEFT rotates x left n bits */
670Sstevel@tonic-gate #define ROTATE_LEFT(x, n) ((((x) << (n)) & 0xffffffff) | ((x) >> (32-(n))))
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* FF, GG and HH are MD4 transformations for rounds 1, 2 and 3 */
700Sstevel@tonic-gate /* Rotation is separate from addition to prevent recomputation */
710Sstevel@tonic-gate #define FF(a, b, c, d, x, s) \
720Sstevel@tonic-gate {(a) += F ((b), (c), (d)) + (x); \
730Sstevel@tonic-gate (a) &= 0xffffffff; \
740Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s));}
750Sstevel@tonic-gate #define GG(a, b, c, d, x, s) \
760Sstevel@tonic-gate {(a) += G ((b), (c), (d)) + (x) + 013240474631UL; \
770Sstevel@tonic-gate (a) &= 0xffffffff; \
780Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s));}
790Sstevel@tonic-gate #define HH(a, b, c, d, x, s) \
800Sstevel@tonic-gate {(a) += H ((b), (c), (d)) + (x) + 015666365641UL; \
810Sstevel@tonic-gate (a) &= 0xffffffff; \
820Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s));}
830Sstevel@tonic-gate
840Sstevel@tonic-gate void
krb5_MD4Init(krb5_MD4_CTX * mdContext)85*7934SMark.Phalan@Sun.COM krb5_MD4Init (krb5_MD4_CTX *mdContext)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate mdContext->i[0] = mdContext->i[1] = (krb5_ui_4)0;
880Sstevel@tonic-gate
890Sstevel@tonic-gate /* Load magic initialization constants.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate mdContext->buf[0] = 0x67452301UL;
920Sstevel@tonic-gate mdContext->buf[1] = 0xefcdab89UL;
930Sstevel@tonic-gate mdContext->buf[2] = 0x98badcfeUL;
940Sstevel@tonic-gate mdContext->buf[3] = 0x10325476UL;
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate void
krb5_MD4Update(krb5_MD4_CTX * mdContext,const unsigned char * inBuf,unsigned int inLen)98*7934SMark.Phalan@Sun.COM krb5_MD4Update (krb5_MD4_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate krb5_ui_4 in[16];
1010Sstevel@tonic-gate int mdi;
1020Sstevel@tonic-gate unsigned int i, ii;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* compute number of bytes mod 64 */
1050Sstevel@tonic-gate mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /* update number of bits */
1080Sstevel@tonic-gate if ((mdContext->i[0] + ((krb5_ui_4)inLen << 3)) < mdContext->i[0])
1090Sstevel@tonic-gate mdContext->i[1]++;
1100Sstevel@tonic-gate mdContext->i[0] += ((krb5_ui_4)inLen << 3);
1110Sstevel@tonic-gate mdContext->i[1] += ((krb5_ui_4)inLen >> 29);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate while (inLen--) {
1140Sstevel@tonic-gate /* add new character to buffer, increment mdi */
1150Sstevel@tonic-gate mdContext->in[mdi++] = *inBuf++;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /* transform if necessary */
1180Sstevel@tonic-gate if (mdi == 0x40) {
1190Sstevel@tonic-gate for (i = 0, ii = 0; i < 16; i++, ii += 4)
1200Sstevel@tonic-gate in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
1210Sstevel@tonic-gate (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
1220Sstevel@tonic-gate (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
1230Sstevel@tonic-gate ((krb5_ui_4)mdContext->in[ii]);
1240Sstevel@tonic-gate Transform (mdContext->buf, in);
1250Sstevel@tonic-gate mdi = 0;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate void
krb5_MD4Final(krb5_MD4_CTX * mdContext)131*7934SMark.Phalan@Sun.COM krb5_MD4Final (krb5_MD4_CTX *mdContext)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate krb5_ui_4 in[16];
1340Sstevel@tonic-gate int mdi;
1350Sstevel@tonic-gate unsigned int i, ii;
1360Sstevel@tonic-gate unsigned int padLen;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* save number of bits */
1390Sstevel@tonic-gate in[14] = mdContext->i[0];
1400Sstevel@tonic-gate in[15] = mdContext->i[1];
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /* compute number of bytes mod 64 */
1430Sstevel@tonic-gate mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* pad out to 56 mod 64 */
1460Sstevel@tonic-gate padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
1470Sstevel@tonic-gate krb5_MD4Update (mdContext, PADDING, padLen);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /* append length in bits and transform */
1500Sstevel@tonic-gate for (i = 0, ii = 0; i < 14; i++, ii += 4)
1510Sstevel@tonic-gate in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
1520Sstevel@tonic-gate (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
1530Sstevel@tonic-gate (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
1540Sstevel@tonic-gate ((krb5_ui_4)mdContext->in[ii]);
1550Sstevel@tonic-gate Transform (mdContext->buf, in);
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /* store buffer in digest */
1590Sstevel@tonic-gate for (i = 0, ii = 0; i < 4; i++, ii += 4) {
1600Sstevel@tonic-gate mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
1610Sstevel@tonic-gate mdContext->digest[ii+1] =
1620Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
1630Sstevel@tonic-gate mdContext->digest[ii+2] =
1640Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
1650Sstevel@tonic-gate mdContext->digest[ii+3] =
1660Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /* Basic MD4 step. Transform buf based on in.
1710Sstevel@tonic-gate */
Transform(krb5_ui_4 * buf,krb5_ui_4 * in)172*7934SMark.Phalan@Sun.COM static void Transform (krb5_ui_4 *buf, krb5_ui_4 *in)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate register krb5_ui_4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
1750Sstevel@tonic-gate
176*7934SMark.Phalan@Sun.COM #ifdef CONFIG_SMALL
177*7934SMark.Phalan@Sun.COM int i;
178*7934SMark.Phalan@Sun.COM #define ROTATE { krb5_ui_4 temp; temp = d, d = c, c = b, b = a, a = temp; }
179*7934SMark.Phalan@Sun.COM for (i = 0; i < 16; i++) {
180*7934SMark.Phalan@Sun.COM static const unsigned char round1consts[] = { 3, 7, 11, 19, };
181*7934SMark.Phalan@Sun.COM FF (a, b, c, d, in[i], round1consts[i%4]); ROTATE;
182*7934SMark.Phalan@Sun.COM }
183*7934SMark.Phalan@Sun.COM for (i = 0; i < 16; i++) {
184*7934SMark.Phalan@Sun.COM static const unsigned char round2indices[] = {
185*7934SMark.Phalan@Sun.COM 0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15
186*7934SMark.Phalan@Sun.COM };
187*7934SMark.Phalan@Sun.COM static const unsigned char round2consts[] = { 3, 5, 9, 13 };
188*7934SMark.Phalan@Sun.COM GG (a, b, c, d, in[round2indices[i]], round2consts[i%4]); ROTATE;
189*7934SMark.Phalan@Sun.COM }
190*7934SMark.Phalan@Sun.COM for (i = 0; i < 16; i++) {
191*7934SMark.Phalan@Sun.COM static const unsigned char round3indices[] = {
192*7934SMark.Phalan@Sun.COM 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
193*7934SMark.Phalan@Sun.COM };
194*7934SMark.Phalan@Sun.COM static const unsigned char round3consts[] = { 3, 9, 11, 15 };
195*7934SMark.Phalan@Sun.COM HH (a, b, c, d, in[round3indices[i]], round3consts[i%4]); ROTATE;
196*7934SMark.Phalan@Sun.COM }
197*7934SMark.Phalan@Sun.COM #else
1980Sstevel@tonic-gate /* Round 1 */
1990Sstevel@tonic-gate FF (a, b, c, d, in[ 0], 3);
2000Sstevel@tonic-gate FF (d, a, b, c, in[ 1], 7);
2010Sstevel@tonic-gate FF (c, d, a, b, in[ 2], 11);
2020Sstevel@tonic-gate FF (b, c, d, a, in[ 3], 19);
2030Sstevel@tonic-gate FF (a, b, c, d, in[ 4], 3);
2040Sstevel@tonic-gate FF (d, a, b, c, in[ 5], 7);
2050Sstevel@tonic-gate FF (c, d, a, b, in[ 6], 11);
2060Sstevel@tonic-gate FF (b, c, d, a, in[ 7], 19);
2070Sstevel@tonic-gate FF (a, b, c, d, in[ 8], 3);
2080Sstevel@tonic-gate FF (d, a, b, c, in[ 9], 7);
2090Sstevel@tonic-gate FF (c, d, a, b, in[10], 11);
2100Sstevel@tonic-gate FF (b, c, d, a, in[11], 19);
2110Sstevel@tonic-gate FF (a, b, c, d, in[12], 3);
2120Sstevel@tonic-gate FF (d, a, b, c, in[13], 7);
2130Sstevel@tonic-gate FF (c, d, a, b, in[14], 11);
2140Sstevel@tonic-gate FF (b, c, d, a, in[15], 19);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /* Round 2 */
2170Sstevel@tonic-gate GG (a, b, c, d, in[ 0], 3);
2180Sstevel@tonic-gate GG (d, a, b, c, in[ 4], 5);
2190Sstevel@tonic-gate GG (c, d, a, b, in[ 8], 9);
2200Sstevel@tonic-gate GG (b, c, d, a, in[12], 13);
2210Sstevel@tonic-gate GG (a, b, c, d, in[ 1], 3);
2220Sstevel@tonic-gate GG (d, a, b, c, in[ 5], 5);
2230Sstevel@tonic-gate GG (c, d, a, b, in[ 9], 9);
2240Sstevel@tonic-gate GG (b, c, d, a, in[13], 13);
2250Sstevel@tonic-gate GG (a, b, c, d, in[ 2], 3);
2260Sstevel@tonic-gate GG (d, a, b, c, in[ 6], 5);
2270Sstevel@tonic-gate GG (c, d, a, b, in[10], 9);
2280Sstevel@tonic-gate GG (b, c, d, a, in[14], 13);
2290Sstevel@tonic-gate GG (a, b, c, d, in[ 3], 3);
2300Sstevel@tonic-gate GG (d, a, b, c, in[ 7], 5);
2310Sstevel@tonic-gate GG (c, d, a, b, in[11], 9);
2320Sstevel@tonic-gate GG (b, c, d, a, in[15], 13);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /* Round 3 */
2350Sstevel@tonic-gate HH (a, b, c, d, in[ 0], 3);
2360Sstevel@tonic-gate HH (d, a, b, c, in[ 8], 9);
2370Sstevel@tonic-gate HH (c, d, a, b, in[ 4], 11);
2380Sstevel@tonic-gate HH (b, c, d, a, in[12], 15);
2390Sstevel@tonic-gate HH (a, b, c, d, in[ 2], 3);
2400Sstevel@tonic-gate HH (d, a, b, c, in[10], 9);
2410Sstevel@tonic-gate HH (c, d, a, b, in[ 6], 11);
2420Sstevel@tonic-gate HH (b, c, d, a, in[14], 15);
2430Sstevel@tonic-gate HH (a, b, c, d, in[ 1], 3);
2440Sstevel@tonic-gate HH (d, a, b, c, in[ 9], 9);
2450Sstevel@tonic-gate HH (c, d, a, b, in[ 5], 11);
2460Sstevel@tonic-gate HH (b, c, d, a, in[13], 15);
2470Sstevel@tonic-gate HH (a, b, c, d, in[ 3], 3);
2480Sstevel@tonic-gate HH (d, a, b, c, in[11], 9);
2490Sstevel@tonic-gate HH (c, d, a, b, in[ 7], 11);
2500Sstevel@tonic-gate HH (b, c, d, a, in[15], 15);
251*7934SMark.Phalan@Sun.COM #endif
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate buf[0] += a;
2540Sstevel@tonic-gate buf[1] += b;
2550Sstevel@tonic-gate buf[2] += c;
2560Sstevel@tonic-gate buf[3] += d;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate **********************************************************************
2610Sstevel@tonic-gate ** End of md4.c **
2620Sstevel@tonic-gate ******************************* (cut) ********************************
2630Sstevel@tonic-gate */
264