GNUnet  0.19.2
Crypto library: hash operations

Provides hashing and operations on hashes. More...

Collaboration diagram for Crypto library: hash operations:

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_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 848 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 151 of file crypto_crc.c.

153 {
154  const uint8_t *data = buf;
155  unsigned int crc = 0;
156  int i;
157  int j;
158 
159  for (j = len; 0 != j; j--)
160  {
161  crc ^= (*data++ << 8);
162  for (i = 8; 0 != i; i--)
163  {
164  if (0 != (crc & 0x8000))
165  crc ^= (0x1070 << 3);
166  crc <<= 1;
167  }
168  }
169  return (uint8_t) (crc >> 8);
170 }
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

Definition at line 133 of file crypto_crc.c.

134 {
135  const uint16_t *hdr = buf;
136  uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
137 
139 }
uint16_t GNUNET_CRYPTO_crc16_finish(uint32_t sum)
Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
Definition: crypto_crc.c:123
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:110

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

Definition at line 99 of file crypto_crc.c.

100 {
101  GNUNET_uLong crc;
102 
103  crc = gn_crc32 (0L, Z_NULL, 0);
104  crc = gn_crc32 (crc, (char *) buf, len);
105  return crc;
106 }
static GNUNET_uLong gn_crc32(GNUNET_uLong crc, const char *buf, size_t len)
Definition: crypto_crc.c:87
#define Z_NULL
Definition: crypto_crc.c:45

References buf, gn_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:708
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 
79  if (GNUNET_OK != GNUNET_STRINGS_utf8_toupper (enc, up_ptr))
80  return GNUNET_SYSERR;
81 
82  return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
83  (unsigned char *) result,
84  sizeof(struct GNUNET_HashCode));
85 }
static OpusEncoder * enc
OPUS encoder.
@ GNUNET_OK
@ GNUNET_SYSERR
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
Convert the utf-8 input string to upper case.
Definition: strings.c:471
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:788

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 89 of file crypto_hash.c.

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

References 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:68

References BENCHMARK_END, BENCHMARK_START, ret, and size.

Referenced by add_file(), add_member_session(), add_to_keyword_counter(), add_to_meta_counter(), advertise_dns_exit(), automaton_create_proofs(), barrier_attached(), barrier_wait_cb(), bind_store_ego(), build_authz_response(), calculate_hmac(), callback_ego_create(), callback_ego_lookup(), callback_ego_rename(), check_and_remove_pending_reversal(), checkvec(), client_request_complete_alice(), code_redirect(), collector(), commit_set(), consume_fail(), consume_ticket(), convert_messenger_key_to_port(), cookie_identity_interpretation(), create_response(), create_target(), cs_full_domain_hash(), database_setup(), decode_short_message(), decrementBitCallback(), decrypt_new_element(), delayed_put(), delete_store_ego(), derive_aes_key(), derive_auth_key(), derive_iv(), derive_pong_iv(), determine_id(), do_flood(), do_rekey(), ego_get_all(), ego_get_response(), ego_sign_data(), encode_short_message(), encrypt_existing_match(), es_to_sh(), find_advertisable_hello(), find_target(), find_trees(), GC_u2h(), GCT_add_channel(), GDS_try_connect(), GDS_u_connect(), get_cadet(), 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_node_info(), get_store_contact(), get_update_information_directory(), get_url_parameter_copy(), GNUNET_b2s(), GNUNET_BLOCK_mingle_hash(), GNUNET_CONVERSATION_phone_create(), GNUNET_CRYPTO_ecc_ecdh(), GNUNET_CRYPTO_edx25519_key_create_from_seed(), GNUNET_CRYPTO_rsa_public_key_hash(), GNUNET_DHT_verify_path(), 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_barrier_get_node(), GNUNET_TESTING_get_topo_from_string(), 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_update(), handle_put(), 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(), load_state(), login_cont(), lookup_diff(), lookup_rfn(), lookup_set(), lookup_task(), maint_child_death(), mq_init(), mysql_plugin_put(), namestore_get(), notify_connect(), notify_srv_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(), pending_reversal_timeout(), post_data_iter(), postgres_plugin_put(), process_kblock_for_unindex(), 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(), set_result_cb(), setup_filter(), sign_path(), sock_read(), sqlite_plugin_put(), start_helper(), start_intersection(), token_endpoint(), try_match_block(), try_open_exit(), try_top_down_reconstruction(), TST_interpreter_add_barrier(), TST_interpreter_get_barrier(), unbind_store_ego(), 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 329 of file crypto_hash.c.

332 {
333  GNUNET_CRYPTO_hmac_raw ((void *) key->key, sizeof(key->key),
334  plaintext, plaintext_len,
335  hmac);
336 }
struct GNUNET_HashCode key
The key used in the DHT.
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't this be the standard hmac function and the abo...
Definition: crypto_hash.c:299

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 169 of file crypto_hash_file.c.

