Botan  2.11.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::Device_EntropySource Class Referencefinal

#include <dev_random.h>

Inheritance diagram for Botan::Device_EntropySource:
Botan::Entropy_Source

Public Member Functions

 Device_EntropySource (const std::vector< std::string > &fsnames)
 
std::string name () const override
 
size_t poll (RandomNumberGenerator &rng) override
 
 ~Device_EntropySource ()
 

Static Public Member Functions

static std::unique_ptr< Entropy_Sourcecreate (const std::string &type)
 

Detailed Description

Entropy source reading from kernel devices like /dev/random

Definition at line 20 of file dev_random.h.

Constructor & Destructor Documentation

◆ Device_EntropySource()

Botan::Device_EntropySource::Device_EntropySource ( const std::vector< std::string > &  fsnames)
explicit

Device_EntropySource constructor Open a file descriptor to each (available) device in fsnames

Definition at line 24 of file dev_random.cpp.

References Botan::PKCS11::flags(), O_NOCTTY, and O_NONBLOCK.

25  {
26 #ifndef O_NONBLOCK
27  #define O_NONBLOCK 0
28 #endif
29 
30 #ifndef O_NOCTTY
31  #define O_NOCTTY 0
32 #endif
33 
34  const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;
35 
36  m_max_fd = 0;
37 
38  for(auto fsname : fsnames)
39  {
40  int fd = ::open(fsname.c_str(), flags);
41 
42  if(fd < 0)
43  {
44  /*
45  ENOENT or EACCES is normal as some of the named devices may not exist
46  on this system. But any other errno value probably indicates
47  either a bug in the application or file descriptor exhaustion.
48  */
49  if(errno != ENOENT && errno != EACCES)
50  throw System_Error("Opening OS RNG device failed", errno);
51  }
52  else
53  {
54  if(fd > FD_SETSIZE)
55  {
56  ::close(fd);
57  throw Invalid_State("Open of OS RNG succeeded but returned fd is too large for fd_set");
58  }
59 
60  m_dev_fds.push_back(fd);
61  m_max_fd = std::max(m_max_fd, fd);
62  }
63  }
64  }
Flags flags(Flag flags)
Definition: p11.h:858
#define O_NONBLOCK
#define O_NOCTTY

◆ ~Device_EntropySource()

Botan::Device_EntropySource::~Device_EntropySource ( )

Device_EntropySource destructor: close all open devices

Definition at line 69 of file dev_random.cpp.

70  {
71  for(int fd : m_dev_fds)
72  {
73  // ignoring return value here, can't throw in destructor anyway
74  ::close(fd);
75  }
76  }

Member Function Documentation

◆ create()

std::unique_ptr< Entropy_Source > Botan::Entropy_Source::create ( const std::string &  type)
staticinherited

Return a new entropy source of a particular type, or null Each entropy source may require substantial resources (eg, a file handle or socket instance), so try to share them among multiple RNGs, or just use the preconfigured global list accessed by Entropy_Sources::global_sources()

Definition at line 63 of file entropy_srcs.cpp.

References BOTAN_UNUSED, Botan::Entropy_Source::name(), and Botan::OS::running_in_privileged_state().

Referenced by Botan::Entropy_Sources::Entropy_Sources().

64  {
65 #if defined(BOTAN_HAS_SYSTEM_RNG)
66  if(name == "system_rng" || name == "win32_cryptoapi")
67  {
68  return std::unique_ptr<Entropy_Source>(new System_RNG_EntropySource);
69  }
70 #endif
71 
72 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
73  if(name == "rdrand")
74  {
75  return std::unique_ptr<Entropy_Source>(new Intel_Rdrand);
76  }
77 #endif
78 
79 #if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
80  if(name == "rdseed")
81  {
82  return std::unique_ptr<Entropy_Source>(new Intel_Rdseed);
83  }
84 #endif
85 
86 #if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
87  if(name == "getentropy")
88  {
89  return std::unique_ptr<Entropy_Source>(new Getentropy);
90  }
91 #endif
92 
93 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
94  if(name == "dev_random")
95  {
96  return std::unique_ptr<Entropy_Source>(new Device_EntropySource(BOTAN_SYSTEM_RNG_POLL_DEVICES));
97  }
98 #endif
99 
100 #if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
101  if(name == "proc_walk" && OS::running_in_privileged_state() == false)
102  {
103  const std::string root_dir = BOTAN_ENTROPY_PROC_FS_PATH;
104  if(!root_dir.empty())
105  return std::unique_ptr<Entropy_Source>(new ProcWalking_EntropySource(root_dir));
106  }
107 #endif
108 
109 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
110  if(name == "system_stats")
111  {
112  return std::unique_ptr<Entropy_Source>(new Win32_EntropySource);
113  }
114 #endif
115 
117  return std::unique_ptr<Entropy_Source>();
118  }
virtual std::string name() const =0
bool running_in_privileged_state()
Definition: os_utils.cpp:135
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ name()

std::string Botan::Device_EntropySource::name ( ) const
inlineoverridevirtual
Returns
name identifying this entropy source

Implements Botan::Entropy_Source.

Definition at line 23 of file dev_random.h.

23 { return "dev_random"; }

◆ poll()

size_t Botan::Device_EntropySource::poll ( RandomNumberGenerator rng)
overridevirtual

Gather entropy from a RNG device

Implements Botan::Entropy_Source.

Definition at line 81 of file dev_random.cpp.

References Botan::RandomNumberGenerator::add_entropy().

82  {
83  size_t bits = 0;
84 
85  if(m_dev_fds.size() > 0)
86  {
87  fd_set read_set;
88  FD_ZERO(&read_set);
89 
90  for(int dev_fd : m_dev_fds)
91  {
92  FD_SET(dev_fd, &read_set);
93  }
94 
95  secure_vector<uint8_t> io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);
96 
97  struct ::timeval timeout;
98  timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000);
99  timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000;
100 
101  if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0)
102  {
103  for(int dev_fd : m_dev_fds)
104  {
105  if(FD_ISSET(dev_fd, &read_set))
106  {
107  const ssize_t got = ::read(dev_fd, io_buf.data(), io_buf.size());
108 
109  if(got > 0)
110  {
111  rng.add_entropy(io_buf.data(), static_cast<size_t>(got));
112  bits += got * 8;
113  }
114  }
115  }
116  }
117  }
118 
119  return bits;
120  }

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