]> code.delx.au - pulseaudio/blob - src/pulsecore/random.c
bdbc143715b5ecfe936e836de17e4791bcaf0744
[pulseaudio] / 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 #ifdef HAVE_WINDOWS_H
35 #include <windows.h>
36 #include <wincrypt.h>
37 #endif
38
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/macro.h>
42
43 #include "random.h"
44
45 static pa_bool_t has_whined = FALSE;
46
47 static const char * const devices[] = { "/dev/urandom", "/dev/random", NULL };
48
49 static int random_proper(void *ret_data, size_t length) {
50 #ifdef OS_IS_WIN32
51 int ret = -1;
52
53 pa_assert(ret_data);
54 pa_assert(length > 0);
55
56 HCRYPTPROV hCryptProv = NULL;
57
58 if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
59 if(CryptGenRandom(hCryptProv, length, ret_data))
60 ret = 0;
61 CryptReleaseContext(hCryptProv, 0);
62 }
63
64 return ret;
65
66 #else /* OS_IS_WIN32 */
67
68 int fd, ret = -1;
69 ssize_t r = 0;
70 const char *const * device;
71
72 pa_assert(ret_data);
73 pa_assert(length > 0);
74
75 device = devices;
76
77 while (*device) {
78 ret = 0;
79
80 if ((fd = pa_open_cloexec(*device, O_RDONLY, 0)) >= 0) {
81
82 if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
83 ret = -1;
84
85 pa_close(fd);
86 } else
87 ret = -1;
88
89 if (ret == 0)
90 break;
91
92 device++;
93 }
94
95 return ret;
96 #endif /* OS_IS_WIN32 */
97 }
98
99 void pa_random_seed(void) {
100 unsigned int seed;
101
102 if (random_proper(&seed, sizeof(unsigned int)) < 0) {
103
104 if (!has_whined) {
105 pa_log_warn("Failed to get proper entropy. Falling back to seeding with current time.");
106 has_whined = TRUE;
107 }
108
109 seed = (unsigned int) time(NULL);
110 }
111
112 srand(seed);
113 }
114
115 void pa_random(void *ret_data, size_t length) {
116 uint8_t *p;
117 size_t l;
118
119 pa_assert(ret_data);
120 pa_assert(length > 0);
121
122 if (random_proper(ret_data, length) >= 0)
123 return;
124
125 if (!has_whined) {
126 pa_log_warn("Failed to get proper entropy. Falling back to unsecure pseudo RNG.");
127 has_whined = TRUE;
128 }
129
130 for (p = ret_data, l = length; l > 0; p++, l--)
131 *p = (uint8_t) rand();
132 }