Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

md4.c (9542B)


      1 /* GLOBAL.H - RSAREF types and constants */
      2 
      3 #include <string.h>
      4 
      5 /* POINTER defines a generic pointer type */
      6 typedef unsigned char *POINTER;
      7 
      8 /* UINT2 defines a two byte word */
      9 typedef unsigned short int UINT2;
     10 
     11 /* UINT4 defines a four byte word */
     12 typedef unsigned long int UINT4;
     13 
     14   
     15 /* MD4.H - header file for MD4C.C */
     16 
     17 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. 
     18 
     19 All rights reserved.
     20   
     21 License to copy and use this software is granted provided that it is identified as the “RSA Data Security, Inc. MD4 Message-Digest Algorithm” in all material mentioning or referencing this software or this function.
     22 License is also granted to make and use derivative works provided that such works are identified as “derived from the RSA Data Security, Inc. MD4 Message-Digest Algorithm” in all material mentioning or referencing the derived work.
     23 RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided “as is” without express or implied warranty of any kind.
     24   
     25 These notices must be retained in any copies of any part of this documentation and/or software. */
     26 
     27 /* MD4 context. */
     28 typedef struct {
     29 	UINT4 state[4];				/* state (ABCD) */
     30 	UINT4 count[2];				/* number of bits, modulo 2^64 (lsb first) */
     31 	unsigned char buffer[64]; 			/* input buffer */
     32 } MD4_CTX;
     33 
     34 void MD4Init (MD4_CTX *);
     35 void MD4Update (MD4_CTX *, unsigned char *, unsigned int);
     36 void MD4Final (unsigned char [16], MD4_CTX *);
     37   
     38 
     39   
     40 /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm */
     41 /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
     42   
     43 License to copy and use this software is granted provided that it is identified as the
     44 RSA Data Security, Inc. MD4 Message-Digest Algorithm
     45  in all material mentioning or referencing this software or this function.
     46 License is also granted to make and use derivative works provided that such works are identified as 
     47 derived from the RSA Data Security, Inc. MD4 Message-Digest Algorithm
     48 in all material mentioning or referencing the derived work.
     49 RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided
     50 as is without express or implied warranty of any kind.
     51   
     52 These notices must be retained in any copies of any part of this documentation and/or software. */
     53 
     54 /* Constants for MD4Transform routine.  */
     55 #define S11 3
     56 #define S12 7
     57 #define S13 11
     58 #define S14 19
     59 #define S21 3
     60 #define S22 5
     61 #define S23 9
     62 #define S24 13
     63 #define S31 3
     64 #define S32 9
     65 #define S33 11
     66 #define S34 15
     67 
     68 static void MD4Transform (UINT4 [4], unsigned char [64]);
     69 static void Encode (unsigned char *, UINT4 *, unsigned int);
     70 static void Decode (UINT4 *, unsigned char *, unsigned int);
     71 static void MD4_memcpy (POINTER, POINTER, unsigned int);
     72 static void MD4_memset (POINTER, int, unsigned int);
     73 
     74 static unsigned char PADDING[64] = {
     75 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     76 };
     77 
     78 /* F, G and H are basic MD4 functions. */
     79 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
     80 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
     81 #define H(x, y, z) ((x) ^ (y) ^ (z))
     82 
     83 /* ROTATE_LEFT rotates x left n bits. */
     84 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
     85 
     86 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
     87 /* Rotation is separate from addition to prevent recomputation */
     88 #define FF(a, b, c, d, x, s) {(a) += F ((b), (c), (d)) + (x); (a) = ROTATE_LEFT ((a), (s));}
     89 
     90 #define GG(a, b, c, d, x, s) {(a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; (a) = ROTATE_LEFT ((a), (s));}
     91 
     92 #define HH(a, b, c, d, x, s) {(a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; (a) = \
     93 ROTATE_LEFT ((a), (s)); }
     94 
     95 
     96 /* MD4 initialization. Begins an MD4 operation, writing a new context. */
     97 void MD4Init (MD4_CTX *context)
     98 {
     99 	context->count[0] = context->count[1] = 0;
    100 
    101 /* Load magic initialization constants.*/
    102 context->state[0] = 0x67452301;
    103 context->state[1] = 0xefcdab89;
    104 context->state[2] = 0x98badcfe;
    105 context->state[3] = 0x10325476;
    106 }
    107 
    108 /* MD4 block update operation. Continues an MD4 message-digest operation, processing another message block, and updating the context. */
    109 void MD4Update (MD4_CTX *context, unsigned char *input, unsigned int inputLen)
    110 {
    111 	unsigned int i, index, partLen;
    112 
    113 	/* Compute number of bytes mod 64 */
    114 	index = (unsigned int)((context->count[0] >> 3) & 0x3F);
    115 
    116 	/* Update number of bits */
    117 	if ((context->count[0] += ((UINT4)inputLen << 3))< ((UINT4)inputLen << 3))
    118 		context->count[1]++;
    119 
    120 	context->count[1] += ((UINT4)inputLen >> 29);
    121 
    122 	partLen = 64 - index;
    123 
    124 	/* Transform as many times as possible.*/
    125 	if (inputLen >= partLen)
    126 	{
    127  		memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
    128  		MD4Transform (context->state, context->buffer);
    129 
    130  		for (i = partLen; i + 63 < inputLen; i += 64)
    131  			MD4Transform (context->state, &input[i]);
    132 
    133  		index = 0;
    134 	}
    135 	else
    136  		i = 0;
    137 
    138 	/* Buffer remaining input */
    139 	memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
    140 }
    141 
    142 
    143 /* MD4 finalization. Ends an MD4 message-digest operation, writing the the message digest and zeroizing the context. */
    144 void MD4Final (unsigned char digest[16], MD4_CTX *context)
    145 {
    146 	unsigned char bits[8];
    147 	unsigned int index, padLen;
    148 
    149 	/* Save number of bits */
    150 	Encode (bits, context->count, 8);
    151 
    152 	/* Pad out to 56 mod 64.*/
    153 	index = (unsigned int)((context->count[0] >> 3) & 0x3f);
    154 	padLen = (index < 56) ? (56 - index) : (120 - index);
    155 	MD4Update (context, PADDING, padLen);
    156 
    157 	/* Append length (before padding) */
    158 	MD4Update (context, bits, 8);
    159 	
    160 	/* Store state in digest */
    161 	Encode (digest, context->state, 16);
    162 
    163 	/* Zeroize sensitive information.*/
    164 	memset ((POINTER)context, 0, sizeof (*context));
    165 }
    166 
    167 
    168 /* MD4 basic transformation. Transforms state based on block. */
    169 static void MD4Transform (UINT4 state[4], unsigned char block[64])
    170 {
    171 	UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
    172 
    173 	Decode (x, block, 64);
    174 
    175 /* Round 1 */
    176 FF (a, b, c, d, x[ 0], S11); 				/* 1 */
    177 FF (d, a, b, c, x[ 1], S12); 				/* 2 */
    178 FF (c, d, a, b, x[ 2], S13); 				/* 3 */
    179 FF (b, c, d, a, x[ 3], S14); 				/* 4 */
    180 FF (a, b, c, d, x[ 4], S11); 				/* 5 */
    181 FF (d, a, b, c, x[ 5], S12); 				/* 6 */
    182 FF (c, d, a, b, x[ 6], S13); 				/* 7 */
    183 FF (b, c, d, a, x[ 7], S14); 				/* 8 */
    184 FF (a, b, c, d, x[ 8], S11); 				/* 9 */
    185 FF (d, a, b, c, x[ 9], S12); 				/* 10 */
    186 FF (c, d, a, b, x[10], S13); 			/* 11 */
    187 FF (b, c, d, a, x[11], S14); 			/* 12 */
    188 FF (a, b, c, d, x[12], S11); 			/* 13 */
    189 FF (d, a, b, c, x[13], S12); 			/* 14 */
    190 FF (c, d, a, b, x[14], S13); 			/* 15 */
    191 FF (b, c, d, a, x[15], S14); 			/* 16 */
    192 
    193 /* Round 2 */
    194 GG (a, b, c, d, x[ 0], S21); 			/* 17 */
    195 GG (d, a, b, c, x[ 4], S22); 			/* 18 */
    196 GG (c, d, a, b, x[ 8], S23); 			/* 19 */
    197 GG (b, c, d, a, x[12], S24); 			/* 20 */
    198 GG (a, b, c, d, x[ 1], S21); 			/* 21 */
    199 GG (d, a, b, c, x[ 5], S22); 			/* 22 */
    200 GG (c, d, a, b, x[ 9], S23); 			/* 23 */
    201 GG (b, c, d, a, x[13], S24); 			/* 24 */
    202 GG (a, b, c, d, x[ 2], S21); 			/* 25 */
    203 GG (d, a, b, c, x[ 6], S22); 			/* 26 */
    204 GG (c, d, a, b, x[10], S23); 			/* 27 */
    205 GG (b, c, d, a, x[14], S24); 			/* 28 */
    206 GG (a, b, c, d, x[ 3], S21); 			/* 29 */
    207 GG (d, a, b, c, x[ 7], S22); 			/* 30 */
    208 GG (c, d, a, b, x[11], S23); 			/* 31 */
    209 GG (b, c, d, a, x[15], S24); 			/* 32 */
    210 
    211 /* Round 3 */
    212 HH (a, b, c, d, x[ 0], S31);				/* 33 */
    213 HH (d, a, b, c, x[ 8], S32); 			/* 34 */
    214 HH (c, d, a, b, x[ 4], S33); 			/* 35 */
    215 HH (b, c, d, a, x[12], S34); 			/* 36 */
    216 HH (a, b, c, d, x[ 2], S31); 			/* 37 */
    217 HH (d, a, b, c, x[10], S32); 			/* 38 */
    218 HH (c, d, a, b, x[ 6], S33); 			/* 39 */
    219 HH (b, c, d, a, x[14], S34); 			/* 40 */
    220 HH (a, b, c, d, x[ 1], S31); 			/* 41 */
    221 HH (d, a, b, c, x[ 9], S32); 			/* 42 */
    222 HH (c, d, a, b, x[ 5], S33); 			/* 43 */
    223 HH (b, c, d, a, x[13], S34); 			/* 44 */
    224 HH (a, b, c, d, x[ 3], S31); 			/* 45 */
    225 HH (d, a, b, c, x[11], S32); 			/* 46 */
    226 HH (c, d, a, b, x[ 7], S33); 			/* 47 */
    227 HH (b, c, d, a, x[15], S34);			/* 48 */
    228 
    229 state[0] += a;
    230 state[1] += b;
    231 state[2] += c;
    232 state[3] += d;
    233 
    234 	/* Zeroize sensitive information.*/
    235 	memset ((POINTER)x, 0, sizeof (x));
    236 }
    237 
    238 
    239 /* Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. */
    240 static void Encode (unsigned char *output, UINT4 *input, unsigned int len)
    241 {
    242 	unsigned int i, j;
    243 
    244 	for (i = 0, j = 0; j < len; i++, j += 4) {
    245  		output[j] = (unsigned char)(input[i] & 0xff);
    246  		output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
    247  		output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
    248  		output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
    249 	}
    250 }
    251 
    252 
    253 /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */
    254 static void Decode (UINT4 *output, unsigned char *input, unsigned int len)
    255 {
    256 unsigned int i, j;
    257 
    258 for (i = 0, j = 0; j < len; i++, j += 4)
    259  	output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
    260 }
    261 
    262 //===================================================================
    263 
    264 unsigned Com_BlockChecksum (void *buffer, int length)
    265 {
    266 	int			digest[4];
    267 	unsigned	val;
    268 	MD4_CTX		ctx;
    269 
    270 	MD4Init (&ctx);
    271 	MD4Update (&ctx, (unsigned char *)buffer, length);
    272 	MD4Final ( (unsigned char *)digest, &ctx);
    273 	
    274 	val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
    275 
    276 	return val;
    277 }