source: roaraudio/libroar/random.c @ 4677:1a402c6e621a

Last change on this file since 4677:1a402c6e621a was 4677:1a402c6e621a, checked in by phi, 13 years ago

optimize a bit the number how often time() is called at nonce generation

File size: 4.2 KB
Line 
1//random.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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  55
55#define TIGER_DIGESTLEN (3*8)
56
57static size_t roar_nonce_salt_len = 0;
58static void * roar_nonce_salt     = NULL;
59
60int roar_random_gen_nonce(void * buffer, size_t len) {
61 static uint32_t buf[14];
62 static int inited = 0;
63 static int idx = 0;
64 volatile pid_t pid = getpid();
65 size_t i, writelen;
66 void * off = buf;
67#ifdef ROAR_HAVE_TIME
68 volatile uint32_t now = time(NULL);
69#endif
70
71 roar_random_init();
72
73 if ( !inited ) {
74  for (i = 0; i < (sizeof(buf)/sizeof(*buf)); i++) {
75#ifdef ROAR_HAVE_RAND
76   buf[i] = rand() + pid;
77#else
78   buf[i] = pid;
79#endif
80  }
81
82  roar_hash_buffer(off,                 buf, TIGER_BLOCKLEN, ROAR_HT_TIGER);
83  roar_hash_buffer(off+TIGER_DIGESTLEN, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER);
84
85  inited = 1;
86 }
87
88 while (len) {
89
90#ifdef ROAR_HAVE_TIME
91  buf[12] += now;
92#endif
93
94#ifdef ROAR_HAVE_RAND
95  buf[12] += rand();
96#endif
97
98  buf[12] += pid;
99  buf[13] += pid;
100
101  off = buf;
102  if ( idx ) {
103   off += TIGER_DIGESTLEN;
104   idx  = 0;
105  } else {
106   idx  = 1;
107  }
108
109  roar_hash_salted_buffer(off, buf, TIGER_BLOCKLEN, ROAR_HT_TIGER, roar_nonce_salt, roar_nonce_salt_len);
110
111  writelen = len >= TIGER_DIGESTLEN ? TIGER_DIGESTLEN : len;
112  memcpy(buffer, off, writelen);
113
114  buffer += writelen;
115  len    -= writelen;
116 }
117
118 return 0;
119}
120
121int roar_random_salt_nonce (void * salt, size_t len) {
122 char buf[1];
123 int ret;
124
125 roar_nonce_salt     = salt;
126 roar_nonce_salt_len = len;
127
128 ret = roar_random_gen_nonce(buf, sizeof(buf));
129
130 roar_nonce_salt     = NULL;
131 roar_nonce_salt_len = 0;
132
133 return ret;
134}
135
136int roar_random_gen(void * buffer, size_t len, int quality) {
137 if ( len == 0 )
138  return 0;
139
140 if ( buffer == NULL )
141  return -1;
142
143 roar_random_init();
144
145 switch (quality) {
146  case ROAR_RANDOM_NONE:
147    // no entropy:
148    memset(buffer, 0, len);
149   break;
150  case ROAR_RANDOM_VERY_WEAK:
151    return roar_random_gen_nonce(buffer, len);
152   break;
153#ifdef ROAR_HAVE_LIBGCRYPT
154  case ROAR_RANDOM_WEAK:
155    gcry_create_nonce(buffer, len);
156   break;
157  case ROAR_RANDOM_NORMAL:
158  case ROAR_RANDOM_STRONG:
159    gcry_randomize(buffer, len, GCRY_STRONG_RANDOM);
160   break;
161  case ROAR_RANDOM_VERY_STRONG:
162    gcry_randomize(buffer, len, GCRY_VERY_STRONG_RANDOM);
163   break;
164#endif
165  default:
166    return -1;
167   break;
168 }
169
170 return 0;
171}
172
173void * roar_random_genbuf(size_t len, int quality, int locked) {
174 void * ret = roar_mm_malloc(len);
175
176 if (ret == NULL)
177  return NULL;
178
179 if ( locked ) {
180  if ( roar_mm_mlock(ret, len) == -1 ) {
181   roar_mm_free(ret);
182   return NULL;
183  }
184 }
185
186 if ( roar_random_gen(ret, len, quality) == -1 ) {
187  roar_mm_free(ret);
188  return NULL;
189 }
190
191 return ret;
192}
193
194//ll
Note: See TracBrowser for help on using the repository browser.