blob: ab43e7b99a434d944830d5da88863d1b317cfe4e [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
11USE_MD5SUM(NEWTOY(md5sum, NULL, TOYFLAG_USR|TOYFLAG_BIN))
12USE_MD5SUM_SHA1SUM(OLDTOY(sha1sum, md5sum, NULL, TOYFLAG_USR|TOYFLAG_BIN))
13
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
24config MD5SUM_SHA1SUM
25 bool "sha1sum"
Rob Landleye5138f42012-11-03 19:21:59 -050026 default y
Rob Landleyab1bdc62012-10-23 16:28:14 -050027 depends on MD5SUM
28 help
29 usage: sha1sum [FILE]...
30
31 calculate sha1 hash for each input file, reading from stdin if one.
32 Output one hash (20 hex digits) for each input file, followed by
33 filename.
34*/
35
36#define FOR_md5sum
37#include "toys.h"
38
39GLOBALS(
40 unsigned state[5];
41 unsigned oldstate[5];
42 uint64_t count;
43 union {
44 char c[64];
45 unsigned i[16];
46 } buffer;
47)
48
Daniel Verkampafe951b2014-05-15 19:05:16 -050049#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
50
Rob Landleyab1bdc62012-10-23 16:28:14 -050051// for(i=0; i<64; i++) md5table[i] = abs(sin(i+1))*(1<<32); But calculating
52// that involves not just floating point but pulling in -lm (and arguing with
53// C about whether 1<<32 is a valid thing to do on 32 bit platforms) so:
54
55static uint32_t md5table[64] = {
56 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
57 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
58 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
59 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
60 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
61 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
62 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
63 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
64 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
65 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
66 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
67};
68
Daniel Verkampafe951b2014-05-15 19:05:16 -050069static const uint8_t md5rot[64] = {
70 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
71 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
72 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
73 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
74};
75
Rob Landleyab1bdc62012-10-23 16:28:14 -050076// Mix next 64 bytes of data into md5 hash
77
78static void md5_transform(void)
79{
Daniel Verkampafe951b2014-05-15 19:05:16 -050080 unsigned x[4], *b = TT.buffer.i;
Rob Landleyab1bdc62012-10-23 16:28:14 -050081 int i;
82
83 memcpy(x, TT.state, sizeof(x));
84
85 for (i=0; i<64; i++) {
Daniel Verkampafe951b2014-05-15 19:05:16 -050086 unsigned int in, temp, swap;
Rob Landleyab1bdc62012-10-23 16:28:14 -050087 if (i<16) {
88 in = i;
Daniel Verkampafe951b2014-05-15 19:05:16 -050089 temp = x[1];
90 temp = (temp & x[2]) | ((~temp) & x[3]);
Rob Landleyab1bdc62012-10-23 16:28:14 -050091 } else if (i<32) {
92 in = (1+(5*i))&15;
Daniel Verkampafe951b2014-05-15 19:05:16 -050093 temp = x[3];
94 temp = (x[1] & temp) | (x[2] & ~temp);
Rob Landleyab1bdc62012-10-23 16:28:14 -050095 } else if (i<48) {
Daniel Verkampafe951b2014-05-15 19:05:16 -050096 in = (3*i+5)&15;
97 temp = x[1] ^ x[2] ^ x[3];
Rob Landleyab1bdc62012-10-23 16:28:14 -050098 } else {
Daniel Verkampafe951b2014-05-15 19:05:16 -050099 in = (7*i)&15;
100 temp = x[2] ^ (x[1] | ~x[3]);
Rob Landleyab1bdc62012-10-23 16:28:14 -0500101 }
Daniel Verkampafe951b2014-05-15 19:05:16 -0500102 temp += x[0] + b[in] + md5table[i];
103 swap = x[3];
104 x[3] = x[2];
105 x[2] = x[1];
106 x[1] += rol(temp, md5rot[i]);
107 x[0] = swap;
Rob Landleyab1bdc62012-10-23 16:28:14 -0500108 }
109 for (i=0; i<4; i++) TT.state[i] += x[i];
110}
111
112// Mix next 64 bytes of data into sha1 hash.
113
114static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
Rob Landleyab1bdc62012-10-23 16:28:14 -0500115
116static void sha1_transform(void)
117{
118 int i, j, k, count;
119 unsigned *block = TT.buffer.i;
120 unsigned *rot[5], *temp;
121
122 // Copy context->state[] to working vars
123 for (i=0; i<5; i++) {
124 TT.oldstate[i] = TT.state[i];
125 rot[i] = TT.state + i;
126 }
127 // 4 rounds of 20 operations each.
128 for (i=count=0; i<4; i++) {
129 for (j=0; j<20; j++) {
130 unsigned work;
131
132 work = *rot[2] ^ *rot[3];
133 if (!i) work = (work & *rot[1]) ^ *rot[3];
134 else {
135 if (i==2) work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]);
136 else work ^= *rot[1];
137 }
138
139 if (!i && j<16)
140 work += block[count] = (rol(block[count],24)&0xFF00FF00)
141 | (rol(block[count],8)&0x00FF00FF);
142 else
143 work += block[count&15] = rol(block[(count+13)&15]
144 ^ block[(count+8)&15] ^ block[(count+2)&15] ^ block[count&15], 1);
145 *rot[4] += work + rol(*rot[0],5) + rconsts[i];
146 *rot[1] = rol(*rot[1],30);
147
148 // Rotate by one for next time.
149 temp = rot[4];
150 for (k=4; k; k--) rot[k] = rot[k-1];
151 *rot = temp;
152 count++;
153 }
154 }
155 // Add the previous values of state[]
156 for (i=0; i<5; i++) TT.state[i] += TT.oldstate[i];
157}
158
159// Fill the 64-byte working buffer and call transform() when full.
160
161static void hash_update(char *data, unsigned int len, void (*transform)(void))
162{
163 unsigned int i, j;
164
165 j = TT.count & 63;
166 TT.count += len;
167
168 // Enough data to process a frame?
169 if ((j + len) > 63) {
170 i = 64-j;
171 memcpy(TT.buffer.c + j, data, i);
172 transform();
173 for ( ; i + 63 < len; i += 64) {
174 memcpy(TT.buffer.c, data + i, 64);
175 transform();
176 }
177 j = 0;
178 } else i = 0;
179 // Grab remaining chunk
180 memcpy(TT.buffer.c + j, data + i, len - i);
181}
182
183// Callback for loopfiles()
184
185static void do_hash(int fd, char *name)
186{
187 uint64_t count;
188 int i, sha1=toys.which->name[0]=='s';;
189 char buf;
190 void (*transform)(void);
191
192 /* SHA1 initialization constants (md5sum uses first 4) */
193 TT.state[0] = 0x67452301;
194 TT.state[1] = 0xEFCDAB89;
195 TT.state[2] = 0x98BADCFE;
196 TT.state[3] = 0x10325476;
197 TT.state[4] = 0xC3D2E1F0;
198 TT.count = 0;
199
200 transform = sha1 ? sha1_transform : md5_transform;
201 for (;;) {
202 i = read(fd, toybuf, sizeof(toybuf));
203 if (i<1) break;
204 hash_update(toybuf, i, transform);
205 }
206
207 count = TT.count << 3;
208
209 // End the message by appending a "1" bit to the data, ending with the
210 // message size (in bits, big endian), and adding enough zero bits in
211 // between to pad to the end of the next 64-byte frame.
212 //
213 // Since our input up to now has been in whole bytes, we can deal with
214 // bytes here too.
215
216 buf = 0x80;
217 do {
218 hash_update(&buf, 1, transform);
219 buf = 0;
220 } while ((TT.count & 63) != 56);
221 if (sha1) count=bswap_64(count);
Rob Landley7aa651a2012-11-13 17:14:08 -0600222 for (i = 0; i < 8; i++) TT.buffer.c[56+i] = count >> (8*i);
Rob Landleyab1bdc62012-10-23 16:28:14 -0500223 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}