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
|
/*
* 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/cpuid.h>
#include <botan/internal/core_engine.h>
#include <botan/internal/stl_util.h>
#include <algorithm>
#if defined(BOTAN_HAS_SELFTESTS)
#include <botan/selftest.h>
#endif
#if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
#include <botan/internal/asm_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 {
/*
* Get a configuration value
*/
std::string Library_State::get(const std::string& section,
const std::string& key)
{
std::lock_guard<std::mutex> 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)
{
std::lock_guard<std::mutex> lock(config_lock);
return config.count(section + "/" + key) != 0;
}
/*
* Set a configuration value
*/
void Library_State::set(const std::string& section, const std::string& key,
const std::string& value, bool overwrite)
{
std::lock_guard<std::mutex> lock(config_lock);
std::string full_key = section + "/" + key;
auto 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)
{
std::string result = key;
while(is_set("alias", result))
result = get("alias", result);
return result;
}
/*
* 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;
}
/*
* Return a reference to the global PRNG
*/
RandomNumberGenerator& Library_State::global_rng()
{
std::lock_guard<std::mutex> lock(global_rng_lock);
if(!global_rng_ptr)
global_rng_ptr = make_global_rng(algorithm_factory(),
global_rng_lock);
return *global_rng_ptr;
}
/*
* Load a set of modules
*/
void Library_State::initialize()
{
CPUID::initialize();
if(m_algorithm_factory)
throw Invalid_State("Library_State has already been initialized");
load_default_config();
m_algorithm_factory = new Algorithm_Factory();
#if defined(BOTAN_HAS_ENGINE_GNU_MP)
algorithm_factory().add_engine(new GMP_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_OPENSSL)
algorithm_factory().add_engine(new OpenSSL_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_AES_ISA)
algorithm_factory().add_engine(new AES_ISA_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_SIMD)
algorithm_factory().add_engine(new SIMD_Engine);
#endif
#if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
algorithm_factory().add_engine(new Assembler_Engine);
#endif
algorithm_factory().add_engine(new Core_Engine);
m_sources = entropy_sources();
#if defined(BOTAN_HAS_SELFTESTS)
confirm_startup_self_tests(algorithm_factory());
#endif
}
/*
* Library_State Constructor
*/
Library_State::Library_State()
{
m_algorithm_factory = nullptr;
global_rng_ptr = nullptr;
}
/*
* Library_State Destructor
*/
Library_State::~Library_State()
{
delete m_algorithm_factory;
m_algorithm_factory = nullptr;
delete global_rng_ptr;
global_rng_ptr = nullptr;
}
}
|