Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | List of all members
Botan::PKCS11::PKCS11_RNG Class Referencefinal abstract

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

`#include <p11_randomgenerator.h>`

Inheritance diagram for Botan::PKCS11::PKCS11_RNG:

## Public Member Functions
virtual void | add_entropy (const uint8_t input[], size_t length)=0
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...
template<typename T >
void | add_entropy_T (const T &t)
void | clear () override
bool | is_seeded () const override
| Always returns true. More...
Module & | module () 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)
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 RandomNumberGenerator * | make_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

Botan::PKCS11::PKCS11_RNG::PKCS11_RNG | ( | Session & | session | ) |
| explicit

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

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

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.

Parameters

  input | a byte array containg the entropy to be added
  length | the length of the byte array in

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

Referenced by Botan::System_RNG::add_entropy(), Botan::Stateful_RNG::initialize_with(), Botan::Getentropy::poll(), Botan::Darwin_SecRandom::poll(), Botan::Intel_Rdrand::poll(), Botan::Win32_CAPI_EntropySource::poll(), Botan::Device_EntropySource::poll(), Botan::ProcWalking_EntropySource::poll(), Botan::RandomNumberGenerator::randomize_with_input(), and Botan::RandomNumberGenerator::reseed_from_rng().

void Botan::PKCS11::PKCS11_RNG::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.

Definition at line 24 of file p11_randomgenerator.cpp.

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

