Skip to content

Commit

Permalink
lib_cxng: Introduce cx_xyz_assert() for praticality
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Chapron committed Jan 22, 2024
1 parent 4841c8a commit 8c1af02
Show file tree
Hide file tree
Showing 23 changed files with 2,045 additions and 0 deletions.
173 changes: 173 additions & 0 deletions lib_cxng/include/lcx_aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ typedef struct {
*/
WARN_UNUSED_RESULT cx_err_t cx_aead_init(cx_aead_context_t *ctx);

/**
* @brief cx_aead_init version which doesn't return in case of failure.
*
* See #cx_aead_init
*/
static inline void cx_aead_init_assert(cx_aead_context_t *ctx)
{
cx_err_t err = cx_aead_init(ctx);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_init", err);
LEDGER_ASSERT(false, "cx_aead_init_assert");
}
}

/**
* @brief AEAD set up.
*
Expand All @@ -161,6 +175,20 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_init(cx_aead_context_t *ctx);
*/
WARN_UNUSED_RESULT cx_err_t cx_aead_setup(cx_aead_context_t *ctx, cx_aead_type_t type);

/**
* @brief cx_aead_setup version which doesn't return in case of failure.
*
* See #cx_aead_setup
*/
static inline void cx_aead_setup_assert(cx_aead_context_t *ctx, cx_aead_type_t type)
{
cx_err_t err = cx_aead_setup(ctx, type);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_setup", err);
LEDGER_ASSERT(false, "cx_aead_setup_assert");
}
}

/**
* @brief Sets the cipher key.
*
Expand All @@ -182,6 +210,23 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_set_key(cx_aead_context_t *ctx,
size_t key_len,
uint32_t mode);

/**
* @brief cx_aead_set_key version which doesn't return in case of failure.
*
* See #cx_aead_set_key
*/
static inline void cx_aead_set_key_assert(cx_aead_context_t *ctx,
const uint8_t *key,
size_t key_len,
uint32_t mode)
{
cx_err_t err = cx_aead_set_key(ctx, key, key_len, mode);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_set_key", err);
LEDGER_ASSERT(false, "cx_aead_set_key_assert");
}
}

/**
* @brief Sets the initialization vector.
*
Expand All @@ -199,6 +244,20 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_set_iv(cx_aead_context_t *ctx,
const uint8_t *iv,
size_t iv_len);

/**
* @brief cx_aead_set_iv version which doesn't return in case of failure.
*
* See #cx_aead_set_iv
*/
static inline void cx_aead_set_iv_assert(cx_aead_context_t *ctx, const uint8_t *iv, size_t iv_len)
{
cx_err_t err = cx_aead_set_iv(ctx, iv, iv_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_set_iv", err);
LEDGER_ASSERT(false, "cx_aead_set_iv_assert");
}
}

/**
* @brief Adds associated data to the context.
*
Expand All @@ -217,6 +276,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_update_ad(cx_aead_context_t *ctx,
const uint8_t *ad,
size_t ad_len);

/**
* @brief cx_aead_update_ad version which doesn't return in case of failure.
*
* See #cx_aead_update_ad
*/
static inline void cx_aead_update_ad_assert(cx_aead_context_t *ctx,
const uint8_t *ad,
size_t ad_len)
{
cx_err_t err = cx_aead_update_ad(ctx, ad, ad_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_update_ad", err);
LEDGER_ASSERT(false, "cx_aead_update_ad_assert");
}
}

/**
* @brief Updates the data to encrypt or decrypt.
*
Expand All @@ -240,6 +315,24 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_update_ad(cx_aead_context_t *ctx,
WARN_UNUSED_RESULT cx_err_t
cx_aead_update(cx_aead_context_t *ctx, uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len);

/**
* @brief cx_aead_update version which doesn't return in case of failure.
*
* See #cx_aead_update
*/
static inline void cx_aead_update_assert(cx_aead_context_t *ctx,
uint8_t *in,
size_t in_len,
uint8_t *out,
size_t *out_len)
{
cx_err_t err = cx_aead_update(ctx, in, in_len, out, out_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_update", err);
LEDGER_ASSERT(false, "cx_aead_update_assert");
}
}

/**
* @brief Writes the tag of the AEAD cipher.
*
Expand All @@ -255,6 +348,20 @@ cx_aead_update(cx_aead_context_t *ctx, uint8_t *in, size_t in_len, uint8_t *out,
*/
WARN_UNUSED_RESULT cx_err_t cx_aead_write_tag(cx_aead_context_t *ctx, uint8_t *tag, size_t tag_len);

/**
* @brief cx_aead_write_tag version which doesn't return in case of failure.
*
* See #cx_aead_write_tag
*/
static inline void cx_aead_write_tag_assert(cx_aead_context_t *ctx, uint8_t *tag, size_t tag_len)
{
cx_err_t err = cx_aead_write_tag(ctx, tag, tag_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_write_tag", err);
LEDGER_ASSERT(false, "cx_aead_write_tag_assert");
}
}

/**
* @brief Checks the tag of the AEAD cipher.
*
Expand All @@ -272,6 +379,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_check_tag(cx_aead_context_t *ctx,
const uint8_t *tag,
size_t tag_len);

/**
* @brief cx_aead_check_tag version which doesn't return in case of failure.
*
* See #cx_aead_check_tag
*/
static inline void cx_aead_check_tag_assert(cx_aead_context_t *ctx,
const uint8_t *tag,
size_t tag_len)
{
cx_err_t err = cx_aead_check_tag(ctx, tag, tag_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_check_tag", err);
LEDGER_ASSERT(false, "cx_aead_check_tag_assert");
}
}

/**
* @brief All-in-one authenticated encryption.
*
Expand Down Expand Up @@ -314,6 +437,31 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_encrypt(cx_aead_context_t *ctx,
uint8_t *tag,
size_t tag_len);

/**
* @brief cx_aead_encrypt version which doesn't return in case of failure.
*
* See #cx_aead_encrypt
*/
static inline void cx_aead_encrypt_assert(cx_aead_context_t *ctx,
const uint8_t *iv,
size_t iv_len,
const uint8_t *ad,
size_t ad_len,
uint8_t *in,
size_t in_len,
uint8_t *out,
size_t *out_len,
uint8_t *tag,
size_t tag_len)
{
cx_err_t err
= cx_aead_encrypt(ctx, iv, iv_len, ad, ad_len, in, in_len, out, out_len, tag, tag_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_encrypt", err);
LEDGER_ASSERT(false, "cx_aead_encrypt_assert");
}
}

/**
* @brief All-in-one authenticated decryption.
*
Expand Down Expand Up @@ -357,6 +505,31 @@ WARN_UNUSED_RESULT cx_err_t cx_aead_decrypt(cx_aead_context_t *ctx,
const uint8_t *tag,
size_t tag_len);

/**
* @brief cx_aead_decrypt version which doesn't return in case of failure.
*
* See #cx_aead_decrypt
*/
static inline void cx_aead_decrypt_assert(cx_aead_context_t *ctx,
const uint8_t *iv,
size_t iv_len,
const uint8_t *ad,
size_t ad_len,
uint8_t *in,
size_t in_len,
uint8_t *out,
size_t *out_len,
const uint8_t *tag,
size_t tag_len)
{
cx_err_t err
= cx_aead_decrypt(ctx, iv, iv_len, ad, ad_len, in, in_len, out, out_len, tag, tag_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aead_decrypt", err);
LEDGER_ASSERT(false, "cx_aead_decrypt_assert");
}
}

#endif // HAVE_AEAD

#endif // LCX_AED_H
86 changes: 86 additions & 0 deletions lib_cxng/include/lcx_aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_init_key_no_throw(const uint8_t *rawkey,
size_t key_len,
cx_aes_key_t *key);

/**
* @brief cx_aes_init_key_no_throw version which doesn't return in case of failure.
*
* See #cx_aes_init_key_no_throw
*/
static inline void cx_aes_init_key_assert(const uint8_t *rawkey, size_t key_len, cx_aes_key_t *key)
{
cx_err_t err = cx_aes_init_key_no_throw(rawkey, key_len, key);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aes_init_key_no_throw", err);
LEDGER_ASSERT(false, "cx_aes_init_key_assert");
}
}

/**
* @deprecated
* See #cx_aes_init_key_no_throw
Expand Down Expand Up @@ -124,6 +138,27 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_iv_no_throw(const cx_aes_key_t *key,
uint8_t *out,
size_t *out_len);

/**
* @brief cx_aes_iv_no_throw version which doesn't return in case of failure.
*
* See #cx_aes_iv_no_throw
*/
static inline void cx_aes_iv_assert(const cx_aes_key_t *key,
uint32_t mode,
const uint8_t *iv,
size_t iv_len,
const uint8_t *in,
size_t in_len,
uint8_t *out,
size_t *out_len)
{
cx_err_t err = cx_aes_iv_no_throw(key, mode, iv, iv_len, in, in_len, out, out_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aes_iv_no_throw", err);
LEDGER_ASSERT(false, "cx_aes_iv_assert");
}
}

/**
* @deprecated
* See #cx_aes_iv_no_throw
Expand Down Expand Up @@ -189,6 +224,25 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_no_throw(const cx_aes_key_t *key,
uint8_t *out,
size_t *out_len);

/**
* @brief cx_aes_no_throw version which doesn't return in case of failure.
*
* See #cx_aes_no_throw
*/
static inline void cx_aes_assert(const cx_aes_key_t *key,
uint32_t mode,
const uint8_t *in,
size_t in_len,
uint8_t *out,
size_t *out_len)
{
cx_err_t err = cx_aes_no_throw(key, mode, in, in_len, out, out_len);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aes_no_throw", err);
LEDGER_ASSERT(false, "cx_aes_assert");
}
}

/**
* @deprecated
* See #cx_aes_no_throw
Expand Down Expand Up @@ -223,6 +277,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_enc_block(const cx_aes_key_t *key,
const uint8_t *inblock,
uint8_t *outblock);

/**
* @brief cx_aes_enc_block version which doesn't return in case of failure.
*
* See #cx_aes_enc_block
*/
static inline void cx_aes_enc_block_assert(const cx_aes_key_t *key,
const uint8_t *inblock,
uint8_t *outblock)
{
cx_err_t err = cx_aes_enc_block(key, inblock, outblock);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aes_enc_block", err);
LEDGER_ASSERT(false, "cx_aes_enc_block_assert");
}
}

/**
* @brief Decrypts a 16-byte block using AES algorithm.
*
Expand All @@ -241,6 +311,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_dec_block(const cx_aes_key_t *key,
const uint8_t *inblock,
uint8_t *outblock);

/**
* @brief cx_aes_dec_block version which doesn't return in case of failure.
*
* See #cx_aes_dec_block
*/
static inline void cx_aes_dec_block_assert(const cx_aes_key_t *key,
const uint8_t *inblock,
uint8_t *outblock)
{
cx_err_t err = cx_aes_dec_block(key, inblock, outblock);
if (err != CX_OK) {
PRINTF("%s failed (err %d)\n", "cx_aes_dec_block", err);
LEDGER_ASSERT(false, "cx_aes_dec_block_assert");
}
}

#endif // HAVE_AES

#endif // LCX_AES_H
Loading

0 comments on commit 8c1af02

Please sign in to comment.