utils: Add a constant time memeq() variant for cryptographic purposes
[strongswan.git] / src / libstrongswan / tests / suites / test_utils.c
index 85a8544..f151fb3 100644 (file)
@@ -307,6 +307,48 @@ START_TEST(test_memxor_aligned)
 END_TEST
 
 /*******************************************************************************
+ * memeq/const
+ */
+
+static struct {
+       char *a;
+       char *b;
+       size_t n;
+       bool res;
+} memeq_data[] = {
+       {NULL, NULL, 0, TRUE},
+       {"a", "b", 0, TRUE},
+       {"", "", 1, TRUE},
+       {"abcdefgh", "abcdefgh", 8, TRUE},
+       {"a", "b", 1, FALSE},
+       {"A", "a", 1, FALSE},
+       {"\0a", "\0b", 2, FALSE},
+       {"abc", "abd", 3, FALSE},
+       {"abc", "dbd", 3, FALSE},
+       {"abcdefgh", "abcdffgh", 8, FALSE},
+       {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
+        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, TRUE},
+       {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
+        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyy", 52, FALSE},
+       {"bbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
+        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, FALSE},
+};
+
+START_TEST(test_memeq)
+{
+       ck_assert(memeq(memeq_data[_i].a, memeq_data[_i].b,
+                                       memeq_data[_i].n) == memeq_data[_i].res);
+}
+END_TEST
+
+START_TEST(test_memeq_const)
+{
+       ck_assert(memeq_const(memeq_data[_i].a, memeq_data[_i].b,
+                                                 memeq_data[_i].n) == memeq_data[_i].res);
+}
+END_TEST
+
+/*******************************************************************************
  * memstr
  */
 
@@ -779,6 +821,11 @@ Suite *utils_suite_create()
        tcase_add_test(tc, test_memxor_aligned);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("memeq");
+       tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
+       tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_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);