Skip to content

Commit

Permalink
Block stream processor debugging #30
Browse files Browse the repository at this point in the history
  • Loading branch information
a1falcon committed May 16, 2020
1 parent ee93438 commit 880957d
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 76 deletions.
23 changes: 17 additions & 6 deletions include/nil/crypto3/block/accumulators/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ namespace nil {
typedef ::nil::crypto3::detail::injector<endian_type, word_bits, block_words, block_bits>
injector_type;

typedef ::nil::crypto3::detail::packer<endian_type, endian_type, word_bits, octet_bits>
packer_type;

public:
typedef digest<block_bits> result_type;

Expand All @@ -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;
}
Expand All @@ -92,18 +99,22 @@ 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);
} else {
processed_block = mode.process_block(cache, total_seen);
}

pack<endian_type>(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;
}
Expand Down
22 changes: 11 additions & 11 deletions include/nil/crypto3/block/algorithm/encrypt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace nil {
namespace crypto3 {

template<typename Cipher>
struct nop_padding {
typedef std::size_t size_type;
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace nil {
*
* @return
*/
template<typename BlockCipher, typename InputIterator, typename KeyIterator, typename OutputIterator>
/*template<typename BlockCipher, typename InputIterator, typename KeyIterator, typename OutputIterator>
OutputIterator encrypt(InputIterator first, InputIterator last, KeyIterator key_first, KeyIterator key_last,
OutputIterator out) {
Expand All @@ -67,7 +67,7 @@ namespace nil {
typedef block::detail::itr_cipher_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
return EncrypterImpl(first, last, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key_first, key_last))));
}
}*/

/*!
* @brief
Expand All @@ -84,7 +84,7 @@ namespace nil {
*
* @return
*/
template<typename BlockCipher, typename InputIterator,
/*template<typename BlockCipher, typename InputIterator,
typename OutputAccumulator = typename block::accumulator_set<
typename block::modes::isomorphic<BlockCipher, nop_padding>::template bind<encryption_policy<BlockCipher>>::type>>
OutputAccumulator &encrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) {
Expand All @@ -93,7 +93,7 @@ namespace nil {
typedef block::detail::range_cipher_impl<StreamEncrypterImpl> EncrypterImpl;
return EncrypterImpl(first, last, std::forward<OutputAccumulator>(acc));
}
}*/

/*!
* @brief
Expand All @@ -110,7 +110,7 @@ namespace nil {
* @return
*/

template<
/*template<
typename BlockCipher, typename SinglePassRange,
typename OutputAccumulator = typename block::accumulator_set<typename block::modes::isomorphic <
BlockCipher, nop_padding>::template bind <typename block::modes::isomorphic<BlockCipher,
Expand All @@ -121,7 +121,7 @@ namespace nil {
typedef block::detail::range_cipher_impl<StreamEncrypterImpl> EncrypterImpl;
return EncrypterImpl(r, acc);
}
}*/

/*!
* @brief
Expand All @@ -140,7 +140,7 @@ namespace nil {
*
* @return
*/
template<typename BlockCipher, typename InputIterator, typename KeyIterator,
/*template<typename BlockCipher, typename InputIterator, typename KeyIterator,
typename CipherAccumulator = typename block::accumulator_set<
typename block::modes::isomorphic<BlockCipher, nop_padding>::template bind<encryption_policy<BlockCipher>>::type>>
block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
Expand All @@ -154,7 +154,7 @@ namespace nil {
typedef block::detail::range_cipher_impl<StreamEncrypterImpl> EncrypterImpl;
return EncrypterImpl(first, last, CipherAccumulator(EncryptionMode(BlockCipher(key_first, key_last))));
}
}*/

/*!
* @brief
Expand All @@ -172,7 +172,7 @@ namespace nil {
*
* @return
*/
template<typename BlockCipher, typename SinglePassRange, typename KeyRange, typename OutputIterator>
/*template<typename BlockCipher, typename SinglePassRange, typename KeyRange, typename OutputIterator>
OutputIterator encrypt(const SinglePassRange &rng, const KeyRange &key, OutputIterator out) {
typedef typename block::modes::isomorphic<BlockCipher, nop_padding>::template bind<encryption_policy<BlockCipher>>::type EncryptionMode;
Expand All @@ -182,7 +182,7 @@ namespace nil {
typedef block::detail::itr_cipher_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
return EncrypterImpl(rng, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key))));
}
}*/

/*!
* @brief
Expand Down
49 changes: 33 additions & 16 deletions include/nil/crypto3/block/detail/block_stream_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <boost/utility/enable_if.hpp>

#include <boost/range/algorithm/copy.hpp>
#include <nil/crypto3/detail/stream_endian.hpp>

namespace nil {
namespace crypto3 {
Expand All @@ -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<value_bits>::least value_type;
Expand All @@ -49,26 +48,44 @@ 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<length_type_bits>::least length_type;

typedef ::nil::crypto3::detail::packer<stream_endian::little_octet_big_bit, endian_type,
value_bits, input_block_bits / block_values> 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<input_endian_type, endian_type, value_bits,
input_block_bits / block_values> packer_type;
/*
template<typename Endianness = input_endian_type>
typename std::enable_if<!(Endianness == stream_endian::big_octet_big_bit)>::type
process_block(std::size_t block_seen = block_bits) {
acc(cache, accumulators::block_bits = block_seen);
}
template<typename Endianness = input_endian_type>
typename std::enable_if<Endianness == stream_endian::big_octet_big_bit>::type
process_block(std::size_t block_seen = block_bits) {
using namespace nil::crypto3::detail;
// Convert the input into words
block_type block;
pack<endian_type, value_bits, word_bits>(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;
seen += value_bits;
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);
Expand All @@ -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);
Expand All @@ -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++);
Expand Down
2 changes: 1 addition & 1 deletion include/nil/crypto3/block/detail/cipher_modes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 14 additions & 9 deletions include/nil/crypto3/block/detail/rijndael/rijndael_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<stream_endian::little_octet_big_bit, CHAR_BIT,
policy_type::word_bits>(
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<stream_endian::big_octet_big_bit,
stream_endian::little_octet_big_bit, CHAR_BIT,
policy_type::word_bits>::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) {
Expand All @@ -189,8 +190,10 @@ namespace nil {
}

std::array<typename policy_type::byte_type, policy_type::key_schedule_bytes> bekey = {0};
::nil::crypto3::detail::pack<stream_endian::little_octet_big_bit, policy_type::word_bits,
CHAR_BIT>(encryption_key, bekey);
::nil::crypto3::detail::packer<stream_endian::little_octet_big_bit,
stream_endian::big_octet_big_bit, policy_type::word_bits,
CHAR_BIT>::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) {
Expand All @@ -200,8 +203,10 @@ namespace nil {
bekey.begin() + round * policy_type::block_bytes);
}

::nil::crypto3::detail::pack<stream_endian::little_octet_big_bit, CHAR_BIT,
policy_type::word_bits>(bekey, decryption_key);
::nil::crypto3::detail::packer<stream_endian::big_octet_big_bit,
stream_endian::little_octet_big_bit, CHAR_BIT,
policy_type::word_bits>::pack(
bekey.begin(), bekey.end(), decryption_key.begin());
}
};
} // namespace detail
Expand Down
11 changes: 4 additions & 7 deletions include/nil/crypto3/block/rijndael.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<template<typename, typename> class Mode, typename StateAccumulator, std::size_t ValueBits,
typename Padding>
template<class Mode, typename StateAccumulator, std::size_t ValueBits>
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<Mode<rijndael<KeyBits, BlockBits>, Padding>, StateAccumulator,
params_type>
type_;
typedef block_stream_processor<Mode, StateAccumulator, params_type> 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);
}
Expand Down
4 changes: 2 additions & 2 deletions include/nil/crypto3/detail/digest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ namespace nil {
a[i] = std::toupper(c, source.getloc()) - 'A' + 0xA;
}
}
detail::packer<stream_endian::big_bit, stream_endian::big_bit, 4, 8>
::pack(a.begin(), a.end(), d.begin());
detail::packer<stream_endian::big_bit, stream_endian::big_bit, 4, 8>::pack(a.begin(),
a.end(), d.begin());
return source;
}
} // namespace crypto3
Expand Down
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ endmacro()

set(TESTS_NAMES
# "pack"
# "rijndael"
"rijndael"
# "aria"
"blowfish"
# "blowfish"
# "camellia"
# "cast"
# "des"
Expand Down
Loading

0 comments on commit 880957d

Please sign in to comment.