~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/lib/rng/rng.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   {
 
20
   /*
 
21
   Form additional input which is provided to the PRNG implementation
 
22
   to paramaterize the KDF output.
 
23
   */
 
24
   uint8_t additional_input[16] = { 0 };
 
25
   store_le(OS::get_system_timestamp_ns(), additional_input);
 
26
   store_le(OS::get_high_resolution_clock(), additional_input + 8);
 
27
 
 
28
   randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
 
29
   }
 
30
 
 
31
void RandomNumberGenerator::randomize_with_input(uint8_t output[], size_t output_len,
 
32
                                                 const uint8_t input[], size_t input_len)
 
33
   {
 
34
   this->add_entropy(input, input_len);
 
35
   this->randomize(output, output_len);
 
36
   }
 
37
 
 
38
size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs,
 
39
                                     size_t poll_bits,
 
40
                                     std::chrono::milliseconds poll_timeout)
 
41
   {
 
42
   return srcs.poll(*this, poll_bits, poll_timeout);
 
43
   }
 
44
 
 
45
void RandomNumberGenerator::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits)
 
46
   {
 
47
   secure_vector<uint8_t> buf(poll_bits / 8);
 
48
   rng.randomize(buf.data(), buf.size());
 
49
   this->add_entropy(buf.data(), buf.size());
 
50
   }
 
51
 
 
52
RandomNumberGenerator* RandomNumberGenerator::make_rng()
 
53
   {
 
54
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
 
55
   return new AutoSeeded_RNG;
 
56
#else
 
57
   throw Exception("make_rng failed, no AutoSeeded_RNG in this build");
 
58
#endif
 
59
   }
 
60
 
 
61
#if defined(BOTAN_TARGET_OS_HAS_THREADS)
 
62
 
 
63
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
 
64
Serialized_RNG::Serialized_RNG() : m_rng(new AutoSeeded_RNG) {}
 
65
#else
 
66
Serialized_RNG::Serialized_RNG()
 
67
   {
 
68
   throw Exception("Serialized_RNG default constructor failed: AutoSeeded_RNG disabled in build");
 
69
   }
 
70
#endif
 
71
 
 
72
#endif
 
73
 
 
74
}