Leancrypto 1.7.2
Post-Quantum Cryptographic Library
Macros | Functions
Symmetric Unauthenticated Encryption Algorithms

Macros

#define LC_AES_XTS_CTX_ON_STACK(name)
 Allocate stack memory for the AES-XTS context. More...
 
#define LC_AES_CBC_CTX_ON_STACK(name)
 Allocate stack memory for the AES-CBC context. More...
 
#define LC_AES_CTR_CTX_ON_STACK(name)
 Allocate stack memory for the AES-CTR context. More...
 
#define LC_AES_CTX_ON_STACK(name)
 Allocate stack memory for the AES block context. More...
 
#define LC_CC20_CTX_ON_STACK(name)
 Allocate stack memory for the ChaCha20 context. More...
 
#define LC_SYM_CTX_ON_STACK(name, symname)
 Allocate stack memory for the sym context. More...
 

Functions

void lc_aes_kw_encrypt (struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
 AES KW encrypt. More...
 
int lc_aes_kw_decrypt (struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
 AES KW decrypt. More...
 
void cc20_block (struct lc_sym_state *state, uint32_t *stream)
 ChaCha20 block function. More...
 
int lc_sym_init (struct lc_sym_ctx *ctx)
 Initialize symmetric context. More...
 
int lc_sym_setkey (struct lc_sym_ctx *ctx, const uint8_t *key, size_t keylen)
 Set key. More...
 
int lc_sym_setiv (struct lc_sym_ctx *ctx, const uint8_t *iv, size_t ivlen)
 Set IV. More...
 
int lc_sym_getiv (const struct lc_sym_ctx *ctx, uint8_t *iv, size_t ivlen)
 Get IV. More...
 
void lc_sym_encrypt (struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
 Symmetric encryption. More...
 
void lc_sym_decrypt (struct lc_sym_ctx *ctx, const uint8_t *in, uint8_t *out, size_t len)
 Symmetric decryption. More...
 
void lc_sym_zero (struct lc_sym_ctx *ctx)
 Zeroize symmetric context allocated with either LC_SYM_CTX_ON_STACK or lc_sym_alloc. More...
 
int lc_sym_alloc (const struct lc_sym *sym, struct lc_sym_ctx **ctx)
 Allocate symmetric algorithm context on heap. More...
 
void lc_sym_zero_free (struct lc_sym_ctx *ctx)
 Symmetric algorithm deallocation and properly zeroization function to frees all buffers and the cipher handle. More...
 
enum lc_alg_status_val lc_sym_alg_status (const struct lc_sym *sym)
 Obtain algorithm status. More...
 
enum lc_alg_status_val lc_sym_ctx_alg_status (const struct lc_sym_ctx *ctx)
 Obtain algorithm status. More...
 

Detailed Description

Concept of symmetric algorithms in leancrypto

All symmetric can be used with the API calls documented below. However, the allocation part is symmetric-algorithm-specific. Thus, perform the following steps

  1. Allocation: Use the stack or heap allocation functions documented in lc_aes.h, lc_chacha20.h.
  2. Use the returned cipher handle with the API calls below.

Macro Definition Documentation

◆ LC_AES_CBC_CTX_ON_STACK

#define LC_AES_CBC_CTX_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, \
LC_SYM_CTX_SIZE_LEN( \
LC_AES_RISCV64_CBC_MAX_BLOCK_SIZE), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, lc_aes_cbc); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")
#define LC_ALIGNED_BUFFER(name, size, alignment)
Allocate aligned stack memory.

Allocate stack memory for the AES-CBC context.

Parameters
[in]nameName of the stack variable

Definition at line 151 of file lc_aes.h.

◆ LC_AES_CTR_CTX_ON_STACK

#define LC_AES_CTR_CTX_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, \
LC_SYM_CTX_SIZE_LEN( \
LC_AES_RISCV64_CTR_MAX_BLOCK_SIZE), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, lc_aes_ctr); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")

Allocate stack memory for the AES-CTR context.

Parameters
[in]nameName of the stack variable

Definition at line 169 of file lc_aes.h.

◆ LC_AES_CTX_ON_STACK

#define LC_AES_CTX_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
name##_ctx_buf, \
LC_SYM_CTX_SIZE_LEN(LC_AES_AESNI_MAX_BLOCK_SIZE), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, lc_aes); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")

