keccak_padding.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Alexander Sokolov <asokolov@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_KECCAK_PADDING_HPP
26 #define CRYPTO3_KECCAK_PADDING_HPP
27 
29 #include <nil/crypto3/detail/inject.hpp>
30 #include <nil/crypto3/detail/unbounded_shift.hpp>
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace hashes {
35  namespace detail {
36  template<typename Hash>
38  typedef Hash policy_type;
39 
40  constexpr static const std::size_t word_bits = policy_type::word_bits;
41  typedef typename policy_type::word_type word_type;
42 
43  constexpr static const std::size_t state_bits = policy_type::state_bits;
44  constexpr static const std::size_t state_words = policy_type::state_words;
45  typedef typename policy_type::state_type state_type;
46 
47  constexpr static const std::size_t block_bits = policy_type::block_bits;
48  constexpr static const std::size_t block_words = policy_type::block_words;
49  typedef typename policy_type::block_type block_type;
50 
51  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
52  typedef typename policy_type::digest_type digest_type;
53 
54  typedef ::nil::crypto3::detail::injector<stream_endian::big_octet_little_bit, word_bits,
55  block_words, block_bits>
57 
58  bool is_last;
59 
60  public:
61  keccak_1600_padding() : is_last(true) {
62  }
63 
64  bool is_last_block() const {
65  return is_last;
66  }
67 
68  void operator()(block_type &block, std::size_t &block_seen) {
69  using namespace nil::crypto3::detail;
70 
71  if ((block_bits - block_seen) > 1) {
72  // try to handle bit NIST tests
73  /*if (block_seen % octet_bits) {
74  pack<stream_endian::big_octet_big_bit, stream_endian::big_octet_little_bit,
75  word_bits, word_bits>(block.begin(), block.end(), block.begin());
76  }*/
77  // pad 1
78  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 1), 7), 1, block,
79  block_seen);
80  // pad 0*
81  block_type zeros;
82  std::fill(zeros.begin(), zeros.end(), 0);
83  injector_type::inject(zeros, block_bits - 1 - block_seen, block, block_seen);
84  // pad 1
85  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 1), 7), 1, block,
86  block_seen);
87  }
88 
89  else {
90  is_last = false;
91  block[block_words - 1] &= ~high_bits<word_bits>(~word_type(), 1);
92  }
93  }
94 
95  void process_last(block_type &block, std::size_t &block_seen) {
96  using namespace nil::crypto3::detail;
97 
98  // pad 0*
99  block_type zeros;
100  std::fill(zeros.begin(), zeros.end(), 0);
101  injector_type::inject(zeros, block_bits - 1, block, block_seen);
102  // pad 1
103  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 1), 7), 1, block,
104  block_seen);
105  }
106  };
107  } // namespace detail
108  } // namespace hashes
109  } // namespace crypto3
110 } // namespace nil
111 
112 #endif // CRYPTO3_KECCAK_PADDING_HPP
Definition: keccak_padding.hpp:37
void operator()(block_type &block, std::size_t &block_seen)
Definition: keccak_padding.hpp:68
void process_last(block_type &block, std::size_t &block_seen)
Definition: keccak_padding.hpp:95
keccak_1600_padding()
Definition: keccak_padding.hpp:61
bool is_last_block() const
Definition: keccak_padding.hpp:64
constexpr matrix< T, N, M > fill(T value)
generates a matrix containing a single value
Definition: matrix/utility.hpp:102
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
T unbounded_shr(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:64
big_unit_little_bit< 8 > big_octet_little_bit
Definition: algebra/include/nil/crypto3/detail/stream_endian.hpp:62
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/detail/inject.hpp:260
static void inject(const block_type &b_src, std::size_t b_src_seen, block_type &b_dst, std::size_t &b_dst_seen, std::size_t block_shift=0)
Definition: block/include/nil/crypto3/detail/inject.hpp:267