Back to index

cell-binutils  2.17cvs20070401
md5.c
Go to the documentation of this file.
00001 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
00002    according to the definition of MD5 in RFC 1321 from April 1992.
00003    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
00004 
00005    NOTE: This source is derived from an old version taken from the GNU C
00006    Library (glibc).
00007 
00008    This program is free software; you can redistribute it and/or modify it
00009    under the terms of the GNU General Public License as published by the
00010    Free Software Foundation; either version 2, or (at your option) any
00011    later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software Foundation,
00020    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 # include <config.h>
00026 #endif
00027 
00028 #include <sys/types.h>
00029 
00030 #if STDC_HEADERS || defined _LIBC
00031 # include <stdlib.h>
00032 # include <string.h>
00033 #else
00034 # ifndef HAVE_MEMCPY
00035 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
00036 # endif
00037 #endif
00038 
00039 #include "ansidecl.h"
00040 #include "md5.h"
00041 
00042 #ifdef _LIBC
00043 # include <endian.h>
00044 # if __BYTE_ORDER == __BIG_ENDIAN
00045 #  define WORDS_BIGENDIAN 1
00046 # endif
00047 #endif
00048 
00049 #ifdef WORDS_BIGENDIAN
00050 # define SWAP(n)                                               \
00051     (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
00052 #else
00053 # define SWAP(n) (n)
00054 #endif
00055 
00056 
00057 /* This array contains the bytes used to pad the buffer to the next
00058    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
00059 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
00060 
00061 
00062 /* Initialize structure containing state of computation.
00063    (RFC 1321, 3.3: Step 3)  */
00064 void
00065 md5_init_ctx (struct md5_ctx *ctx)
00066 {
00067   ctx->A = (md5_uint32) 0x67452301;
00068   ctx->B = (md5_uint32) 0xefcdab89;
00069   ctx->C = (md5_uint32) 0x98badcfe;
00070   ctx->D = (md5_uint32) 0x10325476;
00071 
00072   ctx->total[0] = ctx->total[1] = 0;
00073   ctx->buflen = 0;
00074 }
00075 
00076 /* Put result from CTX in first 16 bytes following RESBUF.  The result
00077    must be in little endian byte order.
00078 
00079    IMPORTANT: On some systems it is required that RESBUF is correctly
00080    aligned for a 32 bits value.  */
00081 void *
00082 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
00083 {
00084   ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
00085   ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
00086   ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
00087   ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
00088 
00089   return resbuf;
00090 }
00091 
00092 /* Process the remaining bytes in the internal buffer and the usual
00093    prolog according to the standard and write the result to RESBUF.
00094 
00095    IMPORTANT: On some systems it is required that RESBUF is correctly
00096    aligned for a 32 bits value.  */
00097 void *
00098 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
00099 {
00100   /* Take yet unprocessed bytes into account.  */
00101   md5_uint32 bytes = ctx->buflen;
00102   size_t pad;
00103 
00104   /* Now count remaining bytes.  */
00105   ctx->total[0] += bytes;
00106   if (ctx->total[0] < bytes)
00107     ++ctx->total[1];
00108 
00109   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
00110   memcpy (&ctx->buffer[bytes], fillbuf, pad);
00111 
00112   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
00113   *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
00114   *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
00115                                                  (ctx->total[0] >> 29));
00116 
00117   /* Process last bytes.  */
00118   md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
00119 
00120   return md5_read_ctx (ctx, resbuf);
00121 }
00122 
00123 /* Compute MD5 message digest for bytes read from STREAM.  The
00124    resulting message digest number will be written into the 16 bytes
00125    beginning at RESBLOCK.  */
00126 int
00127 md5_stream (FILE *stream, void *resblock)
00128 {
00129   /* Important: BLOCKSIZE must be a multiple of 64.  */
00130 #define BLOCKSIZE 4096
00131   struct md5_ctx ctx;
00132   char buffer[BLOCKSIZE + 72];
00133   size_t sum;
00134 
00135   /* Initialize the computation context.  */
00136   md5_init_ctx (&ctx);
00137 
00138   /* Iterate over full file contents.  */
00139   while (1)
00140     {
00141       /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
00142         computation function processes the whole buffer so that with the
00143         next round of the loop another block can be read.  */
00144       size_t n;
00145       sum = 0;
00146 
00147       /* Read block.  Take care for partial reads.  */
00148       do
00149        {
00150          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
00151 
00152          sum += n;
00153        }
00154       while (sum < BLOCKSIZE && n != 0);
00155       if (n == 0 && ferror (stream))
00156         return 1;
00157 
00158       /* If end of file is reached, end the loop.  */
00159       if (n == 0)
00160        break;
00161 
00162       /* Process buffer with BLOCKSIZE bytes.  Note that
00163                      BLOCKSIZE % 64 == 0
00164        */
00165       md5_process_block (buffer, BLOCKSIZE, &ctx);
00166     }
00167 
00168   /* Add the last bytes if necessary.  */
00169   if (sum > 0)
00170     md5_process_bytes (buffer, sum, &ctx);
00171 
00172   /* Construct result in desired memory.  */
00173   md5_finish_ctx (&ctx, resblock);
00174   return 0;
00175 }
00176 
00177 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
00178    result is always in little endian byte order, so that a byte-wise
00179    output yields to the wanted ASCII representation of the message
00180    digest.  */
00181 void *
00182 md5_buffer (const char *buffer, size_t len, void *resblock)
00183 {
00184   struct md5_ctx ctx;
00185 
00186   /* Initialize the computation context.  */
00187   md5_init_ctx (&ctx);
00188 
00189   /* Process whole buffer but last len % 64 bytes.  */
00190   md5_process_bytes (buffer, len, &ctx);
00191 
00192   /* Put result in desired memory area.  */
00193   return md5_finish_ctx (&ctx, resblock);
00194 }
00195 
00196 
00197 void
00198 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
00199 {
00200   /* When we already have some bits in our internal buffer concatenate
00201      both inputs first.  */
00202   if (ctx->buflen != 0)
00203     {
00204       size_t left_over = ctx->buflen;
00205       size_t add = 128 - left_over > len ? len : 128 - left_over;
00206 
00207       memcpy (&ctx->buffer[left_over], buffer, add);
00208       ctx->buflen += add;
00209 
00210       if (left_over + add > 64)
00211        {
00212          md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
00213          /* The regions in the following copy operation cannot overlap.  */
00214          memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
00215                 (left_over + add) & 63);
00216          ctx->buflen = (left_over + add) & 63;
00217        }
00218 
00219       buffer = (const void *) ((const char *) buffer + add);
00220       len -= add;
00221     }
00222 
00223   /* Process available complete blocks.  */
00224   if (len > 64)
00225     {
00226 #if !_STRING_ARCH_unaligned
00227 /* To check alignment gcc has an appropriate operator.  Other
00228    compilers don't.  */
00229 # if __GNUC__ >= 2
00230 #  define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
00231 # else
00232 #  define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
00233 # endif
00234       if (UNALIGNED_P (buffer))
00235         while (len > 64)
00236           {
00237             md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
00238             buffer = (const char *) buffer + 64;
00239             len -= 64;
00240           }
00241       else
00242 #endif
00243       md5_process_block (buffer, len & ~63, ctx);
00244       buffer = (const void *) ((const char *) buffer + (len & ~63));
00245       len &= 63;
00246     }
00247 
00248   /* Move remaining bytes in internal buffer.  */
00249   if (len > 0)
00250     {
00251       memcpy (ctx->buffer, buffer, len);
00252       ctx->buflen = len;
00253     }
00254 }
00255 
00256 
00257 /* These are the four functions used in the four steps of the MD5 algorithm
00258    and defined in the RFC 1321.  The first function is a little bit optimized
00259    (as found in Colin Plumbs public domain implementation).  */
00260 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
00261 #define FF(b, c, d) (d ^ (b & (c ^ d)))
00262 #define FG(b, c, d) FF (d, b, c)
00263 #define FH(b, c, d) (b ^ c ^ d)
00264 #define FI(b, c, d) (c ^ (b | ~d))
00265 
00266 /* Process LEN bytes of BUFFER, accumulating context into CTX.
00267    It is assumed that LEN % 64 == 0.  */
00268 
00269 void
00270 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
00271 {
00272   md5_uint32 correct_words[16];
00273   const md5_uint32 *words = (const md5_uint32 *) buffer;
00274   size_t nwords = len / sizeof (md5_uint32);
00275   const md5_uint32 *endp = words + nwords;
00276   md5_uint32 A = ctx->A;
00277   md5_uint32 B = ctx->B;
00278   md5_uint32 C = ctx->C;
00279   md5_uint32 D = ctx->D;
00280 
00281   /* First increment the byte count.  RFC 1321 specifies the possible
00282      length of the file up to 2^64 bits.  Here we only compute the
00283      number of bytes.  Do a double word increment.  */
00284   ctx->total[0] += len;
00285   if (ctx->total[0] < len)
00286     ++ctx->total[1];
00287 
00288   /* Process all bytes in the buffer with 64 bytes in each round of
00289      the loop.  */
00290   while (words < endp)
00291     {
00292       md5_uint32 *cwp = correct_words;
00293       md5_uint32 A_save = A;
00294       md5_uint32 B_save = B;
00295       md5_uint32 C_save = C;
00296       md5_uint32 D_save = D;
00297 
00298       /* First round: using the given function, the context and a constant
00299         the next context is computed.  Because the algorithms processing
00300         unit is a 32-bit word and it is determined to work on words in
00301         little endian byte order we perhaps have to change the byte order
00302         before the computation.  To reduce the work for the next steps
00303         we store the swapped words in the array CORRECT_WORDS.  */
00304 
00305 #define OP(a, b, c, d, s, T)                                          \
00306       do                                                       \
00307         {                                                      \
00308          a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;            \
00309          ++words;                                              \
00310          CYCLIC (a, s);                                        \
00311          a += b;                                               \
00312         }                                                      \
00313       while (0)
00314 
00315       /* It is unfortunate that C does not provide an operator for
00316         cyclic rotation.  Hope the C compiler is smart enough.  */
00317 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
00318 
00319       /* Before we start, one word to the strange constants.
00320         They are defined in RFC 1321 as
00321 
00322         T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
00323        */
00324 
00325       /* Round 1.  */
00326       OP (A, B, C, D,  7, (md5_uint32) 0xd76aa478);
00327       OP (D, A, B, C, 12, (md5_uint32) 0xe8c7b756);
00328       OP (C, D, A, B, 17, (md5_uint32) 0x242070db);
00329       OP (B, C, D, A, 22, (md5_uint32) 0xc1bdceee);
00330       OP (A, B, C, D,  7, (md5_uint32) 0xf57c0faf);
00331       OP (D, A, B, C, 12, (md5_uint32) 0x4787c62a);
00332       OP (C, D, A, B, 17, (md5_uint32) 0xa8304613);
00333       OP (B, C, D, A, 22, (md5_uint32) 0xfd469501);
00334       OP (A, B, C, D,  7, (md5_uint32) 0x698098d8);
00335       OP (D, A, B, C, 12, (md5_uint32) 0x8b44f7af);
00336       OP (C, D, A, B, 17, (md5_uint32) 0xffff5bb1);
00337       OP (B, C, D, A, 22, (md5_uint32) 0x895cd7be);
00338       OP (A, B, C, D,  7, (md5_uint32) 0x6b901122);
00339       OP (D, A, B, C, 12, (md5_uint32) 0xfd987193);
00340       OP (C, D, A, B, 17, (md5_uint32) 0xa679438e);
00341       OP (B, C, D, A, 22, (md5_uint32) 0x49b40821);
00342 
00343       /* For the second to fourth round we have the possibly swapped words
00344         in CORRECT_WORDS.  Redefine the macro to take an additional first
00345         argument specifying the function to use.  */
00346 #undef OP
00347 #define OP(a, b, c, d, k, s, T)                                       \
00348       do                                                       \
00349        {                                                       \
00350          a += FX (b, c, d) + correct_words[k] + T;                    \
00351          CYCLIC (a, s);                                        \
00352          a += b;                                               \
00353        }                                                       \
00354       while (0)
00355 
00356 #define FX(b, c, d) FG (b, c, d)
00357 
00358       /* Round 2.  */
00359       OP (A, B, C, D,  1,  5, (md5_uint32) 0xf61e2562);
00360       OP (D, A, B, C,  6,  9, (md5_uint32) 0xc040b340);
00361       OP (C, D, A, B, 11, 14, (md5_uint32) 0x265e5a51);
00362       OP (B, C, D, A,  0, 20, (md5_uint32) 0xe9b6c7aa);
00363       OP (A, B, C, D,  5,  5, (md5_uint32) 0xd62f105d);
00364       OP (D, A, B, C, 10,  9, (md5_uint32) 0x02441453);
00365       OP (C, D, A, B, 15, 14, (md5_uint32) 0xd8a1e681);
00366       OP (B, C, D, A,  4, 20, (md5_uint32) 0xe7d3fbc8);
00367       OP (A, B, C, D,  9,  5, (md5_uint32) 0x21e1cde6);
00368       OP (D, A, B, C, 14,  9, (md5_uint32) 0xc33707d6);
00369       OP (C, D, A, B,  3, 14, (md5_uint32) 0xf4d50d87);
00370       OP (B, C, D, A,  8, 20, (md5_uint32) 0x455a14ed);
00371       OP (A, B, C, D, 13,  5, (md5_uint32) 0xa9e3e905);
00372       OP (D, A, B, C,  2,  9, (md5_uint32) 0xfcefa3f8);
00373       OP (C, D, A, B,  7, 14, (md5_uint32) 0x676f02d9);
00374       OP (B, C, D, A, 12, 20, (md5_uint32) 0x8d2a4c8a);
00375 
00376 #undef FX
00377 #define FX(b, c, d) FH (b, c, d)
00378 
00379       /* Round 3.  */
00380       OP (A, B, C, D,  5,  4, (md5_uint32) 0xfffa3942);
00381       OP (D, A, B, C,  8, 11, (md5_uint32) 0x8771f681);
00382       OP (C, D, A, B, 11, 16, (md5_uint32) 0x6d9d6122);
00383       OP (B, C, D, A, 14, 23, (md5_uint32) 0xfde5380c);
00384       OP (A, B, C, D,  1,  4, (md5_uint32) 0xa4beea44);
00385       OP (D, A, B, C,  4, 11, (md5_uint32) 0x4bdecfa9);
00386       OP (C, D, A, B,  7, 16, (md5_uint32) 0xf6bb4b60);
00387       OP (B, C, D, A, 10, 23, (md5_uint32) 0xbebfbc70);
00388       OP (A, B, C, D, 13,  4, (md5_uint32) 0x289b7ec6);
00389       OP (D, A, B, C,  0, 11, (md5_uint32) 0xeaa127fa);
00390       OP (C, D, A, B,  3, 16, (md5_uint32) 0xd4ef3085);
00391       OP (B, C, D, A,  6, 23, (md5_uint32) 0x04881d05);
00392       OP (A, B, C, D,  9,  4, (md5_uint32) 0xd9d4d039);
00393       OP (D, A, B, C, 12, 11, (md5_uint32) 0xe6db99e5);
00394       OP (C, D, A, B, 15, 16, (md5_uint32) 0x1fa27cf8);
00395       OP (B, C, D, A,  2, 23, (md5_uint32) 0xc4ac5665);
00396 
00397 #undef FX
00398 #define FX(b, c, d) FI (b, c, d)
00399 
00400       /* Round 4.  */
00401       OP (A, B, C, D,  0,  6, (md5_uint32) 0xf4292244);
00402       OP (D, A, B, C,  7, 10, (md5_uint32) 0x432aff97);
00403       OP (C, D, A, B, 14, 15, (md5_uint32) 0xab9423a7);
00404       OP (B, C, D, A,  5, 21, (md5_uint32) 0xfc93a039);
00405       OP (A, B, C, D, 12,  6, (md5_uint32) 0x655b59c3);
00406       OP (D, A, B, C,  3, 10, (md5_uint32) 0x8f0ccc92);
00407       OP (C, D, A, B, 10, 15, (md5_uint32) 0xffeff47d);
00408       OP (B, C, D, A,  1, 21, (md5_uint32) 0x85845dd1);
00409       OP (A, B, C, D,  8,  6, (md5_uint32) 0x6fa87e4f);
00410       OP (D, A, B, C, 15, 10, (md5_uint32) 0xfe2ce6e0);
00411       OP (C, D, A, B,  6, 15, (md5_uint32) 0xa3014314);
00412       OP (B, C, D, A, 13, 21, (md5_uint32) 0x4e0811a1);
00413       OP (A, B, C, D,  4,  6, (md5_uint32) 0xf7537e82);
00414       OP (D, A, B, C, 11, 10, (md5_uint32) 0xbd3af235);
00415       OP (C, D, A, B,  2, 15, (md5_uint32) 0x2ad7d2bb);
00416       OP (B, C, D, A,  9, 21, (md5_uint32) 0xeb86d391);
00417 
00418       /* Add the starting values of the context.  */
00419       A += A_save;
00420       B += B_save;
00421       C += C_save;
00422       D += D_save;
00423     }
00424 
00425   /* Put checksum in context given as argument.  */
00426   ctx->A = A;
00427   ctx->B = B;
00428   ctx->C = C;
00429   ctx->D = D;
00430 }