Changeset 4206
- Timestamp:
- 03/14/08 12:06:10 (5 years ago)
- Location:
- trunk/src/target/opkg/libopkg
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/target/opkg/libopkg/md5.c
r4029 r4206 1 /* md5.c - Compute MD5 checksum of files or strings according to the 2 * definition of MD5 in RFC 1321 from April 1992. 3 * Copyright (C) 1995-1999 Free Software Foundation, Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2, or (at your option) 8 * any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software Foundation, 17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 */ 19 20 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> */ 21 /* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> */ 22 23 /* Sucked directly into opkg since the md5sum functions aren't in libbb 24 Dropped a few functions since opkg only needs md5_stream. 25 Got rid of evil, twisted defines of FALSE=1 and TRUE=0 26 6 March 2002 Carl Worth <cworth@east.isi.edu> 27 */ 28 29 /* 30 * June 29, 2001 Manuel Novoa III 31 * 32 * Added MD5SUM_SIZE_VS_SPEED configuration option. 33 * 34 * Current valid values, with data from my system for comparison, are: 35 * (using uClibc and running on linux-2.4.4.tar.bz2) 36 * user times (sec) text size (386) 37 * 0 (fastest) 1.1 6144 38 * 1 1.4 5392 39 * 2 3.0 5088 40 * 3 (smallest) 5.1 4912 41 */ 42 43 #define MD5SUM_SIZE_VS_SPEED 3 44 45 /**********************************************************************/ 46 47 #include <stdio.h> 48 #include <errno.h> 49 #include <ctype.h> 50 #include <getopt.h> 1 /* Functions to compute MD5 message digest of files or memory blocks. 2 according to the definition of MD5 in RFC 1321 from April 1992. 3 Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2006,2008 4 Free Software Foundation, Inc. 5 This file is part of the GNU C Library. 6 7 This program is free software; you can redistribute it and/or modify it 8 under the terms of the GNU General Public License as published by the 9 Free Software Foundation; either version 2, or (at your option) any 10 later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software Foundation, 19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */ 22 23 #include <config.h> 24 25 #include "md5.h" 26 27 #include <stddef.h> 51 28 #include <stdlib.h> 52 29 #include <string.h> 53 #include <endian.h>54 30 #include <sys/types.h> 55 #if defined HAVE_LIMITS_H 56 # include <limits.h> 31 32 #if USE_UNLOCKED_IO 33 # include "unlocked-io.h" 57 34 #endif 58 35 59 #include "md5.h" 60 61 //---------------------------------------------------------------------------- 62 //--------md5.c 63 //---------------------------------------------------------------------------- 64 65 /* md5.c - Functions to compute MD5 message digest of files or memory blocks 66 * according to the definition of MD5 in RFC 1321 from April 1992. 67 */ 68 69 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */ 70 71 //---------------------------------------------------------------------------- 72 //--------md5.h 73 //---------------------------------------------------------------------------- 74 75 /* md5.h - Declaration of functions and data types used for MD5 sum 76 computing library functions. */ 77 78 typedef u_int32_t md5_uint32; 79 80 /* Structure to save state of computation between the single steps. */ 81 struct md5_ctx 82 { 83 md5_uint32 A; 84 md5_uint32 B; 85 md5_uint32 C; 86 md5_uint32 D; 87 88 md5_uint32 total[2]; 89 md5_uint32 buflen; 90 char buffer[128]; 91 }; 92 93 /* 94 * The following three functions are build up the low level used in 95 * the functions `md5_stream' and `md5_buffer'. 96 */ 36 #ifdef _LIBC 37 # include <endian.h> 38 # if __BYTE_ORDER == __BIG_ENDIAN 39 # define WORDS_BIGENDIAN 1 40 # endif 41 /* We need to keep the namespace clean so define the MD5 function 42 protected using leading __ . */ 43 # define md5_init_ctx __md5_init_ctx 44 # define md5_process_block __md5_process_block 45 # define md5_process_bytes __md5_process_bytes 46 # define md5_finish_ctx __md5_finish_ctx 47 # define md5_read_ctx __md5_read_ctx 48 # define md5_stream __md5_stream 49 # define md5_buffer __md5_buffer 50 #endif 51 52 #ifdef WORDS_BIGENDIAN 53 # define SWAP(n) \ 54 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) 55 #else 56 # define SWAP(n) (n) 57 #endif 58 59 #define BLOCKSIZE 4096 60 #if BLOCKSIZE % 64 != 0 61 # error "invalid BLOCKSIZE" 62 #endif 63 64 /* This array contains the bytes used to pad the buffer to the next 65 64-byte boundary. (RFC 1321, 3.1: Step 1) */ 66 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; 67 97 68 98 69 /* Initialize structure containing state of computation. 99 70 (RFC 1321, 3.3: Step 3) */ 100 static void md5_init_ctx __P ((struct md5_ctx *ctx)); 101 102 /* Starting with the result of former calls of this function (or the 103 initialization function update the context for the next LEN bytes 104 starting at BUFFER. 105 It is necessary that LEN is a multiple of 64!!! */ 106 static void md5_process_block __P ((const void *buffer, size_t len, 107 struct md5_ctx *ctx)); 108 109 /* Starting with the result of former calls of this function (or the 110 initialization function update the context for the next LEN bytes 111 starting at BUFFER. 112 It is NOT required that LEN is a multiple of 64. */ 113 static void md5_process_bytes __P ((const void *buffer, size_t len, 114 struct md5_ctx *ctx)); 115 116 /* Process the remaining bytes in the buffer and put result from CTX 117 in first 16 bytes following RESBUF. The result is always in little 118 endian byte order, so that a byte-wise output yields to the wanted 119 ASCII representation of the message digest. 120 121 IMPORTANT: On some systems it is required that RESBUF is correctly 122 aligned for a 32 bits value. */ 123 static void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); 124 125 //---------------------------------------------------------------------------- 126 //--------end of md5.h 127 //---------------------------------------------------------------------------- 128 129 /* Handle endian-ness */ 130 #if __BYTE_ORDER == __LITTLE_ENDIAN 131 #define SWAP(n) (n) 132 #else 133 #define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24)) 134 #endif 135 136 137 138 #if MD5SUM_SIZE_VS_SPEED == 0 139 /* This array contains the bytes used to pad the buffer to the next 140 64-byte boundary. (RFC 1321, 3.1: Step 1) */ 141 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; 142 #endif 143 144 /* Initialize structure containing state of computation. 145 (RFC 1321, 3.3: Step 3) */ 146 void md5_init_ctx(struct md5_ctx *ctx) 71 void 72 md5_init_ctx (struct md5_ctx *ctx) 147 73 { 148 74 ctx->A = 0x67452301; … … 155 81 } 156 82 83 /* Copy the 4 byte value from v into the memory location pointed to by *cp, 84 If your architecture allows unaligned access this is equivalent to 85 * (uint32_t *) cp = v */ 86 static inline void 87 set_uint32 (char *cp, uint32_t v) 88 { 89 memcpy (cp, &v, sizeof v); 90 } 91 92 /* Put result from CTX in first 16 bytes following RESBUF. The result 93 must be in little endian byte order. */ 94 void * 95 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) 96 { 97 char *r = resbuf; 98 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A)); 99 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B)); 100 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C)); 101 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D)); 102 103 return resbuf; 104 } 105 157 106 /* Process the remaining bytes in the internal buffer and the usual 158 prolog according to the standard and write the result to RESBUF. 159 160 IMPORTANT: On some systems it is required that RESBUF is correctly 161 aligned for a 32 bits value. */ 162 static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf) 107 prolog according to the standard and write the result to RESBUF. */ 108 void * 109 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) 163 110 { 164 111 /* Take yet unprocessed bytes into account. */ 165 md5_uint32bytes = ctx->buflen;166 size_t pad;112 uint32_t bytes = ctx->buflen; 113 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; 167 114 168 115 /* Now count remaining bytes. */ … … 171 118 ++ctx->total[1]; 172 119 173 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;174 #if MD5SUM_SIZE_VS_SPEED > 0175 memset(&ctx->buffer[bytes], 0, pad);176 ctx->buffer[bytes] = 0x80;177 #else178 memcpy(&ctx->buffer[bytes], fillbuf, pad);179 #endif180 181 120 /* Put the 64-bit file length in *bits* at the end of the buffer. */ 182 *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3); 183 *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] = 184 SWAP( ((ctx->total[1] << 3) | (ctx->total[0] >> 29)) ); 121 ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3); 122 ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); 123 124 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); 185 125 186 126 /* Process last bytes. */ 187 md5_process_block(ctx->buffer, bytes + pad + 8, ctx); 188 189 /* Put result from CTX in first 16 bytes following RESBUF. The result is 190 always in little endian byte order, so that a byte-wise output yields 191 to the wanted ASCII representation of the message digest. 192 193 IMPORTANT: On some systems it is required that RESBUF is correctly 194 aligned for a 32 bits value. */ 195 ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A); 196 ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B); 197 ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C); 198 ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D); 199 200 return resbuf; 127 md5_process_block (ctx->buffer, size * 4, ctx); 128 129 return md5_read_ctx (ctx, resbuf); 201 130 } 202 131 … … 204 133 resulting message digest number will be written into the 16 bytes 205 134 beginning at RESBLOCK. */ 206 int md5_stream(FILE *stream, void *resblock) 207 { 208 /* Important: BLOCKSIZE must be a multiple of 64. */ 209 static const int BLOCKSIZE = 4096; 135 int 136 md5_stream (FILE *stream, void *resblock) 137 { 210 138 struct md5_ctx ctx; 211 139 char buffer[BLOCKSIZE + 72]; … … 213 141 214 142 /* Initialize the computation context. */ 215 md5_init_ctx (&ctx);143 md5_init_ctx (&ctx); 216 144 217 145 /* Iterate over full file contents. */ 218 while (1) { 219 /* We read the file in blocks of BLOCKSIZE bytes. One call of the 220 computation function processes the whole buffer so that with the 221 next round of the loop another block can be read. */ 222 size_t n; 223 sum = 0; 224 225 /* Read block. Take care for partial reads. */ 226 do { 227 n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); 228 229 sum += n; 146 while (1) 147 { 148 /* We read the file in blocks of BLOCKSIZE bytes. One call of the 149 computation function processes the whole buffer so that with the 150 next round of the loop another block can be read. */ 151 size_t n; 152 sum = 0; 153 154 /* Read block. Take care for partial reads. */ 155 while (1) 156 { 157 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); 158 159 sum += n; 160 161 if (sum == BLOCKSIZE) 162 break; 163 164 if (n == 0) 165 { 166 /* Check for the error flag IFF N == 0, so that we don't 167 exit the loop after a partial read due to e.g., EAGAIN 168 or EWOULDBLOCK. */ 169 if (ferror (stream)) 170 return 1; 171 goto process_partial_block; 172 } 173 174 /* We've read at least one byte, so ignore errors. But always 175 check for EOF, since feof may be true even though N > 0. 176 Otherwise, we could end up calling fread after EOF. */ 177 if (feof (stream)) 178 goto process_partial_block; 179 } 180 181 /* Process buffer with BLOCKSIZE bytes. Note that 182 BLOCKSIZE % 64 == 0 183 */ 184 md5_process_block (buffer, BLOCKSIZE, &ctx); 230 185 } 231 while (sum < BLOCKSIZE && n != 0); 232 if (n == 0 && ferror(stream)) 233 return 1; 234 235 /* If end of file is reached, end the loop. */ 236 if (n == 0) 237 break; 238 239 /* Process buffer with BLOCKSIZE bytes. Note that 240 BLOCKSIZE % 64 == 0 241 */ 242 md5_process_block(buffer, BLOCKSIZE, &ctx); 243 } 244 245 /* Add the last bytes if necessary. */ 186 187 process_partial_block: 188 189 /* Process any remaining bytes. */ 246 190 if (sum > 0) 247 md5_process_bytes (buffer, sum, &ctx);191 md5_process_bytes (buffer, sum, &ctx); 248 192 249 193 /* Construct result in desired memory. */ 250 md5_finish_ctx (&ctx, resblock);194 md5_finish_ctx (&ctx, resblock); 251 195 return 0; 252 196 } … … 256 200 output yields to the wanted ASCII representation of the message 257 201 digest. */ 258 void *md5_buffer(const char *buffer, size_t len, void *resblock) 202 void * 203 md5_buffer (const char *buffer, size_t len, void *resblock) 259 204 { 260 205 struct md5_ctx ctx; 261 206 262 207 /* Initialize the computation context. */ 263 md5_init_ctx (&ctx);208 md5_init_ctx (&ctx); 264 209 265 210 /* Process whole buffer but last len % 64 bytes. */ 266 md5_process_bytes (buffer, len, &ctx);211 md5_process_bytes (buffer, len, &ctx); 267 212 268 213 /* Put result in desired memory area. */ 269 return md5_finish_ctx(&ctx, resblock); 270 } 271 272 static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx) 214 return md5_finish_ctx (&ctx, resblock); 215 } 216 217 218 void 219 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) 273 220 { 274 221 /* When we already have some bits in our internal buffer concatenate 275 222 both inputs first. */ 276 if (ctx->buflen != 0) { 277 size_t left_over = ctx->buflen; 278 size_t add = 128 - left_over > len ? len : 128 - left_over; 279 280 memcpy(&ctx->buffer[left_over], buffer, add); 281 ctx->buflen += add; 282 283 if (left_over + add > 64) { 284 md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx); 285 /* The regions in the following copy operation cannot overlap. */ 286 memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63], 287 (left_over + add) & 63); 288 ctx->buflen = (left_over + add) & 63; 223 if (ctx->buflen != 0) 224 { 225 size_t left_over = ctx->buflen; 226 size_t add = 128 - left_over > len ? len : 128 - left_over; 227 228 memcpy (&((char *) ctx->buffer)[left_over], buffer, add); 229 ctx->buflen += add; 230 231 if (ctx->buflen > 64) 232 { 233 md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); 234 235 ctx->buflen &= 63; 236 /* The regions in the following copy operation cannot overlap. */ 237 memcpy (ctx->buffer, 238 &((char *) ctx->buffer)[(left_over + add) & ~63], 239 ctx->buflen); 240 } 241 242 buffer = (const char *) buffer + add; 243 len -= add; 289 244 } 290 245 291 buffer = (const char *) buffer + add;292 len -= add;293 }294 295 246 /* Process available complete blocks. */ 296 if (len > 64) { 297 md5_process_block(buffer, len & ~63, ctx); 298 buffer = (const char *) buffer + (len & ~63); 299 len &= 63; 300 } 247 if (len >= 64) 248 { 249 #if !_STRING_ARCH_unaligned 250 # define alignof(type) offsetof (struct { char c; type x; }, x) 251 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) 252 if (UNALIGNED_P (buffer)) 253 while (len > 64) 254 { 255 md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); 256 buffer = (const char *) buffer + 64; 257 len -= 64; 258 } 259 else 260 #endif 261 { 262 md5_process_block (buffer, len & ~63, ctx); 263 buffer = (const char *) buffer + (len & ~63); 264 len &= 63; 265 } 266 } 301 267 302 268 /* Move remaining bytes in internal buffer. */ 303 if (len > 0) { 304 memcpy(ctx->buffer, buffer, len); 305 ctx->buflen = len; 306 } 307 } 269 if (len > 0) 270 { 271 size_t left_over = ctx->buflen; 272 273 memcpy (&((char *) ctx->buffer)[left_over], buffer, len); 274 left_over += len; 275 if (left_over >= 64) 276 { 277 md5_process_block (ctx->buffer, 64, ctx); 278 left_over -= 64; 279 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 280 } 281 ctx->buflen = left_over; 282 } 283 } 284 308 285 309 286 /* These are the four functions used in the four steps of the MD5 algorithm … … 318 295 /* Process LEN bytes of BUFFER, accumulating context into CTX. 319 296 It is assumed that LEN % 64 == 0. */ 320 static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx) 321 { 322 md5_uint32 correct_words[16]; 323 const md5_uint32 *words = buffer; 324 size_t nwords = len / sizeof(md5_uint32); 325 const md5_uint32 *endp = words + nwords; 326 #if MD5SUM_SIZE_VS_SPEED > 0 327 static const md5_uint32 C_array[] = { 328 /* round 1 */ 329 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 330 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 331 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 332 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 333 /* round 2 */ 334 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 335 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 336 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 337 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 338 /* round 3 */ 339 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 340 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 341 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, 342 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 343 /* round 4 */ 344 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 345 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 346 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 347 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 348 }; 349 350 static const char P_array[] = { 351 #if MD5SUM_SIZE_VS_SPEED > 1 352 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */ 353 #endif 354 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */ 355 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */ 356 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */ 357 }; 358 359 #if MD5SUM_SIZE_VS_SPEED > 1 360 static const char S_array[] = { 361 7, 12, 17, 22, 362 5, 9, 14, 20, 363 4, 11, 16, 23, 364 6, 10, 15, 21 365 }; 366 #endif 367 #endif 368 369 md5_uint32 A = ctx->A; 370 md5_uint32 B = ctx->B; 371 md5_uint32 C = ctx->C; 372 md5_uint32 D = ctx->D; 297 298 void 299 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) 300 { 301 uint32_t correct_words[16]; 302 const uint32_t *words = buffer; 303 size_t nwords = len / sizeof (uint32_t); 304 const uint32_t *endp = words + nwords; 305 uint32_t A = ctx->A; 306 uint32_t B = ctx->B; 307 uint32_t C = ctx->C; 308 uint32_t D = ctx->D; 373 309 374 310 /* First increment the byte count. RFC 1321 specifies the possible … … 381 317 /* Process all bytes in the buffer with 64 bytes in each round of 382 318 the loop. */ 383 while (words < endp) { 384 md5_uint32 *cwp = correct_words; 385 md5_uint32 A_save = A; 386 md5_uint32 B_save = B; 387 md5_uint32 C_save = C; 388 md5_uint32 D_save = D; 389 390 #if MD5SUM_SIZE_VS_SPEED > 1 391 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) 392 393 const md5_uint32 *pc; 394 const char *pp; 395 const char *ps; 396 int i; 397 md5_uint32 temp; 398 399 for ( i=0 ; i < 16 ; i++ ) { 400 cwp[i] = SWAP(words[i]); 401 } 402 words += 16; 403 404 #if MD5SUM_SIZE_VS_SPEED > 2 405 pc = C_array; pp = P_array; ps = S_array - 4; 406 407 for ( i = 0 ; i < 64 ; i++ ) { 408 if ((i&0x0f) == 0) ps += 4; 409 temp = A; 410 switch (i>>4) { 411 case 0: 412 temp += FF(B,C,D); 413 break; 414 case 1: 415 temp += FG(B,C,D); 416 break; 417 case 2: 418 temp += FH(B,C,D); 419 break; 420 case 3: 421 temp += FI(B,C,D); 422 } 423 temp += cwp[(int)(*pp++)] + *pc++; 424 temp = CYCLIC (temp, ps[i&3]); 425 temp += B; 426 A = D; D = C; C = B; B = temp; 427 } 428 #else 429 pc = C_array; pp = P_array; ps = S_array; 430 431 for ( i = 0 ; i < 16 ; i++ ) { 432 temp = A + FF(B,C,D) + cwp[(int)(*pp++)] + *pc++; 433 temp = CYCLIC (temp, ps[i&3]); 434 temp += B; 435 A = D; D = C; C = B; B = temp; 436 } 437 438 ps += 4; 439 for ( i = 0 ; i < 16 ; i++ ) { 440 temp = A + FG(B,C,D) + cwp[(int)(*pp++)] + *pc++; 441 temp = CYCLIC (temp, ps[i&3]); 442 temp += B; 443 A = D; D = C; C = B; B = temp; 444 } 445 ps += 4; 446 for ( i = 0 ; i < 16 ; i++ ) { 447 temp = A + FH(B,C,D) + cwp[(int)(*pp++)] + *pc++; 448 temp = CYCLIC (temp, ps[i&3]); 449 temp += B; 450 A = D; D = C; C = B; B = temp; 451 } 452 ps += 4; 453 for ( i = 0 ; i < 16 ; i++ ) { 454 temp = A + FI(B,C,D) + cwp[(int)(*pp++)] + *pc++; 455 temp = CYCLIC (temp, ps[i&3]); 456 temp += B; 457 A = D; D = C; C = B; B = temp; 458 } 459 460 #endif 461 #else 462 /* First round: using the given function, the context and a constant 463 the next context is computed. Because the algorithms processing 464 unit is a 32-bit word and it is determined to work on words in 465 little endian byte order we perhaps have to change the byte order 466 before the computation. To reduce the work for the next steps 467 we store the swapped words in the array CORRECT_WORDS. */ 319 while (words < endp) 320 { 321 uint32_t *cwp = correct_words; 322 uint32_t A_save = A; 323 uint32_t B_save = B; 324 uint32_t C_save = C; 325 uint32_t D_save = D; 326 327 /* First round: using the given function, the context and a constant 328 the next context is computed. Because the algorithms processing 329 unit is a 32-bit word and it is determined to work on words in 330 little endian byte order we perhaps have to change the byte order 331 before the computation. To reduce the work for the next steps 332 we store the swapped words in the array CORRECT_WORDS. */ 468 333 469 334 #define OP(a, b, c, d, s, T) \ … … 477 342 while (0) 478 343 479 /* It is unfortunate that C does not provide an operator for 480 cyclic rotation. Hope the C compiler is smart enough. */ 481 /* gcc 2.95.4 seems to be --aaronl */ 344 /* It is unfortunate that C does not provide an operator for 345 cyclic rotation. Hope the C compiler is smart enough. */ 482 346 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) 483 347 484 /* Before we start, one word to the strange constants. 485 They are defined in RFC 1321 as 486 487 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 488 */ 489 490 #if MD5SUM_SIZE_VS_SPEED == 1 491 const md5_uint32 *pc; 492 const char *pp; 493 int i; 494 #endif 495 496 /* Round 1. */ 497 #if MD5SUM_SIZE_VS_SPEED == 1 498 pc = C_array; 499 for ( i=0 ; i < 4 ; i++ ) { 500 OP(A, B, C, D, 7, *pc++); 501 OP(D, A, B, C, 12, *pc++); 502 OP(C, D, A, B, 17, *pc++); 503 OP(B, C, D, A, 22, *pc++); 504 } 505 #else 506 OP(A, B, C, D, 7, 0xd76aa478); 507 OP(D, A, B, C, 12, 0xe8c7b756); 508 OP(C, D, A, B, 17, 0x242070db); 509 OP(B, C, D, A, 22, 0xc1bdceee); 510 OP(A, B, C, D, 7, 0xf57c0faf); 511 OP(D, A, B, C, 12, 0x4787c62a); 512 OP(C, D, A, B, 17, 0xa8304613); 513 OP(B, C, D, A, 22, 0xfd469501); 514 OP(A, B, C, D, 7, 0x698098d8); 515 OP(D, A, B, C, 12, 0x8b44f7af); 516 OP(C, D, A, B, 17, 0xffff5bb1); 517 OP(B, C, D, A, 22, 0x895cd7be); 518 OP(A, B, C, D, 7, 0x6b901122); 519 OP(D, A, B, C, 12, 0xfd987193); 520 OP(C, D, A, B, 17, 0xa679438e); 521 OP(B, C, D, A, 22, 0x49b40821); 522 #endif 523 524 /* For the second to fourth round we have the possibly swapped words 525 in CORRECT_WORDS. Redefine the macro to take an additional first 526 argument specifying the function to use. */ 348 /* Before we start, one word to the strange constants. 349 They are defined in RFC 1321 as 350 351 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 352 353 Here is an equivalent invocation using Perl: 354 355 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' 356 */ 357 358 /* Round 1. */ 359 OP (A, B, C, D, 7, 0xd76aa478); 360 OP (D, A, B, C, 12, 0xe8c7b756); 361 OP (C, D, A, B, 17, 0x242070db); 362 OP (B, C, D, A, 22, 0xc1bdceee); 363 OP (A, B, C, D, 7, 0xf57c0faf); 364 OP (D, A, B, C, 12, 0x4787c62a); 365 OP (C, D, A, B, 17, 0xa8304613); 366 OP (B, C, D, A, 22, 0xfd469501); 367 OP (A, B, C, D, 7, 0x698098d8); 368 OP (D, A, B, C, 12, 0x8b44f7af); 369 OP (C, D, A, B, 17, 0xffff5bb1); 370 OP (B, C, D, A, 22, 0x895cd7be); 371 OP (A, B, C, D, 7, 0x6b901122); 372 OP (D, A, B, C, 12, 0xfd987193); 373 OP (C, D, A, B, 17, 0xa679438e); 374 OP (B, C, D, A, 22, 0x49b40821); 375 376 /* For the second to fourth round we have the possibly swapped words 377 in CORRECT_WORDS. Redefine the macro to take an additional first 378 argument specifying the function to use. */ 527 379 #undef OP 528 380 #define OP(f, a, b, c, d, k, s, T) \ 529 do \381 do \ 530 382 { \ 531 383 a += f (b, c, d) + correct_words[k] + T; \ … … 535 387 while (0) 536 388 537 /* Round 2. */ 538 #if MD5SUM_SIZE_VS_SPEED == 1 539 pp = P_array; 540 for ( i=0 ; i < 4 ; i++ ) { 541 OP(FG, A, B, C, D, (int)(*pp++), 5, *pc++); 542 OP(FG, D, A, B, C, (int)(*pp++), 9, *pc++); 543 OP(FG, C, D, A, B, (int)(*pp++), 14, *pc++); 544 OP(FG, B, C, D, A, (int)(*pp++), 20, *pc++); 389 /* Round 2. */ 390 OP (FG, A, B, C, D, 1, 5, 0xf61e2562); 391 OP (FG, D, A, B, C, 6, 9, 0xc040b340); 392 OP (FG, C, D, A, B, 11, 14, 0x265e5a51); 393 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); 394 OP (FG, A, B, C, D, 5, 5, 0xd62f105d); 395 OP (FG, D, A, B, C, 10, 9, 0x02441453); 396 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); 397 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); 398 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); 399 OP (FG, D, A, B, C, 14, 9, 0xc33707d6); 400 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); 401 OP (FG, B, C, D, A, 8, 20, 0x455a14ed); 402 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); 403 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); 404 OP (FG, C, D, A, B, 7, 14, 0x676f02d9); 405 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); 406 407 /* Round 3. */ 408 OP (FH, A, B, C, D, 5, 4, 0xfffa3942); 409 OP (FH, D, A, B, C, 8, 11, 0x8771f681); 410 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); 411 OP (FH, B, C, D, A, 14, 23, 0xfde5380c); 412 OP (FH, A, B, C, D, 1, 4, 0xa4beea44); 413 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); 414 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); 415 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); 416 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); 417 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); 418 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); 419 OP (FH, B, C, D, A, 6, 23, 0x04881d05); 420 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); 421 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); 422 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); 423 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); 424 425 /* Round 4. */ 426 OP (FI, A, B, C, D, 0, 6, 0xf4292244); 427 OP (FI, D, A, B, C, 7, 10, 0x432aff97); 428 OP (FI, C, D, A, B, 14, 15, 0xab9423a7); 429 OP (FI, B, C, D, A, 5, 21, 0xfc93a039); 430 OP (FI, A, B, C, D, 12, 6, 0x655b59c3); 431 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); 432 OP (FI, C, D, A, B, 10, 15, 0xffeff47d); 433 OP (FI, B, C, D, A, 1, 21, 0x85845dd1); 434 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); 435 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); 436 OP (FI, C, D, A, B, 6, 15, 0xa3014314); 437 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); 438 OP (FI, A, B, C, D, 4, 6, 0xf7537e82); 439 OP (FI, D, A, B, C, 11, 10, 0xbd3af235); 440 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); 441 OP (FI, B, C, D, A, 9, 21, 0xeb86d391); 442 443 /* Add the starting values of the context. */ 444 A += A_save; 445 B += B_save; 446 C += C_save; 447 D += D_save; 545 448 } 546 #else547 OP(FG, A, B, C, D, 1, 5, 0xf61e2562);548 OP(FG, D, A, B, C, 6, 9, 0xc040b340);549 OP(FG, C, D, A, B, 11, 14, 0x265e5a51);550 OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);551 OP(FG, A, B, C, D, 5, 5, 0xd62f105d);552 OP(FG, D, A, B, C, 10, 9, 0x02441453);553 OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);554 OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);555 OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);556 OP(FG, D, A, B, C, 14, 9, 0xc33707d6);557 OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);558 OP(FG, B, C, D, A, 8, 20, 0x455a14ed);559 OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);560 OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);561 OP(FG, C, D, A, B, 7, 14, 0x676f02d9);562 OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);563 #endif564 565 /* Round 3. */566 #if MD5SUM_SIZE_VS_SPEED == 1567 for ( i=0 ; i < 4 ; i++ ) {568 OP(FH, A, B, C, D, (int)(*pp++), 4, *pc++);569 OP(FH, D, A, B, C, (int)(*pp++), 11, *pc++);570 OP(FH, C, D, A, B, (int)(*pp++), 16, *pc++);571 OP(FH, B, C, D, A, (int)(*pp++), 23, *pc++);572 }573 #else574 OP(FH, A, B, C, D, 5, 4, 0xfffa3942);575 OP(FH, D, A, B, C, 8, 11, 0x8771f681);576 OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);577 OP(FH, B, C, D, A, 14, 23, 0xfde5380c);578 OP(FH, A, B, C, D, 1, 4, 0xa4beea44);579 OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);580 OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);581 OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);582 OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);583 OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);584 OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);585 OP(FH, B, C, D, A, 6, 23, 0x04881d05);586 OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);587 OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);588 OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);589 OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);590 #endif591 592 /* Round 4. */593 #if MD5SUM_SIZE_VS_SPEED == 1594 for ( i=0 ; i < 4 ; i++ ) {595 OP(FI, A, B, C, D, (int)(*pp++), 6, *pc++);596 OP(FI, D, A, B, C, (int)(*pp++), 10, *pc++);597 OP(FI, C, D, A, B, (int)(*pp++), 15, *pc++);598 OP(FI, B, C, D, A, (int)(*pp++), 21, *pc++);599 }600 #else601 OP(FI, A, B, C, D, 0, 6, 0xf4292244);602 OP(FI, D, A, B, C, 7, 10, 0x432aff97);603 OP(FI, C, D, A, B, 14, 15, 0xab9423a7);604 OP(FI, B, C, D, A, 5, 21, 0xfc93a039);605 OP(FI, A, B, C, D, 12, 6, 0x655b59c3);606 OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);607 OP(FI, C, D, A, B, 10, 15, 0xffeff47d);608 OP(FI, B, C, D, A, 1, 21, 0x85845dd1);609 OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);610 OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);611 OP(FI, C, D, A, B, 6, 15, 0xa3014314);612 OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);613 OP(FI, A, B, C, D, 4, 6, 0xf7537e82);614 OP(FI, D, A, B, C, 11, 10, 0xbd3af235);615 OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);616 OP(FI, B, C, D, A, 9, 21, 0xeb86d391);617 #endif618 #endif619 620 /* Add the starting values of the context. */621 A += A_save;622 B += B_save;623 C += C_save;624 D += D_save;625 }626 449 627 450 /* Put checksum in context given as argument. */ … … 631 454 ctx->D = D; 632 455 } 633 634 //----------------------------------------------------------------------------635 //--------end of md5.c636 //----------------------------------------------------------------------------637 638 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')639 #define ISXDIGIT(c) (isxdigit (c))640 641 /* The minimum length of a valid digest line in a file produced642 by `md5sum FILE' and read by `md5sum -c'. This length does643 not include any newline character at the end of a line. */644 static const int MIN_DIGEST_LINE_LENGTH = 35; /* 32 - message digest length645 2 - blank and binary indicator646 1 - minimum filename length */647 648 static inline int hex_digits(unsigned char const *s)649 {650 while (*s) {651 if (!ISXDIGIT(*s))652 return 0;653 ++s;654 }655 return 1;656 }657 658 -
trunk/src/target/opkg/libopkg/md5.h
r4029 r4206 1 /* md5.h - Compute MD5 checksum of files or strings according to the 2 * definition of MD5 in RFC 1321 from April 1992. 3 * Copyright (C) 1995-1999 Free Software Foundation, Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2, or (at your option) 8 * any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software Foundation, 17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1 /* Declaration of functions and data types used for MD5 sum computing 2 library functions. 3 Copyright (C) 1995-1997,1999,2000,2001,2004,2005,2006,2008 4 Free Software Foundation, Inc. 5 This file is part of the GNU C Library. 6 7 This program is free software; you can redistribute it and/or modify it 8 under the terms of the GNU General Public License as published by the 9 Free Software Foundation; either version 2, or (at your option) any 10 later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software Foundation, 19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 #ifndef _MD5_H 22 #define _MD5_H 1 23 24 #include <stdio.h> 25 #include <stdint.h> 26 27 #define MD5_DIGEST_SIZE 16 28 #define MD5_BLOCK_SIZE 64 29 30 #ifndef __GNUC_PREREQ 31 # if defined __GNUC__ && defined __GNUC_MINOR__ 32 # define __GNUC_PREREQ(maj, min) \ 33 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 34 # else 35 # define __GNUC_PREREQ(maj, min) 0 36 # endif 37 #endif 38 39 #ifndef __THROW 40 # if defined __cplusplus && __GNUC_PREREQ (2,8) 41 # define __THROW throw () 42 # else 43 # define __THROW 44 # endif 45 #endif 46 47 #ifndef _LIBC 48 # define __md5_buffer md5_buffer 49 # define __md5_finish_ctx md5_finish_ctx 50 # define __md5_init_ctx md5_init_ctx 51 # define __md5_process_block md5_process_block 52 # define __md5_process_bytes md5_process_bytes 53 # define __md5_read_ctx md5_read_ctx 54 # define __md5_stream md5_stream 55 #endif 56 57 /* Structure to save state of computation between the single steps. */ 58 struct md5_ctx 59 { 60 uint32_t A; 61 uint32_t B; 62 uint32_t C; 63 uint32_t D; 64 65 uint32_t total[2]; 66 uint32_t buflen; 67 uint32_t buffer[32]; 68 }; 69 70 /* 71 * The following three functions are build up the low level used in 72 * the functions `md5_stream' and `md5_buffer'. 18 73 */ 19 74 20 #ifndef MD5_H 21 #define MD5_H 75 /* Initialize structure containing state of computation. 76 (RFC 1321, 3.3: Step 3) */ 77 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW; 78 79 /* Starting with the result of former calls of this function (or the 80 initialization function update the context for the next LEN bytes 81 starting at BUFFER. 82 It is necessary that LEN is a multiple of 64!!! */ 83 extern void __md5_process_block (const void *buffer, size_t len, 84 struct md5_ctx *ctx) __THROW; 85 86 /* Starting with the result of former calls of this function (or the 87 initialization function update the context for the next LEN bytes 88 starting at BUFFER. 89 It is NOT required that LEN is a multiple of 64. */ 90 extern void __md5_process_bytes (const void *buffer, size_t len, 91 struct md5_ctx *ctx) __THROW; 92 93 /* Process the remaining bytes in the buffer and put result from CTX 94 in first 16 bytes following RESBUF. The result is always in little 95 endian byte order, so that a byte-wise output yields to the wanted 96 ASCII representation of the message digest. */ 97 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; 98 99 100 /* Put result from CTX in first 16 bytes following RESBUF. The result is 101 always in little endian byte order, so that a byte-wise output yields 102 to the wanted ASCII representation of the message digest. */ 103 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; 104 22 105 23 106 /* Compute MD5 message digest for bytes read from STREAM. The 24 107 resulting message digest number will be written into the 16 bytes 25 108 beginning at RESBLOCK. */ 26 int md5_stream(FILE *stream, void *resblock);109 extern int __md5_stream (FILE *stream, void *resblock) __THROW; 27 110 28 111 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The … … 30 113 output yields to the wanted ASCII representation of the message 31 114 digest. */ 32 void *md5_buffer(const char *buffer, size_t len, void *resblock); 115 extern void *__md5_buffer (const char *buffer, size_t len, 116 void *resblock) __THROW; 33 117 34 #endif 35 118 #endif /* md5.h */
Note: See TracChangeset
for help on using the changeset viewer.
