Git init
[framework/multimedia/pulseaudio.git] / src / pulsecore / random.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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.
11
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.
16
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
20   USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <time.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37
38 #include "random.h"
39
40 static pa_bool_t has_whined = FALSE;
41
42 static const char * const devices[] = { "/dev/urandom", "/dev/random", NULL };
43
44 static int random_proper(void *ret_data, size_t length) {
45 #ifdef OS_IS_WIN32
46     pa_assert(ret_data);
47     pa_assert(length > 0);
48
49     return -1;
50
51 #else /* OS_IS_WIN32 */
52
53     int fd, ret = -1;
54     ssize_t r = 0;
55     const char *const * device;
56
57     pa_assert(ret_data);
58     pa_assert(length > 0);
59
60     device = devices;
61
62     while (*device) {
63         ret = 0;
64
65         if ((fd = open(*device, O_RDONLY
66 #ifdef O_NOCTTY
67                        | O_NOCTTY
68 #endif
69              )) >= 0) {
70
71             if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
72                 ret = -1;
73
74             pa_close(fd);
75         } else
76             ret = -1;
77
78         if (ret == 0)
79             break;
80
81         device++;
82     }
83
84     return ret;
85 #endif /* OS_IS_WIN32 */
86 }
87
88 void pa_random_seed(void) {
89     unsigned int seed;
90
91     if (random_proper(&seed, sizeof(unsigned int)) < 0) {
92
93         if (!has_whined) {
94             pa_log_warn("Failed to get proper entropy. Falling back to seeding with current time.");
95             has_whined = TRUE;
96         }
97
98         seed = (unsigned int) time(NULL);
99     }
100
101     srand(seed);
102 }
103
104 void pa_random(void *ret_data, size_t length) {
105     uint8_t *p;
106     size_t l;
107
108     pa_assert(ret_data);
109     pa_assert(length > 0);
110
111     if (random_proper(ret_data, length) >= 0)
112         return;
113
114     if (!has_whined) {
115         pa_log_warn("Failed to get proper entropy. Falling back to unsecure pseudo RNG.");
116         has_whined = TRUE;
117     }
118
119     for (p = ret_data, l = length; l > 0; p++, l--)
120         *p = (uint8_t) rand();
121 }