1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci/** \file test_suite_constant_time.function
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci * Functional testing of functions in the constant_time module.
5a8e1175bSopenharmony_ci *
6a8e1175bSopenharmony_ci * The tests are instrumented with #TEST_CF_SECRET and #TEST_CF_PUBLIC
7a8e1175bSopenharmony_ci * (see tests/include/test/constant_flow.h) so that running the tests
8a8e1175bSopenharmony_ci * under MSan or Valgrind will detect a non-constant-time implementation.
9a8e1175bSopenharmony_ci */
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_ci#include <stdio.h>
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ci#include <limits.h>
14a8e1175bSopenharmony_ci#include <stdlib.h>
15a8e1175bSopenharmony_ci#include <errno.h>
16a8e1175bSopenharmony_ci
17a8e1175bSopenharmony_ci#include <mbedtls/bignum.h>
18a8e1175bSopenharmony_ci#include <mbedtls/constant_time.h>
19a8e1175bSopenharmony_ci#include <constant_time_internal.h>
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci#include <test/constant_flow.h>
22a8e1175bSopenharmony_ci/* END_HEADER */
23a8e1175bSopenharmony_ci
24a8e1175bSopenharmony_ci/* BEGIN_CASE */
25a8e1175bSopenharmony_civoid mbedtls_ct_memcmp_null()
26a8e1175bSopenharmony_ci{
27a8e1175bSopenharmony_ci    uint32_t x = 0;
28a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ct_memcmp(&x, NULL, 0) == 0);
29a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ct_memcmp(NULL, &x, 0) == 0);
30a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ct_memcmp(NULL, NULL, 0) == 0);
31a8e1175bSopenharmony_ci}
32a8e1175bSopenharmony_ci/* END_CASE */
33a8e1175bSopenharmony_ci
34a8e1175bSopenharmony_ci/* BEGIN_CASE */
35a8e1175bSopenharmony_civoid mbedtls_ct_bool(char *input)
36a8e1175bSopenharmony_ci{
37a8e1175bSopenharmony_ci    mbedtls_ct_uint_t v = (mbedtls_ct_uint_t) strtoull(input, NULL, 16);
38a8e1175bSopenharmony_ci    TEST_ASSERT(errno == 0);
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci    mbedtls_ct_condition_t expected = (v != 0) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
41a8e1175bSopenharmony_ci    TEST_CF_SECRET(&v, sizeof(v));
42a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool(v), expected);
43a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&v, sizeof(v));
44a8e1175bSopenharmony_ci}
45a8e1175bSopenharmony_ci/* END_CASE */
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_ci/* BEGIN_CASE */
48a8e1175bSopenharmony_civoid mbedtls_ct_bool_xxx(char *x_str, char *y_str)
49a8e1175bSopenharmony_ci{
50a8e1175bSopenharmony_ci    mbedtls_ct_uint_t x = strtoull(x_str, NULL, 0);
51a8e1175bSopenharmony_ci    mbedtls_ct_uint_t y = strtoull(y_str, NULL, 0);
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci    mbedtls_ct_uint_t x1 = x;
54a8e1175bSopenharmony_ci    mbedtls_ct_uint_t y1 = y;
55a8e1175bSopenharmony_ci
56a8e1175bSopenharmony_ci    TEST_CF_SECRET(&x, sizeof(x));
57a8e1175bSopenharmony_ci    TEST_CF_SECRET(&y, sizeof(y));
58a8e1175bSopenharmony_ci
59a8e1175bSopenharmony_ci    mbedtls_ct_condition_t expected = x1 ? MBEDTLS_CT_FALSE : MBEDTLS_CT_TRUE;
60a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_not(mbedtls_ct_bool(x)), expected);
61a8e1175bSopenharmony_ci
62a8e1175bSopenharmony_ci    expected = x1 != y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
63a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_ne(x, y), expected);
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci    expected = x1 == y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
66a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_eq(x, y), expected);
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_ci    expected = x1 > y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
69a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_gt(x, y), expected);
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci    expected = x1 < y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
72a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_lt(x, y), expected);
73a8e1175bSopenharmony_ci
74a8e1175bSopenharmony_ci    expected = x1 >= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
75a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_ge(x, y), expected);
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ci    expected = x1 <= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
78a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_le(x, y), expected);
79a8e1175bSopenharmony_ci
80a8e1175bSopenharmony_ci    expected = (!!x1) != (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
81a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_ne(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci    expected = (!!x1) && (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
84a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_and(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
85a8e1175bSopenharmony_ci
86a8e1175bSopenharmony_ci    expected = (!!x1) || (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
87a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_or(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&x, sizeof(x));
90a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&y, sizeof(y));
91a8e1175bSopenharmony_ci}
92a8e1175bSopenharmony_ci/* END_CASE */
93a8e1175bSopenharmony_ci
94a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_BASE64_C */
95a8e1175bSopenharmony_civoid mbedtls_ct_uchar_in_range_if(int li, int hi, int ti)
96a8e1175bSopenharmony_ci{
97a8e1175bSopenharmony_ci    unsigned char l = li, h = hi, t = ti;
98a8e1175bSopenharmony_ci
99a8e1175bSopenharmony_ci    for (unsigned x = 0; x <= 255; x++) {
100a8e1175bSopenharmony_ci        unsigned char expected = (x >= l) && (x <= h) ? t : 0;
101a8e1175bSopenharmony_ci
102a8e1175bSopenharmony_ci        TEST_CF_SECRET(&x, sizeof(x));
103a8e1175bSopenharmony_ci        TEST_CF_SECRET(&l, sizeof(l));
104a8e1175bSopenharmony_ci        TEST_CF_SECRET(&h, sizeof(h));
105a8e1175bSopenharmony_ci        TEST_CF_SECRET(&t, sizeof(t));
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci        TEST_EQUAL(mbedtls_ct_uchar_in_range_if(l, h, (unsigned char) x, t), expected);
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&x, sizeof(x));
110a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&l, sizeof(l));
111a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&h, sizeof(h));
112a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&t, sizeof(t));
113a8e1175bSopenharmony_ci    }
114a8e1175bSopenharmony_ci}
115a8e1175bSopenharmony_ci/* END_CASE */
116a8e1175bSopenharmony_ci
117a8e1175bSopenharmony_ci/* BEGIN_CASE */
118a8e1175bSopenharmony_civoid mbedtls_ct_error_if(int cond, int t, int f)
119a8e1175bSopenharmony_ci{
120a8e1175bSopenharmony_ci    mbedtls_ct_condition_t c = mbedtls_ct_bool(cond);
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci    int expected = c ? t : f;
123a8e1175bSopenharmony_ci    int expected0 = c ? t : 0;
124a8e1175bSopenharmony_ci
125a8e1175bSopenharmony_ci    TEST_CF_SECRET(&c, sizeof(c));
126a8e1175bSopenharmony_ci    TEST_CF_SECRET(&t, sizeof(t));
127a8e1175bSopenharmony_ci    TEST_CF_SECRET(&f, sizeof(f));
128a8e1175bSopenharmony_ci
129a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_error_if(c, t, f), expected);
130a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_error_if_else_0(c, t), expected0);
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&c, sizeof(c));
133a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&t, sizeof(t));
134a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&f, sizeof(f));
135a8e1175bSopenharmony_ci}
136a8e1175bSopenharmony_ci/* END_CASE */
137a8e1175bSopenharmony_ci
138a8e1175bSopenharmony_ci/* BEGIN_CASE */
139a8e1175bSopenharmony_civoid mbedtls_ct_if(char *c_str, char *t_str, char *f_str)
140a8e1175bSopenharmony_ci{
141a8e1175bSopenharmony_ci    mbedtls_ct_condition_t c = mbedtls_ct_bool(strtoull(c_str, NULL, 16));
142a8e1175bSopenharmony_ci    mbedtls_ct_uint_t t = (mbedtls_ct_uint_t) strtoull(t_str, NULL, 16);
143a8e1175bSopenharmony_ci    mbedtls_ct_uint_t f = (mbedtls_ct_uint_t) strtoull(f_str, NULL, 16);
144a8e1175bSopenharmony_ci
145a8e1175bSopenharmony_ci    mbedtls_ct_uint_t expected = c ? t : f;
146a8e1175bSopenharmony_ci    mbedtls_ct_uint_t expected0 = c ? t : 0;
147a8e1175bSopenharmony_ci
148a8e1175bSopenharmony_ci    TEST_CF_SECRET(&c, sizeof(c));
149a8e1175bSopenharmony_ci    TEST_CF_SECRET(&t, sizeof(t));
150a8e1175bSopenharmony_ci    TEST_CF_SECRET(&f, sizeof(f));
151a8e1175bSopenharmony_ci
152a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_if(c, t, f), expected);
153a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_size_if(c, t, f), (size_t) expected);
154a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_if(c, t, f), (unsigned) expected);
155a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_if(c, mbedtls_ct_bool(t), mbedtls_ct_bool(f)),
156a8e1175bSopenharmony_ci               mbedtls_ct_bool(expected));
157a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C)
158a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_mpi_uint_if(c, t, f), (mbedtls_mpi_uint) expected);
159a8e1175bSopenharmony_ci#endif
160a8e1175bSopenharmony_ci
161a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_uint_if_else_0(c, t), (unsigned) expected0);
162a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_size_if_else_0(c, (size_t) t), (size_t) expected0);
163a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_bool_if_else_0(c, mbedtls_ct_bool(t)), mbedtls_ct_bool(expected0));
164a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C)
165a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ct_mpi_uint_if_else_0(c, t), (mbedtls_mpi_uint) expected0);
166a8e1175bSopenharmony_ci#endif
167a8e1175bSopenharmony_ci
168a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&c, sizeof(c));
169a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&t, sizeof(t));
170a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&f, sizeof(f));
171a8e1175bSopenharmony_ci}
172a8e1175bSopenharmony_ci/* END_CASE */
173a8e1175bSopenharmony_ci
174a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:!MBEDTLS_RSA_ALT */
175a8e1175bSopenharmony_civoid mbedtls_ct_zeroize_if(char *c_str, int len)
176a8e1175bSopenharmony_ci{
177a8e1175bSopenharmony_ci    uint8_t *buf = NULL;
178a8e1175bSopenharmony_ci    mbedtls_ct_condition_t c = mbedtls_ct_bool(strtoull(c_str, NULL, 16));
179a8e1175bSopenharmony_ci
180a8e1175bSopenharmony_ci    TEST_CALLOC(buf, len);
181a8e1175bSopenharmony_ci    for (size_t i = 0; i < (size_t) len; i++) {
182a8e1175bSopenharmony_ci        buf[i] = 1;
183a8e1175bSopenharmony_ci    }
184a8e1175bSopenharmony_ci
185a8e1175bSopenharmony_ci    TEST_CF_SECRET(&c, sizeof(c));
186a8e1175bSopenharmony_ci    TEST_CF_SECRET(buf, len);
187a8e1175bSopenharmony_ci    mbedtls_ct_zeroize_if(c, buf, len);
188a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&c, sizeof(c));
189a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(buf, len);
190a8e1175bSopenharmony_ci
191a8e1175bSopenharmony_ci    for (size_t i = 0; i < (size_t) len; i++) {
192a8e1175bSopenharmony_ci        TEST_EQUAL(buf[i], c != 0 ? 0 : 1);
193a8e1175bSopenharmony_ci    }
194a8e1175bSopenharmony_ciexit:
195a8e1175bSopenharmony_ci    mbedtls_free(buf);
196a8e1175bSopenharmony_ci}
197a8e1175bSopenharmony_ci/* END_CASE */
198a8e1175bSopenharmony_ci
199a8e1175bSopenharmony_ci/* BEGIN_CASE */
200a8e1175bSopenharmony_civoid mbedtls_ct_memcmp_single_bit_diff()
201a8e1175bSopenharmony_ci{
202a8e1175bSopenharmony_ci    uint8_t *a = NULL, *b = NULL;
203a8e1175bSopenharmony_ci    size_t size = 32;
204a8e1175bSopenharmony_ci    TEST_CALLOC(a, size);
205a8e1175bSopenharmony_ci    TEST_CALLOC(b, size);
206a8e1175bSopenharmony_ci
207a8e1175bSopenharmony_ci    TEST_CF_SECRET(a, size);
208a8e1175bSopenharmony_ci    TEST_CF_SECRET(b, size);
209a8e1175bSopenharmony_ci    int result = mbedtls_ct_memcmp(a, b, size);
210a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(a, size);
211a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(b, size);
212a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&result, sizeof(result));
213a8e1175bSopenharmony_ci
214a8e1175bSopenharmony_ci    TEST_EQUAL(result, 0);
215a8e1175bSopenharmony_ci
216a8e1175bSopenharmony_ci    for (size_t offset = 0; offset < size; offset++) {
217a8e1175bSopenharmony_ci        for (size_t bit_offset = 0; bit_offset < 8; bit_offset++) {
218a8e1175bSopenharmony_ci            /* Set a single bit to be different at given offset, to test that we
219a8e1175bSopenharmony_ci               detect single-bit differences */
220a8e1175bSopenharmony_ci            a[offset] = 1 << bit_offset;
221a8e1175bSopenharmony_ci
222a8e1175bSopenharmony_ci            TEST_CF_SECRET(a, size);
223a8e1175bSopenharmony_ci            TEST_CF_SECRET(b, size);
224a8e1175bSopenharmony_ci            result = mbedtls_ct_memcmp(a, b, size);
225a8e1175bSopenharmony_ci            TEST_CF_PUBLIC(a, size);
226a8e1175bSopenharmony_ci            TEST_CF_PUBLIC(b, size);
227a8e1175bSopenharmony_ci            TEST_CF_PUBLIC(&result, sizeof(result));
228a8e1175bSopenharmony_ci
229a8e1175bSopenharmony_ci            TEST_ASSERT(result != 0);
230a8e1175bSopenharmony_ci
231a8e1175bSopenharmony_ci            a[offset] = 0;
232a8e1175bSopenharmony_ci        }
233a8e1175bSopenharmony_ci    }
234a8e1175bSopenharmony_ci
235a8e1175bSopenharmony_ci
236a8e1175bSopenharmony_ciexit:
237a8e1175bSopenharmony_ci    mbedtls_free(a);
238a8e1175bSopenharmony_ci    mbedtls_free(b);
239a8e1175bSopenharmony_ci}
240a8e1175bSopenharmony_ci/* END_CASE */
241a8e1175bSopenharmony_ci
242a8e1175bSopenharmony_ci/* BEGIN_CASE */
243a8e1175bSopenharmony_civoid mbedtls_ct_memcmp(int same, int size, int offset)
244a8e1175bSopenharmony_ci{
245a8e1175bSopenharmony_ci    uint8_t *a = NULL, *b = NULL;
246a8e1175bSopenharmony_ci    TEST_CALLOC(a, size + offset);
247a8e1175bSopenharmony_ci    TEST_CALLOC(b, size + offset);
248a8e1175bSopenharmony_ci
249a8e1175bSopenharmony_ci    /* Construct data that matches, if same == -1, otherwise
250a8e1175bSopenharmony_ci     * same gives the number of bytes (after the initial offset)
251a8e1175bSopenharmony_ci     * that will match; after that it will differ.
252a8e1175bSopenharmony_ci     */
253a8e1175bSopenharmony_ci    for (int i = 0; i < size + offset; i++) {
254a8e1175bSopenharmony_ci        a[i] = i & 0xff;
255a8e1175bSopenharmony_ci        if (same == -1 || (i - offset) < same) {
256a8e1175bSopenharmony_ci            b[i] = a[i];
257a8e1175bSopenharmony_ci        } else {
258a8e1175bSopenharmony_ci            b[i] = (i + 1) & 0xff;
259a8e1175bSopenharmony_ci        }
260a8e1175bSopenharmony_ci    }
261a8e1175bSopenharmony_ci
262a8e1175bSopenharmony_ci    int reference = memcmp(a + offset, b + offset, size);
263a8e1175bSopenharmony_ci
264a8e1175bSopenharmony_ci    TEST_CF_SECRET(a, size + offset);
265a8e1175bSopenharmony_ci    TEST_CF_SECRET(b, size + offset);
266a8e1175bSopenharmony_ci
267a8e1175bSopenharmony_ci    int actual = mbedtls_ct_memcmp(a + offset, b + offset, size);
268a8e1175bSopenharmony_ci
269a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(a, size + offset);
270a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(b, size + offset);
271a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&actual, sizeof(actual));
272a8e1175bSopenharmony_ci
273a8e1175bSopenharmony_ci    if (same == -1 || same >= size) {
274a8e1175bSopenharmony_ci        TEST_ASSERT(reference == 0);
275a8e1175bSopenharmony_ci        TEST_ASSERT(actual == 0);
276a8e1175bSopenharmony_ci    } else {
277a8e1175bSopenharmony_ci        TEST_ASSERT(reference != 0);
278a8e1175bSopenharmony_ci        TEST_ASSERT(actual != 0);
279a8e1175bSopenharmony_ci    }
280a8e1175bSopenharmony_ciexit:
281a8e1175bSopenharmony_ci    mbedtls_free(a);
282a8e1175bSopenharmony_ci    mbedtls_free(b);
283a8e1175bSopenharmony_ci}
284a8e1175bSopenharmony_ci/* END_CASE */
285a8e1175bSopenharmony_ci
286a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_NIST_KW_C */
287a8e1175bSopenharmony_ci
288a8e1175bSopenharmony_ci/**
289a8e1175bSopenharmony_ci * Generate two arrays of the given size, and test mbedtls_ct_memcmp_partial
290a8e1175bSopenharmony_ci * over them. The arrays will be identical, except that one byte may be specified
291a8e1175bSopenharmony_ci * to be different.
292a8e1175bSopenharmony_ci *
293a8e1175bSopenharmony_ci * \p diff      Index of byte that differs (if out of range, the arrays will match).
294a8e1175bSopenharmony_ci * \p size      Size of arrays to compare
295a8e1175bSopenharmony_ci * \p skip_head Leading bytes to skip, as per mbedtls_ct_memcmp_partial
296a8e1175bSopenharmony_ci * \p skip_tail Trailing bytes to skip, as per mbedtls_ct_memcmp_partial
297a8e1175bSopenharmony_ci */
298a8e1175bSopenharmony_civoid mbedtls_ct_memcmp_partial(int diff, int size, int skip_head, int skip_tail)
299a8e1175bSopenharmony_ci{
300a8e1175bSopenharmony_ci    uint8_t *a = NULL, *b = NULL;
301a8e1175bSopenharmony_ci
302a8e1175bSopenharmony_ci    TEST_CALLOC_NONNULL(a, size);
303a8e1175bSopenharmony_ci    TEST_CALLOC_NONNULL(b, size);
304a8e1175bSopenharmony_ci
305a8e1175bSopenharmony_ci    TEST_ASSERT((skip_head + skip_tail) <= size);
306a8e1175bSopenharmony_ci
307a8e1175bSopenharmony_ci    /* Construct data that matches, except for specified byte (if in range). */
308a8e1175bSopenharmony_ci    for (int i = 0; i < size; i++) {
309a8e1175bSopenharmony_ci        a[i] = i & 0xff;
310a8e1175bSopenharmony_ci        b[i] = a[i];
311a8e1175bSopenharmony_ci        if (i == diff) {
312a8e1175bSopenharmony_ci            // modify the specified byte
313a8e1175bSopenharmony_ci            b[i] ^= 1;
314a8e1175bSopenharmony_ci        }
315a8e1175bSopenharmony_ci    }
316a8e1175bSopenharmony_ci
317a8e1175bSopenharmony_ci    int reference = memcmp(a + skip_head, b + skip_head, size - skip_head - skip_tail);
318a8e1175bSopenharmony_ci
319a8e1175bSopenharmony_ci    TEST_CF_SECRET(a, size);
320a8e1175bSopenharmony_ci    TEST_CF_SECRET(b, size);
321a8e1175bSopenharmony_ci
322a8e1175bSopenharmony_ci    int actual = mbedtls_ct_memcmp_partial(a, b, size, skip_head, skip_tail);
323a8e1175bSopenharmony_ci
324a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(a, size);
325a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(b, size);
326a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&actual, sizeof(actual));
327a8e1175bSopenharmony_ci
328a8e1175bSopenharmony_ci    TEST_EQUAL(!!reference, !!actual);
329a8e1175bSopenharmony_ciexit:
330a8e1175bSopenharmony_ci    mbedtls_free(a);
331a8e1175bSopenharmony_ci    mbedtls_free(b);
332a8e1175bSopenharmony_ci}
333a8e1175bSopenharmony_ci/* END_CASE */
334a8e1175bSopenharmony_ci
335a8e1175bSopenharmony_ci/* BEGIN_CASE */
336a8e1175bSopenharmony_civoid mbedtls_ct_memcpy_if(int eq, int size, int offset)
337a8e1175bSopenharmony_ci{
338a8e1175bSopenharmony_ci    uint8_t *src = NULL, *src2 = NULL, *result = NULL, *expected = NULL;
339a8e1175bSopenharmony_ci    TEST_CALLOC(src, size + offset);
340a8e1175bSopenharmony_ci    TEST_CALLOC(src2, size + offset);
341a8e1175bSopenharmony_ci    TEST_CALLOC(result, size + offset);
342a8e1175bSopenharmony_ci    TEST_CALLOC(expected, size + offset);
343a8e1175bSopenharmony_ci
344a8e1175bSopenharmony_ci    /* Apply offset to result only */
345a8e1175bSopenharmony_ci    for (int i = 0; i < size + offset; i++) {
346a8e1175bSopenharmony_ci        src[i]      = 1;
347a8e1175bSopenharmony_ci        result[i]   = 0xff;
348a8e1175bSopenharmony_ci        expected[i] = eq ? 1 : 0xff;
349a8e1175bSopenharmony_ci    }
350a8e1175bSopenharmony_ci
351a8e1175bSopenharmony_ci    int secret_eq = eq;
352a8e1175bSopenharmony_ci    TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
353a8e1175bSopenharmony_ci    TEST_CF_SECRET(src, size + offset);
354a8e1175bSopenharmony_ci    TEST_CF_SECRET(result, size + offset);
355a8e1175bSopenharmony_ci
356a8e1175bSopenharmony_ci    mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result + offset, src, NULL, size);
357a8e1175bSopenharmony_ci
358a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
359a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src, size + offset);
360a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(result, size + offset);
361a8e1175bSopenharmony_ci
362a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(expected, size, result + offset, size);
363a8e1175bSopenharmony_ci
364a8e1175bSopenharmony_ci
365a8e1175bSopenharmony_ci    /* Apply offset to src only */
366a8e1175bSopenharmony_ci    for (int i = 0; i < size + offset; i++) {
367a8e1175bSopenharmony_ci        src[i]    = 1;
368a8e1175bSopenharmony_ci        result[i] = 0xff;
369a8e1175bSopenharmony_ci        expected[i] = eq ? 1 : 0xff;
370a8e1175bSopenharmony_ci    }
371a8e1175bSopenharmony_ci
372a8e1175bSopenharmony_ci    TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
373a8e1175bSopenharmony_ci    TEST_CF_SECRET(src, size + offset);
374a8e1175bSopenharmony_ci    TEST_CF_SECRET(result, size + offset);
375a8e1175bSopenharmony_ci
376a8e1175bSopenharmony_ci    mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result, src + offset, NULL, size);
377a8e1175bSopenharmony_ci
378a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
379a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src, size + offset);
380a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(result, size + offset);
381a8e1175bSopenharmony_ci
382a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(expected, size, result, size);
383a8e1175bSopenharmony_ci
384a8e1175bSopenharmony_ci
385a8e1175bSopenharmony_ci    /* Apply offset to src and src2 */
386a8e1175bSopenharmony_ci    for (int i = 0; i < size + offset; i++) {
387a8e1175bSopenharmony_ci        src[i]      = 1;
388a8e1175bSopenharmony_ci        src2[i]     = 2;
389a8e1175bSopenharmony_ci        result[i]   = 0xff;
390a8e1175bSopenharmony_ci        expected[i] = eq ? 1 : 2;
391a8e1175bSopenharmony_ci    }
392a8e1175bSopenharmony_ci
393a8e1175bSopenharmony_ci    TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
394a8e1175bSopenharmony_ci    TEST_CF_SECRET(src, size + offset);
395a8e1175bSopenharmony_ci    TEST_CF_SECRET(src2, size + offset);
396a8e1175bSopenharmony_ci    TEST_CF_SECRET(result, size + offset);
397a8e1175bSopenharmony_ci
398a8e1175bSopenharmony_ci    mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result, src + offset, src2 + offset, size);
399a8e1175bSopenharmony_ci
400a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
401a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src, size + offset);
402a8e1175bSopenharmony_ci    TEST_CF_SECRET(src2, size + offset);
403a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(result, size + offset);
404a8e1175bSopenharmony_ci
405a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(expected, size, result, size);
406a8e1175bSopenharmony_ci
407a8e1175bSopenharmony_ci
408a8e1175bSopenharmony_ci    /* result == src == dest */
409a8e1175bSopenharmony_ci    for (int i = 0; i < size + offset; i++) {
410a8e1175bSopenharmony_ci        src[i]      = 2;
411a8e1175bSopenharmony_ci        expected[i] = 2;
412a8e1175bSopenharmony_ci    }
413a8e1175bSopenharmony_ci
414a8e1175bSopenharmony_ci    TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
415a8e1175bSopenharmony_ci    TEST_CF_SECRET(src, size + offset);
416a8e1175bSopenharmony_ci    TEST_CF_SECRET(result, size + offset);
417a8e1175bSopenharmony_ci
418a8e1175bSopenharmony_ci    mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), src + offset, src + offset, src + offset,
419a8e1175bSopenharmony_ci                         size);
420a8e1175bSopenharmony_ci
421a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
422a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(src, size + offset);
423a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(result, size + offset);
424a8e1175bSopenharmony_ci
425a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(expected, size, src + offset, size);
426a8e1175bSopenharmony_ciexit:
427a8e1175bSopenharmony_ci    mbedtls_free(src);
428a8e1175bSopenharmony_ci    mbedtls_free(src2);
429a8e1175bSopenharmony_ci    mbedtls_free(result);
430a8e1175bSopenharmony_ci    mbedtls_free(expected);
431a8e1175bSopenharmony_ci}
432a8e1175bSopenharmony_ci/* END_CASE */
433a8e1175bSopenharmony_ci
434a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:!MBEDTLS_RSA_ALT */
435a8e1175bSopenharmony_civoid mbedtls_ct_memmove_left(int len, int offset)
436a8e1175bSopenharmony_ci{
437a8e1175bSopenharmony_ci    size_t l = (size_t) len;
438a8e1175bSopenharmony_ci    size_t o = (size_t) offset;
439a8e1175bSopenharmony_ci
440a8e1175bSopenharmony_ci    uint8_t *buf = NULL, *buf_expected = NULL;
441a8e1175bSopenharmony_ci    TEST_CALLOC(buf, l);
442a8e1175bSopenharmony_ci    TEST_CALLOC(buf_expected, l);
443a8e1175bSopenharmony_ci
444a8e1175bSopenharmony_ci    for (size_t i = 0; i < l; i++) {
445a8e1175bSopenharmony_ci        buf[i] = (uint8_t) i;
446a8e1175bSopenharmony_ci        buf_expected[i] = buf[i];
447a8e1175bSopenharmony_ci    }
448a8e1175bSopenharmony_ci
449a8e1175bSopenharmony_ci    TEST_CF_SECRET(&o, sizeof(o));
450a8e1175bSopenharmony_ci    TEST_CF_SECRET(buf, l);
451a8e1175bSopenharmony_ci    mbedtls_ct_memmove_left(buf, l, o);
452a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(&o, sizeof(o));
453a8e1175bSopenharmony_ci    TEST_CF_PUBLIC(buf, l);
454a8e1175bSopenharmony_ci
455a8e1175bSopenharmony_ci    if (l > 0) {
456a8e1175bSopenharmony_ci        memmove(buf_expected, buf_expected + o, l - o);
457a8e1175bSopenharmony_ci        memset(buf_expected + (l - o), 0, o);
458a8e1175bSopenharmony_ci        TEST_ASSERT(memcmp(buf, buf_expected, l) == 0);
459a8e1175bSopenharmony_ci    }
460a8e1175bSopenharmony_ciexit:
461a8e1175bSopenharmony_ci    mbedtls_free(buf);
462a8e1175bSopenharmony_ci    mbedtls_free(buf_expected);
463a8e1175bSopenharmony_ci}
464a8e1175bSopenharmony_ci/* END_CASE */
465a8e1175bSopenharmony_ci
466a8e1175bSopenharmony_ci/* BEGIN_CASE */
467a8e1175bSopenharmony_civoid mbedtls_ct_memcpy_offset(int offset_min, int offset_max, int len)
468a8e1175bSopenharmony_ci{
469a8e1175bSopenharmony_ci    unsigned char *dst = NULL;
470a8e1175bSopenharmony_ci    unsigned char *src = NULL;
471a8e1175bSopenharmony_ci    size_t src_len = offset_max + len;
472a8e1175bSopenharmony_ci    size_t secret;
473a8e1175bSopenharmony_ci
474a8e1175bSopenharmony_ci    TEST_CALLOC(dst, len);
475a8e1175bSopenharmony_ci    TEST_CALLOC(src, src_len);
476a8e1175bSopenharmony_ci
477a8e1175bSopenharmony_ci    /* Fill src in a way that we can detect if we copied the right bytes */
478a8e1175bSopenharmony_ci    mbedtls_test_rnd_std_rand(NULL, src, src_len);
479a8e1175bSopenharmony_ci
480a8e1175bSopenharmony_ci    for (secret = offset_min; secret <= (size_t) offset_max; secret++) {
481a8e1175bSopenharmony_ci        mbedtls_test_set_step((int) secret);
482a8e1175bSopenharmony_ci
483a8e1175bSopenharmony_ci        TEST_CF_SECRET(&secret, sizeof(secret));
484a8e1175bSopenharmony_ci        TEST_CF_SECRET(src, len);
485a8e1175bSopenharmony_ci        TEST_CF_SECRET(dst, len);
486a8e1175bSopenharmony_ci        mbedtls_ct_memcpy_offset(dst, src, secret,
487a8e1175bSopenharmony_ci                                 offset_min, offset_max, len);
488a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(&secret, sizeof(secret));
489a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(src, len);
490a8e1175bSopenharmony_ci        TEST_CF_PUBLIC(dst, len);
491a8e1175bSopenharmony_ci
492a8e1175bSopenharmony_ci        TEST_MEMORY_COMPARE(dst, len, src + secret, len);
493a8e1175bSopenharmony_ci    }
494a8e1175bSopenharmony_ci
495a8e1175bSopenharmony_ciexit:
496a8e1175bSopenharmony_ci    mbedtls_free(dst);
497a8e1175bSopenharmony_ci    mbedtls_free(src);
498a8e1175bSopenharmony_ci}
499a8e1175bSopenharmony_ci/* END_CASE */
500