GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
crypto_random.c File Reference

functions to gather random numbers More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include <sodium.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 ()
 
void GNUNET_CRYPTO_zero_keys (void *buffer, size_t length)
 Zero out buffer, securely against compiler optimizations.
 
void GNUNET_CRYPTO_random_block (void *buffer, size_t length)
 Fill block with a random values.
 
uint32_t GNUNET_CRYPTO_random_u32 (uint32_t max)
 Produce a random value.
 
unsigned int * GNUNET_CRYPTO_random_permute (unsigned int n)
 Get an array with a random permutation of the numbers 0...n-1.
 
uint64_t GNUNET_CRYPTO_random_u64 (uint64_t max)
 Generate a random unsigned 64-bit value.
 
void GNUNET_CRYPTO_random_timeflake (struct GNUNET_Uuid *uuid)
 Fill UUID with a timeflake pseudo-random value.
 
void GNUNET_CRYPTO_random_init (void)
 
void __attribute__ ((constructor))
 Initialize sodium.
 

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 32 of file crypto_random.c.

◆ LOG_STRERROR

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

Definition at line 34 of file crypto_random.c.

58{
60}
61
62
63int32_t
65{
66 int32_t val = glibc_weak_rand32_state;
67
68 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
70 return val;
71}
72
73
74#endif
75
76
85void
86GNUNET_CRYPTO_zero_keys (void *buffer, size_t length)
87{
88#if HAVE_MEMSET_S
89 memset_s (buffer, length, 0, length);
90#elif HAVE_EXPLICIT_BZERO
91 explicit_bzero (buffer, length);
92#else
93 volatile unsigned char *p = buffer;
94 while (length--)
95 *p++ = 0;
96#endif
97}
98
99
100void
101GNUNET_CRYPTO_random_block (void *buffer,
102 size_t length)
103{
104 randombytes_buf (buffer,
105 length);
106}
107
108
109uint32_t
111{
112 GNUNET_assert (max > 0);
113
114 return randombytes_uniform (max);
115}
116
117
118unsigned int *
119GNUNET_CRYPTO_random_permute (unsigned int n)
120{
121 unsigned int *ret;
122 unsigned int i;
123 unsigned int tmp;
124 uint32_t x;
125
126 GNUNET_assert (n > 0);
127 ret = GNUNET_malloc (n * sizeof(unsigned int));
128 for (i = 0; i < n; i++)
129 ret[i] = i;
130 for (i = n - 1; i > 0; i--)
131 {
132 x = GNUNET_CRYPTO_random_u32 (i + 1);
133 tmp = ret[x];
134 ret[x] = ret[i];
135 ret[i] = tmp;
136 }
137 return ret;
138}
139
140
141uint64_t
143{
144 GNUNET_assert (max > 0);
145 return randombytes_uniform (max);
146}
147
148
149void
151{
152 struct GNUNET_TIME_Absolute now;
153 uint64_t ms;
154 uint64_t be;
155 char *base;
156
158 sizeof (struct GNUNET_Uuid));
160 ms = now.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
161 be = GNUNET_htonll (ms);
162 base = (char *) &be;
163 memcpy (uuid,
164 base + 2,
165 sizeof (be) - 2);
166}
167
168
169void
171
175void __attribute__ ((constructor))
177{
178 GNUNET_assert (-1 != sodium_init ());
179}
180
181
182/* end of crypto_random.c */
static int32_t glibc_weak_rand32_state
int32_t glibc_weak_rand32()
void GNUNET_CRYPTO_random_init(void)
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_Process * p
Helper process we started.
Definition gnunet-uri.c:38
struct GNUNET_PQ_ResultSpec __attribute__
uint64_t GNUNET_CRYPTO_random_u64(uint64_t max)
Generate a random unsigned 64-bit value.
void GNUNET_CRYPTO_random_block(void *buffer, size_t length)
Fill block with a random values.
unsigned int * GNUNET_CRYPTO_random_permute(unsigned int n)
Get an array with a random permutation of the numbers 0...n-1.
void GNUNET_CRYPTO_random_timeflake(struct GNUNET_Uuid *uuid)
Fill UUID with a timeflake pseudo-random value.
void GNUNET_CRYPTO_zero_keys(void *buffer, size_t length)
Zero out buffer, securely against compiler optimizations.
uint32_t GNUNET_CRYPTO_random_u32(uint32_t max)
Produce a random value.
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.
Definition time.c:111
#define max(x, y)
Time for absolute times used by GNUnet, in microseconds.
A UUID, a 128 bit "random" value.

◆ RANDOM

#define RANDOM ( )    glibc_weak_rand32 ()

Definition at line 47 of file crypto_random.c.

◆ SRANDOM

#define SRANDOM (   s)    glibc_weak_srand32 (s)

Definition at line 48 of file crypto_random.c.

◆ RAND_MAX

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

Definition at line 52 of file crypto_random.c.

Function Documentation

◆ glibc_weak_srand32()

void glibc_weak_srand32 ( int32_t  s)

Definition at line 58 of file crypto_random.c.

59{
61}

References glibc_weak_rand32_state.

◆ glibc_weak_rand32()

int32_t glibc_weak_rand32 ( )

Definition at line 65 of file crypto_random.c.

66{
67 int32_t val = glibc_weak_rand32_state;
68
69 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
71 return val;
72}

References glibc_weak_rand32_state.

◆ GNUNET_CRYPTO_random_init()

void GNUNET_CRYPTO_random_init ( void  )

◆ __attribute__()

void __attribute__ ( (constructor)  )

Initialize sodium.

Definition at line 176 of file crypto_random.c.

178{
179 GNUNET_assert (-1 != sodium_init ());
180}

References GNUNET_assert.

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().