GNUnet  0.10.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)   GNUNET_log_from_strerror_file(kind, "util-crypto-hash", 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 
)    GNUNET_log_from_strerror_file(kind, "util-crypto-hash", syscall, 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 383 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().

386 {
387  static int once;
388  static gcry_md_hd_t md;
389  const unsigned char *mc;
390 
391  if (!once)
392  {
393  once = 1;
394  GNUNET_assert(GPG_ERR_NO_ERROR ==
395  gcry_md_open(&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
396  }
397  else
398  {
399  gcry_md_reset(md);
400  }
401  gcry_md_setkey(md, key, key_len);
402  gcry_md_write(md, plaintext, plaintext_len);
403  mc = gcry_md_read(md, GCRY_MD_SHA512);
404  GNUNET_assert(NULL != mc);
405  GNUNET_memcpy(hmac->bits, mc, sizeof(hmac->bits));
406 }
#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:67
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 445 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().

446 {
447  struct GNUNET_HashContext *hc;
448 
449  BENCHMARK_START(hash_context_start);
450 
451  hc = GNUNET_new(struct GNUNET_HashContext);
452  GNUNET_assert(0 ==
453  gcry_md_open(&hc->hd,
454  GCRY_MD_SHA512,
455  0));
456 
457  BENCHMARK_END(hash_context_start);
458 
459  return hc;
460 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:53
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define BENCHMARK_END(opname)
Definition: benchmark.h:54
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Context for cummulative hashing.
Definition: crypto_hash.c:431
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:435
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 471 of file crypto_hash.c.

References BENCHMARK_END, BENCHMARK_START, and GNUNET_HashContext::hd.

Referenced by GCCH_hash_port(), and GNUNET_SET_element_hash().

474 {
475  BENCHMARK_START(hash_context_read);
476  gcry_md_write(hc->hd, buf, size);
477  BENCHMARK_END(hash_context_read);
478 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:53
#define BENCHMARK_END(opname)
Definition: benchmark.h:54
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:66
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:435
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 488 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().

490 {
491  const void *res = gcry_md_read(hc->hd, 0);
492 
493  BENCHMARK_START(hash_context_finish);
494 
495  GNUNET_assert(NULL != res);
496  if (NULL != r_hash)
497  GNUNET_memcpy(r_hash,
498  res,
499  sizeof(struct GNUNET_HashCode));
501  BENCHMARK_END(hash_context_finish);
502 }
#define BENCHMARK_START(opname)
Definition: benchmark.h:53
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define BENCHMARK_END(opname)
Definition: benchmark.h:54
#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:511
A 512-bit hashcode.
static int res
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:435
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 511 of file crypto_hash.c.

References GNUNET_free, and GNUNET_HashContext::hd.

Referenced by GNUNET_CRYPTO_hash_context_finish().

512 {
513  gcry_md_close(hc->hd);
514  GNUNET_free(hc);
515 }
gcry_md_hd_t hd
Internal state of the hash function.
Definition: crypto_hash.c:435
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: