1 | //random.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | #ifdef ROAR_HAVE_LIBGCRYPT |
---|
39 | #include <gcrypt.h> |
---|
40 | #endif |
---|
41 | |
---|
42 | static void roar_random_init (void) { |
---|
43 | static int inited = 0; |
---|
44 | |
---|
45 | if (inited) |
---|
46 | return; |
---|
47 | |
---|
48 | // add stuff here needed to bring up random source. |
---|
49 | roar_crypto_init(); |
---|
50 | |
---|
51 | inited = 1; |
---|
52 | } |
---|
53 | |
---|
54 | #define TIGER_BLOCKLEN 55 |
---|
55 | #define TIGER_DIGESTLEN (3*8) |
---|
56 | |
---|
57 | static size_t roar_nonce_salt_len = 0; |
---|
58 | static void * roar_nonce_salt = NULL; |
---|
59 | |
---|
60 | static unsigned char roar_nonce_pool[TIGER_DIGESTLEN]; |
---|
61 | static size_t roar_nonce_pool_len = 0; |
---|
62 | |
---|
63 | int roar_random_gen_nonce(void * buffer, size_t len) { |
---|
64 | static uint32_t buf[14]; |
---|
65 | static int inited = 0; |
---|
66 | static int idx = 0; |
---|
67 | volatile pid_t pid = getpid(); |
---|
68 | size_t i, writelen; |
---|
69 | void * off = buf; |
---|
70 | #ifdef ROAR_HAVE_TIME |
---|
71 | volatile uint32_t now = time(NULL); |
---|
72 | #endif |
---|
73 | |
---|
74 | roar_random_init(); |
---|
75 | |
---|
76 | if ( !inited ) { |
---|
77 | for (i = 0; i < (sizeof(buf)/sizeof(*buf)); i++) { |
---|
78 | #ifdef ROAR_HAVE_RAND |
---|
79 | buf[i] = rand() + pid; |
---|
80 | #else |
---|
81 | buf[i] = pid; |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | #ifdef ROAR_HAVE_TIME |
---|
86 | buf[11] += now; |
---|
87 | #endif |
---|
88 | |
---|
89 | roar_hash_buffer(off, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER); |
---|
90 | roar_hash_buffer(off+TIGER_DIGESTLEN, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER); |
---|
91 | |
---|
92 | inited = 1; |
---|
93 | } |
---|
94 | |
---|
95 | while (len) { |
---|
96 | |
---|
97 | #ifdef ROAR_HAVE_TIME |
---|
98 | buf[12] += now; |
---|
99 | #endif |
---|
100 | |
---|
101 | #ifdef ROAR_HAVE_RAND |
---|
102 | buf[12] += rand(); |
---|
103 | #endif |
---|
104 | |
---|
105 | buf[12] += pid; |
---|
106 | buf[13] += pid; |
---|
107 | |
---|
108 | off = buf; |
---|
109 | if ( idx ) { |
---|
110 | off += TIGER_DIGESTLEN; |
---|
111 | idx = 0; |
---|
112 | } else { |
---|
113 | idx = 1; |
---|
114 | } |
---|
115 | |
---|
116 | roar_hash_salted_buffer(off, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER, roar_nonce_salt, roar_nonce_salt_len); |
---|
117 | |
---|
118 | writelen = len >= TIGER_DIGESTLEN ? TIGER_DIGESTLEN : len; |
---|
119 | memcpy(buffer, off, writelen); |
---|
120 | |
---|
121 | buffer += writelen; |
---|
122 | len -= writelen; |
---|
123 | } |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | int roar_random_salt_nonce (void * salt, size_t len) { |
---|
129 | char buf[1]; |
---|
130 | int ret; |
---|
131 | |
---|
132 | roar_nonce_salt = salt; |
---|
133 | roar_nonce_salt_len = len; |
---|
134 | |
---|
135 | ret = roar_random_gen_nonce(buf, sizeof(buf)); |
---|
136 | |
---|
137 | roar_nonce_salt = NULL; |
---|
138 | roar_nonce_salt_len = 0; |
---|
139 | |
---|
140 | return ret; |
---|
141 | } |
---|
142 | |
---|
143 | uint16_t roar_random_uint16(void) { |
---|
144 | uint16_t ret; |
---|
145 | |
---|
146 | if ( roar_nonce_pool_len < 2 ) { |
---|
147 | roar_random_gen_nonce(roar_nonce_pool, sizeof(roar_nonce_pool)); |
---|
148 | roar_nonce_pool_len = sizeof(roar_nonce_pool); |
---|
149 | } |
---|
150 | |
---|
151 | ret = roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] << 0; |
---|
152 | ret |= roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] << 8; |
---|
153 | |
---|
154 | roar_nonce_pool_len -= 2; |
---|
155 | |
---|
156 | return ret; |
---|
157 | } |
---|
158 | |
---|
159 | uint32_t roar_random_uint32(void) { |
---|
160 | uint32_t ret; |
---|
161 | |
---|
162 | if ( roar_nonce_pool_len < 4 ) { |
---|
163 | roar_random_gen_nonce(roar_nonce_pool, sizeof(roar_nonce_pool)); |
---|
164 | roar_nonce_pool_len = sizeof(roar_nonce_pool); |
---|
165 | } |
---|
166 | |
---|
167 | ret = (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] << 0; |
---|
168 | ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] << 8; |
---|
169 | ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+2] << 16; |
---|
170 | ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+3] << 24; |
---|
171 | |
---|
172 | roar_nonce_pool_len -= 4; |
---|
173 | |
---|
174 | return ret; |
---|
175 | } |
---|
176 | |
---|
177 | int roar_random_gen(void * buffer, size_t len, int quality) { |
---|
178 | if ( len == 0 ) |
---|
179 | return 0; |
---|
180 | |
---|
181 | if ( buffer == NULL ) |
---|
182 | return -1; |
---|
183 | |
---|
184 | roar_random_init(); |
---|
185 | |
---|
186 | switch (quality) { |
---|
187 | case ROAR_RANDOM_NONE: |
---|
188 | // no entropy: |
---|
189 | memset(buffer, 0, len); |
---|
190 | break; |
---|
191 | case ROAR_RANDOM_VERY_WEAK: |
---|
192 | return roar_random_gen_nonce(buffer, len); |
---|
193 | break; |
---|
194 | #ifdef ROAR_HAVE_LIBGCRYPT |
---|
195 | case ROAR_RANDOM_WEAK: |
---|
196 | gcry_create_nonce(buffer, len); |
---|
197 | break; |
---|
198 | case ROAR_RANDOM_NORMAL: |
---|
199 | case ROAR_RANDOM_STRONG: |
---|
200 | gcry_randomize(buffer, len, GCRY_STRONG_RANDOM); |
---|
201 | break; |
---|
202 | case ROAR_RANDOM_VERY_STRONG: |
---|
203 | gcry_randomize(buffer, len, GCRY_VERY_STRONG_RANDOM); |
---|
204 | break; |
---|
205 | #endif |
---|
206 | default: |
---|
207 | return -1; |
---|
208 | break; |
---|
209 | } |
---|
210 | |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|
214 | void * roar_random_genbuf(size_t len, int quality, int locked) { |
---|
215 | void * ret = roar_mm_malloc(len); |
---|
216 | |
---|
217 | if (ret == NULL) |
---|
218 | return NULL; |
---|
219 | |
---|
220 | if ( locked ) { |
---|
221 | if ( roar_mm_mlock(ret, len) == -1 ) { |
---|
222 | roar_mm_free(ret); |
---|
223 | return NULL; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | if ( roar_random_gen(ret, len, quality) == -1 ) { |
---|
228 | roar_mm_free(ret); |
---|
229 | return NULL; |
---|
230 | } |
---|
231 | |
---|
232 | return ret; |
---|
233 | } |
---|
234 | |
---|
235 | //ll |
---|