void
rspamd_cryptobox_hash_init (void *p, const guchar *key, gsize keylen)
{
- if (key != NULL && keylen > 0) {
- crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
- _Alignof(crypto_generichash_blake2b_state));
- crypto_generichash_blake2b_init (st, key, keylen,
- crypto_generichash_blake2b_BYTES_MAX);
- }
- else {
- crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
- _Alignof(crypto_generichash_blake2b_state));
- crypto_generichash_blake2b_init (st, key, keylen,
- crypto_generichash_blake2b_BYTES_MAX);
- }
+ crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p,
+ _Alignof(crypto_generichash_blake2b_state));
+ crypto_generichash_blake2b_init (st, key, keylen,
+ crypto_generichash_blake2b_BYTES_MAX);
}
/**
g_free (h);
}
+static inline void
+rspamd_lua_hash_init_default (struct rspamd_lua_cryptobox_hash *h,
+ const gchar *key, gsize keylen)
+{
+ h->type = LUA_CRYPTOBOX_HASH_BLAKE2;
+ if (posix_memalign ((void **)&h->content.h,
+ _Alignof (rspamd_cryptobox_hash_state_t),
+ sizeof (*h->content.h)) != 0) {
+ g_assert_not_reached ();
+ }
+
+ rspamd_cryptobox_hash_init (h->content.h, key, keylen);
+ h->out_len = rspamd_cryptobox_HASHBYTES;
+}
+
static struct rspamd_lua_cryptobox_hash *
-rspamd_lua_hash_create (const gchar *type)
+rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen)
{
struct rspamd_lua_cryptobox_hash *h;
h->out_len = EVP_MD_size (EVP_sha384 ());
EVP_DigestInit (h->content.c, EVP_sha384 ());
}
- else if (g_ascii_strcasecmp (type, "blake2") == 0) {
- h->type = LUA_CRYPTOBOX_HASH_BLAKE2;
- (void) !posix_memalign ((void **)&h->content.h, _Alignof (rspamd_cryptobox_hash_state_t),
- sizeof (*h->content.h));
- g_assert (h->content.h != NULL);
- h->out_len = rspamd_cryptobox_HASHBYTES;
- rspamd_cryptobox_hash_init (h->content.h, NULL, 0);
- }
else if (g_ascii_strcasecmp (type, "xxh64") == 0) {
h->type = LUA_CRYPTOBOX_HASH_XXHASH64;
h->content.fh = g_malloc0 (sizeof (*h->content.fh));
RSPAMD_CRYPTOBOX_T1HA, 0);
h->out_len = sizeof (guint64);
}
+ else if (g_ascii_strcasecmp (type, "blake2") == 0) {
+ rspamd_lua_hash_init_default (h, key, keylen);
+ }
else {
g_free (h);
}
else {
/* Default hash type */
- h->type = LUA_CRYPTOBOX_HASH_BLAKE2;
- (void) !posix_memalign ((void **)&h->content.h, _Alignof (rspamd_cryptobox_hash_state_t),
- sizeof (*h->content.h));
- g_assert (h->content.h != NULL);
- rspamd_cryptobox_hash_init (h->content.h, NULL, 0);
- h->out_len = rspamd_cryptobox_HASHBYTES;
+ rspamd_lua_hash_init_default (h, key, keylen);
}
return h;
struct rspamd_lua_text *t;
gsize len = 0;
- h = rspamd_lua_hash_create (NULL);
+ h = rspamd_lua_hash_create (NULL, NULL, 0);
if (lua_type (L, 1) == LUA_TSTRING) {
s = lua_tolstring (L, 1, &len);
return luaL_error (L, "invalid arguments");
}
- h = rspamd_lua_hash_create (type);
+ h = rspamd_lua_hash_create (type, NULL, 0);
if (h == NULL) {
return luaL_error (L, "invalid hash type: %s", type);
key = luaL_checklstring (L, 1, &keylen);
if (key != NULL) {
- h = rspamd_lua_hash_create (NULL);
- rspamd_cryptobox_hash_init (h->content.h, key, keylen);
+ h = rspamd_lua_hash_create (NULL, key, keylen);
if (lua_type (L, 2) == LUA_TSTRING) {
s = lua_tolstring (L, 2, &len);