siphash.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2019 Mikhail Komarov <nemo@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_MAC_SIPHASH_HPP
26 #define CRYPTO3_MAC_SIPHASH_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace mac {
39  template<std::size_t Rounds = 2, std::size_t FinalRounds = 4>
40  class siphash {
42 
43  public:
44  constexpr static const std::size_t rounds = policy_type::rounds;
45  constexpr static const std::size_t final_rounds = policy_type::final_rounds;
46 
47  constexpr static const std::size_t word_bits = policy_type::word_bits;
49 
50  constexpr static const std::size_t key_bits = policy_type::key_bits;
51  constexpr static const std::size_t key_words = policy_type::key_words;
52  typedef typename policy_type::key_type key_type;
53 
54  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
55  constexpr static const std::size_t key_schedule_words = policy_type::key_schedule_words;
57 
58  siphash(key_schedule_type &schedule, const key_type &key) {
59  schedule_key(schedule, key);
60  }
61 
62  inline static void begin_message(key_schedule_type &schedule) {
63  return process(schedule);
64  }
65 
66  inline static void process(key_schedule_type &schdeule) {
67  // SipHash counts the message length mod 256
68  m_words += static_cast<uint8_t>(length);
69 
70  if (m_mbuf_pos) {
71  while (length && m_mbuf_pos != 8) {
72  m_mbuf = (m_mbuf >> 8) | (static_cast<uint64_t>(input[0]) << 56);
73  ++m_mbuf_pos;
74  ++input;
75  length--;
76  }
77 
78  if (m_mbuf_pos == 8) {
79  SipRounds(m_mbuf, m_V, m_C);
80  m_mbuf_pos = 0;
81  m_mbuf = 0;
82  }
83  }
84 
85  while (length >= 8) {
86  SipRounds(load_le<uint64_t>(input, 0), schdeule, m_C);
87  input += 8;
88  length -= 8;
89  }
90 
91  for (size_t i = 0; i != length; ++i) {
92  m_mbuf = (m_mbuf >> 8) | (static_cast<uint64_t>(input[i]) << 56);
93  m_mbuf_pos++;
94  }
95  }
96 
97  inline static void end_message(key_schedule_type &schdeule) {
98  if (m_mbuf_pos == 0) {
99  m_mbuf = (static_cast<uint64_t>(m_words) << 56);
100  } else if (m_mbuf_pos < 8) {
101  m_mbuf = (m_mbuf >> (64 - m_mbuf_pos * 8)) | (static_cast<uint64_t>(m_words) << 56);
102  }
103 
104  SipRounds(m_mbuf, m_V, m_C);
105 
106  schdeule[2] ^= 0xFF;
107  SipRounds(0, schdeule, m_D);
108 
109  const word_type X = schdeule[0] ^ schdeule[1] ^ schdeule[2] ^ schdeule[3];
110 
111  store_le(X, mac);
112 
113  clear();
114  }
115 
116  protected:
117  inline void schedule_key(key_schedule_type &schedule, const key_type &key) {
118  const word_type K0 = boost::endian::native_to_little(key[0]);
119  const word_type K1 = boost::endian::native_to_little(key[1]);
120 
121  schedule[0] = K0 ^ 0x736F6D6570736575;
122  schedule[1] = K1 ^ 0x646F72616E646F6D;
123  schedule[2] = K0 ^ 0x6C7967656E657261;
124  schedule[3] = K1 ^ 0x7465646279746573;
125  }
126  };
127  } // namespace mac
128  } // namespace crypto3
129 } // namespace nil
130 
131 #endif
Definition: siphash.hpp:40
constexpr static const std::size_t key_schedule_words
Definition: siphash.hpp:55
siphash(key_schedule_type &schedule, const key_type &key)
Definition: siphash.hpp:58
constexpr static const std::size_t key_schedule_bits
Definition: siphash.hpp:54
void schedule_key(key_schedule_type &schedule, const key_type &key)
Definition: siphash.hpp:117
constexpr static const std::size_t final_rounds
Definition: siphash.hpp:45
constexpr static const std::size_t key_words
Definition: siphash.hpp:51
policy_type::key_schedule_type key_schedule_type
Definition: siphash.hpp:56
static void begin_message(key_schedule_type &schedule)
Definition: siphash.hpp:62
static void end_message(key_schedule_type &schdeule)
Definition: siphash.hpp:97
policy_type::key_type key_type
Definition: siphash.hpp:52
constexpr static const std::size_t word_bits
Definition: siphash.hpp:47
constexpr static const std::size_t rounds
Definition: siphash.hpp:44
policy_type::word_type word_type
Definition: siphash.hpp:48
static void process(key_schedule_type &schdeule)
Definition: siphash.hpp:66
constexpr static const std::size_t key_bits
Definition: siphash.hpp:50
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
Definition: pair.hpp:31
Definition: siphash_functions.hpp:38
constexpr static const std::size_t final_rounds
Definition: siphash_functions.hpp:42
constexpr static const std::size_t key_bits
Definition: siphash_functions.hpp:47
constexpr static const std::size_t word_bits
Definition: siphash_functions.hpp:44
constexpr static const std::size_t rounds
Definition: siphash_functions.hpp:41
policy_type::key_schedule_type key_schedule_type
Definition: siphash_functions.hpp:53
constexpr static const std::size_t key_words
Definition: siphash_functions.hpp:48
constexpr static const std::size_t key_schedule_words
Definition: siphash_functions.hpp:52
policy_type::word_type word_type
Definition: siphash_functions.hpp:45
policy_type::key_type key_type
Definition: siphash_functions.hpp:49
constexpr static const std::size_t key_schedule_bits
Definition: siphash_functions.hpp:51