Botan: Botan::Library_State Class Reference

Botan 1.10.16

Public Member Functions | List of all members
Botan::Library_State Class Reference

`#include <libstate.h>`

## Public Member Functions
void | add_alias (const std::string &key, const std::string &value)
void | add_allocator (Allocator *alloc)
Algorithm_Factory & | algorithm_factory () const
std::string | deref_alias (const std::string &alias) const
std::string | get (const std::string &section, const std::string &key) const
Allocator * | get_allocator (const std::string &name="") const
Mutex * | get_mutex () const
RandomNumberGenerator & | global_rng ()
void | initialize (bool thread_safe)
bool | is_set (const std::string &section, const std::string &key) const
| Library_State ()
void | set (const std::string &section, const std::string &key, const std::string &value, bool overwrite=true)
void | set_default_allocator (const std::string &name)
| ~Library_State ()

## Detailed Description

Global state container aka the buritto at the center of it all

Definition at line 27 of file libstate.h.

## Constructor & Destructor Documentation

## ◆ Library_State()

Botan::Library_State::Library_State | ( |  | ) |

Definition at line 266 of file libstate.cpp.

267 {
268 mutex_factory = 0;
269 allocator_lock = config_lock = 0;
270 cached_default_allocator = 0;
271 m_algorithm_factory = 0;
272
273 global_rng_lock = 0;
274 global_rng_ptr = 0;
275 }

## ◆ ~Library_State()

Botan::Library_State::~Library_State | ( |  | ) |

Definition at line 280 of file libstate.cpp.

281 {
282 delete m_algorithm_factory;
283 delete global_rng_ptr;
284
285 cached_default_allocator = 0;
286
287 for(size_t i = 0; i != allocators.size(); ++i)
288 {
289 allocators[i]->destroy();
290 delete allocators[i];
291 }
292
293 delete global_rng_lock;
294 delete allocator_lock;
295 delete mutex_factory;
296 delete config_lock;
297 }

## Member Function Documentation

## ◆ add_alias()

void Botan::Library_State::add_alias | ( | const std::string & | key,
|  | const std::string & | value
| ) |  |

Add a parameter value to the "alias" section.

Parameters

  key | the name of the parameter which shall have a new alias
  value | the new alias

Definition at line 154 of file libstate.cpp.

155 {
156 set("alias", key, value);
157 }

## ◆ add_allocator()

void Botan::Library_State::add_allocator | ( | Allocator * | alloc | ) |

Add a new allocator to the list of available ones

Parameters

  alloc | the allocator to add

Definition at line 87 of file libstate.cpp.

References Botan::Allocator::init(), and Botan::Allocator::type().

Referenced by initialize().

88 {
89 Mutex_Holder lock(allocator_lock);
90
91 allocator->init();
92
93 allocators.push_back(allocator);
94 alloc_factory[allocator->type()] = allocator;
95 }

## ◆ algorithm_factory()

Algorithm_Factory & Botan::Library_State::algorithm_factory | ( |  | ) | const

Returns
  global Algorithm_Factory

Definition at line 173 of file libstate.cpp.

Referenced by Botan::block_size_of(), Botan::get_block_cipher(), Botan::get_cipher(), Botan::get_eme(), Botan::get_emsa(), Botan::get_hash(), Botan::get_kdf(), Botan::get_mac(), Botan::get_pbe(), Botan::get_pbkdf(), Botan::get_stream_cipher(), global_rng(), Botan::Hash_Filter::Hash_Filter(), Botan::have_algorithm(), Botan::have_block_cipher(), Botan::have_hash(), Botan::have_mac(), Botan::have_stream_cipher(), initialize(), Botan::keylength_multiple_of(), Botan::MAC_Filter::MAC_Filter(), Botan::max_keylength_of(), Botan::min_keylength_of(), Botan::output_length_of(), Botan::retrieve_block_cipher(), Botan::retrieve_hash(), Botan::retrieve_mac(), Botan::retrieve_stream_cipher(), Botan::Record_Writer::set_keys(), Botan::Record_Reader::set_keys(), and Botan::StreamCipher_Filter::StreamCipher_Filter().

