build: skip checking for networks source directories
[mldonkey.git] / src / utils / lib / md4.c
blob48d69ca3c76535d79af3b655a49b2a6858c50829
1 /* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA */
2 /*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "md4.h"
20 #include <memory.h>
22 /* Constants for MD4Transform routine.
24 #define S11 3
25 #define S12 7
26 #define S13 11
27 #define S14 19
28 #define S21 3
29 #define S22 5
30 #define S23 9
31 #define S24 13
32 #define S31 3
33 #define S32 9
34 #define S33 11
35 #define S34 15
37 static void MD4Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
38 static void Encode PROTO_LIST ((unsigned char *, UINT4 *, unsigned int));
39 static void Decode PROTO_LIST ((UINT4 *, unsigned char *, unsigned int));
42 static unsigned char PADDING[64] = {
43 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48 /* F, G and H are basic MD4 functions.
50 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
51 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
52 #define H(x, y, z) ((x) ^ (y) ^ (z))
54 /* ROTATE_LEFT rotates x left n bits.
56 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
58 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
59 /* Rotation is separate from addition to prevent recomputation */
61 #define FF(a, b, c, d, x, s) { \
62 (a) += F ((b), (c), (d)) + (x); \
63 (a) = ROTATE_LEFT ((a), (s)); \
65 #define GG(a, b, c, d, x, s) { \
66 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
67 (a) = ROTATE_LEFT ((a), (s)); \
69 #define HH(a, b, c, d, x, s) { \
70 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
71 (a) = ROTATE_LEFT ((a), (s)); \
74 /* MD4 initialization. Begins an MD4 operation, writing a new context.
76 void MD4Init (context)
77 MD4_CTX *context; /* context */
79 context->count[0] = context->count[1] = 0;
81 /* Load magic initialization constants.
83 context->state[0] = 0x67452301;
84 context->state[1] = 0xefcdab89;
85 context->state[2] = 0x98badcfe;
86 context->state[3] = 0x10325476;
89 /* MD4 block update operation. Continues an MD4 message-digest
90 operation, processing another message block, and updating the
91 context.
93 void MD4Update (context, input, inputLen)
94 MD4_CTX *context; /* context */
95 unsigned char *input; /* input block */
96 unsigned int inputLen; /* length of input block */
98 unsigned int i, index, partLen;
100 /* Compute number of bytes mod 64 */
101 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
102 /* Update number of bits */
103 if ((context->count[0] += ((UINT4)inputLen << 3))
104 < ((UINT4)inputLen << 3))
105 context->count[1]++;
106 context->count[1] += ((UINT4)inputLen >> 29);
108 partLen = 64 - index;
110 /* Transform as many times as possible.
112 if (inputLen >= partLen) {
113 memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
114 MD4Transform (context->state, context->buffer);
116 for (i = partLen; i + 63 < inputLen; i += 64)
117 MD4Transform (context->state, &input[i]);
119 index = 0;
121 else
122 i = 0;
124 /* Buffer remaining input */
125 memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
128 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
129 the message digest and zeroizing the context.
131 void MD4Final (digest, context)
132 unsigned char digest[16]; /* message digest */
133 MD4_CTX *context; /* context */
135 unsigned char bits[8];
136 unsigned int index, padLen;
138 /* Save number of bits */
139 Encode (bits, context->count, 8);
141 /* Pad out to 56 mod 64.
143 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
144 padLen = (index < 56) ? (56 - index) : (120 - index);
145 MD4Update (context, PADDING, padLen);
147 /* Append length (before padding) */
148 MD4Update (context, bits, 8);
149 /* Store state in digest */
150 Encode (digest, context->state, 16);
152 /* Zeroize sensitive information.
154 memset ((POINTER)context, 0, sizeof (*context));
158 /* MD4 basic transformation. Transforms state based on block.
160 static void MD4Transform (state, block)
161 UINT4 state[4];
162 unsigned char block[64];
164 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
166 Decode (x, block, 64);
168 /* Round 1 */
169 FF (a, b, c, d, x[ 0], S11); /* 1 */
170 FF (d, a, b, c, x[ 1], S12); /* 2 */
171 FF (c, d, a, b, x[ 2], S13); /* 3 */
172 FF (b, c, d, a, x[ 3], S14); /* 4 */
173 FF (a, b, c, d, x[ 4], S11); /* 5 */
174 FF (d, a, b, c, x[ 5], S12); /* 6 */
175 FF (c, d, a, b, x[ 6], S13); /* 7 */
176 FF (b, c, d, a, x[ 7], S14); /* 8 */
177 FF (a, b, c, d, x[ 8], S11); /* 9 */
178 FF (d, a, b, c, x[ 9], S12); /* 10 */
179 FF (c, d, a, b, x[10], S13); /* 11 */
180 FF (b, c, d, a, x[11], S14); /* 12 */
181 FF (a, b, c, d, x[12], S11); /* 13 */
182 FF (d, a, b, c, x[13], S12); /* 14 */
183 FF (c, d, a, b, x[14], S13); /* 15 */
184 FF (b, c, d, a, x[15], S14); /* 16 */
186 /* Round 2 */
187 GG (a, b, c, d, x[ 0], S21); /* 17 */
188 GG (d, a, b, c, x[ 4], S22); /* 18 */
189 GG (c, d, a, b, x[ 8], S23); /* 19 */
190 GG (b, c, d, a, x[12], S24); /* 20 */
191 GG (a, b, c, d, x[ 1], S21); /* 21 */
192 GG (d, a, b, c, x[ 5], S22); /* 22 */
193 GG (c, d, a, b, x[ 9], S23); /* 23 */
194 GG (b, c, d, a, x[13], S24); /* 24 */
195 GG (a, b, c, d, x[ 2], S21); /* 25 */
196 GG (d, a, b, c, x[ 6], S22); /* 26 */
197 GG (c, d, a, b, x[10], S23); /* 27 */
198 GG (b, c, d, a, x[14], S24); /* 28 */
199 GG (a, b, c, d, x[ 3], S21); /* 29 */
200 GG (d, a, b, c, x[ 7], S22); /* 30 */
201 GG (c, d, a, b, x[11], S23); /* 31 */
202 GG (b, c, d, a, x[15], S24); /* 32 */
204 /* Round 3 */
205 HH (a, b, c, d, x[ 0], S31); /* 33 */
206 HH (d, a, b, c, x[ 8], S32); /* 34 */
207 HH (c, d, a, b, x[ 4], S33); /* 35 */
208 HH (b, c, d, a, x[12], S34); /* 36 */
209 HH (a, b, c, d, x[ 2], S31); /* 37 */
210 HH (d, a, b, c, x[10], S32); /* 38 */
211 HH (c, d, a, b, x[ 6], S33); /* 39 */
212 HH (b, c, d, a, x[14], S34); /* 40 */
213 HH (a, b, c, d, x[ 1], S31); /* 41 */
214 HH (d, a, b, c, x[ 9], S32); /* 42 */
215 HH (c, d, a, b, x[ 5], S33); /* 43 */
216 HH (b, c, d, a, x[13], S34); /* 44 */
217 HH (a, b, c, d, x[ 3], S31); /* 45 */
218 HH (d, a, b, c, x[11], S32); /* 46 */
219 HH (c, d, a, b, x[ 7], S33); /* 47 */
220 HH (b, c, d, a, x[15], S34); /* 48 */
222 state[0] += a;
223 state[1] += b;
224 state[2] += c;
225 state[3] += d;
227 /* Zeroize sensitive information.
229 memset ((POINTER)x, 0, sizeof (x));
232 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
233 a multiple of 4.
235 static void Encode (output, input, len)
236 unsigned char *output;
237 UINT4 *input;
238 unsigned int len;
240 unsigned int i, j;
242 for (i = 0, j = 0; j < len; i++, j += 4) {
243 output[j] = (unsigned char)(input[i] & 0xff);
244 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
245 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
246 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
250 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
251 a multiple of 4.
254 static void Decode (output, input, len)
256 UINT4 *output;
257 unsigned char *input;
258 unsigned int len;
260 unsigned int i, j;
262 for (i = 0, j = 0; j < len; i++, j += 4)
263 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
264 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);