From 9598de465c95d27aca4c1a9489583d7ed2cd71f7 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 12 Jun 2026 18:01:39 +0200 Subject: [PATCH] unit-tests: Add tests to ensure our Curve25519/448 implementations don't return all-zero secrets Note that wolfSSL before 5.9.2 required building with WOLFSSL_ECDHX_SHARED_NOT_ZERO, which was added with 5.3.0, to get and explicit check. Since the plugin validates the public key, the test case fails nonetheless. --- src/libstrongswan/tests/Makefile.am | 2 + .../tests/suites/test_curve25519.c | 51 ++++++++++++++++++ .../tests/suites/test_curve448.c | 53 +++++++++++++++++++ src/libstrongswan/tests/tests.h | 2 + 4 files changed, 108 insertions(+) create mode 100644 src/libstrongswan/tests/suites/test_curve25519.c create mode 100644 src/libstrongswan/tests/suites/test_curve448.c diff --git a/src/libstrongswan/tests/Makefile.am b/src/libstrongswan/tests/Makefile.am index e04a793e4..814730f9e 100644 --- a/src/libstrongswan/tests/Makefile.am +++ b/src/libstrongswan/tests/Makefile.am @@ -64,6 +64,8 @@ libstrongswan_tests_SOURCES = tests.h tests.c \ suites/test_rng_tester.c \ suites/test_mgf1.c \ suites/test_prf_plus.c \ + suites/test_curve25519.c \ + suites/test_curve448.c \ suites/test_ed25519.c \ suites/test_ed448.c \ suites/test_signature_params.c \ diff --git a/src/libstrongswan/tests/suites/test_curve25519.c b/src/libstrongswan/tests/suites/test_curve25519.c new file mode 100644 index 000000000..41430b6fe --- /dev/null +++ b/src/libstrongswan/tests/suites/test_curve25519.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2026 Tobias Brunner + * + * Copyright (C) secunet Security Networks AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "test_suite.h" + +static chunk_t all_zero = chunk_from_chars( + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00); + +START_TEST(test_reject_all_zero_secret) +{ + key_exchange_t *ke; + chunk_t secret; + + ke = lib->crypto->create_ke(lib->crypto, CURVE_25519); + ck_assert(ke); + + /* implementations might already reject setting the public key */ + ignore_result(ke->set_public_key(ke, all_zero)); + ck_assert(!ke->get_shared_secret(ke, &secret)); + + ke->destroy(ke); +} +END_TEST + +Suite *curve25519_suite_create() +{ + Suite *s; + TCase *tc; + + s = suite_create("curve25519"); + + tc = tcase_create("curve25519_secret"); + tcase_add_test(tc, test_reject_all_zero_secret); + suite_add_tcase(s, tc); + + return s; +} diff --git a/src/libstrongswan/tests/suites/test_curve448.c b/src/libstrongswan/tests/suites/test_curve448.c new file mode 100644 index 000000000..df83bbe98 --- /dev/null +++ b/src/libstrongswan/tests/suites/test_curve448.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2026 Tobias Brunner + * + * Copyright (C) secunet Security Networks AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "test_suite.h" + +static chunk_t all_zero = chunk_from_chars( + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00); + +START_TEST(test_reject_all_zero_secret) +{ + key_exchange_t *ke; + chunk_t secret; + + ke = lib->crypto->create_ke(lib->crypto, CURVE_448); + ck_assert(ke); + + /* implementations might already reject setting the public key */ + ignore_result(ke->set_public_key(ke, all_zero)); + ck_assert(!ke->get_shared_secret(ke, &secret)); + + ke->destroy(ke); +} +END_TEST + +Suite *curve448_suite_create() +{ + Suite *s; + TCase *tc; + + s = suite_create("curve448"); + + tc = tcase_create("curve448_secret"); + tcase_add_test(tc, test_reject_all_zero_secret); + suite_add_tcase(s, tc); + + return s; +} diff --git a/src/libstrongswan/tests/tests.h b/src/libstrongswan/tests/tests.h index bcd8dffd2..8505458b3 100644 --- a/src/libstrongswan/tests/tests.h +++ b/src/libstrongswan/tests/tests.h @@ -61,6 +61,8 @@ TEST_SUITE_DEPEND(mgf1_sha1_suite_create, XOF, XOF_MGF1_SHA1) TEST_SUITE_DEPEND(mgf1_sha256_suite_create, XOF, XOF_MGF1_SHA256) TEST_SUITE_DEPEND(prf_plus_suite_create, KDF, KDF_PRF_PLUS) TEST_SUITE_DEPEND(fetch_http_suite_create, FETCHER, "http://") +TEST_SUITE_DEPEND(curve25519_suite_create, KE, CURVE_25519) +TEST_SUITE_DEPEND(curve448_suite_create, KE, CURVE_448) TEST_SUITE_DEPEND(ed25519_suite_create, PRIVKEY_GEN, KEY_ED25519) TEST_SUITE_DEPEND(ed448_suite_create, PRIVKEY_GEN, KEY_ED448) TEST_SUITE(signature_params_suite_create)