common.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #ifndef DEMOS_COMMON_H_
  4. #define DEMOS_COMMON_H_
  5. #include <tomcrypt.h>
  6. extern prng_state yarrow_prng;
  7. #if defined(LTC_MDSA)
  8. extern const unsigned char ltc_dsa_private_test_key[];
  9. extern const unsigned long ltc_dsa_private_test_key_sz;
  10. int dsa_key_cmp(const int should_type, const dsa_key *should, const dsa_key *is);
  11. #endif
  12. #if defined(LTC_MRSA)
  13. extern const unsigned char ltc_rsa_private_test_key[];
  14. extern const unsigned long ltc_rsa_private_test_key_sz;
  15. int rsa_key_cmp(const int should_type, const rsa_key *should, const rsa_key *is);
  16. #endif
  17. #if defined(LTC_MECC)
  18. extern const unsigned char ltc_ecc_long_pri_test_key[];
  19. extern const unsigned long ltc_ecc_long_pri_test_key_sz;
  20. int ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is);
  21. #endif
  22. #ifdef LTC_VERBOSE
  23. #define DO(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0)
  24. #define DOX(x, str) do { fprintf(stderr, "%s - %s:\n", #x, (str)); run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0)
  25. #define SHOULD_FAIL(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  26. #define SHOULD_FAIL_WITH(x, e) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  27. #define ENSURE(x) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  28. #define ENSUREX(x, str) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
  29. #else
  30. #define DO(x) do { run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0)
  31. #define DOX(x, str) do { run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0)
  32. #define SHOULD_FAIL(x) do { run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  33. #define SHOULD_FAIL_WITH(x, e) do { run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  34. #define ENSURE(x) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
  35. #define ENSUREX(x, str) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
  36. #endif
  37. #define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)
  38. #if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__)) && !defined(LTC_NO_FILE)
  39. #define LTC_TEST_READDIR
  40. typedef int (*dir_iter_cb)(const void *d, unsigned long l, void* ctx);
  41. typedef int (*dir_fiter_cb)(FILE *f, void* ctx);
  42. typedef void (*dir_cleanup_cb)(void* ctx);
  43. int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test);
  44. #endif
  45. void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm);
  46. void print_hex(const char* what, const void* v, const unsigned long l);
  47. 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);
  48. #endif /* DEMOS_COMMON_H_ */