|
Blender
V2.59
|
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 <sys/types.h> 00025 00026 # include <stdlib.h> 00027 # include <string.h> 00028 00029 #include "md5.h" 00030 00031 #ifdef WORDS_BIGENDIAN 00032 # define SWAP(n) \ 00033 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) 00034 #else 00035 # define SWAP(n) (n) 00036 #endif 00037 00038 00039 /* This array contains the bytes used to pad the buffer to the next 00040 64-byte boundary. (RFC 1321, 3.1: Step 1) */ 00041 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; 00042 00043 00044 /* Initialize structure containing state of computation. 00045 (RFC 1321, 3.3: Step 3) */ 00046 void 00047 md5_init_ctx (ctx) 00048 struct md5_ctx *ctx; 00049 { 00050 ctx->A = 0x67452301; 00051 ctx->B = 0xefcdab89; 00052 ctx->C = 0x98badcfe; 00053 ctx->D = 0x10325476; 00054 } 00055 00056 /* Put result from CTX in first 16 bytes following RESBUF. The result must 00057 be in little endian byte order. */ 00058 void * 00059 md5_read_ctx (ctx, resbuf) 00060 const struct md5_ctx *ctx; 00061 void *resbuf; 00062 { 00063 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); 00064 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); 00065 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); 00066 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); 00067 00068 return resbuf; 00069 } 00070 00071 /* Compute MD5 message digest for bytes read from STREAM. The 00072 resulting message digest number will be written into the 16 bytes 00073 beginning at RESBLOCK. */ 00074 int 00075 md5_stream (stream, resblock) 00076 FILE *stream; 00077 void *resblock; 00078 { 00079 /* Important: BLOCKSIZE must be a multiple of 64. */ 00080 #define BLOCKSIZE 4096 00081 struct md5_ctx ctx; 00082 md5_uint32 len[2]; 00083 char buffer[BLOCKSIZE + 72]; 00084 size_t pad, sum; 00085 00086 /* Initialize the computation context. */ 00087 md5_init_ctx (&ctx); 00088 00089 len[0] = 0; 00090 len[1] = 0; 00091 00092 /* Iterate over full file contents. */ 00093 while (1) 00094 { 00095 /* We read the file in blocks of BLOCKSIZE bytes. One call of the 00096 computation function processes the whole buffer so that with the 00097 next round of the loop another block can be read. */ 00098 size_t n; 00099 sum = 0; 00100 00101 /* Read block. Take care for partial reads. */ 00102 do 00103 { 00104 n = fread (buffer, 1, BLOCKSIZE - sum, stream); 00105 00106 sum += n; 00107 } 00108 while (sum < BLOCKSIZE && n != 0); 00109 if (n == 0 && ferror (stream)) 00110 return 1; 00111 00112 /* RFC 1321 specifies the possible length of the file up to 2^64 bits. 00113 Here we only compute the number of bytes. Do a double word 00114 increment. */ 00115 len[0] += sum; 00116 if (len[0] < sum) 00117 ++len[1]; 00118 00119 /* If end of file is reached, end the loop. */ 00120 if (n == 0) 00121 break; 00122 00123 /* Process buffer with BLOCKSIZE bytes. Note that 00124 BLOCKSIZE % 64 == 0 00125 */ 00126 md5_process_block (buffer, BLOCKSIZE, &ctx); 00127 } 00128 00129 /* We can copy 64 byte because the buffer is always big enough. FILLBUF 00130 contains the needed bits. */ 00131 memcpy (&buffer[sum], fillbuf, 64); 00132 00133 /* Compute amount of padding bytes needed. Alignment is done to 00134 (N + PAD) % 64 == 56 00135 There is always at least one byte padded. I.e. even the alignment 00136 is correctly aligned 64 padding bytes are added. */ 00137 pad = sum & 63; 00138 pad = pad >= 56 ? 64 + 56 - pad : 56 - pad; 00139 00140 /* Put the 64-bit file length in *bits* at the end of the buffer. */ 00141 *(md5_uint32 *) &buffer[sum + pad] = SWAP (len[0] << 3); 00142 *(md5_uint32 *) &buffer[sum + pad + 4] = SWAP ((len[1] << 3) 00143 | (len[0] >> 29)); 00144 00145 /* Process last bytes. */ 00146 md5_process_block (buffer, sum + pad + 8, &ctx); 00147 00148 /* Construct result in desired memory. */ 00149 md5_read_ctx (&ctx, resblock); 00150 return 0; 00151 } 00152 00153 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 00154 result is always in little endian byte order, so that a byte-wise 00155 output yields to the wanted ASCII representation of the message 00156 digest. */ 00157 void * 00158 md5_buffer (buffer, len, resblock) 00159 const char *buffer; 00160 size_t len; 00161 void *resblock; 00162 { 00163 struct md5_ctx ctx; 00164 char restbuf[64 + 72]; 00165 size_t blocks = len & ~63; 00166 size_t pad, rest; 00167 00168 /* Initialize the computation context. */ 00169 md5_init_ctx (&ctx); 00170 00171 /* Process whole buffer but last len % 64 bytes. */ 00172 md5_process_block (buffer, blocks, &ctx); 00173 00174 /* REST bytes are not processed yet. */ 00175 rest = len - blocks; 00176 /* Copy to own buffer. */ 00177 memcpy (restbuf, &buffer[blocks], rest); 00178 /* Append needed fill bytes at end of buffer. We can copy 64 byte 00179 because the buffer is always big enough. */ 00180 memcpy (&restbuf[rest], fillbuf, 64); 00181 00182 /* PAD bytes are used for padding to correct alignment. Note that 00183 always at least one byte is padded. */ 00184 pad = rest >= 56 ? 64 + 56 - rest : 56 - rest; 00185 00186 /* Put length of buffer in *bits* in last eight bytes. */ 00187 *(md5_uint32 *) &restbuf[rest + pad] = (md5_uint32) SWAP (len << 3); 00188 *(md5_uint32 *) &restbuf[rest + pad + 4] = (md5_uint32) SWAP (len >> 29); 00189 00190 /* Process last bytes. */ 00191 md5_process_block (restbuf, rest + pad + 8, &ctx); 00192 00193 /* Put result in desired memory area. */ 00194 return md5_read_ctx (&ctx, resblock); 00195 } 00196 00197 00198 /* These are the four functions used in the four steps of the MD5 algorithm 00199 and defined in the RFC 1321. The first function is a little bit optimized 00200 (as found in Colin Plumbs public domain implementation). */ 00201 /* #define FF(b, c, d) ((b & c) | (~b & d)) */ 00202 #define FF(b, c, d) (d ^ (b & (c ^ d))) 00203 #define FG(b, c, d) FF (d, b, c) 00204 #define FH(b, c, d) (b ^ c ^ d) 00205 #define FI(b, c, d) (c ^ (b | ~d)) 00206 00207 /* Process LEN bytes of BUFFER, accumulating context into CTX. 00208 It is assumed that LEN % 64 == 0. */ 00209 00210 void 00211 md5_process_block (buffer, len, ctx) 00212 const void *buffer; 00213 size_t len; 00214 struct md5_ctx *ctx; 00215 { 00216 md5_uint32 correct_words[16]; 00217 const md5_uint32 *words = buffer; 00218 size_t nwords = len / sizeof (md5_uint32); 00219 const md5_uint32 *endp = words + nwords; 00220 md5_uint32 A = ctx->A; 00221 md5_uint32 B = ctx->B; 00222 md5_uint32 C = ctx->C; 00223 md5_uint32 D = ctx->D; 00224 00225 /* Process all bytes in the buffer with 64 bytes in each round of 00226 the loop. */ 00227 while (words < endp) 00228 { 00229 md5_uint32 *cwp = correct_words; 00230 md5_uint32 A_save = A; 00231 md5_uint32 B_save = B; 00232 md5_uint32 C_save = C; 00233 md5_uint32 D_save = D; 00234 00235 /* First round: using the given function, the context and a constant 00236 the next context is computed. Because the algorithms processing 00237 unit is a 32-bit word and it is determined to work on words in 00238 little endian byte order we perhaps have to change the byte order 00239 before the computation. To reduce the work for the next steps 00240 we store the swapped words in the array CORRECT_WORDS. */ 00241 00242 #define OP(a, b, c, d, s, T) \ 00243 do \ 00244 { \ 00245 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ 00246 ++words; \ 00247 CYCLIC (a, s); \ 00248 a += b; \ 00249 } \ 00250 while (0) 00251 00252 /* It is unfortunate that C does not provide an operator for 00253 cyclic rotation. Hope the C compiler is smart enough. */ 00254 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) 00255 00256 /* Before we start, one word to the strange constants. 00257 They are defined in RFC 1321 as 00258 00259 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 00260 */ 00261 00262 /* Round 1. */ 00263 OP (A, B, C, D, 7, 0xd76aa478); 00264 OP (D, A, B, C, 12, 0xe8c7b756); 00265 OP (C, D, A, B, 17, 0x242070db); 00266 OP (B, C, D, A, 22, 0xc1bdceee); 00267 OP (A, B, C, D, 7, 0xf57c0faf); 00268 OP (D, A, B, C, 12, 0x4787c62a); 00269 OP (C, D, A, B, 17, 0xa8304613); 00270 OP (B, C, D, A, 22, 0xfd469501); 00271 OP (A, B, C, D, 7, 0x698098d8); 00272 OP (D, A, B, C, 12, 0x8b44f7af); 00273 OP (C, D, A, B, 17, 0xffff5bb1); 00274 OP (B, C, D, A, 22, 0x895cd7be); 00275 OP (A, B, C, D, 7, 0x6b901122); 00276 OP (D, A, B, C, 12, 0xfd987193); 00277 OP (C, D, A, B, 17, 0xa679438e); 00278 OP (B, C, D, A, 22, 0x49b40821); 00279 00280 /* For the second to fourth round we have the possibly swapped words 00281 in CORRECT_WORDS. Redefine the macro to take an additional first 00282 argument specifying the function to use. */ 00283 #undef OP 00284 #define OP(f, a, b, c, d, k, s, T) \ 00285 do \ 00286 { \ 00287 a += f (b, c, d) + correct_words[k] + T; \ 00288 CYCLIC (a, s); \ 00289 a += b; \ 00290 } \ 00291 while (0) 00292 00293 /* Round 2. */ 00294 OP (FG, A, B, C, D, 1, 5, 0xf61e2562); 00295 OP (FG, D, A, B, C, 6, 9, 0xc040b340); 00296 OP (FG, C, D, A, B, 11, 14, 0x265e5a51); 00297 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); 00298 OP (FG, A, B, C, D, 5, 5, 0xd62f105d); 00299 OP (FG, D, A, B, C, 10, 9, 0x02441453); 00300 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); 00301 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); 00302 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); 00303 OP (FG, D, A, B, C, 14, 9, 0xc33707d6); 00304 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); 00305 OP (FG, B, C, D, A, 8, 20, 0x455a14ed); 00306 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); 00307 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); 00308 OP (FG, C, D, A, B, 7, 14, 0x676f02d9); 00309 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); 00310 00311 /* Round 3. */ 00312 OP (FH, A, B, C, D, 5, 4, 0xfffa3942); 00313 OP (FH, D, A, B, C, 8, 11, 0x8771f681); 00314 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); 00315 OP (FH, B, C, D, A, 14, 23, 0xfde5380c); 00316 OP (FH, A, B, C, D, 1, 4, 0xa4beea44); 00317 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); 00318 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); 00319 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); 00320 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); 00321 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); 00322 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); 00323 OP (FH, B, C, D, A, 6, 23, 0x04881d05); 00324 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); 00325 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); 00326 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); 00327 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); 00328 00329 /* Round 4. */ 00330 OP (FI, A, B, C, D, 0, 6, 0xf4292244); 00331 OP (FI, D, A, B, C, 7, 10, 0x432aff97); 00332 OP (FI, C, D, A, B, 14, 15, 0xab9423a7); 00333 OP (FI, B, C, D, A, 5, 21, 0xfc93a039); 00334 OP (FI, A, B, C, D, 12, 6, 0x655b59c3); 00335 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); 00336 OP (FI, C, D, A, B, 10, 15, 0xffeff47d); 00337 OP (FI, B, C, D, A, 1, 21, 0x85845dd1); 00338 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); 00339 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); 00340 OP (FI, C, D, A, B, 6, 15, 0xa3014314); 00341 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); 00342 OP (FI, A, B, C, D, 4, 6, 0xf7537e82); 00343 OP (FI, D, A, B, C, 11, 10, 0xbd3af235); 00344 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); 00345 OP (FI, B, C, D, A, 9, 21, 0xeb86d391); 00346 00347 /* Add the starting values of the context. */ 00348 A += A_save; 00349 B += B_save; 00350 C += C_save; 00351 D += D_save; 00352 } 00353 00354 /* Put checksum in context given as argument. */ 00355 ctx->A = A; 00356 ctx->B = B; 00357 ctx->C = C; 00358 ctx->D = D; 00359 } 00360