aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/entropy/dev_random/dev_random.cpp
blob: f37831d2e3b4637656ed0f94b58c2a78ad6d9d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Reader of /dev/random and company
* (C) 1999-2009,2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/dev_random.h>
#include <botan/exceptn.h>

#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

namespace Botan {

/**
Device_EntropySource constructor
Open a file descriptor to each (available) device in fsnames
*/
Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnames)
   {
#ifndef O_NONBLOCK
  #define O_NONBLOCK 0
#endif

#ifndef O_NOCTTY
  #define O_NOCTTY 0
#endif

   const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;

   m_max_fd = 0;

   for(auto fsname : fsnames)
      {
      int fd = ::open(fsname.c_str(), flags);

      if(fd < 0)
         {
         /*
         ENOENT or EACCES is normal as some of the named devices may not exist
         on this system. But any other errno value probably indicates
         either a bug in the application or file descriptor exhaustion.
         */
         if(errno != ENOENT && errno != EACCES)
            throw Exception("Opening OS RNG device failed with errno " +
                            std::to_string(errno));
         }
      else
         {
         if(fd > FD_SETSIZE)
            {
            ::close(fd);
            throw Exception("Open of OS RNG succeeded but fd is too large for fd_set");
            }

         m_dev_fds.push_back(fd);
         m_max_fd = std::max(m_max_fd, fd);
         }
      }
   }

/**
Device_EntropySource destructor: close all open devices
*/
Device_EntropySource::~Device_EntropySource()
   {
   for(int fd : m_dev_fds)
      {
      // ignoring return value here, can't throw in destructor anyway
      ::close(fd);
      }
   }

/**
* Gather entropy from a RNG device
*/
size_t Device_EntropySource::poll(RandomNumberGenerator& rng)
   {
   size_t bits = 0;

   if(m_dev_fds.size() > 0)
      {
      fd_set read_set;
      FD_ZERO(&read_set);

      for(int dev_fd : m_dev_fds)
         {
         FD_SET(dev_fd, &read_set);
         }

      secure_vector<uint8_t> io_buf(BOTAN_SYSTEM_RNG_POLL_REQUEST);

      struct ::timeval timeout;
      timeout.tv_sec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS / 1000);
      timeout.tv_usec = (BOTAN_SYSTEM_RNG_POLL_TIMEOUT_MS % 1000) * 1000;

      if(::select(m_max_fd + 1, &read_set, nullptr, nullptr, &timeout) > 0)
         {
         for(int dev_fd : m_dev_fds)
            {
            if(FD_ISSET(dev_fd, &read_set))
               {
               const ssize_t got = ::read(dev_fd, io_buf.data(), io_buf.size());

               if(got > 0)
                  {
                  rng.add_entropy(io_buf.data(), static_cast<size_t>(got));
                  bits += got * 8;
                  }
               }
            }
         }
      }

   return bits;
   }

}