diff options
Diffstat (limited to 'src/lib/modes')
-rw-r--r-- | src/lib/modes/aead/aead.cpp | 119 | ||||
-rw-r--r-- | src/lib/modes/aead/aead.h | 2 | ||||
-rw-r--r-- | src/lib/modes/aead/ccm/ccm.cpp | 1 | ||||
-rw-r--r-- | src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp | 1 | ||||
-rw-r--r-- | src/lib/modes/aead/eax/eax.cpp | 1 | ||||
-rw-r--r-- | src/lib/modes/aead/gcm/gcm.cpp | 2 | ||||
-rw-r--r-- | src/lib/modes/aead/ocb/ocb.cpp | 2 | ||||
-rw-r--r-- | src/lib/modes/aead/siv/siv.cpp | 1 | ||||
-rw-r--r-- | src/lib/modes/cbc/cbc.cpp | 2 | ||||
-rw-r--r-- | src/lib/modes/cfb/cfb.cpp | 1 | ||||
-rw-r--r-- | src/lib/modes/cipher_mode.cpp | 190 | ||||
-rw-r--r-- | src/lib/modes/cipher_mode.h | 3 | ||||
-rw-r--r-- | src/lib/modes/ecb/ecb.cpp | 2 | ||||
-rw-r--r-- | src/lib/modes/info.txt | 4 | ||||
-rw-r--r-- | src/lib/modes/mode_utils.h | 67 | ||||
-rw-r--r-- | src/lib/modes/xts/xts.cpp | 2 |
16 files changed, 202 insertions, 198 deletions
diff --git a/src/lib/modes/aead/aead.cpp b/src/lib/modes/aead/aead.cpp index 88e6cbeaa..1b7b78be7 100644 --- a/src/lib/modes/aead/aead.cpp +++ b/src/lib/modes/aead/aead.cpp @@ -5,8 +5,12 @@ */ #include <botan/aead.h> -#include <botan/internal/mode_utils.h> -#include <botan/internal/algo_registry.h> +#include <botan/scan_name.h> +#include <sstream> + +#if defined(BOTAN_HAS_BLOCK_CIPHER) + #include <botan/block_cipher.h> +#endif #if defined(BOTAN_HAS_AEAD_CCM) #include <botan/ccm.h> @@ -34,42 +38,113 @@ namespace Botan { -AEAD_Mode::~AEAD_Mode() {} +AEAD_Mode* get_aead(const std::string& algo, Cipher_Dir dir) + { +#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) + if(algo == "ChaCha20Poly1305") + { + if(dir == ENCRYPTION) + return new ChaCha20Poly1305_Encryption; + else + return new ChaCha20Poly1305_Decryption; -#if defined(BOTAN_HAS_AEAD_CCM) -BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(CCM_Encryption, CCM_Decryption, 16, 3); + } #endif -#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) -BOTAN_REGISTER_T_NOARGS(Cipher_Mode, ChaCha20Poly1305_Encryption); -BOTAN_REGISTER_T_NOARGS(Cipher_Mode, ChaCha20Poly1305_Decryption); -#endif + if(algo.find('/') != std::string::npos) + { + const std::vector<std::string> algo_parts = split_on(algo, '/'); + const std::string cipher_name = algo_parts[0]; + const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]); -#if defined(BOTAN_HAS_AEAD_EAX) -BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(EAX_Encryption, EAX_Decryption, 0); + if(mode_info.empty()) + return nullptr; + + std::ostringstream alg_args; + + alg_args << '(' << cipher_name; + for(size_t i = 1; i < mode_info.size(); ++i) + alg_args << ',' << mode_info[i]; + for(size_t i = 2; i < algo_parts.size(); ++i) + alg_args << ',' << algo_parts[i]; + alg_args << ')'; + + const std::string mode_name = mode_info[0] + alg_args.str(); + return get_aead(mode_name, dir); + } + +#if defined(BOTAN_HAS_BLOCK_CIPHER) + + SCAN_Name req(algo); + + if(req.arg_count() == 0) + { + return nullptr; + } + + std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0))); + + if(!bc) + { + return nullptr; + } + +#if defined(BOTAN_HAS_AEAD_CCM) + if(req.algo_name() == "CCM") + { + size_t tag_len = req.arg_as_integer(1, 16); + size_t L_len = req.arg_as_integer(2, 3); + if(dir == ENCRYPTION) + return new CCM_Encryption(bc.release(), tag_len, L_len); + else + return new CCM_Decryption(bc.release(), tag_len, L_len); + } #endif #if defined(BOTAN_HAS_AEAD_GCM) -BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(GCM_Encryption, GCM_Decryption, 16); + if(req.algo_name() == "GCM") + { + size_t tag_len = req.arg_as_integer(1, 16); + if(dir == ENCRYPTION) + return new GCM_Encryption(bc.release(), tag_len); + else + return new GCM_Decryption(bc.release(), tag_len); + } #endif #if defined(BOTAN_HAS_AEAD_OCB) -BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(OCB_Encryption, OCB_Decryption, 16); + if(req.algo_name() == "OCB") + { + size_t tag_len = req.arg_as_integer(1, 16); + if(dir == ENCRYPTION) + return new OCB_Encryption(bc.release(), tag_len); + else + return new OCB_Decryption(bc.release(), tag_len); + } #endif -#if defined(BOTAN_HAS_AEAD_SIV) -BOTAN_REGISTER_BLOCK_CIPHER_MODE(SIV_Encryption, SIV_Decryption); +#if defined(BOTAN_HAS_AEAD_EAX) + if(req.algo_name() == "EAX") + { + size_t tag_len = req.arg_as_integer(1, bc->block_size()); + if(dir == ENCRYPTION) + return new EAX_Encryption(bc.release(), tag_len); + else + return new EAX_Decryption(bc.release(), tag_len); + } #endif -AEAD_Mode* get_aead(const std::string& algo_spec, Cipher_Dir direction) - { - std::unique_ptr<Cipher_Mode> mode(get_cipher_mode(algo_spec, direction)); - - if(AEAD_Mode* aead = dynamic_cast<AEAD_Mode*>(mode.get())) +#if defined(BOTAN_HAS_AEAD_SIV) + if(req.algo_name() == "SIV") { - mode.release(); - return aead; + if(dir == ENCRYPTION) + return new SIV_Encryption(bc.release()); + else + return new SIV_Decryption(bc.release()); } +#endif + +#endif return nullptr; } diff --git a/src/lib/modes/aead/aead.h b/src/lib/modes/aead/aead.h index 0769a1829..2cdc6137e 100644 --- a/src/lib/modes/aead/aead.h +++ b/src/lib/modes/aead/aead.h @@ -74,7 +74,7 @@ class BOTAN_DLL AEAD_Mode : public Cipher_Mode */ size_t default_nonce_length() const override { return 12; } - virtual ~AEAD_Mode(); + virtual ~AEAD_Mode() {} }; /** diff --git a/src/lib/modes/aead/ccm/ccm.cpp b/src/lib/modes/aead/ccm/ccm.cpp index d5559bfb5..81b9f4943 100644 --- a/src/lib/modes/aead/ccm/ccm.cpp +++ b/src/lib/modes/aead/ccm/ccm.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/ccm.h> #include <botan/parsing.h> diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp index 04326dede..d2f16c225 100644 --- a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp +++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp @@ -6,7 +6,6 @@ */ #include <botan/chacha20poly1305.h> -#include <botan/internal/mode_utils.h> namespace Botan { diff --git a/src/lib/modes/aead/eax/eax.cpp b/src/lib/modes/aead/eax/eax.cpp index f26a1eae3..c76f15b48 100644 --- a/src/lib/modes/aead/eax/eax.cpp +++ b/src/lib/modes/aead/eax/eax.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/eax.h> #include <botan/cmac.h> #include <botan/ctr.h> diff --git a/src/lib/modes/aead/gcm/gcm.cpp b/src/lib/modes/aead/gcm/gcm.cpp index 32c6c0b70..a73e5ee5b 100644 --- a/src/lib/modes/aead/gcm/gcm.cpp +++ b/src/lib/modes/aead/gcm/gcm.cpp @@ -6,8 +6,8 @@ */ #include <botan/gcm.h> -#include <botan/internal/mode_utils.h> #include <botan/internal/ct_utils.h> +#include <botan/loadstor.h> #include <botan/ctr.h> #if defined(BOTAN_HAS_GCM_CLMUL) diff --git a/src/lib/modes/aead/ocb/ocb.cpp b/src/lib/modes/aead/ocb/ocb.cpp index e21749f3b..0ce2b6f00 100644 --- a/src/lib/modes/aead/ocb/ocb.cpp +++ b/src/lib/modes/aead/ocb/ocb.cpp @@ -5,9 +5,9 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/ocb.h> #include <botan/cmac.h> +#include <botan/internal/bit_ops.h> namespace Botan { diff --git a/src/lib/modes/aead/siv/siv.cpp b/src/lib/modes/aead/siv/siv.cpp index 5f8c13f7f..ce20f3ada 100644 --- a/src/lib/modes/aead/siv/siv.cpp +++ b/src/lib/modes/aead/siv/siv.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/siv.h> #include <botan/cmac.h> #include <botan/ctr.h> diff --git a/src/lib/modes/cbc/cbc.cpp b/src/lib/modes/cbc/cbc.cpp index 8066dae12..7e1fe4d0f 100644 --- a/src/lib/modes/cbc/cbc.cpp +++ b/src/lib/modes/cbc/cbc.cpp @@ -5,9 +5,9 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/cbc.h> #include <botan/mode_pad.h> +#include <botan/internal/rounding.h> namespace Botan { diff --git a/src/lib/modes/cfb/cfb.cpp b/src/lib/modes/cfb/cfb.cpp index cc5e9bae7..793bfaf46 100644 --- a/src/lib/modes/cfb/cfb.cpp +++ b/src/lib/modes/cfb/cfb.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/cfb.h> #include <botan/parsing.h> diff --git a/src/lib/modes/cipher_mode.cpp b/src/lib/modes/cipher_mode.cpp index e7040772c..d622e7754 100644 --- a/src/lib/modes/cipher_mode.cpp +++ b/src/lib/modes/cipher_mode.cpp @@ -7,10 +7,17 @@ #include <botan/cipher_mode.h> #include <botan/stream_mode.h> -#include <botan/internal/mode_utils.h> -#include <botan/internal/algo_registry.h> +#include <botan/scan_name.h> #include <sstream> +#if defined(BOTAN_HAS_BLOCK_CIPHER) + #include <botan/block_cipher.h> +#endif + +#if defined(BOTAN_HAS_AEAD_MODES) + #include <botan/aead.h> +#endif + #if defined(BOTAN_HAS_MODE_ECB) #include <botan/ecb.h> #endif @@ -27,126 +34,127 @@ #include <botan/xts.h> #endif -namespace Botan { - -#define BOTAN_REGISTER_CIPHER_MODE(name, maker) BOTAN_REGISTER_T(Cipher_Mode, name, maker) -#define BOTAN_REGISTER_CIPHER_MODE_NOARGS(name) BOTAN_REGISTER_T_NOARGS(Cipher_Mode, name) - -#if defined(BOTAN_HAS_MODE_ECB) - -template<typename T> -Cipher_Mode* make_ecb_mode(const Cipher_Mode::Spec& spec) - { - std::unique_ptr<BlockCipher> bc(BlockCipher::create(spec.arg(0))); - std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(spec.arg(1, "NoPadding"))); - if(bc && pad) - return new T(bc.release(), pad.release()); - return nullptr; - } - -BOTAN_REGISTER_CIPHER_MODE(ECB_Encryption, make_ecb_mode<ECB_Encryption>); -BOTAN_REGISTER_CIPHER_MODE(ECB_Decryption, make_ecb_mode<ECB_Decryption>); +#if defined(BOTAN_HAS_MODE_XTS) + #include <botan/xts.h> #endif -#if defined(BOTAN_HAS_MODE_CBC) +namespace Botan { -template<typename CBC_T, typename CTS_T> -Cipher_Mode* make_cbc_mode(const Cipher_Mode::Spec& spec) +Cipher_Mode* get_cipher_mode(const std::string& algo, Cipher_Dir direction) { - std::unique_ptr<BlockCipher> bc(BlockCipher::create(spec.arg(0))); - - if(bc) + if(auto sc = StreamCipher::create(algo)) { - const std::string padding = spec.arg(1, "PKCS7"); - - if(padding == "CTS") - return new CTS_T(bc.release()); - else - return new CBC_T(bc.release(), get_bc_pad(padding)); + return new Stream_Cipher_Mode(sc.release()); } - return nullptr; - } - -BOTAN_REGISTER_CIPHER_MODE(CBC_Encryption, (make_cbc_mode<CBC_Encryption,CTS_Encryption>)); -BOTAN_REGISTER_CIPHER_MODE(CBC_Decryption, (make_cbc_mode<CBC_Decryption,CTS_Decryption>)); -#endif - -#if defined(BOTAN_HAS_MODE_CFB) -BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(CFB_Encryption, CFB_Decryption, 0); -#endif - -#if defined(BOTAN_HAS_MODE_XTS) -BOTAN_REGISTER_BLOCK_CIPHER_MODE(XTS_Encryption, XTS_Decryption); +#if defined(BOTAN_HAS_AEAD_MODES) + if(auto aead = get_aead(algo, direction)) + { + return aead; + } #endif -Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction) - { - const std::string provider = ""; + if(algo.find('/') != std::string::npos) + { + const std::vector<std::string> algo_parts = split_on(algo, '/'); + const std::string cipher_name = algo_parts[0]; + const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]); - const char* dir_string = (direction == ENCRYPTION) ? "_Encryption" : "_Decryption"; + if(mode_info.empty()) + return nullptr; - Cipher_Mode::Spec spec(algo_spec, dir_string); + std::ostringstream alg_args; - std::unique_ptr<Cipher_Mode> cipher_mode( - Algo_Registry<Cipher_Mode>::global_registry().make( - Cipher_Mode::Spec(algo_spec, dir_string), - provider) - ); + alg_args << '(' << cipher_name; + for(size_t i = 1; i < mode_info.size(); ++i) + alg_args << ',' << mode_info[i]; + for(size_t i = 2; i < algo_parts.size(); ++i) + alg_args << ',' << algo_parts[i]; + alg_args << ')'; - if(cipher_mode) - { - return cipher_mode.release(); + const std::string mode_name = mode_info[0] + alg_args.str(); + return get_cipher_mode(mode_name, direction); } - const std::vector<std::string> algo_parts = split_on(algo_spec, '/'); - if(algo_parts.size() < 2) - return nullptr; +#if defined(BOTAN_HAS_BLOCK_CIPHER) - const std::string cipher_name = algo_parts[0]; - const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]); + SCAN_Name spec(algo); - if(mode_info.empty()) + if(spec.arg_count() == 0) + { return nullptr; + } - std::ostringstream alg_args; + std::unique_ptr<BlockCipher> bc(BlockCipher::create(spec.arg(0))); - alg_args << '(' << cipher_name; - for(size_t i = 1; i < mode_info.size(); ++i) - alg_args << ',' << mode_info[i]; - for(size_t i = 2; i < algo_parts.size(); ++i) - alg_args << ',' << algo_parts[i]; - alg_args << ')'; + if(!bc) + { + return nullptr; + } - 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(); +#if defined(BOTAN_HAS_MODE_CBC) + if(spec.algo_name() == "CBC") + { + const std::string padding = spec.arg(1, "PKCS7"); - cipher_mode.reset( - Algo_Registry<Cipher_Mode>::global_registry().make( - Cipher_Mode::Spec(mode_name_directional), - provider) - ); + if(padding == "CTS") + { + if(direction == ENCRYPTION) + return new CTS_Encryption(bc.release()); + else + return new CTS_Decryption(bc.release()); + } + else + { + std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(padding)); + + if(pad) + { + if(direction == ENCRYPTION) + return new CBC_Encryption(bc.release(), pad.release()); + else + return new CBC_Decryption(bc.release(), pad.release()); + } + } + } +#endif - if(cipher_mode) +#if defined(BOTAN_HAS_MODE_XTS) + if(spec.algo_name() == "XTS") { - return cipher_mode.release(); + if(direction == ENCRYPTION) + return new XTS_Encryption(bc.release()); + else + return new XTS_Decryption(bc.release()); } +#endif - cipher_mode.reset( - Algo_Registry<Cipher_Mode>::global_registry().make( - Cipher_Mode::Spec(mode_name), - provider) - ); - - if(cipher_mode) +#if defined(BOTAN_HAS_MODE_CFB) + if(spec.algo_name() == "CFB") { - return cipher_mode.release(); + const size_t feedback_bits = spec.arg_as_integer(1, 8*bc->block_size()); + if(direction == ENCRYPTION) + return new CFB_Encryption(bc.release(), feedback_bits); + else + return new CFB_Decryption(bc.release(), feedback_bits); } +#endif - if(auto sc = StreamCipher::create(mode_name, provider)) +#if defined(BOTAN_HAS_MODE_ECB) + if(spec.algo_name() == "ECB") { - return new Stream_Cipher_Mode(sc.release()); + std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(spec.arg(1, "NoPadding"))); + if(pad) + { + if(direction == ENCRYPTION) + return new ECB_Encryption(bc.release(), pad.release()); + else + return new ECB_Decryption(bc.release(), pad.release()); + } } +#endif + +#endif return nullptr; } diff --git a/src/lib/modes/cipher_mode.h b/src/lib/modes/cipher_mode.h index f08989a9e..7c0f8fc57 100644 --- a/src/lib/modes/cipher_mode.h +++ b/src/lib/modes/cipher_mode.h @@ -12,7 +12,6 @@ #include <botan/key_spec.h> #include <botan/exceptn.h> #include <botan/symkey.h> -#include <botan/scan_name.h> #include <string> #include <vector> @@ -24,8 +23,6 @@ namespace Botan { class BOTAN_DLL Cipher_Mode { public: - typedef SCAN_Name Spec; - virtual ~Cipher_Mode() {} /* diff --git a/src/lib/modes/ecb/ecb.cpp b/src/lib/modes/ecb/ecb.cpp index 650cfedf1..b39682fdf 100644 --- a/src/lib/modes/ecb/ecb.cpp +++ b/src/lib/modes/ecb/ecb.cpp @@ -5,8 +5,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/ecb.h> +#include <botan/internal/rounding.h> namespace Botan { diff --git a/src/lib/modes/info.txt b/src/lib/modes/info.txt index 24508ecaa..1e50e7de5 100644 --- a/src/lib/modes/info.txt +++ b/src/lib/modes/info.txt @@ -4,7 +4,3 @@ define MODES 20150626 cipher_mode.h stream_mode.h </header:public> - -<header:internal> -mode_utils.h -</header:internal> diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h deleted file mode 100644 index 3e4b7370b..000000000 --- a/src/lib/modes/mode_utils.h +++ /dev/null @@ -1,67 +0,0 @@ -/* -* Helper include for mode implementations -* (C) 2015 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_MODE_UTILS_H__ -#define BOTAN_MODE_UTILS_H__ - -#include <botan/cipher_mode.h> -#include <botan/block_cipher.h> -#include <botan/loadstor.h> -#include <botan/internal/rounding.h> -#include <botan/internal/bit_ops.h> -#include <algorithm> - -namespace Botan { - -template<typename T> -T* make_block_cipher_mode(const Cipher_Mode::Spec& spec) - { - if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(spec.arg(0))) - return new T(bc.release()); - return nullptr; - } - -template<typename T, size_t LEN1> -T* make_block_cipher_mode_len(const Cipher_Mode::Spec& spec) - { - if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(spec.arg(0))) - { - const size_t len1 = spec.arg_as_integer(1, LEN1); - return new T(bc.release(), len1); - } - - return nullptr; - } - -template<typename T, size_t LEN1, size_t LEN2> -T* make_block_cipher_mode_len2(const Cipher_Mode::Spec& spec) - { - if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(spec.arg(0))) - { - const size_t len1 = spec.arg_as_integer(1, LEN1); - const size_t len2 = spec.arg_as_integer(2, LEN2); - return new T(bc.release(), len1, len2); - } - - return nullptr; - } - -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE(E, D) \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #E, E, make_block_cipher_mode<E>); \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #D, D, make_block_cipher_mode<D>) - -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(E, D, LEN) \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #E, E, (make_block_cipher_mode_len<E, LEN>)); \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #D, D, (make_block_cipher_mode_len<D, LEN>)) - -#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(E, D, LEN1, LEN2) \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #E, E, (make_block_cipher_mode_len2<E, LEN1, LEN2>)); \ - BOTAN_REGISTER_NAMED_T(Cipher_Mode, #D, D, (make_block_cipher_mode_len2<D, LEN1, LEN2>)) - -} - -#endif diff --git a/src/lib/modes/xts/xts.cpp b/src/lib/modes/xts/xts.cpp index 1993bf15f..4b697ae6c 100644 --- a/src/lib/modes/xts/xts.cpp +++ b/src/lib/modes/xts/xts.cpp @@ -5,8 +5,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/mode_utils.h> #include <botan/xts.h> +#include <botan/loadstor.h> namespace Botan { |