~ubuntu-dev/ubuntu/lucid/mutt/lucid-201002110857

« back to all changes in this revision

Viewing changes to md5.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-06-07 17:30:03 UTC
  • mto: (16.2.1 experimental) (2.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090607173003-rg37ui3h2bbv7wl0
Tags: upstream-1.5.19
ImportĀ upstreamĀ versionĀ 1.5.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* md5.c - Functions to compute MD5 message digest of files or memory blocks
2
2
   according to the definition of MD5 in RFC 1321 from April 1992.
3
 
   Copyright (C) 1995, 1996, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
 
3
   Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2006,2008 Free Software Foundation, Inc.
4
4
   NOTE: The canonical source of this file is maintained with the GNU C
5
5
   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6
6
 
80
80
  ctx->buflen = 0;
81
81
}
82
82
 
 
83
/* Copy the 4 byte value from v into the memory location pointed to by *cp,
 
84
   If your architecture allows unaligned access this is equivalent to
 
85
   * (md5_uint32 *) cp = v  */
 
86
static inline void
 
87
set_uint32 (char *cp, md5_uint32 v)
 
88
{
 
89
  memcpy (cp, &v, sizeof v);
 
90
}
 
91
 
83
92
/* Put result from CTX in first 16 bytes following RESBUF.  The result
84
 
   must be in little endian byte order.
85
 
 
86
 
   IMPORTANT: On some systems it is required that RESBUF is correctly
87
 
   aligned for a 32 bits value.  */
 
93
   must be in little endian byte order.  */
88
94
void *
89
95
md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
90
96
{
91
 
  ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
92
 
  ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
93
 
  ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
94
 
  ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
 
97
  char *r = resbuf;
 
98
  set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
 
99
  set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
 
100
  set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
 
101
  set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
95
102
 
96
103
  return resbuf;
97
104
}
98
105
 
99
106
/* Process the remaining bytes in the internal buffer and the usual
100
 
   prolog according to the standard and write the result to RESBUF.
101
 
 
102
 
   IMPORTANT: On some systems it is required that RESBUF is correctly
103
 
   aligned for a 32 bits value.  */
 
107
   prolog according to the standard and write the result to RESBUF.  */
104
108
void *
105
109
md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
106
110
{
107
111
  /* Take yet unprocessed bytes into account.  */
108
112
  md5_uint32 bytes = ctx->buflen;
109
 
  size_t pad;
 
113
  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
110
114
 
111
115
  /* Now count remaining bytes.  */
112
116
  ctx->total[0] += bytes;
113
117
  if (ctx->total[0] < bytes)
114
118
    ++ctx->total[1];
115
119
 
116
 
  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
117
 
  memcpy (&ctx->buffer[bytes], fillbuf, pad);
118
 
 
119
120
  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
120
 
  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
121
 
  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
122
 
                                                        (ctx->total[0] >> 29));
 
121
  ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
 
122
  ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
 
123
 
 
124
  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
123
125
 
124
126
  /* Process last bytes.  */
125
 
  md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
 
127
  md5_process_block (ctx->buffer, size * 4, ctx);
126
128
 
127
129
  return md5_read_ctx (ctx, resbuf);
128
130
}
144
146
  while (1)
145
147
    {
146
148
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
147
 
         computation function processes the whole buffer so that with the
148
 
         next round of the loop another block can be read.  */
 
149
         computation function processes the whole buffer so that with the
 
150
         next round of the loop another block can be read.  */
149
151
      size_t n;
150
152
      sum = 0;
151
153
 
162
164
          if (n == 0)
163
165
            {
164
166
              /* Check for the error flag IFF N == 0, so that we don't
165
 
                 exit the loop after a partial read due to e.g., EAGAIN
166
 
                 or EWOULDBLOCK.  */
 
167
                 exit the loop after a partial read due to e.g., EAGAIN
 
168
                 or EWOULDBLOCK.  */
167
169
              if (ferror (stream))
168
170
                return 1;
169
171
              goto process_partial_block;
177
179
        }
178
180
 
179
181
      /* Process buffer with BLOCKSIZE bytes.  Note that
180
 
                        BLOCKSIZE % 64 == 0
 
182
         BLOCKSIZE % 64 == 0
181
183
       */
182
184
      md5_process_block (buffer, BLOCKSIZE, &ctx);
183
185
    }
184
186
 
185
 
 process_partial_block:;
 
187
process_partial_block:
186
188
 
187
189
  /* Process any remaining bytes.  */
188
190
  if (sum > 0)
223
225
      size_t left_over = ctx->buflen;
224
226
      size_t add = 128 - left_over > len ? len : 128 - left_over;
225
227
 
226
 
      memcpy (&ctx->buffer[left_over], buffer, add);
 
228
      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
227
229
      ctx->buflen += add;
228
230
 
229
231
      if (ctx->buflen > 64)
232
234
 
233
235
          ctx->buflen &= 63;
