common.c 3.9 KB

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