diff options
author | lloyd <[email protected]> | 2015-02-04 04:03:38 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-02-04 04:03:38 +0000 |
commit | 0dd060fed07b0060f94e3bae62e125a85c1bb877 (patch) | |
tree | ed4bc7a961e2b30f17ed5e80769c84b0c313c8b7 /src/lib/modes | |
parent | f9a7c85b74be0f4a7273e8e0591703af83036e81 (diff) |
Remove algo factory, engines, global RNG, global state, etc.
Convert all uses of Algorithm_Factory and the engines to using Algo_Registry
The shared pool of entropy sources remains but is moved to EntropySource.
With that and few remaining initializations (default OIDs and aliases)
moved elsewhere, the global state is empty and init and shutdown are no-ops.
Remove almost all of the headers and code for handling the global
state, except LibraryInitializer which remains as a compatability stub.
Update seeding for blinding so only one hacky almost-global RNG
instance needs to be setup instead of across all pubkey uses (it uses
either the system RNG or an AutoSeeded_RNG if the system RNG is not
available).
Diffstat (limited to 'src/lib/modes')
-rw-r--r-- | src/lib/modes/cipher_mode.cpp | 31 | ||||
-rw-r--r-- | src/lib/modes/cipher_mode.h | 47 | ||||
-rw-r--r-- | src/lib/modes/info.txt | 1 | ||||
-rw-r--r-- | src/lib/modes/mode_utils.h | 20 |
4 files changed, 81 insertions, 18 deletions
diff --git a/src/lib/modes/cipher_mode.cpp b/src/lib/modes/cipher_mode.cpp index ded7b4c81..f568415f4 100644 --- a/src/lib/modes/cipher_mode.cpp +++ b/src/lib/modes/cipher_mode.cpp @@ -6,16 +6,17 @@ */ #include <botan/cipher_mode.h> +#include <botan/lookup.h> #include <sstream> namespace Botan { Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction) { - const char* dir_string = (direction == ENCRYPTION) ? "_Encryption" : "_Decryption"; - const std::string provider = ""; + const char* dir_string = (direction == ENCRYPTION) ? "_Encryption" : "_Decryption"; + std::unique_ptr<Transform> t; t.reset(get_transform(algo_spec, provider, dir_string)); @@ -36,16 +37,19 @@ Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction) if(mode_info.empty()) return nullptr; - std::ostringstream t_name; + std::ostringstream alg_args; - t_name << mode_info[0] << dir_string << '(' << cipher_name; + alg_args << '(' << cipher_name; for(size_t i = 1; i < mode_info.size(); ++i) - t_name << ',' << mode_info[i]; + alg_args << ',' << mode_info[i]; for(size_t i = 2; i < algo_parts.size(); ++i) - t_name << ',' << algo_parts[i]; - t_name << ')'; + alg_args << ',' << algo_parts[i]; + alg_args << ')'; - t.reset(get_transform(t_name.str(), provider)); + const std::string mode_name = mode_info[0] + alg_args.str(); + const std::string mode_name_directional = mode_info[0] + dir_string + alg_args.str(); + + t.reset(get_transform(mode_name_directional, provider)); if(Cipher_Mode* cipher = dynamic_cast<Cipher_Mode*>(t.get())) { @@ -53,6 +57,17 @@ Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction) return cipher; } + t.reset(get_transform(mode_name, provider)); + + if(Cipher_Mode* cipher = dynamic_cast<Cipher_Mode*>(t.get())) + { + t.release(); + return cipher; + } + + if(StreamCipher* stream_cipher = get_stream_cipher(mode_name, provider)) + return new Stream_Cipher_Mode(stream_cipher); + return nullptr; } diff --git a/src/lib/modes/cipher_mode.h b/src/lib/modes/cipher_mode.h index 691852214..19c0af150 100644 --- a/src/lib/modes/cipher_mode.h +++ b/src/lib/modes/cipher_mode.h @@ -9,6 +9,7 @@ #define BOTAN_CIPHER_MODE_H__ #include <botan/transform.h> +#include <botan/stream_cipher.h> namespace Botan { @@ -25,6 +26,52 @@ class BOTAN_DLL Cipher_Mode : public Keyed_Transform virtual bool authenticated() const { return false; } }; +class BOTAN_DLL Stream_Cipher_Mode : public Cipher_Mode + { + public: + Stream_Cipher_Mode(StreamCipher* cipher) : m_cipher(cipher) {} + + void update(secure_vector<byte>& buf, size_t offset) override + { + if(offset < buf.size()) + m_cipher->cipher1(&buf[offset], buf.size() - offset); + } + + void finish(secure_vector<byte>& buf, size_t offset) override + { return update(buf, offset); } + + size_t output_length(size_t input_length) const override { return input_length; } + + size_t update_granularity() const override { return 64; /* arbitrary */ } + + size_t minimum_final_size() const override { return 0; } + + size_t default_nonce_length() const override { return 0; } + + bool valid_nonce_length(size_t nonce_len) const override + { return m_cipher->valid_iv_length(nonce_len); } + + Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); } + + std::string name() const override { return m_cipher->name(); } + + void clear() override { return m_cipher->clear(); } + + private: + secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override + { + m_cipher->set_iv(nonce, nonce_len); + return secure_vector<byte>(); + } + + void key_schedule(const byte key[], size_t length) + { + m_cipher->set_key(key, length); + } + + std::unique_ptr<StreamCipher> m_cipher; + }; + BOTAN_DLL Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction); } diff --git a/src/lib/modes/info.txt b/src/lib/modes/info.txt index b3d6d3b5f..6ed13e782 100644 --- a/src/lib/modes/info.txt +++ b/src/lib/modes/info.txt @@ -1,6 +1,7 @@ <requires> block +stream </requires> <header:public> diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h index 70c996428..ef2840000 100644 --- a/src/lib/modes/mode_utils.h +++ b/src/lib/modes/mode_utils.h @@ -9,7 +9,7 @@ #define BOTAN_MODE_UTILS_H__ #include <botan/cipher_mode.h> -#include <botan/algo_registry.h> +#include <botan/internal/algo_registry.h> #include <botan/block_cipher.h> #include <botan/loadstor.h> #include <botan/internal/xor_buf.h> @@ -52,17 +52,17 @@ T* make_block_cipher_mode_len2(const Transform::Spec& spec) return nullptr; } -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE(E, D) \ - namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode<E>); \ - Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode<D>); } +#define BOTAN_REGISTER_BLOCK_CIPHER_MODE(E, D) \ + BOTAN_REGISTER_NAMED_T(Transform, #E, E, make_block_cipher_mode<E>); \ + BOTAN_REGISTER_NAMED_T(Transform, #D, D, make_block_cipher_mode<D>); -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(E, D, LEN) \ - namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode_len<E, LEN>); \ - Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode_len<D, LEN>); } +#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(E, D, LEN) \ + BOTAN_REGISTER_NAMED_T(Transform, #E, E, (make_block_cipher_mode_len<E, LEN>)); \ + BOTAN_REGISTER_NAMED_T(Transform, #D, D, (make_block_cipher_mode_len<D, LEN>)); -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(E, D, LEN1, LEN2) \ - namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode_len2<E, LEN1, LEN2>); \ - Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode_len2<D, LEN1, LEN2>); } +#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(E, D, LEN1, LEN2) \ + BOTAN_REGISTER_NAMED_T(Transform, #E, E, (make_block_cipher_mode_len2<E, LEN1, LEN2>)); \ + BOTAN_REGISTER_NAMED_T(Transform, #D, D, (make_block_cipher_mode_len2<D, LEN1, LEN2>)); } |