/* * Library Internal/Global State * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include #include #include #include #include #include #include #if defined(BOTAN_HAS_SELFTESTS) #include #endif #if defined(BOTAN_HAS_ENGINE_ASSEMBLER) #include #endif #if defined(BOTAN_HAS_ENGINE_AES_ISA) #include #endif #if defined(BOTAN_HAS_ENGINE_SIMD) #include #endif #if defined(BOTAN_HAS_ENGINE_GNU_MP) #include #endif #if defined(BOTAN_HAS_ENGINE_OPENSSL) #include #endif namespace Botan { /* * Get a configuration value */ std::string Library_State::get(const std::string& section, const std::string& key) { std::lock_guard lock(config_lock); return search_map(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 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 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 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); #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; } }