GNUnet  0.11.x
Data Structures | Macros | Functions
crypto_hash.c File Reference

SHA-512 GNUNET_CRYPTO_hash() related functions. More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_strings_lib.h"
#include "benchmark.h"
#include <gcrypt.h>
Include dependency graph for crypto_hash.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_HashContext
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)
 

Functions

void GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode *ret)
 Compute hash of a given block. 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...
 
unsigned int 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_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...
 
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h)
 Count the number of leading 0 bits in h. More...
 
unsigned int GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h)
 Count the number of tailing 0 bits in h. 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 (struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len,...)
 Derive an authentication key. 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_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 above be renamed? 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_HashContextGNUNET_CRYPTO_hash_context_start ()
 Start incremental hashing operation. More...
 
void GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc, const void *buf, size_t size)
 Add data to be hashed. More...
 
struct GNUNET_HashContextGNUNET_CRYPTO_hash_context_copy (const struct GNUNET_HashContext *hc)
 Make a copy of the hash computation. More...
 
void GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
 Finish the hash computation. More...
 
void GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
 Abort hashing, do not bother calculating final result. More...
 

Detailed Description

SHA-512 GNUNET_CRYPTO_hash() related functions.

Author
Christian Grothoff

Definition in file crypto_hash.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)

Definition at line 32 of file crypto_hash.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)
Value:
"util-crypto-hash", \
syscall, \
static char * filename
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...

Definition at line 34 of file crypto_hash.c.

Function Documentation

◆ GNUNET_CRYPTO_hash_count_leading_zeros()

unsigned int GNUNET_CRYPTO_hash_count_leading_zeros ( const struct GNUNET_HashCode h)

Count the number of leading 0 bits in h.

Parameters
ha hash
Returns
number of leading 0 bits in h

Definition at line 176 of file crypto_hash.c.

177 {
178  const unsigned long long *llp = (const unsigned long long *) h;
179  unsigned int ret = 0;
180  unsigned int i;
181 
182  GNUNET_static_assert (8 == sizeof (unsigned long long));
183  GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
184  for (i = 0; i<sizeof (*h) / sizeof (*llp); i++)
185  {
186  if (0LLU != llp[i])
187  break;
188  ret += sizeof (*llp) * 8;
189  }
190  if (ret == 8 * sizeof (*h))
191  return ret;
192  ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i]));
193  return ret;
194 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
#define GNUNET_static_assert(cond)
Assertion to be checked (if supported by C compiler) at compile time, otherwise checked at runtime an...

References GNUNET_ntohll(), GNUNET_static_assert, h, and ret.

Referenced by add_known_to_bloom(), datacache_get_iterator(), find_proof(), get_matching_bits(), and select_peer().

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

◆ GNUNET_CRYPTO_hash_count_tailing_zeros()

unsigned int GNUNET_CRYPTO_hash_count_tailing_zeros ( const struct GNUNET_HashCode h)

Count the number of tailing 0 bits in h.

Parameters
ha hash
Returns
number of tailing 0 bits in h

Definition at line 198 of file crypto_hash.c.

199 {
200  const unsigned long long *llp = (const unsigned long long *) h;
201  unsigned int ret = 0;
202  int i;
203 
204  GNUNET_static_assert (8 == sizeof (unsigned long long));
205  GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long));
206  for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--)
207  {
208  if (0LLU != llp[i])
209  break;
210  ret += sizeof (*llp) * 8;
211  }
212  if (ret == 8 * sizeof (*h))
213  return ret;
214  ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i]));
215  return ret;
216 }

References GNUNET_ntohll(), GNUNET_static_assert, h, and ret.

Here is the call graph for this function:

◆ GNUNET_CRYPTO_hmac_raw()

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 above be renamed?

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

Definition at line 299 of file crypto_hash.c.

302 {
303  static int once;
304  static gcry_md_hd_t md;
305  const unsigned char *mc;
306 
307  if (! once)
308  {
309  once = 1;
310  GNUNET_assert (GPG_ERR_NO_ERROR ==
311  gcry_md_open (&md,
312  GCRY_MD_SHA512,
313  GCRY_MD_FLAG_HMAC));
314  }
315  else
316  {
317  gcry_md_reset (md);
318  }
319  gcry_md_setkey (md, key, key_len);
320  gcry_md_write (md, plaintext, plaintext_len);
321  mc = gcry_md_read (md, GCRY_MD_SHA512);
322  GNUNET_assert (NULL != mc);
323  GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
324 }
static int once
Global to mark if we've run the initialization.
Definition: gnsrecord.c:68
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint32_t bits[512/8/sizeof(uint32_t)]

References GNUNET_HashCode::bits, GNUNET_assert, GNUNET_memcpy, key, mc, and once.

Referenced by calculate_hmac(), GNUNET_CRYPTO_hmac(), and OIDC_generate_id_token().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_context_start()

struct GNUNET_HashContext* GNUNET_CRYPTO_hash_context_start ( void  )

