crypto_init.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "lib/crypt_ops/crypto_sys.h"
  20. #include "lib/subsys/subsys.h"
  21. #include "ext/siphash.h"
  22. /** Boolean: has our crypto library been initialized? (early phase) */
  23. static int crypto_early_initialized_ = 0;
  24. /** Boolean: has our crypto library been initialized? (late phase) */
  25. static int crypto_global_initialized_ = 0;
  26. static int have_seeded_siphash = 0;
  27. /** Set up the siphash key if we haven't already done so. */
  28. int
  29. crypto_init_siphash_key(void)
  30. {
  31. struct sipkey key;
  32. if (have_seeded_siphash)
  33. return 0;
  34. crypto_rand((char*) &key, sizeof(key));
  35. siphash_set_global_key(&key);
  36. have_seeded_siphash = 1;
  37. return 0;
  38. }
  39. /** Initialize the crypto library. Return 0 on success, -1 on failure.
  40. */
  41. int
  42. crypto_early_init(void)
  43. {
  44. if (!crypto_early_initialized_) {
  45. crypto_early_initialized_ = 1;
  46. #ifdef ENABLE_OPENSSL
  47. crypto_openssl_early_init();
  48. #endif
  49. #ifdef ENABLE_NSS
  50. crypto_nss_early_init(0);
  51. #endif
  52. if (crypto_seed_rng() < 0)
  53. return -1;
  54. if (crypto_init_siphash_key() < 0)
  55. return -1;
  56. curve25519_init();
  57. ed25519_init();
  58. }
  59. return 0;
  60. }
  61. /** Initialize the crypto library. Return 0 on success, -1 on failure.
  62. */
  63. int
  64. crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
  65. {
  66. if (!crypto_global_initialized_) {
  67. if (crypto_early_init() < 0)
  68. return -1;
  69. crypto_global_initialized_ = 1;
  70. crypto_dh_init();
  71. #ifdef ENABLE_OPENSSL
  72. if (crypto_openssl_late_init(useAccel, accelName, accelDir) < 0)
  73. return -1;
  74. #else
  75. (void)useAccel;
  76. (void)accelName;
  77. (void)accelDir;
  78. #endif
  79. #ifdef ENABLE_NSS
  80. if (crypto_nss_late_init() < 0)
  81. return -1;
  82. #endif
  83. }
  84. return 0;
  85. }
  86. /** Free crypto resources held by this thread. */
  87. void
  88. crypto_thread_cleanup(void)
  89. {
  90. #ifdef ENABLE_OPENSSL
  91. crypto_openssl_thread_cleanup();
  92. #endif
  93. }
  94. /**
  95. * Uninitialize the crypto library. Return 0 on success. Does not detect
  96. * failure.
  97. */
  98. int
  99. crypto_global_cleanup(void)
  100. {
  101. crypto_dh_free_all();
  102. #ifdef ENABLE_OPENSSL
  103. crypto_openssl_global_cleanup();
  104. #endif
  105. #ifdef ENABLE_NSS
  106. crypto_nss_global_cleanup();
  107. #endif
  108. crypto_early_initialized_ = 0;
  109. crypto_global_initialized_ = 0;
  110. have_seeded_siphash = 0;
  111. siphash_unset_global_key();
  112. return 0;
  113. }
  114. /** Run operations that the crypto library requires to be happy again
  115. * after forking. */
  116. void
  117. crypto_prefork(void)
  118. {
  119. #ifdef ENABLE_NSS
  120. crypto_nss_prefork();
  121. #endif
  122. }
  123. /** Run operations that the crypto library requires to be happy again
  124. * after forking. */
  125. void
  126. crypto_postfork(void)
  127. {
  128. #ifdef ENABLE_NSS
  129. crypto_nss_postfork();
  130. #endif
  131. }
  132. /** Return the name of the crypto library we're using. */
  133. const char *
  134. crypto_get_library_name(void)
  135. {
  136. #ifdef ENABLE_OPENSSL
  137. return "OpenSSL";
  138. #endif
  139. #ifdef ENABLE_NSS
  140. return "NSS";
  141. #endif
  142. }
  143. /** Return the version of the crypto library we are using, as given in the
  144. * library. */
  145. const char *
  146. crypto_get_library_version_string(void)
  147. {
  148. #ifdef ENABLE_OPENSSL
  149. return crypto_openssl_get_version_str();
  150. #endif
  151. #ifdef ENABLE_NSS
  152. return crypto_nss_get_version_str();
  153. #endif
  154. }
  155. /** Return the version of the crypto library we're using, as given in the
  156. * headers. */
  157. const char *
  158. crypto_get_header_version_string(void)
  159. {
  160. #ifdef ENABLE_OPENSSL
  161. return crypto_openssl_get_header_version_str();
  162. #endif
  163. #ifdef ENABLE_NSS
  164. return crypto_nss_get_header_version_str();
  165. #endif
  166. }
  167. /** Return true iff Tor is using the NSS library. */
  168. int
  169. tor_is_using_nss(void)
  170. {
  171. #ifdef ENABLE_NSS
  172. return 1;
  173. #else
  174. return 0;
  175. #endif
  176. }
  177. static int
  178. subsys_crypto_initialize(void)
  179. {
  180. if (crypto_early_init() < 0)
  181. return -1;
  182. crypto_dh_init();
  183. return 0;
  184. }
  185. static void
  186. subsys_crypto_shutdown(void)
  187. {
  188. crypto_global_cleanup();
  189. }
  190. static void
  191. subsys_crypto_prefork(void)
  192. {
  193. crypto_prefork();
  194. }
  195. static void
  196. subsys_crypto_postfork(void)
  197. {
  198. crypto_postfork();
  199. }
  200. static void
  201. subsys_crypto_thread_cleanup(void)
  202. {
  203. crypto_thread_cleanup();
  204. }
  205. const struct subsys_fns_t sys_crypto = {
  206. .name = "crypto",
  207. .supported = true,
  208. .level = -60,
  209. .initialize = subsys_crypto_initialize,
  210. .shutdown = subsys_crypto_shutdown,
  211. .prefork = subsys_crypto_prefork,
  212. .postfork = subsys_crypto_postfork,
  213. .thread_cleanup = subsys_crypto_thread_cleanup,
  214. };