memory: Add helper function to conditionally copy data in constant time

This commit is contained in:
Tobias Brunner
2024-11-22 14:09:53 +01:00
parent 3b7c49bc31
commit 930381228b
3 changed files with 68 additions and 0 deletions
@@ -539,6 +539,40 @@ START_TEST(test_memeq_const)
}
END_TEST
/*******************************************************************************
* memcpy_cond
*/
#define MEMCPY_COND_DEF "abcdef"
static struct {
char *a;
size_t n;
uint8_t cond;
char *res;
} memcpy_cond_data[] = {
{NULL, 0, 0, NULL},
{NULL, 0, 1, NULL},
{"foobar", 6, 0, MEMCPY_COND_DEF},
{"foobar", 6, 1, "foobar"},
{"foobar", 3, 0, MEMCPY_COND_DEF},
{"foobar", 3, 1, "foodef"},
{"\0bar", 4, 0, MEMCPY_COND_DEF},
{"\0bar", 4, 1, "\0baref"},
/* unpredictable results if condition is not 0 or 1 */
{"foobar", 6, 5, "bkkfev"},
};
START_TEST(test_memcpy_cond)
{
char buf[BUF_LEN] = MEMCPY_COND_DEF;
memcpy_cond(buf, memcpy_cond_data[_i].a, memcpy_cond_data[_i].n,
memcpy_cond_data[_i].cond);
ck_assert(memeq(buf, memcpy_cond_data[_i].res ?: MEMCPY_COND_DEF,
sizeof(MEMCPY_COND_DEF)));
}
END_TEST
/*******************************************************************************
* memstr
*/
@@ -1346,6 +1380,10 @@ Suite *utils_suite_create()
tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
suite_add_tcase(s, tc);
tc = tcase_create("memcpy_cond");
tcase_add_loop_test(tc, test_memcpy_cond, 0, countof(memcpy_cond_data));
suite_add_tcase(s, tc);
tc = tcase_create("memstr");
tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
suite_add_tcase(s, tc);