1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* Copyright (c) 2015, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "rspamd.h"
#include "shingles.h"
#include "fstring.h"
#include "ottery.h"
#include "cryptobox.h"
#include "unix-std.h"
static const int mapping_size = 64 * 8192 + 1;
static const int max_seg = 32;
static const int random_fuzz_cnt = 10000;
static void *
create_mapping (int mapping_len, guchar **beg, guchar **end)
{
void *map;
int psize = getpagesize ();
map = mmap (NULL, mapping_len + psize * 3, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED, -1, 0);
g_assert (map != 0);
memset (map, 0, mapping_len + psize * 3);
mprotect (map, psize, PROT_NONE);
/* Misalign pointer */
*beg = ((guchar *)map) + psize + 1;
*end = *beg + mapping_len;
mprotect (*beg + mapping_len - 1 + psize, psize, PROT_NONE);
return map;
}
static void
check_result (const rspamd_nm_t key, const rspamd_nonce_t nonce,
const rspamd_sig_t mac, guchar *begin, guchar *end)
{
guint64 *t = (guint64 *)begin;
g_assert (rspamd_cryptobox_decrypt_nm_inplace (begin, end - begin, nonce, key,
mac));
while (t < (guint64 *)end) {
g_assert (*t == 0);
t ++;
}
}
static int
create_random_split (struct rspamd_cryptobox_segment *seg, int mseg,
guchar *begin, guchar *end)
{
gsize remain = end - begin;
gint used = 0;
while (remain > 0 && used < mseg - 1) {
seg->data = begin;
seg->len = ottery_rand_range (remain - 1) + 1;
begin += seg->len;
remain -= seg->len;
used ++;
seg ++;
}
if (remain > 0) {
seg->data = begin;
seg->len = remain;
used ++;
}
return used;
}
static int
create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg,
guchar *begin, guchar *end)
{
gsize remain = end - begin;
gint used = 0;
static const int small_seg = 512, medium_seg = 2048;
while (remain > 0 && used < mseg - 1) {
seg->data = begin;
if (ottery_rand_uint32 () % 2 == 0) {
seg->len = ottery_rand_range (small_seg) + 1;
}
else {
seg->len = ottery_rand_range (medium_seg) +
small_seg;
}
if (seg->len > remain) {
seg->len = remain;
}
begin += seg->len;
remain -= seg->len;
used ++;
seg ++;
}
if (remain > 0) {
seg->data = begin;
seg->len = remain;
used ++;
}
return used;
}
static int
create_constrainted_split (struct rspamd_cryptobox_segment *seg, int mseg,
int constraint,
guchar *begin, guchar *end)
{
gsize remain = end - begin;
gint used = 0;
while (remain > 0 && used < mseg - 1) {
seg->data = begin;
seg->len = constraint;
if (seg->len > remain) {
seg->len = remain;
}
begin += seg->len;
remain -= seg->len;
used ++;
seg ++;
}
if (remain > 0) {
seg->data = begin;
seg->len = remain;
used ++;
}
return used;
}
void
rspamd_cryptobox_test_func (void)
{
void *map;
guchar *begin, *end;
rspamd_nm_t key;
rspamd_nonce_t nonce;
rspamd_sig_t mac;
struct rspamd_cryptobox_segment *seg;
double t1, t2;
gint i, cnt, ms;
map = create_mapping (mapping_size, &begin, &end);
ottery_rand_bytes (key, sizeof (key));
ottery_rand_bytes (nonce, sizeof (nonce));
memset (mac, 0, sizeof (mac));
seg = g_slice_alloc0 (sizeof (*seg) * max_seg * 10);
/* Test baseline */
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encrypt_nm_inplace (begin, end - begin, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("baseline encryption: %.6f", t2 - t1);
/* A single chunk as vector */
seg[0].data = begin;
seg[0].len = end - begin;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 1, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("bulk encryption: %.6f", t2 - t1);
/* Two chunks as vector */
seg[0].data = begin;
seg[0].len = (end - begin) / 2;
seg[1].data = begin + seg[0].len;
seg[1].len = (end - begin) - seg[0].len;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("2 equal chunks encryption: %.6f", t2 - t1);
seg[0].data = begin;
seg[0].len = 1;
seg[1].data = begin + seg[0].len;
seg[1].len = (end - begin) - seg[0].len;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("small and large chunks encryption: %.6f", t2 - t1);
seg[0].data = begin;
seg[0].len = (end - begin) - 3;
seg[1].data = begin + seg[0].len;
seg[1].len = (end - begin) - seg[0].len;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("large and small chunks encryption: %.6f", t2 - t1);
/* Random two chunks as vector */
seg[0].data = begin;
seg[0].len = ottery_rand_range (end - begin - 1) + 1;
seg[1].data = begin + seg[0].len;
seg[1].len = (end - begin) - seg[0].len;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("random 2 chunks encryption: %.6f", t2 - t1);
/* 3 specific chunks */
seg[0].data = begin;
seg[0].len = 2;
seg[1].data = begin + seg[0].len;
seg[1].len = 2049;
seg[2].data = begin + seg[0].len + seg[1].len;
seg[2].len = (end - begin) - seg[0].len - seg[1].len;
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, 3, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("small, medium and large chunks encryption: %.6f", t2 - t1);
cnt = create_random_split (seg, max_seg, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("random split of %d chunks encryption: %.6f", cnt, t2 - t1);
cnt = create_realistic_split (seg, max_seg, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("realistic split of %d chunks encryption: %.6f", cnt, t2 - t1);
cnt = create_constrainted_split (seg, max_seg + 1, 32, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
msg_info ("constrainted split of %d chunks encryption: %.6f", cnt, t2 - t1);
for (i = 0; i < random_fuzz_cnt; i ++) {
ms = ottery_rand_range (i % max_seg * 2) + 1;
cnt = create_random_split (seg, ms, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
if (i % 1000 == 0) {
msg_info ("random fuzz iterations: %d", i);
}
}
for (i = 0; i < random_fuzz_cnt; i ++) {
ms = ottery_rand_range (i % max_seg * 2) + 1;
cnt = create_realistic_split (seg, ms, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
if (i % 1000 == 0) {
msg_info ("realistic fuzz iterations: %d", i);
}
}
for (i = 0; i < random_fuzz_cnt; i ++) {
ms = ottery_rand_range (i % max_seg * 10) + 1;
cnt = create_constrainted_split (seg, ms, i, begin, end);
t1 = rspamd_get_ticks ();
rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac);
t2 = rspamd_get_ticks ();
check_result (key, nonce, mac, begin, end);
if (i % 1000 == 0) {
msg_info ("constrainted fuzz iterations: %d", i);
}
}
}
|