Test memcmp with differences starting after the first byte

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2023-01-21 10:29:00 +00:00
parent 7658b63390
commit 22b0d1adbf
2 changed files with 56 additions and 30 deletions

View file

@ -14,25 +14,25 @@ mbedtls_ct_memcmp NULL
mbedtls_ct_memcmp_null
mbedtls_ct_memcmp len 1
mbedtls_ct_memcmp:1:1:0
mbedtls_ct_memcmp:-1:1:0
mbedtls_ct_memcmp len 3
mbedtls_ct_memcmp:1:3:0
mbedtls_ct_memcmp:-1:3:0
mbedtls_ct_memcmp len 4
mbedtls_ct_memcmp:1:4:0
mbedtls_ct_memcmp:-1:4:0
mbedtls_ct_memcmp len 5
mbedtls_ct_memcmp:1:5:0
mbedtls_ct_memcmp:-1:5:0
mbedtls_ct_memcmp len 15
mbedtls_ct_memcmp:1:15:0
mbedtls_ct_memcmp:-1:15:0
mbedtls_ct_memcmp len 16
mbedtls_ct_memcmp:1:16:0
mbedtls_ct_memcmp:-1:16:0
mbedtls_ct_memcmp len 17
mbedtls_ct_memcmp:1:17:0
mbedtls_ct_memcmp:-1:17:0
mbedtls_ct_memcmp len 1 different
mbedtls_ct_memcmp:0:1:0
@ -40,38 +40,56 @@ mbedtls_ct_memcmp:0:1:0
mbedtls_ct_memcmp len 17 different
mbedtls_ct_memcmp:0:17:0
mbedtls_ct_memcmp len 17 different 1
mbedtls_ct_memcmp:1:17:0
mbedtls_ct_memcmp len 17 different 4
mbedtls_ct_memcmp:4:17:0
mbedtls_ct_memcmp len 17 different 10
mbedtls_ct_memcmp:10:17:0
mbedtls_ct_memcmp len 17 different 16
mbedtls_ct_memcmp:16: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 len 17 offset 1 different 1
mbedtls_ct_memcmp:1:17:1
mbedtls_ct_memcmp len 17 offset 1 different 5
mbedtls_ct_memcmp:5: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:-1:17:2
mbedtls_ct_memcmp len 17 offset 3
mbedtls_ct_memcmp:1:17:3
mbedtls_ct_memcmp:-1:17:3
mbedtls_ct_memcpy_if_eq len 1 offset 0
mbedtls_ct_memcpy_if_eq:1:1:0

View file

@ -35,9 +35,17 @@ void mbedtls_ct_memcmp(int same, int size, int offset)
TEST_CF_SECRET(a + offset, size);
TEST_CF_SECRET(b + offset, size);
/* 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.
*/
for (int i = 0; i < size + offset; i++) {
a[i] = i & 0xff;
b[i] = (i & 0xff) + (same ? 0 : 1);
if (same == -1 || (i - offset) < same) {
b[i] = a[i];
} else {
b[i] = (i + 1) & 0xff;
}
}
int reference = memcmp(a + offset, b + offset, size);
@ -45,7 +53,7 @@ void mbedtls_ct_memcmp(int same, int size, int offset)
TEST_CF_PUBLIC(a + offset, size);
TEST_CF_PUBLIC(b + offset, size);
if (same != 0) {
if (same == -1 || same >= size) {
TEST_ASSERT(reference == 0);
TEST_ASSERT(actual == 0);
} else {