1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81#define FORCE_FLAGS
82#define FOR_md5sum
83#include "toys.h"
84
85#if CFG_TOYBOX_LIBCRYPTO
86#include <openssl/md5.h>
87#include <openssl/sha.h>
88#else
89typedef int SHA512_CTX;
90#endif
91
92GLOBALS(
93 int sawline;
94
95
96 unsigned state[5];
97 unsigned oldstate[5];
98 uint64_t count;
99 union {
100 char c[64];
101 unsigned i[16];
102 } buffer;
103)
104
105#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
106
107
108
109
110
111static uint32_t md5table[64] = {
112 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
113 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
114 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
115 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
116 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
117 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
118 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
119 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
120 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
121 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
122 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
123};
124
125static const uint8_t md5rot[64] = {
126 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
127 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
128 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
129 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
130};
131
132
133
134static void md5_transform(void)
135{
136 unsigned x[4], *b = TT.buffer.i;
137 int i;
138
139 memcpy(x, TT.state, sizeof(x));
140
141 for (i=0; i<64; i++) {
142 unsigned int in, temp, swap;
143 if (i<16) {
144 in = i;
145 temp = x[1];
146 temp = (temp & x[2]) | ((~temp) & x[3]);
147 } else if (i<32) {
148 in = (1+(5*i))&15;
149 temp = x[3];
150 temp = (x[1] & temp) | (x[2] & ~temp);
151 } else if (i<48) {
152 in = (3*i+5)&15;
153 temp = x[1] ^ x[2] ^ x[3];
154 } else {
155 in = (7*i)&15;
156 temp = x[2] ^ (x[1] | ~x[3]);
157 }
158 temp += x[0] + b[in] + md5table[i];
159 swap = x[3];
160 x[3] = x[2];
161 x[2] = x[1];
162 x[1] += rol(temp, md5rot[i]);
163 x[0] = swap;
164 }
165 for (i=0; i<4; i++) TT.state[i] += x[i];
166}
167
168
169
170static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
171
172static void sha1_transform(void)
173{
174 int i, j, k, count;
175 unsigned *block = TT.buffer.i;
176 unsigned *rot[5], *temp;
177
178
179 for (i=0; i<5; i++) {
180 TT.oldstate[i] = TT.state[i];
181 rot[i] = TT.state + i;
182 }
183
184 for (i=count=0; i<4; i++) {
185 for (j=0; j<20; j++) {
186 unsigned work;
187
188 work = *rot[2] ^ *rot[3];
189 if (!i) work = (work & *rot[1]) ^ *rot[3];
190 else {
191 if (i==2) work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]);
192 else work ^= *rot[1];
193 }
194
195 if (!i && j<16)
196 work += block[count] = (rol(block[count],24)&0xFF00FF00)
197 | (rol(block[count],8)&0x00FF00FF);
198 else
199 work += block[count&15] = rol(block[(count+13)&15]
200 ^ block[(count+8)&15] ^ block[(count+2)&15] ^ block[count&15], 1);
201 *rot[4] += work + rol(*rot[0],5) + rconsts[i];
202 *rot[1] = rol(*rot[1],30);
203
204
205 temp = rot[4];
206 for (k=4; k; k--) rot[k] = rot[k-1];
207 *rot = temp;
208 count++;
209 }
210 }
211
212 for (i=0; i<5; i++) TT.state[i] += TT.oldstate[i];
213}
214
215
216
217static void hash_update(char *data, unsigned int len, void (*transform)(void))
218{
219 unsigned int i, j;
220
221 j = TT.count & 63;
222 TT.count += len;
223
224 for (;;) {
225
226 i = 64 - j;
227 if (i>len) i = len;
228 memcpy(TT.buffer.c+j, data, i);
229 if (j+i != 64) break;
230
231
232 if (IS_BIG_ENDIAN)
233 for (j=0; j<16; j++) TT.buffer.i[j] = SWAP_LE32(TT.buffer.i[j]);
234 transform();
235 j=0;
236 data += i;
237 len -= i;
238 }
239}
240
241
242#define HASH_INIT(name, prefix) { name, (void *)prefix##_Init, \
243 (void *)prefix##_Update, (void *)prefix##_Final, \
244 prefix##_DIGEST_LENGTH, }
245#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
246
247
248static void do_lib_hash(int fd, char *name)
249{
250
251 SHA512_CTX ctx;
252 struct hash {
253 char *name;
254 int (*init)(void *);
255 int (*update)(void *, void *, size_t);
256 int (*final)(void *, void *);
257 int digest_length;
258 } algorithms[] = {
259 USE_TOYBOX_LIBCRYPTO(
260 USE_MD5SUM(HASH_INIT("md5sum", MD5),)
261 USE_SHA1SUM(HASH_INIT("sha1sum", SHA1),)
262 USE_SHA224SUM(HASH_INIT("sha224sum", SHA224),)
263 USE_SHA256SUM(HASH_INIT("sha256sum", SHA256),)
264 USE_SHA384SUM(HASH_INIT("sha384sum", SHA384),)
265 USE_SHA512SUM(HASH_INIT("sha512sum", SHA512),)
266 )
267 }, * hash;
268 int i;
269
270
271 for (i = 0; i<ARRAY_LEN(algorithms); i++)
272 if (!strcmp(toys.which->name, algorithms[i].name)) break;
273 hash = algorithms+i;
274
275 hash->init(&ctx);
276 for (;;) {
277 i = read(fd, toybuf, sizeof(toybuf));
278 if (i<1) break;
279 hash->update(&ctx, toybuf, i);
280 }
281 hash->final(toybuf+128, &ctx);
282
283 for (i = 0; i<hash->digest_length; i++)
284 sprintf(toybuf+2*i, "%02x", toybuf[i+128]);
285}
286
287
288
289static void do_builtin_hash(int fd, char *name)
290{
291 uint64_t count;
292 int i, sha1=toys.which->name[0]=='s';
293 char buf;
294 void (*transform)(void);
295
296
297 TT.state[0] = 0x67452301;
298 TT.state[1] = 0xEFCDAB89;
299 TT.state[2] = 0x98BADCFE;
300 TT.state[3] = 0x10325476;
301 TT.state[4] = 0xC3D2E1F0;
302 TT.count = 0;
303
304 transform = sha1 ? sha1_transform : md5_transform;
305 for (;;) {
306 i = read(fd, toybuf, sizeof(toybuf));
307 if (i<1) break;
308 hash_update(toybuf, i, transform);
309 }
310
311 count = TT.count << 3;
312
313
314
315
316
317
318
319
320 buf = 0x80;
321 do {
322 hash_update(&buf, 1, transform);
323 buf = 0;
324 } while ((TT.count & 63) != 56);
325 count = sha1 ? SWAP_BE64(count) : SWAP_LE64(count);
326 hash_update((void *)&count, 8, transform);
327
328 if (sha1)
329 for (i = 0; i < 20; i++)
330 sprintf(toybuf+2*i, "%02x", 255&(TT.state[i>>2] >> ((3-(i & 3)) * 8)));
331 else for (i=0; i<4; i++) sprintf(toybuf+8*i, "%08x", bswap_32(TT.state[i]));
332
333
334 memset(TT.state, 0, sizeof(TT)-((long)TT.state-(long)&TT));
335 i = strlen(toybuf)+1;
336 memset(toybuf+i, 0, sizeof(toybuf)-i);
337}
338
339
340static void do_hash(int fd, char *name)
341{
342 if (CFG_TOYBOX_LIBCRYPTO) do_lib_hash(fd, name);
343 else do_builtin_hash(fd, name);
344
345 if (name)
346 printf((toys.optflags & FLAG_b) ? "%s\n" : "%s %s\n", toybuf, name);
347}
348
349static int do_c_line(char *line)
350{
351 int space = 0, fail = 0;
352 char *name;
353
354 for (name = line; *name; name++) {
355 if (isspace(*name)) {
356 space++;
357 *name = 0;
358 } else if (space) break;
359 }
360
361 if (!space || !*line || !*name) error_msg("bad line %s", line);
362 else {
363 int fd = !strcmp(name, "-") ? 0 : open(name, O_RDONLY);
364
365 TT.sawline = 1;
366 if (fd==-1) {
367 perror_msg_raw(name);
368 *toybuf = 0;
369 } else do_hash(fd, 0);
370 if (strcasecmp(line, toybuf)) toys.exitval = fail = 1;
371 if (!FLAG(s)) printf("%s: %s\n", name, fail ? "FAILED" : "OK");
372 if (fd>0) close(fd);
373 }
374
375 return 0;
376}
377
378
379static void do_c_file(char *name)
380{
381 FILE *fp = !strcmp(name, "-") ? stdin : fopen(name, "r");
382
383 if (!fp) {
384 perror_msg_raw(name);
385 return;
386 }
387
388 TT.sawline = 0;
389
390 for (;;) {
391 char *line = 0;
392 ssize_t len;
393
394 if ((len = getline(&line, (void *)&len, fp))<1) break;
395 if (line[len-1]=='\n') line[len-1] = 0;
396 do_c_line(line);
397 free(line);
398 }
399 if (fp!=stdin) fclose(fp);
400
401 if (!TT.sawline) error_msg("%s: no lines", name);
402}
403
404void md5sum_main(void)
405{
406 char **arg;
407
408 if (FLAG(c)) for (arg = toys.optargs; *arg; arg++) do_c_file(*arg);
409 else {
410 if (FLAG(s)) error_exit("-s only with -c");
411 loopfiles(toys.optargs, do_hash);
412 }
413}
414
415void sha1sum_main(void)
416{
417 md5sum_main();
418}
419