2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2019 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/crypto_pow.c
22 * @brief proof-of-work hashing
23 * @author Christian Grothoff
27 #include "gnunet_crypto_lib.h"
31 * Calculate the 'proof-of-work' hash (an expensive hash).
32 * We're using a non-standard formula to avoid issues with
33 * ASICs appearing (see #3795).
35 * @param salt salt for the hash
36 * @param buf data to hash
37 * @param buf_len number of bytes in @a buf
38 * @param result where to write the resulting hash
41 GNUNET_CRYPTO_pow_hash (const char *salt,
44 struct GNUNET_HashCode *result)
47 char twofish_iv[128 / 8]; //128 bit IV
48 char twofish_key[256 / 8]; //256 bit Key
51 gcry_cipher_hd_t handle;
53 GNUNET_break (0 == gcry_kdf_derive (buf,
59 2 /* iterations; keep cost of individual op small */,
63 GNUNET_CRYPTO_kdf (twofish_iv,
65 "gnunet-proof-of-work-iv",
66 strlen ("gnunet-proof-of-work-iv"),
73 gcry_cipher_open (&handle, GCRY_CIPHER_TWOFISH,
74 GCRY_CIPHER_MODE_CFB, 0));
75 rc = gcry_cipher_setkey (handle,
78 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
79 rc = gcry_cipher_setiv (handle,
82 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
83 GNUNET_assert (0 == gcry_cipher_encrypt (handle, &rbuf, buf_len, buf, buf_len));
84 gcry_cipher_close (handle);
86 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
87 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
90 GNUNET_break (0 == gcry_kdf_derive (buf,
96 2 /* iterations; keep cost of individual op small */,
99 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
101 "gnunet-proof-of-work-iv",
102 strlen ("gnunet-proof-of-work-iv"),
106 GNUNET_CRYPTO_symmetric_encrypt (buf,
112 GNUNET_break (0 == gcry_kdf_derive (rbuf,
118 2 /* iterations; keep cost of individual op small */,
119 sizeof(struct GNUNET_HashCode),
124 /* end of crypto_pow.c */