2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
40 static pa_bool_t has_whined = TRUE;
42 static const char * const devices[] = { "/dev/urandom", "/dev/random", NULL };
44 static int random_proper(void *ret_data, size_t length) {
47 pa_assert(length > 0);
51 #else /* OS_IS_WIN32 */
55 const char *const * device;
58 pa_assert(length > 0);
65 if ((fd = pa_open_cloexec(*device, O_RDONLY, 0)) >= 0) {
67 if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
81 #endif /* OS_IS_WIN32 */
84 void pa_random_seed(void) {
87 if (random_proper(&seed, sizeof(unsigned int)) < 0) {
90 pa_log_warn("Failed to get proper entropy. Falling back to seeding with current time.");
94 seed = (unsigned int) time(NULL);
100 void pa_random(void *ret_data, size_t length) {
105 pa_assert(length > 0);
107 if (random_proper(ret_data, length) >= 0)
111 pa_log_warn("Failed to get proper entropy. Falling back to unsecure pseudo RNG.");
115 for (p = ret_data, l = length; l > 0; p++, l--)
116 *p = (uint8_t) rand();