aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-10-29 16:41:02 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-10-29 16:41:02 +0000
commit4f261de996daf0c2100eeb19cf3201f8e76930fc (patch)
tree7020963258efefab320613de0537d04dd4758d04
parent07b82363864a0b477e0959512b73961406aef90f (diff)
downloadgnunet-4f261de996daf0c2100eeb19cf3201f8e76930fc.tar.gz
gnunet-4f261de996daf0c2100eeb19cf3201f8e76930fc.zip
function to fill buffer with random values
-rw-r--r--src/include/gnunet_crypto_lib.h11
-rw-r--r--src/util/crypto_random.c41
2 files changed, 52 insertions, 0 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index da4de4c87..980710b19 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -410,6 +410,17 @@ GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
410 410
411/** 411/**
412 * @ingroup crypto 412 * @ingroup crypto
413 * Fill block with a random values.
414 *
415 * @param mode desired quality of the random number
416 * @param buffer the buffer to fill
417 * @param length buffer length
418 */
419void
420GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length);
421
422/**
423 * @ingroup crypto
413 * Produce a random value. 424 * Produce a random value.
414 * 425 *
415 * @param mode desired quality of the random number 426 * @param mode desired quality of the random number
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index e66c3c71b..504741200 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -95,6 +95,47 @@ GNUNET_CRYPTO_seed_weak_random (int32_t seed)
95 SRANDOM (seed); 95 SRANDOM (seed);
96} 96}
97 97
98/**
99 * @ingroup crypto
100 * Fill block with a random values.
101 *
102 * @param mode desired quality of the random number
103 * @param buffer the buffer to fill
104 * @param length buffer length
105 */
106void
107GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
108{
109#ifdef gcry_fast_random_poll
110 static unsigned int invokeCount;
111#endif
112 switch (mode)
113 {
114 case GNUNET_CRYPTO_QUALITY_STRONG:
115 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
116#ifdef gcry_fast_random_poll
117 if ((invokeCount++ % 256) == 0)
118 gcry_fast_random_poll ();
119#endif
120 gcry_randomize (buffer, length, GCRY_STRONG_RANDOM);
121 return;
122 case GNUNET_CRYPTO_QUALITY_NONCE:
123 gcry_create_nonce (buffer, length);
124 return;
125 case GNUNET_CRYPTO_QUALITY_WEAK:
126 /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
127#ifdef gcry_fast_random_poll
128 if ((invokeCount++ % 256) == 0)
129 gcry_fast_random_poll ();
130#endif
131 gcry_randomize (buffer, length, GCRY_WEAK_RANDOM);
132 return;
133 return;
134 default:
135 GNUNET_assert (0);
136 }
137}
138
98 139
99/** 140/**
100 * Produce a random value. 141 * Produce a random value.