Embedded SDK
Embedded SDK
Classes | Macros | Enumerations | Functions
lcx_cipher.h File Reference

API for block ciphers. More...

#include "cx_errors.h"
#include "lcx_wrappers.h"
#include "lcx_common.h"
#include "lcx_aes.h"
#include <stddef.h>
Include dependency graph for lcx_cipher.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cipher_key_t
 
struct  cx_cipher_base_t
 
struct  cx_cipher_info_t
 
struct  cx_cipher_context_t
 

Macros

#define MAX_IV_LENGTH   16
 
#define MAX_BLOCK_LENGTH   16
 

Enumerations

enum  cx_cipher_id_t { CX_CIPHER_NONE = 0 , CX_CIPHER_AES_128 , CX_CIPHER_AES_192 , CX_CIPHER_AES_256 }
 

Functions

WARN_UNUSED_RESULT cx_err_t cx_cipher_init (cx_cipher_context_t *ctx)
 Initialize a cipher context as NONE. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_setup (cx_cipher_context_t *ctx, const cx_cipher_id_t type, uint32_t mode)
 Initialize and fill the context structure given the cipher info. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey (cx_cipher_context_t *ctx, const uint8_t *key, uint32_t key_bitlen, uint32_t operation)
 Set the key to use. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv (cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len)
 Set the initialization vector. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_set_padding (cx_cipher_context_t *ctx, uint32_t padding)
 Set the padding type. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_update (cx_cipher_context_t *ctx, const uint8_t *input, size_t in_len, uint8_t *output, size_t *out_len)
 Encrypt or decrypt with the given context. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_finish (cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len)
 Finalize the operation. More...
 
WARN_UNUSED_RESULT cx_err_t cx_cipher_enc_dec (cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len, const uint8_t *input, size_t in_len, uint8_t *output, size_t *out_len)
 All-in-one encryption or decryption. More...
 
void cx_cipher_reset (cx_cipher_context_t *ctx)
 
void add_one_and_zeros_padding (uint8_t *output, size_t out_len, size_t data_len)
 

Detailed Description

API for block ciphers.

This file contains the functions which enable to use standard block ciphers like AES for encryption and decryption in a specific mode of operation.

Definition in file lcx_cipher.h.

Macro Definition Documentation

◆ MAX_BLOCK_LENGTH

#define MAX_BLOCK_LENGTH   16

Maximum block size in bytes

Definition at line 21 of file lcx_cipher.h.

◆ MAX_IV_LENGTH

#define MAX_IV_LENGTH   16

Maximum length of the initialization vector in bytes

Definition at line 19 of file lcx_cipher.h.

Enumeration Type Documentation

◆ cx_cipher_id_t

Supported cipher identifiers

Enumerator
CX_CIPHER_NONE 

No cipher.

CX_CIPHER_AES_128 

AES with a 128-bit key.

CX_CIPHER_AES_192 

AES with a 192-bit key.

CX_CIPHER_AES_256 

AES with a 256-bit key.

Definition at line 36 of file lcx_cipher.h.

Function Documentation

◆ add_one_and_zeros_padding()

void add_one_and_zeros_padding ( uint8_t output,
size_t  out_len,
size_t  data_len 
)

Definition at line 79 of file cx_cipher.c.

◆ cx_cipher_enc_dec()

WARN_UNUSED_RESULT cx_err_t cx_cipher_enc_dec ( cx_cipher_context_t ctx,
const uint8_t iv,
size_t  iv_len,
const uint8_t input,
size_t  in_len,
uint8_t output,
size_t *  out_len 
)

All-in-one encryption or decryption.

This function must be called after the cipher context is initialized with cx_cipher_init and set with cx_cipher_setup. The key must be set with cx_cipher_setkey, the padding method to use must be set with cx_cipher_set_padding.

Parameters
[in]ctxPointer to the context.
[in]ivInitialization vector: a buffer of at least iv_len bytes.
[in]iv_lenLength of the initialization vector in bytes.
[in]inputInput data: a buffer of at least in_len bytes.
[in]in_lenLength of the input in bytes.
[out]outputOutput data: a buffer of at least in_len + block_size bytes.
[out]out_lenLength of the output.
Returns
Error code:
  • CX_OK on success
  • CX_INTERNAL_ERROR
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 492 of file cx_cipher.c.

◆ cx_cipher_finish()

WARN_UNUSED_RESULT cx_err_t cx_cipher_finish ( cx_cipher_context_t ctx,
uint8_t output,
size_t *  out_len 
)

Finalize the operation.

This function must be called after the cipher context is initialized with cx_cipher_init and set with cx_cipher_setup. The key must be set with cx_cipher_setkey, the padding method to use must be set with cx_cipher_set_padding and the Initialization Vector must be set with cx_cipher_setiv. This function must be called after the last cx_cipher_update.

Parameters
[in]ctxPointer to the context.
[out]outputOutput data: buffer of at least block_size bytes.
[out]out_lenLength of the data written to the output.
Returns
Error code:
  • CX_Ok on success
  • CX_INTERNAL_ERROR
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 411 of file cx_cipher.c.

◆ cx_cipher_init()

WARN_UNUSED_RESULT cx_err_t cx_cipher_init ( cx_cipher_context_t ctx)

Initialize a cipher context as NONE.

This function must be called first.

Parameters
[in]ctxPointer to the context. This must not be NULL.
Returns
Error code:
  • CX_OK on success
  • CX_INVALID_PARAMETER

Definition at line 209 of file cx_cipher.c.

◆ cx_cipher_reset()

void cx_cipher_reset ( cx_cipher_context_t ctx)

Definition at line 516 of file cx_cipher.c.

◆ cx_cipher_set_padding()

WARN_UNUSED_RESULT cx_err_t cx_cipher_set_padding ( cx_cipher_context_t ctx,
uint32_t  padding 
)

Set the padding type.

This function must be called after the cipher context is initialized with cx_cipher_init.

Parameters
[in]ctxPointer to the context.
[in]paddingType of padding:
  • CX_PAD_NONE: no padding
  • CX_PAD_ISO9797M1: pad with zeros only
  • CX_PAD_ISO9797M2: pad with a single one and several zeros.
Returns
Error code:
  • CX_OK on success
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 301 of file cx_cipher.c.

◆ cx_cipher_setiv()

WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv ( cx_cipher_context_t ctx,
const uint8_t iv,
size_t  iv_len 
)

Set the initialization vector.

This function must be called after the cipher context is initialized with cx_cipher_init.

Parameters
[in]ctxPointer to the context.
[in]ivInitialization vector: a buffer of at least iv_len bytes.
[in]iv_lenLength of the initialization vector in bytes.
Returns
Error code:
  • CX_OK on success
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 276 of file cx_cipher.c.

◆ cx_cipher_setkey()

WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey ( cx_cipher_context_t ctx,
const uint8_t key,
uint32_t  key_bitlen,
uint32_t  operation 
)

Set the key to use.

This function must be called after the cipher context is initialized with cx_cipher_init.

Parameters
[in]ctxPointer to the context.
[in]keyKey to use: a buffer of at least key_bitlen bits.
[in]key_bitlenLength of key in bits.
[in]operationThe operation that the key will be used for: encryption or decryption.
Returns
Error code:
  • CX_OK on success
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_SIZE
  • CX_INVALID_PARAMETER_VALUE

Definition at line 253 of file cx_cipher.c.

◆ cx_cipher_setup()

WARN_UNUSED_RESULT cx_err_t cx_cipher_setup ( cx_cipher_context_t ctx,
const cx_cipher_id_t  type,
uint32_t  mode 
)

Initialize and fill the context structure given the cipher info.

Parameters
[in]ctxPointer to the context.
[in]typeCipher to use:
  • CX_CIPHER_AES_128
  • CX_CIPHER_AES_192
  • CX_CIPHER_AES_256
[in]modeMode of operation:
  • ECB
  • CBC
  • CTR
Returns
Error code:
  • CX_OK on success
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 218 of file cx_cipher.c.

◆ cx_cipher_update()

WARN_UNUSED_RESULT cx_err_t cx_cipher_update ( cx_cipher_context_t ctx,
const uint8_t input,
size_t  in_len,
uint8_t output,
size_t *  out_len 
)

Encrypt or decrypt with the given context.

This function must be called after the cipher context is initialized with cx_cipher_init and set with cx_cipher_setup. The key must be set with cx_cipher_setkey, the padding method to use must be set with cx_cipher_set_padding and the Initialization Vector must be set with cx_cipher_setiv. One can call this function multiple times depending on the length of the data that needed to be processed.

Parameters
[in]ctxPointer to the context.
[in]inputInput data: buffer of at least in_len bytes.
[in]in_lenLength of the input in bytes.
[out]outputOutput data: a buffer of at least in_len + block_size bytes.
[out]out_lenLength of the data written to the output.
Returns
Error code:
  • CX_OK on success
  • CX_INTERNAL_ERROR
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

Definition at line 324 of file cx_cipher.c.