Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/rng/system_rng/system_rng.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* System RNG
3
* (C) 2014,2015,2017,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/system_rng.h>
9
10
#if defined(BOTAN_TARGET_OS_HAS_RTLGENRANDOM)
11
  #include <botan/dyn_load.h>
12
  #define NOMINMAX 1
13
  #define _WINSOCKAPI_ // stop windows.h including winsock.h
14
  #include <windows.h>
15
16
#elif defined(BOTAN_TARGET_OS_HAS_CRYPTO_NG)
17
  #include <bcrypt.h>
18
19
#elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM)
20
  #include <stdlib.h>
21
22
#elif defined(BOTAN_TARGET_OS_HAS_GETRANDOM)
23
  #include <sys/random.h>
24
  #include <errno.h>
25
26
#elif defined(BOTAN_TARGET_OS_HAS_DEV_RANDOM)
27
  #include <sys/types.h>
28
  #include <sys/stat.h>
29
  #include <fcntl.h>
30
  #include <unistd.h>
31
  #include <errno.h>
32
#endif
33
34
namespace Botan {
35
36
namespace {
37
38
#if defined(BOTAN_TARGET_OS_HAS_RTLGENRANDOM)
39
40
class System_RNG_Impl final : public RandomNumberGenerator
41
   {
42
   public:
43
      System_RNG_Impl() : m_advapi("advapi32.dll")
44
         {
45
         // This throws if the function is not found
46
         m_rtlgenrandom = m_advapi.resolve<RtlGenRandom_fptr>("SystemFunction036");
47
         }
48
49
      void randomize(uint8_t buf[], size_t len) override
50
         {
51
         bool success = m_rtlgenrandom(buf, ULONG(len)) == TRUE;
52
         if(!success)
53
            throw System_Error("RtlGenRandom failed");
54
         }
55
56
      void add_entropy(const uint8_t[], size_t) override { /* ignored */ }
57
      bool is_seeded() const override { return true; }
58
      bool accepts_input() const override { return false; }
59
      void clear() override { /* not possible */ }
60
      std::string name() const override { return "RtlGenRandom"; }
61
   private:
62
      // Use type BYTE instead of BOOLEAN because of a naming conflict
63
      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694(v=vs.85).aspx
64
      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
65
      using RtlGenRandom_fptr = BYTE (NTAPI *)(PVOID, ULONG);
66
67
      Dynamically_Loaded_Library m_advapi;
68
      RtlGenRandom_fptr m_rtlgenrandom;
69
   };
70
71
#elif defined(BOTAN_TARGET_OS_HAS_CRYPTO_NG)
72
73
class System_RNG_Impl final : public RandomNumberGenerator
74
   {
75
   public:
76
      System_RNG_Impl()
77
         {
78
         NTSTATUS ret = ::BCryptOpenAlgorithmProvider(&m_prov,
79
                                                      BCRYPT_RNG_ALGORITHM,
80
                                                      MS_PRIMITIVE_PROVIDER, 0);
81
         if(ret != STATUS_SUCCESS)
82
            throw System_Error("System_RNG failed to acquire crypto provider", ret);
83
         }
84
85
      ~System_RNG_Impl()
86
         {
87
         ::BCryptCloseAlgorithmProvider(m_prov, 0);
88
         }
89
90
      void randomize(uint8_t buf[], size_t len) override
91
         {
92
         NTSTATUS ret = ::BCryptGenRandom(m_prov, static_cast<PUCHAR>(buf), static_cast<ULONG>(len), 0);
93
         if(ret != STATUS_SUCCESS)
94
            throw System_Error("System_RNG call to BCryptGenRandom failed", ret);
95
         }
96
97
      void add_entropy(const uint8_t in[], size_t length) override
98
         {
99
         /*
100
         There is a flag BCRYPT_RNG_USE_ENTROPY_IN_BUFFER to provide
101
         entropy inputs, but it is ignored in Windows 8 and later.
102
         */
103
         }
104
105
      bool is_seeded() const override { return true; }
106
      bool accepts_input() const override { return false; }
107
      void clear() override { /* not possible */ }
108
      std::string name() const override { return "crypto_ng"; }
109
   private:
110
      BCRYPT_ALG_HANDLE m_prov;
111
   };
112
113
#elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM)
114
115
class System_RNG_Impl final : public RandomNumberGenerator
116
   {
117
   public:
118
      // No constructor or destructor needed as no userland state maintained
119
120
      void randomize(uint8_t buf[], size_t len) override
121
         {
122
         ::arc4random_buf(buf, len);
123
         }
124
125
      bool accepts_input() const override { return false; }
126
      void add_entropy(const uint8_t[], size_t) override { /* ignored */ }
127
      bool is_seeded() const override { return true; }
128
      void clear() override { /* not possible */ }
129
      std::string name() const override { return "arc4random"; }
130
   };
131
132
#elif defined(BOTAN_TARGET_OS_HAS_GETRANDOM)
133
134
class System_RNG_Impl final : public RandomNumberGenerator
135
   {
136
   public:
137
      // No constructor or destructor needed as no userland state maintained
138
139
      void randomize(uint8_t buf[], size_t len) override
140
         {
141
         const unsigned int flags = 0;
142
143
         while(len > 0)
144
            {
145
            const ssize_t got = ::getrandom(buf, len, flags);
146
147
            if(got < 0)
148
               {
149
               if(errno == EINTR)
150
                  continue;
151
               throw System_Error("System_RNG getrandom failed", errno);
152
               }
153
154
            buf += got;
155
            len -= got;
156
            }
157
         }
158
159
      bool accepts_input() const override { return false; }
160
      void add_entropy(const uint8_t[], size_t) override { /* ignored */ }
161
      bool is_seeded() const override { return true; }
162
      void clear() override { /* not possible */ }
163
      std::string name() const override { return "getrandom"; }
164
   };
165
166
167
#elif defined(BOTAN_TARGET_OS_HAS_DEV_RANDOM)
168
169
// Read a random device
170
171
class System_RNG_Impl final : public RandomNumberGenerator
172
   {
173
   public:
174
      System_RNG_Impl()
175
0
         {
176
#ifndef O_NOCTTY
177
#define O_NOCTTY 0
178
#endif
179
180
0
         m_fd = ::open(BOTAN_SYSTEM_RNG_DEVICE, O_RDWR | O_NOCTTY);
181
0
182
0
         if(m_fd >= 0)
183
0
            {
184
0
            m_writable = true;
185
0
            }
186
0
         else
187
0
            {
188
0
            /*
189
0
            Cannot open in read-write mode. Fall back to read-only,
190
0
            calls to add_entropy will fail, but randomize will work
191
0
            */
192
0
            m_fd = ::open(BOTAN_SYSTEM_RNG_DEVICE, O_RDONLY | O_NOCTTY);
193
0
            m_writable = false;
194
0
            }
195
0
196
0
         if(m_fd < 0)
197
0
            throw System_Error("System_RNG failed to open RNG device", errno);
198
0
         }
199
200
      ~System_RNG_Impl()
201
0
         {
202
0
         ::close(m_fd);
203
0
         m_fd = -1;
204
0
         }
205
206
      void randomize(uint8_t buf[], size_t len) override;
207
      void add_entropy(const uint8_t in[], size_t length) override;
208
0
      bool is_seeded() const override { return true; }
209
0
      bool accepts_input() const override { return m_writable; }
210
0
      void clear() override { /* not possible */ }
211
0
      std::string name() const override { return BOTAN_SYSTEM_RNG_DEVICE; }
212
   private:
213
      int m_fd;
214
      bool m_writable;
215
   };
216
217
void System_RNG_Impl::randomize(uint8_t buf[], size_t len)
218
0
   {
219
0
   while(len)
220
0
      {
221
0
      ssize_t got = ::read(m_fd, buf, len);
222
0
223
0
      if(got < 0)
224
0
         {
225
0
         if(errno == EINTR)
226
0
            continue;
227
0
         throw System_Error("System_RNG read failed", errno);
228
0
         }
229
0
      if(got == 0)
230
0
         throw System_Error("System_RNG EOF on device"); // ?!?
231
0
232
0
      buf += got;
233
0
      len -= got;
234
0
      }
235
0
   }
236
237
void System_RNG_Impl::add_entropy(const uint8_t input[], size_t len)
238
0
   {
239
0
   if(!m_writable)
240
0
      return;
241
0
242
0
   while(len)
243
0
      {
244
0
      ssize_t got = ::write(m_fd, input, len);
245
0
246
0
      if(got < 0)
247
0
         {
248
0
         if(errno == EINTR)
249
0
            continue;
250
0
251
0
         /*
252
0
         * This is seen on OS X CI, despite the fact that the man page
253
0
         * for macOS urandom explicitly states that writing to it is
254
0
         * supported, and write(2) does not document EPERM at all.
255
0
         * But in any case EPERM seems indicative of a policy decision
256
0
         * by the OS or sysadmin that additional entropy is not wanted
257
0
         * in the system pool, so we accept that and return here,
258
0
         * since there is no corrective action possible.
259
0
         *
260
0
         * In Linux EBADF or EPERM is returned if m_fd is not opened for
261
0
         * writing.
262
0
         */
263
0
         if(errno == EPERM || errno == EBADF)
264
0
            return;
265
0
266
0
         // maybe just ignore any failure here and return?
267
0
         throw System_Error("System_RNG write failed", errno);
268
0
         }
269
0
270
0
      input += got;
271
0
      len -= got;
272
0
      }
273
0
   }
274
275
#endif
276
277
}
278
279
RandomNumberGenerator& system_rng()
280
0
   {
281
0
   static System_RNG_Impl g_system_rng;
282
0
   return g_system_rng;
283
0
   }
284
285
}