23#include "ext_headers.h"
34 uint8_t partial[LC_SHA_MAX_SIZE_DIGEST];
37 uint8_t rng_initialized : 1;
38 struct lc_hmac_ctx hmac_ctx;
41#define LC_HKDF_STATE_SIZE(hashname) (LC_HMAC_CTX_SIZE(hashname))
42#define LC_HKDF_CTX_SIZE(hashname) \
43 (sizeof(struct lc_hkdf_ctx) + LC_HKDF_STATE_SIZE(hashname))
45#define _LC_HKDF_SET_CTX(name, hashname, ctx, offset) \
46 _LC_HMAC_SET_CTX((&(name)->hmac_ctx), hashname, ctx, offset)
48#define LC_HKDF_SET_CTX(name, hashname) \
49 _LC_HKDF_SET_CTX(name, hashname, name, sizeof(struct lc_hkdf_ctx))
74 size_t ikmlen,
const uint8_t *salt,
size_t saltlen);
93 size_t infolen, uint8_t *dst,
size_t dlen);
110 hkdf_ctx->ctr = 0x01;
111 hkdf_ctx->rng_initialized = 0;
112 hkdf_ctx->partial_ptr = LC_SHA_MAX_SIZE_DIGEST;
142#define LC_HKDF_CTX_ON_STACK(name, hashname) \
143 _Pragma("GCC diagnostic push") \
144 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
145 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
146 LC_ALIGNED_BUFFER(name##_ctx_buf, \
147 LC_HKDF_CTX_SIZE(hashname), \
148 LC_HASH_COMMON_ALIGNMENT); \
149 struct lc_hkdf_ctx *name = (struct lc_hkdf_ctx *)name##_ctx_buf; \
150 LC_HKDF_SET_CTX(name, hashname); \
151 lc_hkdf_zero(name); \
152 _Pragma("GCC diagnostic pop")
174 size_t ikmlen,
const uint8_t *salt,
size_t saltlen,
175 const uint8_t *info,
size_t infolen, uint8_t *dst,
205#define LC_HKDF_DRNG_CTX_SIZE(hashname) \
206 (sizeof(struct lc_rng_ctx) + LC_HKDF_CTX_SIZE(hashname))
208#define LC_HKDF_DRNG_SET_CTX(name, hashname) LC_HKDF_SET_CTX(name, hashname)
210#define LC_HKDF_RNG_CTX(name, hashname) \
211 LC_RNG_CTX(name, lc_hkdf_rng); \
212 LC_HKDF_DRNG_SET_CTX(((struct lc_hkdf_ctx *)(name->rng_state)), \
225#define LC_HKDF_DRNG_CTX_ON_STACK(name, hashname) \
226 _Pragma("GCC diagnostic push") \
227 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
228 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
229 LC_ALIGNED_BUFFER(name##_ctx_buf, \
230 LC_HKDF_DRNG_CTX_SIZE(hashname), \
231 LC_HASH_COMMON_ALIGNMENT); \
232 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
233 LC_HKDF_RNG_CTX(name, hashname); \
234 _Pragma("GCC diagnostic pop")
static void lc_hmac_zero(struct lc_hmac_ctx *hmac_ctx)
Zeroize HMAC context allocated with either HMAC_CTX_ON_STACK or hmac_alloc.
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_hkdf_expand(struct lc_hkdf_ctx *hkdf_ctx, const uint8_t *info, size_t infolen, uint8_t *dst, size_t dlen)
HMAC-based Extract-and-Expand Key Derivation Function (HKDF) - RFC5869 Expand phase.
#define LC_HKDF_CTX_ON_STACK(name, hashname)
Allocate stack memory for the HKDF context.
static int lc_hkdf(const struct lc_hash *hash, const uint8_t *ikm, size_t ikmlen, const uint8_t *salt, size_t saltlen, const uint8_t *info, size_t infolen, uint8_t *dst, size_t dlen)
HMAC-based Extract-and-Expand Key Derivation Function (HKDF) - RFC5869 Complete implementation.
void lc_hkdf_zero_free(struct lc_hkdf_ctx *hkdf_ctx)
Zeroize and free HKDF context.
static void lc_hkdf_zero(struct lc_hkdf_ctx *hkdf_ctx)
Zeroize HKDF context allocated with either LC_HKDF_CTX_ON_STACK or hkdf_alloc.
int lc_hkdf_alloc(const struct lc_hash *hash, struct lc_hkdf_ctx **hkdf_ctx)
Allocate HKDF context on heap.
int lc_hkdf_extract(struct lc_hkdf_ctx *hkdf_ctx, const uint8_t *ikm, size_t ikmlen, const uint8_t *salt, size_t saltlen)
HMAC-based Extract-and-Expand Key Derivation Function (HKDF) - RFC5869 Extract phase.
int lc_hkdf_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash)
Allocation of a HKDF DRNG context.
const struct lc_rng * lc_hkdf_rng
static void lc_memset_secure(void *s, int c, size_t n)