blob: 472765704d410134ac1ee04c0b921372081c4018 (
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
|
/*
* (C) 2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "cli.h"
#include "../tests/test_rng.h" // FIXME
#include <botan/entropy_src.h>
#if defined(BOTAN_HAS_COMPRESSION)
#include <botan/compression.h>
#endif
namespace Botan_CLI {
class Entropy final : public Command
{
public:
Entropy() : Command("entropy --truncate-at=128 source") {}
std::string group() const override
{
return "misc";
}
std::string description() const override
{
return "Sample a raw entropy source";
}
void go() override
{
const std::string req_source = get_arg("source");
const size_t truncate_sample = get_arg_sz("truncate-at");
auto& entropy_sources = Botan::Entropy_Sources::global_sources();
std::vector<std::string> sources;
if(req_source == "all")
sources = entropy_sources.enabled_sources();
else
sources.push_back(req_source);
for(std::string source : sources)
{
Botan_Tests::SeedCapturing_RNG rng;
const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
if(rng.samples() == 0)
{
output() << "Source " << source << " is unavailable\n";
continue;
}
const auto& sample = rng.seed_material();
output() << "Polling " << source << " gathered " << sample.size()
<< " bytes in " << rng.samples() << " outputs with estimated entropy "
<< entropy_estimate << "\n";
#if defined(BOTAN_HAS_COMPRESSION)
if(!sample.empty())
{
auto comp = Botan::Compression_Algorithm::create("zlib");
if(comp)
{
try
{
Botan::secure_vector<uint8_t> compressed;
compressed.assign(sample.begin(), sample.end());
comp->start(9);
comp->finish(compressed);
if(compressed.size() < sample.size())
{
output() << "Sample from " << source << " was zlib compressed from " << sample.size()
<< " bytes to " << compressed.size() << " bytes\n";
}
}
catch(std::exception& e)
{
error_output() << "Error while attempting to compress: " << e.what() << "\n";
}
}
}
#endif
if(sample.size() <= truncate_sample)
{
output() << Botan::hex_encode(sample) << "\n";
}
else if(truncate_sample > 0)
{
output() << Botan::hex_encode(&sample[0], truncate_sample) << "...\n";
}
}
}
};
BOTAN_REGISTER_COMMAND("entropy", Entropy);
}
|