Botan  2.19.1
Crypto and TLS for C++11
getentropy.cpp
Go to the documentation of this file.
1 /*
2 * System Call getentropy(2)
3 * (C) 2017 Alexander Bluhm (genua GmbH)
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/getentropy.h>
9 
10 #if defined(BOTAN_TARGET_OS_IS_OPENBSD) || defined(BOTAN_TARGET_OS_IS_FREEBSD)
11  #include <unistd.h>
12 #else
13  #if defined(BOTAN_TARGET_OS_HAS_POSIX1)
14  // Allows successful compilation on macOS older than 10.12: Provides a missing typedef for `u_int`.
15  #include <sys/types.h>
16  #endif
17  #include <sys/random.h>
18 #endif
19 
20 namespace Botan {
21 
22 /**
23 * Gather 256 bytes entropy from getentropy(2). Note that maximum
24 * buffer size is limited to 256 bytes. On OpenBSD this does neither
25 * block nor fail.
26 */
28  {
29  secure_vector<uint8_t> buf(256);
30 
31  if(::getentropy(buf.data(), buf.size()) == 0)
32  {
33  rng.add_entropy(buf.data(), buf.size());
34  return buf.size() * 8;
35  }
36 
37  return 0;
38  }
39 }
virtual void add_entropy(const uint8_t input[], size_t length)=0
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Definition: alg_id.cpp:13
size_t poll(RandomNumberGenerator &rng) override
Definition: getentropy.cpp:27