39#include "sodium/utils.h"
41#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hkdf", __VA_ARGS__)
46#include "sodium/crypto_auth_hmacsha256.h"
51 const unsigned char *prk,
55 unsigned char *outbuf = (
unsigned char*)
result;
60 if (out_len > (0xff * crypto_auth_hmacsha256_BYTES))
66 while (NULL != va_arg (
args,
void *))
68 size_t nxt = va_arg (
args,
size_t);
69 if (nxt + ctx_len < nxt)
81 if ( (crypto_auth_hmacsha256_BYTES + ctx_len < ctx_len) ||
82 (crypto_auth_hmacsha256_BYTES + ctx_len + 1 < ctx_len) )
89 memset (
result, 0, out_len);
92 size_t left = out_len;
94 unsigned char tmp[crypto_auth_hmacsha256_BYTES];
95 unsigned char ctx[ctx_len];
96 unsigned char *dst =
ctx;
97 crypto_auth_hmacsha256_state
st;
98 unsigned char counter = 1U;
100 sodium_memzero (
ctx,
sizeof ctx);
101 va_copy (
args, argp);
102 while ((ctx_arg = va_arg (
args,
void *)))
106 len = va_arg (
args,
size_t);
112 for (i = 0; left > 0; i += crypto_auth_hmacsha256_BYTES)
114 crypto_auth_hmacsha256_init(&
st, prk, prk_len);
117 crypto_auth_hmacsha256_update(&
st,
118 &outbuf[i - crypto_auth_hmacsha256_BYTES],
119 crypto_auth_hmacsha256_BYTES);
121 crypto_auth_hmacsha256_update(&
st,
ctx, ctx_len);
122 crypto_auth_hmacsha256_update(&
st, &counter, 1);
123 if (left >= crypto_auth_hmacsha256_BYTES)
125 crypto_auth_hmacsha256_final(&
st, &outbuf[i]);
126 left -= crypto_auth_hmacsha256_BYTES;
130 crypto_auth_hmacsha256_final(&
st, tmp);
131 memcpy (&outbuf[i], tmp, left);
132 sodium_memzero(tmp,
sizeof tmp);
137 sodium_memzero(&
st,
sizeof st);
150 (
unsigned char*) prk,
sizeof *prk,
164 va_start (argp, prk);
180 unsigned char prk[crypto_auth_hmacsha512_BYTES];
181 crypto_auth_hmacsha512_state
st;
183 memset (
result, 0, out_len);
184 if (crypto_auth_hmacsha512_init (&
st, xts, xts_len))
186 if (crypto_auth_hmacsha512_update (&
st, skm, skm_len))
188 crypto_auth_hmacsha512_final (&
st, (
unsigned char*) prk);
189 sodium_memzero (&
st,
sizeof st);
209 va_start (argp, skm_len);
230 crypto_auth_hmacsha256_state
st;
231 if (crypto_auth_hmacsha256_init (&
st, xts, xts_len))
233 if (crypto_auth_hmacsha256_update (&
st, skm, skm_len))
235 crypto_auth_hmacsha256_final (&
st, (
unsigned char*) prk);
236 sodium_memzero (&
st,
sizeof st);
static enum GNUNET_GenericReturnValue hkdf_expand(void *result, size_t out_len, const unsigned char *prk, size_t prk_len, va_list argp)
static int ret
Final status code.
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
static struct GNUNET_FS_Handle * ctx
static int result
Global testing status.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_extract(struct GNUNET_ShortHashCode *prk, const void *xts, size_t xts_len, const void *skm, size_t skm_len)
HKDF-Extract using SHA256.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_gnunet(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_gnunet_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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_expand(void *result, size_t out_len, const struct GNUNET_ShortHashCode *prk,...)
HKDF-Expand using SHA256.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_expand_v(void *result, size_t out_len, const struct GNUNET_ShortHashCode *prk, va_list argp)
HKDF-Expand using SHA256.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.