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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <library.h>
#include <debug.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;
/**
@@ -31,6 +46,41 @@ struct private_random_plugin_t {
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*,
private_random_plugin_t *this)
{
@@ -52,6 +102,14 @@ METHOD(plugin_t, get_features, int,
METHOD(plugin_t, destroy, void,
private_random_plugin_t *this)
{
if (dev_random != -1)
{
close(dev_random);
}
if (dev_urandom != -1)
{
close(dev_urandom);
}
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;
}
@@ -39,4 +39,14 @@ struct random_plugin_t {
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_ @}*/
+14 -36
View File
@@ -15,22 +15,12 @@
*/
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
#include "random_rng.h"
#ifndef DEV_RANDOM
# define DEV_RANDOM "/dev/random"
#endif
#ifndef DEV_URANDOM
# define DEV_URANDOM "/dev/urandom"
#endif
#include "random_plugin.h"
typedef struct private_random_rng_t private_random_rng_t;
@@ -47,12 +37,7 @@ struct private_random_rng_t {
/**
* random device, depends on quality
*/
int dev;
/**
* file we read random bytes from
*/
char *file;
int fd;
};
METHOD(rng_t, get_bytes, void,
@@ -65,14 +50,12 @@ METHOD(rng_t, get_bytes, void,
while (done < bytes)
{
got = read(this->dev, buffer + done, bytes - done);
got = read(this->fd, buffer + done, bytes - done);
if (got <= 0)
{
DBG1(DBG_LIB, "reading from \"%s\" failed: %s, retrying...",
this->file, strerror(errno));
close(this->dev);
DBG1(DBG_LIB, "reading from random FD %d failed: %s, retrying...",
this->fd, strerror(errno));
sleep(1);
this->dev = open(this->file, 0);
}
done += got;
}
@@ -88,7 +71,6 @@ METHOD(rng_t, allocate_bytes, void,
METHOD(rng_t, destroy, void,
private_random_rng_t *this)
{
close(this->dev);
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;
}
else
{
this->file = DEV_URANDOM;
case RNG_TRUE:
this->fd = random_plugin_get_dev_random();
break;
case RNG_STRONG:
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;
}