aboutsummaryrefslogtreecommitdiffstats
path: root/src/def_alg.cpp
blob: d719cfa05e4a41f17b188b51ac97765ec71edc90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*************************************************
* Default Engine Algorithms Source File          *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/eng_def.h>
#include <botan/libstate.h>
#include <botan/parsing.h>

#ifdef BOTAN_HAS_AES
   #include <botan/aes.h>
#endif

#ifdef BOTAN_HAS_BLOWFISH
   #include <botan/blowfish.h>
#endif

#ifdef BOTAN_HAS_CAST
   #include <botan/cast128.h>
   #include <botan/cast256.h>
#endif

#ifdef BOTAN_HAS_DES
   #include <botan/des.h>
#endif

#ifdef BOTAN_HAS_GOST
   #include <botan/gost.h>
#endif

#ifdef BOTAN_HAS_IDEA
   #include <botan/idea.h>
#endif

#ifdef BOTAN_HAS_KASUMI
   #include <botan/kasumi.h>
#endif

#ifdef BOTAN_HAS_LION
   #include <botan/lion.h>
#endif

#ifdef BOTAN_HAS_LUBYRACK
   #include <botan/lubyrack.h>
#endif

#ifdef BOTAN_HAS_MARS
   #include <botan/mars.h>
#endif

#ifdef BOTAN_HAS_MISTY1
   #include <botan/misty1.h>
#endif

#ifdef BOTAN_HAS_NOEKEON
   #include <botan/noekeon.h>
#endif

#ifdef BOTAN_HAS_RC2
   #include <botan/rc2.h>
#endif

#ifdef BOTAN_HAS_RC5
   #include <botan/rc5.h>
#endif

#ifdef BOTAN_HAS_RC6
   #include <botan/rc6.h>
#endif

#ifdef BOTAN_HAS_SAFER
   #include <botan/safer_sk.h>
#endif

#ifdef BOTAN_HAS_SEED
   #include <botan/seed.h>
#endif

#ifdef BOTAN_HAS_SERPENT
   #include <botan/serpent.h>
#endif

#ifdef BOTAN_HAS_SKIPJACK
   #include <botan/skipjack.h>
#endif

#ifdef BOTAN_HAS_SQUARE
   #include <botan/square.h>
#endif

#ifdef BOTAN_HAS_TEA
   #include <botan/tea.h>
#endif

#ifdef BOTAN_HAS_TWOFISH
   #include <botan/twofish.h>
#endif

#ifdef BOTAN_HAS_XTEA
   #include <botan/xtea.h>
#endif

#ifdef BOTAN_HAS_ARC4
   #include <botan/arc4.h>
#endif

#ifdef BOTAN_HAS_SALSA20
   #include <botan/salsa20.h>
#endif

#ifdef BOTAN_HAS_TURING
   #include <botan/turing.h>
#endif

#ifdef BOTAN_HAS_WID_WAKE
   #include <botan/wid_wake.h>
#endif

#ifdef BOTAN_HAS_ADLER32
  #include <botan/adler32.h>
#endif

#include <botan/crc24.h>
#include <botan/crc32.h>
#include <botan/fork256.h>
#include <botan/has160.h>
#include <botan/md2.h>
#include <botan/md4.h>
#include <botan/md5.h>
#include <botan/rmd128.h>
#include <botan/rmd160.h>
#include <botan/sha160.h>
#include <botan/sha256.h>
#include <botan/sha_64.h>
#include <botan/tiger.h>
#include <botan/whrlpool.h>
#include <botan/par_hash.h>

#include <botan/cbc_mac.h>
#include <botan/cmac.h>
#include <botan/hmac.h>
#include <botan/ssl3_mac.h>
#include <botan/x919_mac.h>

#include <botan/mode_pad.h>
#include <botan/pgp_s2k.h>
#include <botan/pkcs5.h>

