Coverage Report

Created: 2022-06-23 06:44

/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_COMMONCRYPTO)
81
  #include <botan/internal/commoncrypto.h>
82
#endif
83
84
namespace Botan {
85
86
std::unique_ptr<BlockCipher>
87
BlockCipher::create(const std::string& algo,
88
                    const std::string& provider)
89
1.33k
   {
90
#if defined(BOTAN_HAS_COMMONCRYPTO)
91
   if(provider.empty() || provider == "commoncrypto")
92
      {
93
      if(auto bc = make_commoncrypto_block_cipher(algo))
94
         return bc;
95
96
      if(!provider.empty())
97
         return nullptr;
98
      }
99
#endif
100
101
   // TODO: CryptoAPI
102
   // TODO: /dev/crypto
103
104
   // Only base providers from here on out
105
1.33k
   if(provider.empty() == false && provider != "base")
106
176
      return nullptr;
107
108
1.15k
#if defined(BOTAN_HAS_AES)
109
1.15k
   if(algo == "AES-128")
110
197
      {
111
197
      return std::make_unique<AES_128>();
112
197
      }
113
114
958
   if(algo == "AES-192")
115
0
      {
116
0
      return std::make_unique<AES_192>();
117
0
      }
118
119
958
   if(algo == "AES-256")
120
705
      {
121
705
      return std::make_unique<AES_256>();
122
705
      }
123
253
#endif
124
125
253
#if defined(BOTAN_HAS_ARIA)
126
253
   if(algo == "ARIA-128")
127
27
      {
128
27
      return std::make_unique<ARIA_128>();
129
27
      }
130
131
226
   if(algo == "ARIA-192")
132
0
      {
133
0
      return std::make_unique<ARIA_192>();
134
0
      }
135
136
226
   if(algo == "ARIA-256")
137
49
      {
138
49
      return std::make_unique<ARIA_256>();
139
49
      }
140
177
#endif
141
142
177
#if defined(BOTAN_HAS_SERPENT)
143
177
   if(algo == "Serpent")
144
0
      {
145
0
      return std::make_unique<Serpent>();
146
0
      }
147
177
#endif
148
149
177
#if defined(BOTAN_HAS_SHACAL2)
150
177
   if(algo == "SHACAL2")
151
0
      {
152
0
      return std::make_unique<SHACAL2>();
153
0
      }
154
177
#endif
155
156
177
#if defined(BOTAN_HAS_TWOFISH)
157
177
   if(algo == "Twofish")
158
0
      {
159
0
      return std::make_unique<Twofish>();
160
0
      }
161
177
#endif
162
163
177
#if defined(BOTAN_HAS_THREEFISH_512)
164
177
   if(algo == "Threefish-512")
165
0
      {
166
0
      return std::make_unique<Threefish_512>();
167
0
      }
168
177
#endif
169
170
177
#if defined(BOTAN_HAS_BLOWFISH)
171
177
   if(algo == "Blowfish")
172
0
      {
173
0
      return std::make_unique<Blowfish>();
174
0
      }
175
177
#endif
176
177
177
#if defined(BOTAN_HAS_CAMELLIA)
178
177
   if(algo == "Camellia-128")
179
49
      {
180
49
      return std::make_unique<Camellia_128>();
181
49
      }
182
183
128
   if(algo == "Camellia-192")
184
0
      {
185
0
      return std::make_unique<Camellia_192>();
186
0
      }
187
188
128
   if(algo == "Camellia-256")
189
33
      {
190
33
      return std::make_unique<Camellia_256>();
191
33
      }
192
95
#endif
193
194
95
#if defined(BOTAN_HAS_DES)
195
95
   if(algo == "DES")
196
0
      {
197
0
      return std::make_unique<DES>();
198
0
      }
199
200
95
   if(algo == "TripleDES" || algo == "3DES" || algo == "DES-EDE")
201
95
      {
202
95
      return std::make_unique<TripleDES>();
203
95
      }
204
0
#endif
205
206
0
#if defined(BOTAN_HAS_NOEKEON)
207
0
   if(algo == "Noekeon")
208
0
      {
209
0
      return std::make_unique<Noekeon>();
210
0
      }
211
0
#endif
212
213
0
#if defined(BOTAN_HAS_CAST_128)
214
0
   if(algo == "CAST-128" || algo == "CAST5")
215
0
      {
216
0
      return std::make_unique<CAST_128>();
217
0
      }
218
0
#endif
219
220
0
#if defined(BOTAN_HAS_IDEA)
221
0
   if(algo == "IDEA")
222
0
      {
223
0
      return std::make_unique<IDEA>();
224
0
      }
225
0
#endif
226
227
0
#if defined(BOTAN_HAS_SEED)
228
0
   if(algo == "SEED")
229
0
      {
230
0
      return std::make_unique<SEED>();
231
0
      }
232
0
#endif
233
234
0
#if defined(BOTAN_HAS_SM4)
235
0
   if(algo == "SM4")
236
0
      {
237
0
      return std::make_unique<SM4>();
238
0
      }
239
0
#endif
240
241
0
   const SCAN_Name req(algo);
242
243
0
#if defined(BOTAN_HAS_GOST_28147_89)
244
0
   if(req.algo_name() == "GOST-28147-89")
245
0
      {
246
0
      return std::make_unique<GOST_28147_89>(req.arg(0, "R3411_94_TestParam"));
247
0
      }
248
0
#endif
249
250
0
#if defined(BOTAN_HAS_CASCADE)
251
0
   if(req.algo_name() == "Cascade" && req.arg_count() == 2)
252
0
      {
253
0
      std::unique_ptr<BlockCipher> c1 = BlockCipher::create(req.arg(0));
254
0
      std::unique_ptr<BlockCipher> c2 = BlockCipher::create(req.arg(1));
255
256
0
      if(c1 && c2)
257
0
         return std::make_unique<Cascade_Cipher>(std::move(c1), std::move(c2));
258
0
      }
259
0
#endif
260
261
0
#if defined(BOTAN_HAS_LION)
262
0
   if(req.algo_name() == "Lion" && req.arg_count_between(2, 3))
263
0
      {
264
0
      std::unique_ptr<HashFunction> hash = HashFunction::create(req.arg(0));
265
0
      std::unique_ptr<StreamCipher> stream = StreamCipher::create(req.arg(1));
266
267
0
      if(hash && stream)
268
0
         {
269
0
         const size_t block_size = req.arg_as_integer(2, 1024);
270
0
         return std::make_unique<Lion>(std::move(hash), std::move(stream), block_size);
271
0
         }
272
0
      }
273
0
#endif
274
275
0
   BOTAN_UNUSED(req);
276
0
   BOTAN_UNUSED(provider);
277
278
0
   return nullptr;
279
0
   }
280
281
//static
282
std::unique_ptr<BlockCipher>
283
BlockCipher::create_or_throw(const std::string& algo,
284
                             const std::string& provider)
285
366
   {
286
366
   if(auto bc = BlockCipher::create(algo, provider))
287
366
      {
288
366
      return bc;
289
366
      }
290
0
   throw Lookup_Error("Block cipher", algo, provider);
291
366
   }
292
293
std::vector<std::string> BlockCipher::providers(const std::string& algo)
294
176
   {
295
176
   return probe_providers_of<BlockCipher>(algo, { "base", "commoncrypto" });
296
176
   }
297
298
}