implement new simplified POW for LSD001
authorSchanzenbach, Martin <mschanzenbach@posteo.de>
Sun, 15 Mar 2020 14:18:53 +0000 (15:18 +0100)
committerSchanzenbach, Martin <mschanzenbach@posteo.de>
Sun, 15 Mar 2020 14:18:53 +0000 (15:18 +0100)
src/util/crypto_pow.c

index be575e537183ceb67e025c251b157b6688bf142e..9b20ab345f05232ad7d42b33618aab91a926f6f5 100644 (file)
@@ -27,7 +27,6 @@
 #include "gnunet_crypto_lib.h"
 #include <gcrypt.h>
 
-
 /**
  * Calculate the 'proof-of-work' hash (an expensive hash).
  * We're using a non-standard formula to avoid issues with
@@ -44,6 +43,46 @@ GNUNET_CRYPTO_pow_hash (const char *salt,
                         size_t buf_len,
                         struct GNUNET_HashCode *result)
 {
+#ifdef LSD001
+  char twofish_iv[128 / 8]; //128 bit IV
+  char twofish_key[256 / 8]; //256 bit Key
+  char rbuf[buf_len];
+  int rc;
+  gcry_cipher_hd_t handle;
+
+  GNUNET_break (0 == gcry_kdf_derive (buf,
+                                      buf_len,
+                                      GCRY_KDF_SCRYPT,
+                                      1 /* subalgo */,
+                                      salt,
+                                      strlen (salt),
+                                      2 /* iterations; keep cost of individual op small */,
+                                      sizeof(twofish_key),
+                                      &twofish_key));
+
+  GNUNET_CRYPTO_kdf (twofish_iv,
+                     sizeof (twofish_iv),
+                     "gnunet-proof-of-work-iv",
+                     strlen ("gnunet-proof-of-work-iv"),
+                     twofish_key,
+                     sizeof(twofish_key),
+                     salt,
+                     strlen (salt),
+                     NULL, 0);
+  GNUNET_assert (0 ==
+                 gcry_cipher_open (&handle, GCRY_CIPHER_TWOFISH,
+                                   GCRY_CIPHER_MODE_CFB, 0));
+  rc = gcry_cipher_setkey (handle,
+                           twofish_key,
+                           sizeof(twofish_key));
+  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
+  rc = gcry_cipher_setiv (handle,
+                          twofish_iv,
+                          sizeof(twofish_iv));
+  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
+  GNUNET_assert (0 == gcry_cipher_encrypt (handle, &rbuf, buf_len, buf, buf_len));
+  gcry_cipher_close (handle);
+#else
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
   char rbuf[buf_len];
@@ -69,6 +108,7 @@ GNUNET_CRYPTO_pow_hash (const char *salt,
                                    &skey,
                                    &iv,
                                    &rbuf);
+#endif
   GNUNET_break (0 == gcry_kdf_derive (rbuf,
                                       buf_len,
                                       GCRY_KDF_SCRYPT,