aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_random.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-17 10:45:23 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-17 10:45:23 +0000
commit7e065c18499688141eb68513058131a49344cac1 (patch)
treee441b44c8f0db8a4f214775e4945039cc820cf2f /src/util/crypto_random.c
parentb3ad920b6e0107c3da946fe1f2f720955dbac151 (diff)
downloadgnunet-7e065c18499688141eb68513058131a49344cac1.tar.gz
gnunet-7e065c18499688141eb68513058131a49344cac1.zip
fixing #1551/#2503
Diffstat (limited to 'src/util/crypto_random.c')
-rw-r--r--src/util/crypto_random.c37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index dbf71d78a..8dce1080c 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors) 3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -34,6 +34,14 @@
34 34
35#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall) 35#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36 36
37
38/**
39 * GNUNET_YES if we are using a 'weak' (low-entropy) PRNG.
40 */
41static int weak_random;
42
43
44
37/* TODO: ndurner, move this to plibc? */ 45/* TODO: ndurner, move this to plibc? */
38/* The code is derived from glibc, obviously */ 46/* The code is derived from glibc, obviously */
39#if MINGW 47#if MINGW
@@ -49,14 +57,18 @@
49#undef RAND_MAX 57#undef RAND_MAX
50#endif 58#endif
51#define RAND_MAX 0x7fffffff /* Hopefully this is correct */ 59#define RAND_MAX 0x7fffffff /* Hopefully this is correct */
60
61
52static int32_t glibc_weak_rand32_state = 1; 62static int32_t glibc_weak_rand32_state = 1;
53 63
64
54void 65void
55glibc_weak_srand32 (int32_t s) 66glibc_weak_srand32 (int32_t s)
56{ 67{
57 glibc_weak_rand32_state = s; 68 glibc_weak_rand32_state = s;
58} 69}
59 70
71
60int32_t 72int32_t
61glibc_weak_rand32 () 73glibc_weak_rand32 ()
62{ 74{
@@ -74,11 +86,12 @@ glibc_weak_rand32 ()
74 * @return number between 0 and 1. 86 * @return number between 0 and 1.
75 */ 87 */
76static double 88static double
77weak_random () 89get_weak_random ()
78{ 90{
79 return ((double) RANDOM () / RAND_MAX); 91 return ((double) RANDOM () / RAND_MAX);
80} 92}
81 93
94
82/** 95/**
83 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator 96 * Seed a weak random generator. Only GNUNET_CRYPTO_QUALITY_WEAK-mode generator
84 * can be seeded. 97 * can be seeded.
@@ -91,6 +104,7 @@ GNUNET_CRYPTO_seed_weak_random (int32_t seed)
91 SRANDOM (seed); 104 SRANDOM (seed);
92} 105}
93 106
107
94/** 108/**
95 * Produce a random value. 109 * Produce a random value.
96 * 110 *
@@ -134,7 +148,7 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
134 while (ret >= ul); 148 while (ret >= ul);
135 return ret % i; 149 return ret % i;
136 case GNUNET_CRYPTO_QUALITY_WEAK: 150 case GNUNET_CRYPTO_QUALITY_WEAK:
137 ret = i * weak_random (); 151 ret = i * get_weak_random ();
138 if (ret >= i) 152 if (ret >= i)
139 ret = i - 1; 153 ret = i - 1;
140 return ret; 154 return ret;
@@ -211,7 +225,7 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
211 225
212 return ret % max; 226 return ret % max;
213 case GNUNET_CRYPTO_QUALITY_WEAK: 227 case GNUNET_CRYPTO_QUALITY_WEAK:
214 ret = max * weak_random (); 228 ret = max * get_weak_random ();
215 if (ret >= max) 229 if (ret >= max)
216 ret = max - 1; 230 ret = max - 1;
217 return ret; 231 return ret;
@@ -221,6 +235,19 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
221 return 0; 235 return 0;
222} 236}
223 237
238
239/**
240 * Check if we are using weak random number generation.
241 *
242 * @return GNUNET_YES if weak number generation is on
243 */
244int
245GNUNET_CRYPTO_random_is_weak ()
246{
247 return weak_random;
248}
249
250
224/** 251/**
225 * This function should only be called in testcases 252 * This function should only be called in testcases
226 * where strong entropy gathering is not desired 253 * where strong entropy gathering is not desired
@@ -229,6 +256,7 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
229void 256void
230GNUNET_CRYPTO_random_disable_entropy_gathering () 257GNUNET_CRYPTO_random_disable_entropy_gathering ()
231{ 258{
259 weak_random = GNUNET_YES;
232 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); 260 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
233} 261}
234 262
@@ -239,6 +267,7 @@ GNUNET_CRYPTO_random_disable_entropy_gathering ()
239 */ 267 */
240static struct GNUNET_OS_Process *genproc; 268static struct GNUNET_OS_Process *genproc;
241 269
270
242/** 271/**
243 * Function called by libgcrypt whenever we are 272 * Function called by libgcrypt whenever we are
244 * blocked gathering entropy. 273 * blocked gathering entropy.