diff --git a/include/nil/crypto3/block/accumulators/block.hpp b/include/nil/crypto3/block/accumulators/block.hpp index 24ea2d7..092b9e5 100644 --- a/include/nil/crypto3/block/accumulators/block.hpp +++ b/include/nil/crypto3/block/accumulators/block.hpp @@ -54,6 +54,9 @@ namespace nil { typedef ::nil::crypto3::detail::injector injector_type; + typedef ::nil::crypto3::detail::packer + packer_type; + public: typedef digest result_type; @@ -69,15 +72,19 @@ namespace nil { } inline result_type result(boost::accumulators::dont_care) const { + using namespace ::nil::crypto3::detail; + result_type res = dgst; - block_type processed_block = mode.end_message(cache, previous_block, total_seen); + block_type processed_block = mode.end_message(cache, total_seen); + + packer_type::pack(processed_block.begin(), processed_block.end(), res.end()); - std::move(processed_block.begin(), processed_block.end(), std::inserter(new_dgst_part, new_dgst_part.end())); + /*std::move(processed_block.begin(), processed_block.end(), std::inserter(new_dgst_part, new_dgst_part.end())); res.insert(res.end(), processed_block.begin(), processed_block.end()); - std::reverse(res.begin(), res.end()); + std::reverse(res.begin(), res.end());*/ return res; } @@ -92,6 +99,10 @@ namespace nil { } inline void process_block() { + std::cout << "In process block" << std::endl; + + using namespace ::nil::crypto3::detail; + block_type processed_block; if (dgst.empty()) { processed_block = mode.begin_message(cache, total_seen); @@ -99,11 +110,11 @@ namespace nil { processed_block = mode.process_block(cache, total_seen); } - pack(processed_block.begin(), processed_block.end(), dgst.end()); + packer_type::pack(processed_block.begin(), processed_block.end(), dgst.end()); - std::move(processed_block.begin(), processed_block.end(), std::inserter(new_dgst_part, new_dgst_part.end())); + /*std::move(processed_block.begin(), processed_block.end(), std::inserter(new_dgst_part, new_dgst_part.end())); - res.insert(res.end(), processed_block.begin(), processed_block.end()); + res.insert(res.end(), processed_block.begin(), processed_block.end());*/ filled = false; } diff --git a/include/nil/crypto3/block/algorithm/encrypt.hpp b/include/nil/crypto3/block/algorithm/encrypt.hpp index 05f7dd0..e48da90 100644 --- a/include/nil/crypto3/block/algorithm/encrypt.hpp +++ b/include/nil/crypto3/block/algorithm/encrypt.hpp @@ -22,7 +22,7 @@ namespace nil { namespace crypto3 { - + template struct nop_padding { typedef std::size_t size_type; @@ -55,7 +55,7 @@ namespace nil { * * @return */ - template + /*template OutputIterator encrypt(InputIterator first, InputIterator last, KeyIterator key_first, KeyIterator key_last, OutputIterator out) { @@ -67,7 +67,7 @@ namespace nil { typedef block::detail::itr_cipher_impl EncrypterImpl; return EncrypterImpl(first, last, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key_first, key_last)))); - } + }*/ /*! * @brief @@ -84,7 +84,7 @@ namespace nil { * * @return */ - template::template bind>::type>> OutputAccumulator &encrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) { @@ -93,7 +93,7 @@ namespace nil { typedef block::detail::range_cipher_impl EncrypterImpl; return EncrypterImpl(first, last, std::forward(acc)); - } + }*/ /*! * @brief @@ -110,7 +110,7 @@ namespace nil { * @return */ - template< + /*template< typename BlockCipher, typename SinglePassRange, typename OutputAccumulator = typename block::accumulator_set::template bind EncrypterImpl; return EncrypterImpl(r, acc); - } + }*/ /*! * @brief @@ -140,7 +140,7 @@ namespace nil { * * @return */ - template::template bind>::type>> block::detail::range_cipher_impl> @@ -154,7 +154,7 @@ namespace nil { typedef block::detail::range_cipher_impl EncrypterImpl; return EncrypterImpl(first, last, CipherAccumulator(EncryptionMode(BlockCipher(key_first, key_last)))); - } + }*/ /*! * @brief @@ -172,7 +172,7 @@ namespace nil { * * @return */ - template + /*template OutputIterator encrypt(const SinglePassRange &rng, const KeyRange &key, OutputIterator out) { typedef typename block::modes::isomorphic::template bind>::type EncryptionMode; @@ -182,7 +182,7 @@ namespace nil { typedef block::detail::itr_cipher_impl EncrypterImpl; return EncrypterImpl(rng, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key)))); - } + }*/ /*! * @brief diff --git a/include/nil/crypto3/block/detail/block_stream_processor.hpp b/include/nil/crypto3/block/detail/block_stream_processor.hpp index 73d5199..e480363 100644 --- a/include/nil/crypto3/block/detail/block_stream_processor.hpp +++ b/include/nil/crypto3/block/detail/block_stream_processor.hpp @@ -20,6 +20,7 @@ #include #include +#include namespace nil { namespace crypto3 { @@ -31,14 +32,12 @@ namespace nil { typedef StateAccumulator accumulator_type; typedef Params params_type; - typedef typename mode_type::input_block_type input_block_type; - constexpr static const std::size_t input_block_bits = mode_type::input_block_bits; - - typedef typename mode_type::output_block_type output_block_type; - constexpr static const std::size_t output_block_bits = mode_type::output_block_bits; + typedef typename mode_type::block_type input_block_type; + constexpr static const std::size_t input_block_bits = mode_type::block_bits; public: - typedef typename params_type::endian_type endian_type; + typedef typename mode_type::endian_type endian_type; + typedef typename mode_type::input_endian_type input_endian_type; constexpr static const std::size_t value_bits = params_type::value_bits; typedef typename boost::uint_t::least value_type; @@ -49,18 +48,36 @@ namespace nil { private: constexpr static const std::size_t length_bits = params_type::length_bits; // FIXME: do something more intelligent than capping at 64 - constexpr static const std::size_t length_type_bits = - length_bits < input_block_bits ? input_block_bits : length_bits > 64 ? 64 : length_bits; - typedef typename boost::uint_t::least length_type; - - typedef ::nil::crypto3::detail::packer block_packer; + //constexpr static const std::size_t length_type_bits = + //length_bits < input_block_bits ? input_block_bits : length_bits > 64 ? 64 : length_bits; + typedef typename boost::uint_t<64>::least length_type; - BOOST_STATIC_ASSERT(!length_bits || length_bits % input_block_bits == 0); + //BOOST_STATIC_ASSERT(!length_bits || length_bits % input_block_bits == 0); BOOST_STATIC_ASSERT(input_block_bits % value_bits == 0); BOOST_STATIC_ASSERT(!length_bits || value_bits <= length_bits); + typedef ::nil::crypto3::detail::packer packer_type; +/* + template + typename std::enable_if::type + process_block(std::size_t block_seen = block_bits) { + acc(cache, accumulators::block_bits = block_seen); + } + template + typename std::enable_if::type + process_block(std::size_t block_seen = block_bits) { + using namespace nil::crypto3::detail; + // Convert the input into words + block_type block; + pack(cache, block); + // Process the block + acc(block, accumulators::block_bits = block_seen); + } + */ + + void update_one(value_type value) { std::size_t i = seen % input_block_bits; cache[i / value_bits] = value; @@ -68,7 +85,7 @@ namespace nil { if (i == input_block_bits - value_bits) { // Convert the input into words input_block_type block = {0}; - block_packer::pack(cache.begin(), cache.end(), block.begin()); + packer_type::pack(cache.begin(), cache.end(), block.begin()); // Process the block state(block); @@ -92,7 +109,7 @@ namespace nil { for (; n >= block_values; n -= block_values, first += block_values) { // Convert the input into words input_block_type block = {0}; - block_packer::pack(first, first + block_values, block.begin()); + packer_type::pack(first, first + block_values, block.begin()); seen += value_bits * block_values; state(block); @@ -117,7 +134,7 @@ namespace nil { virtual ~block_stream_processor() { if (!cache.empty()) { input_block_type block = {0}; - block_packer::pack(cache.begin(), cache.begin() + cache.size(), block.begin()); + packer_type::pack(cache.begin(), cache.begin() + cache.size(), block.begin()); typename input_block_type::const_iterator v = block.cbegin(); for (length_type itr = seen - (seen % input_block_bits); itr < seen; itr += value_bits) { state(*v++); diff --git a/include/nil/crypto3/block/detail/cipher_modes.hpp b/include/nil/crypto3/block/detail/cipher_modes.hpp index cc4ec72..daa7175 100644 --- a/include/nil/crypto3/block/detail/cipher_modes.hpp +++ b/include/nil/crypto3/block/detail/cipher_modes.hpp @@ -105,7 +105,7 @@ namespace nil { } - block_type end_message(const block_type &plaintext, std::size_t total_seen) { + block_type end_message(const block_type &plaintext, std::size_t total_seen) const { return policy_type::end_message(cipher, plaintext); } diff --git a/include/nil/crypto3/block/detail/rijndael/rijndael_impl.hpp b/include/nil/crypto3/block/detail/rijndael/rijndael_impl.hpp index 4043c44..17472ca 100644 --- a/include/nil/crypto3/block/detail/rijndael/rijndael_impl.hpp +++ b/include/nil/crypto3/block/detail/rijndael/rijndael_impl.hpp @@ -167,14 +167,15 @@ namespace nil { return state; } - + static void schedule_key(const key_type &key, key_schedule_type &encryption_key, key_schedule_type &decryption_key) { // the first key_words words are the original key - ::nil::crypto3::detail::pack( - key.begin(), key.begin() + policy_type::key_words * policy_type::word_bytes, - encryption_key.begin(), encryption_key.begin() + policy_type::key_words); + ::nil::crypto3::detail::packer::pack( + key.begin(), key.begin() + policy_type::key_words * policy_type::word_bytes, + encryption_key.begin()); #pragma clang loop unroll(full) for (std::size_t i = policy_type::key_words; i < policy_type::key_schedule_words; ++i) { @@ -189,8 +190,10 @@ namespace nil { } std::array bekey = {0}; - ::nil::crypto3::detail::pack(encryption_key, bekey); + ::nil::crypto3::detail::packer::pack( + encryption_key.begin(), encryption_key.end(), bekey.begin()); #pragma clang loop unroll(full) for (std::uint8_t round = 1; round < policy_type::rounds; ++round) { @@ -200,8 +203,10 @@ namespace nil { bekey.begin() + round * policy_type::block_bytes); } - ::nil::crypto3::detail::pack(bekey, decryption_key); + ::nil::crypto3::detail::packer::pack( + bekey.begin(), bekey.end(), decryption_key.begin()); } }; } // namespace detail diff --git a/include/nil/crypto3/block/rijndael.hpp b/include/nil/crypto3/block/rijndael.hpp index a527cef..ad91c29 100644 --- a/include/nil/crypto3/block/rijndael.hpp +++ b/include/nil/crypto3/block/rijndael.hpp @@ -139,22 +139,19 @@ namespace nil { constexpr static const std::uint8_t rounds = policy_type::rounds; typedef typename policy_type::round_constants_type round_constants_type; - template class Mode, typename StateAccumulator, std::size_t ValueBits, - typename Padding> + template struct stream_processor { struct params_type { - typedef typename stream_endian::little_octet_big_bit endian_type; - constexpr static const std::size_t value_bits = ValueBits; constexpr static const std::size_t length_bits = policy_type::word_bits * 2; }; - typedef block_stream_processor, Padding>, StateAccumulator, - params_type> - type_; + typedef block_stream_processor type; }; + typedef typename stream_endian::little_octet_big_bit endian_type; + rijndael(const key_type &key) : encryption_key({0}), decryption_key({0}) { impl_type::schedule_key(key, encryption_key, decryption_key); } diff --git a/include/nil/crypto3/detail/digest.hpp b/include/nil/crypto3/detail/digest.hpp index beb4c19..b64561e 100644 --- a/include/nil/crypto3/detail/digest.hpp +++ b/include/nil/crypto3/detail/digest.hpp @@ -188,8 +188,8 @@ namespace nil { a[i] = std::toupper(c, source.getloc()) - 'A' + 0xA; } } - detail::packer - ::pack(a.begin(), a.end(), d.begin()); + detail::packer::pack(a.begin(), + a.end(), d.begin()); return source; } } // namespace crypto3 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d3b8eca..bd58fe2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -41,9 +41,9 @@ endmacro() set(TESTS_NAMES # "pack" -# "rijndael" + "rijndael" # "aria" - "blowfish" +# "blowfish" # "camellia" # "cast" # "des" diff --git a/test/rijndael.cpp b/test/rijndael.cpp index bd66ab1..7564e77 100644 --- a/test/rijndael.cpp +++ b/test/rijndael.cpp @@ -188,40 +188,54 @@ inline bool operator!=(const byte_string &lhs, const byte_string &rhs) { template struct cipher_fixture { + +private: + typedef packer::key_type::value_type) * CHAR_BIT> + key_packer; + + typedef packer::block_type::value_type) * CHAR_BIT> + block_packer; + + typedef packer::block_type::value_type) * CHAR_BIT, + sizeof(byte_string::value_type) * CHAR_BIT> + text_packer; + +public: cipher_fixture(const std::string &ckey, const std::string &cplaintext, const std::string &ccipher_text) : original_plaintext(cplaintext), original_cipher_text(ccipher_text), cipher_text(ccipher_text.size() / 2), - plaintext(ccipher_text.size() / 2), c(key) { + plaintext(ccipher_text.size() / 2) { //c(key) { byte_string packed_string(ckey); - pack::key_type::value_type) * CHAR_BIT>(packed_string, key); - c = rijndael(key); + key_packer::pack(packed_string.begin(), packed_string.end(), key.begin()); + //c = rijndael(key); } - void encrypt() { + void encrypt_1() { typename rijndael::block_type block, result; - pack::block_type::value_type) * CHAR_BIT>(original_plaintext, block); - - result = c.encrypt(block); + block_packer::pack(original_plaintext.begin(), original_plaintext.end(), block.begin()); + + result = encrypt>(block, key); - pack::block_type::value_type) * CHAR_BIT, - sizeof(byte_string::value_type) * CHAR_BIT>(result, cipher_text); + text_packer::pack(result.begin(), result.end(), cipher_text.begin()); } void decrypt() { typename rijndael::block_type block, result; - pack::block_type::value_type) * CHAR_BIT>(cipher_text, block); + block_packer::pack(cipher_text.begin(), cipher_text.end(), block.begin()); + + result = decrypt(block.begin(), block.end(), key.begin(), key.end()); - result = c.decrypt(block); - pack::block_type::value_type) * CHAR_BIT, - sizeof(byte_string::value_type) * CHAR_BIT>(result, plaintext); + text_packer::pack(result.begin(), result.end(), plaintext.begin()); } typename rijndael::key_type key; const byte_string original_plaintext, original_cipher_text; byte_string cipher_text, plaintext; - rijndael c; + //rijndael c; }; BOOST_AUTO_TEST_SUITE(rijndael_cipher_test_suite) @@ -231,12 +245,13 @@ BOOST_AUTO_TEST_CASE(rijndael_128_128_cipher) { cipher_fixture<128, 128> f("000102030405060708090a0b0c0d0e0f", "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a"); - f.encrypt(); + + f.encrypt_1(); BOOST_CHECK_EQUAL(f.cipher_text, f.original_cipher_text); - f.decrypt(); - BOOST_CHECK_EQUAL(f.plaintext, f.original_plaintext); + //f.decrypt(); + //BOOST_CHECK_EQUAL(f.plaintext, f.original_plaintext); } - +/* BOOST_AUTO_TEST_CASE(rijndael_160_128_cipher) { cipher_fixture<160, 128> f("2b7e151628aed2a6abf7158809cf4f3c762e7160", "3243f6a8885a308d313198a2e0370734", "231d844639b31b412211cfe93712b880"); @@ -493,6 +508,6 @@ BOOST_AUTO_TEST_CASE(rijndael_256_256_cipher) { BOOST_CHECK_EQUAL(f.cipher_text, f.original_cipher_text); f.decrypt(); BOOST_CHECK_EQUAL(f.plaintext, f.original_plaintext); -} +}*/ BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file