ecb.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_ELECTRONIC_CODE_BOOK_HPP
26 #define CRYPTO3_BLOCK_MODE_ELECTRONIC_CODE_BOOK_HPP
27 
28 #include <boost/integer.hpp>
29 
32 
34 
35 //#include <nil/crypto3/codec/logic.hpp>
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace block {
40  namespace modes {
41  namespace detail {
42  template<typename Cipher, typename Padding, template<typename> class Allocator = std::allocator>
43  struct ecb_policy {
44  typedef std::size_t size_type;
45 
46  typedef Cipher cipher_type;
47  typedef Padding padding_type;
48 
49  constexpr static const size_type block_bits = cipher_type::block_bits;
50  constexpr static const size_type block_words = cipher_type::block_words;
51  typedef typename cipher_type::block_type block_type;
52  };
53 
54  template<typename Cipher, typename Padding, typename CiphertextStealingMode>
55  struct ecb_encryption_policy : public ecb_policy<Cipher, Padding> {};
56 
57  template<typename Cipher, typename Padding>
58  struct ecb_encryption_policy<Cipher, Padding, cts<0, Cipher, Padding>>
59  : public ecb_policy<Cipher, Padding> {
61 
64 
68 
70  return cipher.encrypt(plaintext);
71  }
72 
74  return cipher.encrypt(plaintext);
75  }
76 
77  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
78  const size_type rem = len % block_size;
79  if (rem || padding_type::always_pad) {
80  unsigned char block[block_size];
81  std::memcpy(block, in, rem);
82  padding_type::pad(block, block_size, rem);
83  c_.encrypt_block(block, <#initializer #>);
84  }
85  }
86  };
87 
88  template<typename Cipher, typename Padding>
89  struct ecb_encryption_policy<Cipher, Padding, cts<1, Cipher, Padding>>
90  : public ecb_policy<Cipher, Padding> {
92 
95 
99 
101  return cipher.encrypt(plaintext);
102  }
103 
105  return cipher.encrypt(plaintext);
106  }
107 
108  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
109  const size_type rem = len % block_size;
110  if (rem || padding_type::always_pad) {
111  unsigned char block[block_size];
112  std::memcpy(block, in, rem);
113  padding_type::pad(block, block_size, rem);
114  c_.encrypt_block(block, <#initializer #>);
115  }
116  }
117  };
118 
119  template<typename Cipher, typename Padding>
120  struct ecb_encryption_policy<Cipher, Padding, cts<2, Cipher, Padding>>
121  : public ecb_policy<Cipher, Padding> {
123 
126 
130 
132  return cipher.encrypt(plaintext);
133  }
134 
136  return cipher.encrypt(plaintext);
137  }
138 
139  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
140  const size_type rem = len % block_size;
141  if (rem || padding_type::always_pad) {
142  unsigned char block[block_size];
143  std::memcpy(block, in, rem);
144  padding_type::pad(block, block_size, rem);
145  c_.encrypt_block(block, <#initializer #>);
146  }
147  }
148  };
149 
150  template<typename Cipher, typename Padding>
151  struct ecb_encryption_policy<Cipher, Padding, cts<3, Cipher, Padding>>
152  : public ecb_policy<Cipher, Padding> {
154 
157 
161 
163  return cipher.encrypt(plaintext);
164  }
165 
167  return cipher.encrypt(plaintext);
168  }
169 
170  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
171  const size_type rem = len % block_size;
172  if (rem || padding_type::always_pad) {
173  unsigned char block[block_size];
174  std::memcpy(block, in, rem);
175  padding_type::pad(block, block_size, rem);
176  c_.encrypt_block(block, <#initializer #>);
177  }
178  }
179  };
180 
181  template<typename Cipher, typename Padding, typename CiphertextStealingMode>
182  struct ecb_decryption_policy : public ecb_policy<Cipher, Padding> {};
183 
184  template<typename Cipher, typename Padding>
185  struct ecb_decryption_policy<Cipher, Padding, cts<0, Cipher, Padding>>
186  : public ecb_policy<Cipher, Padding> {
188 
191 
195 
197  return cipher.decrypt(plaintext);
198  }
199 
201  return cipher.decrypt(plaintext);
202  }
203 
204  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
205  const size_type rem = len % block_size;
206  if (rem || padding_type::always_pad) {
207  return cipher.decrypt_block(plaintext, <#initializer #>);
208  }
209  }
210  };
211 
212  template<typename Cipher, typename Padding>
213  struct ecb_decryption_policy<Cipher, Padding, cts<1, Cipher, Padding>>
214  : public ecb_policy<Cipher, Padding> {
216 
219 
223 
225  return cipher.decrypt(plaintext);
226  }
227 
229  return cipher.decrypt(plaintext);
230  }
231 
232  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
233  const size_type rem = len % block_size;
234  if (rem || padding_type::always_pad) {
235  return cipher.decrypt_block(plaintext, <#initializer #>);
236  }
237  }
238  };
239 
240  template<typename Cipher, typename Padding>
241  struct ecb_decryption_policy<Cipher, Padding, cts<2, Cipher, Padding>>
242  : public ecb_policy<Cipher, Padding> {
244 
247 
251 
253  return cipher.decrypt(plaintext);
254  }
255 
257  return cipher.decrypt(plaintext);
258  }
259 
260  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
261  const size_type rem = len % block_size;
262  if (rem || padding_type::always_pad) {
263  return cipher.decrypt_block(plaintext, <#initializer #>);
264  }
265  }
266  };
267 
268  template<typename Cipher, typename Padding>
269  struct ecb_decryption_policy<Cipher, Padding, cts<3, Cipher, Padding>>
270  : public ecb_policy<Cipher, Padding> {
272 
275 
279 
281  return cipher.decrypt(plaintext);
282  }
283 
285  return cipher.decrypt(plaintext);
286  }
287 
288  block_type end_message(const cipher_type &cipher, const block_type &plaintext) {
289  const size_type rem = len % block_size;
290  if (rem || padding_type::always_pad) {
291  return cipher.decrypt_block(plaintext, <#initializer #>);
292  }
293  }
294  };
295 
296  // Electronic Code Book CodecMode (ECB)
297  template<typename Policy>
299  typedef Policy policy_type;
300 
301  public:
302  typedef typename policy_type::cipher_type cipher_type;
303  typedef typename policy_type::padding_type padding_type;
304 
305  typedef typename policy_type::size_type size_type;
306 
307  constexpr static const std::size_t block_bits = policy_type::block_bits;
308  constexpr static const std::size_t block_words = policy_type::block_words;
309  typedef typename cipher_type::block_type block_type;
310 
312  }
313 
314  block_type begin_message(const block_type &plaintext) {
315  return policy_type::begin_message(cipher, plaintext);
316  }
317 
318  block_type process_block(const block_type &plaintext) {
319  return policy_type::process_block(cipher, plaintext);
320  }
321 
322  block_type end_message(const block_type &plaintext) {
323  return policy_type::end_message(cipher, plaintext);
324  }
325 
327  return padding_type::required_output_size(inputlen, block_size);
328  }
329 
330  constexpr static const unsigned int key_length = cipher_type::key_length / 8;
331  constexpr static const unsigned int block_size = cipher_type::block_size / 8;
332 
333  private:
335  };
336  } // namespace detail
337 
345  template<typename Cipher, template<typename> class Padding,
346  template<typename, typename> class CiphertextStealingMode = cts0>
348  typedef Cipher cipher_type;
349  typedef Padding<Cipher> padding_type;
350  typedef CiphertextStealingMode<Cipher, Padding<Cipher>> ciphertext_stealing_type;
351 
356 
357  template<template<typename, typename> class Policy>
358  struct bind {
360  };
361  };
362 
370  template<typename Cipher, template<typename> class Padding>
372  } // namespace modes
373  } // namespace block
374  } // namespace crypto3
375 } // namespace nil
376 
377 #endif // CRYPTO3_ECB_HPP
electronic_code_book(const cipher_type &c)
Definition: ecb.hpp:311
constexpr static const unsigned int block_size
Definition: ecb.hpp:331
policy_type::cipher_type cipher_type
Definition: ecb.hpp:302
constexpr static const std::size_t block_bits
Definition: ecb.hpp:307
block_type end_message(const block_type &plaintext)
Definition: ecb.hpp:322
cipher_type::block_type block_type
Definition: ecb.hpp:309
block_type begin_message(const block_type &plaintext)
Definition: ecb.hpp:314
constexpr static const unsigned int key_length
Definition: ecb.hpp:330
block_type process_block(const block_type &plaintext)
Definition: ecb.hpp:318
policy_type::padding_type padding_type
Definition: ecb.hpp:303
size_type required_output_size(size_type inputlen) const
Definition: ecb.hpp:326
policy_type::size_type size_type
Definition: ecb.hpp:305
constexpr static const std::size_t block_words
Definition: ecb.hpp:308
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
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:246
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:256
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:260
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:252
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:196
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:190
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:204
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:200
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:224
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:218
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:228
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:232
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:284
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:280
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:274
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:288
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:100
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:94
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:108
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:104
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:135
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:131
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:125
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:139
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:156
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:166
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:170
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:162
block_type end_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:77
block_type process_block(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:73
block_type begin_message(const cipher_type &cipher, const block_type &plaintext)
Definition: ecb.hpp:69
ecb_policy< Cipher, Padding >::padding_type padding_type
Definition: ecb.hpp:63
constexpr static const size_type block_words
Definition: ecb.hpp:50
Cipher cipher_type
Definition: ecb.hpp:46
cipher_type::block_type block_type
Definition: ecb.hpp:51
constexpr static const size_type block_bits
Definition: ecb.hpp:49
std::size_t size_type
Definition: ecb.hpp:44
Padding padding_type
Definition: ecb.hpp:47
detail::electronic_code_book< Policy< cipher_type, padding_type > > type
Definition: ecb.hpp:359
Electronic Code Book Mode (ECB)
Definition: ecb.hpp:347
Padding< Cipher > padding_type
Definition: ecb.hpp:349
detail::ecb_decryption_policy< cipher_type, padding_type, ciphertext_stealing_type > decryption_policy
Definition: ecb.hpp:355
detail::ecb_encryption_policy< cipher_type, padding_type, ciphertext_stealing_type > encryption_policy
Definition: ecb.hpp:353
CiphertextStealingMode< Cipher, Padding< Cipher > > ciphertext_stealing_type
Definition: ecb.hpp:350
Cipher cipher_type
Definition: ecb.hpp:348