aead.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 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_MODE_AEAD_MODE_HPP
26 #define CRYPTO3_MODE_AEAD_MODE_HPP
27 
28 #include <memory>
29 
30 #include <boost/integer.hpp>
31 
32 #include <nil/crypto3/detail/pack.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace block {
37  namespace modes {
38  namespace detail {
39  template<typename Cipher, typename Padding, template<typename> class Allocator>
41  typedef Cipher cipher_type;
42  typedef Padding padding_type;
43 
44  template<typename T>
45  using allocator_type = Allocator<T>;
46 
47  constexpr static const std::size_t block_bits = cipher_type::block_bits;
48  constexpr static const std::size_t block_words = cipher_type::block_words;
49  typedef typename cipher_type::block_type block_type;
50 
51  typedef std::vector<boost::uint_t<CHAR_BIT>, Allocator<boost::uint_t<CHAR_BIT>>>
53  };
54 
55  template<typename Cipher, typename Padding, template<typename> class Allocator>
57 
58  template<typename Cipher, typename Padding, template<typename> class Allocator>
60  : public aead_policy<Cipher, Padding, Allocator> {
61 
63 
66 
68 
69  constexpr static const std::size_t block_bits = policy_type::block_bits;
70  constexpr static const std::size_t block_words = policy_type::block_words;
72 
73  inline static block_type begin_message(const cipher_type &cipher, const block_type &plaintext) {
74  block_type block = {0};
75 
76  return cipher.encrypt(block);
77  }
78 
79  inline static block_type process_block(const cipher_type &cipher, const block_type &plaintext) {
80  block_type block = {0};
81 
82  return cipher.encrypt(block);
83  }
84 
85  inline static block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
86  block_type result = {0};
87 
88  return result;
89  }
90  };
91 
92  template<typename Cipher, typename Padding, template<typename> class Allocator>
95 
96  template<typename Cipher, typename Padding, template<typename> class Allocator>
98  : public aead_policy<Cipher, Padding, Allocator> {
99 
101 
104 
106 
107  constexpr static const std::size_t block_bits = policy_type::block_bits;
108  constexpr static const std::size_t block_words = policy_type::block_words;
110 
111  inline static block_type begin_message(const cipher_type &cipher, const block_type &plaintext) {
112  block_type block = {0};
113 
114  return cipher.encrypt(block);
115  }
116 
117  inline static block_type process_block(const cipher_type &cipher, const block_type &plaintext) {
118  block_type block = {0};
119 
120  return cipher.encrypt(block);
121  }
122 
123  inline static block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
124  block_type result = {0};
125 
126  return result;
127  }
128  };
129 
130  template<typename Cipher, typename Padding, template<typename> class Allocator>
133 
134  template<typename Policy>
136  typedef Policy policy_type;
137 
138  public:
139  typedef typename policy_type::cipher_type cipher_type;
140  typedef typename policy_type::padding_type padding_type;
141 
142  typedef typename cipher_type::key_type key_type;
143  typedef typename policy_type::authenticated_data_type authenticated_data_type;
144 
145  constexpr static const std::size_t block_bits = policy_type::block_bits;
146  constexpr static const std::size_t block_words = policy_type::block_words;
147  typedef typename cipher_type::block_type block_type;
148 
149  template<typename AssociatedDataContainer>
151  const AssociatedDataContainer &associated_data) :
152  cipher(cipher) {
153  schedule_associated_data(associated_data);
154  }
155 
156  block_type begin_message(const block_type &plaintext) {
157  return policy_type::begin_message(cipher, plaintext, ad);
158  }
159 
160  block_type process_block(const block_type &plaintext) {
161  return policy_type::process_block(cipher, plaintext, ad);
162  }
163 
164  block_type end_message(const block_type &plaintext) {
165  return policy_type::end_message(cipher, plaintext, ad);
166  }
167 
168  inline static std::size_t required_output_size(std::size_t inputlen) {
169  return padding_type::required_output_size(inputlen);
170  }
171 
172  protected:
173  template<typename AssociatedDataContainer>
174  inline void schedule_associated_data(const AssociatedDataContainer &iad) {
175  pack(iad, ad);
176  }
177 
179 
181  };
182 
183  template<typename Policy>
185  } // namespace detail
186 
198  template<typename Cipher,
199  template<typename>
200  class Padding,
201  template<typename> class Allocator = std::allocator>
203  typedef Cipher cipher_type;
204  typedef Padding<Cipher> padding_type;
205 
206  template<typename T>
207  using allocator_type = Allocator<T>;
208 
211 
212  template<template<typename, typename, template<typename> class> class Policy>
213  struct bind {
215  Policy<cipher_type, padding_type, allocator_type>>
217  };
218  };
219 
226  template<typename Cipher,
227  template<typename>
228  class Padding,
229  template<typename> class Allocator = std::allocator>
231  } // namespace modes
232  } // namespace block
233  } // namespace crypto3
234 } // namespace nil
235 
236 #endif
policy_type::authenticated_data_type authenticated_data_type
Definition: aead.hpp:143
static std::size_t required_output_size(std::size_t inputlen)
Definition: aead.hpp:168
block_type end_message(const block_type &plaintext)
Definition: aead.hpp:164
authenticated_encryption_associated_data(const cipher_type &cipher, const AssociatedDataContainer &associated_data)
Definition: aead.hpp:150
block_type begin_message(const block_type &plaintext)
Definition: aead.hpp:156
constexpr static const std::size_t block_words
Definition: aead.hpp:146
constexpr static const std::size_t block_bits
Definition: aead.hpp:145
void schedule_associated_data(const AssociatedDataContainer &iad)
Definition: aead.hpp:174
policy_type::cipher_type cipher_type
Definition: aead.hpp:139
block_type process_block(const block_type &plaintext)
Definition: aead.hpp:160
policy_type::padding_type padding_type
Definition: aead.hpp:140
void pack(InputIterator first, InputIterator last, std::random_access_iterator_tag, OutputIterator out)
Packs elements from the range [first, last) into elements starting from out. Works for input containe...
Definition: block/include/nil/crypto3/detail/pack.hpp:835
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: pair.hpp:31
Definition: cipher.hpp:38
detail::authenticated_encryption_associated_data< Policy< cipher_type, padding_type, allocator_type > > type
Definition: aead.hpp:216
Interface for AEAD (Authenticated Encryption with Associated Data) modes. These modes provide both en...
Definition: aead.hpp:202
detail::aead_decryption_policy< cipher_type, padding_type, allocator_type > decryption_policy
Definition: aead.hpp:210
detail::aead_encryption_policy< cipher_type, padding_type, allocator_type > encryption_policy
Definition: aead.hpp:209
static block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:123
aead_policy< Cipher, Padding, Allocator > policy_type
Definition: aead.hpp:100
static block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:111
static block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:117
policy_type::associated_data_type associated_data_type
Definition: aead.hpp:105
static block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:73
static block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:79
aead_policy< Cipher, Padding, Allocator > policy_type
Definition: aead.hpp:62
static block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: aead.hpp:85
constexpr static const std::size_t block_words
Definition: aead.hpp:48
constexpr static const std::size_t block_bits
Definition: aead.hpp:47
std::vector< boost::uint_t< CHAR_BIT >, Allocator< boost::uint_t< CHAR_BIT > > > associated_data_type
Definition: aead.hpp:52