diff options
| -rw-r--r-- | aesxx/include/aesxx/box.hpp | 22 | ||||
| -rw-r--r-- | aesxx/include/aesxx/key.hpp | 49 | ||||
| -rw-r--r-- | aesxx/utils/decrypt_block.cpp | 4 | ||||
| -rw-r--r-- | aesxx/utils/decrypt_bmp.cpp | 4 | ||||
| -rw-r--r-- | aesxx/utils/decrypt_file.cpp | 4 | ||||
| -rw-r--r-- | aesxx/utils/encrypt_block.cpp | 4 | ||||
| -rw-r--r-- | aesxx/utils/encrypt_bmp.cpp | 4 | ||||
| -rw-r--r-- | aesxx/utils/encrypt_file.cpp | 4 |
8 files changed, 63 insertions, 32 deletions
diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index e625f38..aac2162 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -8,6 +8,7 @@ #include "algorithm.hpp" #include "block.hpp" #include "error.hpp" +#include "key.hpp" #include "mode.hpp" #include <aes/all.h> @@ -22,12 +23,6 @@ namespace aes { class Box { public: - using Key = AES_Key; - - static void parse_key(Key& dest, Algorithm algorithm, std::string_view src) { - aes_parse_key(algorithm, &dest, src.data(), ErrorDetailsThrowsInDestructor{}); - } - Box(Algorithm algorithm, const Key& key, Mode mode, @@ -35,7 +30,12 @@ public: bool verbose = false) : verbose{verbose} { aes_box_init( - &impl, algorithm, &key, mode, iv ? iv->ptr() : NULL, ErrorDetailsThrowsInDestructor{} + &impl, + algorithm, + key.ptr(), + mode, + iv ? iv->ptr() : NULL, + ErrorDetailsThrowsInDestructor{} ); dump_key(key); } @@ -113,15 +113,9 @@ public: } private: - static std::string format_key(const Key& src, Algorithm algorithm) { - AES_KeyString str; - aes_format_key(algorithm, &str, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast<const char*>(&str); - } - void dump_key(const Key& src) const { if (verbose) - std::cout << std::format("Key : {}\n", format_key(src, get_algorithm())); + std::cout << std::format("Key : {}\n", src.to_string()); } Block get_iv() const { diff --git a/aesxx/include/aesxx/key.hpp b/aesxx/include/aesxx/key.hpp new file mode 100644 index 0000000..7036814 --- /dev/null +++ b/aesxx/include/aesxx/key.hpp @@ -0,0 +1,49 @@ +// Copyright (c) 2015 Egor Tensin <egor@tensin.name> +// This file is part of the "AES tools" project. +// For details, see https://github.com/egor-tensin/aes-tools. +// Distributed under the MIT License. + +#pragma once + +#include "algorithm.hpp" +#include "error.hpp" + +#include <aes/all.h> + +#include <string> +#include <string_view> + +namespace aes { + +class Key { +public: + using Impl = AES_Key; + + static Key parse(std::string_view src, Algorithm algorithm) { + Impl dest; + aes_parse_key(algorithm, &dest, src.data(), ErrorDetailsThrowsInDestructor{}); + return Key{dest, algorithm}; + } + + explicit Key(const Impl& impl, Algorithm algorithm) : impl{impl}, algorithm{algorithm} {} + + Impl* ptr() { + return &impl; + } + + const Impl* ptr() const { + return &impl; + } + + std::string to_string() const { + AES_KeyString str; + aes_format_key(algorithm, &str, &impl, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast<const char*>(&str); + } + +private: + Impl impl; + Algorithm algorithm; +}; + +} // namespace aes diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp index 9f9ee2a..d1dd059 100644 --- a/aesxx/utils/decrypt_block.cpp +++ b/aesxx/utils/decrypt_block.cpp @@ -23,9 +23,7 @@ void decrypt_blocks( const BlockSettings::Input& input, bool verbose = false ) { - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.get_key()); - + const auto key = aes::Key::parse(input.get_key(), algorithm); aes::Box box{algorithm, key, mode, input.get_iv(), verbose}; for (const auto& ciphertext : input.get_blocks()) { diff --git a/aesxx/utils/decrypt_bmp.cpp b/aesxx/utils/decrypt_bmp.cpp index e78adfc..384c32d 100644 --- a/aesxx/utils/decrypt_bmp.cpp +++ b/aesxx/utils/decrypt_bmp.cpp @@ -33,9 +33,7 @@ void decrypt_bmp( void decrypt_bmp(const FileSettings& settings) { const auto algorithm = settings.get_algorithm(); const auto mode = settings.get_mode(); - - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key()); + const auto key = aes::Key::parse(settings.get_key(), algorithm); aes::Box box{algorithm, key, mode, settings.get_iv()}; decrypt_bmp(box, settings.get_input_path(), settings.get_output_path()); diff --git a/aesxx/utils/decrypt_file.cpp b/aesxx/utils/decrypt_file.cpp index 6419c35..811fe51 100644 --- a/aesxx/utils/decrypt_file.cpp +++ b/aesxx/utils/decrypt_file.cpp @@ -31,9 +31,7 @@ void decrypt_file( void decrypt_file(const FileSettings& settings) { const auto algorithm = settings.get_algorithm(); const auto mode = settings.get_mode(); - - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key()); + const auto key = aes::Key::parse(settings.get_key(), algorithm); aes::Box box{algorithm, key, mode, settings.get_iv()}; decrypt_file(box, settings.get_input_path(), settings.get_output_path()); diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index 5f84922..acb9191 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_block.cpp @@ -23,9 +23,7 @@ void encrypt_blocks( const BlockSettings::Input& input, bool verbose = false ) { - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.get_key()); - + const auto key = aes::Key::parse(input.get_key(), algorithm); aes::Box box{algorithm, key, mode, input.get_iv(), verbose}; for (const auto& plaintext : input.get_blocks()) { diff --git a/aesxx/utils/encrypt_bmp.cpp b/aesxx/utils/encrypt_bmp.cpp index 858755c..e140d70 100644 --- a/aesxx/utils/encrypt_bmp.cpp +++ b/aesxx/utils/encrypt_bmp.cpp @@ -31,9 +31,7 @@ void encrypt_bmp( void encrypt_bmp(const FileSettings& settings) { const auto algorithm = settings.get_algorithm(); const auto mode = settings.get_mode(); - - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key()); + const auto key = aes::Key::parse(settings.get_key(), algorithm); aes::Box box{algorithm, key, mode, settings.get_iv()}; encrypt_bmp(box, settings.get_input_path(), settings.get_output_path()); diff --git a/aesxx/utils/encrypt_file.cpp b/aesxx/utils/encrypt_file.cpp index 7be07ad..c94f6ee 100644 --- a/aesxx/utils/encrypt_file.cpp +++ b/aesxx/utils/encrypt_file.cpp @@ -31,9 +31,7 @@ void encrypt_file( void encrypt_file(const FileSettings& settings) { const auto algorithm = settings.get_algorithm(); const auto mode = settings.get_mode(); - - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key()); + const auto key = aes::Key::parse(settings.get_key(), algorithm); aes::Box box{algorithm, key, mode, settings.get_iv()}; encrypt_file(box, settings.get_input_path(), settings.get_output_path()); |
