2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2012 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"
30 #include "gnunet_os_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39 * GNUNET_YES if we are using a 'weak' (low-entropy) PRNG.
41 static int weak_random;
45 /* TODO: ndurner, move this to plibc? */
46 /* The code is derived from glibc, obviously */
54 #define RANDOM() glibc_weak_rand32()
55 #define SRANDOM(s) glibc_weak_srand32(s)
59 #define RAND_MAX 0x7fffffff /* Hopefully this is correct */
62 static int32_t glibc_weak_rand32_state = 1;
66 glibc_weak_srand32 (int32_t s)
68 glibc_weak_rand32_state = s;
75 int32_t val = glibc_weak_rand32_state;
77 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
78 glibc_weak_rand32_state = val;
84 * Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
86 * @return number between 0 and 1.
91 return ((double) RANDOM () / RAND_MAX);
96 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
99 * @param seed the seed to use
102 GNUNET_CRYPTO_seed_weak_random (int32_t seed)
109 * Produce a random value.
111 * @param mode desired quality of the random number
112 * @param i the upper limit (exclusive) for the random number
113 * @return a random value in the interval [0,i[.
116 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
118 #ifdef gcry_fast_random_poll
119 static unsigned int invokeCount;
124 GNUNET_assert (i > 0);
128 case GNUNET_CRYPTO_QUALITY_STRONG:
129 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
130 #ifdef gcry_fast_random_poll
131 if ((invokeCount++ % 256) == 0)
132 gcry_fast_random_poll ();
134 ul = UINT32_MAX - (UINT32_MAX % i);
137 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t),
142 case GNUNET_CRYPTO_QUALITY_NONCE:
143 ul = UINT32_MAX - (UINT32_MAX % i);
146 gcry_create_nonce (&ret, sizeof (ret));
150 case GNUNET_CRYPTO_QUALITY_WEAK:
151 ret = i * get_weak_random ();
163 * Get an array with a random permutation of the
165 * @param mode GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
166 * PRNG should be used, GNUNET_RANDOM_QUALITY_WEAK otherwise
167 * @param n the size of the array
168 * @return the permutation array (allocated from heap)
171 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
178 GNUNET_assert (n > 0);
179 ret = GNUNET_malloc (n * sizeof (unsigned int));
180 for (i = 0; i < n; i++)
182 for (i = n - 1; i > 0; i--)
184 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
193 * Random on unsigned 64-bit values.
196 * @param mode desired quality of the random number
197 * @param max value returned will be in range [0,max) (exclusive)
198 * @return random 64-bit number
201 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
206 GNUNET_assert (max > 0);
209 case GNUNET_CRYPTO_QUALITY_STRONG:
210 ul = UINT64_MAX - (UINT64_MAX % max);
213 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
218 case GNUNET_CRYPTO_QUALITY_NONCE:
219 ul = UINT64_MAX - (UINT64_MAX % max);
222 gcry_create_nonce (&ret, sizeof (ret));
227 case GNUNET_CRYPTO_QUALITY_WEAK:
228 ret = max * get_weak_random ();
240 * Check if we are using weak random number generation.
242 * @return GNUNET_YES if weak number generation is on
245 GNUNET_CRYPTO_random_is_weak ()
252 * This function should only be called in testcases
253 * where strong entropy gathering is not desired
254 * (for example, for hostkey generation).
257 GNUNET_CRYPTO_random_disable_entropy_gathering ()
259 weak_random = GNUNET_YES;
260 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
265 * Process ID of the "find" process that we use for
268 static struct GNUNET_OS_Process *genproc;
272 * Function called by libgcrypt whenever we are
273 * blocked gathering entropy.
276 entropy_generator (void *cls, const char *what, int printchar, int current,
280 enum GNUNET_OS_ProcessStatusType type;
283 if (0 != strcmp (what, "need_entropy"))
285 if (current == total)
289 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
290 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
291 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
292 GNUNET_OS_process_destroy (genproc);
299 ret = GNUNET_OS_process_status (genproc, &type, &code);
300 if (ret == GNUNET_NO)
301 return; /* still running */
302 if (ret == GNUNET_SYSERR)
307 if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
308 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "kill");
309 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
310 GNUNET_OS_process_destroy (genproc);
313 LOG (GNUNET_ERROR_TYPE_INFO, _("Starting `%s' process to generate entropy\n"),
316 GNUNET_OS_start_process (GNUNET_NO, 0,
317 NULL, NULL, "sh", "sh", "-c",
318 "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null",
328 GNUNET_OS_process_kill (genproc, SIGKILL);
329 GNUNET_OS_process_destroy (genproc);
335 void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
337 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
338 if (!gcry_check_version (NEED_LIBGCRYPT_VERSION))
342 ("libgcrypt has not the expected version (version %s is required).\n"),
343 NEED_LIBGCRYPT_VERSION);
346 #ifdef GCRYCTL_INITIALIZATION_FINISHED
347 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
349 #ifdef gcry_fast_random_poll
350 gcry_fast_random_poll ();
352 gcry_set_progress_handler (&entropy_generator, NULL);
354 GNUNET_CRYPTO_seed_weak_random (time (NULL) ^
355 GNUNET_CRYPTO_random_u32
356 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
360 void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini ()
362 gcry_set_progress_handler (NULL, NULL);
367 /* end of crypto_random.c */