/* Given <b>commit</b> give the line that we should place in our votes. * It's the responsibility of the caller to free the string. */ static char * get_vote_line_from_commit(const sr_commit_t *commit, sr_phase_t phase) { char *vote_line = NULL; switch (phase) { case SR_PHASE_COMMIT: tor_asprintf(&vote_line, "%s %u %s %s %s\n", commit_ns_str, SR_PROTO_VERSION, crypto_digest_algorithm_get_name(commit->alg), sr_commit_get_rsa_fpr(commit), commit->encoded_commit); break; case SR_PHASE_REVEAL: { /* Send a reveal value for this commit if we have one. */ const char *reveal_str = commit->encoded_reveal; if (tor_mem_is_zero(commit->encoded_reveal, sizeof(commit->encoded_reveal))) { reveal_str = ""; } tor_asprintf(&vote_line, "%s %u %s %s %s %s\n", commit_ns_str, SR_PROTO_VERSION, crypto_digest_algorithm_get_name(commit->alg), sr_commit_get_rsa_fpr(commit), commit->encoded_commit, reveal_str); break; } default: tor_assert(0); } log_debug(LD_DIR, "SR: Commit vote line: %s", vote_line); return vote_line; }
static void bench_digest(void) { char buf[8192]; char out[DIGEST512_LEN]; const int lens[] = { 1, 16, 32, 64, 128, 512, 1024, 2048, -1 }; const int N = 300000; uint64_t start, end; crypto_rand(buf, sizeof(buf)); for (int alg = 0; alg < N_DIGEST_ALGORITHMS; alg++) { for (int i = 0; lens[i] > 0; ++i) { reset_perftime(); start = perftime(); for (int j = 0; j < N; ++j) { switch (alg) { case DIGEST_SHA1: crypto_digest(out, buf, lens[i]); break; case DIGEST_SHA256: case DIGEST_SHA3_256: crypto_digest256(out, buf, lens[i], alg); break; case DIGEST_SHA512: case DIGEST_SHA3_512: crypto_digest512(out, buf, lens[i], alg); break; default: tor_assert(0); } } end = perftime(); printf("%s(%d): %.2f ns per call\n", crypto_digest_algorithm_get_name(alg), lens[i], NANOCOUNT(start,end,N)); } } }
/* From a valid commit object and an allocated config line, set the line's * value to the state string representation of a commit. */ static void disk_state_put_commit_line(const sr_commit_t *commit, config_line_t *line) { char *reveal_str = NULL; tor_assert(commit); tor_assert(line); if (!tor_mem_is_zero(commit->encoded_reveal, sizeof(commit->encoded_reveal))) { /* Add extra whitespace so we can format the line correctly. */ tor_asprintf(&reveal_str, " %s", commit->encoded_reveal); } tor_asprintf(&line->value, "%u %s %s %s%s", SR_PROTO_VERSION, crypto_digest_algorithm_get_name(commit->alg), sr_commit_get_rsa_fpr(commit), commit->encoded_commit, reveal_str != NULL ? reveal_str : ""); if (reveal_str != NULL) { memwipe(reveal_str, 0, strlen(reveal_str)); tor_free(reveal_str); } }