ofb.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_BLOCK_MODE_OUTPUT_FEEDBACK_HPP
26 #define CRYPTO3_BLOCK_MODE_OUTPUT_FEEDBACK_HPP
27 
30 
32 
33 //#include <nil/crypto3/codec/logic.hpp>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace block {
38  namespace modes {
39  namespace detail {
40  template<typename Cipher, typename Padding, template<typename> class Allocator = std::allocator>
41  struct ofb_policy {
42  typedef std::size_t size_type;
43 
44  typedef Cipher cipher_type;
45  typedef Padding padding_type;
46 
47  constexpr static const size_type block_bits = cipher_type::block_bits;
48  constexpr static const size_type 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>>> iv_type;
52  };
53 
54  template<typename Cipher, typename Padding, typename CiphertextStealingMode>
55  struct ofb_encryption_policy : public ofb_policy<Cipher, Padding> {};
56 
57  template<typename Cipher, typename Padding>
58  struct ofb_encryption_policy<Cipher, Padding, cts<0, Cipher, Padding>>
59  : public ofb_policy<Cipher, Padding> {
61 
64 
68 
70 
71  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
72  return plaintext;
73  }
74 
75  block_type process_block(const block_type &plaintext, const block_type &previous) {
76  return plaintext;
77  }
78 
79  block_type end_message(const block_type &plaintext, const block_type &previous,
80  const iv_type &iv) {
81  return plaintext;
82  }
83  };
84 
85  template<typename Cipher, typename Padding>
86  struct ofb_encryption_policy<Cipher, Padding, cts<1, Cipher, Padding>>
87  : public ofb_policy<Cipher, Padding> {
89 
92 
96 
98 
99  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
100  return plaintext;
101  }
102 
103  block_type process_block(const block_type &plaintext, const block_type &previous) {
104  return plaintext;
105  }
106 
107  block_type end_message(const block_type &plaintext, const block_type &previous,
108  const iv_type &iv) {
109  return plaintext;
110  }
111  };
112 
113  template<typename Cipher, typename Padding>
114  struct ofb_encryption_policy<Cipher, Padding, cts<2, Cipher, Padding>>
115  : public ofb_policy<Cipher, Padding> {
117 
120 
124 
126 
127  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
128  return plaintext;
129  }
130 
131  block_type process_block(const block_type &plaintext, const block_type &previous) {
132  return plaintext;
133  }
134 
135  block_type end_message(const block_type &plaintext, const block_type &previous,
136  const iv_type &iv) {
137  return plaintext;
138  }
139  };
140 
141  template<typename Cipher, typename Padding>
142  struct ofb_encryption_policy<Cipher, Padding, cts<3, Cipher, Padding>>
143  : public ofb_policy<Cipher, Padding> {
145 
148 
152 
154 
155  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
156  return plaintext;
157  }
158 
159  block_type process_block(const block_type &plaintext, const block_type &previous) {
160  return plaintext;
161  }
162 
163  block_type end_message(const block_type &plaintext, const block_type &previous,
164  const iv_type &iv) {
165  return plaintext;
166  }
167  };
168 
169  template<typename Cipher, typename Padding, typename CiphertextStealingMode>
170  struct ofb_decryption_policy : public ofb_policy<Cipher, Padding> {};
171 
172  template<typename Cipher, typename Padding>
173  struct ofb_decryption_policy<Cipher, Padding, cts<0, Cipher, Padding>>
174  : public ofb_policy<Cipher, Padding> {
176 
179 
183 
185 
186  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
187  return plaintext;
188  }
189 
190  block_type process_block(const block_type &plaintext, const block_type &previous) {
191  return plaintext;
192  }
193 
194  block_type end_message(const block_type &plaintext, const block_type &previous,
195  const iv_type &iv) {
196  return plaintext;
197  }
198  };
199 
200  template<typename Cipher, typename Padding>
201  struct ofb_decryption_policy<Cipher, Padding, cts<1, Cipher, Padding>>
202  : public ofb_policy<Cipher, Padding> {
204 
207 
211 
213 
214  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
215  return plaintext;
216  }
217 
218  block_type process_block(const block_type &plaintext, const block_type &previous) {
219  return plaintext;
220  }
221 
222  block_type end_message(const block_type &plaintext, const block_type &previous,
223  const iv_type &iv) {
224  return plaintext;
225  }
226  };
227 
228  template<typename Cipher, typename Padding>
229  struct ofb_decryption_policy<Cipher, Padding, cts<2, Cipher, Padding>>
230  : public ofb_policy<Cipher, Padding> {
232 
235 
239 
241 
242  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
243  return plaintext;
244  }
245 
246  block_type process_block(const block_type &plaintext, const block_type &previous) {
247  return plaintext;
248  }
249 
250  block_type end_message(const block_type &plaintext, const block_type &previous,
251  const iv_type &iv) {
252  return plaintext;
253  }
254  };
255 
256  template<typename Cipher, typename Padding>
257  struct ofb_decryption_policy<Cipher, Padding, cts<3, Cipher, Padding>>
258  : public ofb_policy<Cipher, Padding> {
260 
263 
267 
269 
270  block_type begin_message(const block_type &plaintext, const iv_type &iv) {
271  return plaintext;
272  }
273 
274  block_type process_block(const block_type &plaintext, const block_type &previous) {
275  return plaintext;
276  }
277 
278  block_type end_message(const block_type &plaintext, const block_type &previous,
279  const iv_type &iv) {
280  return plaintext;
281  }
282  };
283 
292  template<typename Policy>
294  typedef Policy policy_type;
295 
296  public:
297  typedef typename policy_type::cipher_type cipher_type;
298  typedef typename policy_type::padding_type padding_type;
299 
300  typedef typename policy_type::size_type size_type;
301 
302  typedef typename cipher_type::key_type key_type;
303  typedef typename policy_type::iv_type iv_type;
304 
305  constexpr static const size_type block_bits = policy_type::block_bits;
306  constexpr static const size_type block_words = policy_type::block_words;
307  typedef typename cipher_type::block_type block_type;
308 
310  }
311 
313  buf.fill(0);
314  }
315 
316  void encrypt(const void *key, const iv_type &iv, const void *in, void *out, size_type len) {
317  this->c_.encrypt(key, iv_, buf_);
318  while (len > 0) {
319  buf_[i] ^= in[i];
320  std::memcpy(out, buf_, bufsize);
321  this->c_.encrypt(key, buf_, buf_);
322  in += block_size;
323  out += block_size;
324  len -= block_size;
325  }
326  }
327 
328  void decrypt(const void *key, const void *iv, const void *in, void *out, size_type len) {
329  }
330 
331  private:
333  block_type buf;
334  };
335  } // namespace detail
336 
345  template<typename Cipher, template<typename> class Padding,
346  template<typename, template<typename> class> class CiphertextStealingMode = cts0>
348  typedef Cipher cipher_type;
349  typedef Padding<Cipher> padding_type;
350  typedef CiphertextStealingMode<Cipher, Padding> ciphertext_stealing_type;
351 
356 
357  template<template<typename, typename> class Policy>
358  struct bind {
360  };
361  };
362 
372  template<typename Cipher, template<typename> class Padding,
373  template<typename, template<typename> class> class CiphertextStealingMode = cts0>
375  } // namespace modes
376  } // namespace block
377  } // namespace crypto3
378 } // namespace nil
379 
380 #endif
Output Feedback Mode (OFB)
Definition: ofb.hpp:293
policy_type::size_type size_type
Definition: ofb.hpp:300
void encrypt(const void *key, const iv_type &iv, const void *in, void *out, size_type len)
Definition: ofb.hpp:316
policy_type::iv_type iv_type
Definition: ofb.hpp:303
output_feedback_mode(const cipher_type &cipher)
Definition: ofb.hpp:309
policy_type::padding_type padding_type
Definition: ofb.hpp:298
constexpr static const size_type block_words
Definition: ofb.hpp:306
cipher_type::key_type key_type
Definition: ofb.hpp:302
policy_type::cipher_type cipher_type
Definition: ofb.hpp:297
cipher_type::block_type block_type
Definition: ofb.hpp:307
void decrypt(const void *key, const void *iv, const void *in, void *out, size_type len)
Definition: ofb.hpp:328
constexpr static const size_type block_bits
Definition: ofb.hpp:305
ciphertext_stealing_mode< 0, Cipher, Padding > cts0
Definition: cts.hpp:107
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
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:206
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:218
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:222
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:214
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:178
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:186
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:190
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:194
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:246
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:250
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:234
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:242
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:270
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:274
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:262
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:278
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:103
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:99
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:91
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:107
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:71
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:79
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:63
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:75
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:131
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:119
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:127
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:135
block_type begin_message(const block_type &plaintext, const iv_type &iv)
Definition: ofb.hpp:155
ofb_policy< Cipher, Padding >::padding_type padding_type
Definition: ofb.hpp:147
block_type process_block(const block_type &plaintext, const block_type &previous)
Definition: ofb.hpp:159
block_type end_message(const block_type &plaintext, const block_type &previous, const iv_type &iv)
Definition: ofb.hpp:163
Cipher cipher_type
Definition: ofb.hpp:44
cipher_type::block_type block_type
Definition: ofb.hpp:49
Padding padding_type
Definition: ofb.hpp:45
std::size_t size_type
Definition: ofb.hpp:42
std::vector< boost::uint_t< CHAR_BIT >, Allocator< boost::uint_t< CHAR_BIT > > > iv_type
Definition: ofb.hpp:51
constexpr static const size_type block_words
Definition: ofb.hpp:48
constexpr static const size_type block_bits
Definition: ofb.hpp:47
detail::output_feedback_mode< Policy< cipher_type, padding_type > > type
Definition: ofb.hpp:359
Padding< Cipher > padding_type
Definition: ofb.hpp:349
detail::ofb_encryption_policy< cipher_type, padding_type, ciphertext_stealing_type > encryption_policy
Definition: ofb.hpp:353
Cipher cipher_type
Definition: ofb.hpp:348
CiphertextStealingMode< Cipher, Padding > ciphertext_stealing_type
Definition: ofb.hpp:350
detail::ofb_decryption_policy< cipher_type, padding_type, ciphertext_stealing_type > decryption_policy
Definition: ofb.hpp:355