tor  0.4.0.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-2018, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
13 #include "orconfig.h"
14 
16 
23 #include "lib/crypt_ops/crypto_sys.h"
24 
25 #include "lib/subsys/subsys.h"
26 
27 #include "ext/siphash.h"
28 
31 
34 
35 static int have_seeded_siphash = 0;
36 
38 int
40 {
41  struct sipkey key;
42  if (have_seeded_siphash)
43  return 0;
44 
45  crypto_rand((char*) &key, sizeof(key));
46  siphash_set_global_key(&key);
47  have_seeded_siphash = 1;
48  return 0;
49 }
50 
53 int
55 {
57 
59 
60 #ifdef ENABLE_OPENSSL
62 #endif
63 #ifdef ENABLE_NSS
64  crypto_nss_early_init(0);
65 #endif
66 
67  if (crypto_seed_rng() < 0)
68  return -1;
69  if (crypto_init_siphash_key() < 0)
70  return -1;
71 
73  ed25519_init();
74  }
75  return 0;
76 }
77 
80 int
81 crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
82 {
84  if (crypto_early_init() < 0)
85  return -1;
86 
88 
89  crypto_dh_init();
90 
91 #ifdef ENABLE_OPENSSL
92  if (crypto_openssl_late_init(useAccel, accelName, accelDir) < 0)
93  return -1;
94 #else
95  (void)useAccel;
96  (void)accelName;
97  (void)accelDir;
98 #endif
99 #ifdef ENABLE_NSS
100  if (crypto_nss_late_init() < 0)
101  return -1;
102 #endif
103  }
104  return 0;
105 }
106 
108 void
110 {
111 #ifdef ENABLE_OPENSSL
113 #endif
114 }
115 
120 int
122 {
123  crypto_dh_free_all();
124 
125 #ifdef ENABLE_OPENSSL
127 #endif
128 #ifdef ENABLE_NSS
129  crypto_nss_global_cleanup();
130 #endif
131 
134  have_seeded_siphash = 0;
135  siphash_unset_global_key();
136 
137  return 0;
138 }
139 
142 void
144 {
145 #ifdef ENABLE_NSS
146  crypto_nss_prefork();
147 #endif
148 }
149 
152 void
154 {
155 #ifdef ENABLE_NSS
156  crypto_nss_postfork();
157 #endif
158 }
159 
161 const char *
163 {
164 #ifdef ENABLE_OPENSSL
165  return "OpenSSL";
166 #endif
167 #ifdef ENABLE_NSS
168  return "NSS";
169 #endif
170 }
171 
174 const char *
176 {
177 #ifdef ENABLE_OPENSSL
178  return crypto_openssl_get_version_str();
179 #endif
180 #ifdef ENABLE_NSS
181  return crypto_nss_get_version_str();
182 #endif
183 }
184 
187 const char *
189 {
190 #ifdef ENABLE_OPENSSL
191  return crypto_openssl_get_header_version_str();
192 #endif
193 #ifdef ENABLE_NSS
194  return crypto_nss_get_header_version_str();
195 #endif
196 }
197 
199 int
201 {
202 #ifdef ENABLE_NSS
203  return 1;
204 #else
205  return 0;
206 #endif
207 }
208 
209 static int
210 subsys_crypto_initialize(void)
211 {
212  if (crypto_early_init() < 0)
213  return -1;
214  crypto_dh_init();
215  return 0;
216 }
217 
218 static void
219 subsys_crypto_shutdown(void)
220 {
222 }
223 
224 static void
225 subsys_crypto_prefork(void)
226 {
227  crypto_prefork();
228 }
229 
230 static void
231 subsys_crypto_postfork(void)
232 {
233  crypto_postfork();
234 }
235 
236 static void
237 subsys_crypto_thread_cleanup(void)
238 {
240 }
241 
242 const struct subsys_fns_t sys_crypto = {
243  .name = "crypto",
244  .supported = true,
245  .level = -60,
246  .initialize = subsys_crypto_initialize,
247  .shutdown = subsys_crypto_shutdown,
248  .prefork = subsys_crypto_prefork,
249  .postfork = subsys_crypto_postfork,
250  .thread_cleanup = subsys_crypto_thread_cleanup,
251 };
int crypto_init_siphash_key(void)
Definition: crypto_init.c:39
Common functions for using (pseudo-)random number generators.
Headers for crypto_dh.c.
int crypto_early_init(void)
Definition: crypto_init.c:54
void crypto_openssl_early_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:200
void crypto_prefork(void)
Definition: crypto_init.c:143
const char * crypto_get_library_version_string(void)
Definition: crypto_init.c:175
void crypto_thread_cleanup(void)
Definition: crypto_init.c:109
static int crypto_early_initialized_
Definition: crypto_init.c:30
int crypto_global_cleanup(void)
Definition: crypto_init.c:121
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:33
const char * crypto_get_header_version_string(void)
Definition: crypto_init.c:188
const char * name
Definition: subsys.h:28
void crypto_postfork(void)
Definition: crypto_init.c:153
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:81
Headers for crypto_init.c.
Headers for crypto_nss_mgt.c.
const char * crypto_get_library_name(void)
Definition: crypto_init.c:162