Blender V2.61 - r43446

md5.c

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