diff --git a/.gitignore b/.gitignore index 6a900ef..c398f78 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ install/ *.pdf *.dec *.enc +tests/test-file.out +tests/ntru_cunit # tmp files diff --git a/.travis.yml b/.travis.yml index 4eef998..4f89f20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ before_script: - cd ../.. script: - make CC=clang + - make CC=clang check - make doc after_script: - ./update-gh-pages.sh diff --git a/Makefile b/Makefile index f3f664f..259ca62 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,16 @@ uninstall: $(MAKE) -C src uninstall $(MAKE) -C include uninstall +check: + $(MAKE) -C tests check + +test: + $(MAKE) -C tests check + clean: $(MAKE) -C include clean $(MAKE) -C src clean + $(MAKE) -C tests clean doc: $(MAKE) -C include doc diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..394c1da --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,47 @@ +include ../common.mk + + +# sources, headers, objects +CUNIT_SOURCES = \ + ntru_cunit.c \ + ntru_file_cunit.c \ + ntru_poly_cunit.c \ + ntru_keypair_cunit.c \ + ntru_encrypt_cunit.c \ + ntru_decrypt_cunit.c + +CUNIT_OBJS = $(patsubst %.c, %.o, $(CUNIT_SOURCES)) + +CUNIT_HEADERS = \ + ntru_cunit.h + + +# libs +LIBS += -L. -lcunit -llz4 -lgmp -lmpfr -lflint \ + $(shell $(PKG_CONFIG) --libs glib-2.0) -lm + +# includes +INCS = -I. -I../include -I/usr/include/flint $(shell $(PKG_CONFIG) --cflags glib-2.0) + +CFLAGS += -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED + + +%.o: %.c + $(CC) -fPIC $(CFLAGS) $(CPPFLAGS) $(INCS) -c $*.c + +check: ntru_cunit + ./ntru_cunit + +test: ntru_cunit + +libpqc.a: + $(MAKE) -C ../src libpqc.a + +ntru_cunit: $(CUNIT_OBJS) $(CUNIT_HEADERS) libpqc.a + $(CC) $(CFLAGS) -o $@ $(CUNIT_OBJS) ../src/libpqc.a $(LDFLAGS) $(LIBS) + +clean: + rm -f *.o ntru_cunit *.orig core test-file.out pub.key + + +.PHONY: check clean test diff --git a/tests/ntru_cunit.c b/tests/ntru_cunit.c new file mode 100644 index 0000000..fbe9860 --- /dev/null +++ b/tests/ntru_cunit.c @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_cunit.c + * Main cunit file, registering the test suites. + * @brief test suite registration + */ + +#include "ntru_cunit.h" + +#include +#include +#include +#include +#include +#include + + +int init_suite(void) +{ + return 0; +} + +int clean_suite(void) +{ + return 0; +} + +int main(void) +{ + CU_pSuite pSuite = NULL; + int my_stderr; + + /* initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) + return CU_get_error(); + + /* add a suite to the registry */ + pSuite = CU_add_suite("filereader tests", + init_suite, + clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "test1 reading plain text file", + test_read_text_file1)) || + (NULL == CU_add_test(pSuite, "test2 reading plain text file", + test_read_text_file2)) || + (NULL == CU_add_test(pSuite, "test3 reading plain text file", + test_read_text_file3)) || + (NULL == CU_add_test(pSuite, "test4 reading plain text file", + test_read_text_file4)) || + (NULL == CU_add_test(pSuite, "test1 writing plain text file", + test_write_text_file1)) || + (NULL == CU_add_test(pSuite, "test2 writing plain text file", + test_write_text_file2)) || + (NULL == CU_add_test(pSuite, "test3 writing plain text file", + test_write_text_file3)) || + (NULL == CU_add_test(pSuite, "test4 writing plain text file", + test_write_text_file4)) || + (NULL == CU_add_test(pSuite, "test5 writing plain text file", + test_write_text_file5)) + ) { + + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add a suite to the registry */ + pSuite = CU_add_suite("polynomial tests", + init_suite, + clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "test1 polynomial creation", + test_poly_new1)) || + (NULL == CU_add_test(pSuite, "test2 polynomial creation", + test_poly_new2)) + ) { + + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add a suite to the registry */ + pSuite = CU_add_suite("keypair tests", + init_suite, + clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "test1 keypair creation", + test_create_keypair1)) || + (NULL == CU_add_test(pSuite, "test2 keypair creation", + test_create_keypair2)) || + (NULL == CU_add_test(pSuite, "test1 public key export", + test_export_public_key1)) || + (NULL == CU_add_test(pSuite, "test2 public key export", + test_export_public_key2)) || + (NULL == CU_add_test(pSuite, "test1 priv key export", + test_export_private_key1)) || + (NULL == CU_add_test(pSuite, "test2 priv key export", + test_export_private_key2)) || + (NULL == CU_add_test(pSuite, "test1 pub key import", + test_import_public_key1)) || + (NULL == CU_add_test(pSuite, "test2 pub key import", + test_import_public_key2)) || + (NULL == CU_add_test(pSuite, "test1 priv key import", + test_import_private_key1)) || + (NULL == CU_add_test(pSuite, "test2 priv key import", + test_import_private_key2)) + ) { + + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add a suite to the registry */ + pSuite = CU_add_suite("encryption tests", + init_suite, + clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "test1 string encryption", + test_encrypt_string1)) + ) { + + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add a suite to the registry */ + pSuite = CU_add_suite("decryption tests", + init_suite, + clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "test1 string decryption", + test_decrypt_string1)) + ) { + + CU_cleanup_registry(); + return CU_get_error(); + } + + /* save stderr stream and close it */ + my_stderr = dup(STDERR_FILENO); + close(STDERR_FILENO); + + /* Run all tests using the basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + printf("\n"); + CU_basic_show_failures(CU_get_failure_list()); + printf("\n\n"); + + /* Clean up registry and return */ + CU_cleanup_registry(); + + /* restore stderr stream */ + dup2(my_stderr, STDERR_FILENO); + + return CU_get_error(); +} diff --git a/tests/ntru_cunit.h b/tests/ntru_cunit.h new file mode 100644 index 0000000..942bdfc --- /dev/null +++ b/tests/ntru_cunit.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_cunit.h + * @brief header for ntru_cunit.c + */ + +/* + * filereader/writer + */ +void test_read_text_file1(void); +void test_read_text_file2(void); +void test_read_text_file3(void); +void test_read_text_file4(void); +void test_write_text_file1(void); +void test_write_text_file2(void); +void test_write_text_file3(void); +void test_write_text_file4(void); +void test_write_text_file5(void); + +/* + * poly + */ +void test_poly_new1(void); +void test_poly_new2(void); + +/* + * keypair + */ +void test_create_keypair1(void); +void test_create_keypair2(void); +void test_export_public_key1(void); +void test_export_public_key2(void); +void test_export_private_key1(void); +void test_export_private_key2(void); +void test_import_public_key1(void); +void test_import_public_key2(void); +void test_import_private_key1(void); +void test_import_private_key2(void); + +/* + * encryption + */ +void test_encrypt_string1(void); + +/* + * decryption + */ +void test_decrypt_string1(void); diff --git a/tests/ntru_decrypt_cunit.c b/tests/ntru_decrypt_cunit.c new file mode 100644 index 0000000..8cf71d9 --- /dev/null +++ b/tests/ntru_decrypt_cunit.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_encrypt_cunit.c + * Test cases for encrypting strings. + * @brief tests for ntru_encrypt.c + */ + +#include "ntru.h" +#include "ntru_decrypt.h" +#include "ntru_keypair.h" + +#include +#include +#include +#include +#include +#include +#include + + +/** + * Test decrypting an encrypted string. + */ +void test_decrypt_string1(void) +{ + keypair pair; + fmpz_poly_t f, g, rnd; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + int rnd_c[] = {-1, 0, 1, 1, 1, -1, 0, -1, 0, 0, 0}; + ntru_params params; + string *enc_string, + *dec_string; + char dec_c_str[1024]; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + poly_new(rnd, rnd_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + + enc_string = read_file("to-decrypt.txt"); + dec_string = ntru_decrypt_string(enc_string, pair.priv, pair.priv_inv, + ¶ms); + + memcpy(dec_c_str, dec_string->ptr, dec_string->len); + dec_c_str[dec_string->len] = '\0'; + string_delete(enc_string); + string_delete(dec_string); + + CU_ASSERT_EQUAL(strcmp(dec_c_str, "BLAHFASEL\n"), 0); +} diff --git a/tests/ntru_encrypt_cunit.c b/tests/ntru_encrypt_cunit.c new file mode 100644 index 0000000..5cac998 --- /dev/null +++ b/tests/ntru_encrypt_cunit.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_encrypt_cunit.c + * Test cases for encrypting strings. + * @brief tests for ntru_encrypt.c + */ + +#include "ntru.h" +#include "ntru_encrypt.h" +#include "ntru_keypair.h" + +#include +#include +#include +#include +#include +#include +#include + + +/** + * Test encrypting a string. + */ +void test_encrypt_string1(void) +{ + keypair pair; + fmpz_poly_t f, g, rnd; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + int rnd_c[] = {-1, 0, 1, 1, 1, -1, 0, -1, 0, 0, 0}; + ntru_params params; + string *enc_string, + *clear_string; + char enc_c_str[1024]; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + poly_new(rnd, rnd_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + + clear_string = read_file("to-encrypt.txt"); + + enc_string = ntru_encrypt_string(clear_string, pair.pub, + rnd, ¶ms); + + memcpy(enc_c_str, enc_string->ptr, enc_string->len); + enc_c_str[enc_string->len] = '\0'; + string_delete(enc_string); + + CU_ASSERT_EQUAL(strcmp(enc_c_str, + "EAobFg4PHQYZBhEOChkYDg8fBhkGEw4KGRgOD" + "x0GGQYREAoZGA4PHQYbBBEODBsWDhEdBhkEER" + "AKGxYQDx0IGwQTDgoZGA4RHQgZBBMQChkWDg8" + "dCBkGEQ=="), 0); +} diff --git a/tests/ntru_file_cunit.c b/tests/ntru_file_cunit.c new file mode 100644 index 0000000..724da81 --- /dev/null +++ b/tests/ntru_file_cunit.c @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_file_cunit.c + * Test cases for file reading and writing. + * @brief tests for ntru_file.c + */ + +#include "ntru.h" + +#include +#include +#include +#include +#include +#include +#include + + +/** + * Read a valid text file and compare it with the correct expected + * string. + */ +void test_read_text_file1(void) +{ + string *tmp_str = read_file("test-file.txt"); + char actual_string[512]; + char *expected_string = "This test file is a test file.\n"; + memcpy(actual_string, tmp_str->ptr, tmp_str->len); + actual_string[tmp_str->len] = '\0'; + + string_delete(tmp_str); + + CU_ASSERT_PTR_NOT_NULL(actual_string); + CU_ASSERT_EQUAL((strcmp(actual_string, expected_string)), 0); +} + +/** + * Read a valid text file and compare it with an uncorrect expected + * string. + */ +void test_read_text_file2(void) +{ + string *tmp_str = read_file("test-file.txt"); + char actual_string[512]; + char *expected_string = "foo\n"; + memcpy(actual_string, tmp_str->ptr, tmp_str->len); + actual_string[tmp_str->len] = '\0'; + + string_delete(tmp_str); + + CU_ASSERT_PTR_NOT_NULL(actual_string); + CU_ASSERT_NOT_EQUAL((strcmp(actual_string, expected_string)), 0); +} + +/** + * Read an invalid text file. + */ +void test_read_text_file3(void) +{ + string *actual_string = read_file("asd"); + + CU_ASSERT_PTR_NULL(actual_string); +} + +/** + * Pass NULL to the read_file() function. + */ +void test_read_text_file4(void) +{ + string *actual_string = read_file(NULL); + + CU_ASSERT_PTR_NULL(actual_string); +} + +/** + * Test if writing the file works, without checking the content. + */ +void test_write_text_file1(void) +{ + string *tmp_str = read_file("test-file.txt"); + write_file(tmp_str, "test-file.out"); + string_delete(tmp_str); + + CU_ASSERT_EQUAL(0, access("test-file.out", F_OK)); +} + +/** + * Test if the written file has the expected content. + */ +void test_write_text_file2(void) +{ + string *tmp_str = read_file("test-file.txt"); + char actual_string[512]; + char *expected_string = "This test file is a test file.\n"; + + write_file(tmp_str, "test-file.out"); + string_delete(tmp_str); + tmp_str = read_file("test-file.out"); + + memcpy(actual_string, tmp_str->ptr, tmp_str->len); + actual_string[tmp_str->len] = '\0'; + + string_delete(tmp_str); + + CU_ASSERT_PTR_NOT_NULL(actual_string); + CU_ASSERT_EQUAL((strcmp(actual_string, expected_string)), 0); +} + +/** + * Check error handling when trying to write to directory. + */ +void test_write_text_file3(void) +{ + string str; + char *c_str = "This test file is a test file.\n"; + str.ptr = c_str; + str.len = 0; + str.len = strlen(c_str); + + CU_ASSERT_EQUAL(false, write_file(&str, ".")); +} + +/** + * Check error handling when passing NULL pointers. + */ +void test_write_text_file4(void) +{ + CU_ASSERT_EQUAL(false, write_file(NULL, "test-file.out")); +} + +/** + * Check error handling when passing NULL pointers. + */ +void test_write_text_file5(void) +{ + string str; + char *c_str = "This test file is a test file.\n"; + str.ptr = c_str; + str.len = strlen(c_str); + + CU_ASSERT_EQUAL(false, write_file(&str, NULL)); +} diff --git a/tests/ntru_keypair_cunit.c b/tests/ntru_keypair_cunit.c new file mode 100644 index 0000000..6e91f4d --- /dev/null +++ b/tests/ntru_keypair_cunit.c @@ -0,0 +1,341 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_keypair_cunit.c + * Test cases for generating private and public keys. + * @brief tests for ntru_keypair.c + */ + +#include "ntru.h" +#include "ntru_keypair.h" + +#include +#include +#include +#include +#include +#include +#include + + +/** + * Test keypair creation. + */ +void test_create_keypair1(void) +{ + keypair pair; + fmpz_poly_t f, g, pub, priv_inv; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + int pub_c[] = { 8, 25, 22, 20, 12, 24, 15, 19, 12, 19, 16 }; + int priv_inv_c[] = { 1, 2, 0, 2, 2, 1, 0, 2, 1, 2, 0 }; + ntru_params params; + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + poly_new(pub, pub_c, 11); + poly_new(priv_inv, priv_inv_c, 11); + + CU_ASSERT_EQUAL(true, ntru_create_keypair(&pair, f, g, ¶ms)); + CU_ASSERT_EQUAL(1, fmpz_poly_equal(pub, pair.pub)); + CU_ASSERT_EQUAL(1, fmpz_poly_equal(priv_inv, pair.priv_inv)); + CU_ASSERT_EQUAL(1, fmpz_poly_equal(f, pair.priv)); +} + +/** + * Test keypair creation error handling via non-invertible polynomial. + */ +void test_create_keypair2(void) +{ + keypair pair; + fmpz_poly_t f, g, priv_inv; + int f_c[] = { 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + int priv_inv_c[] = { 1, 2, 0, 2, 2, 1, 0, 2, 1, 2, 0 }; + ntru_params params; + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + poly_new(priv_inv, priv_inv_c, 11); + + CU_ASSERT_EQUAL(false, ntru_create_keypair(&pair, f, g, ¶ms)); +} + +/** + * Test exporting public key and reading the resulting file. + */ +void test_export_public_key1(void) +{ + keypair pair; + fmpz_poly_t f, g; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + string *pub_string; + char *expected_pub_c_str = "CBkWFAwYDxMMExA="; + char actual_pub_c_str[512] = "\0"; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_public_key("pub.key", pair.pub, ¶ms); + + if ((pub_string = read_file("pub.key"))) { + memcpy(actual_pub_c_str, pub_string->ptr, pub_string->len); + actual_pub_c_str[pub_string->len] = '\0'; + string_delete(pub_string); + } + + remove("pub.key"); + + CU_ASSERT_EQUAL(strcmp(expected_pub_c_str, actual_pub_c_str), 0); +} + +/** + * Test error handling of exporting public key. + */ +void test_export_public_key2(void) +{ + keypair pair; + fmpz_poly_t f, g; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + string *pub_string; + char *expected_pub_c_str = "CBkWFAwYDxMMExA="; + char actual_pub_c_str[512] = "\0"; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_public_key(".", pair.pub, ¶ms); + + if ((pub_string = read_file("pub.key"))) { + memcpy(actual_pub_c_str, pub_string->ptr, pub_string->len); + actual_pub_c_str[pub_string->len] = '\0'; + string_delete(pub_string); + } + + CU_ASSERT_NOT_EQUAL(strcmp(expected_pub_c_str, actual_pub_c_str), 0); +} + +/** + * Test exporting private key and reading the resulting file. + */ +void test_export_private_key1(void) +{ + keypair pair; + fmpz_poly_t f, g; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + string *pub_string; + char *expected_priv_c_str = "AgEBAgAAAAEAAQE="; + char actual_priv_c_str[512] = "\0"; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_priv_key("priv.key", pair.pub, ¶ms); + + if ((pub_string = read_file("priv.key"))) { + memcpy(actual_priv_c_str, pub_string->ptr, pub_string->len); + actual_priv_c_str[pub_string->len] = '\0'; + string_delete(pub_string); + } + + remove("priv.key"); + + CU_ASSERT_EQUAL(strcmp(expected_priv_c_str, actual_priv_c_str), 0); +} + +/** + * Test error handling of exporting private key. + */ +void test_export_private_key2(void) +{ + keypair pair; + fmpz_poly_t f, g; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + string *pub_string; + char *expected_priv_c_str = "AgEBAgAAAAEAAQE="; + char actual_priv_c_str[512] = "\0"; + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_priv_key(".", pair.pub, ¶ms); + + if ((pub_string = read_file("priv.key"))) { + memcpy(actual_priv_c_str, pub_string->ptr, pub_string->len); + actual_priv_c_str[pub_string->len] = '\0'; + string_delete(pub_string); + } + + CU_ASSERT_NOT_EQUAL(strcmp(expected_priv_c_str, actual_priv_c_str), 0); +} + +/** + * Test importing public key and reading. + */ +void test_import_public_key1(void) +{ + keypair pair; + fmpz_poly_t f, g, pub; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + + fmpz_poly_init(pub); + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_public_key("pub.key", pair.pub, ¶ms); + import_public_key(pub, "pub.key", ¶ms); + + remove("pub.key"); + + CU_ASSERT_EQUAL(1, fmpz_poly_equal(pub, pair.pub)); +} + +/** + * Test error handling of importing public key. + */ +void test_import_public_key2(void) +{ + keypair pair; + fmpz_poly_t f, g, pub; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + + fmpz_poly_init(pub); + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_public_key("pub.key", pair.pub, ¶ms); + import_public_key(pub, "foo", ¶ms); + + remove("pub.key"); + + CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(pub, pair.pub)); +} + +/** + * Test importing private key and reading. + */ +void test_import_private_key1(void) +{ + keypair pair; + fmpz_poly_t f, g, priv, priv_inv; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + + fmpz_poly_init(priv); + fmpz_poly_init(priv_inv); + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_priv_key("priv.key", pair.priv, ¶ms); + import_priv_key(priv, priv_inv, "priv.key", ¶ms); + + remove("priv.key"); + + CU_ASSERT_EQUAL(1, fmpz_poly_equal(priv, pair.priv)); +} + +/** + * Test error handling of importing private key. + */ +void test_import_private_key2(void) +{ + keypair pair; + fmpz_poly_t f, g, priv, priv_inv; + int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; + int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; + ntru_params params; + + fmpz_poly_init(priv); + fmpz_poly_init(priv_inv); + + params.N = 11; + params.p = 3; + params.q = 32; + + poly_new(f, f_c, 11); + poly_new(g, g_c, 11); + + ntru_create_keypair(&pair, f, g, ¶ms); + export_priv_key("priv.key", pair.priv, ¶ms); + import_priv_key(priv, priv_inv, ".", ¶ms); + + remove("priv.key"); + + CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(priv, pair.priv)); +} diff --git a/tests/ntru_poly_cunit.c b/tests/ntru_poly_cunit.c new file mode 100644 index 0000000..5313abc --- /dev/null +++ b/tests/ntru_poly_cunit.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_poly_cunit.c + * Test cases for polynomial creation. + * @brief tests for ntru_poly.c + */ + +#include "ntru.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * Test creating a new polynomial. + */ +void test_poly_new1(void) +{ + fmpz_poly_t new_poly; + int c[11] = { 1, 0, 1, 0, 1, 1, 0, 1, 0, -1 , 1}; + + poly_new(new_poly, c, 11); + + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 0), 1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 1), 0), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 2), 1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 3), 0), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 4), 1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 5), 1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 6), 0), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 7), 1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 8), 0), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 9), -1), 0); + CU_ASSERT_EQUAL(fmpz_cmp_si(fmpz_poly_get_coeff_ptr(new_poly, 10), 1), 0); +} + +/** + * Test creating a new polynomial, without coefficients. + */ +void test_poly_new2(void) +{ + fmpz_poly_t new_poly; + + poly_new(new_poly, NULL, 0); + + CU_ASSERT_PTR_NULL(fmpz_poly_get_coeff_ptr(new_poly, 0)); +} diff --git a/tests/test-file.txt b/tests/test-file.txt new file mode 100644 index 0000000..abc2699 --- /dev/null +++ b/tests/test-file.txt @@ -0,0 +1 @@ +This test file is a test file. diff --git a/tests/to-decrypt.txt b/tests/to-decrypt.txt new file mode 100644 index 0000000..c074baf --- /dev/null +++ b/tests/to-decrypt.txt @@ -0,0 +1 @@ +EAobFg4PHQYZBhEOChkYDg8fBhkGEw4KGRgODx0GGQYREAoZGA4PHQYbBBEODBsWDhEdBhkEERAKGxYQDx0IGwQTDgoZGA4RHQgZBBMQChkWDg8dCBkGEQ== diff --git a/tests/to-encrypt.txt b/tests/to-encrypt.txt new file mode 100644 index 0000000..6fd0d1f --- /dev/null +++ b/tests/to-encrypt.txt @@ -0,0 +1 @@ +BLAHFASEL