diff options
Diffstat (limited to 'toxcore/crypto_core_test.cc')
-rw-r--r-- | toxcore/crypto_core_test.cc | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/toxcore/crypto_core_test.cc b/toxcore/crypto_core_test.cc index ee3cb366..66569506 100644 --- a/toxcore/crypto_core_test.cc +++ b/toxcore/crypto_core_test.cc | |||
@@ -1,6 +1,7 @@ | |||
1 | #include "crypto_core.h" | 1 | #include "crypto_core.h" |
2 | 2 | ||
3 | #include <algorithm> | 3 | #include <algorithm> |
4 | #include <vector> | ||
4 | 5 | ||
5 | #include <gtest/gtest.h> | 6 | #include <gtest/gtest.h> |
6 | 7 | ||
@@ -29,7 +30,7 @@ enum { | |||
29 | CRYPTO_TEST_MEMCMP_EPS = 10, | 30 | CRYPTO_TEST_MEMCMP_EPS = 10, |
30 | }; | 31 | }; |
31 | 32 | ||
32 | clock_t memcmp_time(void *a, void *b, size_t len) { | 33 | clock_t memcmp_time(uint8_t const *a, uint8_t const *b, size_t len) { |
33 | clock_t start = clock(); | 34 | clock_t start = clock(); |
34 | volatile int result = crypto_memcmp(a, b, len); | 35 | volatile int result = crypto_memcmp(a, b, len); |
35 | (void)result; | 36 | (void)result; |
@@ -41,8 +42,8 @@ clock_t memcmp_time(void *a, void *b, size_t len) { | |||
41 | * equal and non-equal arrays to reduce the influence of external effects | 42 | * equal and non-equal arrays to reduce the influence of external effects |
42 | * such as the machine being a little more busy 1 second later. | 43 | * such as the machine being a little more busy 1 second later. |
43 | */ | 44 | */ |
44 | void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *same_median, | 45 | std::pair<clock_t, clock_t> memcmp_median(uint8_t const *src, uint8_t const *same, |
45 | clock_t *not_same_median) { | 46 | uint8_t const *not_same, size_t len) { |
46 | clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; | 47 | clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; |
47 | clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; | 48 | clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; |
48 | 49 | ||
@@ -52,9 +53,10 @@ void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *s | |||
52 | } | 53 | } |
53 | 54 | ||
54 | std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); | 55 | std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); |
55 | *same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; | 56 | clock_t const same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; |
56 | std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); | 57 | 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 | clock_t const not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; |
59 | return {same_median, not_same_median}; | ||
58 | } | 60 | } |
59 | 61 | ||
60 | /** | 62 | /** |
@@ -63,32 +65,28 @@ void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *s | |||
63 | */ | 65 | */ |
64 | TEST(CryptoCore, MemcmpTimingIsDataIndependent) { | 66 | TEST(CryptoCore, MemcmpTimingIsDataIndependent) { |
65 | // A random piece of memory. | 67 | // A random piece of memory. |
66 | auto *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; | 68 | std::vector<uint8_t> src(CRYPTO_TEST_MEMCMP_SIZE); |
67 | random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE); | 69 | random_bytes(src.data(), CRYPTO_TEST_MEMCMP_SIZE); |
68 | 70 | ||
69 | // A separate piece of memory containing the same data. | 71 | // A separate piece of memory containing the same data. |
70 | auto *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; | 72 | std::vector<uint8_t> same = src; |
71 | memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE); | ||
72 | 73 | ||
73 | // Another piece of memory containing different data. | 74 | // Another piece of memory containing different data. |
74 | auto *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; | 75 | std::vector<uint8_t> not_same(CRYPTO_TEST_MEMCMP_SIZE); |
75 | random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE); | 76 | random_bytes(not_same.data(), CRYPTO_TEST_MEMCMP_SIZE); |
76 | 77 | ||
77 | clock_t same_median; | 78 | // Once we have C++17: |
78 | clock_t not_same_median; | 79 | // auto const [same_median, not_same_median] = |
79 | memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, ¬_same_median); | 80 | auto const result = |
80 | 81 | memcmp_median(src.data(), same.data(), not_same.data(), CRYPTO_TEST_MEMCMP_SIZE); | |
81 | delete[] not_same; | ||
82 | delete[] same; | ||
83 | delete[] src; | ||
84 | 82 | ||
85 | clock_t const delta = | 83 | clock_t const delta = |
86 | same_median > not_same_median ? same_median - not_same_median : not_same_median - same_median; | 84 | std::max(result.first, result.second) - std::min(result.first, result.second); |
87 | 85 | ||
88 | EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) | 86 | EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) |
89 | << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" | 87 | << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" |
90 | << "Time of the same data comparison: " << same_median << " clocks\n" | 88 | << "Time of the same data comparison: " << result.first << " clocks\n" |
91 | << "Time of the different data comparison: " << not_same_median << " clocks"; | 89 | << "Time of the different data comparison: " << result.second << " clocks"; |
92 | } | 90 | } |
93 | 91 | ||
94 | } // namespace | 92 | } // namespace |