diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
commit | 9ef4abad615bea12d13be542b8ae5fbeb2dfee32 (patch) | |
tree | 8875a687e004d331c9ea6a1d511a328c72b88113 /src/util/gnunet-scrypt.c | |
parent | e95236b3ed78cd597c15f34b89385295702b627f (diff) | |
download | gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.tar.gz gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.zip |
NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/
This also includes a necessary API refactoring of crypto from IDENTITY
to UTIL.
Diffstat (limited to 'src/util/gnunet-scrypt.c')
-rw-r--r-- | src/util/gnunet-scrypt.c | 325 |
1 files changed, 0 insertions, 325 deletions
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c deleted file mode 100644 index 3d1b9c017..000000000 --- a/src/util/gnunet-scrypt.c +++ /dev/null | |||
@@ -1,325 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2014 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/gnunet-scrypt.c | ||
22 | * @brief tool to manipulate SCRYPT proofs of work. | ||
23 | * @author Bart Polot | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include <gcrypt.h> | ||
29 | |||
30 | |||
31 | /** | ||
32 | * Salt for PoW calcualations. | ||
33 | */ | ||
34 | static struct GNUNET_CRYPTO_PowSalt salt = { "gnunet-nse-proof" }; | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits. | ||
39 | */ | ||
40 | static unsigned long long nse_work_required; | ||
41 | |||
42 | /** | ||
43 | * Interval between proof find runs. | ||
44 | */ | ||
45 | static struct GNUNET_TIME_Relative proof_find_delay; | ||
46 | |||
47 | static struct GNUNET_CRYPTO_EddsaPublicKey pub; | ||
48 | |||
49 | static uint64_t proof; | ||
50 | |||
51 | static struct GNUNET_SCHEDULER_Task *proof_task; | ||
52 | |||
53 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
54 | |||
55 | static char *pkfn; | ||
56 | |||
57 | static char *pwfn; | ||
58 | |||
59 | |||
60 | /** | ||
61 | * Write our current proof to disk. | ||
62 | * | ||
63 | * @param cls closure | ||
64 | */ | ||
65 | static void | ||
66 | shutdown_task (void *cls) | ||
67 | { | ||
68 | (void) cls; | ||
69 | if (GNUNET_OK != | ||
70 | GNUNET_DISK_fn_write (pwfn, | ||
71 | &proof, | ||
72 | sizeof(proof), | ||
73 | GNUNET_DISK_PERM_USER_READ | ||
74 | | GNUNET_DISK_PERM_USER_WRITE)) | ||
75 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | ||
76 | "write", | ||
77 | pwfn); | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
82 | * Find our proof of work. | ||
83 | * | ||
84 | * @param cls closure (unused) | ||
85 | */ | ||
86 | static void | ||
87 | find_proof (void *cls) | ||
88 | { | ||
89 | #define ROUND_SIZE 10 | ||
90 | uint64_t counter; | ||
91 | char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey) | ||
92 | + sizeof(uint64_t)] GNUNET_ALIGN; | ||
93 | struct GNUNET_HashCode result; | ||
94 | unsigned int i; | ||
95 | struct GNUNET_TIME_Absolute timestamp; | ||
96 | struct GNUNET_TIME_Relative elapsed; | ||
97 | |||
98 | (void) cls; | ||
99 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
100 | "Got Proof of Work %llu\n", | ||
101 | (unsigned long long) proof); | ||
102 | proof_task = NULL; | ||
103 | GNUNET_memcpy (&buf[sizeof(uint64_t)], | ||
104 | &pub, | ||
105 | sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)); | ||
106 | i = 0; | ||
107 | counter = proof; | ||
108 | timestamp = GNUNET_TIME_absolute_get (); | ||
109 | while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) | ||
110 | { | ||
111 | GNUNET_memcpy (buf, &counter, sizeof(uint64_t)); | ||
112 | GNUNET_CRYPTO_pow_hash (&salt, | ||
113 | buf, | ||
114 | sizeof(buf), | ||
115 | &result); | ||
116 | if (nse_work_required <= | ||
117 | GNUNET_CRYPTO_hash_count_leading_zeros (&result)) | ||
118 | { | ||
119 | proof = counter; | ||
120 | fprintf (stdout, | ||
121 | "Proof of work found: %llu!\n", | ||
122 | (unsigned long long) proof); | ||
123 | GNUNET_SCHEDULER_shutdown (); | ||
124 | return; | ||
125 | } | ||
126 | counter++; | ||
127 | i++; | ||
128 | } | ||
129 | elapsed = GNUNET_TIME_absolute_get_duration (timestamp); | ||
130 | elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE); | ||
131 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
132 | "Current: %llu [%s/proof]\n", | ||
133 | (unsigned long long) counter, | ||
134 | GNUNET_STRINGS_relative_time_to_string (elapsed, 0)); | ||
135 | if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) | ||
136 | { | ||
137 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
138 | "Testing proofs currently at %llu\n", | ||
139 | (unsigned long long) counter); | ||
140 | /* remember progress every 100 rounds */ | ||
141 | proof = counter; | ||
142 | shutdown_task (NULL); | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | proof = counter; | ||
147 | } | ||
148 | proof_task = | ||
149 | GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, | ||
150 | GNUNET_SCHEDULER_PRIORITY_IDLE, | ||
151 | &find_proof, | ||
152 | NULL); | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Main function that will be run by the scheduler. | ||
158 | * | ||
159 | * @param cls closure | ||
160 | * @param args remaining command-line arguments | ||
161 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
162 | * @param cfg configuration | ||
163 | */ | ||
164 | static void | ||
165 | run (void *cls, | ||
166 | char *const *args, | ||
167 | const char *cfgfile, | ||
168 | const struct GNUNET_CONFIGURATION_Handle *config) | ||
169 | { | ||
170 | struct GNUNET_CRYPTO_EddsaPrivateKey pk; | ||
171 | char *pids; | ||
172 | |||
173 | (void) cls; | ||
174 | (void) args; | ||
175 | (void) cfgfile; | ||
176 | cfg = config; | ||
177 | /* load proof of work */ | ||
178 | if (NULL == pwfn) | ||
179 | { | ||
180 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
181 | "NSE", | ||
182 | "PROOFFILE", | ||
183 | &pwfn)) | ||
184 | { | ||
185 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE"); | ||
186 | GNUNET_SCHEDULER_shutdown (); | ||
187 | return; | ||
188 | } | ||
189 | } | ||
190 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn); | ||
191 | if ((GNUNET_YES != GNUNET_DISK_file_test (pwfn)) || | ||
192 | (sizeof(proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof(proof)))) | ||
193 | proof = 0; | ||
194 | |||
195 | /* load private key */ | ||
196 | if (NULL == pkfn) | ||
197 | { | ||
198 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
199 | "PEER", | ||
200 | "PRIVATE_KEY", | ||
201 | &pkfn)) | ||
202 | { | ||
203 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
204 | "PEER", | ||
205 | "PRIVATE_KEY"); | ||
206 | return; | ||
207 | } | ||
208 | } | ||
209 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn); | ||
210 | if (GNUNET_SYSERR == | ||
211 | GNUNET_CRYPTO_eddsa_key_from_file (pkfn, | ||
212 | GNUNET_YES, | ||
213 | &pk)) | ||
214 | { | ||
215 | fprintf (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn); | ||
216 | GNUNET_free (pkfn); | ||
217 | return; | ||
218 | } | ||
219 | GNUNET_free (pkfn); | ||
220 | GNUNET_CRYPTO_eddsa_key_get_public (&pk, | ||
221 | &pub); | ||
222 | pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); | ||
223 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids); | ||
224 | GNUNET_free (pids); | ||
225 | |||
226 | /* get target bit amount */ | ||
227 | if (0 == nse_work_required) | ||
228 | { | ||
229 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, | ||
230 | "NSE", | ||
231 | "WORKBITS", | ||
232 | &nse_work_required)) | ||
233 | { | ||
234 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS"); | ||
235 | GNUNET_SCHEDULER_shutdown (); | ||
236 | return; | ||
237 | } | ||
238 | if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8) | ||
239 | { | ||
240 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, | ||
241 | "NSE", | ||
242 | "WORKBITS", | ||
243 | _ ("Value is too large.\n")); | ||
244 | GNUNET_SCHEDULER_shutdown (); | ||
245 | return; | ||
246 | } | ||
247 | else if (0 == nse_work_required) | ||
248 | { | ||
249 | GNUNET_SCHEDULER_shutdown (); | ||
250 | return; | ||
251 | } | ||
252 | } | ||
253 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required); | ||
254 | |||
255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
256 | "Delay between tries: %s\n", | ||
257 | GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); | ||
258 | proof_task = | ||
259 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, | ||
260 | &find_proof, | ||
261 | NULL); | ||
262 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
263 | } | ||
264 | |||
265 | |||
266 | /** | ||
267 | * Program to manipulate ECC key files. | ||
268 | * | ||
269 | * @param argc number of arguments from the command line | ||
270 | * @param argv command line arguments | ||
271 | * @return 0 ok, 1 on error | ||
272 | */ | ||
273 | int | ||
274 | main (int argc, char *const *argv) | ||
275 | { | ||
276 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
277 | GNUNET_GETOPT_option_ulong ( | ||
278 | 'b', | ||
279 | "bits", | ||
280 | "BITS", | ||
281 | gettext_noop ("number of bits to require for the proof of work"), | ||
282 | &nse_work_required), | ||
283 | GNUNET_GETOPT_option_filename ( | ||
284 | 'k', | ||
285 | "keyfile", | ||
286 | "FILE", | ||
287 | gettext_noop ("file with private key, otherwise default is used"), | ||
288 | &pkfn), | ||
289 | GNUNET_GETOPT_option_filename ( | ||
290 | 'o', | ||
291 | "outfile", | ||
292 | "FILE", | ||
293 | gettext_noop ("file with proof of work, otherwise default is used"), | ||
294 | &pwfn), | ||
295 | GNUNET_GETOPT_option_relative_time ('t', | ||
296 | "timeout", | ||
297 | "TIME", | ||
298 | gettext_noop ( | ||
299 | "time to wait between calculations"), | ||
300 | &proof_find_delay), | ||
301 | GNUNET_GETOPT_OPTION_END | ||
302 | }; | ||
303 | int ret; | ||
304 | |||
305 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
306 | return 2; | ||
307 | |||
308 | ret = | ||
309 | (GNUNET_OK == | ||
310 | GNUNET_PROGRAM_run (argc, | ||
311 | argv, | ||
312 | "gnunet-scrypt [OPTIONS] prooffile", | ||
313 | gettext_noop ("Manipulate GNUnet proof of work files"), | ||
314 | options, | ||
315 | &run, | ||
316 | NULL)) | ||
317 | ? 0 | ||
318 | : 1; | ||
319 | GNUNET_free_nz ((void *) argv); | ||
320 | GNUNET_free (pwfn); | ||
321 | return ret; | ||
322 | } | ||
323 | |||
324 | |||
325 | /* end of gnunet-scrypt.c */ | ||