util: add UTI_GetRandomBytesUrandom()

This function always uses /dev/urandom, even if arc4random() is
available, and is intended for generating long-term keys.
This commit is contained in:
Miroslav Lichvar
2016-01-13 11:57:36 +01:00
parent 0d12410eaa
commit 32ac6ffa26
2 changed files with 19 additions and 5 deletions

17
util.c
View File

@@ -1141,17 +1141,26 @@ UTI_DropRoot(uid_t uid, gid_t gid)
#define DEV_URANDOM "/dev/urandom"
void
UTI_GetRandomBytes(void *buf, unsigned int len)
UTI_GetRandomBytesUrandom(void *buf, unsigned int len)
{
#ifdef HAVE_ARC4RANDOM
arc4random_buf(buf, len);
#else
static FILE *f = NULL;
if (!f)
f = fopen(DEV_URANDOM, "r");
if (!f)
LOG_FATAL(LOGF_Util, "Can't open %s : %s", DEV_URANDOM, strerror(errno));
if (fread(buf, 1, len, f) != len)
LOG_FATAL(LOGF_Util, "Can't read from %s", DEV_URANDOM);
}
/* ================================================== */
void
UTI_GetRandomBytes(void *buf, unsigned int len)
{
#ifdef HAVE_ARC4RANDOM
arc4random_buf(buf, len);
#else
UTI_GetRandomBytesUrandom(buf, len);
#endif
}