block/include/nil/crypto3/block/algorithm/decrypt.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_DECRYPT_HPP
27 #define CRYPTO3_BLOCK_DECRYPT_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 decrypt(InputIterator first, InputIterator last, KeyInputIterator key_first,
67  KeyInputIterator key_last, OutputIterator out) {
68 
70  block::decryption_policy<BlockCipher>>::type DecryptionMode;
71  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
72 
73  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
75 
76  return DecrypterImpl(first, last, std::move(out),
77  CipherAccumulator(DecryptionMode(
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 decrypt(InputIterator first, InputIterator last, const KeySinglePassRange &key,
101  OutputIterator out) {
102 
104  block::decryption_policy<BlockCipher>>::type DecryptionMode;
105  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
106 
107  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
109 
110  return DecrypterImpl(
111  first, last, std::move(out),
112  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
113  }
114 
132  template<typename BlockCipher, typename InputIterator, typename K, typename OutputIterator,
133  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
134  OutputIterator decrypt(InputIterator first, InputIterator last, std::initializer_list<K> key,
135  OutputIterator out) {
136 
138  block::decryption_policy<BlockCipher>>::type DecryptionMode;
139  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
140 
141  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
143 
144  return DecrypterImpl(
145  first, last, std::move(out),
146  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
147  }
148 
165  template<typename BlockCipher, typename InputIterator, typename OutputIterator,
166  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
167  OutputIterator decrypt(InputIterator first, InputIterator last, const block::cipher_key<BlockCipher> &key,
168  OutputIterator out) {
169 
171  block::decryption_policy<BlockCipher>>::type DecryptionMode;
172  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
173 
174  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
176 
177  return DecrypterImpl(first, last, std::move(out), CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
178  }
179 
195  template<typename BlockCipher, typename InputIterator,
196  typename OutputAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
197  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
198  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
199  typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
200  OutputAccumulator>::type &
201  decrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) {
202 
203  typedef block::detail::ref_cipher_impl<OutputAccumulator> StreamDecrypterImpl;
205 
206  return DecrypterImpl(first, last, std::forward<OutputAccumulator>(acc));
207  }
208 
224  template<typename BlockCipher, typename SinglePassRange,
225  typename OutputAccumulator = typename block::accumulator_set<
226  typename block::modes::isomorphic<BlockCipher, block::nop_padding>::template bind<
228  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
229  typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
230  OutputAccumulator>::type &
231  decrypt(const SinglePassRange &r, OutputAccumulator &acc) {
232 
233  typedef block::detail::ref_cipher_impl<OutputAccumulator> StreamDecrypterImpl;
235 
236  return DecrypterImpl(r, acc);
237  }
238 
254  template<typename BlockCipher, typename T,
255  typename OutputAccumulator = typename block::accumulator_set<
256  typename block::modes::isomorphic<BlockCipher, block::nop_padding>::template bind<
258  typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
259  OutputAccumulator>::type &
260  decrypt(std::initializer_list<T> r, OutputAccumulator &acc) {
261 
262  typedef block::detail::ref_cipher_impl<OutputAccumulator> StreamDecrypterImpl;
264 
265  return DecrypterImpl(r, acc);
266  }
267 
285  template<typename BlockCipher, typename InputIterator, typename KeyInputIterator,
286  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
287  BlockCipher, block::nop_padding>::template bind<block::decryption_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  decrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last) {
291 
293  block::decryption_policy<BlockCipher>>::type DecryptionMode;
294 
295  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
297 
298  return DecrypterImpl(first, last,
299  CipherAccumulator(DecryptionMode(
300  BlockCipher(block::cipher_key<BlockCipher>(key_first, key_last).key))));
301  }
302 
317  template<typename BlockCipher, typename InputIterator, typename KeySinglePassRange,
318  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
319  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
320  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
321  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
322  decrypt(InputIterator first, InputIterator last, const KeySinglePassRange &key) {
323 
325  block::decryption_policy<BlockCipher>>::type DecryptionMode;
326 
327  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
329 
330  return DecrypterImpl(
331  first, last, CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
332  }
333 
348  template<typename BlockCipher, typename InputIterator, typename KeySinglePassRange,
349  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
350  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
351  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
352  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
353  decrypt(InputIterator first, InputIterator last, std::initializer_list<KeySinglePassRange> key) {
354 
356  block::decryption_policy<BlockCipher>>::type DecryptionMode;
357 
358  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
360 
361  return DecrypterImpl(
362  first, last, CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
363  }
364 
378  template<typename BlockCipher, typename InputIterator,
379  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
380  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
381  typename = typename std::enable_if<detail::is_iterator<InputIterator>::value>::type>
382  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
383  decrypt(InputIterator first, InputIterator last, const block::cipher_key<BlockCipher> &key) {
384 
386  block::decryption_policy<BlockCipher>>::type DecryptionMode;
387 
388  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
390 
391  return DecrypterImpl(first, last, CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
392  }
393 
410  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange, typename OutputIterator,
411  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
412  OutputIterator decrypt(const SinglePassRange &rng, const KeySinglePassRange &key, OutputIterator out) {
413 
415  block::decryption_policy<BlockCipher>>::type DecryptionMode;
416  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
417 
418  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
420 
421  return DecrypterImpl(
422  rng, std::move(out),
423  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
424  }
425 
442  template<typename BlockCipher, typename T, typename K, typename OutputIterator>
443  OutputIterator decrypt(std::initializer_list<T> rng, std::initializer_list<K> key, OutputIterator out) {
444 
446  block::decryption_policy<BlockCipher>>::type DecryptionMode;
447  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
448 
449  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
451 
452  return DecrypterImpl(
453  rng, std::move(out),
454  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
455  }
456 
471  template<typename BlockCipher, typename SinglePassRange, typename OutputIterator,
472  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
473  OutputIterator decrypt(const SinglePassRange &rng, const block::cipher_key<BlockCipher> &key,
474  OutputIterator out) {
475 
477  block::decryption_policy<BlockCipher>>::type DecryptionMode;
478  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
479 
480  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
482 
483  return DecrypterImpl(rng, std::move(out), CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
484  }
485 
500  template<typename BlockCipher, typename T, typename OutputIterator>
501  OutputIterator decrypt(std::initializer_list<T> rng, const block::cipher_key<BlockCipher> &key,
502  OutputIterator out) {
503 
505  block::decryption_policy<BlockCipher>>::type DecryptionMode;
506  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
507 
508  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
510 
511  return DecrypterImpl(rng, std::move(out), CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
512  }
513 
528  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange, typename OutputRange,
529  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
530  OutputRange &decrypt(const SinglePassRange &rng, const KeySinglePassRange &key, OutputRange &out) {
531 
533  block::decryption_policy<BlockCipher>>::type DecryptionMode;
534  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
535 
536  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
538 
539  return DecrypterImpl(
540  rng, std::move(out),
541  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
542  }
543 
558  template<typename BlockCipher, typename T, typename K, typename OutputRange>
559  OutputRange &decrypt(std::initializer_list<T> rng, std::initializer_list<K> key, OutputRange &out) {
560 
562  block::decryption_policy<BlockCipher>>::type DecryptionMode;
563  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
564 
565  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
567 
568  return DecrypterImpl(
569  rng, std::move(out),
570  CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
571  }
572 
586  template<typename BlockCipher, typename SinglePassRange, typename OutputRange,
587  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
588  OutputRange &decrypt(const SinglePassRange &rng, const block::cipher_key<BlockCipher> &key, OutputRange &out) {
589 
591  block::decryption_policy<BlockCipher>>::type DecryptionMode;
592  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
593 
594  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
596 
597  return DecrypterImpl(rng, std::move(out), CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
598  }
599 
613  template<typename BlockCipher, typename T, typename OutputRange>
614  OutputRange &decrypt(std::initializer_list<T> rng, const block::cipher_key<BlockCipher> &key,
615  OutputRange &out) {
616 
618  block::decryption_policy<BlockCipher>>::type DecryptionMode;
619  typedef typename block::accumulator_set<DecryptionMode> CipherAccumulator;
620 
621  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
623 
624  return DecrypterImpl(rng, std::move(out), CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
625  }
626 
642  template<typename BlockCipher, typename SinglePassRange, typename KeySinglePassRange,
643  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
644  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
645  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
646  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
647  decrypt(const SinglePassRange &r, const KeySinglePassRange &key) {
648 
650  block::decryption_policy<BlockCipher>>::type DecryptionMode;
651 
652  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
654 
655  return DecrypterImpl(
656  r, CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
657  }
658 
674  template<typename BlockCipher, typename T, typename K,
675  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
676  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>>
677  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
678  decrypt(std::initializer_list<T> r, std::initializer_list<K> key) {
679 
681  block::decryption_policy<BlockCipher>>::type DecryptionMode;
682 
683  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
685 
686  return DecrypterImpl(
687  r, CipherAccumulator(DecryptionMode(BlockCipher(block::cipher_key<BlockCipher>(key).key))));
688  }
689 
702  template<typename BlockCipher, typename SinglePassRange,
703  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
704  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>,
705  typename = typename std::enable_if<detail::is_range<SinglePassRange>::value>::type>
706  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
707  decrypt(const SinglePassRange &r, const block::cipher_key<BlockCipher> &key) {
708 
710  block::decryption_policy<BlockCipher>>::type DecryptionMode;
711 
712  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
714 
715  return DecrypterImpl(r, CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
716  }
717 
730  template<typename BlockCipher, typename T,
731  typename CipherAccumulator = typename block::accumulator_set<typename block::modes::isomorphic<
732  BlockCipher, block::nop_padding>::template bind<block::decryption_policy<BlockCipher>>::type>>
733  block::detail::range_cipher_impl<block::detail::value_cipher_impl<CipherAccumulator>>
734  decrypt(std::initializer_list<T> r, const block::cipher_key<BlockCipher> &key) {
735 
737  block::decryption_policy<BlockCipher>>::type DecryptionMode;
738 
739  typedef block::detail::value_cipher_impl<CipherAccumulator> StreamDecrypterImpl;
741 
742  return DecrypterImpl(r, CipherAccumulator(DecryptionMode(BlockCipher(key.key))));
743  }
744  } // namespace crypto3
745 } // namespace nil
746 
747 #endif // include guard
OutputIterator decrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last, OutputIterator out)
Definition: block/include/nil/crypto3/block/algorithm/decrypt.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
typename modes::isomorphic< BlockCipher, nop_padding >::decryption_policy decryption_policy
Definition: block/include/nil/crypto3/block/algorithm/decrypt.hpp:43
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
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_decryption_policy< cipher_type, padding_type > decryption_policy
Definition: cipher_modes.hpp:135