Skip to content

Commit

Permalink
Extend injector interface, add tests, add host endian
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Apr 28, 2024
1 parent 549e798 commit 4f708aa
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 235 deletions.
2 changes: 1 addition & 1 deletion include/nil/crypto3/block/accumulators/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace nil {
constexpr static const std::size_t value_bits = sizeof(typename block_type::value_type) * CHAR_BIT;
constexpr static const std::size_t block_values = block_bits / value_bits;

typedef ::nil::crypto3::detail::injector<endian_type, value_bits, block_values, block_bits>
typedef ::nil::crypto3::detail::injector<endian_type, endian_type, value_bits, block_values>
injector_type;

public:
Expand Down
4 changes: 2 additions & 2 deletions include/nil/crypto3/detail/endian_shift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ namespace nil {

std::size_t sz[2] = {UnitBits - shift_rem, shift_rem};
word_type masks[2] = {
unbounded_shr(high_bits<word_bits>(~word_type(), sz[0]), shift_unit_bits),
unbounded_shr(high_bits<word_bits>(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0])};
unbounded_shr(high_bits<word_bits, word_type>(~word_type(), sz[0]), shift_unit_bits),
unbounded_shr(high_bits<word_bits, word_type>(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0])};

std::size_t bits_left = word_bits - shift;
word_type w_combined = 0;
Expand Down
341 changes: 113 additions & 228 deletions include/nil/crypto3/detail/inject.hpp

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions include/nil/crypto3/detail/reverser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace nil {
b = unbounded_shr<16>(((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU);
#elif (BOOST_ARCH_CURRENT_WORD_BITS == 64)
b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
#else
#else
#error "BOOST_ARCH_CURRENT_WORD_BITS not set"
#endif
}
Expand Down Expand Up @@ -156,7 +156,7 @@ namespace nil {

/*!
* @brief bit_in_unit_reverser transforms the sequence of bits in each unit of
* the input value into reversed sequence of bytes in each unit of the output value.
* the input value into reversed sequence of bits in each unit of the output value.
* The function reverse is recursively invoked and the parameter k is used to track
* the number of already processed input units. The recursion ends, when all input
* units have been processed, i.e. when k == InputBits.
Expand Down Expand Up @@ -510,6 +510,38 @@ namespace nil {
return out;
}
};

/*!
* @brief reverser reverses both the sequence of units in the given value and with within a unit, if InputEndianness
* and OutputEndianness endiannesses have different unit orders, and the sequence of bits in each unit of the given value,
* if InputEndianness and OutputEndianness endiannesses have different bit orders.
*
* @ingroup reverser
*
* @tparam InputEndianness
* @tparam OutputEndianness
* @tparam UnitBits
*/
template<typename InputEndianness, typename OutputEndianness, int UnitBits>
class reverser {
private:
using unit_reverser_specified = unit_reverser<InputEndianness, OutputEndianness, UnitBits>;
using bit_reverser_specified = bit_reverser<InputEndianness, OutputEndianness, UnitBits>;
public:
template<typename ValueType, int ValueBits = sizeof(ValueType) * CHAR_BIT>
inline static void reverse(ValueType &val) {
unit_reverser_specified::reverse(val);
bit_reverser_specified::reverse(val);
}

template<typename ValueType, int ValueBits = sizeof(ValueType) * CHAR_BIT>
inline static ValueType reverse(ValueType const &val) {
ValueType out = unit_reverser_specified::reverse(val);
bit_reverser_specified::reverse(out);
return out;
}
};

} // namespace detail
} // namespace crypto3
} // namespace nil
Expand Down
15 changes: 15 additions & 0 deletions include/nil/crypto3/detail/stream_endian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef CRYPTO3_STREAM_ENDIAN_HPP
#define CRYPTO3_STREAM_ENDIAN_HPP

#include <boost/predef/other/endian.h>
#include <boost/static_assert.hpp>

#include <climits>
Expand All @@ -34,6 +35,7 @@ namespace nil {
namespace stream_endian {
// General versions; There should be no need to use these directly

// TODO: int -> std::size_t
template<int UnitBits>
struct big_unit_big_bit { };
template<int UnitBits>
Expand Down Expand Up @@ -65,6 +67,19 @@ namespace nil {

typedef host_unit<CHAR_BIT> host_byte;

using host_endian =
#ifdef BOOST_ENDIAN_BIG_BYTE_AVAILABLE
stream_endian::big_octet_big_bit;
#elif defined(BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE)
stream_endian::little_octet_big_bit;
#elif defined(BOOST_ENDIAN_BIG_WORD_AVAILABLE)
stream_endian::big_unit_big_bit<BOOST_ARCH_CURRENT_WORD_BITS>;
#elif defined(BOOST_ENDIAN_LITTLE_WORD_AVAILABLE)
stream_endian::little_unit_big_bit<BOOST_ARCH_CURRENT_WORD_BITS>;
#else
#error "Unknown endianness"
#endif

} // namespace stream_endian
} // namespace crypto3
} // namespace nil
Expand Down
5 changes: 3 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ macro(define_block_cipher_test name)
endmacro()

set(TESTS_NAMES
"pack"
"rijndael"
"injector"
"kasumi"
"md4"
"md5"
"pack"
"rijndael"
"shacal"
"shacal2")

Expand Down
Loading

0 comments on commit 4f708aa

Please sign in to comment.