use search path
[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  * Produce a random value.
34  *
35  * @param mode desired quality of the random number
36  * @param i the upper limit (exclusive) for the random number
37  * @return a random value in the interval [0,i[.
38  */
39 uint32_t
40 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
41 {
42 #ifdef gcry_fast_random_poll
43   static unsigned int invokeCount;
44 #endif
45   uint32_t ret;
46
47   GNUNET_assert (i > 0);
48
49   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
50     {
51       /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
52 #ifdef gcry_fast_random_poll
53       if ((invokeCount++ % 256) == 0)
54         gcry_fast_random_poll ();
55 #endif
56       gcry_randomize ((unsigned char *) &ret,
57                       sizeof (uint32_t), GCRY_STRONG_RANDOM);
58       return ret % i;
59     }
60   else
61     {
62       ret = i * ((double) RANDOM () / RAND_MAX);
63       if (ret >= i)
64         ret = i - 1;
65       return ret;
66     }
67 }
68
69
70 /**
71  * Get an array with a random permutation of the
72  * numbers 0...n-1.
73  * @param mode GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
74  *        PRNG should be used, GNUNET_RANDOM_QUALITY_WEAK otherwise
75  * @param n the size of the array
76  * @return the permutation array (allocated from heap)
77  */
78 unsigned int *
79 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
80 {
81   unsigned int *ret;
82   unsigned int i;
83   unsigned int tmp;
84   uint32_t x;
85
86   GNUNET_assert (n > 0);
87   ret = GNUNET_malloc (n * sizeof (unsigned int));
88   for (i = 0; i < n; i++)
89     ret[i] = i;
90   for (i = 0; i < n; i++)
91     {
92       x = GNUNET_CRYPTO_random_u32 (mode, n);
93       tmp = ret[x];
94       ret[x] = ret[i];
95       ret[i] = tmp;
96     }
97   return ret;
98 }
99
100 /**
101  * Random on unsigned 64-bit values.
102  *
103  *
104  * @param mode desired quality of the random number
105  * @param max value returned will be in range [0,max) (exclusive)
106  * @return random 64-bit number
107  */
108 uint64_t
109 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
110 {
111   uint64_t ret;
112
113   GNUNET_assert (max > 0);
114   if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
115     {
116       gcry_randomize ((unsigned char *) &ret,
117                       sizeof (uint64_t), GCRY_STRONG_RANDOM);
118       return ret % max;
119     }
120   else
121     {
122       ret = max * ((double) RANDOM () / RAND_MAX);
123       if (ret >= max)
124         ret = max - 1;
125       return ret;
126     }
127 }
128
129 /**
130  * This function should only be called in testcases
131  * where strong entropy gathering is not desired
132  * (for example, for hostkey generation).
133  */
134 void
135 GNUNET_CRYPTO_random_disable_entropy_gathering ()
136 {
137   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
138 }
139
140 /**
141  * Initializer
142  */
143 void __attribute__ ((constructor)) GNUNET_util_random_init ()
144 {
145   SRANDOM (time (NULL));
146 }
147
148 /* end of crypto_random.c */