monotone

monotone Mtn Source Tree

Root/botan/init.cpp

1/*************************************************
2* Initialization Function Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/init.h>
7
8#include <botan/allocate.h>
9#include <botan/parsing.h>
10#include <botan/conf.h>
11#include <botan/look_add.h>
12#include <botan/mutex.h>
13#include <botan/rng.h>
14
15#include <botan/x931_rng.h>
16#include <botan/fips140.h>
17#include <botan/es_file.h>
18#include <map>
19
20#if defined(BOTAN_EXT_MUTEX_PTHREAD)
21 #include <botan/mux_pthr.h>
22#elif defined(BOTAN_EXT_MUTEX_WIN32)
23 #include <botan/mux_win.h>
24#elif defined(BOTAN_EXT_MUTEX_QT)
25 #include <botan/mux_qt.h>
26#endif
27
28#if defined(BOTAN_EXT_ALLOC_MMAP)
29 #include <botan/mmap_mem.h>
30#endif
31
32#if defined(BOTAN_EXT_TIMER_HARDWARE)
33 #include <botan/tm_hard.h>
34#elif defined(BOTAN_EXT_TIMER_POSIX)
35 #include <botan/tm_posix.h>
36#elif defined(BOTAN_EXT_TIMER_UNIX)
37 #include <botan/tm_unix.h>
38#elif defined(BOTAN_EXT_TIMER_WIN32)
39 #include <botan/tm_win32.h>
40#endif
41
42#if defined(BOTAN_EXT_ENGINE_AEP)
43 #include <botan/eng_aep.h>
44#endif
45
46#if defined(BOTAN_EXT_ENGINE_GNU_MP)
47 #include <botan/eng_gmp.h>
48#endif
49
50#if defined(BOTAN_EXT_ENGINE_OPENSSL)
51 #include <botan/eng_ossl.h>
52#endif
53
54#if defined(BOTAN_EXT_ENTROPY_SRC_AEP)
55 #include <botan/es_aep.h>
56#endif
57
58#if defined(BOTAN_EXT_ENTROPY_SRC_EGD)
59 #include <botan/es_egd.h>
60#endif
61
62#if defined(BOTAN_EXT_ENTROPY_SRC_UNIX)
63 #include <botan/es_unix.h>
64#endif
65
66#if defined(BOTAN_EXT_ENTROPY_SRC_BEOS)
67 #include <botan/es_beos.h>
68#endif
69
70#if defined(BOTAN_EXT_ENTROPY_SRC_CAPI)
71 #include <botan/es_capi.h>
72#endif
73
74#if defined(BOTAN_EXT_ENTROPY_SRC_WIN32)
75 #include <botan/es_win32.h>
76#endif
77
78#if defined(BOTAN_EXT_ENTROPY_SRC_FTW)
79 #include <botan/es_ftw.h>
80#endif
81
82namespace Botan {
83
84bool paranoid_memory_clearing = true;
85
86/*************************************************
87* Library Initialization *
88*************************************************/
89LibraryInitializer::LibraryInitializer(const std::string& arg_string)
90 {
91 Init::initialize(arg_string);
92 }
93
94/*************************************************
95* Library Shutdown *
96*************************************************/
97LibraryInitializer::~LibraryInitializer()
98 {
99 Init::deinitialize();
100 }
101
102namespace Init {
103
104namespace {
105
106/*************************************************
107* Register a mutex type, if possible *
108*************************************************/
109void set_mutex()
110 {
111#if defined(BOTAN_EXT_MUTEX_PTHREAD)
112 set_mutex_type(new Pthread_Mutex);
113#elif defined(BOTAN_EXT_MUTEX_WIN32)
114 set_mutex_type(new Win32_Mutex);
115#elif defined(BOTAN_EXT_MUTEX_QT)
116 set_mutex_type(new Qt_Mutex);
117#else
118 throw Exception("LibraryInitializer: thread safety impossible");
119#endif
120 }
121
122/*************************************************
123* Register a high resolution timer, if possible *
124*************************************************/
125void set_timer()
126 {
127#if defined(BOTAN_EXT_TIMER_HARDWARE)
128 set_timer_type(new Hardware_Timer);
129#elif defined(BOTAN_EXT_TIMER_POSIX)
130 set_timer_type(new POSIX_Timer);
131#elif defined(BOTAN_EXT_TIMER_UNIX)
132 set_timer_type(new Unix_Timer);
133#elif defined(BOTAN_EXT_TIMER_WIN32)
134 set_timer_type(new Win32_Timer);
135#endif
136 }
137
138/*************************************************
139* Register any usable entropy sources *
140*************************************************/
141void add_entropy_sources()
142 {
143 Global_RNG::add_es(new File_EntropySource);
144
145#if defined(BOTAN_EXT_ENTROPY_SRC_AEP)
146 Global_RNG::add_es(new AEP_EntropySource);
147#endif
148
149#if defined(BOTAN_EXT_ENTROPY_SRC_EGD)
150 Global_RNG::add_es(new EGD_EntropySource);
151#endif
152
153#if defined(BOTAN_EXT_ENTROPY_SRC_CAPI)
154 Global_RNG::add_es(new Win32_CAPI_EntropySource);
155#endif
156
157#if defined(BOTAN_EXT_ENTROPY_SRC_WIN32)
158 Global_RNG::add_es(new Win32_EntropySource);
159#endif
160
161#if defined(BOTAN_EXT_ENTROPY_SRC_UNIX)
162 Global_RNG::add_es(new Unix_EntropySource);
163#endif
164
165#if defined(BOTAN_EXT_ENTROPY_SRC_BEOS)
166 Global_RNG::add_es(new BeOS_EntropySource);
167#endif
168
169#if defined(BOTAN_EXT_ENTROPY_SRC_FTW)
170 Global_RNG::add_es(new FTW_EntropySource);
171#endif
172 }
173
174/*************************************************
175* Register a more secure allocator, if possible *
176*************************************************/
177void set_safe_allocator()
178 {
179#if defined(BOTAN_EXT_ALLOC_MMAP)
180 add_allocator_type("mmap", new MemoryMapping_Allocator);
181 set_default_allocator("mmap");
182#endif
183 }
184
185/*************************************************
186* Register any usable engines *
187*************************************************/
188void set_engines()
189 {
190#if defined(BOTAN_EXT_ENGINE_AEP)
191 Botan::Engine_Core::add_engine(new Botan::AEP_Engine);
192#endif
193
194#if defined(BOTAN_EXT_ENGINE_GNU_MP)
195 Botan::Engine_Core::add_engine(new Botan::GMP_Engine);
196#endif
197
198#if defined(BOTAN_EXT_ENGINE_OPENSSL)
199 Botan::Engine_Core::add_engine(new Botan::OpenSSL_Engine);
200#endif
201 }
202
203/*************************************************
204* Parse the options string *
205*************************************************/
206std::map<std::string, std::string> parse_args(const std::string& arg_string)
207 {
208 std::map<std::string, std::string> arg_map;
209 std::vector<std::string> args = split_on(arg_string, ' ');
210 for(u32bit j = 0; j != args.size(); j++)
211 {
212 if(args[j].find('=') == std::string::npos)
213 arg_map[args[j]] = "";
214 else
215 {
216 std::vector<std::string> name_and_value = split_on(args[j], '=');
217 arg_map[name_and_value[0]] = name_and_value[1];
218 }
219 }
220
221 return arg_map;
222 }
223
224/*************************************************
225* Check if an option is set in the argument *
226*************************************************/
227bool arg_set(const std::map<std::string, std::string>& args,
228 const std::string& option)
229 {
230 return (args.find(option) != args.end());
231 }
232
233}
234
235/*************************************************
236* Library Initialization *
237*************************************************/
238void initialize(const std::string& arg_string)
239 {
240 std::map<std::string, std::string> args = parse_args(arg_string);
241
242 if(arg_set(args, "thread_safe"))
243 set_mutex();
244
245 startup_conf();
246 startup_oids();
247 set_default_options();
248 startup_memory_subsystem();
249
250 init_lookup_tables();
251
252 if(arg_set(args, "secure_memory"))
253 set_safe_allocator();
254 set_timer();
255
256 if(!arg_set(args, "no_aliases")) add_default_aliases();
257 if(!arg_set(args, "no_oids")) add_default_oids();
258 if(arg_set(args, "config") && args["config"] != "")
259 Config::load(args["config"]);
260
261 startup_engines();
262 if(arg_set(args, "use_engines"))
263 set_engines();
264 init_rng_subsystem();
265
266 set_global_rngs(new ANSI_X931_RNG, new ANSI_X931_RNG);
267 add_entropy_sources();
268
269 if(!FIPS140::passes_self_tests())
270 {
271 deinitialize();
272 throw Self_Test_Failure("FIPS-140 startup tests");
273 }
274
275 const u32bit min_entropy = Config::get_u32bit("rng/min_entropy");
276
277 if(min_entropy != 0 && !arg_set(args, "no_rng_seed"))
278 {
279 u32bit total_bits = 0;
280 for(u32bit j = 0; j != 4; j++)
281 {
282 total_bits += Global_RNG::seed(true, min_entropy - total_bits);
283 if(total_bits >= min_entropy)
284 break;
285 }
286
287 if(total_bits < min_entropy)
288 throw PRNG_Unseeded("Unable to collect sufficient entropy");
289 }
290
291 startup_dl_cache();
292 }
293
294/*************************************************
295* Library Shutdown *
296*************************************************/
297void deinitialize()
298 {
299 shutdown_engines();
300 shutdown_rng_subsystem();
301 destroy_lookup_tables();
302 shutdown_dl_cache();
303 shutdown_conf();
304 shutdown_oids();
305 set_timer_type(0);
306 set_mutex_type(0);
307 shutdown_memory_subsystem();
308 }
309
310}
311
312}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status