source: roaraudio/libroar/random.c @ 5889:d866fb1213d6

Last change on this file since 5889:d866fb1213d6 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 5.9 KB
Line 
1//random.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013
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
42static 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  64
55#define TIGER_DIGESTLEN (3*8)
56
57static size_t roar_nonce_salt_len = 0;
58static void * roar_nonce_salt     = NULL;
59
60static unsigned char roar_nonce_pool[TIGER_DIGESTLEN];
61static size_t        roar_nonce_pool_len = 0;
62
63int roar_random_gen_nonce(void * buffer, size_t len) {
64 static uint32_t buf[TIGER_BLOCKLEN/4];
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_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}",
117           (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]);
118
119  roar_hash_salted_buffer(off, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER, roar_nonce_salt, roar_nonce_salt_len);
120
121  writelen = len >= TIGER_DIGESTLEN ? TIGER_DIGESTLEN : len;
122  memcpy(buffer, off, writelen);
123
124  buffer += writelen;
125  len    -= writelen;
126 }
127
128 return 0;
129}
130
131int roar_random_salt_nonce (void * salt, size_t len) {
132 char buf[1];
133 int ret;
134
135 roar_nonce_salt     = salt;
136 roar_nonce_salt_len = len;
137
138 ret = roar_random_gen_nonce(buf, sizeof(buf));
139
140 roar_nonce_salt     = NULL;
141 roar_nonce_salt_len = 0;
142
143 return ret;
144}
145
146uint16_t roar_random_uint16(void) {
147 uint16_t ret;
148
149 if ( roar_nonce_pool_len < 2 ) {
150  roar_random_gen_nonce(roar_nonce_pool, sizeof(roar_nonce_pool));
151  roar_nonce_pool_len = sizeof(roar_nonce_pool);
152 }
153
154 ret  = roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] <<  0;
155 ret |= roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] <<  8;
156
157 roar_nonce_pool_len -= 2;
158
159 return ret;
160}
161
162uint32_t roar_random_uint32(void) {
163 uint32_t ret;
164
165 if ( roar_nonce_pool_len < 4 ) {
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  = (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+0] <<  0;
171 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+1] <<  8;
172 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+2] << 16;
173 ret |= (uint32_t)roar_nonce_pool[sizeof(roar_nonce_pool)-roar_nonce_pool_len+3] << 24;
174
175 roar_nonce_pool_len -= 4;
176
177 return ret;
178}
179
180int roar_random_gen(void * buffer, size_t len, int quality) {
181 if ( len == 0 )
182  return 0;
183
184 if ( buffer == NULL )
185  return -1;
186
187 roar_random_init();
188
189 switch (quality) {
190  case ROAR_RANDOM_NONE:
191    // no entropy:
192    memset(buffer, 0, len);
193   break;
194  case ROAR_RANDOM_VERY_WEAK:
195    return roar_random_gen_nonce(buffer, len);
196   break;
197#ifdef ROAR_HAVE_LIBGCRYPT
198  case ROAR_RANDOM_WEAK:
199    gcry_create_nonce(buffer, len);
200   break;
201  case ROAR_RANDOM_NORMAL:
202  case ROAR_RANDOM_STRONG:
203    gcry_randomize(buffer, len, GCRY_STRONG_RANDOM);
204   break;
205  case ROAR_RANDOM_VERY_STRONG:
206    gcry_randomize(buffer, len, GCRY_VERY_STRONG_RANDOM);
207   break;
208#endif
209  default:
210    return -1;
211   break;
212 }
213
214 return 0;
215}
216
217void * roar_random_genbuf(size_t len, int quality, int locked) {
218 void * ret = roar_mm_malloc(len);
219
220 if (ret == NULL)
221  return NULL;
222
223 if ( locked ) {
224  if ( roar_mm_mlock(ret, len) == -1 ) {
225   roar_mm_free(ret);
226   return NULL;
227  }
228 }
229
230 if ( roar_random_gen(ret, len, quality) == -1 ) {
231  roar_mm_free(ret);
232  return NULL;
233 }
234
235 return ret;
236}
237
238//ll
Note: See TracBrowser for help on using the repository browser.