tor  0.4.1.0-alpha-dev
crypto_init.c
Go to the documentation of this file.
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 
13 #include "orconfig.h"
14 
15 #define CRYPTO_PRIVATE
16 
18 
25 #include "lib/crypt_ops/crypto_sys.h"
26 
27 #include "lib/subsys/subsys.h"
28 
29 #include "ext/siphash.h"
30 
33 
36 
37 static int have_seeded_siphash = 0;
38 
40 int
42 {
43  struct sipkey key;
44  if (have_seeded_siphash)
45  return 0;
46 
47  crypto_rand((char*) &key, sizeof(key));
48  siphash_set_global_key(&key);
49  have_seeded_siphash = 1;
50  return 0;
51 }
52 
55 int
57 {
59 
61 
62 #ifdef ENABLE_OPENSSL
64 #endif
65 #ifdef ENABLE_NSS
66  crypto_nss_early_init(0);
67 #endif
68 
69  if (crypto_seed_rng() < 0)
70  return -1;
71  if (crypto_init_siphash_key() < 0)
72  return -1;
73 
75 
77  ed25519_init();
78  }
79  return 0;
80 }
81 
84 int
85 crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
86 {
88  if (crypto_early_init() < 0)
89  return -1;
90 
92 
93  crypto_dh_init();
94 
95 #ifdef ENABLE_OPENSSL
96  if (crypto_openssl_late_init(useAccel, accelName, accelDir) < 0)
97  return -1;
98 #else
99  (void)useAccel;
100  (void)accelName;
101  (void)accelDir;
102 #endif
103 #ifdef ENABLE_NSS
104  if (crypto_nss_late_init() < 0)
105  return -1;
106 #endif
107  }
108  return 0;
109 }
110 
112 void
114 {
115 #ifdef ENABLE_OPENSSL
117 #endif
119 }
120 
125 int
127 {
128  crypto_dh_free_all();
129 
130 #ifdef ENABLE_OPENSSL
132 #endif
133 #ifdef ENABLE_NSS
134  crypto_nss_global_cleanup();
135 #endif
136 
138 
141  have_seeded_siphash = 0;
142  siphash_unset_global_key();
143 
144  return 0;
145 }
146 
149 void
151 {
152 #ifdef ENABLE_NSS
153  crypto_nss_prefork();
154 #endif
155 }
156 
159 void
161 {
162 #ifdef ENABLE_NSS
163  crypto_nss_postfork();
164 #endif
165 }
166 
168 const char *
170 {
171 #ifdef ENABLE_OPENSSL
172  return "OpenSSL";
173 #endif
174 #ifdef ENABLE_NSS
175  return "NSS";
176 #endif
177 }
178 
181 const char *
183 {
184 #ifdef ENABLE_OPENSSL
185  return crypto_openssl_get_version_str();
186 #endif
187 #ifdef ENABLE_NSS
188  return crypto_nss_get_version_str();
189 #endif
190 }
191 
194 const char *
196 {
197 #ifdef ENABLE_OPENSSL
198  return crypto_openssl_get_header_version_str();
199 #endif
200 #ifdef ENABLE_NSS
201  return crypto_nss_get_header_version_str();
202 #endif
203 }
204 
206 int
208 {
209 #ifdef ENABLE_NSS
210  return 1;
211 #else
212  return 0;
213 #endif
214 }
215 
216 static int
217 subsys_crypto_initialize(void)
218 {
219  if (crypto_early_init() < 0)
220  return -1;
221  crypto_dh_init();
222  return 0;
223 }
224 
225 static void
226 subsys_crypto_shutdown(void)
227 {
229 }
230 
231 static void
232 subsys_crypto_prefork(void)
233 {
234  crypto_prefork();
235 }
236 
237 static void
238 subsys_crypto_postfork(void)
239 {
240  crypto_postfork();
241 }
242 
243 static void
244 subsys_crypto_thread_cleanup(void)
245 {
247 }
248 
249 const struct subsys_fns_t sys_crypto = {
250  .name = "crypto",
251  .supported = true,
252  .level = -60,
253  .initialize = subsys_crypto_initialize,
254  .shutdown = subsys_crypto_shutdown,
255  .prefork = subsys_crypto_prefork,
256  .postfork = subsys_crypto_postfork,
257  .thread_cleanup = subsys_crypto_thread_cleanup,
258 };
int crypto_init_siphash_key(void)
Definition: crypto_init.c:41
Common functions for using (pseudo-)random number generators.
Headers for crypto_dh.c.
void crypto_rand_fast_shutdown(void)
int crypto_early_init(void)
Definition: crypto_init.c:56
void crypto_openssl_early_init(void)
void crypto_rand_fast_init(void)
Headers for crypto_openssl_mgt.c.
void crypto_openssl_global_cleanup(void)
void crypto_openssl_thread_cleanup(void)
int tor_is_using_nss(void)
Definition: crypto_init.c:207
void crypto_prefork(void)
Definition: crypto_init.c:150
const char * crypto_get_library_version_string(void)
Definition: crypto_init.c:182
void crypto_thread_cleanup(void)
Definition: crypto_init.c:113
static int crypto_early_initialized_
Definition: crypto_init.c:32
int crypto_global_cleanup(void)
Definition: crypto_init.c:126
int crypto_openssl_late_init(int useAccel, const char *accelName, const char *accelDir)
Header for crypto_ed25519.c.
static int crypto_global_initialized_
Definition: crypto_init.c:35
const char * crypto_get_header_version_string(void)
Definition: crypto_init.c:195
const char * name
Definition: subsys.h:28
void crypto_postfork(void)
Definition: crypto_init.c:160
int crypto_seed_rng(void)
Definition: crypto_rand.c:451
void curve25519_init(void)
Header for crypto_curve25519.c.
int crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
Definition: crypto_init.c:85
Headers for crypto_init.c.
Headers for crypto_nss_mgt.c.
void destroy_thread_fast_rng(void)
const char * crypto_get_library_name(void)
Definition: crypto_init.c:169