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