2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @file util/crypto_random.c
24 * @brief functions to gather random numbers
25 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_crypto_lib.h"
33 * Produce a random value.
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[.
40 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
43 #ifdef gcry_fast_random_poll
44 static unsigned int invokeCount;
48 GNUNET_assert (i > 0);
50 if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
52 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
53 #ifdef gcry_fast_random_poll
54 if ((invokeCount++ % 256) == 0)
55 gcry_fast_random_poll ();
57 gcry_randomize ((unsigned char *) &ret,
64 ret = i * ((double) RANDOM () / RAND_MAX);
73 * Get an array with a random permutation of the
75 * @param mode GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
76 * PRNG should be used, GNUNET_RANDOM_QUALITY_WEAK otherwise
77 * @param n the size of the array
78 * @return the permutation array (allocated from heap)
81 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
88 GNUNET_assert (n > 0);
89 ret = GNUNET_malloc (n * sizeof (unsigned int));
90 for (i = 0; i < n; i++)
92 for (i = 0; i < n; i++)
94 x = GNUNET_CRYPTO_random_u32 (mode, n);
103 * Random on unsigned 64-bit values.
106 * @param mode desired quality of the random number
107 * @param max value returned will be in range [0,max) (exclusive)
108 * @return random 64-bit number
111 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
116 GNUNET_assert (max > 0);
117 if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
119 gcry_randomize ((unsigned char *) &ret,
126 ret = max * ((double) RANDOM () / RAND_MAX);
134 * This function should only be called in testcases
135 * where strong entropy gathering is not desired
136 * (for example, for hostkey generation).
139 GNUNET_CRYPTO_random_disable_entropy_gathering ()
141 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
147 void __attribute__ ((constructor))
148 GNUNET_util_random_init()
150 SRANDOM (time (NULL));
153 /* end of crypto_random.c */