Commit 263ecdde authored by Christian Kellner's avatar Christian Kellner
Browse files

common: support for getrandom(2) as RNG

parent 7d7740bb
......@@ -23,26 +23,61 @@
#include "bolt-io.h"
#include "bolt-rnd.h"
#include <gio/gio.h>
#include <errno.h>
#include <string.h>
#if HAVE_FN_GETRANDOM
#include <sys/random.h>
# else
# define GRND_NONBLOCK 0
#endif
int
bolt_get_random_data (void *buf, gsize n)
{
gboolean ok;
BoltRng method = BOLT_RNG_URANDOM;
ok = bolt_random_getrandom (buf, n, GRND_NONBLOCK, NULL);
if (ok)
return BOLT_RNG_GETRANDOM;
ok = bolt_random_urandom (buf, n);
if (!ok)
if (ok)
return BOLT_RNG_URANDOM;
bolt_random_prng (buf, n);
return BOLT_RNG_PRNG;
}
/* specific implementations */
gboolean
bolt_random_getrandom (void *buf,
gsize n,
unsigned flags,
GError **error)
{
int r = -1;
#if HAVE_FN_GETRANDOM
r = getrandom (buf, n, flags);
#else
errno = ENOSYS;
#endif
if (r < 0)
{
method = BOLT_RNG_PRNG;
bolt_random_prng (buf, n);
g_set_error (error, G_IO_ERROR,
g_io_error_from_errno (errno),
"failed to get random data: %s",
g_strerror (errno));
return FALSE;
}
return method;
return TRUE;
}
/* specific implementations */
gboolean
bolt_random_urandom (void *buf, gsize n)
{
......
......@@ -28,13 +28,18 @@ G_BEGIN_DECLS
typedef enum {
BOLT_RNG_ERROR = -1,
BOLT_RNG_URANDOM = 1,
BOLT_RNG_PRNG = 2
BOLT_RNG_PRNG = 2,
BOLT_RNG_GETRANDOM = 3,
} BoltRng;
BoltRng bolt_get_random_data (void *buf,
gsize n);
/* specific implementations */
gboolean bolt_random_getrandom (void *buf,
gsize n,
unsigned flags,
GError **error);
gboolean bolt_random_urandom (void *buf,
gsize n);
void bolt_random_prng (void *buf,
......
......@@ -95,7 +95,8 @@ conf.set_quoted('BOLT_DBDIR', dbdir)
conf.set('_GNU_SOURCE', true)
foreach fn : [
['explicit_bzero' , '''#include <string.h>''']
['explicit_bzero', '''#include <string.h>'''],
['getrandom', '''#include <sys/random.h>''']
]
have = compiler.has_function(fn[0], prefix : fn[1])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment