common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 + 1);
  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. ((unsigned char *)buf)[sz] = 0x0;
  100. err = process(buf, sz, ctx);
  101. out:
  102. XFREE(buf);
  103. return err;
  104. }
  105. int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test)
  106. {
  107. char mypath[PATH_MAX];
  108. DIR *d = s_opendir(path, mypath, sizeof(mypath));
  109. struct dirent *de;
  110. char fname[PATH_MAX];
  111. FILE *f = NULL;
  112. off_t fsz;
  113. int err = CRYPT_FILE_NOTFOUND;
  114. if (d == NULL)
  115. return CRYPT_FILE_NOTFOUND;
  116. while((de = readdir(d)) != NULL) {
  117. fname[0] = '\0';
  118. if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, "README.txt") == 0)
  119. continue;
  120. strcat(fname, mypath);
  121. strcat(fname, "/");
  122. strcat(fname, de->d_name);
  123. fsz = fsize(fname);
  124. if (fsz == 0)
  125. continue;
  126. if (fsz == -1) {
  127. err = CRYPT_FILE_NOTFOUND;
  128. break;
  129. }
  130. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  131. fprintf(stderr, "%s: Try to process %s\n", test, fname);
  132. #endif
  133. f = fopen(fname, "rb");
  134. if (iter) {
  135. err = s_read_and_process(f, fsz, ctx, iter);
  136. } else if (fiter) {
  137. err = fiter(f, ctx);
  138. } else {
  139. err = CRYPT_NOP;
  140. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  141. fprintf(stderr, "%s: No call-back set for %s\n", test, fname);
  142. #endif
  143. }
  144. if (err == CRYPT_NOP) {
  145. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  146. fprintf(stderr, "%s: Skip: %s\n", test, fname);
  147. #endif
  148. err = CRYPT_OK;
  149. goto continue_loop;
  150. } else if (err != CRYPT_OK) {
  151. #if defined(LTC_TEST_DBG)
  152. fprintf(stderr, "%s: Test %s failed (cause: %s).\n\n", test, fname, error_to_string(err));
  153. #else
  154. LTC_UNUSED_PARAM(test);
  155. #endif
  156. break;
  157. }
  158. if (cleanup != NULL) {
  159. cleanup(ctx);
  160. }
  161. continue_loop:
  162. fclose(f);
  163. f = NULL;
  164. }
  165. if (f != NULL) fclose(f);
  166. closedir(d);
  167. return err;
  168. }
  169. #endif
  170. prng_state yarrow_prng;