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