diff --git a/lib_cxng/include/lcx_hash.h b/lib_cxng/include/lcx_hash.h index 3320a3995..93b57fe3f 100644 --- a/lib_cxng/include/lcx_hash.h +++ b/lib_cxng/include/lcx_hash.h @@ -41,6 +41,7 @@ #ifdef HAVE_HASH +#include "ledger_assert.h" #include "cx_errors.h" #include "lcx_wrappers.h" #include "lcx_common.h" @@ -150,6 +151,21 @@ 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) +{ + LEDGER_ASSERT(cx_hash_no_throw(hash, mode, in, len, out, out_len) == CX_OK, "cx_hash_no_throw"); +} + /** * @deprecated * See #cx_hash_no_throw @@ -179,6 +195,16 @@ 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) +{ + LEDGER_ASSERT(cx_hash_init(hash, hash_id) == CX_OK, "cx_hash_init"); +} + /** * @brief Initializes a hash context. * @@ -201,6 +227,16 @@ 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) +{ + LEDGER_ASSERT(cx_hash_init_ex(hash, hash_id, output_size) == CX_OK, "cx_hash_init_ex"); +} + /** * @brief Adds more data to hash. * @@ -220,6 +256,16 @@ 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) +{ + LEDGER_ASSERT(cx_hash_update(hash, in, in_len) == CX_OK, "cx_hash_update"); +} + /** * @brief Finalizes the hash. * @@ -235,6 +281,16 @@ 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) +{ + LEDGER_ASSERT(cx_hash_final(hash, digest) == CX_OK, "cx_hash_final"); +} + #endif // HAVE_HASH #endif // LCX_HASH_H