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
|
/*************************************************
* Default Initialization Function Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/init.h>
#include <botan/libstate.h>
#include <botan/modules.h>
#include <botan/conf.h>
#include <botan/parsing.h>
#include <botan/defalloc.h>
#include <botan/eng_def.h>
#include <botan/fips140.h>
#include <botan/x931_rng.h>
namespace Botan {
/*************************************************
* Library Initialization *
*************************************************/
LibraryInitializer::LibraryInitializer(const std::string& arg_string)
{
Init::initialize(arg_string);
}
/*************************************************
* Library Shutdown *
*************************************************/
LibraryInitializer::~LibraryInitializer()
{
Init::deinitialize();
}
namespace Init {
namespace {
/*************************************************
* Parse the options string *
*************************************************/
std::map<std::string, std::string> parse_args(const std::string& arg_string)
{
std::map<std::string, std::string> arg_map;
std::vector<std::string> args = split_on(arg_string, ' ');
for(u32bit j = 0; j != args.size(); ++j)
{
if(args[j].find('=') == std::string::npos)
arg_map[args[j]] = "";
else
{
std::vector<std::string> name_and_value = split_on(args[j], '=');
arg_map[name_and_value[0]] = name_and_value[1];
}
}
return arg_map;
}
/*************************************************
* Check if an option is set in the argument *
*************************************************/
bool arg_set(const std::map<std::string, std::string>& args,
const std::string& option)
{
return (args.find(option) != args.end());
}
}
/*************************************************
* Library Initialization *
*************************************************/
void initialize(const std::string& arg_string)
{
std::map<std::string, std::string> args = parse_args(arg_string);
Mutex_Factory* mutex_factory = 0;
if(arg_set(args, "thread_safe"))
{
mutex_factory = Modules::get_mutex_factory();
if(!mutex_factory)
throw Exception("LibraryInitializer: thread safety impossible");
}
set_global_state(new Library_State(mutex_factory,
Modules::get_timer()));
global_state().add_allocator("malloc", new Malloc_Allocator);
global_state().add_allocator("locking", new Locking_Allocator);
if(arg_set(args, "secure_memory"))
{
std::map<std::string, Allocator*> allocators = Modules::get_allocators();
for(std::map<std::string, Allocator*>::iterator i = allocators.begin();
i != allocators.end(); ++i)
global_state().add_allocator(i->first, i->second);
}
if(arg_set(args, "config") && args["config"] != "")
Config::load(args["config"], global_state());
if(arg_set(args, "use_engines"))
{
std::vector<Engine*> engines = Modules::get_engines();
for(u32bit j = 0; j != engines.size(); ++j)
global_state().add_engine(engines[j]);
}
global_state().add_engine(new Default_Engine);
global_state().set_prng(new ANSI_X931_RNG);
std::vector<EntropySource*> sources = Modules::get_entropy_sources();
for(u32bit j = 0; j != sources.size(); ++j)
global_state().add_entropy_source(sources[j], true);
const u32bit min_entropy = Config::get_u32bit("rng/min_entropy");
if(min_entropy != 0 && !arg_set(args, "no_rng_seed"))
{
u32bit total_bits = 0;
for(u32bit j = 0; j != 4; ++j)
{
total_bits += global_state().seed_prng(true,
min_entropy - total_bits);
if(total_bits >= min_entropy)
break;
}
if(total_bits < min_entropy)
throw PRNG_Unseeded("Unable to collect sufficient entropy");
}
if(!FIPS140::passes_self_tests())
{
set_global_state(0);
throw Self_Test_Failure("FIPS-140 startup tests");
}
}
/*************************************************
* Library Shutdown *
*************************************************/
void deinitialize()
{
set_global_state(0);
}
}
}
|