From 08ffc1a49bb0f1a1a42a57d7c55bbf0d9b6b8336 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Tue, 24 Oct 2017 11:51:12 -0400 Subject: Avoid using namespace, other cleanups --- src/cli/encryption.cpp | 51 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'src/cli') diff --git a/src/cli/encryption.cpp b/src/cli/encryption.cpp index ffc1d0b48..c041f78ee 100644 --- a/src/cli/encryption.cpp +++ b/src/cli/encryption.cpp @@ -9,10 +9,9 @@ #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_AEAD_MODES) #include +#include #include -using namespace Botan; - namespace Botan_CLI { namespace { @@ -31,41 +30,39 @@ auto VALID_MODES = std::map{ { "aes-256-xts", "AES-256/XTS" }, }; -bool is_aead(const std::string &cipher) - { - return cipher.find("/GCM") != std::string::npos - || cipher.find("/OCB") != std::string::npos; - } - -secure_vector do_crypt(const std::string &cipher, - const std::vector &input, - const SymmetricKey &key, - const InitializationVector &iv, - const OctetString &ad, - Cipher_Dir direction) +Botan::secure_vector +do_crypt(const std::string &cipher, + const std::vector &input, + const Botan::SymmetricKey &key, + const Botan::InitializationVector &iv, + const std::vector& ad, + Botan::Cipher_Dir direction) { - if (iv.size() == 0) throw std::invalid_argument("IV must not be empty"); + if(iv.size() == 0) + throw CLI_Usage_Error("IV must not be empty"); // TODO: implement streaming - std::shared_ptr processor(Botan::get_cipher_mode(cipher, direction)); - if(!processor) throw std::runtime_error("Cipher algorithm not found"); + std::unique_ptr processor(Botan::get_cipher_mode(cipher, direction)); + if(!processor) + throw CLI_Error("Cipher algorithm not found"); // Set key processor->set_key(key); - // Set associated data - if (is_aead(cipher)) + if(Botan::AEAD_Mode* aead = dynamic_cast(processor.get())) + { + aead->set_ad(ad); + } + else if(ad.size() != 0) { - auto aead_processor = std::dynamic_pointer_cast(processor); - if(!aead_processor) throw std::runtime_error("Cipher algorithm not could not be converted to AEAD"); - aead_processor->set_ad(ad.bits_of()); + throw CLI_Usage_Error("Cannot specify associated data with non-AEAD mode"); } // Set IV processor->start(iv.bits_of()); - secure_vector buf(input.begin(), input.end()); + Botan::secure_vector buf(input.begin(), input.end()); processor->finish(buf); return buf; @@ -103,11 +100,11 @@ class Encryption final : public Command error_output() << "Got " << input.size() << " bytes of input data.\n"; } - auto key = SymmetricKey(key_hex); - auto iv = InitializationVector(iv_hex); - auto ad = OctetString(ad_hex); + const Botan::SymmetricKey key(key_hex); + const Botan::InitializationVector iv(iv_hex); + const std::vector ad = Botan::hex_decode(ad_hex); - auto direction = flag_set("decrypt") ? Cipher_Dir::DECRYPTION : Cipher_Dir::ENCRYPTION; + auto direction = flag_set("decrypt") ? Botan::Cipher_Dir::DECRYPTION : Botan::Cipher_Dir::ENCRYPTION; write_output(do_crypt(VALID_MODES[mode], input, key, iv, ad, direction)); } }; -- cgit v1.2.3