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