2004-12-12 22:58:53 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-12-12 22:58:53 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-12-12 22:58:53 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-12 22:58:53 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2004-12-12 22:58:53 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-12-12 22:58:53 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-12-12 22:58:53 +00:00
|
|
|
#include "random.h"
|
|
|
|
|
|
2006-04-18 15:16:24 +00:00
|
|
|
static int has_whined = 0;
|
|
|
|
|
|
|
|
|
|
static const char *devices[] = { "/dev/urandom", "/dev/random", NULL };
|
|
|
|
|
|
2006-04-18 15:40:36 +00:00
|
|
|
static int random_proper(void *ret_data, size_t length) {
|
2006-04-18 15:16:24 +00:00
|
|
|
#ifdef OS_IS_WIN32
|
2006-04-18 16:33:17 +00:00
|
|
|
assert(ret_data && length);
|
2004-12-12 22:58:53 +00:00
|
|
|
|
2006-04-18 15:16:24 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
#else /* OS_IS_WIN32 */
|
|
|
|
|
|
2006-05-24 13:23:15 +00:00
|
|
|
int fd, ret = -1;
|
2006-04-18 15:16:24 +00:00
|
|
|
ssize_t r = 0;
|
|
|
|
|
const char **device;
|
2004-12-12 22:58:53 +00:00
|
|
|
|
2006-04-18 16:33:17 +00:00
|
|
|
assert(ret_data && length);
|
|
|
|
|
|
2006-04-18 15:16:24 +00:00
|
|
|
device = devices;
|
|
|
|
|
|
|
|
|
|
while (*device) {
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
if ((fd = open(*device, O_RDONLY)) >= 0) {
|
|
|
|
|
|
2006-07-14 22:42:01 +00:00
|
|
|
if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
|
2006-04-18 15:16:24 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
} else
|
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
break;
|
2004-12-12 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-04-18 15:16:24 +00:00
|
|
|
return ret;
|
|
|
|
|
#endif /* OS_IS_WIN32 */
|
|
|
|
|
}
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2006-04-18 15:40:36 +00:00
|
|
|
void pa_random_seed(void) {
|
2006-04-18 15:16:24 +00:00
|
|
|
unsigned int seed;
|
|
|
|
|
|
2006-04-18 15:40:36 +00:00
|
|
|
if (random_proper(&seed, sizeof(unsigned int)) < 0) {
|
2006-04-18 15:16:24 +00:00
|
|
|
if (!has_whined)
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("failed to get proper entropy. Falling back to seeding with current time.");
|
2006-04-18 15:16:24 +00:00
|
|
|
has_whined = 1;
|
2004-12-12 22:58:53 +00:00
|
|
|
|
2006-04-18 15:16:24 +00:00
|
|
|
seed = (unsigned int) time(NULL);
|
2004-12-12 22:58:53 +00:00
|
|
|
}
|
2006-04-18 15:16:24 +00:00
|
|
|
|
|
|
|
|
srand(seed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_random(void *ret_data, size_t length) {
|
|
|
|
|
uint8_t *p;
|
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
|
|
assert(ret_data && length);
|
|
|
|
|
|
2006-04-18 15:40:36 +00:00
|
|
|
if (random_proper(ret_data, length) >= 0)
|
2006-04-18 15:16:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!has_whined)
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("failed to get proper entropy. Falling back to unsecure pseudo RNG.");
|
2006-04-18 15:16:24 +00:00
|
|
|
has_whined = 1;
|
|
|
|
|
|
|
|
|
|
for (p = ret_data, l = length; l > 0; p++, l--)
|
|
|
|
|
*p = (uint8_t) rand();
|
2004-12-12 22:58:53 +00:00
|
|
|
}
|