md5_sha1_sum.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003 Glenn L. McGrath
  4. * Copyright (C) 2003-2004 Erik Andersen
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config MD5SUM
  9. //config: bool "md5sum"
  10. //config: default y
  11. //config: help
  12. //config: md5sum is used to print or check MD5 checksums.
  13. //config:
  14. //config:config SHA1SUM
  15. //config: bool "sha1sum"
  16. //config: default y
  17. //config: help
  18. //config: Compute and check SHA1 message digest
  19. //config:
  20. //config:config SHA256SUM
  21. //config: bool "sha256sum"
  22. //config: default y
  23. //config: help
  24. //config: Compute and check SHA256 message digest
  25. //config:
  26. //config:config SHA512SUM
  27. //config: bool "sha512sum"
  28. //config: default y
  29. //config: help
  30. //config: Compute and check SHA512 message digest
  31. //config:
  32. //config:config SHA3SUM
  33. //config: bool "sha3sum"
  34. //config: default y
  35. //config: help
  36. //config: Compute and check SHA3 message digest
  37. //config:
  38. //config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum"
  39. //config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
  40. //config:
  41. //config:config FEATURE_MD5_SHA1_SUM_CHECK
  42. //config: bool "Enable -c, -s and -w options"
  43. //config: default y
  44. //config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
  45. //config: help
  46. //config: Enabling the -c options allows files to be checked
  47. //config: against pre-calculated hash values.
  48. //config:
  49. //config: -s and -w are useful options when verifying checksums.
  50. //applet:IF_MD5SUM(APPLET_NOEXEC(md5sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, md5sum))
  51. //applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum))
  52. //applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum))
  53. //applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum))
  54. //applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum))
  55. //kbuild:lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o
  56. //kbuild:lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o
  57. //kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o
  58. //kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o
  59. //kbuild:lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o
  60. //usage:#define md5sum_trivial_usage
  61. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  62. //usage:#define md5sum_full_usage "\n\n"
  63. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " MD5 checksums"
  64. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  65. //usage: "\n -c Check sums against list in FILEs"
  66. //usage: "\n -s Don't output anything, status code shows success"
  67. //usage: "\n -w Warn about improperly formatted checksum lines"
  68. //usage: )
  69. //usage:
  70. //usage:#define md5sum_example_usage
  71. //usage: "$ md5sum < busybox\n"
  72. //usage: "6fd11e98b98a58f64ff3398d7b324003\n"
  73. //usage: "$ md5sum busybox\n"
  74. //usage: "6fd11e98b98a58f64ff3398d7b324003 busybox\n"
  75. //usage: "$ md5sum -c -\n"
  76. //usage: "6fd11e98b98a58f64ff3398d7b324003 busybox\n"
  77. //usage: "busybox: OK\n"
  78. //usage: "^D\n"
  79. //usage:
  80. //usage:#define sha1sum_trivial_usage
  81. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  82. //usage:#define sha1sum_full_usage "\n\n"
  83. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA1 checksums"
  84. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  85. //usage: "\n -c Check sums against list in FILEs"
  86. //usage: "\n -s Don't output anything, status code shows success"
  87. //usage: "\n -w Warn about improperly formatted checksum lines"
  88. //usage: )
  89. //usage:
  90. //usage:#define sha256sum_trivial_usage
  91. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  92. //usage:#define sha256sum_full_usage "\n\n"
  93. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA256 checksums"
  94. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  95. //usage: "\n -c Check sums against list in FILEs"
  96. //usage: "\n -s Don't output anything, status code shows success"
  97. //usage: "\n -w Warn about improperly formatted checksum lines"
  98. //usage: )
  99. //usage:
  100. //usage:#define sha512sum_trivial_usage
  101. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  102. //usage:#define sha512sum_full_usage "\n\n"
  103. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA512 checksums"
  104. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  105. //usage: "\n -c Check sums against list in FILEs"
  106. //usage: "\n -s Don't output anything, status code shows success"
  107. //usage: "\n -w Warn about improperly formatted checksum lines"
  108. //usage: )
  109. //usage:
  110. //usage:#define sha3sum_trivial_usage
  111. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[-a BITS] [FILE]..."
  112. //usage:#define sha3sum_full_usage "\n\n"
  113. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3 checksums"
  114. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  115. //usage: "\n -c Check sums against list in FILEs"
  116. //usage: "\n -s Don't output anything, status code shows success"
  117. //usage: "\n -w Warn about improperly formatted checksum lines"
  118. //usage: "\n -a BITS 224 (default), 256, 384, 512"
  119. //usage: )
  120. //FIXME: GNU coreutils 8.25 has no -s option, it has only these two long opts:
  121. // --quiet don't print OK for each successfully verified file
  122. // --status don't output anything, status code shows success
  123. #include "libbb.h"
  124. /* This is a NOEXEC applet. Be very careful! */
  125. enum {
  126. /* 4th letter of applet_name is... */
  127. HASH_MD5 = 's', /* "md5>s<um" */
  128. HASH_SHA1 = '1',
  129. HASH_SHA256 = '2',
  130. HASH_SHA3 = '3',
  131. HASH_SHA512 = '5',
  132. };
  133. #define FLAG_SILENT 1
  134. #define FLAG_CHECK 2
  135. #define FLAG_WARN 4
  136. /* This might be useful elsewhere */
  137. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  138. unsigned hash_length)
  139. {
  140. /* xzalloc zero-terminates */
  141. char *hex_value = xzalloc((hash_length * 2) + 1);
  142. bin2hex(hex_value, (char*)hash_value, hash_length);
  143. return (unsigned char *)hex_value;
  144. }
  145. #if !ENABLE_SHA3SUM
  146. # define hash_file(f,w) hash_file(f)
  147. #endif
  148. static uint8_t *hash_file(const char *filename, unsigned sha3_width)
  149. {
  150. int src_fd, hash_len, count;
  151. union _ctx_ {
  152. sha3_ctx_t sha3;
  153. sha512_ctx_t sha512;
  154. sha256_ctx_t sha256;
  155. sha1_ctx_t sha1;
  156. md5_ctx_t md5;
  157. } context;
  158. uint8_t *hash_value;
  159. void FAST_FUNC (*update)(void*, const void*, size_t);
  160. void FAST_FUNC (*final)(void*, void*);
  161. char hash_algo;
  162. src_fd = open_or_warn_stdin(filename);
  163. if (src_fd < 0) {
  164. return NULL;
  165. }
  166. hash_algo = applet_name[3];
  167. /* figure specific hash algorithms */
  168. if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
  169. md5_begin(&context.md5);
  170. update = (void*)md5_hash;
  171. final = (void*)md5_end;
  172. hash_len = 16;
  173. }
  174. else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
  175. sha1_begin(&context.sha1);
  176. update = (void*)sha1_hash;
  177. final = (void*)sha1_end;
  178. hash_len = 20;
  179. }
  180. else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
  181. sha256_begin(&context.sha256);
  182. update = (void*)sha256_hash;
  183. final = (void*)sha256_end;
  184. hash_len = 32;
  185. }
  186. else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
  187. sha512_begin(&context.sha512);
  188. update = (void*)sha512_hash;
  189. final = (void*)sha512_end;
  190. hash_len = 64;
  191. }
  192. #if ENABLE_SHA3SUM
  193. else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
  194. sha3_begin(&context.sha3);
  195. update = (void*)sha3_hash;
  196. final = (void*)sha3_end;
  197. /*
  198. * Should support 224, 256, 384, 512.
  199. * We allow any value which does not blow the algorithm up.
  200. */
  201. if (sha3_width >= 1600/2 /* input block can't be <= 0 */
  202. || sha3_width == 0 /* hash len can't be 0 */
  203. || (sha3_width & 0x1f) /* should be multiple of 32 */
  204. /* (because input uses up to 8 byte wide word XORs. 32/4=8) */
  205. ) {
  206. bb_error_msg_and_die("bad -a%u", sha3_width);
  207. }
  208. sha3_width /= 4;
  209. context.sha3.input_block_bytes = 1600/8 - sha3_width;
  210. hash_len = sha3_width/2;
  211. }
  212. #endif
  213. else {
  214. xfunc_die(); /* can't reach this */
  215. }
  216. {
  217. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  218. while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
  219. update(&context, in_buf, count);
  220. }
  221. hash_value = NULL;
  222. if (count < 0)
  223. bb_perror_msg("can't read '%s'", filename);
  224. else /* count == 0 */ {
  225. final(&context, in_buf);
  226. hash_value = hash_bin_to_hex(in_buf, hash_len);
  227. }
  228. RELEASE_CONFIG_BUFFER(in_buf);
  229. }
  230. if (src_fd != STDIN_FILENO) {
  231. close(src_fd);
  232. }
  233. return hash_value;
  234. }
  235. int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  236. int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
  237. {
  238. int return_value = EXIT_SUCCESS;
  239. unsigned flags;
  240. #if ENABLE_SHA3SUM
  241. unsigned sha3_width = 224;
  242. #endif
  243. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
  244. /* -s and -w require -c */
  245. opt_complementary = "s?c:w?c";
  246. /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
  247. #if ENABLE_SHA3SUM
  248. if (applet_name[3] == HASH_SHA3)
  249. flags = getopt32(argv, "scwbta:+", &sha3_width);
  250. else
  251. #endif
  252. flags = getopt32(argv, "scwbt");
  253. } else {
  254. #if ENABLE_SHA3SUM
  255. if (applet_name[3] == HASH_SHA3)
  256. getopt32(argv, "a:+", &sha3_width);
  257. else
  258. #endif
  259. getopt32(argv, "");
  260. }
  261. argv += optind;
  262. //argc -= optind;
  263. if (!*argv)
  264. *--argv = (char*)"-";
  265. do {
  266. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  267. FILE *pre_computed_stream;
  268. char *line;
  269. int count_total = 0;
  270. int count_failed = 0;
  271. pre_computed_stream = xfopen_stdin(*argv);
  272. while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
  273. uint8_t *hash_value;
  274. char *filename_ptr;
  275. count_total++;
  276. filename_ptr = strstr(line, " ");
  277. /* handle format for binary checksums */
  278. if (filename_ptr == NULL) {
  279. filename_ptr = strstr(line, " *");
  280. }
  281. if (filename_ptr == NULL) {
  282. if (flags & FLAG_WARN) {
  283. bb_error_msg("invalid format");
  284. }
  285. count_failed++;
  286. return_value = EXIT_FAILURE;
  287. free(line);
  288. continue;
  289. }
  290. *filename_ptr = '\0';
  291. filename_ptr += 2;
  292. hash_value = hash_file(filename_ptr, sha3_width);
  293. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  294. if (!(flags & FLAG_SILENT))
  295. printf("%s: OK\n", filename_ptr);
  296. } else {
  297. if (!(flags & FLAG_SILENT))
  298. printf("%s: FAILED\n", filename_ptr);
  299. count_failed++;
  300. return_value = EXIT_FAILURE;
  301. }
  302. /* possible free(NULL) */
  303. free(hash_value);
  304. free(line);
  305. }
  306. if (count_failed && !(flags & FLAG_SILENT)) {
  307. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  308. count_failed, count_total);
  309. }
  310. if (count_total == 0) {
  311. return_value = EXIT_FAILURE;
  312. /*
  313. * md5sum from GNU coreutils 8.25 says:
  314. * md5sum: <FILE>: no properly formatted MD5 checksum lines found
  315. */
  316. bb_error_msg("%s: no checksum lines found", *argv);
  317. }
  318. fclose_if_not_stdin(pre_computed_stream);
  319. } else {
  320. uint8_t *hash_value = hash_file(*argv, sha3_width);
  321. if (hash_value == NULL) {
  322. return_value = EXIT_FAILURE;
  323. } else {
  324. printf("%s %s\n", hash_value, *argv);
  325. free(hash_value);
  326. }
  327. }
  328. } while (*++argv);
  329. return return_value;
  330. }