3
0

md5_sha1_sum.c 11 KB

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