aboutsummaryrefslogtreecommitdiffstats
path: root/checks/dolook.cpp
blob: 92792c24bfd09a30aaeb90a7088b6cd289075980 (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
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <vector>
#include <string>

#include <botan/lookup.h>
#include <botan/filters.h>
#include <botan/libstate.h>
#include <botan/hmac.h>
#include <botan/sha2_32.h>
#include <botan/sha2_64.h>
#include <botan/parsing.h>

#ifdef BOTAN_HAS_COMPRESSOR_BZIP2
#include <botan/bzip2.h>
#endif

#ifdef BOTAN_HAS_COMPRESSOR_GZIP
#include <botan/gzip.h>
#endif

#ifdef BOTAN_HAS_COMPRESSOR_ZLIB
#include <botan/zlib.h>
#endif

#if defined(BOTAN_HAS_RANDPOOL)
  #include <botan/randpool.h>
#endif

#if defined(BOTAN_HAS_HMAC_RNG)
  #include <botan/hmac_rng.h>
#endif

#if defined(BOTAN_HAS_AES)
  #include <botan/aes.h>
#endif

#if defined(BOTAN_HAS_DES)
  #include <botan/des.h>
#endif

#if defined(BOTAN_HAS_X931_RNG)
  #include <botan/x931_rng.h>
#endif

#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
   #include <botan/auto_rng.h>
#endif

using namespace Botan;

#include "common.h"

namespace {

/* Not too useful generally; just dumps random bits for benchmarking */
class RNG_Filter : public Filter
   {
   public:
      std::string name() const { return rng->name(); }

      void write(const byte[], size_t);

      RNG_Filter(RandomNumberGenerator* r) : rng(r) {}
      ~RNG_Filter() { delete rng; }
   private:
      RandomNumberGenerator* rng;
   };

void RNG_Filter::write(const byte[], size_t length)
   {
   if(length)
      {
      send(rng->random_vec(length));
      }
   }

Filter* lookup_rng(const std::string& algname,
                   const std::string& key)
   {
   if(algname.find("X9.31-RNG(") == std::string::npos)
      return nullptr;

   RandomNumberGenerator* prng = nullptr;

#if defined(BOTAN_HAS_X931_RNG)

#if defined(BOTAN_HAS_DES)
   if(algname == "X9.31-RNG(TripleDES)")
      prng = new ANSI_X931_RNG(new TripleDES,
                               new Fixed_Output_RNG(hex_decode(key)));
#endif

#if defined(BOTAN_HAS_AES)
   if(algname == "X9.31-RNG(AES-128)")
      prng = new ANSI_X931_RNG(new AES_128,
                               new Fixed_Output_RNG(hex_decode(key)));
   else if(algname == "X9.31-RNG(AES-192)")
      prng = new ANSI_X931_RNG(new AES_192,
                               new Fixed_Output_RNG(hex_decode(key)));
   else if(algname == "X9.31-RNG(AES-256)")
      prng = new ANSI_X931_RNG(new AES_256,
                               new Fixed_Output_RNG(hex_decode(key)));
#endif

#endif

   if(prng)
      {
      prng->add_entropy(reinterpret_cast<const byte*>(key.c_str()),
                        key.length());
      return new RNG_Filter(prng);
      }

   return nullptr;
   }

Filter* lookup_encoder(const std::string& algname)
   {
   if(algname == "Base64_Encode")
      return new Base64_Encoder;
   if(algname == "Base64_Decode")
      return new Base64_Decoder;

#ifdef BOTAN_HAS_COMPRESSOR_BZIP2
   if(algname == "Bzip_Compression")
      return new Bzip_Compression(9);
   if(algname == "Bzip_Decompression")
      return new Bzip_Decompression;
#endif

#ifdef BOTAN_HAS_COMPRESSOR_GZIP
   if(algname == "Gzip_Compression")
      return new Gzip_Compression(9);
   if(algname == "Gzip_Decompression")
      return new Gzip_Decompression;
#endif

#ifdef BOTAN_HAS_COMPRESSOR_ZLIB
   if(algname == "Zlib_Compression")
      return new Zlib_Compression(9);
   if(algname == "Zlib_Decompression")
      return new Zlib_Decompression;
#endif

   return nullptr;
   }

}

Filter* lookup(const std::string& algname,
               const std::vector<std::string>& params)
   {
   std::string key = params[0];
   std::string iv = params[1];
   Filter* filter = nullptr;

   // The order of the lookup has to change based on how the names are
   // formatted and parsed.
   filter = lookup_rng(algname, key);
   if(filter) return filter;

   filter = lookup_encoder(algname);
   if(filter) return filter;

   return nullptr;
   }