unit: Add unit/test-crypto test

And move the PSK generation from passphrase unit tests there
This commit is contained in:
Denis Kenzior 2014-11-14 21:41:59 -06:00
parent d87d7d469f
commit 9e8654976d
3 changed files with 125 additions and 88 deletions

View File

@ -66,7 +66,8 @@ noinst_PROGRAMS = tools/hwsim
tools_hwsim_LDADD = ell/libell-internal.la
unit_tests = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ie
unit_tests = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ie \
unit/test-crypto
if MAINTAINER_MODE
noinst_PROGRAMS += $(unit_tests)
@ -83,6 +84,11 @@ unit_test_prf_sha1_LDADD = ell/libell-internal.la
unit_test_ie_SOURCES = unit/test-ie.c src/ie.h src/ie.c
unit_test_ie_LDADD = ell/libell-internal.la
unit_test_crypto_SOURCES = unit/test-crypto.c \
src/sha1.h src/sha1.c \
src/crypto.h src/crypto.c
unit_test_crypto_LDADD = ell/libell-internal.la
TESTS = $(unit_tests)
manual_pages = doc/iwmon.1

118
unit/test-crypto.c Normal file
View File

@ -0,0 +1,118 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2013-2014 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 <stdio.h>
#include <string.h>
#include <assert.h>
#include <ell/ell.h>
#include "src/sha1.h"
#include "src/crypto.h"
struct psk_data {
const char *passphrase;
const unsigned char *ssid;
size_t ssid_len;
const char *psk;
};
static const unsigned char psk_test_case_1_ssid[] = { 'I', 'E', 'E', 'E' };
static const struct psk_data psk_test_case_1 = {
.passphrase = "password",
.ssid = psk_test_case_1_ssid,
.ssid_len = sizeof(psk_test_case_1_ssid),
.psk = "f42c6fc52df0ebef9ebb4b90b38a5f90"
"2e83fe1b135a70e23aed762e9710a12e",
};
static const unsigned char psk_test_case_2_ssid[] = { 'T', 'h', 'i', 's',
'I', 's', 'A', 'S', 'S', 'I', 'D' };
static const struct psk_data psk_test_case_2 = {
.passphrase = "ThisIsAPassword",
.ssid = psk_test_case_2_ssid,
.ssid_len = sizeof(psk_test_case_2_ssid),
.psk = "0dc0d6eb90555ed6419756b9a15ec3e3"
"209b63df707dd508d14581f8982721af",
};
static const unsigned char psk_test_case_3_ssid[] = {
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z' };
static const struct psk_data psk_test_case_3 = {
.passphrase = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
.ssid = psk_test_case_3_ssid,
.ssid_len = sizeof(psk_test_case_3_ssid),
.psk = "becb93866bb8c3832cb777c2f559807c"
"8c59afcb6eae734885001300a981cc62",
};
static void psk_test(const void *data)
{
const struct psk_data *test = data;
unsigned char output[32];
char psk[65];
unsigned int i;
int result;
printf("Passphrase = \"%s\"\n", test->passphrase);
printf("SSID = {");
for (i = 0; i < test->ssid_len; i++)
printf("%s'%c'", i == 0 ? " " : ", ", test->ssid[i]);
printf(" }\n");
printf("SSID Length = %zd\n", test->ssid_len);
printf("PSK = %s\n", test->psk);
result = crypto_psk_from_passphrase(test->passphrase,
test->ssid, test->ssid_len,
output);
assert(result == 0);
for (i = 0; i < sizeof(output); i++)
sprintf(psk + (i * 2), "%02x", output[i]);
printf("Result = %s\n", psk);
assert(strcmp(test->psk, psk) == 0);
}
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("/Passphrase Generator/PSK Test Case 1",
psk_test, &psk_test_case_1);
l_test_add("/Passphrase Generator/PSK Test Case 2",
psk_test, &psk_test_case_2);
l_test_add("/Passphrase Generator/PSK Test Case 3",
psk_test, &psk_test_case_3);
return l_test_run();
}

View File

@ -179,89 +179,6 @@ static const struct pbkdf2_data athena_test_vector_7 = {
.key = "6b9cf26d45455a43a5b8bb276a403b39",
};
struct psk_data {
const char *passphrase;
const unsigned char *ssid;
size_t ssid_len;
const char *network;
const char *psk;
};
static void psk_test(const void *data)
{
const struct psk_data *test = data;
unsigned char ssid[32];
size_t ssid_len;
unsigned char output[32];
char psk[65];
unsigned int i;
bool result;
if (test->network == NULL) {
memcpy(ssid, test->ssid, test->ssid_len);
ssid_len = test->ssid_len;
} else {
ssid_len = strlen(test->network);
memcpy(ssid, test->network, ssid_len);
}
printf("Passphrase = \"%s\"\n", test->passphrase);
printf("SSID = {");
for (i = 0; i < ssid_len; i++)
printf("%s'%c'", i == 0 ? " " : ", ", ssid[i]);
printf(" }\n");
printf("SSID Length = %ld\n", ssid_len);
printf("PSK = %s\n", test->psk);
result = pbkdf2_sha1(test->passphrase, strlen(test->passphrase),
ssid, ssid_len, 4096,
output, sizeof(output));
assert(result == true);
for (i = 0; i < sizeof(output); i++)
sprintf(psk + (i * 2), "%02x", output[i]);
printf("Result = %s\n", psk);
assert(strcmp(test->psk, psk) == 0);
}
static const unsigned char psk_test_case_1_ssid[] = { 'I', 'E', 'E', 'E' };
static const struct psk_data psk_test_case_1 = {
.passphrase = "password",
.ssid = psk_test_case_1_ssid,
.ssid_len = sizeof(psk_test_case_1_ssid),
.psk = "f42c6fc52df0ebef9ebb4b90b38a5f90"
"2e83fe1b135a70e23aed762e9710a12e",
};
static const unsigned char psk_test_case_2_ssid[] = { 'T', 'h', 'i', 's',
'I', 's', 'A', 'S', 'S', 'I', 'D' };
static const struct psk_data psk_test_case_2 = {
.passphrase = "ThisIsAPassword",
.ssid = psk_test_case_2_ssid,
.ssid_len = sizeof(psk_test_case_2_ssid),
.psk = "0dc0d6eb90555ed6419756b9a15ec3e3"
"209b63df707dd508d14581f8982721af",
};
static const unsigned char psk_test_case_3_ssid[] = {
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z' };
static const struct psk_data psk_test_case_3 = {
.passphrase = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
.ssid = psk_test_case_3_ssid,
.ssid_len = sizeof(psk_test_case_3_ssid),
.psk = "becb93866bb8c3832cb777c2f559807c"
"8c59afcb6eae734885001300a981cc62",
};
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
@ -294,9 +211,5 @@ int main(int argc, char *argv[])
l_test_add("/pbkdf2-sha1/ATHENA Test vector 7",
pbkdf2_test, &athena_test_vector_7);
l_test_add("/pbkdf2-sha1/PSK Test case 1", psk_test, &psk_test_case_1);
l_test_add("/pbkdf2-sha1/PSK Test case 2", psk_test, &psk_test_case_2);
l_test_add("/pbkdf2-sha1/PSK Test case 3", psk_test, &psk_test_case_3);
return l_test_run();
}