diff options
Diffstat (limited to 'src/lib/stream')
-rw-r--r-- | src/lib/stream/ctr/ctr.cpp | 10 | ||||
-rw-r--r-- | src/lib/stream/ctr/ctr.h | 2 | ||||
-rw-r--r-- | src/lib/stream/ofb/ofb.cpp | 10 | ||||
-rw-r--r-- | src/lib/stream/ofb/ofb.h | 2 | ||||
-rw-r--r-- | src/lib/stream/rc4/rc4.cpp | 9 | ||||
-rw-r--r-- | src/lib/stream/rc4/rc4.h | 2 | ||||
-rw-r--r-- | src/lib/stream/stream_cipher.cpp | 97 | ||||
-rw-r--r-- | src/lib/stream/stream_cipher.h | 23 |
8 files changed, 93 insertions, 62 deletions
diff --git a/src/lib/stream/ctr/ctr.cpp b/src/lib/stream/ctr/ctr.cpp index 43609ba2d..c4552d459 100644 --- a/src/lib/stream/ctr/ctr.cpp +++ b/src/lib/stream/ctr/ctr.cpp @@ -9,16 +9,6 @@ namespace Botan { -CTR_BE* CTR_BE::make(const Spec& spec) - { - if(spec.algo_name() == "CTR-BE" && spec.arg_count() == 1) - { - if(auto c = BlockCipher::create(spec.arg(0))) - return new CTR_BE(c.release()); - } - return nullptr; - } - CTR_BE::CTR_BE(BlockCipher* ciph) : m_cipher(ciph), m_counter(m_cipher->parallel_bytes()), diff --git a/src/lib/stream/ctr/ctr.h b/src/lib/stream/ctr/ctr.h index 385edac54..c4a28bd2b 100644 --- a/src/lib/stream/ctr/ctr.h +++ b/src/lib/stream/ctr/ctr.h @@ -38,8 +38,6 @@ class BOTAN_DLL CTR_BE final : public StreamCipher void clear() override; - static CTR_BE* make(const Spec& spec); - /** * @param cipher the block cipher to use */ diff --git a/src/lib/stream/ofb/ofb.cpp b/src/lib/stream/ofb/ofb.cpp index 3337a0c14..0c23188d5 100644 --- a/src/lib/stream/ofb/ofb.cpp +++ b/src/lib/stream/ofb/ofb.cpp @@ -9,16 +9,6 @@ namespace Botan { -OFB* OFB::make(const Spec& spec) - { - if(spec.algo_name() == "OFB" && spec.arg_count() == 1) - { - if(auto c = BlockCipher::create(spec.arg(0))) - return new OFB(c.release()); - } - return nullptr; - } - OFB::OFB(BlockCipher* cipher) : m_cipher(cipher), m_buffer(m_cipher->block_size()), diff --git a/src/lib/stream/ofb/ofb.h b/src/lib/stream/ofb/ofb.h index 7d77bae7a..f8beb4956 100644 --- a/src/lib/stream/ofb/ofb.h +++ b/src/lib/stream/ofb/ofb.h @@ -38,8 +38,6 @@ class BOTAN_DLL OFB final : public StreamCipher void clear() override; - static OFB* make(const Spec& spec); - /** * @param cipher the block cipher to use */ diff --git a/src/lib/stream/rc4/rc4.cpp b/src/lib/stream/rc4/rc4.cpp index e5ea2e2b8..7200288b6 100644 --- a/src/lib/stream/rc4/rc4.cpp +++ b/src/lib/stream/rc4/rc4.cpp @@ -10,15 +10,6 @@ namespace Botan { -RC4* RC4::make(const Spec& spec) - { - if(spec.algo_name() == "RC4") - return new RC4(spec.arg_as_integer(0, 0)); - if(spec.algo_name() == "RC4_drop") - return new RC4(768); - return nullptr; - } - /* * Combine cipher stream with message */ diff --git a/src/lib/stream/rc4/rc4.h b/src/lib/stream/rc4/rc4.h index 82dd6097b..46715f7d2 100644 --- a/src/lib/stream/rc4/rc4.h +++ b/src/lib/stream/rc4/rc4.h @@ -33,8 +33,6 @@ class BOTAN_DLL RC4 final : public StreamCipher return Key_Length_Specification(1, 256); } - static RC4* make(const Spec& spec); - /** * @param skip skip this many initial bytes in the keystream */ diff --git a/src/lib/stream/stream_cipher.cpp b/src/lib/stream/stream_cipher.cpp index 7c41722a0..74b9db9e7 100644 --- a/src/lib/stream/stream_cipher.cpp +++ b/src/lib/stream/stream_cipher.cpp @@ -1,12 +1,12 @@ /* * Stream Ciphers -* (C) 2015 Jack Lloyd +* (C) 2015,2016 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #include <botan/stream_cipher.h> -#include <botan/internal/algo_registry.h> +#include <botan/scan_name.h> #if defined(BOTAN_HAS_CHACHA) #include <botan/chacha.h> @@ -32,44 +32,101 @@ #include <botan/rc4.h> #endif +#if defined(BOTAN_HAS_OPENSSL) + #include <botan/internal/openssl.h> +#endif + namespace Botan { std::unique_ptr<StreamCipher> StreamCipher::create(const std::string& algo_spec, const std::string& provider) { - return std::unique_ptr<StreamCipher>(make_a<StreamCipher>(Botan::StreamCipher::Spec(algo_spec), provider)); - } - -std::vector<std::string> StreamCipher::providers(const std::string& algo_spec) - { - return providers_of<StreamCipher>(StreamCipher::Spec(algo_spec)); - } + const SCAN_Name req(algo_spec); -StreamCipher::StreamCipher() {} -StreamCipher::~StreamCipher() {} +#if defined(BOTAN_HAS_CTR_BE) + if(req.algo_name() == "CTR-BE" && req.arg_count() == 1) + { + if(provider.empty() || provider == "base") + { + if(auto c = BlockCipher::create(req.arg(0))) + return std::unique_ptr<StreamCipher>(new CTR_BE(c.release())); + } + } +#endif #if defined(BOTAN_HAS_CHACHA) -BOTAN_REGISTER_T_1LEN(StreamCipher, ChaCha, 20); + if(req.algo_name() == "ChaCha") + { + if(provider.empty() || provider == "base") + return std::unique_ptr<StreamCipher>(new ChaCha(req.arg_as_integer(0, 20))); + } #endif #if defined(BOTAN_HAS_SALSA20) -BOTAN_REGISTER_T_NOARGS(StreamCipher, Salsa20); + if(req.algo_name() == "Salsa20") + { + if(provider.empty() || provider == "base") + return std::unique_ptr<StreamCipher>(new Salsa20); + } #endif #if defined(BOTAN_HAS_SHAKE_CIPHER) -BOTAN_REGISTER_NAMED_T(StreamCipher, "SHAKE-128", SHAKE_128, make_new_T<SHAKE_128>); -#endif - -#if defined(BOTAN_HAS_CTR_BE) -BOTAN_REGISTER_NAMED_T(StreamCipher, "CTR-BE", CTR_BE, CTR_BE::make); + if(req.algo_name() == "SHAKE-128") + { + if(provider.empty() || provider == "base") + return std::unique_ptr<StreamCipher>(new SHAKE_128); + } #endif #if defined(BOTAN_HAS_OFB) -BOTAN_REGISTER_NAMED_T(StreamCipher, "OFB", OFB, OFB::make); + if(req.algo_name() == "OFB" && req.arg_count() == 1) + { + if(provider.empty() || provider == "base") + { + if(auto c = BlockCipher::create(req.arg(0))) + return std::unique_ptr<StreamCipher>(new OFB(c.release())); + } + } #endif #if defined(BOTAN_HAS_RC4) -BOTAN_REGISTER_NAMED_T(StreamCipher, "RC4", RC4, RC4::make); + + if(req.algo_name() == "RC4") + { + const size_t skip = req.arg_as_integer(0, 0); +#if defined(BOTAN_HAS_OPENSSL) + if(provider.empty() || provider == "openssl") + { + return std::unique_ptr<StreamCipher>(make_openssl_rc4(skip)); + } #endif + if(provider.empty() || provider == "base") + { + return std::unique_ptr<StreamCipher>(new RC4(skip)); + } + } + +#endif + + return nullptr; + } + +//static +std::unique_ptr<StreamCipher> +StreamCipher::create_or_throw(const std::string& algo, + const std::string& provider) + { + if(auto sc = StreamCipher::create(algo, provider)) + { + return sc; + } + throw Lookup_Error("Stream cipher", algo, provider); + } + +std::vector<std::string> StreamCipher::providers(const std::string& algo_spec) + { + return probe_providers_of<StreamCipher>(algo_spec, {"base", "openssl"}); + } + } diff --git a/src/lib/stream/stream_cipher.h b/src/lib/stream/stream_cipher.h index 0cbdf3c65..7654bf427 100644 --- a/src/lib/stream/stream_cipher.h +++ b/src/lib/stream/stream_cipher.h @@ -9,7 +9,7 @@ #define BOTAN_STREAM_CIPHER_H__ #include <botan/sym_algo.h> -#include <botan/scan_name.h> +#include <string> namespace Botan { @@ -19,7 +19,7 @@ namespace Botan { class BOTAN_DLL StreamCipher : public SymmetricAlgorithm { public: - typedef SCAN_Name Spec; + virtual ~StreamCipher() {} /** * Create an instance based on a name @@ -28,8 +28,20 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm * @param provider provider implementation to use * @return a null pointer if the algo/provider combination cannot be found */ - static std::unique_ptr<StreamCipher> create(const std::string& algo_spec, - const std::string& provider = ""); + static std::unique_ptr<StreamCipher> + create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Create an instance based on a name + * If provider is empty then best available is chosen. + * @param algo_spec algorithm name + * @param provider provider implementation to use + * Throws a Lookup_Error if the algo/provider combination cannot be found + */ + static std::unique_ptr<StreamCipher> + create_or_throw(const std::string& algo_spec, + const std::string& provider = ""); /** * @return list of available providers for this algorithm, empty if not available @@ -109,9 +121,6 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm * might also return "sse2", "avx2", "openssl", or some other arbitrary string. */ virtual std::string provider() const { return "base"; } - - StreamCipher(); - virtual ~StreamCipher(); }; } |