gnunet

Main GNUnet Logic
Log | Files | Refs | Submodules | README | LICENSE

commit 4f261de996daf0c2100eeb19cf3201f8e76930fc
parent 07b82363864a0b477e0959512b73961406aef90f
Author: Matthias Wachs <wachs@net.in.tum.de>
Date:   Tue, 29 Oct 2013 16:41:02 +0000

function to fill buffer with random values


Diffstat:
Msrc/include/gnunet_crypto_lib.h | 11+++++++++++
Msrc/util/crypto_random.c | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)

diff --git 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); /** * @ingroup crypto + * Fill block with a random values. + * + * @param mode desired quality of the random number + * @param buffer the buffer to fill + * @param length buffer length + */ +void +GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length); + +/** + * @ingroup crypto * Produce a random value. * * @param mode desired quality of the random number diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c @@ -95,6 +95,47 @@ GNUNET_CRYPTO_seed_weak_random (int32_t seed) SRANDOM (seed); } +/** + * @ingroup crypto + * Fill block with a random values. + * + * @param mode desired quality of the random number + * @param buffer the buffer to fill + * @param length buffer length + */ +void +GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length) +{ +#ifdef gcry_fast_random_poll + static unsigned int invokeCount; +#endif + switch (mode) + { + case GNUNET_CRYPTO_QUALITY_STRONG: + /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ +#ifdef gcry_fast_random_poll + if ((invokeCount++ % 256) == 0) + gcry_fast_random_poll (); +#endif + gcry_randomize (buffer, length, GCRY_STRONG_RANDOM); + return; + case GNUNET_CRYPTO_QUALITY_NONCE: + gcry_create_nonce (buffer, length); + return; + case GNUNET_CRYPTO_QUALITY_WEAK: + /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */ +#ifdef gcry_fast_random_poll + if ((invokeCount++ % 256) == 0) + gcry_fast_random_poll (); +#endif + gcry_randomize (buffer, length, GCRY_WEAK_RANDOM); + return; + return; + default: + GNUNET_assert (0); + } +} + /** * Produce a random value.