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