Botan 3.13.0
Crypto and TLS for C&
p11_randomgenerator.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 Random Generator
3* (C) 2016 Daniel Neus, Sirrix AG
4* (C) 2016 Philipp Weber, Sirrix AG
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/p11_randomgenerator.h>
10
11#include <algorithm>
12#include <limits>
13
14namespace Botan::PKCS11 {
15
16PKCS11_RNG::PKCS11_RNG(Session& session) : m_session(session) {}
17
18size_t PKCS11_RNG::reseed_from_sources(Entropy_Sources& /*srcs*/, size_t /*bits*/) {
19 return 0;
20}
21
22void PKCS11_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) {
23 // Chunk by std::numeric_limits<Ulong>::max() so requests larger than the
24 // platform's CK_ULONG (32 bits on LLP64) are not silently truncated.
25 const size_t chunk_max = std::numeric_limits<Ulong>::max();
26
27 while(!input.empty()) {
28 const size_t this_chunk = std::min(input.size(), chunk_max);
30 m_session.get().handle(), const_cast<uint8_t*>(input.data()), static_cast<Ulong>(this_chunk));
31 input = input.subspan(this_chunk);
32 }
33
34 while(!output.empty()) {
35 const size_t this_chunk = std::min(output.size(), chunk_max);
36 module()->C_GenerateRandom(m_session.get().handle(), output.data(), static_cast<Ulong>(this_chunk));
37 output = output.subspan(this_chunk);
38 }
39}
40
41} // namespace Botan::PKCS11
bool C_GenerateRandom(SessionHandle session, Byte *random_data_ptr, Ulong random_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:1164
bool C_SeedRandom(SessionHandle session, const Byte *seed_ptr, Ulong seed_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:1156
PKCS11_RNG(Session &session)
Initialize the RNG with the PKCS#11 session that provides access to the cryptoki functions.
size_t reseed_from_sources(Entropy_Sources &, size_t) override
No operation - always returns 0.
Represents a PKCS#11 session.
Definition p11_types.h:125
CK_ULONG Ulong
Definition p11.h:1204