234
236
          /* The regions in the following copy operation cannot overlap.  */
235
 
          memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
 
237
          memcpy (ctx->buffer,
 
238
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
236
239
                  ctx->buflen);
237
240
        }
238
241
 
267
270
    {
268
271
      size_t left_over = ctx->buflen;
269
272
 
270
 
      memcpy (&ctx->buffer[left_over], buffer, len);
 
273
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
271
274
      left_over += len;
272
275
      if (left_over >= 64)
273
276
        {
274
277
          md5_process_block (ctx->buffer, 64, ctx);
275
278
          left_over -= 64;
276
 
          memcpy (ctx->buffer, &ctx->buffer[64], left_over);
 
279
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
277
280
        }
278
281
      ctx->buflen = left_over;
279
282
    }
322
325
      md5_uint32 D_save = D;
323
326
 
324
327
      /* First round: using the given function, the context and a constant
325
 
         the next context is computed.  Because the algorithms processing
326
 
         unit is a 32-bit word and it is determined to work on words in
327
 
         little endian byte order we perhaps have to change the byte order
328
 
         before the computation.  To reduce the work for the next steps
329
 
         we store the swapped words in the array CORRECT_WORDS.  */
 
328
         the next context is computed.  Because the algorithms processing
 
329
         unit is a 32-bit word and it is determined to work on words in
 
330
         little endian byte order we perhaps have to change the byte order
 
331
         before the computation.  To reduce the work for the next steps
 
332
         we store the swapped words in the array CORRECT_WORDS.  */
330
333
 
331
334
#define OP(a, b, c, d, s, T)                                            \
332
335
      do                                                                \
339
342
      while (0)
340
343
 
341
344
      /* It is unfortunate that C does not provide an operator for
342
 
         cyclic rotation.  Hope the C compiler is smart enough.  */
 
345
         cyclic rotation.  Hope the C compiler is smart enough.  */
343
346
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
344
347
 
345
348
      /* Before we start, one word to the strange constants.
346
 
         They are defined in RFC 1321 as
347
 
 
348
 
         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
349
 
 
350
 
         Here is an equivalent invocation using Perl:
351
 
 
352
 
         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
 
349
         They are defined in RFC 1321 as
 
350
 
 
351
         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
 
352
 
 
353
         Here is an equivalent invocation using Perl:
 
354
 
 
355
         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
353
356
       */
354
357
 
355
358
      /* Round 1.  */
356
 
      OP (A, B, C, D,  7, 0xd76aa478);
 
359
      OP (A, B, C, D, 7, 0xd76aa478);
357
360
      OP (D, A, B, C, 12, 0xe8c7b756);
358
361
      OP (C, D, A, B, 17, 0x242070db);
359
362
      OP (B, C, D, A, 22, 0xc1bdceee);
360
 
      OP (A, B, C, D,  7, 0xf57c0faf);
 
363
      OP (A, B, C, D, 7, 0xf57c0faf);
361
364
      OP (D, A, B, C, 12, 0x4787c62a);
362
365
      OP (C, D, A, B, 17, 0xa8304613);
363
366
      OP (B, C, D, A, 22, 0xfd469501);
364
 
      OP (A, B, C, D,  7, 0x698098d8);
 
367
      OP (A, B, C, D, 7, 0x698098d8);
365
368
      OP (D, A, B, C, 12, 0x8b44f7af);
366
369
      OP (C, D, A, B, 17, 0xffff5bb1);
367
370
      OP (B, C, D, A, 22, 0x895cd7be);
368
 
      OP (A, B, C, D,  7, 0x6b901122);
 
371
      OP (A, B, C, D, 7, 0x6b901122);
369
372
      OP (D, A, B, C, 12, 0xfd987193);
370
373
      OP (C, D, A, B, 17, 0xa679438e);
371
374
      OP (B, C, D, A, 22, 0x49b40821);
372
375
 
373
376
      /* For the second to fourth round we have the possibly swapped words
374
 
         in CORRECT_WORDS.  Redefine the macro to take an additional first
375
 
         argument specifying the function to use.  */
 
377
         in CORRECT_WORDS.  Redefine the macro to take an additional first
 
378
         argument specifying the function to use.  */
376
379
#undef OP
377
380
#define OP(f, a, b, c, d, k, s, T)                                      \
378
381
      do                                                                \
384
387
      while (0)
385
388
 
386
389
      /* Round 2.  */
387
 
      OP (FG, A, B, C, D,  1,  5, 0xf61e2562);
388
 
      OP (FG, D, A, B, C,  6,  9, 0xc040b340);
 
390
      OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
 
391
      OP (FG, D, A, B, C, 6, 9, 0xc040b340);
389
392
      OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
390
 
      OP (FG, B, C, D, A,  0, 20, 0xe9b6c7aa);
391
 
      OP (FG, A, B, C, D,  5,  5, 0xd62f105d);
392
 
      OP (FG, D, A, B, C, 10,  9, 0x02441453);
 
