QOF  0.8.7
md5.c
1 /* md5.c - 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, 2011 Free Software Foundation, Inc.
4  NOTE: The canonical source of this file is maintained with the GNU C
5  Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 
7  This program is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2, or (at your option) any
10  later version.
11 
12  This program 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
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 
21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
22 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <sys/types.h>
30 
31 #if STDC_HEADERS || defined _LIBC
32 # include <stdlib.h>
33 # include <string.h>
34 #else
35 # ifndef HAVE_MEMCPY
36 #include <string.h>
37 # define memcpy(d, s, n) bcopy ((s), (d), (n))
38 # endif
39 #endif
40 
41 #include "md5.h"
42 
43 #ifdef _LIBC
44 # include <endian.h>
45 # if __BYTE_ORDER == __BIG_ENDIAN
46 # define WORDS_BIGENDIAN 1
47 # endif
48 #endif
49 
50 #ifdef WORDS_BIGENDIAN
51 # define SWAP(n) \
52  (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
53 #else
54 # define SWAP(n) (n)
55 #endif
56 
57 
58 /* This array contains the bytes used to pad the buffer to the next
59  64-byte boundary. (RFC 1321, 3.1: Step 1) */
60 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
61 
62 
63 /* Initialize structure containing state of computation.
64  (RFC 1321, 3.3: Step 3) */
65 void
66 md5_init_ctx (ctx)
67  struct md5_ctx *ctx;
68 {
69  ctx->A = 0x67452301;
70  ctx->B = 0xefcdab89;
71  ctx->C = 0x98badcfe;
72  ctx->D = 0x10325476;
73 
74  ctx->total[0] = ctx->total[1] = 0;
75  ctx->buflen = 0;
76 }
77 
78 /* Put result from CTX in first 16 bytes following RESBUF. The result
79  must be in little endian byte order.
80 
81  IMPORTANT: On some systems it is required that RESBUF is correctly
82  aligned for a 32 bits value. */
83 void *
84 md5_read_ctx (ctx, resbuf)
85  const struct md5_ctx *ctx;
86  void *resbuf;
87 {
88  ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
89  ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
90  ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
91  ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
92 
93  return resbuf;
94 }
95 
96 /* Process the remaining bytes in the internal buffer and the usual
97  prolog according to the standard and write the result to RESBUF.
98 
99  IMPORTANT: On some systems it is required that RESBUF is correctly
100  aligned for a 32 bits value. */
101 void *
102 md5_finish_ctx (ctx, resbuf)
103  struct md5_ctx *ctx;
104  void *resbuf;
105 {
106  /* Take yet unprocessed bytes into account. */
107  md5_uint32 bytes = ctx->buflen;
108  size_t pad;
109 
110  /* Now count remaining bytes. */
111  ctx->total[0] += bytes;
112  if (ctx->total[0] < bytes)
113  ++ctx->total[1];
114 
115  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
116  memcpy (&ctx->buffer[bytes], fillbuf, pad);
117 
118  /* Put the 64-bit file length in *bits* at the end of the buffer. */
119  ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
120  ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
121  (ctx->total[0] >> 29));
122 
123  /* Process last bytes. */
124  md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
125 
126  return md5_read_ctx (ctx, resbuf);
127 }
128 
129 /* Compute MD5 message digest for bytes read from STREAM. The
130  resulting message digest number will be written into the 16 bytes
131  beginning at RESBLOCK. */
132 int
133 md5_stream (stream, resblock)
134  FILE *stream;
135  void *resblock;
136 {
137  /* Important: BLOCKSIZE must be a multiple of 64. */
138 #define BLOCKSIZE 4096
139  struct md5_ctx ctx;
140  char buffer[BLOCKSIZE + 72];
141  size_t sum;
142 
143  /* Initialize the computation context. */
144  md5_init_ctx (&ctx);
145 
146  /* Iterate over full file contents. */
147  while (1)
148  {
149  /* We read the file in blocks of BLOCKSIZE bytes. One call of the
150  computation function processes the whole buffer so that with the
151  next round of the loop another block can be read. */
152  size_t n;
153  sum = 0;
154 
155  /* Read block. Take care for partial reads. */
156  do
157  {
158  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
159 
160  sum += n;
161  }
162  while (sum < BLOCKSIZE && n != 0);
163  if (n == 0 && ferror (stream))
164  return 1;
165 
166  /* If end of file is reached, end the loop. */
167  if (n == 0)
168  break;
169 
170  /* Process buffer with BLOCKSIZE bytes. Note that
171  BLOCKSIZE % 64 == 0
172  */
173  md5_process_block (buffer, BLOCKSIZE, &ctx);
174  }
175 
176  /* Add the last bytes if necessary. */
177  if (sum > 0)
178  md5_process_bytes (buffer, sum, &ctx);
179 
180  /* Construct result in desired memory. */
181  md5_finish_ctx (&ctx, resblock);
182  return 0;
183 }
184 
185 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
186  result is always in little endian byte order, so that a byte-wise
187  output yields to the wanted ASCII representation of the message
188  digest. */
189 void *
190 md5_buffer (buffer, len, resblock)
191  const char *buffer;
192  size_t len;
193  void *resblock;
194 {
195  struct md5_ctx ctx;
196 
197  /* Initialize the computation context. */
198  md5_init_ctx (&ctx);
199 
200  /* Process whole buffer but last len % 64 bytes. */
201  md5_process_bytes (buffer, len, &ctx);
202 
203  /* Put result in desired memory area. */
204  return md5_finish_ctx (&ctx, resblock);
205 }
206 
207 
208 void
209 md5_process_bytes (buffer, len, ctx)
210  const void *buffer;
211  size_t len;
212  struct md5_ctx *ctx;
213 {
214 #define NUM_MD5_WORDS 1024
215  size_t add = 0;
216 
217  /* When we already have some bits in our internal buffer concatenate
218  both inputs first. */
219  if (ctx->buflen != 0)
220  {
221  size_t left_over = ctx->buflen;
222 
223  add = 128 - left_over > len ? len : 128 - left_over;
224 
225  memcpy (&ctx->buffer[left_over], buffer, add);
226  ctx->buflen += add;
227 
228  if (left_over + add > 64)
229  {
230  md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
231  /* The regions in the following copy operation cannot overlap. */
232  memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
233  (left_over + add) & 63);
234  ctx->buflen = (left_over + add) & 63;
235  }
236 
237  buffer = (const char *) buffer + add;
238  len -= add;
239  }
240 
241  /* Process available complete blocks. */
242  if (len > 64)
243  {
244  if ((add & 3) == 0) /* buffer is still 32-bit aligned */
245  {
246  md5_process_block (buffer, len & ~63, ctx);
247  buffer = (const char *) buffer + (len & ~63);
248  }
249  else /* buffer is not 32-bit aligned */
250  {
251  md5_uint32 md5_buffer[NUM_MD5_WORDS];
252  size_t num_bytes;
253  size_t buf_bytes;
254 
255  num_bytes = len & ~63;
256  while (num_bytes > 0)
257  {
258  buf_bytes = (num_bytes < sizeof (md5_buffer)) ?
259  num_bytes : sizeof (md5_buffer);
260  memcpy (md5_buffer, buffer, buf_bytes);
261  md5_process_block (md5_buffer, buf_bytes, ctx);
262  num_bytes -= buf_bytes;
263  buffer = (const char *) buffer + buf_bytes;
264  }
265  }
266 
267  len &= 63;
268  }
269 
270  /* Move remaining bytes in internal buffer. */
271  if (len > 0)
272  {
273  memcpy (ctx->buffer, buffer, len);
274  ctx->buflen = len;
275  }
276 }
277 
278 
279 /* These are the four functions used in the four steps of the MD5 algorithm
280  and defined in the RFC 1321. The first function is a little bit optimized
281  (as found in Colin Plumbs public domain implementation). */
282 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
283 #define FF(b, c, d) (d ^ (b & (c ^ d)))
284 #define FG(b, c, d) FF (d, b, c)
285 #define FH(b, c, d) (b ^ c ^ d)
286 #define FI(b, c, d) (c ^ (b | ~d))
287 
288 /* Process LEN bytes of BUFFER, accumulating context into CTX.
289  It is assumed that LEN % 64 == 0. */
290 
291 void
292 md5_process_block (buffer, len, ctx)
293  const void *buffer;
294  size_t len;
295  struct md5_ctx *ctx;
296 {
297  md5_uint32 correct_words[16];
298  const md5_uint32 *words = buffer;
299  size_t nwords = len / sizeof (md5_uint32);
300  const md5_uint32 *endp = words + nwords;
301  md5_uint32 A = ctx->A;
302  md5_uint32 B = ctx->B;
303  md5_uint32 C = ctx->C;
304  md5_uint32 D = ctx->D;
305 
306  /* First increment the byte count. RFC 1321 specifies the possible
307  length of the file up to 2^64 bits. Here we only compute the
308  number of bytes. Do a double word increment. */
309  ctx->total[0] += len;
310  if (ctx->total[0] < len)
311  ++ctx->total[1];
312 
313  /* Process all bytes in the buffer with 64 bytes in each round of
314  the loop. */
315  while (words < endp)
316  {
317  md5_uint32 *cwp = correct_words;
318  md5_uint32 A_save = A;
319  md5_uint32 B_save = B;
320  md5_uint32 C_save = C;
321  md5_uint32 D_save = D;
322 
323  /* First round: using the given function, the context and a constant
324  the next context is computed. Because the algorithms processing
325  unit is a 32-bit word and it is determined to work on words in
326  little endian byte order we perhaps have to change the byte order
327  before the computation. To reduce the work for the next steps
328  we store the swapped words in the array CORRECT_WORDS. */
329 
330 #define OP(a, b, c, d, s, T) \
331  do \
332  { \
333  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
334  ++words; \
335  CYCLIC (a, s); \
336  a += b; \
337  } \
338  while (0)
339 
340  /* It is unfortunate that C does not provide an operator for
341  cyclic rotation. Hope the C compiler is smart enough. */
342 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
343 
344  /* Before we start, one word to the strange constants.
345  They are defined in RFC 1321 as
346 
347  T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
348  */
349 
350  /* Round 1. */
351  OP (A, B, C, D, 7, 0xd76aa478);
352  OP (D, A, B, C, 12, 0xe8c7b756);
353  OP (C, D, A, B, 17, 0x242070db);
354  OP (B, C, D, A, 22, 0xc1bdceee);
355  OP (A, B, C, D, 7, 0xf57c0faf);
356  OP (D, A, B, C, 12, 0x4787c62a);
357  OP (C, D, A, B, 17, 0xa8304613);
358  OP (B, C, D, A, 22, 0xfd469501);
359  OP (A, B, C, D, 7, 0x698098d8);
360  OP (D, A, B, C, 12, 0x8b44f7af);
361  OP (C, D, A, B, 17, 0xffff5bb1);
362  OP (B, C, D, A, 22, 0x895cd7be);
363  OP (A, B, C, D, 7, 0x6b901122);
364  OP (D, A, B, C, 12, 0xfd987193);
365  OP (C, D, A, B, 17, 0xa679438e);
366  OP (B, C, D, A, 22, 0x49b40821);
367 
368  /* For the second to fourth round we have the possibly swapped words
369  in CORRECT_WORDS. Redefine the macro to take an additional first
370  argument specifying the function to use. */
371 #undef OP
372 #define OP(f, a, b, c, d, k, s, T) \
373  do \
374  { \
375  a += f (b, c, d) + correct_words[k] + T; \
376  CYCLIC (a, s); \
377  a += b; \
378  } \
379  while (0)
380 
381  /* Round 2. */
382  OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
383  OP (FG, D, A, B, C, 6, 9, 0xc040b340);
384  OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
385  OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
386  OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
387  OP (FG, D, A, B, C, 10, 9, 0x02441453);
388  OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
389  OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
390  OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
391  OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
392  OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
393  OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
394  OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
395  OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
396  OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
397  OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
398 
399  /* Round 3. */
400  OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
401  OP (FH, D, A, B, C, 8, 11, 0x8771f681);
402  OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
403  OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
404  OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
405  OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
406  OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
407  OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
408  OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
409  OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
410  OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
411  OP (FH, B, C, D, A, 6, 23, 0x04881d05);
412  OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
413  OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
414  OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
415  OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
416 
417  /* Round 4. */
418  OP (FI, A, B, C, D, 0, 6, 0xf4292244);
419  OP (FI, D, A, B, C, 7, 10, 0x432aff97);
420  OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
421  OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
422  OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
423  OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
424  OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
425  OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
426  OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
427  OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
428  OP (FI, C, D, A, B, 6, 15, 0xa3014314);
429  OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
430  OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
431  OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
432  OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
433  OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
434 
435  /* Add the starting values of the context. */
436  A += A_save;
437  B += B_save;
438  C += C_save;
439  D += D_save;
440  }
441 
442  /* Put checksum in context given as argument. */
443  ctx->A = A;
444  ctx->B = B;
445  ctx->C = C;
446  ctx->D = D;
447 }
Definition: md5.h:80