23#include "ext_headers.h"
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,
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;
49 const struct lc_sym *sym;
50 struct lc_sym_state *sym_state;
63#ifndef LC_SYM_COMMON_ALIGNMENT
64#define LC_SYM_COMMON_ALIGNMENT (16)
66#define LC_SYM_ALIGNMENT(symname) LC_SYM_COMMON_ALIGNMENT
67#define LC_SYM_ALIGNMASK(symname) (LC_SYM_ALIGNMENT(symname) - 1)
69#define LC_ALIGN_SYM_MASK(p, symname) \
70 LC_ALIGN_PTR_64(p, LC_SYM_ALIGNMASK(symname))
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))
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))
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); \
88#define LC_SYM_SET_CTX(name, symname) \
89 _LC_SYM_SET_CTX(name, symname, name, sizeof(struct lc_sym_ctx))
132int lc_sym_setkey(
struct lc_sym_ctx *ctx,
const uint8_t *key,
size_t keylen);
149int lc_sym_setiv(
struct lc_sym_ctx *ctx,
const uint8_t *iv,
size_t ivlen);
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); \
264 _Pragma("GCC diagnostic pop")
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.