Start incremental hashing operation.

Returns
context for incremental hash computation

Definition at line 348 of file crypto_hash.c.

349 {
350  struct GNUNET_HashContext *hc;
351 
352  BENCHMARK_START (hash_context_start);
353  hc = GNUNET_new (struct GNUNET_HashContext);
354  GNUNET_assert (0 ==
355  gcry_md_open (&hc->hd,
356  GCRY_MD_SHA512,
357  0));
358  BENCHMARK_END (hash_context_start);
359  return hc;
360 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:57
#define BENCHMARK_END(opname)
Definition: benchmark.h:58
#define GNUNET_new(type)
Allocate a struct or union of the given type.
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:343

References BENCHMARK_END, BENCHMARK_START, GNUNET_assert, GNUNET_new, and GNUNET_HashContext::hd.

Referenced by create_randomized_element_iterator(), decode_and_send(), GCCH_get_id(), GNUNET_CRYPTO_edx25519_private_key_derive(), GNUNET_SET_element_hash(), GNUNET_SETI_element_hash(), GNUNET_SETU_element_hash(), handle_union_p2p_inquiry(), and hash_addresses().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_context_read()

void GNUNET_CRYPTO_hash_context_read ( struct GNUNET_HashContext hc,
const void *  buf,
size_t  size 
)

Add data to be hashed.

Parameters
hccumulative hash context
bufdata to add
sizenumber of bytes in buf

Definition at line 364 of file crypto_hash.c.

367 {
368  BENCHMARK_START (hash_context_read);
369  gcry_md_write (hc->hd, buf, size);
370  BENCHMARK_END (hash_context_read);
371 }
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67

References BENCHMARK_END, BENCHMARK_START, buf, GNUNET_HashContext::hd, and size.

Referenced by create_randomized_element_iterator(), decode_and_send(), GCCH_get_id(), GNUNET_CRYPTO_edx25519_private_key_derive(), GNUNET_SET_element_hash(), GNUNET_SETI_element_hash(), GNUNET_SETU_element_hash(), handle_union_p2p_inquiry(), and hash_addresses().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_context_copy()

struct GNUNET_HashContext* GNUNET_CRYPTO_hash_context_copy ( const struct GNUNET_HashContext hc)

Make a copy of the hash computation.

Parameters
hchash context to use (to continue hashing independently)
Returns
copy of hc

Definition at line 375 of file crypto_hash.c.

376 {
377  struct GNUNET_HashContext *cp;
378 
379  cp = GNUNET_new (struct GNUNET_HashContext);
380  GNUNET_assert (0 ==
381  gcry_md_copy (&cp->hd,
382  hc->hd));
383  return cp;
384 }

References GNUNET_assert, GNUNET_new, and GNUNET_HashContext::hd.

◆ GNUNET_CRYPTO_hash_context_finish()

void GNUNET_CRYPTO_hash_context_finish ( struct GNUNET_HashContext hc,
struct GNUNET_HashCode r_hash 
)

Finish the hash computation.

Parameters
hchash context to use, is freed in the process
r_hashwhere to write the latest / final hash code

Definition at line 388 of file crypto_hash.c.

390 {
391  const void *res = gcry_md_read (hc->hd, 0);
392 
393  BENCHMARK_START (hash_context_finish);
394 
395  GNUNET_assert (NULL != res);
396  if (NULL != r_hash)
397  GNUNET_memcpy (r_hash,
398  res,
399  sizeof(struct GNUNET_HashCode));
401  BENCHMARK_END (hash_context_finish);
402 }
void GNUNET_CRYPTO_hash_context_abort(struct GNUNET_HashContext *hc)
Abort hashing, do not bother calculating final result.
Definition: crypto_hash.c:406
static int res
A 512-bit hashcode.

References BENCHMARK_END, BENCHMARK_START, GNUNET_assert, GNUNET_CRYPTO_hash_context_abort(), GNUNET_memcpy, GNUNET_HashContext::hd, and res.

Referenced by create_randomized_element_iterator(), decode_and_send(), GCCH_get_id(), GNUNET_CRYPTO_edx25519_private_key_derive(), GNUNET_SET_element_hash(), GNUNET_SETI_element_hash(), GNUNET_SETU_element_hash(), handle_union_p2p_inquiry(), and hash_addresses().

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

◆ GNUNET_CRYPTO_hash_context_abort()

void GNUNET_CRYPTO_hash_context_abort ( struct GNUNET_HashContext hc)

Abort hashing, do not bother calculating final result.

Parameters
hchash context to destroy

Definition at line 406 of file crypto_hash.c.

407 {
408  gcry_md_close (hc->hd);
409  GNUNET_free (hc);
410 }
#define GNUNET_free(ptr)
Wrapper around free.

References GNUNET_free, and GNUNET_HashContext::hd.

Referenced by GNUNET_CRYPTO_hash_context_finish().

Here is the caller graph for this function: