5
#include <botan/botan.h>
6
#include <botan/lookup.h>
7
#include <botan/look_pk.h>
8
#include <botan/filters.h>
9
#include <botan/randpool.h>
10
#include <botan/x931_rng.h>
11
#include <botan/rng.h>
12
using namespace Botan;
14
/* A weird little hack to fit S2K algorithms into the validation suite
15
You probably wouldn't ever want to actually use the S2K algorithms like
16
this, the raw S2K interface is more convenient for actually using them
18
class S2K_Filter : public Filter
21
void write(const byte in[], u32bit len)
22
{ passphrase += std::string((const char*)in, len); }
25
s2k->change_salt(salt, salt.size());
26
s2k->set_iterations(iterations);
27
SymmetricKey x = s2k->derive_key(outlen, passphrase);
30
S2K_Filter(S2K* algo, const SymmetricKey& s, u32bit o, u32bit i)
37
~S2K_Filter() { delete s2k; }
39
std::string passphrase;
41
SecureVector<byte> salt;
42
u32bit outlen, iterations;
45
/* Not too useful generally; just dumps random bits for benchmarking */
46
class RNG_Filter : public Filter
49
void write(const byte[], u32bit);
50
RNG_Filter(RandomNumberGenerator* r) : rng(r), buffer(1024)
52
Global_RNG::randomize(buffer, buffer.size());
53
rng->add_entropy(buffer, buffer.size());
55
~RNG_Filter() { delete rng; }
57
RandomNumberGenerator* rng;
58
SecureVector<byte> buffer;
61
class KDF_Filter : public Filter
64
void write(const byte in[], u32bit len)
65
{ secret.append(in, len); }
68
SymmetricKey x = kdf->derive_key(outlen,
69
secret, secret.size(),
71
send(x.bits_of(), x.length());
73
KDF_Filter(KDF* algo, const SymmetricKey& s, u32bit o)
79
~KDF_Filter() { delete kdf; }
81
SecureVector<byte> secret;
82
SecureVector<byte> salt;
87
Filter* lookup_s2k(const std::string& algname,
88
const std::vector<std::string>& params)
93
s2k = get_s2k(algname);
98
return new S2K_Filter(s2k, params[0], to_u32bit(params[1]),
99
to_u32bit(params[2]));
103
void RNG_Filter::write(const byte[], u32bit length)
107
u32bit gen = std::min(buffer.size(), length);
108
rng->randomize(buffer, gen);
113
Filter* lookup_rng(const std::string& algname)
115
if(algname == "X9.31-RNG")
116
return new RNG_Filter(new ANSI_X931_RNG);
117
if(algname == "Randpool")
118
return new RNG_Filter(new Randpool);
122
Filter* lookup_kdf(const std::string& algname, const std::string& salt,
123
const std::string& params)
127
kdf = get_kdf(algname);
129
catch(...) { return 0; }
132
return new KDF_Filter(kdf, salt, to_u32bit(params));