676416c63328e918f3ca3df3a1bc9d25d3187cc3
[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   uint32_t ul;
59
60   GNUNET_assert (i > 0);
61
62   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
63     {
64       /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
65 #ifdef gcry_fast_random_poll
66       if ((invokeCount++ % 256) == 0)
67         gcry_fast_random_poll ();
68 #endif
69       ul = ((uint32_t)-1) - (((uint32_t)-1) % i);
70       do
71         {
72           gcry_randomize ((unsigned char *) &ret,
73                           sizeof (uint32_t), GCRY_STRONG_RANDOM);
74         }
75       while (ret >= ul);
76       return ret % i;
77     }
78   else
79     {
80       ret = i * weak_random ();
81       if (ret >= i)
82         ret = i - 1;
83       return ret;
84     }
85 }
86
87
88 /**
89  * Get an array with a random permutation of the
90  * numbers 0...n-1.
91  * @param mode GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
92  *        PRNG should be used, GNUNET_RANDOM_QUALITY_WEAK otherwise
93  * @param n the size of the array
94  * @return the permutation array (allocated from heap)
95  */
96 unsigned int *
97 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
98 {
99   unsigned int *ret;
100   unsigned int i;
101   unsigned int tmp;
102   uint32_t x;
103
104   GNUNET_assert (n > 0);
105   ret = GNUNET_malloc (n * sizeof (unsigned int));
106   for (i = 0; i < n; i++)
107     ret[i] = i;
108   for (i = n - 1; i > 0; i--)
109     {
110       x = GNUNET_CRYPTO_random_u32 (mode, i+1);
111       tmp = ret[x];
112       ret[x] = ret[i];
113       ret[i] = tmp;
114     }
115   return ret;
116 }
117
118 /**
119  * Random on unsigned 64-bit values.
120  *
121  *
122  * @param mode desired quality of the random number
123  * @param max value returned will be in range [0,max) (exclusive)
124  * @return random 64-bit number
125  */
126 uint64_t
127 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
128 {
129   uint64_t ret;
130   uint64_t ul;
131
132   GNUNET_assert (max > 0);
133   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
134     {
135       ul = ((uint64_t)-1LL) - (((uint64_t)-1LL) % max);
136       do
137         {
138           gcry_randomize ((unsigned char *) &ret,
139                           sizeof (uint64_t), GCRY_STRONG_RANDOM);
140         }
141       while (ret >= ul);
142       return ret % max;
143     }
144   else
145     {
146       ret = max * weak_random ();
147       if (ret >= max)
148         ret = max - 1;
149       return ret;
150     }
151 }
152
153 /**
154  * This function should only be called in testcases
155  * where strong entropy gathering is not desired
156  * (for example, for hostkey generation).
157  */
158 void
159 GNUNET_CRYPTO_random_disable_entropy_gathering ()
160 {
161   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
162 }
163
164 /**
165  * Initializer
166  */
167 void __attribute__ ((constructor)) GNUNET_util_random_init ()
168 {
169   SRANDOM (time (NULL));
170 }
171
172 /* end of crypto_random.c */