Allocate stack memory for the AES block context.

Parameters
[in]nameName of the stack variable

Definition at line 187 of file lc_aes.h.

◆ LC_AES_XTS_CTX_ON_STACK

#define LC_AES_XTS_CTX_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, \
LC_SYM_CTX_SIZE_LEN( \
LC_AES_RISCV64_XTS_MAX_BLOCK_SIZE), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, lc_aes_xts); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")

Allocate stack memory for the AES-XTS context.

Parameters
[in]nameName of the stack variable

Definition at line 133 of file lc_aes.h.

◆ LC_CC20_CTX_ON_STACK

#define LC_CC20_CTX_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, \
LC_SYM_CTX_SIZE_LEN(LC_CC20_STATE_SIZE), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, lc_chacha20); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")

Allocate stack memory for the ChaCha20 context.

Parameters
[in]nameName of the stack variable

Definition at line 50 of file lc_chacha20.h.

◆ LC_SYM_CTX_ON_STACK

#define LC_SYM_CTX_ON_STACK (   name,
  symname 
)
Value:
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, \
LC_SYM_CTX_SIZE(symname), \
LC_SYM_COMMON_ALIGNMENT); \
struct lc_sym_ctx *name = (struct lc_sym_ctx *)name##_ctx_buf; \
LC_SYM_SET_CTX(name, symname); \
lc_sym_zero(name); \
_Pragma("GCC diagnostic pop")

Allocate stack memory for the sym context.

Parameters
[in]nameName of the stack variable
[in]symnamePointer of type struct sym referencing the sym implementation to be used

Definition at line 283 of file lc_sym.h.

Function Documentation

◆ cc20_block()

void cc20_block ( struct lc_sym_state *  state,
uint32_t *  stream 
)

ChaCha20 block function.

Block operation from the ChaCah20 state

Parameters
[in]stateChaCha20 state from which to derive the block output
[out]streamChaCha20 key stream output

◆ lc_aes_kw_decrypt()

int lc_aes_kw_decrypt ( struct lc_sym_ctx *  ctx,
const uint8_t *  in,
uint8_t *  out,
size_t  len 
)

AES KW decrypt.

Parameters
[in]ctxReference to sym context implementation to be used to perform sym calculation with.
[in]inCiphertext to be decrypted
[out]outPlaintext resulting of the decryption
[in]lenSize of the input / output buffer
Returns
0 on success, -EBADMSG on authentication error

The plaintext and the ciphertext buffer may be identical to support in-place cryptographic operations.

NOTE: The output buffer MAY be 8 bytes smaller than the input buffer.

This function is a helper function. It provides the same operation as lc_sym_decrypt. The difference is that it also performs the authentication.

◆ lc_aes_kw_encrypt()

void lc_aes_kw_encrypt ( struct lc_sym_ctx *  ctx,
const uint8_t *  in,
uint8_t *  out,
size_t  len 
)

AES KW encrypt.

Parameters
[in]ctxReference to sym context implementation to be used to perform sym calculation with.
[in]inPlaintext to be encrypted
[out]outCiphertext resulting of the encryption
[in]lenSize of the input / output buffer

The plaintext and the ciphertext buffer may be identical to support in-place cryptographic operations.

NOTE: The output buffer MUST be 8 bytes larger than the input buffer!

This function is a helper function. It provides the same operation as lc_sym_encrypt. The difference is that it also obtains the tag from the AES KW operation.

◆ lc_sym_alg_status()

enum lc_alg_status_val lc_sym_alg_status ( const struct lc_sym *  sym)

Obtain algorithm status.

Parameters
[in]symAEAD algorithm instance
Returns
algorithm status

◆ lc_sym_alloc()

int lc_sym_alloc ( const struct lc_sym *  sym,
struct lc_sym_ctx **  ctx 
)

Allocate symmetric algorithm context on heap.

Parameters
[in]symSymmetric algorithm implementation of type struct lc_sym
[out]ctxAllocated symmetrc algorithm context
Returns
0 on success, < 0 on error

