Use a single set of FDs for all random plugin RNG instances

This commit is contained in:
Martin Willi
2012-03-20 17:31:40 +01:00
parent eff331f799
commit 35852af7b1
3 changed files with 89 additions and 36 deletions
@@ -15,9 +15,24 @@
#include "random_plugin.h" #include "random_plugin.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <library.h> #include <library.h>
#include <debug.h>
#include "random_rng.h" #include "random_rng.h"
#ifndef DEV_RANDOM
# define DEV_RANDOM "/dev/random"
#endif
#ifndef DEV_URANDOM
# define DEV_URANDOM "/dev/urandom"
#endif
typedef struct private_random_plugin_t private_random_plugin_t; typedef struct private_random_plugin_t private_random_plugin_t;
/** /**
@@ -31,6 +46,41 @@ struct private_random_plugin_t {
random_plugin_t public; random_plugin_t public;
}; };
/** /dev/random file descriptor */
static int dev_random = -1;
/** /dev/urandom file descriptor */
static int dev_urandom = -1;
/**
* See header.
*/
int random_plugin_get_dev_random()
{
return dev_random;
}
/**
* See header.
*/
int random_plugin_get_dev_urandom()
{
return dev_urandom;
}
/**
* Open a random device file
*/
static bool open_dev(char *file, int *fd)
{
*fd = open(file, O_RDONLY);
if (*fd == -1)
{
DBG1(DBG_LIB, "opening \"%s\" failed: %s", file, strerror(errno));
return FALSE;
}
return TRUE;
}
METHOD(plugin_t, get_name, char*, METHOD(plugin_t, get_name, char*,
private_random_plugin_t *this) private_random_plugin_t *this)
{ {
@@ -52,6 +102,14 @@ METHOD(plugin_t, get_features, int,
METHOD(plugin_t, destroy, void, METHOD(plugin_t, destroy, void,
private_random_plugin_t *this) private_random_plugin_t *this)
{ {
if (dev_random != -1)
{
close(dev_random);
}
if (dev_urandom != -1)
{
close(dev_urandom);
}
free(this); free(this);
} }
@@ -72,6 +130,13 @@ plugin_t *random_plugin_create()
}, },
); );
if (!open_dev(DEV_URANDOM, &dev_urandom) ||
!open_dev(DEV_RANDOM, &dev_random))
{
destroy(this);
return NULL;
}
return &this->public.plugin; return &this->public.plugin;
} }
@@ -39,4 +39,14 @@ struct random_plugin_t {
plugin_t plugin; plugin_t plugin;
}; };
/**
* Get the /dev/random file descriptor
*/
int random_plugin_get_dev_random();
/**
* Get the /dev/urandom file descriptor
*/
int random_plugin_get_dev_urandom();
#endif /** RANDOM_PLUGIN_H_ @}*/ #endif /** RANDOM_PLUGIN_H_ @}*/
+14 -36
View File
@@ -15,22 +15,12 @@
*/ */
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include "random_rng.h" #include "random_rng.h"
#include "random_plugin.h"
#ifndef DEV_RANDOM
# define DEV_RANDOM "/dev/random"
#endif
#ifndef DEV_URANDOM
# define DEV_URANDOM "/dev/urandom"
#endif
typedef struct private_random_rng_t private_random_rng_t; typedef struct private_random_rng_t private_random_rng_t;
@@ -47,12 +37,7 @@ struct private_random_rng_t {
/** /**
* random device, depends on quality * random device, depends on quality
*/ */
int dev; int fd;
/**
* file we read random bytes from
*/
char *file;
}; };
METHOD(rng_t, get_bytes, void, METHOD(rng_t, get_bytes, void,
@@ -65,14 +50,12 @@ METHOD(rng_t, get_bytes, void,
while (done < bytes) while (done < bytes)
{ {
got = read(this->dev, buffer + done, bytes - done); got = read(this->fd, buffer + done, bytes - done);
if (got <= 0) if (got <= 0)
{ {
DBG1(DBG_LIB, "reading from \"%s\" failed: %s, retrying...", DBG1(DBG_LIB, "reading from random FD %d failed: %s, retrying...",
this->file, strerror(errno)); this->fd, strerror(errno));
close(this->dev);
sleep(1); sleep(1);
this->dev = open(this->file, 0);
} }
done += got; done += got;
} }
@@ -88,7 +71,6 @@ METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, destroy, void, METHOD(rng_t, destroy, void,
private_random_rng_t *this) private_random_rng_t *this)
{ {
close(this->dev);
free(this); free(this);
} }
@@ -109,22 +91,18 @@ random_rng_t *random_rng_create(rng_quality_t quality)
}, },
); );
if (quality == RNG_TRUE) switch (quality)
{ {
this->file = DEV_RANDOM; case RNG_TRUE:
} this->fd = random_plugin_get_dev_random();
else break;
{ case RNG_STRONG:
this->file = DEV_URANDOM; case RNG_WEAK:
default:
this->fd = random_plugin_get_dev_urandom();
break;
} }
this->dev = open(this->file, 0);
if (this->dev < 0)
{
DBG1(DBG_LIB, "opening \"%s\" failed: %s", this->file, strerror(errno));
free(this);
return NULL;
}
return &this->public; return &this->public;
} }