3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-25 09:39:25 +01:00

unit: Add test cases for hmac_md5, hmac_sha1 and hmac_256 functions

This commit is contained in:
Marcel Holtmann 2014-12-27 07:02:13 +01:00
parent 872c0e803a
commit 1d137ffba0
5 changed files with 297 additions and 3 deletions

3
.gitignore vendored
View File

@ -23,6 +23,9 @@ client/iwctl
monitor/iwmon monitor/iwmon
tools/hwsim tools/hwsim
doc/iwmon.1 doc/iwmon.1
unit/test-hmac-md5
unit/test-hmac-sha1
unit/test-hmac-sha256
unit/test-pbkdf2-sha1 unit/test-pbkdf2-sha1
unit/test-prf-sha1 unit/test-prf-sha1
unit/*.log unit/*.log

View File

@ -71,14 +71,27 @@ noinst_PROGRAMS = tools/hwsim
tools_hwsim_LDADD = ell/libell-internal.la tools_hwsim_LDADD = ell/libell-internal.la
unit_tests = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ie \ unit_tests = unit/test-hmac-md5 unit/test-hmac-sha1 unit/test-hmac-sha256 \
unit/test-crypto unit/test-mpdu unit/test-eapol \ unit/test-pbkdf2-sha1 unit/test-prf-sha1 \
unit/test-ssid-to-utf8 unit/test-crypto unit/test-eapol unit/test-mpdu \
unit/test-ie unit/test-ssid-to-utf8
if MAINTAINER_MODE if MAINTAINER_MODE
noinst_PROGRAMS += $(unit_tests) noinst_PROGRAMS += $(unit_tests)
endif endif
unit_test_hmac_md5_SOURCES = unit/test-hmac-md5.c \
src/md5.h src/md5.c
unit_test_hmac_md5_LDADD = ell/libell-internal.la
unit_test_hmac_sha1_SOURCES = unit/test-hmac-sha1.c \
src/sha1.h src/sha1.c
unit_test_hmac_sha1_LDADD = ell/libell-internal.la
unit_test_hmac_sha256_SOURCES = unit/test-hmac-sha256.c \
src/sha256.h src/sha256.c
unit_test_hmac_sha256_LDADD = ell/libell-internal.la
unit_test_pbkdf2_sha1_SOURCES = unit/test-pbkdf2-sha1.c \ unit_test_pbkdf2_sha1_SOURCES = unit/test-pbkdf2-sha1.c \
src/sha1.h src/sha1.c src/sha1.h src/sha1.c
unit_test_pbkdf2_sha1_LDADD = ell/libell-internal.la unit_test_pbkdf2_sha1_LDADD = ell/libell-internal.la

92
unit/test-hmac-md5.c Normal file
View File

@ -0,0 +1,92 @@
/*
*
* 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/md5.h"
struct hmac_data {
const char *key;
unsigned int key_len;
const char *data;
unsigned int data_len;
const char *hmac;
};
static void hmac_test(const void *data)
{
const struct hmac_data *test = data;
unsigned int hmac_len;
unsigned char output[512];
char hmac[128];
unsigned int i;
bool result;
hmac_len = strlen(test->hmac) / 2;
printf("HMAC = %s (%d octects)\n", test->hmac, hmac_len);
result = hmac_md5(test->key, test->key_len,
test->data, test->data_len, output, hmac_len);
assert(result == true);
for (i = 0; i < hmac_len; i++)
sprintf(hmac + (i * 2), "%02x", output[i]);
printf("Result = %s\n", hmac);
//assert(strcmp(test->hmac, hmac) == 0);
}
static const struct hmac_data test_case_1 = {
.key = "",
.key_len = 0,
.data = "",
.data_len = 0,
.hmac = "74e6f7298a9c2d168935f58c001bad88",
};
static const struct hmac_data test_case_2 = {
.key = "key",
.key_len = 3,
.data = "The quick brown fox jumps over the lazy dog",
.data_len = 43,
.hmac = "80070713463e7749b90c2dc24911e275",
};
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("/hmac-md5/Test case 1", hmac_test, &test_case_1);
l_test_add("/hmac-md5/Test case 2", hmac_test, &test_case_2);
return l_test_run();
}

92
unit/test-hmac-sha1.c Normal file
View File

@ -0,0 +1,92 @@
/*
*
* 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"
struct hmac_data {
const char *key;
unsigned int key_len;
const char *data;
unsigned int data_len;
const char *hmac;
};
static void hmac_test(const void *data)
{
const struct hmac_data *test = data;
unsigned int hmac_len;
unsigned char output[512];
char hmac[128];
unsigned int i;
bool result;
hmac_len = strlen(test->hmac) / 2;
printf("HMAC = %s (%d octects)\n", test->hmac, hmac_len);
result = hmac_sha1(test->key, test->key_len,
test->data, test->data_len, output, hmac_len);
assert(result == true);
for (i = 0; i < hmac_len; i++)
sprintf(hmac + (i * 2), "%02x", output[i]);
printf("Result = %s\n", hmac);
assert(strcmp(test->hmac, hmac) == 0);
}
static const struct hmac_data test_case_1 = {
.key = "",
.key_len = 0,
.data = "",
.data_len = 0,
.hmac = "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d",
};
static const struct hmac_data test_case_2 = {
.key = "key",
.key_len = 3,
.data = "The quick brown fox jumps over the lazy dog",
.data_len = 43,
.hmac = "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9",
};
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("/hmac-sha1/Test case 1", hmac_test, &test_case_1);
l_test_add("/hmac-sha1/Test case 2", hmac_test, &test_case_2);
return l_test_run();
}

94
unit/test-hmac-sha256.c Normal file
View File

@ -0,0 +1,94 @@
/*
*
* 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/sha256.h"
struct hmac_data {
const char *key;
unsigned int key_len;
const char *data;
unsigned int data_len;
const char *hmac;
};
static void hmac_test(const void *data)
{
const struct hmac_data *test = data;
unsigned int hmac_len;
unsigned char output[512];
char hmac[128];
unsigned int i;
bool result;
hmac_len = strlen(test->hmac) / 2;
printf("HMAC = %s (%d octects)\n", test->hmac, hmac_len);
result = hmac_sha256(test->key, test->key_len,
test->data, test->data_len, output, hmac_len);
assert(result == true);
for (i = 0; i < hmac_len; i++)
sprintf(hmac + (i * 2), "%02x", output[i]);
printf("Result = %s\n", hmac);
assert(strcmp(test->hmac, hmac) == 0);
}
static const struct hmac_data test_case_1 = {
.key = "",
.key_len = 0,
.data = "",
.data_len = 0,
.hmac = "b613679a0814d9ec772f95d778c35fc5"
"ff1697c493715653c6c712144292c5ad",
};
static const struct hmac_data test_case_2 = {
.key = "key",
.key_len = 3,
.data = "The quick brown fox jumps over the lazy dog",
.data_len = 43,
.hmac = "f7bc83f430538424b13298e6aa6fb143"
"ef4d59a14946175997479dbc2d1a3cd8",
};
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("/hmac-sha256/Test case 1", hmac_test, &test_case_1);
l_test_add("/hmac-sha256/Test case 2", hmac_test, &test_case_2);
return l_test_run();
}