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