GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
crypto_random.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet. Copyright (C) 2001-2014 Christian Grothoff
3 (and other contributing authors)
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19
20 */
21
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include <sodium.h>
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-random", __VA_ARGS__)
33
34#define LOG_STRERROR(kind, syscall) \
35 GNUNET_log_from_strerror (kind, "util-crypto-random", syscall)
36
37
38/* TODO: ndurner, move this to plibc? */
39/* The code is derived from glibc, obviously */
40#if ! HAVE_RANDOM || ! HAVE_SRANDOM
41#ifdef RANDOM
42#undef RANDOM
43#endif
44#ifdef SRANDOM
45#undef SRANDOM
46#endif
47#define RANDOM() glibc_weak_rand32 ()
48#define SRANDOM(s) glibc_weak_srand32 (s)
49#if defined(RAND_MAX)
50#undef RAND_MAX
51#endif
52#define RAND_MAX 0x7fffffff /* Hopefully this is correct */
53
54static int32_t glibc_weak_rand32_state = 1;
55
56
57void
59{
61}
62
63
64int32_t
66{
67 int32_t val = glibc_weak_rand32_state;
68
69 val = ((glibc_weak_rand32_state * 1103515245) + 12345) & 0x7fffffff;
71 return val;
72}
73
74
75#endif
76
77
86void
87GNUNET_CRYPTO_zero_keys (void *buffer, size_t length)
88{
89#if HAVE_MEMSET_S
90 memset_s (buffer, length, 0, length);
91#elif HAVE_EXPLICIT_BZERO
92 explicit_bzero (buffer, length);
93#else
94 volatile unsigned char *p = buffer;
95 while (length--)
96 *p++ = 0;
97#endif
98}
99
100
101void
103 size_t length)
104{
105 randombytes_buf (buffer,
106 length);
107}
108
109
110uint32_t
112{
113 GNUNET_assert (max > 0);
114
115 return randombytes_uniform (max);
116}
117
118
119unsigned int *
121{
122 unsigned int *ret;
123 unsigned int i;
124 unsigned int tmp;
125 uint32_t x;
126
127 GNUNET_assert (n > 0);
128 ret = GNUNET_malloc (n * sizeof(unsigned int));
129 for (i = 0; i < n; i++)
130 ret[i] = i;
131 for (i = n - 1; i > 0; i--)
132 {
133 x = GNUNET_CRYPTO_random_u32 (i + 1);
134 tmp = ret[x];
135 ret[x] = ret[i];
136 ret[i] = tmp;
137 }
138 return ret;
139}
140
141
142uint64_t
144{
145 GNUNET_assert (max > 0);
146 return randombytes_uniform (max);
147}
148
149
150void
152{
153 struct GNUNET_TIME_Absolute now;
154 uint64_t ms;
155 uint64_t be;
156 char *base;
157
159 sizeof (struct GNUNET_Uuid));
161 ms = now.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
162 be = GNUNET_htonll (ms);
163 base = (char *) &be;
164 memcpy (uuid,
165 base + 2,
166 sizeof (be) - 2);
167}
168
169
170void
172
176void __attribute__ ((constructor))
178{
179 GNUNET_assert (-1 != sodium_init ());
180}
181
182
183/* end of crypto_random.c */
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 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.
uint64_t abs_value_us
The actual value.
A UUID, a 128 bit "random" value.