Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_chacha20_poly1305.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2025, 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_CHACHA20_POLY1305_H
21#define LC_CHACHA20_POLY1305_H
22
23#include "lc_aead.h"
24#include "lc_chacha20.h"
25#include "lc_memset_secure.h"
26#include "lc_poly1305.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33struct lc_chacha20_poly1305_cryptor {
34 struct lc_sym_ctx chacha20;
35 struct lc_poly1305_context poly1305_ctx;
36 size_t datalen;
37 size_t aadlen;
38};
39
40#define LC_CHACHA20_POLY1305_STATE_SIZE (LC_SYM_STATE_SIZE(lc_chacha20))
41#define LC_CHACHA20_POLY1305_CTX_SIZE \
42 (sizeof(struct lc_aead) + \
43 sizeof(struct lc_chacha20_poly1305_cryptor) + \
44 LC_CHACHA20_POLY1305_STATE_SIZE)
45
46/* AES-CBC with HMAC based AEAD-algorithm */
47extern const struct lc_aead *lc_chacha20_poly1305_aead;
48
49#define _LC_CHACHA20_POLY1305_SET_CTX(name) \
50 _LC_SYM_SET_CTX((&name->chacha20), lc_chacha20, name, \
51 (sizeof(struct lc_chacha20_poly1305_cryptor))); \
52 (name)->datalen = 0; \
53 (name)->aadlen = 0
54
55#define LC_CHACHA20_POLY1305_SET_CTX(name) \
56 LC_AEAD_CTX(name, lc_chacha20_poly1305_aead); \
57 _LC_CHACHA20_POLY1305_SET_CTX( \
58 ((struct lc_chacha20_poly1305_cryptor *)name->aead_state))
60
68int lc_chacha20_poly1305_alloc(struct lc_aead_ctx **ctx);
69
75#define LC_CHACHA20_POLY1305_CTX_ON_STACK(name) \
76 _Pragma("GCC diagnostic push") \
77 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
78 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
79 LC_ALIGNED_BUFFER(name##_ctx_buf, \
80 LC_CHACHA20_POLY1305_CTX_SIZE, \
81 LC_MEM_COMMON_ALIGNMENT); \
82 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
83 LC_CHACHA20_POLY1305_SET_CTX(name); \
84 lc_aead_zero(name); \
85 _Pragma("GCC diagnostic pop")
86
87#ifdef __cplusplus
88}
89#endif
90
91#endif /* LC_CHACHA20_POLY1305_H */
int lc_chacha20_poly1305_alloc(struct lc_aead_ctx **ctx)
Allocate ChaCha20 Poly1305 cryptor context on heap.