/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/internal/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 | | /* |
23 | | Form additional input which is provided to the PRNG implementation |
24 | | to paramaterize the KDF output. |
25 | | */ |
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 | |
|
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 | | } |