2 #include "../precompiled.h"
6 RSA Data Security, Inc., MD4 message-digest algorithm. (RFC1320)
11 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
14 License to copy and use this software is granted provided that it
15 is identified as the "RSA Data Security, Inc. MD4 Message-Digest
16 Algorithm" in all material mentioning or referencing this software
19 License is also granted to make and use derivative works provided
20 that such works are identified as "derived from the RSA Data
21 Security, Inc. MD4 Message-Digest Algorithm" in all material
22 mentioning or referencing the derived work.
24 RSA Data Security, Inc. makes no representations concerning either
25 the merchantability of this software or the suitability of this
26 software for any particular purpose. It is provided "as is"
27 without express or implied warranty of any kind.
29 These notices must be retained in any copies of any part of this
30 documentation and/or software.
34 /* POINTER defines a generic pointer type */
35 typedef unsigned char *POINTER;
37 /* UINT2 defines a two byte word */
38 typedef unsigned short int UINT2;
40 /* UINT4 defines a four byte word */
41 typedef unsigned long int UINT4;
45 UINT4 state[4]; /* state (ABCD) */
46 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
47 unsigned char buffer[64]; /* input buffer */
50 /* Constants for MD4Transform routine. */
64 static unsigned char PADDING[64] = {
65 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
68 /* F, G and H are basic MD4 functions. */
69 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
70 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
71 #define H(x, y, z) ((x) ^ (y) ^ (z))
73 /* ROTATE_LEFT rotates x left n bits. */
74 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
76 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
77 /* Rotation is separate from addition to prevent recomputation */
78 #define FF(a, b, c, d, x, s) {(a) += F ((b), (c), (d)) + (x); (a) = ROTATE_LEFT ((a), (s));}
80 #define GG(a, b, c, d, x, s) {(a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; (a) = ROTATE_LEFT ((a), (s));}
82 #define HH(a, b, c, d, x, s) {(a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; (a) = ROTATE_LEFT ((a), (s));}
84 /* Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. */
85 static void Encode( unsigned char *output, UINT4 *input, unsigned int len ) {
88 for ( i = 0, j = 0; j < len; i++, j += 4 ) {
89 output[j] = (unsigned char)(input[i] & 0xff);
90 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
91 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
92 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
96 /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */
97 static void Decode( UINT4 *output, const unsigned char *input, unsigned int len ) {
100 for ( i = 0, j = 0; j < len; i++, j += 4 ) {
101 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
105 /* MD4 basic transformation. Transforms state based on block. */
106 static void MD4_Transform( UINT4 state[4], const unsigned char block[64] ) {
107 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
109 Decode (x, block, 64);
112 FF (a, b, c, d, x[ 0], S11); /* 1 */
113 FF (d, a, b, c, x[ 1], S12); /* 2 */
114 FF (c, d, a, b, x[ 2], S13); /* 3 */
115 FF (b, c, d, a, x[ 3], S14); /* 4 */
116 FF (a, b, c, d, x[ 4], S11); /* 5 */
117 FF (d, a, b, c, x[ 5], S12); /* 6 */
118 FF (c, d, a, b, x[ 6], S13); /* 7 */
119 FF (b, c, d, a, x[ 7], S14); /* 8 */
120 FF (a, b, c, d, x[ 8], S11); /* 9 */
121 FF (d, a, b, c, x[ 9], S12); /* 10 */
122 FF (c, d, a, b, x[10], S13); /* 11 */
123 FF (b, c, d, a, x[11], S14); /* 12 */
124 FF (a, b, c, d, x[12], S11); /* 13 */
125 FF (d, a, b, c, x[13], S12); /* 14 */
126 FF (c, d, a, b, x[14], S13); /* 15 */
127 FF (b, c, d, a, x[15], S14); /* 16 */
130 GG (a, b, c, d, x[ 0], S21); /* 17 */
131 GG (d, a, b, c, x[ 4], S22); /* 18 */
132 GG (c, d, a, b, x[ 8], S23); /* 19 */
133 GG (b, c, d, a, x[12], S24); /* 20 */
134 GG (a, b, c, d, x[ 1], S21); /* 21 */
135 GG (d, a, b, c, x[ 5], S22); /* 22 */
136 GG (c, d, a, b, x[ 9], S23); /* 23 */
137 GG (b, c, d, a, x[13], S24); /* 24 */
138 GG (a, b, c, d, x[ 2], S21); /* 25 */
139 GG (d, a, b, c, x[ 6], S22); /* 26 */
140 GG (c, d, a, b, x[10], S23); /* 27 */
141 GG (b, c, d, a, x[14], S24); /* 28 */
142 GG (a, b, c, d, x[ 3], S21); /* 29 */
143 GG (d, a, b, c, x[ 7], S22); /* 30 */
144 GG (c, d, a, b, x[11], S23); /* 31 */
145 GG (b, c, d, a, x[15], S24); /* 32 */
148 HH (a, b, c, d, x[ 0], S31); /* 33 */
149 HH (d, a, b, c, x[ 8], S32); /* 34 */
150 HH (c, d, a, b, x[ 4], S33); /* 35 */
151 HH (b, c, d, a, x[12], S34); /* 36 */
152 HH (a, b, c, d, x[ 2], S31); /* 37 */
153 HH (d, a, b, c, x[10], S32); /* 38 */
154 HH (c, d, a, b, x[ 6], S33); /* 39 */
155 HH (b, c, d, a, x[14], S34); /* 40 */
156 HH (a, b, c, d, x[ 1], S31); /* 41 */
157 HH (d, a, b, c, x[ 9], S32); /* 42 */
158 HH (c, d, a, b, x[ 5], S33); /* 43 */
159 HH (b, c, d, a, x[13], S34); /* 44 */
160 HH (a, b, c, d, x[ 3], S31); /* 45 */
161 HH (d, a, b, c, x[11], S32); /* 46 */
162 HH (c, d, a, b, x[ 7], S33); /* 47 */
163 HH (b, c, d, a, x[15], S34); /* 48 */
170 /* Zeroize sensitive information.*/
171 memset ((POINTER)x, 0, sizeof (x));
174 /* MD4 initialization. Begins an MD4 operation, writing a new context. */
175 void MD4_Init( MD4_CTX *context ) {
176 context->count[0] = context->count[1] = 0;
178 /* Load magic initialization constants.*/
179 context->state[0] = 0x67452301;
180 context->state[1] = 0xefcdab89;
181 context->state[2] = 0x98badcfe;
182 context->state[3] = 0x10325476;
185 /* MD4 block update operation. Continues an MD4 message-digest operation, processing another message block, and updating the context. */
186 void MD4_Update( MD4_CTX *context, const unsigned char *input, unsigned int inputLen ) {
187 unsigned int i, index, partLen;
189 /* Compute number of bytes mod 64 */
190 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
192 /* Update number of bits */
193 if ((context->count[0] += ((UINT4)inputLen << 3))< ((UINT4)inputLen << 3)) {
197 context->count[1] += ((UINT4)inputLen >> 29);
199 partLen = 64 - index;
201 /* Transform as many times as possible.*/
202 if ( inputLen >= partLen ) {
203 memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
204 MD4_Transform (context->state, context->buffer);
206 for ( i = partLen; i + 63 < inputLen; i += 64 ) {
207 MD4_Transform (context->state, &input[i]);
215 /* Buffer remaining input */
216 memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
219 /* MD4 finalization. Ends an MD4 message-digest operation, writing the message digest and zeroizing the context. */
220 void MD4_Final( MD4_CTX *context, unsigned char digest[16] ) {
221 unsigned char bits[8];
222 unsigned int index, padLen;
224 /* Save number of bits */
225 Encode( bits, context->count, 8 );
227 /* Pad out to 56 mod 64.*/
228 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
229 padLen = (index < 56) ? (56 - index) : (120 - index);
230 MD4_Update (context, PADDING, padLen);
232 /* Append length (before padding) */
233 MD4_Update( context, bits, 8 );
235 /* Store state in digest */
236 Encode( digest, context->state, 16 );
238 /* Zeroize sensitive information.*/
239 memset ((POINTER)context, 0, sizeof (*context));
247 unsigned long MD4_BlockChecksum( const void *data, int length ) {
248 unsigned long digest[4];
253 MD4_Update( &ctx, (unsigned char *)data, length );
254 MD4_Final( &ctx, (unsigned char *)digest );
256 val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];