From: Reto Buerki Date: Wed, 29 Aug 2012 07:48:14 +0000 (+0200) Subject: Add keymat IKE key derivation test case X-Git-Tag: 5.0.3rc1~39^2~78 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4be8471fab1bccd078d7ee2fccba77e97f76b52b;p=thirdparty%2Fstrongswan.git Add keymat IKE key derivation test case --- diff --git a/src/charon-tkm/tests/keymat_tests.c b/src/charon-tkm/tests/keymat_tests.c new file mode 100644 index 0000000000..fbaed24e4b --- /dev/null +++ b/src/charon-tkm/tests/keymat_tests.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2012 Reto Buerki + * Copyright (C) 2012 Adrian-Ken Rueegsegger + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program 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 General Public License + * for more details. + */ + +#include +#include +#include +#include +#include +#include + +#include "tkm.h" +#include "tkm_nonceg.h" +#include "tkm_diffie_hellman.h" +#include "tkm_keymat.h" + +START_TEST(test_derive_ike_keys) +{ + fail_if(!library_init(NULL), "Unable to init library"); + fail_if(!libhydra_init("tkm-tests"), "Unable to init libhydra"); + fail_if(!libcharon_init("tkm-tests"), "Unable to init libcharon"); + + /* Register TKM specific plugins */ + static plugin_feature_t features[] = { + PLUGIN_REGISTER(NONCE_GEN, tkm_nonceg_create), + PLUGIN_PROVIDE(NONCE_GEN), + PLUGIN_REGISTER(DH, tkm_diffie_hellman_create), + PLUGIN_PROVIDE(DH, MODP_3072_BIT), + PLUGIN_PROVIDE(DH, MODP_4096_BIT), + }; + lib->plugins->add_static_features(lib->plugins, "tkm-tests", features, + countof(features), TRUE); + + fail_if(!charon->initialize(charon, PLUGINS), "Unable to init charon"); + + proposal_t *proposal = proposal_create_from_string(PROTO_IKE, + "aes256-sha512-modp4096"); + fail_if(!proposal, "Unable to create proposal"); + ike_sa_id_t *ike_sa_id = ike_sa_id_create(IKEV2_MAJOR_VERSION, + 123912312312, 32312313122, TRUE); + fail_if(!ike_sa_id, "Unable to create IKE SA ID"); + + tkm_keymat_t *keymat = tkm_keymat_create(TRUE); + fail_if(!keymat, "Unable to create keymat"); + + chunk_t nonce; + tkm_nonceg_t *ng = tkm_nonceg_create(); + fail_if(!ng, "Unable to create nonce generator"); + fail_unless(ng->nonce_gen.allocate_nonce(&ng->nonce_gen, 32, &nonce), + "Unable to allocate nonce"); + ng->nonce_gen.destroy(&ng->nonce_gen); + + tkm_diffie_hellman_t *dh = tkm_diffie_hellman_create(MODP_4096_BIT); + fail_if(!dh, "Unable to create DH"); + + /* Use the same pubvalue for both sides */ + chunk_t pubvalue; + dh->dh.get_my_public_value(&dh->dh, &pubvalue); + dh->dh.set_other_public_value(&dh->dh, pubvalue); + + fail_unless(keymat->derive_ike_keys(keymat, proposal, &dh->dh, nonce, nonce, + ike_sa_id, PRF_UNDEFINED, chunk_empty), "Key derivation failed"); + chunk_free(&nonce); + + aead_t * const aead = keymat->keymat.get_aead(&keymat->keymat, TRUE); + fail_if(!aead, "AEAD is NULL"); + + fail_if(aead->get_key_size(aead) != 96, "Key size mismatch %d", + aead->get_key_size(aead)); + fail_if(aead->get_block_size(aead) != 16, "Block size mismatch %d", + aead->get_block_size(aead)); + + proposal->destroy(proposal); + dh->dh.destroy(&dh->dh); + ike_sa_id->destroy(ike_sa_id); + keymat->keymat.destroy(&keymat->keymat); + chunk_free(&pubvalue); + + libcharon_deinit(); + libhydra_deinit(); + library_deinit(); +} +END_TEST + +TCase *make_keymat_tests(void) +{ + TCase *tc = tcase_create("Keymat tests"); + tcase_add_test(tc, test_derive_ike_keys); + + return tc; +} diff --git a/src/charon-tkm/tests/test_runner.c b/src/charon-tkm/tests/test_runner.c index d29396c99d..6ab990d926 100644 --- a/src/charon-tkm/tests/test_runner.c +++ b/src/charon-tkm/tests/test_runner.c @@ -32,6 +32,7 @@ int main(void) suite_add_tcase(s, make_utility_tests()); suite_add_tcase(s, make_nonceg_tests()); suite_add_tcase(s, make_diffie_hellman_tests()); + suite_add_tcase(s, make_keymat_tests()); SRunner *sr = srunner_create(s); diff --git a/src/charon-tkm/tests/test_runner.h b/src/charon-tkm/tests/test_runner.h index c3dee9820d..c8cc0c0db0 100644 --- a/src/charon-tkm/tests/test_runner.h +++ b/src/charon-tkm/tests/test_runner.h @@ -24,5 +24,6 @@ TCase *make_chunk_map_tests(void); TCase *make_utility_tests(void); TCase *make_nonceg_tests(void); TCase *make_diffie_hellman_tests(void); +TCase *make_keymat_tests(void); #endif /** TEST_RUNNER_H_ */