rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/contract.h>
21#include <xrpl/beast/utility/instrumentation.h>
22#include <xrpl/crypto/csprng.h>
23#include <array>
24#include <openssl/rand.h>
25#include <random>
26#include <stdexcept>
27
28namespace 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
45void
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
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
73void
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)
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
A cryptographically secure random number engine.
Definition: csprng.h:38
std::uint64_t result_type
Definition: csprng.h:43
result_type operator()()
Generate a random integer.
Definition: csprng.cpp:91
std::mutex mutex_
Definition: csprng.h:40
void mix_entropy(void *buffer=nullptr, std::size_t count=0)
Mix entropy into the pool.
Definition: csprng.cpp:46
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
Definition: csprng.cpp:99
T size(T... args)