393
      OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
 
394
      OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
 
395
      OP (FG, D, A, B, C, 10, 9, 0x02441453);
393
396
      OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
394
 
      OP (FG, B, C, D, A,  4, 20, 0xe7d3fbc8);
395
 
      OP (FG, A, B, C, D,  9,  5, 0x21e1cde6);
396
 
      OP (FG, D, A, B, C, 14,  9, 0xc33707d6);
397
 
      OP (FG, C, D, A, B,  3, 14, 0xf4d50d87);
398
 
      OP (FG, B, C, D, A,  8, 20, 0x455a14ed);
399
 
      OP (FG, A, B, C, D, 13,  5, 0xa9e3e905);
400
 
      OP (FG, D, A, B, C,  2,  9, 0xfcefa3f8);
401
 
      OP (FG, C, D, A, B,  7, 14, 0x676f02d9);
 
397
      OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
 
398
      OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
 
399
      OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
 
400
      OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
 
401
      OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
 
402
      OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
 
403
      OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
 
404
      OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
402
405
      OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
403
406
 
404
407
      /* Round 3.  */
405
 
      OP (FH, A, B, C, D,  5,  4, 0xfffa3942);
406
 
      OP (FH, D, A, B, C,  8, 11, 0x8771f681);
 
408
      OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
 
409
      OP (FH, D, A, B, C, 8, 11, 0x8771f681);
407
410
      OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
408
411
      OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
409
 
      OP (FH, A, B, C, D,  1,  4, 0xa4beea44);
410
 
      OP (FH, D, A, B, C,  4, 11, 0x4bdecfa9);
411
 
      OP (FH, C, D, A, B,  7, 16, 0xf6bb4b60);
 
412
      OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
 
413
      OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
 
414
      OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
412
415
      OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
413
 
      OP (FH, A, B, C, D, 13,  4, 0x289b7ec6);
414
 
      OP (FH, D, A, B, C,  0, 11, 0xeaa127fa);
415
 
      OP (FH, C, D, A, B,  3, 16, 0xd4ef3085);
416
 
      OP (FH, B, C, D, A,  6, 23, 0x04881d05);
417
 
      OP (FH, A, B, C, D,  9,  4, 0xd9d4d039);
 
416
      OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
 
417
      OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
 
418
      OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
 
419
      OP (FH, B, C, D, A, 6, 23, 0x04881d05);
 
420
      OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
418
421
      OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
419
422
      OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
420
 
      OP (FH, B, C, D, A,  2, 23, 0xc4ac5665);
 
423
      OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
421
424
 
422
425
      /* Round 4.  */
423
 
      OP (FI, A, B, C, D,  0,  6, 0xf4292244);
424
 
      OP (FI, D, A, B, C,  7, 10, 0x432aff97);
 
426
      OP (FI, A, B, C, D, 0, 6, 0xf4292244);
 
427
      OP (FI, D, A, B, C, 7, 10, 0x432aff97);
425
428
      OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
426
 
      OP (FI, B, C, D, A,  5, 21, 0xfc93a039);
427
 
      OP (FI, A, B, C, D, 12,  6, 0x655b59c3);
428
 
      OP (FI, D, A, B, C,  3, 10, 0x8f0ccc92);
 
429
      OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
 
430
      OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
 
431
      OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
429
432
      OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
430
 
      OP (FI, B, C, D, A,  1, 21, 0x85845dd1);
431
 
      OP (FI, A, B, C, D,  8,  6, 0x6fa87e4f);
 
433
      OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
 
434
      OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
432
435
      OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
433
 
      OP (FI, C, D, A, B,  6, 15, 0xa3014314);
 
436
      OP (FI, C, D, A, B, 6, 15, 0xa3014314);
434
437
      OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
435
 
      OP (FI, A, B, C, D,  4,  6, 0xf7537e82);
 
438
      OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
436
439
      OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
437
 
      OP (FI, C, D, A, B,  2, 15, 0x2ad7d2bb);
438
 
      OP (FI, B, C, D, A,  9, 21, 0xeb86d391);
 
440
      OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
 
441
      OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
439
442
 
440
443
      /* Add the starting values of the context.  */
441
444
      A += A_save;
450
453
  ctx->C = C;
451
454
  ctx->D = D;
452
455
}
 
456
 
 
457
#ifdef MD5UTIL
 
458
/* local md5 equivalent for header cache versioning */
 
459
int main(int argc, char** argv)
 
460
{
 
461
  unsigned char r[16];
 
462
  int rc;
 
463
 
 
464
  if ((rc = md5_stream(stdin, r)))
 
465
    return rc;
 
466
 
 
467
  printf("%02x%02x%02x%02x%02x%02x%02x%02x"
 
468
         "%02x%02x%02x%02x%02x%02x%02x%02x\n",
 
469
         r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7],
 
470
         r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]);
 
471
 
 
472
  return 0;
 
473
}
 
474
#endif