1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/*
* Library Internal/Global State
* (C) 1999-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/libstate.h>
#include <botan/charset.h>
#include <botan/engine.h>
#include <botan/init.h>
#include <botan/internal/defalloc.h>
#include <botan/internal/default_engine.h>
#include <botan/internal/mutex.h>
#include <botan/internal/mux_noop.h>
#include <botan/internal/stl_util.h>
#include <algorithm>
#if defined(BOTAN_HAS_SELFTESTS)
#include <botan/selftest.h>
#endif
#if defined(BOTAN_HAS_MUTEX_PTHREAD)
#include <botan/internal/mux_pthr.h>
#elif defined(BOTAN_HAS_MUTEX_WIN32)
#include <botan/internal/mux_win32.h>
#elif defined(BOTAN_HAS_MUTEX_QT)
#include <botan/internal/mux_qt.h>
#endif
#if defined(BOTAN_HAS_ALLOC_MMAP)
#include <botan/internal/mmap_mem.h>
#endif
#if defined(BOTAN_HAS_ENGINE_IA32_ASSEMBLER)
#include <botan/internal/ia32_engine.h>
#endif
#if defined(BOTAN_HAS_ENGINE_AMD64_ASSEMBLER)
#include <botan/internal/amd64_engine.h>
#endif
#if defined(BOTAN_HAS_ENGINE_AES_ISA)
#include <botan/internal/aes_isa_engine.h>
#endif
#if defined(BOTAN_HAS_ENGINE_SIMD)
#include <botan/internal/simd_engine.h>
#endif
#if defined(BOTAN_HAS_ENGINE_GNU_MP)
#include <botan/internal/gnump_engine.h>
#endif
#if defined(BOTAN_HAS_ENGINE_OPENSSL)
#include <botan/internal/openssl_engine.h>
#endif
namespace Botan {
/*
* Botan's global state
*/
namespace {
Library_State* global_lib_state = 0;
}
/*
* Access the global state object
*/
Library_State& global_state()
{
/* Lazy initialization. Botan still needs to be deinitialized later
on or memory might leak.
*/
if(!global_lib_state)
LibraryInitializer::initialize("thread_safe=true");
return (*global_lib_state);
}
/*
* Set a new global state object
*/
void set_global_state(Library_State* new_state)
{
delete swap_global_state(new_state);
}
/*
* Swap two global state objects
*/
Library_State* swap_global_state(Library_State* new_state)
{
Library_State* old_state = global_lib_state;
global_lib_state = new_state;
return old_state;
}
/*
* Get a new mutex object
*/
Mutex* Library_State::get_mutex() const
{
return mutex_factory->make();
}
/*
* Get an allocator by its name
*/
Allocator* Library_State::get_allocator(const std::string& type) const
{
Mutex_Holder lock(allocator_lock);
if(type != "")
return search_map<std::string, Allocator*>(alloc_factory, type, 0);
if(!cached_default_allocator)
{
cached_default_allocator =
search_map<std::string, Allocator*>(alloc_factory,
default_allocator_name, 0);
}
return cached_default_allocator;
}
/*
* Create a new name to object mapping
*/
void Library_State::add_allocator(Allocator* allocator)
{
Mutex_Holder lock(allocator_lock);
allocator->init();
allocators.push_back(allocator);
alloc_factory[allocator->type()] = allocator;
}
/*
* Set the default allocator type
*/
void Library_State::set_default_allocator(const std::string& type)
{
Mutex_Holder lock(allocator_lock);
if(type == "")
return;
default_allocator_name = type;
cached_default_allocator = 0;
}
/*
* Get a configuration value
*/
std::string Library_State::get(const std::string& section,
const std::string& key) const
{
Mutex_Holder lock(config_lock);
return search_map<std::string, std::string>(config,
section + "/" + key, "");
}
/*
* See if a particular option has been set
*/
bool Library_State::is_set(const std::string& section,
const std::string& key) const
{
Mutex_Holder lock(config_lock);
return search_map(config, section + "/" + key, false, true);
}
/*
* Set a configuration value
*/
void Library_State::set(const std::string& section, const std::string& key,
const std::string& value, bool overwrite)
{
Mutex_Holder lock(config_lock);
std::string full_key = section + "/" + key;
std::map<std::string, std::string>::const_iterator i =
config.find(full_key);
if(overwrite || i == config.end() || i->second == "")
config[full_key] = value;
}
/*
* Add an alias
*/
void Library_State::add_alias(const std::string& key, const std::string& value)
{
set("alias", key, value);
}
/*
* Dereference an alias to a fixed name
*/
std::string Library_State::deref_alias(const std::string& key) const
{
std::string result = key;
while(is_set("alias", result))
result = get("alias", result);
return result;
}
/*
* Set/Add an option
*/
void Library_State::set_option(const std::string& key,
const std::string& value)
{
set("conf", key, value);
}
/*
* Get an option value
*/
std::string Library_State::option(const std::string& key) const
{
return get("conf", key);
}
/**
Return a reference to the Algorithm_Factory
*/
Algorithm_Factory& Library_State::algorithm_factory() const
{
if(!m_algorithm_factory)
throw Invalid_State("Uninitialized in Library_State::algorithm_factory");
return *m_algorithm_factory;
}
/*
* Load a set of modules
*/
void Library_State::initialize(bool thread_safe)
{
if(mutex_factory)
throw Invalid_State("Library_State has already been initialized");
if(!thread_safe)
{
mutex_factory = new Noop_Mutex_Factory;
}
else
{
#if defined(BOTAN_HAS_MUTEX_PTHREAD)
mutex_factory = new Pthread_Mutex_Factory;
#elif defined(BOTAN_HAS_MUTEX_WIN32)
mutex_factory = new Win32_Mutex_Factory;
#elif defined(BOTAN_HAS_MUTEX_QT)
mutex_factory Qt_Mutex_Factory;
#else
throw Invalid_State("Could not find a thread-safe mutex object to use");
#endif
}
allocator_lock = mutex_factory->make();
config_lock = mutex_factory->make();
cached_default_allocator = 0;
default_allocator_name = "locking";
add_allocator(new Malloc_Allocator);
add_allocator(new Locking_Allocator(mutex_factory->make()));
#if defined(BOTAN_HAS_ALLOC_MMAP)
add_allocator(new MemoryMapping_Allocator(mutex_factory->make()));
#endif
load_default_config();
std::vector<Engine*> engines;
#if defined(BOTAN_HAS_ENGINE_GNU_MP)
engines.push_back(new GMP_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_OPENSSL)
engines.push_back(new OpenSSL_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_AES_ISA)
engines.push_back(new AES_ISA_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_SIMD)
engines.push_back(new SIMD_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_AMD64_ASSEMBLER)
engines.push_back(new AMD64_Assembler_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_IA32_ASSEMBLER)
engines.push_back(new IA32_Assembler_Engine);
#endif
engines.push_back(new Default_Engine);
m_algorithm_factory = new Algorithm_Factory(engines, *mutex_factory);
#if defined(BOTAN_HAS_SELFTESTS)
confirm_startup_self_tests(algorithm_factory());
#endif
}
/*
* Library_State Constructor
*/
Library_State::Library_State()
{
mutex_factory = 0;
allocator_lock = config_lock = 0;
cached_default_allocator = 0;
m_algorithm_factory = 0;
}
/*
* Library_State Destructor
*/
Library_State::~Library_State()
{
delete m_algorithm_factory;
cached_default_allocator = 0;
for(u32bit j = 0; j != allocators.size(); ++j)
{
allocators[j]->destroy();
delete allocators[j];
}
delete allocator_lock;
delete mutex_factory;
delete config_lock;
}
}
|