merkle_damgard_padding.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Alexander Sokolov <asokolov@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020 Mikhail Komarov <nemo@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------////
26 
27 #ifndef CRYPTO3_MERKLE_DAMGARD_PADDING_HPP
28 #define CRYPTO3_MERKLE_DAMGARD_PADDING_HPP
29 
30 #include <nil/crypto3/detail/inject.hpp>
31 #include <nil/crypto3/detail/pack.hpp>
32 #include <nil/crypto3/detail/octet.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace hashes {
37  namespace detail {
38  template<typename Hash>
40  typedef Hash policy_type;
41 
42  typedef typename policy_type::digest_endian endian_type;
43 
44  constexpr static const std::size_t word_bits = policy_type::word_bits;
45  typedef typename policy_type::word_type word_type;
46 
47  constexpr static const std::size_t state_bits = policy_type::state_bits;
48  constexpr static const std::size_t state_words = policy_type::state_words;
49  typedef typename policy_type::state_type state_type;
50 
51  constexpr static const std::size_t block_bits = policy_type::block_bits;
52  constexpr static const std::size_t block_words = policy_type::block_words;
53  typedef typename policy_type::block_type block_type;
54 
55  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
56  typedef typename policy_type::digest_type digest_type;
57 
58  typedef ::nil::crypto3::detail::injector<endian_type, word_bits, block_words, block_bits>
60 
61  public:
62  void operator()(block_type &block, std::size_t &block_seen) {
63  using namespace nil::crypto3::detail;
64  // Remove garbage
65  block_type block_of_zeros;
66  std::size_t seen_copy = block_seen;
67  std::fill(block_of_zeros.begin(), block_of_zeros.end(), 0);
68  injector_type::inject(block_of_zeros, block_bits - block_seen, block, seen_copy);
69 
70  // Get bit 1 in the endianness used by the hashes
71  std::array<octet_type, word_bits / octet_bits> bit_one = {{0x80}};
72  std::array<word_type, 1> bit_one_word {};
73  pack<stream_endian::big_octet_big_bit, endian_type, octet_bits, word_bits>(
74  bit_one.begin(), bit_one.end(), bit_one_word.begin());
75 
76  // Add 1 bit to block
77  injector_type::inject(bit_one_word[0], 1, block, block_seen);
78  }
79  };
80  } // namespace detail
81  } // namespace hashes
82  } // namespace crypto3
83 } // namespace nil
84 
85 #endif // CRYPTO3_MERKLE_DAMGARD_PADDING_HPP
Definition: merkle_damgard_padding.hpp:39
void operator()(block_type &block, std::size_t &block_seen)
Definition: merkle_damgard_padding.hpp:62
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