Browse Source

[Minor] Fix keyed blake2b creation

tags/2.6
Vsevolod Stakhov 3 years ago
parent
commit
f40d6f8502
2 changed files with 27 additions and 31 deletions
  1. 4
    12
      src/libcryptobox/cryptobox.c
  2. 23
    19
      src/lua/lua_cryptobox.c

+ 4
- 12
src/libcryptobox/cryptobox.c View File

@@ -1437,18 +1437,10 @@ rspamd_cryptobox_mac_bytes (enum rspamd_cryptobox_mode mode)
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);
}

/**

+ 23
- 19
src/lua/lua_cryptobox.c View File

@@ -998,8 +998,23 @@ lua_cryptobox_hash_dtor (struct rspamd_lua_cryptobox_hash *h)
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;

@@ -1046,14 +1061,6 @@ rspamd_lua_hash_create (const gchar *type)
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));
@@ -1082,6 +1089,9 @@ rspamd_lua_hash_create (const gchar *type)
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);

@@ -1090,12 +1100,7 @@ rspamd_lua_hash_create (const gchar *type)
}
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;
@@ -1116,7 +1121,7 @@ lua_cryptobox_hash_create (lua_State *L)
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);
@@ -1163,7 +1168,7 @@ lua_cryptobox_hash_create_specific (lua_State *L)
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);
@@ -1213,8 +1218,7 @@ lua_cryptobox_hash_create_keyed (lua_State *L)
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);

Loading…
Cancel
Save