md5sum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* md5sum.c - Calculate hashes md5, sha1, sha224, sha256, sha384, sha512.
  2. *
  3. * Copyright 2012, 2021 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. * and http://www.ietf.org/rfc/rfc4634.txt
  8. *
  9. * They're combined this way to share infrastructure, and because md5sum is
  10. * a LSB standard command (but sha1sum and newer hashes are a good idea,
  11. * see http://valerieaurora.org/hash.html).
  12. *
  13. * We optionally use openssl (or equivalent) to access assembly optimized
  14. * versions of these functions, but provide a built-in version to reduce
  15. * required dependencies.
  16. *
  17. * coreutils supports --status but not -s, busybox supports -s but not --status
  18. USE_MD5SUM(NEWTOY(md5sum, "bc(check)s(status)[!bc]", TOYFLAG_USR|TOYFLAG_BIN))
  19. USE_SHA1SUM(OLDTOY(sha1sum, md5sum, TOYFLAG_USR|TOYFLAG_BIN))
  20. USE_SHA224SUM(OLDTOY(sha224sum, md5sum, TOYFLAG_USR|TOYFLAG_BIN))
  21. USE_SHA256SUM(OLDTOY(sha256sum, md5sum, TOYFLAG_USR|TOYFLAG_BIN))
  22. USE_SHA384SUM(OLDTOY(sha384sum, md5sum, TOYFLAG_USR|TOYFLAG_BIN))
  23. USE_SHA512SUM(OLDTOY(sha512sum, md5sum, TOYFLAG_USR|TOYFLAG_BIN))
  24. config MD5SUM
  25. bool "md5sum"
  26. default y
  27. help
  28. usage: ???sum [-bcs] [FILE]...
  29. Calculate hash for each input file, reading from stdin if none, writing
  30. hexadecimal digits to stdout for each input file (md5=32 hex digits,
  31. sha1=40, sha224=56, sha256=64, sha384=96, sha512=128) followed by filename.
  32. -b Brief (hash only, no filename)
  33. -c Check each line of each FILE is the same hash+filename we'd output
  34. -s No output, exit status 0 if all hashes match, 1 otherwise
  35. config SHA1SUM
  36. bool "sha1sum"
  37. default y
  38. help
  39. See md5sum
  40. config SHA224SUM
  41. bool "sha224sum"
  42. default y
  43. help
  44. See md5sum
  45. config SHA256SUM
  46. bool "sha256sum"
  47. default y
  48. help
  49. See md5sum
  50. config SHA384SUM
  51. bool "sha384sum"
  52. default y
  53. help
  54. See md5sum
  55. config SHA512SUM
  56. bool "sha512sum"
  57. default y
  58. help
  59. See md5sum
  60. */
  61. #define FORCE_FLAGS
  62. #define FOR_md5sum
  63. #include "toys.h"
  64. #if CFG_TOYBOX_LIBCRYPTO
  65. #include <openssl/md5.h>
  66. #include <openssl/sha.h>
  67. #else
  68. typedef int SHA512_CTX;
  69. #endif
  70. GLOBALS(
  71. int sawline;
  72. unsigned *rconsttable32;
  73. unsigned long long *rconsttable64; // for sha384,sha512
  74. // Crypto variables blanked after summing
  75. unsigned long long count, overflow;
  76. union {
  77. char c[128]; // bytes, 1024 bits
  78. unsigned i32[16]; // 512 bits for md5,sha1,sha224,sha256
  79. unsigned long long i64[16]; // 1024 bits for sha384,sha512
  80. } state, buffer;
  81. )
  82. // Round constants. Static table for when we haven't got floating point support
  83. #if ! CFG_TOYBOX_FLOAT
  84. static const unsigned md5nofloat[64] = {
  85. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
  86. 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  87. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
  88. 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  89. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
  90. 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  91. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
  92. 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  93. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
  94. 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  95. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  96. };
  97. #else
  98. #define md5nofloat 0
  99. #endif
  100. static unsigned long long sha512nofloat[80] = {
  101. // we cannot calculate these 64-bit values using the readily
  102. // available floating point data types and math functions,
  103. // so we always use this lookup table (80 * 8 bytes)
  104. 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
  105. 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
  106. 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
  107. 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
  108. 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
  109. 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
  110. 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
  111. 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
  112. 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
  113. 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
  114. 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
  115. 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
  116. 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
  117. 0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
  118. 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
  119. 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
  120. 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
  121. 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
  122. 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
  123. 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
  124. 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
  125. 0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
  126. 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
  127. 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
  128. 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
  129. 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
  130. 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
  131. };
  132. // sha1 needs only 4 round constant values, so prefer precomputed
  133. static const unsigned sha1rconsts[] = {
  134. 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
  135. };
  136. // bit rotations
  137. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  138. #define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
  139. #define ror64(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits))))
  140. // Mix next 64 bytes of data into md5 hash
  141. static void md5_transform(void)
  142. {
  143. unsigned x[4], *b = TT.buffer.i32;
  144. int i;
  145. memcpy(x, TT.state.i32, sizeof(x));
  146. for (i = 0; i<64; i++) {
  147. unsigned in, a, rot, temp;
  148. a = (-i)&3;
  149. if (i<16) {
  150. in = i;
  151. rot = 7+(5*(i&3));
  152. temp = x[(a+1)&3];
  153. temp = (temp & x[(a+2)&3]) | ((~temp) & x[(a+3)&3]);
  154. } else if (i<32) {
  155. in = (1+(5*i))&15;
  156. temp = (i&3)+1;
  157. rot = temp*5;
  158. if (temp&2) rot--;
  159. temp = x[(a+3)&3];
  160. temp = (x[(a+1)&3] & temp) | (x[(a+2)&3] & ~temp);
  161. } else if (i<48) {
  162. in = (5+(3*(i&15)))&15;
  163. rot = i&3;
  164. rot = 4+(5*rot)+((rot+1)&6);
  165. temp = x[(a+1)&3] ^ x[(a+2)&3] ^ x[(a+3)&3];
  166. } else {
  167. in = (7*(i&15))&15;
  168. rot = (i&3)+1;
  169. rot = (5*rot)+(((rot+2)&2)>>1);
  170. temp = x[(a+2)&3] ^ (x[(a+1)&3] | ~x[(a+3)&3]);
  171. }
  172. temp += x[a] + b[in] + TT.rconsttable32[i];
  173. x[a] = x[(a+1)&3] + ((temp<<rot) | (temp>>(32-rot)));
  174. }
  175. for (i = 0; i<4; i++) TT.state.i32[i] += x[i];
  176. }
  177. // Mix next 64 bytes of data into sha1 hash.
  178. static void sha1_transform(void)
  179. {
  180. int i, j, k, count;
  181. unsigned *block = TT.buffer.i32, oldstate[5], *rot[5], *temp, work;
  182. // Copy context->state.i32[] to working vars
  183. for (i = 0; i<5; i++) {
  184. oldstate[i] = TT.state.i32[i];
  185. rot[i] = TT.state.i32 + i;
  186. }
  187. // 4 rounds of 20 operations each.
  188. for (i = count = 0; i<4; i++) {
  189. for (j = 0; j<20; j++) {
  190. work = *rot[2] ^ *rot[3];
  191. if (!i) work = (work & *rot[1]) ^ *rot[3];
  192. else {
  193. if (i==2) work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]);
  194. else work ^= *rot[1];
  195. }
  196. if (!i && j<16)
  197. work += block[count] = (ror(block[count],8)&0xFF00FF00)
  198. | (rol(block[count],8)&0x00FF00FF);
  199. else
  200. work += block[count&15] = rol(block[(count+13)&15]
  201. ^ block[(count+8)&15] ^ block[(count+2)&15] ^ block[count&15], 1);
  202. *rot[4] += work + rol(*rot[0],5) + sha1rconsts[i];
  203. *rot[1] = rol(*rot[1],30);
  204. // Rotate by one for next time.
  205. temp = rot[4];
  206. for (k = 4; k; k--) rot[k] = rot[k-1];
  207. *rot = temp;
  208. count++;
  209. }
  210. }
  211. // Add the previous values of state.i32[]
  212. for (i = 0; i<5; i++) TT.state.i32[i] += oldstate[i];
  213. }
  214. static void sha2_32_transform(void)
  215. {
  216. unsigned block[64], s0, s1, S0, S1, ch, maj, temp1, temp2, rot[8];
  217. int i;
  218. for (i = 0; i<16; i++) block[i] = SWAP_BE32(TT.buffer.i32[i]);
  219. // Extend the message schedule array beyond first 16 words
  220. for (i = 16; i<64; i++) {
  221. s0 = ror(block[i-15], 7) ^ ror(block[i-15], 18) ^ (block[i-15] >> 3);
  222. s1 = ror(block[i-2], 17) ^ ror(block[i-2], 19) ^ (block[i-2] >> 10);
  223. block[i] = block[i-16] + s0 + block[i-7] + s1;
  224. }
  225. // Copy context->state.i32[] to working vars
  226. for (i = 0; i<8; i++) rot[i] = TT.state.i32[i];
  227. // 64 rounds
  228. for (i = 0; i<64; i++) {
  229. S1 = ror(rot[4],6) ^ ror(rot[4],11) ^ ror(rot[4], 25);
  230. ch = (rot[4] & rot[5]) ^ ((~ rot[4]) & rot[6]);
  231. temp1 = rot[7] + S1 + ch + TT.rconsttable32[i] + block[i];
  232. S0 = ror(rot[0],2) ^ ror(rot[0],13) ^ ror(rot[0], 22);
  233. maj = (rot[0] & rot[1]) ^ (rot[0] & rot[2]) ^ (rot[1] & rot[2]);
  234. temp2 = S0 + maj;
  235. memmove(rot+1, rot, 28);
  236. rot[4] += temp1;
  237. rot[0] = temp1 + temp2;
  238. }
  239. // Add the previous values of state.i32[]
  240. for (i = 0; i<8; i++) TT.state.i32[i] += rot[i];
  241. }
  242. static void sha2_64_transform(void)
  243. {
  244. unsigned long long block[80], s0, s1, S0, S1, ch, maj, temp1, temp2, rot[8];
  245. int i;
  246. for (i=0; i<16; i++) block[i] = SWAP_BE64(TT.buffer.i64[i]);
  247. // Extend the message schedule array beyond first 16 words
  248. for (i = 16; i<80; i++) {
  249. s0 = ror64(block[i-15], 1) ^ ror64(block[i-15], 8) ^ (block[i-15] >> 7);
  250. s1 = ror64(block[i-2], 19) ^ ror64(block[i-2], 61) ^ (block[i-2] >> 6);
  251. block[i] = block[i-16] + s0 + block[i-7] + s1;
  252. }
  253. // Copy context->state.i64[] to working vars
  254. for (i = 0; i<8; i++) rot[i] = TT.state.i64[i];
  255. // 80 rounds
  256. for (i = 0; i<80; i++) {
  257. S1 = ror64(rot[4],14) ^ ror64(rot[4],18) ^ ror64(rot[4], 41);
  258. ch = (rot[4] & rot[5]) ^ ((~ rot[4]) & rot[6]);
  259. temp1 = rot[7] + S1 + ch + TT.rconsttable64[i] + block[i];
  260. S0 = ror64(rot[0],28) ^ ror64(rot[0],34) ^ ror64(rot[0], 39);
  261. maj = (rot[0] & rot[1]) ^ (rot[0] & rot[2]) ^ (rot[1] & rot[2]);
  262. temp2 = S0 + maj;
  263. memmove(rot+1, rot, 56);
  264. rot[4] += temp1;
  265. rot[0] = temp1 + temp2;
  266. }
  267. // Add the previous values of state.i64[]
  268. for (i=0; i<8; i++) TT.state.i64[i] += rot[i];
  269. }
  270. // Fill the 64/128-byte (512/1024-bit) working buffer and call transform() when full.
  271. static void hash_update(char *data, unsigned int len, void (*transform)(void),
  272. int chunksize)
  273. {
  274. unsigned int i, j;
  275. j = TT.count & (chunksize - 1);
  276. if (TT.count+len<TT.count) TT.overflow++;
  277. TT.count += len;
  278. for (;;) {
  279. // Grab next chunk of data, return if it's not enough to process a frame
  280. i = chunksize - j;
  281. if (i>len) i = len;
  282. memcpy(TT.buffer.c+j, data, i);
  283. if (j+i != chunksize) break;
  284. // Process a frame
  285. transform();
  286. j=0;
  287. data += i;
  288. len -= i;
  289. }
  290. }
  291. // Initialize array tersely
  292. #define HASH_INIT(name, prefix) { name, (void *)prefix##_Init, \
  293. (void *)prefix##_Update, (void *)prefix##_Final, \
  294. prefix##_DIGEST_LENGTH, }
  295. #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
  296. // Call the assembly optimized library code when CFG_TOYBOX_LIBCRYPTO
  297. static void do_lib_hash(int fd, char *name)
  298. {
  299. // Largest context
  300. SHA512_CTX ctx;
  301. struct hash {
  302. char *name;
  303. int (*init)(void *);
  304. int (*update)(void *, void *, size_t);
  305. int (*final)(void *, void *);
  306. int digest_length;
  307. } algorithms[] = {
  308. USE_TOYBOX_LIBCRYPTO(
  309. USE_MD5SUM(HASH_INIT("md5sum", MD5),)
  310. USE_SHA1SUM(HASH_INIT("sha1sum", SHA1),)
  311. USE_SHA224SUM(HASH_INIT("sha224sum", SHA224),)
  312. USE_SHA256SUM(HASH_INIT("sha256sum", SHA256),)
  313. USE_SHA384SUM(HASH_INIT("sha384sum", SHA384),)
  314. USE_SHA512SUM(HASH_INIT("sha512sum", SHA512),)
  315. )
  316. }, * hash;
  317. int i;
  318. // This should never NOT match, so no need to check
  319. for (i = 0; i<ARRAY_LEN(algorithms); i++)
  320. if (!strcmp(toys.which->name, algorithms[i].name)) break;
  321. hash = algorithms+i;
  322. hash->init(&ctx);
  323. for (;;) {
  324. i = read(fd, toybuf, sizeof(toybuf));
  325. if (i<1) break;
  326. hash->update(&ctx, toybuf, i);
  327. }
  328. hash->final(toybuf+128, &ctx);
  329. for (i = 0; i<hash->digest_length; i++)
  330. sprintf(toybuf+2*i, "%02x", toybuf[i+128]);
  331. }
  332. static void do_builtin_hash(int fd, char *name)
  333. {
  334. unsigned long long count[2];
  335. int i, chunksize, digestlen, method;
  336. volatile char *pp;
  337. void (*transform)(void);
  338. char buf;
  339. // md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum
  340. method = stridx("us2581", toys.which->name[4]);
  341. // select hash type
  342. transform = (void *[]){md5_transform, sha1_transform, sha2_32_transform,
  343. sha2_32_transform, sha2_64_transform, sha2_64_transform}[method];
  344. digestlen = (char []){16, 20, 28, 32, 48, 64}[method];
  345. chunksize = 64<<(method>=4);
  346. if (method<=1)
  347. memcpy(TT.state.i32, (unsigned []){0x67452301, 0xEFCDAB89, 0x98BADCFE,
  348. 0x10325476, 0xC3D2E1F0}, 20);
  349. else if (method==2)
  350. memcpy(TT.state.i32, (unsigned []){0xc1059ed8, 0x367cd507, 0x3070dd17,
  351. 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}, 32);
  352. else if (method==3)
  353. memcpy(TT.state.i32, (unsigned []){0x6a09e667, 0xbb67ae85, 0x3c6ef372,
  354. 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}, 32);
  355. else if (method==4)
  356. memcpy(TT.state.i64, (unsigned long long []){0xcbbb9d5dc1059ed8,
  357. 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
  358. 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7,
  359. 0x47b5481dbefa4fa4}, 64);
  360. else memcpy(TT.state.i64, (unsigned long long []){0x6a09e667f3bcc908,
  361. 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
  362. 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b,
  363. 0x5be0cd19137e2179}, 64);
  364. TT.count = 0;
  365. for (;;) {
  366. i = read(fd, toybuf, sizeof(toybuf));
  367. if (i<1) break;
  368. hash_update(toybuf, i, transform, chunksize);
  369. }
  370. // End the message by appending a "1" bit to the data, ending with the
  371. // message size (in bits, big endian), and adding enough zero bits in
  372. // between to pad to the end of the next frame.
  373. //
  374. // Since our input up to now has been in whole bytes, we can deal with
  375. // bytes here too. sha384 and 512 use 128 bit counter, so track overflow.
  376. buf = 0x80;
  377. count[0] = (TT.overflow<<3)+(TT.count>>61);
  378. count[1] = TT.count<<3; // convert to bits
  379. for (i = 0; i<2; i++)
  380. count[i] = !method ? SWAP_LE64(count[i]) : SWAP_BE64(count[i]);
  381. i = 8<<(method>=4);
  382. do {
  383. hash_update(&buf, 1, transform, chunksize);
  384. buf = 0;
  385. } while ((TT.count&(chunksize-1)) != chunksize-i);
  386. hash_update((void *)(count+(method<4)), i, transform, chunksize);
  387. // write digest to toybuf
  388. if (method>=4) for (i=0; i<digestlen/8; i++)
  389. sprintf(toybuf+16*i, "%016llx", TT.state.i64[i]);
  390. else for (i=0; i<digestlen/4; i++)
  391. sprintf(toybuf+8*i, "%08x",
  392. !method ? bswap_32(TT.state.i32[i]) : TT.state.i32[i]);
  393. // Wipe variables. Cryptographer paranoia. Avoid "optimizing" out memset
  394. // by looping on a volatile pointer.
  395. i = sizeof(struct md5sum_data)-offsetof(struct md5sum_data, state.i64);
  396. for (pp = (void *)TT.state.i64; i; i--) *pp++ = 0;
  397. pp = toybuf+strlen(toybuf)+1;
  398. for (i = sizeof(toybuf)-(pp-toybuf); i; i--) *pp++ = 0;
  399. }
  400. // Callback for loopfiles()
  401. // Call builtin or lib hash function, then display output if necessary
  402. static void do_hash(int fd, char *name)
  403. {
  404. if (CFG_TOYBOX_LIBCRYPTO) do_lib_hash(fd, name);
  405. else do_builtin_hash(fd, name);
  406. if (name) printf("%s %s\n"+4*!!FLAG(b), toybuf, name);
  407. }
  408. static void do_c_line(char *line)
  409. {
  410. int space = 0, fail = 0, fd;
  411. char *name;
  412. for (name = line; *name; name++) {
  413. if (isspace(*name)) {
  414. space++;
  415. *name = 0;
  416. } else if (space) break;
  417. }
  418. if (!space || !*line || !*name) return error_msg("bad line %s", line);
  419. fd = !strcmp(name, "-") ? 0 : open(name, O_RDONLY);
  420. TT.sawline = 1;
  421. if (fd==-1) {
  422. perror_msg_raw(name);
  423. *toybuf = 0;
  424. } else do_hash(fd, 0);
  425. if (strcasecmp(line, toybuf)) toys.exitval = fail = 1;
  426. if (!FLAG(s)) printf("%s: %s\n", name, fail ? "FAILED" : "OK");
  427. if (fd>0) close(fd);
  428. }
  429. // Used instead of loopfiles_line to report error on files containing no hashes.
  430. static void do_c_file(char *name)
  431. {
  432. FILE *fp = !strcmp(name, "-") ? stdin : fopen(name, "r");
  433. char *line;
  434. if (!fp) return perror_msg_raw(name);
  435. TT.sawline = 0;
  436. for (;;) {
  437. if (!(line = xgetline(fp))) break;
  438. do_c_line(line);
  439. free(line);
  440. }
  441. if (fp!=stdin) fclose(fp);
  442. if (!TT.sawline) error_msg("%s: no lines", name);
  443. }
  444. void md5sum_main(void)
  445. {
  446. int i;
  447. // Calculate table if we have floating point. Static version should drop
  448. // out at compile time when we don't need it.
  449. if (!CFG_TOYBOX_LIBCRYPTO) {
  450. if (*toys.which->name == 'm') { // MD5
  451. if (CFG_TOYBOX_FLOAT) {
  452. TT.rconsttable32 = xmalloc(64*4);
  453. for (i = 0; i<64; i++) TT.rconsttable32[i] = fabs(sin(i+1))*(1LL<<32);
  454. } else TT.rconsttable32 = md5nofloat;
  455. } else if (toys.which->name[3] == '2') { // sha224, sha256
  456. TT.rconsttable32 = xmalloc(64*4);
  457. for (i=0; i<64; i++) TT.rconsttable32[i] = sha512nofloat[i] >> 32;
  458. } else TT.rconsttable64 = sha512nofloat; // sha384, sha512
  459. }
  460. if (FLAG(c)) for (i = 0; toys.optargs[i]; i++) do_c_file(toys.optargs[i]);
  461. else {
  462. if (FLAG(s)) error_exit("-s only with -c");
  463. loopfiles(toys.optargs, do_hash);
  464. }
  465. }