hashsum.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. /*
  4. * Written by Daniel Richards <[email protected]> 6/7/2002
  5. * hash.c: This app uses libtomcrypt to hash either stdin or a file
  6. * This file is Public Domain. No rights are reserved.
  7. * Compile with 'gcc hashsum.c -o hashsum -ltomcrypt'
  8. * This example isn't really big enough to warrent splitting into
  9. * more functions ;)
  10. */
  11. #include <tomcrypt.h>
  12. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
  13. #include <libgen.h>
  14. #else
  15. #define basename(x) x
  16. #endif
  17. #if !defined(PATH_MAX) && defined(_MSC_VER)
  18. #include <windows.h>
  19. #define PATH_MAX MAX_PATH
  20. #endif
  21. /* thanks http://stackoverflow.com/a/8198009 */
  22. #define s_base(x) ((x >= '0' && x <= '9') ? '0' : \
  23. (x >= 'a' && x <= 'f') ? 'a' - 10 : \
  24. (x >= 'A' && x <= 'F') ? 'A' - 10 : \
  25. '\255')
  26. #define HEXOF(x) (x - s_base(x))
  27. static char* hashsum;
  28. static void cleanup(void)
  29. {
  30. free(hashsum);
  31. }
  32. static void die(int status)
  33. {
  34. unsigned long w, x;
  35. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  36. fprintf(o,
  37. "Usage: %s [-a <algorithm>...] [-c|-h] [<file>...]\n\n"
  38. "\t-c\tCheck the hash(es) of the file(s) written in <file>.\n"
  39. "\t\tNote: -a is not required when checking the hash(es).\n"
  40. "\t-h\tThis help\n\n"
  41. "Examples:\n"
  42. "\t%s -a sha1 file > file.sha1sum\n"
  43. "\t%s -c file.sha1sum\n"
  44. "\t%s -a sha1 -a sha256 -a sha512-256 file > file.hashsum\n"
  45. "\t%s -c file.hashsum\n\n"
  46. "Algorithms:\n\t", hashsum, hashsum, hashsum, hashsum, hashsum);
  47. w = 0;
  48. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  49. w += fprintf(o, "%-14s", hash_descriptor[x].name);
  50. if (w >= 70) {
  51. fprintf(o, "\n\t");
  52. w = 0;
  53. }
  54. }
  55. if (w != 0) fprintf(o, "\n");
  56. exit(status);
  57. }
  58. static void printf_hex(unsigned char* hash_buffer, unsigned long w)
  59. {
  60. unsigned long x;
  61. for (x = 0; x < w; x++) {
  62. printf("%02x",hash_buffer[x]);
  63. }
  64. }
  65. static void check_file(int argn, int argc, char **argv)
  66. {
  67. int err, failed = 0, invalid = 0;
  68. unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
  69. char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
  70. /* iterate through all files */
  71. while(argn < argc) {
  72. char* s;
  73. FILE* f = fopen(argv[argn], "rb");
  74. if(f == NULL) {
  75. int n = snprintf(buf, sizeof(buf), "%s: %s", hashsum, argv[argn]);
  76. if (n > 0 && n < (int)sizeof(buf))
  77. perror(buf);
  78. else
  79. perror(argv[argn]);
  80. exit(EXIT_FAILURE);
  81. }
  82. /* read the file line by line */
  83. while((s = fgets(buf, sizeof(buf), f)) != NULL)
  84. {
  85. int tries, n;
  86. unsigned long hash_len, w, x;
  87. char* space = strstr(s, " ");
  88. /* skip lines with comments */
  89. if (buf[0] == '#') continue;
  90. if (space == NULL) {
  91. fprintf(stderr, "%s: no properly formatted checksum lines found\n", hashsum);
  92. goto ERR;
  93. }
  94. hash_len = space - s;
  95. hash_len /= 2;
  96. if (hash_len > sizeof(should_buffer)) {
  97. fprintf(stderr, "%s: hash too long\n", hashsum);
  98. goto ERR;
  99. }
  100. /* convert the hex-string back to binary */
  101. for (x = 0; x < hash_len; ++x) {
  102. should_buffer[x] = HEXOF(s[x*2]) << 4 | HEXOF(s[x*2 + 1]);
  103. }
  104. space++;
  105. if (*space != '*') {
  106. fprintf(stderr, "%s: unsupported input mode '%c'\n", hashsum, *space);
  107. goto ERR;
  108. }
  109. space++;
  110. for (n = 0; n < (buf + sizeof(buf)) - space; ++n) {
  111. if(iscntrl((int)space[n])) {
  112. space[n] = '\0';
  113. break;
  114. }
  115. }
  116. /* try all hash algorithms that have the appropriate hash size */
  117. tries = 0;
  118. for (x = 0; hash_descriptor[x].name != NULL; ++x) {
  119. if (hash_descriptor[x].hashsize == hash_len) {
  120. tries++;
  121. w = sizeof(is_buffer);
  122. if ((err = hash_file(x, space, is_buffer, &w)) != CRYPT_OK) {
  123. fprintf(stderr, "%s: File hash error: %s: %s\n", hashsum, space, error_to_string(err));
  124. ERR:
  125. fclose(f);
  126. exit(EXIT_FAILURE);
  127. }
  128. if(XMEMCMP(should_buffer, is_buffer, w) == 0) {
  129. printf("%s: OK\n", space);
  130. break;
  131. }
  132. }
  133. } /* for */
  134. if (hash_descriptor[x].name == NULL) {
  135. if(tries > 0) {
  136. printf("%s: FAILED\n", space);
  137. failed++;
  138. }
  139. else {
  140. invalid++;
  141. }
  142. }
  143. } /* while */
  144. fclose(f);
  145. if(invalid) {
  146. fprintf(stderr, "%s: WARNING: %d %s is improperly formatted\n", hashsum, invalid, invalid > 1?"lines":"line");
  147. }
  148. if(failed) {
  149. fprintf(stderr, "%s: WARNING: %d computed %s did NOT match\n", hashsum, failed, failed > 1?"checksums":"checksum");
  150. }
  151. argn++;
  152. }
  153. exit(failed == 0 && invalid == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  154. }
  155. int main(int argc, char **argv)
  156. {
  157. int idxs[TAB_SIZE], idx, check, y, z, err, argn;
  158. unsigned long w, x;
  159. unsigned char hash_buffer[MAXBLOCKSIZE];
  160. hashsum = strdup(basename(argv[0]));
  161. atexit(cleanup);
  162. /* You need to register algorithms before using them */
  163. register_all_ciphers();
  164. register_all_hashes();
  165. if (argc > 1 && strstr(argv[1], "-h")) {
  166. die(EXIT_SUCCESS);
  167. }
  168. if (argc < 3) {
  169. die(EXIT_FAILURE);
  170. }
  171. for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
  172. idxs[x] = -2;
  173. }
  174. argn = 1;
  175. check = 0;
  176. idx = 0;
  177. while(argn < argc){
  178. if(strcmp("-a", argv[argn]) == 0) {
  179. argn++;
  180. if(argn < argc) {
  181. idxs[idx] = find_hash(argv[argn]);
  182. if (idxs[idx] == -1) {
  183. struct {
  184. const char* is;
  185. const char* should;
  186. } shasum_compat[] =
  187. {
  188. #ifdef LTC_SHA1
  189. { "1", sha1_desc.name },
  190. #endif
  191. #ifdef LTC_SHA224
  192. { "224", sha224_desc.name },
  193. #endif
  194. #ifdef LTC_SHA256
  195. { "256", sha256_desc.name },
  196. #endif
  197. #ifdef LTC_SHA384
  198. { "384", sha384_desc.name },
  199. #endif
  200. #ifdef LTC_SHA512
  201. { "512", sha512_desc.name },
  202. #endif
  203. #ifdef LTC_SHA512_224
  204. { "512224", sha512_224_desc.name },
  205. #endif
  206. #ifdef LTC_SHA512_256
  207. { "512256", sha512_256_desc.name },
  208. #endif
  209. { NULL, NULL }
  210. };
  211. for (x = 0; shasum_compat[x].is != NULL; ++x) {
  212. if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) {
  213. idxs[idx] = find_hash(shasum_compat[x].should);
  214. break;
  215. }
  216. }
  217. }
  218. if (idxs[idx] == -1) {
  219. fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
  220. die(EXIT_FAILURE);
  221. }
  222. idx++;
  223. if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
  224. fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
  225. die(EXIT_FAILURE);
  226. }
  227. argn++;
  228. continue;
  229. }
  230. else {
  231. die(EXIT_FAILURE);
  232. }
  233. }
  234. if(strcmp("-c", argv[argn]) == 0) {
  235. check = 1;
  236. argn++;
  237. continue;
  238. }
  239. break;
  240. }
  241. if (check == 1) {
  242. check_file(argn, argc, argv);
  243. }
  244. if (argc == argn) {
  245. w = sizeof(hash_buffer);
  246. if ((err = hash_filehandle(idxs[0], stdin, hash_buffer, &w)) != CRYPT_OK) {
  247. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  248. return EXIT_FAILURE;
  249. } else {
  250. for (x = 0; x < w; x++) {
  251. printf("%02x",hash_buffer[x]);
  252. }
  253. printf(" *-\n");
  254. }
  255. } else {
  256. for (z = argn; z < argc; z++) {
  257. for (y = 0; y < idx; ++y) {
  258. w = sizeof(hash_buffer);
  259. if ((err = hash_file(idxs[y],argv[z],hash_buffer,&w)) != CRYPT_OK) {
  260. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  261. return EXIT_FAILURE;
  262. } else {
  263. printf_hex(hash_buffer, w);
  264. printf(" *%s\n", argv[z]);
  265. }
  266. }
  267. }
  268. }
  269. return EXIT_SUCCESS;
  270. }