Botan 2.19.2
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
Botan::PKCS11::PKCS11_RNG Class Referencefinalabstract

A random generator that only fetches random from the PKCS#11 RNG. More...

#include <p11_randomgenerator.h>

Inheritance diagram for Botan::PKCS11::PKCS11_RNG:
Botan::Hardware_RNG Botan::RandomNumberGenerator

Public Member Functions

bool accepts_input () const override
void add_entropy (const uint8_t in[], std::size_t length) override
 Calls C_SeedRandom to add entropy to the random generation function of the token/middleware. More...
virtual void add_entropy (const uint8_t input[], size_t length)=0
template<typename T >
void add_entropy_T (const T &t)
virtual void clear () final override
bool is_seeded () const override
 Always returns true. More...
Modulemodule () const
std::string name () const override
uint8_t next_byte ()
uint8_t next_nonzero_byte ()
 PKCS11_RNG (Session &session)
 Initialize the RNG with the PKCS#11 session that provides access to the cryptoki functions. More...
secure_vector< uint8_t > random_vec (size_t bytes)
template<typename Alloc >
void random_vec (std::vector< uint8_t, Alloc > &v, size_t bytes)
virtual void randomize (uint8_t output[], size_t length)=0
void randomize (uint8_t output[], std::size_t length) override
 Calls C_GenerateRandom to generate random data. More...
