libhasha 2.2.2
Loading...
Searching...
No Matches
evp.h
Go to the documentation of this file.
1
15
16#ifndef __HASHA_EVP_H
17#define __HASHA_EVP_H
18
19#include "./internal/feature.h"
20#include "./internal/hadefs.h"
21#include "./internal/internal.h"
22
26#define ha_ctx(hash) ha_##hash##_context
27
32#define ha_init_fun(hash) ha_##hash##_init
33#define ha_update_fun(hash) ha_##hash##_update
34#define ha_final_fun(hash) ha_##hash##_final
35#define ha_hash_fun(hash) ha_##hash##_hash
36
40#define ha_init(hash, ctx) ha_##hash##_init(ctx)
41
45#define ha_update(hash, ctx, buf, buflen) \
46 ha_##hash##_update(ctx, buf, buflen)
47
51#define ha_final(hash, ctx, ...) ha_##hash##_final(ctx, ##__VA_ARGS__)
52
56#define ha_hash(hash, buf, buflen, digest, ...) \
57 ha_##hash##_hash(buf, buflen, digest, ##__VA_ARGS__)
58
59#ifdef HA_ADA
63#define ha_ada_hash(hash, buf, len, digest, ...) \
64 do { \
65 ha_ctx(hash) ctx; \
66 ha_init(hash, &ctx); \
67 ha_update(hash, &ctx, buf, len); \
68 ha_final(hash, &ctx, digest, ##__VA_ARGS__); \
69 } while (0)
70
74#define ha_ada_stream_hash(hash, stream, size, chunksize, buffer, digest, \
75 ...) \
76 do { \
77 ha_ctx(hash) ctx; \
78 ha_init(hash, &ctx); \
79 size_t bytes; \
80 while ((bytes = fread((buffer), (size), (chunksize), (stream))) > 0) \
81 { \
82 ha_update(hash, &ctx, buffer, bytes); \
83 } \
84 ha_final(hash, &ctx, digest, ##__VA_ARGS__); \
85 } while (0)
86
90#ifdef HA_ADA_COMPATIBILITY
91#define ha_buffer_digest(...) ha_ada_hash(__VA_ARGS__)
92#define ha_stream_digest(...) ha_ada_stream_hash(__VA_ARGS__)
93#endif
94#endif
95
96#if ha_has_feature(EVP)
97
106enum ha_evp_hashty ha_enum_base(uint8_t)
107{
108 HA_EVPTY_UNDEFINED,
109 HA_EVPTY_BLAKE2B,
110 HA_EVPTY_BLAKE2S,
111 HA_EVPTY_BLAKE3,
112 HA_EVPTY_KECCAK,
113 HA_EVPTY_MD5,
114 HA_EVPTY_SHA1,
115 HA_EVPTY_SHA2,
116 HA_EVPTY_SHA3,
117};
118
119enum ha_enum_base(int8_t)
120{
121 /* constant: i8 */
122 HA_EVPTY_SIZE_DYNAMIC = -1,
123};
124
128extern const size_t g_ha_evp_hasher_size;
129
133typedef struct ha_evp_hasher ha_evp_hasher_t, *ha_evp_phasher_t;
134
136
141void ha_evp_put(struct ha_evp_hasher *hasher, ha_cdigest_t digest, const char *end);
142
147void ha_evp_fput(struct ha_evp_hasher *hasher, FILE *stream, ha_cdigest_t digest, const char *end);
148
155signed long ha_evp_hashty_get_digestlen(enum ha_evp_hashty hashty);
156
159const char *ha_evp_hashty_tostr(enum ha_evp_hashty hashty);
160
161/* Used before initialization (ha_evp_setup_hasher) of hasher */
162
167void ha_evp_hasher_set_keccak_rate(struct ha_evp_hasher *hasher,
168 uint16_t rate);
173size_t ha_evp_hasher_keccak_rate(struct ha_evp_hasher *hasher);
174
179void ha_evp_hasher_set_keccak_custom(struct ha_evp_hasher *hasher,
180 bool custom);
181
186bool ha_evp_hasher_keccak_custom(struct ha_evp_hasher *hasher);
187
193size_t ha_evp_hasher_ctxsize(struct ha_evp_hasher *hasher);
194
200enum ha_evp_hashty ha_evp_hasher_hashty(struct ha_evp_hasher *hasher);
201
206void ha_evp_hasher_set_hashty(struct ha_evp_hasher *hasher,
207 enum ha_evp_hashty hashty);
208
214size_t ha_evp_hasher_digestlen(struct ha_evp_hasher *hasher);
215
220void ha_evp_hasher_set_digestlen(struct ha_evp_hasher *hasher,
221 size_t digestlen);
222
231HA_PUBFUN struct ha_evp_hasher *ha_evp_hasher_new(void);
232
241HA_PUBFUN void ha_evp_hasher_delete(struct ha_evp_hasher *ptr);
242
254HA_PUBFUN void ha_evp_hasher_init(struct ha_evp_hasher *hasher,
255 enum ha_evp_hashty hashty,
256 size_t digestlen);
257
265HA_PUBFUN void ha_evp_hasher_cleanup(struct ha_evp_hasher *hasher);
266
278HA_PUBFUN void ha_evp_hasher_reinit(struct ha_evp_hasher *hasher,
279 enum ha_evp_hashty hashty,
280 size_t digestlen);
281
287HA_PUBFUN void ha_evp_hasher_commit(struct ha_evp_hasher *hasher);
288
297HA_PUBFUN void ha_evp_init(struct ha_evp_hasher *hasher);
298
309HA_PUBFUN void ha_evp_update(struct ha_evp_hasher *hasher, ha_inbuf_t buf,
310 size_t len);
311
323HA_PUBFUN void ha_evp_final(struct ha_evp_hasher *hasher,
324 ha_digest_t digest);
325
339HA_PUBFUN void ha_evp_hash(struct ha_evp_hasher *hasher, ha_inbuf_t buf,
340 size_t len, ha_digest_t digest);
341
356void ha_evp_digest(struct ha_evp_hasher *hasher, ha_inbuf_t buf,
357 size_t len, ha_digest_t digest);
358
360
361#endif /* ha_has_feature(EVP) */
362
363#endif
HA_PUBFUN void ha_evp_fput(struct ha_evp_hasher *hasher, FILE *stream, ha_cdigest_t digest, const char *end)
Put hash to FILE *.
HA_PUBFUN void ha_evp_init(struct ha_evp_hasher *hasher)
Initializes the EVP hash. ( like ha_init(hash, ctx) )
HA_PUBFUN enum ha_evp_hashty ha_evp_hasher_hashty(struct ha_evp_hasher *hasher)
Getter for ha_evp_hasher ctx_hashty field.
HA_PUBFUN struct ha_evp_hasher * ha_evp_hasher_new(void)
Creates a new EVP hasher. ( malloc(g_ha_evp_hasher_size) )
HA_PUBFUN size_t ha_evp_hasher_ctxsize(struct ha_evp_hasher *hasher)
Getter for ha_evp_hasher ctx_size field.
HA_PUBFUN void ha_evp_hasher_set_keccak_custom(struct ha_evp_hasher *hasher, bool custom)
Setter for ha_evp_hasher kustom field.
HA_PUBFUN void ha_evp_hasher_cleanup(struct ha_evp_hasher *hasher)
Cleans up the internal state of the EVP hasher.
HA_PUBFUN void ha_evp_hasher_set_hashty(struct ha_evp_hasher *hasher, enum ha_evp_hashty hashty)
Setter for ha_evp_hasher ctx_hashty field.
HA_PUBFUN void ha_evp_hasher_delete(struct ha_evp_hasher *ptr)
Frees the memory of an EVP hasher.
const size_t g_ha_evp_hasher_size
Size of the EVP hasher structure.
HA_PUBFUN size_t ha_evp_hasher_keccak_rate(struct ha_evp_hasher *hasher)
Getter for ha_evp_hasher krate field.
HA_PUBFUN void ha_evp_hasher_set_digestlen(struct ha_evp_hasher *hasher, size_t digestlen)
Setter for ha_evp_hasher ctx_digestlen field.
HA_PUBFUN void ha_evp_hasher_set_keccak_rate(struct ha_evp_hasher *hasher, uint16_t rate)
Setter for ha_evp_hasher krate field.
HA_PUBFUN void ha_evp_hasher_init(struct ha_evp_hasher *hasher, enum ha_evp_hashty hashty, size_t digestlen)
Initializes the EVP hasher for a specific algorithm and digest length.
HA_PUBFUN void ha_evp_digest(struct ha_evp_hasher *hasher, ha_inbuf_t buf, size_t len, ha_digest_t digest)
Computes the EVP hash in a init, update, final operation. ( like ha_ada_hash(hash,...
HA_PUBFUN bool ha_evp_hasher_keccak_custom(struct ha_evp_hasher *hasher)
Getter for ha_evp_hasher kustom field.
HA_PUBFUN const char * ha_evp_hashty_tostr(enum ha_evp_hashty hashty)
Returns g_ha_evp_hashty_strings[hashty] (with error handling)
HA_PUBFUN void ha_evp_hash(struct ha_evp_hasher *hasher, ha_inbuf_t buf, size_t len, ha_digest_t digest)
Computes the EVP hash in a single (hash) operation. ( like ha_hash(hash, buf, len,...
HA_PUBFUN void ha_evp_hasher_commit(struct ha_evp_hasher *hasher)
Reinitializes the EVP hasher.
HA_EXTERN_C_BEG HA_PUBFUN void ha_evp_put(struct ha_evp_hasher *hasher, ha_cdigest_t digest, const char *end)
Put hash to stdout.
HA_PUBFUN signed long ha_evp_hashty_get_digestlen(enum ha_evp_hashty hashty)
Get fixed hash size.
HA_PUBFUN void ha_evp_update(struct ha_evp_hasher *hasher, ha_inbuf_t buf, size_t len)
Updates the EVP hash with input data. ( like ha_update(hash, ctx, buf, len) )
HA_PUBFUN void ha_evp_hasher_reinit(struct ha_evp_hasher *hasher, enum ha_evp_hashty hashty, size_t digestlen)
Reinitializes the EVP hasher with a new algorithm and digest length.
struct ha_evp_hasher * ha_evp_phasher_t
Definition evp.h:133
HA_PUBFUN void ha_evp_final(struct ha_evp_hasher *hasher, ha_digest_t digest)
Finalizes the EVP hash and produces the output digest. ( like ha_final(hash, ctx, digest,...
HA_PUBFUN size_t ha_evp_hasher_digestlen(struct ha_evp_hasher *hasher)
Getter for ha_evp_hasher ctx_digestlen field.
struct ha_evp_hasher ha_evp_hasher_t
Opaque structure for the EVP hasher state.
Definition evp.h:133
#define ha_enum_base(T)
Definition hadefs.h:97
#define HA_PUBFUN
Definition hadefs.h:34
#define HA_EXTERN_C_END
Definition hadefs.h:78
#define HA_EXTERN_C_BEG
Definition hadefs.h:70
const __ha_out_buf_type ha_cdigest_t
Definition types.h:16
__ha_out_buf_type ha_digest_t
Definition types.h:15
__ha_in_buf_type ha_inbuf_t
Definition types.h:12