blob: a40149ab439e9956b6cc67efc032bc0068bb957a [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
18/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +010019void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010020{
21 unsigned char *dst = NULL;
22 unsigned char *src = NULL;
23 size_t src_len = offset_max + len;
24 size_t secret;
25
Gilles Peskine449bd832023-01-11 14:50:10 +010026 ASSERT_ALLOC(dst, len);
27 ASSERT_ALLOC(src, src_len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010028
29 /* Fill src in a way that we can detect if we copied the right bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +010030 mbedtls_test_rnd_std_rand(NULL, src, src_len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 for (secret = offset_min; secret <= (size_t) offset_max; secret++) {
33 mbedtls_test_set_step((int) secret);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010034
Gilles Peskine449bd832023-01-11 14:50:10 +010035 TEST_CF_SECRET(&secret, sizeof(secret));
36 mbedtls_ct_memcpy_offset(dst, src, secret,
37 offset_min, offset_max, len);
38 TEST_CF_PUBLIC(&secret, sizeof(secret));
39 TEST_CF_PUBLIC(dst, len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 ASSERT_COMPARE(dst, len, src + secret, len);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010042 }
43
44exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010045 mbedtls_free(dst);
46 mbedtls_free(src);
Gilles Peskine3ffd6bc2022-11-29 15:44:21 +010047}
48/* END_CASE */