Coverage Report

Created: 2021-06-10 10:30

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