Tor 0.4.9.0-alpha-dev
Data Structures | Functions
hs_pow.c File Reference

Contains code to handle proof-of-work computations when a hidden service is defending against DoS attacks. More...

#include <stdio.h>
#include "core/or/or.h"
#include "app/config/config.h"
#include "ext/ht.h"
#include "ext/compat_blake2.h"
#include "core/or/circuitlist.h"
#include "core/or/origin_circuit_st.h"
#include "ext/equix/include/equix.h"
#include "feature/hs/hs_cache.h"
#include "feature/hs/hs_descriptor.h"
#include "feature/hs/hs_circuitmap.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_pow.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/arch/bytes.h"
#include "lib/cc/ctassert.h"
#include "core/mainloop/cpuworker.h"
#include "lib/evloop/workqueue.h"
#include "lib/time/compat_time.h"

Go to the source code of this file.

Data Structures

struct  nonce_cache_entry_t
 
struct  pow_worker_job_t
 

Functions

static int nonce_cache_entries_eq_ (const struct nonce_cache_entry_t *entry1, const struct nonce_cache_entry_t *entry2)
 
static unsigned nonce_cache_entry_hash_ (const struct nonce_cache_entry_t *ent)
 
static HT_HEAD (HT_PROTOTYPE(nonce_cache_table_ht, HT_PROTOTYPE(nonce_cache_entry_t)
 
static void increment_and_set_nonce (uint8_t *nonce, uint8_t *challenge)
 
static uint8_t * build_equix_challenge (const ed25519_public_key_t *blinded_id, const uint8_t *seed, const uint8_t *nonce, const uint32_t effort)
 
static bool validate_equix_challenge (const uint8_t *challenge, const uint8_t *solution_bytes, const uint32_t effort)
 
static void pack_equix_solution (const equix_solution *sol_in, uint8_t *bytes_out)
 
static void unpack_equix_solution (const uint8_t *bytes_in, equix_solution *sol_out)
 
static equix_ctx_flags hs_pow_equix_option_flags (int CompiledProofOfWorkHash)
 
int hs_pow_solve (const hs_pow_solver_inputs_t *pow_inputs, hs_pow_solution_t *pow_solution_out)
 
int hs_pow_verify (const ed25519_public_key_t *service_blinded_id, const hs_pow_service_state_t *pow_state, const hs_pow_solution_t *pow_solution)
 
void hs_pow_remove_seed_from_cache (const uint8_t *seed_head)
 
void hs_pow_free_service_state (hs_pow_service_state_t *state)
 
static workqueue_reply_t pow_worker_threadfn (void *state_, void *work_)
 
static void pow_worker_job_free (pow_worker_job_t *job)
 
static void pow_worker_replyfn (void *work_)
 
int hs_pow_queue_work (uint32_t intro_circ_identifier, const uint8_t *rend_circ_cookie, const hs_pow_solver_inputs_t *pow_inputs)
 

Detailed Description

Contains code to handle proof-of-work computations when a hidden service is defending against DoS attacks.

Definition in file hs_pow.c.

Function Documentation

◆ build_equix_challenge()

static uint8_t * build_equix_challenge ( const ed25519_public_key_t blinded_id,
const uint8_t *  seed,
const uint8_t *  nonce,
const uint32_t  effort 
)
static

Definition at line 98 of file hs_pow.c.

◆ hs_pow_equix_option_flags()

static equix_ctx_flags hs_pow_equix_option_flags ( int  CompiledProofOfWorkHash)
static

Helper: Map the CompiledProofOfWorkHash configuration option to its corresponding equix_ctx_flags bit.

Definition at line 181 of file hs_pow.c.

◆ hs_pow_free_service_state()

void hs_pow_free_service_state ( hs_pow_service_state_t state)

Free a given PoW service state.

Definition at line 404 of file hs_pow.c.

◆ hs_pow_queue_work()

int hs_pow_queue_work ( uint32_t  intro_circ_identifier,
const uint8_t *  rend_circ_cookie,
const hs_pow_solver_inputs_t pow_inputs 
)

Queue the job of solving the pow in a worker thread.

Definition at line 543 of file hs_pow.c.

◆ hs_pow_remove_seed_from_cache()

void hs_pow_remove_seed_from_cache ( const uint8_t *  seed_head)

Remove entries from the (nonce, seed) replay cache which are for the seed beginning with seed_head. If seed_head is NULL, remove all cache entries.

Definition at line 395 of file hs_pow.c.

◆ hs_pow_solve()

int hs_pow_solve ( const hs_pow_solver_inputs_t pow_inputs,
hs_pow_solution_t pow_solution_out 
)

Solve the EquiX/blake2b PoW scheme using the parameters in pow_params, and store the solution in pow_solution_out. Returns 0 on success and -1 otherwise. Called by a client, from a cpuworker thread.

Definition at line 197 of file hs_pow.c.

◆ hs_pow_verify()

int hs_pow_verify ( const ed25519_public_key_t service_blinded_id,
const hs_pow_service_state_t pow_state,
const hs_pow_solution_t pow_solution 
)

Verify the solution in pow_solution using the service's current PoW parameters found in pow_state. Returns 0 on success and -1 otherwise. Called by the service.

Definition at line 312 of file hs_pow.c.

◆ HT_HEAD()

static HT_HEAD ( HT_PROTOTYPE nonce_cache_table_ht,
HT_PROTOTYPE nonce_cache_entry_t 
)
static

Definition at line 57 of file hs_pow.c.

◆ increment_and_set_nonce()

static void increment_and_set_nonce ( uint8_t *  nonce,
uint8_t *  challenge 
)
inlinestatic

Helper: Increment a given nonce and set it in the challenge at the right offset. Use by the solve function.

Definition at line 84 of file hs_pow.c.

◆ nonce_cache_entries_eq_()

static int nonce_cache_entries_eq_ ( const struct nonce_cache_entry_t entry1,
const struct nonce_cache_entry_t entry2 
)
inlinestatic

Return true if the two (nonce, seed) replay cache entries are the same

Definition at line 44 of file hs_pow.c.

◆ nonce_cache_entry_hash_()

static unsigned nonce_cache_entry_hash_ ( const struct nonce_cache_entry_t ent)
inlinestatic

Hash function to hash the (nonce, seed) tuple entry.

Definition at line 52 of file hs_pow.c.

◆ pack_equix_solution()

static void pack_equix_solution ( const equix_solution sol_in,
uint8_t *  bytes_out 
)
static

Helper: Convert equix_solution to a byte array in little-endian order

Definition at line 158 of file hs_pow.c.

◆ pow_worker_job_free()

static void pow_worker_job_free ( pow_worker_job_t job)
static

Helper: release all storage held in job.

Definition at line 460 of file hs_pow.c.

◆ pow_worker_replyfn()

static void pow_worker_replyfn ( void *  work_)
static

Worker function: This function runs in the main thread, and receives a pow_worker_job_t that the worker thread has already processed.

Definition at line 473 of file hs_pow.c.

◆ pow_worker_threadfn()

static workqueue_reply_t pow_worker_threadfn ( void *  state_,
void *  work_ 
)
static

Worker function. This function runs inside a worker thread and receives a pow_worker_job_t as its input.

Definition at line 443 of file hs_pow.c.

◆ unpack_equix_solution()

static void unpack_equix_solution ( const uint8_t *  bytes_in,
equix_solution sol_out 
)
static

Helper: Build an equix_solution from its corresponding byte array.

Definition at line 169 of file hs_pow.c.

◆ validate_equix_challenge()

static bool validate_equix_challenge ( const uint8_t *  challenge,
const uint8_t *  solution_bytes,
const uint32_t  effort 
)
static

Helper: Return true iff the given challenge and solution for the given effort do validate as in: R * E <= UINT32_MAX.

Definition at line 133 of file hs_pow.c.