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