blob: 44bafb1fdf070eda349109d18d16cd030311e897 [file] [log] [blame]
Rob Landleyab1bdc62012-10-23 16:28:14 -05001/* vi: set sw=4 ts=4:
2 *
3 * md5sum.c - Calculate RFC 1321 md5 hash and sha1 hash.
4 *
5 * Copyright 2012 Rob Landley <rob@landley.net>
6 *
7 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/md5sum.html
8 * and http://www.ietf.org/rfc/rfc1321.txt
9 *
10 * They're combined this way to share infrastructure, and because md5sum is
11 * and LSB standard command, sha1sum is just a good idea.
12
13USE_MD5SUM(NEWTOY(md5sum, NULL, TOYFLAG_USR|TOYFLAG_BIN))
14USE_MD5SUM_SHA1SUM(OLDTOY(sha1sum, md5sum, NULL, TOYFLAG_USR|TOYFLAG_BIN))
15
16config MD5SUM
17 bool "md5sum"
18 default n
19 help
20 usage: md5sum [FILE]...
21
22 Calculate md5 hash for each input file, reading from stdin if none.
23 Output one hash (16 hex digits) for each input file, followed by
24 filename.
25
26config MD5SUM_SHA1SUM
27 bool "sha1sum"
28 depends on MD5SUM
29 help
30 usage: sha1sum [FILE]...
31
32 calculate sha1 hash for each input file, reading from stdin if one.
33 Output one hash (20 hex digits) for each input file, followed by
34 filename.
35*/
36
37#define FOR_md5sum
38#include "toys.h"
39
40GLOBALS(
41 unsigned state[5];
42 unsigned oldstate[5];
43 uint64_t count;
44 union {
45 char c[64];
46 unsigned i[16];
47 } buffer;
48)
49
50// for(i=0; i<64; i++) md5table[i] = abs(sin(i+1))*(1<<32); But calculating
51// that involves not just floating point but pulling in -lm (and arguing with
52// C about whether 1<<32 is a valid thing to do on 32 bit platforms) so:
53
54static uint32_t md5table[64] = {
55 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
56 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
57 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
58 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
59 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
60 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
61 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
62 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
63 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
64 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
65 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
66};
67
68// Mix next 64 bytes of data into md5 hash
69
70static void md5_transform(void)
71{
72 unsigned x[4], *b = (unsigned *)TT.buffer.c;
73 int i;
74
75 memcpy(x, TT.state, sizeof(x));
76
77 for (i=0; i<64; i++) {
78 unsigned int in, a, rot, temp;
79
80 a = (-i)&3;
81 if (i<16) {
82 in = i;
83 rot = 7+(5*(i&3));
84 temp = x[(a+1)&3];
85 temp = (temp & x[(a+2)&3]) | ((~temp) & x[(a+3)&3]);
86 } else if (i<32) {
87 in = (1+(5*i))&15;
88 temp = (i&3)+1;
89 rot = temp*5;
90 if (temp&2) rot--;
91 temp = x[(a+3)&3];
92 temp = (x[(a+1)&3] & temp) | (x[(a+2)&3] & ~temp);
93 } else if (i<48) {
94 in = (5+(3*(i&15)))&15;
95 rot = i&3;
96 rot = 4+(5*rot)+((rot+1)&6);
97 temp = x[(a+1)&3] ^ x[(a+2)&3] ^ x[(a+3)&3];
98 } else {
99 in = (7*(i&15))&15;
100 rot = (i&3)+1;
101 rot = (5*rot)+(((rot+2)&2)>>1);
102 temp = x[(a+2)&3] ^ (x[(a+1)&3] | ~x[(a+3)&3]);
103 }
104 temp += x[a] + b[in] + md5table[i];
105 x[a] = x[(a+1)&3] + ((temp<<rot) | (temp>>(32-rot)));
106 }
107 for (i=0; i<4; i++) TT.state[i] += x[i];
108}
109
110// Mix next 64 bytes of data into sha1 hash.
111
112static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
113#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
114
115static void sha1_transform(void)
116{
117 int i, j, k, count;
118 unsigned *block = TT.buffer.i;
119 unsigned *rot[5], *temp;
120
121 // Copy context->state[] to working vars
122 for (i=0; i<5; i++) {
123 TT.oldstate[i] = TT.state[i];
124 rot[i] = TT.state + i;
125 }
126 // 4 rounds of 20 operations each.
127 for (i=count=0; i<4; i++) {
128 for (j=0; j<20; j++) {
129 unsigned work;
130
131 work = *rot[2] ^ *rot[3];
132 if (!i) work = (work & *rot[1]) ^ *rot[3];
133 else {
134 if (i==2) work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]);
135 else work ^= *rot[1];
136 }
137
138 if (!i && j<16)
139 work += block[count] = (rol(block[count],24)&0xFF00FF00)
140 | (rol(block[count],8)&0x00FF00FF);
141 else
142 work += block[count&15] = rol(block[(count+13)&15]
143 ^ block[(count+8)&15] ^ block[(count+2)&15] ^ block[count&15], 1);
144 *rot[4] += work + rol(*rot[0],5) + rconsts[i];
145 *rot[1] = rol(*rot[1],30);
146
147 // Rotate by one for next time.
148 temp = rot[4];
149 for (k=4; k; k--) rot[k] = rot[k-1];
150 *rot = temp;
151 count++;
152 }
153 }
154 // Add the previous values of state[]
155 for (i=0; i<5; i++) TT.state[i] += TT.oldstate[i];
156}
157
158// Fill the 64-byte working buffer and call transform() when full.
159
160static void hash_update(char *data, unsigned int len, void (*transform)(void))
161{
162 unsigned int i, j;
163
164 j = TT.count & 63;
165 TT.count += len;
166
167 // Enough data to process a frame?
168 if ((j + len) > 63) {
169 i = 64-j;
170 memcpy(TT.buffer.c + j, data, i);
171 transform();
172 for ( ; i + 63 < len; i += 64) {
173 memcpy(TT.buffer.c, data + i, 64);
174 transform();
175 }
176 j = 0;
177 } else i = 0;
178 // Grab remaining chunk
179 memcpy(TT.buffer.c + j, data + i, len - i);
180}
181
182// Callback for loopfiles()
183
184static void do_hash(int fd, char *name)
185{
186 uint64_t count;
187 int i, sha1=toys.which->name[0]=='s';;
188 char buf;
189 void (*transform)(void);
190
191 /* SHA1 initialization constants (md5sum uses first 4) */
192 TT.state[0] = 0x67452301;
193 TT.state[1] = 0xEFCDAB89;
194 TT.state[2] = 0x98BADCFE;
195 TT.state[3] = 0x10325476;
196 TT.state[4] = 0xC3D2E1F0;
197 TT.count = 0;
198
199 transform = sha1 ? sha1_transform : md5_transform;
200 for (;;) {
201 i = read(fd, toybuf, sizeof(toybuf));
202 if (i<1) break;
203 hash_update(toybuf, i, transform);
204 }
205
206 count = TT.count << 3;
207
208 // End the message by appending a "1" bit to the data, ending with the
209 // message size (in bits, big endian), and adding enough zero bits in
210 // between to pad to the end of the next 64-byte frame.
211 //
212 // Since our input up to now has been in whole bytes, we can deal with
213 // bytes here too.
214
215 buf = 0x80;
216 do {
217 hash_update(&buf, 1, transform);
218 buf = 0;
219 } while ((TT.count & 63) != 56);
220 if (sha1) count=bswap_64(count);
221 for (i = 0; i < 8; i++)
222 TT.buffer.c[56+i] = count >> (8*i);
223 transform();
224
225 if (sha1)
226 for (i = 0; i < 20; i++)
227 printf("%02x", 255&(TT.state[i>>2] >> ((3-(i & 3)) * 8)));
228 else for (i=0; i<4; i++) printf("%08x", SWAP_BE32(TT.state[i]));
229
230 // Wipe variables. Cryptographer paranoia.
231 memset(&TT, 0, sizeof(TT));
232
233 printf(" %s\n", name);
234}
235
236void md5sum_main(void)
237{
238 loopfiles(toys.optargs, do_hash);
239}