diff options
-rw-r--r-- | src/include/gnunet_crypto_lib.h | 13 | ||||
-rw-r--r-- | src/nse/gnunet-service-nse.c | 27 | ||||
-rw-r--r-- | src/util/Makefile.am | 1 | ||||
-rw-r--r-- | src/util/crypto_pow.c | 58 | ||||
-rw-r--r-- | src/util/gnunet-scrypt.c | 25 |
5 files changed, 75 insertions, 49 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 507705e50..4a42c5c74 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h | |||
@@ -655,6 +655,19 @@ GNUNET_CRYPTO_hash (const void *block, | |||
655 | 655 | ||
656 | 656 | ||
657 | /** | 657 | /** |
658 | * Calculate the 'proof-of-work' hash (an expensive hash). | ||
659 | * | ||
660 | * @param buf data to hash | ||
661 | * @param buf_len number of bytes in @a buf | ||
662 | * @param result where to write the resulting hash | ||
663 | */ | ||
664 | void | ||
665 | GNUNET_CRYPTO_pow_hash (const void *buf, | ||
666 | size_t buf_len, | ||
667 | struct GNUNET_HashCode *result); | ||
668 | |||
669 | |||
670 | /** | ||
658 | * Context for cummulative hashing. | 671 | * Context for cummulative hashing. |
659 | */ | 672 | */ |
660 | struct GNUNET_HashContext; | 673 | struct GNUNET_HashContext; |
diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c index 3e72be1c5..3f04314a6 100644 --- a/src/nse/gnunet-service-nse.c +++ b/src/nse/gnunet-service-nse.c | |||
@@ -488,29 +488,6 @@ get_delay_randomization (uint32_t matching_bits) | |||
488 | 488 | ||
489 | 489 | ||
490 | /** | 490 | /** |
491 | * Calculate the 'proof-of-work' hash (an expensive hash). | ||
492 | * | ||
493 | * @param buf data to hash | ||
494 | * @param buf_len number of bytes in @a buf | ||
495 | * @param result where to write the resulting hash | ||
496 | */ | ||
497 | static void | ||
498 | pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result) | ||
499 | { | ||
500 | GNUNET_break ( | ||
501 | 0 == gcry_kdf_derive (buf, | ||
502 | buf_len, | ||
503 | GCRY_KDF_SCRYPT, | ||
504 | 1 /* subalgo */, | ||
505 | "gnunet-proof-of-work", | ||
506 | strlen ("gnunet-proof-of-work"), | ||
507 | 2 /* iterations; keep cost of individual op small */, | ||
508 | sizeof(struct GNUNET_HashCode), | ||
509 | result)); | ||
510 | } | ||
511 | |||
512 | |||
513 | /** | ||
514 | * Get the number of matching bits that the given timestamp has to the given peer ID. | 491 | * Get the number of matching bits that the given timestamp has to the given peer ID. |
515 | * | 492 | * |
516 | * @param timestamp time to generate key | 493 | * @param timestamp time to generate key |
@@ -828,7 +805,7 @@ check_proof_of_work (const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, | |||
828 | GNUNET_memcpy (&buf[sizeof(val)], | 805 | GNUNET_memcpy (&buf[sizeof(val)], |
829 | pkey, | 806 | pkey, |
830 | sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)); | 807 | sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)); |
831 | pow_hash (buf, sizeof(buf), &result); | 808 | GNUNET_CRYPTO_pow_hash (buf, sizeof(buf), &result); |
832 | return (count_leading_zeroes (&result) >= nse_work_required) ? GNUNET_YES | 809 | return (count_leading_zeroes (&result) >= nse_work_required) ? GNUNET_YES |
833 | : GNUNET_NO; | 810 | : GNUNET_NO; |
834 | } | 811 | } |
@@ -880,7 +857,7 @@ find_proof (void *cls) | |||
880 | while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) | 857 | while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) |
881 | { | 858 | { |
882 | GNUNET_memcpy (buf, &counter, sizeof(uint64_t)); | 859 | GNUNET_memcpy (buf, &counter, sizeof(uint64_t)); |
883 | pow_hash (buf, sizeof(buf), &result); | 860 | GNUNET_CRYPTO_pow_hash (buf, sizeof(buf), &result); |
884 | if (nse_work_required <= count_leading_zeroes (&result)) | 861 | if (nse_work_required <= count_leading_zeroes (&result)) |
885 | { | 862 | { |
886 | my_proof = counter; | 863 | my_proof = counter; |
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 67e131810..0f6251f96 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -64,6 +64,7 @@ libgnunetutil_la_SOURCES = \ | |||
64 | crypto_kdf.c \ | 64 | crypto_kdf.c \ |
65 | crypto_mpi.c \ | 65 | crypto_mpi.c \ |
66 | crypto_paillier.c \ | 66 | crypto_paillier.c \ |
67 | crypto_pow.c \ | ||
67 | crypto_random.c \ | 68 | crypto_random.c \ |
68 | crypto_rsa.c \ | 69 | crypto_rsa.c \ |
69 | disk.c \ | 70 | disk.c \ |
diff --git a/src/util/crypto_pow.c b/src/util/crypto_pow.c new file mode 100644 index 000000000..b4dfbf53a --- /dev/null +++ b/src/util/crypto_pow.c | |||
@@ -0,0 +1,58 @@ | |||
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 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_crypto_lib.h" | ||
29 | #include <gcrypt.h> | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Calculate the 'proof-of-work' hash (an expensive hash). | ||
34 | * We're using a non-standard formula to avoid issues with | ||
35 | * ASICs appearing (see #3795). | ||
36 | * | ||
37 | * @param buf data to hash | ||
38 | * @param buf_len number of bytes in @a buf | ||
39 | * @param result where to write the resulting hash | ||
40 | */ | ||
41 | void | ||
42 | GNUNET_CRYPTO_pow_hash (const void *buf, size_t buf_len, struct | ||
43 | GNUNET_HashCode *result) | ||
44 | { | ||
45 | GNUNET_break ( | ||
46 | 0 == gcry_kdf_derive (buf, | ||
47 | buf_len, | ||
48 | GCRY_KDF_SCRYPT, | ||
49 | 1 /* subalgo */, | ||
50 | "gnunet-proof-of-work", | ||
51 | strlen ("gnunet-proof-of-work"), | ||
52 | 2 /* iterations; keep cost of individual op small */, | ||
53 | sizeof(struct GNUNET_HashCode), | ||
54 | result)); | ||
55 | } | ||
56 | |||
57 | |||
58 | /* end of crypto_pow.c */ | ||
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c index 8d8451950..d84f486a7 100644 --- a/src/util/gnunet-scrypt.c +++ b/src/util/gnunet-scrypt.c | |||
@@ -68,29 +68,6 @@ shutdown_task (void *cls) | |||
68 | 68 | ||
69 | 69 | ||
70 | /** | 70 | /** |
71 | * Calculate the 'proof-of-work' hash (an expensive hash). | ||
72 | * | ||
73 | * @param buf data to hash | ||
74 | * @param buf_len number of bytes in @a buf | ||
75 | * @param result where to write the resulting hash | ||
76 | */ | ||
77 | static void | ||
78 | pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result) | ||
79 | { | ||
80 | GNUNET_break ( | ||
81 | 0 == gcry_kdf_derive (buf, | ||
82 | buf_len, | ||
83 | GCRY_KDF_SCRYPT, | ||
84 | 1 /* subalgo */, | ||
85 | "gnunet-proof-of-work", | ||
86 | strlen ("gnunet-proof-of-work"), | ||
87 | 2 /* iterations; keep cost of individual op small */, | ||
88 | sizeof(struct GNUNET_HashCode), | ||
89 | result)); | ||
90 | } | ||
91 | |||
92 | |||
93 | /** | ||
94 | * Count the leading zeroes in hash. | 71 | * Count the leading zeroes in hash. |
95 | * | 72 | * |
96 | * @param hash to count leading zeros in | 73 | * @param hash to count leading zeros in |
@@ -140,7 +117,7 @@ find_proof (void *cls) | |||
140 | while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) | 117 | while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) |
141 | { | 118 | { |
142 | GNUNET_memcpy (buf, &counter, sizeof(uint64_t)); | 119 | GNUNET_memcpy (buf, &counter, sizeof(uint64_t)); |
143 | pow_hash (buf, sizeof(buf), &result); | 120 | GNUNET_CRYPTO_pow_hash (buf, sizeof(buf), &result); |
144 | if (nse_work_required <= count_leading_zeroes (&result)) | 121 | if (nse_work_required <= count_leading_zeroes (&result)) |
145 | { | 122 | { |
146 | proof = counter; | 123 | proof = counter; |