174 {
175 if(!m_algorithm_factory)
176 throw Invalid_State("Uninitialized in Library_State::algorithm_factory");
177 return *m_algorithm_factory;
178 }

## ◆ deref_alias()

std::string Botan::Library_State::deref_alias | ( | const std::string & | alias | ) | const

Resolve an alias.

Parameters

  alias | the alias to resolve.

Returns
  what the alias stands for

Definition at line 162 of file libstate.cpp.

References is_set().

Referenced by Botan::get_pbe(), and Botan::SCAN_Name::SCAN_Name().

163 {
164 std::string result = key;
165 while(is_set("alias", result))
166 result = get("alias", result);
167 return result;
168 }
Botan::Library_State::is_set
bool is_set(const std::string &section, const std::string &key) const
Definition: libstate.cpp:126

## ◆ get()

std::string Botan::Library_State::get | ( | const std::string & | section,
|  | const std::string & | key
| ) |  | const

Get a parameter value as std::string.

Parameters

  section | the section of the desired key
  key | the desired keys name

Returns
  the value of the parameter

Definition at line 114 of file libstate.cpp.

Referenced by Botan::DL_Group::DL_Group(), Botan::EC_Group::EC_Group(), and Botan::OIDS::lookup().

116 {
117 Mutex_Holder lock(config_lock);
118
119 return search_map<std::string, std::string>(config,
120 section + "/" + key, "");
121 }

## ◆ get_allocator()

Allocator * Botan::Library_State::get_allocator | ( | const std::string & | name = `""` | ) | const

Parameters

  name | the name of the allocator

Returns
  allocator matching this name, or NULL

Definition at line 67 of file libstate.cpp.

Referenced by Botan::Allocator::get().

68 {
69 Mutex_Holder lock(allocator_lock);
70
71 if(type != "")
72 return search_map<std::string, Allocator*>(alloc_factory, type, 0);
73
74 if(!cached_default_allocator)
75 {
76 cached_default_allocator =
77 search_map<std::string, Allocator*>(alloc_factory,
78 default_allocator_name, 0);
79 }
80
81 return cached_default_allocator;
82 }

## ◆ get_mutex()

Mutex * Botan::Library_State::get_mutex | ( |  | ) | const

Returns
  newly created Mutex (free with delete)

Definition at line 59 of file libstate.cpp.

References Botan::Mutex_Factory::make().

Referenced by initialize().

60 {
61 return mutex_factory->make();
62 }
Botan::Mutex_Factory::make
virtual Mutex * make()=0

## ◆ global_rng()

RandomNumberGenerator & Botan::Library_State::global_rng | ( |  | ) |

Returns
  global RandomNumberGenerator

Definition at line 183 of file libstate.cpp.

References algorithm_factory().

184 {
185 Mutex_Holder lock(global_rng_lock);
186
187 if(!global_rng_ptr)
188 global_rng_ptr = make_global_rng(algorithm_factory(),
189 global_rng_lock);
190
191 return *global_rng_ptr;
192 }
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173

## ◆ initialize()

void Botan::Library_State::initialize | ( | bool | thread_safe | ) |

Parameters

  thread_safe | should a mutex be used for serialization

Definition at line 197 of file libstate.cpp.

References add_allocator(), Botan::Algorithm_Factory::add_engine(), algorithm_factory(), Botan::confirm_startup_self_tests(), get_mutex(), Botan::has_mlock(), and Botan::CPUID::initialize().

Referenced by Botan::Global_State_Management::global_state(), and Botan::LibraryInitializer::initialize().