namespace Botan {

/*************************************************
* Some macros to simplify control flow           *
*************************************************/
#define HANDLE_TYPE_NO_ARGS(NAME, TYPE)        \
   if(algo_name == NAME)                       \
      {                                        \
      if(name.size() == 1)                     \
         return new TYPE;                      \
      throw Invalid_Algorithm_Name(algo_spec); \
      }

#define HANDLE_TYPE_ONE_U32BIT(NAME, TYPE, DEFAULT) \
   if(algo_name == NAME)                            \
      {                                             \
      if(name.size() == 1)                          \
         return new TYPE(DEFAULT);                  \
      if(name.size() == 2)                          \
         return new TYPE(to_u32bit(name[1]));       \
      throw Invalid_Algorithm_Name(algo_spec);      \
      }

#define HANDLE_TYPE_TWO_U32BIT(NAME, TYPE, DEFAULT)               \
   if(algo_name == NAME)                                          \
      {                                                           \
      if(name.size() == 1)                                        \
         return new TYPE(DEFAULT);                                \
      if(name.size() == 2)                                        \
         return new TYPE(to_u32bit(name[1]));                     \
      if(name.size() == 3)                                        \
         return new TYPE(to_u32bit(name[1]), to_u32bit(name[2])); \
      throw Invalid_Algorithm_Name(algo_spec);                    \
      }

#define HANDLE_TYPE_ONE_STRING(NAME, TYPE)     \
   if(algo_name == NAME)                       \
      {                                        \
      if(name.size() == 2)                     \
         return new TYPE(name[1]);             \
      throw Invalid_Algorithm_Name(algo_spec); \
      }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
BlockCipher*
Default_Engine::find_block_cipher(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;
   const std::string algo_name = global_state().deref_alias(name[0]);

#ifdef BOTAN_HAS_AES
   HANDLE_TYPE_NO_ARGS("AES", AES);
   HANDLE_TYPE_NO_ARGS("AES-128", AES_128);
   HANDLE_TYPE_NO_ARGS("AES-192", AES_192);
   HANDLE_TYPE_NO_ARGS("AES-256", AES_256);
#endif

#ifdef BOTAN_HAS_BLOWFISH
   HANDLE_TYPE_NO_ARGS("Blowfish", Blowfish);
#endif

#ifdef BOTAN_HAS_CAST
   HANDLE_TYPE_NO_ARGS("CAST-128", CAST_128);
   HANDLE_TYPE_NO_ARGS("CAST-256", CAST_256);
#endif

#ifdef BOTAN_HAS_DES
   HANDLE_TYPE_NO_ARGS("DES", DES);
   HANDLE_TYPE_NO_ARGS("DESX", DESX);
   HANDLE_TYPE_NO_ARGS("TripleDES", TripleDES);
#endif

#ifdef BOTAN_HAS_GOST
   HANDLE_TYPE_NO_ARGS("GOST", GOST);
#endif

#ifdef BOTAN_HAS_IDEA
   HANDLE_TYPE_NO_ARGS("IDEA", IDEA);
#endif

#ifdef BOTAN_HAS_KASUMI
   HANDLE_TYPE_NO_ARGS("KASUMI", KASUMI);
#endif

#ifdef BOTAN_HAS_MARS
   HANDLE_TYPE_NO_ARGS("MARS", MARS);
#endif

#ifdef BOTAN_HAS_MISTY1
   HANDLE_TYPE_ONE_U32BIT("MISTY1", MISTY1, 8);
#endif

#ifdef BOTAN_HAS_NOEKEON
   HANDLE_TYPE_NO_ARGS("Noekeon", Noekeon);
#endif

#ifdef BOTAN_HAS_RC2
   HANDLE_TYPE_NO_ARGS("RC2", RC2);
#endif

#ifdef BOTAN_HAS_RC5
   HANDLE_TYPE_ONE_U32BIT("RC5", RC5, 12);
#endif

#ifdef BOTAN_HAS_RC6
   HANDLE_TYPE_NO_ARGS("RC6", RC6);
#endif

#ifdef BOTAN_HAS_SAFER
   HANDLE_TYPE_ONE_U32BIT("SAFER-SK", SAFER_SK, 10);
#endif

#ifdef BOTAN_HAS_SEED
   HANDLE_TYPE_NO_ARGS("SEED", SEED);
#endif

#ifdef BOTAN_HAS_SERPENT
   HANDLE_TYPE_NO_ARGS("Serpent", Serpent);
#endif

#ifdef BOTAN_HAS_SKIPJACK
   HANDLE_TYPE_NO_ARGS("Skipjack", Skipjack);
#endif

#ifdef BOTAN_HAS_SQUARE
   HANDLE_TYPE_NO_ARGS("Square", Square);
#endif

#ifdef BOTAN_HAS_TEA
   HANDLE_TYPE_NO_ARGS("TEA", TEA);
#endif

#ifdef BOTAN_HAS_TWOFISH
   HANDLE_TYPE_NO_ARGS("Twofish", Twofish);
#endif

#ifdef BOTAN_HAS_XTEA
   HANDLE_TYPE_NO_ARGS("XTEA", XTEA);
#endif

#ifdef BOTAN_HAS_LUBY_RACKOFF
   if(algo_name == "Luby-Rackoff" && name.size() >= 2)
      {
      HashFunction* hash = find_hash(name[1]);
      if(hash)
         return new LubyRackoff(hash);
      }
#endif

#ifdef BOTAN_HAS_LION
   if(algo_name == "Lion")
      {
      if(name.size() != 4)
         throw Invalid_Algorithm_Name(algo_spec);
      return new Lion(name[1], name[2], to_u32bit(name[3]));
      }
#endif

   return 0;
   }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
StreamCipher*
Default_Engine::find_stream_cipher(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;
   const std::string algo_name = global_state().deref_alias(name[0]);

#ifdef BOTAN_HAS_ARC4
   HANDLE_TYPE_ONE_U32BIT("ARC4", ARC4, 0);
   HANDLE_TYPE_ONE_U32BIT("RC4_drop", ARC4, 768);
#endif

#ifdef BOTAN_HAS_SALSA20
   HANDLE_TYPE_NO_ARGS("Salsa20", Salsa20);
#endif

#ifdef BOTAN_HAS_TURING
   HANDLE_TYPE_NO_ARGS("Turing", Turing);
#endif

#ifdef BOTAN_HAS_WID_WAKE
   HANDLE_TYPE_NO_ARGS("WiderWake4+1-BE", WiderWake_41_BE);
#endif

   return 0;
   }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
HashFunction*
Default_Engine::find_hash(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;
   const std::string algo_name = global_state().deref_alias(name[0]);

#ifdef BOTAN_HAS_ADLER32
   HANDLE_TYPE_NO_ARGS("Adler32", Adler32);
#endif

   HANDLE_TYPE_NO_ARGS("CRC24", CRC24);
   HANDLE_TYPE_NO_ARGS("CRC32", CRC32);
   HANDLE_TYPE_NO_ARGS("FORK-256", FORK_256);
   HANDLE_TYPE_NO_ARGS("HAS-160", HAS_160);
   HANDLE_TYPE_NO_ARGS("MD2", MD2);
   HANDLE_TYPE_NO_ARGS("MD4", MD4);
   HANDLE_TYPE_NO_ARGS("MD5", MD5);
   HANDLE_TYPE_NO_ARGS("RIPEMD-128", RIPEMD_128);
   HANDLE_TYPE_NO_ARGS("RIPEMD-160", RIPEMD_160);
   HANDLE_TYPE_NO_ARGS("SHA-160", SHA_160);
   HANDLE_TYPE_NO_ARGS("SHA-256", SHA_256);
   HANDLE_TYPE_NO_ARGS("SHA-384", SHA_384);
   HANDLE_TYPE_NO_ARGS("SHA-512", SHA_512);
   HANDLE_TYPE_TWO_U32BIT("Tiger", Tiger, 24);
   HANDLE_TYPE_NO_ARGS("Whirlpool", Whirlpool);

   if(algo_name == "Parallel")
      {
      if(name.size() < 2)
         throw Invalid_Algorithm_Name(algo_spec);
      name.erase(name.begin());
      return new Parallel(name);
      }
   return 0;
   }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
MessageAuthenticationCode*
Default_Engine::find_mac(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;
   const std::string algo_name = global_state().deref_alias(name[0]);

   HANDLE_TYPE_ONE_STRING("CBC-MAC", CBC_MAC);
   HANDLE_TYPE_ONE_STRING("CMAC", CMAC);
   HANDLE_TYPE_ONE_STRING("HMAC", HMAC);
   HANDLE_TYPE_ONE_STRING("SSL3-MAC", SSL3_MAC);
   HANDLE_TYPE_NO_ARGS("X9.19-MAC", ANSI_X919_MAC);

   return 0;
   }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
S2K* Default_Engine::find_s2k(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;

   const std::string algo_name = global_state().deref_alias(name[0]);

   HANDLE_TYPE_ONE_STRING("PBKDF1", PKCS5_PBKDF1);
   HANDLE_TYPE_ONE_STRING("PBKDF2", PKCS5_PBKDF2);
   HANDLE_TYPE_ONE_STRING("OpenPGP-S2K", OpenPGP_S2K);

   return 0;
   }

/*************************************************
* Look for an algorithm with this name           *
*************************************************/
BlockCipherModePaddingMethod*
Default_Engine::find_bc_pad(const std::string& algo_spec) const
   {
   std::vector<std::string> name = parse_algorithm_name(algo_spec);
   if(name.empty())
      return 0;

   const std::string algo_name = global_state().deref_alias(name[0]);

   HANDLE_TYPE_NO_ARGS("PKCS7",       PKCS7_Padding);
   HANDLE_TYPE_NO_ARGS("OneAndZeros", OneAndZeros_Padding);
   HANDLE_TYPE_NO_ARGS("X9.23",       ANSI_X923_Padding);
   HANDLE_TYPE_NO_ARGS("NoPadding",   Null_Padding);

   return 0;
   }

}