#include "base64/base64.h"
#include "ottery.h"
#include "printf.h"
+#define XXH_INLINE_ALL
+#define XXH_PRIVATE_API
#include "xxhash.h"
#define MUM_TARGET_INDEPENDENT_HASH 1 /* For 32/64 bit equal hashes */
#include "../../contrib/mumhash/mum.h"
rspamd_cryptobox_hash_final (rspamd_cryptobox_hash_state_t *p, guchar *out)
{
crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
- _Alignof(crypto_generichash_blake2b_state));
+ RSPAMD_ALIGNOF(crypto_generichash_blake2b_state));
crypto_generichash_blake2b_final (st, out, crypto_generichash_blake2b_BYTES_MAX);
}
G_STATIC_ASSERT (sizeof (t1ha_context_t) <=
sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
+G_STATIC_ASSERT (sizeof (struct XXH3_state_s) <=
+ sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque));
struct RSPAMD_ALIGNED(16) _mum_iuf {
unsigned rem;
};
+rspamd_cryptobox_fast_hash_state_t*
+rspamd_cryptobox_fast_hash_new(void)
+{
+ rspamd_cryptobox_fast_hash_state_t *nst;
+ int ret = posix_memalign ((void **)&nst, RSPAMD_ALIGNOF(rspamd_cryptobox_fast_hash_state_t),
+ sizeof(rspamd_cryptobox_fast_hash_state_t));
+
+ if (ret != 0) {
+ abort();
+ }
+
+ return nst;
+}
+
+void
+rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st)
+{
+ free(st);
+}
+
void
rspamd_cryptobox_fast_hash_init (rspamd_cryptobox_fast_hash_state_t *st,
guint64 seed)
{
- t1ha_context_t *rst = (t1ha_context_t *)st->opaque;
- st->type = RSPAMD_CRYPTOBOX_T1HA;
- t1ha2_init (rst, seed, 0);
+ XXH3_state_t *rst = (XXH3_state_t *)st->opaque;
+ st->type = RSPAMD_CRYPTOBOX_XXHASH3;
+ XXH3_64bits_reset_withSeed (rst, seed);
}
void
XXH32_reset (xst, seed);
break;
}
+ case RSPAMD_CRYPTOBOX_XXHASH3:
+ {
+ XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
+ st->type = RSPAMD_CRYPTOBOX_XXHASH3;
+ XXH3_64bits_reset_withSeed (xst, seed);
+ break;
+ }
case RSPAMD_CRYPTOBOX_MUMHASH: {
struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
st->type = RSPAMD_CRYPTOBOX_MUMHASH;
XXH32_update (xst, data, len);
break;
}
+ case RSPAMD_CRYPTOBOX_XXHASH3:
+ {
+ XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
+ XXH3_64bits_update (xst, data, len);
+ break;
+ }
case RSPAMD_CRYPTOBOX_MUMHASH: {
struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
gsize drem = len;
ret = XXH32_digest (xst);
break;
}
+ case RSPAMD_CRYPTOBOX_XXHASH3: {
+ XXH3_state_t *xst = (XXH3_state_t *) st->opaque;
+ ret = XXH3_64bits_digest (xst);
+ break;
+ }
case RSPAMD_CRYPTOBOX_MUMHASH: {
struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque;
iuf->h = mum_hash_step (iuf->h, iuf->buf.ll);
rspamd_cryptobox_fast_hash_machdep (const void *data,
gsize len, guint64 seed)
{
- return t1ha2_atonce (data, len, seed);
+ return XXH3_64bits_withSeed(data, len, seed);
}
static inline guint64
rspamd_cryptobox_fast_hash_indep (const void *data,
gsize len, guint64 seed)
{
- return t1ha2_atonce (data, len, seed);
+ return XXH3_64bits_withSeed(data, len, seed);
}
guint64
switch (type) {
case RSPAMD_CRYPTOBOX_XXHASH32:
return XXH32 (data, len, seed);
+ case RSPAMD_CRYPTOBOX_XXHASH3:
+ return XXH3_64bits_withSeed (data, len, seed);
case RSPAMD_CRYPTOBOX_XXHASH64:
return XXH64 (data, len, seed);
case RSPAMD_CRYPTOBOX_MUMHASH:
enum rspamd_cryptobox_fast_hash_type {
RSPAMD_CRYPTOBOX_XXHASH64 = 0,
RSPAMD_CRYPTOBOX_XXHASH32,
+ RSPAMD_CRYPTOBOX_XXHASH3,
RSPAMD_CRYPTOBOX_MUMHASH,
RSPAMD_CRYPTOBOX_T1HA,
RSPAMD_CRYPTOBOX_HASHFAST,
};
/* Non crypto hash IUF interface */
-typedef struct rspamd_cryptobox_fast_hash_state_s {
- guint64 opaque[11];
+typedef struct CRYPTO_ALIGN(64) rspamd_cryptobox_fast_hash_state_s {
+ guchar opaque[576]; /* Required for xxhash3 */
enum rspamd_cryptobox_fast_hash_type type;
} rspamd_cryptobox_fast_hash_state_t;
+
+/**
+ * Creates a new cryptobox state properly aligned
+ * @return
+ */
+rspamd_cryptobox_fast_hash_state_t* rspamd_cryptobox_fast_hash_new(void);
+void rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st);
+
/**
* Init cryptobox hash state using key if needed, `st` must point to the buffer
* with at least rspamd_cryptobox_HASHSTATEBYTES bytes length. If keylen == 0, then
free (h->content.h); /* Allocated by posix_memalign */
}
else {
- g_free (h->content.fh);
+ rspamd_cryptobox_fast_hash_free (h->content.fh);
}
g_free (h);
{
h->type = LUA_CRYPTOBOX_HASH_BLAKE2;
if (posix_memalign ((void **)&h->content.h,
- _Alignof (rspamd_cryptobox_hash_state_t),
+ RSPAMD_ALIGNOF(rspamd_cryptobox_hash_state_t),
sizeof (*h->content.h)) != 0) {
g_assert_not_reached ();
}
}
else if (g_ascii_strcasecmp (type, "xxh64") == 0) {
h->type = LUA_CRYPTOBOX_HASH_XXHASH64;
- h->content.fh = g_malloc0 (sizeof (*h->content.fh));
+ h->content.fh = rspamd_cryptobox_fast_hash_new ();
rspamd_cryptobox_fast_hash_init_specific (h->content.fh,
RSPAMD_CRYPTOBOX_XXHASH64, 0);
h->out_len = sizeof (guint64);
}
else if (g_ascii_strcasecmp (type, "xxh32") == 0) {
h->type = LUA_CRYPTOBOX_HASH_XXHASH32;
- h->content.fh = g_malloc0 (sizeof (*h->content.fh));
+ h->content.fh = rspamd_cryptobox_fast_hash_new ();
rspamd_cryptobox_fast_hash_init_specific (h->content.fh,
RSPAMD_CRYPTOBOX_XXHASH32, 0);
h->out_len = sizeof (guint32);
}
else if (g_ascii_strcasecmp (type, "mum") == 0) {
h->type = LUA_CRYPTOBOX_HASH_MUM;
- h->content.fh = g_malloc0 (sizeof (*h->content.fh));
+ h->content.fh = rspamd_cryptobox_fast_hash_new ();
rspamd_cryptobox_fast_hash_init_specific (h->content.fh,
RSPAMD_CRYPTOBOX_MUMHASH, 0);
h->out_len = sizeof (guint64);
}
else if (g_ascii_strcasecmp (type, "t1ha") == 0) {
h->type = LUA_CRYPTOBOX_HASH_T1HA;
- h->content.fh = g_malloc0 (sizeof (*h->content.fh));
+ h->content.fh = rspamd_cryptobox_fast_hash_new ();
rspamd_cryptobox_fast_hash_init_specific (h->content.fh,
RSPAMD_CRYPTOBOX_T1HA, 0);
h->out_len = sizeof (guint64);