rippled
csprng.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/ByteUtilities.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/crypto/csprng.h>
23 #include <array>
24 #include <cassert>
25 #include <openssl/rand.h>
26 #include <random>
27 #include <stdexcept>
28 
29 namespace ripple {
30 
31 void
32 csprng_engine::mix(void* data, std::size_t size, double bitsPerByte)
33 {
34  assert(data != nullptr);
35  assert(size != 0);
36  assert(bitsPerByte != 0);
37 
38  std::lock_guard lock(mutex_);
39  RAND_add(data, size, (size * bitsPerByte) / 8.0);
40 }
41 
43 {
44  mix_entropy();
45 }
46 
48 {
49  RAND_cleanup();
50 }
51 
52 void
54 {
56 
57  {
58  // On every platform we support, std::random_device
59  // is non-deterministic and should provide some good
60  // quality entropy.
62 
63  for (auto& e : entropy)
64  e = rd();
65  }
66 
67  // Assume 2 bits per byte for the system entropy:
68  mix(entropy.data(),
69  entropy.size() * sizeof(std::random_device::result_type),
70  2.0);
71 
72  // We want to be extremely conservative about estimating
73  // how much entropy the buffer the user gives us contains
74  // and assume only 0.5 bits of entropy per byte:
75  if (buffer != nullptr && count != 0)
76  mix(buffer, count, 0.5);
77 }
78 
81 {
82  result_type ret;
83 
84  std::lock_guard lock(mutex_);
85 
86  auto const result =
87  RAND_bytes(reinterpret_cast<unsigned char*>(&ret), sizeof(ret));
88 
89  if (result == 0)
90  Throw<std::runtime_error>("Insufficient entropy");
91 
92  return ret;
93 }
94 
95 void
97 {
98  std::lock_guard lock(mutex_);
99 
100  auto const result =
101  RAND_bytes(reinterpret_cast<unsigned char*>(ptr), count);
102 
103  if (result != 1)
104  Throw<std::runtime_error>("Insufficient entropy");
105 }
106 
109 {
110  static csprng_engine engine;
111  return engine;
112 }
113 
114 } // namespace ripple
ripple::csprng_engine::~csprng_engine
~csprng_engine()
Definition: csprng.cpp:47
ripple::csprng_engine::mix_entropy
void mix_entropy(void *buffer=nullptr, std::size_t count=0)
Mix entropy into the pool.
Definition: csprng.cpp:53
ripple::csprng_engine::mutex_
std::mutex mutex_
Definition: csprng.h:40
std::array::size
T size(T... args)
ripple::crypto_prng
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
Definition: csprng.cpp:108
random
std::lock_guard
STL class.
ripple::csprng_engine::mix
void mix(void *buffer, std::size_t count, double bitsPerByte)
Definition: csprng.cpp:32
stdexcept
std::random_device
ripple::csprng_engine::operator()
result_type operator()()
Generate a random integer.
Definition: csprng.cpp:80
ripple::csprng_engine::csprng_engine
csprng_engine()
Definition: csprng.cpp:42
array
ripple::csprng_engine
A cryptographically secure random number engine.
Definition: csprng.h:37
std::uint64_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
cassert
std::size_t
std::array::data
T data(T... args)