1 /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
\r
4 /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
\r
6 License to copy and use this software is granted provided that it
\r
7 is identified as the "RSA Data Security, Inc. MD4 Message-Digest
\r
8 Algorithm" in all material mentioning or referencing this software
\r
11 License is also granted to make and use derivative works provided
\r
12 that such works are identified as "derived from the RSA Data
\r
13 Security, Inc. MD4 Message-Digest Algorithm" in all material
\r
14 mentioning or referencing the derived work.
\r
16 RSA Data Security, Inc. makes no representations concerning either
\r
17 the merchantability of this software or the suitability of this
\r
18 software for any particular purpose. It is provided "as is"
\r
19 without express or implied warranty of any kind.
\r
21 These notices must be retained in any copies of any part of this
\r
22 documentation and/or software.
\r
27 /* Constants for MD4Transform routine.
\r
42 static void MD4Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
\r
43 static void Encode PROTO_LIST
\r
44 ((unsigned char *, UINT4 *, unsigned int));
\r
45 static void Decode PROTO_LIST
\r
46 ((UINT4 *, unsigned char *, unsigned int));
\r
47 static void MD4_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
\r
48 static void MD4_memset PROTO_LIST ((POINTER, int, unsigned int));
\r
50 static unsigned char PADDING[64] = {
\r
51 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
\r
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
\r
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
\r
56 /* F, G and H are basic MD4 functions.
\r
58 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
\r
59 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
\r
60 #define H(x, y, z) ((x) ^ (y) ^ (z))
\r
62 /* ROTATE_LEFT rotates x left n bits.
\r
64 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
\r
66 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
\r
67 /* Rotation is separate from addition to prevent recomputation */
\r
69 #define FF(a, b, c, d, x, s) { \
\r
70 (a) += F ((b), (c), (d)) + (x); \
\r
71 (a) = ROTATE_LEFT ((a), (s)); \
\r
73 #define GG(a, b, c, d, x, s) { \
\r
74 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
\r
75 (a) = ROTATE_LEFT ((a), (s)); \
\r
77 #define HH(a, b, c, d, x, s) { \
\r
78 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
\r
79 (a) = ROTATE_LEFT ((a), (s)); \
\r
82 /* MD4 initialization. Begins an MD4 operation, writing a new context.
\r
84 void MD4Init (context)
\r
85 MD4_CTX *context; /* context */
\r
87 context->count[0] = context->count[1] = 0;
\r
89 /* Load magic initialization constants.
\r
91 context->state[0] = 0x67452301;
\r
92 context->state[1] = 0xefcdab89;
\r
93 context->state[2] = 0x98badcfe;
\r
94 context->state[3] = 0x10325476;
\r
97 /* MD4 block update operation. Continues an MD4 message-digest
\r
98 operation, processing another message block, and updating the
\r
101 void MD4Update (context, input, inputLen)
\r
102 MD4_CTX *context; /* context */
\r
103 unsigned char *input; /* input block */
\r
104 unsigned int inputLen; /* length of input block */
\r
106 unsigned int i, index, partLen;
\r
108 /* Compute number of bytes mod 64 */
\r
109 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
\r
110 /* Update number of bits */
\r
111 if ((context->count[0] += ((UINT4)inputLen << 3))
\r
112 < ((UINT4)inputLen << 3))
\r
113 context->count[1]++;
\r
114 context->count[1] += ((UINT4)inputLen >> 29);
\r
116 partLen = 64 - index;
\r
118 /* Transform as many times as possible.
\r
120 if (inputLen >= partLen) {
\r
122 ((POINTER)&context->buffer[index], (POINTER)input, partLen);
\r
123 MD4Transform (context->state, context->buffer);
\r
125 for (i = partLen; i + 63 < inputLen; i += 64)
\r
126 MD4Transform (context->state, &input[i]);
\r
133 /* Buffer remaining input */
\r
135 ((POINTER)&context->buffer[index], (POINTER)&input[i],
\r
139 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
\r
140 the message digest and zeroizing the context.
\r
142 void MD4Final (digest, context)
\r
143 unsigned char digest[16]; /* message digest */
\r
144 MD4_CTX *context; /* context */
\r
146 unsigned char bits[8];
\r
147 unsigned int index, padLen;
\r
149 /* Save number of bits */
\r
150 Encode (bits, context->count, 8);
\r
152 /* Pad out to 56 mod 64.
\r
154 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
\r
155 padLen = (index < 56) ? (56 - index) : (120 - index);
\r
156 MD4Update (context, PADDING, padLen);
\r
158 /* Append length (before padding) */
\r
159 MD4Update (context, bits, 8);
\r
160 /* Store state in digest */
\r
161 Encode (digest, context->state, 16);
\r
163 /* Zeroize sensitive information.
\r
165 MD4_memset ((POINTER)context, 0, sizeof (*context));
\r
168 /* MD4 basic transformation. Transforms state based on block.
\r
170 static void MD4Transform (state, block)
\r
172 unsigned char block[64];
\r
174 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
\r
176 Decode (x, block, 64);
\r
179 FF (a, b, c, d, x[ 0], S11); /* 1 */
\r
180 FF (d, a, b, c, x[ 1], S12); /* 2 */
\r
181 FF (c, d, a, b, x[ 2], S13); /* 3 */
\r
182 FF (b, c, d, a, x[ 3], S14); /* 4 */
\r
183 FF (a, b, c, d, x[ 4], S11); /* 5 */
\r
184 FF (d, a, b, c, x[ 5], S12); /* 6 */
\r
185 FF (c, d, a, b, x[ 6], S13); /* 7 */
\r
186 FF (b, c, d, a, x[ 7], S14); /* 8 */
\r
187 FF (a, b, c, d, x[ 8], S11); /* 9 */
\r
188 FF (d, a, b, c, x[ 9], S12); /* 10 */
\r
189 FF (c, d, a, b, x[10], S13); /* 11 */
\r
190 FF (b, c, d, a, x[11], S14); /* 12 */
\r
191 FF (a, b, c, d, x[12], S11); /* 13 */
\r
192 FF (d, a, b, c, x[13], S12); /* 14 */
\r
193 FF (c, d, a, b, x[14], S13); /* 15 */
\r
194 FF (b, c, d, a, x[15], S14); /* 16 */
\r
197 GG (a, b, c, d, x[ 0], S21); /* 17 */
\r
198 GG (d, a, b, c, x[ 4], S22); /* 18 */
\r
199 GG (c, d, a, b, x[ 8], S23); /* 19 */
\r
200 GG (b, c, d, a, x[12], S24); /* 20 */
\r
201 GG (a, b, c, d, x[ 1], S21); /* 21 */
\r
202 GG (d, a, b, c, x[ 5], S22); /* 22 */
\r
203 GG (c, d, a, b, x[ 9], S23); /* 23 */
\r
204 GG (b, c, d, a, x[13], S24); /* 24 */
\r
205 GG (a, b, c, d, x[ 2], S21); /* 25 */
\r
206 GG (d, a, b, c, x[ 6], S22); /* 26 */
\r
207 GG (c, d, a, b, x[10], S23); /* 27 */
\r
208 GG (b, c, d, a, x[14], S24); /* 28 */
\r
209 GG (a, b, c, d, x[ 3], S21); /* 29 */
\r
210 GG (d, a, b, c, x[ 7], S22); /* 30 */
\r
211 GG (c, d, a, b, x[11], S23); /* 31 */
\r
212 GG (b, c, d, a, x[15], S24); /* 32 */
\r
215 HH (a, b, c, d, x[ 0], S31); /* 33 */
\r
216 HH (d, a, b, c, x[ 8], S32); /* 34 */
\r
217 HH (c, d, a, b, x[ 4], S33); /* 35 */
\r
218 HH (b, c, d, a, x[12], S34); /* 36 */
\r
219 HH (a, b, c, d, x[ 2], S31); /* 37 */
\r
220 HH (d, a, b, c, x[10], S32); /* 38 */
\r
221 HH (c, d, a, b, x[ 6], S33); /* 39 */
\r
222 HH (b, c, d, a, x[14], S34); /* 40 */
\r
223 HH (a, b, c, d, x[ 1], S31); /* 41 */
\r
224 HH (d, a, b, c, x[ 9], S32); /* 42 */
\r
225 HH (c, d, a, b, x[ 5], S33); /* 43 */
\r
226 HH (b, c, d, a, x[13], S34); /* 44 */
\r
227 HH (a, b, c, d, x[ 3], S31); /* 45 */
\r
228 HH (d, a, b, c, x[11], S32); /* 46 */
\r
229 HH (c, d, a, b, x[ 7], S33); /* 47 */
\r
230 HH (b, c, d, a, x[15], S34); /* 48 */
\r
237 /* Zeroize sensitive information.
\r
239 MD4_memset ((POINTER)x, 0, sizeof (x));
\r
242 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
\r
245 static void Encode (output, input, len)
\r
246 unsigned char *output;
\r
252 for (i = 0, j = 0; j < len; i++, j += 4) {
\r
253 output[j] = (unsigned char)(input[i] & 0xff);
\r
254 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
\r
255 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
\r
256 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
\r
260 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
\r
263 static void Decode (output, input, len)
\r
266 unsigned char *input;
\r
271 for (i = 0, j = 0; j < len; i++, j += 4)
\r
272 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
\r
273 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
\r
276 /* Note: Replace "for loop" with standard memcpy if possible.
\r
278 static void MD4_memcpy (output, input, len)
\r
285 for (i = 0; i < len; i++)
\r
286 output[i] = input[i];
\r
289 /* Note: Replace "for loop" with standard memset if possible.
\r
291 static void MD4_memset (output, value, len)
\r
298 for (i = 0; i < len; i++)
\r
299 ((char *)output)[i] = (char)value;
\r