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