Data Structures | Functions
di_ops.c File Reference

Functions for data-independent operations. More...

#include "orconfig.h"
#include "lib/ctime/di_ops.h"
#include "lib/err/torerr.h"
#include "lib/malloc/malloc.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  di_digest256_map_t


int tor_memcmp (const void *a, const void *b, size_t len)
int tor_memeq (const void *a, const void *b, size_t sz)
void dimap_free_ (di_digest256_map_t *map, dimap_free_fn free_fn)
void dimap_add_entry (di_digest256_map_t **map, const uint8_t *key, void *val)
void * dimap_search (const di_digest256_map_t *map, const uint8_t *key, void *dflt_val)
int safe_mem_is_zero (const void *mem, size_t sz)
static int gt_i64_timei (uint64_t a, uint64_t b)
int select_array_member_cumulative_timei (const uint64_t *entries, int n_entries, uint64_t total, uint64_t rand_val)
void memcpy_if_true_timei (bool s, void *dest, const void *src, size_t n)

Detailed Description

Functions for data-independent operations.

Definition in file di_ops.c.

Function Documentation

◆ dimap_add_entry()

void dimap_add_entry ( di_digest256_map_t **  map,
const uint8_t *  key,
void *  val 

Adjust the map at *map, adding an entry for key -> val, where key is a DIGEST256_LEN-byte key.

The caller MUST NOT add a key that already appears in the map.

Definition at line 176 of file di_ops.c.

Referenced by construct_ntor_key_map().

◆ dimap_free_()

void dimap_free_ ( di_digest256_map_t map,
dimap_free_fn  free_fn 

Release all storage held in map, calling free_fn on each value as we go.

Definition at line 159 of file di_ops.c.

◆ dimap_search()

void * dimap_search ( const di_digest256_map_t map,
const uint8_t *  key,
void *  dflt_val 

Search the map at map for an entry whose key is key (a DIGEST256_LEN-byte key) returning the corresponding value if we found one, and returning dflt_val if the key wasn't found.

This operation takes an amount of time dependent only on the length of map, not on the position or presence of key within map.

Definition at line 200 of file di_ops.c.

Referenced by dimap_add_entry().

◆ gt_i64_timei()

static int gt_i64_timei ( uint64_t  a,
uint64_t  b 

Time-invariant 64-bit greater-than; works on two integers in the range (0,INT64_MAX).

Definition at line 243 of file di_ops.c.

Referenced by select_array_member_cumulative_timei().

◆ memcpy_if_true_timei()

void memcpy_if_true_timei ( bool  s,
void *  dest,
const void *  src,
size_t  n 

If s is true, then copy n bytes from src to dest. Otherwise leave dest alone.

This function behaves the same as

 if (s)
    memcpy(dest, src, n);

except that it tries to run in the same amount of time whether s is true or not.

Definition at line 296 of file di_ops.c.

◆ safe_mem_is_zero()

int safe_mem_is_zero ( const void *  mem,
size_t  sz 

Return true iff the sz bytes at mem are all zero. Runs in time independent of the contents of mem.

Definition at line 224 of file di_ops.c.

Referenced by curve25519_public_key_is_ok(), ed25519_public_key_is_zero(), hs_ntor_service_get_introduce1_keys_multi(), onion_ntor3_client_handshake(), onion_skin_ntor3_server_handshake_part2_nokeygen(), tor_digest256_is_zero(), and tor_digest_is_zero().

◆ select_array_member_cumulative_timei()

int select_array_member_cumulative_timei ( const uint64_t *  entries,
int  n_entries,
uint64_t  total,
uint64_t  rand_val 

Given an array of list of n_entries uint64_t values, whose sum is total, find the first i such that the total of all elements 0...i is greater than rand_val.

Try to perform this operation in a constant-time way.

Definition at line 259 of file di_ops.c.

Referenced by choose_array_element_by_weight().

◆ tor_memcmp()

int tor_memcmp ( const void *  a,
const void *  b,
size_t  len 

Timing-safe version of memcmp. As memcmp, compare the sz bytes at a with the sz bytes at b, and return less than 0 if the bytes at a lexically precede those at b, 0 if the byte ranges are equal, and greater than zero if the bytes at a lexically follow those at b.

This implementation differs from memcmp in that its timing behavior is not data-dependent: it should return in the same amount of time regardless of the contents of a and b.

Note that if all you care about is equality, this implementation is overkill: it would be better to use tor_memeq() or tor_memneq().

Definition at line 31 of file di_ops.c.

Referenced by compare_digest_to_fetch_hsdir_index(), compare_digest_to_routerstatus_entry(), compare_digest_to_store_first_hsdir_index(), compare_digest_to_store_second_hsdir_index(), compare_digest_to_vote_routerstatus_entry(), compare_digests256_(), compare_digests_(), compare_node_fetch_hsdir_index(), compare_node_store_first_hsdir_index(), compare_node_store_second_hsdir_index(), compare_srvs_(), crypto_pk_cmp_keys(), hs_address_is_valid(), and service_authorized_client_cmp().

◆ tor_memeq()

int tor_memeq ( const void *  a,
const void *  b,
size_t  sz