tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / crypto_kdf.c
index c760ba33acbe871cce03a53f44916be98c12b013..ac68ed7f136862b173d81b49f15485734208383f 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      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
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -30,7 +30,7 @@
 #include "platform.h"
 #include "gnunet_crypto_lib.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-kdf", __VA_ARGS__)
 
 /**
  * @brief Derive key
  * @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)
 {
   /*
@@ -61,8 +64,15 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len,
    * http://eprint.iacr.org/2010/264
    */
 
-  return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, GCRY_MD_SHA256,
-                               xts, xts_len, skm, skm_len, argp);
+  return GNUNET_CRYPTO_hkdf_v (result,
+                               out_len,
+                               GCRY_MD_SHA512,
+                               GCRY_MD_SHA256,
+                               xts,
+                               xts_len,
+                               skm,
+                               skm_len,
+                               argp);
 }
 
 
@@ -78,15 +88,24 @@ GNUNET_CRYPTO_kdf_v (void *result, size_t out_len,
  * @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;
 
   va_start (argp, skm_len);
-  ret = GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp);
+  ret = GNUNET_CRYPTO_kdf_v (result,
+                             out_len,
+                             xts,
+                             xts_len,
+                             skm,
+                             skm_len,
+                             argp);
   va_end (argp);
 
   return ret;
@@ -108,8 +127,8 @@ GNUNET_CRYPTO_kdf (void *result, size_t out_len,
 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 void *xts, size_t xts_len,
+                           const void *skm, size_t skm_len,
                            const char *ctx)
 {
   gcry_error_t rc;
@@ -121,7 +140,8 @@ GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
   /* GNUNET_assert (nbits > 512); */
 
   ctr = 0;
-  do {
+  while (1)
+  {
     /* Ain't clear if n is always divisible by 8 */
     uint8_t buf[ (nbits-1)/8 + 1 ];
 
@@ -145,7 +165,10 @@ GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
     GNUNET_assert( 0 == gcry_mpi_test_bit (*r, nbits) );
     ++ctr;
     /* We reject this FDH if either *r > n and retry with another ctr */
-  } while ( 0 <= gcry_mpi_cmp(*r,n) );
+    if (0 > gcry_mpi_cmp(*r, n))
+      break;
+    gcry_mpi_release (*r);
+  }
 }
 
-
+/* end of crypto_kdf.c */