You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shingles.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "shingles.h"
  17. #include "fstring.h"
  18. #include "cryptobox.h"
  19. #include "images.h"
  20. #include "libstat/stat_api.h"
  21. #define SHINGLES_WINDOW 3
  22. #define SHINGLES_KEY_SIZE rspamd_cryptobox_SIPKEYBYTES
  23. static guint
  24. rspamd_shingles_keys_hash(gconstpointer k)
  25. {
  26. return rspamd_cryptobox_fast_hash(k, SHINGLES_KEY_SIZE,
  27. rspamd_hash_seed());
  28. }
  29. static gboolean
  30. rspamd_shingles_keys_equal(gconstpointer k1, gconstpointer k2)
  31. {
  32. return (memcmp(k1, k2, SHINGLES_KEY_SIZE) == 0);
  33. }
  34. static void
  35. rspamd_shingles_keys_free(gpointer p)
  36. {
  37. guchar **k = p;
  38. guint i;
  39. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  40. g_free(k[i]);
  41. }
  42. g_free(k);
  43. }
  44. static guchar **
  45. rspamd_shingles_keys_new(void)
  46. {
  47. guchar **k;
  48. guint i;
  49. k = g_malloc0(sizeof(guchar *) * RSPAMD_SHINGLE_SIZE);
  50. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  51. k[i] = g_malloc0(sizeof(guchar) * SHINGLES_KEY_SIZE);
  52. }
  53. return k;
  54. }
  55. static guchar **
  56. rspamd_shingles_get_keys_cached(const guchar key[SHINGLES_KEY_SIZE])
  57. {
  58. static GHashTable *ht = NULL;
  59. guchar **keys = NULL, *key_cpy;
  60. rspamd_cryptobox_hash_state_t bs;
  61. const guchar *cur_key;
  62. guchar shabuf[rspamd_cryptobox_HASHBYTES], *out_key;
  63. guint i;
  64. if (ht == NULL) {
  65. ht = g_hash_table_new_full(rspamd_shingles_keys_hash,
  66. rspamd_shingles_keys_equal, g_free, rspamd_shingles_keys_free);
  67. }
  68. else {
  69. keys = g_hash_table_lookup(ht, key);
  70. }
  71. if (keys == NULL) {
  72. keys = rspamd_shingles_keys_new();
  73. key_cpy = g_malloc(SHINGLES_KEY_SIZE);
  74. memcpy(key_cpy, key, SHINGLES_KEY_SIZE);
  75. /* Generate keys */
  76. rspamd_cryptobox_hash_init(&bs, NULL, 0);
  77. cur_key = key;
  78. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  79. /*
  80. * To generate a set of hashes we just apply sha256 to the
  81. * initial key as many times as many hashes are required and
  82. * xor left and right parts of sha256 to get a single 16 bytes SIP key.
  83. */
  84. out_key = keys[i];
  85. rspamd_cryptobox_hash_update(&bs, cur_key, 16);
  86. rspamd_cryptobox_hash_final(&bs, shabuf);
  87. memcpy(out_key, shabuf, 16);
  88. rspamd_cryptobox_hash_init(&bs, NULL, 0);
  89. cur_key = out_key;
  90. }
  91. g_hash_table_insert(ht, key_cpy, keys);
  92. }
  93. return keys;
  94. }
  95. struct rspamd_shingle *RSPAMD_OPTIMIZE("unroll-loops")
  96. rspamd_shingles_from_text(GArray *input,
  97. const guchar key[16],
  98. rspamd_mempool_t *pool,
  99. rspamd_shingles_filter filter,
  100. gpointer filterd,
  101. enum rspamd_shingle_alg alg)
  102. {
  103. struct rspamd_shingle *res;
  104. uint64_t **hashes;
  105. guchar **keys;
  106. rspamd_fstring_t *row;
  107. rspamd_stat_token_t *word;
  108. uint64_t val;
  109. gint i, j, k;
  110. gsize hlen, ilen = 0, beg = 0, widx = 0;
  111. enum rspamd_cryptobox_fast_hash_type ht;
  112. if (pool != NULL) {
  113. res = rspamd_mempool_alloc(pool, sizeof(*res));
  114. }
  115. else {
  116. res = g_malloc(sizeof(*res));
  117. }
  118. row = rspamd_fstring_sized_new(256);
  119. for (i = 0; i < input->len; i++) {
  120. word = &g_array_index(input, rspamd_stat_token_t, i);
  121. if (!((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0)) {
  122. ilen++;
  123. }
  124. }
  125. /* Init hashes pipes and keys */
  126. hashes = g_malloc(sizeof(*hashes) * RSPAMD_SHINGLE_SIZE);
  127. hlen = ilen > SHINGLES_WINDOW ? (ilen - SHINGLES_WINDOW + 1) : 1;
  128. keys = rspamd_shingles_get_keys_cached(key);
  129. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  130. hashes[i] = g_malloc(hlen * sizeof(uint64_t));
  131. }
  132. /* Now parse input words into a vector of hashes using rolling window */
  133. if (alg == RSPAMD_SHINGLES_OLD) {
  134. for (i = 0; i <= (gint) ilen; i++) {
  135. if (i - beg >= SHINGLES_WINDOW || i == (gint) ilen) {
  136. for (j = beg; j < i; j++) {
  137. word = NULL;
  138. while (widx < input->len) {
  139. word = &g_array_index(input, rspamd_stat_token_t, widx);
  140. if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0) {
  141. widx++;
  142. }
  143. else {
  144. break;
  145. }
  146. }
  147. if (word == NULL) {
  148. /* Nothing but exceptions */
  149. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  150. g_free(hashes[i]);
  151. }
  152. g_free(hashes);
  153. if (pool == NULL) {
  154. g_free(res);
  155. }
  156. rspamd_fstring_free(row);
  157. return NULL;
  158. }
  159. row = rspamd_fstring_append(row, word->stemmed.begin,
  160. word->stemmed.len);
  161. }
  162. /* Now we need to create a new row here */
  163. for (j = 0; j < RSPAMD_SHINGLE_SIZE; j++) {
  164. rspamd_cryptobox_siphash((guchar *) &val, row->str, row->len,
  165. keys[j]);
  166. g_assert(hlen > beg);
  167. hashes[j][beg] = val;
  168. }
  169. beg++;
  170. widx++;
  171. row = rspamd_fstring_assign(row, "", 0);
  172. }
  173. }
  174. }
  175. else {
  176. uint64_t window[SHINGLES_WINDOW * RSPAMD_SHINGLE_SIZE], seed;
  177. switch (alg) {
  178. case RSPAMD_SHINGLES_XXHASH:
  179. ht = RSPAMD_CRYPTOBOX_XXHASH64;
  180. break;
  181. case RSPAMD_SHINGLES_MUMHASH:
  182. ht = RSPAMD_CRYPTOBOX_MUMHASH;
  183. break;
  184. default:
  185. ht = RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT;
  186. break;
  187. }
  188. memset(window, 0, sizeof(window));
  189. for (i = 0; i <= ilen; i++) {
  190. if (i - beg >= SHINGLES_WINDOW || i == ilen) {
  191. for (j = 0; j < RSPAMD_SHINGLE_SIZE; j++) {
  192. /* Shift hashes window to right */
  193. for (k = 0; k < SHINGLES_WINDOW - 1; k++) {
  194. window[j * SHINGLES_WINDOW + k] =
  195. window[j * SHINGLES_WINDOW + k + 1];
  196. }
  197. word = NULL;
  198. while (widx < input->len) {
  199. word = &g_array_index(input, rspamd_stat_token_t, widx);
  200. if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0) {
  201. widx++;
  202. }
  203. else {
  204. break;
  205. }
  206. }
  207. if (word == NULL) {
  208. /* Nothing but exceptions */
  209. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  210. g_free(hashes[i]);
  211. }
  212. if (pool == NULL) {
  213. g_free(res);
  214. }
  215. g_free(hashes);
  216. rspamd_fstring_free(row);
  217. return NULL;
  218. }
  219. /* Insert the last element to the pipe */
  220. memcpy(&seed, keys[j], sizeof(seed));
  221. window[j * SHINGLES_WINDOW + SHINGLES_WINDOW - 1] =
  222. rspamd_cryptobox_fast_hash_specific(ht,
  223. word->stemmed.begin, word->stemmed.len,
  224. seed);
  225. val = 0;
  226. for (k = 0; k < SHINGLES_WINDOW; k++) {
  227. val ^= window[j * SHINGLES_WINDOW + k] >>
  228. (8 * (SHINGLES_WINDOW - k - 1));
  229. }
  230. g_assert(hlen > beg);
  231. hashes[j][beg] = val;
  232. }
  233. beg++;
  234. widx++;
  235. }
  236. }
  237. }
  238. /* Now we need to filter all hashes and make a shingles result */
  239. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  240. res->hashes[i] = filter(hashes[i], hlen,
  241. i, key, filterd);
  242. g_free(hashes[i]);
  243. }
  244. g_free(hashes);
  245. rspamd_fstring_free(row);
  246. return res;
  247. }
  248. struct rspamd_shingle *RSPAMD_OPTIMIZE("unroll-loops")
  249. rspamd_shingles_from_image(guchar *dct,
  250. const guchar key[16],
  251. rspamd_mempool_t *pool,
  252. rspamd_shingles_filter filter,
  253. gpointer filterd,
  254. enum rspamd_shingle_alg alg)
  255. {
  256. struct rspamd_shingle *shingle;
  257. uint64_t **hashes;
  258. guchar **keys;
  259. uint64_t d;
  260. uint64_t val;
  261. gint i, j;
  262. gsize hlen, beg = 0;
  263. enum rspamd_cryptobox_fast_hash_type ht;
  264. uint64_t res[SHINGLES_WINDOW * RSPAMD_SHINGLE_SIZE], seed;
  265. if (pool != NULL) {
  266. shingle = rspamd_mempool_alloc(pool, sizeof(*shingle));
  267. }
  268. else {
  269. shingle = g_malloc(sizeof(*shingle));
  270. }
  271. /* Init hashes pipes and keys */
  272. hashes = g_malloc(sizeof(*hashes) * RSPAMD_SHINGLE_SIZE);
  273. hlen = RSPAMD_DCT_LEN / NBBY + 1;
  274. keys = rspamd_shingles_get_keys_cached(key);
  275. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  276. hashes[i] = g_malloc(hlen * sizeof(uint64_t));
  277. }
  278. switch (alg) {
  279. case RSPAMD_SHINGLES_OLD:
  280. ht = RSPAMD_CRYPTOBOX_MUMHASH;
  281. break;
  282. case RSPAMD_SHINGLES_XXHASH:
  283. ht = RSPAMD_CRYPTOBOX_XXHASH64;
  284. break;
  285. case RSPAMD_SHINGLES_MUMHASH:
  286. ht = RSPAMD_CRYPTOBOX_MUMHASH;
  287. break;
  288. default:
  289. ht = RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT;
  290. break;
  291. }
  292. memset(res, 0, sizeof(res));
  293. #define INNER_CYCLE_SHINGLES(s, e) \
  294. for (j = (s); j < (e); j++) { \
  295. d = dct[beg]; \
  296. memcpy(&seed, keys[j], sizeof(seed)); \
  297. val = rspamd_cryptobox_fast_hash_specific(ht, \
  298. &d, sizeof(d), \
  299. seed); \
  300. hashes[j][beg] = val; \
  301. }
  302. for (i = 0; i < RSPAMD_DCT_LEN / NBBY; i++) {
  303. INNER_CYCLE_SHINGLES(0, RSPAMD_SHINGLE_SIZE / 4);
  304. INNER_CYCLE_SHINGLES(RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE / 2);
  305. INNER_CYCLE_SHINGLES(RSPAMD_SHINGLE_SIZE / 2, 3 * RSPAMD_SHINGLE_SIZE / 4);
  306. INNER_CYCLE_SHINGLES(3 * RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE);
  307. beg++;
  308. }
  309. #undef INNER_CYCLE_SHINGLES
  310. /* Now we need to filter all hashes and make a shingles result */
  311. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  312. shingle->hashes[i] = filter(hashes[i], hlen,
  313. i, key, filterd);
  314. g_free(hashes[i]);
  315. }
  316. g_free(hashes);
  317. return shingle;
  318. }
  319. uint64_t
  320. rspamd_shingles_default_filter(uint64_t *input, gsize count,
  321. gint shno, const guchar *key, gpointer ud)
  322. {
  323. uint64_t minimal = G_MAXUINT64;
  324. gsize i;
  325. for (i = 0; i < count; i++) {
  326. if (minimal > input[i]) {
  327. minimal = input[i];
  328. }
  329. }
  330. return minimal;
  331. }
  332. gdouble rspamd_shingles_compare(const struct rspamd_shingle *a,
  333. const struct rspamd_shingle *b)
  334. {
  335. gint i, common = 0;
  336. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) {
  337. if (a->hashes[i] == b->hashes[i]) {
  338. common++;
  339. }
  340. }
  341. return (gdouble) common / (gdouble) RSPAMD_SHINGLE_SIZE;
  342. }