crypto_init.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_init.c
  8. *
  9. * \brief Initialize and shut down Tor's crypto library and subsystem.
  10. **/
  11. #include "orconfig.h"
  12. #include "lib/crypt_ops/crypto_init.h"
  13. #include "lib/crypt_ops/crypto_curve25519.h"
  14. #include "lib/crypt_ops/crypto_dh.h"
  15. #include "lib/crypt_ops/crypto_ed25519.h"
  16. #include "lib/crypt_ops/crypto_openssl_mgt.h"
  17. #include "lib/crypt_ops/crypto_nss_mgt.h"
  18. #include "lib/crypt_ops/crypto_rand.h"
  19. #include "siphash.h"
  20. /** Boolean: has OpenSSL's crypto been initialized? */
  21. static int crypto_early_initialized_ = 0;
  22. /** Boolean: has OpenSSL's crypto been initialized? */
  23. static int crypto_global_initialized_ = 0;
  24. static int have_seeded_siphash = 0;
  25. /** Set up the siphash key if we haven't already done so. */
  26. int
  27. crypto_init_siphash_key(void)
  28. {
  29. struct sipkey key;
  30. if (have_seeded_siphash)
  31. return 0;
  32. crypto_rand((char*) &key, sizeof(key));
  33. siphash_set_global_key(&key);
  34. have_seeded_siphash = 1;
  35. return 0;
  36. }
  37. /** Initialize the crypto library. Return 0 on success, -1 on failure.
  38. */
  39. int
  40. crypto_early_init(void)
  41. {
  42. if (!crypto_early_initialized_) {
  43. crypto_early_initialized_ = 1;
  44. #ifdef ENABLE_OPENSSL
  45. crypto_openssl_early_init();
  46. #endif
  47. #ifdef ENABLE_NSS
  48. crypto_nss_early_init();
  49. #endif
  50. if (crypto_seed_rng() < 0)
  51. return -1;
  52. if (crypto_init_siphash_key() < 0)
  53. return -1;
  54. curve25519_init();
  55. ed25519_init();
  56. }
  57. return 0;
  58. }
  59. /** Initialize the crypto library. Return 0 on success, -1 on failure.
  60. */
  61. int
  62. crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
  63. {
  64. if (!crypto_global_initialized_) {
  65. if (crypto_early_init() < 0)
  66. return -1;
  67. crypto_global_initialized_ = 1;
  68. #ifdef ENABLE_OPENSSL
  69. if (crypto_openssl_late_init(useAccel, accelName, accelDir) < 0)
  70. return -1;
  71. #endif
  72. #ifdef ENABLE_NSS
  73. if (crypto_nss_late_init() < 0)
  74. return -1;
  75. #endif
  76. }
  77. return 0;
  78. }
  79. /** Free crypto resources held by this thread. */
  80. void
  81. crypto_thread_cleanup(void)
  82. {
  83. #ifdef ENABLE_OPENSSL
  84. crypto_openssl_thread_cleanup();
  85. #endif
  86. }
  87. /**
  88. * Uninitialize the crypto library. Return 0 on success. Does not detect
  89. * failure.
  90. */
  91. int
  92. crypto_global_cleanup(void)
  93. {
  94. crypto_dh_free_all();
  95. #ifdef ENABLE_OPENSSL
  96. crypto_openssl_global_cleanup();
  97. #endif
  98. #ifdef ENABLE_NSS
  99. crypto_nss_global_cleanup();
  100. #endif
  101. crypto_early_initialized_ = 0;
  102. crypto_global_initialized_ = 0;
  103. have_seeded_siphash = 0;
  104. siphash_unset_global_key();
  105. return 0;
  106. }
  107. /** Run operations that the crypto library requires to be happy again
  108. * after forking. */
  109. void
  110. crypto_postfork(void)
  111. {
  112. #ifdef ENABLE_NSS
  113. crypto_nss_postfork();
  114. #endif
  115. }