wok-current annotate syslinux/stuff/extra/md5sum.c @ rev 12242

syslinux: 4.0 string
author Christophe Lincoln <pankso@slitaz.org>
date Mon Apr 09 17:56:22 2012 +0200 (2012-04-09)
parents 880772e418b7
children af17de69f535
rev   line source
pascal@12210 1 /*
pascal@12215 2 * Based on busybox code.
pascal@12210 3 *
pascal@12215 4 * Compute MD5 checksum of strings according to the
pascal@12215 5 * definition of MD5 in RFC 1321 from April 1992.
pascal@12210 6 *
pascal@12215 7 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
pascal@12215 8 *
pascal@12215 9 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
pascal@12215 10 * Copyright (C) 2001 Manuel Novoa III
pascal@12215 11 * Copyright (C) 2003 Glenn L. McGrath
pascal@12215 12 * Copyright (C) 2003 Erik Andersen
pascal@12210 13 * Copyright (C) 2010 Denys Vlasenko
pascal@12215 14 * Copyright (C) 2012 Pascal Bellard
pascal@12210 15 *
pascal@12215 16 * Licensed under GPLv2 or later
pascal@12210 17 */
pascal@12210 18
pascal@12210 19 #include <stdio.h>
pascal@12210 20 #include <stdlib.h>
pascal@12210 21 #include <string.h>
pascal@12210 22 #include <unistd.h>
pascal@12210 23 #include <fcntl.h>
pascal@12210 24 #include <console.h>
pascal@12210 25 #include <com32.h>
pascal@12210 26
pascal@12210 27 #define ALIGN1
pascal@12215 28
pascal@12215 29 static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
pascal@12215 30 static uint64_t total64; /* must be directly before hash[] */
pascal@12215 31 static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
pascal@12210 32
pascal@12210 33 /* Emit a string of hex representation of bytes */
pascal@12215 34 static char* bin2hex(char *p)
pascal@12210 35 {
pascal@12215 36 static const char bb_hexdigits_upcase[] ALIGN1 = "0123456789abcdef";
pascal@12215 37 int count = 16;
pascal@12215 38 const char *cp = (const char *) hash;
pascal@12210 39 while (count) {
pascal@12210 40 unsigned char c = *cp++;
pascal@12210 41 /* put lowercase hex digits */
pascal@12215 42 *p++ = bb_hexdigits_upcase[c >> 4];
pascal@12215 43 *p++ = bb_hexdigits_upcase[c & 0xf];
pascal@12210 44 count--;
pascal@12210 45 }
pascal@12210 46 return p;
pascal@12210 47 }
pascal@12210 48
pascal@12210 49 //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
pascal@12210 50 static uint32_t rotl32(uint32_t x, unsigned n)
pascal@12210 51 {
pascal@12210 52 return (x << n) | (x >> (32 - n));
pascal@12210 53 }
pascal@12210 54
pascal@12215 55 static void md5_process_block64(void);
pascal@12210 56
pascal@12210 57 /* Feed data through a temporary buffer.
pascal@12210 58 * The internal buffer remembers previous data until it has 64
pascal@12210 59 * bytes worth to pass on.
pascal@12210 60 */
pascal@12215 61 static void common64_hash(const void *buffer, size_t len)
pascal@12210 62 {
pascal@12215 63 unsigned bufpos = total64 & 63;
pascal@12210 64
pascal@12215 65 total64 += len;
pascal@12210 66
pascal@12210 67 while (1) {
pascal@12210 68 unsigned remaining = 64 - bufpos;
pascal@12210 69 if (remaining > len)
pascal@12210 70 remaining = len;
pascal@12210 71 /* Copy data into aligned buffer */
pascal@12215 72 memcpy(wbuffer + bufpos, buffer, remaining);
pascal@12210 73 len -= remaining;
pascal@12210 74 buffer = (const char *)buffer + remaining;
pascal@12210 75 bufpos += remaining;
pascal@12210 76 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
pascal@12210 77 bufpos -= 64;
pascal@12210 78 if (bufpos != 0)
pascal@12210 79 break;
pascal@12210 80 /* Buffer is filled up, process it */
pascal@12215 81 md5_process_block64();
pascal@12210 82 /*bufpos = 0; - already is */
pascal@12210 83 }
pascal@12210 84 }
pascal@12210 85
pascal@12210 86 /* Process the remaining bytes in the buffer */
pascal@12215 87 static void common64_end(void)
pascal@12210 88 {
pascal@12215 89 unsigned bufpos = total64 & 63;
pascal@12210 90 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
pascal@12215 91 wbuffer[bufpos++] = 0x80;
pascal@12210 92
pascal@12210 93 /* This loop iterates either once or twice, no more, no less */
pascal@12210 94 while (1) {
pascal@12210 95 unsigned remaining = 64 - bufpos;
pascal@12215 96 memset(wbuffer + bufpos, 0, remaining);
pascal@12210 97 /* Do we have enough space for the length count? */
pascal@12210 98 if (remaining >= 8) {
pascal@12210 99 /* Store the 64-bit counter of bits in the buffer */
pascal@12215 100 uint64_t t = total64 << 3;
pascal@12210 101 /* wbuffer is suitably aligned for this */
pascal@12215 102 *(uint64_t *) (&wbuffer[64 - 8]) = t;
pascal@12210 103 }
pascal@12215 104 md5_process_block64();
pascal@12210 105 if (remaining >= 8)
pascal@12210 106 break;
pascal@12210 107 bufpos = 0;
pascal@12210 108 }
pascal@12210 109 }
pascal@12210 110
pascal@12210 111 /* These are the four functions used in the four steps of the MD5 algorithm
pascal@12210 112 * and defined in the RFC 1321. The first function is a little bit optimized
pascal@12210 113 * (as found in Colin Plumbs public domain implementation).
pascal@12210 114 * #define FF(b, c, d) ((b & c) | (~b & d))
pascal@12210 115 */
pascal@12210 116 #undef FF
pascal@12210 117 #undef FG
pascal@12210 118 #undef FH
pascal@12210 119 #undef FI
pascal@12210 120 #define FF(b, c, d) (d ^ (b & (c ^ d)))
pascal@12210 121 #define FG(b, c, d) FF(d, b, c)
pascal@12210 122 #define FH(b, c, d) (b ^ c ^ d)
pascal@12210 123 #define FI(b, c, d) (c ^ (b | ~d))
pascal@12210 124
pascal@12210 125 /* Hash a single block, 64 bytes long and 4-byte aligned */
pascal@12215 126 static void md5_process_block64(void)
pascal@12210 127 {
pascal@12210 128 /* Before we start, one word to the strange constants.
pascal@12210 129 They are defined in RFC 1321 as
pascal@12210 130 T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
pascal@12210 131 */
pascal@12210 132 static const uint32_t C_array[] = {
pascal@12210 133 /* round 1 */
pascal@12210 134 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
pascal@12210 135 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
pascal@12210 136 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
pascal@12210 137 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
pascal@12210 138 /* round 2 */
pascal@12210 139 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
pascal@12210 140 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
pascal@12210 141 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
pascal@12210 142 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
pascal@12210 143 /* round 3 */
pascal@12210 144 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
pascal@12210 145 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
pascal@12210 146 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
pascal@12210 147 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
pascal@12210 148 /* round 4 */
pascal@12210 149 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
pascal@12210 150 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
pascal@12210 151 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
pascal@12210 152 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
pascal@12210 153 };
pascal@12210 154 static const char P_array[] ALIGN1 = {
pascal@12210 155 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
pascal@12210 156 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
pascal@12210 157 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
pascal@12210 158 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
pascal@12210 159 };
pascal@12215 160 uint32_t *words = (void*) wbuffer;
pascal@12215 161 uint32_t A = hash[0];
pascal@12215 162 uint32_t B = hash[1];
pascal@12215 163 uint32_t C = hash[2];
pascal@12215 164 uint32_t D = hash[3];
pascal@12210 165
pascal@12210 166 static const char S_array[] ALIGN1 = {
pascal@12210 167 7, 12, 17, 22,
pascal@12210 168 5, 9, 14, 20,
pascal@12210 169 4, 11, 16, 23,
pascal@12210 170 6, 10, 15, 21
pascal@12210 171 };
pascal@12210 172 const uint32_t *pc;
pascal@12210 173 const char *pp;
pascal@12210 174 const char *ps;
pascal@12210 175 int i;
pascal@12210 176 uint32_t temp;
pascal@12210 177
pascal@12210 178
pascal@12210 179 pc = C_array;
pascal@12210 180 pp = P_array;
pascal@12210 181 ps = S_array - 4;
pascal@12210 182
pascal@12210 183 for (i = 0; i < 64; i++) {
pascal@12210 184 if ((i & 0x0f) == 0)
pascal@12210 185 ps += 4;
pascal@12210 186 temp = A;
pascal@12210 187 switch (i >> 4) {
pascal@12210 188 case 0:
pascal@12210 189 temp += FF(B, C, D);
pascal@12210 190 break;
pascal@12210 191 case 1:
pascal@12210 192 temp += FG(B, C, D);
pascal@12210 193 break;
pascal@12210 194 case 2:
pascal@12210 195 temp += FH(B, C, D);
pascal@12210 196 break;
pascal@12210 197 case 3:
pascal@12210 198 temp += FI(B, C, D);
pascal@12210 199 }
pascal@12210 200 temp += words[(int) (*pp++)] + *pc++;
pascal@12210 201 temp = rotl32(temp, ps[i & 3]);
pascal@12210 202 temp += B;
pascal@12210 203 A = D;
pascal@12210 204 D = C;
pascal@12210 205 C = B;
pascal@12210 206 B = temp;
pascal@12210 207 }
pascal@12210 208 /* Add checksum to the starting values */
pascal@12215 209 hash[0] += A;
pascal@12215 210 hash[1] += B;
pascal@12215 211 hash[2] += C;
pascal@12215 212 hash[3] += D;
pascal@12210 213
pascal@12210 214 }
pascal@12210 215 #undef FF
pascal@12210 216 #undef FG
pascal@12210 217 #undef FH
pascal@12210 218 #undef FI
pascal@12210 219
pascal@12210 220 /* Initialize structure containing state of computation.
pascal@12210 221 * (RFC 1321, 3.3: Step 3)
pascal@12210 222 */
pascal@12215 223 static void md5_begin(void)
pascal@12210 224 {
pascal@12215 225 hash[0] = 0x67452301;
pascal@12215 226 hash[1] = 0xefcdab89;
pascal@12215 227 hash[2] = 0x98badcfe;
pascal@12215 228 hash[3] = 0x10325476;
pascal@12215 229 total64 = 0;
pascal@12210 230 }
pascal@12210 231
pascal@12210 232 /* Used also for sha1 and sha256 */
pascal@12215 233 #define md5_hash common64_hash
pascal@12210 234
pascal@12210 235 /* Process the remaining bytes in the buffer and put result from CTX
pascal@12210 236 * in first 16 bytes following RESBUF. The result is always in little
pascal@12210 237 * endian byte order, so that a byte-wise output yields to the wanted
pascal@12210 238 * ASCII representation of the message digest.
pascal@12210 239 */
pascal@12215 240 #define md5_end common64_end
pascal@12210 241
pascal@12210 242 /*
pascal@12210 243 * Copyright (C) 2003 Glenn L. McGrath
pascal@12210 244 * Copyright (C) 2003-2004 Erik Andersen
pascal@12210 245 *
pascal@12210 246 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
pascal@12210 247 */
pascal@12210 248
pascal@12215 249 static char *unrockridge(const char *name)
pascal@12212 250 {
pascal@12212 251 static char buffer[256];
pascal@12215 252 int i = 0, j = 8;
pascal@12215 253 while (*name && i < 255) {
pascal@12215 254 char c = *name++;
pascal@12215 255 //if (c == '\\') c = '/';
pascal@12215 256 if (c == '.') {
pascal@12215 257 for (j = i; --j >= 0 && buffer[j] != '/';)
pascal@12215 258 if (buffer[j] == '.') buffer[j] = '_';
pascal@12215 259 if (i - j > 9) i = j + 9;
pascal@12215 260 j = i + 4;
pascal@12212 261 }
pascal@12215 262 else if (c == '/') j = i + 9;
pascal@12212 263 else if (i >= j) continue;
pascal@12215 264 else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
pascal@12215 265 else if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) c = '_';
pascal@12215 266 buffer[i++] = c;
pascal@12212 267 }
pascal@12215 268 buffer[i] = 0;
pascal@12212 269 return buffer;
pascal@12212 270 }
pascal@12212 271
pascal@12210 272 static uint8_t *hash_file(const char *filename)
pascal@12210 273 {
pascal@12210 274 int src_fd, count;
pascal@12215 275 uint8_t in_buf[4096];
pascal@12215 276 static uint8_t hash_value[16*2+1];
pascal@12210 277
pascal@12210 278 src_fd = open(filename, O_RDONLY);
pascal@12210 279 if (src_fd < 0) {
pascal@12215 280 src_fd = open(unrockridge(filename), O_RDONLY);
pascal@12212 281 }
pascal@12212 282 if (src_fd < 0) {
pascal@12210 283 return NULL;
pascal@12210 284 }
pascal@12210 285
pascal@12215 286 md5_begin();
pascal@12210 287 while ((count = read(src_fd, in_buf, 4096)) > 0) {
pascal@12215 288 md5_hash(in_buf, count);
pascal@12210 289 }
pascal@12210 290
pascal@12210 291 close(src_fd);
pascal@12215 292
pascal@12215 293 if (count)
pascal@12215 294 return NULL;
pascal@12215 295
pascal@12215 296 md5_end();
pascal@12215 297 bin2hex((char *)hash_value);
pascal@12210 298
pascal@12210 299 return hash_value;
pascal@12210 300 }
pascal@12210 301
pascal@12210 302 int main(int argc, char **argv)
pascal@12210 303 {
pascal@12215 304 int files = 0, tested = 0, good = 0;
pascal@12215 305 static char clear_eol[] = " ";
pascal@12210 306
pascal@12210 307 (void) argc;
pascal@12210 308 /* -c implied */
pascal@12210 309 openconsole(&dev_rawcon_r, &dev_stdcon_w);
pascal@12210 310
pascal@12210 311 do {
pascal@12210 312 FILE *fp;
pascal@12215 313 char eol, *line, buffer[256];
pascal@12210 314 fp = fopen(*argv,"r");
pascal@12212 315 if (fp == NULL)
pascal@12215 316 fp = fopen(unrockridge(*argv),"r");
pascal@12210 317
pascal@12210 318 while ((line = fgets(buffer,256,fp)) != NULL) {
pascal@12210 319 uint8_t *hash_value;
pascal@12210 320 char *filename_ptr, *status;
pascal@12210 321 int len = strlen(line);
pascal@12210 322
pascal@12210 323 if (line[0] < '0')
pascal@12210 324 continue;
pascal@12210 325 if (line[len-1] == '\n')
pascal@12210 326 line[len-1] = 0;
pascal@12210 327 filename_ptr = strstr(line, " ");
pascal@12210 328 /* handle format for binary checksums */
pascal@12210 329 if (filename_ptr == NULL) {
pascal@12210 330 filename_ptr = strstr(line, " *");
pascal@12210 331 }
pascal@12210 332 if (filename_ptr == NULL) {
pascal@12210 333 continue;
pascal@12210 334 }
pascal@12210 335 *filename_ptr = '\0';
pascal@12210 336 *++filename_ptr = '/';
pascal@12212 337 if (filename_ptr[1] == '/')
pascal@12212 338 filename_ptr++;
pascal@12210 339
pascal@12215 340 files++;
pascal@12215 341 status = "NOT CHECKED";
pascal@12215 342 eol = '\n';
pascal@12211 343 hash_value = hash_file(filename_ptr);
pascal@12211 344 if (hash_value) {
pascal@12215 345 tested++;
pascal@12215 346 status = "BROKEN";
pascal@12215 347 if (!strcmp((char*)hash_value, line)) {
pascal@12215 348 good++;
pascal@12215 349 status = "OK";
pascal@12215 350 eol = ' ';
pascal@12210 351 }
pascal@12210 352 }
pascal@12215 353 printf("\r%s: %s%s%c", filename_ptr, status, clear_eol, eol);
pascal@12210 354 }
pascal@12210 355 fclose(fp);
pascal@12210 356 } while (*++argv);
pascal@12215 357 printf("\r%d files OK, %d broken, %d not checked.%s\n",
pascal@12215 358 good, tested - good, files - tested, clear_eol);
pascal@12215 359 }
pascal@12210 360