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
|
/*
* (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/aes.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_X931_RNG)
#include <botan/x931_rng.h>
#include <botan/des.h>
#endif
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
#include <botan/auto_rng.h>
#endif
using namespace Botan;
#include "common.h"
/* A weird little hack to fit S2K algorithms into the validation suite
You probably wouldn't ever want to actually use the S2K algorithms like
this, the raw S2K interface is more convenient for actually using them
*/
class S2K_Filter : public Filter
{
public:
void write(const byte in[], u32bit len)
{ passphrase += std::string(reinterpret_cast<const char*>(in), len); }
void end_msg()
{
SymmetricKey x = s2k->derive_key(outlen, passphrase,
&salt[0], salt.size(),
iterations);
send(x.bits_of());
}
S2K_Filter(S2K* algo, const SymmetricKey& s, u32bit o, u32bit i)
{
s2k = algo;
outlen = o;
iterations = i;
salt = s.bits_of();
}
~S2K_Filter() { delete s2k; }
private:
std::string passphrase;
S2K* s2k;
SecureVector<byte> salt;
u32bit outlen, iterations;
};
/* Not too useful generally; just dumps random bits for benchmarking */
class RNG_Filter : public Filter
{
public:
void write(const byte[], u32bit);
RNG_Filter(RandomNumberGenerator* r) : rng(r) {}
~RNG_Filter() { delete rng; }
private:
RandomNumberGenerator* rng;
};
class KDF_Filter : public Filter
{
public:
void write(const byte in[], u32bit len)
{ secret.append(in, len); }
void end_msg()
{
SymmetricKey x = kdf->derive_key(outlen,
secret, secret.size(),
salt, salt.size());
send(x.bits_of(), x.length());
}
KDF_Filter(KDF* algo, const SymmetricKey& s, u32bit o)
{
kdf = algo;
outlen = o;
salt = s.bits_of();
}
~KDF_Filter() { delete kdf; }
private:
SecureVector<byte> secret;
SecureVector<byte> salt;
KDF* kdf;
u32bit outlen;
};
Filter* lookup_s2k(const std::string& algname,
const std::vector<std::string>& params)
{
S2K* s2k = 0;
try {
s2k = get_s2k(algname);
}
catch(...) { }
if(s2k)
return new S2K_Filter(s2k, params[0], to_u32bit(params[1]),
to_u32bit(params[2]));
return 0;
}
void RNG_Filter::write(const byte[], u32bit length)
{
if(length)
{
SecureVector<byte> out(length);
rng->randomize(out, out.size());
send(out);
}
}
Filter* lookup_rng(const std::string& algname,
const std::string& key)
{
RandomNumberGenerator* prng = 0;
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
if(algname == "AutoSeeded")
prng = new AutoSeeded_RNG;
#endif
#if defined(BOTAN_HAS_X931_RNG)
if(algname == "X9.31-RNG(TripleDES)")
prng = new ANSI_X931_RNG(new TripleDES,
new Fixed_Output_RNG(decode_hex(key)));
else if(algname == "X9.31-RNG(AES-128)")
prng = new ANSI_X931_RNG(new AES_128,
new Fixed_Output_RNG(decode_hex(key)));
else if(algname == "X9.31-RNG(AES-192)")
prng = new ANSI_X931_RNG(new AES_192,
new Fixed_Output_RNG(decode_hex(key)));
else if(algname == "X9.31-RNG(AES-256)")
prng = new ANSI_X931_RNG(new AES_256,
new Fixed_Output_RNG(decode_hex(key)));
#endif
#if defined(BOTAN_HAS_RANDPOOL)
if(algname == "Randpool")
{
prng = new Randpool(new AES_256, new HMAC(new SHA_256));
prng->add_entropy(reinterpret_cast<const byte*>(key.c_str()),
key.length());
}
#endif
#if defined(BOTAN_HAS_X931_RNG)
// these are used for benchmarking: AES-256/SHA-256 matches library
// defaults, so benchmark reflects real-world performance (maybe)
if(algname == "X9.31-RNG")
{
RandomNumberGenerator* rng =
#if defined(BOTAN_HAS_HMAC_RNG)
new HMAC_RNG(new HMAC(new SHA_512), new HMAC(new SHA_256));
#elif defined(BOTAN_HAS_RANDPOOL)
new Randpool(new AES_256, new HMAC(new SHA_256));
#endif
prng = new ANSI_X931_RNG(new AES_256, rng);
}
#endif
#if defined(BOTAN_HAS_HMAC_RNG)
if(algname == "HMAC_RNG")
{
prng = new HMAC_RNG(new HMAC(new SHA_512), new HMAC(new SHA_256));
}
#endif
if(prng)
{
prng->add_entropy(reinterpret_cast<const byte*>(key.c_str()),
key.length());
return new RNG_Filter(prng);
}
return 0;
}
Filter* lookup_kdf(const std::string& algname, const std::string& salt,
const std::string& params)
{
KDF* kdf = 0;
try {
kdf = get_kdf(algname);
}
catch(...) { return 0; }
if(kdf)
return new KDF_Filter(kdf, salt, to_u32bit(params));
return 0;
}
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 0;
}
Filter* lookup(const std::string& algname,
const std::vector<std::string>& params)
{
std::string key = params[0];
std::string iv = params[1];
Filter* filter = 0;
// The order of the lookup has to change based on how the names are
// formatted and parsed.
filter = lookup_kdf(algname, key, iv);
if(filter) return filter;
filter = lookup_rng(algname, key);
if(filter) return filter;
filter = lookup_encoder(algname);
if(filter) return filter;
filter = lookup_s2k(algname, params);
if(filter) return filter;
return 0;
}
|