md5_sha1_sum.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. //usage:#define md5sum_trivial_usage
  9. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  10. //usage:#define md5sum_full_usage "\n\n"
  11. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " MD5 checksums"
  12. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  13. //usage: "\n -c Check sums against list in FILEs"
  14. //usage: "\n -s Don't output anything, status code shows success"
  15. //usage: "\n -w Warn about improperly formatted checksum lines"
  16. //usage: )
  17. //usage:
  18. //usage:#define md5sum_example_usage
  19. //usage: "$ md5sum < busybox\n"
  20. //usage: "6fd11e98b98a58f64ff3398d7b324003\n"
  21. //usage: "$ md5sum busybox\n"
  22. //usage: "6fd11e98b98a58f64ff3398d7b324003 busybox\n"
  23. //usage: "$ md5sum -c -\n"
  24. //usage: "6fd11e98b98a58f64ff3398d7b324003 busybox\n"
  25. //usage: "busybox: OK\n"
  26. //usage: "^D\n"
  27. //usage:
  28. //usage:#define sha1sum_trivial_usage
  29. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  30. //usage:#define sha1sum_full_usage "\n\n"
  31. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA1 checksums"
  32. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  33. //usage: "\n -c Check sums against list in FILEs"
  34. //usage: "\n -s Don't output anything, status code shows success"
  35. //usage: "\n -w Warn about improperly formatted checksum lines"
  36. //usage: )
  37. //usage:
  38. //usage:#define sha256sum_trivial_usage
  39. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  40. //usage:#define sha256sum_full_usage "\n\n"
  41. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA256 checksums"
  42. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  43. //usage: "\n -c Check sums against list in FILEs"
  44. //usage: "\n -s Don't output anything, status code shows success"
  45. //usage: "\n -w Warn about improperly formatted checksum lines"
  46. //usage: )
  47. //usage:
  48. //usage:#define sha512sum_trivial_usage
  49. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  50. //usage:#define sha512sum_full_usage "\n\n"
  51. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA512 checksums"
  52. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  53. //usage: "\n -c Check sums against list in FILEs"
  54. //usage: "\n -s Don't output anything, status code shows success"
  55. //usage: "\n -w Warn about improperly formatted checksum lines"
  56. //usage: )
  57. //usage:
  58. //usage:#define sha3sum_trivial_usage
  59. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
  60. //usage:#define sha3sum_full_usage "\n\n"
  61. //usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3-512 checksums"
  62. //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
  63. //usage: "\n -c Check sums against list in FILEs"
  64. //usage: "\n -s Don't output anything, status code shows success"
  65. //usage: "\n -w Warn about improperly formatted checksum lines"
  66. //usage: )
  67. #include "libbb.h"
  68. /* This is a NOEXEC applet. Be very careful! */
  69. enum {
  70. /* 4th letter of applet_name is... */
  71. HASH_MD5 = 's', /* "md5>s<um" */
  72. HASH_SHA1 = '1',
  73. HASH_SHA256 = '2',
  74. HASH_SHA3 = '3',
  75. HASH_SHA512 = '5',
  76. };
  77. #define FLAG_SILENT 1
  78. #define FLAG_CHECK 2
  79. #define FLAG_WARN 4
  80. /* This might be useful elsewhere */
  81. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  82. unsigned hash_length)
  83. {
  84. /* xzalloc zero-terminates */
  85. char *hex_value = xzalloc((hash_length * 2) + 1);
  86. bin2hex(hex_value, (char*)hash_value, hash_length);
  87. return (unsigned char *)hex_value;
  88. }
  89. static uint8_t *hash_file(const char *filename)
  90. {
  91. int src_fd, hash_len, count;
  92. union _ctx_ {
  93. sha3_ctx_t sha3;
  94. sha512_ctx_t sha512;
  95. sha256_ctx_t sha256;
  96. sha1_ctx_t sha1;
  97. md5_ctx_t md5;
  98. } context;
  99. uint8_t *hash_value;
  100. void FAST_FUNC (*update)(void*, const void*, size_t);
  101. void FAST_FUNC (*final)(void*, void*);
  102. char hash_algo;
  103. src_fd = open_or_warn_stdin(filename);
  104. if (src_fd < 0) {
  105. return NULL;
  106. }
  107. hash_algo = applet_name[3];
  108. /* figure specific hash algorithms */
  109. if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
  110. md5_begin(&context.md5);
  111. update = (void*)md5_hash;
  112. final = (void*)md5_end;
  113. hash_len = 16;
  114. } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
  115. sha1_begin(&context.sha1);
  116. update = (void*)sha1_hash;
  117. final = (void*)sha1_end;
  118. hash_len = 20;
  119. } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
  120. sha256_begin(&context.sha256);
  121. update = (void*)sha256_hash;
  122. final = (void*)sha256_end;
  123. hash_len = 32;
  124. } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
  125. sha512_begin(&context.sha512);
  126. update = (void*)sha512_hash;
  127. final = (void*)sha512_end;
  128. hash_len = 64;
  129. } else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
  130. sha3_begin(&context.sha3);
  131. update = (void*)sha3_hash;
  132. final = (void*)sha3_end;
  133. hash_len = 64;
  134. } else {
  135. xfunc_die(); /* can't reach this */
  136. }
  137. {
  138. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  139. while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
  140. update(&context, in_buf, count);
  141. }
  142. hash_value = NULL;
  143. if (count < 0)
  144. bb_perror_msg("can't read '%s'", filename);
  145. else /* count == 0 */ {
  146. final(&context, in_buf);
  147. hash_value = hash_bin_to_hex(in_buf, hash_len);
  148. }
  149. RELEASE_CONFIG_BUFFER(in_buf);
  150. }
  151. if (src_fd != STDIN_FILENO) {
  152. close(src_fd);
  153. }
  154. return hash_value;
  155. }
  156. int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  157. int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
  158. {
  159. int return_value = EXIT_SUCCESS;
  160. unsigned flags;
  161. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
  162. /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
  163. flags = getopt32(argv, "scwbt");
  164. argv += optind;
  165. //argc -= optind;
  166. } else {
  167. argv += 1;
  168. //argc -= 1;
  169. }
  170. if (!*argv)
  171. *--argv = (char*)"-";
  172. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  173. if (flags & FLAG_SILENT) {
  174. bb_error_msg_and_die("-%c is meaningful only with -c", 's');
  175. }
  176. if (flags & FLAG_WARN) {
  177. bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
  178. }
  179. }
  180. do {
  181. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  182. FILE *pre_computed_stream;
  183. char *line;
  184. int count_total = 0;
  185. int count_failed = 0;
  186. pre_computed_stream = xfopen_stdin(*argv);
  187. while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
  188. uint8_t *hash_value;
  189. char *filename_ptr;
  190. count_total++;
  191. filename_ptr = strstr(line, " ");
  192. /* handle format for binary checksums */
  193. if (filename_ptr == NULL) {
  194. filename_ptr = strstr(line, " *");
  195. }
  196. if (filename_ptr == NULL) {
  197. if (flags & FLAG_WARN) {
  198. bb_error_msg("invalid format");
  199. }
  200. count_failed++;
  201. return_value = EXIT_FAILURE;
  202. free(line);
  203. continue;
  204. }
  205. *filename_ptr = '\0';
  206. filename_ptr += 2;
  207. hash_value = hash_file(filename_ptr);
  208. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  209. if (!(flags & FLAG_SILENT))
  210. printf("%s: OK\n", filename_ptr);
  211. } else {
  212. if (!(flags & FLAG_SILENT))
  213. printf("%s: FAILED\n", filename_ptr);
  214. count_failed++;
  215. return_value = EXIT_FAILURE;
  216. }
  217. /* possible free(NULL) */
  218. free(hash_value);
  219. free(line);
  220. }
  221. if (count_failed && !(flags & FLAG_SILENT)) {
  222. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  223. count_failed, count_total);
  224. }
  225. fclose_if_not_stdin(pre_computed_stream);
  226. } else {
  227. uint8_t *hash_value = hash_file(*argv);
  228. if (hash_value == NULL) {
  229. return_value = EXIT_FAILURE;
  230. } else {
  231. printf("%s %s\n", hash_value, *argv);
  232. free(hash_value);
  233. }
  234. }
  235. } while (*++argv);
  236. return return_value;
  237. }