revocation_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file revocation/revocation_api.c
  18. * @brief API to perform and access key revocations
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_revocation_service.h"
  23. #include "gnunet_signatures.h"
  24. #include "gnunet_protocols.h"
  25. #include "revocation.h"
  26. #include <inttypes.h>
  27. /**
  28. * Handle for the key revocation query.
  29. */
  30. struct GNUNET_REVOCATION_Query
  31. {
  32. /**
  33. * Message queue to the service.
  34. */
  35. struct GNUNET_MQ_Handle *mq;
  36. /**
  37. * Function to call with the result.
  38. */
  39. GNUNET_REVOCATION_Callback func;
  40. /**
  41. * Closure for @e func.
  42. */
  43. void *func_cls;
  44. };
  45. /**
  46. * Helper struct that holds a found pow nonce
  47. * and the corresponding number of leading zeroes.
  48. */
  49. struct BestPow
  50. {
  51. /**
  52. * PoW nonce
  53. */
  54. uint64_t pow;
  55. /**
  56. * Corresponding zero bits in hash
  57. */
  58. unsigned int bits;
  59. };
  60. /**
  61. * The handle to a PoW calculation.
  62. * Used in iterative PoW rounds.
  63. */
  64. struct GNUNET_REVOCATION_PowCalculationHandle
  65. {
  66. /**
  67. * Current set of found PoWs
  68. */
  69. struct BestPow best[POW_COUNT];
  70. /**
  71. * The final PoW result data structure.
  72. */
  73. struct GNUNET_REVOCATION_PowP *pow;
  74. /**
  75. * The current nonce to try
  76. */
  77. uint64_t current_pow;
  78. /**
  79. * Epochs how long the PoW should be valid.
  80. * This is added on top of the difficulty in the PoW.
  81. */
  82. unsigned int epochs;
  83. /**
  84. * The difficulty (leading zeros) to achieve.
  85. */
  86. unsigned int difficulty;
  87. };
  88. /**
  89. * Generic error handler, called with the appropriate
  90. * error code and the same closure specified at the creation of
  91. * the message queue.
  92. * Not every message queue implementation supports an error handler.
  93. *
  94. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  95. * @param error error code
  96. */
  97. static void
  98. query_mq_error_handler (void *cls,
  99. enum GNUNET_MQ_Error error)
  100. {
  101. struct GNUNET_REVOCATION_Query *q = cls;
  102. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  103. "Revocation query MQ error\n");
  104. q->func (q->func_cls,
  105. GNUNET_SYSERR);
  106. GNUNET_REVOCATION_query_cancel (q);
  107. }
  108. /**
  109. * Handle response to our revocation query.
  110. *
  111. * @param cls our `struct GNUNET_REVOCATION_Query` handle
  112. * @param qrm response we got
  113. */
  114. static void
  115. handle_revocation_query_response (void *cls,
  116. const struct QueryResponseMessage *qrm)
  117. {
  118. struct GNUNET_REVOCATION_Query *q = cls;
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  120. "Revocation query result: %d\n",
  121. (uint32_t) ntohl (qrm->is_valid));
  122. q->func (q->func_cls,
  123. ntohl (qrm->is_valid));
  124. GNUNET_REVOCATION_query_cancel (q);
  125. }
  126. /**
  127. * Check if a key was revoked.
  128. *
  129. * @param cfg the configuration to use
  130. * @param key key to check for revocation
  131. * @param func funtion to call with the result of the check
  132. * @param func_cls closure to pass to @a func
  133. * @return handle to use in #GNUNET_REVOCATION_query_cancel to stop REVOCATION from invoking the callback
  134. */
  135. struct GNUNET_REVOCATION_Query *
  136. GNUNET_REVOCATION_query (const struct GNUNET_CONFIGURATION_Handle *cfg,
  137. const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
  138. GNUNET_REVOCATION_Callback func,
  139. void *func_cls)
  140. {
  141. struct GNUNET_REVOCATION_Query *q
  142. = GNUNET_new (struct GNUNET_REVOCATION_Query);
  143. struct GNUNET_MQ_MessageHandler handlers[] = {
  144. GNUNET_MQ_hd_fixed_size (revocation_query_response,
  145. GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE,
  146. struct QueryResponseMessage,
  147. q),
  148. GNUNET_MQ_handler_end ()
  149. };
  150. struct QueryMessage *qm;
  151. struct GNUNET_MQ_Envelope *env;
  152. q->mq = GNUNET_CLIENT_connect (cfg,
  153. "revocation",
  154. handlers,
  155. &query_mq_error_handler,
  156. q);
  157. if (NULL == q->mq)
  158. {
  159. GNUNET_free (q);
  160. return NULL;
  161. }
  162. q->func = func;
  163. q->func_cls = func_cls;
  164. env = GNUNET_MQ_msg (qm,
  165. GNUNET_MESSAGE_TYPE_REVOCATION_QUERY);
  166. qm->reserved = htonl (0);
  167. qm->key = *key;
  168. GNUNET_MQ_send (q->mq,
  169. env);
  170. return q;
  171. }
  172. /**
  173. * Cancel key revocation check.
  174. *
  175. * @param q query to cancel
  176. */
  177. void
  178. GNUNET_REVOCATION_query_cancel (struct GNUNET_REVOCATION_Query *q)
  179. {
  180. if (NULL != q->mq)
  181. {
  182. GNUNET_MQ_destroy (q->mq);
  183. q->mq = NULL;
  184. }
  185. GNUNET_free (q);
  186. }
  187. /**
  188. * Handle for the key revocation operation.
  189. */
  190. struct GNUNET_REVOCATION_Handle
  191. {
  192. /**
  193. * Message queue to the service.
  194. */
  195. struct GNUNET_MQ_Handle *mq;
  196. /**
  197. * Function to call once we are done.
  198. */
  199. GNUNET_REVOCATION_Callback func;
  200. /**
  201. * Closure for @e func.
  202. */
  203. void *func_cls;
  204. };
  205. /**
  206. * Generic error handler, called with the appropriate
  207. * error code and the same closure specified at the creation of
  208. * the message queue.
  209. * Not every message queue implementation supports an error handler.
  210. *
  211. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  212. * @param error error code
  213. */
  214. static void
  215. revocation_mq_error_handler (void *cls,
  216. enum GNUNET_MQ_Error error)
  217. {
  218. struct GNUNET_REVOCATION_Handle *h = cls;
  219. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  220. "Revocation MQ error\n");
  221. h->func (h->func_cls,
  222. GNUNET_SYSERR);
  223. GNUNET_REVOCATION_revoke_cancel (h);
  224. }
  225. /**
  226. * Handle response to our revocation query.
  227. *
  228. * @param cls our `struct GNUNET_REVOCATION_Handle` handle
  229. * @param rrm response we got
  230. */
  231. static void
  232. handle_revocation_response (void *cls,
  233. const struct RevocationResponseMessage *rrm)
  234. {
  235. struct GNUNET_REVOCATION_Handle *h = cls;
  236. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  237. "Revocation transmission result: %d\n",
  238. (uint32_t) ntohl (rrm->is_valid));
  239. h->func (h->func_cls,
  240. ntohl (rrm->is_valid));
  241. GNUNET_REVOCATION_revoke_cancel (h);
  242. }
  243. /**
  244. * Perform key revocation.
  245. *
  246. * @param cfg the configuration to use
  247. * @param key public key of the key to revoke
  248. * @param sig signature to use on the revocation (should have been
  249. * created using #GNUNET_REVOCATION_sign_revocation).
  250. * @param ts revocation timestamp
  251. * @param pow proof of work to use (should have been created by
  252. * iteratively calling #GNUNET_REVOCATION_check_pow)
  253. * @param func funtion to call with the result of the check
  254. * (called with `is_valid` being #GNUNET_NO if
  255. * the revocation worked).
  256. * @param func_cls closure to pass to @a func
  257. * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
  258. */
  259. struct GNUNET_REVOCATION_Handle *
  260. GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
  261. const struct GNUNET_REVOCATION_PowP *pow,
  262. GNUNET_REVOCATION_Callback func,
  263. void *func_cls)
  264. {
  265. struct GNUNET_REVOCATION_Handle *h
  266. = GNUNET_new (struct GNUNET_REVOCATION_Handle);
  267. struct GNUNET_MQ_MessageHandler handlers[] = {
  268. GNUNET_MQ_hd_fixed_size (revocation_response,
  269. GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE,
  270. struct RevocationResponseMessage,
  271. h),
  272. GNUNET_MQ_handler_end ()
  273. };
  274. unsigned long long matching_bits;
  275. struct GNUNET_TIME_Relative epoch_duration;
  276. struct RevokeMessage *rm;
  277. struct GNUNET_MQ_Envelope *env;
  278. if ((GNUNET_OK !=
  279. GNUNET_CONFIGURATION_get_value_number (cfg,
  280. "REVOCATION",
  281. "WORKBITS",
  282. &matching_bits)))
  283. {
  284. GNUNET_break (0);
  285. GNUNET_free (h);
  286. return NULL;
  287. }
  288. if ((GNUNET_OK !=
  289. GNUNET_CONFIGURATION_get_value_time (cfg,
  290. "REVOCATION",
  291. "EPOCH_DURATION",
  292. &epoch_duration)))
  293. {
  294. GNUNET_break (0);
  295. GNUNET_free (h);
  296. return NULL;
  297. }
  298. if (GNUNET_YES != GNUNET_REVOCATION_check_pow (pow,
  299. (unsigned int) matching_bits,
  300. epoch_duration))
  301. {
  302. GNUNET_break (0);
  303. GNUNET_free (h);
  304. return NULL;
  305. }
  306. h->mq = GNUNET_CLIENT_connect (cfg,
  307. "revocation",
  308. handlers,
  309. &revocation_mq_error_handler,
  310. h);
  311. if (NULL == h->mq)
  312. {
  313. GNUNET_free (h);
  314. return NULL;
  315. }
  316. h->func = func;
  317. h->func_cls = func_cls;
  318. env = GNUNET_MQ_msg (rm,
  319. GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
  320. rm->reserved = htonl (0);
  321. rm->proof_of_work = *pow;
  322. GNUNET_MQ_send (h->mq,
  323. env);
  324. return h;
  325. }
  326. /**
  327. * Cancel key revocation.
  328. *
  329. * @param h operation to cancel
  330. */
  331. void
  332. GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
  333. {
  334. if (NULL != h->mq)
  335. {
  336. GNUNET_MQ_destroy (h->mq);
  337. h->mq = NULL;
  338. }
  339. GNUNET_free (h);
  340. }
  341. /**
  342. * Count the leading zeroes in hash.
  343. *
  344. * @param hash to count leading zeros in
  345. * @return the number of leading zero bits.
  346. */
  347. static unsigned int
  348. count_leading_zeroes (const struct GNUNET_HashCode *hash)
  349. {
  350. unsigned int hash_count;
  351. hash_count = 0;
  352. while ((0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)))
  353. hash_count++;
  354. return hash_count;
  355. }
  356. /**
  357. * Calculate the average zeros in the pows.
  358. *
  359. * @param ph the PowHandle
  360. * @return the average number of zeroes.
  361. */
  362. static unsigned int
  363. calculate_score (const struct GNUNET_REVOCATION_PowCalculationHandle *ph)
  364. {
  365. double sum = 0.0;
  366. for (unsigned int j = 0; j<POW_COUNT; j++)
  367. sum += ph->best[j].bits;
  368. double avg = sum / POW_COUNT;
  369. return avg;
  370. }
  371. /**
  372. * Check if the given proof-of-work is valid.
  373. *
  374. * @param pow proof of work
  375. * @param matching_bits how many bits must match (configuration)
  376. * @param epoch_duration length of single epoch in configuration
  377. * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
  378. */
  379. enum GNUNET_GenericReturnValue
  380. GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_PowP *pow,
  381. unsigned int difficulty,
  382. struct GNUNET_TIME_Relative epoch_duration)
  383. {
  384. char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  385. + sizeof (struct GNUNET_TIME_AbsoluteNBO)
  386. + sizeof (uint64_t)] GNUNET_ALIGN;
  387. struct GNUNET_REVOCATION_SignaturePurposePS spurp;
  388. struct GNUNET_HashCode result;
  389. struct GNUNET_TIME_Absolute ts;
  390. struct GNUNET_TIME_Absolute exp;
  391. struct GNUNET_TIME_Relative ttl;
  392. struct GNUNET_TIME_Relative buffer;
  393. unsigned int score = 0;
  394. unsigned int tmp_score = 0;
  395. unsigned int epochs;
  396. uint64_t pow_val;
  397. /**
  398. * Check if signature valid
  399. */
  400. spurp.key = pow->key;
  401. spurp.timestamp = pow->timestamp;
  402. spurp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  403. spurp.purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
  404. + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  405. + sizeof (struct GNUNET_TIME_AbsoluteNBO));
  406. if (GNUNET_OK !=
  407. GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
  408. &spurp.purpose,
  409. &pow->signature,
  410. &pow->key))
  411. {
  412. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  413. "Proof of work signature invalid!\n");
  414. return GNUNET_NO;
  415. }
  416. /**
  417. * First, check if PoW set is strictly monotically increasing
  418. */
  419. for (unsigned int i = 0; i < POW_COUNT - 1; i++)
  420. {
  421. if (GNUNET_ntohll (pow->pow[i]) >= GNUNET_ntohll (pow->pow[i + 1]))
  422. return GNUNET_NO;
  423. }
  424. GNUNET_memcpy (&buf[sizeof(uint64_t)],
  425. &pow->timestamp,
  426. sizeof (uint64_t));
  427. GNUNET_memcpy (&buf[sizeof(uint64_t) * 2],
  428. &pow->key,
  429. sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
  430. for (unsigned int i = 0; i < POW_COUNT; i++)
  431. {
  432. pow_val = GNUNET_ntohll (pow->pow[i]);
  433. GNUNET_memcpy (buf, &pow->pow[i], sizeof(uint64_t));
  434. GNUNET_CRYPTO_pow_hash ("GnsRevocationPow",
  435. buf,
  436. sizeof(buf),
  437. &result);
  438. tmp_score = count_leading_zeroes (&result);
  439. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  440. "Score %u with %" PRIu64 " (#%u)\n",
  441. tmp_score, pow_val, i);
  442. score += tmp_score;
  443. }
  444. score = score / POW_COUNT;
  445. if (score < difficulty)
  446. return GNUNET_NO;
  447. epochs = score - difficulty;
  448. /**
  449. * Check expiration
  450. */
  451. ts = GNUNET_TIME_absolute_ntoh (pow->timestamp);
  452. ttl = GNUNET_TIME_relative_multiply (epoch_duration,
  453. epochs);
  454. /**
  455. * Extend by 10% for unsynchronized clocks
  456. */
  457. buffer = GNUNET_TIME_relative_divide (epoch_duration,
  458. 10);
  459. exp = GNUNET_TIME_absolute_add (ts, ttl);
  460. exp = GNUNET_TIME_absolute_add (exp,
  461. buffer);
  462. if (0 != GNUNET_TIME_absolute_get_remaining (ts).rel_value_us)
  463. return GNUNET_NO; /* Not yet valid. */
  464. /* Revert to actual start time */
  465. ts = GNUNET_TIME_absolute_add (ts,
  466. buffer);
  467. if (0 == GNUNET_TIME_absolute_get_remaining (exp).rel_value_us)
  468. return GNUNET_NO; /* expired */
  469. return GNUNET_YES;
  470. }
  471. /**
  472. * Initializes a fresh PoW computation.
  473. *
  474. * @param key the key to calculate the PoW for.
  475. * @param[out] pow starting point for PoW calculation (not yet valid)
  476. */
  477. void
  478. GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
  479. struct GNUNET_REVOCATION_PowP *pow)
  480. {
  481. struct GNUNET_TIME_Absolute ts = GNUNET_TIME_absolute_get ();
  482. struct GNUNET_REVOCATION_SignaturePurposePS rp;
  483. /**
  484. * Predate the validity period to prevent rejections due to
  485. * unsynchronized clocks
  486. */
  487. ts = GNUNET_TIME_absolute_subtract (ts,
  488. GNUNET_TIME_UNIT_WEEKS);
  489. pow->timestamp = GNUNET_TIME_absolute_hton (ts);
  490. rp.timestamp = pow->timestamp;
  491. rp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  492. rp.purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
  493. + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  494. + sizeof (struct GNUNET_TIME_AbsoluteNBO));
  495. GNUNET_CRYPTO_ecdsa_key_get_public (key, &pow->key);
  496. rp.key = pow->key;
  497. GNUNET_assert (GNUNET_OK ==
  498. GNUNET_CRYPTO_ecdsa_sign_ (key,
  499. &rp.purpose,
  500. &pow->signature));
  501. }
  502. /**
  503. * Starts a proof-of-work calculation given the pow object as well as
  504. * target epochs and difficulty.
  505. *
  506. * @param pow the PoW to based calculations on.
  507. * @param epochs the number of epochs for which the PoW must be valid.
  508. * @param difficulty the base difficulty of the PoW.
  509. * @return a handle for use in PoW rounds
  510. */
  511. struct GNUNET_REVOCATION_PowCalculationHandle*
  512. GNUNET_REVOCATION_pow_start (struct GNUNET_REVOCATION_PowP *pow,
  513. int epochs,
  514. unsigned int difficulty)
  515. {
  516. struct GNUNET_REVOCATION_PowCalculationHandle *pc;
  517. struct GNUNET_TIME_Relative ttl;
  518. pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle);
  519. pc->pow = pow;
  520. ttl = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_YEARS,
  521. epochs);
  522. pc->pow->ttl = GNUNET_TIME_relative_hton (ttl);
  523. pc->current_pow = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  524. UINT64_MAX);
  525. pc->difficulty = difficulty;
  526. pc->epochs = epochs;
  527. return pc;
  528. }
  529. /**
  530. * Comparison function for quicksort
  531. *
  532. * @param a left element
  533. * @param b right element
  534. * @return a-b
  535. */
  536. static int
  537. cmp_pow_value (const void *a, const void *b)
  538. {
  539. return (GNUNET_ntohll (*(uint64_t*) a) - GNUNET_ntohll (*(uint64_t*) b));
  540. }
  541. /**
  542. * Calculate a key revocation valid for broadcasting for a number
  543. * of epochs.
  544. *
  545. * @param pc handle to the PoW, initially called with NULL.
  546. * @param epochs number of epochs for which the revocation must be valid.
  547. * @param pow current pow value to try
  548. * @param difficulty current base difficulty to achieve
  549. * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
  550. */
  551. enum GNUNET_GenericReturnValue
  552. GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc)
  553. {
  554. char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  555. + sizeof (uint64_t)
  556. + sizeof (uint64_t)] GNUNET_ALIGN;
  557. struct GNUNET_HashCode result;
  558. unsigned int zeros;
  559. int ret;
  560. uint64_t pow_nbo;
  561. pc->current_pow++;
  562. /**
  563. * Do not try duplicates
  564. */
  565. for (unsigned int i = 0; i < POW_COUNT; i++)
  566. if (pc->current_pow == pc->best[i].pow)
  567. return GNUNET_NO;
  568. pow_nbo = GNUNET_htonll (pc->current_pow);
  569. GNUNET_memcpy (buf, &pow_nbo, sizeof(uint64_t));
  570. GNUNET_memcpy (&buf[sizeof(uint64_t)],
  571. &pc->pow->timestamp,
  572. sizeof (uint64_t));
  573. GNUNET_memcpy (&buf[sizeof(uint64_t) * 2],
  574. &pc->pow->key,
  575. sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
  576. GNUNET_CRYPTO_pow_hash ("GnsRevocationPow",
  577. buf,
  578. sizeof(buf),
  579. &result);
  580. zeros = count_leading_zeroes (&result);
  581. for (unsigned int i = 0; i < POW_COUNT; i++)
  582. {
  583. if (pc->best[i].bits < zeros)
  584. {
  585. pc->best[i].bits = zeros;
  586. pc->best[i].pow = pc->current_pow;
  587. pc->pow->pow[i] = pow_nbo;
  588. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  589. "New best score %u with %" PRIu64 " (#%u)\n",
  590. zeros, pc->current_pow, i);
  591. break;
  592. }
  593. }
  594. ret = calculate_score (pc) >= pc->difficulty + pc->epochs ? GNUNET_YES :
  595. GNUNET_NO;
  596. if (GNUNET_YES == ret)
  597. {
  598. /* Sort POWs) */
  599. qsort (pc->pow->pow, POW_COUNT, sizeof (uint64_t), &cmp_pow_value);
  600. }
  601. return ret;
  602. }
  603. /**
  604. * Stop a PoW calculation
  605. *
  606. * @param pc the calculation to clean up
  607. * @return #GNUNET_YES if pow valid, #GNUNET_NO if pow was set but is not
  608. * valid
  609. */
  610. void
  611. GNUNET_REVOCATION_pow_stop (struct GNUNET_REVOCATION_PowCalculationHandle *pc)
  612. {
  613. GNUNET_free (pc);
  614. }
  615. /* end of revocation_api.c */