23#include "ext_headers.h"
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,
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;
51 const struct lc_sym *sym;
52 struct lc_sym_state *sym_state;
65#ifndef LC_SYM_COMMON_ALIGNMENT
66#define LC_SYM_COMMON_ALIGNMENT (16)
68#define LC_SYM_ALIGNMENT(symname) LC_SYM_COMMON_ALIGNMENT
69#define LC_SYM_ALIGNMASK(symname) (LC_SYM_ALIGNMENT(symname) - 1)
71#define LC_ALIGN_SYM_MASK(p, symname) \
72 LC_ALIGN_PTR_64(p, LC_SYM_ALIGNMASK(symname))
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))
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))
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); \
104#define LC_SYM_SET_CTX(name, symname) \
105 _LC_SYM_SET_CTX(name, symname, name, sizeof(struct lc_sym_ctx))
148int lc_sym_setkey(
struct lc_sym_ctx *ctx,
const uint8_t *key,
size_t keylen);
165int lc_sym_setiv(
struct lc_sym_ctx *ctx,
const uint8_t *iv,
size_t ivlen);
178int lc_sym_getiv(
const struct lc_sym_ctx *ctx, uint8_t *iv,
size_t ivlen);
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); \
293 _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.
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.