Botan 3.14.1
Crypto and TLS for C&
block_cipher.cpp
Go to the documentation of this file.
1/*
2* Block Ciphers
3* (C) 2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/block_cipher.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/internal/algorithm_spec.h>
13#include <botan/internal/probe_providers.h>
14#include <memory>
15#include <utility>
16
17#if defined(BOTAN_HAS_AES)
18 #include <botan/internal/aes.h>
19#endif
20
21#if defined(BOTAN_HAS_ARIA)
22 #include <botan/internal/aria.h>
23#endif
24
25#if defined(BOTAN_HAS_BLOWFISH)
26 #include <botan/internal/blowfish.h>
27#endif
28
29#if defined(BOTAN_HAS_CAMELLIA)
30 #include <botan/internal/camellia.h>
31#endif
32
33#if defined(BOTAN_HAS_CAST_128)
34 #include <botan/internal/cast128.h>
35#endif
36
37#if defined(BOTAN_HAS_CASCADE)
38 #include <botan/internal/cascade.h>
39#endif
40
41#if defined(BOTAN_HAS_DES)
42 #include <botan/internal/des.h>
43#endif
44
45#if defined(BOTAN_HAS_GOST_28147_89)
46 #include <botan/internal/gost_28147.h>
47#endif
48
49#if defined(BOTAN_HAS_IDEA)
50 #include <botan/internal/idea.h>
51#endif
52
53#if defined(BOTAN_HAS_KUZNYECHIK)
54 #include <botan/internal/kuznyechik.h>
55#endif
56
57#if defined(BOTAN_HAS_LION)
58 #include <botan/hash.h>
59 #include <botan/stream_cipher.h>
60 #include <botan/internal/lion.h>
61#endif
62
63#if defined(BOTAN_HAS_NOEKEON)
64 #include <botan/internal/noekeon.h>
65#endif
66
67#if defined(BOTAN_HAS_SEED)
68 #include <botan/internal/seed.h>
69#endif
70
71#if defined(BOTAN_HAS_SERPENT)
72 #include <botan/internal/serpent.h>
73#endif
74
75#if defined(BOTAN_HAS_SHACAL2)
76 #include <botan/internal/shacal2.h>
77#endif
78
79#if defined(BOTAN_HAS_SM4)
80 #include <botan/internal/sm4.h>
81#endif
82
83#if defined(BOTAN_HAS_TWOFISH)
84 #include <botan/internal/twofish.h>
85#endif
86
87#if defined(BOTAN_HAS_THREEFISH_512)
88 #include <botan/internal/threefish_512.h>
89#endif
90
91#if defined(BOTAN_HAS_COMMONCRYPTO)
92 #include <botan/internal/commoncrypto.h>
93#endif
94
95namespace Botan {
96
97std::unique_ptr<BlockCipher> BlockCipher::create(std::string_view algo, std::string_view provider) {
98#if defined(BOTAN_HAS_COMMONCRYPTO)
99 if(provider.empty() || provider == "commoncrypto") {
100 if(auto bc = make_commoncrypto_block_cipher(algo))
101 return bc;
102
103 if(!provider.empty())
104 return nullptr;
105 }
106#endif
107
108 // TODO: CryptoAPI
109 // TODO: /dev/crypto
110
111 // Only base providers from here on out
112 if(provider.empty() == false && provider != "base") {
113 return nullptr;
114 }
115
116#if defined(BOTAN_HAS_AES)
117 if(algo == "AES-128") {
118 return std::make_unique<AES_128>();
119 }
120
121 if(algo == "AES-192") {
122 return std::make_unique<AES_192>();
123 }
124
125 if(algo == "AES-256") {
126 return std::make_unique<AES_256>();
127 }
128#endif
129
130#if defined(BOTAN_HAS_ARIA)
131 if(algo == "ARIA-128") {
132 return std::make_unique<ARIA_128>();
133 }
134
135 if(algo == "ARIA-192") {
136 return std::make_unique<ARIA_192>();
137 }
138
139 if(algo == "ARIA-256") {
140 return std::make_unique<ARIA_256>();
141 }
142#endif
143
144#if defined(BOTAN_HAS_SERPENT)
145 if(algo == "Serpent") {
146 return std::make_unique<Serpent>();
147 }
148#endif
149
150#if defined(BOTAN_HAS_SHACAL2)
151 if(algo == "SHACAL2") {
152 return std::make_unique<SHACAL2>();
153 }
154#endif
155
156#if defined(BOTAN_HAS_TWOFISH)
157 if(algo == "Twofish") {
158 return std::make_unique<Twofish>();
159 }
160#endif
161
162#if defined(BOTAN_HAS_THREEFISH_512)
163 if(algo == "Threefish-512") {
164 return std::make_unique<Threefish_512>();
165 }
166#endif
167
168#if defined(BOTAN_HAS_BLOWFISH)
169 if(algo == "Blowfish") {
170 return std::make_unique<Blowfish>();
171 }
172#endif
173
174#if defined(BOTAN_HAS_CAMELLIA)
175 if(algo == "Camellia-128") {
176 return std::make_unique<Camellia_128>();
177 }
178
179 if(algo == "Camellia-192") {
180 return std::make_unique<Camellia_192>();
181 }
182
183 if(algo == "Camellia-256") {
184 return std::make_unique<Camellia_256>();
185 }
186#endif
187
188#if defined(BOTAN_HAS_DES)
189 if(algo == "DES") {
190 return std::make_unique<DES>();
191 }
192
193 if(algo == "TripleDES" || algo == "3DES" || algo == "DES-EDE") {
194 return std::make_unique<TripleDES>();
195 }
196#endif
197
198#if defined(BOTAN_HAS_NOEKEON)
199 if(algo == "Noekeon") {
200 return std::make_unique<Noekeon>();
201 }
202#endif
203
204#if defined(BOTAN_HAS_CAST_128)
205 if(algo == "CAST-128" || algo == "CAST5") {
206 return std::make_unique<CAST_128>();
207 }
208#endif
209
210#if defined(BOTAN_HAS_IDEA)
211 if(algo == "IDEA") {
212 return std::make_unique<IDEA>();
213 }
214#endif
215
216#if defined(BOTAN_HAS_KUZNYECHIK)
217 if(algo == "Kuznyechik") {
218 return std::make_unique<Kuznyechik>();
219 }
220#endif
221
222#if defined(BOTAN_HAS_SEED)
223 if(algo == "SEED") {
224 return std::make_unique<SEED>();
225 }
226#endif
227
228#if defined(BOTAN_HAS_SM4)
229 if(algo == "SM4") {
230 return std::make_unique<SM4>();
231 }
232#endif
233
234 const AlgorithmSpec req(algo);
235
236#if defined(BOTAN_HAS_GOST_28147_89)
237 if(auto m = req.match("GOST-28147-89({params:str=R3411_94_TestParam})")) {
238 return std::make_unique<GOST_28147_89>(m->str("params"));
239 }
240#endif
241
242#if defined(BOTAN_HAS_CASCADE)
243 if(auto m = req.match("Cascade({cipher1},{cipher2})")) {
244 auto c1 = BlockCipher::create(m->str("cipher1"));
245 auto c2 = BlockCipher::create(m->str("cipher2"));
246
247 if(c1 && c2) {
248 return std::make_unique<Cascade_Cipher>(std::move(c1), std::move(c2));
249 }
250 }
251#endif
252
253#if defined(BOTAN_HAS_LION)
254 if(auto m = req.match("Lion({hash},{stream},{block_size:int=1024})")) {
255 auto hash = HashFunction::create(m->str("hash"));
256 auto stream = StreamCipher::create(m->str("stream"));
257
258 if(hash && stream) {
259 return std::make_unique<Lion>(std::move(hash), std::move(stream), m->integer("block_size"));
260 }
261 }
262#endif
263
264 BOTAN_UNUSED(req);
266
267 return nullptr;
268}
269
270//static
271std::unique_ptr<BlockCipher> BlockCipher::create_or_throw(std::string_view algo, std::string_view provider) {
272 if(auto bc = BlockCipher::create(algo, provider)) {
273 return bc;
274 }
275 throw Lookup_Error("Block cipher", algo, provider);
276}
277
278std::vector<std::string> BlockCipher::providers(std::string_view algo) {
279 return probe_providers_of<BlockCipher>(algo, {"base", "commoncrypto"});
280}
281
282} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< BlockCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::vector< std::string > providers(std::string_view algo_spec)
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
virtual std::string provider() const
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
std::unique_ptr< BlockCipher > make_commoncrypto_block_cipher(std::string_view name)
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})