GNUnet  0.11.x
Macros | Functions
Crypto library: hash operations

Provides hashing and operations on hashes. More...

Macros

#define GNUNET_CRYPTO_hash_from_string(enc, result)    GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)
 Convert ASCII encoding back to struct GNUNET_HashCode More...
 

Functions

uint8_t GNUNET_CRYPTO_crc8_n (const void *buf, size_t len)
 Calculate the checksum of a buffer in one step. More...
 
uint16_t GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
 Calculate the checksum of a buffer in one step. More...
 
int32_t GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
 Compute the CRC32 checksum for the first len bytes of the buffer. More...
 
void GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
 Convert hash to ASCII encoding. More...
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen, struct GNUNET_HashCode *result)
 Convert ASCII encoding back to a 'struct GNUNET_HashCode'. More...
 
uint32_t GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b)
 Compute the distance between 2 hashcodes. More...
 
void GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode *ret)
 Compute hash of a given block. More...
 
void GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
 Calculate HMAC of a message (RFC 2104) More...
 
struct GNUNET_CRYPTO_FileHashContextGNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, const char *filename, size_t blocksize, GNUNET_CRYPTO_HashCompletedCallback callback, void *callback_cls)
 Compute the hash of an entire file. More...
 
void GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, struct GNUNET_HashCode *result)
 Create a random hash code. More...
 
void GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
 compute result = b - a More...
 
void GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *delta, struct GNUNET_HashCode *result)
 compute result = a + delta More...
 
void GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
 compute result = a ^ b More...
 
void GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
 Convert a hashcode into a key. More...
 
int GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, unsigned int bit)
 Obtain a bit from a hashcode. More...
 
unsigned int GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first, const struct GNUNET_HashCode *second)
 Determine how many low order bits match in two struct GNUNET_HashCodes. More...
 
int GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
 Compare function for HashCodes, producing a total ordering of all hashcodes. More...
 
int GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2, const struct GNUNET_HashCode *target)
 Find out which of the two GNUNET_CRYPTO_hash codes is closer to target in the XOR metric (Kademlia). More...
 
void GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len, va_list argp)
 Derive an authentication key. More...
 
void GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len,...)
 Derive an authentication key. More...
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
 Derive key. More...
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
 Derive key. More...
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
 Derive key. More...
 

Detailed Description

Provides hashing and operations on hashes.

See also
Documentation

Macro Definition Documentation

◆ GNUNET_CRYPTO_hash_from_string

#define GNUNET_CRYPTO_hash_from_string (   enc,
  result 
)     GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)

Convert ASCII encoding back to struct GNUNET_HashCode

Parameters
encthe encoding
resultwhere to store the hash code
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Definition at line 665 of file gnunet_crypto_lib.h.

Function Documentation

◆ GNUNET_CRYPTO_crc8_n()

uint8_t GNUNET_CRYPTO_crc8_n ( const void *  buf,
size_t  len 
)

Calculate the checksum of a buffer in one step.

Parameters
bufbuffer to calculate CRC over
lennumber of bytes in buf
Returns
crc8 value

Definition at line 179 of file crypto_crc.c.

181 {
182  const uint8_t *data = buf;
183  unsigned int crc = 0;
184  int i;
185  int j;
186 
187  for (j = len; 0 != j; j--)
188  {
189  crc ^= (*data++ << 8);
190  for (i = 8; 0 != i; i--)
191  {
192  if (0 != (crc & 0x8000))
193  crc ^= (0x1070 << 3);
194  crc <<= 1;
195  }
196  }
197  return (uint8_t) (crc >> 8);
198 }
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static char buf[2048]

References buf, data, and len.

◆ GNUNET_CRYPTO_crc16_n()

uint16_t GNUNET_CRYPTO_crc16_n ( const void *  buf,
size_t  len 
)

Calculate the checksum of a buffer in one step.

Parameters
bufbuffer to calculate CRC over (must be 16-bit aligned)
lennumber of bytes in buf, must be multiple of 2
Returns
crc16 value
Parameters
bufbuffer to calculate CRC over (must be 16-bit aligned)
lennumber of bytes in hdr, must be multiple of 2
Returns
crc16 value

Definition at line 161 of file crypto_crc.c.

162 {
163  const uint16_t *hdr = buf;
164  uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
165 
167 }
uint16_t GNUNET_CRYPTO_crc16_finish(uint32_t sum)
Convert results from GNUNET_CRYPTO_crc16_step() to final crc16.
Definition: crypto_crc.c:144
uint32_t GNUNET_CRYPTO_crc16_step(uint32_t sum, const void *buf, size_t len)
Perform an incremental step in a CRC16 (for TCP/IP) calculation.
Definition: crypto_crc.c:125

References buf, GNUNET_CRYPTO_crc16_finish(), GNUNET_CRYPTO_crc16_step(), len, and consensus-simulation::sum.

Referenced by GNUNET_TUN_initialize_ipv4_header().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_crc32_n()

int32_t GNUNET_CRYPTO_crc32_n ( const void *  buf,
size_t  len 
)

Compute the CRC32 checksum for the first len bytes of the buffer.

Parameters
bufthe data over which we're taking the CRC
lenthe length of the buffer buf in bytes
Returns
the resulting CRC32 checksum
Parameters
bufthe data over which we're taking the CRC
lenthe length of the buffer
Returns
the resulting CRC32 checksum

Definition at line 106 of file crypto_crc.c.

107 {
108  GNUNET_uLong crc;
109 
110  crc = crc32 (0L, Z_NULL, 0);
111  crc = crc32 (crc, (char *) buf, len);
112  return crc;
113 }
#define Z_NULL
Definition: crypto_crc.c:44
static GNUNET_uLong crc32(GNUNET_uLong crc, const char *buf, size_t len)
Definition: crypto_crc.c:87

References buf, crc32(), len, and Z_NULL.

Referenced by do_decrypt(), do_encrypt(), ibf_get_indices(), process_data(), and wlan_plugin_send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_to_enc()

void GNUNET_CRYPTO_hash_to_enc ( const struct GNUNET_HashCode block,
struct GNUNET_CRYPTO_HashAsciiEncoded result 
)

Convert hash to ASCII encoding.

Parameters
blockthe hash code
resultwhere to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be safely cast to char*, a '\0' termination is set).

Definition at line 55 of file crypto_hash.c.

57 {
58  char *np;
59 
60  np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
61  sizeof(struct GNUNET_HashCode),
62  (char *) result,
63  sizeof(struct
65  - 1);
66  GNUNET_assert (NULL != np);
67  *np = '\0';
68 }
static int result
Global testing status.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:695
0-terminated ASCII encoding of a struct GNUNET_HashCode.
A 512-bit hashcode.

Referenced by get_update_information_directory(), GNUNET_e2s(), GNUNET_e2s2(), GNUNET_h2s(), GNUNET_h2s2(), GNUNET_h2s_full(), GNUNET_NETWORK_shorten_unixpath(), GNUNET_p2s(), GNUNET_p2s2(), store_and_free_entries(), uri_chk_to_string(), and uri_loc_to_string().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_from_string2()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hash_from_string2 ( const char *  enc,
size_t  enclen,
struct GNUNET_HashCode result 
)

Convert ASCII encoding back to a 'struct GNUNET_HashCode'.

Parameters
encthe encoding
enclennumber of characters in enc (without 0-terminator, which can be missing)
resultwhere to store the hash code
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Definition at line 55 of file crypto_hash.c.

75 {
76  char upper_enc[enclen];
77  char *up_ptr = upper_enc;
78 
80 
81  return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
82  (unsigned char *) result,
83  sizeof(struct GNUNET_HashCode));
84 }
static OpusEncoder * enc
OPUS encoder.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:775
void GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
Convert the utf-8 input string to upper case.
Definition: strings.c:463

References GNUNET_assert, GNUNET_STRINGS_data_to_string(), and result.

Here is the call graph for this function:

◆ GNUNET_CRYPTO_hash_distance_u32()

uint32_t GNUNET_CRYPTO_hash_distance_u32 ( const struct GNUNET_HashCode a,
const struct GNUNET_HashCode b 
)

Compute the distance between 2 hashcodes.

The computation must be fast, not involve a[0] or a[4] (they're used elsewhere), and be somewhat consistent. And of course, the result should be a positive number.

Parameters
asome hash code
bsome hash code
Returns
number between 0 and UINT32_MAX

Definition at line 88 of file crypto_hash.c.

90 {
91  unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
92  unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
93 
94  return(x1 * x2);
95 }
uint32_t bits[512/8/sizeof(uint32_t)]

References testconfigure::b, and GNUNET_HashCode::bits.

Referenced by score_content().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash()

void GNUNET_CRYPTO_hash ( const void *  block,
size_t  size,
struct GNUNET_HashCode ret 
)

Compute hash of a given block.

Parameters
blockthe data to hash
sizesize of the block
retpointer to where to write the hashcode

Definition at line 41 of file crypto_hash.c.

44 {
45  BENCHMARK_START (hash);
46  gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
47  BENCHMARK_END (hash);
48 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:57
#define BENCHMARK_END(opname)
Definition: benchmark.h:58
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static unsigned int size
Size of the "table".
Definition: peer.c:67

References BENCHMARK_END, BENCHMARK_START, ret, and size.

Referenced by add_file(), add_known_to_bloom(), add_member_session(), add_to_keyword_counter(), add_to_meta_counter(), advertise_dns_exit(), automaton_create_proofs(), barrier_wait_cb(), block_plugin_fs_get_key(), block_plugin_revocation_get_key(), build_authz_response(), calculate_hmac(), callback_ego_rename(), checkvec(), client_request_complete_alice(), code_redirect(), collector(), commit_set(), connect_peers_finish(), consume_timeout(), convert_messenger_key_to_port(), cookie_identity_interpretation(), core_connect_cb(), core_init(), core_init_cb(), create_response(), create_source(), create_target(), database_setup(), decode_short_message(), decrypt_new_element(), delayed_put(), derive_aes_key(), derive_auth_key(), derive_iv(), derive_pong_iv(), determine_id(), do_rekey(), ego_get_all(), ego_get_response(), encode_short_message(), encrypt_existing_match(), es_to_sh(), find_advertisable_hello(), find_target(), find_trees(), forward_reply(), GC_u2h(), GCT_add_channel(), get_cadet(), get_connections(), get_context_from_member(), get_destination_key_from_ip(), get_fair_encryption_challenge(), get_file_handle(), get_gns_cont(), get_matching_bits(), get_member_session(), get_store_contact(), get_update_information_directory(), get_url_parameter_copy(), GNUNET_BLOCK_mingle_hash(), GNUNET_CONVERSATION_phone_create(), GNUNET_CRYPTO_ecc_ecdh(), GNUNET_CRYPTO_rsa_public_key_hash(), GNUNET_e2s(), GNUNET_e2s2(), GNUNET_FS_handle_on_demand_block(), GNUNET_FS_namespace_list_updateable(), GNUNET_FS_publish_ublock_(), GNUNET_FS_search_start_searching_(), GNUNET_FS_tree_encoder_next(), GNUNET_FS_unindex_do_remove_kblocks_(), GNUNET_FS_uri_to_key(), GNUNET_HELLO_extract_address(), GNUNET_HELLO_sign_address(), GNUNET_IDENTITY_ego_get_anonymous(), GNUNET_NETWORK_shorten_unixpath(), GNUNET_p2s(), GNUNET_p2s2(), GNUNET_TESTBED_barrier_init_(), GNUNET_TESTING_get_topo_from_file(), GNUNET_TUN_service_name_to_hash(), GSC_KX_encrypt_and_transmit(), GSC_KX_init(), GSC_TYPEMAP_hash(), GSF_cadet_start_server(), handle_barrier_cancel(), handle_barrier_init(), handle_barrier_status(), handle_barrier_wait(), handle_client_decrypt(), handle_core_connect(), handle_encrypted(), handle_ephemeral_key(), handle_identity_set_default(), handle_identity_update(), handle_put(), handle_query_message(), handle_regex_result(), handle_transport_notify_connect(), hash_message(), hash_pkey_and_label(), header_iterator(), iface_proc(), init_socket(), insert_decrypt_element(), insert_round1_element(), iterate_initial_edge(), iterateBits(), load_state(), login_cont(), lookup_diff(), lookup_rfn(), lookup_set(), lookup_store_ego(), lookup_task(), maint_child_death(), mysql_plugin_put(), namestore_get(), notify_connect(), notify_handle_message(), ns_lookup_result_cb(), on_identity(), on_peer(), output_vectors(), parse_credentials_basic_auth(), parse_credentials_post_body(), peerinfo_get(), peerstore_flat_store_record(), PEERSTORE_hash_key(), post_data_iter(), postgres_plugin_put(), process_client_result(), process_kblock_for_unindex(), process_reply(), publicize_rm(), put_diff(), put_rfn(), put_set(), put_task(), queue(), queue_destroy(), REGEX_BLOCK_check_proof(), REGEX_BLOCK_get_key(), REGEX_INTERNAL_get_first_key(), remove_high_frequency_keywords(), remove_member_session(), remove_room_member_session(), remove_store_contact(), reset_cadet(), run(), schedule_next_hello(), schedule_transmit_search_request(), score_content(), second_stage(), select_store_contact_map(), send_key(), send_kx_auth(), send_simple_run(), set_result_cb(), setup_filter(), sock_read(), sqlite_plugin_put(), start_helper(), start_intersection(), token_endpoint(), try_connect(), try_match_block(), try_open_exit(), try_top_down_reconstruction(), update_store_contact(), update_store_ego(), url_iterator(), and userinfo_endpoint().

◆ GNUNET_CRYPTO_hmac()

void GNUNET_CRYPTO_hmac ( const struct GNUNET_CRYPTO_AuthKey key,
const void *  plaintext,
size_t  plaintext_len,
struct GNUNET_HashCode hmac 
)

Calculate HMAC of a message (RFC 2104)

Parameters
keysecret key
plaintextinput plaintext
plaintext_lenlength of plaintext
hmacwhere to store the hmac

Definition at line 301 of file crypto_hash.c.

304 {
305  GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key),
306  plaintext, plaintext_len,
307  hmac);
308 }
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn' this be the standard hmac function and the abov...
Definition: crypto_hash.c:274
struct GNUNET_HashCode key
The key used in the DHT.

References GNUNET_CRYPTO_hmac_raw(), and key.

Referenced by dv_hmac(), GSC_KX_encrypt_and_transmit(), handle_encrypted(), RPS_sampler_elem_next(), t_ax_hmac_hash(), and t_hmac().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_file()

struct GNUNET_CRYPTO_FileHashContext* GNUNET_CRYPTO_hash_file ( enum GNUNET_SCHEDULER_Priority  priority,
const char *  filename,
size_t  blocksize,
GNUNET_CRYPTO_HashCompletedCallback  callback,
void *  callback_cls 
)

Compute the hash of an entire file.

Parameters
priorityscheduling priority to use
filenamename of file to hash
blocksizenumber of bytes to process in one task
callbackfunction to call upon completion
callback_clsclosure for callback
Returns
NULL on (immediate) error

Definition at line 178 of file crypto_hash_file.c.

183 {
184  struct GNUNET_CRYPTO_FileHashContext *fhc;
185 
186  GNUNET_assert (blocksize > 0);
187  fhc =
188  GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize);
189  fhc->callback = callback;
190  fhc->callback_cls = callback_cls;
191  fhc->buffer = (unsigned char *) &fhc[1];
193  if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
194  {
195  GNUNET_break (0);
196  GNUNET_free (fhc);
197  return NULL;
198  }
199  fhc->bsize = blocksize;
200  if (GNUNET_OK !=
202  &fhc->fsize,
203  GNUNET_NO,
204  GNUNET_YES))
205  {
206  GNUNET_free (fhc->filename);
207  GNUNET_free (fhc);
208  return NULL;
209  }
213  if (! fhc->fh)
214  {
215  GNUNET_free (fhc->filename);
216  GNUNET_free (fhc);
217  return NULL;
218  }
219  fhc->priority = priority;
222  fhc);
223  return fhc;
224 }
static void file_hash_task(void *cls)
File hashing task.
static char * filename
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1235
enum GNUNET_GenericReturnValue GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:222
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority.
Definition: scheduler.c:1223
Context used when hashing a file.
uint64_t fsize
Size of the file.
gcry_md_hd_t md
Cummulated hash.
unsigned char * buffer
IO buffer.
GNUNET_CRYPTO_HashCompletedCallback callback
Function to call upon completion.
struct GNUNET_DISK_FileHandle * fh
File descriptor.
void * callback_cls
Closure for callback.
struct GNUNET_SCHEDULER_Task * task
Current task for hashing.
char * filename
Name of the file we are hashing.
enum GNUNET_SCHEDULER_Priority priority
Priority we use.

References GNUNET_CRYPTO_FileHashContext::bsize, GNUNET_CRYPTO_FileHashContext::buffer, GNUNET_CRYPTO_FileHashContext::callback, GNUNET_CRYPTO_FileHashContext::callback_cls, GNUNET_CRYPTO_FileHashContext::fh, file_hash_task(), filename, GNUNET_CRYPTO_FileHashContext::filename, GNUNET_CRYPTO_FileHashContext::fsize, GNUNET_assert, GNUNET_break, GNUNET_DISK_file_open(), GNUNET_DISK_file_size(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE, GNUNET_free, GNUNET_malloc, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_with_priority(), GNUNET_strdup, GNUNET_YES, GNUNET_CRYPTO_FileHashContext::md, GNUNET_CRYPTO_FileHashContext::priority, and GNUNET_CRYPTO_FileHashContext::task.

Referenced by deserialize_unindex_file(), GNUNET_FS_publish_main_(), GNUNET_FS_unindex_start(), and handle_client_index_start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_create_random()

void GNUNET_CRYPTO_hash_create_random ( enum GNUNET_CRYPTO_Quality  mode,
struct GNUNET_HashCode result 
)

Create a random hash code.

Parameters
modedesired quality level
resulthash code that is randomized

Definition at line 99 of file crypto_hash.c.

101 {
102  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1;
103  i >= 0;
104  i--)
105  result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
106 }
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.

References GNUNET_CRYPTO_random_u32(), mode, and result.

Referenced by commit_set(), do_consensus(), GDS_DATACACHE_get_random_key(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_difference()

void GNUNET_CRYPTO_hash_difference ( const struct GNUNET_HashCode a,
const struct GNUNET_HashCode b,
struct GNUNET_HashCode result 
)

compute result = b - a

Parameters
asome hash code
bsome hash code
resultset to b - a

Definition at line 110 of file crypto_hash.c.

113 {
114  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
115  i >= 0;
116  i--)
117  result->bits[i] = b->bits[i] - a->bits[i];
118 }

References testconfigure::b, GNUNET_HashCode::bits, and result.

◆ GNUNET_CRYPTO_hash_sum()

void GNUNET_CRYPTO_hash_sum ( const struct GNUNET_HashCode a,
const struct GNUNET_HashCode delta,
struct GNUNET_HashCode result 
)

compute result = a + delta

Parameters
asome hash code
deltasome hash code
resultset to a + delta

Definition at line 122 of file crypto_hash.c.

125 {
126  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
127  i >= 0;
128  i--)
129  result->bits[i] = delta->bits[i] + a->bits[i];
130 }
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35

References GNUNET_HashCode::bits, delta, and result.

Referenced by convert_messenger_key_to_port().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_xor()

void GNUNET_CRYPTO_hash_xor ( const struct GNUNET_HashCode a,
const struct GNUNET_HashCode b,
struct GNUNET_HashCode result 
)

compute result = a ^ b

Parameters
asome hash code
bsome hash code
resultset to a ^ b

Definition at line 134 of file crypto_hash.c.

137 {
138  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
139  i >= 0;
140  i--)
141  result->bits[i] = a->bits[i] ^ b->bits[i];
142 }

References testconfigure::b, GNUNET_HashCode::bits, and result.

Referenced by determine_id(), filter_all(), filtered_map_initialization(), get_context_from_member(), GNUNET_BLOCK_mingle_hash(), initialize_map_unfiltered(), iterator_bf_reduce(), and process_sks_result().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_to_aes_key()

void GNUNET_CRYPTO_hash_to_aes_key ( const struct GNUNET_HashCode hc,
struct GNUNET_CRYPTO_SymmetricSessionKey skey,
struct GNUNET_CRYPTO_SymmetricInitializationVector iv 
)

Convert a hashcode into a key.

Parameters
hchash code that serves to generate the key
skeyset to a valid session key
ivset to a valid initialization vector

Definition at line 146 of file crypto_hash.c.

150 {
153  skey,
154  sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
155  "Hash key derivation",
156  strlen ("Hash key derivation"),
157  hc, sizeof(struct GNUNET_HashCode),
158  NULL, 0));
161  iv,
163  "Initialization vector derivation",
164  strlen ("Initialization vector derivation"),
165  hc, sizeof(struct GNUNET_HashCode),
166  NULL, 0));
167 }
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:90

References GNUNET_assert, GNUNET_CRYPTO_kdf(), and GNUNET_YES.

Referenced by derive_ublock_encryption_key(), encrypt_existing_match(), GNUNET_FS_handle_on_demand_block(), GNUNET_FS_tree_encoder_next(), GNUNET_IDENTITY_decrypt(), GNUNET_IDENTITY_encrypt(), process_result_with_request(), and try_match_block().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_get_bit_ltr()

int GNUNET_CRYPTO_hash_get_bit_ltr ( const struct GNUNET_HashCode code,
unsigned int  bit 
)

Obtain a bit from a hashcode.

Parameters
codethe struct GNUNET_HashCode to index bit-wise
bitindex into the hashcode, [0...159] where 0 is the leftmost bit (bytes in code interpreted big endian)
Returns
Bit bit from hashcode code, -1 for invalid index

Definition at line 171 of file crypto_hash.c.

173 {
174  GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
175  return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0;
176 }

References GNUNET_assert.

Referenced by count_leading_zeroes().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_matching_bits()

unsigned int GNUNET_CRYPTO_hash_matching_bits ( const struct GNUNET_HashCode first,
const struct GNUNET_HashCode second 
)

Determine how many low order bits match in two struct GNUNET_HashCodes.

e.g. - 010011 and 011111 share the first two lowest order bits, and therefore the return value is two (NOT XOR distance, nor how many bits match absolutely!).

Parameters
firstthe first hashcode
secondthe hashcode to compare first to
Returns
the number of bits that match

Definition at line 189 of file crypto_hash.c.

191 {
192  for (unsigned int i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++)
193  if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) !=
194  GNUNET_CRYPTO_hash_get_bit_rtl (second, i))
195  return i;
196  return sizeof(struct GNUNET_HashCode) * 8;
197 }
int GNUNET_CRYPTO_hash_get_bit_rtl(const struct GNUNET_HashCode *code, unsigned int bit)
Obtain a bit from a hashcode.
Definition: crypto_hash.c:180

References GNUNET_CRYPTO_hash_get_bit_rtl().

Referenced by find_bucket(), GDS_am_closest_peer(), GDS_DATACACHE_handle_put(), get_matching_bits(), handle_dht_p2p_get(), handle_dht_p2p_put(), and select_peer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_cmp()

int GNUNET_CRYPTO_hash_cmp ( const struct GNUNET_HashCode h1,
const struct GNUNET_HashCode h2 
)

Compare function for HashCodes, producing a total ordering of all hashcodes.

Parameters
h1some hash code
h2some hash code
Returns
1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.

Definition at line 201 of file crypto_hash.c.

203 {
204  unsigned int *i1;
205  unsigned int *i2;
206 
207  i1 = (unsigned int *) h1;
208  i2 = (unsigned int *) h2;
209  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
210  i >= 0;
211  i--)
212  {
213  if (i1[i] > i2[i])
214  return 1;
215  if (i1[i] < i2[i])
216  return -1;
217  }
218  return 0;
219 }

References consensus-simulation::int.

Referenced by check_member_session_completion(), create_message_request(), decode_short_message(), element_cmp(), find_closest(), get_store_message(), handle_client_join(), handle_core_connect(), handle_transport_notify_connect(), op_get_element_iterator(), REGEX_BLOCK_check_proof(), remove_from_list_messages(), RPS_sampler_elem_next(), and select_store_contact_map().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_xorcmp()

int GNUNET_CRYPTO_hash_xorcmp ( const struct GNUNET_HashCode h1,
const struct GNUNET_HashCode h2,
const struct GNUNET_HashCode target 
)

Find out which of the two GNUNET_CRYPTO_hash codes is closer to target in the XOR metric (Kademlia).

Parameters
h1some hash code
h2some hash code
targetsome hash code
Returns
-1 if h1 is closer, 1 if h2 is closer and 0 if h1== h2.

Definition at line 223 of file crypto_hash.c.

226 {
227  unsigned int d1;
228  unsigned int d2;
229 
230  for (ssize_t i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1;
231  i >= 0;
232  i--)
233  {
234  d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
235  d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
236  if (d1 > d2)
237  return 1;
238  else if (d1 < d2)
239  return -1;
240  }
241  return 0;
242 }

◆ GNUNET_CRYPTO_hmac_derive_key_v()

void GNUNET_CRYPTO_hmac_derive_key_v ( struct GNUNET_CRYPTO_AuthKey key,
const struct GNUNET_CRYPTO_SymmetricSessionKey rkey,
const void *  salt,
size_t  salt_len,
va_list  argp 
)

Derive an authentication key.

Parameters
keyauthentication key
rkeyroot key
saltsalt
salt_lensize of the salt
argppair of void * & size_t for context chunks, terminated by NULL

Definition at line 260 of file crypto_hash.c.

265 {
266  GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key),
267  salt, salt_len,
268  rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
269  argp);
270 }
static struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calcualations.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf_v(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
Derive key.
Definition: crypto_kdf.c:47

References GNUNET_CRYPTO_kdf_v(), key, and salt.

Referenced by GNUNET_CRYPTO_hmac_derive_key().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hmac_derive_key()

void GNUNET_CRYPTO_hmac_derive_key ( struct GNUNET_CRYPTO_AuthKey key,
const struct GNUNET_CRYPTO_SymmetricSessionKey rkey,
const void *  salt,
size_t  salt_len,
  ... 
)

Derive an authentication key.

Parameters
keyauthentication key
rkeyroot key
saltsalt
salt_lensize of the salt
...pair of void * & size_t for context chunks, terminated by NULL

Definition at line 246 of file crypto_hash.c.

250 {
251  va_list argp;
252 
253  va_start (argp, salt_len);
254  GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
255  va_end (argp);
256 }
void GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len, va_list argp)
Derive an authentication key.
Definition: crypto_hash.c:260

References GNUNET_CRYPTO_hmac_derive_key_v(), key, and salt.

Referenced by derive_auth_key(), t_ax_hmac_hash(), and t_hmac().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hkdf()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf ( void *  result,
size_t  out_len,
int  xtr_algo,
int  prf_algo,
const void *  xts,
size_t  xts_len,
const void *  skm,
size_t  skm_len,
  ... 
)

Derive key.

Parameters
resultbuffer for the derived key, allocated by caller
out_lendesired length of the derived key
xtr_algohash algorithm for the extraction phase, GCRY_MD_...
prf_algohash algorithm for the expansion phase, GCRY_MD_...
xtssalt
xts_lenlength of xts
skmsource key material
skm_lenlength of skm
...pair of void * & size_t for context chunks, terminated by NULL
Returns
GNUNET_YES on success
Parameters
resultbuffer for the derived key, allocated by caller
out_lendesired length of the derived key
xtr_algohash algorithm for the extraction phase, GCRY_MD_...
prf_algohash algorithm for the expansion phase, GCRY_MD_...
xtssalt
xts_lenlength of xts
skmsource key material
skm_lenlength of skm
Returns
GNUNET_YES on success

Definition at line 324 of file crypto_hkdf.c.

327 {
328  va_list argp;
329  int ret;
330 
331  va_start (argp, skm_len);
332  ret =
333  GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
334  skm, skm_len, argp);
335  va_end (argp);
336 
337  return ret;
338 }
int GNUNET_CRYPTO_hkdf_v(void *result, size_t out_len, int xtr_algo, int prf_algo, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
Derive key.
Definition: crypto_hkdf.c:148

References GNUNET_CRYPTO_hkdf_v(), result, and ret.

Referenced by calculate_cmac(), get_iv_key(), and get_kid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hkdf_v()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_v ( void *  result,
size_t  out_len,
int  xtr_algo,
int  prf_algo,
const void *  xts,
size_t  xts_len,
const void *  skm,
size_t  skm_len,
va_list  argp 
)

Derive key.

Parameters
resultbuffer for the derived key, allocated by caller
out_lendesired length of the derived key
xtr_algohash algorithm for the extraction phase, GCRY_MD_...
prf_algohash algorithm for the expansion phase, GCRY_MD_...
xtssalt
xts_lenlength of xts
skmsource key material
skm_lenlength of skm
argpva_list of void * & size_t pairs for context chunks
Returns
GNUNET_YES on success

Definition at line 148 of file crypto_hkdf.c.

151 {
152  gcry_md_hd_t xtr;
153  gcry_md_hd_t prf;
154  const void *hc;
155  unsigned long i;
156  unsigned long t;
157  unsigned long d;
158  unsigned int k = gcry_md_get_algo_dlen (prf_algo);
159  unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
160  char prk[xtr_len];
161  int ret;
162  size_t ctx_len;
163  va_list args;
164 
165  BENCHMARK_START (hkdf);
166 
167  if (0 == k)
168  return GNUNET_SYSERR;
169  if (GPG_ERR_NO_ERROR !=
170  gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
171  return GNUNET_SYSERR;
172  if (GPG_ERR_NO_ERROR !=
173  gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
174  {
175  gcry_md_close (xtr);
176  return GNUNET_SYSERR;
177  }
178  va_copy (args, argp);
179 
180  ctx_len = 0;
181  while (NULL != va_arg (args, void *))
182  {
183  size_t nxt = va_arg (args, size_t);
184  if (nxt + ctx_len < nxt)
185  {
186  /* integer overflow */
187  GNUNET_break (0);
188  va_end (args);
189  goto hkdf_error;
190  }
191  ctx_len += nxt;
192  }
193 
194  va_end (args);
195 
196  if ( (k + ctx_len < ctx_len) ||
197  (k + ctx_len + 1 < ctx_len) )
198  {
199  /* integer overflow */
200  GNUNET_break (0);
201  goto hkdf_error;
202  }
203 
204  memset (result, 0, out_len);
205  if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
206  goto hkdf_error;
207 #if DEBUG_HKDF
208  dump ("PRK", prk, xtr_len);
209 #endif
210 
211  t = out_len / k;
212  d = out_len % k;
213 
214  /* K(1) */
215  {
216  size_t plain_len = k + ctx_len + 1;
217  char *plain;
218  const void *ctx;
219  char *dst;
220 
221  plain = GNUNET_malloc (plain_len);
222  dst = plain + k;
223  va_copy (args, argp);
224  while ((ctx = va_arg (args, void *)))
225  {
226  size_t len;
227 
228  len = va_arg (args, size_t);
229  GNUNET_memcpy (dst, ctx, len);
230  dst += len;
231  }
232  va_end (args);
233 
234  if (t > 0)
235  {
236  plain[k + ctx_len] = (char) 1;
237 #if DEBUG_HKDF
238  dump ("K(1)", plain, plain_len);
239 #endif
240  hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
241  if (hc == NULL)
242  {
243  GNUNET_free (plain);
244  goto hkdf_error;
245  }
246  GNUNET_memcpy (result, hc, k);
247  result += k;
248  }
249 
250  /* K(i+1) */
251  for (i = 1; i < t; i++)
252  {
253  GNUNET_memcpy (plain, result - k, k);
254  plain[k + ctx_len] = (char) (i + 1);
255  gcry_md_reset (prf);
256 #if DEBUG_HKDF
257  dump ("K(i+1)", plain, plain_len);
258 #endif
259  hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
260  if (hc == NULL)
261  {
262  GNUNET_free (plain);
263  goto hkdf_error;
264  }
265  GNUNET_memcpy (result, hc, k);
266  result += k;
267  }
268 
269  /* K(t):d */
270  if (d > 0)
271  {
272  if (t > 0)
273  {
274  GNUNET_memcpy (plain, result - k, k);
275  i++;
276  }
277  plain[k + ctx_len] = (char) i;
278  gcry_md_reset (prf);
279 #if DEBUG_HKDF
280  dump ("K(t):d", plain, plain_len);
281 #endif
282  if (t > 0)
283  hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
284  else
285  hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
286  if (hc == NULL)
287  {
288  GNUNET_free (plain);
289  goto hkdf_error;
290  }
291  GNUNET_memcpy (result, hc, d);
292  }
293 #if DEBUG_HKDF
294  dump ("result", result - k, out_len);
295 #endif
296 
297  ret = GNUNET_YES;
298  GNUNET_free (plain);
299  goto hkdf_ok;
300  }
301 hkdf_error:
302  ret = GNUNET_SYSERR;
303 hkdf_ok:
304  gcry_md_close (xtr);
305  gcry_md_close (prf);
306  BENCHMARK_END (hkdf);
307  return ret;
308 }
static int getPRK(gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm, size_t skm_len, void *prk)
Generate pseudo-random key.
Definition: crypto_hkdf.c:102
static const void * doHMAC(gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf, size_t buf_len)
Compute the HMAC.
Definition: crypto_hkdf.c:77
static int dump
Dump the database.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
static struct GNUNET_SCHEDULER_Task * t
Main task.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_SYSERR
Definition: gnunet_common.h:93

References consensus-simulation::args, BENCHMARK_END, BENCHMARK_START, ctx, doHMAC(), dump, getPRK(), GNUNET_break, GNUNET_free, GNUNET_malloc, GNUNET_memcpy, GNUNET_SYSERR, GNUNET_YES, len, result, ret, and t.

Referenced by GNUNET_CRYPTO_hkdf(), and GNUNET_CRYPTO_kdf_v().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_kdf()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf ( void *  result,
size_t  out_len,
const void *  xts,
size_t  xts_len,
const void *  skm,
size_t  skm_len,
  ... 
)

Derive key.

Parameters
resultbuffer for the derived key, allocated by caller
out_lendesired length of the derived key
xtssalt
xts_lenlength of xts
skmsource key material
skm_lenlength of skm
...void * & size_t pairs for context chunks
Returns
GNUNET_YES on success

Definition at line 90 of file crypto_kdf.c.

96 {
97  va_list argp;
98  int ret;
99 
100  va_start (argp, skm_len);
102  out_len,
103  xts,
104  xts_len,
105  skm,
106  skm_len,
107  argp);
108  va_end (argp);
109 
110  return ret;
111 }
int GNUNET_CRYPTO_kdf_v(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
Derive key.
Definition: crypto_kdf.c:47

References GNUNET_CRYPTO_kdf_v(), result, and ret.

Referenced by checkvec(), compute_global_id(), derive_aes_key(), derive_block_aes_key(), derive_block_xsalsa_key(), derive_h(), derive_ublock_encryption_key(), dv_setup_key_state_from_km(), GNUNET_CRYPTO_hash_to_aes_key(), GNUNET_CRYPTO_kdf_mod_mpi(), hash_from_share_val(), output_vectors(), setup_cipher(), t_ax_decrypt_and_validate(), t_ax_encrypt(), t_hmac_derive_key(), and update_ax_by_kx().

Here is the call graph for this function:
Here is the caller graph for this function: