block/include/nil/crypto3/block/algorithm/encrypt.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_BLOCK_ENCRYPT_HPP
27 #define CRYPTO3_BLOCK_ENCRYPT_HPP
28 
29 #include <nil/crypto3/detail/type_traits.hpp>
30 
32 
36 
38 
39 namespace nil {
40  namespace crypto3 {
41  namespace block {
42  template<typename BlockCipher>
44  }
45 
64  template<typename BlockCipher, typename InputIterator, typename KeyInputIterator, typename OutputIterator,
65  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
66  OutputIterator encrypt(InputIterator first, InputIterator last, KeyInputIterator key_first,
67  KeyInputIterator key_last, OutputIterator out) {
68 
70  block::encryption_policy<BlockCipher>>::type EncryptionMode;
71  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
72 
73  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
75 
76  return EncrypterImpl(first, last, std::move(out),
77  CipherAccumulator(EncryptionMode(
78  BlockCipher(block::cipher_key<BlockCipher>(key_first, key_last).key))));
79  }
80 
98  template<typename BlockCipher, typename InputIterator, typename KeySinglePassRange, typename OutputIterator,
99  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
100  OutputIterator encrypt(InputIterator first, InputIterator last, const KeySinglePassRange &key,
101  OutputIterator out) {
102 
104  block::encryption_policy<BlockCipher>>::type EncryptionMode;
105  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
106 
107  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
109 
110  return EncrypterImpl(
111  first, last, std::move(out),
112  CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
113  }
114 
131  template<typename BlockCipher, typename InputIterator, typename OutputIterator,
132  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
133  OutputIterator encrypt(InputIterator first, InputIterator last, const block::cipher_key<BlockCipher> &key,
134  OutputIterator out) {
135 
137  block::encryption_policy<BlockCipher>>::type EncryptionMode;
138  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
139 
140  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
142 
143  return EncrypterImpl(first, last, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
144  }
145 
161  template<typename BlockCipher, typename InputIterator,
162  typename OutputAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
163  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
164  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
165  typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
166  OutputAccumulator>::type &
167  encrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) {
168 
169  typedef block::detail::ref_cipher_impl<OutputAccumulator> StreamEncrypterImpl;
171 
172  return EncrypterImpl(first, last, std::forward<OutputAccumulator>(acc));
173  }
174 
190  template<typename BlockCipher, typename SinglePassRange,
191  typename OutputAccumulator = typename block::accumulator_set<
192  typename block::modes::isomorphic<BlockCipher, block::nop_padding>::template bind<
194  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
195  typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
196  OutputAccumulator>::type &
197  encrypt(const SinglePassRange &r, OutputAccumulator &acc) {
198 
199  typedef block::detail::ref_cipher_impl<OutputAccumulator> StreamEncrypterImpl;
201 
202  return EncrypterImpl(r, acc);
203  }
204 
222  template<typename BlockCipher, typename InputIterator, typename KeyInputIterator,
223  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
224  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
225  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
226  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
227  encrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last) {
228 
230  block::encryption_policy<BlockCipher>>::type EncryptionMode;
231 
232  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
234 
235  return EncrypterImpl(first, last,
236  CipherAccumulator(EncryptionMode(
237  BlockCipher(block::cipher_key<BlockCipher>(key_first, key_last).key))));
238  }
239 
254  template<typename BlockCipher, typename InputIterator, typename KeySinglePassRange,
255  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
256  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
257  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
258  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
259  encrypt(InputIterator first, InputIterator last, const KeySinglePassRange &key) {
260 
262  block::encryption_policy<BlockCipher>>::type EncryptionMode;
263 
264  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
266 
267  return EncrypterImpl(
268  first, last, CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
269  }
270 
285  template<typename BlockCipher, typename InputIterator, typename K,
286  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
287  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
288  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
289  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
290  encrypt(InputIterator first, InputIterator last, std::initializer_list<K> key) {
291 
293  block::encryption_policy<BlockCipher>>::type EncryptionMode;
294 
295  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
297 
298  return EncrypterImpl(
299  first, last, CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
300  }
301 
315  template<typename BlockCipher, typename InputIterator,
316  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
317  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
318  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
319  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
320  encrypt(InputIterator first, InputIterator last, const block::cipher_key<BlockCipher> &key) {
321 
323  block::encryption_policy<BlockCipher>>::type EncryptionMode;
324 
325  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
327 
328  return EncrypterImpl(first, last, CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
329  }
330 
347  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange, typename OutputIterator,
348  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
349  OutputIterator encrypt(const SinglePassRange &rng, const KeySinglePassRange &key, OutputIterator out) {
350 
352  block::encryption_policy<BlockCipher>>::type EncryptionMode;
353  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
354 
355  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
357 
358  return EncrypterImpl(
359  rng, std::move(out),
360  CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
361  }
362 
377  template<typename BlockCipher, typename SinglePassRange, typename OutputIterator,
378  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
379  OutputIterator encrypt(const SinglePassRange &rng, const block::cipher_key<BlockCipher> &key,
380  OutputIterator out) {
381 
383  block::encryption_policy<BlockCipher>>::type EncryptionMode;
384  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
385 
386  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
388 
389  return EncrypterImpl(rng, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
390  }
391 
406  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange, typename OutputRange,
407  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
408  OutputRange &encrypt(const SinglePassRange &rng, const KeySinglePassRange &key, OutputRange &out) {
409 
411  block::encryption_policy<BlockCipher>>::type EncryptionMode;
412  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
413 
414  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
416 
417  return EncrypterImpl(
418  rng, std::move(out),
419  CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
420  }
421 
435  template<typename BlockCipher, typename SinglePassRange, typename OutputRange,
436  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
437  OutputRange &encrypt(const SinglePassRange &rng, const block::cipher_key<BlockCipher> &key, OutputRange &out) {
438 
440  block::encryption_policy<BlockCipher>>::type EncryptionMode;
441  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
442 
443  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
445 
446  return EncrypterImpl(rng, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
447  }
448 
464  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange,
465  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
466  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
467  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
468  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
469  encrypt(const SinglePassRange &r, const KeySinglePassRange &key) {
470 
472  block::encryption_policy<BlockCipher>>::type EncryptionMode;
473 
474  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
476 
477  return EncrypterImpl(
478  r, CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
479  }
480 
493  template<typename BlockCipher, typename SinglePassRange,
494  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
495  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>,
496  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
497  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
498  encrypt(const SinglePassRange &r, const block::cipher_key<BlockCipher> &key) {
499 
501  block::encryption_policy<BlockCipher>>::type EncryptionMode;
502 
503  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
505 
506  return EncrypterImpl(r, CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
507  }
508 
525  template<typename BlockCipher, typename T, typename K, typename OutputIterator>
526  OutputIterator encrypt(std::initializer_list<T> r, std::initializer_list<K> key, OutputIterator out) {
527 
529  block::encryption_policy<BlockCipher>>::type EncryptionMode;
530  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
531 
532  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
534 
535  return EncrypterImpl(
536  r, std::move(out),
537  CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
538  }
539 
554  template<typename BlockCipher, typename T, typename OutputIterator>
555  OutputIterator encrypt(std::initializer_list<T> r, const block::cipher_key<BlockCipher> &key,
556  OutputIterator out) {
557 
559  block::encryption_policy<BlockCipher>>::type EncryptionMode;
560  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
561 
562  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
564 
565  return EncrypterImpl(r, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
566  }
567 
582  template<typename BlockCipher, typename T, typename K, typename OutputRange>
583  OutputRange &encrypt(std::initializer_list<T> r, std::initializer_list<K> key, OutputRange &out) {
584 
586  block::encryption_policy<BlockCipher>>::type EncryptionMode;
587  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
588 
589  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
591 
592  return EncrypterImpl(
593  r, std::move(out),
594  CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
595  }
596 
610  template<typename BlockCipher, typename T, typename OutputRange>
611  OutputRange &encrypt(std::initializer_list<T> r, const block::cipher_key<BlockCipher> &key, OutputRange &out) {
612 
614  block::encryption_policy<BlockCipher>>::type EncryptionMode;
615  typedef typename block::accumulator_set<EncryptionMode> CipherAccumulator;
616 
617  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
619 
620  return EncrypterImpl(r, std::move(out), CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
621  }
622 
638  template<typename BlockCipher, typename T, typename K,
639  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
640  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>>
641  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
642  encrypt(std::initializer_list<T> il, std::initializer_list<K> key) {
643 
645  block::encryption_policy<BlockCipher>>::type EncryptionMode;
646 
647  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
649 
650  return EncrypterImpl(
651  il, CipherAccumulator(EncryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
652  }
653 
666  template<typename BlockCipher, typename SinglePassRange,
667  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
668  BlockCipher, block::nop_padding>::template bind<block::encryption_policy<BlockCipher>>::type>>
669  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
670  encrypt(std::initializer_list<SinglePassRange> r, const block::cipher_key<BlockCipher> &key) {
671 
673  block::encryption_policy<BlockCipher>>::type EncryptionMode;
674 
675  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamEncrypterImpl;
677 
678  return EncrypterImpl(r, CipherAccumulator(EncryptionMode(BlockCipher(key.key))));
679  }
680  } // namespace crypto3
681 } // namespace nil
682 
683 #endif // include guard
OutputIterator encrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last, OutputIterator out)
Definition: block/include/nil/crypto3/block/algorithm/encrypt.hpp:66
boost::accumulators::accumulator_set< digest< ProcessingMode::block_bits >, boost::accumulators::features< accumulators::tag::block< ProcessingMode > >, std::size_t > accumulator_set
Accumulator set with pre-defined block cipher accumulator params.
Definition: block/include/nil/crypto3/block/cipher_state.hpp:51
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
typename block::modes::isomorphic< BlockCipher, nop_padding >::encryption_policy encryption_policy
Definition: block/include/nil/crypto3/block/algorithm/encrypt.hpp:43
boost::accumulators::accumulator_set< mac::digest< MessageAuthenticationCode::input_block_bits >, boost::accumulators::features< accumulators::tag::mac< MessageAuthenticationCode > >> BlockCipher
Definition: cbc_mac_state.hpp:40
Definition: pair.hpp:31
Definition: cipher_key.hpp:37
key_type key
Definition: cipher_key.hpp:79
Definition: block/include/nil/crypto3/block/cipher_value.hpp:165
Definition: block/include/nil/crypto3/block/cipher_value.hpp:77
Definition: block/include/nil/crypto3/block/cipher_value.hpp:47
Definition: block/include/nil/crypto3/block/cipher_value.hpp:62
Definition: cipher_modes.hpp:130
detail::isomorphic_encryption_policy< cipher_type, padding_type > encryption_policy
Definition: cipher_modes.hpp:134