blake2b.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 // Copyright (c) 2020 Nikita Kaskov <nbering@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_HASH_BLAKE2B_HPP
28 #define CRYPTO3_HASH_BLAKE2B_HPP
29 
32 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace hashes {
39  template<std::size_t DigestBits>
41  protected:
43 
44  typedef typename policy_type::state_type::value_type value_type;
45 
46  public:
48 
49  constexpr static const std::size_t word_bits = policy_type::word_bits;
51 
52  constexpr static const std::size_t state_bits = policy_type::state_bits;
53  constexpr static const std::size_t state_words = policy_type::state_words;
55 
56  constexpr static const std::size_t block_bits = policy_type::block_bits;
57  constexpr static const std::size_t block_words = policy_type::block_words;
59 
60  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
62  constexpr static const salt_type salt_value = policy_type::salt_value;
63 
64  static void process_block(state_type &state, const block_type &block, value_type seen = value_type(),
65  value_type finalizator = value_type()) {
66  std::array<word_type, state_words * 2> v;
67 
68  std::move(state.begin(), state.end(), v.begin());
69  std::move(iv_generator()().begin(), iv_generator()().end(), v.begin() + state_words);
70 
71  std::array<typename state_type::value_type, 2> s = {seen / CHAR_BIT + ((seen % CHAR_BIT) ? 1 : 0),
72  0x00};
73 
74  v[12] ^= s[0];
75  v[13] ^= s[1];
76 
77  s = {finalizator, 0};
78 
79  v[14] ^= s[0];
80  v[15] ^= s[1];
81 
82  s.fill(0);
83 
84  policy_type::template round<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>(v, block);
85  policy_type::template round<14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3>(v, block);
86  policy_type::template round<11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4>(v, block);
87  policy_type::template round<7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8>(v, block);
88  policy_type::template round<9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13>(v, block);
89  policy_type::template round<2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9>(v, block);
90  policy_type::template round<12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11>(v, block);
91  policy_type::template round<13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10>(v, block);
92  policy_type::template round<6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5>(v, block);
93  policy_type::template round<10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0>(v, block);
94  policy_type::template round<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>(v, block);
95  policy_type::template round<14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3>(v, block);
96 
97  for (size_t i = 0; i < state_words; i++) {
98  state[i] ^= v[i] ^ v[i + state_words];
99  }
100  }
101  };
102 
111  template<std::size_t DigestBits>
112  class blake2b {
114 
115  public:
116  constexpr static const std::size_t word_bits = policy_type::word_bits;
118 
119  constexpr static const std::size_t block_bits = policy_type::block_bits;
120  constexpr static const std::size_t block_words = policy_type::block_words;
122 
123  constexpr static const std::size_t digest_bits = DigestBits;
125 
126  struct construction {
127  struct params_type {
129 
130  constexpr static const std::size_t length_bits = policy_type::length_bits;
131  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
132  };
133 
137  };
138 
139  template<typename StateAccumulator, std::size_t ValueBits>
141  struct params_type {
143 
144  constexpr static const std::size_t value_bits = ValueBits;
145  };
146 
148  };
149  };
150 
151  } // namespace hashes
152  } // namespace crypto3
153 } // namespace nil
154 
155 #endif
policy_type::word_type word_type
Definition: blake2b.hpp:50
constexpr static const std::size_t state_words
Definition: blake2b.hpp:53
static void process_block(state_type &state, const block_type &block, value_type seen=value_type(), value_type finalizator=value_type())
Definition: blake2b.hpp:64
policy_type::salt_type salt_type
Definition: blake2b.hpp:61
constexpr static const std::size_t block_bits
Definition: blake2b.hpp:56
policy_type::iv_generator iv_generator
Definition: blake2b.hpp:47
constexpr static const std::size_t block_words
Definition: blake2b.hpp:57
policy_type::state_type state_type
Definition: blake2b.hpp:54
constexpr static const std::size_t state_bits
Definition: blake2b.hpp:52
constexpr static const std::size_t salt_bits
Definition: blake2b.hpp:60
constexpr static const std::size_t word_bits
Definition: blake2b.hpp:49
policy_type::block_type block_type
Definition: blake2b.hpp:58
policy_type::state_type::value_type value_type
Definition: blake2b.hpp:44
constexpr static const salt_type salt_value
Definition: blake2b.hpp:62
detail::blake2b_functions< DigestBits > policy_type
Definition: blake2b.hpp:42
Blake2b. A recently designed hashes function. Very fast on 64-bit processors. Can output a hashes of ...
Definition: blake2b.hpp:112
policy_type::word_type word_type
Definition: blake2b.hpp:117
constexpr static const std::size_t block_bits
Definition: blake2b.hpp:119
policy_type::digest_type digest_type
Definition: blake2b.hpp:124
policy_type::block_type block_type
Definition: blake2b.hpp:121
constexpr static const std::size_t word_bits
Definition: blake2b.hpp:116
constexpr static const std::size_t block_words
Definition: blake2b.hpp:120
constexpr static const std::size_t digest_bits
Definition: blake2b.hpp:123
This will do the usual Merkle-Damgård-style strengthening, padding with a 1 bit, then 0 bits as neede...
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:55
Definition: blake2b_padding.hpp:37
Definition: haifa_construction.hpp:55
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
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
constexpr static const std::size_t word_bits
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:41
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:42
policy_type::digest_endian digest_endian
Definition: blake2b.hpp:128
constexpr static const std::size_t digest_bits
Definition: blake2b.hpp:131
constexpr static const std::size_t length_bits
Definition: blake2b.hpp:130
haifa_construction< params_type, typename policy_type::iv_generator, blake2b_compressor< DigestBits >, detail::blake2b_padding< policy_type > > type
Definition: blake2b.hpp:136
constexpr static const std::size_t value_bits
Definition: blake2b.hpp:144
policy_type::digest_endian digest_endian
Definition: blake2b.hpp:142
block_stream_processor< construction, StateAccumulator, params_type > type
Definition: blake2b.hpp:147
Definition: blake2b_functions.hpp:40
policy_type::word_type word_type
Definition: blake2b_functions.hpp:43
constexpr static const std::size_t state_words
Definition: blake2b_functions.hpp:45
Definition: blake2b_policy.hpp:38
std::array< word_type, state_words > state_type
Definition: blake2b_policy.hpp:42
constexpr static const std::size_t block_bits
Definition: blake2b_policy.hpp:44
constexpr static const std::size_t state_bits
Definition: blake2b_policy.hpp:40
constexpr static const std::size_t length_bits
Definition: blake2b_policy.hpp:48
constexpr static const std::size_t block_words
Definition: blake2b_policy.hpp:45
boost::uint_t< salt_bits >::exact salt_type
Definition: blake2b_policy.hpp:67
std::array< word_type, block_words > block_type
Definition: blake2b_policy.hpp:46
constexpr static const std::size_t salt_bits
Definition: blake2b_policy.hpp:66
constexpr static const salt_type salt_value
Definition: blake2b_policy.hpp:68
constexpr static const std::size_t digest_bits
Definition: blake2b_policy.hpp:52
Definition: algebra/include/nil/crypto3/detail/stream_endian.hpp:45