crypto_init.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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-2019, 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 our crypto library been initialized? (early phase) */
  21. static int crypto_early_initialized_ = 0;
  22. /** Boolean: has our crypto library been initialized? (late phase) */
  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(0);
  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. crypto_dh_init();
  69. #ifdef ENABLE_OPENSSL
  70. if (crypto_openssl_late_init(useAccel, accelName, accelDir) < 0)
  71. return -1;
  72. #else
  73. (void)useAccel;
  74. (void)accelName;
  75. (void)accelDir;
  76. #endif
  77. #ifdef ENABLE_NSS
  78. if (crypto_nss_late_init() < 0)
  79. return -1;
  80. #endif
  81. }
  82. return 0;
  83. }
  84. /** Free crypto resources held by this thread. */
  85. void
  86. crypto_thread_cleanup(void)
  87. {
  88. #ifdef ENABLE_OPENSSL
  89. crypto_openssl_thread_cleanup();
  90. #endif
  91. }
  92. /**
  93. * Uninitialize the crypto library. Return 0 on success. Does not detect
  94. * failure.
  95. */
  96. int
  97. crypto_global_cleanup(void)
  98. {
  99. crypto_dh_free_all();
  100. #ifdef ENABLE_OPENSSL
  101. crypto_openssl_global_cleanup();
  102. #endif
  103. #ifdef ENABLE_NSS
  104. crypto_nss_global_cleanup();
  105. #endif
  106. crypto_early_initialized_ = 0;
  107. crypto_global_initialized_ = 0;
  108. have_seeded_siphash = 0;
  109. siphash_unset_global_key();
  110. return 0;
  111. }
  112. /** Run operations that the crypto library requires to be happy again
  113. * after forking. */
  114. void
  115. crypto_prefork(void)
  116. {
  117. #ifdef ENABLE_NSS
  118. crypto_nss_prefork();
  119. #endif
  120. }
  121. /** Run operations that the crypto library requires to be happy again
  122. * after forking. */
  123. void
  124. crypto_postfork(void)
  125. {
  126. #ifdef ENABLE_NSS
  127. crypto_nss_postfork();
  128. #endif
  129. }
  130. /** Return the name of the crypto library we're using. */
  131. const char *
  132. crypto_get_library_name(void)
  133. {
  134. #ifdef ENABLE_OPENSSL
  135. return "OpenSSL";
  136. #endif
  137. #ifdef ENABLE_NSS
  138. return "NSS";
  139. #endif
  140. }
  141. /** Return the version of the crypto library we are using, as given in the
  142. * library. */
  143. const char *
  144. crypto_get_library_version_string(void)
  145. {
  146. #ifdef ENABLE_OPENSSL
  147. return crypto_openssl_get_version_str();
  148. #endif
  149. #ifdef ENABLE_NSS
  150. return crypto_nss_get_version_str();
  151. #endif
  152. }
  153. /** Return the version of the crypto library we're using, as given in the
  154. * headers. */
  155. const char *
  156. crypto_get_header_version_string(void)
  157. {
  158. #ifdef ENABLE_OPENSSL
  159. return crypto_openssl_get_header_version_str();
  160. #endif
  161. #ifdef ENABLE_NSS
  162. return crypto_nss_get_header_version_str();
  163. #endif
  164. }
  165. /** Return true iff Tor is using the NSS library. */
  166. int
  167. tor_is_using_nss(void)
  168. {
  169. #ifdef ENABLE_NSS
  170. return 1;
  171. #else
  172. return 0;
  173. #endif
  174. }