diff options
Diffstat (limited to 'src/libstrongswan/tests')
-rw-r--r-- | src/libstrongswan/tests/Makefile.in | 1 | ||||
-rw-r--r-- | src/libstrongswan/tests/suites/test_chunk.c | 9 | ||||
-rw-r--r-- | src/libstrongswan/tests/suites/test_enumerator.c | 24 | ||||
-rw-r--r-- | src/libstrongswan/tests/suites/test_ntru.c | 524 | ||||
-rw-r--r-- | src/libstrongswan/tests/suites/test_vectors.c | 13 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_runner.c | 121 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_suite.c | 3 |
7 files changed, 580 insertions, 115 deletions
diff --git a/src/libstrongswan/tests/Makefile.in b/src/libstrongswan/tests/Makefile.in index 656be4efb..e58831c5b 100644 --- a/src/libstrongswan/tests/Makefile.in +++ b/src/libstrongswan/tests/Makefile.in @@ -402,7 +402,6 @@ nm_LIBS = @nm_LIBS@ nm_ca_dir = @nm_ca_dir@ nm_plugins = @nm_plugins@ oldincludedir = @oldincludedir@ -openac_plugins = @openac_plugins@ pcsclite_CFLAGS = @pcsclite_CFLAGS@ pcsclite_LIBS = @pcsclite_LIBS@ pdfdir = @pdfdir@ diff --git a/src/libstrongswan/tests/suites/test_chunk.c b/src/libstrongswan/tests/suites/test_chunk.c index e373fbdb6..34ace2894 100644 --- a/src/libstrongswan/tests/suites/test_chunk.c +++ b/src/libstrongswan/tests/suites/test_chunk.c @@ -117,10 +117,13 @@ START_TEST(test_chunk_clear) } chunk_clear(&chunk); /* check memory area of freed chunk. We can't use ck_assert() for this - * test directly, as it might allocate data at the freed area. */ - for (i = 0; i < 64; i++) + * test directly, as it might allocate data at the freed area. comparing + * two bytes at once reduces the chances of conflicts if memory got + * overwritten already */ + for (i = 0; i < 64; i += 2) { - if (ptr[i] != 0 && ptr[i] == i) + if (ptr[i] != 0 && ptr[i] == i && + ptr[i+1] != 0 && ptr[i+1] == i+1) { cleared = FALSE; break; diff --git a/src/libstrongswan/tests/suites/test_enumerator.c b/src/libstrongswan/tests/suites/test_enumerator.c index b5dde4650..9bd6d24f2 100644 --- a/src/libstrongswan/tests/suites/test_enumerator.c +++ b/src/libstrongswan/tests/suites/test_enumerator.c @@ -104,10 +104,10 @@ static void destroy_data(void *data) * filtered test */ -static bool filter(void *data, int *v, int *vo, int *w, int *wo, - int *x, int *xo, int *y, int *yo, int *z, int *zo) +static bool filter(int *data, int **v, int *vo, int **w, int *wo, + int **x, int *xo, int **y, int *yo, int **z, int *zo) { - int val = *v; + int val = **v; *vo = val++; *wo = val++; @@ -118,21 +118,21 @@ static bool filter(void *data, int *v, int *vo, int *w, int *wo, return TRUE; } -static bool filter_odd(void *data, int *item, int *out) +static bool filter_odd(void *data, int **item, int *out) { fail_if(data != (void*)101, "data does not match '101' in filter function"); - *out = *item; - return *item % 2 == 0; + *out = **item; + return **item % 2 == 0; } START_TEST(test_filtered) { - int round, v, w, x, y, z; + int data[5] = {1,2,3,4,5}, round, v, w, x, y, z; linked_list_t *list; enumerator_t *enumerator; - list = linked_list_create_with_items((void*)1, (void*)2, (void*)3, (void*)4, - (void*)5, NULL); + list = linked_list_create_with_items(&data[0], &data[1], &data[2], &data[3], + &data[4], NULL); round = 1; enumerator = enumerator_create_filter(list->create_enumerator(list), @@ -155,12 +155,12 @@ END_TEST START_TEST(test_filtered_filter) { - int count, x; + int data[5] = {1,2,3,4,5}, count, x; linked_list_t *list; enumerator_t *enumerator; - list = linked_list_create_with_items((void*)1, (void*)2, (void*)3, (void*)4, - (void*)5, NULL); + list = linked_list_create_with_items(&data[0], &data[1], &data[2], &data[3], + &data[4], NULL); count = 0; /* should also work without destructor, so set this manually */ diff --git a/src/libstrongswan/tests/suites/test_ntru.c b/src/libstrongswan/tests/suites/test_ntru.c index a46f5742c..7c0cb81bf 100644 --- a/src/libstrongswan/tests/suites/test_ntru.c +++ b/src/libstrongswan/tests/suites/test_ntru.c @@ -20,6 +20,8 @@ #include <plugins/ntru/ntru_mgf1.h> #include <plugins/ntru/ntru_trits.h> #include <plugins/ntru/ntru_poly.h> +#include <plugins/ntru/ntru_param_set.h> +#include <plugins/ntru/ntru_private_key.h> #include <utils/test.h> IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_drbg_create, ntru_drbg_t*, @@ -41,6 +43,18 @@ IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_poly_create_from_data, ntru_poly_t*, uint32_t indices_len_p, uint32_t indices_len_m, bool is_product_form) +IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_param_set_get_by_id, ntru_param_set_t* , + ntru_param_set_id_t id) + +IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_private_key_create, ntru_private_key_t*, + ntru_drbg_t *drbg, ntru_param_set_t *params) + +IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_private_key_create_from_data, ntru_private_key_t*, + ntru_drbg_t *drbg, chunk_t data) + +IMPORT_FUNCTION_FOR_TESTS(ntru, ntru_public_key_create_from_data, ntru_public_key_t*, + ntru_drbg_t *drbg, chunk_t data) + /** * NTRU parameter sets to test */ @@ -86,7 +100,8 @@ START_TEST(test_ntru_drbg_strength) entropy = lib->crypto->create_rng(lib->crypto, RNG_STRONG); ck_assert(entropy != NULL); - drbg = ntru_drbg_create(strengths[_i].requested, chunk_empty, entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, strengths[_i].requested, + chunk_empty, entropy); if (strengths[_i].standard) { ck_assert(drbg != NULL); @@ -243,7 +258,8 @@ START_TEST(test_ntru_drbg) out = chunk_alloc(128); entropy = test_rng_create(drbg_tests[_i].entropy); - drbg = ntru_drbg_create(256, drbg_tests[_i].pers_str, entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, 256, drbg_tests[_i].pers_str, + entropy); ck_assert(drbg != NULL); ck_assert(drbg->reseed(drbg)); ck_assert(drbg->generate(drbg, 256, 128, out.ptr)); @@ -265,7 +281,7 @@ START_TEST(test_ntru_drbg_reseed) "libstrongswan.plugins.ntru.max_drbg_requests", 2); out = chunk_alloc(128); entropy = test_rng_create(drbg_tests[0].entropy); - drbg = ntru_drbg_create(256, chunk_empty, entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, 256, chunk_empty, entropy); /* bad output parameters */ ck_assert(!drbg->generate(drbg, 256, 0, out.ptr)); @@ -283,13 +299,13 @@ START_TEST(test_ntru_drbg_reseed) drbg->destroy(drbg); /* no entropy available for DRBG instantiation */ - drbg = ntru_drbg_create(256, chunk_empty, entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, 256, chunk_empty, entropy); ck_assert(drbg == NULL); entropy->destroy(entropy); /* one automatic reseeding occurs */ entropy = test_rng_create(drbg_tests[0].entropy); - drbg = ntru_drbg_create(256, chunk_empty, entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, 256, chunk_empty, entropy); ck_assert(drbg->generate(drbg, 256, 128, out.ptr)); ck_assert(drbg->generate(drbg, 256, 128, out.ptr)); ck_assert(drbg->generate(drbg, 256, 128, out.ptr)); @@ -374,7 +390,7 @@ uint16_t indices_ees1171ep1[] = { */ mgf1_test_t mgf1_tests[] = { { HASH_SHA1, 20, 60, 20, 15, 24, - chunk_from_chars( + chunk_from_chars( 0xED, 0xA5, 0xC3, 0xBC, 0xAF, 0xB3, 0x20, 0x7D, 0x14, 0xA1, 0x54, 0xF7, 0x8B, 0x37, 0xF2, 0x8D, 0x8C, 0x9B, 0xD5, 0x63, 0x57, 0x38, 0x11, 0xC2, @@ -408,7 +424,7 @@ mgf1_test_t mgf1_tests[] = { 0x40, 0x4B, 0xE7, 0x22, 0x3A, 0x56, 0x10, 0x6D, 0x4D, 0x29, 0x0B, 0xCE, 0xA6, 0x21, 0xB5, 0x5C, 0x71, 0x66, 0x2F, 0x70, 0x35, 0xD8, 0x8A, 0x92, - 0x33, 0xF0, 0x16, 0xD4, 0x0E, 0x43, 0x8A, 0x14), + 0x33, 0xF0, 0x16, 0xD4, 0x0E, 0x43, 0x8A, 0x14), chunk_from_chars( 1, 2, 1, 0, 0, 1, 1, 1, 2, 0, 1, 0, 1, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 1, 0, 2, 0, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 0, 0, @@ -466,7 +482,7 @@ mgf1_test_t mgf1_tests[] = { 0x76, 0x89, 0x8B, 0x1B, 0x60, 0xEC, 0x10, 0x9D, 0x8F, 0x13, 0xF2, 0xFE, 0xD9, 0x85, 0xC1, 0xAB, 0x7E, 0xEE, 0xB1, 0x31, 0xDD, 0xF7, 0x7F, 0x0C, - 0x7D, 0xF9, 0x6B, 0x7B, 0x19, 0x80, 0xBD, 0x28), + 0x7D, 0xF9, 0x6B, 0x7B, 0x19, 0x80, 0xBD, 0x28), chunk_from_chars( 0xF1, 0x19, 0x02, 0x4F, 0xDA, 0x58, 0x05, 0x9A, 0x07, 0xDF, 0x61, 0x81, 0x22, 0x0E, 0x15, 0x46, @@ -542,14 +558,17 @@ START_TEST(test_ntru_mgf1) mask2.len = mgf1_tests[_i].ml2; mask3.len = mgf1_tests[_i].ml3; - mgf1 = ntru_mgf1_create(HASH_UNKNOWN, mgf1_tests[_i].seed, TRUE); + mgf1 = TEST_FUNCTION(ntru, ntru_mgf1_create, HASH_UNKNOWN, + mgf1_tests[_i].seed, TRUE); ck_assert(mgf1 == NULL); - mgf1 = ntru_mgf1_create(mgf1_tests[_i].alg, chunk_empty, TRUE); + mgf1 = TEST_FUNCTION(ntru, ntru_mgf1_create, mgf1_tests[_i].alg, + chunk_empty, TRUE); ck_assert(mgf1 == NULL); /* return mask in allocated chunk */ - mgf1 = ntru_mgf1_create(mgf1_tests[_i].alg, mgf1_tests[_i].seed, TRUE); + mgf1 = TEST_FUNCTION(ntru, ntru_mgf1_create, mgf1_tests[_i].alg, + mgf1_tests[_i].seed, TRUE); ck_assert(mgf1); /* check hash size */ @@ -565,14 +584,16 @@ START_TEST(test_ntru_mgf1) mgf1->destroy(mgf1); /* copy mask to pre-allocated buffer */ - mgf1 = ntru_mgf1_create(mgf1_tests[_i].alg, mgf1_tests[_i].seed, TRUE); + mgf1 = TEST_FUNCTION(ntru, ntru_mgf1_create, mgf1_tests[_i].alg, + mgf1_tests[_i].seed, TRUE); ck_assert(mgf1); ck_assert(mgf1->get_mask(mgf1, mgf1_tests[_i].mask.len, mask.ptr)); ck_assert(chunk_equals(mask, mgf1_tests[_i].mask)); mgf1->destroy(mgf1); /* get mask in batches without hashing the seed */ - mgf1 = ntru_mgf1_create(mgf1_tests[_i].alg, mgf1_tests[_i].hashed_seed, FALSE); + mgf1 = TEST_FUNCTION(ntru, ntru_mgf1_create, mgf1_tests[_i].alg, + mgf1_tests[_i].hashed_seed, FALSE); ck_assert(mgf1); /* first batch */ @@ -600,16 +621,16 @@ START_TEST(test_ntru_trits) ntru_trits_t *mask; chunk_t trits; - mask = ntru_trits_create(mgf1_tests[_i].trits.len, HASH_UNKNOWN, - mgf1_tests[_i].seed); + mask = TEST_FUNCTION(ntru, ntru_trits_create, mgf1_tests[_i].trits.len, + HASH_UNKNOWN, mgf1_tests[_i].seed); ck_assert(mask == NULL); - mask = ntru_trits_create(mgf1_tests[_i].trits.len, mgf1_tests[_i].alg, - chunk_empty); + mask = TEST_FUNCTION(ntru, ntru_trits_create, mgf1_tests[_i].trits.len, + mgf1_tests[_i].alg, chunk_empty); ck_assert(mask == NULL); - mask = ntru_trits_create(mgf1_tests[_i].trits.len, mgf1_tests[_i].alg, - mgf1_tests[_i].seed); + mask = TEST_FUNCTION(ntru, ntru_trits_create, mgf1_tests[_i].trits.len, + mgf1_tests[_i].alg, mgf1_tests[_i].seed); ck_assert(mask); trits = chunk_create(mask->get_trits(mask), mask->get_size(mask)); @@ -617,7 +638,8 @@ START_TEST(test_ntru_trits) mask->destroy(mask); /* generate a multiple of 5 trits */ - mask = ntru_trits_create(10, mgf1_tests[_i].alg, mgf1_tests[_i].seed); + mask = TEST_FUNCTION(ntru, ntru_trits_create, 10, mgf1_tests[_i].alg, + mgf1_tests[_i].seed); ck_assert(mask); trits = chunk_create(mask->get_trits(mask), mask->get_size(mask)); @@ -638,17 +660,17 @@ START_TEST(test_ntru_poly) seed.len = mgf1_tests[_i].seed_len; p = &mgf1_tests[_i].poly_test[0]; - poly = ntru_poly_create_from_seed(HASH_UNKNOWN, seed, p->c_bits, p->N, p->q, - p->indices_len, p->indices_len, - p->is_product_form); + poly = TEST_FUNCTION(ntru, ntru_poly_create_from_seed, HASH_UNKNOWN, seed, + p->c_bits, p->N, p->q, p->indices_len, p->indices_len, + p->is_product_form); ck_assert(poly == NULL); for (n = 0; n < 2; n++) { p = &mgf1_tests[_i].poly_test[n]; - poly = ntru_poly_create_from_seed(mgf1_tests[_i].alg, seed, p->c_bits, - p->N, p->q, p->indices_len, - p->indices_len, p->is_product_form); + poly = TEST_FUNCTION(ntru, ntru_poly_create_from_seed, + mgf1_tests[_i].alg, seed, p->c_bits, p->N, p->q, + p->indices_len, p->indices_len, p->is_product_form); ck_assert(poly != NULL && poly->get_size(poly) == p->indices_size); indices = poly->get_indices(poly); @@ -748,8 +770,9 @@ START_TEST(test_ntru_ring_mult) int i; t = &ring_mult_tests[_i]; - poly = ntru_poly_create_from_data(t->indices, t->N, t->q, t->indices_len_p, - t->indices_len_m, t->is_product_form); + poly = TEST_FUNCTION(ntru, ntru_poly_create_from_data, t->indices, t->N, + t->q, t->indices_len_p, t->indices_len_m, + t->is_product_form); ck_assert(poly != NULL); c = malloc(t->N * sizeof(uint16_t)); @@ -776,8 +799,9 @@ START_TEST(test_ntru_array) t = &ring_mult_tests[array_tests[_i]]; - poly = ntru_poly_create_from_data(t->indices, t->N, t->q, t->indices_len_p, - t->indices_len_m, t->is_product_form); + poly = TEST_FUNCTION(ntru, ntru_poly_create_from_data, t->indices, t->N, + t->q, t->indices_len_p, t->indices_len_m, + t->is_product_form); ck_assert(poly != NULL); c = malloc(t->N * sizeof(uint16_t)); @@ -793,62 +817,413 @@ START_TEST(test_ntru_array) } END_TEST +START_TEST(test_ntru_param_set) +{ + ck_assert(TEST_FUNCTION(ntru, ntru_param_set_get_by_id, -1) == NULL); + ck_assert(TEST_FUNCTION(ntru, ntru_param_set_get_by_id, 16) == NULL); +} +END_TEST + +typedef struct { + ntru_param_set_id_t id; + chunk_t entropy; + chunk_t encoding; +} privkey_test_t; + +privkey_test_t privkey_tests[] = { + { + NTRU_EES401EP1, + chunk_from_chars( + 0x0C, 0x2F, 0x24, 0xE1, 0xA4, 0x81, 0x26, 0xA2, + 0x6C, 0xEA, 0xCD, 0x1A, 0xF3, 0xEB, 0x3D, 0xBF, + 0xEA, 0xAE, 0xC3, 0x0D, 0xC1), + chunk_from_chars( + 0x02, 0x03, 0x00, 0x02, 0x04, 0x3E, 0xF3, 0xCB, + 0x7A, 0x58, 0x13, 0x75, 0xBB, 0x87, 0xF5, 0xBF, + 0x2E, 0x18, 0xAE, 0x03, 0xAF, 0xB8, 0x33, 0x85, + 0xD8, 0xBF, 0x8A, 0xB5, 0x8C, 0xA6, 0xDF, 0x03, + 0x90, 0x1E, 0xE4, 0x83, 0xA4, 0x95, 0x40, 0xB5, + 0x08, 0x92, 0x29, 0xD8, 0x83, 0xA8, 0x42, 0xB2, + 0x69, 0xC2, 0x00, 0x8B, 0xAE, 0x80, 0x00, 0x4F, + 0x3D, 0xDD, 0xFB, 0xDB, 0x9A, 0xD8, 0x0F, 0xFF, + 0xBC, 0x21, 0xD5, 0xE6, 0x04, 0x9C, 0xDD, 0x3B, + 0x2D, 0x16, 0x4B, 0xC7, 0x3D, 0xBE, 0xDE, 0xBB, + 0x6F, 0xF4, 0x8A, 0x31, 0xCD, 0x23, 0x19, 0xC2, + 0x3C, 0xE1, 0xE2, 0xEE, 0xE4, 0xE7, 0x2E, 0xFC, + 0x5C, 0xDD, 0xAD, 0x0C, 0x9D, 0x98, 0xC5, 0x18, + 0x2A, 0x80, 0x21, 0x93, 0x61, 0xC4, 0x9A, 0x16, + 0xE8, 0x9B, 0xF7, 0x3B, 0x6D, 0x06, 0x91, 0x9E, + 0x71, 0x59, 0xBE, 0x8E, 0x65, 0x61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C, 0xA1, 0xD1, 0x5A, 0x3E, 0x94, 0xF8, + 0x03, 0xDE, 0xB8, 0x94, 0x11, 0x92, 0x80, 0x77, + 0x57, 0x45, 0x1E, 0x6B, 0xA5, 0x15, 0xDB, 0x48, + 0xB6, 0x9E, 0x02, 0xF1, 0x61, 0x4A, 0xAC, 0x1D, + 0x49, 0xBC, 0xA9, 0x3F, 0x03, 0x50, 0xAC, 0x02, + 0x8E, 0x84, 0xE0, 0x12, 0x37, 0x76, 0xBC, 0x4A, + 0xF9, 0xC6, 0x74, 0x36, 0xFC, 0x92, 0x1D, 0x59, + 0x0C, 0x04, 0xD2, 0x14, 0xB7, 0x11, 0xE9, 0xE2, + 0xFE, 0x0C, 0xE1, 0xDA, 0x8B, 0xCA, 0x10, 0xA1, + 0x60, 0xB6, 0x57, 0x51, 0x00, 0xD6, 0x5B, 0x55, + 0x09, 0x60, 0xE8, 0x00, 0x40, 0x45, 0x56, 0xBA, + 0x83, 0x1E, 0x36, 0x12, 0x59, 0x4B, 0x19, 0x00, + 0x53, 0xAE, 0x62, 0xA6, 0x29, 0x39, 0xED, 0x87, + 0x24, 0x37, 0x1E, 0x1B, 0xCF, 0x3F, 0x3A, 0x71, + 0x31, 0xB5, 0x50, 0x8D, 0x4B, 0x53, 0x53, 0x75, + 0x3F, 0x33, 0x39, 0x09, 0x2A, 0x78, 0xA8, 0x71, + 0x3E, 0x63, 0xC5, 0x61, 0x73, 0xB6, 0xE1, 0x71, + 0x16, 0xDA, 0x06, 0xBF, 0x3F, 0x22, 0x74, 0x89, + 0x08, 0xD2, 0x05, 0x0B, 0x16, 0xC8, 0xF0, 0x17, + 0x4E, 0xA2, 0x65, 0x67, 0x6D, 0x02) + }, + { + NTRU_EES743EP1, + chunk_from_chars( + 0x9B, 0xAB, 0x57, 0xDB, 0x2C, 0x60, 0x83, 0x48, + 0x9F, 0xC9, 0x70, 0x8F, 0x69, 0xF7, 0xB4, 0xBB, + 0x63, 0x5C, 0x9A, 0x63, 0x07, 0x80, 0x17, 0xD3, + 0xCD, 0xB1, 0x57, 0x79, 0xFE, 0x8D, 0x81, 0x70, + 0xEB, 0x50, 0xFA, 0x05, 0xFB, 0x97, 0xB2, 0xAB, + 0x25, 0xED, 0xD8, 0x18, 0x1C, 0xFE, 0x96, 0x7D), + chunk_from_chars( + 0x02, 0x03, 0x00, 0x06, 0x10, 0x14, 0x53, 0x73, + 0x56, 0xF5, 0xA9, 0x34, 0xDE, 0xA6, 0x4D, 0x46, + 0x05, 0x9E, 0x80, 0xAE, 0xB6, 0x74, 0x91, 0xFF, + 0xFB, 0x48, 0xD3, 0x5C, 0x61, 0x12, 0x46, 0x02, + 0x9F, 0x53, 0x45, 0x87, 0x47, 0xBD, 0x6B, 0x26, + 0xF7, 0x36, 0xD3, 0x99, 0x1B, 0xD7, 0xEA, 0xA3, + 0xA8, 0x94, 0xFF, 0x93, 0x46, 0x7C, 0x2C, 0x5F, + 0x87, 0x8C, 0x38, 0xB3, 0x7B, 0xC6, 0x49, 0xE2, + 0x88, 0xCA, 0x67, 0x89, 0xD0, 0x6D, 0x7C, 0xAE, + 0x7C, 0x98, 0x84, 0xDA, 0x6B, 0x93, 0x92, 0xEF, + 0x4A, 0xD1, 0x4A, 0xD2, 0x5B, 0x13, 0xF8, 0x59, + 0x15, 0x2E, 0xBC, 0x70, 0x8D, 0x2D, 0xA9, 0x47, + 0xA1, 0x99, 0x19, 0x3F, 0x67, 0xE8, 0x18, 0xA7, + 0x17, 0x07, 0xB3, 0x14, 0xF6, 0x20, 0xA1, 0xD8, + 0x33, 0xE8, 0x08, 0x6A, 0xC1, 0x39, 0x99, 0x08, + 0xB4, 0x88, 0xEB, 0x48, 0x7D, 0xFB, 0xF5, 0xEF, + 0x03, 0x0D, 0x25, 0xB7, 0x98, 0xF3, 0xF1, 0x15, + 0x63, 0xE4, 0x0F, 0xFD, 0x54, 0x9F, 0x56, 0xE9, + 0xD1, 0x44, 0xE5, 0x89, 0x66, 0x14, 0x91, 0x1C, + 0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0x68, 0x89, 0x3F, 0x15, + 0x25, 0xD4, 0x99, 0x53, 0x4C, 0x85, 0x98, 0x78, + 0x1D, 0x35, 0x4A, 0x83, 0x79, 0x9A, 0x29, 0x90, + 0x2B, 0x45, 0x76, 0x0C, 0x13, 0x80, 0x4A, 0xE0, + 0x40, 0xED, 0x6B, 0x2E, 0x2A, 0x43, 0xA9, 0x28, + 0xB0, 0x2F, 0x89, 0x01, 0x6B, 0x39, 0x8C, 0x5E, + 0x80, 0x61, 0xD9, 0xEE, 0x0F, 0x41, 0x75, 0xB5, + 0xAE, 0xB6, 0xC2, 0x42, 0x49, 0x8D, 0x89, 0xD8, + 0xF4, 0x78, 0x1D, 0x90, 0x46, 0x26, 0x4C, 0x56, + 0xB7, 0xC0, 0xD9, 0x98, 0x7B, 0x07, 0xA1, 0x20) + } +}; + +START_TEST(test_ntru_privkey) +{ + rng_t *entropy; + ntru_drbg_t *drbg; + ntru_private_key_t *privkey; + ntru_public_key_t *pubkey; + ntru_param_set_t *params; + uint32_t strength; + chunk_t encoding, privkey_encoding, pubkey_encoding; + + params = TEST_FUNCTION(ntru, ntru_param_set_get_by_id, + privkey_tests[_i].id); + strength = params->sec_strength_len * BITS_PER_BYTE; + entropy = test_rng_create(privkey_tests[_i].entropy); + drbg = TEST_FUNCTION(ntru, ntru_drbg_create, strength, + chunk_from_str("IKE NTRU-KE"), entropy); + ck_assert(drbg != NULL); + + privkey = TEST_FUNCTION(ntru, ntru_private_key_create, drbg, params); + ck_assert(privkey); + ck_assert(privkey->get_id(privkey) == privkey_tests[_i].id); + + privkey_encoding = privkey->get_encoding(privkey); + encoding = privkey_tests[_i].encoding; + ck_assert(chunk_equals(privkey_encoding, encoding)); + + /* load private key as a packed blob */ + privkey->destroy(privkey); + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, chunk_empty); + ck_assert(privkey == NULL); + + encoding = chunk_clone(encoding); + encoding.ptr[0] = NTRU_PUBKEY_TAG; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + ck_assert(privkey == NULL); + + encoding.ptr[0] = NTRU_PRIVKEY_TRITS_TAG; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + if (params->is_product_form) + { + ck_assert(privkey == NULL); + } + else + { + ck_assert(privkey != NULL); + privkey->destroy(privkey); + } + + encoding.ptr[0] = NTRU_PRIVKEY_INDICES_TAG; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + if (params->is_product_form) + { + ck_assert(privkey != NULL); + privkey->destroy(privkey); + } + else + { + ck_assert(privkey == NULL); + } + + encoding.ptr[0] = NTRU_PRIVKEY_DEFAULT_TAG; + encoding.ptr[1] = NTRU_OID_LEN - 1; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + ck_assert(privkey == NULL); + + encoding.ptr[1] = NTRU_OID_LEN; + encoding.ptr[2] = 0xff; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + ck_assert(privkey == NULL); + + encoding.ptr[2] = params->oid[0]; + privkey = TEST_FUNCTION(ntru, ntru_private_key_create_from_data, + drbg, encoding); + privkey_encoding = privkey->get_encoding(privkey); + ck_assert(chunk_equals(privkey_encoding, encoding)); + + pubkey = privkey->get_public_key(privkey); + pubkey_encoding = pubkey->get_encoding(pubkey); + + encoding.ptr[0] = NTRU_PUBKEY_TAG; + encoding.len = pubkey_encoding.len; + ck_assert(chunk_equals(pubkey_encoding, encoding)); + + /* load public key as a packed blob */ + pubkey->destroy(pubkey); + pubkey = TEST_FUNCTION(ntru, ntru_public_key_create_from_data, + drbg, encoding); + pubkey_encoding = pubkey->get_encoding(pubkey); + ck_assert(chunk_equals(pubkey_encoding, encoding)); + + chunk_free(&encoding); + privkey->destroy(privkey); + pubkey->destroy(pubkey); + drbg->destroy(drbg); + entropy->destroy(entropy); +} +END_TEST + START_TEST(test_ntru_ke) { chunk_t pub_key, cipher_text, i_shared_secret, r_shared_secret; diffie_hellman_t *i_ntru, *r_ntru; char buf[10]; - int n, len; + int k, n, len; status_t status; + k = (_i) / countof(parameter_sets); + n = (_i) % countof(parameter_sets); + len = snprintf(buf, sizeof(buf), "%N", diffie_hellman_group_names, - params[_i].group); + params[k].group); ck_assert(len == 8); - ck_assert(streq(buf, params[_i].group_name)); - - for (n = 0; n < countof(parameter_sets); n++) - { - lib->settings->set_str(lib->settings, - "libstrongswan.plugins.ntru.parameter_set", - parameter_sets[n]); + ck_assert(streq(buf, params[k].group_name)); - i_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group); - ck_assert(i_ntru != NULL); - ck_assert(i_ntru->get_dh_group(i_ntru) == params[_i].group); + lib->settings->set_str(lib->settings, + "libstrongswan.plugins.ntru.parameter_set", parameter_sets[n]); - i_ntru->get_my_public_value(i_ntru, &pub_key); - ck_assert(pub_key.len > 0); + i_ntru = lib->crypto->create_dh(lib->crypto, params[k].group); + ck_assert(i_ntru != NULL); + ck_assert(i_ntru->get_dh_group(i_ntru) == params[k].group); - r_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group); - ck_assert(r_ntru != NULL); + i_ntru->get_my_public_value(i_ntru, &pub_key); + ck_assert(pub_key.len > 0); - r_ntru->set_other_public_value(r_ntru, pub_key); - r_ntru->get_my_public_value(r_ntru, &cipher_text); - ck_assert(cipher_text.len > 0); + r_ntru = lib->crypto->create_dh(lib->crypto, params[k].group); + ck_assert(r_ntru != NULL); - status = r_ntru->get_shared_secret(r_ntru, &r_shared_secret); - ck_assert(status == SUCCESS); - ck_assert(r_shared_secret.len > 0); + r_ntru->set_other_public_value(r_ntru, pub_key); + r_ntru->get_my_public_value(r_ntru, &cipher_text); + ck_assert(cipher_text.len > 0); - i_ntru->set_other_public_value(i_ntru, cipher_text); - status = i_ntru->get_shared_secret(i_ntru, &i_shared_secret); + status = r_ntru->get_shared_secret(r_ntru, &r_shared_secret); + ck_assert(status == SUCCESS); + ck_assert(r_shared_secret.len > 0); - if (status == SUCCESS) - { - ck_assert(chunk_equals(i_shared_secret, r_shared_secret)); - } - else - { - ck_assert(i_shared_secret.len == 0); - } + i_ntru->set_other_public_value(i_ntru, cipher_text); + status = i_ntru->get_shared_secret(i_ntru, &i_shared_secret); + ck_assert(status == SUCCESS); + ck_assert(chunk_equals(i_shared_secret, r_shared_secret)); - chunk_clear(&i_shared_secret); - chunk_clear(&r_shared_secret); - chunk_free(&pub_key); - chunk_free(&cipher_text); - i_ntru->destroy(i_ntru); - r_ntru->destroy(r_ntru); - } + chunk_clear(&i_shared_secret); + chunk_clear(&r_shared_secret); + chunk_free(&pub_key); + chunk_free(&cipher_text); + i_ntru->destroy(i_ntru); + r_ntru->destroy(r_ntru); } END_TEST @@ -1015,8 +1390,17 @@ Suite *ntru_suite_create() tcase_add_loop_test(tc, test_ntru_array, 0, countof(array_tests)); suite_add_tcase(s, tc); + tc = tcase_create("param_set"); + tcase_add_test(tc, test_ntru_param_set); + suite_add_tcase(s, tc); + + tc = tcase_create("privkey"); + tcase_add_loop_test(tc, test_ntru_privkey, 0, countof(privkey_tests)); + suite_add_tcase(s, tc); + tc = tcase_create("ke"); - tcase_add_loop_test(tc, test_ntru_ke, 0, countof(params)); + tcase_add_loop_test(tc, test_ntru_ke, 0, + countof(params) * countof(parameter_sets)); suite_add_tcase(s, tc); tc = tcase_create("retransmission"); diff --git a/src/libstrongswan/tests/suites/test_vectors.c b/src/libstrongswan/tests/suites/test_vectors.c index 242ac9d09..a1205d0be 100644 --- a/src/libstrongswan/tests/suites/test_vectors.c +++ b/src/libstrongswan/tests/suites/test_vectors.c @@ -1,4 +1,7 @@ /* + * Copyright (C) 2014 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * * Copyright (C) 2013 Martin Willi * Copyright (C) 2013 revosec AG * @@ -15,13 +18,15 @@ #include "test_suite.h" -/******************************************************************************* - * Check if test vectors have been successful during transform registration - */ +#include <utils/test.h> + +IMPORT_FUNCTION_FOR_TESTS(crypto, verify_registered_algorithms, u_int, + crypto_factory_t *factory); START_TEST(test_vectors) { - u_int failed = lib->crypto->get_test_vector_failures(lib->crypto); + u_int failed = TEST_FUNCTION(crypto, verify_registered_algorithms, + lib->crypto); fail_if(failed > 0, "%u test vectors failed", failed); } END_TEST diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c index 0b26ee128..5ec4198e7 100644 --- a/src/libstrongswan/tests/test_runner.c +++ b/src/libstrongswan/tests/test_runner.c @@ -22,6 +22,7 @@ #include <collections/array.h> #include <utils/test.h> +#include <stdlib.h> #include <dirent.h> #include <unistd.h> #include <limits.h> @@ -32,31 +33,85 @@ #define TTY(color) tty_escape_get(2, TTY_FG_##color) /** - * Initialize the lookup table for testable functions (defined in libstrongswan) + * Initialize the lookup table for testable functions (defined in + * libstrongswan). We don't use the constructor attribute as the order can't + * really be defined (clang does not support it and gcc does not adhere to it in + * the monolithic build). The function here is a weak symbol in libstrongswan. */ -static void testable_functions_create() __attribute__ ((constructor(1000))); -static void testable_functions_create() +void testable_functions_create() { - testable_functions = hashtable_create(hashtable_hash_str, - hashtable_equals_str, 8); + if (!testable_functions) + { + testable_functions = hashtable_create(hashtable_hash_str, + hashtable_equals_str, 8); + } } /** * Destroy the lookup table for testable functions */ -static void testable_functions_destroy() __attribute__ ((destructor(1000))); +static void testable_functions_destroy() __attribute__ ((destructor)); static void testable_functions_destroy() { - testable_functions->destroy(testable_functions); + DESTROY_IF(testable_functions); /* if leak detective is enabled plugins are not actually unloaded, which * means their destructor is called AFTER this one when the process - * terminates, even though the priority says differently, make sure this - * does not crash */ + * terminates, make sure this does not crash */ testable_functions = NULL; } /** - * Load all available test suites + * Destroy a single test suite and associated data + */ +static void destroy_suite(test_suite_t *suite) +{ + test_case_t *tcase; + + while (array_remove(suite->tcases, 0, &tcase)) + { + array_destroy(tcase->functions); + array_destroy(tcase->fixtures); + } + free(suite); +} + +/** + * Removes and destroys test suites that are not selected. + */ +static void filter_suites(array_t *loaded) +{ + enumerator_t *enumerator, *names; + hashtable_t *selected; + test_suite_t *suite; + char *suites, *name; + + suites = getenv("TESTS_SUITES"); + if (!suites) + { + return; + } + selected = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8); + names = enumerator_create_token(suites, ",", " "); + while (names->enumerate(names, &name)) + { + selected->put(selected, name, name); + } + enumerator = array_create_enumerator(loaded); + while (enumerator->enumerate(enumerator, &suite)) + { + if (!selected->get(selected, suite->name)) + { + array_remove_at(loaded, enumerator); + destroy_suite(suite); + } + } + enumerator->destroy(enumerator); + selected->destroy(selected); + names->destroy(names); +} + +/** + * Load all available test suites, or optionally only selected ones. */ static array_t *load_suites(test_configuration_t configs[], test_runner_init_t init) @@ -91,6 +146,7 @@ static array_t *load_suites(test_configuration_t configs[], array_insert(suites, -1, configs[i].suite()); } } + filter_suites(suites); if (lib->leak_detective) { @@ -112,16 +168,10 @@ static array_t *load_suites(test_configuration_t configs[], static void unload_suites(array_t *suites) { test_suite_t *suite; - test_case_t *tcase; while (array_remove(suites, 0, &suite)) { - while (array_remove(suite->tcases, 0, &tcase)) - { - array_destroy(tcase->functions); - array_destroy(tcase->fixtures); - } - free(suite); + destroy_suite(suite); } array_destroy(suites); } @@ -178,6 +228,9 @@ static bool call_fixture(test_case_t *tcase, bool up) */ static bool pre_test(test_runner_init_t init) { + level_t level = LEVEL_SILENT; + char *verbosity; + library_init(NULL, "test-runner"); /* use non-blocking RNG to generate keys fast */ @@ -185,6 +238,9 @@ static bool pre_test(test_runner_init_t init) "libstrongswan.plugins.random.random", lib->settings->get_str(lib->settings, "libstrongswan.plugins.random.urandom", "/dev/urandom")); + /* same for the gcrypt plugin */ + lib->settings->set_default_str(lib->settings, + "libstrongswan.plugins.gcrypt.quick_random", "yes"); if (lib->leak_detective) { @@ -197,7 +253,12 @@ static bool pre_test(test_runner_init_t init) library_deinit(); return FALSE; } - dbg_default_set_level(LEVEL_SILENT); + verbosity = getenv("TESTS_VERBOSITY"); + if (verbosity) + { + level = atoi(verbosity); + } + dbg_default_set_level(level); return TRUE; } @@ -254,7 +315,7 @@ static void sum_leaks(report_data_t *data, int count, size_t bytes, * Do library cleanup and optionally check for memory leaks */ static bool post_test(test_runner_init_t init, bool check_leaks, - array_t *failures, char *name, int i) + array_t *failures, char *name, int i, int *leaks) { report_data_t data = { .failures = failures, @@ -264,7 +325,15 @@ static bool post_test(test_runner_init_t init, bool check_leaks, if (init) { - init(FALSE); + if (test_restore_point()) + { + init(FALSE); + } + else + { + library_deinit(); + return FALSE; + } } if (check_leaks && lib->leak_detective) { @@ -274,7 +343,8 @@ static bool post_test(test_runner_init_t init, bool check_leaks, } library_deinit(); - return data.leaks != 0; + *leaks = data.leaks; + return TRUE; } /** @@ -346,7 +416,8 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init) { if (pre_test(init)) { - bool ok = FALSE, leaks = FALSE; + bool ok = FALSE; + int leaks = 0; test_setup_timeout(tcase->timeout); @@ -363,9 +434,11 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init) { call_fixture(tcase, FALSE); } - } - leaks = post_test(init, ok, failures, tfun->name, i); + if (!post_test(init, ok, failures, tfun->name, i, &leaks)) + { + ok = FALSE; + } test_setup_timeout(0); diff --git a/src/libstrongswan/tests/test_suite.c b/src/libstrongswan/tests/test_suite.c index 0f2e74b7c..fb40b05c1 100644 --- a/src/libstrongswan/tests/test_suite.c +++ b/src/libstrongswan/tests/test_suite.c @@ -136,7 +136,8 @@ static inline void test_failure() else { pthread_kill(main_thread, SIGUSR1); - /* how can we stop just the thread? longjmp to a restore point? */ + /* terminate thread to prevent it from going wild */ + pthread_exit(NULL); } } |