handle errors better
[oweals/gnunet.git] / src / util / crypto_random.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19
20 */
21
22 /**
23  * @file util/crypto_random.c
24  * @brief functions to gather random numbers
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_crypto_lib.h"
30 #include <gcrypt.h>
31
32 /**
33  * Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
34  * 
35  * @return number between 0 and 1.
36  */
37 static double
38 weak_random ()
39 {
40   return ((double) RANDOM () / RAND_MAX);
41 }
42
43
44 /**
45  * Produce a random value.
46  *
47  * @param mode desired quality of the random number
48  * @param i the upper limit (exclusive) for the random number
49  * @return a random value in the interval [0,i[.
50  */
51 uint32_t
52 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
53 {
54 #ifdef gcry_fast_random_poll
55   static unsigned int invokeCount;
56 #endif
57   uint32_t ret;
58
59   GNUNET_assert (i > 0);
60
61   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
62     {
63       /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
64 #ifdef gcry_fast_random_poll
65       if ((invokeCount++ % 256) == 0)
66         gcry_fast_random_poll ();
67 #endif
68       gcry_randomize ((unsigned char *) &ret,
69                       sizeof (uint32_t), GCRY_STRONG_RANDOM);
70       return ret % i;
71     }
72   else
73     {
74       ret = i * weak_random ();
75       if (ret >= i)
76         ret = i - 1;
77       return ret;
78     }
79 }
80
81
82 /**
83  * Get an array with a random permutation of the
84  * numbers 0...n-1.
85  * @param mode GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
86  *        PRNG should be used, GNUNET_RANDOM_QUALITY_WEAK otherwise
87  * @param n the size of the array
88  * @return the permutation array (allocated from heap)
89  */
90 unsigned int *
91 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
92 {
93   unsigned int *ret;
94   unsigned int i;
95   unsigned int tmp;
96   uint32_t x;
97
98   GNUNET_assert (n > 0);
99   ret = GNUNET_malloc (n * sizeof (unsigned int));
100   for (i = 0; i < n; i++)
101     ret[i] = i;
102   for (i = 0; i < n; i++)
103     {
104       x = GNUNET_CRYPTO_random_u32 (mode, n);
105       tmp = ret[x];
106       ret[x] = ret[i];
107       ret[i] = tmp;
108     }
109   return ret;
110 }
111
112 /**
113  * Random on unsigned 64-bit values.
114  *
115  *
116  * @param mode desired quality of the random number
117  * @param max value returned will be in range [0,max) (exclusive)
118  * @return random 64-bit number
119  */
120 uint64_t
121 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
122 {
123   uint64_t ret;
124
125   GNUNET_assert (max > 0);
126   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
127     {
128       gcry_randomize ((unsigned char *) &ret,
129                       sizeof (uint64_t), GCRY_STRONG_RANDOM);
130       return ret % max;
131     }
132   else
133     {
134       ret = max * weak_random ();
135       if (ret >= max)
136         ret = max - 1;
137       return ret;
138     }
139 }
140
141 /**
142  * This function should only be called in testcases
143  * where strong entropy gathering is not desired
144  * (for example, for hostkey generation).
145  */
146 void
147 GNUNET_CRYPTO_random_disable_entropy_gathering ()
148 {
149   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
150 }
151
152 /**
153  * Initializer
154  */
155 void __attribute__ ((constructor)) GNUNET_util_random_init ()
156 {
157   SRANDOM (time (NULL));
158 }
159
160 /* end of crypto_random.c */