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((
double) random () /
RAND_MAX);
100 srandom_deterministic (seed);
119 memset_s (buffer, length, 0, length);
120 #elif HAVE_EXPLICIT_BZERO
121 explicit_bzero (buffer, length);
123 volatile unsigned char *
p = buffer;
143 #ifdef gcry_fast_random_poll
144 static unsigned int invokeCount;
150 #ifdef gcry_fast_random_poll
151 if ((invokeCount++ % 256) == 0)
152 gcry_fast_random_poll ();
154 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
158 gcry_create_nonce (buffer, length);
163 #ifdef gcry_fast_random_poll
164 if ((invokeCount++ % 256) == 0)
165 gcry_fast_random_poll ();
167 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
187 #ifdef gcry_fast_random_poll
188 static unsigned int invokeCount;
199 #ifdef gcry_fast_random_poll
200 if ((invokeCount++ % 256) == 0)
201 gcry_fast_random_poll ();
203 ul = UINT32_MAX - (UINT32_MAX % i);
206 gcry_randomize ((
unsigned char *) &
ret,
214 ul = UINT32_MAX - (UINT32_MAX % i);
217 gcry_create_nonce (&
ret,
sizeof(
ret));
254 for (i = 0; i < n; i++)
256 for (i = n - 1; i > 0; i--)
285 ul = UINT64_MAX - (UINT64_MAX %
max);
288 gcry_randomize ((
unsigned char *) &
ret,
296 ul = UINT64_MAX - (UINT64_MAX %
max);
299 gcry_create_nonce (&
ret,
sizeof(
ret));
357 return calloc (n, 1);
377 GNUNET_CRYPTO_random_init ()
381 if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
385 _ (
"libgcrypt has not the expected version (version %s is required).\n"),
386 NEED_LIBGCRYPT_VERSION);
392 if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
394 "Failed to set libgcrypt option %s: %s\n",
399 if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
401 "Failed to set libgcrypt option %s: %s\n",
402 "ENABLE_QUICK_RANDOM",
404 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
405 gcry_fast_random_poll ();
416 GNUNET_CRYPTO_random_fini ()
418 gcry_set_progress_handler (NULL, NULL);
419 #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
420 (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()
static void * w_malloc(size_t n)
Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.
static double get_weak_random(void)
Create a cryptographically weak pseudo-random number in the interval of 0 to 1.
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
Return value of the commandline.
static struct GNUNET_OS_Process * p
Helper process we started.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
cryptographic primitives for GNUnet
static enum @8 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 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.
#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.