]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix keyed blake2b creation
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 24 Aug 2020 16:02:38 +0000 (17:02 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 24 Aug 2020 16:02:38 +0000 (17:02 +0100)
src/libcryptobox/cryptobox.c
src/lua/lua_cryptobox.c

index cfb1ca4611529e34c97f2f14e6443c22b1cbee90..10f9fd975d1e66ce4831d00e635832e676e1ccd5 100644 (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);
 }
 
 /**
index 66d68413b7376a8c84d2fda8d49edaa459a35140..7a7f4419b7e3bc80de2a327b8868b1109bb80b2a 100644 (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);