aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-scrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-scrypt.c')
-rw-r--r--src/util/gnunet-scrypt.c213
1 files changed, 103 insertions, 110 deletions
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c
index 5e15eb16a..f4149a398 100644
--- a/src/util/gnunet-scrypt.c
+++ b/src/util/gnunet-scrypt.c
@@ -57,15 +57,13 @@ static char *pwfn;
57static void 57static void
58shutdown_task (void *cls) 58shutdown_task (void *cls)
59{ 59{
60 if (sizeof (proof) != 60 (void) cls;
61 GNUNET_DISK_fn_write (pwfn, 61 if (sizeof (proof) != GNUNET_DISK_fn_write (pwfn,
62 &proof, 62 &proof,
63 sizeof (proof), 63 sizeof (proof),
64 GNUNET_DISK_PERM_USER_READ | 64 GNUNET_DISK_PERM_USER_READ |
65 GNUNET_DISK_PERM_USER_WRITE)) 65 GNUNET_DISK_PERM_USER_WRITE))
66 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 66 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", pwfn);
67 "write",
68 pwfn);
69} 67}
70 68
71 69
@@ -77,17 +75,18 @@ shutdown_task (void *cls)
77 * @param result where to write the resulting hash 75 * @param result where to write the resulting hash
78 */ 76 */
79static void 77static void
80pow_hash (const void *buf, 78pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
81 size_t buf_len,
82 struct GNUNET_HashCode *result)
83{ 79{
84 GNUNET_break (0 == 80 GNUNET_break (
85 gcry_kdf_derive (buf, buf_len, 81 0 == gcry_kdf_derive (buf,
86 GCRY_KDF_SCRYPT, 82 buf_len,
87 1 /* subalgo */, 83 GCRY_KDF_SCRYPT,
88 "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"), 84 1 /* subalgo */,
89 2 /* iterations; keep cost of individual op small */, 85 "gnunet-proof-of-work",
90 sizeof (struct GNUNET_HashCode), result)); 86 strlen ("gnunet-proof-of-work"),
87 2 /* iterations; keep cost of individual op small */,
88 sizeof (struct GNUNET_HashCode),
89 result));
91} 90}
92 91
93 92
@@ -118,7 +117,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash)
118static void 117static void
119find_proof (void *cls) 118find_proof (void *cls)
120{ 119{
121 #define ROUND_SIZE 10 120#define ROUND_SIZE 10
122 uint64_t counter; 121 uint64_t counter;
123 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + 122 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
124 sizeof (uint64_t)] GNUNET_ALIGN; 123 sizeof (uint64_t)] GNUNET_ALIGN;
@@ -127,12 +126,14 @@ find_proof (void *cls)
127 struct GNUNET_TIME_Absolute timestamp; 126 struct GNUNET_TIME_Absolute timestamp;
128 struct GNUNET_TIME_Relative elapsed; 127 struct GNUNET_TIME_Relative elapsed;
129 128
129 (void) cls;
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Got Proof of Work %llu\n", 131 "Got Proof of Work %llu\n",
132 (unsigned long long) proof); 132 (unsigned long long) proof);
133 proof_task = NULL; 133 proof_task = NULL;
134 GNUNET_memcpy (&buf[sizeof (uint64_t)], &pub, 134 GNUNET_memcpy (&buf[sizeof (uint64_t)],
135 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 135 &pub,
136 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
136 i = 0; 137 i = 0;
137 counter = proof; 138 counter = proof;
138 timestamp = GNUNET_TIME_absolute_get (); 139 timestamp = GNUNET_TIME_absolute_get ();
@@ -143,7 +144,8 @@ find_proof (void *cls)
143 if (nse_work_required <= count_leading_zeroes (&result)) 144 if (nse_work_required <= count_leading_zeroes (&result))
144 { 145 {
145 proof = counter; 146 proof = counter;
146 FPRINTF (stdout, "Proof of work found: %llu!\n", 147 FPRINTF (stdout,
148 "Proof of work found: %llu!\n",
147 (unsigned long long) proof); 149 (unsigned long long) proof);
148 GNUNET_SCHEDULER_shutdown (); 150 GNUNET_SCHEDULER_shutdown ();
149 return; 151 return;
@@ -159,7 +161,8 @@ find_proof (void *cls)
159 GNUNET_STRINGS_relative_time_to_string (elapsed, 0)); 161 GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
160 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) 162 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
161 { 163 {
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n", 164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Testing proofs currently at %llu\n",
163 (unsigned long long) counter); 166 (unsigned long long) counter);
164 /* remember progress every 100 rounds */ 167 /* remember progress every 100 rounds */
165 proof = counter; 168 proof = counter;
@@ -171,8 +174,9 @@ find_proof (void *cls)
171 } 174 }
172 proof_task = 175 proof_task =
173 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, 176 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
174 GNUNET_SCHEDULER_PRIORITY_IDLE, 177 GNUNET_SCHEDULER_PRIORITY_IDLE,
175 &find_proof, NULL); 178 &find_proof,
179 NULL);
176} 180}
177 181
178 182
@@ -193,54 +197,46 @@ run (void *cls,
193 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 197 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
194 char *pids; 198 char *pids;
195 199
200 (void) cls;
201 (void) args;
202 (void) cfgfile;
196 cfg = config; 203 cfg = config;
197 /* load proof of work */ 204 /* load proof of work */
198 if (NULL == pwfn) 205 if (NULL == pwfn)
199 { 206 {
200 if (GNUNET_OK != 207 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
201 GNUNET_CONFIGURATION_get_value_filename (cfg, 208 "NSE",
202 "NSE", 209 "PROOFFILE",
203 "PROOFFILE", 210 &pwfn))
204 &pwfn))
205 { 211 {
206 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 212 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE");
207 "NSE",
208 "PROOFFILE");
209 GNUNET_SCHEDULER_shutdown (); 213 GNUNET_SCHEDULER_shutdown ();
210 return; 214 return;
211 } 215 }
212 } 216 }
213 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn);
214 "Proof of Work file: %s\n", 218 if ((GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
215 pwfn); 219 (sizeof (proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof))))
216 if ( (GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
217 (sizeof (proof) !=
218 GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof))))
219 proof = 0; 220 proof = 0;
220 221
221 /* load private key */ 222 /* load private key */
222 if (NULL == pkfn) 223 if (NULL == pkfn)
223 { 224 {
224 if (GNUNET_OK != 225 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
225 GNUNET_CONFIGURATION_get_value_filename (cfg, 226 "PEER",
226 "PEER", 227 "PRIVATE_KEY",
227 "PRIVATE_KEY", 228 &pkfn))
228 &pkfn))
229 { 229 {
230 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 230 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
231 "PEER", 231 "PEER",
232 "PRIVATE_KEY"); 232 "PRIVATE_KEY");
233 return; 233 return;
234 } 234 }
235 } 235 }
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
237 "Private Key file: %s\n",
238 pkfn);
239 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) 237 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
240 { 238 {
241 FPRINTF (stderr, 239 FPRINTF (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn);
242 _("Loading hostkey from `%s' failed.\n"),
243 pkfn);
244 GNUNET_free (pkfn); 240 GNUNET_free (pkfn);
245 return; 241 return;
246 } 242 }
@@ -248,32 +244,27 @@ run (void *cls,
248 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub); 244 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
249 GNUNET_free (pk); 245 GNUNET_free (pk);
250 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); 246 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids);
252 "Peer ID: %s\n",
253 pids);
254 GNUNET_free (pids); 248 GNUNET_free (pids);
255 249
256 /* get target bit amount */ 250 /* get target bit amount */
257 if (0 == nse_work_required) 251 if (0 == nse_work_required)
258 { 252 {
259 if (GNUNET_OK != 253 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
260 GNUNET_CONFIGURATION_get_value_number (cfg, 254 "NSE",
261 "NSE", 255 "WORKBITS",
262 "WORKBITS", 256 &nse_work_required))
263 &nse_work_required))
264 { 257 {
265 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 258 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
266 "NSE",
267 "WORKBITS");
268 GNUNET_SCHEDULER_shutdown (); 259 GNUNET_SCHEDULER_shutdown ();
269 return; 260 return;
270 } 261 }
271 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8) 262 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
272 { 263 {
273 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 264 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
274 "NSE", 265 "NSE",
275 "WORKBITS", 266 "WORKBITS",
276 _("Value is too large.\n")); 267 _ ("Value is too large.\n"));
277 GNUNET_SCHEDULER_shutdown (); 268 GNUNET_SCHEDULER_shutdown ();
278 return; 269 return;
279 } 270 }
@@ -283,19 +274,16 @@ run (void *cls,
283 return; 274 return;
284 } 275 }
285 } 276 }
286 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 277 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required);
287 "Bits: %llu\n",
288 nse_work_required);
289 278
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Delay between tries: %s\n", 280 "Delay between tries: %s\n",
292 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); 281 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
293 proof_task = 282 proof_task =
294 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 283 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
295 &find_proof, 284 &find_proof,
296 NULL); 285 NULL);
297 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 286 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
298 NULL);
299} 287}
300 288
301 289
@@ -307,46 +295,51 @@ run (void *cls,
307 * @return 0 ok, 1 on error 295 * @return 0 ok, 1 on error
308 */ 296 */
309int 297int
310main (int argc, 298main (int argc, char *const *argv)
311 char *const *argv)
312{ 299{
313 struct GNUNET_GETOPT_CommandLineOption options[] = { 300 struct GNUNET_GETOPT_CommandLineOption options[] =
314 GNUNET_GETOPT_option_ulong ('b', 301 {GNUNET_GETOPT_option_ulong (
315 "bits", 302 'b',
316 "BITS", 303 "bits",
317 gettext_noop ("number of bits to require for the proof of work"), 304 "BITS",
318 &nse_work_required), 305 gettext_noop ("number of bits to require for the proof of work"),
319 GNUNET_GETOPT_option_filename ('k', 306 &nse_work_required),
320 "keyfile", 307 GNUNET_GETOPT_option_filename (
321 "FILE", 308 'k',
322 gettext_noop ("file with private key, otherwise default is used"), 309 "keyfile",
323 &pkfn), 310 "FILE",
324 GNUNET_GETOPT_option_filename ('o', 311 gettext_noop ("file with private key, otherwise default is used"),
325 "outfile", 312 &pkfn),
326 "FILE", 313 GNUNET_GETOPT_option_filename (
327 gettext_noop ("file with proof of work, otherwise default is used"), 314 'o',
328 &pwfn), 315 "outfile",
329 GNUNET_GETOPT_option_relative_time ('t', 316 "FILE",
330 "timeout", 317 gettext_noop ("file with proof of work, otherwise default is used"),
331 "TIME", 318 &pwfn),
332 gettext_noop ("time to wait between calculations"), 319 GNUNET_GETOPT_option_relative_time ('t',
333 &proof_find_delay), 320 "timeout",
334 GNUNET_GETOPT_OPTION_END 321 "TIME",
335 }; 322 gettext_noop (
323 "time to wait between calculations"),
324 &proof_find_delay),
325 GNUNET_GETOPT_OPTION_END};
336 int ret; 326 int ret;
337 327
338 if (GNUNET_OK != 328 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
339 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
340 return 2; 329 return 2;
341 330
342 ret = (GNUNET_OK == 331 ret =
343 GNUNET_PROGRAM_run (argc, argv, 332 (GNUNET_OK ==
344 "gnunet-scrypt [OPTIONS] prooffile", 333 GNUNET_PROGRAM_run (argc,
345 gettext_noop ("Manipulate GNUnet proof of work files"), 334 argv,
346 options, 335 "gnunet-scrypt [OPTIONS] prooffile",
347 &run, 336 gettext_noop ("Manipulate GNUnet proof of work files"),
348 NULL)) ? 0 : 1; 337 options,
349 GNUNET_free ((void*) argv); 338 &run,
339 NULL))
340 ? 0
341 : 1;
342 GNUNET_free ((void *) argv);
350 GNUNET_free_non_null (pwfn); 343 GNUNET_free_non_null (pwfn);
351 return ret; 344 return ret;
352} 345}