hashsum.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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, "usage: %s -a algorithm [-c] [file...]\n\n", hashsum);
  37. fprintf(o, "\t-c\tCheck the hash(es) of the file(s) written in [file].\n");
  38. fprintf(o, "\t\t(-a not required)\n");
  39. fprintf(o, "\nAlgorithms:\n\t");
  40. w = 0;
  41. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  42. w += fprintf(o, "%-14s", hash_descriptor[x].name);
  43. if (w >= 70) {
  44. fprintf(o, "\n\t");
  45. w = 0;
  46. }
  47. }
  48. if (w != 0) fprintf(o, "\n");
  49. exit(status);
  50. }
  51. static void printf_hex(unsigned char* hash_buffer, unsigned long w)
  52. {
  53. unsigned long x;
  54. for (x = 0; x < w; x++) {
  55. printf("%02x",hash_buffer[x]);
  56. }
  57. }
  58. static void check_file(int argn, int argc, char **argv)
  59. {
  60. int err, failed, invalid;
  61. unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
  62. char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
  63. /* iterate through all files */
  64. while(argn < argc) {
  65. char* s;
  66. FILE* f = fopen(argv[argn], "rb");
  67. if(f == NULL) {
  68. int n = snprintf(buf, sizeof(buf), "%s: %s", hashsum, argv[argn]);
  69. if (n > 0 && n < (int)sizeof(buf))
  70. perror(buf);
  71. else
  72. perror(argv[argn]);
  73. exit(EXIT_FAILURE);
  74. }
  75. failed = 0;
  76. invalid = 0;
  77. /* read the file line by line */
  78. while((s = fgets(buf, sizeof(buf), f)) != NULL)
  79. {
  80. int tries, n;
  81. unsigned long hash_len, w, x;
  82. char* space = strstr(s, " ");
  83. /* skip lines with comments */
  84. if (buf[0] == '#') continue;
  85. if (space == NULL) {
  86. fprintf(stderr, "%s: no properly formatted checksum lines found\n", hashsum);
  87. goto ERR;
  88. }
  89. hash_len = space - s;
  90. hash_len /= 2;
  91. if (hash_len > sizeof(should_buffer)) {
  92. fprintf(stderr, "%s: hash too long\n", hashsum);
  93. goto ERR;
  94. }
  95. /* convert the hex-string back to binary */
  96. for (x = 0; x < hash_len; ++x) {
  97. should_buffer[x] = HEXOF(s[x*2]) << 4 | HEXOF(s[x*2 + 1]);
  98. }
  99. space++;
  100. if (*space != '*') {
  101. fprintf(stderr, "%s: unsupported input mode '%c'\n", hashsum, *space);
  102. goto ERR;
  103. }
  104. space++;
  105. for (n = 0; n < (buf + sizeof(buf)) - space; ++n) {
  106. if(iscntrl((int)space[n])) {
  107. space[n] = '\0';
  108. break;
  109. }
  110. }
  111. /* try all hash algorithms that have the appropriate hash size */
  112. tries = 0;
  113. for (x = 0; hash_descriptor[x].name != NULL; ++x) {
  114. if (hash_descriptor[x].hashsize == hash_len) {
  115. tries++;
  116. w = sizeof(is_buffer);
  117. if ((err = hash_file(x, space, is_buffer, &w)) != CRYPT_OK) {
  118. fprintf(stderr, "%s: File hash error: %s: %s\n", hashsum, space, error_to_string(err));
  119. ERR:
  120. fclose(f);
  121. exit(EXIT_FAILURE);
  122. }
  123. if(XMEMCMP(should_buffer, is_buffer, w) == 0) {
  124. printf("%s: OK\n", space);
  125. break;
  126. }
  127. }
  128. } /* for */
  129. if (hash_descriptor[x].name == NULL) {
  130. if(tries > 0) {
  131. printf("%s: FAILED\n", space);
  132. failed++;
  133. }
  134. else {
  135. invalid++;
  136. }
  137. }
  138. } /* while */
  139. fclose(f);
  140. if(invalid) {
  141. fprintf(stderr, "%s: WARNING: %d %s is improperly formatted\n", hashsum, invalid, invalid > 1?"lines":"line");
  142. }
  143. if(failed) {
  144. fprintf(stderr, "%s: WARNING: %d computed %s did NOT match\n", hashsum, failed, failed > 1?"checksums":"checksum");
  145. }
  146. argn++;
  147. }
  148. exit(EXIT_SUCCESS);
  149. }
  150. int main(int argc, char **argv)
  151. {
  152. int idxs[TAB_SIZE], idx, check, y, z, err, argn;
  153. unsigned long w, x;
  154. unsigned char hash_buffer[MAXBLOCKSIZE];
  155. hashsum = strdup(basename(argv[0]));
  156. atexit(cleanup);
  157. /* You need to register algorithms before using them */
  158. register_all_ciphers();
  159. register_all_hashes();
  160. if (argc > 1 && (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)) {
  161. die(EXIT_SUCCESS);
  162. }
  163. if (argc < 3) {
  164. die(EXIT_FAILURE);
  165. }
  166. for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
  167. idxs[x] = -2;
  168. }
  169. argn = 1;
  170. check = 0;
  171. idx = 0;
  172. while(argn < argc){
  173. if(strcmp("-a", argv[argn]) == 0) {
  174. argn++;
  175. if(argn < argc) {
  176. idxs[idx] = find_hash(argv[argn]);
  177. if (idxs[idx] == -1) {
  178. struct {
  179. const char* is;
  180. const char* should;
  181. } shasum_compat[] =
  182. {
  183. #ifdef LTC_SHA1
  184. { "1", sha1_desc.name },
  185. #endif
  186. #ifdef LTC_SHA224
  187. { "224", sha224_desc.name },
  188. #endif
  189. #ifdef LTC_SHA256
  190. { "256", sha256_desc.name },
  191. #endif
  192. #ifdef LTC_SHA384
  193. { "384", sha384_desc.name },
  194. #endif
  195. #ifdef LTC_SHA512
  196. { "512", sha512_desc.name },
  197. #endif
  198. #ifdef LTC_SHA512_224
  199. { "512224", sha512_224_desc.name },
  200. #endif
  201. #ifdef LTC_SHA512_256
  202. { "512256", sha512_256_desc.name },
  203. #endif
  204. { NULL, NULL }
  205. };
  206. for (x = 0; shasum_compat[x].is != NULL; ++x) {
  207. if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) {
  208. idxs[idx] = find_hash(shasum_compat[x].should);
  209. break;
  210. }
  211. }
  212. }
  213. if (idxs[idx] == -1) {
  214. fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
  215. die(EXIT_FAILURE);
  216. }
  217. idx++;
  218. if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
  219. fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
  220. die(EXIT_FAILURE);
  221. }
  222. argn++;
  223. continue;
  224. }
  225. else {
  226. die(EXIT_FAILURE);
  227. }
  228. }
  229. if(strcmp("-c", argv[argn]) == 0) {
  230. check = 1;
  231. argn++;
  232. continue;
  233. }
  234. break;
  235. }
  236. if (check == 1) {
  237. check_file(argn, argc, argv);
  238. }
  239. if (argc == argn) {
  240. w = sizeof(hash_buffer);
  241. if ((err = hash_filehandle(idxs[0], stdin, hash_buffer, &w)) != CRYPT_OK) {
  242. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  243. return EXIT_FAILURE;
  244. } else {
  245. for (x = 0; x < w; x++) {
  246. printf("%02x",hash_buffer[x]);
  247. }
  248. printf(" *-\n");
  249. }
  250. } else {
  251. for (z = argn; z < argc; z++) {
  252. for (y = 0; y < idx; ++y) {
  253. w = sizeof(hash_buffer);
  254. if ((err = hash_file(idxs[y],argv[z],hash_buffer,&w)) != CRYPT_OK) {
  255. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  256. return EXIT_FAILURE;
  257. } else {
  258. printf_hex(hash_buffer, w);
  259. printf(" *%s\n", argv[z]);
  260. }
  261. }
  262. }
  263. }
  264. return EXIT_SUCCESS;
  265. }