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