cipher_modes.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_CIPHER_MODES_HPP
27 #define CRYPTO3_CIPHER_MODES_HPP
28 
29 #include <nil/crypto3/detail/stream_endian.hpp>
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace block {
34  namespace detail {
35 
36  template<typename Cipher, typename Padding>
38  typedef std::size_t size_type;
39 
40  typedef Cipher cipher_type;
41  typedef Padding padding_type;
42 
43  constexpr static const size_type block_bits = cipher_type::block_bits;
44  constexpr static const size_type block_words = cipher_type::block_words;
45  typedef typename cipher_type::block_type block_type;
46 
47  typedef typename cipher_type::endian_type endian_type;
48  };
49 
50  template<typename Cipher, typename Padding>
51  struct isomorphic_encryption_policy : public isomorphic_policy<Cipher, Padding> {
54 
55  inline static block_type begin_message(const cipher_type &cipher, const block_type &plaintext) {
56  return cipher.encrypt(plaintext);
57  }
58 
59  inline static block_type process_block(const cipher_type &cipher, const block_type &plaintext) {
60  return cipher.encrypt(plaintext);
61  }
62 
63  inline static block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
64  return cipher.encrypt(plaintext);
65  }
66  };
67 
68  template<typename Cipher, typename Padding>
69  struct isomorphic_decryption_policy : public isomorphic_policy<Cipher, Padding> {
72 
73  inline static block_type begin_message(const cipher_type &cipher, const block_type &ciphertext) {
74  return cipher.decrypt(ciphertext);
75  }
76 
77  inline static block_type process_block(const cipher_type &cipher, const block_type &ciphertext) {
78  return cipher.decrypt(ciphertext);
79  }
80 
81  inline static block_type end_message(const cipher_type &cipher, const block_type &ciphertext) {
82  return cipher.decrypt(ciphertext);
83  }
84  };
85 
86  template<typename Policy>
87  class isomorphic {
88  typedef Policy policy_type;
89 
90  public:
91  typedef typename policy_type::cipher_type cipher_type;
92  typedef typename policy_type::padding_type padding_type;
93 
94  typedef typename policy_type::size_type size_type;
95 
96  typedef typename cipher_type::key_type key_type;
97 
98  typedef typename policy_type::endian_type endian_type;
99 
100  typedef typename cipher_type::block_type block_type;
101  typedef typename cipher_type::word_type word_type;
102 
103  constexpr static const size_type block_bits = policy_type::block_bits;
104  constexpr static const size_type block_words = policy_type::block_words;
105  constexpr static const size_type word_bits = cipher_type::word_bits;
106 
108  }
109 
110  block_type begin_message(const block_type &plaintext, std::size_t total_seen) {
111  return policy_type::begin_message(cipher, plaintext);
112  }
113 
114  block_type process_block(const block_type &plaintext, std::size_t total_seen) {
115  return policy_type::process_block(cipher, plaintext);
116  }
117 
118  block_type end_message(const block_type &plaintext, std::size_t total_seen) const {
119  return policy_type::end_message(cipher, plaintext);
120  }
121 
122  protected:
124  };
125  } // namespace detail
126 
127  namespace modes {
128 
129  template<typename Cipher, template<typename> class Padding>
130  struct isomorphic {
131  typedef Cipher cipher_type;
132  typedef Padding<Cipher> padding_type;
133 
136 
137  template<typename Policy>
138  struct bind {
140  };
141  };
142  } // namespace modes
143  } // namespace block
144  } // namespace crypto3
145 } // namespace nil
146 
147 #endif // CRYPTO3_CIPHER_MODES_HPP
Definition: cipher_modes.hpp:87
cipher_type::key_type key_type
Definition: cipher_modes.hpp:96
policy_type::endian_type endian_type
Definition: cipher_modes.hpp:98
block_type end_message(const block_type &plaintext, std::size_t total_seen) const
Definition: cipher_modes.hpp:118
cipher_type::block_type block_type
Definition: cipher_modes.hpp:100
constexpr static const size_type block_bits
Definition: cipher_modes.hpp:103
constexpr static const size_type word_bits
Definition: cipher_modes.hpp:105
constexpr static const size_type block_words
Definition: cipher_modes.hpp:104
isomorphic(const cipher_type &cipher)
Definition: cipher_modes.hpp:107
block_type begin_message(const block_type &plaintext, std::size_t total_seen)
Definition: cipher_modes.hpp:110
cipher_type::word_type word_type
Definition: cipher_modes.hpp:101
cipher_type cipher
Definition: cipher_modes.hpp:123
policy_type::cipher_type cipher_type
Definition: cipher_modes.hpp:91
policy_type::size_type size_type
Definition: cipher_modes.hpp:94
policy_type::padding_type padding_type
Definition: cipher_modes.hpp:92
block_type process_block(const block_type &plaintext, std::size_t total_seen)
Definition: cipher_modes.hpp:114
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
isomorphic_policy< Cipher, Padding >::block_type block_type
Definition: cipher_modes.hpp:71
static block_type begin_message(const cipher_type &cipher, const block_type &ciphertext)
Definition: cipher_modes.hpp:73
isomorphic_policy< Cipher, Padding >::cipher_type cipher_type
Definition: cipher_modes.hpp:70
static block_type process_block(const cipher_type &cipher, const block_type &ciphertext)
Definition: cipher_modes.hpp:77
static block_type end_message(const cipher_type &cipher, const block_type &ciphertext)
Definition: cipher_modes.hpp:81
static block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: cipher_modes.hpp:55
static block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: cipher_modes.hpp:59
static block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: cipher_modes.hpp:63
isomorphic_policy< Cipher, Padding >::block_type block_type
Definition: cipher_modes.hpp:53
isomorphic_policy< Cipher, Padding >::cipher_type cipher_type
Definition: cipher_modes.hpp:52
Definition: cipher_modes.hpp:37
cipher_type::block_type block_type
Definition: cipher_modes.hpp:45
cipher_type::endian_type endian_type
Definition: cipher_modes.hpp:47
std::size_t size_type
Definition: cipher_modes.hpp:38
Cipher cipher_type
Definition: cipher_modes.hpp:40
constexpr static const size_type block_bits
Definition: cipher_modes.hpp:43
Padding padding_type
Definition: cipher_modes.hpp:41
constexpr static const size_type block_words
Definition: cipher_modes.hpp:44
Definition: cipher_modes.hpp:138
detail::isomorphic< Policy > type
Definition: cipher_modes.hpp:139
Definition: cipher_modes.hpp:130
detail::isomorphic_encryption_policy< cipher_type, padding_type > encryption_policy
Definition: cipher_modes.hpp:134
Padding< Cipher > padding_type
Definition: cipher_modes.hpp:132
Cipher cipher_type
Definition: cipher_modes.hpp:131
detail::isomorphic_decryption_policy< cipher_type, padding_type > decryption_policy
Definition: cipher_modes.hpp:135