25 {
26 module()->C_SeedRandom(m_session.get().handle(), const_cast<uint8_t*>(in), length);
27 }
Botan::PKCS11::PKCS11_RNG::module
Module & module() const
Definition: p11_randomgenerator.h:52
Botan::PKCS11::LowLevel::C_SeedRandom
bool C_SeedRandom(SessionHandle session, Byte *seed_ptr, Ulong seed_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:734
template<typename T >

void Botan::RandomNumberGenerator::add_entropy_T | ( | const T & | t | ) |
| inline inherited

Incorporate some additional data into the RNG state.

Definition at line 60 of file rng.h.

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

61 {
62 this->add_entropy(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
63 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0

void Botan::PKCS11::PKCS11_RNG::clear | ( |  | ) |
| inline override virtual

Clear all internally held values of this RNG

Postcondition
  is_seeded() == false

Implements Botan::RandomNumberGenerator.

Definition at line 31 of file p11_randomgenerator.h.

32 {}

bool Botan::PKCS11::PKCS11_RNG::is_seeded | ( |  | ) | const
| inline override virtual

Always returns true.

Implements Botan::RandomNumberGenerator.

Definition at line 40 of file p11_randomgenerator.h.

41 {
42 return true;
43 }

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng | ( |  | ) |
| static inherited

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

Definition at line 51 of file rng.cpp.

52 {
53 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
54 return new AutoSeeded_RNG;
55 #else
56 throw Exception("make_rng failed, no AutoSeeded_RNG in this build");
57 #endif
58 }

Module& Botan::PKCS11::PKCS11_RNG::module | ( |  | ) | const
| inline

Returns
  the module used by this RNG

Definition at line 52 of file p11_randomgenerator.h.

Referenced by add_entropy(), and randomize().

53 {
54 return m_session.get().module();
55 }

std::string Botan::PKCS11::PKCS11_RNG::name | ( |  | ) | const
| inline override virtual

Returns
  the name of this RNG type

Implements Botan::RandomNumberGenerator.

Definition at line 34 of file p11_randomgenerator.h.

35 {
36 return "PKCS11_RNG";
37 }

uint8_t Botan::RandomNumberGenerator::next_byte | ( |  | ) |
| inline inherited

Return a random byte

Returns
  random byte

Definition at line 144 of file rng.h.

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), and Botan::random_prime().

145 {
146 uint8_t b;
147 this->randomize(&b, 1);
148 return b;
149 }
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0

uint8_t Botan::RandomNumberGenerator::next_nonzero_byte | ( |  | ) |
| inline inherited

Returns
  a random byte that is greater than zero

Definition at line 154 of file rng.h.

155 {
156 uint8_t b = this->next_byte();
157 while(b == 0)
158 b = this->next_byte();
159 return b;
160 }
Botan::RandomNumberGenerator::next_byte
uint8_t next_byte()
Definition: rng.h:144

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

Return a random vector

Parameters

  bytes | number of bytes in the result

Returns
  randomized vector of length bytes

Definition at line 133 of file rng.h.

Referenced by Botan::CECPQ1_accept(), Botan::CECPQ1_offer(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::PK_Decryptor::decrypt_or_random(), Botan::TLS::Session::encrypt(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::mceies_encrypt(), Botan::OctetString::OctetString(), Botan::BigInt::randomize(), Botan::TLS::Session_Manager_In_Memory::remove_all(), and Botan::TLS::Session_Manager_SQL::Session_Manager_SQL().

134 {
135 secure_vector<uint8_t> output(bytes);
136 this->randomize(output.data(), output.size());
137 return output;
138 }
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0

virtual void Botan::RandomNumberGenerator::randomize | ( | uint8_t | output[],
|  | size_t | length
| ) |  |
| pure virtual inherited

Randomize a byte array.

Parameters

  output | the byte array to hold the random output.
  length | the length of the byte array output in bytes.

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

Referenced by Botan::TLS::Connection_Cipher_State::aead_nonce(), botan_mp_rand_bits(), botan_rng_get(), Botan::CryptoBox::encrypt(), Botan::generate_dsa_primes(), Botan::generate_passhash9(), Botan::TLS::make_hello_random(), Botan::newhope_keygen(), Botan::random_gf2m(), Botan::McEliece_PublicKey::random_plaintext_element(), Botan::System_RNG::randomize(), Botan::RandomNumberGenerator::randomize_with_input(), Botan::RandomNumberGenerator::reseed_from_rng(), Botan::KeyPair::signature_consistency_check(), Botan::RTSS_Share::split(), and Botan::UUID::UUID().

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

Calls `C_GenerateRandom` to generate random data.

Definition at line 19 of file p11_randomgenerator.cpp.

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

20 {
21 module()->C_GenerateRandom(m_session.get().handle(), output, length);
22 }
Botan::PKCS11::PKCS11_RNG::module
Module & module() const
Definition: p11_randomgenerator.h:52
Botan::PKCS11::LowLevel::C_GenerateRandom
bool C_GenerateRandom(SessionHandle session, Byte *random_data_ptr, Ulong random_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:742

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

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.

Parameters

  output | buffer to hold the random output
  output_len | size of the output buffer in bytes
  input | entropy buffer to incorporate
  input_len | size of the input buffer in bytes

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

Definition at line 30 of file rng.cpp.

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

Referenced by Botan::Stateful_RNG::randomize_with_ts_input(), and Botan::RandomNumberGenerator::randomize_with_ts_input().

32 {
33 this->add_entropy(input, input_len);
34 this->randomize(output, output_len);
35 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0

void Botan::RandomNumberGenerator::randomize_with_ts_input | ( | uint8_t | output[],
|  | size_t | output_len
| ) |  |
| virtual inherited

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 17 of file rng.cpp.

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

Referenced by Botan::AutoSeeded_RNG::randomize().

18 {
19 /*
20 Form additional input which is provided to the PRNG implementation
21 to paramaterize the KDF output.
22 */
23 uint8_t additional_input[16] = { 0 };
24 store_le(OS::get_system_timestamp_ns(), additional_input);
25 store_le(OS::get_high_resolution_clock(), additional_input + 8);
26
27 randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
28 }
Botan::RandomNumberGenerator::randomize_with_input
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:30
Botan::OS::get_system_timestamp_ns
uint64_t BOTAN_DLL get_system_timestamp_ns()
Definition: os_utils.cpp:157
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:457
Botan::OS::get_high_resolution_clock
uint64_t BOTAN_DLL get_high_resolution_clock()
Definition: os_utils.cpp:109

size_t Botan::PKCS11::PKCS11_RNG::reseed | ( | Entropy_Sources & | ,
|  | size_t | ,
|  | std::chrono::milliseconds |
| ) |  |
| inline override virtual

No operation - always returns 0.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 46 of file p11_randomgenerator.h.

47 {
48 return 0;
49 }

void Botan::RandomNumberGenerator::reseed_from_rng | ( | RandomNumberGenerator & | rng,
|  | size_t | poll_bits = `BOTAN_RNG_RESEED_POLL_BITS`
| ) |  |
| virtual inherited

Reseed by reading specified bits from the RNG

Reimplemented in Botan::Stateful_RNG.

Definition at line 44 of file rng.cpp.

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

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

45 {
46 secure_vector<uint8_t> buf(poll_bits / 8);
47 rng.randomize(buf.data(), buf.size());
48 this->add_entropy(buf.data(), buf.size());
49 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0

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

- src/lib/prov/pkcs11/p11_randomgenerator.h
- src/lib/prov/pkcs11/p11_randomgenerator.cpp