Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_aes_gcm.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_AES_GCM_H
21#define LC_AES_GCM_H
22
23#include "lc_aead.h"
24#include "lc_aes.h"
25#include "lc_sym.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32struct lc_gcm_ctx {
33 uint64_t len; // cipher data length processed so far
34 uint64_t aad_len; // total add data length
35 uint64_t HL[16]; // precalculated lo-half HTable
36 uint64_t HH[16]; // precalculated hi-half HTable
37
38 /*
39 * Htable has 32 uint64_t variables at its disposal. The caller
40 * uses the HL[0] as start point and the function below can consume
41 * HL[] and HH[] from the variable definitions above.
42 */
43 void (*gcm_gmult_accel)(uint64_t Xi[2], const uint64_t *Htable);
44
45 /* y and buf must be aligned to 64 bits due to accel */
46 uint8_t y[16]; // the current cipher-input IV|Counter value
47 uint8_t buf[16]; // buf working value
48
49 uint8_t base_ectr[16]; // first counter-mode cipher output for tag
50 uint8_t ectr[16]; // CTR ciphertext
51
52 uint8_t rem_aad_inserted : 1; // Was remaining AAD inserted?
53};
54
55struct lc_aes_gcm_cryptor {
56 struct lc_gcm_ctx gcm_ctx;
57 struct lc_sym_ctx sym_ctx;
58};
59
60#define LC_AES_GCM_CTX_COMMON_SIZE \
61 (sizeof(struct lc_aead) + sizeof(struct lc_aes_gcm_cryptor))
62
63#define LC_AES_GCM_STATE_SIZE \
64 (LC_AES_GCM_CTX_SIZE_LEN(LC_AES_AESNI_MAX_BLOCK_SIZE))
65#define LC_AES_GCM_CTX_SIZE (LC_AES_GCM_CTX_COMMON_SIZE + LC_AES_GCM_STATE_SIZE)
66
67#define LC_AES_GCM_STATE_SIZE_LEN(len) (LC_SYM_STATE_SIZE_LEN(len))
68#define LC_AES_GCM_CTX_SIZE_LEN(len) \
69 (LC_AES_GCM_CTX_COMMON_SIZE + LC_AES_GCM_STATE_SIZE_LEN(len))
70
71/* AES-GCM AEAD-algorithm */
72extern const struct lc_aead *lc_aes_gcm_aead;
73
74#define _LC_AES_GCM_SET_CTX(name) \
75 _LC_SYM_SET_CTX((&name->sym_ctx), lc_aes, name, \
76 (sizeof(struct lc_aes_gcm_cryptor)))
77
78#define LC_AES_GCM_SET_CTX(name) \
79 LC_AEAD_CTX(name, lc_aes_gcm_aead); \
80 _LC_AES_GCM_SET_CTX(((struct lc_aes_gcm_cryptor *)name->aead_state))
82
90int lc_aes_gcm_alloc(struct lc_aead_ctx **ctx);
91
94};
95
119int lc_aes_gcm_generate_iv(struct lc_aead_ctx *ctx, const uint8_t *fixed_field,
120 size_t fixed_field_len, uint8_t *iv, size_t ivlen,
121 enum lc_aes_gcm_iv_type type);
122
128#define LC_AES_GCM_CTX_ON_STACK(name) \
129 _Pragma("GCC diagnostic push") _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:92
@ lc_aes_gcm_iv_generate_new
Definition: lc_aes_gcm.h:93
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.