32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
40#if ! HAVE_RANDOM || ! HAVE_SRANDOM
47#define RANDOM() glibc_weak_rand32 ()
48#define SRANDOM(s) glibc_weak_srand32 (s)
52#define RAND_MAX 0x7fffffff
86 return(random () / (
double)
RAND_MAX);
94 srandom_deterministic (
seed);
113 memset_s (buffer, length, 0, length);
114#elif HAVE_EXPLICIT_BZERO
115 explicit_bzero (buffer, length);
117 volatile unsigned char *
p = buffer;
137#ifdef gcry_fast_random_poll
138 static unsigned int invokeCount;
144#ifdef gcry_fast_random_poll
145 if ((invokeCount++ % 256) == 0)
146 gcry_fast_random_poll ();
148 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
152 gcry_create_nonce (buffer, length);
157#ifdef gcry_fast_random_poll
158 if ((invokeCount++ % 256) == 0)
159 gcry_fast_random_poll ();
161 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
181#ifdef gcry_fast_random_poll
182 static unsigned int invokeCount;
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,
208 ul = UINT32_MAX - (UINT32_MAX % i);
211 gcry_create_nonce (&
ret,
sizeof(
ret));
248 for (i = 0; i < n; i++)
250 for (i = n - 1; i > 0; i--)
272 ul = UINT64_MAX - (UINT64_MAX %
max);
275 gcry_randomize ((
unsigned char *) &
ret,
283 ul = UINT64_MAX - (UINT64_MAX %
max);
286 gcry_create_nonce (&
ret,
sizeof(
ret));
344 return calloc (n, 1);
370 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
374 _ (
"libgcrypt has not the expected version (version %s is required).\n"),
375 NEED_LIBGCRYPT_VERSION);
381 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
383 "Failed to set libgcrypt option %s: %s\n",
388 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
390 "Failed to set libgcrypt option %s: %s\n",
391 "ENABLE_QUICK_RANDOM",
393 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
394 gcry_fast_random_poll ();
409 gcry_set_progress_handler (NULL, NULL);
410#ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
411 (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
static int32_t glibc_weak_rand32_state
void glibc_weak_srand32(int32_t s)
int32_t glibc_weak_rand32()
void GNUNET_CRYPTO_random_init(void)
static double get_weak_random(void)
Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
void GNUNET_CRYPTO_random_fini(void)
static void * w_malloc(size_t n)
Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
static int w_check(const void *p)
Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
void __attribute__((constructor))
Initialize libgcrypt.
static int ret
Final status code.
static struct GNUNET_OS_Process * p
Helper process we started.
static enum @44 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
void GNUNET_CRYPTO_random_timeflake(enum GNUNET_CRYPTO_Quality mode, struct GNUNET_Uuid *uuid)
Fill UUID with a timeflake pseudo-random value.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
GNUNET_CRYPTO_Quality
Desired quality level for random numbers.
void GNUNET_CRYPTO_seed_weak_random(int32_t seed)
Seed a weak random generator.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random unsigned 32-bit number modulo i.
unsigned int * GNUNET_CRYPTO_random_permute(enum GNUNET_CRYPTO_Quality mode, unsigned int n)
Get an array with a random permutation of the numbers 0...n-1.
void GNUNET_CRYPTO_zero_keys(void *buffer, size_t length)
Zero out buffer, securely against compiler optimizations.
@ GNUNET_CRYPTO_QUALITY_STRONG
High-quality operations are desired.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
A UUID, a 128 bit "random" value.