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
 Context for cummulative hashing. More...
 

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)
 Hash block of given size. More...
 
void GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
 Convert GNUNET_CRYPTO_hash to ASCII encoding. More...
 
int GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen, struct GNUNET_HashCode *result)
 Convert ASCII encoding back to hash code. 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_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(delta) = b - a More...
 
void GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *delta, struct GNUNET_HashCode *result)
 compute result(b) = 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 (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 struct GNUNET_HashCodes 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' 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...
 
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, \
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * filename

Definition at line 34 of file crypto_hash.c.

Function Documentation

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

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

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

406 {
407  static int once;
408  static gcry_md_hd_t md;
409  const unsigned char *mc;
410 
411  if (! once)
412  {
413  once = 1;
414  GNUNET_assert (GPG_ERR_NO_ERROR ==
415  gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
416  }
417  else
418  {
419  gcry_md_reset (md);
420  }
421  gcry_md_setkey (md, key, key_len);
422  gcry_md_write (md, plaintext, plaintext_len);
423  mc = gcry_md_read (md, GCRY_MD_SHA512);
424  GNUNET_assert (NULL != mc);
425  GNUNET_memcpy (hmac->bits, mc, sizeof(hmac->bits));
426 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int once
Global to mark if we&#39;ve run the initialization.
Definition: gnsrecord.c:68
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t bits[512/8/sizeof(uint32_t)]
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 466 of file crypto_hash.c.

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

Referenced by GCCH_hash_port(), and GNUNET_SET_element_hash().

467 {
468  struct GNUNET_HashContext *hc;
469 
470  BENCHMARK_START (hash_context_start);
471 
472  hc = GNUNET_new (struct GNUNET_HashContext);
473  GNUNET_assert (0 ==
474  gcry_md_open (&hc->hd,
475  GCRY_MD_SHA512,
476  0));
477 
478  BENCHMARK_END (hash_context_start);
479 
480  return hc;
481 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:57
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define BENCHMARK_END(opname)
Definition: benchmark.h:58
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Context for cummulative hashing.
Definition: crypto_hash.c:451
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:456
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
hccummulative hash context
bufdata to add
sizenumber of bytes in buf

Definition at line 492 of file crypto_hash.c.

References BENCHMARK_END, BENCHMARK_START, and GNUNET_HashContext::hd.

Referenced by GCCH_hash_port(), and GNUNET_SET_element_hash().

495 {
496  BENCHMARK_START (hash_context_read);
497  gcry_md_write (hc->hd, buf, size);
498  BENCHMARK_END (hash_context_read);
499 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:57
#define BENCHMARK_END(opname)
Definition: benchmark.h:58
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:456
Here is the caller graph for this function:

◆ 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
r_hashwhere to write the latest / final hash code

Definition at line 509 of file crypto_hash.c.

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

Referenced by GCCH_hash_port(), and GNUNET_SET_element_hash().

511 {
512  const void *res = gcry_md_read (hc->hd, 0);
513 
514  BENCHMARK_START (hash_context_finish);
515 
516  GNUNET_assert (NULL != res);
517  if (NULL != r_hash)
518  GNUNET_memcpy (r_hash,
519  res,
520  sizeof(struct GNUNET_HashCode));
522  BENCHMARK_END (hash_context_finish);
523 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:57
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define BENCHMARK_END(opname)
Definition: benchmark.h:58
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_CRYPTO_hash_context_abort(struct GNUNET_HashContext *hc)
Abort hashing, do not bother calculating final result.
Definition: crypto_hash.c:532
A 512-bit hashcode.
static int res
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:456
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 532 of file crypto_hash.c.

References GNUNET_free, and GNUNET_HashContext::hd.

Referenced by GNUNET_CRYPTO_hash_context_finish().

533 {
534  gcry_md_close (hc->hd);
535  GNUNET_free (hc);
536 }
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:456
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: