diff options
author | Jan Hutter <jhutter@hsr.ch> | 2005-11-28 09:12:49 +0000 |
---|---|---|
committer | Jan Hutter <jhutter@hsr.ch> | 2005-11-28 09:12:49 +0000 |
commit | 7b0dafeb52c3f41259e9a3adedaa49f4a3bc2afe (patch) | |
tree | d2a39a23efb502cc01b3055760abd9d48dc3e725 | |
parent | 363a7dab5e61c86c806ac53b61fa74e6a03450b5 (diff) | |
download | strongswan-7b0dafeb52c3f41259e9a3adedaa49f4a3bc2afe.tar.bz2 strongswan-7b0dafeb52c3f41259e9a3adedaa49f4a3bc2afe.tar.xz |
- wrote test for aes_cbc but encryption does not seem to work :-(
-rw-r--r-- | Source/charon/Makefile | 7 | ||||
-rw-r--r-- | Source/charon/testcases/Makefile.testcases | 5 | ||||
-rw-r--r-- | Source/charon/testcases/aes_cbc_crypter_test.c | 143 | ||||
-rw-r--r-- | Source/charon/testcases/aes_cbc_crypter_test.h | 36 | ||||
-rw-r--r-- | Source/charon/testcases/testcases.c | 16 | ||||
-rw-r--r-- | Source/charon/transforms/crypters/Makefile.crypters | 4 | ||||
-rw-r--r-- | Source/charon/transforms/crypters/aes_cbc_crypter.c (renamed from Source/charon/transforms/crypters/aes_crypter.c) | 42 | ||||
-rw-r--r-- | Source/charon/transforms/crypters/aes_cbc_crypter.h (renamed from Source/charon/transforms/crypters/aes_crypter.h) | 18 | ||||
-rw-r--r-- | Source/charon/utils/logger_manager.c | 2 |
9 files changed, 234 insertions, 39 deletions
diff --git a/Source/charon/Makefile b/Source/charon/Makefile index 72b84f88c..1491efcc2 100644 --- a/Source/charon/Makefile +++ b/Source/charon/Makefile @@ -18,7 +18,12 @@ MAIN_DIR= ./ LDFLAGS= -lgmp -lpthread -CFLAGS+= -Wall -Werror -DLEAK_DETECTIVE -I. -g +CFLAGS+= -Wall \ + -DLEAK_DETECTIVE \ + -I. \ + -g + +# -Werror # objects is extended by each included Makefile OBJS= diff --git a/Source/charon/testcases/Makefile.testcases b/Source/charon/testcases/Makefile.testcases index fd7a5f589..a1087d4e9 100644 --- a/Source/charon/testcases/Makefile.testcases +++ b/Source/charon/testcases/Makefile.testcases @@ -15,6 +15,11 @@ TESTCASES_DIR= $(MAIN_DIR)testcases/ +TEST_OBJS+= $(BUILD_DIR)aes_cbc_crypter_test.o +$(BUILD_DIR)aes_cbc_crypter_test.o : $(TESTCASES_DIR)aes_cbc_crypter_test.c $(TESTCASES_DIR)aes_cbc_crypter_test.h + $(CC) $(CFLAGS) -c -o $@ $< + + TEST_OBJS+= $(BUILD_DIR)diffie_hellman_test.o $(BUILD_DIR)diffie_hellman_test.o : $(TESTCASES_DIR)diffie_hellman_test.c $(TESTCASES_DIR)diffie_hellman_test.h $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Source/charon/testcases/aes_cbc_crypter_test.c b/Source/charon/testcases/aes_cbc_crypter_test.c new file mode 100644 index 000000000..736d6f0f1 --- /dev/null +++ b/Source/charon/testcases/aes_cbc_crypter_test.c @@ -0,0 +1,143 @@ +/** + * @file aes_cbc_crypter_test.c + * + * @brief Tests the aes_cbc_crypter_t class. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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 <string.h> + +#include "aes_cbc_crypter_test.h" + +#include <utils/allocator.h> +#include <globals.h> + +void test_aes_cbc_crypter(tester_t *tester) +{ + /* + * Test 1 of RFC3602 + * Key : 0x06a9214036b8a15b512e03d534120006 + * IV : 0x3dafba429d9eb430b422da802c9fac41 + * Plaintext : "Single block msg" + * Ciphertext: 0xe353779c1079aeb82708942dbe77181a + */ + crypter_t *crypter; + u_int8_t key1[] = {0x06,0xa9,0x21,0x40,0x36,0xb8,0xa1,0x5b, + 0x51,0x2e,0x03,0xd5,0x34,0x12,0x00,0x06}; + chunk_t key1_chunk = {ptr: key1, len : 16}; + u_int8_t iv1[] = {0x3d,0xaf,0xba,0x42,0x9d,0x9e,0xb4,0x30, + 0xb4,0x22,0xda,0x80,0x2c,0x9f,0xac,0x41}; + chunk_t iv1_chunk = {ptr: iv1, len : 16}; + u_int8_t ciphertext1[] = { 0xe3,0x53,0x77,0x9c,0x10,0x79,0xae,0xb8, + 0x27,0x08,0x94,0x2d,0xbe,0x77,0x18,0x1a}; + + chunk_t expected_encrypted1 = {ptr: ciphertext1, len : 16}; + char * plaintext1 = "Single block msg"; + chunk_t data1 = {ptr: plaintext1, len : 16}; + chunk_t encrypted1; + chunk_t decrypted1; + logger_t *logger; + + logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"AES CBC"); + + crypter = (crypter_t *) aes_cbc_crypter_create(16); + tester->assert_true(tester, (crypter != NULL), "create call test"); + + tester->assert_true(tester, (crypter->set_key(crypter,key1_chunk) == SUCCESS), "set_key call test"); + + tester->assert_true(tester, (crypter->encrypt(crypter,data1,iv1_chunk,&encrypted1) == SUCCESS), "encrypt call test"); + + tester->assert_true(tester, (memcmp(encrypted1.ptr, expected_encrypted1.ptr, 16) == 0), "Encrypted value"); + + logger->log_chunk(logger,RAW,"exptected encrypted :", &expected_encrypted1); + logger->log_chunk(logger,RAW,"encrypted :", &encrypted1); + + allocator_free_chunk(&encrypted1); + + tester->assert_true(tester, (crypter->decrypt(crypter,data1,iv1_chunk,&decrypted1) == SUCCESS), "decrypt call test"); + + tester->assert_true(tester, (memcmp(decrypted1.ptr, plaintext1, 16) == 0), "decrypted value"); + + logger->log_chunk(logger,RAW,"expected decrypted :", &data1); + logger->log_chunk(logger,RAW,"decrypted :", &decrypted1); + + allocator_free_chunk(&encrypted1); + + tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test"); + + + + /* + * Test 2 of RFC3602 + * Key : 0xc286696d887c9aa0611bbb3e2025a45a + * IV : 0x562e17996d093d28ddb3ba695a2e6f58 + * Plaintext : 0x000102030405060708090a0b0c0d0e0f + * 101112131415161718191a1b1c1d1e1f + * Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a + * 7586602d253cfff91b8266bea6d61ab1 + */ + u_int8_t key2[] = {0xc2,0x86,0x69,0x6d,0x88,0x7c,0x9a,0xa0, + 0x61,0x1b,0xbb,0x3e,0x20,0x25,0xa4,0x5a}; + chunk_t key2_chunk = {ptr: key2, len : 16}; + u_int8_t iv2[] = {0x56,0x2e,0x17,0x99,0x6d,0x09,0x3d,0x28, + 0xdd,0xb3,0xba,0x69,0x5a,0x2e,0x6f,0x58}; + chunk_t iv2_chunk = {ptr: iv2, len : 16}; + u_int8_t ciphertext2[] = { 0xd2,0x96,0xcd,0x94,0xc2,0xcc,0xcf,0x8a, + 0x3a,0x86,0x30,0x28,0xb5,0xe1,0xdc,0x0a, + 0x75,0x86,0x60,0x2d,0x25,0x3c,0xff,0xf9, + 0x1b,0x82,0x66,0xbe,0xa6,0xd6,0x1a,0xb1}; + + chunk_t expected_encrypted2 = {ptr: ciphertext2, len : 32}; + u_int8_t plaintext2[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, + 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, + 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, + 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f}; + chunk_t data2 = {ptr: plaintext2, len : 32}; + chunk_t encrypted2; + chunk_t decrypted2; + + crypter = (crypter_t *) aes_cbc_crypter_create(16); + tester->assert_true(tester, (crypter != NULL), "create call test"); + + tester->assert_true(tester, (crypter->set_key(crypter,key2_chunk) == SUCCESS), "set_key call test"); + + tester->assert_true(tester, (crypter->encrypt(crypter,data2,iv2_chunk,&encrypted2) == SUCCESS), "encrypt call test"); + + tester->assert_true(tester, (memcmp(encrypted2.ptr, expected_encrypted2.ptr, 26) == 0), "Encrypted value"); + + logger->log_chunk(logger,RAW,"exptected encrypted :", &expected_encrypted2); + logger->log_chunk(logger,RAW,"encrypted :", &encrypted2); + + allocator_free_chunk(&encrypted2); + + tester->assert_true(tester, (crypter->decrypt(crypter,data2,iv2_chunk,&decrypted2) == SUCCESS), "decrypt call test"); + + tester->assert_true(tester, (memcmp(decrypted2.ptr, plaintext2, 26) == 0), "decrypted value"); + + logger->log_chunk(logger,RAW,"expected decrypted :", &data2); + logger->log_chunk(logger,RAW,"decrypted :", &decrypted2); + + allocator_free_chunk(&encrypted2); + + tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test"); + + + + global_logger_manager->destroy_logger(global_logger_manager,logger); +} + diff --git a/Source/charon/testcases/aes_cbc_crypter_test.h b/Source/charon/testcases/aes_cbc_crypter_test.h new file mode 100644 index 000000000..72005bbf0 --- /dev/null +++ b/Source/charon/testcases/aes_cbc_crypter_test.h @@ -0,0 +1,36 @@ +/** + * @file aes_cbc_crypter_test.h + * + * @brief Tests the aes_cbc_crypter_t class. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#ifndef _AES_CBC_CRYPTER_TEST_H_ +#define _AES_CBC_CRYPTER_TEST_H_ + +#include <transforms/crypters/aes_cbc_crypter.h> +#include <utils/tester.h> + +/** + * @brief Test function used to test the aes_cbc_crypter_t class. + * + * @param tester associated tester object + */ +void test_aes_cbc_crypter(tester_t *tester); + +#endif //_AES_CBC_CRYPTER_TEST_H_ diff --git a/Source/charon/testcases/testcases.c b/Source/charon/testcases/testcases.c index 10dc56ed4..7cff4b724 100644 --- a/Source/charon/testcases/testcases.c +++ b/Source/charon/testcases/testcases.c @@ -51,7 +51,7 @@ #include <testcases/hasher_test.h> #include <testcases/hmac_test.h> #include <testcases/prf_plus_test.h> - +#include <testcases/aes_cbc_crypter_test.h> /* output for test messages */ extern FILE * stderr; @@ -196,6 +196,10 @@ test_t hmac_test2 = {test_hmac_md5, "HMAC using MD5"}; */ test_t prf_plus_test = {test_prf_plus, "prf+"}; +/** + * Test for AES CBC crypter + */ +test_t aes_cbc_crypter_test = {test_aes_cbc_crypter, "AES CBC"}; @@ -275,9 +279,10 @@ logger_manager_t *global_logger_manager; &hmac_test1, &hmac_test2, &prf_plus_test, + &aes_cbc_crypter_test, NULL }; - global_logger_manager = logger_manager_create(0); + global_logger_manager = logger_manager_create(FULL); global_socket = socket_create(4600); @@ -287,13 +292,14 @@ logger_manager_t *global_logger_manager; global_configuration_manager = configuration_manager_create(); global_ike_sa_manager = ike_sa_manager_create(); - global_logger_manager->disable_logger_level(global_logger_manager,TESTER,FULL); +// global_logger_manager->disable_logger_level(global_logger_manager,TESTER,FULL); +// global_logger_manager->enable_logger_level(global_logger_manager,TESTER,RAW); tester_t *tester = tester_create(test_output, FALSE); - tester->perform_tests(tester,all_tests); -// tester->perform_test(tester,&sha1_hasher_test); +// tester->perform_tests(tester,all_tests); + tester->perform_test(tester,&aes_cbc_crypter_test); diff --git a/Source/charon/transforms/crypters/Makefile.crypters b/Source/charon/transforms/crypters/Makefile.crypters index 8260c239b..232787cd8 100644 --- a/Source/charon/transforms/crypters/Makefile.crypters +++ b/Source/charon/transforms/crypters/Makefile.crypters @@ -18,6 +18,6 @@ OBJS+= $(BUILD_DIR)crypter.o $(BUILD_DIR)crypter.o : $(CRYPTERS_DIR)crypter.c $(CRYPTERS_DIR)crypter.h $(CC) $(CFLAGS) -c -o $@ $< -OBJS+= $(BUILD_DIR)aes_crypter.o -$(BUILD_DIR)aes_crypter.o : $(CRYPTERS_DIR)aes_crypter.c $(CRYPTERS_DIR)aes_crypter.h +OBJS+= $(BUILD_DIR)aes_cbc_crypter.o +$(BUILD_DIR)aes_cbc_crypter.o : $(CRYPTERS_DIR)aes_cbc_crypter.c $(CRYPTERS_DIR)aes_cbc_crypter.h $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Source/charon/transforms/crypters/aes_crypter.c b/Source/charon/transforms/crypters/aes_cbc_crypter.c index 6ed182146..be1c0d27e 100644 --- a/Source/charon/transforms/crypters/aes_crypter.c +++ b/Source/charon/transforms/crypters/aes_cbc_crypter.c @@ -1,7 +1,7 @@ /** - * @file aes_crypter.c + * @file aes_cbc_crypter.c * - * @brief Implementation of aes_crypter_t + * @brief Implementation of aes_cbc_crypter_t * */ @@ -21,7 +21,7 @@ * for more details. */ -#include "aes_crypter.h" +#include "aes_cbc_crypter.h" #include <utils/allocator.h> @@ -41,19 +41,19 @@ #define AES_KS_LENGTH 120 #define AES_RC_LENGTH 29 -typedef struct private_aes_crypter_t private_aes_crypter_t; +typedef struct private_aes_cbc_crypter_t private_aes_cbc_crypter_t; /** * @brief Class implementing the AES symmetric encryption algorithm. * * @ingroup crypters */ -struct private_aes_crypter_t { +struct private_aes_cbc_crypter_t { /** * Public part of this class. */ - aes_crypter_t public; + aes_cbc_crypter_t public; /** * Number of words in the key input block. @@ -96,7 +96,7 @@ struct private_aes_crypter_t { * @return * - SUCCESS in any case */ - status_t (*encrypt_block) (private_aes_crypter_t *this, u_int8_t *data, u_int8_t *encrypted); + status_t (*encrypt_block) (private_aes_cbc_crypter_t *this, u_int8_t *data, u_int8_t *encrypted); /** * @brief Decrypt a chunk of data with blocksize length. @@ -109,7 +109,7 @@ struct private_aes_crypter_t { * @return * - SUCCESS in any case */ - status_t (*decrypt_block) (private_aes_crypter_t *this, u_int8_t *data, u_int8_t *decrypted); + status_t (*decrypt_block) (private_aes_cbc_crypter_t *this, u_int8_t *data, u_int8_t *decrypted); }; @@ -1263,9 +1263,9 @@ switch(this->aes_Ncol) \ /** - * Implementation of private_aes_crypter_t.encrypt_block. + * Implementation of private_aes_cbc_crypter_t.encrypt_block. */ -static status_t encrypt_block (private_aes_crypter_t *this, u_int8_t *data, u_int8_t *encrypted) +static status_t encrypt_block (private_aes_cbc_crypter_t *this, u_int8_t *data, u_int8_t *encrypted) { u_int32_t locals(b0, b1); const u_int32_t *kp = this->aes_e_key; @@ -1338,9 +1338,9 @@ static status_t encrypt_block (private_aes_crypter_t *this, u_int8_t *data, u_in } /** - * Implementation of private_aes_crypter_t.decrypt_block. + * Implementation of private_aes_cbc_crypter_t.decrypt_block. */ -static status_t decrypt_block (private_aes_crypter_t *this, u_int8_t *data, u_int8_t *decrypted) +static status_t decrypt_block (private_aes_cbc_crypter_t *this, u_int8_t *data, u_int8_t *decrypted) { u_int32_t locals(b0, b1); const u_int32_t *kp = this->aes_d_key; @@ -1417,7 +1417,7 @@ static status_t decrypt_block (private_aes_crypter_t *this, u_int8_t *data, u_in /** * Implementation of crypter_t.decrypt. */ -static status_t decrypt (private_aes_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted) +static status_t decrypt (private_aes_cbc_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted) { int ret, pos; const u_int32_t *iv_i; @@ -1465,7 +1465,7 @@ static status_t decrypt (private_aes_crypter_t *this, chunk_t data, chunk_t iv, /** * Implementation of crypter_t.decrypt. */ -static status_t encrypt (private_aes_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted) +static status_t encrypt (private_aes_cbc_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted) { int ret, pos; const u_int32_t *iv_i; @@ -1510,7 +1510,7 @@ static status_t encrypt (private_aes_crypter_t *this, chunk_t data, chunk_t iv, /** * Implementation of crypter_t.get_block_size. */ -static size_t get_block_size (private_aes_crypter_t *this) +static size_t get_block_size (private_aes_cbc_crypter_t *this) { return this->blocksize; } @@ -1518,7 +1518,7 @@ static size_t get_block_size (private_aes_crypter_t *this) /** * Implementation of crypter_t.set_key. */ -static status_t set_key (private_aes_crypter_t *this, chunk_t key) +static status_t set_key (private_aes_cbc_crypter_t *this, chunk_t key) { u_int32_t *kf, *kt, rci, f = 0; @@ -1612,18 +1612,18 @@ static status_t set_key (private_aes_crypter_t *this, chunk_t key) } /** - * Implementation of crypter_t.destroy and aes_crypter_t.destroy. + * Implementation of crypter_t.destroy and aes_cbc_crypter_t.destroy. */ -static status_t destroy (private_aes_crypter_t *this) +static status_t destroy (private_aes_cbc_crypter_t *this) { allocator_free(this); return SUCCESS; } -aes_crypter_t *aes_crypter_create(size_t blocksize) +aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize) { - private_aes_crypter_t *this = allocator_alloc_thing(private_aes_crypter_t); + private_aes_cbc_crypter_t *this = allocator_alloc_thing(private_aes_cbc_crypter_t); if (this == NULL) { return NULL; @@ -1657,7 +1657,7 @@ aes_crypter_t *aes_crypter_create(size_t blocksize) this->public.crypter_interface.destroy = (status_t (*) (crypter_t *)) destroy; /* public functions */ - this->public.destroy = (status_t (*) (aes_crypter_t *)) destroy; + this->public.destroy = (status_t (*) (aes_cbc_crypter_t *)) destroy; /* private functions */ this->encrypt_block = encrypt_block; diff --git a/Source/charon/transforms/crypters/aes_crypter.h b/Source/charon/transforms/crypters/aes_cbc_crypter.h index 2bb8fa171..4a8e935c8 100644 --- a/Source/charon/transforms/crypters/aes_crypter.h +++ b/Source/charon/transforms/crypters/aes_cbc_crypter.h @@ -1,7 +1,7 @@ /** - * @file aes_crypter.h + * @file aes_cbc_crypter.h * - * @brief Interface of aes_crypter_t + * @brief Interface of aes_cbc_crypter_t * */ @@ -27,14 +27,14 @@ #include <transforms/crypters/crypter.h> -typedef struct aes_crypter_t aes_crypter_t; +typedef struct aes_cbc_crypter_t aes_cbc_crypter_t; /** * @brief Class implementing the AES symmetric encryption algorithm. * * @ingroup crypters */ -struct aes_crypter_t { +struct aes_cbc_crypter_t { /** * crypter_t interface. @@ -42,26 +42,26 @@ struct aes_crypter_t { crypter_t crypter_interface; /** - * @brief Destroys a aes_crypter_t object. + * @brief Destroys a aes_cbc_crypter_t object. * * @param this crypter_t object to destroy * @return * - SUCCESS in any case */ - status_t (*destroy) (aes_crypter_t *this); + status_t (*destroy) (aes_cbc_crypter_t *this); }; /** - * @brief Constructor to create aes_crypter_t objects. + * @brief Constructor to create aes_cbc_crypter_t objects. * * @param blocksize block size of AES crypter * (16, 24 or 32 are supported) * Default size is set to 16. * @return - * - aes_crypter_t if successfully + * - aes_cbc_crypter_t if successfully * - NULL if out of ressources */ -aes_crypter_t *aes_crypter_create(size_t blocksize); +aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize); #endif //_AES_CRYPTER_H_ diff --git a/Source/charon/utils/logger_manager.c b/Source/charon/utils/logger_manager.c index d63351345..021eeb2e4 100644 --- a/Source/charon/utils/logger_manager.c +++ b/Source/charon/utils/logger_manager.c @@ -145,7 +145,7 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t case TESTER: log_thread_ids = FALSE; output = stdout; - logger_level = 0; + logger_level |= FULL; break; case IKE_SA: case IKE_SA_MANAGER: |