Add unit tests for mbedtls_ct_memcmp and mbedtls_ct_memcpy_if_eq

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2022-12-23 12:27:04 +00:00
parent 52a7aeebf3
commit 39188c0a2a
2 changed files with 185 additions and 0 deletions

View file

@ -9,3 +9,111 @@ ssl_cf_memcpy_offset:0:255:32
# we could get this with 255-bytes plaintext and untruncated SHA-384
Constant-flow memcpy from offset: large
ssl_cf_memcpy_offset:100:339:48
mbedtls_ct_memcmp NULL
mbedtls_ct_memcmp_null
mbedtls_ct_memcmp len 1
mbedtls_ct_memcmp:1:1:0
mbedtls_ct_memcmp len 3
mbedtls_ct_memcmp:1:3:0
mbedtls_ct_memcmp len 4
mbedtls_ct_memcmp:1:4:0
mbedtls_ct_memcmp len 5
mbedtls_ct_memcmp:1:5:0
mbedtls_ct_memcmp len 15
mbedtls_ct_memcmp:1:15:0
mbedtls_ct_memcmp len 16
mbedtls_ct_memcmp:1:16:0
mbedtls_ct_memcmp len 17
mbedtls_ct_memcmp:1:17:0
mbedtls_ct_memcmp len 1 different
mbedtls_ct_memcmp:0:1:0
mbedtls_ct_memcmp len 17 different
mbedtls_ct_memcmp:0:17:0
mbedtls_ct_memcmp len 1 offset 1 different
mbedtls_ct_memcmp:0:1:1
mbedtls_ct_memcmp len 17 offset 1 different
mbedtls_ct_memcmp:0:17:1
mbedtls_ct_memcmp len 1 offset 1
mbedtls_ct_memcmp:1:1:1
mbedtls_ct_memcmp len 1 offset 2
mbedtls_ct_memcmp:1:1:2
mbedtls_ct_memcmp len 1 offset 3
mbedtls_ct_memcmp:1:1:3
mbedtls_ct_memcmp len 5 offset 1
mbedtls_ct_memcmp:1:5:1
mbedtls_ct_memcmp len 5 offset 2
mbedtls_ct_memcmp:1:5:2
mbedtls_ct_memcmp len 5 offset 3
mbedtls_ct_memcmp:1:5:3
mbedtls_ct_memcmp len 17 offset 1
mbedtls_ct_memcmp:1:17:1
mbedtls_ct_memcmp len 17 offset 2
mbedtls_ct_memcmp:1:17:2
mbedtls_ct_memcmp len 17 offset 3
mbedtls_ct_memcmp:1:17:3
mbedtls_ct_memcpy_if_eq len 1 offset 0
mbedtls_ct_memcpy_if_eq:1:1:0
mbedtls_ct_memcpy_if_eq len 1 offset 1
mbedtls_ct_memcpy_if_eq:1:1:1
mbedtls_ct_memcpy_if_eq len 4 offset 0
mbedtls_ct_memcpy_if_eq:1:1:0
mbedtls_ct_memcpy_if_eq len 4 offset 1
mbedtls_ct_memcpy_if_eq:1:1:1
mbedtls_ct_memcpy_if_eq len 4 offset 2
mbedtls_ct_memcpy_if_eq:1:1:2
mbedtls_ct_memcpy_if_eq len 4 offset 3
mbedtls_ct_memcpy_if_eq:1:1:3
mbedtls_ct_memcpy_if_eq len 15 offset 0
mbedtls_ct_memcpy_if_eq:1:15:0
mbedtls_ct_memcpy_if_eq len 15 offset 1
mbedtls_ct_memcpy_if_eq:1:15:1
mbedtls_ct_memcpy_if_eq len 16 offset 0
mbedtls_ct_memcpy_if_eq:1:16:0
mbedtls_ct_memcpy_if_eq len 16 offset 1
mbedtls_ct_memcpy_if_eq:1:16:1
mbedtls_ct_memcpy_if_eq len 17 offset 0
mbedtls_ct_memcpy_if_eq:1:17:0
mbedtls_ct_memcpy_if_eq len 17 offset 1
mbedtls_ct_memcpy_if_eq:1:17:1
mbedtls_ct_memcpy_if_eq len 0 not eq
mbedtls_ct_memcpy_if_eq:0:17:0
mbedtls_ct_memcpy_if_eq len 5 offset 1 not eq
mbedtls_ct_memcpy_if_eq:0:5:1
mbedtls_ct_memcpy_if_eq len 17 offset 3 not eq
mbedtls_ct_memcpy_if_eq:0:17:3

View file

@ -15,6 +15,83 @@
#include <test/constant_flow.h>
/* END_HEADER */
#include <stdio.h>
/* BEGIN_CASE */
void mbedtls_ct_memcmp_null()
{
uint32_t x;
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 */
/* BEGIN_CASE */
void mbedtls_ct_memcmp(int same, int size, int offset)
{
uint8_t *a = NULL, *b = NULL;
ASSERT_ALLOC(a, size + offset);
ASSERT_ALLOC(b, size + offset);
TEST_CF_SECRET(a + offset, size);
TEST_CF_SECRET(b + offset, size);
for (int i = 0; i < size + offset; i++) {
a[i] = i & 0xff;
b[i] = (i & 0xff) + (same ? 0 : 1);
}
int reference = memcmp(a + offset, b + offset, size);
int actual = mbedtls_ct_memcmp(a + offset, b + offset, size);
TEST_CF_PUBLIC(a + offset, size);
TEST_CF_PUBLIC(b + offset, size);
if (same != 0) {
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 */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_MAC */
void mbedtls_ct_memcpy_if_eq(int eq, int size, int offset)
{
uint8_t *src = NULL, *result = NULL, *expected = NULL;
ASSERT_ALLOC(src, size + offset);
ASSERT_ALLOC(result, size + offset);
ASSERT_ALLOC(expected, size + offset);
for (int i = 0; i < size + offset; i++) {
src[i] = 1;
result[i] = 0xff;
expected[i] = eq ? 1 : 0xff;
}
mbedtls_ct_memcpy_if_eq(result + offset, src, size, eq, 1);
ASSERT_COMPARE(expected, size, result + offset, size);
for (int i = 0; i < size + offset; i++) {
src[i] = 1;
result[i] = 0xff;
expected[i] = eq ? 1 : 0xff;
}
mbedtls_ct_memcpy_if_eq(result, src + offset, size, eq, 1);
ASSERT_COMPARE(expected, size, result, size);
exit:
mbedtls_free(src);
mbedtls_free(result);
mbedtls_free(expected);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
{