virtual void randomize_with_input (uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
virtual void randomize_with_ts_input (uint8_t output[], size_t output_len)
size_t reseed (Entropy_Sources &, size_t, std::chrono::milliseconds) override
 No operation - always returns 0. More...
virtual void reseed_from_rng (RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)

Static Public Member Functions

static RandomNumberGeneratormake_rng ()

Detailed Description

A random generator that only fetches random from the PKCS#11 RNG.

Definition at line 25 of file p11_randomgenerator.h.

Constructor & Destructor Documentation

◆ PKCS11_RNG()

Botan::PKCS11::PKCS11_RNG::PKCS11_RNG ( Session session)

Initialize the RNG with the PKCS#11 session that provides access to the cryptoki functions.

Definition at line 15 of file p11_randomgenerator.cpp.

16 : m_session(session)
17 {}

Member Function Documentation

◆ accepts_input()

bool Botan::PKCS11::PKCS11_RNG::accepts_input ( ) const

Returns false if it is known that this RNG object is not able to accept externally provided inputs (via add_entropy, randomize_with_input, etc). In this case, any such provided inputs are ignored.

If this function returns true, then inputs may or may not be accepted.

Implements Botan::RandomNumberGenerator.

Definition at line 61 of file p11_randomgenerator.h.

61{ return true; }

◆ add_entropy() [1/2]

void Botan::PKCS11::PKCS11_RNG::add_entropy ( const uint8_t  in[],
std::size_t  length 

Calls C_SeedRandom to add entropy to the random generation function of the token/middleware.

Definition at line 24 of file p11_randomgenerator.cpp.

25 {
26 module()->C_SeedRandom(m_session.get().handle(), const_cast<uint8_t*>(in), Ulong(length));
27 }
bool C_SeedRandom(SessionHandle session, Byte *seed_ptr, Ulong seed_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:735
Definition: p11.h:838

References Botan::PKCS11::LowLevel::C_SeedRandom(), and module().

◆ add_entropy() [2/2]

virtual void Botan::RandomNumberGenerator::add_entropy ( const uint8_t  input[],
size_t  length 
pure virtualinherited

Incorporate some additional data into the RNG state. For example adding nonces or timestamps from a peer's protocol message can help hedge against VM state rollback attacks. A few RNG types do not accept any externally provided input, in which case this function is a no-op.

inputa byte array containg the entropy to be added
lengththe length of the byte array in

Implemented in Botan::TPM_RNG, Botan::AutoSeeded_RNG, Botan::System_RNG, Botan::Stateful_RNG, Botan::Processor_RNG, Botan::RDRAND_RNG, and Botan::Null_RNG.

Referenced by Botan::System_RNG::add_entropy(), botan_rng_add_entropy(), Botan::Device_EntropySource::poll(), Botan::Getentropy::poll(), Botan::Intel_Rdseed::poll(), Botan::RandomNumberGenerator::randomize_with_input(), and Botan::RandomNumberGenerator::reseed_from_rng().

◆ add_entropy_T()

template<typename T >
void Botan::RandomNumberGenerator::add_entropy_T ( const T t)

Incorporate some additional data into the RNG state.

Definition at line 69 of file rng.h.

70 {
71 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value, "add_entropy_T data must be POD");
72 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
73 }
virtual void add_entropy(const uint8_t input[], size_t length)=0
fe T
Definition: ge.cpp:37

References T.

Referenced by Botan::Win32_EntropySource::poll().

◆ clear()

virtual void Botan::Hardware_RNG::clear ( )

Clear all internally held values of this RNG

is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 199 of file rng.h.

199{ /* no way to clear state of hardware RNG */ }

◆ is_seeded()

bool Botan::PKCS11::PKCS11_RNG::is_seeded ( ) const

Always returns true.

Implements Botan::RandomNumberGenerator.

Definition at line 37 of file p11_randomgenerator.h.

38 {
39 return true;
40 }

◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng ( )

Create a seeded and active RNG object for general application use Added in 1.8.0 Use AutoSeeded_RNG instead

Definition at line 69 of file rng.cpp.

70 {
72 return new AutoSeeded_RNG;
74 throw Not_Implemented("make_rng failed, no AutoSeeded_RNG in this build");
76 }

◆ module()

Module & Botan::PKCS11::PKCS11_RNG::module ( ) const
the module used by this RNG

Definition at line 49 of file p11_randomgenerator.h.

50 {
51 return m_session.get().module();
52 }

Referenced by add_entropy(), and randomize().

◆ name()

std::string Botan::PKCS11::PKCS11_RNG::name ( ) const
the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 31 of file p11_randomgenerator.h.

32 {
33 return "PKCS11_RNG";
34 }

◆ next_byte()

uint8_t Botan::RandomNumberGenerator::next_byte ( )

Return a random byte

random byte

Definition at line 161 of file rng.h.

162 {
163 uint8_t b;
164 this->randomize(&b, 1);
165 return b;
166 }
virtual void randomize(uint8_t output[], size_t length)=0

Referenced by Botan::random_prime().

◆ next_nonzero_byte()

uint8_t Botan::RandomNumberGenerator::next_nonzero_byte ( )
a random byte that is greater than zero

Definition at line 171 of file rng.h.

172 {
173 uint8_t b = this->next_byte();
174 while(b == 0)
175 b = this->next_byte();
176 return b;
177 }

Referenced by Botan::EME_PKCS1v15::pad().

◆ random_vec() [1/2]

secure_vector< uint8_t > Botan::RandomNumberGenerator::random_vec ( size_t  bytes)

◆ random_vec() [2/2]

template<typename Alloc >
void Botan::RandomNumberGenerator::random_vec ( std::vector< uint8_t, Alloc > &  v,
size_t  bytes 

Definition at line 151 of file rng.h.

152 {
153 v.resize(bytes);
154 this->randomize(, v.size());
155 }

◆ randomize() [1/2]

virtual void Botan::RandomNumberGenerator::randomize ( uint8_t  output[],
size_t  length 
pure virtualinherited

◆ randomize() [2/2]

void Botan::PKCS11::PKCS11_RNG::randomize ( uint8_t  output[],
std::size_t  length 

Calls C_GenerateRandom to generate random data.

Definition at line 19 of file p11_randomgenerator.cpp.

20 {
21 module()->C_GenerateRandom(m_session.get().handle(), output, Ulong(length));
22 }
bool C_GenerateRandom(SessionHandle session, Byte *random_data_ptr, Ulong random_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:743

References Botan::PKCS11::LowLevel::C_GenerateRandom(), and module().

◆ randomize_with_input()

void Botan::RandomNumberGenerator::randomize_with_input ( uint8_t  output[],
size_t  output_len,
const uint8_t  input[],
size_t  input_len 

Incorporate entropy into the RNG state then produce output. Some RNG types implement this using a single operation, default calls add_entropy + randomize in sequence.

Use this to further bind the outputs to your current process/protocol state. For instance if generating a new key for use in a session, include a session ID or other such value. See NIST SP 800-90 A, B, C series for more ideas.

outputbuffer to hold the random output
output_lensize of the output buffer in bytes
inputentropy buffer to incorporate
input_lensize of the input buffer in bytes

Reimplemented in Botan::AutoSeeded_RNG, and Botan::Stateful_RNG.

Definition at line 38 of file rng.cpp.

40 {
41 this->add_entropy(input, input_len);
42 this->randomize(output, output_len);
43 }

References Botan::RandomNumberGenerator::add_entropy(), and Botan::RandomNumberGenerator::randomize().

Referenced by Botan::RandomNumberGenerator::randomize_with_ts_input().

◆ randomize_with_ts_input()

void Botan::RandomNumberGenerator::randomize_with_ts_input ( uint8_t  output[],
size_t  output_len 

This calls randomize_with_input using some timestamps as extra input.

For a stateful RNG using non-random but potentially unique data the extra input can help protect against problems with fork, VM state rollback, or other cases where somehow an RNG state is duplicated. If both of the duplicated RNG states later incorporate a timestamp (and the timestamps don't themselves repeat), their outputs will diverge.

Reimplemented in Botan::Stateful_RNG.

Definition at line 18 of file rng.cpp.

19 {
20 if(this->accepts_input())
21 {
22 /*
23 Form additional input which is provided to the PRNG implementation
24 to paramaterize the KDF output.
25 */
26 uint8_t additional_input[16] = { 0 };
27 store_le(OS::get_system_timestamp_ns(), additional_input);
28 store_le(OS::get_high_resolution_clock(), additional_input + 8);
30 this->randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
31 }
32 else
33 {
34 this->randomize(output, output_len);
35 }
36 }
virtual bool accepts_input() const =0
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:38
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:241
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:293
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:454

References Botan::RandomNumberGenerator::accepts_input(), Botan::OS::get_high_resolution_clock(), Botan::OS::get_system_timestamp_ns(), Botan::RandomNumberGenerator::randomize(), Botan::RandomNumberGenerator::randomize_with_input(), and Botan::store_le().

◆ reseed()

size_t Botan::PKCS11::PKCS11_RNG::reseed ( Entropy_Sources ,
size_t  ,

No operation - always returns 0.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 43 of file p11_randomgenerator.h.

44 {
45 return 0;
46 }

◆ reseed_from_rng()

void Botan::RandomNumberGenerator::reseed_from_rng ( RandomNumberGenerator rng,
size_t  poll_bits = BOTAN_RNG_RESEED_POLL_BITS 

Reseed by reading specified bits from the RNG

Reimplemented in Botan::Stateful_RNG.

Definition at line 59 of file rng.cpp.

60 {
61 if(this->accepts_input())
62 {
63 secure_vector<uint8_t> buf(poll_bits / 8);
64 rng.randomize(, buf.size());
65 this->add_entropy(, buf.size());
66 }
67 }

References Botan::RandomNumberGenerator::accepts_input(), Botan::RandomNumberGenerator::add_entropy(), and Botan::RandomNumberGenerator::randomize().

Referenced by botan_rng_reseed(), botan_rng_reseed_from_rng(), and Botan::Stateful_RNG::reseed_from_rng().

The documentation for this class was generated from the following files: