Changeset View
Changeset View
Standalone View
Standalone View
src/random.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_RANDOM_H | #ifndef BITCOIN_RANDOM_H | ||||
#define BITCOIN_RANDOM_H | #define BITCOIN_RANDOM_H | ||||
#include <crypto/chacha20.h> | #include <crypto/chacha20.h> | ||||
#include <crypto/common.h> | #include <crypto/common.h> | ||||
#include <uint256.h> | #include <uint256.h> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <limits> | #include <limits> | ||||
/** | /** | ||||
* Seed OpenSSL PRNG with additional entropy data. | * Generate random data via the internal PRNG. | ||||
*/ | * | ||||
void RandAddSeed(); | * These functions are designed to be fast (sub microsecond), but do not | ||||
* necessarily meaningfully add entropy to the PRNG state. | |||||
/** | * | ||||
* Functions to gather random data via the OpenSSL PRNG | * Thread-safe. | ||||
*/ | */ | ||||
void GetRandBytes(uint8_t *buf, int num); | void GetRandBytes(uint8_t *buf, int num); | ||||
uint64_t GetRand(uint64_t nMax); | uint64_t GetRand(uint64_t nMax); | ||||
int GetRandInt(int nMax); | int GetRandInt(int nMax); | ||||
uint256 GetRandHash(); | uint256 GetRandHash(); | ||||
/** | /** | ||||
* Add a little bit of randomness to the output of GetStrongRangBytes. | * Gather entropy from various sources, feed it into the internal PRNG, and | ||||
* This sleeps for a millisecond, so should only be called when there is no | * generate random data using it. | ||||
* other work to be done. | * | ||||
* This function will cause failure whenever the OS RNG fails. | |||||
* | |||||
* Thread-safe. | |||||
*/ | */ | ||||
void RandAddSeedSleep(); | void GetStrongRandBytes(uint8_t *buf, int num); | ||||
/** | /** | ||||
* Function to gather random data from multiple sources, failing whenever any of | * Sleep for 1ms, gather entropy from various sources, and feed them to the PRNG | ||||
* those sources fail to provide a result. | * state. | ||||
* | |||||
* Thread-safe. | |||||
*/ | */ | ||||
void GetStrongRandBytes(uint8_t *buf, int num); | void RandAddSeedSleep(); | ||||
/** | /** | ||||
* Fast randomness source. This is seeded once with secure random data, but is | * Fast randomness source. This is seeded once with secure random data, but | ||||
* completely deterministic and insecure after that. | * is completely deterministic and does not gather more entropy after that. | ||||
* | |||||
* This class is not thread-safe. | * This class is not thread-safe. | ||||
*/ | */ | ||||
class FastRandomContext { | class FastRandomContext { | ||||
private: | private: | ||||
bool requires_seed; | bool requires_seed; | ||||
ChaCha20 rng; | ChaCha20 rng; | ||||
uint8_t bytebuf[64]; | uint8_t bytebuf[64]; | ||||
▲ Show 20 Lines • Show All 149 Lines • Show Last 20 Lines |