blob: b0b35419f675a2afa56a48bf460c3e9017006999 [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"
Rob Landleye5138f42012-11-03 19:21:59 -050018 default y
Rob Landleyab1bdc62012-10-23 16:28:14 -050019 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"
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
33 calculate sha1 hash for each input file, reading from stdin if one.
34 Output one hash (20 hex digits) for each input file, followed by
35 filename.
36*/
37
38#define FOR_md5sum
39#include "toys.h"
40
41GLOBALS(
42 unsigned state[5];
43 unsigned oldstate[5];
44 uint64_t count;
45 union {
46 char c[64];
47 unsigned i[16];
48 } buffer;
49)
50
51// 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
69// Mix next 64 bytes of data into md5 hash
70
71static void md5_transform(void)
72{
73 unsigned x[4], *b = (unsigned *)TT.buffer.c;
74 int i;
75
76 memcpy(x, TT.state, sizeof(x));
77
78 for (i=0; i<64; i++) {
79 unsigned int in, a, rot, temp;
80
81 a = (-i)&3;
82 if (i<16) {
83 in = i;
84 rot = 7+(5*(i&3));
85 temp = x[(a+1)&3];
86 temp = (temp & x[(a+2)&3]) | ((~temp) & x[(a+3)&3]);
87 } else if (i<32) {
88 in = (1+(5*i))&15;
89 temp = (i&3)+1;
90 rot = temp*5;
91 if (temp&2) rot--;
92 temp = x[(a+3)&3];
93 temp = (x[(a+1)&3] & temp) | (x[(a+2)&3] & ~temp);
94 } else if (i<48) {
95 in = (5+(3*(i&15)))&15;
96 rot = i&3;
97 rot = 4+(5*rot)+((rot+1)&6);
98 temp = x[(a+1)&3] ^ x[(a+2)&3] ^ x[(a+3)&3];
99 } else {
100 in = (7*(i&15))&15;
101 rot = (i&3)+1;
102 rot = (5*rot)+(((rot+2)&2)>>1);
103 temp = x[(a+2)&3] ^ (x[(a+1)&3] | ~x[(a+3)&3]);
104 }
105 temp += x[a] + b[in] + md5table[i];
106 x[a] = x[(a+1)&3] + ((temp<<rot) | (temp>>(32-rot)));
107 }
108 for (i=0; i<4; i++) TT.state[i] += x[i];
109}
110
111// Mix next 64 bytes of data into sha1 hash.
112
113static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
114#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
115
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);
222 for (i = 0; i < 8; i++)
223 TT.buffer.c[56+i] = count >> (8*i);
224 transform();
225
226 if (sha1)
227 for (i = 0; i < 20; i++)
228 printf("%02x", 255&(TT.state[i>>2] >> ((3-(i & 3)) * 8)));
229 else for (i=0; i<4; i++) printf("%08x", SWAP_BE32(TT.state[i]));
230
231 // Wipe variables. Cryptographer paranoia.
232 memset(&TT, 0, sizeof(TT));
233
234 printf(" %s\n", name);
235}
236
237void md5sum_main(void)
238{
239 loopfiles(toys.optargs, do_hash);
240}