From dddbf22ab7c1bc3613553b3572d74a5763fe0618 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Fri, 16 Nov 2018 14:22:49 -0800 Subject: [PATCH] unit: added ECDH unit tests --- Makefile.am | 7 +++- unit/test-ecdh.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 unit/test-ecdh.c diff --git a/Makefile.am b/Makefile.am index 8e7581ab..a0a3c137 100644 --- a/Makefile.am +++ b/Makefile.am @@ -299,7 +299,7 @@ unit_tests = unit/test-cmac-aes \ unit/test-ie unit/test-ssid-to-utf8 unit/test-ssid-security \ unit/test-arc4 unit/test-wsc unit/test-eap-mschapv2 \ unit/test-eap-sim unit/test-client unit/test-ecc \ - unit/test-sae + unit/test-sae unit/test-ecdh ell_pem_files = cert-ca-key.pem cert-client-key.pem cert-client-key-pkcs8.pem \ @@ -428,6 +428,11 @@ unit_test_sae_SOURCES = unit/test-sae.c \ src/ecc.h src/ecc.c unit_test_sae_LDADD = $(ell_ldadd) +unit_test_ecdh_SOURCES = unit/test-ecdh.c \ + src/ecdh.h src/ecdh.c \ + src/ecc.h src/ecc.c +unit_test_ecdh_LDADD = $(ell_ldadd) + TESTS = $(unit_tests) manual_pages = doc/iwmon.1 diff --git a/unit/test-ecdh.c b/unit/test-ecdh.c new file mode 100644 index 00000000..a100b1f0 --- /dev/null +++ b/unit/test-ecdh.c @@ -0,0 +1,91 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include "src/ecdh.h" +#include "src/ecc.h" + +/* + * Tests the most basic case. Generate two full public keys and use to create + * two identical shared secrets. + */ +static void test_basic(const void *data) +{ + uint8_t private1[32]; + uint8_t private2[32]; + + uint8_t public1[64]; + uint8_t public2[64]; + + uint8_t secret1[32]; + uint8_t secret2[32]; + + assert(ecdh_generate_key_pair(private1, 32, public1, 64)); + assert(ecdh_generate_key_pair(private2, 32, public2, 64)); + + assert(ecdh_generate_shared_secret(private1, public2, 64, secret1, 32)); + assert(ecdh_generate_shared_secret(private2, public1, 64, secret2, 32)); + + assert(!memcmp(secret1, secret2, 32)); +} + +/* + * Tests public key compliance. When generating the public keys, only specify + * half their length (32). This requires ECDH to compute the remainder of the + * public key when generating the shared secret. + */ +static void test_compliant_key(const void *data) +{ + uint8_t private1[32]; + uint8_t private2[32]; + + uint8_t public1[32]; + uint8_t public2[32]; + + uint8_t secret1[32]; + uint8_t secret2[32]; + + assert(ecdh_generate_key_pair(private1, 32, public1, 32)); + assert(ecdh_generate_key_pair(private2, 32, public2, 32)); + + assert(ecdh_generate_shared_secret(private1, public2, 32, secret1, 32)); + assert(ecdh_generate_shared_secret(private2, public1, 32, secret2, 32)); + + assert(!memcmp(secret1, secret2, 32)); +} + +int main(int argc, char *argv[]) +{ + l_test_init(&argc, &argv); + l_test_add("ECDH Basic", test_basic, NULL); + l_test_add("ECDH Compliant key", test_compliant_key, NULL); + + return l_test_run(); +}