pull
[9413project001.git] / Decrypt / Decrypt / md5.cpp
blobc1856ff71ddbfeb77c3cab5671b3e5fc8f520917
1 /* Functions to compute MD5 message digest of files or memory blocks.
2 according to the definition of MD5 in RFC 1321 from April 1992.
3 Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2011
4 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
23 #include <sys/types.h>
24 # include <stdlib.h>
25 # include <string.h>
26 #include "crypt.h"
27 #include "md5.h"
28 #define NEW_ALGORITHM
30 #ifdef NEW_ALGORITHM
31 MD5_EXPORT void constructBuffer32(struct md5_ctx *ctx) MD5_FUNC_RESTRICT
33 unsigned int i;
34 for (i=0; i<64; i+=4)
36 ctx->buffer32[i>>2] = ctx->tempbuf[i] + (ctx->tempbuf[i+1]<<8) + (ctx->tempbuf[i+2]<<16) + (ctx->tempbuf[i+3]<<24);
39 #endif
41 #if MD5_LIB == LIB_CPU
42 /* This array contains the bytes used to pad the buffer to the next
43 64-byte boundary. (RFC 1321, 3.1: Step 1) */
44 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
45 #endif
47 /* Initialize structure containing state of computation.
48 (RFC 1321, 3.3: Step 3) */
49 MD5_EXPORT void md5_init_ctx (struct md5_ctx *ctx) MD5_FUNC_RESTRICT
51 ctx->A = 0x67452301;
52 ctx->B = 0xefcdab89;
53 ctx->C = 0x98badcfe;
54 ctx->D = 0x10325476;
56 ctx->total[0] = ctx->total[1] = 0;
57 ctx->buflen = 0;
60 /* Put result from CTX in first 16 bytes following RESBUF. The result
61 must be in little endian byte order.
63 IMPORTANT: On some systems it is required that RESBUF is correctly
64 aligned for a 32 bits value. */
65 MD5_EXPORT void* md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) MD5_FUNC_RESTRICT
67 ((md5_uint32 *) resbuf)[0] = (ctx->A);
68 ((md5_uint32 *) resbuf)[1] = (ctx->B);
69 ((md5_uint32 *) resbuf)[2] = (ctx->C);
70 ((md5_uint32 *) resbuf)[3] = (ctx->D);
72 return resbuf;
75 /* Process the remaining bytes in the internal buffer and the usual
76 prolog according to the standard and write the result to RESBUF.
78 IMPORTANT: On some systems it is required that RESBUF is correctly
79 aligned for a 32 bits value. */
80 MD5_EXPORT void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) MD5_FUNC_RESTRICT
82 /* Take yet unprocessed bytes into account. */
83 md5_uint32 bytes = ctx->buflen;
84 size_t pad;
86 /* Now count remaining bytes. */
87 ctx->total[0] += bytes;
88 if (ctx->total[0] < bytes)
89 ++ctx->total[1];
91 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
92 #ifdef NEW_ALGORITHM
93 for (unsigned int i=0; i<pad; i++)
95 if (i == 0)
97 ctx->tempbuf[bytes + i] = 0x80;
99 else
101 ctx->tempbuf[bytes + i] = 0;
104 #else
105 memcpy (&ctx->buffer[bytes], fillbuf, pad);
106 #endif
107 #ifdef NEW_ALGORITHM
108 constructBuffer32(ctx);
109 #endif
110 /* Put the 64-bit file length in *bits* at the end of the buffer. */
111 ctx->buffer32[(bytes + pad) / 4] = (ctx->total[0] << 3);
112 ctx->buffer32[(bytes + pad + 4) / 4] = ((ctx->total[1] << 3) |
113 (ctx->total[0] >> 29));
115 /* Process last bytes. */
117 md5_process_block (ctx->buffer32, bytes + pad + 8, ctx);
119 return md5_read_ctx (ctx, resbuf);
123 MD5_EXPORT void md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) MD5_FUNC_RESTRICT
125 /* When we already have some bits in our internal buffer concatenate
126 both inputs first. */
127 if (ctx->buflen != 0)
129 size_t left_over = ctx->buflen;
130 size_t add = 128 - left_over > len ? len : 128 - left_over;
132 #ifdef NEW_ALGORITHM
133 for (unsigned int i=0; i<add; ++i)
135 int shiftBit = (i&0x3)<<3;
136 int oneChar = ((const unsigned int*)buffer)[i>>2] & (0xff<<shiftBit);
137 // and 0xff to avoid sign carrying shift
138 oneChar = (oneChar>>shiftBit)&0xff;
139 ctx->tempbuf[left_over+i] = oneChar;
141 #else
142 memcpy (&ctx->buffer[left_over], buffer, add);
143 #endif
144 ctx->buflen += add;
146 //if (ctx->buflen > 64)
148 // md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
150 // ctx->buflen &= 63;
151 // /* The regions in the following copy operation cannot overlap. */
152 // memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], ctx->buflen);
155 //buffer = (const char *) buffer + add;
156 //buffer = (const char *) buffer + add;
157 len -= add;
160 /* Process available complete blocks. */
161 //if (len >= 64)
163 // {
164 // md5_process_block (buffer, len & ~63, ctx);
165 // buffer = (const char *) buffer + (len & ~63);
166 // len &= 63;
167 // }
170 /* Move remaining bytes in internal buffer. */
171 if (len > 0)
173 size_t left_over = ctx->buflen;
174 #ifdef NEW_ALGORITHM
175 for (unsigned int i=0; i<len; i++)
177 int shiftBit = (i&0x3)<<3;
178 int oneChar = ((const unsigned int*)buffer)[i>>2] & (0xff<<shiftBit);
179 oneChar = oneChar>>shiftBit;
180 ctx->tempbuf[left_over+i] = oneChar;
182 #else
183 memcpy (&ctx->buffer[left_over], buffer, len);
184 #endif
185 left_over += len;
186 //if (left_over >= 64)
188 // md5_process_block (ctx->buffer, 64, ctx);
189 // left_over -= 64;
190 // memcpy (ctx->buffer, &ctx->buffer[64], left_over);
192 ctx->buflen = left_over;
197 /* These are the four functions used in the four steps of the MD5 algorithm
198 and defined in the RFC 1321. The first function is a little bit optimized
199 (as found in Colin Plumbs public domain implementation). */
200 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
201 #define FF(b, c, d) (d ^ (b & (c ^ d)))
202 #define FG(b, c, d) FF (d, b, c)
203 #define FH(b, c, d) (b ^ c ^ d)
204 #define FI(b, c, d) (c ^ (b | ~d))
206 /* Process LEN bytes of BUFFER, accumulating context into CTX.
207 It is assumed that LEN % 64 == 0. */
209 MD5_EXPORT void md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) MD5_FUNC_RESTRICT
211 md5_uint32 correct_words[16];
212 const md5_uint32 *words = static_cast<const md5_uint32 *>(buffer);
213 size_t nwords = len / sizeof (md5_uint32);
214 const md5_uint32 *endp = words + nwords;
215 md5_uint32 A = ctx->A;
216 md5_uint32 B = ctx->B;
217 md5_uint32 C = ctx->C;
218 md5_uint32 D = ctx->D;
220 /* First increment the byte count. RFC 1321 specifies the possible
221 length of the file up to 2^64 bits. Here we only compute the
222 number of bytes. Do a double word increment. */
223 ctx->total[0] += len;
224 if (ctx->total[0] < len)
225 ++ctx->total[1];
227 /* Process all bytes in the buffer with 64 bytes in each round of
228 the loop. */
229 while (words < endp)
231 md5_uint32 *cwp = correct_words;
232 md5_uint32 A_save = A;
233 md5_uint32 B_save = B;
234 md5_uint32 C_save = C;
235 md5_uint32 D_save = D;
237 /* First round: using the given function, the context and a constant
238 the next context is computed. Because the algorithms processing
239 unit is a 32-bit word and it is determined to work on words in
240 little endian byte order we perhaps have to change the byte order
241 before the computation. To reduce the work for the next steps
242 we store the swapped words in the array CORRECT_WORDS. */
244 #define OP(a, b, c, d, s, T) \
246 a += FF (b, c, d) + (*cwp++ = *words) + T; \
247 ++words; \
248 CYCLIC (a, s); \
249 a += b; \
251 /* It is unfortunate that C does not provide an operator for
252 cyclic rotation. Hope the C compiler is smart enough. */
253 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
255 /* Before we start, one word to the strange constants.
256 They are defined in RFC 1321 as
258 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
261 /* Round 1. */
262 OP (A, B, C, D, 7, 0xd76aa478);
263 OP (D, A, B, C, 12, 0xe8c7b756);
264 OP (C, D, A, B, 17, 0x242070db);
265 OP (B, C, D, A, 22, 0xc1bdceee);
266 OP (A, B, C, D, 7, 0xf57c0faf);
267 OP (D, A, B, C, 12, 0x4787c62a);
268 OP (C, D, A, B, 17, 0xa8304613);
269 OP (B, C, D, A, 22, 0xfd469501);
270 OP (A, B, C, D, 7, 0x698098d8);
271 OP (D, A, B, C, 12, 0x8b44f7af);
272 OP (C, D, A, B, 17, 0xffff5bb1);
273 OP (B, C, D, A, 22, 0x895cd7be);
274 OP (A, B, C, D, 7, 0x6b901122);
275 OP (D, A, B, C, 12, 0xfd987193);
276 OP (C, D, A, B, 17, 0xa679438e);
277 OP (B, C, D, A, 22, 0x49b40821);
279 /* For the second to fourth round we have the possibly swapped words
280 in CORRECT_WORDS. Redefine the macro to take an additional first
281 argument specifying the function to use. */
282 #undef OP
283 #define OP(f, a, b, c, d, k, s, T) \
286 a += f (b, c, d) + correct_words[k] + T; \
287 CYCLIC (a, s); \
288 a += b; \
291 /* Round 2. */
292 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
293 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
294 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
295 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
296 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
297 OP (FG, D, A, B, C, 10, 9, 0x02441453);
298 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
299 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
300 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
301 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
302 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
303 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
304 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
305 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
306 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
307 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
309 /* Round 3. */
310 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
311 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
312 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
313 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
314 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
315 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
316 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
317 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
318 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
319 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
320 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
321 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
322 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
323 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
324 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
325 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
327 /* Round 4. */
328 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
329 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
330 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
331 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
332 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
333 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
334 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
335 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
336 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
337 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
338 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
339 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
340 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
341 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
342 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
343 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
345 /* Add the starting values of the context. */
346 A += A_save;
347 B += B_save;
348 C += C_save;
349 D += D_save;
352 /* Put checksum in context given as argument. */
353 ctx->A = A;
354 ctx->B = B;
355 ctx->C = C;
356 ctx->D = D;