shared_random.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_SHARED_RANDOM_H
  4. #define TOR_SHARED_RANDOM_H
  5. /*
  6. * This file contains ABI/API of the shared random protocol defined in
  7. * proposal #250. Every public functions and data structure are namespaced
  8. * with "sr_" which stands for shared random.
  9. */
  10. #include "or.h"
  11. /* Protocol version */
  12. #define SR_PROTO_VERSION 1
  13. /* Default digest algorithm. */
  14. #define SR_DIGEST_ALG DIGEST_SHA3_256
  15. /* Invariant token in the SRV calculation. */
  16. #define SR_SRV_TOKEN "shared-random"
  17. /* Don't count the NUL terminated byte even though the TOKEN has it. */
  18. #define SR_SRV_TOKEN_LEN (sizeof(SR_SRV_TOKEN) - 1)
  19. /* Length of the random number (in bytes). */
  20. #define SR_RANDOM_NUMBER_LEN 32
  21. /* Size of a decoded commit value in a vote or state. It's a hash and a
  22. * timestamp. It adds up to 40 bytes. */
  23. #define SR_COMMIT_LEN (sizeof(uint64_t) + DIGEST256_LEN)
  24. /* Size of a decoded reveal value from a vote or state. It's a 64 bit
  25. * timestamp and the hashed random number. This adds up to 40 bytes. */
  26. #define SR_REVEAL_LEN (sizeof(uint64_t) + DIGEST256_LEN)
  27. /* Size of SRV message length. The construction is has follow:
  28. * "shared-random" | INT_8(reveal_num) | INT_8(version) | PREV_SRV */
  29. #define SR_SRV_MSG_LEN \
  30. (SR_SRV_TOKEN_LEN + 1 + 1 + DIGEST256_LEN)
  31. /* Length of base64 encoded commit NOT including the NULL terminated byte.
  32. * Formula is taken from base64_encode_size. */
  33. #define SR_COMMIT_BASE64_LEN \
  34. (((SR_COMMIT_LEN - 1) / 3) * 4 + 4)
  35. /* Length of base64 encoded reveal NOT including the NULL terminated byte.
  36. * Formula is taken from base64_encode_size. This adds up to 56 bytes. */
  37. #define SR_REVEAL_BASE64_LEN \
  38. (((SR_REVEAL_LEN - 1) / 3) * 4 + 4)
  39. /* Length of base64 encoded shared random value. It's 32 bytes long so 44
  40. * bytes from the base64_encode_size formula. That includes the '='
  41. * character at the end. */
  42. #define SR_SRV_VALUE_BASE64_LEN \
  43. (((DIGEST256_LEN - 1) / 3) * 4 + 4)
  44. /* Protocol phase. */
  45. typedef enum {
  46. /* Commitment phase */
  47. SR_PHASE_COMMIT = 1,
  48. /* Reveal phase */
  49. SR_PHASE_REVEAL = 2,
  50. } sr_phase_t;
  51. /* A shared random value (SRV). */
  52. typedef struct sr_srv_t {
  53. /* The number of reveal values used to derive this SRV. */
  54. int num_reveals;
  55. /* The actual value. This is the stored result of SHA3-256. */
  56. uint8_t value[DIGEST256_LEN];
  57. } sr_srv_t;
  58. /* A commit (either ours or from another authority). */
  59. typedef struct sr_commit_t {
  60. /* Hashing algorithm used. */
  61. digest_algorithm_t alg;
  62. /* Commit owner info */
  63. /* The RSA identity key of the authority. */
  64. char rsa_identity[DIGEST_LEN];
  65. /* Commitment information */
  66. /* Timestamp of reveal. Correspond to TIMESTAMP. */
  67. uint64_t reveal_ts;
  68. /* H(REVEAL) as found in COMMIT message. */
  69. char hashed_reveal[DIGEST256_LEN];
  70. /* Base64 encoded COMMIT. We use this to put it in our vote. */
  71. char encoded_commit[SR_COMMIT_BASE64_LEN + 1];
  72. /* Reveal information */
  73. /* H(RN) which is what we used as the random value for this commit. We
  74. * don't use the raw bytes since those are sent on the network thus
  75. * avoiding possible information leaks of our PRNG. */
  76. uint8_t random_number[SR_RANDOM_NUMBER_LEN];
  77. /* Timestamp of commit. Correspond to TIMESTAMP. */
  78. uint64_t commit_ts;
  79. /* This is the whole reveal message. We use it during verification */
  80. char encoded_reveal[SR_REVEAL_BASE64_LEN + 1];
  81. } sr_commit_t;
  82. /* API */
  83. /* Public methods: */
  84. int sr_init(int save_to_disk);
  85. void sr_save_and_cleanup(void);
  86. void sr_act_post_consensus(const networkstatus_t *consensus);
  87. void sr_handle_received_commits(smartlist_t *commits,
  88. crypto_pk_t *voter_key);
  89. sr_commit_t *sr_parse_commit(const smartlist_t *args);
  90. sr_srv_t *sr_parse_srv(const smartlist_t *args);
  91. char *sr_get_string_for_vote(void);
  92. char *sr_get_string_for_consensus(const smartlist_t *votes);
  93. void sr_commit_free(sr_commit_t *commit);
  94. void sr_srv_encode(char *dst, const sr_srv_t *srv);
  95. /* Private methods (only used by shared_random_state.c): */
  96. static inline
  97. const char *sr_commit_get_rsa_fpr(const sr_commit_t *commit)
  98. {
  99. return hex_str((const char *) commit->rsa_identity,
  100. sizeof(commit->rsa_identity));
  101. }
  102. void sr_compute_srv(void);
  103. sr_commit_t *sr_generate_our_commit(time_t timestamp,
  104. const authority_cert_t *my_rsa_cert);
  105. #ifdef SHARED_RANDOM_PRIVATE
  106. /* Encode */
  107. STATIC int reveal_encode(const sr_commit_t *commit, char *dst, size_t len);
  108. STATIC int commit_encode(const sr_commit_t *commit, char *dst, size_t len);
  109. /* Decode. */
  110. STATIC int commit_decode(const char *encoded, sr_commit_t *commit);
  111. STATIC int reveal_decode(const char *encoded, sr_commit_t *commit);
  112. STATIC int commit_has_reveal_value(const sr_commit_t *commit);
  113. STATIC int verify_commit_and_reveal(const sr_commit_t *commit);
  114. STATIC sr_srv_t *get_majority_srv_from_votes(const smartlist_t *votes,
  115. int current);
  116. STATIC void save_commit_to_state(sr_commit_t *commit);
  117. STATIC sr_srv_t *srv_dup(const sr_srv_t *orig);
  118. STATIC int commitments_are_the_same(const sr_commit_t *commit_one,
  119. const sr_commit_t *commit_two);
  120. STATIC int commit_is_authoritative(const sr_commit_t *commit,
  121. const char *voter_key);
  122. STATIC int should_keep_commit(const sr_commit_t *commit,
  123. const char *voter_key,
  124. sr_phase_t phase);
  125. STATIC void save_commit_during_reveal_phase(const sr_commit_t *commit);
  126. #endif /* SHARED_RANDOM_PRIVATE */
  127. #endif /* TOR_SHARED_RANDOM_H */