Embedded SDK
Embedded SDK
Macros | Functions
cx_cipher.c File Reference
#include "cx_cipher.h"
#include "os_math.h"
#include "os_utils.h"
#include <stddef.h>
#include <string.h>
Include dependency graph for cx_cipher.c:

Go to the source code of this file.

Macros

#define CX_MAX_BLOCK_SIZE   16
 

Functions

void add_one_and_zeros_padding (uint8_t *output, size_t out_len, size_t data_len)
 
cx_err_t cx_cipher_init (cx_cipher_context_t *ctx)
 Initialize a cipher context as NONE. More...
 
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...
 
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...
 
cx_err_t cx_cipher_setiv (cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len)
 Set the initialization vector. More...
 
cx_err_t cx_cipher_set_padding (cx_cipher_context_t *ctx, uint32_t padding)
 Set the padding type. More...
 
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...
 
cx_err_t cx_cipher_finish (cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len)
 Finalize the operation. More...
 
cx_err_t cx_cipher_mac (cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len, size_t *finish_len)
 
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)
 

Macro Definition Documentation

◆ CX_MAX_BLOCK_SIZE

#define CX_MAX_BLOCK_SIZE   16

Definition at line 10 of file cx_cipher.c.

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()

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()

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()

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_mac()

cx_err_t cx_cipher_mac ( cx_cipher_context_t ctx,
uint8_t output,
size_t *  out_len,
size_t *  finish_len 
)

Definition at line 471 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()

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()

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()

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()

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()

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.