source: roaraudio/libroar/random.c @ 4447:330209bb6de3

Last change on this file since 4447:330209bb6de3 was 4447:330209bb6de3, checked in by phi, 14 years ago

added general random functions and support for gcrypt backend

File size: 2.6 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
50 inited = 1;
51}
52
53int roar_random_gen(void * buffer, size_t len, int quality) {
54 if ( len == 0 )
55  return 0;
56
57 if ( buffer == NULL )
58  return -1;
59
60 roar_random_init();
61
62 switch (quality) {
63  case ROAR_RANDOM_NONE:
64    // no entropy:
65    memset(buffer, 0, len);
66   break;
67#ifdef ROAR_HAVE_LIBGCRYPT
68  case ROAR_RANDOM_VERY_WEAK:
69  case ROAR_RANDOM_WEAK:
70    gcry_create_nonce(buffer, len);
71   break;
72  case ROAR_RANDOM_NORMAL:
73  case ROAR_RANDOM_STRONG:
74    gcry_randomize(buffer, len, GCRY_STRONG_RANDOM);
75   break;
76  case ROAR_RANDOM_VERY_STRONG:
77    gcry_randomize(buffer, len, GCRY_VERY_STRONG_RANDOM);
78   break;
79#endif
80  default:
81    return -1;
82   break;
83 }
84
85 return 0;
86}
87
88void * roar_random_genbuf(size_t len, int quality, int locked) {
89 void * ret = roar_mm_malloc(len);
90
91 if (ret == NULL)
92  return NULL;
93
94 if ( locked ) {
95  if ( roar_mm_mlock(ret, len) == -1 ) {
96   roar_mm_free(ret);
97   return NULL;
98  }
99 }
100
101 if ( roar_random_gen(ret, len, quality) == -1 ) {
102  roar_mm_free(ret);
103  return NULL;
104 }
105
106 return ret;
107}
108
109//ll
Note: See TracBrowser for help on using the repository browser.