summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am6
-rw-r--r--unit/test-ecc.c98
3 files changed, 105 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 82304b0b7..484f41f39 100644
--- a/.gitignore
+++ b/.gitignore
@@ -92,6 +92,7 @@ unit/test-mgmt
unit/test-uhid
unit/test-hfp
unit/test-crypto
+unit/test-ecc
tools/mgmt-tester
tools/smp-tester
tools/gap-tester
diff --git a/Makefile.am b/Makefile.am
index c84d63ab8..c5061228b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -102,6 +102,7 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
src/shared/util.h src/shared/util.c \
src/shared/mgmt.h src/shared/mgmt.c \
src/shared/crypto.h src/shared/crypto.c \
+ src/shared/ecc.h src/shared/ecc.c \
src/shared/ringbuf.h src/shared/ringbuf.c \
src/shared/tester.h src/shared/tester.c \
src/shared/hci.h src/shared/hci.c \
@@ -282,6 +283,11 @@ unit_tests += unit/test-crypto
unit_test_crypto_SOURCES = unit/test-crypto.c
unit_test_crypto_LDADD = src/libshared-glib.la @GLIB_LIBS@
+unit_tests += unit/test-ecc
+
+unit_test_ecc_SOURCES = unit/test-ecc.c
+unit_test_ecc_LDADD = src/libshared-glib.la @GLIB_LIBS@
+
unit_tests += unit/test-ringbuf unit/test-queue
unit_test_ringbuf_SOURCES = unit/test-ringbuf.c
diff --git a/unit/test-ecc.c b/unit/test-ecc.c
new file mode 100644
index 000000000..59475f4a4
--- /dev/null
+++ b/unit/test-ecc.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2013, Kenneth MacKay
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "src/shared/ecc.h"
+
+static void vli_print(uint8_t *vli, size_t size)
+{
+ while (size) {
+ printf("%02X ", vli[size - 1]);
+ size--;
+ }
+}
+
+#define PAIR_COUNT 200
+
+static void test_basic(void)
+{
+ uint8_t public1[64], public2[64];
+ uint8_t private1[32], private2[32];
+ uint8_t shared1[32], shared2[32];
+ int i;
+
+ printf("Testing %u random private key pairs\n", PAIR_COUNT);
+
+ for (i = 0; i < PAIR_COUNT; i++) {
+ printf(".");
+ fflush(stdout);
+
+ ecc_make_key(public1, private1);
+ ecc_make_key(public2, private2);
+
+ ecdh_shared_secret(public1, private2, shared1);
+ ecdh_shared_secret(public2, private1, shared2);
+
+ if (memcmp(shared1, shared2, sizeof(shared1)) != 0) {
+ printf("Shared secrets are not identical!\n");
+ printf("Shared secret 1 = ");
+ vli_print(shared1, sizeof(shared1));
+ printf("\n");
+ printf("Shared secret 2 = ");
+ vli_print(shared2, sizeof(shared2));
+ printf("\n");
+ printf("Private key 1 = ");
+ vli_print(private1, sizeof(private1));
+ printf("\n");
+ printf("Private key 2 = ");
+ vli_print(private2, sizeof(private2));
+ printf("\n");
+ g_assert_not_reached();
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/ecdh", test_basic);
+
+ return g_test_run();
+}