Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_symkmac.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2026, 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_SYMKMAC_H
21#define LC_SYMKMAC_H
22
23#include "lc_aead.h"
24#include "lc_sym.h"
25#include "lc_kmac.h"
26#include "lc_memset_secure.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33struct lc_kh_cryptor {
34 struct lc_sym_ctx sym;
35 struct lc_kmac_ctx auth_ctx;
36};
37
38#define LC_KH_STATE_SIZE(sym) (LC_SYM_STATE_SIZE(sym) + LC_KMAC_STATE_SIZE)
39#define LC_KH_CTX_SIZE(sym) \
40 (sizeof(struct lc_aead) + sizeof(struct lc_kh_cryptor) + \
41 LC_KH_STATE_SIZE(sym))
42
43/* AES-CBC with KMAC based AEAD-algorithm */
44extern const struct lc_aead *lc_symkmac_aead;
45
46#define _LC_KH_SET_CTX(name, symalgo, hash) \
47 _LC_SYM_SET_CTX((&name->sym), symalgo, name, \
48 (sizeof(struct lc_kh_cryptor))); \
49 _LC_KMAC_SET_CTX( \
50 (&name->auth_ctx), hash, name, \
51 (sizeof(struct lc_kh_cryptor) + LC_SYM_STATE_SIZE(symalgo)))
52
53#define LC_KH_SET_CTX(name, sym, hash) \
54 LC_AEAD_CTX(name, lc_symkmac_aead); \
55 _LC_KH_SET_CTX(((struct lc_kh_cryptor *)name->aead_state), sym, hash)
57
296int lc_kh_alloc(const struct lc_sym *sym, const struct lc_hash *hash,
297 struct lc_aead_ctx **ctx);
298
310#define LC_KH_CTX_ON_STACK(name, sym, hash) \
311 _Pragma("GCC diagnostic push") \
312 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
313 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
314 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_KH_CTX_SIZE(sym), \
315 LC_HASH_COMMON_ALIGNMENT); \
316 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
317 LC_KH_SET_CTX(name, sym, hash); \
318 lc_aead_zero(name); \
319 _Pragma("GCC diagnostic pop")
320
321#ifdef __cplusplus
322}
323#endif
324
325#endif /* LC_SYMKMAC_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_kh_alloc(const struct lc_sym *sym, const struct lc_hash *hash, struct lc_aead_ctx **ctx)
Allocate symmetric algorithm with KMAC cryptor context on heap.