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/contract.h>
21 #include <ripple/crypto/csprng.h>
22 #include <array>
23 #include <cassert>
24 #include <openssl/rand.h>
25 #include <random>
26 #include <stdexcept>
27 
28 namespace ripple {
29 
31 {
32  // This is not strictly necessary
33  if (RAND_poll() != 1)
34  Throw<std::runtime_error>("CSPRNG: Initial polling failed");
35 }
36 
38 {
39  // This cleanup function is not needed in newer versions of OpenSSL
40 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
41  RAND_cleanup();
42 #endif
43 }
44 
45 void
47 {
49 
50  {
51  // On every platform we support, std::random_device
52  // is non-deterministic and should provide some good
53  // quality entropy.
55 
56  for (auto& e : entropy)
57  e = rd();
58  }
59 
60  std::lock_guard lock(mutex_);
61 
62  // We add data to the pool, but we conservatively assume that
63  // it contributes no actual entropy.
64  RAND_add(
65  entropy.data(),
66  entropy.size() * sizeof(std::random_device::result_type),
67  0);
68 
69  if (buffer != nullptr && count != 0)
70  RAND_add(buffer, count, 0);
71 }
72 
73 void
75 {
76  // RAND_bytes is thread-safe on OpenSSL 1.1.0 and later when compiled
77  // with thread support, so we don't need to grab a mutex.
78  // https://mta.openssl.org/pipermail/openssl-users/2020-November/013146.html
79 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || !defined(OPENSSL_THREADS)
80  std::lock_guard lock(mutex_);
81 #endif
82 
83  auto const result =
84  RAND_bytes(reinterpret_cast<unsigned char*>(ptr), count);
85 
86  if (result != 1)
87  Throw<std::runtime_error>("CSPRNG: Insufficient entropy");
88 }
89 
92 {
93  result_type ret;
94  (*this)(&ret, sizeof(result_type));
95  return ret;
96 }
97 
100 {
101  static csprng_engine engine;
102  return engine;
103 }
104 
105 } // namespace ripple
ripple::csprng_engine::~csprng_engine
~csprng_engine()
Definition: csprng.cpp:37
ripple::csprng_engine::mix_entropy
void mix_entropy(void *buffer=nullptr, std::size_t count=0)
Mix entropy into the pool.
Definition: csprng.cpp:46
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:99
random
std::lock_guard
STL class.
stdexcept
ripple::csprng_engine::result_type
std::uint64_t result_type
Definition: csprng.h:43
std::random_device
ripple::csprng_engine::operator()
result_type operator()()
Generate a random integer.
Definition: csprng.cpp:91
ripple::csprng_engine::csprng_engine
csprng_engine()
Definition: csprng.cpp:30
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)