commit dddbf22ab7c1bc3613553b3572d74a5763fe0618
parent 5811e729408067eea57b91000f58b55fd38e25b1
Author: James Prestwood <james.prestwood@linux.intel.com>
Date: Fri, 16 Nov 2018 14:22:49 -0800
unit: added ECDH unit tests
Diffstat:
2 files changed, 97 insertions(+), 1 deletion(-)
diff --git 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
@@ -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 <config.h>
+#endif
+
+#include <string.h>
+#include <assert.h>
+#include <ell/ell.h>
+
+#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();
+}