Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_sym.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 - 2026, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see COPYING 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_SYM_H
21#define _LC_SYM_H
22
23#include "ext_headers.h"
24#include "lc_memset_secure.h"
25#include "lc_memory_support.h"
26#include "lc_status.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33struct lc_sym_state;
34struct lc_sym {
35 int (*init)(struct lc_sym_state *ctx);
36 int (*init_nocheck)(struct lc_sym_state *ctx);
37 int (*setkey)(struct lc_sym_state *ctx, const uint8_t *key,
38 size_t keylen);
39 int (*setiv)(struct lc_sym_state *ctx, const uint8_t *iv, size_t ivlen);
40 int (*getiv)(const struct lc_sym_state *ctx, uint8_t *iv, size_t ivlen);
41 void (*encrypt)(struct lc_sym_state *ctx, const uint8_t *in,
42 uint8_t *out, size_t len);
43 void (*decrypt)(struct lc_sym_state *ctx, const uint8_t *in,
44 uint8_t *out, size_t len);
45 uint64_t algorithm_type;
46 unsigned int statesize;
47 unsigned int blocksize;
48};
49
50struct lc_sym_ctx {
51 const struct lc_sym *sym;
52 struct lc_sym_state *sym_state;
53};
54
55/*
56 * Align the lc_sym_state structure to 16 bytes boundary irrespective where
57 * it is embedded into. This is achieved by adding 15 more bytes than necessary
58 * to LC_ALIGNED_SYM_BUFFER and then adjusting the pointer offset in that range
59 * accordingly.
60 *
61 * TODO: make this adjustable with a lc_sym->alignment setting - but the
62 * question is which pre-processor macro to use to select the proper
63 * LC_ALIGN_PTR_XX macro depending on lc_sym->alignment during compile time.
64 */
65#ifndef LC_SYM_COMMON_ALIGNMENT
66#define LC_SYM_COMMON_ALIGNMENT (16)
67#endif
68#define LC_SYM_ALIGNMENT(symname) LC_SYM_COMMON_ALIGNMENT
69#define LC_SYM_ALIGNMASK(symname) (LC_SYM_ALIGNMENT(symname) - 1)
70
71#define LC_ALIGN_SYM_MASK(p, symname) \
72 LC_ALIGN_PTR_64(p, LC_SYM_ALIGNMASK(symname))
73
74/*
75 * This is the source of the compiler warning of using Variable-Length-Arrays
76 * (VLA). It is considered to be harmless to have this VLA here, because all
77 * length definitions are defined by leancrypto at compile time. If you do not
78 * want it, you have the following options:
79 *
80 * 1. Use the type-specific LC_AES_*_CTX_ON_STACK
81 * 2. Do not use stack-allocation function.
82 * 3. Ignore the warning by using
83 * #pragma GCC diagnostic push
84 * #pragma GCC diagnostic ignored "-Wvla"
85 * LC_HASH_CTX_ON_STACK()
86 * #pragma GCC diagnostic pop
87 */
88#define LC_SYM_STATE_SIZE_NONALIGNED(x) ((unsigned long)(x->statesize))
89#define LC_SYM_STATE_SIZE(x) \
90 (LC_SYM_STATE_SIZE_NONALIGNED(x) + LC_SYM_COMMON_ALIGNMENT)
91#define LC_SYM_CTX_SIZE_NONALIGNED(x) \
92 (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE_NONALIGNED(x))
93#define LC_SYM_CTX_SIZE(x) (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE(x))
94
95#define LC_SYM_STATE_SIZE_LEN(len) ((len) + LC_SYM_COMMON_ALIGNMENT)
96#define LC_SYM_CTX_SIZE_LEN(len) \
97 (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE_LEN(len))
98
99#define _LC_SYM_SET_CTX(name, symname, ctx, offset) \
100 name->sym_state = (struct lc_sym_state *)LC_ALIGN_SYM_MASK( \
101 ((uint8_t *)(ctx)) + (offset), symname); \
102 name->sym = symname
103
104#define LC_SYM_SET_CTX(name, symname) \
105 _LC_SYM_SET_CTX(name, symname, name, sizeof(struct lc_sym_ctx))
107
131int lc_sym_init(struct lc_sym_ctx *ctx);
132
148int lc_sym_setkey(struct lc_sym_ctx *ctx, const uint8_t *key, size_t keylen);
149
165int lc_sym_setiv(struct lc_sym_ctx *ctx, const uint8_t *iv, size_t ivlen);
166
178int lc_sym_getiv(const struct lc_sym_ctx *ctx, uint8_t *iv, size_t ivlen);
179
200void lc_sym_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
201 size_t len);
202
223void lc_sym_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
224 size_t len);
225
233void lc_sym_zero(struct lc_sym_ctx *ctx);
234
244int lc_sym_alloc(const struct lc_sym *sym, struct lc_sym_ctx **ctx);
245
253void lc_sym_zero_free(struct lc_sym_ctx *ctx);
254
263enum lc_alg_status_val lc_sym_alg_status(const struct lc_sym *sym);
264
273enum lc_alg_status_val lc_sym_ctx_alg_status(const struct lc_sym_ctx *ctx);
274
283#define LC_SYM_CTX_ON_STACK(name, symname) \
284 _Pragma("GCC diagnostic push") \
285 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
286 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
287 LC_ALIGNED_BUFFER(name##_ctx_buf, \
288 LC_SYM_CTX_SIZE(symname), \
289 LC_SYM_COMMON_ALIGNMENT); \
290 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
291 LC_SYM_SET_CTX(name, symname); \
292 lc_sym_zero(name); \
293 _Pragma("GCC diagnostic pop")
294
295#ifdef __cplusplus
296}
297#endif
298
299#endif /* _LC_SYM_H */
void lc_sym_zero(struct lc_sym_ctx *ctx)
Zeroize symmetric context allocated with either LC_SYM_CTX_ON_STACK or lc_sym_alloc.
void lc_sym_zero_free(struct lc_sym_ctx *ctx)
Symmetric algorithm deallocation and properly zeroization function to frees all buffers and the ciphe...
int lc_sym_init(struct lc_sym_ctx *ctx)
Initialize symmetric context.
enum lc_alg_status_val lc_sym_ctx_alg_status(const struct lc_sym_ctx *ctx)
Obtain algorithm status.
int lc_sym_setkey(struct lc_sym_ctx *ctx, const uint8_t *key, size_t keylen)
Set key.
int lc_sym_getiv(const struct lc_sym_ctx *ctx, uint8_t *iv, size_t ivlen)
Get IV.
int lc_sym_alloc(const struct lc_sym *sym, struct lc_sym_ctx **ctx)
Allocate symmetric algorithm context on heap.
enum lc_alg_status_val lc_sym_alg_status(const struct lc_sym *sym)
Obtain algorithm status.
void lc_sym_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
Symmetric decryption.
void lc_sym_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
Symmetric encryption.
int lc_sym_setiv(struct lc_sym_ctx *ctx, const uint8_t *iv, size_t ivlen)
Set IV.
lc_alg_status_val
Definition: lc_status.h:63