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 18, 2024
1 parent 1d376c4 commit b9f60d6
Show file tree
Hide file tree
Showing 22 changed files with 1,598 additions and 0 deletions.
138 changes: 138 additions & 0 deletions lib_cxng/include/lcx_aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#if defined(HAVE_AEAD)

#include "cx_errors.h"
#include "ledger_assert.h"
#include <stddef.h>
#if defined(HAVE_AES_GCM)
#include "lcx_aes_gcm.h"
Expand Down Expand Up @@ -143,6 +144,16 @@ 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)
{
LEDGER_ASSERT(cx_aead_init(ctx) == CX_OK, "cx_aead_init");
}

/**
* @brief AEAD set up.
*
Expand All @@ -160,6 +171,16 @@ 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)
{
LEDGER_ASSERT(cx_aead_setup(ctx, type) == CX_OK, "cx_aead_setup");
}

/**
* @brief Sets the cipher key.
*
Expand All @@ -181,6 +202,19 @@ 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)
{
LEDGER_ASSERT(cx_aead_set_key(ctx, key, key_len, mode) == CX_OK, "cx_aead_set_key");
}

/**
* @brief Sets the initialization vector.
*
Expand All @@ -198,6 +232,16 @@ 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)
{
LEDGER_ASSERT(cx_aead_set_iv(ctx, iv, iv_len) == CX_OK, "cx_aead_set_iv");
}

/**
* @brief Adds associated data to the context.
*
Expand All @@ -216,6 +260,18 @@ 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)
{
LEDGER_ASSERT(cx_aead_update_ad(ctx, ad, ad_len) == CX_OK, "cx_aead_update_ad");
}

/**
* @brief Updates the data to encrypt or decrypt.
*
Expand All @@ -239,6 +295,20 @@ 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)
{
LEDGER_ASSERT(cx_aead_update(ctx, in, in_len, out, out_len) == CX_OK, "cx_aead_update");
}

/**
* @brief Writes the tag of the AEAD cipher.
*
Expand All @@ -254,6 +324,16 @@ 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)
{
LEDGER_ASSERT(cx_aead_write_tag(ctx, tag, tag_len) == CX_OK, "cx_aead_write_tag");
}

/**
* @brief Checks the tag of the AEAD cipher.
*
Expand All @@ -271,6 +351,18 @@ 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)
{
LEDGER_ASSERT(cx_aead_check_tag(ctx, tag, tag_len) == CX_OK, "cx_aead_check_tag");
}

/**
* @brief All-in-one authenticated encryption.
*
Expand Down Expand Up @@ -313,6 +405,29 @@ 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)
{
LEDGER_ASSERT(
cx_aead_encrypt(ctx, iv, iv_len, ad, ad_len, in, in_len, out, out_len, tag, tag_len)
== CX_OK,
"cx_aead_encrypt");
}

/**
* @brief All-in-one authenticated decryption.
*
Expand Down Expand Up @@ -356,6 +471,29 @@ 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)
{
LEDGER_ASSERT(
cx_aead_decrypt(ctx, iv, iv_len, ad, ad_len, in, in_len, out, out_len, tag, tag_len)
== CX_OK,
"cx_aead_decrypt");
}

#endif // HAVE_AEAD

#endif // LCX_AED_H
69 changes: 69 additions & 0 deletions lib_cxng/include/lcx_aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "lcx_wrappers.h"
#include "lcx_common.h"
#include "ox_aes.h"
#include "ledger_assert.h"

/**
* @brief Initializes an AES Key.
Expand All @@ -58,6 +59,17 @@ 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)
{
LEDGER_ASSERT(cx_aes_init_key_no_throw(rawkey, key_len, key) == CX_OK,
"cx_aes_init_key_no_throw");
}

/**
* @deprecated
* See #cx_aes_init_key_no_throw
Expand Down Expand Up @@ -123,6 +135,24 @@ 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)
{
LEDGER_ASSERT(cx_aes_iv_no_throw(key, mode, iv, iv_len, in, in_len, out, out_len) == CX_OK,
"cx_aes_iv_no_throw");
}

/**
* @deprecated
* See #cx_aes_iv_no_throw
Expand Down Expand Up @@ -188,6 +218,21 @@ 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)
{
LEDGER_ASSERT(cx_aes_no_throw(key, mode, in, in_len, out, out_len) == CX_OK, "cx_aes_no_throw");
}

/**
* @deprecated
* See #cx_aes_no_throw
Expand Down Expand Up @@ -222,6 +267,18 @@ 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)
{
LEDGER_ASSERT(cx_aes_enc_block(key, inblock, outblock) == CX_OK, "cx_aes_enc_block");
}

/**
* @brief Decrypts a 16-byte block using AES algorithm.
*
Expand All @@ -240,6 +297,18 @@ 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)
{
LEDGER_ASSERT(cx_aes_dec_block(key, inblock, outblock) == CX_OK, "cx_aes_dec_block");
}

#endif // HAVE_AES

#endif // LCX_AES_H
Loading

0 comments on commit b9f60d6

Please sign in to comment.