Back to index

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