Botan  2.7.0
Crypto and TLS for C++11
block_cipher.h
Go to the documentation of this file.
1 /*
2 * Block Cipher Base Class
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_BLOCK_CIPHER_H_
9 #define BOTAN_BLOCK_CIPHER_H_
10 
11 #include <botan/sym_algo.h>
12 #include <string>
13 #include <memory>
14 
15 namespace Botan {
16 
17 /**
18 * This class represents a block cipher object.
19 */
21  {
22  public:
23 
24  /**
25  * Create an instance based on a name
26  * If provider is empty then best available is chosen.
27  * @param algo_spec algorithm name
28  * @param provider provider implementation to choose
29  * @return a null pointer if the algo/provider combination cannot be found
30  */
31  static std::unique_ptr<BlockCipher>
32  create(const std::string& algo_spec,
33  const std::string& provider = "");
34 
35  /**
36  * Create an instance based on a name, or throw if the
37  * algo/provider combination cannot be found. If provider is
38  * empty then best available is chosen.
39  */
40  static std::unique_ptr<BlockCipher>
41  create_or_throw(const std::string& algo_spec,
42  const std::string& provider = "");
43 
44  /**
45  * @return list of available providers for this algorithm, empty if not available
46  * @param algo_spec algorithm name
47  */
48  static std::vector<std::string> providers(const std::string& algo_spec);
49 
50  /**
51  * @return block size of this algorithm
52  */
53  virtual size_t block_size() const = 0;
54 
55  /**
56  * @return native parallelism of this cipher in blocks
57  */
58  virtual size_t parallelism() const { return 1; }
59 
60  /**
61  * @return prefererred parallelism of this cipher in bytes
62  */
63  size_t parallel_bytes() const
64  {
65  return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT;
66  }
67 
68  /**
69  * @return provider information about this implementation. Default is "base",
70  * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
71  */
72  virtual std::string provider() const { return "base"; }
73 
74  /**
75  * Encrypt a block.
76  * @param in The plaintext block to be encrypted as a byte array.
77  * Must be of length block_size().
78  * @param out The byte array designated to hold the encrypted block.
79  * Must be of length block_size().
80  */
81  void encrypt(const uint8_t in[], uint8_t out[]) const
82  { encrypt_n(in, out, 1); }
83 
84  /**
85  * Decrypt a block.
86  * @param in The ciphertext block to be decypted as a byte array.
87  * Must be of length block_size().
88  * @param out The byte array designated to hold the decrypted block.
89  * Must be of length block_size().
90  */
91  void decrypt(const uint8_t in[], uint8_t out[]) const
92  { decrypt_n(in, out, 1); }
93 
94  /**
95  * Encrypt a block.
96  * @param block the plaintext block to be encrypted
97  * Must be of length block_size(). Will hold the result when the function
98  * has finished.
99  */
100  void encrypt(uint8_t block[]) const { encrypt_n(block, block, 1); }
101 
102  /**
103  * Decrypt a block.
104  * @param block the ciphertext block to be decrypted
105  * Must be of length block_size(). Will hold the result when the function
106  * has finished.
107  */
108  void decrypt(uint8_t block[]) const { decrypt_n(block, block, 1); }
109 
110  /**
111  * Encrypt one or more blocks
112  * @param block the input/output buffer (multiple of block_size())
113  */
114  template<typename Alloc>
115  void encrypt(std::vector<uint8_t, Alloc>& block) const
116  {
117  return encrypt_n(block.data(), block.data(), block.size() / block_size());
118  }
119 
120  /**
121  * Decrypt one or more blocks
122  * @param block the input/output buffer (multiple of block_size())
123  */
124  template<typename Alloc>
125  void decrypt(std::vector<uint8_t, Alloc>& block) const
126  {
127  return decrypt_n(block.data(), block.data(), block.size() / block_size());
128  }
129 
130  /**
131  * Encrypt one or more blocks
132  * @param in the input buffer (multiple of block_size())
133  * @param out the output buffer (same size as in)
134  */
135  template<typename Alloc, typename Alloc2>
136  void encrypt(const std::vector<uint8_t, Alloc>& in,
137  std::vector<uint8_t, Alloc2>& out) const
138  {
139  return encrypt_n(in.data(), out.data(), in.size() / block_size());
140  }
141 
142  /**
143  * Decrypt one or more blocks
144  * @param in the input buffer (multiple of block_size())
145  * @param out the output buffer (same size as in)
146  */
147  template<typename Alloc, typename Alloc2>
148  void decrypt(const std::vector<uint8_t, Alloc>& in,
149  std::vector<uint8_t, Alloc2>& out) const
150  {
151  return decrypt_n(in.data(), out.data(), in.size() / block_size());
152  }
153 
154  /**
155  * Encrypt one or more blocks
156  * @param in the input buffer (multiple of block_size())
157  * @param out the output buffer (same size as in)
158  * @param blocks the number of blocks to process
159  */
160  virtual void encrypt_n(const uint8_t in[], uint8_t out[],
161  size_t blocks) const = 0;
162 
163  /**
164  * Decrypt one or more blocks
165  * @param in the input buffer (multiple of block_size())
166  * @param out the output buffer (same size as in)
167  * @param blocks the number of blocks to process
168  */
169  virtual void decrypt_n(const uint8_t in[], uint8_t out[],
170  size_t blocks) const = 0;
171 
172  virtual void encrypt_n_xex(uint8_t data[],
173  const uint8_t mask[],
174  size_t blocks) const
175  {
176  const size_t BS = block_size();
177  xor_buf(data, mask, blocks * BS);
178  encrypt_n(data, data, blocks);
179  xor_buf(data, mask, blocks * BS);
180  }
181 
182  virtual void decrypt_n_xex(uint8_t data[],
183  const uint8_t mask[],
184  size_t blocks) const
185  {
186  const size_t BS = block_size();
187  xor_buf(data, mask, blocks * BS);
188  decrypt_n(data, data, blocks);
189  xor_buf(data, mask, blocks * BS);
190  }
191 
192  /**
193  * @return new object representing the same algorithm as *this
194  */
195  virtual BlockCipher* clone() const = 0;
196 
197  virtual ~BlockCipher() = default;
198  };
199 
200 /**
201 * Represents a block cipher with a single fixed block size
202 */
203 template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1>
205  {
206  public:
207  enum { BLOCK_SIZE = BS };
208  size_t block_size() const override { return BS; }
209 
210  // override to take advantage of compile time constant block size
211  void encrypt_n_xex(uint8_t data[],
212  const uint8_t mask[],
213  size_t blocks) const override
214  {
215  xor_buf(data, mask, blocks * BS);
216  encrypt_n(data, data, blocks);
217  xor_buf(data, mask, blocks * BS);
218  }
219 
220  void decrypt_n_xex(uint8_t data[],
221  const uint8_t mask[],
222  size_t blocks) const override
223  {
224  xor_buf(data, mask, blocks * BS);
225  decrypt_n(data, data, blocks);
226  xor_buf(data, mask, blocks * BS);
227  }
228 
230  {
231  return Key_Length_Specification(KMIN, KMAX, KMOD);
232  }
233  };
234 
235 }
236 
237 #endif
size_t parallel_bytes() const
Definition: block_cipher.h:63
virtual size_t parallelism() const
Definition: block_cipher.h:58
virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
void decrypt(const std::vector< uint8_t, Alloc > &in, std::vector< uint8_t, Alloc2 > &out) const
Definition: block_cipher.h:148
void encrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const override
Definition: block_cipher.h:211
void decrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const override
Definition: block_cipher.h:220
void decrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:91
void encrypt(std::vector< uint8_t, Alloc > &block) const
Definition: block_cipher.h:115
void encrypt(uint8_t block[]) const
Definition: block_cipher.h:100
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
virtual void encrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const
Definition: block_cipher.h:172
Key_Length_Specification key_spec() const override
Definition: block_cipher.h:229
void decrypt(uint8_t block[]) const
Definition: block_cipher.h:108
Definition: alg_id.cpp:13
virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
void encrypt(const std::vector< uint8_t, Alloc > &in, std::vector< uint8_t, Alloc2 > &out) const
Definition: block_cipher.h:136
void encrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:81
void decrypt(std::vector< uint8_t, Alloc > &block) const
Definition: block_cipher.h:125
virtual std::string provider() const
Definition: block_cipher.h:72
size_t block_size() const override
Definition: block_cipher.h:208
virtual void decrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const
Definition: block_cipher.h:182