198 {
199 CPUID::initialize();
200
201 if(mutex_factory)
202 throw Invalid_State("Library_State has already been initialized");
203
204 if(!thread_safe)
205 {
206 mutex_factory = new Noop_Mutex_Factory;
207 }
208 else
209 {
210 #if defined(BOTAN_HAS_MUTEX_PTHREAD)
211 mutex_factory = new Pthread_Mutex_Factory;
212 #elif defined(BOTAN_HAS_MUTEX_WIN32)
213 mutex_factory = new Win32_Mutex_Factory;
214 #else
215 throw Invalid_State("Could not find a thread-safe mutex object to use");
216 #endif
217 }
218
219 allocator_lock = get_mutex();
220 config_lock = get_mutex();
221 global_rng_lock = get_mutex();
222
223 default_allocator_name = has_mlock() ? "locking" : "malloc";
224
225 add_allocator(new Malloc_Allocator);
226 add_allocator(new Locking_Allocator(get_mutex()));
227
228 #if defined(BOTAN_HAS_ALLOC_MMAP)
229 add_allocator(new MemoryMapping_Allocator(get_mutex()));
230 #endif
231
232 load_default_config();
233
234 m_algorithm_factory = new Algorithm_Factory(*mutex_factory);
235
236 #if defined(BOTAN_HAS_ENGINE_GNU_MP)
237 algorithm_factory().add_engine(new GMP_Engine);
238 #endif
239
240 #if defined(BOTAN_HAS_ENGINE_OPENSSL)
241 algorithm_factory().add_engine(new OpenSSL_Engine);
242 #endif
243
244 #if defined(BOTAN_HAS_ENGINE_AES_ISA)
245 algorithm_factory().add_engine(new AES_ISA_Engine);
246 #endif
247
248 #if defined(BOTAN_HAS_ENGINE_SIMD)
249 algorithm_factory().add_engine(new SIMD_Engine);
250 #endif
251
252 #if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
253 algorithm_factory().add_engine(new Assembler_Engine);
254 #endif
255
256 algorithm_factory().add_engine(new Core_Engine);
257
258 #if defined(BOTAN_HAS_SELFTESTS)
259 confirm_startup_self_tests(algorithm_factory());
260 #endif
261 }
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::confirm_startup_self_tests
void confirm_startup_self_tests(Algorithm_Factory &af)
Definition: selftest.cpp:226
Botan::has_mlock
bool has_mlock()
Definition: mlock.cpp:19
Botan::Library_State::add_allocator
void add_allocator(Allocator *alloc)
Definition: libstate.cpp:87
Botan::Algorithm_Factory::add_engine
void add_engine(Engine *engine)
Definition: algo_factory.cpp:129
Botan::CPUID::initialize
static void initialize()
Definition: cpuid.cpp:190
Botan::Library_State::get_mutex
Mutex * get_mutex() const
Definition: libstate.cpp:59

## ◆ is_set()

bool Botan::Library_State::is_set | ( | const std::string & | section,
|  | const std::string & | key
| ) |  | const

Check whether a certain parameter is set or not.

Parameters

  section | the section of the desired key
  key | the desired keys name

Returns
  true if the parameters value is set, false otherwise

Definition at line 126 of file libstate.cpp.

Referenced by deref_alias(), and Botan::OIDS::have_oid().

128 {
129 Mutex_Holder lock(config_lock);
130
131 return config.count(section + "/" + key) != 0;
132 }

## ◆ set()

void Botan::Library_State::set | ( | const std::string & | section,
|  | const std::string & | key,
|  | const std::string & | value,
|  | bool | overwrite = `true`
| ) |  |

Set a configuration parameter.

Parameters

  section | the section of the desired key
  key | the desired keys name
  value | the new value
  overwrite | if set to true, the parameters value will be overwritten even if it is already set, otherwise no existing values will be overwritten.

Definition at line 137 of file libstate.cpp.

Referenced by Botan::OIDS::add_oid().

139 {
140 Mutex_Holder lock(config_lock);
141
142 std::string full_key = section + "/" + key;
143
144 std::map<std::string, std::string>::const_iterator i =
145 config.find(full_key);
146
147 if(overwrite || i == config.end() || i->second == "")
148 config[full_key] = value;
149 }

## ◆ set_default_allocator()

void Botan::Library_State::set_default_allocator | ( | const std::string & | name | ) |

Set the default allocator

Parameters

  name | the name of the allocator to use as the default

Definition at line 100 of file libstate.cpp.

101 {
102 Mutex_Holder lock(allocator_lock);
103
104 if(type == "")
105 return;
106
107 default_allocator_name = type;
108 cached_default_allocator = 0;
109 }

The documentation for this class was generated from the following files:

- src/libstate/libstate.h
- src/libstate/global_rng.cpp
- src/libstate/libstate.cpp
- src/libstate/policy.cpp