Botan  1.11.4
libstate.cpp
Go to the documentation of this file.
1 /*
2 * Library Internal/Global State
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/libstate.h>
9 #include <botan/charset.h>
10 #include <botan/engine.h>
11 #include <botan/cpuid.h>
12 #include <botan/internal/core_engine.h>
13 #include <botan/internal/stl_util.h>
14 #include <algorithm>
15 
16 #if defined(BOTAN_HAS_SELFTESTS)
17  #include <botan/selftest.h>
18 #endif
19 
20 #if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
21  #include <botan/internal/asm_engine.h>
22 #endif
23 
24 #if defined(BOTAN_HAS_ENGINE_AES_ISA)
25  #include <botan/internal/aes_isa_engine.h>
26 #endif
27 
28 #if defined(BOTAN_HAS_ENGINE_SIMD)
29  #include <botan/internal/simd_engine.h>
30 #endif
31 
32 #if defined(BOTAN_HAS_ENGINE_GNU_MP)
33  #include <botan/internal/gnump_engine.h>
34 #endif
35 
36 #if defined(BOTAN_HAS_ENGINE_OPENSSL)
37  #include <botan/internal/openssl_engine.h>
38 #endif
39 
40 namespace Botan {
41 
42 /*
43 * Get a configuration value
44 */
45 std::string Library_State::get(const std::string& section,
46  const std::string& key)
47  {
48  std::lock_guard<std::mutex> lock(config_lock);
49 
50  return search_map<std::string, std::string>(config,
51  section + "/" + key, "");
52  }
53 
54 /*
55 * See if a particular option has been set
56 */
57 bool Library_State::is_set(const std::string& section,
58  const std::string& key)
59  {
60  std::lock_guard<std::mutex> lock(config_lock);
61 
62  return config.count(section + "/" + key) != 0;
63  }
64 
65 /*
66 * Set a configuration value
67 */
68 void Library_State::set(const std::string& section, const std::string& key,
69  const std::string& value, bool overwrite)
70  {
71  std::lock_guard<std::mutex> lock(config_lock);
72 
73  std::string full_key = section + "/" + key;
74 
75  auto i = config.find(full_key);
76 
77  if(overwrite || i == config.end() || i->second == "")
78  config[full_key] = value;
79  }
80 
81 /*
82 * Add an alias
83 */
84 void Library_State::add_alias(const std::string& key, const std::string& value)
85  {
86  set("alias", key, value);
87  }
88 
89 /*
90 * Dereference an alias to a fixed name
91 */
92 std::string Library_State::deref_alias(const std::string& key)
93  {
94  std::string result = key;
95  while(is_set("alias", result))
96  result = get("alias", result);
97  return result;
98  }
99 
100 /*
101 * Return a reference to the Algorithm_Factory
102 */
104  {
105  if(!m_algorithm_factory)
106  throw Invalid_State("Uninitialized in Library_State::algorithm_factory");
107  return *m_algorithm_factory;
108  }
109 
110 /*
111 * Return a reference to the global PRNG
112 */
114  {
115  std::lock_guard<std::mutex> lock(global_rng_lock);
116 
117  if(!global_rng_ptr)
118  global_rng_ptr = make_global_rng(algorithm_factory(),
119  global_rng_lock);
120 
121  return *global_rng_ptr;
122  }
123 
124 /*
125 * Load a set of modules
126 */
128  {
130 
131  if(m_algorithm_factory)
132  throw Invalid_State("Library_State has already been initialized");
133 
134  load_default_config();
135 
136  m_algorithm_factory = new Algorithm_Factory();
137 
138 #if defined(BOTAN_HAS_ENGINE_GNU_MP)
140 #endif
141 
142 #if defined(BOTAN_HAS_ENGINE_OPENSSL)
144 #endif
145 
146 #if defined(BOTAN_HAS_ENGINE_AES_ISA)
148 #endif
149 
150 #if defined(BOTAN_HAS_ENGINE_SIMD)
152 #endif
153 
154 #if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
156 #endif
157 
159 
160 #if defined(BOTAN_HAS_SELFTESTS)
162 #endif
163  }
164 
165 /*
166 * Library_State Constructor
167 */
169  {
170  m_algorithm_factory = nullptr;
171 
172  global_rng_ptr = nullptr;
173  }
174 
175 /*
176 * Library_State Destructor
177 */
179  {
180  delete m_algorithm_factory;
181  m_algorithm_factory = nullptr;
182 
183  delete global_rng_ptr;
184  global_rng_ptr = nullptr;
185  }
186 
187 }