Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_kdf_ctr.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2024, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_KDF_CTR_H
21#define LC_KDF_CTR_H
22
23#include "ext_headers.h"
24#include "lc_hash.h"
25#include "lc_rng.h"
26#include "lc_hmac.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
46int lc_kdf_ctr_init(struct lc_hmac_ctx *hmac_ctx, const uint8_t *key,
47 size_t keylen);
48
64int lc_kdf_ctr_generate(struct lc_hmac_ctx *hmac_ctx, const uint8_t *label,
65 size_t labellen, uint8_t *dst, size_t dlen);
66
83int lc_kdf_ctr(const struct lc_hash *hash, const uint8_t *key, size_t keylen,
84 const uint8_t *label, size_t labellen, uint8_t *dst,
85 size_t dlen);
86
87/***************************** Counter KDF as RNG *****************************/
88
90struct lc_kdf_ctr_ctx {
91 uint32_t counter;
92 uint8_t rng_initialized : 1;
93 struct lc_hmac_ctx hmac_ctx;
94};
95
96#define LC_CTR_KDF_STATE_SIZE(hashname) (LC_HMAC_CTX_SIZE(hashname))
97#define LC_CTR_KDF_CTX_SIZE(hashname) \
98 (sizeof(struct lc_kdf_ctr_ctx) + LC_CTR_KDF_STATE_SIZE(hashname))
99
100#define _LC_CTR_KDF_SET_CTX(name, hashname, ctx, offset) \
101 _LC_HMAC_SET_CTX((&(name)->hmac_ctx), hashname, ctx, offset)
102
103#define LC_CTR_KDF_SET_CTX(name, hashname) \
104 _LC_CTR_KDF_SET_CTX(name, hashname, name, sizeof(struct lc_kdf_ctr_ctx))
105
106/* CTR_KDF DRNG implementation */
107extern const struct lc_rng *lc_kdf_ctr_rng;
108
109#define LC_CTR_KDF_DRNG_CTX_SIZE(hashname) \
110 (sizeof(struct lc_rng_ctx) + LC_CTR_KDF_CTX_SIZE(hashname))
111
112#define LC_CTR_KDF_DRNG_SET_CTX(name, hashname) \
113 LC_CTR_KDF_SET_CTX(name, hashname)
114
115#define LC_CTR_KDF_RNG_CTX(name, hashname) \
116 LC_RNG_CTX(name, lc_kdf_ctr_rng); \
117 LC_CTR_KDF_DRNG_SET_CTX(((struct lc_kdf_ctr_ctx *)(name->rng_state)), \
118 hashname); \
119 lc_rng_zero(name)
121
131#define LC_CTR_KDF_DRNG_CTX_ON_STACK(name, hashname) \
132 _Pragma("GCC diagnostic push") \
133 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
134 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
135 LC_ALIGNED_BUFFER(name##_ctx_buf, \
136 LC_CTR_KDF_DRNG_CTX_SIZE(hashname), \
137 LC_HASH_COMMON_ALIGNMENT); \
138 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
139 LC_CTR_KDF_RNG_CTX(name, hashname); \
140 _Pragma("GCC diagnostic pop")
141
157int lc_kdf_ctr_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash);
158
159#ifdef __cplusplus
160}
161#endif
162
163#endif /* LC_KDF_CTR_H */
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_kdf_ctr_generate(struct lc_hmac_ctx *hmac_ctx, const uint8_t *label, size_t labellen, uint8_t *dst, size_t dlen)
Key-based Key Derivation in Counter Mode - SP800-108 - data generation.
int lc_kdf_ctr_init(struct lc_hmac_ctx *hmac_ctx, const uint8_t *key, size_t keylen)
Key-based Key Derivation in Counter Mode - SP800-108 - initialization.
int lc_kdf_ctr(const struct lc_hash *hash, const uint8_t *key, size_t keylen, const uint8_t *label, size_t labellen, uint8_t *dst, size_t dlen)
One-shot Key-based Key Derivation in Counter Mode - SP800-108.
int lc_kdf_ctr_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash)
Allocation of a Counter KDF DRNG context.