unit: added ECDH unit tests

This commit is contained in:
James Prestwood 2018-11-16 14:22:49 -08:00 committed by Denis Kenzior
parent 5811e72940
commit dddbf22ab7
2 changed files with 97 additions and 1 deletions

View File

@ -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

91
unit/test-ecdh.c Normal file
View File

@ -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();
}