Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_aes_gcm.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_AES_GCM_H
21#define LC_AES_GCM_H
22
23#include "lc_aead.h"
24#include "lc_sym.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
31struct lc_gcm_ctx {
32 uint64_t len; // cipher data length processed so far
33 uint64_t aad_len; // total add data length
34 uint64_t HL[16]; // precalculated lo-half HTable
35 uint64_t HH[16]; // precalculated hi-half HTable
36
37 /*
38 * Htable has 32 uint64_t variables at its disposal. The caller
39 * uses the HL[0] as start point and the function below can consume
40 * HL[] and HH[] from the variable definitions above.
41 */
42 void (*gcm_gmult_accel)(uint64_t Xi[2], const uint64_t *Htable);
43
44 /* y and buf must be aligned to 64 bits due to accel */
45 uint8_t y[16]; // the current cipher-input IV|Counter value
46 uint8_t buf[16]; // buf working value
47
48 uint8_t base_ectr[16]; // first counter-mode cipher output for tag
49 uint8_t ectr[16]; // CTR ciphertext
50
51 uint8_t rem_aad_inserted : 1; // Was remaining AAD inserted?
52};
53
54struct lc_aes_gcm_cryptor {
55 struct lc_gcm_ctx gcm_ctx;
56 struct lc_sym_ctx sym_ctx;
57};
58
59#define LC_AES_GCM_CTX_COMMON_SIZE \
60 (sizeof(struct lc_aead) + sizeof(struct lc_aes_gcm_cryptor))
61
62#define LC_AES_GCM_STATE_SIZE(x) (LC_SYM_STATE_SIZE(x))
63#define LC_AES_GCM_CTX_SIZE \
64 (LC_AES_GCM_CTX_COMMON_SIZE + LC_AES_GCM_STATE_SIZE(lc_aes))
65
66#define LC_AES_GCM_STATE_SIZE_LEN(len) (LC_SYM_STATE_SIZE_LEN(len))
67#define LC_AES_GCM_CTX_SIZE_LEN(len) \
68 (LC_AES_GCM_CTX_COMMON_SIZE + LC_AES_GCM_STATE_SIZE_LEN(len))
69
70/* AES-CBC with HMAC based AEAD-algorithm */
71extern const struct lc_aead *lc_aes_gcm_aead;
72
73#define _LC_AES_GCM_SET_CTX(name) \
74 _LC_SYM_SET_CTX((&name->sym_ctx), lc_aes, name, \
75 (sizeof(struct lc_aes_gcm_cryptor)))
76
77#define LC_AES_GCM_SET_CTX(name) \
78 LC_AEAD_CTX(name, lc_aes_gcm_aead); \
79 _LC_AES_GCM_SET_CTX(((struct lc_aes_gcm_cryptor *)name->aead_state))
81
89int lc_aes_gcm_alloc(struct lc_aead_ctx **ctx);
90
94
118int lc_aes_gcm_generate_iv(struct lc_aead_ctx *ctx, const uint8_t *fixed_field,
119 size_t fixed_field_len, uint8_t *iv, size_t ivlen,
120 enum lc_aes_gcm_iv_type type);
121
127#define LC_AES_GCM_CTX_ON_STACK(name) \
128 _Pragma("GCC diagnostic push") \
129 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
130 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
131 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_AES_GCM_CTX_SIZE, \
132 LC_MEM_COMMON_ALIGNMENT); \
133 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
134 LC_AES_GCM_SET_CTX(name); \
135 lc_aead_zero(name); \
136 _Pragma("GCC diagnostic pop")
137
138#ifdef __cplusplus
139}
140#endif
141
142#endif /* LC_AES_GCM_H */
lc_aes_gcm_iv_type
Definition lc_aes_gcm.h:91
@ lc_aes_gcm_iv_generate_new
Definition lc_aes_gcm.h:92
int lc_aes_gcm_generate_iv(struct lc_aead_ctx *ctx, const uint8_t *fixed_field, size_t fixed_field_len, uint8_t *iv, size_t ivlen, enum lc_aes_gcm_iv_type type)
Generate IV, set the IV to the GCM state and return it to the caller.
int lc_aes_gcm_alloc(struct lc_aead_ctx **ctx)
Allocate AES GCM cryptor context on heap.