source: roaraudio/libroar/random.c @ 6076:b81c397aee90

Last change on this file since 6076:b81c397aee90 was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 6.2 KB
Line 
1//random.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2015
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#ifdef ROAR_HAVE_UNAME
43#include <sys/utsname.h>
44#endif
45
46static void roar_random_init (void) {
47 static int inited = 0;
48
49 if (inited)
50  return;
51
52 // add stuff here needed to bring up random source.
53 roar_crypto_init();
54
55 inited = 1;
56}
57
58#define TIGER_BLOCKLEN  64
59#define TIGER_DIGESTLEN (3*8)
60
61static size_t roar_nonce_salt_len = 0;
62static void * roar_nonce_salt     = NULL;
63
64static unsigned char roar_nonce_pool[TIGER_DIGESTLEN];
65static size_t        roar_nonce_pool_len = 0;
66
67int roar_random_gen_nonce(void * buffer, size_t len) {
68 static uint32_t buf[TIGER_BLOCKLEN/4];
69 static int inited = 0;
70 static int idx = 0;
71 volatile pid_t pid = getpid();
72 size_t i, writelen;
73 void * off = buf;
74#ifdef ROAR_HAVE_TIME
75 volatile uint32_t now = time(NULL);
76#endif
77#ifdef ROAR_HAVE_UNAME
78 static struct utsname utsname;
79#endif
80
81 roar_random_init();
82
83 if ( !inited ) {
84  for (i = 0; i < (sizeof(buf)/sizeof(*buf)); i++) {
85#ifdef ROAR_HAVE_RAND
86   buf[i] = rand() + pid;
87#else
88   buf[i] = pid;
89#endif
90  }
91
92#ifdef ROAR_HAVE_TIME
93  buf[11] += now;
94#endif
95
96  roar_hash_buffer(off,                 buf, TIGER_BLOCKLEN, ROAR_HT_TIGER);
97  roar_hash_buffer(off+TIGER_DIGESTLEN, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER);
98
99  // init is now done:
100  inited = 1;
101
102  // do additional seeding:
103
104#ifdef ROAR_HAVE_UNAME
105  if ( uname(&utsname) == 0 ) {
106   roar_random_salt_nonce(&utsname, sizeof(utsname));
107  }
108#endif
109 }
110
111 while (len) {
112
113#ifdef ROAR_HAVE_TIME
114  buf[12] += now;
115#endif
116
117#ifdef ROAR_HAVE_RAND
118  buf[12] += rand();
119#endif
120
121  buf[12] += pid;
122  buf[13] += pid;
123
124  off = buf;
125  if ( idx ) {
126   off += TIGER_DIGESTLEN;
127   idx  = 0;
128  } else {
129   idx  = 1;
130  }
131
132  ROAR_DBG("roar_random_gen_nonce(*): buf={0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x}",
133           (unsigned int)buf[0], (unsigned int)buf[1], (unsigned int)buf[2], (unsigned int)buf[3], (unsigned int)buf[4], (unsigned int)buf[5], (unsigned int)buf[6], (unsigned int)buf[7], (unsigned int)buf[8], (unsigned int)buf[9], (unsigned int)buf[10], (unsigned int)buf[11], (unsigned int)buf[12], (unsigned int)buf[13], (unsigned int)buf[14], (unsigned int)buf[15]);
134
135  roar_hash_salted_buffer(off, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER, roar_nonce_salt, roar_nonce_salt_len);
136
137  writelen = len >= TIGER_DIGESTLEN ? TIGER_DIGESTLEN : len;
138  memcpy(buffer, off, writelen);
139
140  buffer += writelen;
141  len    -= writelen;
142 }
143
144 return 0;
145}
146
147int roar_random_salt_nonce (void * salt, size_t len) {
148 char buf[1];
149 int ret;
150
151 roar_nonce_salt     = salt;
152 roar_nonce_salt_len = len;
153
154 ret = roar_random_gen_nonce(buf, sizeof(buf));
155
156 roar_nonce_salt     = NULL;
157 roar_nonce_salt_len = 0;
158
159 return ret;
160}
161
162uint16_t roar_random_uint16(void) {
163 uint16_t ret;
164
165 if ( roar_nonce_pool_len < 2 ) {
166  roar_random_gen_nonce(roar_nonce_pool, sizeof(roar_nonce_pool));
167  roar_nonce_pool_len = sizeof(roar_nonce_pool);
168 }
169
170 ret  = roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] <<  0;
171 ret |= roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] <<  8;
172
173 roar_nonce_pool_len -= 2;
174
175 return ret;
176}
177
178uint32_t roar_random_uint32(void) {
179 uint32_t ret;
180
181 if ( roar_nonce_pool_len < 4 ) {
182  roar_random_gen_nonce(roar_nonce_pool, sizeof(roar_nonce_pool));
183  roar_nonce_pool_len = sizeof(roar_nonce_pool);
184 }
185
186 ret  = (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] <<  0;
187 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] <<  8;
188 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+2] << 16;
189 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+3] << 24;
190
191 roar_nonce_pool_len -= 4;
192
193 return ret;
194}
195
196int roar_random_gen(void * buffer, size_t len, int quality) {
197 if ( len == 0 )
198  return 0;
199
200 if ( buffer == NULL ) {
201  roar_err_set(ROAR_ERROR_FAULT);
202  return -1;
203 }
204
205 roar_random_init();
206
207 switch (quality) {
208  case ROAR_RANDOM_NONE:
209    // no entropy:
210    memset(buffer, 0, len);
211   break;
212  case ROAR_RANDOM_VERY_WEAK:
213    return roar_random_gen_nonce(buffer, len);
214   break;
215#ifdef ROAR_HAVE_LIBGCRYPT
216  case ROAR_RANDOM_WEAK:
217    gcry_create_nonce(buffer, len);
218   break;
219  case ROAR_RANDOM_NORMAL:
220  case ROAR_RANDOM_STRONG:
221    gcry_randomize(buffer, len, GCRY_STRONG_RANDOM);
222   break;
223  case ROAR_RANDOM_VERY_STRONG:
224    gcry_randomize(buffer, len, GCRY_VERY_STRONG_RANDOM);
225   break;
226#endif
227  default:
228    roar_err_set(ROAR_ERROR_NOENT);
229    return -1;
230   break;
231 }
232
233 return 0;
234}
235
236void * roar_random_genbuf(size_t len, int quality, int locked) {
237 void * ret = roar_mm_malloc(len);
238
239 if (ret == NULL)
240  return NULL;
241
242 if ( locked ) {
243  if ( roar_mm_mlock(ret, len) == -1 ) {
244   roar_mm_free_noerror(ret);
245   return NULL;
246  }
247 }
248
249 if ( roar_random_gen(ret, len, quality) == -1 ) {
250  roar_mm_free_noerror(ret);
251  return NULL;
252 }
253
254 return ret;
255}
256
257//ll
Note: See TracBrowser for help on using the repository browser.