6176afc33739363bfe427c44deecfeb22e43c4b7
[oweals/gnunet.git] / src / util / crypto_pow.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013, 2019 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file util/crypto_pow.c
22  * @brief proof-of-work hashing
23  * @author Christian Grothoff
24  * @author Bart Polot
25  */
26 #include "platform.h"
27 #include "gnunet_crypto_lib.h"
28 #include <argon2.h>
29
30 /**
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).
34  *
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
39  */
40 void
41 GNUNET_CRYPTO_pow_hash (const char *salt,
42                         const void *buf,
43                         size_t buf_len,
44                         struct GNUNET_HashCode *result)
45 {
46   GNUNET_break (ARGON2_OK ==
47                 argon2id_hash_raw (3, /* iterations */
48                                    1024,              /* memory (1 MiB) */
49                                    1,              /* threads */
50                                    buf,
51                                    buf_len,
52                                    salt,
53                                    strlen (salt),
54                                    result,
55                                    sizeof (struct GNUNET_HashCode)));
56 }
57
58
59 /* end of crypto_pow.c */