Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
14 namespace Botan {
15
16 /**
17 * This class represents a block cipher object.
18 */
19 class BOTAN_DLL BlockCipher : public SymmetricAlgorithm
20 {
21 public:
22
23 /**
24 * Create an instance based on a name
25 * If provider is empty then best available is chosen.
26 * @param algo_spec algorithm name
27 * @param provider provider implementation to choose
28 * @return a null pointer if the algo/provider combination cannot be found
29 */
30 static std::unique_ptr<BlockCipher>
31 create(const std::string& algo_spec,
32 const std::string& provider = "");
33
34 /**
35 * Create an instance based on a name, or throw if the
36 * algo/provider combination cannot be found. If provider is
37 * empty then best available is chosen.
38 */
39 static std::unique_ptr<BlockCipher>
40 create_or_throw(const std::string& algo_spec,
41 const std::string& provider = "");
42
43 /**
44 * @return list of available providers for this algorithm, empty if not available
45 * @param algo_spec algorithm name
46 */
47 static std::vector<std::string> providers(const std::string& algo_spec);
48
49 /**
50 * @return block size of this algorithm
51 */
52 virtual size_t block_size() const = 0;
53
54 /**
55 * @return native parallelism of this cipher in blocks
56 */
57 virtual size_t parallelism() const { return 1; }
58
59 /**
60 * @return prefererred parallelism of this cipher in bytes
61 */
62 size_t parallel_bytes() const
63 {
64 return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT;
65 }
66
67 /**
68 * @return provider information about this implementation. Default is "base",
69 * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
70 */
71 virtual std::string provider() const { return "base"; }
72
73 /**
74 * Encrypt a block.
75 * @param in The plaintext block to be encrypted as a byte array.
76 * Must be of length block_size().
77 * @param out The byte array designated to hold the encrypted block.
78 * Must be of length block_size().
79 */
80 void encrypt(const uint8_t in[], uint8_t out[]) const
81 { encrypt_n(in, out, 1); }
82
83 /**
84 * Decrypt a block.
85 * @param in The ciphertext block to be decypted as a byte array.
86 * Must be of length block_size().
87 * @param out The byte array designated to hold the decrypted block.
88 * Must be of length block_size().
89 */
90 void decrypt(const uint8_t in[], uint8_t out[]) const
91 { decrypt_n(in, out, 1); }
92
93 /**
94 * Encrypt a block.
95 * @param block the plaintext block to be encrypted
96 * Must be of length block_size(). Will hold the result when the function
97 * has finished.
98 */
99 void encrypt(uint8_t block[]) const { encrypt_n(block, block, 1); }
100
101 /**
102 * Decrypt a block.
103 * @param block the ciphertext block to be decrypted
104 * Must be of length block_size(). Will hold the result when the function
105 * has finished.
106 */
107 void decrypt(uint8_t block[]) const { decrypt_n(block, block, 1); }
108
109 /**
110 * Encrypt one or more blocks
111 * @param block the input/output buffer (multiple of block_size())
112 */
113 template<typename Alloc>
114 void encrypt(std::vector<uint8_t, Alloc>& block) const
115 {
116 return encrypt_n(block.data(), block.data(), block.size() / block_size());
117 }
118
119 /**
120 * Decrypt one or more blocks
121 * @param block the input/output buffer (multiple of block_size())
122 */
123 template<typename Alloc>
124 void decrypt(std::vector<uint8_t, Alloc>& block) const
125 {
126 return decrypt_n(block.data(), block.data(), block.size() / block_size());
127 }
128
129 /**
130 * Encrypt one or more blocks
131 * @param in the input buffer (multiple of block_size())
132 * @param out the output buffer (same size as in)
133 */
134 template<typename Alloc, typename Alloc2>
135 void encrypt(const std::vector<uint8_t, Alloc>& in,
136 std::vector<uint8_t, Alloc2>& out) const
137 {
138 return encrypt_n(in.data(), out.data(), in.size() / block_size());
139 }
140
141 /**
142 * Decrypt one or more blocks
143 * @param in the input buffer (multiple of block_size())
144 * @param out the output buffer (same size as in)
145 */
146 template<typename Alloc, typename Alloc2>
147 void decrypt(const std::vector<uint8_t, Alloc>& in,
148 std::vector<uint8_t, Alloc2>& out) const
149 {
150 return decrypt_n(in.data(), out.data(), in.size() / block_size());
151 }
152
153 /**
154 * Encrypt one or more blocks
155 * @param in the input buffer (multiple of block_size())
156 * @param out the output buffer (same size as in)
157 * @param blocks the number of blocks to process
158 */
159 virtual void encrypt_n(const uint8_t in[], uint8_t out[],
160 size_t blocks) const = 0;
161
162 /**
163 * Decrypt one or more blocks
164 * @param in the input buffer (multiple of block_size())
165 * @param out the output buffer (same size as in)
166 * @param blocks the number of blocks to process
167 */
168 virtual void decrypt_n(const uint8_t in[], uint8_t out[],
169 size_t blocks) const = 0;
170
171 /**
172 * @return new object representing the same algorithm as *this
173 */
174 virtual BlockCipher* clone() const = 0;
175
176 virtual ~BlockCipher() = default;
177 };
178
179 /**
180 * Represents a block cipher with a single fixed block size
181 */
182 template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1>
183 class Block_Cipher_Fixed_Params : public BlockCipher
184 {
185 public:
186 enum { BLOCK_SIZE = BS };
187 size_t block_size() const override { return BS; }
188
189 Key_Length_Specification key_spec() const override
190 {
191 return Key_Length_Specification(KMIN, KMAX, KMOD);
192 }
193 };
194
195 }
196
197 #endif
Botan::BlockCipher::encrypt
void encrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:80
Botan::BlockCipher::encrypt
void encrypt(std::vector< uint8_t, Alloc > &block) const
Definition: block_cipher.h:114
Botan::BlockCipher::decrypt
void decrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:90
Botan::Block_Cipher_Fixed_Params::BLOCK_SIZE
Definition: block_cipher.h:186
Botan::Key_Length_Specification
Definition: key_spec.h:18
Botan::BlockCipher::encrypt
void encrypt(uint8_t block[]) const
Definition: block_cipher.h:99
Botan::SymmetricAlgorithm
Definition: sym_algo.h:21
Botan::Block_Cipher_Fixed_Params::key_spec
Key_Length_Specification key_spec() const override
Definition: block_cipher.h:189
Botan::BlockCipher::decrypt
void decrypt(uint8_t block[]) const
Definition: block_cipher.h:107
Botan::BlockCipher::parallel_bytes
size_t parallel_bytes() const
Definition: block_cipher.h:62
Botan
Definition: alg_id.cpp:13
Botan::BlockCipher::parallelism
virtual size_t parallelism() const
Definition: block_cipher.h:57
Botan::BlockCipher::decrypt
void decrypt(const std::vector< uint8_t, Alloc > &in, std::vector< uint8_t, Alloc2 > &out) const
Definition: block_cipher.h:147
Botan::BlockCipher::provider
virtual std::string provider() const
Definition: block_cipher.h:71
Botan::Block_Cipher_Fixed_Params
Definition: block_cipher.h:183
Botan::Block_Cipher_Fixed_Params::block_size
size_t block_size() const override
Definition: block_cipher.h:187
Botan::BlockCipher::encrypt
void encrypt(const std::vector< uint8_t, Alloc > &in, std::vector< uint8_t, Alloc2 > &out) const
Definition: block_cipher.h:135
Botan::BlockCipher::decrypt
void decrypt(std::vector< uint8_t, Alloc > &block) const
Definition: block_cipher.h:124
Botan::BlockCipher
Definition: block_cipher.h:19