◆ lc_sym_ctx_alg_status()

enum lc_alg_status_val lc_sym_ctx_alg_status ( const struct lc_sym_ctx *  ctx)

Obtain algorithm status.

Parameters
[in]ctxSymmetric context handle
Returns
algorithm status

◆ lc_sym_decrypt()

void lc_sym_decrypt ( struct lc_sym_ctx *  ctx,
const uint8_t *  in,
uint8_t *  out,
size_t  len 
)

Symmetric decryption.

Parameters
[in]ctxReference to sym context implementation to be used to perform sym calculation with.
[in]inCiphertext to be decrypted
[out]outPlaintext resulting of the decryption
[in]lenSize of the input / output buffer

The plaintext and the ciphertext buffer may be identical to support in-place cryptographic operations.

Note
This call is allowed to be called multiple times to operate in stream mode. However, the caller must enforce the following:
  1. All invocations except for the last data block must contain data that is always a multiple of 16 bytes.
  2. Only the last invocation is allowed to be not a multiple of 16 bytes for algorithms that allow it (e.g. XTS, CTR)

◆ lc_sym_encrypt()

void lc_sym_encrypt ( struct lc_sym_ctx *  ctx,
const uint8_t *  in,
uint8_t *  out,
size_t  len 
)

Symmetric encryption.

Parameters
[in]ctxReference to sym context implementation to be used to perform sym calculation with.
[in]inPlaintext to be encrypted
[out]outCiphertext resulting of the encryption
[in]lenSize of the input / output buffer

The plaintext and the ciphertext buffer may be identical to support in-place cryptographic operations.

Note
This call is allowed to be called multiple times to operate in stream mode. However, the caller must enforce the following:
  1. All invocations except for the last data block must contain data that is always a multiple of 16 bytes.
  2. Only the last invocation is allowed to be not a multiple of 16 bytes for algorithms that allow it (e.g. XTS, CTR)

◆ lc_sym_getiv()

int lc_sym_getiv ( const struct lc_sym_ctx *  ctx,
uint8_t *  iv,
size_t  ivlen 
)

Get IV.

Parameters
[in]ctxReference to sym context implementation to be used to set the IV.
[in]ivbuffer to be filled with IV
[in]ivlenlength of buffer
Returns
0 on success, < 0 on error

◆ lc_sym_init()

int lc_sym_init ( struct lc_sym_ctx *  ctx)

Initialize symmetric context.

Parameters
[in]ctxReference to sym context implementation to be used to perform sym calculation with.

The caller must provide an allocated ctx.

◆ lc_sym_setiv()

int lc_sym_setiv ( struct lc_sym_ctx *  ctx,
const uint8_t *  iv,
size_t  ivlen 
)

Set IV.

Note
For XTS, it is required that this function call MUST come AFTER the lc_sym_setkey invocation as the tweak value is immediately calculated from the IV using the XTS key.
Parameters
[in]ctxReference to sym context implementation to be used to set the IV.
[in]ivIV to be set
[in]ivlenIV length to be set
Returns
0 on success, < 0 on error

◆ lc_sym_setkey()

int lc_sym_setkey ( struct lc_sym_ctx *  ctx,
const uint8_t *  key,
size_t  keylen 
)

Set key.

Note
For XTS, it is required that this function call MUST come BEFORE the lc_sym_setiv invocation as the tweak value is immediately calculated from the IV using the XTS key.
Parameters
[in]ctxReference to sym context implementation to be used to set the key.
[in]keyKey to be set
[in]keylenKey length to be set
Returns
0 on success, < 0 on error

◆ lc_sym_zero()

void lc_sym_zero ( struct lc_sym_ctx *  ctx)

Zeroize symmetric context allocated with either LC_SYM_CTX_ON_STACK or lc_sym_alloc.

Parameters
[in]ctxSymmetric context to be zeroized

◆ lc_sym_zero_free()

void lc_sym_zero_free ( struct lc_sym_ctx *  ctx)

Symmetric algorithm deallocation and properly zeroization function to frees all buffers and the cipher handle.

Parameters
[in]ctxSymmtric algorithm context handle