X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fcrypto_kdf.c;h=b62b5c8478fd1168c4a747d26785c6b0d6855bab;hb=4707789ebfb4cef9672db31e3ceb8f98381901d0;hp=64c2cf408a10deb720d6ea2ef282b19993b329b5;hpb=f27338992f0a5915ee974faea05f764c2df6f584;p=oweals%2Fgnunet.git diff --git a/src/util/crypto_kdf.c b/src/util/crypto_kdf.c index 64c2cf408..b62b5c847 100644 --- a/src/util/crypto_kdf.c +++ b/src/util/crypto_kdf.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2010 Christian Grothoff (and other contributing authors) + Copyright (C) 2010 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -22,6 +22,7 @@ * @file src/util/crypto_kdf.c * @brief Key derivation * @author Nils Durner + * @author Jeffrey Burdges */ #include @@ -43,8 +44,9 @@ * @return #GNUNET_YES on success */ int -GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, - size_t xts_len, const void *skm, size_t skm_len, +GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, + const void *xts, size_t xts_len, + const void *skm, size_t skm_len, va_list argp) { /* @@ -76,8 +78,9 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, * @return #GNUNET_YES on success */ int -GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, - size_t xts_len, const void *skm, size_t skm_len, ...) +GNUNET_CRYPTO_kdf (void *result, size_t out_len, + const void *xts, size_t xts_len, + const void *skm, size_t skm_len, ...) { va_list argp; int ret; @@ -88,3 +91,63 @@ GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, return ret; } + + +/** + * Deterministically generate a pseudo-random number uniformly from the + * integers modulo a libgcrypt mpi. + * + * @param[out] r MPI value set to the FDH + * @param n MPI to work modulo + * @param xts salt + * @param xts_len length of @a xts + * @param skm source key material + * @param skm_len length of @a skm + * @param ctx context string + */ +void +GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r, + gcry_mpi_t n, + const void *xts, size_t xts_len, + const void *skm, size_t skm_len, + const char *ctx) +{ + gcry_error_t rc; + unsigned int nbits; + size_t rsize; + unsigned int ctr; + + nbits = gcry_mpi_get_nbits (n); + /* GNUNET_assert (nbits > 512); */ + + ctr = 0; + while (1) + { + /* Ain't clear if n is always divisible by 8 */ + uint8_t buf[ (nbits-1)/8 + 1 ]; + + rc = GNUNET_CRYPTO_kdf (buf, + sizeof (buf), + xts, xts_len, + skm, skm_len, + ctx, strlen(ctx), + &ctr, sizeof(ctr), + NULL, 0); + GNUNET_assert (GNUNET_YES == rc); + + rc = gcry_mpi_scan (r, + GCRYMPI_FMT_USG, + (const unsigned char *) buf, + sizeof (buf), + &rsize); + GNUNET_assert (0 == rc); /* Allocation erro? */ + + gcry_mpi_clear_highbit (*r, nbits); + GNUNET_assert( 0 == gcry_mpi_test_bit (*r, nbits) ); + ++ctr; + /* We reject this FDH if either *r > n and retry with another ctr */ + if (0 > gcry_mpi_cmp(*r, n)) + break; + gcry_mpi_release (*r); + } +}