GNUnet  0.10.x
Macros | Functions | Variables
crypto_random.c File Reference

functions to gather random numbers More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include <gcrypt.h>
Include dependency graph for crypto_random.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
 
#define RANDOM()   glibc_weak_rand32 ()
 
#define SRANDOM(s)   glibc_weak_srand32 (s)
 
#define RAND_MAX   0x7fffffff /* Hopefully this is correct */
 

Functions

void glibc_weak_srand32 (int32_t s)
 
int32_t glibc_weak_rand32 ()
 
static double get_weak_random ()
 Create a cryptographically weak pseudo-random number in the interval of 0 to 1. More...
 
void GNUNET_CRYPTO_seed_weak_random (int32_t seed)
 Seed a weak random generator. More...
 
void GNUNET_CRYPTO_zero_keys (void *buffer, size_t length)
 Zero out buffer, securely against compiler optimizations. More...
 
void GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
 Fill block with a random values. More...
 
uint32_t GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
 Produce a random unsigned 32-bit number modulo i. More...
 
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. More...
 
uint64_t GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
 Generate random unsigned 64-bit value. More...
 
static void * w_malloc (size_t n)
 Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation. More...
 
static int w_check (const void *p)
 Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation. More...
 
void __attribute__ ((constructor))
 Initialize libgcrypt. More...
 
void __attribute__ ((destructor))
 Nicely shut down libgcrypt. More...
 

Variables

static int32_t glibc_weak_rand32_state = 1
 

Detailed Description

functions to gather random numbers

Author
Christian Grothoff

Definition in file crypto_random.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)

Definition at line 31 of file crypto_random.c.

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)

Definition at line 33 of file crypto_random.c.

◆ RANDOM

#define RANDOM ( )    glibc_weak_rand32 ()

Definition at line 46 of file crypto_random.c.

Referenced by get_weak_random().

◆ SRANDOM

#define SRANDOM (   s)    glibc_weak_srand32 (s)

Definition at line 47 of file crypto_random.c.

Referenced by GNUNET_CRYPTO_seed_weak_random().

◆ RAND_MAX

#define RAND_MAX   0x7fffffff /* Hopefully this is correct */

Definition at line 51 of file crypto_random.c.

Referenced by get_weak_random().

Function Documentation

◆ glibc_weak_srand32()

void glibc_weak_srand32 ( int32_t  s)

Definition at line 58 of file crypto_random.c.

References glibc_weak_rand32_state.

59 {
61 }
static int32_t glibc_weak_rand32_state
Definition: crypto_random.c:54

◆ glibc_weak_rand32()

int32_t glibc_weak_rand32 ( )

Definition at line 65 of file crypto_random.c.

References glibc_weak_rand32_state.

66 {
67  int32_t val = glibc_weak_rand32_state;
68 
69  val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
71  return val;
72 }
static int32_t glibc_weak_rand32_state
Definition: crypto_random.c:54

◆ get_weak_random()

static double get_weak_random ( )
static

Create a cryptographically weak pseudo-random number in the interval of 0 to 1.

Returns
number between 0 and 1.

Definition at line 81 of file crypto_random.c.

References RAND_MAX, and RANDOM.

Referenced by GNUNET_CRYPTO_random_u32(), and GNUNET_CRYPTO_random_u64().

82 {
83  return ((double) RANDOM () / RAND_MAX);
84 }
#define RANDOM()
Definition: crypto_random.c:46
#define RAND_MAX
Definition: crypto_random.c:51
Here is the caller graph for this function:

◆ w_malloc()

static void* w_malloc ( size_t  n)
static

Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.

Definition at line 300 of file crypto_random.c.

Referenced by __attribute__().

301 {
302  return calloc (n, 1);
303 }
Here is the caller graph for this function:

◆ w_check()

static int w_check ( const void *  p)
static

Allocation wrapper for libgcrypt, used to avoid bad locking strategy of libgcrypt implementation.

Definition at line 311 of file crypto_random.c.

Referenced by __attribute__().

312 {
313  (void) p;
314  return 0; /* not secure memory */
315 }
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
Here is the caller graph for this function:

◆ __attribute__() [1/2]

void __attribute__ ( (constructor)  )

Initialize libgcrypt.

Definition at line 321 of file crypto_random.c.

References _, FPRINTF, GNUNET_assert, GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_u32(), GNUNET_CRYPTO_seed_weak_random(), w_check(), and w_malloc().

322 {
323  gcry_error_t rc;
324 
325  if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
326  {
327  FPRINTF (
328  stderr,
329  _ ("libgcrypt has not the expected version (version %s is required).\n"),
330  NEED_LIBGCRYPT_VERSION);
331  GNUNET_assert (0);
332  }
333  /* set custom allocators */
334  gcry_set_allocation_handler (&w_malloc, &w_malloc, &w_check, &realloc, &free);
335  /* Disable use of secure memory */
336  if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
337  FPRINTF (stderr,
338  "Failed to set libgcrypt option %s: %s\n",
339  "DISABLE_SECMEM",
340  gcry_strerror (rc));
341  /* Otherwise gnunet-ecc takes forever to complete, besides
342  we are fine with "just" using GCRY_STRONG_RANDOM */
343  if ((rc = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0)))
344  FPRINTF (stderr,
345  "Failed to set libgcrypt option %s: %s\n",
346  "ENABLE_QUICK_RANDOM",
347  gcry_strerror (rc));
348  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
349  gcry_fast_random_poll ();
351  time (NULL) ^
353 }
#define FPRINTF
Definition: plibc.h:683
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...
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random unsigned 32-bit number modulo i.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
Randomness for IVs etc.
void GNUNET_CRYPTO_seed_weak_random(int32_t seed)
Seed a weak random generator.
Definition: crypto_random.c:94
Here is the call graph for this function:

◆ __attribute__() [2/2]

void __attribute__ ( (destructor)  )

Nicely shut down libgcrypt.

Definition at line 359 of file crypto_random.c.

360 {
361  gcry_set_progress_handler (NULL, NULL);
362 #ifdef GCRYCTL_CLOSE_RANDOM_DEVICE
363  (void) gcry_control (GCRYCTL_CLOSE_RANDOM_DEVICE, 0);
364 #endif
365 }

Variable Documentation

◆ glibc_weak_rand32_state

int32_t glibc_weak_rand32_state = 1
static

Definition at line 54 of file crypto_random.c.

Referenced by glibc_weak_rand32(), and glibc_weak_srand32().