// Copyright (c) 2015 Egor Tensin // 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 "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 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 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 void expand_key( const typename Types::Key& key, typename Types::RoundKeys& encryption_keys); template void derive_decryption_keys( const typename Types::RoundKeys& encryption_keys, typename Types::RoundKeys& decryption_keys); template ::value>::type* = nullptr> 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> void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block& ciphertext); template ::value>::type* = nullptr> 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> 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> void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block& plaintext); template ::value>::type* = nullptr> 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); } 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); } } 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; }; }