/** * \file * \author Egor Tensin * \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 #include #include namespace aes { template struct Types; template std::size_t get_number_of_rounds(); template void from_string( typename Types::Block&, const char*); template inline void from_string( typename Types::Block& dest, const std::string& src) { from_string(dest, src.c_str()); } template std::string to_string(const typename Types::Block&); template std::string to_matrix_string(const typename Types::Block&); template void from_string( typename Types::Key&, const char*); template inline void from_string( typename Types::Key& dest, const std::string& src) { from_string(dest, src.c_str()); } template std::string to_string(const typename Types::Key&); template inline void expand_key( const typename Types::Key& key, typename Types::RoundKeys& encryption_keys); template inline void derive_decryption_keys( const typename Types::RoundKeys& encryption_keys, typename Types::RoundKeys& decryption_keys); template ::value>::type* = nullptr> inline void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block& iv, typename Types::Block& ciphertext); template ::value>::type* = nullptr> inline void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block& ciphertext); template ::value>::type* = nullptr> inline void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block&, typename Types::Block& ciphertext) { encrypt_block(plaintext, round_keys, ciphertext); } template ::value>::type* = nullptr> inline void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block& iv, typename Types::Block& plaintext); template ::value>::type* = nullptr> inline void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block& plaintext); template ::value>::type* = nullptr> inline void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block&, typename Types::Block& plaintext) { decrypt_block(ciphertext, round_keys, plaintext); } template struct EncryptWrapper { EncryptWrapper( const typename Types::Key& key, const typename Types::Block& iv) : iv(iv) { expand_key(key, encryption_keys); } inline void encrypt_block( const typename Types::Block& plaintext, typename Types::Block& ciphertext) { aes::encrypt_block( plaintext, encryption_keys, iv, ciphertext); } typename Types::Block iv; typename Types::RoundKeys encryption_keys; }; template struct DecryptWrapper { DecryptWrapper( const typename Types::Key& key, const typename Types::Block& iv) : iv(iv) { typename Types::RoundKeys encryption_keys; expand_key(key, encryption_keys); if (ModeUsesEncryptionKeysOnly::value) { decryption_keys = encryption_keys; } else { derive_decryption_keys(encryption_keys, decryption_keys); } } inline void decrypt_block( const typename Types::Block& ciphertext, typename Types::Block& plaintext) { aes::decrypt_block( ciphertext, decryption_keys, iv, plaintext); } typename Types::Block iv; typename Types::RoundKeys decryption_keys; }; }