2 This file is part of GNUnet. Copyright (C) 2001-2014 Christian Grothoff
3 (and other contributing authors)
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
18 * @file util/crypto_random.c
19 * @brief functions to gather random numbers
20 * @author Christian Grothoff
23 #include "gnunet_crypto_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
28 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
31 /* TODO: ndurner, move this to plibc? */
32 /* The code is derived from glibc, obviously */
33 #if !HAVE_RANDOM || !HAVE_SRANDOM
40 #define RANDOM() glibc_weak_rand32()
41 #define SRANDOM(s) glibc_weak_srand32(s)
45 #define RAND_MAX 0x7fffffff /* Hopefully this is correct */
48 static int32_t glibc_weak_rand32_state = 1;
52 glibc_weak_srand32 (int32_t s)
54 glibc_weak_rand32_state = s;
61 int32_t val = glibc_weak_rand32_state;
63 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
64 glibc_weak_rand32_state = val;
70 * Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
72 * @return number between 0 and 1.
77 return ((double) RANDOM () / RAND_MAX);
82 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
85 * @param seed the seed to use
88 GNUNET_CRYPTO_seed_weak_random (int32_t seed)
96 * Fill block with a random values.
98 * @param mode desired quality of the random number
99 * @param buffer the buffer to fill
100 * @param length buffer length
103 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
105 #ifdef gcry_fast_random_poll
106 static unsigned int invokeCount;
110 case GNUNET_CRYPTO_QUALITY_STRONG:
111 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
112 #ifdef gcry_fast_random_poll
113 if ((invokeCount++ % 256) == 0)
114 gcry_fast_random_poll ();
116 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
118 case GNUNET_CRYPTO_QUALITY_NONCE:
119 gcry_create_nonce (buffer, length);
121 case GNUNET_CRYPTO_QUALITY_WEAK:
122 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
123 #ifdef gcry_fast_random_poll
124 if ((invokeCount++ % 256) == 0)
125 gcry_fast_random_poll ();
127 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
136 * Produce a random unsigned 32-bit number modulo @a i.
138 * @param mode desired quality of the random number
139 * @param i the upper limit (exclusive) for the random number
140 * @return a random value in the interval [0,i[.
143 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
146 #ifdef gcry_fast_random_poll
147 static unsigned int invokeCount;
152 GNUNET_assert (i > 0);
156 case GNUNET_CRYPTO_QUALITY_STRONG:
157 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
158 #ifdef gcry_fast_random_poll
159 if ((invokeCount++ % 256) == 0)
160 gcry_fast_random_poll ();
162 ul = UINT32_MAX - (UINT32_MAX % i);
165 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t),
170 case GNUNET_CRYPTO_QUALITY_NONCE:
171 ul = UINT32_MAX - (UINT32_MAX % i);
174 gcry_create_nonce (&ret, sizeof (ret));
178 case GNUNET_CRYPTO_QUALITY_WEAK:
179 ret = i * get_weak_random ();
191 * Get an array with a random permutation of the
193 * @param mode #GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
194 * PRNG should be used, #GNUNET_RANDOM_QUALITY_WEAK otherwise
195 * @param n the size of the array
196 * @return the permutation array (allocated from heap)
199 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
207 GNUNET_assert (n > 0);
208 ret = GNUNET_malloc (n * sizeof (unsigned int));
209 for (i = 0; i < n; i++)
211 for (i = n - 1; i > 0; i--)
213 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
223 * Generate random unsigned 64-bit value.
225 * @param mode desired quality of the random number
226 * @param max value returned will be in range [0,max) (exclusive)
227 * @return random 64-bit number
230 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
235 GNUNET_assert (max > 0);
238 case GNUNET_CRYPTO_QUALITY_STRONG:
239 ul = UINT64_MAX - (UINT64_MAX % max);
242 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
247 case GNUNET_CRYPTO_QUALITY_NONCE:
248 ul = UINT64_MAX - (UINT64_MAX % max);
251 gcry_create_nonce (&ret, sizeof (ret));
256 case GNUNET_CRYPTO_QUALITY_WEAK:
257 ret = max * get_weak_random ();
269 * Initialize libgcrypt.
271 void __attribute__ ((constructor))
272 GNUNET_CRYPTO_random_init ()
276 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
279 _("libgcrypt has not the expected version (version %s is required).\n"),
280 NEED_LIBGCRYPT_VERSION);
283 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
285 "Failed to set libgcrypt option %s: %s\n",
288 /* Otherwise gnunet-ecc takes forever to complete, besides
289 we are fine with "just" using GCRY_STRONG_RANDOM */
290 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
292 "Failed to set libgcrypt option %s: %s\n",
293 "ENABLE_QUICK_RANDOM",
295 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
296 gcry_fast_random_poll ();
297 GNUNET_CRYPTO_seed_weak_random (time (NULL) ^
298 GNUNET_CRYPTO_random_u32
299 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
304 * Nicely shut down libgcrypt.
306 void __attribute__ ((destructor))
307 GNUNET_CRYPTO_random_fini ()
309 gcry_set_progress_handler (NULL, NULL);
310 #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
311 (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
317 /* end of crypto_random.c */