-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / util / gnunet-scrypt.c
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 /**
19  * @file util/gnunet-scrypt.c
20  * @brief tool to manipulate SCRYPT proofs of work.
21  * @author Bart Polot
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include <gcrypt.h>
26
27 /**
28  * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
29  */
30 static unsigned long long nse_work_required;
31
32 /**
33  * Interval between proof find runs.
34  */
35 static struct GNUNET_TIME_Relative proof_find_delay;
36
37 static struct GNUNET_CRYPTO_EddsaPublicKey pub;
38
39 static uint64_t proof;
40
41 static struct GNUNET_SCHEDULER_Task *proof_task;
42
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 static char *pkfn;
46
47 static char *pwfn;
48
49
50 /**
51  * Write our current proof to disk.
52  *
53  * @param cls closure
54  */
55 static void
56 shutdown_task (void *cls)
57 {
58   if (sizeof (proof) !=
59       GNUNET_DISK_fn_write (pwfn,
60                             &proof,
61                             sizeof (proof),
62                             GNUNET_DISK_PERM_USER_READ |
63                             GNUNET_DISK_PERM_USER_WRITE))
64     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
65                               "write",
66                               pwfn);
67 }
68
69
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,
79           size_t buf_len,
80           struct GNUNET_HashCode *result)
81 {
82   GNUNET_break (0 ==
83   gcry_kdf_derive (buf, buf_len,
84                    GCRY_KDF_SCRYPT,
85                    1 /* subalgo */,
86                    "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
87                    2 /* iterations; keep cost of individual op small */,
88                    sizeof (struct GNUNET_HashCode), result));
89 }
90
91
92 /**
93  * Count the leading zeroes in hash.
94  *
95  * @param hash to count leading zeros in
96  * @return the number of leading zero bits.
97  */
98 static unsigned int
99 count_leading_zeroes (const struct GNUNET_HashCode *hash)
100 {
101   unsigned int hash_count;
102
103   hash_count = 0;
104   while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
105     hash_count++;
106   return hash_count;
107 }
108
109
110 /**
111  * Find our proof of work.
112  *
113  * @param cls closure (unused)
114  * @param tc task context
115  */
116 static void
117 find_proof (void *cls)
118 {
119   #define ROUND_SIZE 10
120   uint64_t counter;
121   char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
122            sizeof (uint64_t)] GNUNET_ALIGN;
123   struct GNUNET_HashCode result;
124   unsigned int i;
125   struct GNUNET_TIME_Absolute timestamp;
126   struct GNUNET_TIME_Relative elapsed;
127
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Got Proof of Work %llu\n",
130               (unsigned long long) proof);
131   proof_task = NULL;
132   GNUNET_memcpy (&buf[sizeof (uint64_t)], &pub,
133           sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
134   i = 0;
135   counter = proof;
136   timestamp = GNUNET_TIME_absolute_get ();
137   while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
138   {
139     GNUNET_memcpy (buf, &counter, sizeof (uint64_t));
140     pow_hash (buf, sizeof (buf), &result);
141     if (nse_work_required <= count_leading_zeroes (&result))
142     {
143       proof = counter;
144       FPRINTF (stdout, "Proof of work found: %llu!\n",
145                (unsigned long long) proof);
146       GNUNET_SCHEDULER_shutdown ();
147       return;
148     }
149     counter++;
150     i++;
151   }
152   elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
153   elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
154   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155               "Current: %llu [%s/proof]\n",
156               (unsigned long long) counter,
157               GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
158   if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n",
161                 (unsigned long long) counter);
162     /* remember progress every 100 rounds */
163     proof = counter;
164     shutdown_task (NULL);
165   }
166   else
167   {
168     proof = counter;
169   }
170   proof_task =
171     GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
172                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
173                                                 &find_proof, NULL);
174 }
175
176
177 /**
178  * Main function that will be run by the scheduler.
179  *
180  * @param cls closure
181  * @param args remaining command-line arguments
182  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
183  * @param cfg configuration
184  */
185 static void
186 run (void *cls,
187      char *const *args,
188      const char *cfgfile,
189      const struct GNUNET_CONFIGURATION_Handle *config)
190 {
191   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
192   char *pids;
193
194   cfg = config;
195   /* load proof of work */
196   if (NULL == pwfn)
197   {
198     if (GNUNET_OK !=
199         GNUNET_CONFIGURATION_get_value_filename (cfg,
200                                                  "NSE",
201                                                  "PROOFFILE",
202                                                  &pwfn))
203     {
204       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
205                                  "NSE",
206                                  "PROOFFILE");
207       GNUNET_SCHEDULER_shutdown ();
208       return;
209     }
210   }
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212               "Proof of Work file: %s\n",
213               pwfn);
214   if ( (GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
215        (sizeof (proof) !=
216         GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof))))
217     proof = 0;
218
219   /* load private key */
220   if (NULL == pkfn)
221   {
222     if (GNUNET_OK !=
223         GNUNET_CONFIGURATION_get_value_filename (cfg,
224                                                  "PEER",
225                                                  "PRIVATE_KEY",
226                                                  &pkfn))
227     {
228       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
229                                  "PEER",
230                                  "PRIVATE_KEY");
231       return;
232     }
233   }
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235               "Private Key file: %s\n",
236               pkfn);
237   if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
238   {
239     FPRINTF (stderr,
240              _("Loading hostkey from `%s' failed.\n"),
241              pkfn);
242     GNUNET_free (pkfn);
243     return;
244   }
245   GNUNET_free (pkfn);
246   GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
247   GNUNET_free (pk);
248   pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
249   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250               "Peer ID: %s\n",
251               pids);
252   GNUNET_free (pids);
253
254   /* get target bit amount */
255   if (0 == nse_work_required)
256   {
257     if (GNUNET_OK !=
258         GNUNET_CONFIGURATION_get_value_number (cfg,
259                                                "NSE",
260                                                "WORKBITS",
261                                                &nse_work_required))
262     {
263       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
264                                  "NSE",
265                                  "WORKBITS");
266       GNUNET_SCHEDULER_shutdown ();
267       return;
268     }
269     if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
270     {
271       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
272                                  "NSE",
273                                  "WORKBITS",
274                                 _("Value is too large.\n"));
275       GNUNET_SCHEDULER_shutdown ();
276       return;
277     }
278     else if (0 == nse_work_required)
279     {
280       GNUNET_SCHEDULER_shutdown ();
281       return;
282     }
283   }
284   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
285               "Bits: %llu\n",
286               nse_work_required);
287
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               "Delay between tries: %s\n",
290               GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
291   proof_task =
292     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
293                                         &find_proof,
294                                         NULL);
295   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
296                                  NULL);
297 }
298
299
300 /**
301  * Program to manipulate ECC key files.
302  *
303  * @param argc number of arguments from the command line
304  * @param argv command line arguments
305  * @return 0 ok, 1 on error
306  */
307 int
308 main (int argc,
309       char *const *argv)
310 {
311   struct GNUNET_GETOPT_CommandLineOption options[] = {
312     GNUNET_GETOPT_option_ulong ('b',
313                                     "bits",
314                                     "BITS",
315                                     gettext_noop ("number of bits to require for the proof of work"),
316                                     &nse_work_required),
317     GNUNET_GETOPT_option_filename ('k',
318                                    "keyfile",
319                                    "FILE",
320                                    gettext_noop ("file with private key, otherwise default is used"),
321                                    &pkfn),
322     GNUNET_GETOPT_option_filename ('o',
323                                    "outfile",
324                                    "FILE",
325                                    gettext_noop ("file with proof of work, otherwise default is used"),
326                                    &pwfn),
327     GNUNET_GETOPT_option_relative_time ('t',
328                                             "timeout",
329                                             "TIME",
330                                             gettext_noop ("time to wait between calculations"),
331                                             &proof_find_delay),
332     GNUNET_GETOPT_OPTION_END
333   };
334   int ret;
335
336   if (GNUNET_OK !=
337       GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
338     return 2;
339
340   ret = (GNUNET_OK ==
341          GNUNET_PROGRAM_run (argc, argv,
342                              "gnunet-scrypt [OPTIONS] prooffile",
343                              gettext_noop ("Manipulate GNUnet proof of work files"),
344                              options,
345                              &run,
346                              NULL)) ? 0 : 1;
347   GNUNET_free ((void*) argv);
348   GNUNET_free_non_null (pwfn);
349   return ret;
350 }
351
352 /* end of gnunet-scrypt.c */