aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-08-02 20:35:38 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-08-02 20:35:38 +0300
commit2f7feb1d9222e0afaac8ae17db98d1556aa46aa4 (patch)
tree84adde32dc3ff844f7c7e63477c2d0cc0529e7ac
parentcxx: more mode helpers (diff)
downloadaes-tools-2f7feb1d9222e0afaac8ae17db98d1556aa46aa4.tar.gz
aes-tools-2f7feb1d9222e0afaac8ae17db98d1556aa46aa4.zip
cxx: more algorithm-agnostic API
The code (in the utilities in particular) is a mess though, so a refactoring's coming up.
Diffstat (limited to '')
-rw-r--r--cxx/include/aesnixx/aes.hpp823
-rw-r--r--cxx/include/aesnixx/all.hpp1
-rw-r--r--cxx/include/aesnixx/api.hpp171
-rw-r--r--cxx/include/aesnixx/mode.hpp108
-rw-r--r--utils/aes_block_common.hpp140
-rw-r--r--utils/aes_decrypt_block.cpp98
-rw-r--r--utils/aes_decrypt_bmp.cpp132
-rw-r--r--utils/aes_decrypt_file.cpp133
-rw-r--r--utils/aes_encrypt_block.cpp102
-rw-r--r--utils/aes_encrypt_bmp.cpp132
-rw-r--r--utils/aes_encrypt_file.cpp133
11 files changed, 995 insertions, 978 deletions
diff --git a/cxx/include/aesnixx/aes.hpp b/cxx/include/aesnixx/aes.hpp
index 5d293ca..b2febf5 100644
--- a/cxx/include/aesnixx/aes.hpp
+++ b/cxx/include/aesnixx/aes.hpp
@@ -9,581 +9,270 @@
#pragma once
#include "algorithm.hpp"
-#include "data.hpp"
+#include "api.hpp"
+#include "error.hpp"
#include "mode.hpp"
#include <aesni/all.h>
+#include <cstddef>
+
#include <string>
namespace aesni
{
- namespace aes
+ namespace aes128
+ {
+ typedef AesNI_AES128_Block Block;
+ typedef AesNI_AES128_RoundKeys RoundKeys;
+ typedef AesNI_AES128_Key Key;
+ }
+
+ template <>
+ struct Types<AESNI_AES128>
+ {
+ typedef aes128::Block Block;
+ typedef aes128::RoundKeys RoundKeys;
+ typedef aes128::Key Key;
+ };
+
+ template <>
+ std::size_t get_number_of_rounds<AESNI_AES128>()
+ {
+ return 11;
+ }
+
+ template <>
+ void from_string<AESNI_AES128>(aes128::Block& dest, const char* src)
+ {
+ aesni_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES128>(const aes128::Block& src)
+ {
+ AesNI_AES128_BlockString str;
+ aesni_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ std::string to_matrix_string<AESNI_AES128>(const aes128::Block& src)
+ {
+ AesNI_AES128_BlockMatrixString str;
+ aesni_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ void from_string<AESNI_AES128>(aes128::Key& dest, const char* src)
+ {
+ aesni_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES128>(const aes128::Key& src)
+ {
+ AesNI_AES128_KeyString str;
+ aesni_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ inline void expand_key<AESNI_AES128>(
+ const aes128::Key& key,
+ aes128::RoundKeys& encryption_keys)
+ {
+ aesni_AES128_expand_key(&key, &encryption_keys);
+ }
+
+ template <>
+ inline void derive_decryption_keys<AESNI_AES128>(
+ const aes128::RoundKeys& encryption_keys,
+ aes128::RoundKeys& decryption_keys)
+ {
+ aesni_AES128_derive_decryption_keys(
+ &encryption_keys, &decryption_keys);
+ }
+
+ AESNIXX_ENCRYPT_BLOCK_ECB(AES128);
+ AESNIXX_DECRYPT_BLOCK_ECB(AES128);
+ AESNIXX_ENCRYPT_BLOCK_CBC(AES128);
+ AESNIXX_DECRYPT_BLOCK_CBC(AES128);
+ AESNIXX_ENCRYPT_BLOCK_CFB(AES128);
+ AESNIXX_DECRYPT_BLOCK_CFB(AES128);
+ AESNIXX_ENCRYPT_BLOCK_OFB(AES128);
+ AESNIXX_DECRYPT_BLOCK_OFB(AES128);
+ AESNIXX_ENCRYPT_BLOCK_CTR(AES128);
+ AESNIXX_DECRYPT_BLOCK_CTR(AES128);
+
+ namespace aes192
+ {
+ typedef AesNI_AES192_Block Block;
+ typedef AesNI_AES192_RoundKeys RoundKeys;
+ typedef AesNI_AES192_Key Key;
+ }
+
+ template <>
+ struct Types<AESNI_AES192>
+ {
+ typedef aes192::Block Block;
+ typedef aes192::RoundKeys RoundKeys;
+ typedef aes192::Key Key;
+ };
+
+ template <>
+ std::size_t get_number_of_rounds<AESNI_AES192>()
+ {
+ return 13;
+ }
+
+ template <>
+ void from_string<AESNI_AES192>(aes192::Block& dest, const char* src)
+ {
+ aesni_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES192>(const aes192::Block& src)
+ {
+ AesNI_AES192_BlockString str;
+ aesni_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ std::string to_matrix_string<AESNI_AES192>(const aes192::Block& src)
+ {
+ AesNI_AES192_BlockMatrixString str;
+ aesni_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ void from_string<AESNI_AES192>(aes192::Key& dest, const char* src)
+ {
+ aesni_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES192>(const aes192::Key& src)
+ {
+ AesNI_AES192_KeyString str;
+ aesni_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ inline void expand_key<AESNI_AES192>(
+ const aes192::Key& key,
+ aes192::RoundKeys& encryption_keys)
+ {
+ aesni_AES192_expand_key(&key, &encryption_keys);
+ }
+
+ template <>
+ inline void derive_decryption_keys<AESNI_AES192>(
+ const aes192::RoundKeys& encryption_keys,
+ aes192::RoundKeys& decryption_keys)
+ {
+ aesni_AES192_derive_decryption_keys(
+ &encryption_keys, &decryption_keys);
+ }
+
+ AESNIXX_ENCRYPT_BLOCK_ECB(AES192);
+ AESNIXX_DECRYPT_BLOCK_ECB(AES192);
+ AESNIXX_ENCRYPT_BLOCK_CBC(AES192);
+ AESNIXX_DECRYPT_BLOCK_CBC(AES192);
+ AESNIXX_ENCRYPT_BLOCK_CFB(AES192);
+ AESNIXX_DECRYPT_BLOCK_CFB(AES192);
+ AESNIXX_ENCRYPT_BLOCK_OFB(AES192);
+ AESNIXX_DECRYPT_BLOCK_OFB(AES192);
+ AESNIXX_ENCRYPT_BLOCK_CTR(AES192);
+ AESNIXX_DECRYPT_BLOCK_CTR(AES192);
+
+ namespace aes256
{
- typedef AesNI_AES_Block Block;
-
- typedef AesNI_AES128_Key Key128;
- typedef AesNI_AES192_Key Key192;
- typedef AesNI_AES256_Key Key256;
-
- inline void make_block(Block& dest, int hi3, int hi2, int lo1, int lo0)
- {
- aesni_AES_make_block(&dest, hi3, hi2, lo1, lo0);
- }
-
- inline void make_key(Key128& dest, int hi3, int hi2, int lo1, int lo0)
- {
- aesni_AES128_make_key(&dest, hi3, hi2, lo1, lo0);
- }
-
- inline void make_key(Key192& dest, int hi5, int hi4, int hi3, int lo2, int lo1, int lo0)
- {
- aesni_AES192_make_key(&dest, hi5, hi4, hi3, lo2, lo1, lo0);
- }
-
- inline void make_key(Key256& dest, int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0)
- {
- aesni_AES256_make_key(&dest, hi7, hi6, hi5, hi4, lo3, lo2, lo1, lo0);
- }
-
- std::string to_string(const Block& block)
- {
- AesNI_AES_BlockString str;
- aesni_AES_format_block(&str, &block, ErrorDetailsThrowsInDestructor());
- return std::string(str.str);
- }
-
- std::string to_matrix_string(const Block& block)
- {
- AesNI_AES_BlockMatrixString str;
- aesni_AES_format_block_as_matrix(&str, &block, ErrorDetailsThrowsInDestructor());
- return std::string(str.str);
- }
-
- inline void from_string(Block& dest, const char* src)
- {
- aesni_AES_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
- }
-
- inline void from_string(Block& dest, const std::string& src)
- {
- from_string(dest, src.c_str());
- }
-
- std::string to_string(const Key128& block)
- {
- AesNI_AES128_KeyString str;
- aesni_AES128_format_key(&str, &block, ErrorDetailsThrowsInDestructor());
- return std::string(str.str);
- }
-
- std::string to_string(const Key192& block)
- {
- AesNI_AES192_KeyString str;
- aesni_AES192_format_key(&str, &block, ErrorDetailsThrowsInDestructor());
- return std::string(str.str);
- }
-
- std::string to_string(const Key256& block)
- {
- AesNI_AES256_KeyString str;
- aesni_AES256_format_key(&str, &block, ErrorDetailsThrowsInDestructor());
- return std::string(str.str);
- }
-
- inline void from_string(Key128& dest, const char* src)
- {
- aesni_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
- }
-
- inline void from_string(Key192& dest, const char* src)
- {
- aesni_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
- }
-
- inline void from_string(Key256& dest, const char* src)
- {
- aesni_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
- }
-
- inline void from_string(Key128& dest, const std::string& src)
- {
- return from_string(dest, src.c_str());
- }
-
- inline void from_string(Key192& dest, const std::string& src)
- {
- return from_string(dest, src.c_str());
- }
-
- inline void from_string(Key256& dest, const std::string& src)
- {
- return from_string(dest, src.c_str());
- }
-
- typedef AesNI_AES128_RoundKeys RoundKeys128;
- typedef AesNI_AES192_RoundKeys RoundKeys192;
- typedef AesNI_AES256_RoundKeys RoundKeys256;
-
- template <typename RoundKeysT>
- inline std::size_t get_number_of_rounds(const RoundKeysT& round_keys)
- {
- return sizeof(round_keys) / sizeof(Block128);
- }
-
- inline void expand_key(
- const Key128& key,
- RoundKeys128& encryption_keys)
- {
- aesni_AES128_expand_key(&key, &encryption_keys);
- }
-
- inline void expand_key(
- const Key192& key,
- RoundKeys192& encryption_keys)
- {
- aesni_AES192_expand_key(&key, &encryption_keys);
- }
-
- inline void expand_key(
- const Key256& key,
- RoundKeys256& encryption_keys)
- {
- aesni_AES256_expand_key(&key, &encryption_keys);
- }
-
- inline void derive_decryption_keys(
- const RoundKeys128& encryption_keys,
- RoundKeys128& decryption_keys)
- {
- aesni_AES128_derive_decryption_keys(
- &encryption_keys, &decryption_keys);
- }
-
- inline void derive_decryption_keys(
- const RoundKeys192& encryption_keys,
- RoundKeys192& decryption_keys)
- {
- aesni_AES192_derive_decryption_keys(
- &encryption_keys, &decryption_keys);
- }
-
- inline void derive_decryption_keys(
- const RoundKeys256& encryption_keys,
- RoundKeys256& decryption_keys)
- {
- aesni_AES256_derive_decryption_keys(
- &encryption_keys, &decryption_keys);
- }
-
- inline Block encrypt_ecb(
- const Block& plaintext,
- const RoundKeys128& encryption_keys)
- {
- return aesni_AES128_encrypt_block_ECB(plaintext, &encryption_keys);
- }
-
- inline Block decrypt_ecb(
- const Block& ciphertext,
- const RoundKeys128& decryption_keys)
- {
- return aesni_AES128_decrypt_block_ECB(ciphertext, &decryption_keys);
- }
-
- inline Block encrypt_cbc(
- const Block& plaintext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cbc(
- const Block& ciphertext,
- const RoundKeys128& decryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_cfb(
- const Block& plaintext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cfb(
- const Block& ciphertext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ofb(
- const Block& plaintext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ofb(
- const Block& ciphertext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ctr(
- const Block& plaintext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ctr(
- const Block& ciphertext,
- const RoundKeys128& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES128_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ecb(
- const Block& plaintext,
- const RoundKeys192& encryption_keys)
- {
- return aesni_AES192_encrypt_block_ECB(plaintext, &encryption_keys);
- }
-
- inline Block decrypt_ecb(
- const Block& ciphertext,
- const RoundKeys192& decryption_keys)
- {
- return aesni_AES192_decrypt_block_ECB(ciphertext, &decryption_keys);
- }
-
- inline Block encrypt_cbc(
- const Block& plaintext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cbc(
- const Block& ciphertext,
- const RoundKeys192& decryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_cfb(
- const Block& plaintext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cfb(
- const Block& ciphertext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ofb(
- const Block& plaintext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ofb(
- const Block& ciphertext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ctr(
- const Block& plaintext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ctr(
- const Block& ciphertext,
- const RoundKeys192& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES192_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ecb(
- const Block& plaintext,
- const RoundKeys256& encryption_keys)
- {
- return aesni_AES256_encrypt_block_ECB(plaintext, &encryption_keys);
- }
-
- inline Block decrypt_ecb(
- const Block& ciphertext,
- const RoundKeys256& decryption_keys)
- {
- return aesni_AES256_decrypt_block_ECB(ciphertext, &decryption_keys);
- }
-
- inline Block encrypt_cbc(
- const Block& plaintext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cbc(
- const Block& ciphertext,
- const RoundKeys256& decryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_cfb(
- const Block& plaintext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_cfb(
- const Block& ciphertext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ofb(
- const Block& plaintext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ofb(
- const Block& ciphertext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block encrypt_ctr(
- const Block& plaintext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv);
- }
-
- inline Block decrypt_ctr(
- const Block& ciphertext,
- const RoundKeys256& encryption_keys,
- const Block& iv,
- Block& next_iv)
- {
- return aesni_AES256_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv);
- }
-
- template <Algorithm>
- struct Types;
-
- template <>
- struct Types<AESNI_AES128>
- {
- typedef aesni::aes::Block BlockT;
- typedef aesni::aes::Key128 KeyT;
- typedef aesni::aes::RoundKeys128 RoundKeysT;
- };
-
- template <>
- struct Types<AESNI_AES192>
- {
- typedef aesni::aes::Block BlockT;
- typedef aesni::aes::Key192 KeyT;
- typedef aesni::aes::RoundKeys192 RoundKeysT;
- };
-
- template <>
- struct Types<AESNI_AES256>
- {
- typedef aesni::aes::Block BlockT;
- typedef aesni::aes::Key256 KeyT;
- typedef aesni::aes::RoundKeys256 RoundKeysT;
- };
-
- template <Algorithm algorithm, Mode mode>
- struct Encrypt;
-
- template <Algorithm algorithm>
- struct Encrypt<algorithm, AESNI_ECB>
- {
- Encrypt(const typename Types<algorithm>::KeyT& key,
- const typename Types<algorithm>::BlockT& iv)
- {
- expand_key(key, encryption_keys);
- derive_decryption_keys(encryption_keys, decryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT encrypt(const typename Types<algorithm>::BlockT& plaintext)
- {
- return encrypt_ecb(plaintext, encryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT decrypt(const typename Types<algorithm>::BlockT& ciphertext)
- {
- return decrypt_ecb(ciphertext, decryption_keys);
- }
-
- typename Types<algorithm>::RoundKeysT encryption_keys;
- typename Types<algorithm>::RoundKeysT decryption_keys;
- };
-
- template <Algorithm algorithm>
- struct Encrypt<algorithm, AESNI_CBC>
- {
- Encrypt(const typename Types<algorithm>::KeyT& key,
- const typename Types<algorithm>::BlockT& iv)
- : iv(iv)
- {
- expand_key(key, encryption_keys);
- derive_decryption_keys(encryption_keys, decryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT encrypt(const typename Types<algorithm>::BlockT& plaintext)
- {
- return encrypt_cbc(plaintext, encryption_keys, iv, iv);
- }
-
- inline typename Types<algorithm>::BlockT decrypt(const typename Types<algorithm>::BlockT& ciphertext)
- {
- return decrypt_cbc(ciphertext, decryption_keys, iv, iv);
- }
-
- typename Types<algorithm>::BlockT iv;
- typename Types<algorithm>::RoundKeysT encryption_keys;
- typename Types<algorithm>::RoundKeysT decryption_keys;
- };
-
- template <Algorithm algorithm>
- struct Encrypt<algorithm, AESNI_CFB>
- {
- Encrypt(const typename Types<algorithm>::KeyT& key,
- const typename Types<algorithm>::BlockT& iv)
- : iv(iv)
- {
- expand_key(key, encryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT encrypt(const typename Types<algorithm>::BlockT& plaintext)
- {
- return encrypt_cfb(plaintext, encryption_keys, iv, iv);
- }
-
- inline typename Types<algorithm>::BlockT decrypt(const typename Types<algorithm>::BlockT& ciphertext)
- {
- return decrypt_cfb(ciphertext, encryption_keys, iv, iv);
- }
-
- typename Types<algorithm>::BlockT iv;
- typename Types<algorithm>::RoundKeysT encryption_keys;
- };
-
- template <Algorithm algorithm>
- struct Encrypt<algorithm, AESNI_OFB>
- {
- Encrypt(const typename Types<algorithm>::KeyT& key,
- const typename Types<algorithm>::BlockT& iv)
- : iv(iv)
- {
- expand_key(key, encryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT encrypt(const typename Types<algorithm>::BlockT& plaintext)
- {
- return encrypt_ofb(plaintext, encryption_keys, iv, iv);
- }
-
- inline typename Types<algorithm>::BlockT decrypt(const typename Types<algorithm>::BlockT& ciphertext)
- {
- return decrypt_ofb(ciphertext, encryption_keys, iv, iv);
- }
-
- typename Types<algorithm>::BlockT iv;
- typename Types<algorithm>::RoundKeysT encryption_keys;
- };
-
- template <Algorithm algorithm>
- struct Encrypt<algorithm, AESNI_CTR>
- {
- Encrypt(const typename Types<algorithm>::KeyT& key,
- const typename Types<algorithm>::BlockT& iv)
- : iv(iv)
- {
- expand_key(key, encryption_keys);
- }
-
- inline typename Types<algorithm>::BlockT encrypt(const typename Types<algorithm>::BlockT& plaintext)
- {
- return encrypt_ctr(plaintext, encryption_keys, iv, iv);
- }
-
- inline typename Types<algorithm>::BlockT decrypt(const typename Types<algorithm>::BlockT& ciphertext)
- {
- return decrypt_ctr(ciphertext, encryption_keys, iv, iv);
- }
-
- typename Types<algorithm>::RoundKeysT encryption_keys;
- typename Types<algorithm>::BlockT iv;
- };
+ typedef AesNI_AES256_Block Block;
+ typedef AesNI_AES256_RoundKeys RoundKeys;
+ typedef AesNI_AES256_Key Key;
}
+
+ template <>
+ struct Types<AESNI_AES256>
+ {
+ typedef aes256::Block Block;
+ typedef aes256::RoundKeys RoundKeys;
+ typedef aes256::Key Key;
+ };
+
+ template <>
+ std::size_t get_number_of_rounds<AESNI_AES256>()
+ {
+ return 15;
+ }
+
+ template <>
+ void from_string<AESNI_AES256>(aes256::Block& dest, const char* src)
+ {
+ aesni_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES256>(const aes256::Block& src)
+ {
+ AesNI_AES256_BlockString str;
+ aesni_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ std::string to_matrix_string<AESNI_AES256>(const aes256::Block& src)
+ {
+ AesNI_AES256_BlockMatrixString str;
+ aesni_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ void from_string<AESNI_AES256>(aes256::Key& dest, const char* src)
+ {
+ aesni_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ }
+
+ template <>
+ std::string to_string<AESNI_AES256>(const aes256::Key& src)
+ {
+ AesNI_AES256_KeyString str;
+ aesni_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
+ return { str.str };
+ }
+
+ template <>
+ inline void expand_key<AESNI_AES256>(
+ const aes256::Key& key,
+ aes256::RoundKeys& encryption_keys)
+ {
+ aesni_AES256_expand_key(&key, &encryption_keys);
+ }
+
+ template <>
+ inline void derive_decryption_keys<AESNI_AES256>(
+ const aes256::RoundKeys& encryption_keys,
+ aes256::RoundKeys& decryption_keys)
+ {
+ aesni_AES256_derive_decryption_keys(
+ &encryption_keys, &decryption_keys);
+ }
+
+ AESNIXX_ENCRYPT_BLOCK_ECB(AES256);
+ AESNIXX_DECRYPT_BLOCK_ECB(AES256);
+ AESNIXX_ENCRYPT_BLOCK_CBC(AES256);
+ AESNIXX_DECRYPT_BLOCK_CBC(AES256);
+ AESNIXX_ENCRYPT_BLOCK_CFB(AES256);
+ AESNIXX_DECRYPT_BLOCK_CFB(AES256);
+ AESNIXX_ENCRYPT_BLOCK_OFB(AES256);
+ AESNIXX_DECRYPT_BLOCK_OFB(AES256);
+ AESNIXX_ENCRYPT_BLOCK_CTR(AES256);
+ AESNIXX_DECRYPT_BLOCK_CTR(AES256);
}
diff --git a/cxx/include/aesnixx/all.hpp b/cxx/include/aesnixx/all.hpp
index 2bdf301..417d9ea 100644
--- a/cxx/include/aesnixx/all.hpp
+++ b/cxx/include/aesnixx/all.hpp
@@ -10,6 +10,7 @@
#include "aes.hpp"
#include "algorithm.hpp"
+#include "api.hpp"
#include "data.hpp"
#include "debug.hpp"
#include "error.hpp"
diff --git a/cxx/include/aesnixx/api.hpp b/cxx/include/aesnixx/api.hpp
new file mode 100644
index 0000000..6995ae2
--- /dev/null
+++ b/cxx/include/aesnixx/api.hpp
@@ -0,0 +1,171 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#pragma once
+
+#include "algorithm.hpp"
+#include "mode.hpp"
+
+#include <cstddef>
+
+#include <string>
+#include <type_traits>
+
+namespace aesni
+{
+ template <Algorithm algorithm>
+ struct Types;
+
+ template <Algorithm algorithm>
+ std::size_t get_number_of_rounds();
+
+ template <Algorithm algorithm>
+ void from_string(
+ typename Types<algorithm>::Block&,
+ const char*);
+
+ template <Algorithm algorithm>
+ inline void from_string(
+ typename Types<algorithm>::Block& dest,
+ const std::string& src)
+ {
+ from_string<algorithm>(dest, src.c_str());
+ }
+
+ template <Algorithm algorithm>
+ std::string to_string(const typename Types<algorithm>::Block&);
+
+ template <Algorithm algorithm>
+ std::string to_matrix_string(const typename Types<algorithm>::Block&);
+
+ template <Algorithm algorithm>
+ void from_string(
+ typename Types<algorithm>::Key&,
+ const char*);
+
+ template <Algorithm algorithm>
+ inline void from_string(
+ typename Types<algorithm>::Key& dest,
+ const std::string& src)
+ {
+ from_string<algorithm>(dest, src.c_str());
+ }
+
+ template <Algorithm algorithm>
+ std::string to_string(const typename Types<algorithm>::Key&);
+
+ template <Algorithm algorithm>
+ inline void expand_key(
+ const typename Types<algorithm>::Key& key,
+ typename Types<algorithm>::RoundKeys& encryption_keys);
+
+ template <Algorithm algorithm>
+ inline void derive_decryption_keys(
+ const typename Types<algorithm>::RoundKeys& encryption_keys,
+ typename Types<algorithm>::RoundKeys& decryption_keys);
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void encrypt_block(
+ const typename Types<algorithm>::Block& plaintext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block& iv,
+ typename Types<algorithm>::Block& ciphertext);
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<!ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void encrypt_block(
+ const typename Types<algorithm>::Block& plaintext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block& ciphertext);
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<!ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void encrypt_block(
+ const typename Types<algorithm>::Block& plaintext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block&,
+ typename Types<algorithm>::Block& ciphertext)
+ {
+ encrypt_block<algorithm, mode>(plaintext, round_keys, ciphertext);
+ }
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void decrypt_block(
+ const typename Types<algorithm>::Block& ciphertext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block& iv,
+ typename Types<algorithm>::Block& plaintext);
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<!ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void decrypt_block(
+ const typename Types<algorithm>::Block& ciphertext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block& plaintext);
+
+ template <Algorithm algorithm, Mode mode, typename std::enable_if<!ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ inline void decrypt_block(
+ const typename Types<algorithm>::Block& ciphertext,
+ const typename Types<algorithm>::RoundKeys& round_keys,
+ typename Types<algorithm>::Block&,
+ typename Types<algorithm>::Block& plaintext)
+ {
+ decrypt_block<algorithm, mode>(ciphertext, round_keys, plaintext);
+ }
+
+ template <Algorithm algorithm, Mode mode>
+ struct EncryptWrapper
+ {
+ EncryptWrapper(
+ const typename Types<algorithm>::Key& key,
+ const typename Types<algorithm>::Block& iv) : iv(iv)
+ {
+ expand_key<algorithm>(key, encryption_keys);
+ }
+
+ inline void encrypt_block(
+ const typename Types<algorithm>::Block& plaintext,
+ typename Types<algorithm>::Block& ciphertext)
+ {
+ aesni::encrypt_block<algorithm, mode>(
+ plaintext, encryption_keys, iv, ciphertext);
+ }
+
+ typename Types<algorithm>::Block iv;
+ typename Types<algorithm>::RoundKeys encryption_keys;
+ };
+
+ template <Algorithm algorithm, Mode mode>
+ struct DecryptWrapper
+ {
+ DecryptWrapper(
+ const typename Types<algorithm>::Key& key,
+ const typename Types<algorithm>::Block& iv) : iv(iv)
+ {
+ typename Types<algorithm>::RoundKeys encryption_keys;
+ expand_key<algorithm>(key, encryption_keys);
+
+ if (ModeUsesEncryptionKeysOnly<mode>::value)
+ {
+ decryption_keys = encryption_keys;
+ }
+ else
+ {
+ derive_decryption_keys<algorithm>(encryption_keys, decryption_keys);
+ }
+ }
+
+ inline void decrypt_block(
+ const typename Types<algorithm>::Block& ciphertext,
+ typename Types<algorithm>::Block& plaintext)
+ {
+ aesni::decrypt_block<algorithm, mode>(
+ ciphertext, decryption_keys, iv, plaintext);
+ }
+
+ typename Types<algorithm>::Block iv;
+ typename Types<algorithm>::RoundKeys decryption_keys;
+ };
+}
diff --git a/cxx/include/aesnixx/mode.hpp b/cxx/include/aesnixx/mode.hpp
index a0af051..e19dbbd 100644
--- a/cxx/include/aesnixx/mode.hpp
+++ b/cxx/include/aesnixx/mode.hpp
@@ -45,4 +45,112 @@ namespace aesni
{
return mode != AESNI_ECB && mode != AESNI_CBC;
}
+
+#define AESNIXX_ENCRYPT_BLOCK_ECB(prefix) \
+ template <> \
+ inline void encrypt_block<AESNI_## prefix, AESNI_ECB>( \
+ const typename Types<AESNI_## prefix>::Block& plaintext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& ciphertext) \
+ { \
+ ciphertext = aesni_## prefix ##_encrypt_block_ECB(plaintext, &encryption_keys); \
+ }
+
+#define AESNIXX_DECRYPT_BLOCK_ECB(prefix) \
+ template <> \
+ inline void decrypt_block<AESNI_## prefix, AESNI_ECB>( \
+ const typename Types<AESNI_## prefix>::Block& ciphertext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& decryption_keys, \
+ typename Types<AESNI_## prefix>::Block& plaintext) \
+ { \
+ plaintext = aesni_## prefix ##_decrypt_block_ECB(ciphertext, &decryption_keys); \
+ }
+
+#define AESNIXX_ENCRYPT_BLOCK_CBC(prefix) \
+ template <> \
+ inline void encrypt_block<AESNI_## prefix, AESNI_CBC>( \
+ const typename Types<AESNI_## prefix>::Block& plaintext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& ciphertext) \
+ { \
+ ciphertext = aesni_## prefix ##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_DECRYPT_BLOCK_CBC(prefix) \
+ template <> \
+ inline void decrypt_block<AESNI_## prefix, AESNI_CBC>( \
+ const typename Types<AESNI_## prefix>::Block& ciphertext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& decryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& plaintext) \
+ { \
+ plaintext = aesni_## prefix ##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_ENCRYPT_BLOCK_CFB(prefix) \
+ template <> \
+ inline void encrypt_block<AESNI_## prefix, AESNI_CFB>( \
+ const typename Types<AESNI_## prefix>::Block& plaintext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& ciphertext) \
+ { \
+ ciphertext = aesni_## prefix ##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_DECRYPT_BLOCK_CFB(prefix) \
+ template <> \
+ inline void decrypt_block<AESNI_## prefix, AESNI_CFB>( \
+ const typename Types<AESNI_## prefix>::Block& ciphertext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& plaintext) \
+ { \
+ plaintext = aesni_## prefix ##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_ENCRYPT_BLOCK_OFB(prefix) \
+ template <> \
+ inline void encrypt_block<AESNI_## prefix, AESNI_OFB>( \
+ const typename Types<AESNI_## prefix>::Block& plaintext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& ciphertext) \
+ { \
+ ciphertext = aesni_## prefix ##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_DECRYPT_BLOCK_OFB(prefix) \
+ template <> \
+ inline void decrypt_block<AESNI_## prefix, AESNI_OFB>( \
+ const typename Types<AESNI_## prefix>::Block& ciphertext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& plaintext) \
+ { \
+ plaintext = aesni_## prefix ##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_ENCRYPT_BLOCK_CTR(prefix) \
+ template <> \
+ inline void encrypt_block<AESNI_## prefix, AESNI_CTR>( \
+ const typename Types<AESNI_## prefix>::Block& plaintext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& ciphertext) \
+ { \
+ ciphertext = aesni_## prefix ##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \
+ }
+
+#define AESNIXX_DECRYPT_BLOCK_CTR(prefix) \
+ template <> \
+ inline void decrypt_block<AESNI_## prefix, AESNI_CTR>( \
+ const typename Types<AESNI_## prefix>::Block& ciphertext, \
+ const typename Types<AESNI_## prefix>::RoundKeys& encryption_keys, \
+ typename Types<AESNI_## prefix>::Block& iv, \
+ typename Types<AESNI_## prefix>::Block& plaintext) \
+ { \
+ plaintext = aesni_## prefix ##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \
+ }
}
diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp
index bdb6ac6..52b9ce3 100644
--- a/utils/aes_block_common.hpp
+++ b/utils/aes_block_common.hpp
@@ -151,126 +151,92 @@ namespace
namespace
{
- void dump_block(const char* name, const aesni::aes::Block& block)
+ template <aesni::Algorithm algorithm>
+ void dump_block(const char* name, const typename aesni::Types<algorithm>::Block& block)
{
- std::cout << name << ": " << aesni::aes::to_string(block) << "\n" << aesni::aes::to_matrix_string(block) << "\n";
+ std::cout << name << ": " << aesni::to_string<algorithm>(block) << "\n" << aesni::to_matrix_string<algorithm>(block) << "\n";
}
- void dump_plaintext(const aesni::aes::Block& block)
+ template <aesni::Algorithm algorithm>
+ void dump_plaintext(const typename aesni::Types<algorithm>::Block& block)
{
- dump_block("Plaintext", block);
+ dump_block<algorithm>("Plaintext", block);
}
- template <typename KeyT>
- void dump_key(const KeyT& key)
+ template <aesni::Algorithm algorithm>
+ void dump_key(const typename aesni::Types<algorithm>::Key& key)
{
- std::cout << "Key: " << aesni::aes::to_string(key) << "\n\n";
+ std::cout << "Key: " << aesni::to_string<algorithm>(key) << "\n\n";
}
- void dump_ciphertext(const aesni::aes::Block& ciphertext)
+ template <aesni::Algorithm algorithm>
+ void dump_ciphertext(const typename aesni::Types<algorithm>::Block& ciphertext)
{
- dump_block("Ciphertext", ciphertext);
+ dump_block<algorithm>("Ciphertext", ciphertext);
}
- void dump_iv(const aesni::aes::Block& iv)
+ template <aesni::Algorithm algorithm>
+ void dump_iv(const typename aesni::Types<algorithm>::Block& iv)
{
- dump_block("Initialization vector", iv);
+ dump_block<algorithm>("Initialization vector", iv);
}
- void dump_next_iv(const aesni::aes::Block& next_iv)
- {
- dump_block("Next initialization vector", next_iv);
- }
-
- template <typename RoundKeysT>
- void dump_round_keys(const char* name, const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_round_keys(const char* name, const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
std::cout << name << ":\n";
- for (std::size_t i = 0; i < aesni::aes::get_number_of_rounds(round_keys); ++i)
- std::cout << "\t[" << i << "]: " << aesni::aes::to_string(round_keys.keys[i]) << "\n";
+ for (std::size_t i = 0; i < aesni::get_number_of_rounds<algorithm>(); ++i)
+ std::cout << "\t[" << i << "]: " << aesni::to_string<algorithm>(round_keys.keys[i]) << "\n";
std::cout << "\n";
}
- template <typename RoundKeysT>
- void dump_encryption_keys(const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_encryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
- dump_round_keys("Encryption round keys", round_keys);
+ dump_round_keys<algorithm>("Encryption round keys", round_keys);
}
- template <typename RoundKeysT>
- void dump_decryption_keys(const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_decryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
- dump_round_keys("Decryption round keys", round_keys);
+ dump_round_keys<algorithm>("Decryption round keys", round_keys);
}
- template <aesni::Algorithm algo, aesni::Mode mode>
- struct Dumper;
-
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_ECB>
- {
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_ECB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- dump_decryption_keys(encrypt.decryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_ECB>&)
- { }
- };
-
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CBC>
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CBC>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- dump_decryption_keys(encrypt.decryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CBC>&)
- { }
- };
+ dump_encryption_keys<algorithm>(wrapper.encryption_keys);
+ }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CFB>
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ dump_decryption_keys<algorithm>(wrapper.decryption_keys);
+ }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_OFB>
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>&)
+ { }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CTR>
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>&)
+ { }
}
diff --git a/utils/aes_decrypt_block.cpp b/utils/aes_decrypt_block.cpp
index bab5f50..0f41b47 100644
--- a/utils/aes_decrypt_block.cpp
+++ b/utils/aes_decrypt_block.cpp
@@ -25,48 +25,48 @@ namespace
std::deque<std::string>& ciphertexts,
bool verbose = false)
{
- typename aesni::aes::Types<algorithm>::BlockT iv;
+ typename aesni::Types<algorithm>::Block iv;
if (aesni::ModeRequiresInitializationVector<mode>())
{
if (ciphertexts.empty())
return false;
- aesni::aes::from_string(iv, ciphertexts.front());
+ aesni::from_string<algorithm>(iv, ciphertexts.front());
ciphertexts.pop_front();
if (verbose)
- dump_iv(iv);
+ dump_iv<algorithm>(iv);
}
- typename aesni::aes::Types<algorithm>::KeyT key;
- aesni::aes::from_string(key, key_str);
+ typename aesni::Types<algorithm>::Key key;
+ aesni::from_string<algorithm>(key, key_str);
if (verbose)
- dump_key(key);
+ dump_key<algorithm>(key);
- aesni::aes::Encrypt<algorithm, mode> encrypt(key, iv);
+ aesni::DecryptWrapper<algorithm, mode> decrypt(key, iv);
if (verbose)
- Dumper<algorithm, mode>::dump_round_keys(encrypt);
+ dump_wrapper<algorithm, mode>(decrypt);
while (!ciphertexts.empty())
{
- typename aesni::aes::Types<algorithm>::BlockT ciphertext;
- aesni::aes::from_string(ciphertext, ciphertexts.front());
+ typename aesni::Types<algorithm>::Block ciphertext, plaintext;
+ aesni::from_string<algorithm>(ciphertext, ciphertexts.front());
ciphertexts.pop_front();
- const auto plaintext = encrypt.decrypt(ciphertext);
+ decrypt.decrypt_block(ciphertext, plaintext);
if (verbose)
{
- dump_ciphertext(ciphertext);
- dump_plaintext(plaintext);
- Dumper<algorithm, mode>::dump_next_iv(encrypt);
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_plaintext<algorithm>(plaintext);
+ dump_next_iv<algorithm, mode>(decrypt);
}
else
{
- std::cout << aesni::aes::to_string(plaintext) << "\n";
+ std::cout << aesni::to_string<algorithm>(plaintext) << "\n";
}
}
@@ -102,7 +102,7 @@ namespace
}
}
- bool decrypt(
+ bool decrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
const std::string& key_str,
@@ -125,32 +125,13 @@ namespace
}
}
- bool decrypt_using_boxes(
- aesni::Algorithm algorithm,
+ template <aesni::Algorithm algorithm>
+ bool decrypt_using_boxes_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
aesni::Mode mode,
const std::string& key,
std::deque<std::string> ciphertexts)
{
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (algorithm)
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, key);
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, key);
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, key);
- break;
-
- default:
- return false;
- }
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
@@ -159,7 +140,7 @@ namespace
if (ciphertexts.empty())
return false;
- aesni::aes::from_string(iv.aes_block, ciphertexts.front());
+ aesni::from_string<algorithm>(iv.aes_block, ciphertexts.front());
iv_ptr = &iv;
ciphertexts.pop_front();
}
@@ -176,7 +157,7 @@ namespace
while (!ciphertexts.empty())
{
AesNI_BoxBlock ciphertext;
- aesni::aes::from_string(ciphertext.aes_block, ciphertexts.front());
+ aesni::from_string<algorithm>(ciphertext.aes_block, ciphertexts.front());
ciphertexts.pop_front();
AesNI_BoxBlock plaintext;
@@ -186,11 +167,44 @@ namespace
&plaintext,
aesni::ErrorDetailsThrowsInDestructor());
- std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n";
+ std::cout << aesni::to_string<algorithm>(plaintext.aes_block) << "\n";
}
return true;
}
+
+ bool decrypt_using_boxes(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ const std::string& key,
+ std::deque<std::string> ciphertexts)
+ {
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, key, ciphertexts);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, key, ciphertexts);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, key, ciphertexts);
+
+ default:
+ return false;
+ }
+ }
}
int main(int argc, char** argv)
@@ -228,7 +242,7 @@ int main(int argc, char** argv)
const auto success = cmd_parser.use_boxes()
? decrypt_using_boxes(algorithm, mode, key, ciphertexts)
- : decrypt(algorithm, mode, key, ciphertexts, cmd_parser.verbose());
+ : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose());
if (!success)
{
diff --git a/utils/aes_decrypt_bmp.cpp b/utils/aes_decrypt_bmp.cpp
index b9f669e..cf96847 100644
--- a/utils/aes_decrypt_bmp.cpp
+++ b/utils/aes_decrypt_bmp.cpp
@@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -58,72 +59,28 @@ namespace
ofs.open(path, std::ofstream::binary);
ofs.write(src.data(), src.size());
}
-}
-int main(int argc, char** argv)
-{
- try
+ template <aesni::Algorithm algorithm>
+ bool decrypt_bmp_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- CommandLineParser cmd_parser("aes_decrypt_bmp.exe");
-
- if (!cmd_parser.parse_options(argc, argv))
- return 0;
-
- auto args = cmd_parser.get_args();
-
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
-
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (cmd_parser.get_algorithm())
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, args.front());
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, args.front());
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, args.front());
- break;
- }
-
- args.pop_front();
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
- switch (cmd_parser.get_mode())
+ if (aesni::mode_requires_initialization_vector(mode))
{
- case AESNI_ECB:
- break;
-
- case AESNI_CBC:
- case AESNI_CFB:
- case AESNI_OFB:
- case AESNI_CTR:
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
- aesni::aes::from_string(iv.aes_block, args.front());
- iv_ptr = &iv;
- args.pop_front();
- break;
+ if (args.empty())
+ return false;
+
+ aesni::from_string<algorithm>(iv.aes_block, args.front());
+ iv_ptr = &iv;
+ args.pop_front();
}
if (args.size() != 2)
- {
- cmd_parser.print_usage();
- return 1;
- }
+ return false;
const auto src_path = args[0];
const auto dest_path = args[1];
@@ -140,9 +97,9 @@ int main(int argc, char** argv)
aesni_box_init(
&box,
- cmd_parser.get_algorithm(),
+ algorithm,
&algorithm_params,
- cmd_parser.get_mode(),
+ mode,
iv_ptr,
aesni::ErrorDetailsThrowsInDestructor());
@@ -171,6 +128,63 @@ int main(int argc, char** argv)
dest_buf.resize(header_size + pixels_size);
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool decrypt_bmp(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
+ {
+ if (args.empty())
+ return false;
+
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, args.front());
+ args.pop_front();
+ return decrypt_bmp_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, args.front());
+ args.pop_front();
+ return decrypt_bmp_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, args.front());
+ args.pop_front();
+ return decrypt_bmp_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_decrypt_bmp.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!decrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args()))
+ {
+ cmd_parser.print_usage();
+ return 1;
+ }
+
return 0;
}
catch (const boost::program_options::error& e)
diff --git a/utils/aes_decrypt_file.cpp b/utils/aes_decrypt_file.cpp
index 650ca5e..d456b67 100644
--- a/utils/aes_decrypt_file.cpp
+++ b/utils/aes_decrypt_file.cpp
@@ -16,6 +16,7 @@
#include <cstdlib>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -55,72 +56,28 @@ namespace
ofs.open(path, std::ofstream::binary);
ofs.write(src.data(), src.size());
}
-}
-int main(int argc, char** argv)
-{
- try
+ template <aesni::Algorithm algorithm>
+ bool decrypt_file_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- CommandLineParser cmd_parser("aes_encrypt_file.exe");
-
- if (!cmd_parser.parse_options(argc, argv))
- return 0;
-
- auto args = cmd_parser.get_args();
-
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
-
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (cmd_parser.get_algorithm())
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, args.front());
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, args.front());
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, args.front());
- break;
- }
-
- args.pop_front();
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
- switch (cmd_parser.get_mode())
+ if (aesni::mode_requires_initialization_vector(mode))
{
- case AESNI_ECB:
- break;
-
- case AESNI_CBC:
- case AESNI_CFB:
- case AESNI_OFB:
- case AESNI_CTR:
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
- aesni::aes::from_string(iv.aes_block, args.front());
- iv_ptr = &iv;
- args.pop_front();
- break;
+ if (args.empty())
+ return false;
+
+ aesni::from_string<algorithm>(iv.aes_block, args.front());
+ iv_ptr = &iv;
+ args.pop_front();
}
if (args.size() != 2)
- {
- cmd_parser.print_usage();
- return 1;
- }
+ return false;
const auto src_path = args[0];
const auto dest_path = args[1];
@@ -131,9 +88,9 @@ int main(int argc, char** argv)
aesni_box_init(
&box,
- cmd_parser.get_algorithm(),
+ algorithm,
&algorithm_params,
- cmd_parser.get_mode(),
+ mode,
iv_ptr,
aesni::ErrorDetailsThrowsInDestructor());
@@ -159,9 +116,65 @@ int main(int argc, char** argv)
aesni::ErrorDetailsThrowsInDestructor());
dest_buf.resize(dest_size);
-
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool decrypt_file(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
+ {
+ if (args.empty())
+ return false;
+
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_encrypt_file.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!decrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args()))
+ {
+ cmd_parser.print_usage();
+ return 1;
+ }
+
return 0;
}
catch (const boost::program_options::error& e)
diff --git a/utils/aes_encrypt_block.cpp b/utils/aes_encrypt_block.cpp
index b814e99..1b6b788 100644
--- a/utils/aes_encrypt_block.cpp
+++ b/utils/aes_encrypt_block.cpp
@@ -25,47 +25,47 @@ namespace
std::deque<std::string>& plaintexts,
bool verbose = false)
{
- typename aesni::aes::Types<algorithm>::BlockT iv;
+ typename aesni::Types<algorithm>::Block iv;
- if (aesni::ModeRequiresInitializationVector<mode>())
+ if (aesni::ModeRequiresInitializationVector<mode>::value)
{
if (plaintexts.empty())
return false;
- aesni::aes::from_string(iv, plaintexts.front());
+ aesni::from_string<algorithm>(iv, plaintexts.front());
plaintexts.pop_front();
if (verbose)
- dump_iv(iv);
+ dump_iv<algorithm>(iv);
}
- typename aesni::aes::Types<algorithm>::KeyT key;
- aesni::aes::from_string(key, key_str);
+ typename aesni::Types<algorithm>::Key key;
+ aesni::from_string<algorithm>(key, key_str);
if (verbose)
- dump_key(key);
+ dump_key<algorithm>(key);
- aesni::aes::Encrypt<algorithm, mode> encrypt(key, iv);
+ aesni::EncryptWrapper<algorithm, mode> encrypt(key, iv);
if (verbose)
- Dumper<algorithm, mode>::dump_round_keys(encrypt);
+ dump_wrapper<algorithm, mode>(encrypt);
while (!plaintexts.empty())
{
- typename aesni::aes::Types<algorithm>::BlockT plaintext;
- aesni::aes::from_string(plaintext, plaintexts.front());
+ typename aesni::Types<algorithm>::Block plaintext, ciphertext;
+ aesni::from_string<algorithm>(plaintext, plaintexts.front());
plaintexts.pop_front();
- const auto ciphertext = encrypt.encrypt(plaintext);
+ encrypt.encrypt_block(plaintext, ciphertext);
if (verbose)
{
- dump_plaintext(plaintext);
- dump_ciphertext(ciphertext);
- Dumper<algorithm, mode>::dump_next_iv(encrypt);
+ dump_plaintext<algorithm>(plaintext);
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_next_iv<algorithm, mode>(encrypt);
}
else
{
- std::cout << aesni::aes::to_string(ciphertext) << "\n";
+ std::cout << aesni::to_string<algorithm>(ciphertext) << "\n";
}
}
@@ -101,11 +101,11 @@ namespace
}
}
- bool encrypt(
+ bool encrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
const std::string& key_str,
- std::deque<std::string> plaintexts,
+ std::deque<std::string>& plaintexts,
bool verbose = false)
{
switch (algorithm)
@@ -124,32 +124,13 @@ namespace
}
}
- bool encrypt_using_boxes(
- aesni::Algorithm algorithm,
+ template <aesni::Algorithm algorithm>
+ bool encrypt_using_boxes_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
aesni::Mode mode,
const std::string& key,
std::deque<std::string> plaintexts)
{
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (algorithm)
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, key);
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, key);
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, key);
- break;
-
- default:
- return false;
- }
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
@@ -158,7 +139,7 @@ namespace
if (plaintexts.empty())
return false;
- aesni::aes::from_string(iv.aes_block, plaintexts.front());
+ aesni::from_string<AESNI_AES128>(iv.aes_block, plaintexts.front());
iv_ptr = &iv;
plaintexts.pop_front();
}
@@ -175,7 +156,7 @@ namespace
while (!plaintexts.empty())
{
AesNI_BoxBlock plaintext;
- aesni::aes::from_string(plaintext.aes_block, plaintexts.front());
+ aesni::from_string<algorithm>(plaintext.aes_block, plaintexts.front());
plaintexts.pop_front();
AesNI_BoxBlock ciphertext;
@@ -185,11 +166,44 @@ namespace
&ciphertext,
aesni::ErrorDetailsThrowsInDestructor());
- std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n";
+ std::cout << aesni::to_string<algorithm>(ciphertext.aes_block) << "\n";
}
return true;
}
+
+ bool encrypt_using_boxes(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ const std::string& key,
+ std::deque<std::string> plaintexts)
+ {
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, key, plaintexts);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, key, plaintexts);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, key, plaintexts);
+
+ default:
+ return false;
+ }
+ }
}
int main(int argc, char** argv)
@@ -227,7 +241,7 @@ int main(int argc, char** argv)
const auto success = cmd_parser.use_boxes()
? encrypt_using_boxes(algorithm, mode, key, plaintexts)
- : encrypt(algorithm, mode, key, plaintexts, cmd_parser.verbose());
+ : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose());
if (!success)
{
diff --git a/utils/aes_encrypt_bmp.cpp b/utils/aes_encrypt_bmp.cpp
index f36cfb0..2615889 100644
--- a/utils/aes_encrypt_bmp.cpp
+++ b/utils/aes_encrypt_bmp.cpp
@@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -58,72 +59,28 @@ namespace
ofs.open(path, std::ofstream::binary);
ofs.write(src.data(), src.size());
}
-}
-int main(int argc, char** argv)
-{
- try
+ template <aesni::Algorithm algorithm>
+ bool encrypt_bmp_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- CommandLineParser cmd_parser("aes_encrypt_bmp.exe");
-
- if (!cmd_parser.parse_options(argc, argv))
- return 0;
-
- auto args = cmd_parser.get_args();
-
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
-
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (cmd_parser.get_algorithm())
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, args.front());
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, args.front());
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, args.front());
- break;
- }
-
- args.pop_front();
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
- switch (cmd_parser.get_mode())
+ if (aesni::mode_requires_initialization_vector(mode))
{
- case AESNI_ECB:
- break;
-
- case AESNI_CBC:
- case AESNI_CFB:
- case AESNI_OFB:
- case AESNI_CTR:
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
- aesni::aes::from_string(iv.aes_block, args.front());
- iv_ptr = &iv;
- args.pop_front();
- break;
+ if (args.empty())
+ return false;
+
+ aesni::from_string<algorithm>(iv.aes_block, args.front());
+ iv_ptr = &iv;
+ args.pop_front();
}
if (args.size() != 2)
- {
- cmd_parser.print_usage();
- return 1;
- }
+ return false;
const auto src_path = args[0];
const auto dest_path = args[1];
@@ -140,9 +97,9 @@ int main(int argc, char** argv)
aesni_box_init(
&box,
- cmd_parser.get_algorithm(),
+ algorithm,
&algorithm_params,
- cmd_parser.get_mode(),
+ mode,
iv_ptr,
aesni::ErrorDetailsThrowsInDestructor());
@@ -170,6 +127,63 @@ int main(int argc, char** argv)
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool encrypt_bmp(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
+ {
+ if (args.empty())
+ return false;
+
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, args.front());
+ args.pop_front();
+ return encrypt_bmp_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, args.front());
+ args.pop_front();
+ return encrypt_bmp_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, args.front());
+ args.pop_front();
+ return encrypt_bmp_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_encrypt_bmp.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!encrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args()))
+ {
+ cmd_parser.print_usage();
+ return 1;
+ }
+
return 0;
}
catch (const boost::program_options::error& e)
diff --git a/utils/aes_encrypt_file.cpp b/utils/aes_encrypt_file.cpp
index 93b7cbd..61b9ba1 100644
--- a/utils/aes_encrypt_file.cpp
+++ b/utils/aes_encrypt_file.cpp
@@ -16,6 +16,7 @@
#include <cstdlib>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -55,72 +56,28 @@ namespace
ofs.open(path, std::ofstream::binary);
ofs.write(src.data(), src.size());
}
-}
-int main(int argc, char** argv)
-{
- try
+ template <aesni::Algorithm algorithm>
+ bool encrypt_file_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- CommandLineParser cmd_parser("aes_encrypt_file.exe");
-
- if (!cmd_parser.parse_options(argc, argv))
- return 0;
-
- auto args = cmd_parser.get_args();
-
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
-
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (cmd_parser.get_algorithm())
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, args.front());
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, args.front());
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, args.front());
- break;
- }
-
- args.pop_front();
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
- switch (cmd_parser.get_mode())
+ if (aesni::mode_requires_initialization_vector(mode))
{
- case AESNI_ECB:
- break;
-
- case AESNI_CBC:
- case AESNI_CFB:
- case AESNI_OFB:
- case AESNI_CTR:
- if (args.empty())
- {
- cmd_parser.print_usage();
- return 1;
- }
- aesni::aes::from_string(iv.aes_block, args.front());
- iv_ptr = &iv;
- args.pop_front();
- break;
+ if (args.empty())
+ return false;
+
+ aesni::from_string<algorithm>(iv.aes_block, args.front());
+ iv_ptr = &iv;
+ args.pop_front();
}
if (args.size() != 2)
- {
- cmd_parser.print_usage();
- return 1;
- }
+ return true;
const auto src_path = args[0];
const auto dest_path = args[1];
@@ -131,9 +88,9 @@ int main(int argc, char** argv)
aesni_box_init(
&box,
- cmd_parser.get_algorithm(),
+ algorithm,
&algorithm_params,
- cmd_parser.get_mode(),
+ mode,
iv_ptr,
aesni::ErrorDetailsThrowsInDestructor());
@@ -159,9 +116,65 @@ int main(int argc, char** argv)
aesni::ErrorDetailsThrowsInDestructor());
dest_buf.resize(dest_size);
-
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool encrypt_file(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
+ {
+ if (args.empty())
+ return false;
+
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, args.front());
+ args.pop_front();
+ return encrypt_file_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, args.front());
+ args.pop_front();
+ return encrypt_file_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, args.front());
+ args.pop_front();
+ return encrypt_file_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_encrypt_file.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!encrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args()))
+ {
+ cmd_parser.print_usage();
+ return 1;
+ }
+
return 0;
}
catch (const boost::program_options::error& e)