md5_sha1_sum.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "libbb.h"
  58. /* This is a NOEXEC applet. Be very careful! */
  59. enum {
  60. /* 4th letter of applet_name is... */
  61. HASH_MD5 = 's', /* "md5>s<um" */
  62. HASH_SHA1 = '1',
  63. HASH_SHA256 = '2',
  64. HASH_SHA512 = '5',
  65. };
  66. #define FLAG_SILENT 1
  67. #define FLAG_CHECK 2
  68. #define FLAG_WARN 4
  69. /* This might be useful elsewhere */
  70. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  71. unsigned hash_length)
  72. {
  73. /* xzalloc zero-terminates */
  74. char *hex_value = xzalloc((hash_length * 2) + 1);
  75. bin2hex(hex_value, (char*)hash_value, hash_length);
  76. return (unsigned char *)hex_value;
  77. }
  78. static uint8_t *hash_file(const char *filename)
  79. {
  80. int src_fd, hash_len, count;
  81. union _ctx_ {
  82. sha512_ctx_t sha512;
  83. sha256_ctx_t sha256;
  84. sha1_ctx_t sha1;
  85. md5_ctx_t md5;
  86. } context;
  87. uint8_t *hash_value;
  88. void FAST_FUNC (*update)(void*, const void*, size_t);
  89. void FAST_FUNC (*final)(void*, void*);
  90. char hash_algo;
  91. src_fd = open_or_warn_stdin(filename);
  92. if (src_fd < 0) {
  93. return NULL;
  94. }
  95. hash_algo = applet_name[3];
  96. /* figure specific hash algorithms */
  97. if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
  98. md5_begin(&context.md5);
  99. update = (void*)md5_hash;
  100. final = (void*)md5_end;
  101. hash_len = 16;
  102. } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
  103. sha1_begin(&context.sha1);
  104. update = (void*)sha1_hash;
  105. final = (void*)sha1_end;
  106. hash_len = 20;
  107. } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
  108. sha256_begin(&context.sha256);
  109. update = (void*)sha256_hash;
  110. final = (void*)sha256_end;
  111. hash_len = 32;
  112. } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
  113. sha512_begin(&context.sha512);
  114. update = (void*)sha512_hash;
  115. final = (void*)sha512_end;
  116. hash_len = 64;
  117. } else {
  118. xfunc_die(); /* can't reach this */
  119. }
  120. {
  121. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  122. while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
  123. update(&context, in_buf, count);
  124. }
  125. hash_value = NULL;
  126. if (count == 0) {
  127. final(&context, in_buf);
  128. hash_value = hash_bin_to_hex(in_buf, hash_len);
  129. }
  130. RELEASE_CONFIG_BUFFER(in_buf);
  131. }
  132. if (src_fd != STDIN_FILENO) {
  133. close(src_fd);
  134. }
  135. return hash_value;
  136. }
  137. int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  138. int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
  139. {
  140. int return_value = EXIT_SUCCESS;
  141. unsigned flags;
  142. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
  143. /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
  144. flags = getopt32(argv, "scwbt");
  145. argv += optind;
  146. //argc -= optind;
  147. } else {
  148. argv += 1;
  149. //argc -= 1;
  150. }
  151. if (!*argv)
  152. *--argv = (char*)"-";
  153. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  154. if (flags & FLAG_SILENT) {
  155. bb_error_msg_and_die("-%c is meaningful only with -c", 's');
  156. }
  157. if (flags & FLAG_WARN) {
  158. bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
  159. }
  160. }
  161. do {
  162. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  163. FILE *pre_computed_stream;
  164. char *line;
  165. int count_total = 0;
  166. int count_failed = 0;
  167. pre_computed_stream = xfopen_stdin(*argv);
  168. while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
  169. uint8_t *hash_value;
  170. char *filename_ptr;
  171. count_total++;
  172. filename_ptr = strstr(line, " ");
  173. /* handle format for binary checksums */
  174. if (filename_ptr == NULL) {
  175. filename_ptr = strstr(line, " *");
  176. }
  177. if (filename_ptr == NULL) {
  178. if (flags & FLAG_WARN) {
  179. bb_error_msg("invalid format");
  180. }
  181. count_failed++;
  182. return_value = EXIT_FAILURE;
  183. free(line);
  184. continue;
  185. }
  186. *filename_ptr = '\0';
  187. filename_ptr += 2;
  188. hash_value = hash_file(filename_ptr);
  189. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  190. if (!(flags & FLAG_SILENT))
  191. printf("%s: OK\n", filename_ptr);
  192. } else {
  193. if (!(flags & FLAG_SILENT))
  194. printf("%s: FAILED\n", filename_ptr);
  195. count_failed++;
  196. return_value = EXIT_FAILURE;
  197. }
  198. /* possible free(NULL) */
  199. free(hash_value);
  200. free(line);
  201. }
  202. if (count_failed && !(flags & FLAG_SILENT)) {
  203. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  204. count_failed, count_total);
  205. }
  206. fclose_if_not_stdin(pre_computed_stream);
  207. } else {
  208. uint8_t *hash_value = hash_file(*argv);
  209. if (hash_value == NULL) {
  210. return_value = EXIT_FAILURE;
  211. } else {
  212. printf("%s %s\n", hash_value, *argv);
  213. free(hash_value);
  214. }
  215. }
  216. } while (*++argv);
  217. return return_value;
  218. }