libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
spamsum.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 * Copyright (C) 2002 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2006 ManTech International Corporation
6 * Copyright (C) 2013 Helmut Grohne <helmut@subdivi.de>
7 *
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <errno.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#if defined(_WIN32) || defined(_WIN64)
29#include <windows.h>
30#endif
31
32#include "aaruformat.h"
33
34#include "spamsum.h"
35
36static inline void aaruf_set_spamsum_error(const int error_code)
37{
38 errno = error_code;
39#if defined(_WIN32) || defined(_WIN64)
40 SetLastError((DWORD)error_code);
41#endif
42}
43
44static uint8_t b64[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
45 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
46 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
47 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F};
48
50{
51 spamsum_ctx *ctx = malloc(sizeof(spamsum_ctx));
52 if(!ctx) return NULL;
53
54 memset(ctx, 0, sizeof(spamsum_ctx));
55
56 ctx->bh_end = 1;
57 ctx->bh[0].h = HASH_INIT;
58 ctx->bh[0].half_h = HASH_INIT;
59
60 return ctx;
61}
62
71AARU_EXPORT int AARU_CALL aaruf_spamsum_update(spamsum_ctx *ctx, const uint8_t *data, const uint32_t len)
72{
73 if(!ctx || !data) return -1;
74
75 for(int i = 0; i < len; i++) fuzzy_engine_step(ctx, data[i]);
76
77 ctx->total_size += len;
78
79 return 0;
80}
81
88{
89 if(ctx) free(ctx);
90}
91
92#define ROLL_SUM(ctx) ((ctx)->roll.h1 + (ctx)->roll.h2 + (ctx)->roll.h3)
93#define SUM_HASH(c, h) (((h) * HASH_PRIME) ^ (c));
94#define SSDEEP_BS(index) (MIN_BLOCKSIZE << (index))
95
96AARU_LOCAL inline void fuzzy_engine_step(spamsum_ctx *ctx, uint8_t c)
97{
98 uint32_t i = 0;
99 /* At each character we update the rolling hash and the normal hashes.
100 * When the rolling hash hits a reset value then we emit a normal hash
101 * as a element of the signature and reset the normal hash. */
102 roll_hash(ctx, c);
103 const uint64_t h = ROLL_SUM(ctx);
104
105 for(i = ctx->bh_start; i < ctx->bh_end; ++i)
106 {
107 ctx->bh[i].h = SUM_HASH(c, ctx->bh[i].h);
108 ctx->bh[i].half_h = SUM_HASH(c, ctx->bh[i].half_h);
109 }
110
111 for(i = ctx->bh_start; i < ctx->bh_end; ++i)
112 {
113 /* With growing blocksize almost no runs fail the next test. */
114 if(h % SSDEEP_BS(i) != SSDEEP_BS(i) - 1)
115 /* Once this condition is false for one bs, it is
116 * automatically false for all further bs. I.e. if
117 * h === -1 (mod 2*bs) then h === -1 (mod bs). */
118 break;
119
120 /* We have hit a reset point. We now emit hashes which are
121 * based on all characters in the piece of the message between
122 * the last reset point and this one */
123 if(0 == ctx->bh[i].d_len) fuzzy_try_fork_blockhash(ctx);
124
125 ctx->bh[i].digest[ctx->bh[i].d_len] = b64[ctx->bh[i].h % 64];
126 ctx->bh[i].half_digest = b64[ctx->bh[i].half_h % 64];
127
128 if(ctx->bh[i].d_len < SPAMSUM_LENGTH - 1)
129 {
130 /* We can have a problem with the tail overflowing. The
131 * easiest way to cope with this is to only reset the
132 * normal hash if we have room for more characters in
133 * our signature. This has the effect of combining the
134 * last few pieces of the message into a single piece
135 * */
136 ctx->bh[i].digest[++ctx->bh[i].d_len] = 0;
137 ctx->bh[i].h = HASH_INIT;
138
139 if(ctx->bh[i].d_len >= SPAMSUM_LENGTH / 2) continue;
140
141 ctx->bh[i].half_h = HASH_INIT;
142 ctx->bh[i].half_digest = 0;
143 }
144 else
146 }
147}
148
149AARU_LOCAL inline void roll_hash(spamsum_ctx *ctx, uint8_t c)
150{
151 ctx->roll.h2 -= ctx->roll.h1;
152 ctx->roll.h2 += ROLLING_WINDOW * c;
153
154 ctx->roll.h1 += c;
155 ctx->roll.h1 -= ctx->roll.window[ctx->roll.n % ROLLING_WINDOW];
156
157 ctx->roll.window[ctx->roll.n % ROLLING_WINDOW] = c;
158 ctx->roll.n++;
159
160 /* The original spamsum AND'ed this value with 0xFFFFFFFF which
161 * in theory should have no effect. This AND has been removed
162 * for performance (jk) */
163 ctx->roll.h3 <<= 5;
164 ctx->roll.h3 ^= c;
165}
166
168{
169 // assert(ctx->bh_start < ctx->bh_end);
170
171 if(ctx->bh_end - ctx->bh_start < 2) /* Need at least two working hashes. */
172 return;
173
174 if((uint64_t)SSDEEP_BS(ctx->bh_start) * SPAMSUM_LENGTH >= ctx->total_size)
175 /* Initial blocksize estimate would select this or a smaller
176 * blocksize. */
177 return;
178
179 if(ctx->bh[ctx->bh_start + 1].d_len < SPAMSUM_LENGTH / 2) /* Estimate adjustment would select this blocksize. */
180 return;
181
182 /* At this point we are clearly no longer interested in the
183 * start_blocksize. Get rid of it. */
184 ++ctx->bh_start;
185}
186
188{
189 if(ctx->bh_end >= NUM_BLOCKHASHES) return;
190
191 // assert(ctx->bh_end != 0);
192
193 const uint32_t obh = ctx->bh_end - 1;
194 const uint32_t nbh = ctx->bh_end;
195 ctx->bh[nbh].h = ctx->bh[obh].h;
196 ctx->bh[nbh].half_h = ctx->bh[obh].half_h;
197 ctx->bh[nbh].digest[0] = 0;
198 ctx->bh[nbh].half_digest = 0;
199 ctx->bh[nbh].d_len = 0;
200 ++ctx->bh_end;
201}
202
204{
206
207 uint32_t bi = ctx->bh_start;
208 uint32_t h = ROLL_SUM(ctx);
209 int remain = FUZZY_MAX_RESULT - 1; /* Exclude terminating '\0'. */
210
211 if(!result) return -1;
212
213 /* Verify that our elimination was not overeager. */
214 // assert(bi == 0 || (uint64_t)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < ctx->total_size);
215
216 /* Initial blocksize guess. */
217 while((uint64_t)SSDEEP_BS(bi) * SPAMSUM_LENGTH < ctx->total_size)
218 {
219 ++bi;
220
221 if(bi >= NUM_BLOCKHASHES)
222 {
223 aaruf_set_spamsum_error(EOVERFLOW);
224 return -1;
225 }
226 }
227
228 /* Adapt blocksize guess to actual digest length. */
229 while(bi >= ctx->bh_end) --bi;
230
231 while(bi > ctx->bh_start && ctx->bh[bi].d_len < SPAMSUM_LENGTH / 2) --bi;
232
233 // assert(!(bi > 0 && ctx->bh[bi].d_len < SPAMSUM_LENGTH / 2));
234
235 int i = snprintf((char *)result, (size_t)remain, "%lu:", (unsigned long)SSDEEP_BS(bi));
236
237 if(i <= 0) /* Maybe snprintf has set errno here? */
238 return -1;
239
240 // assert(i < remain);
241
242 remain -= i;
243 result += i;
244
245 i = (int)ctx->bh[bi].d_len;
246
247 // assert(i <= remain);
248
249 memcpy(result, ctx->bh[bi].digest, (size_t)i);
250 result += i;
251 remain -= i;
252
253 if(h != 0)
254 {
255 // assert(remain > 0);
256
257 *result = b64[ctx->bh[bi].h % 64];
258
259 if(i < 3 || *result != result[-1] || *result != result[-2] || *result != result[-3])
260 {
261 ++result;
262 --remain;
263 }
264 }
265 else if(ctx->bh[bi].digest[i] != 0)
266 {
267 // assert(remain > 0);
268
269 *result = ctx->bh[bi].digest[i];
270
271 if(i < 3 || *result != result[-1] || *result != result[-2] || *result != result[-3])
272 {
273 ++result;
274 --remain;
275 }
276 }
277
278 // assert(remain > 0);
279
280 *result++ = ':';
281 --remain;
282
283 if(bi < ctx->bh_end - 1)
284 {
285 ++bi;
286 i = (int)ctx->bh[bi].d_len;
287
288 if(i <= remain)
289 ;
290
291 memcpy(result, ctx->bh[bi].digest, (size_t)i);
292 result += i;
293 remain -= i;
294
295 if(h != 0)
296 {
297 // assert(remain > 0);
298
299 h = ctx->bh[bi].half_h;
300 *result = b64[h % 64];
301
302 if(i < 3 || *result != result[-1] || *result != result[-2] || *result != result[-3])
303 {
304 ++result;
305 --remain;
306 }
307 }
308 else
309 {
310 i = ctx->bh[bi].half_digest;
311
312 if(i != 0)
313 {
314 // assert(remain > 0);
315
316 *result = (uint8_t)i;
317
318 if(i < 3 || *result != result[-1] || *result != result[-2] || *result != result[-3])
319 {
320 ++result;
321 --remain;
322 }
323 }
324 }
325 }
326 else if(h != 0)
327 {
328 // assert(ctx->bh[bi].d_len == 0);
329
330 // assert(remain > 0);
331
332 *result++ = b64[ctx->bh[bi].h % 64];
333 /* No need to bother with FUZZY_FLAG_ELIMSEQ, because this
334 * digest has length 1. */
335 --remain;
336 }
337
338 *result = 0;
339
340 return 0;
341}
#define AARU_CALL
Definition decls.h:46
#define AARU_LOCAL
Definition decls.h:56
#define AARU_EXPORT
Definition decls.h:55
#define SUM_HASH(c, h)
Definition spamsum.c:93
static uint8_t b64[]
Definition spamsum.c:44
void fuzzy_try_fork_blockhash(spamsum_ctx *ctx)
Definition spamsum.c:187
#define SSDEEP_BS(index)
Definition spamsum.c:94
void fuzzy_engine_step(spamsum_ctx *ctx, uint8_t c)
Definition spamsum.c:96
void aaruf_spamsum_free(spamsum_ctx *ctx)
Frees a spamsum (fuzzy hash) context.
Definition spamsum.c:87
#define ROLL_SUM(ctx)
Definition spamsum.c:92
spamsum_ctx * aaruf_spamsum_init(void)
Definition spamsum.c:49
void fuzzy_try_reduce_blockhash(spamsum_ctx *ctx)
Definition spamsum.c:167
void roll_hash(spamsum_ctx *ctx, uint8_t c)
Definition spamsum.c:149
static void aaruf_set_spamsum_error(const int error_code)
Definition spamsum.c:36
int aaruf_spamsum_final(spamsum_ctx *ctx, uint8_t *result)
Definition spamsum.c:203
int aaruf_spamsum_update(spamsum_ctx *ctx, const uint8_t *data, const uint32_t len)
Updates the spamsum context with new data.
Definition spamsum.c:71
#define SPAMSUM_LENGTH
Definition spamsum.h:24
#define FUZZY_MAX_RESULT
Definition spamsum.h:30
#define ROLLING_WINDOW
Definition spamsum.h:26
#define NUM_BLOCKHASHES
Definition spamsum.h:25
#define HASH_INIT
Definition spamsum.h:27
uint32_t d_len
Definition spamsum.h:38
uint32_t h
Definition spamsum.h:34
uint8_t half_digest
Definition spamsum.h:37
uint8_t digest[64]
Definition spamsum.h:36
uint32_t half_h
Definition spamsum.h:35
uint32_t h2
Definition spamsum.h:45
uint32_t n
Definition spamsum.h:47
uint32_t h1
Definition spamsum.h:44
uint32_t h3
Definition spamsum.h:46
uint8_t window[7]
Definition spamsum.h:43
blockhash_ctx bh[31]
Definition spamsum.h:54
roll_state roll
Definition spamsum.h:56
uint32_t bh_end
Definition spamsum.h:53
uint64_t total_size
Definition spamsum.h:55
uint32_t bh_start
Definition spamsum.h:52