Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_sym.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 - 2025, 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
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32struct lc_sym_state;
33struct lc_sym {
34 int (*init)(struct lc_sym_state *ctx);
35 int (*init_nocheck)(struct lc_sym_state *ctx);
36 int (*setkey)(struct lc_sym_state *ctx, const uint8_t *key,
37 size_t keylen);
38 int (*setiv)(struct lc_sym_state *ctx, const uint8_t *iv, size_t ivlen);
39 void (*encrypt)(struct lc_sym_state *ctx, const uint8_t *in,
40 uint8_t *out, size_t len);
41 void (*decrypt)(struct lc_sym_state *ctx, const uint8_t *in,
42 uint8_t *out, size_t len);
43 uint64_t algorithm_type;
44 unsigned int statesize;
45 unsigned int blocksize;
46};
47
48struct lc_sym_ctx {
49 const struct lc_sym *sym;
50 struct lc_sym_state *sym_state;
51};
52
53/*
54 * Align the lc_sym_state structure to 16 bytes boundary irrespective where
55 * it is embedded into. This is achieved by adding 15 more bytes than necessary
56 * to LC_ALIGNED_SYM_BUFFER and then adjusting the pointer offset in that range
57 * accordingly.
58 *
59 * TODO: make this adjustable with a lc_sym->alignment setting - but the
60 * question is which pre-processor macro to use to select the proper
61 * LC_ALIGN_PTR_XX macro depending on lc_sym->alignment during compile time.
62 */
63#ifndef LC_SYM_COMMON_ALIGNMENT
64#define LC_SYM_COMMON_ALIGNMENT (16)
65#endif
66#define LC_SYM_ALIGNMENT(symname) LC_SYM_COMMON_ALIGNMENT
67#define LC_SYM_ALIGNMASK(symname) (LC_SYM_ALIGNMENT(symname) - 1)
68
69#define LC_ALIGN_SYM_MASK(p, symname) \
70 LC_ALIGN_PTR_64(p, LC_SYM_ALIGNMASK(symname))
71
72#define LC_SYM_STATE_SIZE_NONALIGNED(x) ((unsigned long)(x->statesize))
73#define LC_SYM_STATE_SIZE(x) \
74 (LC_SYM_STATE_SIZE_NONALIGNED(x) + LC_SYM_COMMON_ALIGNMENT)
75#define LC_SYM_CTX_SIZE_NONALIGNED(x) \
76 (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE_NONALIGNED(x))
77#define LC_SYM_CTX_SIZE(x) (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE(x))
78
79#define LC_SYM_STATE_SIZE_LEN(len) ((len) + LC_SYM_COMMON_ALIGNMENT)
80#define LC_SYM_CTX_SIZE_LEN(len) \
81 (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE_LEN(len))
82
83#define _LC_SYM_SET_CTX(name, symname, ctx, offset) \
84 name->sym_state = (struct lc_sym_state *)LC_ALIGN_SYM_MASK( \
85 ((uint8_t *)(ctx)) + (offset), symname); \
86 name->sym = symname
87
88#define LC_SYM_SET_CTX(name, symname) \
89 _LC_SYM_SET_CTX(name, symname, name, sizeof(struct lc_sym_ctx))
91
105
115int lc_sym_init(struct lc_sym_ctx *ctx);
116
132int lc_sym_setkey(struct lc_sym_ctx *ctx, const uint8_t *key, size_t keylen);
133
149int lc_sym_setiv(struct lc_sym_ctx *ctx, const uint8_t *iv, size_t ivlen);
150
171void lc_sym_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
172 size_t len);
173
194void lc_sym_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out,
195 size_t len);
196
204void lc_sym_zero(struct lc_sym_ctx *ctx);
205
215int lc_sym_alloc(const struct lc_sym *sym, struct lc_sym_ctx **ctx);
216
224void lc_sym_zero_free(struct lc_sym_ctx *ctx);
225
234uint64_t lc_sym_algorithm_type(const struct lc_sym *sym);
235
244uint64_t lc_sym_ctx_algorithm_type(const struct lc_sym_ctx *ctx);
245
254#define LC_SYM_CTX_ON_STACK(name, symname) \
255 _Pragma("GCC diagnostic push") \
256 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
257 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
258 LC_ALIGNED_BUFFER(name##_ctx_buf, \
259 LC_SYM_CTX_SIZE(symname), \
260 LC_SYM_COMMON_ALIGNMENT); \
261 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
262 LC_SYM_SET_CTX(name, symname); \
263 lc_sym_zero(name); \
264 _Pragma("GCC diagnostic pop")
265
266#ifdef __cplusplus
267}
268#endif
269
270#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.
uint64_t lc_sym_algorithm_type(const struct lc_sym *sym)
Obtain algorithm type usable with lc_alg_status.
int lc_sym_setkey(struct lc_sym_ctx *ctx, const uint8_t *key, size_t keylen)
Set key.
int lc_sym_alloc(const struct lc_sym *sym, struct lc_sym_ctx **ctx)
Allocate symmetric algorithm context on heap.
uint64_t lc_sym_ctx_algorithm_type(const struct lc_sym_ctx *ctx)
Obtain algorithm type usable with lc_alg_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.