Tor  0.4.3.0-alpha-dev
shared_random.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file shared_random.c
6  *
7  * \brief Functions and data structure needed to accomplish the shared
8  * random protocol as defined in proposal #250.
9  *
10  * \details
11  *
12  * This file implements the dirauth-only commit-and-reveal protocol specified
13  * by proposal #250. The protocol has two phases (sr_phase_t): the commitment
14  * phase and the reveal phase (see get_sr_protocol_phase()).
15  *
16  * During the protocol, directory authorities keep state in memory (using
17  * sr_state_t) and in disk (using sr_disk_state_t). The synchronization between
18  * these two data structures happens in disk_state_update() and
19  * disk_state_parse().
20  *
21  * Here is a rough protocol outline:
22  *
23  * 1) In the beginning of the commitment phase, dirauths generate a
24  * commitment/reveal value for the current protocol run (see
25  * new_protocol_run() and sr_generate_our_commit()).
26  *
27  * 2) During voting, dirauths publish their commits in their votes
28  * depending on the current phase. Dirauths also include the two
29  * latest shared random values (SRV) in their votes.
30  * (see sr_get_string_for_vote())
31  *
32  * 3) Upon receiving a commit from a vote, authorities parse it, verify
33  * it, and attempt to save any new commitment or reveal information in
34  * their state file (see extract_shared_random_commits() and
35  * sr_handle_received_commits()). They also parse SRVs from votes to
36  * decide which SRV should be included in the final consensus (see
37  * extract_shared_random_srvs()).
38  *
39  * 3) After voting is done, we count the SRVs we extracted from the votes,
40  * to find the one voted by the majority of dirauths which should be
41  * included in the final consensus (see get_majority_srv_from_votes()).
42  * If an appropriate SRV is found, it is embedded in the consensus (see
43  * sr_get_string_for_consensus()).
44  *
45  * 4) At the end of the reveal phase, dirauths compute a fresh SRV for the
46  * day using the active commits (see sr_compute_srv()). This new SRV
47  * is embedded in the votes as described above.
48  *
49  * Some more notes:
50  *
51  * - To support rebooting authorities and to avoid double voting, each dirauth
52  * saves the current state of the protocol on disk so that it can resume
53  * normally in case of reboot. The disk state (sr_disk_state_t) is managed by
54  * shared_random_state.c:state_query() and we go to extra lengths to ensure
55  * that the state is flushed on disk everytime we receive any useful
56  * information like commits or SRVs.
57  *
58  * - When we receive a commit from a vote, we examine it to see if it's useful
59  * to us and whether it's appropriate to receive it according to the current
60  * phase of the protocol (see should_keep_commit()). If the commit is useful
61  * to us, we save it in our disk state using save_commit_to_state(). When we
62  * receive the reveal information corresponding to a commitment, we verify
63  * that they indeed match using verify_commit_and_reveal().
64  *
65  * - We treat consensuses as the ground truth, so everytime we generate a new
66  * consensus we update our SR state accordingly even if our local view was
67  * different (see sr_act_post_consensus()).
68  *
69  * - After a consensus has been composed, the SR protocol state gets prepared
70  * for the next voting session using sr_state_update(). That function takes
71  * care of housekeeping and also rotates the SRVs and commits in case a new
72  * protocol run is coming up. We also call sr_state_update() on bootup (in
73  * sr_state_init()), to prepare the state for the very first voting session.
74  *
75  * Terminology:
76  *
77  * - "Commitment" is the commitment value of the commit-and-reveal protocol.
78  *
79  * - "Reveal" is the reveal value of the commit-and-reveal protocol.
80  *
81  * - "Commit" is a struct (sr_commit_t) that contains a commitment value and
82  * optionally also a corresponding reveal value.
83  *
84  * - "SRV" is the Shared Random Value that gets generated as the result of the
85  * commit-and-reveal protocol.
86  **/
87 
88 #define SHARED_RANDOM_PRIVATE
89 
90 #include "core/or/or.h"
92 #include "app/config/config.h"
93 #include "lib/confmgt/confmgt.h"
97 #include "feature/relay/router.h"
103 
104 #include "feature/dirauth/dirvote.h"
106 
109 
110 /** String prefix of shared random values in votes/consensuses. */
111 static const char previous_srv_str[] = "shared-rand-previous-value";
112 static const char current_srv_str[] = "shared-rand-current-value";
113 static const char commit_ns_str[] = "shared-rand-commit";
114 static const char sr_flag_ns_str[] = "shared-rand-participate";
115 
116 /** The value of the consensus param AuthDirNumSRVAgreements found in the
117  * vote. This is set once the consensus creation subsystem requests the
118  * SRV(s) that should be put in the consensus. We use this value to decide
119  * if we keep or not an SRV. */
121 
122 /** Return a heap allocated copy of the SRV <b>orig</b>. */
123 sr_srv_t *
124 sr_srv_dup(const sr_srv_t *orig)
125 {
126  sr_srv_t *duplicate = NULL;
127 
128  if (!orig) {
129  return NULL;
130  }
131 
132  duplicate = tor_malloc_zero(sizeof(sr_srv_t));
133  duplicate->num_reveals = orig->num_reveals;
134  memcpy(duplicate->value, orig->value, sizeof(duplicate->value));
135  return duplicate;
136 }
137 
138 /** Allocate a new commit object and initializing it with <b>rsa_identity</b>
139  * that MUST be provided. The digest algorithm is set to the default one
140  * that is supported. The rest is uninitialized. This never returns NULL. */
141 static sr_commit_t *
142 commit_new(const char *rsa_identity)
143 {
144  sr_commit_t *commit;
145 
146  tor_assert(rsa_identity);
147 
148  commit = tor_malloc_zero(sizeof(*commit));
149  commit->alg = SR_DIGEST_ALG;
150  memcpy(commit->rsa_identity, rsa_identity, sizeof(commit->rsa_identity));
151  base16_encode(commit->rsa_identity_hex, sizeof(commit->rsa_identity_hex),
152  commit->rsa_identity, sizeof(commit->rsa_identity));
153  return commit;
154 }
155 
156 /** Issue a log message describing <b>commit</b>. */
157 static void
158 commit_log(const sr_commit_t *commit)
159 {
160  tor_assert(commit);
161 
162  log_debug(LD_DIR, "SR: Commit from %s", sr_commit_get_rsa_fpr(commit));
163  log_debug(LD_DIR, "SR: Commit: [TS: %" PRIu64 "] [Encoded: %s]",
164  commit->commit_ts, commit->encoded_commit);
165  log_debug(LD_DIR, "SR: Reveal: [TS: %" PRIu64 "] [Encoded: %s]",
166  commit->reveal_ts, safe_str(commit->encoded_reveal));
167 }
168 
169 /** Make sure that the commitment and reveal information in <b>commit</b>
170  * match. If they match return 0, return -1 otherwise. This function MUST be
171  * used everytime we receive a new reveal value. Furthermore, the commit
172  * object MUST have a reveal value and the hash of the reveal value. */
173 STATIC int
175 {
176  tor_assert(commit);
177 
178  log_debug(LD_DIR, "SR: Validating commit from authority %s",
179  sr_commit_get_rsa_fpr(commit));
180 
181  /* Check that the timestamps match. */
182  if (commit->commit_ts != commit->reveal_ts) {
183  log_warn(LD_BUG, "SR: Commit timestamp %" PRIu64 " doesn't match reveal "
184  "timestamp %" PRIu64, commit->commit_ts,
185  commit->reveal_ts);
186  goto invalid;
187  }
188 
189  /* Verify that the hashed_reveal received in the COMMIT message, matches
190  * the reveal we just received. */
191  {
192  /* We first hash the reveal we just received. */
193  char received_hashed_reveal[sizeof(commit->hashed_reveal)];
194 
195  /* Only sha3-256 is supported. */
196  if (commit->alg != SR_DIGEST_ALG) {
197  goto invalid;
198  }
199 
200  /* Use the invariant length since the encoded reveal variable has an
201  * extra byte for the NUL terminated byte. */
202  if (crypto_digest256(received_hashed_reveal, commit->encoded_reveal,
203  SR_REVEAL_BASE64_LEN, commit->alg) < 0) {
204  /* Unable to digest the reveal blob, this is unlikely. */
205  goto invalid;
206  }
207 
208  /* Now compare that with the hashed_reveal we received in COMMIT. */
209  if (fast_memneq(received_hashed_reveal, commit->hashed_reveal,
210  sizeof(received_hashed_reveal))) {
211  log_warn(LD_BUG, "SR: Received reveal value from authority %s "
212  "doesn't match the commit value.",
213  sr_commit_get_rsa_fpr(commit));
214  goto invalid;
215  }
216  }
217 
218  return 0;
219  invalid:
220  return -1;
221 }
222 
223 /** Return true iff the commit contains an encoded reveal value. */
224 STATIC int
226 {
227  return !fast_mem_is_zero(commit->encoded_reveal,
228  sizeof(commit->encoded_reveal));
229 }
230 
231 /** Parse the encoded commit. The format is:
232  * base64-encode( TIMESTAMP || H(REVEAL) )
233  *
234  * If successfully decoded and parsed, commit is updated and 0 is returned.
235  * On error, return -1. */
236 STATIC int
237 commit_decode(const char *encoded, sr_commit_t *commit)
238 {
239  int decoded_len = 0;
240  size_t offset = 0;
241  char b64_decoded[SR_COMMIT_LEN];
242 
243  tor_assert(encoded);
244  tor_assert(commit);
245 
246  if (strlen(encoded) > SR_COMMIT_BASE64_LEN) {
247  /* This means that if we base64 decode successfully the reveiced commit,
248  * we'll end up with a bigger decoded commit thus unusable. */
249  goto error;
250  }
251 
252  /* Decode our encoded commit. Let's be careful here since _encoded_ is
253  * coming from the network in a dirauth vote so we expect nothing more
254  * than the base64 encoded length of a commit. */
255  decoded_len = base64_decode(b64_decoded, sizeof(b64_decoded),
256  encoded, strlen(encoded));
257  if (decoded_len < 0) {
258  log_warn(LD_BUG, "SR: Commit from authority %s can't be decoded.",
259  sr_commit_get_rsa_fpr(commit));
260  goto error;
261  }
262 
263  if (decoded_len != SR_COMMIT_LEN) {
264  log_warn(LD_BUG, "SR: Commit from authority %s decoded length doesn't "
265  "match the expected length (%d vs %u).",
266  sr_commit_get_rsa_fpr(commit), decoded_len,
267  (unsigned)SR_COMMIT_LEN);
268  goto error;
269  }
270 
271  /* First is the timestamp (8 bytes). */
272  commit->commit_ts = tor_ntohll(get_uint64(b64_decoded));
273  offset += sizeof(uint64_t);
274  /* Next is hashed reveal. */
275  memcpy(commit->hashed_reveal, b64_decoded + offset,
276  sizeof(commit->hashed_reveal));
277  /* Copy the base64 blob to the commit. Useful for voting. */
278  strlcpy(commit->encoded_commit, encoded, sizeof(commit->encoded_commit));
279 
280  return 0;
281 
282  error:
283  return -1;
284 }
285 
286 /** Parse the b64 blob at <b>encoded</b> containing reveal information and
287  * store the information in-place in <b>commit</b>. Return 0 on success else
288  * a negative value. */
289 STATIC int
290 reveal_decode(const char *encoded, sr_commit_t *commit)
291 {
292  int decoded_len = 0;
293  char b64_decoded[SR_REVEAL_LEN];
294 
295  tor_assert(encoded);
296  tor_assert(commit);
297 
298  if (strlen(encoded) > SR_REVEAL_BASE64_LEN) {
299  /* This means that if we base64 decode successfully the received reveal
300  * value, we'll end up with a bigger decoded value thus unusable. */
301  goto error;
302  }
303 
304  /* Decode our encoded reveal. Let's be careful here since _encoded_ is
305  * coming from the network in a dirauth vote so we expect nothing more
306  * than the base64 encoded length of our reveal. */
307  decoded_len = base64_decode(b64_decoded, sizeof(b64_decoded),
308  encoded, strlen(encoded));
309  if (decoded_len < 0) {
310  log_warn(LD_BUG, "SR: Reveal from authority %s can't be decoded.",
311  sr_commit_get_rsa_fpr(commit));
312  goto error;
313  }
314 
315  if (decoded_len != SR_REVEAL_LEN) {
316  log_warn(LD_BUG, "SR: Reveal from authority %s decoded length is "
317  "doesn't match the expected length (%d vs %u)",
318  sr_commit_get_rsa_fpr(commit), decoded_len,
319  (unsigned)SR_REVEAL_LEN);
320  goto error;
321  }
322 
323  commit->reveal_ts = tor_ntohll(get_uint64(b64_decoded));
324  /* Copy the last part, the random value. */
325  memcpy(commit->random_number, b64_decoded + 8,
326  sizeof(commit->random_number));
327  /* Also copy the whole message to use during verification */
328  strlcpy(commit->encoded_reveal, encoded, sizeof(commit->encoded_reveal));
329 
330  return 0;
331 
332  error:
333  return -1;
334 }
335 
336 /** Encode a reveal element using a given commit object to dst which is a
337  * buffer large enough to put the base64-encoded reveal construction. The
338  * format is as follow:
339  * REVEAL = base64-encode( TIMESTAMP || H(RN) )
340  * Return base64 encoded length on success else a negative value.
341  */
342 STATIC int
343 reveal_encode(const sr_commit_t *commit, char *dst, size_t len)
344 {
345  int ret;
346  size_t offset = 0;
347  char buf[SR_REVEAL_LEN] = {0};
348 
349  tor_assert(commit);
350  tor_assert(dst);
351 
352  set_uint64(buf, tor_htonll(commit->reveal_ts));
353  offset += sizeof(uint64_t);
354  memcpy(buf + offset, commit->random_number,
355  sizeof(commit->random_number));
356 
357  /* Let's clean the buffer and then b64 encode it. */
358  memset(dst, 0, len);
359  ret = base64_encode(dst, len, buf, sizeof(buf), 0);
360  /* Wipe this buffer because it contains our random value. */
361  memwipe(buf, 0, sizeof(buf));
362  return ret;
363 }
364 
365 /** Encode the given commit object to dst which is a buffer large enough to
366  * put the base64-encoded commit. The format is as follow:
367  * COMMIT = base64-encode( TIMESTAMP || H(H(RN)) )
368  * Return base64 encoded length on success else a negative value.
369  */
370 STATIC int
371 commit_encode(const sr_commit_t *commit, char *dst, size_t len)
372 {
373  size_t offset = 0;
374  char buf[SR_COMMIT_LEN] = {0};
375 
376  tor_assert(commit);
377  tor_assert(dst);
378 
379  /* First is the timestamp (8 bytes). */
380  set_uint64(buf, tor_htonll(commit->commit_ts));
381  offset += sizeof(uint64_t);
382  /* and then the hashed reveal. */
383  memcpy(buf + offset, commit->hashed_reveal,
384  sizeof(commit->hashed_reveal));
385 
386  /* Clean the buffer and then b64 encode it. */
387  memset(dst, 0, len);
388  return base64_encode(dst, len, buf, sizeof(buf), 0);
389 }
390 
391 /** Cleanup both our global state and disk state. */
392 static void
394 {
396 }
397 
398 /** Using <b>commit</b>, return a newly allocated string containing the commit
399  * information that should be used during SRV calculation. It's the caller
400  * responsibility to free the memory. Return NULL if this is not a commit to be
401  * used for SRV calculation. */
402 static char *
404 {
405  char *element;
406  tor_assert(commit);
407 
408  if (!commit_has_reveal_value(commit)) {
409  return NULL;
410  }
411 
412  tor_asprintf(&element, "%s%s", sr_commit_get_rsa_fpr(commit),
413  commit->encoded_reveal);
414  return element;
415 }
416 
417 /** Return a srv object that is built with the construction:
418  * SRV = SHA3-256("shared-random" | INT_8(reveal_num) |
419  * INT_4(version) | HASHED_REVEALS | previous_SRV)
420  * This function cannot fail. */
421 static sr_srv_t *
422 generate_srv(const char *hashed_reveals, uint64_t reveal_num,
423  const sr_srv_t *previous_srv)
424 {
425  char msg[DIGEST256_LEN + SR_SRV_MSG_LEN] = {0};
426  size_t offset = 0;
427  sr_srv_t *srv;
428 
429  tor_assert(hashed_reveals);
430 
431  /* Add the invariant token. */
432  memcpy(msg, SR_SRV_TOKEN, SR_SRV_TOKEN_LEN);
433  offset += SR_SRV_TOKEN_LEN;
434  set_uint64(msg + offset, tor_htonll(reveal_num));
435  offset += sizeof(uint64_t);
436  set_uint32(msg + offset, htonl(SR_PROTO_VERSION));
437  offset += sizeof(uint32_t);
438  memcpy(msg + offset, hashed_reveals, DIGEST256_LEN);
439  offset += DIGEST256_LEN;
440  if (previous_srv != NULL) {
441  memcpy(msg + offset, previous_srv->value, sizeof(previous_srv->value));
442  }
443 
444  /* Ok we have our message and key for the HMAC computation, allocate our
445  * srv object and do the last step. */
446  srv = tor_malloc_zero(sizeof(*srv));
447  crypto_digest256((char *) srv->value, msg, sizeof(msg), SR_DIGEST_ALG);
448  srv->num_reveals = reveal_num;
449 
450  {
451  /* Debugging. */
452  char srv_hash_encoded[SR_SRV_VALUE_BASE64_LEN + 1];
453  sr_srv_encode(srv_hash_encoded, sizeof(srv_hash_encoded), srv);
454  log_info(LD_DIR, "SR: Generated SRV: %s", srv_hash_encoded);
455  }
456  return srv;
457 }
458 
459 /** Compare reveal values and return the result. This should exclusively be
460  * used by smartlist_sort(). */
461 static int
462 compare_reveal_(const void **_a, const void **_b)
463 {
464  const sr_commit_t *a = *_a, *b = *_b;
465  return fast_memcmp(a->hashed_reveal, b->hashed_reveal,
466  sizeof(a->hashed_reveal));
467 }
468 
469 /** Given <b>commit</b> give the line that we should place in our votes.
470  * It's the responsibility of the caller to free the string. */
471 static char *
473 {
474  char *vote_line = NULL;
475 
476  switch (phase) {
477  case SR_PHASE_COMMIT:
478  tor_asprintf(&vote_line, "%s %u %s %s %s\n",
479  commit_ns_str,
482  sr_commit_get_rsa_fpr(commit),
483  commit->encoded_commit);
484  break;
485  case SR_PHASE_REVEAL:
486  {
487  /* Send a reveal value for this commit if we have one. */
488  const char *reveal_str = commit->encoded_reveal;
489  if (fast_mem_is_zero(commit->encoded_reveal,
490  sizeof(commit->encoded_reveal))) {
491  reveal_str = "";
492  }
493  tor_asprintf(&vote_line, "%s %u %s %s %s %s\n",
494  commit_ns_str,
497  sr_commit_get_rsa_fpr(commit),
498  commit->encoded_commit, reveal_str);
499  break;
500  }
501  default:
502  tor_assert(0);
503  }
504 
505  log_debug(LD_DIR, "SR: Commit vote line: %s", vote_line);
506  return vote_line;
507 }
508 
509 /** Return a heap allocated string that contains the given <b>srv</b> string
510  * representation formatted for a networkstatus document using the
511  * <b>key</b> as the start of the line. This doesn't return NULL. */
512 static char *
513 srv_to_ns_string(const sr_srv_t *srv, const char *key)
514 {
515  char *srv_str;
516  char srv_hash_encoded[SR_SRV_VALUE_BASE64_LEN + 1];
517  tor_assert(srv);
518  tor_assert(key);
519 
520  sr_srv_encode(srv_hash_encoded, sizeof(srv_hash_encoded), srv);
521  tor_asprintf(&srv_str, "%s %" PRIu64 " %s\n", key,
522  srv->num_reveals, srv_hash_encoded);
523  log_debug(LD_DIR, "SR: Consensus SRV line: %s", srv_str);
524  return srv_str;
525 }
526 
527 /** Given the previous SRV and the current SRV, return a heap allocated
528  * string with their data that could be put in a vote or a consensus. Caller
529  * must free the returned string. Return NULL if no SRVs were provided. */
530 static char *
531 get_ns_str_from_sr_values(const sr_srv_t *prev_srv, const sr_srv_t *cur_srv)
532 {
533  smartlist_t *chunks = NULL;
534  char *srv_str;
535 
536  if (!prev_srv && !cur_srv) {
537  return NULL;
538  }
539 
540  chunks = smartlist_new();
541 
542  if (prev_srv) {
543  char *srv_line = srv_to_ns_string(prev_srv, previous_srv_str);
544  smartlist_add(chunks, srv_line);
545  }
546 
547  if (cur_srv) {
548  char *srv_line = srv_to_ns_string(cur_srv, current_srv_str);
549  smartlist_add(chunks, srv_line);
550  }
551 
552  /* Join the line(s) here in one string to return. */
553  srv_str = smartlist_join_strings(chunks, "", 0, NULL);
554  SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
555  smartlist_free(chunks);
556 
557  return srv_str;
558 }
559 
560 /** Return 1 iff the two commits have the same commitment values. This
561  * function does not care about reveal values. */
562 STATIC int
564  const sr_commit_t *commit_two)
565 {
566  tor_assert(commit_one);
567  tor_assert(commit_two);
568 
569  if (strcmp(commit_one->encoded_commit, commit_two->encoded_commit)) {
570  return 0;
571  }
572  return 1;
573 }
574 
575 /** We just received a commit from the vote of authority with
576  * <b>identity_digest</b>. Return 1 if this commit is authorititative that
577  * is, it belongs to the authority that voted it. Else return 0 if not. */
578 STATIC int
580  const char *voter_key)
581 {
582  tor_assert(commit);
583  tor_assert(voter_key);
584 
585  return fast_memeq(commit->rsa_identity, voter_key,
586  sizeof(commit->rsa_identity));
587 }
588 
589 /** Decide if the newly received <b>commit</b> should be kept depending on
590  * the current phase and state of the protocol. The <b>voter_key</b> is the
591  * RSA identity key fingerprint of the authority's vote from which the
592  * commit comes from. The <b>phase</b> is the phase we should be validating
593  * the commit for. Return 1 if the commit should be added to our state or 0
594  * if not. */
595 STATIC int
596 should_keep_commit(const sr_commit_t *commit, const char *voter_key,
597  sr_phase_t phase)
598 {
599  const sr_commit_t *saved_commit;
600 
601  tor_assert(commit);
602  tor_assert(voter_key);
603 
604  log_debug(LD_DIR, "SR: Inspecting commit from %s (voter: %s)?",
605  sr_commit_get_rsa_fpr(commit),
606  hex_str(voter_key, DIGEST_LEN));
607 
608  /* For a commit to be considered, it needs to be authoritative (it should
609  * be the voter's own commit). */
610  if (!commit_is_authoritative(commit, voter_key)) {
611  log_debug(LD_DIR, "SR: Ignoring non-authoritative commit.");
612  goto ignore;
613  }
614 
615  /* Let's make sure, for extra safety, that this fingerprint is known to
616  * us. Even though this comes from a vote, doesn't hurt to be
617  * extracareful. */
619  log_warn(LD_DIR, "SR: Fingerprint %s is not from a recognized "
620  "authority. Discarding commit.",
621  escaped(commit->rsa_identity));
622  goto ignore;
623  }
624 
625  /* Check if the authority that voted for <b>commit</b> has already posted
626  * a commit before. */
627  saved_commit = sr_state_get_commit(commit->rsa_identity);
628 
629  switch (phase) {
630  case SR_PHASE_COMMIT:
631  /* Already having a commit for an authority so ignore this one. */
632  if (saved_commit) {
633  /* Receiving known commits should happen naturally since commit phase
634  lasts multiple rounds. However if the commitment value changes
635  during commit phase, it might be a bug so log more loudly. */
636  if (!commitments_are_the_same(commit, saved_commit)) {
637  log_info(LD_DIR,
638  "SR: Received altered commit from %s in commit phase.",
639  sr_commit_get_rsa_fpr(commit));
640  } else {
641  log_debug(LD_DIR, "SR: Ignoring known commit during commit phase.");
642  }
643  goto ignore;
644  }
645 
646  /* A commit with a reveal value during commitment phase is very wrong. */
647  if (commit_has_reveal_value(commit)) {
648  log_warn(LD_DIR, "SR: Commit from authority %s has a reveal value "
649  "during COMMIT phase. (voter: %s)",
650  sr_commit_get_rsa_fpr(commit),
651  hex_str(voter_key, DIGEST_LEN));
652  goto ignore;
653  }
654  break;
655  case SR_PHASE_REVEAL:
656  /* We are now in reveal phase. We keep a commit if and only if:
657  *
658  * - We have already seen a commit by this auth, AND
659  * - the saved commit has the same commitment value as this one, AND
660  * - the saved commit has no reveal information, AND
661  * - this commit does have reveal information, AND
662  * - the reveal & commit information are matching.
663  *
664  * If all the above are true, then we are interested in this new commit
665  * for its reveal information. */
666 
667  if (!saved_commit) {
668  log_debug(LD_DIR, "SR: Ignoring commit first seen in reveal phase.");
669  goto ignore;
670  }
671 
672  if (!commitments_are_the_same(commit, saved_commit)) {
673  log_warn(LD_DIR, "SR: Commit from authority %s is different from "
674  "previous commit in our state (voter: %s)",
675  sr_commit_get_rsa_fpr(commit),
676  hex_str(voter_key, DIGEST_LEN));
677  goto ignore;
678  }
679 
680  if (commit_has_reveal_value(saved_commit)) {
681  log_debug(LD_DIR, "SR: Ignoring commit with known reveal info.");
682  goto ignore;
683  }
684 
685  if (!commit_has_reveal_value(commit)) {
686  log_debug(LD_DIR, "SR: Ignoring commit without reveal value.");
687  goto ignore;
688  }
689 
690  if (verify_commit_and_reveal(commit) < 0) {
691  log_warn(LD_BUG, "SR: Commit from authority %s has an invalid "
692  "reveal value. (voter: %s)",
693  sr_commit_get_rsa_fpr(commit),
694  hex_str(voter_key, DIGEST_LEN));
695  goto ignore;
696  }
697  break;
698  default:
699  tor_assert(0);
700  }
701 
702  return 1;
703 
704  ignore:
705  return 0;
706 }
707 
708 /** We are in reveal phase and we found a valid and verified <b>commit</b> in
709  * a vote that contains reveal values that we could use. Update the commit
710  * we have in our state. Never call this with an unverified commit. */
711 STATIC void
713 {
714  sr_commit_t *saved_commit;
715 
716  tor_assert(commit);
717 
718  /* Get the commit from our state. */
719  saved_commit = sr_state_get_commit(commit->rsa_identity);
720  tor_assert(saved_commit);
721  /* Safety net. They can not be different commitments at this point. */
722  int same_commits = commitments_are_the_same(commit, saved_commit);
723  tor_assert(same_commits);
724 
725  /* Copy reveal information to our saved commit. */
726  sr_state_copy_reveal_info(saved_commit, commit);
727 }
728 
729 /** Save <b>commit</b> to our persistent state. Depending on the current
730  * phase, different actions are taken. Steals reference of <b>commit</b>.
731  * The commit object MUST be valid and verified before adding it to the
732  * state. */
733 STATIC void
735 {
736  sr_phase_t phase = sr_state_get_phase();
737 
738  ASSERT_COMMIT_VALID(commit);
739 
740  switch (phase) {
741  case SR_PHASE_COMMIT:
742  /* During commit phase, just save any new authoritative commit */
743  sr_state_add_commit(commit);
744  break;
745  case SR_PHASE_REVEAL:
747  sr_commit_free(commit);
748  break;
749  default:
750  tor_assert(0);
751  }
752 }
753 
754 /** Return 1 if we should we keep an SRV voted by <b>n_agreements</b> auths.
755  * Return 0 if we should ignore it. */
756 static int
757 should_keep_srv(int n_agreements)
758 {
759  /* Check if the most popular SRV has reached majority. */
760  int n_voters = get_n_authorities(V3_DIRINFO);
761  int votes_required_for_majority = (n_voters / 2) + 1;
762 
763  /* We need at the very least majority to keep a value. */
764  if (n_agreements < votes_required_for_majority) {
765  log_notice(LD_DIR, "SR: SRV didn't reach majority [%d/%d]!",
766  n_agreements, votes_required_for_majority);
767  return 0;
768  }
769 
770  /* When we just computed a new SRV, we need to have super majority in order
771  * to keep it. */
772  if (sr_state_srv_is_fresh()) {
773  /* Check if we have super majority for this new SRV value. */
774  if (n_agreements < num_srv_agreements_from_vote) {
775  log_notice(LD_DIR, "SR: New SRV didn't reach agreement [%d/%d]!",
776  n_agreements, num_srv_agreements_from_vote);
777  return 0;
778  }
779  }
780 
781  return 1;
782 }
783 
784 /** Helper: compare two DIGEST256_LEN digests. */
785 static int
786 compare_srvs_(const void **_a, const void **_b)
787 {
788  const sr_srv_t *a = *_a, *b = *_b;
789  return tor_memcmp(a->value, b->value, sizeof(a->value));
790 }
791 
792 /** Return the most frequent member of the sorted list of DIGEST256_LEN
793  * digests in <b>sl</b> with the count of that most frequent element. */
794 static sr_srv_t *
796 {
797  return smartlist_get_most_frequent_(sl, compare_srvs_, count_out);
798 }
799 
800 /** Compare two SRVs. Used in smartlist sorting. */
801 static int
802 compare_srv_(const void **_a, const void **_b)
803 {
804  const sr_srv_t *a = *_a, *b = *_b;
805  return fast_memcmp(a->value, b->value,
806  sizeof(a->value));
807 }
808 
809 /** Using a list of <b>votes</b>, return the SRV object from them that has
810  * been voted by the majority of dirauths. If <b>current</b> is set, we look
811  * for the current SRV value else the previous one. The returned pointer is
812  * an object located inside a vote. NULL is returned if no appropriate value
813  * could be found. */
815 get_majority_srv_from_votes(const smartlist_t *votes, int current)
816 {
817  int count = 0;
818  sr_srv_t *most_frequent_srv = NULL;
819  sr_srv_t *the_srv = NULL;
820  smartlist_t *srv_list;
821 
822  tor_assert(votes);
823 
824  srv_list = smartlist_new();
825 
826  /* Walk over votes and register any SRVs found. */
828  sr_srv_t *srv_tmp = NULL;
829 
830  if (!v->sr_info.participate) {
831  /* Ignore vote that do not participate. */
832  continue;
833  }
834  /* Do we want previous or current SRV? */
835  srv_tmp = current ? v->sr_info.current_srv : v->sr_info.previous_srv;
836  if (!srv_tmp) {
837  continue;
838  }
839 
840  smartlist_add(srv_list, srv_tmp);
841  } SMARTLIST_FOREACH_END(v);
842 
843  smartlist_sort(srv_list, compare_srv_);
844  most_frequent_srv = smartlist_get_most_frequent_srv(srv_list, &count);
845  if (!most_frequent_srv) {
846  goto end;
847  }
848 
849  /* Was this SRV voted by enough auths for us to keep it? */
850  if (!should_keep_srv(count)) {
851  goto end;
852  }
853 
854  /* We found an SRV that we can use! Habemus SRV! */
855  the_srv = most_frequent_srv;
856 
857  {
858  /* Debugging */
859  char encoded[SR_SRV_VALUE_BASE64_LEN + 1];
860  sr_srv_encode(encoded, sizeof(encoded), the_srv);
861  log_debug(LD_DIR, "SR: Chosen SRV by majority: %s (%d votes)", encoded,
862  count);
863  }
864 
865  end:
866  /* We do not free any sr_srv_t values, we don't have the ownership. */
867  smartlist_free(srv_list);
868  return the_srv;
869 }
870 
871 /** Free a commit object. */
872 void
874 {
875  if (commit == NULL) {
876  return;
877  }
878  /* Make sure we do not leave OUR random number in memory. */
879  memwipe(commit->random_number, 0, sizeof(commit->random_number));
880  tor_free(commit);
881 }
882 
883 /** Generate the commitment/reveal value for the protocol run starting at
884  * <b>timestamp</b>. <b>my_rsa_cert</b> is our authority RSA certificate. */
885 sr_commit_t *
886 sr_generate_our_commit(time_t timestamp, const authority_cert_t *my_rsa_cert)
887 {
888  sr_commit_t *commit = NULL;
889  char digest[DIGEST_LEN];
890 
891  tor_assert(my_rsa_cert);
892 
893  /* Get our RSA identity fingerprint */
894  if (crypto_pk_get_digest(my_rsa_cert->identity_key, digest) < 0) {
895  goto error;
896  }
897 
898  /* New commit with our identity key. */
899  commit = commit_new(digest);
900 
901  /* Generate the reveal random value */
903  sizeof(commit->random_number));
904  commit->commit_ts = commit->reveal_ts = timestamp;
905 
906  /* Now get the base64 blob that corresponds to our reveal */
907  if (reveal_encode(commit, commit->encoded_reveal,
908  sizeof(commit->encoded_reveal)) < 0) {
909  log_err(LD_DIR, "SR: Unable to encode our reveal value!");
910  goto error;
911  }
912 
913  /* Now let's create the commitment */
914  tor_assert(commit->alg == SR_DIGEST_ALG);
915  /* The invariant length is used here since the encoded reveal variable
916  * has an extra byte added for the NULL terminated byte. */
917  if (crypto_digest256(commit->hashed_reveal, commit->encoded_reveal,
918  SR_REVEAL_BASE64_LEN, commit->alg) < 0) {
919  goto error;
920  }
921 
922  /* Now get the base64 blob that corresponds to our commit. */
923  if (commit_encode(commit, commit->encoded_commit,
924  sizeof(commit->encoded_commit)) < 0) {
925  log_err(LD_DIR, "SR: Unable to encode our commit value!");
926  goto error;
927  }
928 
929  log_debug(LD_DIR, "SR: Generated our commitment:");
930  commit_log(commit);
931  /* Our commit better be valid :). */
932  commit->valid = 1;
933  return commit;
934 
935  error:
936  sr_commit_free(commit);
937  return NULL;
938 }
939 
940 /** Compute the shared random value based on the active commits in our
941  * state. */
942 void
944 {
945  uint64_t reveal_num = 0;
946  char *reveals = NULL;
947  smartlist_t *chunks, *commits;
948  digestmap_t *state_commits;
949 
950  /* Computing a shared random value in the commit phase is very wrong. This
951  * should only happen at the very end of the reveal phase when a new
952  * protocol run is about to start. */
953  if (BUG(sr_state_get_phase() != SR_PHASE_REVEAL))
954  return;
955  state_commits = sr_state_get_commits();
956 
957  commits = smartlist_new();
958  chunks = smartlist_new();
959 
960  /* We must make a list of commit ordered by authority fingerprint in
961  * ascending order as specified by proposal 250. */
962  DIGESTMAP_FOREACH(state_commits, key, sr_commit_t *, c) {
963  /* Extra safety net, make sure we have valid commit before using it. */
965  /* Let's not use a commit from an authority that we don't know. It's
966  * possible that an authority could be removed during a protocol run so
967  * that commit value should never be used in the SRV computation. */
968  if (trusteddirserver_get_by_v3_auth_digest(c->rsa_identity) == NULL) {
969  log_warn(LD_DIR, "SR: Fingerprint %s is not from a recognized "
970  "authority. Discarding commit for the SRV computation.",
971  sr_commit_get_rsa_fpr(c));
972  continue;
973  }
974  /* We consider this commit valid. */
975  smartlist_add(commits, c);
978 
979  /* Now for each commit for that sorted list in ascending order, we'll
980  * build the element for each authority that needs to go into the srv
981  * computation. */
982  SMARTLIST_FOREACH_BEGIN(commits, const sr_commit_t *, c) {
983  char *element = get_srv_element_from_commit(c);
984  if (element) {
985  smartlist_add(chunks, element);
986  reveal_num++;
987  }
988  } SMARTLIST_FOREACH_END(c);
989  smartlist_free(commits);
990 
991  {
992  /* Join all reveal values into one giant string that we'll hash so we
993  * can generated our shared random value. */
994  sr_srv_t *current_srv;
995  char hashed_reveals[DIGEST256_LEN];
996  reveals = smartlist_join_strings(chunks, "", 0, NULL);
997  SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
998  smartlist_free(chunks);
999  if (crypto_digest256(hashed_reveals, reveals, strlen(reveals),
1000  SR_DIGEST_ALG) < 0) {
1001  goto end;
1002  }
1003  current_srv = generate_srv(hashed_reveals, reveal_num,
1005  sr_state_set_current_srv(current_srv);
1006  /* We have a fresh SRV, flag our state. */
1008  }
1009 
1010  end:
1011  tor_free(reveals);
1012 }
1013 
1014 /** Parse a commit from a vote or from our disk state and return a newly
1015  * allocated commit object. NULL is returned on error.
1016  *
1017  * The commit's data is in <b>args</b> and the order matters very much:
1018  * version, algname, RSA fingerprint, commit value[, reveal value]
1019  */
1020 sr_commit_t *
1022 {
1023  uint32_t version;
1024  char *value, digest[DIGEST_LEN];
1025  digest_algorithm_t alg;
1026  const char *rsa_identity_fpr;
1027  sr_commit_t *commit = NULL;
1028 
1029  if (smartlist_len(args) < 4) {
1030  goto error;
1031  }
1032 
1033  /* First is the version number of the SR protocol which indicates at which
1034  * version that commit was created. */
1035  value = smartlist_get(args, 0);
1036  version = (uint32_t) tor_parse_ulong(value, 10, 1, UINT32_MAX, NULL, NULL);
1037  if (version > SR_PROTO_VERSION) {
1038  log_info(LD_DIR, "SR: Commit version %" PRIu32 " (%s) is not supported.",
1039  version, escaped(value));
1040  goto error;
1041  }
1042 
1043  /* Second is the algorithm. */
1044  value = smartlist_get(args, 1);
1046  if (alg != SR_DIGEST_ALG) {
1047  log_warn(LD_BUG, "SR: Commit algorithm %s is not recognized.",
1048  escaped(value));
1049  goto error;
1050  }
1051 
1052  /* Third argument is the RSA fingerprint of the auth and turn it into a
1053  * digest value. */
1054  rsa_identity_fpr = smartlist_get(args, 2);
1055  if (base16_decode(digest, DIGEST_LEN, rsa_identity_fpr,
1056  HEX_DIGEST_LEN) < 0) {
1057  log_warn(LD_DIR, "SR: RSA fingerprint %s not decodable",
1058  escaped(rsa_identity_fpr));
1059  goto error;
1060  }
1061 
1062  /* Allocate commit since we have a valid identity now. */
1063  commit = commit_new(digest);
1064 
1065  /* Fourth argument is the commitment value base64-encoded. */
1066  value = smartlist_get(args, 3);
1067  if (commit_decode(value, commit) < 0) {
1068  goto error;
1069  }
1070 
1071  /* (Optional) Fifth argument is the revealed value. */
1072  if (smartlist_len(args) > 4) {
1073  value = smartlist_get(args, 4);
1074  if (reveal_decode(value, commit) < 0) {
1075  goto error;
1076  }
1077  }
1078 
1079  return commit;
1080 
1081  error:
1082  sr_commit_free(commit);
1083  return NULL;
1084 }
1085 
1086 /** Called when we are done parsing a vote by <b>voter_key</b> that might
1087  * contain some useful <b>commits</b>. Find if any of them should be kept
1088  * and update our state accordingly. Once done, the list of commitments will
1089  * be empty. */
1090 void
1092 {
1093  char rsa_identity[DIGEST_LEN];
1094 
1095  tor_assert(voter_key);
1096 
1097  /* It's possible that the vote has _NO_ commits. */
1098  if (commits == NULL) {
1099  return;
1100  }
1101 
1102  /* Get the RSA identity fingerprint of this voter */
1103  if (crypto_pk_get_digest(voter_key, rsa_identity) < 0) {
1104  return;
1105  }
1106 
1107  SMARTLIST_FOREACH_BEGIN(commits, sr_commit_t *, commit) {
1108  /* We won't need the commit in this list anymore, kept or not. */
1109  SMARTLIST_DEL_CURRENT(commits, commit);
1110  /* Check if this commit is valid and should be stored in our state. */
1111  if (!should_keep_commit(commit, rsa_identity,
1112  sr_state_get_phase())) {
1113  sr_commit_free(commit);
1114  continue;
1115  }
1116  /* Ok, we have a valid commit now that we are about to put in our state.
1117  * so flag it valid from now on. */
1118  commit->valid = 1;
1119  /* Everything lines up: save this commit to state then! */
1120  save_commit_to_state(commit);
1121  } SMARTLIST_FOREACH_END(commit);
1122 }
1123 
1124 /** Return a heap-allocated string containing commits that should be put in
1125  * the votes. It's the responsibility of the caller to free the string.
1126  * This always return a valid string, either empty or with line(s). */
1127 char *
1129 {
1130  char *vote_str = NULL;
1131  digestmap_t *state_commits;
1132  smartlist_t *chunks = smartlist_new();
1133  const or_options_t *options = get_options();
1134 
1135  /* Are we participating in the protocol? */
1136  if (!options->AuthDirSharedRandomness) {
1137  goto end;
1138  }
1139 
1140  log_debug(LD_DIR, "SR: Preparing our vote info:");
1141 
1142  /* First line, put in the vote the participation flag. */
1143  {
1144  char *sr_flag_line;
1145  tor_asprintf(&sr_flag_line, "%s\n", sr_flag_ns_str);
1146  smartlist_add(chunks, sr_flag_line);
1147  }
1148 
1149  /* In our vote we include every commitment in our permanent state. */
1150  state_commits = sr_state_get_commits();
1151  smartlist_t *state_commit_vote_lines = smartlist_new();
1152  DIGESTMAP_FOREACH(state_commits, key, const sr_commit_t *, commit) {
1153  char *line = get_vote_line_from_commit(commit, sr_state_get_phase());
1154  smartlist_add(state_commit_vote_lines, line);
1156 
1157  /* Sort the commit strings by version (string, not numeric), algorithm,
1158  * and fingerprint. This makes sure the commit lines in votes are in a
1159  * recognisable, stable order. */
1160  smartlist_sort_strings(state_commit_vote_lines);
1161 
1162  /* Now add the sorted list of commits to the vote */
1163  smartlist_add_all(chunks, state_commit_vote_lines);
1164  smartlist_free(state_commit_vote_lines);
1165 
1166  /* Add the SRV value(s) if any. */
1167  {
1170  if (srv_lines) {
1171  smartlist_add(chunks, srv_lines);
1172  }
1173  }
1174 
1175  end:
1176  vote_str = smartlist_join_strings(chunks, "", 0, NULL);
1177  SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
1178  smartlist_free(chunks);
1179  return vote_str;
1180 }
1181 
1182 /** Return a heap-allocated string that should be put in the consensus and
1183  * contains the shared randomness values. It's the responsibility of the
1184  * caller to free the string. NULL is returned if no SRV(s) available.
1185  *
1186  * This is called when a consensus (any flavor) is bring created thus it
1187  * should NEVER change the state nor the state should be changed in between
1188  * consensus creation.
1189  *
1190  * <b>num_srv_agreements</b> is taken from the votes thus the voted value
1191  * that should be used.
1192  * */
1193 char *
1195  int32_t num_srv_agreements)
1196 {
1197  char *srv_str;
1198  const or_options_t *options = get_options();
1199 
1200  tor_assert(votes);
1201 
1202  /* Not participating, avoid returning anything. */
1203  if (!options->AuthDirSharedRandomness) {
1204  log_info(LD_DIR, "SR: Support disabled (AuthDirSharedRandomness %d)",
1205  options->AuthDirSharedRandomness);
1206  goto end;
1207  }
1208 
1209  /* Set the global value of AuthDirNumSRVAgreements found in the votes. */
1210  num_srv_agreements_from_vote = num_srv_agreements;
1211 
1212  /* Check the votes and figure out if SRVs should be included in the final
1213  * consensus. */
1214  sr_srv_t *prev_srv = get_majority_srv_from_votes(votes, 0);
1215  sr_srv_t *cur_srv = get_majority_srv_from_votes(votes, 1);
1216  srv_str = get_ns_str_from_sr_values(prev_srv, cur_srv);
1217  if (!srv_str) {
1218  goto end;
1219  }
1220 
1221  return srv_str;
1222  end:
1223  return NULL;
1224 }
1225 
1226 /** We just computed a new <b>consensus</b>. Update our state with the SRVs
1227  * from the consensus (might be NULL as well). Register the SRVs in our SR
1228  * state and prepare for the upcoming protocol round. */
1229 void
1231 {
1232  const or_options_t *options = get_options();
1233 
1234  /* Don't act if our state hasn't been initialized. We can be called during
1235  * boot time when loading consensus from disk which is prior to the
1236  * initialization of the SR subsystem. We also should not be doing
1237  * anything if we are _not_ a directory authority and if we are a bridge
1238  * authority. */
1239  if (!sr_state_is_initialized() || !authdir_mode_v3(options) ||
1240  authdir_mode_bridge(options)) {
1241  return;
1242  }
1243 
1244  /* Set the majority voted SRVs in our state even if both are NULL. It
1245  * doesn't matter this is what the majority has decided. Obviously, we can
1246  * only do that if we have a consensus. */
1247  if (consensus) {
1248  /* Start by freeing the current SRVs since the SRVs we believed during
1249  * voting do not really matter. Now that all the votes are in, we use the
1250  * majority's opinion on which are the active SRVs. */
1252  /* Reset the fresh flag of the SRV so we know that from now on we don't
1253  * have a new SRV to vote for. We just used the one from the consensus
1254  * decided by the majority. */
1256  /* Set the SR values from the given consensus. */
1257  sr_state_set_previous_srv(sr_srv_dup(consensus->sr_info.previous_srv));
1258  sr_state_set_current_srv(sr_srv_dup(consensus->sr_info.current_srv));
1259  }
1260 
1261  /* Prepare our state so that it's ready for the next voting period. */
1262  sr_state_update(voting_schedule_get_next_valid_after_time());
1263 }
1264 
1265 /** Initialize shared random subsystem. This MUST be called early in the boot
1266  * process of tor. Return 0 on success else -1 on error. */
1267 int
1268 sr_init(int save_to_disk)
1269 {
1270  return sr_state_init(save_to_disk, 1);
1271 }
1272 
1273 /** Save our state to disk and cleanup everything. */
1274 void
1276 {
1277  sr_state_save();
1278  sr_cleanup();
1279 }
1280 
1281 #ifdef TOR_UNIT_TESTS
1282 
1283 /** Set the global value of number of SRV agreements so the test can play
1284  * along by calling specific functions that don't parse the votes prior for
1285  * the AuthDirNumSRVAgreements value. */
1286 void
1287 set_num_srv_agreements(int32_t value)
1288 {
1290 }
1291 
1292 #endif /* defined(TOR_UNIT_TESTS) */
static uint64_t tor_ntohll(uint64_t a)
Definition: bytes.h:193
static sr_srv_t * smartlist_get_most_frequent_srv(const smartlist_t *sl, int *count_out)
const sr_srv_t * sr_state_get_current_srv(void)
Common functions for using (pseudo-)random number generators.
static char * get_vote_line_from_commit(const sr_commit_t *commit, sr_phase_t phase)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
char encoded_reveal[SR_REVEAL_BASE64_LEN+1]
Header for shared_random_state.c.
static int compare_srv_(const void **_a, const void **_b)
#define SR_COMMIT_LEN
Definition: shared_random.h:30
char rsa_identity[DIGEST_LEN]
Definition: shared_random.h:80
uint64_t num_reveals
Definition: shared_random.h:64
void sr_state_update(time_t valid_after)
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: parse_int.c:78
static uint64_t get_uint64(const void *cp)
Definition: bytes.h:66
STATIC int commit_is_authoritative(const sr_commit_t *commit, const char *voter_key)
#define SR_PROTO_VERSION
Definition: shared_random.h:18
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:55
unsigned int valid
Definition: shared_random.h:74
sr_commit_t * sr_state_get_commit(const char *rsa_identity)
int crypto_digest_algorithm_parse_name(const char *name)
Definition: crypto_digest.c:68
void smartlist_add(smartlist_t *sl, void *element)
#define SR_REVEAL_BASE64_LEN
Definition: shared_random.h:44
sr_phase_t sr_state_get_phase(void)
STATIC int commitments_are_the_same(const sr_commit_t *commit_one, const sr_commit_t *commit_two)
void sr_compute_srv(void)
STATIC int commit_decode(const char *encoded, sr_commit_t *commit)
int sr_state_init(int save_to_disk, int read_from_disk)
static void commit_log(const sr_commit_t *commit)
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:67
static char * srv_to_ns_string(const sr_srv_t *srv, const char *key)
Header file for config.c.
char * sr_get_string_for_consensus(const smartlist_t *votes, int32_t num_srv_agreements)
const or_options_t * get_options(void)
Definition: config.c:941
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define tor_assert(expr)
Definition: util_bug.h:102
uint64_t commit_ts
Definition: shared_random.h:99
static sr_srv_t * generate_srv(const char *hashed_reveals, uint64_t reveal_num, const sr_srv_t *previous_srv)
int AuthDirSharedRandomness
unsigned int sr_state_srv_is_fresh(void)
void * smartlist_get_most_frequent_(const smartlist_t *sl, int(*compare)(const void **a, const void **b), int *count_out)
Definition: smartlist.c:348
#define tor_free(p)
Definition: malloc.h:52
#define SR_SRV_VALUE_BASE64_LEN
Definition: shared_random.h:48
#define SMARTLIST_DEL_CURRENT(sl, var)
void sr_state_add_commit(sr_commit_t *commit)
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
smartlist_t * smartlist_new(void)
const sr_srv_t * sr_state_get_previous_srv(void)
networkstatus_sr_info_t sr_info
STATIC int reveal_decode(const char *encoded, sr_commit_t *commit)
#define STATIC
Definition: testsupport.h:32
static void set_uint32(void *cp, uint32_t v)
Definition: bytes.h:87
void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags)
Definition: binascii.c:215
Header file for directory authority mode.
sr_phase_t
Definition: shared_random.h:54
#define DIGEST256_LEN
Definition: digest_sizes.h:23
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Definition: map.h:153
Common functions for cryptographic routines.
void sr_save_and_cleanup(void)
uint64_t reveal_ts
Definition: shared_random.h:86
static char * get_ns_str_from_sr_values(const sr_srv_t *prev_srv, const sr_srv_t *cur_srv)
int tor_memcmp(const void *a, const void *b, size_t len)
Definition: di_ops.c:31
void sr_handle_received_commits(smartlist_t *commits, crypto_pk_t *voter_key)
void sr_commit_free_(sr_commit_t *commit)
STATIC sr_srv_t * get_majority_srv_from_votes(const smartlist_t *votes, int current)
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int sr_state_is_initialized(void)
void sr_state_free_all(void)
static void set_uint64(void *cp, uint64_t v)
Definition: bytes.h:96
uint8_t value[DIGEST256_LEN]
Definition: shared_random.h:66
#define DIGEST_LEN
Definition: digest_sizes.h:20
uint8_t random_number[SR_RANDOM_NUMBER_LEN]
Definition: shared_random.h:97
Master header file for Tor-specific functionality.
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
#define fast_memneq(a, b, c)
Definition: di_ops.h:42
#define ASSERT_COMMIT_VALID(c)
Definition: shared_random.h:51
Header file for shared_random_client.c.
Header for routerkeys.c.
STATIC int reveal_encode(const sr_commit_t *commit, char *dst, size_t len)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
static uint64_t tor_htonll(uint64_t a)
Definition: bytes.h:184
STATIC void save_commit_during_reveal_phase(const sr_commit_t *commit)
void sr_state_save(void)
Header file for voting_schedule.c.
static int compare_reveal_(const void **_a, const void **_b)
static int compare_srvs_(const void **_a, const void **_b)
#define DIGESTMAP_FOREACH_END
Definition: map.h:167
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
#define LD_DIR
Definition: log.h:88
static sr_commit_t * commit_new(const char *rsa_identity)
STATIC int commit_encode(const sr_commit_t *commit, char *dst, size_t len)
digest_algorithm_t alg
Definition: shared_random.h:72
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
Definition: crypto_digest.c:44
void sr_act_post_consensus(const networkstatus_t *consensus)
char * sr_get_string_for_vote(void)
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:396
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Header file for router.c.
const char * escaped(const char *s)
Definition: escape.c:126
#define SR_SRV_TOKEN
Definition: shared_random.h:22
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
sr_srv_t * sr_srv_dup(const sr_srv_t *orig)
STATIC int verify_commit_and_reveal(const sr_commit_t *commit)
STATIC int should_keep_commit(const sr_commit_t *commit, const char *voter_key, sr_phase_t phase)
#define SR_SRV_MSG_LEN
Definition: shared_random.h:36
void crypto_strongest_rand(uint8_t *out, size_t out_len)
Definition: crypto_rand.c:340
sr_commit_t * sr_parse_commit(const smartlist_t *args)
Definition: or.h:894
#define SR_SRV_TOKEN_LEN
Definition: shared_random.h:24
crypto_pk_t * identity_key
#define fast_memcmp(a, b, c)
Definition: di_ops.h:28
#define SR_REVEAL_LEN
Definition: shared_random.h:33
static char * get_srv_element_from_commit(const sr_commit_t *commit)
Header for confmgt.c.
#define SR_DIGEST_ALG
Definition: shared_random.h:20
void sr_state_copy_reveal_info(sr_commit_t *saved_commit, const sr_commit_t *commit)
static void sr_cleanup(void)
void sr_state_set_current_srv(const sr_srv_t *srv)
Header file for dirlist.c.
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:167
#define SR_COMMIT_BASE64_LEN
Definition: shared_random.h:41
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
STATIC void save_commit_to_state(sr_commit_t *commit)
static int should_keep_srv(int n_agreements)
static int32_t num_srv_agreements_from_vote
void sr_state_set_fresh_srv(void)
void sr_state_clean_srvs(void)
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
void sr_state_set_previous_srv(const sr_srv_t *srv)
digest_algorithm_t
Definition: crypto_digest.h:44
STATIC int commit_has_reveal_value(const sr_commit_t *commit)
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
Authority certificate structure.
This file contains ABI/API of the shared random protocol defined in proposal #250....
int sr_init(int save_to_disk)
sr_commit_t * sr_generate_our_commit(time_t timestamp, const authority_cert_t *my_rsa_cert)
void sr_state_unset_fresh_srv(void)
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
digestmap_t * sr_state_get_commits(void)
static const char previous_srv_str[]
Networkstatus consensus/vote structure.
#define fast_memeq(a, b, c)
Definition: di_ops.h:35