From 8c1af02f9f9e0f6fac5bc67e99c5a50206ea9183 Mon Sep 17 00:00:00 2001 From: Xavier Chapron Date: Mon, 22 Jan 2024 11:28:23 +0100 Subject: [PATCH] lib_cxng: Introduce cx_xyz_assert() for praticality --- lib_cxng/include/lcx_aead.h | 173 ++++++++++++++++++++++++ lib_cxng/include/lcx_aes.h | 86 ++++++++++++ lib_cxng/include/lcx_aes_gcm.h | 144 ++++++++++++++++++++ lib_cxng/include/lcx_aes_siv.h | 137 +++++++++++++++++++ lib_cxng/include/lcx_blake2.h | 33 +++++ lib_cxng/include/lcx_blake3.h | 65 ++++++++++ lib_cxng/include/lcx_chacha.h | 70 ++++++++++ lib_cxng/include/lcx_chacha_poly.h | 146 +++++++++++++++++++++ lib_cxng/include/lcx_cipher.h | 131 +++++++++++++++++++ lib_cxng/include/lcx_cmac.h | 65 ++++++++++ lib_cxng/include/lcx_ecdh.h | 47 +++++++ lib_cxng/include/lcx_ecdsa.h | 44 +++++++ lib_cxng/include/lcx_ecfp.h | 151 +++++++++++++++++++++ lib_cxng/include/lcx_ecschnorr.h | 20 +++ lib_cxng/include/lcx_eddsa.h | 19 +++ lib_cxng/include/lcx_groestl.h | 47 +++++++ lib_cxng/include/lcx_hash.h | 75 +++++++++++ lib_cxng/include/lcx_hmac.h | 144 ++++++++++++++++++++ lib_cxng/include/lcx_math.h | 202 +++++++++++++++++++++++++++++ lib_cxng/include/lcx_ripemd160.h | 14 ++ lib_cxng/include/lcx_rng.h | 22 ++++ lib_cxng/include/lcx_rsa.h | 140 ++++++++++++++++++++ lib_cxng/include/lcx_sha3.h | 70 ++++++++++ 23 files changed, 2045 insertions(+) diff --git a/lib_cxng/include/lcx_aead.h b/lib_cxng/include/lcx_aead.h index 3e7545d75..a35625424 100644 --- a/lib_cxng/include/lcx_aead.h +++ b/lib_cxng/include/lcx_aead.h @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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 diff --git a/lib_cxng/include/lcx_aes.h b/lib_cxng/include/lcx_aes.h index c2d632ca0..3a6272a9d 100644 --- a/lib_cxng/include/lcx_aes.h +++ b/lib_cxng/include/lcx_aes.h @@ -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 @@ -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 @@ -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 @@ -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. * @@ -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 diff --git a/lib_cxng/include/lcx_aes_gcm.h b/lib_cxng/include/lcx_aes_gcm.h index 5e4e9cef4..73975b5d3 100644 --- a/lib_cxng/include/lcx_aes_gcm.h +++ b/lib_cxng/include/lcx_aes_gcm.h @@ -58,20 +58,100 @@ void cx_aes_gcm_init(cx_aes_gcm_context_t *ctx); WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_set_key(cx_aes_gcm_context_t *ctx, const uint8_t *raw_key, size_t key_len); + +/** + * @brief cx_aes_gcm_set_key version which doesn't return in case of failure. + * + * See #cx_aes_gcm_set_key + */ +static inline void cx_aes_gcm_set_key_assert(cx_aes_gcm_context_t *ctx, + const uint8_t *raw_key, + size_t key_len) +{ + cx_err_t err = cx_aes_gcm_set_key(ctx, raw_key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_set_key", err); + LEDGER_ASSERT(false, "cx_aes_gcm_set_key_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_start(cx_aes_gcm_context_t *ctx, uint32_t mode, const uint8_t *iv, size_t iv_len); + +/** + * @brief cx_aes_gcm_start version which doesn't return in case of failure. + * + * See #cx_aes_gcm_start + */ +static inline void cx_aes_gcm_start_assert(cx_aes_gcm_context_t *ctx, + uint32_t mode, + const uint8_t *iv, + size_t iv_len) +{ + cx_err_t err = cx_aes_gcm_start(ctx, mode, iv, iv_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_start", err); + LEDGER_ASSERT(false, "cx_aes_gcm_start_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_update_aad(cx_aes_gcm_context_t *ctx, const uint8_t *aad, size_t aad_len); + +/** + * @brief cx_aes_gcm_update_aad version which doesn't return in case of failure. + * + * See #cx_aes_gcm_update_aad + */ +static inline void cx_aes_gcm_update_aad_assert(cx_aes_gcm_context_t *ctx, + const uint8_t *aad, + size_t aad_len) +{ + cx_err_t err = cx_aes_gcm_update_aad(ctx, aad, aad_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_update_aad", err); + LEDGER_ASSERT(false, "cx_aes_gcm_update_aad_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_update(cx_aes_gcm_context_t *ctx, const uint8_t *in, uint8_t *out, size_t len); + +/** + * @brief cx_aes_gcm_update version which doesn't return in case of failure. + * + * See #cx_aes_gcm_update + */ +static inline void cx_aes_gcm_update_assert(cx_aes_gcm_context_t *ctx, + const uint8_t *in, + uint8_t *out, + size_t len) +{ + cx_err_t err = cx_aes_gcm_update(ctx, in, out, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_update", err); + LEDGER_ASSERT(false, "cx_aes_gcm_update_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_finish(cx_aes_gcm_context_t *ctx, uint8_t *tag, size_t tag_len); + +/** + * @brief cx_aes_gcm_finish version which doesn't return in case of failure. + * + * See #cx_aes_gcm_finish + */ +static inline void cx_aes_gcm_finish_assert(cx_aes_gcm_context_t *ctx, uint8_t *tag, size_t tag_len) +{ + cx_err_t err = cx_aes_gcm_finish(ctx, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_finish", err); + LEDGER_ASSERT(false, "cx_aes_gcm_finish_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_encrypt_and_tag(cx_aes_gcm_context_t *ctx, uint8_t *in, size_t len, @@ -82,6 +162,30 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_encrypt_and_tag(cx_aes_gcm_context_t *ctx uint8_t *out, uint8_t *tag, size_t tag_len); + +/** + * @brief cx_aes_gcm_encrypt_and_tag version which doesn't return in case of failure. + * + * See #cx_aes_gcm_encrypt_and_tag + */ +static inline void cx_aes_gcm_encrypt_and_tag_assert(cx_aes_gcm_context_t *ctx, + uint8_t *in, + size_t len, + const uint8_t *iv, + size_t iv_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *out, + uint8_t *tag, + size_t tag_len) +{ + cx_err_t err + = cx_aes_gcm_encrypt_and_tag(ctx, in, len, iv, iv_len, aad, aad_len, out, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_encrypt_and_tag", err); + LEDGER_ASSERT(false, "cx_aes_gcm_encrypt_and_tag_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_decrypt_and_auth(cx_aes_gcm_context_t *ctx, uint8_t *in, size_t len, @@ -92,10 +196,50 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_decrypt_and_auth(cx_aes_gcm_context_t *ct uint8_t *out, const uint8_t *tag, size_t tag_len); + +/** + * @brief cx_aes_gcm_decrypt_and_auth version which doesn't return in case of failure. + * + * See #cx_aes_gcm_decrypt_and_auth + */ +static inline void cx_aes_gcm_decrypt_and_auth_assert(cx_aes_gcm_context_t *ctx, + uint8_t *in, + size_t len, + const uint8_t *iv, + size_t iv_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *out, + const uint8_t *tag, + size_t tag_len) +{ + cx_err_t err + = cx_aes_gcm_decrypt_and_auth(ctx, in, len, iv, iv_len, aad, aad_len, out, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_decrypt_and_auth", err); + LEDGER_ASSERT(false, "cx_aes_gcm_decrypt_and_auth_assert"); + } +} WARN_UNUSED_RESULT cx_err_t cx_aes_gcm_check_tag(cx_aes_gcm_context_t *ctx, const uint8_t *tag, size_t tag_len); +/** + * @brief cx_aes_gcm_check_tag version which doesn't return in case of failure. + * + * See #cx_aes_gcm_check_tag + */ +static inline void cx_aes_gcm_check_tag_assert(cx_aes_gcm_context_t *ctx, + const uint8_t *tag, + size_t tag_len) +{ + cx_err_t err = cx_aes_gcm_check_tag(ctx, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_gcm_check_tag", err); + LEDGER_ASSERT(false, "cx_aes_gcm_check_tag_assert"); + } +} + #endif // HAVE_AES && HAVE_AES_GCM #endif // LCX_AES_GCM_H diff --git a/lib_cxng/include/lcx_aes_siv.h b/lib_cxng/include/lcx_aes_siv.h index 52221af82..736796501 100644 --- a/lib_cxng/include/lcx_aes_siv.h +++ b/lib_cxng/include/lcx_aes_siv.h @@ -56,6 +56,20 @@ typedef struct _cx_aes_siv_context { */ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_init(cx_aes_siv_context_t *ctx); +/** + * @brief cx_aes_siv_init version which doesn't return in case of failure. + * + * See #cx_aes_siv_init + */ +static inline void cx_aes_siv_init_assert(cx_aes_siv_context_t *ctx) +{ + cx_err_t err = cx_aes_siv_init(ctx); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_init", err); + LEDGER_ASSERT(false, "cx_aes_siv_init_assert"); + } +} + /** * @brief Sets the key to compute AES-SIV. * @@ -76,6 +90,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_set_key(cx_aes_siv_context_t *ctx, const uint8_t *key, size_t key_bitlen); +/** + * @brief cx_aes_siv_set_key version which doesn't return in case of failure. + * + * See #cx_aes_siv_set_key + */ +static inline void cx_aes_siv_set_key_assert(cx_aes_siv_context_t *ctx, + const uint8_t *key, + size_t key_bitlen) +{ + cx_err_t err = cx_aes_siv_set_key(ctx, key, key_bitlen); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_set_key", err); + LEDGER_ASSERT(false, "cx_aes_siv_set_key_assert"); + } +} + /** * @brief Starts the S2V algorithm following RFC5297 specification. * @@ -93,6 +123,23 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_start(cx_aes_siv_context_t *ctx, const uint8_t *iv, size_t iv_len); +/** + * @brief cx_aes_siv_start version which doesn't return in case of failure. + * + * See #cx_aes_siv_start + */ +static inline void cx_aes_siv_start_assert(cx_aes_siv_context_t *ctx, + uint32_t mode, + const uint8_t *iv, + size_t iv_len) +{ + cx_err_t err = cx_aes_siv_start(ctx, mode, iv, iv_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_start", err); + LEDGER_ASSERT(false, "cx_aes_siv_start_assert"); + } +} + /** * @brief Processes additional data. * @@ -105,6 +152,22 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_update_aad(cx_aes_siv_context_t *ctx, const uint8_t *aad, size_t aad_len); +/** + * @brief cx_aes_siv_update_aad version which doesn't return in case of failure. + * + * See #cx_aes_siv_update_aad + */ +static inline void cx_aes_siv_update_aad_assert(cx_aes_siv_context_t *ctx, + const uint8_t *aad, + size_t aad_len) +{ + cx_err_t err = cx_aes_siv_update_aad(ctx, aad, aad_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_update_aad", err); + LEDGER_ASSERT(false, "cx_aes_siv_update_aad_assert"); + } +} + /** * @brief Processes plaintext or ciphertext with AES-CTR. * @@ -120,6 +183,23 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_update(cx_aes_siv_context_t *ctx, uint8_t *output, size_t len); +/** + * @brief cx_aes_siv_update version which doesn't return in case of failure. + * + * See #cx_aes_siv_update + */ +static inline void cx_aes_siv_update_assert(cx_aes_siv_context_t *ctx, + const uint8_t *input, + uint8_t *output, + size_t len) +{ + cx_err_t err = cx_aes_siv_update(ctx, input, output, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_update", err); + LEDGER_ASSERT(false, "cx_aes_siv_update_assert"); + } +} + /** * @brief Finishes the S2V algorithm and prepares for the * AES-CTR computation. @@ -136,6 +216,23 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_finish(cx_aes_siv_context_t *ctx, size_t in_len, uint8_t *tag); +/** + * @brief cx_aes_siv_finish version which doesn't return in case of failure. + * + * See #cx_aes_siv_finish + */ +static inline void cx_aes_siv_finish_assert(cx_aes_siv_context_t *ctx, + const uint8_t *input, + size_t in_len, + uint8_t *tag) +{ + cx_err_t err = cx_aes_siv_finish(ctx, input, in_len, tag); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_finish", err); + LEDGER_ASSERT(false, "cx_aes_siv_finish_assert"); + } +} + /** * @brief All-in-one encryption. * @@ -158,6 +255,26 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_encrypt(cx_aes_siv_context_t *ctx, uint8_t *output, uint8_t *tag); +/** + * @brief cx_aes_siv_encrypt version which doesn't return in case of failure. + * + * See #cx_aes_siv_encrypt + */ +static inline void cx_aes_siv_encrypt_assert(cx_aes_siv_context_t *ctx, + const uint8_t *input, + size_t in_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *output, + uint8_t *tag) +{ + cx_err_t err = cx_aes_siv_encrypt(ctx, input, in_len, aad, aad_len, output, tag); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_encrypt", err); + LEDGER_ASSERT(false, "cx_aes_siv_encrypt_assert"); + } +} + /** * @brief All-in-one decryption. * @@ -180,6 +297,26 @@ WARN_UNUSED_RESULT cx_err_t cx_aes_siv_decrypt(cx_aes_siv_context_t *ctx, uint8_t *output, uint8_t *tag); +/** + * @brief cx_aes_siv_decrypt version which doesn't return in case of failure. + * + * See #cx_aes_siv_decrypt + */ +static inline void cx_aes_siv_decrypt_assert(cx_aes_siv_context_t *ctx, + const uint8_t *input, + size_t in_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *output, + uint8_t *tag) +{ + cx_err_t err = cx_aes_siv_decrypt(ctx, input, in_len, aad, aad_len, output, tag); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_aes_siv_decrypt", err); + LEDGER_ASSERT(false, "cx_aes_siv_decrypt_assert"); + } +} + #endif /* LCX_AES_SIV_H */ #endif // HAVE_AES_SIV diff --git a/lib_cxng/include/lcx_blake2.h b/lib_cxng/include/lcx_blake2.h index 21d07de87..c6c7c4c85 100644 --- a/lib_cxng/include/lcx_blake2.h +++ b/lib_cxng/include/lcx_blake2.h @@ -82,6 +82,20 @@ typedef struct cx_blake2b_s cx_blake2b_t; */ WARN_UNUSED_RESULT cx_err_t cx_blake2b_init_no_throw(cx_blake2b_t *hash, size_t out_len); +/** + * @brief cx_blake2b_init_no_throw version which doesn't return in case of failure. + * + * See #cx_blake2b_init_no_throw + */ +static inline void cx_blake2b_init_assert(cx_blake2b_t *hash, size_t out_len) +{ + cx_err_t err = cx_blake2b_init_no_throw(hash, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake2b_init_no_throw", err); + LEDGER_ASSERT(false, "cx_blake2b_init_assert"); + } +} + /** * @deprecated * See #cx_blake2b_init_no_throw @@ -120,6 +134,25 @@ WARN_UNUSED_RESULT cx_err_t cx_blake2b_init2_no_throw(cx_blake2b_t *hash, uint8_t *perso, size_t perso_len); +/** + * @brief cx_blake2b_init2_no_throw version which doesn't return in case of failure. + * + * See #cx_blake2b_init2_no_throw + */ +static inline void cx_blake2b_init2_assert(cx_blake2b_t *hash, + size_t out_len, + uint8_t *salt, + size_t salt_len, + uint8_t *perso, + size_t perso_len) +{ + cx_err_t err = cx_blake2b_init2_no_throw(hash, out_len, salt, salt_len, perso, perso_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake2b_init2_no_throw", err); + LEDGER_ASSERT(false, "cx_blake2b_init2_assert"); + } +} + /** * @deprecated * See #cx_blake2b_init2_no_throw diff --git a/lib_cxng/include/lcx_blake3.h b/lib_cxng/include/lcx_blake3.h index 13069053b..5682c5265 100644 --- a/lib_cxng/include/lcx_blake3.h +++ b/lib_cxng/include/lcx_blake3.h @@ -108,6 +108,24 @@ WARN_UNUSED_RESULT cx_err_t cx_blake3_init(cx_blake3_t *hash, const void *context, unsigned int context_len); +/** + * @brief cx_blake3_init version which doesn't return in case of failure. + * + * See #cx_blake3_init + */ +static inline void cx_blake3_init_assert(cx_blake3_t *hash, + uint8_t mode, + const unsigned char *key, + const void *context, + unsigned int context_len) +{ + cx_err_t err = cx_blake3_init(hash, mode, key, context, context_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake3_init", err); + LEDGER_ASSERT(false, "cx_blake3_init_assert"); + } +} + /** * @brief Computes the digest of a message using blake3. * @@ -132,6 +150,25 @@ WARN_UNUSED_RESULT cx_err_t cx_blake3(cx_blake3_t *hash, uint8_t *out, size_t out_len); +/** + * @brief cx_blake3 version which doesn't return in case of failure. + * + * See #cx_blake3 + */ +static inline void cx_blake3_assert(cx_blake3_t *hash, + uint8_t mode, + const void *input, + size_t input_len, + uint8_t *out, + size_t out_len) +{ + cx_err_t err = cx_blake3(hash, mode, input, input_len, out, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake3", err); + LEDGER_ASSERT(false, "cx_blake3_assert"); + } +} + /** * @brief Adds more data to process to the context. * @@ -147,6 +184,20 @@ WARN_UNUSED_RESULT cx_err_t cx_blake3_update(cx_blake3_t *hash, const void *input, size_t input_len); +/** + * @brief cx_blake3_update version which doesn't return in case of failure. + * + * See #cx_blake3_update + */ +static inline void cx_blake3_update_assert(cx_blake3_t *hash, const void *input, size_t input_len) +{ + cx_err_t err = cx_blake3_update(hash, input, input_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake3_update", err); + LEDGER_ASSERT(false, "cx_blake3_update_assert"); + } +} + /** * @brief Finalizes the hash. * @@ -160,5 +211,19 @@ WARN_UNUSED_RESULT cx_err_t cx_blake3_update(cx_blake3_t *hash, */ WARN_UNUSED_RESULT cx_err_t cx_blake3_final(cx_blake3_t *hash, uint8_t *output, size_t out_len); +/** + * @brief cx_blake3_final version which doesn't return in case of failure. + * + * See #cx_blake3_final + */ +static inline void cx_blake3_final_assert(cx_blake3_t *hash, uint8_t *output, size_t out_len) +{ + cx_err_t err = cx_blake3_final(hash, output, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_blake3_final", err); + LEDGER_ASSERT(false, "cx_blake3_final_assert"); + } +} + #endif // LCX_BLAKE3_H #endif // HAVE_BLAKE3 diff --git a/lib_cxng/include/lcx_chacha.h b/lib_cxng/include/lcx_chacha.h index 107728b10..ad867de76 100644 --- a/lib_cxng/include/lcx_chacha.h +++ b/lib_cxng/include/lcx_chacha.h @@ -85,6 +85,22 @@ WARN_UNUSED_RESULT cx_err_t cx_chacha_set_key(cx_chacha_context_t *ctx, const uint8_t *key, size_t key_len); +/** + * @brief cx_chacha_set_key version which doesn't return in case of failure. + * + * See #cx_chacha_set_key + */ +static inline void cx_chacha_set_key_assert(cx_chacha_context_t *ctx, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_chacha_set_key(ctx, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chacha_set_key", err); + LEDGER_ASSERT(false, "cx_chacha_set_key_assert"); + } +} + /** * @brief Set the nonce and initial counter value. * @@ -109,6 +125,22 @@ WARN_UNUSED_RESULT cx_err_t cx_chacha_start(cx_chacha_context_t *ctx, const uint8_t *iv, size_t iv_len); +/** + * @brief cx_chacha_start version which doesn't return in case of failure. + * + * See #cx_chacha_start + */ +static inline void cx_chacha_start_assert(cx_chacha_context_t *ctx, + const uint8_t *iv, + size_t iv_len) +{ + cx_err_t err = cx_chacha_start(ctx, iv, iv_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chacha_start", err); + LEDGER_ASSERT(false, "cx_chacha_start_assert"); + } +} + /** * @brief Update the stream: encrypt or decrypt data. * @@ -133,6 +165,23 @@ WARN_UNUSED_RESULT cx_err_t cx_chacha_update(cx_chacha_context_t *ctx, uint8_t *output, size_t len); +/** + * @brief cx_chacha_update version which doesn't return in case of failure. + * + * See #cx_chacha_update + */ +static inline void cx_chacha_update_assert(cx_chacha_context_t *ctx, + const uint8_t *input, + uint8_t *output, + size_t len) +{ + cx_err_t err = cx_chacha_update(ctx, input, output, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chacha_update", err); + LEDGER_ASSERT(false, "cx_chacha_update_assert"); + } +} + /** * @brief Encrypt or decrypt data with Chacha and a given key and nonce. * @@ -165,6 +214,27 @@ WARN_UNUSED_RESULT cx_err_t cx_chacha_cipher(uint32_t nrounds, uint8_t *output, size_t len); +/** + * @brief cx_chacha_cipher version which doesn't return in case of failure. + * + * See #cx_chacha_cipher + */ +static inline void cx_chacha_cipher_assert(uint32_t nrounds, + const uint8_t *key, + size_t key_len, + const uint8_t *iv, + size_t iv_len, + const uint8_t *input, + uint8_t *output, + size_t len) +{ + cx_err_t err = cx_chacha_cipher(nrounds, key, key_len, iv, iv_len, input, output, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chacha_cipher", err); + LEDGER_ASSERT(false, "cx_chacha_cipher_assert"); + } +} + #endif // HAVE_CHACHA #endif // LCX_CHACHA_H diff --git a/lib_cxng/include/lcx_chacha_poly.h b/lib_cxng/include/lcx_chacha_poly.h index 0abb9346d..fd938e139 100644 --- a/lib_cxng/include/lcx_chacha_poly.h +++ b/lib_cxng/include/lcx_chacha_poly.h @@ -53,24 +53,106 @@ WARN_UNUSED_RESULT cx_err_t cx_chachapoly_set_key(cx_chachapoly_context_t *ctx, const uint8_t *key, size_t key_len); +/** + * @brief cx_chachapoly_set_key version which doesn't return in case of failure. + * + * See #cx_chachapoly_set_key + */ +static inline void cx_chachapoly_set_key_assert(cx_chachapoly_context_t *ctx, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_chachapoly_set_key(ctx, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_set_key", err); + LEDGER_ASSERT(false, "cx_chachapoly_set_key_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_start(cx_chachapoly_context_t *ctx, uint32_t mode, const uint8_t *iv, size_t iv_len); +/** + * @brief cx_chachapoly_start version which doesn't return in case of failure. + * + * See #cx_chachapoly_start + */ +static inline void cx_chachapoly_start_assert(cx_chachapoly_context_t *ctx, + uint32_t mode, + const uint8_t *iv, + size_t iv_len) +{ + cx_err_t err = cx_chachapoly_start(ctx, mode, iv, iv_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_start", err); + LEDGER_ASSERT(false, "cx_chachapoly_start_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_update_aad(cx_chachapoly_context_t *ctx, const uint8_t *aad, size_t aad_len); +/** + * @brief cx_chachapoly_update_aad version which doesn't return in case of failure. + * + * See #cx_chachapoly_update_aad + */ +static inline void cx_chachapoly_update_aad_assert(cx_chachapoly_context_t *ctx, + const uint8_t *aad, + size_t aad_len) +{ + cx_err_t err = cx_chachapoly_update_aad(ctx, aad, aad_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_update_aad", err); + LEDGER_ASSERT(false, "cx_chachapoly_update_aad_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_update(cx_chachapoly_context_t *ctx, const uint8_t *input, uint8_t *output, size_t len); +/** + * @brief cx_chachapoly_update version which doesn't return in case of failure. + * + * See #cx_chachapoly_update + */ +static inline void cx_chachapoly_update_assert(cx_chachapoly_context_t *ctx, + const uint8_t *input, + uint8_t *output, + size_t len) +{ + cx_err_t err = cx_chachapoly_update(ctx, input, output, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_update", err); + LEDGER_ASSERT(false, "cx_chachapoly_update_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_finish(cx_chachapoly_context_t *ctx, uint8_t *tag, size_t tag_len); +/** + * @brief cx_chachapoly_finish version which doesn't return in case of failure. + * + * See #cx_chachapoly_finish + */ +static inline void cx_chachapoly_finish_assert(cx_chachapoly_context_t *ctx, + uint8_t *tag, + size_t tag_len) +{ + cx_err_t err = cx_chachapoly_finish(ctx, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_finish", err); + LEDGER_ASSERT(false, "cx_chachapoly_finish_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_encrypt_and_tag(cx_chachapoly_context_t *ctx, const uint8_t *input, size_t len, @@ -82,6 +164,30 @@ WARN_UNUSED_RESULT cx_err_t cx_chachapoly_encrypt_and_tag(cx_chachapoly_context_ uint8_t *tag, size_t tag_len); +/** + * @brief cx_chachapoly_encrypt_and_tag version which doesn't return in case of failure. + * + * See #cx_chachapoly_encrypt_and_tag + */ +static inline void cx_chachapoly_encrypt_and_tag_assert(cx_chachapoly_context_t *ctx, + const uint8_t *input, + size_t len, + const uint8_t *iv, + size_t iv_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *output, + uint8_t *tag, + size_t tag_len) +{ + cx_err_t err = cx_chachapoly_encrypt_and_tag( + ctx, input, len, iv, iv_len, aad, aad_len, output, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_encrypt_and_tag", err); + LEDGER_ASSERT(false, "cx_chachapoly_encrypt_and_tag_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_decrypt_and_auth(cx_chachapoly_context_t *ctx, const uint8_t *input, size_t len, @@ -93,10 +199,50 @@ WARN_UNUSED_RESULT cx_err_t cx_chachapoly_decrypt_and_auth(cx_chachapoly_context const uint8_t *tag, size_t tag_len); +/** + * @brief cx_chachapoly_decrypt_and_auth version which doesn't return in case of failure. + * + * See #cx_chachapoly_decrypt_and_auth + */ +static inline void cx_chachapoly_decrypt_and_auth_assert(cx_chachapoly_context_t *ctx, + const uint8_t *input, + size_t len, + const uint8_t *iv, + size_t iv_len, + const uint8_t *aad, + size_t aad_len, + uint8_t *output, + const uint8_t *tag, + size_t tag_len) +{ + cx_err_t err = cx_chachapoly_decrypt_and_auth( + ctx, input, len, iv, iv_len, aad, aad_len, output, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_decrypt_and_auth", err); + LEDGER_ASSERT(false, "cx_chachapoly_decrypt_and_auth_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_chachapoly_check_tag(cx_chachapoly_context_t *ctx, const uint8_t *tag, size_t tag_len); +/** + * @brief cx_chachapoly_check_tag version which doesn't return in case of failure. + * + * See #cx_chachapoly_check_tag + */ +static inline void cx_chachapoly_check_tag_assert(cx_chachapoly_context_t *ctx, + const uint8_t *tag, + size_t tag_len) +{ + cx_err_t err = cx_chachapoly_check_tag(ctx, tag, tag_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_chachapoly_check_tag", err); + LEDGER_ASSERT(false, "cx_chachapoly_check_tag_assert"); + } +} + #endif // HAVE_POLY1305 && HAVE_CHACHA #endif // HAVE_CHACHA_POLY #endif /* LCX_CHACHA_POLY_H */ diff --git a/lib_cxng/include/lcx_cipher.h b/lib_cxng/include/lcx_cipher.h index ea5f983fa..d50e3d6a8 100644 --- a/lib_cxng/include/lcx_cipher.h +++ b/lib_cxng/include/lcx_cipher.h @@ -112,6 +112,20 @@ typedef struct { */ WARN_UNUSED_RESULT cx_err_t cx_cipher_init(cx_cipher_context_t *ctx); +/** + * @brief cx_cipher_init version which doesn't return in case of failure. + * + * See #cx_cipher_init + */ +static inline void cx_cipher_init_assert(cx_cipher_context_t *ctx) +{ + cx_err_t err = cx_cipher_init(ctx); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_init", err); + LEDGER_ASSERT(false, "cx_cipher_init_assert"); + } +} + /** * @brief Initialize and fill the context structure given the cipher info. * @@ -136,6 +150,22 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_setup(cx_cipher_context_t *ctx, const cx_cipher_id_t type, uint32_t mode); +/** + * @brief cx_cipher_setup version which doesn't return in case of failure. + * + * See #cx_cipher_setup + */ +static inline void cx_cipher_setup_assert(cx_cipher_context_t *ctx, + const cx_cipher_id_t type, + uint32_t mode) +{ + cx_err_t err = cx_cipher_setup(ctx, type, mode); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_setup", err); + LEDGER_ASSERT(false, "cx_cipher_setup_assert"); + } +} + /** * @brief Set the key to use. * @@ -161,6 +191,23 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey(cx_cipher_context_t *ctx, uint32_t key_bitlen, uint32_t operation); +/** + * @brief cx_cipher_setkey version which doesn't return in case of failure. + * + * See #cx_cipher_setkey + */ +static inline void cx_cipher_setkey_assert(cx_cipher_context_t *ctx, + const uint8_t *key, + uint32_t key_bitlen, + uint32_t operation) +{ + cx_err_t err = cx_cipher_setkey(ctx, key, key_bitlen, operation); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_setkey", err); + LEDGER_ASSERT(false, "cx_cipher_setkey_assert"); + } +} + /** * @brief Set the initialization vector. * @@ -182,6 +229,22 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv(cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len); +/** + * @brief cx_cipher_setiv version which doesn't return in case of failure. + * + * See #cx_cipher_setiv + */ +static inline void cx_cipher_setiv_assert(cx_cipher_context_t *ctx, + const uint8_t *iv, + size_t iv_len) +{ + cx_err_t err = cx_cipher_setiv(ctx, iv, iv_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_setiv", err); + LEDGER_ASSERT(false, "cx_cipher_setiv_assert"); + } +} + /** * @brief Set the padding type. * @@ -202,6 +265,20 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv(cx_cipher_context_t *ctx, */ WARN_UNUSED_RESULT cx_err_t cx_cipher_set_padding(cx_cipher_context_t *ctx, uint32_t padding); +/** + * @brief cx_cipher_set_padding version which doesn't return in case of failure. + * + * See #cx_cipher_set_padding + */ +static inline void cx_cipher_set_padding_assert(cx_cipher_context_t *ctx, uint32_t padding) +{ + cx_err_t err = cx_cipher_set_padding(ctx, padding); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_set_padding", err); + LEDGER_ASSERT(false, "cx_cipher_set_padding_assert"); + } +} + /** * @brief Encrypt or decrypt with the given context. * @@ -235,6 +312,24 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_update(cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len); +/** + * @brief cx_cipher_update version which doesn't return in case of failure. + * + * See #cx_cipher_update + */ +static inline void cx_cipher_update_assert(cx_cipher_context_t *ctx, + const uint8_t *input, + size_t in_len, + uint8_t *output, + size_t *out_len) +{ + cx_err_t err = cx_cipher_update(ctx, input, in_len, output, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_update", err); + LEDGER_ASSERT(false, "cx_cipher_update_assert"); + } +} + /** * @brief Finalize the operation. * @@ -261,6 +356,22 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_finish(cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len); +/** + * @brief cx_cipher_finish version which doesn't return in case of failure. + * + * See #cx_cipher_finish + */ +static inline void cx_cipher_finish_assert(cx_cipher_context_t *ctx, + uint8_t *output, + size_t *out_len) +{ + cx_err_t err = cx_cipher_finish(ctx, output, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_finish", err); + LEDGER_ASSERT(false, "cx_cipher_finish_assert"); + } +} + /** * @brief All-in-one encryption or decryption. * @@ -297,6 +408,26 @@ WARN_UNUSED_RESULT cx_err_t cx_cipher_enc_dec(cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len); +/** + * @brief cx_cipher_enc_dec version which doesn't return in case of failure. + * + * See #cx_cipher_enc_dec + */ +static inline void cx_cipher_enc_dec_assert(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) +{ + cx_err_t err = cx_cipher_enc_dec(ctx, iv, iv_len, input, in_len, output, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cipher_enc_dec", err); + LEDGER_ASSERT(false, "cx_cipher_enc_dec_assert"); + } +} + 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); diff --git a/lib_cxng/include/lcx_cmac.h b/lib_cxng/include/lcx_cmac.h index c298f95a4..151f26162 100644 --- a/lib_cxng/include/lcx_cmac.h +++ b/lib_cxng/include/lcx_cmac.h @@ -17,12 +17,58 @@ WARN_UNUSED_RESULT cx_err_t cx_cmac_start(cx_cipher_context_t *ctx, const uint8_t *key, size_t key_bitlen); +/** + * @brief cx_cmac_start version which doesn't return in case of failure. + * + * See #cx_cmac_start + */ +static inline void cx_cmac_start_assert(cx_cipher_context_t *ctx, + const uint8_t *key, + size_t key_bitlen) +{ + cx_err_t err = cx_cmac_start(ctx, key, key_bitlen); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cmac_start", err); + LEDGER_ASSERT(false, "cx_cmac_start_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_cmac_update(cx_cipher_context_t *ctx, const uint8_t *input, size_t in_len); +/** + * @brief cx_cmac_update version which doesn't return in case of failure. + * + * See #cx_cmac_update + */ +static inline void cx_cmac_update_assert(cx_cipher_context_t *ctx, + const uint8_t *input, + size_t in_len) +{ + cx_err_t err = cx_cmac_update(ctx, input, in_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cmac_update", err); + LEDGER_ASSERT(false, "cx_cmac_update_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_cmac_finish(cx_cipher_context_t *ctx, uint8_t *output); +/** + * @brief cx_cmac_finish version which doesn't return in case of failure. + * + * See #cx_cmac_finish + */ +static inline void cx_cmac_finish_assert(cx_cipher_context_t *ctx, uint8_t *output) +{ + cx_err_t err = cx_cmac_finish(ctx, output); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cmac_finish", err); + LEDGER_ASSERT(false, "cx_cmac_finish_assert"); + } +} + WARN_UNUSED_RESULT cx_err_t cx_cmac(const cx_cipher_id_t type, const uint8_t *key, size_t key_bitlen, @@ -30,6 +76,25 @@ WARN_UNUSED_RESULT cx_err_t cx_cmac(const cx_cipher_id_t type, size_t in_len, uint8_t *output); +/** + * @brief cx_cmac version which doesn't return in case of failure. + * + * See #cx_cmac + */ +static inline void cx_cmac_assert(const cx_cipher_id_t type, + const uint8_t *key, + size_t key_bitlen, + const uint8_t *input, + size_t in_len, + uint8_t *output) +{ + cx_err_t err = cx_cmac(type, key, key_bitlen, input, in_len, output); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_cmac", err); + LEDGER_ASSERT(false, "cx_cmac_assert"); + } +} + #endif /* LCX_CMAC_H */ #endif // HAVE_CMAC diff --git a/lib_cxng/include/lcx_ecdh.h b/lib_cxng/include/lcx_ecdh.h index 46f6285e2..0f9b1d951 100644 --- a/lib_cxng/include/lcx_ecdh.h +++ b/lib_cxng/include/lcx_ecdh.h @@ -80,6 +80,25 @@ WARN_UNUSED_RESULT cx_err_t cx_ecdh_no_throw(const cx_ecfp_private_key_t *pvkey, uint8_t *secret, size_t secret_len); +/** + * @brief cx_ecdh_no_throw version which doesn't return in case of failure. + * + * See #cx_ecdh_no_throw + */ +static inline void cx_ecdh_assert(const cx_ecfp_private_key_t *pvkey, + uint32_t mode, + const uint8_t *P, + size_t P_len, + uint8_t *secret, + size_t secret_len) +{ + cx_err_t err = cx_ecdh_no_throw(pvkey, mode, P, P_len, secret, secret_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecdh_no_throw", err); + LEDGER_ASSERT(false, "cx_ecdh_assert"); + } +} + /** * @deprecated * See #cx_ecdh_no_throw @@ -134,6 +153,20 @@ DEPRECATED static inline size_t cx_ecdh(const cx_ecfp_private_key_t *pvkey, * - CX_INVALID_PARAMETER_VALUE */ WARN_UNUSED_RESULT cx_err_t cx_x25519(uint8_t *u, const uint8_t *k, size_t k_len); + +/** + * @brief cx_x25519 version which doesn't return in case of failure. + * + * See #cx_x25519 + */ +static inline void cx_x25519_assert(uint8_t *u, const uint8_t *k, size_t k_len) +{ + cx_err_t err = cx_x25519(u, k, k_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_x25519", err); + LEDGER_ASSERT(false, "cx_x25519_assert"); + } +} #endif // HAVE_X25519 #if defined(HAVE_X448) @@ -164,6 +197,20 @@ WARN_UNUSED_RESULT cx_err_t cx_x25519(uint8_t *u, const uint8_t *k, size_t k_len * - CX_INVALID_PARAMETER_VALUE */ WARN_UNUSED_RESULT cx_err_t cx_x448(uint8_t *u, const uint8_t *k, size_t k_len); + +/** + * @brief cx_x448 version which doesn't return in case of failure. + * + * See #cx_x448 + */ +static inline void cx_x448_assert(uint8_t *u, const uint8_t *k, size_t k_len) +{ + cx_err_t err = cx_x448(u, k, k_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_x448", err); + LEDGER_ASSERT(false, "cx_x448_assert"); + } +} #endif // HAVE_X448 #endif // HAVE_ECDH || HAVE_X25519 || HAVE_X448 diff --git a/lib_cxng/include/lcx_ecdsa.h b/lib_cxng/include/lcx_ecdsa.h index 9c0fff671..f796a9e9c 100644 --- a/lib_cxng/include/lcx_ecdsa.h +++ b/lib_cxng/include/lcx_ecdsa.h @@ -90,6 +90,27 @@ WARN_UNUSED_RESULT cx_err_t cx_ecdsa_sign_no_throw(const cx_ecfp_private_key_t * size_t *sig_len, uint32_t *info); +/** + * @brief cx_ecdsa_sign_no_throw version which doesn't return in case of failure. + * + * See #cx_ecdsa_sign_no_throw + */ +static inline void cx_ecdsa_sign_assert(const cx_ecfp_private_key_t *pvkey, + uint32_t mode, + cx_md_t hashID, + const uint8_t *hash, + size_t hash_len, + uint8_t *sig, + size_t *sig_len, + uint32_t *info) +{ + cx_err_t err = cx_ecdsa_sign_no_throw(pvkey, mode, hashID, hash, hash_len, sig, sig_len, info); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecdsa_sign_no_throw", err); + LEDGER_ASSERT(false, "cx_ecdsa_sign_assert"); + } +} + /** * @deprecated * See #cx_ecdsa_sign_no_throw @@ -163,6 +184,29 @@ WARN_UNUSED_RESULT cx_err_t cx_ecdsa_sign_rs_no_throw(const cx_ecfp_private_key_ uint8_t *sig_s, uint32_t *info); +/** + * @brief cx_ecdsa_sign_rs_no_throw version which doesn't return in case of failure. + * + * See #cx_ecdsa_sign_rs_no_throw + */ +static inline void cx_ecdsa_sign_rs_assert(const cx_ecfp_private_key_t *key, + uint32_t mode, + cx_md_t hashID, + const uint8_t *hash, + size_t hash_len, + size_t rs_len, + uint8_t *sig_r, + uint8_t *sig_s, + uint32_t *info) +{ + cx_err_t err + = cx_ecdsa_sign_rs_no_throw(key, mode, hashID, hash, hash_len, rs_len, sig_r, sig_s, info); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecdsa_sign_rs_no_throw", err); + LEDGER_ASSERT(false, "cx_ecdsa_sign_rs_assert"); + } +} + /** * @brief Verifies an ECDSA signature according to ECDSA specification. * diff --git a/lib_cxng/include/lcx_ecfp.h b/lib_cxng/include/lcx_ecfp.h index fb1851aa7..450004d9a 100644 --- a/lib_cxng/include/lcx_ecfp.h +++ b/lib_cxng/include/lcx_ecfp.h @@ -166,6 +166,23 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_add_point_no_throw(cx_curve_t curve, const uint8_t *P, const uint8_t *Q); +/** + * @brief cx_ecfp_add_point_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_add_point_no_throw + */ +static inline void cx_ecfp_add_point_assert(cx_curve_t curve, + uint8_t *R, + const uint8_t *P, + const uint8_t *Q) +{ + cx_err_t err = cx_ecfp_add_point_no_throw(curve, R, P, Q); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_add_point_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_add_point_assert"); + } +} + /** * @deprecated * See #cx_ecfp_add_point_no_throw @@ -216,6 +233,23 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_scalar_mult_no_throw(cx_curve_t curve, const uint8_t *k, size_t k_len); +/** + * @brief cx_ecfp_scalar_mult_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_scalar_mult_no_throw + */ +static inline void cx_ecfp_scalar_mult_assert(cx_curve_t curve, + uint8_t *P, + const uint8_t *k, + size_t k_len) +{ + cx_err_t err = cx_ecfp_scalar_mult_no_throw(curve, P, k, k_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_scalar_mult_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_scalar_mult_assert"); + } +} + /** * @deprecated * See #cx_ecfp_scalar_mult_no_throw @@ -264,6 +298,23 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_init_public_key_no_throw(cx_curve_t size_t key_len, cx_ecfp_public_key_t *key); +/** + * @brief cx_ecfp_init_public_key_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_init_public_key_no_throw + */ +static inline void cx_ecfp_init_public_key_assert(cx_curve_t curve, + const uint8_t *rawkey, + size_t key_len, + cx_ecfp_public_key_t *key) +{ + cx_err_t err = cx_ecfp_init_public_key_no_throw(curve, rawkey, key_len, key); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_init_public_key_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_init_public_key_assert"); + } +} + /** * @deprecated * See #cx_ecfp_init_public_key_no_throw @@ -302,6 +353,23 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_init_private_key_no_throw(cx_curve_t size_t key_len, cx_ecfp_private_key_t *pvkey); +/** + * @brief cx_ecfp_init_private_key_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_init_private_key_no_throw + */ +static inline void cx_ecfp_init_private_key_assert(cx_curve_t curve, + const uint8_t *rawkey, + size_t key_len, + cx_ecfp_private_key_t *pvkey) +{ + cx_err_t err = cx_ecfp_init_private_key_no_throw(curve, rawkey, key_len, pvkey); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_init_private_key_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_init_private_key_assert"); + } +} + /** * @deprecated * See #cx_ecfp_init_private_key_no_throw @@ -345,6 +413,23 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_generate_pair_no_throw(cx_curve_t cx_ecfp_private_key_t *privkey, bool keepprivate); +/** + * @brief cx_ecfp_generate_pair_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_generate_pair_no_throw + */ +static inline void cx_ecfp_generate_pair_assert(cx_curve_t curve, + cx_ecfp_public_key_t *pubkey, + cx_ecfp_private_key_t *privkey, + bool keepprivate) +{ + cx_err_t err = cx_ecfp_generate_pair_no_throw(curve, pubkey, privkey, keepprivate); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_generate_pair_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_generate_pair_assert"); + } +} + /** * @deprecated * See #cx_ecfp_generate_pair_no_throw @@ -391,6 +476,24 @@ WARN_UNUSED_RESULT cx_err_t cx_ecfp_generate_pair2_no_throw(cx_curve_t bool keepprivate, cx_md_t hashID); +/** + * @brief cx_ecfp_generate_pair2_no_throw version which doesn't return in case of failure. + * + * See #cx_ecfp_generate_pair2_no_throw + */ +static inline void cx_ecfp_generate_pair2_assert(cx_curve_t curve, + cx_ecfp_public_key_t *pubkey, + cx_ecfp_private_key_t *privkey, + bool keepprivate, + cx_md_t hashID) +{ + cx_err_t err = cx_ecfp_generate_pair2_no_throw(curve, pubkey, privkey, keepprivate, hashID); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecfp_generate_pair2_no_throw", err); + LEDGER_ASSERT(false, "cx_ecfp_generate_pair2_assert"); + } +} + /** * @deprecated * See #cx_ecfp_generate_pair2_no_throw @@ -449,6 +552,26 @@ WARN_UNUSED_RESULT cx_err_t cx_eddsa_get_public_key_no_throw(const cx_ecfp_priva uint8_t *h, size_t h_len); +/** + * @brief cx_eddsa_get_public_key_no_throw version which doesn't return in case of failure. + * + * See #cx_eddsa_get_public_key_no_throw + */ +static inline void cx_eddsa_get_public_key_assert(const cx_ecfp_private_key_t *pvkey, + cx_md_t hashID, + cx_ecfp_public_key_t *pukey, + uint8_t *a, + size_t a_len, + uint8_t *h, + size_t h_len) +{ + cx_err_t err = cx_eddsa_get_public_key_no_throw(pvkey, hashID, pukey, a, a_len, h, h_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_eddsa_get_public_key_no_throw", err); + LEDGER_ASSERT(false, "cx_eddsa_get_public_key_assert"); + } +} + /** * @deprecated * See #cx_eddsa_get_public_key_no_throw @@ -490,6 +613,20 @@ WARN_UNUSED_RESULT cx_err_t cx_edwards_compress_point_no_throw(cx_curve_t curve, uint8_t *p, size_t p_len); +/** + * @brief cx_edwards_compress_point_no_throw version which doesn't return in case of failure. + * + * See #cx_edwards_compress_point_no_throw + */ +static inline void cx_edwards_compress_point_assert(cx_curve_t curve, uint8_t *p, size_t p_len) +{ + cx_err_t err = cx_edwards_compress_point_no_throw(curve, p, p_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_edwards_compress_point_no_throw", err); + LEDGER_ASSERT(false, "cx_edwards_compress_point_assert"); + } +} + /** * @deprecated * See #cx_edwards_compress_point_no_throw @@ -527,6 +664,20 @@ WARN_UNUSED_RESULT cx_err_t cx_edwards_decompress_point_no_throw(cx_curve_t curv uint8_t *p, size_t p_len); +/** + * @brief cx_edwards_decompress_point_no_throw version which doesn't return in case of failure. + * + * See #cx_edwards_decompress_point_no_throw + */ +static inline void cx_edwards_decompress_point_assert(cx_curve_t curve, uint8_t *p, size_t p_len) +{ + cx_err_t err = cx_edwards_decompress_point_no_throw(curve, p, p_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_edwards_decompress_point_no_throw", err); + LEDGER_ASSERT(false, "cx_edwards_decompress_point_assert"); + } +} + /** * @deprecated * See #cx_edwards_decompress_point_no_throw diff --git a/lib_cxng/include/lcx_ecschnorr.h b/lib_cxng/include/lcx_ecschnorr.h index 664894dbf..8bbd2a5e0 100644 --- a/lib_cxng/include/lcx_ecschnorr.h +++ b/lib_cxng/include/lcx_ecschnorr.h @@ -79,6 +79,26 @@ WARN_UNUSED_RESULT cx_err_t cx_ecschnorr_sign_no_throw(const cx_ecfp_private_key uint8_t *sig, size_t *sig_len); +/** + * @brief cx_ecschnorr_sign_no_throw version which doesn't return in case of failure. + * + * See #cx_ecschnorr_sign_no_throw + */ +static inline void cx_ecschnorr_sign_assert(const cx_ecfp_private_key_t *pvkey, + uint32_t mode, + cx_md_t hashID, + const uint8_t *msg, + size_t msg_len, + uint8_t *sig, + size_t *sig_len) +{ + cx_err_t err = cx_ecschnorr_sign_no_throw(pvkey, mode, hashID, msg, msg_len, sig, sig_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ecschnorr_sign_no_throw", err); + LEDGER_ASSERT(false, "cx_ecschnorr_sign_assert"); + } +} + /** * @deprecated * See #cx_ecschnorr_sign_no_throw diff --git a/lib_cxng/include/lcx_eddsa.h b/lib_cxng/include/lcx_eddsa.h index bf2548684..4da10939b 100644 --- a/lib_cxng/include/lcx_eddsa.h +++ b/lib_cxng/include/lcx_eddsa.h @@ -79,6 +79,25 @@ WARN_UNUSED_RESULT cx_err_t cx_eddsa_sign_no_throw(const cx_ecfp_private_key_t * uint8_t *sig, size_t sig_len); +/** + * @brief cx_eddsa_sign_no_throw version which doesn't return in case of failure. + * + * See #cx_eddsa_sign_no_throw + */ +static inline void cx_eddsa_sign_assert(const cx_ecfp_private_key_t *pvkey, + cx_md_t hashID, + const uint8_t *hash, + size_t hash_len, + uint8_t *sig, + size_t sig_len) +{ + cx_err_t err = cx_eddsa_sign_no_throw(pvkey, hashID, hash, hash_len, sig, sig_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_eddsa_sign_no_throw", err); + LEDGER_ASSERT(false, "cx_eddsa_sign_assert"); + } +} + /** * @deprecated * See #cx_eddsa_sign_no_throw diff --git a/lib_cxng/include/lcx_groestl.h b/lib_cxng/include/lcx_groestl.h index 58ac2170a..21f01ea09 100644 --- a/lib_cxng/include/lcx_groestl.h +++ b/lib_cxng/include/lcx_groestl.h @@ -76,6 +76,20 @@ size_t cx_groestl_get_output_size(const cx_groestl_t *ctx); */ WARN_UNUSED_RESULT cx_err_t cx_groestl_init_no_throw(cx_groestl_t *hash, size_t size); +/** + * @brief cx_groestl_init_no_throw version which doesn't return in case of failure. + * + * See #cx_groestl_init_no_throw + */ +static inline void cx_groestl_init_assert(cx_groestl_t *hash, size_t size) +{ + cx_err_t err = cx_groestl_init_no_throw(hash, size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_groestl_init_no_throw", err); + LEDGER_ASSERT(false, "cx_groestl_init_assert"); + } +} + /** * @deprecated * See #cx_groestl_init_no_throw @@ -119,6 +133,25 @@ WARN_UNUSED_RESULT cx_err_t cx_groestl(cx_groestl_t *hash, uint8_t *out, size_t out_len); +/** + * @brief cx_groestl version which doesn't return in case of failure. + * + * See #cx_groestl + */ +static inline void cx_groestl_assert(cx_groestl_t *hash, + uint32_t mode, + const uint8_t *in, + size_t len, + uint8_t *out, + size_t out_len) +{ + cx_err_t err = cx_groestl(hash, mode, in, len, out, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_groestl", err); + LEDGER_ASSERT(false, "cx_groestl_assert"); + } +} + /** * @brief Adds more data to hash. * @@ -137,6 +170,20 @@ WARN_UNUSED_RESULT cx_err_t cx_groestl(cx_groestl_t *hash, */ WARN_UNUSED_RESULT cx_err_t cx_groestl_update(cx_groestl_t *ctx, const uint8_t *data, size_t len); +/** + * @brief cx_groestl_update version which doesn't return in case of failure. + * + * See #cx_groestl_update + */ +static inline void cx_groestl_update_assert(cx_groestl_t *ctx, const uint8_t *data, size_t len) +{ + cx_err_t err = cx_groestl_update(ctx, data, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_groestl_update", err); + LEDGER_ASSERT(false, "cx_groestl_update_assert"); + } +} + /** * @brief Finalizes the hash. * diff --git a/lib_cxng/include/lcx_hash.h b/lib_cxng/include/lcx_hash.h index f7cc4492b..458bda199 100644 --- a/lib_cxng/include/lcx_hash.h +++ b/lib_cxng/include/lcx_hash.h @@ -151,6 +151,25 @@ WARN_UNUSED_RESULT cx_err_t cx_hash_no_throw(cx_hash_t *hash, uint8_t *out, size_t out_len); +/** + * @brief cx_hash_no_throw version which doesn't return in case of failure. + * + * See #cx_hash_no_throw + */ +static inline void cx_hash_assert(cx_hash_t *hash, + uint32_t mode, + const uint8_t *in, + size_t len, + uint8_t *out, + size_t out_len) +{ + cx_err_t err = cx_hash_no_throw(hash, mode, in, len, out, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hash_no_throw", err); + LEDGER_ASSERT(false, "cx_hash_assert"); + } +} + /** * @deprecated * See #cx_hash_no_throw @@ -180,6 +199,20 @@ DEPRECATED static inline size_t cx_hash(cx_hash_t *hash, */ WARN_UNUSED_RESULT cx_err_t cx_hash_init(cx_hash_t *hash, cx_md_t hash_id); +/** + * @brief cx_hash_init version which doesn't return in case of failure. + * + * See #cx_hash_init + */ +static inline void cx_hash_init_assert(cx_hash_t *hash, cx_md_t hash_id) +{ + cx_err_t err = cx_hash_init(hash, hash_id); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hash_init", err); + LEDGER_ASSERT(false, "cx_hash_init_assert"); + } +} + /** * @brief Initializes a hash context. * @@ -202,6 +235,20 @@ WARN_UNUSED_RESULT cx_err_t cx_hash_init(cx_hash_t *hash, cx_md_t hash_id); */ WARN_UNUSED_RESULT cx_err_t cx_hash_init_ex(cx_hash_t *hash, cx_md_t hash_id, size_t output_size); +/** + * @brief cx_hash_init_ex version which doesn't return in case of failure. + * + * See #cx_hash_init_ex + */ +static inline void cx_hash_init_ex_assert(cx_hash_t *hash, cx_md_t hash_id, size_t output_size) +{ + cx_err_t err = cx_hash_init_ex(hash, hash_id, output_size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hash_init_ex", err); + LEDGER_ASSERT(false, "cx_hash_init_ex_assert"); + } +} + /** * @brief Adds more data to hash. * @@ -221,6 +268,20 @@ WARN_UNUSED_RESULT cx_err_t cx_hash_init_ex(cx_hash_t *hash, cx_md_t hash_id, si */ WARN_UNUSED_RESULT cx_err_t cx_hash_update(cx_hash_t *hash, const uint8_t *in, size_t in_len); +/** + * @brief cx_hash_update version which doesn't return in case of failure. + * + * See #cx_hash_update + */ +static inline void cx_hash_update_assert(cx_hash_t *hash, const uint8_t *in, size_t in_len) +{ + cx_err_t err = cx_hash_update(hash, in, in_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hash_update", err); + LEDGER_ASSERT(false, "cx_hash_update_assert"); + } +} + /** * @brief Finalizes the hash. * @@ -236,6 +297,20 @@ WARN_UNUSED_RESULT cx_err_t cx_hash_update(cx_hash_t *hash, const uint8_t *in, s */ WARN_UNUSED_RESULT cx_err_t cx_hash_final(cx_hash_t *hash, uint8_t *digest); +/** + * @brief cx_hash_final version which doesn't return in case of failure. + * + * See #cx_hash_final + */ +static inline void cx_hash_final_assert(cx_hash_t *hash, uint8_t *digest) +{ + cx_err_t err = cx_hash_final(hash, digest); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hash_final", err); + LEDGER_ASSERT(false, "cx_hash_final_assert"); + } +} + #endif // HAVE_HASH #endif // LCX_HASH_H diff --git a/lib_cxng/include/lcx_hmac.h b/lib_cxng/include/lcx_hmac.h index ffb192712..c66ec0b8e 100644 --- a/lib_cxng/include/lcx_hmac.h +++ b/lib_cxng/include/lcx_hmac.h @@ -79,6 +79,22 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_ripemd160_init_no_throw(cx_hmac_ripemd160_t const uint8_t *key, size_t key_len); +/** + * @brief cx_hmac_ripemd160_init_no_throw version which doesn't return in case of failure. + * + * See #cx_hmac_ripemd160_init_no_throw + */ +static inline void cx_hmac_ripemd160_init_assert(cx_hmac_ripemd160_t *hmac, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_hmac_ripemd160_init_no_throw(hmac, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_ripemd160_init_no_throw", err); + LEDGER_ASSERT(false, "cx_hmac_ripemd160_init_assert"); + } +} + /** * @deprecated * See #cx_hmac_ripemd160_init_no_throw @@ -125,6 +141,22 @@ typedef struct { WARN_UNUSED_RESULT cx_err_t cx_hmac_sha224_init(cx_hmac_sha256_t *hmac, const uint8_t *key, unsigned int key_len); + +/** + * @brief cx_hmac_sha224_init version which doesn't return in case of failure. + * + * See #cx_hmac_sha224_init + */ +static inline void cx_hmac_sha224_init_assert(cx_hmac_sha256_t *hmac, + const uint8_t *key, + unsigned int key_len) +{ + cx_err_t err = cx_hmac_sha224_init(hmac, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_sha224_init", err); + LEDGER_ASSERT(false, "cx_hmac_sha224_init_assert"); + } +} #endif #ifdef HAVE_SHA256 @@ -151,6 +183,22 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_sha256_init_no_throw(cx_hmac_sha256_t *hmac, const uint8_t *key, size_t key_len); +/** + * @brief cx_hmac_sha256_init_no_throw version which doesn't return in case of failure. + * + * See #cx_hmac_sha256_init_no_throw + */ +static inline void cx_hmac_sha256_init_assert(cx_hmac_sha256_t *hmac, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_hmac_sha256_init_no_throw(hmac, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_sha256_init_no_throw", err); + LEDGER_ASSERT(false, "cx_hmac_sha256_init_assert"); + } +} + /** * @deprecated * See #cx_hmac_sha256_init_no_throw @@ -224,6 +272,22 @@ typedef struct { WARN_UNUSED_RESULT cx_err_t cx_hmac_sha384_init(cx_hmac_sha512_t *hmac, const uint8_t *key, unsigned int key_len); + +/** + * @brief cx_hmac_sha384_init version which doesn't return in case of failure. + * + * See #cx_hmac_sha384_init + */ +static inline void cx_hmac_sha384_init_assert(cx_hmac_sha512_t *hmac, + const uint8_t *key, + unsigned int key_len) +{ + cx_err_t err = cx_hmac_sha384_init(hmac, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_sha384_init", err); + LEDGER_ASSERT(false, "cx_hmac_sha384_init_assert"); + } +} #endif #ifdef HAVE_SHA512 @@ -250,6 +314,22 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_sha512_init_no_throw(cx_hmac_sha512_t *hmac, const uint8_t *key, size_t key_len); +/** + * @brief cx_hmac_sha512_init_no_throw version which doesn't return in case of failure. + * + * See #cx_hmac_sha512_init_no_throw + */ +static inline void cx_hmac_sha512_init_assert(cx_hmac_sha512_t *hmac, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_hmac_sha512_init_no_throw(hmac, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_sha512_init_no_throw", err); + LEDGER_ASSERT(false, "cx_hmac_sha512_init_assert"); + } +} + /** * @deprecated * See #cx_hmac_sha512_init_no_throw @@ -328,6 +408,25 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_no_throw(cx_hmac_t *hmac, uint8_t *mac, size_t mac_len); +/** + * @brief cx_hmac_no_throw version which doesn't return in case of failure. + * + * See #cx_hmac_no_throw + */ +static inline void cx_hmac_assert(cx_hmac_t *hmac, + uint32_t mode, + const uint8_t *in, + size_t len, + uint8_t *mac, + size_t mac_len) +{ + cx_err_t err = cx_hmac_no_throw(hmac, mode, in, len, mac, mac_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_no_throw", err); + LEDGER_ASSERT(false, "cx_hmac_assert"); + } +} + /** * @deprecated * See #cx_hmac_no_throw @@ -393,6 +492,23 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_init(cx_hmac_t *hmac, const uint8_t *key, size_t key_len); +/** + * @brief cx_hmac_init version which doesn't return in case of failure. + * + * See #cx_hmac_init + */ +static inline void cx_hmac_init_assert(cx_hmac_t *hmac, + cx_md_t hash_id, + const uint8_t *key, + size_t key_len) +{ + cx_err_t err = cx_hmac_init(hmac, hash_id, key, key_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_init", err); + LEDGER_ASSERT(false, "cx_hmac_init_assert"); + } +} + /** * @brief Adds more data to compute the HMAC. * @@ -412,6 +528,20 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_init(cx_hmac_t *hmac, */ WARN_UNUSED_RESULT cx_err_t cx_hmac_update(cx_hmac_t *hmac, const uint8_t *in, size_t in_len); +/** + * @brief cx_hmac_update version which doesn't return in case of failure. + * + * See #cx_hmac_update + */ +static inline void cx_hmac_update_assert(cx_hmac_t *hmac, const uint8_t *in, size_t in_len) +{ + cx_err_t err = cx_hmac_update(hmac, in, in_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_update", err); + LEDGER_ASSERT(false, "cx_hmac_update_assert"); + } +} + /** * @brief Finalizes the HMAC algorithm. * @@ -429,6 +559,20 @@ WARN_UNUSED_RESULT cx_err_t cx_hmac_update(cx_hmac_t *hmac, const uint8_t *in, s */ WARN_UNUSED_RESULT cx_err_t cx_hmac_final(cx_hmac_t *ctx, uint8_t *out, size_t *out_len); +/** + * @brief cx_hmac_final version which doesn't return in case of failure. + * + * See #cx_hmac_final + */ +static inline void cx_hmac_final_assert(cx_hmac_t *ctx, uint8_t *out, size_t *out_len) +{ + cx_err_t err = cx_hmac_final(ctx, out, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_hmac_final", err); + LEDGER_ASSERT(false, "cx_hmac_final_assert"); + } +} + #endif // HAVE_HMAC #endif // LCX_HMAC_H diff --git a/lib_cxng/include/lcx_math.h b/lib_cxng/include/lcx_math.h index ed15a6c7c..bbe240b2b 100644 --- a/lib_cxng/include/lcx_math.h +++ b/lib_cxng/include/lcx_math.h @@ -57,6 +57,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_cmp_no_throw(const uint8_t *a, size_t length, int *diff); +/** + * @brief cx_math_cmp_no_throw version which doesn't return in case of failure. + * + * See #cx_math_cmp_no_throw + */ +static inline void cx_math_cmp_assert(const uint8_t *a, const uint8_t *b, size_t length, int *diff) +{ + cx_err_t err = cx_math_cmp_no_throw(a, b, length, diff); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_cmp_no_throw", err); + LEDGER_ASSERT(false, "cx_math_cmp_assert"); + } +} + /** * @deprecated * See #cx_math_cmp_no_throw @@ -92,6 +106,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_add_no_throw(uint8_t *r, const uint8_t *b, size_t len); +/** + * @brief cx_math_add_no_throw version which doesn't return in case of failure. + * + * See #cx_math_add_no_throw + */ +static inline void cx_math_add_assert(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len) +{ + cx_err_t err = cx_math_add_no_throw(r, a, b, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_add_no_throw", err); + LEDGER_ASSERT(false, "cx_math_add_assert"); + } +} + /** * @brief Adds two integers represented as byte arrays. * @@ -150,6 +178,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_sub_no_throw(uint8_t *r, const uint8_t *b, size_t len); +/** + * @brief cx_math_sub_no_throw version which doesn't return in case of failure. + * + * See #cx_math_sub_no_throw + */ +static inline void cx_math_sub_assert(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len) +{ + cx_err_t err = cx_math_sub_no_throw(r, a, b, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_sub_no_throw", err); + LEDGER_ASSERT(false, "cx_math_sub_assert"); + } +} + /** * @brief Subtracts two integers represented as byte arrays. * @@ -208,6 +250,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_mult_no_throw(uint8_t *r, const uint8_t *b, size_t len); +/** + * @brief cx_math_mult_no_throw version which doesn't return in case of failure. + * + * See #cx_math_mult_no_throw + */ +static inline void cx_math_mult_assert(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len) +{ + cx_err_t err = cx_math_mult_no_throw(r, a, b, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_mult_no_throw", err); + LEDGER_ASSERT(false, "cx_math_mult_assert"); + } +} + /** * @deprecated * See #cx_math_mult_no_throw @@ -247,6 +303,24 @@ DEPRECATED static inline void cx_math_mult(uint8_t *r, WARN_UNUSED_RESULT cx_err_t cx_math_addm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len); +/** + * @brief cx_math_addm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_addm_no_throw + */ +static inline void cx_math_addm_assert(uint8_t *r, + const uint8_t *a, + const uint8_t *b, + const uint8_t *m, + size_t len) +{ + cx_err_t err = cx_math_addm_no_throw(r, a, b, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_addm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_addm_assert"); + } +} + /** * @deprecated * See #cx_math_addm_no_throw @@ -287,6 +361,24 @@ DEPRECATED static inline void cx_math_addm(uint8_t *r, WARN_UNUSED_RESULT cx_err_t cx_math_subm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len); +/** + * @brief cx_math_subm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_subm_no_throw + */ +static inline void cx_math_subm_assert(uint8_t *r, + const uint8_t *a, + const uint8_t *b, + const uint8_t *m, + size_t len) +{ + cx_err_t err = cx_math_subm_no_throw(r, a, b, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_subm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_subm_assert"); + } +} + /** * @deprecated * See #cx_math_subm_no_throw @@ -330,6 +422,24 @@ WARN_UNUSED_RESULT cx_err_t cx_math_multm_no_throw(uint8_t *r, const uint8_t *m, size_t len); +/** + * @brief cx_math_multm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_multm_no_throw + */ +static inline void cx_math_multm_assert(uint8_t *r, + const uint8_t *a, + const uint8_t *b, + const uint8_t *m, + size_t len) +{ + cx_err_t err = cx_math_multm_no_throw(r, a, b, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_multm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_multm_assert"); + } +} + /** * @deprecated * See #cx_math_multm_no_throw @@ -369,6 +479,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_modm_no_throw(uint8_t *v, const uint8_t *m, size_t len_m); +/** + * @brief cx_math_modm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_modm_no_throw + */ +static inline void cx_math_modm_assert(uint8_t *v, size_t len_v, const uint8_t *m, size_t len_m) +{ + cx_err_t err = cx_math_modm_no_throw(v, len_v, m, len_m); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_modm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_modm_assert"); + } +} + /** * @deprecated * See #cx_math_modm_no_throw @@ -410,6 +534,25 @@ WARN_UNUSED_RESULT cx_err_t cx_math_powm_no_throw(uint8_t *r, const uint8_t *m, size_t len); +/** + * @brief cx_math_powm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_powm_no_throw + */ +static inline void cx_math_powm_assert(uint8_t *r, + const uint8_t *a, + const uint8_t *e, + size_t len_e, + const uint8_t *m, + size_t len) +{ + cx_err_t err = cx_math_powm_no_throw(r, a, e, len_e, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_powm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_powm_assert"); + } +} + /** * @deprecated * See #cx_math_powm_no_throw @@ -450,6 +593,23 @@ WARN_UNUSED_RESULT cx_err_t cx_math_invprimem_no_throw(uint8_t *r, const uint8_t *m, size_t len); +/** + * @brief cx_math_invprimem_no_throw version which doesn't return in case of failure. + * + * See #cx_math_invprimem_no_throw + */ +static inline void cx_math_invprimem_assert(uint8_t *r, + const uint8_t *a, + const uint8_t *m, + size_t len) +{ + cx_err_t err = cx_math_invprimem_no_throw(r, a, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_invprimem_no_throw", err); + LEDGER_ASSERT(false, "cx_math_invprimem_assert"); + } +} + /** * @deprecated * See #cx_math_invprimem_no_throw @@ -489,6 +649,20 @@ WARN_UNUSED_RESULT cx_err_t cx_math_invintm_no_throw(uint8_t *r, const uint8_t *m, size_t len); +/** + * @brief cx_math_invintm_no_throw version which doesn't return in case of failure. + * + * See #cx_math_invintm_no_throw + */ +static inline void cx_math_invintm_assert(uint8_t *r, uint32_t a, const uint8_t *m, size_t len) +{ + cx_err_t err = cx_math_invintm_no_throw(r, a, m, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_invintm_no_throw", err); + LEDGER_ASSERT(false, "cx_math_invintm_assert"); + } +} + /** * @deprecated * See #cx_math_invintm_no_throw @@ -519,6 +693,20 @@ DEPRECATED static inline void cx_math_invintm(uint8_t *r, uint32_t a, const uint */ WARN_UNUSED_RESULT cx_err_t cx_math_is_prime_no_throw(const uint8_t *r, size_t len, bool *prime); +/** + * @brief cx_math_is_prime_no_throw version which doesn't return in case of failure. + * + * See #cx_math_is_prime_no_throw + */ +static inline void cx_math_is_prime_assert(const uint8_t *r, size_t len, bool *prime) +{ + cx_err_t err = cx_math_is_prime_no_throw(r, len, prime); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_is_prime_no_throw", err); + LEDGER_ASSERT(false, "cx_math_is_prime_assert"); + } +} + /** * @deprecated * See #cx_math_is_prime_no_throw @@ -549,6 +737,20 @@ DEPRECATED static inline bool cx_math_is_prime(const uint8_t *r, size_t len) */ WARN_UNUSED_RESULT cx_err_t cx_math_next_prime_no_throw(uint8_t *r, uint32_t len); +/** + * @brief cx_math_next_prime_no_throw version which doesn't return in case of failure. + * + * See #cx_math_next_prime_no_throw + */ +static inline void cx_math_next_prime_assert(uint8_t *r, uint32_t len) +{ + cx_err_t err = cx_math_next_prime_no_throw(r, len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_math_next_prime_no_throw", err); + LEDGER_ASSERT(false, "cx_math_next_prime_assert"); + } +} + /** * @deprecated * See #cx_math_next_prime_no_throw diff --git a/lib_cxng/include/lcx_ripemd160.h b/lib_cxng/include/lcx_ripemd160.h index 83914fc92..e677d1dca 100644 --- a/lib_cxng/include/lcx_ripemd160.h +++ b/lib_cxng/include/lcx_ripemd160.h @@ -61,6 +61,20 @@ typedef struct cx_ripemd160_s cx_ripemd160_t; */ WARN_UNUSED_RESULT cx_err_t cx_ripemd160_init_no_throw(cx_ripemd160_t *hash); +/** + * @brief cx_ripemd160_init_no_throw version which doesn't return in case of failure. + * + * See #cx_ripemd160_init_no_throw + */ +static inline void cx_ripemd160_init_assert(cx_ripemd160_t *hash) +{ + cx_err_t err = cx_ripemd160_init_no_throw(hash); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_ripemd160_init_no_throw", err); + LEDGER_ASSERT(false, "cx_ripemd160_init_assert"); + } +} + /** * @brief Initializes a RIPEMD-160 context. * diff --git a/lib_cxng/include/lcx_rng.h b/lib_cxng/include/lcx_rng.h index fac437f28..e52ff2a9e 100644 --- a/lib_cxng/include/lcx_rng.h +++ b/lib_cxng/include/lcx_rng.h @@ -158,6 +158,28 @@ WARN_UNUSED_RESULT cx_err_t cx_rng_rfc6979(cx_md_t hash_id, uint8_t *out, size_t out_len); +/** + * @brief cx_rng_rfc6979 version which doesn't return in case of failure. + * + * See #cx_rng_rfc6979 + */ +static inline void cx_rng_rfc6979_assert(cx_md_t hash_id, + const uint8_t *x, + size_t x_len, + const uint8_t *h1, + size_t h1_len, + const uint8_t *q, + size_t q_len, + uint8_t *out, + size_t out_len) +{ + cx_err_t err = cx_rng_rfc6979(hash_id, x, x_len, h1, h1_len, q, q_len, out, out_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rng_rfc6979", err); + LEDGER_ASSERT(false, "cx_rng_rfc6979_assert"); + } +} + #endif // HAVE_RNG #endif // LCX_RNG_H diff --git a/lib_cxng/include/lcx_rsa.h b/lib_cxng/include/lcx_rsa.h index e9dea1ab4..59989424b 100644 --- a/lib_cxng/include/lcx_rsa.h +++ b/lib_cxng/include/lcx_rsa.h @@ -159,6 +159,25 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_init_public_key_no_throw(const uint8_t size_t modulus_len, cx_rsa_public_key_t *key); +/** + * @brief cx_rsa_init_public_key_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_init_public_key_no_throw + */ +static inline void cx_rsa_init_public_key_assert(const uint8_t *exponent, + size_t exponent_len, + const uint8_t *modulus, + size_t modulus_len, + cx_rsa_public_key_t *key) +{ + cx_err_t err + = cx_rsa_init_public_key_no_throw(exponent, exponent_len, modulus, modulus_len, key); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_init_public_key_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_init_public_key_assert"); + } +} + /** * @deprecated * See #cx_rsa_init_public_key_no_throw @@ -202,6 +221,25 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_init_private_key_no_throw(const uint8_t size_t modulus_len, cx_rsa_private_key_t *key); +/** + * @brief cx_rsa_init_private_key_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_init_private_key_no_throw + */ +static inline void cx_rsa_init_private_key_assert(const uint8_t *exponent, + size_t exponent_len, + const uint8_t *modulus, + size_t modulus_len, + cx_rsa_private_key_t *key) +{ + cx_err_t err + = cx_rsa_init_private_key_no_throw(exponent, exponent_len, modulus, modulus_len, key); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_init_private_key_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_init_private_key_assert"); + } +} + /** * @deprecated * See #cx_rsa_init_private_key_no_throw @@ -257,6 +295,26 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_generate_pair_no_throw(size_t size_t exponent_len, const uint8_t *externalPQ); +/** + * @brief cx_rsa_generate_pair_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_generate_pair_no_throw + */ +static inline void cx_rsa_generate_pair_assert(size_t modulus_len, + cx_rsa_public_key_t *public_key, + cx_rsa_private_key_t *private_key, + const uint8_t *pub_exponent, + size_t exponent_len, + const uint8_t *externalPQ) +{ + cx_err_t err = cx_rsa_generate_pair_no_throw( + modulus_len, public_key, private_key, pub_exponent, exponent_len, externalPQ); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_generate_pair_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_generate_pair_assert"); + } +} + /** * @deprecated * See #cx_rsa_generate_pair_no_throw @@ -321,6 +379,28 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_sign_with_salt_len(const cx_rsa_private_key_t size_t sig_len, size_t salt_len); +/** + * @brief cx_rsa_sign_with_salt_len version which doesn't return in case of failure. + * + * See #cx_rsa_sign_with_salt_len + */ +static inline void cx_rsa_sign_with_salt_len_assert(const cx_rsa_private_key_t *key, + uint32_t mode, + cx_md_t hashID, + const uint8_t *hash, + size_t hash_len, + uint8_t *sig, + size_t sig_len, + size_t salt_len) +{ + cx_err_t err + = cx_rsa_sign_with_salt_len(key, mode, hashID, hash, hash_len, sig, sig_len, salt_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_sign_with_salt_len", err); + LEDGER_ASSERT(false, "cx_rsa_sign_with_salt_len_assert"); + } +} + /** * @brief Computes a message digest signature according to RSA specification. * @@ -367,6 +447,26 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_sign_no_throw(const cx_rsa_private_key_t *key uint8_t *sig, size_t sig_len); +/** + * @brief cx_rsa_sign_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_sign_no_throw + */ +static inline void cx_rsa_sign_assert(const cx_rsa_private_key_t *key, + uint32_t mode, + cx_md_t hashID, + const uint8_t *hash, + size_t hash_len, + uint8_t *sig, + size_t sig_len) +{ + cx_err_t err = cx_rsa_sign_no_throw(key, mode, hashID, hash, hash_len, sig, sig_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_sign_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_sign_assert"); + } +} + /** * @deprecated * See #cx_rsa_sign_no_throw @@ -508,6 +608,26 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_encrypt_no_throw(const cx_rsa_public_key_t *k uint8_t *enc, size_t enc_len); +/** + * @brief cx_rsa_encrypt_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_encrypt_no_throw + */ +static inline void cx_rsa_encrypt_assert(const cx_rsa_public_key_t *key, + uint32_t mode, + cx_md_t hashID, + const uint8_t *mesg, + size_t mesg_len, + uint8_t *enc, + size_t enc_len) +{ + cx_err_t err = cx_rsa_encrypt_no_throw(key, mode, hashID, mesg, mesg_len, enc, enc_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_encrypt_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_encrypt_assert"); + } +} + /** * @deprecated * See #cx_rsa_encrypt_no_throw @@ -564,6 +684,26 @@ WARN_UNUSED_RESULT cx_err_t cx_rsa_decrypt_no_throw(const cx_rsa_private_key_t * uint8_t *dec, size_t *dec_len); +/** + * @brief cx_rsa_decrypt_no_throw version which doesn't return in case of failure. + * + * See #cx_rsa_decrypt_no_throw + */ +static inline void cx_rsa_decrypt_assert(const cx_rsa_private_key_t *key, + uint32_t mode, + cx_md_t hashID, + const uint8_t *mesg, + size_t mesg_len, + uint8_t *dec, + size_t *dec_len) +{ + cx_err_t err = cx_rsa_decrypt_no_throw(key, mode, hashID, mesg, mesg_len, dec, dec_len); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_rsa_decrypt_no_throw", err); + LEDGER_ASSERT(false, "cx_rsa_decrypt_assert"); + } +} + /** * @deprecated * See #cx_rsa_decrypt_no_throw diff --git a/lib_cxng/include/lcx_sha3.h b/lib_cxng/include/lcx_sha3.h index 17eee1938..7919f6e9f 100644 --- a/lib_cxng/include/lcx_sha3.h +++ b/lib_cxng/include/lcx_sha3.h @@ -70,6 +70,20 @@ typedef struct cx_sha3_s cx_sha3_t; */ WARN_UNUSED_RESULT cx_err_t cx_sha3_init_no_throw(cx_sha3_t *hash, size_t size); +/** + * @brief cx_sha3_init_no_throw version which doesn't return in case of failure. + * + * See #cx_sha3_init_no_throw + */ +static inline void cx_sha3_init_assert(cx_sha3_t *hash, size_t size) +{ + cx_err_t err = cx_sha3_init_no_throw(hash, size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_sha3_init_no_throw", err); + LEDGER_ASSERT(false, "cx_sha3_init_assert"); + } +} + /** * @deprecated * See #cx_sha3_init_no_throw @@ -101,6 +115,20 @@ DEPRECATED static inline int cx_sha3_init(cx_sha3_t *hash, size_t size) */ WARN_UNUSED_RESULT cx_err_t cx_keccak_init_no_throw(cx_sha3_t *hash, size_t size); +/** + * @brief cx_keccak_init_no_throw version which doesn't return in case of failure. + * + * See #cx_keccak_init_no_throw + */ +static inline void cx_keccak_init_assert(cx_sha3_t *hash, size_t size) +{ + cx_err_t err = cx_keccak_init_no_throw(hash, size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_keccak_init_no_throw", err); + LEDGER_ASSERT(false, "cx_keccak_init_assert"); + } +} + /** * @deprecated * See #cx_keccak_init_no_throw @@ -131,6 +159,20 @@ DEPRECATED static inline int cx_keccak_init(cx_sha3_t *hash, size_t size) */ WARN_UNUSED_RESULT cx_err_t cx_shake128_init_no_throw(cx_sha3_t *hash, size_t out_size); +/** + * @brief cx_shake128_init_no_throw version which doesn't return in case of failure. + * + * See #cx_shake128_init_no_throw + */ +static inline void cx_shake128_init_assert(cx_sha3_t *hash, size_t out_size) +{ + cx_err_t err = cx_shake128_init_no_throw(hash, out_size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_shake128_init_no_throw", err); + LEDGER_ASSERT(false, "cx_shake128_init_assert"); + } +} + /** * @deprecated * See #cx_shake128_init_no_throw @@ -161,6 +203,20 @@ DEPRECATED static inline int cx_shake128_init(cx_sha3_t *hash, unsigned int out_ */ WARN_UNUSED_RESULT cx_err_t cx_shake256_init_no_throw(cx_sha3_t *hash, size_t out_size); +/** + * @brief cx_shake256_init_no_throw version which doesn't return in case of failure. + * + * See #cx_shake256_init_no_throw + */ +static inline void cx_shake256_init_assert(cx_sha3_t *hash, size_t out_size) +{ + cx_err_t err = cx_shake256_init_no_throw(hash, out_size); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_shake256_init_no_throw", err); + LEDGER_ASSERT(false, "cx_shake256_init_assert"); + } +} + /** * @deprecated * See #cx_shake256_init_no_throw @@ -195,6 +251,20 @@ WARN_UNUSED_RESULT cx_err_t cx_sha3_xof_init_no_throw(cx_sha3_t *hash, size_t size, size_t out_length); +/** + * @brief cx_sha3_xof_init_no_throw version which doesn't return in case of failure. + * + * See #cx_sha3_xof_init_no_throw + */ +static inline void cx_sha3_xof_init_assert(cx_sha3_t *hash, size_t size, size_t out_length) +{ + cx_err_t err = cx_sha3_xof_init_no_throw(hash, size, out_length); + if (err != CX_OK) { + PRINTF("%s failed (err %d)\n", "cx_sha3_xof_init_no_throw", err); + LEDGER_ASSERT(false, "cx_sha3_xof_init_assert"); + } +} + /** * @deprecated * See #cx_sha3_xof_init_no_throw