blob: 211a656bb0e48cd132b34fe2d4ffc072cfe78a87 [file] [log] [blame]
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +01001/* BEGIN_HEADER */
2/** \file test_suite_constant_time.function
3 *
4 * Functional testing of functions in the constant_time module.
5 *
6 * The tests are instrumented with #TEST_CF_SECRET and #TEST_CF_PUBLIC
7 * (see tests/include/test/constant_flow.h) so that running the tests
8 * under MSan or Valgrind will detect a non-constant-time implementation.
9 */
10
11#include <mbedtls/constant_time.h>
12#include <constant_time_internal.h>
13#include <constant_time_invasive.h>
14
15#include <test/constant_flow.h>
16/* END_HEADER */
17
Dave Rodgman39188c02022-12-23 12:27:04 +000018#include <stdio.h>
19
20/* BEGIN_CASE */
21void mbedtls_ct_memcmp_null()
22{
23 uint32_t x;
24 TEST_ASSERT(mbedtls_ct_memcmp(&x, NULL, 0) == 0);
25 TEST_ASSERT(mbedtls_ct_memcmp(NULL, &x, 0) == 0);
26 TEST_ASSERT(mbedtls_ct_memcmp(NULL, NULL, 0) == 0);
27}
28/* END_CASE */
29
30/* BEGIN_CASE */
31void mbedtls_ct_memcmp(int same, int size, int offset)
32{
33 uint8_t *a = NULL, *b = NULL;
34 ASSERT_ALLOC(a, size + offset);
35 ASSERT_ALLOC(b, size + offset);
36
37 TEST_CF_SECRET(a + offset, size);
38 TEST_CF_SECRET(b + offset, size);
39
40 for (int i = 0; i < size + offset; i++) {
41 a[i] = i & 0xff;
42 b[i] = (i & 0xff) + (same ? 0 : 1);
43 }
44
45 int reference = memcmp(a + offset, b + offset, size);
46 int actual = mbedtls_ct_memcmp(a + offset, b + offset, size);
47 TEST_CF_PUBLIC(a + offset, size);
48 TEST_CF_PUBLIC(b + offset, size);
49
50 if (same != 0) {
51 TEST_ASSERT(reference == 0);
52 TEST_ASSERT(actual == 0);
53 } else {
54 TEST_ASSERT(reference != 0);
55 TEST_ASSERT(actual != 0);
56 }
57exit:
58 mbedtls_free(a);
59 mbedtls_free(b);
60}
61/* END_CASE */
62
63/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_MAC */
64void mbedtls_ct_memcpy_if_eq(int eq, int size, int offset)
65{
66 uint8_t *src = NULL, *result = NULL, *expected = NULL;
67 ASSERT_ALLOC(src, size + offset);
68 ASSERT_ALLOC(result, size + offset);
69 ASSERT_ALLOC(expected, size + offset);
70
71 for (int i = 0; i < size + offset; i++) {
72 src[i] = 1;
73 result[i] = 0xff;
74 expected[i] = eq ? 1 : 0xff;
75 }
76
77 mbedtls_ct_memcpy_if_eq(result + offset, src, size, eq, 1);
78 ASSERT_COMPARE(expected, size, result + offset, size);
79
80 for (int i = 0; i < size + offset; i++) {
81 src[i] = 1;
82 result[i] = 0xff;
83 expected[i] = eq ? 1 : 0xff;
84 }
85 mbedtls_ct_memcpy_if_eq(result, src + offset, size, eq, 1);
86 ASSERT_COMPARE(expected, size, result, size);
87
88exit:
89 mbedtls_free(src);
90 mbedtls_free(result);
91 mbedtls_free(expected);
92}
93/* END_CASE */
94
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010095/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010096void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010097{
98 unsigned char *dst = NULL;
99 unsigned char *src = NULL;
100 size_t src_len = offset_max + len;
101 size_t secret;
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 ASSERT_ALLOC(dst, len);
104 ASSERT_ALLOC(src, src_len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100105
106 /* Fill src in a way that we can detect if we copied the right bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_test_rnd_std_rand(NULL, src, src_len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 for (secret = offset_min; secret <= (size_t) offset_max; secret++) {
110 mbedtls_test_set_step((int) secret);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_CF_SECRET(&secret, sizeof(secret));
113 mbedtls_ct_memcpy_offset(dst, src, secret,
114 offset_min, offset_max, len);
115 TEST_CF_PUBLIC(&secret, sizeof(secret));
116 TEST_CF_PUBLIC(dst, len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 ASSERT_COMPARE(dst, len, src + secret, len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100119 }
120
121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_free(dst);
123 mbedtls_free(src);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +0100124}
125/* END_CASE */