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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <windows.h>
#include <wincrypt.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
#include "warnp.h"
#include "entropy.h"
/**
* XXX Portability
* XXX We obtain random bytes from the operating system by opening
* XXX /dev/urandom and reading them from that device; this works on
* XXX modern UNIX-like operating systems but not on systems like
* XXX win32 where there is no concept of /dev/urandom.
*/
/**
* Entropy reader state. This structure holds OS-dependent state:
* - On Unix: a file descriptor for /dev/urandom
* - On Windows: a cryptographic provider handle
*/
struct entropy_read_cookie {
#ifdef _MSC_VER
HCRYPTPROV hCryptProv;
#else
int fd;
#endif
};
/**
* entropy_read_init(void):
* Initialize the ability to produce random bytes from the operating system,
* and return a cookie.
*/
struct entropy_read_cookie *
entropy_read_init(void)
{
struct entropy_read_cookie * er;
/* Allocate cookie. */
if ((er = malloc(sizeof(struct entropy_read_cookie))) == NULL) {
warnp("malloc");
goto err0;
}
#ifdef _MSC_VER
/* Acquire a cryptographic provider context handle. */
if (!CryptAcquireContext(&er->hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
warnp("CryptAcquireContext");
goto err1;
}
#else
/* Open /dev/urandom. */
if ((er->fd = open("/dev/urandom", O_RDONLY)) == -1) {
warnp("open(/dev/urandom)");
goto err1;
}
#endif
/* Success! */
return (er);
err1:
free(er);
err0:
/* Failure! */
return (NULL);
}
/**
* entropy_read_fill(er, buf, buflen):
* Fill the given buffer with random bytes provided by the operating system
* using the resources in ${er}.
*/
int
entropy_read_fill(struct entropy_read_cookie * er, uint8_t * buf,
size_t buflen)
{
/* Sanity checks. */
assert(er != NULL);
assert(buflen <= SSIZE_MAX);
#ifdef _MSC_VER
/* Use Windows CryptoAPI to generate random bytes */
if (!CryptGenRandom(er->hCryptProv, (DWORD)buflen, buf)) {
warnp("CryptGenRandom");
goto err0;
}
#else
ssize_t lenread;
/* Read bytes until we have filled the buffer. */
while (buflen > 0) {
if ((lenread = read(er->fd, buf, buflen)) == -1) {
warnp("read(/dev/urandom)");
goto err0;
}
/* The random device should never EOF. */
if (lenread == 0) {
warn0("EOF on /dev/urandom?");
goto err0;
}
/* We've filled a portion of the buffer. */
buf += (size_t)lenread;
buflen -= (size_t)lenread;
}
#endif
/* Success! */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* entropy_read_done(er):
* Release any resources used by ${er}.
*/
int
entropy_read_done(struct entropy_read_cookie * er)
{
/* Sanity check. */
assert(er != NULL);
#ifdef _MSC_VER
/* Release the cryptographic provider context */
if (!CryptReleaseContext(er->hCryptProv, 0)) {
warnp("CryptReleaseContext");
goto err1;
}
#else
/* Close the device. */
while (close(er->fd) == -1) {
if (errno != EINTR) {
warnp("close(/dev/urandom)");
goto err1;
}
}
#endif
/* Clean up. */
free(er);
/* Success! */
return (0);
err1:
free(er);
/* Failure! */
return (-1);
}
/**
* entropy_read(buf, buflen):
* Fill the given buffer with random bytes provided by the operating system.
*/
int
entropy_read(uint8_t * buf, size_t buflen)
{
struct entropy_read_cookie * er;
/* Sanity-check the buffer size. */
assert(buflen <= SSIZE_MAX);
/* Open /dev/urandom. */
if ((er = entropy_read_init()) == NULL) {
warn0("entropy_read_init");
goto err0;
}
/* Read bytes until we have filled the buffer. */
if (entropy_read_fill(er, buf, buflen)) {
warn0("entropy_read_fill");
goto err1;
}
/* Close the device. */
if (entropy_read_done(er)) {
warn0("entropy_read_done");
goto err0;
}
/* Success! */
return (0);
err1:
entropy_read_done(er);
err0:
/* Failure! */
return (-1);
}
|