2022-11-29 15:44:21 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
/** \file test_suite_constant_time.function
|
|
|
|
*
|
|
|
|
* Functional testing of functions in the constant_time module.
|
|
|
|
*
|
|
|
|
* The tests are instrumented with #TEST_CF_SECRET and #TEST_CF_PUBLIC
|
|
|
|
* (see tests/include/test/constant_flow.h) so that running the tests
|
|
|
|
* under MSan or Valgrind will detect a non-constant-time implementation.
|
|
|
|
*/
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <mbedtls/bignum.h>
|
2022-11-29 15:44:21 +01:00
|
|
|
#include <mbedtls/constant_time.h>
|
|
|
|
#include <constant_time_internal.h>
|
|
|
|
|
2023-07-31 17:34:19 +02:00
|
|
|
#include <test/constant_flow.h>
|
2022-11-29 15:44:21 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
2022-12-23 13:27:04 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_memcmp_null()
|
|
|
|
{
|
2023-02-13 19:15:30 +01:00
|
|
|
uint32_t x = 0;
|
2022-12-23 13:27:04 +01:00
|
|
|
TEST_ASSERT(mbedtls_ct_memcmp(&x, NULL, 0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ct_memcmp(NULL, &x, 0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ct_memcmp(NULL, NULL, 0) == 0);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_bool(char *input)
|
|
|
|
{
|
|
|
|
mbedtls_ct_uint_t v = (mbedtls_ct_uint_t) strtoull(input, NULL, 16);
|
|
|
|
TEST_ASSERT(errno == 0);
|
|
|
|
|
|
|
|
mbedtls_ct_condition_t expected = (v != 0) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
|
|
|
TEST_CF_SECRET(&v, sizeof(v));
|
|
|
|
TEST_EQUAL(mbedtls_ct_bool(v), expected);
|
|
|
|
TEST_CF_PUBLIC(&v, sizeof(v));
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_bool_xxx(char *x_str, char *y_str)
|
|
|
|
{
|
|
|
|
mbedtls_ct_uint_t x = strtoull(x_str, NULL, 0);
|
|
|
|
mbedtls_ct_uint_t y = strtoull(y_str, NULL, 0);
|
|
|
|
|
|
|
|
mbedtls_ct_uint_t x1 = x;
|
|
|
|
mbedtls_ct_uint_t y1 = y;
|
|
|
|
|
|
|
|
TEST_CF_SECRET(&x, sizeof(x));
|
|
|
|
TEST_CF_SECRET(&y, sizeof(y));
|
|
|
|
|
|
|
|
mbedtls_ct_condition_t expected = x1 ? MBEDTLS_CT_FALSE : MBEDTLS_CT_TRUE;
|
|
|
|
TEST_EQUAL(mbedtls_ct_bool_not(mbedtls_ct_bool(x)), expected);
|
|
|
|
|
|
|
|
expected = x1 != y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_ne(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
expected = x1 == y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_eq(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
expected = x1 > y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_gt(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
expected = x1 < y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_lt(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
expected = x1 >= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_ge(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
expected = x1 <= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-08-10 12:58:18 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uint_le(x, y), expected);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
2023-07-31 13:35:26 +02:00
|
|
|
expected = (!!x1) ^ (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_bool_xor(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
|
|
|
|
|
2023-07-31 13:35:26 +02:00
|
|
|
expected = (!!x1) && (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_bool_and(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
|
|
|
|
|
2023-07-31 13:35:26 +02:00
|
|
|
expected = (!!x1) || (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_bool_or(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
|
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&x, sizeof(x));
|
|
|
|
TEST_CF_PUBLIC(&y, sizeof(y));
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_BASE64_C */
|
2023-05-18 12:23:59 +02:00
|
|
|
void mbedtls_ct_uchar_in_range_if(int li, int hi, int ti)
|
2023-05-11 17:19:27 +02:00
|
|
|
{
|
2023-05-18 12:23:59 +02:00
|
|
|
unsigned char l = li, h = hi, t = ti;
|
2023-05-11 17:19:27 +02:00
|
|
|
|
2023-05-18 12:23:59 +02:00
|
|
|
for (unsigned x = 0; x <= 255; x++) {
|
|
|
|
unsigned char expected = (x >= l) && (x <= h) ? t : 0;
|
2023-05-11 17:19:27 +02:00
|
|
|
|
2023-05-18 12:23:59 +02:00
|
|
|
TEST_CF_SECRET(&x, sizeof(x));
|
|
|
|
TEST_CF_SECRET(&l, sizeof(l));
|
|
|
|
TEST_CF_SECRET(&h, sizeof(h));
|
|
|
|
TEST_CF_SECRET(&t, sizeof(t));
|
2023-05-11 17:19:27 +02:00
|
|
|
|
2023-05-18 12:23:59 +02:00
|
|
|
TEST_EQUAL(mbedtls_ct_uchar_in_range_if(l, h, (unsigned char) x, t), expected);
|
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&x, sizeof(x));
|
|
|
|
TEST_CF_PUBLIC(&l, sizeof(l));
|
|
|
|
TEST_CF_PUBLIC(&h, sizeof(h));
|
|
|
|
TEST_CF_PUBLIC(&t, sizeof(t));
|
|
|
|
}
|
2023-05-11 17:19:27 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_if(char *c_str, char *t_str, char *f_str)
|
|
|
|
{
|
|
|
|
mbedtls_ct_condition_t c = mbedtls_ct_bool(strtoull(c_str, NULL, 16));
|
|
|
|
mbedtls_ct_uint_t t = (mbedtls_ct_uint_t) strtoull(t_str, NULL, 16);
|
|
|
|
mbedtls_ct_uint_t f = (mbedtls_ct_uint_t) strtoull(f_str, NULL, 16);
|
|
|
|
|
|
|
|
mbedtls_ct_uint_t expected = c ? t : f;
|
|
|
|
mbedtls_ct_uint_t expected0 = c ? t : 0;
|
|
|
|
|
|
|
|
TEST_CF_SECRET(&c, sizeof(c));
|
|
|
|
TEST_CF_SECRET(&t, sizeof(t));
|
|
|
|
TEST_CF_SECRET(&f, sizeof(f));
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_ct_if(c, t, f), expected);
|
|
|
|
TEST_EQUAL(mbedtls_ct_size_if(c, t, f), (size_t) expected);
|
|
|
|
TEST_EQUAL(mbedtls_ct_uint_if(c, t, f), (unsigned) expected);
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
TEST_EQUAL(mbedtls_ct_mpi_uint_if(c, t, f), (mbedtls_mpi_uint) expected);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_ct_uint_if0(c, t), (unsigned) expected0);
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
TEST_EQUAL(mbedtls_ct_mpi_uint_if0(c, t), (mbedtls_mpi_uint) expected0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&c, sizeof(c));
|
|
|
|
TEST_CF_PUBLIC(&t, sizeof(t));
|
|
|
|
TEST_CF_PUBLIC(&f, sizeof(f));
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:!MBEDTLS_RSA_ALT */
|
|
|
|
void mbedtls_ct_zeroize_if(char *c_str, int len)
|
|
|
|
{
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
mbedtls_ct_condition_t c = mbedtls_ct_bool(strtoull(c_str, NULL, 16));
|
|
|
|
|
2023-08-07 12:49:12 +02:00
|
|
|
TEST_CALLOC(buf, len);
|
2023-05-11 17:19:27 +02:00
|
|
|
for (size_t i = 0; i < (size_t) len; i++) {
|
|
|
|
buf[i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CF_SECRET(&c, sizeof(c));
|
|
|
|
TEST_CF_SECRET(buf, len);
|
|
|
|
mbedtls_ct_zeroize_if(c, buf, len);
|
|
|
|
TEST_CF_PUBLIC(&c, sizeof(c));
|
|
|
|
TEST_CF_PUBLIC(buf, len);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < (size_t) len; i++) {
|
|
|
|
TEST_EQUAL(buf[i], c != 0 ? 0 : 1);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
mbedtls_free(buf);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-12-23 13:27:04 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_memcmp(int same, int size, int offset)
|
|
|
|
{
|
|
|
|
uint8_t *a = NULL, *b = NULL;
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(a, size + offset);
|
|
|
|
TEST_CALLOC(b, size + offset);
|
2022-12-23 13:27:04 +01:00
|
|
|
|
2023-01-21 11:29:00 +01:00
|
|
|
/* Construct data that matches, if same == -1, otherwise
|
|
|
|
* same gives the number of bytes (after the initial offset)
|
|
|
|
* that will match; after that it will differ.
|
|
|
|
*/
|
2022-12-23 13:27:04 +01:00
|
|
|
for (int i = 0; i < size + offset; i++) {
|
|
|
|
a[i] = i & 0xff;
|
2023-01-21 11:29:00 +01:00
|
|
|
if (same == -1 || (i - offset) < same) {
|
|
|
|
b[i] = a[i];
|
|
|
|
} else {
|
|
|
|
b[i] = (i + 1) & 0xff;
|
|
|
|
}
|
2022-12-23 13:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int reference = memcmp(a + offset, b + offset, size);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
TEST_CF_SECRET(a, size + offset);
|
|
|
|
TEST_CF_SECRET(b, size + offset);
|
|
|
|
|
2022-12-23 13:27:04 +01:00
|
|
|
int actual = mbedtls_ct_memcmp(a + offset, b + offset, size);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
TEST_CF_PUBLIC(a, size + offset);
|
|
|
|
TEST_CF_PUBLIC(b, size + offset);
|
|
|
|
TEST_CF_PUBLIC(&actual, sizeof(actual));
|
2022-12-23 13:27:04 +01:00
|
|
|
|
2023-01-21 11:29:00 +01:00
|
|
|
if (same == -1 || same >= size) {
|
2022-12-23 13:27:04 +01:00
|
|
|
TEST_ASSERT(reference == 0);
|
|
|
|
TEST_ASSERT(actual == 0);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(reference != 0);
|
|
|
|
TEST_ASSERT(actual != 0);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
mbedtls_free(a);
|
|
|
|
mbedtls_free(b);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_ct_memcpy_if(int eq, int size, int offset)
|
2022-12-23 13:27:04 +01:00
|
|
|
{
|
2023-05-11 17:19:27 +02:00
|
|
|
uint8_t *src = NULL, *src2 = NULL, *result = NULL, *expected = NULL;
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(src, size + offset);
|
2023-08-07 12:47:35 +02:00
|
|
|
TEST_CALLOC(src2, size + offset);
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(result, size + offset);
|
|
|
|
TEST_CALLOC(expected, size + offset);
|
2022-12-23 13:27:04 +01:00
|
|
|
|
2023-05-18 14:47:13 +02:00
|
|
|
/* Apply offset to result only */
|
2022-12-23 13:27:04 +01:00
|
|
|
for (int i = 0; i < size + offset; i++) {
|
2023-05-11 17:19:27 +02:00
|
|
|
src[i] = 1;
|
|
|
|
result[i] = 0xff;
|
2022-12-23 13:27:04 +01:00
|
|
|
expected[i] = eq ? 1 : 0xff;
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
int secret_eq = eq;
|
|
|
|
TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_SECRET(src, size + offset);
|
|
|
|
TEST_CF_SECRET(result, size + offset);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result + offset, src, NULL, size);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_PUBLIC(src, size + offset);
|
|
|
|
TEST_CF_PUBLIC(result, size + offset);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected, size, result + offset, size);
|
2022-12-23 13:27:04 +01:00
|
|
|
|
2023-05-18 14:47:13 +02:00
|
|
|
|
|
|
|
/* Apply offset to src only */
|
2022-12-23 13:27:04 +01:00
|
|
|
for (int i = 0; i < size + offset; i++) {
|
|
|
|
src[i] = 1;
|
|
|
|
result[i] = 0xff;
|
|
|
|
expected[i] = eq ? 1 : 0xff;
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_SECRET(src, size + offset);
|
|
|
|
TEST_CF_SECRET(result, size + offset);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result, src + offset, NULL, size);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_PUBLIC(src, size + offset);
|
|
|
|
TEST_CF_PUBLIC(result, size + offset);
|
|
|
|
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected, size, result, size);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
2023-05-18 14:47:13 +02:00
|
|
|
|
|
|
|
/* Apply offset to src and src2 */
|
2023-05-11 17:19:27 +02:00
|
|
|
for (int i = 0; i < size + offset; i++) {
|
|
|
|
src[i] = 1;
|
|
|
|
src2[i] = 2;
|
|
|
|
result[i] = 0xff;
|
|
|
|
expected[i] = eq ? 1 : 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_SECRET(src, size + offset);
|
|
|
|
TEST_CF_SECRET(src2, size + offset);
|
|
|
|
TEST_CF_SECRET(result, size + offset);
|
|
|
|
|
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), result, src + offset, src2 + offset, size);
|
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_PUBLIC(src, size + offset);
|
|
|
|
TEST_CF_SECRET(src2, size + offset);
|
|
|
|
TEST_CF_PUBLIC(result, size + offset);
|
2023-01-21 12:00:30 +01:00
|
|
|
|
2023-08-07 12:47:35 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected, size, result, size);
|
2023-05-18 14:47:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* result == src == dest */
|
|
|
|
for (int i = 0; i < size + offset; i++) {
|
|
|
|
src[i] = 2;
|
|
|
|
expected[i] = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_SECRET(src, size + offset);
|
|
|
|
TEST_CF_SECRET(result, size + offset);
|
|
|
|
|
2023-05-18 21:41:02 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_bool(secret_eq), src + offset, src + offset, src + offset,
|
|
|
|
size);
|
2023-05-18 14:47:13 +02:00
|
|
|
|
|
|
|
TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
|
|
|
|
TEST_CF_PUBLIC(src, size + offset);
|
|
|
|
TEST_CF_PUBLIC(result, size + offset);
|
|
|
|
|
2023-08-07 12:47:35 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected, size, src + offset, size);
|
2022-12-23 13:27:04 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_free(src);
|
2023-05-11 17:19:27 +02:00
|
|
|
mbedtls_free(src2);
|
2022-12-23 13:27:04 +01:00
|
|
|
mbedtls_free(result);
|
|
|
|
mbedtls_free(expected);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-05-11 17:19:27 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:!MBEDTLS_RSA_ALT */
|
|
|
|
void mbedtls_ct_memmove_left(int len, int offset)
|
|
|
|
{
|
|
|
|
size_t l = (size_t) len;
|
|
|
|
size_t o = (size_t) offset;
|
|
|
|
|
|
|
|
uint8_t *buf = NULL, *buf_expected = NULL;
|
2023-08-07 12:49:12 +02:00
|
|
|
TEST_CALLOC(buf, l);
|
|
|
|
TEST_CALLOC(buf_expected, l);
|
2023-05-11 17:19:27 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < l; i++) {
|
|
|
|
buf[i] = (uint8_t) i;
|
|
|
|
buf_expected[i] = buf[i];
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:13:52 +02:00
|
|
|
TEST_CF_SECRET(&o, sizeof(o));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_SECRET(buf, l);
|
|
|
|
mbedtls_ct_memmove_left(buf, l, o);
|
2023-07-28 17:13:52 +02:00
|
|
|
TEST_CF_PUBLIC(&o, sizeof(o));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_PUBLIC(buf, l);
|
|
|
|
|
|
|
|
if (l > 0) {
|
|
|
|
memmove(buf_expected, buf_expected + o, l - o);
|
|
|
|
memset(buf_expected + (l - o), 0, o);
|
|
|
|
TEST_ASSERT(memcmp(buf, buf_expected, l) == 0);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
mbedtls_free(buf);
|
|
|
|
mbedtls_free(buf_expected);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-07-31 18:28:26 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-05-11 17:19:27 +02:00
|
|
|
void mbedtls_ct_memcpy_offset(int offset_min, int offset_max, int len)
|
2022-11-29 15:44:21 +01:00
|
|
|
{
|
|
|
|
unsigned char *dst = NULL;
|
|
|
|
unsigned char *src = NULL;
|
|
|
|
size_t src_len = offset_max + len;
|
|
|
|
size_t secret;
|
|
|
|
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(dst, len);
|
|
|
|
TEST_CALLOC(src, src_len);
|
2022-11-29 15:44:21 +01:00
|
|
|
|
|
|
|
/* Fill src in a way that we can detect if we copied the right bytes */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand(NULL, src, src_len);
|
2022-11-29 15:44:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (secret = offset_min; secret <= (size_t) offset_max; secret++) {
|
|
|
|
mbedtls_test_set_step((int) secret);
|
2022-11-29 15:44:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(&secret, sizeof(secret));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_SECRET(src, len);
|
|
|
|
TEST_CF_SECRET(dst, len);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_memcpy_offset(dst, src, secret,
|
|
|
|
offset_min, offset_max, len);
|
|
|
|
TEST_CF_PUBLIC(&secret, sizeof(secret));
|
2023-05-11 17:19:27 +02:00
|
|
|
TEST_CF_PUBLIC(src, len);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(dst, len);
|
2022-11-29 15:44:21 +01:00
|
|
|
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(dst, len, src + secret, len);
|
2022-11-29 15:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(dst);
|
|
|
|
mbedtls_free(src);
|
2022-11-29 15:44:21 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|