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
|
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "test_rng.h"
#include "tests.h"
#include <botan/hex.h>
#include <botan/lookup.h>
#include <iostream>
#include <fstream>
#if defined(BOTAN_HAS_HMAC_DRBG)
#include <botan/hmac_drbg.h>
#endif
#if defined(BOTAN_HAS_X931_RNG)
#include <botan/x931_rng.h>
#endif
using namespace Botan;
namespace {
RandomNumberGenerator* get_rng(const std::string& algo_str, const std::string& ikm_hex)
{
class AllOnce_RNG : public Fixed_Output_RNG
{
public:
AllOnce_RNG(const std::vector<byte>& in) : Fixed_Output_RNG(in) {}
Botan::secure_vector<byte> random_vec(size_t)
{
Botan::secure_vector<byte> vec(this->remaining());
this->randomize(&vec[0], vec.size());
return vec;
}
};
const auto ikm = hex_decode(ikm_hex);
const auto algo_name = parse_algorithm_name(algo_str);
const std::string rng_name = algo_name[0];
#if defined(BOTAN_HAS_HMAC_DRBG)
if(rng_name == "HMAC_DRBG")
return new HMAC_DRBG(get_mac("HMAC(" + algo_name[1] + ")"), new AllOnce_RNG(ikm));
#endif
#if defined(BOTAN_HAS_X931_RNG)
if(rng_name == "X9.31-RNG")
return new ANSI_X931_RNG(get_block_cipher(algo_name[1]),
new Fixed_Output_RNG(ikm));
#endif
return nullptr;
}
size_t x931_test(const std::string& algo,
const std::string& ikm,
const std::string& out,
size_t L)
{
std::unique_ptr<RandomNumberGenerator> rng(get_rng(algo, ikm));
if(!rng)
throw std::runtime_error("Unknown RNG " + algo);
const std::string got = hex_encode(rng->random_vec(L));
if(got != out)
{
std::cout << "X9.31 " << got << " != " << out << std::endl;
return 1;
}
return 0;
}
size_t hmac_drbg_test(std::map<std::string, std::string> m)
{
const std::string algo = m["RNG"];
const std::string ikm = m["EntropyInput"];
std::unique_ptr<RandomNumberGenerator> rng(get_rng(algo, ikm));
if(!rng)
throw std::runtime_error("Unknown RNG " + algo);
rng->reseed(0); // force initialization
// now reseed
const auto reseed_input = hex_decode(m["EntropyInputReseed"]);
rng->add_entropy(&reseed_input[0], reseed_input.size());
const std::string out = m["Out"];
const size_t out_len = out.size() / 2;
rng->random_vec(out_len); // gen 1st block (discarded)
const std::string got = hex_encode(rng->random_vec(out_len));
if(got != out)
{
std::cout << algo << " " << got << " != " << out << std::endl;
return 1;
}
return 0;
}
}
size_t test_rngs()
{
std::ifstream hmac_drbg_vec(TEST_DATA_DIR "/hmac_drbg.vec");
std::ifstream x931_vec(TEST_DATA_DIR "/x931.vec");
size_t fails = 0;
fails += run_tests_bb(hmac_drbg_vec, "RNG", "Out", true, hmac_drbg_test);
fails += run_tests_bb(x931_vec, "RNG", "Out", true,
[](std::map<std::string, std::string> m) -> size_t
{
return x931_test(m["RNG"], m["IKM"], m["Out"], to_u32bit(m["L"]));
});
return fails;
}
|