poly1305.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_POLY1305_HPP
26 #define CRYPTO3_MAC_POLY1305_HPP
27 
28 #include <memory>
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace mac {
40  class poly_1305 {
42 
43  public:
44  constexpr static const std::size_t block_bits = policy_type::block_bits;
45  constexpr static const std::size_t block_words = policy_type::block_words;
47 
48  constexpr static const std::size_t key_words = policy_type::key_words;
49  constexpr static const std::size_t key_bits = policy_type::key_bits;
50  typedef typename policy_type::key_type key_type;
51 
52  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
53  constexpr static const std::size_t key_schedule_words = policy_type::key_schedule_words;
55 
56  poly_1305(const key_type &key) {
57  schedule_key(key);
58  }
59 
61  if (m_buf_pos) {
62  buffer_insert(m_buf, m_buf_pos, input, length);
63 
64  if (m_buf_pos + length >= m_buf.size()) {
65  poly1305_blocks(m_poly, m_buf.data(), 1);
66  input += (m_buf.size() - m_buf_pos);
67  length -= (m_buf.size() - m_buf_pos);
68  m_buf_pos = 0;
69  }
70  }
71 
72  const size_t full_blocks = length / m_buf.size();
73  const size_t remaining = length % m_buf.size();
74 
75  if (full_blocks) {
76  poly1305_blocks(m_poly, input, full_blocks);
77  }
78 
79  buffer_insert(m_buf, m_buf_pos, input + full_blocks * m_buf.size(), remaining);
80  m_buf_pos += remaining;
81  }
82 
83  void end_message(const block_type &block) {
84  if (m_buf_pos != 0) {
85  m_buf[m_buf_pos] = 1;
86  const size_t len = m_buf.size() - m_buf_pos - 1;
87  if (len > 0) {
88  clear_mem(&m_buf[m_buf_pos + 1], len);
89  }
90  poly1305_blocks(m_poly, m_buf.data(), 1, true);
91  }
92 
93  poly1305_finish(m_poly, out);
94 
95  m_poly.clear();
96  m_buf_pos = 0;
97  }
98 
99  protected:
100  inline void schedule_key(const key_type &key) {
102  }
103 
105  };
106  } // namespace mac
107  } // namespace crypto3
108 } // namespace nil
109 
110 #endif
DJB's Poly1305.
Definition: poly1305.hpp:40
policy_type::key_type key_type
Definition: poly1305.hpp:50
policy_type::block_type block_type
Definition: poly1305.hpp:46
constexpr static const std::size_t key_words
Definition: poly1305.hpp:48
constexpr static const std::size_t key_bits
Definition: poly1305.hpp:49
poly_1305(const key_type &key)
Definition: poly1305.hpp:56
constexpr static const std::size_t key_schedule_bits
Definition: poly1305.hpp:52
constexpr static const std::size_t key_schedule_words
Definition: poly1305.hpp:53
void process_block(const block_type &block)
Definition: poly1305.hpp:60
key_schedule_type schedule
Definition: poly1305.hpp:104
policy_type::key_schedule_type key_schedule_type
Definition: poly1305.hpp:54
constexpr static const std::size_t block_words
Definition: poly1305.hpp:45
void schedule_key(const key_type &key)
Definition: poly1305.hpp:100
void end_message(const block_type &block)
Definition: poly1305.hpp:83
constexpr static const std::size_t block_bits
Definition: poly1305.hpp:44
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
void clear_mem(T *ptr, size_t n)
Definition: memory_operations.hpp:175
Definition: pair.hpp:31
Definition: poly1305_functions.hpp:39
constexpr static const std::size_t key_schedule_bits
Definition: poly1305_functions.hpp:49
static void poly1305_init(key_schedule_type &X, const key_type &key)
Definition: poly1305_functions.hpp:53
constexpr static const std::size_t key_words
Definition: poly1305_functions.hpp:45
policy_type::key_schedule_type key_schedule_type
Definition: poly1305_functions.hpp:51
constexpr static const std::size_t key_bits
Definition: poly1305_functions.hpp:46
constexpr static const std::size_t key_schedule_words
Definition: poly1305_functions.hpp:50
policy_type::key_type key_type
Definition: poly1305_functions.hpp:47
constexpr static const std::size_t block_words
Definition: poly1305_policy.hpp:46
constexpr static const std::size_t block_bits
Definition: poly1305_policy.hpp:47
std::array< word_type, block_words > block_type
Definition: poly1305_policy.hpp:48