Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_symhmac.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_SYMHMAC_H
21#define LC_SYMHMAC_H
22
23#include "lc_aead.h"
24#include "lc_sym.h"
25#include "lc_hmac.h"
26#include "lc_memset_secure.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33struct lc_sh_cryptor {
34 struct lc_sym_ctx sym;
35 struct lc_hmac_ctx auth_ctx;
36};
37
38#define LC_SH_STATE_SIZE(sym, hash) \
39 (LC_SYM_STATE_SIZE(sym) + LC_HMAC_STATE_SIZE(hash))
40#define LC_SH_CTX_SIZE(sym, hash) \
41 (sizeof(struct lc_aead) + sizeof(struct lc_sh_cryptor) + \
42 LC_SH_STATE_SIZE(sym, hash))
43
44/* AES-CBC with HMAC based AEAD-algorithm */
45extern const struct lc_aead *lc_symhmac_aead;
46
47#define _LC_SH_SET_CTX(name, symalgo, hash) \
48 _LC_SYM_SET_CTX((&name->sym), symalgo, name, \
49 (sizeof(struct lc_sh_cryptor))); \
50 _LC_HMAC_SET_CTX( \
51 (&name->auth_ctx), hash, name, \
52 (sizeof(struct lc_sh_cryptor) + LC_SYM_STATE_SIZE(symalgo)))
53
54#define LC_SH_SET_CTX(name, sym, hash) \
55 LC_AEAD_CTX(name, lc_symhmac_aead); \
56 _LC_SH_SET_CTX(((struct lc_sh_cryptor *)name->aead_state), sym, hash)
58
310int lc_sh_alloc(const struct lc_sym *sym, const struct lc_hash *hash,
311 struct lc_aead_ctx **ctx);
312
322#define LC_SH_CTX_ON_STACK(name, sym, hash) \
323 _Pragma("GCC diagnostic push") \
324 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
325 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
326 LC_ALIGNED_BUFFER(name##_ctx_buf, \
327 LC_SH_CTX_SIZE(sym, hash), \
328 LC_HASH_COMMON_ALIGNMENT); \
329 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
330 LC_SH_SET_CTX(name, sym, hash); \
331 lc_aead_zero(name); \
332 _Pragma("GCC diagnostic pop")
333
334#ifdef __cplusplus
335}
336#endif
337
338#endif /* LC_SYMHMAC_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_sh_alloc(const struct lc_sym *sym, const struct lc_hash *hash, struct lc_aead_ctx **ctx)
Allocate symmetric algorithm with HMAC cryptor context on heap.