Gilles Peskine | 3ffd6bc | 2022-11-29 15:44:21 +0100 | [diff] [blame] | 1 | /* 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 */ |
| 19 | void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len ) |
| 20 | { |
| 21 | unsigned char *dst = NULL; |
| 22 | unsigned char *src = NULL; |
| 23 | size_t src_len = offset_max + len; |
| 24 | size_t secret; |
| 25 | |
| 26 | ASSERT_ALLOC( dst, len ); |
| 27 | ASSERT_ALLOC( src, src_len ); |
| 28 | |
| 29 | /* Fill src in a way that we can detect if we copied the right bytes */ |
| 30 | mbedtls_test_rnd_std_rand( NULL, src, src_len ); |
| 31 | |
| 32 | for( secret = offset_min; secret <= (size_t) offset_max; secret++ ) |
| 33 | { |
| 34 | mbedtls_test_set_step( (int) secret ); |
| 35 | |
| 36 | TEST_CF_SECRET( &secret, sizeof( secret ) ); |
| 37 | mbedtls_ct_memcpy_offset( dst, src, secret, |
| 38 | offset_min, offset_max, len ); |
| 39 | TEST_CF_PUBLIC( &secret, sizeof( secret ) ); |
| 40 | TEST_CF_PUBLIC( dst, len ); |
| 41 | |
| 42 | ASSERT_COMPARE( dst, len, src + secret, len ); |
| 43 | } |
| 44 | |
| 45 | exit: |
| 46 | mbedtls_free( dst ); |
| 47 | mbedtls_free( src ); |
| 48 | } |
| 49 | /* END_CASE */ |