Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/rng/rng.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2016 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/rng.h>
8
#include <botan/entropy_src.h>
9
#include <botan/loadstor.h>
10
#include <botan/internal/os_utils.h>
11
12
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
13
  #include <botan/auto_rng.h>
14
#endif
15
16
namespace Botan {
17
18
void RandomNumberGenerator::randomize_with_ts_input(uint8_t output[], size_t output_len)
19
0
   {
20
0
   if(this->accepts_input())
21
0
      {
22
0
      /*
23
0
      Form additional input which is provided to the PRNG implementation
24
0
      to paramaterize the KDF output.
25
0
      */
26
0
      uint8_t additional_input[16] = { 0 };
27
0
      store_le(OS::get_system_timestamp_ns(), additional_input);
28
0
      store_le(OS::get_high_resolution_clock(), additional_input + 8);
29
0
30
0
      this->randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
31
0
      }
32
0
   else
33
0
      {
34
0
      this->randomize(output, output_len);
35
0
      }
36
0
   }
37
38
void RandomNumberGenerator::randomize_with_input(uint8_t output[], size_t output_len,
39
                                                 const uint8_t input[], size_t input_len)
40
0
   {
41
0
   this->add_entropy(input, input_len);
42
0
   this->randomize(output, output_len);
43
0
   }
44
45
size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs,
46
                                     size_t poll_bits,
47
                                     std::chrono::milliseconds poll_timeout)
48
0
   {
49
0
   if(this->accepts_input())
50
0
      {
51
0
      return srcs.poll(*this, poll_bits, poll_timeout);
52
0
      }
53
0
   else
54
0
      {
55
0
      return 0;
56
0
      }
57
0
   }
58
59
void RandomNumberGenerator::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits)
60
0
   {
61
0
   if(this->accepts_input())
62
0
      {
63
0
      secure_vector<uint8_t> buf(poll_bits / 8);
64
0
      rng.randomize(buf.data(), buf.size());
65
0
      this->add_entropy(buf.data(), buf.size());
66
0
      }
67
0
   }
68
69
RandomNumberGenerator* RandomNumberGenerator::make_rng()
70
0
   {
71
0
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
72
0
   return new AutoSeeded_RNG;
73
#else
74
   throw Not_Implemented("make_rng failed, no AutoSeeded_RNG in this build");
75
#endif
76
   }
77
78
#if defined(BOTAN_TARGET_OS_HAS_THREADS)
79
80
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
81
0
Serialized_RNG::Serialized_RNG() : m_rng(new AutoSeeded_RNG) {}
82
#else
83
Serialized_RNG::Serialized_RNG()
84
   {
85
   throw Not_Implemented("Serialized_RNG default constructor failed: AutoSeeded_RNG disabled in build");
86
   }
87
#endif
88
89
#endif
90
91
}