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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
23 * @file util/crypto_random.c
24 * @brief functions to gather random numbers
25 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
36 /* TODO: ndurner, move this to plibc? */
37 /* The code is derived from glibc, obviously */
38 #if !HAVE_RANDOM || !HAVE_SRANDOM
45 #define RANDOM() glibc_weak_rand32()
46 #define SRANDOM(s) glibc_weak_srand32(s)
50 #define RAND_MAX 0x7fffffff /* Hopefully this is correct */
53 static int32_t glibc_weak_rand32_state = 1;
57 glibc_weak_srand32 (int32_t s)
59 glibc_weak_rand32_state = s;
66 int32_t val = glibc_weak_rand32_state;
68 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
69 glibc_weak_rand32_state = val;
75 * Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
77 * @return number between 0 and 1.
82 return ((double) RANDOM () / RAND_MAX);
87 * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
90 * @param seed the seed to use
93 GNUNET_CRYPTO_seed_weak_random (int32_t seed)
101 * Zero out @a buffer, securely against compiler optimizations.
102 * Used to delete key material.
104 * @param buffer the buffer to zap
105 * @param length buffer length
108 GNUNET_CRYPTO_zero_keys (void *buffer,
116 #elif HAVE_EXPLICIT_BZERO
117 explicit_bzero (buffer,
120 volatile unsigned char *p = buffer;
129 * Fill block with a random values.
131 * @param mode desired quality of the random number
132 * @param buffer the buffer to fill
133 * @param length buffer length
136 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
140 #ifdef gcry_fast_random_poll
141 static unsigned int invokeCount;
145 case GNUNET_CRYPTO_QUALITY_STRONG:
146 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
147 #ifdef gcry_fast_random_poll
148 if ((invokeCount++ % 256) == 0)
149 gcry_fast_random_poll ();
151 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
153 case GNUNET_CRYPTO_QUALITY_NONCE:
154 gcry_create_nonce (buffer, length);
156 case GNUNET_CRYPTO_QUALITY_WEAK:
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 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
171 * Produce a random unsigned 32-bit number modulo @a i.
173 * @param mode desired quality of the random number
174 * @param i the upper limit (exclusive) for the random number
175 * @return a random value in the interval [0,i[.
178 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
181 #ifdef gcry_fast_random_poll
182 static unsigned int invokeCount;
187 GNUNET_assert (i > 0);
191 case GNUNET_CRYPTO_QUALITY_STRONG:
192 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
193 #ifdef gcry_fast_random_poll
194 if ((invokeCount++ % 256) == 0)
195 gcry_fast_random_poll ();
197 ul = UINT32_MAX - (UINT32_MAX % i);
200 gcry_randomize ((unsigned char *) &ret, sizeof (uint32_t),
205 case GNUNET_CRYPTO_QUALITY_NONCE:
206 ul = UINT32_MAX - (UINT32_MAX % i);
209 gcry_create_nonce (&ret, sizeof (ret));
213 case GNUNET_CRYPTO_QUALITY_WEAK:
214 ret = i * get_weak_random ();
226 * Get an array with a random permutation of the
228 * @param mode #GNUNET_RANDOM_QUALITY_STRONG if the strong (but expensive)
229 * PRNG should be used, #GNUNET_RANDOM_QUALITY_WEAK otherwise
230 * @param n the size of the array
231 * @return the permutation array (allocated from heap)
234 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
242 GNUNET_assert (n > 0);
243 ret = GNUNET_malloc (n * sizeof (unsigned int));
244 for (i = 0; i < n; i++)
246 for (i = n - 1; i > 0; i--)
248 x = GNUNET_CRYPTO_random_u32 (mode, i + 1);
258 * Generate random unsigned 64-bit value.
260 * @param mode desired quality of the random number
261 * @param max value returned will be in range [0,max) (exclusive)
262 * @return random 64-bit number
265 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
271 GNUNET_assert (max > 0);
274 case GNUNET_CRYPTO_QUALITY_STRONG:
275 ul = UINT64_MAX - (UINT64_MAX % max);
278 gcry_randomize ((unsigned char *) &ret, sizeof (uint64_t),
283 case GNUNET_CRYPTO_QUALITY_NONCE:
284 ul = UINT64_MAX - (UINT64_MAX % max);
287 gcry_create_nonce (&ret, sizeof (ret));
292 case GNUNET_CRYPTO_QUALITY_WEAK:
293 ret = max * get_weak_random ();
305 * Allocation wrapper for libgcrypt, used to avoid bad locking
306 * strategy of libgcrypt implementation.
311 return calloc (n, 1);
316 * Allocation wrapper for libgcrypt, used to avoid bad locking
317 * strategy of libgcrypt implementation.
320 w_check (const void *p)
322 return 0; /* not secure memory */
327 * Initialize libgcrypt.
329 void __attribute__ ((constructor))
330 GNUNET_CRYPTO_random_init ()
334 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
337 _("libgcrypt has not the expected version (version %s is required).\n"),
338 NEED_LIBGCRYPT_VERSION);
341 /* set custom allocators */
342 gcry_set_allocation_handler (&w_malloc,
347 /* Disable use of secure memory */
348 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
350 "Failed to set libgcrypt option %s: %s\n",
353 /* Otherwise gnunet-ecc takes forever to complete, besides
354 we are fine with "just" using GCRY_STRONG_RANDOM */
355 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
357 "Failed to set libgcrypt option %s: %s\n",
358 "ENABLE_QUICK_RANDOM",
360 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
361 gcry_fast_random_poll ();
362 GNUNET_CRYPTO_seed_weak_random (time (NULL) ^
363 GNUNET_CRYPTO_random_u32
364 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
369 * Nicely shut down libgcrypt.
371 void __attribute__ ((destructor))
372 GNUNET_CRYPTO_random_fini ()
374 gcry_set_progress_handler (NULL, NULL);
375 #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
376 (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
382 /* end of crypto_random.c */