Back to index

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