haifa_construction.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Alexander Sokolov <asokolov@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_HASH_HAIFA_CONSTRUCTION_HPP
27 #define CRYPTO3_HASH_HAIFA_CONSTRUCTION_HPP
28 
30 
31 #include <nil/crypto3/detail/static_digest.hpp>
32 #include <nil/crypto3/detail/pack.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace hashes {
53  template<typename Params, typename IV, typename Compressor, typename Padding,
54  typename Finalizer = detail::nop_finalizer>
56  public:
57  typedef Compressor compressor_functor;
58  typedef Padding padding_functor;
59  typedef Finalizer finalizer_functor;
60 
61  typedef typename Params::digest_endian endian_type;
62 
63  constexpr static const std::size_t salt_bits = compressor_functor::salt_bits;
64  typedef typename compressor_functor::salt_type salt_type;
65  constexpr static const salt_type salt_value = compressor_functor::salt_value;
66 
67  typedef typename compressor_functor::iv_generator iv_generator;
68 
69  constexpr static const std::size_t word_bits = compressor_functor::word_bits;
70  typedef typename compressor_functor::word_type word_type;
71 
72  constexpr static const std::size_t state_bits = compressor_functor::state_bits;
73  constexpr static const std::size_t state_words = compressor_functor::state_words;
74  typedef typename compressor_functor::state_type state_type;
75 
76  constexpr static const std::size_t block_bits = compressor_functor::block_bits;
77  constexpr static const std::size_t block_words = compressor_functor::block_words;
78  typedef typename compressor_functor::block_type block_type;
79 
80  constexpr static const std::size_t digest_bits = Params::digest_bits;
81  constexpr static const std::size_t digest_bytes = digest_bits / octet_bits;
82  constexpr static const std::size_t digest_words =
83  digest_bits / word_bits + ((digest_bits % word_bits) ? 1 : 0);
85 
86  protected:
87  constexpr static const std::size_t length_bits = Params::length_bits;
88  // FIXME: do something more intelligent than capping at 64
89  constexpr static const std::size_t length_type_bits = length_bits < word_bits ? word_bits :
90  length_bits > 64 ? 64 :
92  typedef typename boost::uint_t<length_type_bits>::least length_type;
93  constexpr static const std::size_t length_words = length_bits / word_bits;
95 
96  public:
97  template<typename Integer = std::size_t>
98  inline haifa_construction &process_block(const block_type &block, Integer seen,
99  Integer finalization = 0) {
100  compressor_functor::process_block(state_, block, seen, finalization);
101  return *this;
102  }
103 
105  std::size_t total_seen = length_type()) {
106  using namespace nil::crypto3::detail;
107 
108  block_type b = block;
109  // Process block if it is full
110  if (total_seen && !(total_seen % block_bits))
111  process_block(b, total_seen);
112 
113  // Pad last message block
114  padding_functor padding;
115  padding(b, total_seen);
116 
117  // Process last block
118  process_block(b, total_seen, salt_value);
119 
120  // Apply finalizer
121  finalizer_functor()(state_);
122 
123  // Convert digest to byte representation
124  std::array<octet_type, state_bits / octet_bits> d_full;
125  pack_from<endian_type, word_bits, octet_bits>(state_.begin(), state_.end(), d_full.begin());
126  digest_type d;
127  std::copy(d_full.begin(), d_full.begin() + digest_bytes, d.begin());
128 
129  return d;
130  }
131 
133  reset();
134  }
135 
136  void reset(const state_type &s) {
137  state_ = s;
138  state_[0] ^= 0x01010000U ^ (digest_bits / CHAR_BIT);
139  }
140 
141  void reset() {
142  iv_generator iv;
143  reset(iv());
144  }
145 
146  state_type const &state() const {
147  return state_;
148  }
149 
150  private:
151  state_type state_;
152  };
153 
154  } // namespace hashes
155  } // namespace crypto3
156 } // namespace nil
157 
158 #endif // CRYPTO3_HASH_HAIFA_CONSTRUCTION_HPP
Definition: haifa_construction.hpp:55
BOOST_STATIC_ASSERT(!length_bits||length_bits % word_bits==0)
Padding padding_functor
Definition: haifa_construction.hpp:58
constexpr static const std::size_t block_bits
Definition: haifa_construction.hpp:76
constexpr static const std::size_t word_bits
Definition: haifa_construction.hpp:69
constexpr static const std::size_t length_bits
Definition: haifa_construction.hpp:87
Compressor compressor_functor
Definition: haifa_construction.hpp:57
compressor_functor::iv_generator iv_generator
Definition: haifa_construction.hpp:67
constexpr static const std::size_t digest_bytes
Definition: haifa_construction.hpp:81
constexpr static const std::size_t state_bits
Definition: haifa_construction.hpp:72
digest_type digest(const block_type &block=block_type(), std::size_t total_seen=length_type())
Definition: haifa_construction.hpp:104
Params::digest_endian endian_type
Definition: haifa_construction.hpp:61
void reset()
Definition: haifa_construction.hpp:141
constexpr static const std::size_t state_words
Definition: haifa_construction.hpp:73
compressor_functor::salt_type salt_type
Definition: haifa_construction.hpp:64
constexpr static const std::size_t salt_bits
Definition: haifa_construction.hpp:63
Finalizer finalizer_functor
Definition: haifa_construction.hpp:59
constexpr static const salt_type salt_value
Definition: haifa_construction.hpp:65
void reset(const state_type &s)
Definition: haifa_construction.hpp:136
constexpr static const std::size_t length_words
Definition: haifa_construction.hpp:93
static_digest< digest_bits > digest_type
Definition: haifa_construction.hpp:84
constexpr static const std::size_t digest_bits
Definition: haifa_construction.hpp:80
constexpr static const std::size_t block_words
Definition: haifa_construction.hpp:77
boost::uint_t< length_type_bits >::least length_type
Definition: haifa_construction.hpp:92
haifa_construction & process_block(const block_type &block, Integer seen, Integer finalization=0)
Definition: haifa_construction.hpp:98
constexpr static const std::size_t length_type_bits
Definition: haifa_construction.hpp:89
haifa_construction()
Definition: haifa_construction.hpp:132
compressor_functor::state_type state_type
Definition: haifa_construction.hpp:74
compressor_functor::block_type block_type
Definition: haifa_construction.hpp:78
compressor_functor::word_type word_type
Definition: haifa_construction.hpp:70
state_type const & state() const
Definition: haifa_construction.hpp:146
constexpr static const std::size_t digest_words
Definition: haifa_construction.hpp:82
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
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