commit f40d6f8: [Minor] Fix keyed blake2b creation

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Aug 24 16:07:11 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-08-24 17:02:38 +0100
URL: https://github.com/rspamd/rspamd/commit/f40d6f8502561b84d71045378cab86cc4f462584 (HEAD -> master)

[Minor] Fix keyed blake2b creation

---
 src/libcryptobox/cryptobox.c | 16 ++++------------
 src/lua/lua_cryptobox.c      | 42 +++++++++++++++++++++++-------------------
 2 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c
index cfb1ca461..10f9fd975 100644
--- a/src/libcryptobox/cryptobox.c
+++ b/src/libcryptobox/cryptobox.c
@@ -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);
 }
 
 /**
diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c
index 66d68413b..7a7f4419b 100644
--- a/src/lua/lua_cryptobox.c
+++ b/src/lua/lua_cryptobox.c
@@ -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);


More information about the Commits mailing list