test_crypto_openssl.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CRYPTO_PRIVATE
  7. #include "crypto.h"
  8. #include "test.h"
  9. #include <openssl/evp.h>
  10. #include <openssl/rand.h>
  11. #include "compat_openssl.h"
  12. /* Test for rectifying openssl RAND engine. */
  13. static void
  14. test_crypto_rng_engine(void *arg)
  15. {
  16. (void)arg;
  17. RAND_METHOD dummy_method;
  18. memset(&dummy_method, 0, sizeof(dummy_method));
  19. /* We should be a no-op if we're already on RAND_OpenSSL */
  20. tt_int_op(0, ==, crypto_force_rand_ssleay());
  21. tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
  22. /* We should correct the method if it's a dummy. */
  23. RAND_set_rand_method(&dummy_method);
  24. #ifdef LIBRESSL_VERSION_NUMBER
  25. /* On libressl, you can't override the RNG. */
  26. tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
  27. tt_int_op(0, ==, crypto_force_rand_ssleay());
  28. #else
  29. tt_assert(RAND_get_rand_method() == &dummy_method);
  30. tt_int_op(1, ==, crypto_force_rand_ssleay());
  31. #endif
  32. tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
  33. /* Make sure we aren't calling dummy_method */
  34. crypto_rand((void *) &dummy_method, sizeof(dummy_method));
  35. crypto_rand((void *) &dummy_method, sizeof(dummy_method));
  36. done:
  37. ;
  38. }
  39. struct testcase_t crypto_openssl_tests[] = {
  40. { "rng_engine", test_crypto_rng_engine, TT_FORK, NULL, NULL },
  41. END_OF_TESTCASES
  42. };