]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Pre-calculate blake2 digest for all parts
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 13 Aug 2016 10:22:44 +0000 (11:22 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 13 Aug 2016 10:23:12 +0000 (11:23 +0100)
src/libmime/images.c
src/libmime/images.h
src/libmime/message.c
src/libmime/message.h
src/plugins/fuzzy_check.c

index 5a556ce9c068763f438b8e36ef1708e493d8f814..f2c27f8652f074ecda56e0d7f6f81f26e9c3ff91 100644 (file)
@@ -226,6 +226,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
                        img->width, img->height,
                        task->message_id);
                img->filename = part->filename;
+               img->parent = part;
                part->flags |= RSPAMD_MIME_PART_IMAGE;
                part->specific_data = img;
 
index aa63d92e2cc237d9ea77c8cc2d8a8ab42e32f0ca..73e00aebd34e8937acf8c95bbef8abe36ee56713 100644 (file)
@@ -5,6 +5,7 @@
 
 struct html_image;
 struct rspamd_task;
+struct rspamd_mime_part;
 
 enum rspamd_image_type {
        IMAGE_TYPE_PNG = 0,
@@ -15,12 +16,13 @@ enum rspamd_image_type {
 };
 
 struct rspamd_image {
-       enum rspamd_image_type type;
+       struct rspamd_mime_part *parent;
        GByteArray *data;
-       guint32 width;
-       guint32 height;
        const gchar *filename;
        struct html_image *html_image;
+       enum rspamd_image_type type;
+       guint32 width;
+       guint32 height;
 };
 
 /*
index a3b673f28452ea2dacb96dcc9e8c276ba50ecfa8..f97de592f7db9313b6591779ddcec35e4e54fbe9 100644 (file)
@@ -1061,6 +1061,17 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
        GMimeStream *part_stream;
        GByteArray *part_content;
        gchar *hdrs;
+       /* Blake2b applied to string 'rspamd' */
+       static const guchar hash_key[] = {
+                       0x78,0x6a,0x02,0xf7,0x42,0x01,0x59,0x03,
+                       0xc6,0xc6,0xfd,0x85,0x25,0x52,0xd2,0x72,
+                       0x91,0x2f,0x47,0x40,0xe1,0x58,0x47,0x61,
+                       0x8a,0x86,0xe2,0x17,0xf7,0x1f,0x54,0x19,
+                       0xd2,0x5e,0x10,0x31,0xaf,0xee,0x58,0x53,
+                       0x13,0x89,0x64,0x44,0x93,0x4e,0xb0,0x4b,
+                       0x90,0x3a,0x68,0x5b,0x14,0x48,0xb7,0x55,
+                       0xd5,0x6f,0x70,0x1a,0xfe,0x9b,0xe2,0xce
+       };
 
        task = md->task;
        /* 'part' points to the current part node that g_mime_message_foreach_part() is iterating over */
@@ -1139,6 +1150,11 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
                mime_part->mime = part;
                mime_part->boundary = g_mime_multipart_get_boundary (GMIME_MULTIPART (part));
 
+               if (mime_part->boundary) {
+                       rspamd_cryptobox_hash (mime_part->digest, mime_part->boundary,
+                                       strlen (mime_part->boundary), hash_key, sizeof (hash_key));
+               }
+
                debug_task ("found part with content-type: %s/%s",
                                type->type,
                                type->subtype);
@@ -1203,6 +1219,12 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
                                                        part));
                                mime_part->mime = part;
 
+                               if (mime_part->content->len > 0) {
+                                       rspamd_cryptobox_hash (mime_part->digest,
+                                                       mime_part->content->data, mime_part->content->len,
+                                                       hash_key, sizeof (hash_key));
+                               }
+
                                debug_task ("found part with content-type: %s/%s",
                                        type->type,
                                        type->subtype);
index 3fe26e685650a8f8ef1a2734c9fbe0efcd8e0d97..b6f7062dc59cfec401623a9a85a49cc47020a9f2 100644 (file)
@@ -9,6 +9,7 @@
 #include "config.h"
 #include "email_addr.h"
 #include "addr.h"
+#include "cryptobox.h"
 #include <gmime/gmime.h>
 
 struct rspamd_task;
@@ -29,11 +30,11 @@ struct rspamd_mime_part {
        GMimeObject *mime;
        GHashTable *raw_headers;
        gchar *raw_headers_str;
-       gchar *checksum;
+       guchar digest[rspamd_cryptobox_HASHBYTES];
        const gchar *filename;
        const gchar *boundary;
-       enum rspamd_mime_part_flags flags;
        gpointer specific_data;
+       enum rspamd_mime_part_flags flags;
 };
 
 #define RSPAMD_MIME_TEXT_PART_FLAG_UTF (1 << 0)
index 82800e714442161dfff33f9e84729ee63c17d756..429360e1cc71e1047bf85f02cccffd5f86575665 100644 (file)
@@ -1288,13 +1288,11 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule,
                gint flag,
                guint32 weight,
                rspamd_mempool_t *pool,
-               const guchar *data,
-               gsize datalen)
+               guchar digest[rspamd_cryptobox_HASHBYTES])
 {
        struct rspamd_fuzzy_cmd *cmd;
        struct rspamd_fuzzy_encrypted_cmd *enccmd = NULL;
        struct fuzzy_cmd_io *io;
-       rspamd_cryptobox_hash_state_t st;
 
        if (rule->peer_key) {
                enccmd = rspamd_mempool_alloc0 (pool, sizeof (*enccmd));
@@ -1312,10 +1310,7 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule,
        }
        cmd->shingles_count = 0;
        cmd->tag = ottery_rand_uint32 ();
-       /* Use blake2b for digest */
-       rspamd_cryptobox_hash_init (&st, rule->hash_key->str, rule->hash_key->len);
-       rspamd_cryptobox_hash_update (&st, data, datalen);
-       rspamd_cryptobox_hash_final (&st, cmd->digest);
+       memcpy (cmd->digest, digest, sizeof (cmd->digest));
 
        io = rspamd_mempool_alloc (pool, sizeof (*io));
        io->flags = 0;
@@ -2059,7 +2054,7 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule,
                                                        fuzzy_module_ctx->min_width) {
                                                io = fuzzy_cmd_from_data_part (rule, c, flag, value,
                                                                task->task_pool,
-                                                               image->data->data, image->data->len);
+                                                               image->parent->digest);
                                                if (io) {
                                                        g_ptr_array_add (res, io);
                                                }
@@ -2074,7 +2069,7 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule,
                                fuzzy_module_ctx->min_bytes) {
                                io = fuzzy_cmd_from_data_part (rule, c, flag, value,
                                                task->task_pool,
-                                               mime_part->content->data, mime_part->content->len);
+                                               mime_part->digest);
                                if (io) {
                                        g_ptr_array_add (res, io);
                                }