common.c 4.5 KB

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