common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include "common.h"
  4. /**
  5. @file common.c
  6. Steffen Jaeckel
  7. */
  8. void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm)
  9. {
  10. if (res != CRYPT_OK) {
  11. fprintf(stderr, "%s (%d)%s%s\n%s:%d:%s\n",
  12. error_to_string(res), res,
  13. (algorithm ? " - " : ""), (algorithm ? algorithm : ""),
  14. file, line, cmd);
  15. if (res != CRYPT_NOP) {
  16. exit(EXIT_FAILURE);
  17. }
  18. }
  19. }
  20. void print_hex(const char* what, const void* v, const unsigned long l)
  21. {
  22. const unsigned char* p = v;
  23. unsigned long x, y = 0, z;
  24. fprintf(stderr, "%s contents: \n", what);
  25. for (x = 0; x < l; ) {
  26. fprintf(stderr, "%02X ", p[x]);
  27. if (!(++x % 16) || x == l) {
  28. if((x % 16) != 0) {
  29. z = 16 - (x % 16);
  30. if(z >= 8)
  31. fprintf(stderr, " ");
  32. for (; z != 0; --z) {
  33. fprintf(stderr, " ");
  34. }
  35. }
  36. fprintf(stderr, " | ");
  37. for(; y < x; y++) {
  38. if((y % 8) == 0)
  39. fprintf(stderr, " ");
  40. if(isgraph(p[y]))
  41. fprintf(stderr, "%c", p[y]);
  42. else
  43. fprintf(stderr, ".");
  44. }
  45. fprintf(stderr, "\n");
  46. }
  47. else if((x % 8) == 0) {
  48. fprintf(stderr, " ");
  49. }
  50. }
  51. }
  52. int do_compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
  53. {
  54. if (compare_testvector(is, is_len, should, should_len, what, which) == 0) {
  55. return CRYPT_OK;
  56. } else {
  57. return CRYPT_FAIL_TESTVECTOR;
  58. }
  59. }
  60. #ifdef LTC_TEST_READDIR
  61. #include <sys/stat.h>
  62. #include <sys/types.h>
  63. #include <dirent.h>
  64. static off_t fsize(const char *filename)
  65. {
  66. struct stat st;
  67. if (stat(filename, &st) == 0) return st.st_size;
  68. return -1;
  69. }
  70. int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
  71. {
  72. DIR *d = opendir(path);
  73. struct dirent *de;
  74. char fname[PATH_MAX];
  75. void* buf = NULL;
  76. FILE *f = NULL;
  77. off_t fsz;
  78. unsigned long sz;
  79. int err = CRYPT_FILE_NOTFOUND;
  80. if (d == NULL)
  81. return CRYPT_FILE_NOTFOUND;
  82. while((de = readdir(d)) != NULL) {
  83. fname[0] = '\0';
  84. if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, "README.txt") == 0)
  85. continue;
  86. strcat(fname, path);
  87. strcat(fname, "/");
  88. strcat(fname, de->d_name);
  89. fsz = fsize(fname);
  90. if (fsz == -1) {
  91. err = CRYPT_FILE_NOTFOUND;
  92. break;
  93. }
  94. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  95. fprintf(stderr, "%s: Try to process %s\n", test, fname);
  96. #endif
  97. f = fopen(fname, "rb");
  98. sz = fsz;
  99. buf = XMALLOC(fsz);
  100. if (fread(buf, 1, sz, f) != sz) {
  101. err = CRYPT_ERROR;
  102. break;
  103. }
  104. err = process(buf, sz, ctx);
  105. if (err == CRYPT_NOP) {
  106. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  107. fprintf(stderr, "%s: Skip: %s\n", test, fname);
  108. #endif
  109. break;
  110. } else if (err != CRYPT_OK) {
  111. #if defined(LTC_TEST_DBG)
  112. fprintf(stderr, "%s: Test %s failed (cause: %s).\n\n", test, fname, error_to_string(err));
  113. #else
  114. LTC_UNUSED_PARAM(test);
  115. #endif
  116. break;
  117. }
  118. if ((err != CRYPT_NOP) && (cleanup != NULL)) {
  119. cleanup(ctx);
  120. }
  121. XFREE(buf);
  122. buf = NULL;
  123. fclose(f);
  124. f = NULL;
  125. }
  126. if (buf != NULL) XFREE(buf);
  127. if (f != NULL) fclose(f);
  128. closedir(d);
  129. return err;
  130. }
  131. #endif
  132. prng_state yarrow_prng;