sha3_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_SHA3_PADDING_HPP
26 #define CRYPTO3_SHA3_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>
37  class sha3_padding {
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  sha3_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) > 3) {
72  /*if (block_seen % octet_bits) {
73  pack<stream_endian::big_octet_big_bit, stream_endian::big_octet_little_bit,
74  word_bits, word_bits>(block.begin(), block.end(), block.begin());
75  }*/
76  // pad 011
77  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 2), 5), 3, block,
78  block_seen);
79  // pad 0*
80  block_type zeros;
81  std::fill(zeros.begin(), zeros.end(), 0);
82  injector_type::inject(zeros, block_bits - 1 - block_seen, block, block_seen);
83  // pad 1
84  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 1), 7), 1, block,
85  block_seen);
86  }
87 
88  else {
89  is_last = false;
90  std::size_t ind = block_bits - block_seen - 1;
91  block[block_words - 1] &= ~high_bits<word_bits>(~word_type(), ind + 1);
92  block[block_words - 1] |= high_bits<word_bits>(~word_type(), ind);
93  }
94  }
95 
96  void process_last(block_type &block, std::size_t &block_seen) {
97  using namespace nil::crypto3::detail;
98 
99  std::size_t ind = 3 - (block_bits - block_seen);
100  // Insert remaining padding bits
101  injector_type::inject(low_bits<word_bits>(~word_type(), ind), ind, block, block_seen);
102  // pad 0*
103  block_type zeros;
104  std::fill(zeros.begin(), zeros.end(), 0);
105  injector_type::inject(zeros, block_bits - 1 - ind, block, block_seen);
106  // pad 1
107  injector_type::inject(unbounded_shr(high_bits<word_bits>(~word_type(), 1), 7), 1, block,
108  block_seen);
109  }
110  };
111  } // namespace detail
112  } // namespace hashes
113  } // namespace crypto3
114 } // namespace nil
115 
116 #endif // CRYPTO3_SHA3_PADDING_HPP
Definition: sha3_padding.hpp:37
sha3_padding()
Definition: sha3_padding.hpp:61
void operator()(block_type &block, std::size_t &block_seen)
Definition: sha3_padding.hpp:68
void process_last(block_type &block, std::size_t &block_seen)
Definition: sha3_padding.hpp:96
bool is_last_block() const
Definition: sha3_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