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