23#include "ext_headers.h"
34 void (*init)(
struct lc_sym_state *ctx);
35 int (*setkey)(
struct lc_sym_state *ctx,
const uint8_t *key,
37 int (*setiv)(
struct lc_sym_state *ctx,
const uint8_t *iv,
size_t ivlen);
38 void (*encrypt)(
struct lc_sym_state *ctx,
const uint8_t *in,
39 uint8_t *out,
size_t len);
40 void (*decrypt)(
struct lc_sym_state *ctx,
const uint8_t *in,
41 uint8_t *out,
size_t len);
42 unsigned int statesize;
43 unsigned int blocksize;
47 const struct lc_sym *sym;
48 struct lc_sym_state *sym_state;
61#ifndef LC_SYM_ALIGNMENT_COMMON
62#define LC_SYM_ALIGNMENT_COMMON (8)
64#define LC_SYM_ALIGNMENT(symname) LC_SYM_ALIGNMENT_COMMON
65#define LC_SYM_ALIGNMASK(symname) (LC_SYM_ALIGNMENT(symname) - 1)
67#define LC_ALIGN_SYM_MASK(p, symname) \
68 LC_ALIGN_PTR_64(p, LC_SYM_ALIGNMASK(symname))
70#define LC_SYM_STATE_SIZE_NONALIGNED(x) ((unsigned long)(x->statesize))
71#define LC_SYM_STATE_SIZE(x) \
72 (LC_SYM_STATE_SIZE_NONALIGNED(x) + LC_SYM_ALIGNMENT_COMMON)
73#define LC_SYM_CTX_SIZE_NONALIGNED(x) \
74 (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE_NONALIGNED(x))
75#define LC_SYM_CTX_SIZE(x) (sizeof(struct lc_sym_ctx) + LC_SYM_STATE_SIZE(x))
82#define LC_ALIGNED_SYM_BUFFER(name, symname, size) \
83 uint64_t name[(size + sizeof(uint64_t) - 1) / sizeof(uint64_t)] \
84 __attribute__((aligned(LC_SYM_ALIGNMENT(symname))))
86#define _LC_SYM_SET_CTX(name, symname, ctx, offset) \
87 name->sym_state = (struct lc_sym_state *)LC_ALIGN_SYM_MASK( \
88 ((uint8_t *)(ctx)) + (offset), symname); \
91#define LC_SYM_SET_CTX(name, symname) \
92 _LC_SYM_SET_CTX(name, symname, name, sizeof(struct lc_sym_ctx))
120 const struct lc_sym *sym = ctx->sym;
122 sym->init(ctx->sym_state);
139 const struct lc_sym *sym = ctx->sym;
141 return sym->setkey(ctx->sym_state, key, keylen);
155static inline int lc_sym_setiv(
struct lc_sym_ctx *ctx,
const uint8_t *iv,
158 const struct lc_sym *sym = ctx->sym;
160 return sym->setiv(ctx->sym_state, iv, ivlen);
177 uint8_t *out,
size_t len)
179 const struct lc_sym *sym = ctx->sym;
181 sym->encrypt(ctx->sym_state, in, out, len);
198 uint8_t *out,
size_t len)
200 const struct lc_sym *sym = ctx->sym;
202 sym->decrypt(ctx->sym_state, in, out, len);
214 const struct lc_sym *sym = ctx->sym;
217 LC_SYM_STATE_SIZE_NONALIGNED(sym));
248#define LC_SYM_CTX_ON_STACK(name, symname) \
249 _Pragma("GCC diagnostic push") \
250 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
251 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
252 LC_ALIGNED_SYM_BUFFER( \
253 name##_ctx_buf, symname, \
254 LC_SYM_CTX_SIZE_NONALIGNED(symname)); \
255 struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
256 LC_SYM_SET_CTX(name, symname); \
258 _Pragma("GCC diagnostic pop")
static void lc_sym_decrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
Symmetric decryption.
static void lc_sym_init(struct lc_sym_ctx *ctx)
Initialize symmetric context.
static void lc_sym_encrypt(struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
Symmetric encryption.
void lc_sym_zero_free(struct lc_sym_ctx *ctx)
Symmetric algorithm deallocation and properly zeroization function to frees all buffers and the ciphe...
static int lc_sym_setiv(struct lc_sym_ctx *ctx, const uint8_t *iv, size_t ivlen)
Set IV.
static 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.
static void lc_sym_zero(struct lc_sym_ctx *ctx)
Zeroize symmetric context allocated with either LC_SYM_CTX_ON_STACK or lc_sym_alloc.
static void lc_memset_secure(void *s, int c, size_t n)