• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 11268576238

10 Oct 2024 06:22AM UTC coverage: 90.983% (-0.02%) from 91.007%
11268576238

push

github

web-flow
Merge pull request #4325 from plancksecurity/jitter

Add jitterentropy as RNG

89487 of 98356 relevant lines covered (90.98%)

8933666.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/cli/entropy.cpp
1
/*
2
* (C) 2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "../tests/test_rng.h"  // FIXME
8
#include "cli.h"
9

10
#include <botan/entropy_src.h>
11

12
#if defined(BOTAN_HAS_COMPRESSION)
13
   #include <botan/compression.h>
14
#endif
15

16
namespace Botan_CLI {
17

18
class Entropy final : public Command {
19
   public:
20
      Entropy() : Command("entropy --truncate-at=128 source") {}
4✔
21

22
      std::string group() const override { return "misc"; }
1✔
23

24
      std::string description() const override { return "Sample a raw entropy source"; }
1✔
25

26
      void go() override {
1✔
27
         const std::string req_source = get_arg("source");
1✔
28
         const size_t truncate_sample = get_arg_sz("truncate-at");
1✔
29

30
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
31

32
         std::vector<std::string> sources;
1✔
33
         if(req_source == "all") {
1✔
34
            sources = entropy_sources.enabled_sources();
1✔
35
         } else {
36
            sources.push_back(req_source);
×
37
         }
38

39
         for(const std::string& source : sources) {
5✔
40
            Botan_Tests::SeedCapturing_RNG rng;
4✔
41
            const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
4✔
42

43
            if(rng.samples() == 0) {
4✔
44
               output() << "Source " << source << " is unavailable\n";
×
45
               continue;
×
46
            }
47

48
            const auto& sample = rng.seed_material();
4✔
49

50
            output() << "Polling " << source << " gathered " << sample.size() << " bytes in " << rng.samples()
4✔
51
                     << " outputs with estimated entropy " << entropy_estimate << "\n";
4✔
52

53
#if defined(BOTAN_HAS_COMPRESSION)
54
            if(!sample.empty()) {
4✔
55
               auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
56
               if(comp) {
4✔
57
                  try {
4✔
58
                     Botan::secure_vector<uint8_t> compressed;
4✔
59
                     compressed.assign(sample.begin(), sample.end());
4✔
60
                     comp->start(9);
4✔
61
                     comp->finish(compressed);
4✔
62

63
                     if(compressed.size() < sample.size()) {
4✔
64
                        output() << "Sample from " << source << " was zlib compressed from " << sample.size()
×
65
                                 << " bytes to " << compressed.size() << " bytes\n";
×
66
                     }
67
                  } catch(std::exception& e) {
4✔
68
                     error_output() << "Error while attempting to compress: " << e.what() << "\n";
×
69
                  }
×
70
               }
71
            }
4✔
72
#endif
73

74
            if(sample.size() <= truncate_sample) {
4✔
75
               output() << Botan::hex_encode(sample) << "\n";
2✔
76
            } else if(truncate_sample > 0) {
3✔
77
               output() << Botan::hex_encode(&sample[0], truncate_sample) << "...\n";
9✔
78
            }
79
         }
4✔
80
      }
1✔
81
};
82

83
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
84

85
}  // namespace Botan_CLI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc