commit e62f3133bb0404b3c70dd59f37b2706bf75eb800 Author: David Goulet dgoulet@torproject.org Date: Thu May 26 12:25:01 2016 -0400
prop250: Change reveal_num to uint64_t and version to uint32_t
Signed-off-by: David Goulet dgoulet@torproject.org --- src/or/shared_random.c | 26 +++++++++++++------------- src/or/shared_random.h | 6 +++--- src/or/shared_random_state.c | 4 ++-- src/or/shared_random_state.h | 4 ++-- src/test/test_shared_random.c | 3 --- 5 files changed, 20 insertions(+), 23 deletions(-)
diff --git a/src/or/shared_random.c b/src/or/shared_random.c index 599273f..5f61170 100644 --- a/src/or/shared_random.c +++ b/src/or/shared_random.c @@ -403,10 +403,10 @@ get_srv_element_from_commit(const sr_commit_t *commit)
/* Return a srv object that is built with the construction: * SRV = SHA3-256("shared-random" | INT_8(reveal_num) | - * INT_8(version) | HASHED_REVEALS | previous_SRV) + * INT_4(version) | HASHED_REVEALS | previous_SRV) * This function cannot fail. */ static sr_srv_t * -generate_srv(const char *hashed_reveals, uint8_t reveal_num, +generate_srv(const char *hashed_reveals, uint64_t reveal_num, const sr_srv_t *previous_srv) { char msg[DIGEST256_LEN + SR_SRV_MSG_LEN] = {0}; @@ -418,10 +418,10 @@ generate_srv(const char *hashed_reveals, uint8_t reveal_num, /* Add the invariant token. */ memcpy(msg, SR_SRV_TOKEN, SR_SRV_TOKEN_LEN); offset += SR_SRV_TOKEN_LEN; - set_uint8(msg + offset, reveal_num); - offset += 1; - set_uint8(msg + offset, SR_PROTO_VERSION); - offset += 1; + set_uint64(msg + offset, tor_htonll(reveal_num)); + offset += sizeof(uint64_t); + set_uint32(msg + offset, htonl(SR_PROTO_VERSION)); + offset += sizeof(uint32_t); memcpy(msg + offset, hashed_reveals, DIGEST256_LEN); offset += DIGEST256_LEN; if (previous_srv != NULL) { @@ -505,7 +505,7 @@ srv_to_ns_string(const sr_srv_t *srv, const char *key) tor_assert(key);
sr_srv_encode(srv_hash_encoded, sizeof(srv_hash_encoded), srv); - tor_asprintf(&srv_str, "%s %d %s\n", key, + tor_asprintf(&srv_str, "%s %" PRIu64 " %s\n", key, srv->num_reveals, srv_hash_encoded); log_debug(LD_DIR, "SR: Consensus SRV line: %s", srv_str); return srv_str; @@ -962,7 +962,7 @@ sr_generate_our_commit(time_t timestamp, const authority_cert_t *my_rsa_cert) void sr_compute_srv(void) { - size_t reveal_num = 0; + uint64_t reveal_num = 0; char *reveals = NULL; smartlist_t *chunks, *commits; digestmap_t *state_commits; @@ -1019,8 +1019,7 @@ sr_compute_srv(void) SR_DIGEST_ALG)) { goto end; } - tor_assert(reveal_num < UINT8_MAX); - current_srv = generate_srv(hashed_reveals, (uint8_t) reveal_num, + current_srv = generate_srv(hashed_reveals, reveal_num, sr_state_get_previous_srv()); sr_state_set_current_srv(current_srv); /* We have a fresh SRV, flag our state. */ @@ -1042,7 +1041,8 @@ sr_srv_t * sr_parse_srv(const smartlist_t *args) { char *value; - int num_reveals, ok, ret; + int ok, ret; + uint64_t num_reveals; sr_srv_t *srv = NULL;
tor_assert(args); @@ -1052,8 +1052,8 @@ sr_parse_srv(const smartlist_t *args) }
/* First argument is the number of reveal values */ - num_reveals = (int)tor_parse_long(smartlist_get(args, 0), - 10, 0, INT32_MAX, &ok, NULL); + num_reveals = tor_parse_uint64(smartlist_get(args, 0), + 10, 0, UINT64_MAX, &ok, NULL); if (!ok) { goto end; } diff --git a/src/or/shared_random.h b/src/or/shared_random.h index 4b16d26..6d68ad7 100644 --- a/src/or/shared_random.h +++ b/src/or/shared_random.h @@ -30,9 +30,9 @@ * timestamp and the hashed random number. This adds up to 40 bytes. */ #define SR_REVEAL_LEN (sizeof(uint64_t) + DIGEST256_LEN) /* Size of SRV message length. The construction is has follow: - * "shared-random" | INT_8(reveal_num) | INT_8(version) | PREV_SRV */ + * "shared-random" | INT_8(reveal_num) | INT_4(version) | PREV_SRV */ #define SR_SRV_MSG_LEN \ - (SR_SRV_TOKEN_LEN + 1 + 1 + DIGEST256_LEN) + (SR_SRV_TOKEN_LEN + sizeof(uint64_t) + sizeof(uint32_t) + DIGEST256_LEN)
/* Length of base64 encoded commit NOT including the NULL terminated byte. * Formula is taken from base64_encode_size. */ @@ -62,7 +62,7 @@ typedef enum { /* A shared random value (SRV). */ typedef struct sr_srv_t { /* The number of reveal values used to derive this SRV. */ - int num_reveals; + uint64_t num_reveals; /* The actual value. This is the stored result of SHA3-256. */ uint8_t value[DIGEST256_LEN]; } sr_srv_t; diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c index 87126f5..eeffb14 100644 --- a/src/or/shared_random_state.c +++ b/src/or/shared_random_state.c @@ -60,7 +60,7 @@ disk_state_validate_cb(void *old_state, void *state, void *default_state,
/* Array of variables that are saved to disk as a persistent state. */ static config_var_t state_vars[] = { - V(Version, INT, "0"), + V(Version, UINT, "0"), V(TorVersion, STRING, NULL), V(ValidAfter, ISOTIME, NULL), V(ValidUntil, ISOTIME, NULL), @@ -590,7 +590,7 @@ disk_state_put_srv_line(const sr_srv_t *srv, config_line_t *line) return; } sr_srv_encode(encoded, sizeof(encoded), srv); - tor_asprintf(&line->value, "%d %s", srv->num_reveals, encoded); + tor_asprintf(&line->value, "%" PRIu64 " %s", srv->num_reveals, encoded); }
/* Reset disk state that is free allocated memory and zeroed the object. */ diff --git a/src/or/shared_random_state.h b/src/or/shared_random_state.h index d013650..305797f 100644 --- a/src/or/shared_random_state.h +++ b/src/or/shared_random_state.h @@ -36,7 +36,7 @@ typedef struct sr_state_t { /* Filename of the state file on disk. */ char *fname; /* Version of the protocol. */ - uint8_t version; + uint32_t version; /* The valid-after of the voting period we have prepared the state for. */ time_t valid_after; /* Until when is this state valid? */ @@ -76,7 +76,7 @@ typedef struct sr_state_t { typedef struct sr_disk_state_t { uint32_t magic_; /* Version of the protocol. */ - int Version; + uint32_t Version; /* Version of our running tor. */ char *TorVersion; /* Creation time of this state */ diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c index dcd71e0..8940045 100644 --- a/src/test/test_shared_random.c +++ b/src/test/test_shared_random.c @@ -788,9 +788,6 @@ test_sr_compute_srv(void *arg) MOCK(trusteddirserver_get_by_v3_auth_digest, trusteddirserver_get_by_v3_auth_digest_m);
- MOCK(trusteddirserver_get_by_v3_auth_digest, - trusteddirserver_get_by_v3_auth_digest_m); - init_authority_state();
/* Setup the commits for this unittest */
tor-commits@lists.torproject.org