diff options
Diffstat (limited to 'src/lib/modes/aead/aead.cpp')
-rw-r--r-- | src/lib/modes/aead/aead.cpp | 119 |
1 files changed, 97 insertions, 22 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; } |