Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_symkmac.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_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, hash) \
39 (LC_SYM_STATE_SIZE(sym) + LC_KMAC_STATE_SIZE(hash))
40#define LC_KH_CTX_SIZE(sym, hash) \
41 (sizeof(struct lc_aead) + sizeof(struct lc_kh_cryptor) + \
42 LC_KH_STATE_SIZE(sym, hash))
43
44/* AES-CBC with KMAC based AEAD-algorithm */
45extern const struct lc_aead *lc_symkmac_aead;
46
47#define _LC_KH_SET_CTX(name, symalgo, hash) \
48 _LC_SYM_SET_CTX((&name->sym), symalgo, name, \
49 (sizeof(struct lc_kh_cryptor))); \
50 _LC_KMAC_SET_CTX( \
51 (&name->auth_ctx), hash, name, \
52 (sizeof(struct lc_kh_cryptor) + LC_SYM_STATE_SIZE(symalgo)))
53
54#define LC_KH_SET_CTX(name, sym, hash) \
55 LC_AEAD_CTX(name, lc_symkmac_aead); \
56 _LC_KH_SET_CTX(((struct lc_kh_cryptor *)name->aead_state), sym, hash)
58
297int lc_kh_alloc(const struct lc_sym *sym, const struct lc_hash *hash,
298 struct lc_aead_ctx **ctx);
299
311#define LC_KH_CTX_ON_STACK(name, sym, hash) \
312 _Pragma("GCC diagnostic push") \
313 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
314 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
315 LC_ALIGNED_BUFFER(name##_ctx_buf, \
316 LC_KH_CTX_SIZE(sym, hash), \
317 LC_HASH_COMMON_ALIGNMENT); \
318 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
319 LC_KH_SET_CTX(name, sym, hash); \
320 lc_aead_zero(name); \
321 _Pragma("GCC diagnostic pop")
322
323#ifdef __cplusplus
324}
325#endif
326
327#endif /* LC_SYMKMAC_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_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.