Embedded SDK
Embedded SDK
|
API for block ciphers. More...
#include "cx_errors.h"
#include "lcx_wrappers.h"
#include "lcx_common.h"
#include "lcx_aes.h"
#include <stddef.h>
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) |
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.
#define MAX_BLOCK_LENGTH 16 |
Maximum block size in bytes
Definition at line 21 of file lcx_cipher.h.
#define MAX_IV_LENGTH 16 |
Maximum length of the initialization vector in bytes
Definition at line 19 of file lcx_cipher.h.
enum 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.
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.
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.
[in] | ctx | Pointer to the context. |
[in] | iv | Initialization vector: a buffer of at least iv_len bytes. |
[in] | iv_len | Length of the initialization vector in bytes. |
[in] | input | Input data: a buffer of at least in_len bytes. |
[in] | in_len | Length of the input in bytes. |
[out] | output | Output data: a buffer of at least in_len + block_size bytes. |
[out] | out_len | Length of the output. |
Definition at line 492 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[out] | output | Output data: buffer of at least block_size bytes. |
[out] | out_len | Length of the data written to the output. |
Definition at line 411 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. This must not be NULL. |
Definition at line 209 of file cx_cipher.c.
void cx_cipher_reset | ( | cx_cipher_context_t * | ctx | ) |
Definition at line 516 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[in] | padding | Type of padding:
|
Definition at line 301 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[in] | iv | Initialization vector: a buffer of at least iv_len bytes. |
[in] | iv_len | Length of the initialization vector in bytes. |
Definition at line 276 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[in] | key | Key to use: a buffer of at least key_bitlen bits. |
[in] | key_bitlen | Length of key in bits. |
[in] | operation | The operation that the key will be used for: encryption or decryption. |
Definition at line 253 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[in] | type | Cipher to use:
|
[in] | mode | Mode of operation:
|
Definition at line 218 of file cx_cipher.c.
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.
[in] | ctx | Pointer to the context. |
[in] | input | Input data: buffer of at least in_len bytes. |
[in] | in_len | Length of the input in bytes. |
[out] | output | Output data: a buffer of at least in_len + block_size bytes. |
[out] | out_len | Length of the data written to the output. |
Definition at line 324 of file cx_cipher.c.