Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_aes.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 * This code is derived in parts from the code distribution provided with
21 * https://github.com/kokke/tiny-AES-c
22 *
23 * This is free and unencumbered software released into the public domain.
24 */
25
26#ifndef LC_AES_H
27#define LC_AES_H
28
29#include "lc_sym.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/* AES ECB mode */
36extern const struct lc_sym *lc_aes_ecb;
37
38/* AES CBC mode */
39extern const struct lc_sym *lc_aes_cbc;
40
41/* AES CTR mode */
42extern const struct lc_sym *lc_aes_ctr;
43
44/* AES KW mode */
45extern const struct lc_sym *lc_aes_kw;
46
47/* AES raw block operation */
48extern const struct lc_sym *lc_aes;
49
50/* AES XTS mode */
51extern const struct lc_sym *lc_aes_xts;
52
72void lc_aes_kw_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
73 size_t len);
74
95int lc_aes_kw_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
96 size_t len);
97
99/*
100 * Maximum size of the AES context - sizeof(struct lc_sym_state)
101 *
102 * Note, there is a separate lc_sym_state per block chaining mode in
103 * aes_*_*.c
104 */
105#define LC_AES_C_INTERNAL_SIZE (248)
106#define LC_AES_RISCV64_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2)
107#define LC_AES_RISCV64_XTS_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 3 + 48)
108#define LC_AES_RISCV64_CBC_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2 + 48)
109#define LC_AES_RISCV64_CTR_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE + 48)
110
111#define LC_AES_C_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE)
112#define LC_AES_C_XTS_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2 + 48)
113#define LC_AES_C_CBC_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE + 48)
114#define LC_AES_C_CTR_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE + 48)
115
116#define LC_AES_ARMCE_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2)
117#define LC_AES_ARMCE_XTS_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 3 + 16 + 1)
118#define LC_AES_ARMCE_CBC_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2 + 16)
119#define LC_AES_ARMCE_CTR_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE + 16)
120
121#define LC_AES_AESNI_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2)
122#define LC_AES_AESNI_XTS_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 3 + 16 + 1)
123#define LC_AES_AESNI_CBC_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE * 2 + 16)
124#define LC_AES_AESNI_CTR_MAX_BLOCK_SIZE (LC_AES_C_INTERNAL_SIZE + 16)
126
133#define LC_AES_XTS_CTX_ON_STACK(name) \
134 _Pragma("GCC diagnostic push") _Pragma( \
135 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
136 LC_ALIGNED_BUFFER(name##_ctx_buf, \
137 LC_SYM_CTX_SIZE_LEN( \
138 LC_AES_RISCV64_XTS_MAX_BLOCK_SIZE), \
139 LC_SYM_COMMON_ALIGNMENT); \
140 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
141 LC_SYM_SET_CTX(name, lc_aes_xts); \
142 lc_sym_zero(name); \
143 _Pragma("GCC diagnostic pop")
144
151#define LC_AES_CBC_CTX_ON_STACK(name) \
152 _Pragma("GCC diagnostic push") _Pragma( \
153 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
154 LC_ALIGNED_BUFFER(name##_ctx_buf, \
155 LC_SYM_CTX_SIZE_LEN( \
156 LC_AES_RISCV64_CBC_MAX_BLOCK_SIZE), \
157 LC_SYM_COMMON_ALIGNMENT); \
158 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
159 LC_SYM_SET_CTX(name, lc_aes_cbc); \
160 lc_sym_zero(name); \
161 _Pragma("GCC diagnostic pop")
162
169#define LC_AES_CTR_CTX_ON_STACK(name) \
170 _Pragma("GCC diagnostic push") _Pragma( \
171 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
172 LC_ALIGNED_BUFFER(name##_ctx_buf, \
173 LC_SYM_CTX_SIZE_LEN( \
174 LC_AES_RISCV64_CTR_MAX_BLOCK_SIZE), \
175 LC_SYM_COMMON_ALIGNMENT); \
176 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
177 LC_SYM_SET_CTX(name, lc_aes_ctr); \
178 lc_sym_zero(name); \
179 _Pragma("GCC diagnostic pop")
180
187#define LC_AES_CTX_ON_STACK(name) \
188 _Pragma("GCC diagnostic push") _Pragma( \
189 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
190 LC_ALIGNED_BUFFER( \
191 name##_ctx_buf, \
192 LC_SYM_CTX_SIZE_LEN(LC_AES_AESNI_MAX_BLOCK_SIZE), \
193 LC_SYM_COMMON_ALIGNMENT); \
194 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
195 LC_SYM_SET_CTX(name, lc_aes); \
196 lc_sym_zero(name); \
197 _Pragma("GCC diagnostic pop")
198
199#ifdef __cplusplus
200}
201#endif
202
203#endif /* LC_AES_H */
void lc_aes_kw_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
AES KW encrypt.
int lc_aes_kw_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
AES KW decrypt.
const struct lc_sym * lc_aes_cbc
const struct lc_sym * lc_aes_ecb
const struct lc_sym * lc_aes_kw
const struct lc_sym * lc_aes_ctr
const struct lc_sym * lc_aes
const struct lc_sym * lc_aes_xts