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