common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. static DIR *s_opendir(const char *path, char *mypath, unsigned long l)
  71. {
  72. #ifdef CMAKE_SOURCE_DIR
  73. #define SOURCE_PREFIX CMAKE_SOURCE_DIR "/"
  74. #else
  75. #define SOURCE_PREFIX ""
  76. #endif
  77. DIR *d = NULL;
  78. int r = snprintf(mypath, l, "%s%s", SOURCE_PREFIX, path);
  79. if (r > 0 && (unsigned int)r < l) {
  80. d = opendir(mypath);
  81. }
  82. return d;
  83. }
  84. int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
  85. {
  86. char mypath[PATH_MAX];
  87. DIR *d = s_opendir(path, mypath, sizeof(mypath));
  88. struct dirent *de;
  89. char fname[PATH_MAX];
  90. void* buf = NULL;
  91. FILE *f = NULL;
  92. off_t fsz;
  93. unsigned long sz;
  94. int err = CRYPT_FILE_NOTFOUND;
  95. if (d == NULL)
  96. return CRYPT_FILE_NOTFOUND;
  97. while((de = readdir(d)) != NULL) {
  98. fname[0] = '\0';
  99. if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, "README.txt") == 0)
  100. continue;
  101. strcat(fname, mypath);
  102. strcat(fname, "/");
  103. strcat(fname, de->d_name);
  104. fsz = fsize(fname);
  105. if (fsz == -1) {
  106. err = CRYPT_FILE_NOTFOUND;
  107. break;
  108. }
  109. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  110. fprintf(stderr, "%s: Try to process %s\n", test, fname);
  111. #endif
  112. f = fopen(fname, "rb");
  113. sz = fsz;
  114. buf = XMALLOC(fsz);
  115. if (fread(buf, 1, sz, f) != sz) {
  116. err = CRYPT_ERROR;
  117. break;
  118. }
  119. err = process(buf, sz, ctx);
  120. if (err == CRYPT_NOP) {
  121. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  122. fprintf(stderr, "%s: Skip: %s\n", test, fname);
  123. #endif
  124. goto continue_loop;
  125. } else if (err != CRYPT_OK) {
  126. #if defined(LTC_TEST_DBG)
  127. fprintf(stderr, "%s: Test %s failed (cause: %s).\n\n", test, fname, error_to_string(err));
  128. #else
  129. LTC_UNUSED_PARAM(test);
  130. #endif
  131. break;
  132. }
  133. if ((err != CRYPT_NOP) && (cleanup != NULL)) {
  134. cleanup(ctx);
  135. }
  136. continue_loop:
  137. XFREE(buf);
  138. buf = NULL;
  139. fclose(f);
  140. f = NULL;
  141. }
  142. if (buf != NULL) XFREE(buf);
  143. if (f != NULL) fclose(f);
  144. closedir(d);
  145. return err;
  146. }
  147. #endif
  148. prng_state yarrow_prng;