GNUnet  0.11.x
Macros | Functions
crypto_hkdf.c File Reference

Hash-based KDF as defined in RFC 5869. More...

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

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-hkdf", __VA_ARGS__)
 
#define GNUNET_BUILD   1
 Set this to 0 if you compile this code outside of GNUnet. More...
 
#define DEBUG_HKDF   0
 Enable debugging. More...
 

Functions

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. More...
 
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. More...
 
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. More...
 
int 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...
 

Detailed Description

Hash-based KDF as defined in RFC 5869.

See also
http://www.rfc-editor.org/rfc/rfc5869.txt
Todo:
remove GNUNET references
Author
Nils Durner

The following list of people have reviewed this code and considered it correct on the date given (if you reviewed it, please have your name added to the list):

Definition in file crypto_hkdf.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 39 of file crypto_hkdf.c.

◆ GNUNET_BUILD

#define GNUNET_BUILD   1

Set this to 0 if you compile this code outside of GNUnet.

Definition at line 44 of file crypto_hkdf.c.

◆ DEBUG_HKDF

#define DEBUG_HKDF   0

Enable debugging.

Definition at line 49 of file crypto_hkdf.c.

Function Documentation

◆ doHMAC()

static const void* doHMAC ( gcry_md_hd_t  mac,
const void *  key,
size_t  key_len,
const void *  buf,
size_t  buf_len 
)
static

Compute the HMAC.

Todo:
use chunked buffers
Parameters
macgcrypt MAC handle
keyHMAC key
key_lenlength of key
bufmessage to be processed
buf_lenlength of buf
Returns
HMAC, freed by caller via gcry_md_close/_reset

Definition at line 77 of file crypto_hkdf.c.

79 {
80  if (GPG_ERR_NO_ERROR != gcry_md_setkey (mac, key, key_len))
81  {
82  GNUNET_break (0);
83  return NULL;
84  }
85  gcry_md_write (mac, buf, buf_len);
86 
87  return (const void *) gcry_md_read (mac, 0);
88 }
struct GNUNET_HashCode key
The key used in the DHT.
static char buf[2048]
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.

References buf, GNUNET_break, and key.

Referenced by getPRK(), and GNUNET_CRYPTO_hkdf_v().

Here is the caller graph for this function:

◆ getPRK()

static int getPRK ( gcry_md_hd_t  mac,
const void *  xts,
size_t  xts_len,
const void *  skm,
size_t  skm_len,
void *  prk 
)
static

Generate pseudo-random key.

Parameters
macgcrypt HMAC handle
xtssalt
xts_lenlength of the xts salt
skmsource key material
skm_lenlength of skm
prkresult buffer (allocated by caller; at least gcry_md_dlen() bytes)
Returns
GNUNET_YES on success

Definition at line 102 of file crypto_hkdf.c.

104 {
105  const void *ret;
106  size_t dlen;
107 
108  dlen = gcry_md_get_algo_dlen (gcry_md_get_algo (mac));
109 
110  /* sanity check to bound stack allocation */
111  GNUNET_assert (dlen <= 512);
112 
113  /* From RFC 5869:
114  * salt - optional salt value (a non-secret random value);
115  * if not provided, it is set to a string of HashLen zeros. */
116 
117  if (xts_len == 0)
118  {
119  char zero_salt[dlen];
120  memset (zero_salt, 0, dlen);
121  ret = doHMAC (mac, zero_salt, dlen, skm, skm_len);
122  }
123  else
124  {
125  ret = doHMAC (mac, xts, xts_len, skm, skm_len);
126  }
127  if (ret == NULL)
128  return GNUNET_SYSERR;
129  GNUNET_memcpy (prk, ret, dlen);
130 
131  return GNUNET_YES;
132 }
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 ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.

References doHMAC(), GNUNET_assert, GNUNET_memcpy, GNUNET_SYSERR, GNUNET_YES, and ret.

Referenced by GNUNET_CRYPTO_hkdf_v().

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