174 {
175  struct GNUNET_CRYPTO_FileHashContext *fhc;
176 
177  GNUNET_assert (blocksize > 0);
178  fhc =
179  GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize);
180  fhc->callback = callback;
181  fhc->callback_cls = callback_cls;
182  fhc->buffer = (unsigned char *) &fhc[1];
184  if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
185  {
186  GNUNET_break (0);
187  GNUNET_free (fhc);
188  return NULL;
189  }
190  fhc->bsize = blocksize;
191  if (GNUNET_OK !=
193  &fhc->fsize,
194  GNUNET_NO,
195  GNUNET_YES))
196  {
197  GNUNET_free (fhc->filename);
198  GNUNET_free (fhc);
199  return NULL;
200  }
204  if (! fhc->fh)
205  {
206  GNUNET_free (fhc->filename);
207  GNUNET_free (fhc);
208  return NULL;
209  }
210  fhc->priority = priority;
213  fhc);
214  return fhc;
215 }
static void file_hash_task(void *cls)
File hashing task.
static char * filename
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:1234
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:221
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_YES
@ GNUNET_NO
#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:1195
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 100 of file crypto_hash.c.

102 {
103  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(uint32_t)) - 1;
104  i >= 0;
105  i--)
106  result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
107 }
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(), 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 111 of file crypto_hash.c.

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

References 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 123 of file crypto_hash.c.

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

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 135 of file crypto_hash.c.

138 {
139  const unsigned long long *lla = (const unsigned long long *) a;
140  const unsigned long long *llb = (const unsigned long long *) b;
141  unsigned long long *llr = (unsigned long long *) result;
142 
143  GNUNET_static_assert (8 == sizeof (unsigned long long));
144  GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long));
145  for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--)
146  llr[i] = lla[i] ^ llb[i];
147 }
#define GNUNET_static_assert(cond)
Assertion to be checked (if supported by C compiler) at compile time, otherwise checked at runtime an...

References GNUNET_static_assert, and result.

Referenced by add_known_to_bloom(), datacache_get_iterator(), determine_id(), filter_all(), filtered_map_initialization(), get_context_from_member(), get_matching_bits(), initialize_map_unfiltered(), iterator_bf_reduce(), process_sks_result(), and select_peer().

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 151 of file crypto_hash.c.

155 {
158  skey,
159  sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
160  "Hash key derivation",
161  strlen ("Hash key derivation"),
162  hc, sizeof(struct GNUNET_HashCode),
163  NULL, 0));
166  iv,
168  "Initialization vector derivation",
169  strlen ("Initialization vector derivation"),
170  hc, sizeof(struct GNUNET_HashCode),
171  NULL, 0));
172 }
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:70

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_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 220 of file crypto_hash.c.

222 {
223  unsigned int *i1;
224  unsigned int *i2;
225 
226  i1 = (unsigned int *) h1;
227  i2 = (unsigned int *) h2;
228  for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1;
229  i >= 0;
230  i--)
231  {
232  if (i1[i] > i2[i])
233  return 1;
234  if (i1[i] < i2[i])
235  return -1;
236  }
237  return 0;
238 }

References consensus-simulation::int.

Referenced by check_member_session_completion(), create_message_request(), decode_short_message(), element_cmp(), 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 242 of file crypto_hash.c.

245 {
246  const unsigned long long *l1 = (const unsigned long long *) h1;
247  const unsigned long long *l2 = (const unsigned long long *) h2;
248  const unsigned long long *t = (const unsigned long long *) target;
249 
250  GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
251  for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
252  {
253  unsigned long long x1 = l1[i] ^ t[i];
254  unsigned long long x2 = l2[i] ^ t[i];
255 
256  if (x1 > x2)
257  return 1;
258  if (x1 < x2)
259  return -1;
260  }
261  return 0;
262 }
static struct GNUNET_SCHEDULER_Task * t
Main task.

References GNUNET_static_assert, and t.

Referenced by select_peer().

Here is the caller graph for this function:

◆ 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 285 of file crypto_hash.c.

290 {
291  GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key),
292  salt, salt_len,
293  rkey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
294  argp);
295 }
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:38

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 266 of file crypto_hash.c.

271 {
272  va_list argp;
273 
274  va_start (argp,
275  salt_len);
277  rkey,
278  salt, salt_len,
279  argp);
280  va_end (argp);
281 }
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:285

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

Definition at line 78 of file crypto_hkdf.c.

349 {
350  va_list argp;
352 
353  va_start (argp, skm_len);
354  ret =
356  out_len,
357  xtr_algo,
358  prf_algo,
359  xts,
360  xts_len,
361  skm,
362  skm_len,
363  argp);
364  va_end (argp);
365  return ret;
366 }
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.
Definition: crypto_hkdf.c:166
GNUNET_GenericReturnValue
Named constants for return values.

References buf, GNUNET_break, and key.

Referenced by calculate_cmac(), get_iv_key(), get_kid(), GNUNET_CRYPTO_cs_blinding_secrets_derive(), GNUNET_CRYPTO_cs_sign_derive(), and output_vectors().

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 78 of file crypto_hkdf.c.

175 {
176  gcry_md_hd_t xtr;
177  gcry_md_hd_t prf;
178  const void *hc;
179  unsigned long i;
180  unsigned long t;
181  unsigned long d;
182  unsigned int k = gcry_md_get_algo_dlen (prf_algo);
183  unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
184  char prk[xtr_len];
185  int ret;
186  size_t ctx_len;
187  va_list args;
188 
189  BENCHMARK_START (hkdf);
190 
191  if (0 == k)
192  return GNUNET_SYSERR;
193  if (GPG_ERR_NO_ERROR !=
194  gcry_md_open (&xtr,
195  xtr_algo,
196  GCRY_MD_FLAG_HMAC))
197  return GNUNET_SYSERR;
198  if (GPG_ERR_NO_ERROR !=
199  gcry_md_open (&prf,
200  prf_algo,
201  GCRY_MD_FLAG_HMAC))
202  {
203  gcry_md_close (xtr);
204  return GNUNET_SYSERR;
205  }
206  va_copy (args, argp);
207 
208  ctx_len = 0;
209  while (NULL != va_arg (args, void *))
210  {
211  size_t nxt = va_arg (args, size_t);
212  if (nxt + ctx_len < nxt)
213  {
214  /* integer overflow */
215  GNUNET_break (0);
216  va_end (args);
217  goto hkdf_error;
218  }
219  ctx_len += nxt;
220  }
221 
222  va_end (args);
223 
224  if ( (k + ctx_len < ctx_len) ||
225  (k + ctx_len + 1 < ctx_len) )
226  {
227  /* integer overflow */
228  GNUNET_break (0);
229  goto hkdf_error;
230  }
231 
232  memset (result, 0, out_len);
233  if (GNUNET_YES !=
234  getPRK (xtr, xts, xts_len, skm, skm_len, prk))
235  goto hkdf_error;
236 #if DEBUG_HKDF
237  dump ("PRK", prk, xtr_len);
238 #endif
239 
240  t = out_len / k;
241  d = out_len % k;
242 
243  /* K(1) */
244  {
245  size_t plain_len = k + ctx_len + 1;
246  char *plain;
247  const void *ctx;
248  char *dst;
249 
250  plain = GNUNET_malloc (plain_len);
251  dst = plain + k;
252  va_copy (args, argp);
253  while ((ctx = va_arg (args, void *)))
254  {
255  size_t len;
256 
257  len = va_arg (args, size_t);
258  GNUNET_memcpy (dst, ctx, len);
259  dst += len;
260  }
261  va_end (args);
262 
263  if (t > 0)
264  {
265  plain[k + ctx_len] = (char) 1;
266 #if DEBUG_HKDF
267  dump ("K(1)", plain, plain_len);
268 #endif
269  hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
270  if (hc == NULL)
271  {
272  GNUNET_free (plain);
273  goto hkdf_error;
274  }
275  GNUNET_memcpy (result, hc, k);
276  result += k;
277  }
278 
279  /* K(i+1) */
280  for (i = 1; i < t; i++)
281  {
282  GNUNET_memcpy (plain, result - k, k);
283  plain[k + ctx_len] = (char) (i + 1);
284  gcry_md_reset (prf);
285 #if DEBUG_HKDF
286  dump ("K(i+1)", plain, plain_len);
287 #endif
288  hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
289  if (NULL == hc)
290  {
291  GNUNET_free (plain);
292  goto hkdf_error;
293  }
294  GNUNET_memcpy (result, hc, k);
295  result += k;
296  }
297 
298  /* K(t):d */
299  if (d > 0)
300  {
301  if (t > 0)
302  {
303  GNUNET_memcpy (plain, result - k, k);
304  i++;
305  }
306  plain[k + ctx_len] = (char) i;
307  gcry_md_reset (prf);
308 #if DEBUG_HKDF
309  dump ("K(t):d", plain, plain_len);
310 #endif
311  if (t > 0)
312  hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
313  else
314  hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
315  if (hc == NULL)
316  {
317  GNUNET_free (plain);
318  goto hkdf_error;
319  }
320  GNUNET_memcpy (result, hc, d);
321  }
322 #if DEBUG_HKDF
323  dump ("result", result - k, out_len);
324 #endif
325 
326  ret = GNUNET_YES;
327  GNUNET_free (plain);
328  goto hkdf_ok;
329  }
330 hkdf_error:
331  ret = GNUNET_SYSERR;
332 hkdf_ok:
333  gcry_md_close (xtr);
334  gcry_md_close (prf);
335  BENCHMARK_END (hkdf);
336  return ret;
337 }
static enum GNUNET_GenericReturnValue 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:108
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:78
static int dump
Dump the database.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.

◆ 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 1 of file crypto_kdf.c.

76 {
77  va_list argp;
78  int ret;
79 
80  va_start (argp, skm_len);
82  out_len,
83  xts,
84  xts_len,
85  skm,
86  skm_len,
87  argp);
88  va_end (argp);
89 
90  return ret;
91 }

Referenced by checkvec(), compute_global_id(), derive_aes_key(), derive_h(), derive_ublock_encryption_key(), dh_key_derive_eph_pid(), GNR_derive_block_aes_key(), GNR_derive_block_xsalsa_key(), GNUNET_CRYPTO_cs_r_derive(), GNUNET_CRYPTO_ecdsa_sign_derived(), 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 caller graph for this function: