19 #include "app_config.h"
36 #include "exceptions.h"
38 const cx_hash_info_t *cx_hash_get_info(cx_md_t md_type)
43 return &cx_sha256_info;
48 return &cx_ripemd160_info;
53 return &cx_sha224_info;
58 return &cx_sha384_info;
63 return &cx_sha512_info;
72 return &cx_keccak_info;
74 return &cx_shake128_info;
76 return &cx_shake256_info;
81 return &cx_blake2b_info;
89 size_t cx_hash_get_size(
const cx_hash_t *ctx)
91 const cx_hash_info_t *info = ctx->info;
92 if (info->output_size) {
93 return info->output_size;
95 return info->output_size_func(ctx);
98 cx_err_t cx_hash_init(cx_hash_t *ctx, cx_md_t md_type)
100 const cx_hash_info_t *info = cx_hash_get_info(md_type);
102 return CX_INVALID_PARAMETER;
104 if (info->output_size == 0) {
105 return CX_INVALID_PARAMETER;
107 info->init_func(ctx);
111 cx_err_t cx_hash_init_ex(cx_hash_t *ctx, cx_md_t md_type,
size_t output_size)
113 const cx_hash_info_t *info = cx_hash_get_info(md_type);
115 return CX_INVALID_PARAMETER;
117 if (info->output_size != 0) {
118 if (info->output_size != output_size) {
119 return CX_INVALID_PARAMETER;
121 return cx_hash_init(ctx, md_type);
123 return info->init_ex_func(ctx, output_size * 8);
126 cx_err_t cx_hash_update(cx_hash_t *ctx,
const uint8_t *data,
size_t len)
128 const cx_hash_info_t *info = ctx->info;
129 return info->update_func(ctx, data, len);
132 cx_err_t cx_hash_final(cx_hash_t *ctx,
uint8_t *digest)
134 const cx_hash_info_t *info = ctx->info;
135 return info->finish_func(ctx, digest);
138 cx_err_t cx_hash_no_throw(cx_hash_t *hash,
145 unsigned int digest_len;
149 digest_len = (
unsigned int) cx_hash_get_size(hash);
150 CX_CHECK(cx_hash_update(hash, in, len));
153 if (out_len < digest_len) {
154 return INVALID_PARAMETER;
156 CX_CHECK(cx_hash_final(hash, out));
163 void cx_hash_destroy(cx_hash_t *hash_ctx)
165 explicit_bzero(hash_ctx, hash_ctx->info->ctx_size);