GNUnet 0.28.1-dev.7-1-g2ce91c818
 
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 uint64_t value;
145 uint64_t threshold;
146
147 GNUNET_assert (max > 0);
148 /* randombytes_uniform() only supports 32-bit bounds. Reject the
149 incomplete residue range before reducing a full 64-bit value modulo
150 max, so every possible result has the same probability. */
151 /* threshold = 2^{64} \bmod max */
152 threshold = (-max) % max;
153 do
154 {
155 randombytes_buf (&value,
156 sizeof (value));
157 }
158 while (value < threshold);
159 return value % max;
160}
161
162
163void
165{
166 struct GNUNET_TIME_Absolute now;
167 uint64_t ms;
168 uint64_t be;
169 char *base;
170
172 sizeof (struct GNUNET_Uuid));
174 ms = now.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
175 be = GNUNET_htonll (ms);
176 base = (char *) &be;
177 memcpy (uuid,
178 base + 2,
179 sizeof (be) - 2);
180}
181
182
183void
185
189void __attribute__ ((constructor))
191{
192 GNUNET_assert (-1 != sodium_init ());
193}
194
195
196/* 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 char * value
Value of the record to add/remove.
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 190 of file crypto_random.c.

192{
193 GNUNET_assert (-1 != sodium_init ());
194}

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