summaryrefslogtreecommitdiff
path: root/toxcore/crypto_core_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/crypto_core_test.cpp')
-rw-r--r--toxcore/crypto_core_test.cpp96
1 files changed, 0 insertions, 96 deletions
diff --git a/toxcore/crypto_core_test.cpp b/toxcore/crypto_core_test.cpp
deleted file mode 100644
index 8f91dce8..00000000
--- a/toxcore/crypto_core_test.cpp
+++ /dev/null
@@ -1,96 +0,0 @@
1#include "crypto_core.h"
2
3#include <algorithm>
4
5#include <gtest/gtest.h>
6
7namespace
8{
9
10enum {
11 /**
12 * The size of the arrays to compare. This was chosen to take around 2000
13 * CPU clocks on x86_64.
14 */
15 CRYPTO_TEST_MEMCMP_SIZE = 1024 * 1024, // 1 MiB
16 /**
17 * The number of times we run memcmp in the test.
18 *
19 * We compute the median time taken to reduce error margins.
20 */
21 CRYPTO_TEST_MEMCMP_ITERATIONS = 500,
22 /**
23 * The margin of error (in clocks) we allow for this test.
24 *
25 * Should be within 0.5% of ~2000 CPU clocks. In reality, the code is much
26 * more precise and is usually within 1 CPU clock.
27 */
28 CRYPTO_TEST_MEMCMP_EPS = 10,
29};
30
31clock_t memcmp_time(void *a, void *b, size_t len)
32{
33 clock_t start = clock();
34 crypto_memcmp(a, b, len);
35 return clock() - start;
36}
37
38/**
39 * This function performs the actual timing. It interleaves comparison of
40 * equal and non-equal arrays to reduce the influence of external effects
41 * such as the machine being a little more busy 1 second later.
42 */
43void memcmp_median(void *src, void *same, void *not_same, size_t len,
44 clock_t *same_median, clock_t *not_same_median)
45{
46 clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS];
47 clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS];
48
49 for (size_t i = 0; i < CRYPTO_TEST_MEMCMP_ITERATIONS; i++) {
50 same_results[i] = memcmp_time(src, same, len);
51 not_same_results[i] = memcmp_time(src, not_same, len);
52 }
53
54 std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS);
55 *same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2];
56 std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS);
57 *not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2];
58}
59
60/**
61 * This test checks whether crypto_memcmp takes the same time for equal and
62 * non-equal chunks of memory.
63 */
64TEST(CryptoCore, MemcmpTimingIsDataIndependent)
65{
66 // A random piece of memory.
67 uint8_t *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE];
68 random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE);
69
70 // A separate piece of memory containing the same data.
71 uint8_t *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE];
72 memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE);
73
74 // Another piece of memory containing different data.
75 uint8_t *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE];
76 random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE);
77
78 clock_t same_median;
79 clock_t not_same_median;
80 memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, &not_same_median);
81
82 delete[] not_same;
83 delete[] same;
84 delete[] src;
85
86 clock_t const delta = same_median > not_same_median
87 ? same_median - not_same_median
88 : not_same_median - same_median;
89
90 EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS)
91 << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n"
92 << "Time of the same data comparation: " << same_median << " clocks\n"
93 << "Time of the different data comparation: " << not_same_median << " clocks";
94}
95
96} // namespace