tiger_padding.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Pavel Kharitonov <ipavrus@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_TIGER_PADDING_HPP
26 #define CRYPTO3_TIGER_PADDING_HPP
27 
28 #include <nil/crypto3/detail/inject.hpp>
29 #include <nil/crypto3/detail/pack.hpp>
30 #include <nil/crypto3/detail/octet.hpp>
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace hashes {
35  namespace detail {
36  template<typename Hash>
37  class tiger_padding {
38  typedef Hash policy_type;
39 
40  typedef typename policy_type::digest_endian endian_type;
41 
42  constexpr static const std::size_t word_bits = policy_type::word_bits;
43  typedef typename policy_type::word_type word_type;
44 
45  constexpr static const std::size_t state_bits = policy_type::state_bits;
46  constexpr static const std::size_t state_words = policy_type::state_words;
47  typedef typename policy_type::state_type state_type;
48 
49  constexpr static const std::size_t block_bits = policy_type::block_bits;
50  constexpr static const std::size_t block_words = policy_type::block_words;
51  typedef typename policy_type::block_type block_type;
52 
53  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
54  typedef typename policy_type::digest_type digest_type;
55 
56  typedef ::nil::crypto3::detail::injector<endian_type, word_bits, block_words, block_bits>
58 
59  public:
60  void operator()(block_type &block, std::size_t &block_seen) {
61  using namespace nil::crypto3::detail;
62  // Remove garbage
63  block_type block_of_zeros;
64  std::size_t seen_copy = block_seen;
65  std::fill(block_of_zeros.begin(), block_of_zeros.end(), 0);
66  injector_type::inject(block_of_zeros, block_bits - block_seen, block, seen_copy);
67 
68  // Get bit 1 in the endianness used by the hashes
69  std::array<octet_type, word_bits / octet_bits> bit_one = {{0x01}};
70  std::array<word_type, 1> bit_one_word {};
71  pack<stream_endian::big_octet_big_bit, endian_type, octet_bits, word_bits>(
72  bit_one.begin(), bit_one.end(), bit_one_word.begin());
73 
74  // Add 1 bit to block
75  injector_type::inject(bit_one_word[0], 8, block, block_seen);
76  }
77  };
78  } // namespace detail
79  } // namespace hashes
80  } // namespace crypto3
81 } // namespace nil
82 
83 #endif // CRYPTO3_TIGERS_PADDING_HPP
Definition: tiger_padding.hpp:37
void operator()(block_type &block, std::size_t &block_seen)
Definition: tiger_padding.hpp:60
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
boost::uint_t< octet_bits >::least octet_type
Definition: algebra/include/nil/crypto3/detail/octet.hpp:33
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