From e4b6ca84e5defabd196d12b99a614a89d890f2ee Mon Sep 17 00:00:00 2001 From: hasufell Date: Mon, 9 Jun 2014 22:11:07 +0200 Subject: [PATCH 01/18] ENC/DEC: implement compression via lz4 This speeds up encryption/decryption considerably, but also causes complete failure for any non-recoverable char in a message, since the decompression will fail then. Also remove the double base64 encode/decode. --- README.md | 1 + include/doxygen.dox | 1 + src/Makefile | 2 +- src/doxygen.dox | 1 + src/ntru_ascii_poly.c | 11 +-------- src/ntru_decrypt.c | 53 +++++++++++++++++++++++++++++++++++++++++-- src/ntru_encrypt.c | 46 ++++++++++++++++++++++++++++++++++++- src/ntru_poly_ascii.c | 17 ++++---------- 8 files changed, 106 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c3f2457..f99231c 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ implementation with the primary goal of learning. * FLINT (compiled with gmp and mpfr) * glib-2.0 +* lz4 (https://code.google.com/p/lz4) * pkgconfig (for the build only) ### Compiling the library diff --git a/include/doxygen.dox b/include/doxygen.dox index 0483ef7..3ec468c 100644 --- a/include/doxygen.dox +++ b/include/doxygen.dox @@ -31,6 +31,7 @@ Further work is based on FLINT-2.4.3 or later (compiled with gmp and mpfr) \* glib-2.0 + \* lz4 \* pkg-config (for the build only) \section install_sec Installation diff --git a/src/Makefile b/src/Makefile index 127dbd1..d30ee32 100644 --- a/src/Makefile +++ b/src/Makefile @@ -34,7 +34,7 @@ PQC_HEADERS = \ # libs -LIBS += -L. -lgmp -lmpfr -lflint $(shell $(PKG_CONFIG) --libs glib-2.0) -lm +LIBS += -L. -llz4 -lgmp -lmpfr -lflint $(shell $(PKG_CONFIG) --libs glib-2.0) -lm # includes INCS = -I. -I/usr/include/flint $(shell $(PKG_CONFIG) --cflags glib-2.0) diff --git a/src/doxygen.dox b/src/doxygen.dox index 986e439..9173bf9 100644 --- a/src/doxygen.dox +++ b/src/doxygen.dox @@ -21,6 +21,7 @@ Further work is based on FLINT-2.4.3 or later (compiled with gmp and mpfr) \* glib-2.0 + \* lz4 \* pkg-config (for the build only) \section install_sec Installation diff --git a/src/ntru_ascii_poly.c b/src/ntru_ascii_poly.c index 2ac66a6..acb3f1c 100644 --- a/src/ntru_ascii_poly.c +++ b/src/ntru_ascii_poly.c @@ -146,8 +146,7 @@ base64_to_poly_arr(const string *to_poly, const ntru_params *params) uint32_t i = 0, polyc = 0; gsize out_len; - guchar *base64_decoded = NULL, - *base_tmp = NULL; + guchar *base64_decoded = NULL; string *new_string = ntru_malloc(sizeof(*new_string)); fmpz_poly_t **poly_array; char *tmp = ntru_malloc(sizeof(char) * (to_poly->len + 1)); @@ -156,13 +155,6 @@ base64_to_poly_arr(const string *to_poly, const ntru_params *params) memcpy(tmp, to_poly->ptr, to_poly->len); tmp[to_poly->len] = '\0'; - base_tmp = g_base64_decode((const gchar *)tmp, &out_len); - - /* g_base64_decode() needs it null-terminated */ - REALLOC(tmp, sizeof(char) * (out_len + 1)); - memcpy(tmp, base_tmp, out_len); - tmp[out_len] = '\0'; - base64_decoded = g_base64_decode((const gchar *)tmp, &out_len); new_string->ptr = (char *)base64_decoded; @@ -200,7 +192,6 @@ base64_to_poly_arr(const string *to_poly, const ntru_params *params) poly_array[polyc] = NULL; string_delete(new_string); - free(base_tmp); free(tmp); return poly_array; diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index aedd3c5..ced90d8 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -28,11 +28,13 @@ #include "ntru_ascii_poly.h" #include "ntru_decrypt.h" +#include "ntru_mem.h" #include "ntru_params.h" #include "ntru_poly.h" #include "ntru_poly_ascii.h" #include "ntru_string.h" +#include #include #include @@ -40,6 +42,49 @@ #include +/** + * Decompress a string and return it, newly allocated. + * + * @param compr_str the compressed string to decompress + * @return the decompressed string, newly allocated + */ +static string * +get_decompressed_str(const string *compr_str); + + +/*------------------------------------------------------------------------*/ + +static string * +get_decompressed_str(const string *compr_str) +{ + int out_len = 0; + const uint32_t max_lz4_ratio = 3; + uint32_t max_out_len = 0; + string *decompressed_msg = NULL; + + if (!compr_str) + NTRU_ABORT_DEBUG("Unexpected NULL parameters"); + + max_out_len = compr_str->len * max_lz4_ratio; + decompressed_msg = ntru_malloc(sizeof(string)); + decompressed_msg->ptr = ntru_malloc( + /* this is more than needed, but safe */ + sizeof(char) * max_out_len); + + out_len = LZ4_decompress_safe( + (const char *)compr_str->ptr, + decompressed_msg->ptr, + compr_str->len, + max_out_len); + + if (out_len > 0) + decompressed_msg->len = out_len; + else + NTRU_ABORT_DEBUG("Failed decompressing the message"); + + return decompressed_msg; +} + /*------------------------------------------------------------------------*/ void @@ -98,6 +143,7 @@ ntru_decrypt_string( uint32_t i = 0; string *decr_msg; fmpz_poly_t **poly_array; + string *decompressed_msg = NULL; if (!encr_msg || !encr_msg->len) NTRU_ABORT_DEBUG("Unexpected NULL parameters"); @@ -116,9 +162,12 @@ ntru_decrypt_string( decr_msg = bin_poly_arr_to_ascii((const fmpz_poly_t **)poly_array, i, params); - poly_delete_array(poly_array); + decompressed_msg = get_decompressed_str(decr_msg); - return decr_msg; + poly_delete_array(poly_array); + string_delete(decr_msg); + + return decompressed_msg; } /*------------------------------------------------------------------------*/ diff --git a/src/ntru_encrypt.c b/src/ntru_encrypt.c index ca7f398..172c366 100644 --- a/src/ntru_encrypt.c +++ b/src/ntru_encrypt.c @@ -34,12 +34,52 @@ #include "ntru_poly_ascii.h" #include "ntru_string.h" +#include #include #include #include +/** + * Compress a string and return it, newly allocated. + * + * @param str the string to compress + * @return the compressed string, newly allocated + */ +static string * +get_compressed_str(const string *str); + + +/*------------------------------------------------------------------------*/ + +static string * +get_compressed_str(const string *str) +{ + int out_len = 0; + string *compressed_str; + uint32_t max_output_size; + + if (!str) + NTRU_ABORT_DEBUG("Unexpected NULL parameters"); + + max_output_size = str->len; + compressed_str = ntru_malloc(sizeof(string)); + compressed_str->ptr = ntru_malloc( + sizeof(char) * max_output_size); + out_len = LZ4_compress( + (const char*) str->ptr, + compressed_str->ptr, + str->len); + + if (out_len > 0) + compressed_str->len = out_len; + else + NTRU_ABORT_DEBUG("Failed compressing the message"); + + return compressed_str; +} + /*------------------------------------------------------------------------*/ void @@ -80,11 +120,14 @@ ntru_encrypt_string( uint32_t i = 0; string *enc_msg; fmpz_poly_t **poly_array; + string *compressed_msg; if (!msg || !msg->len) NTRU_ABORT_DEBUG("Unexpected NULL parameters"); - poly_array = ascii_to_bin_poly_arr(msg, params); + compressed_msg = get_compressed_str(msg); + + poly_array = ascii_to_bin_poly_arr(compressed_msg, params); while (*poly_array[i]) { ntru_encrypt_poly(*poly_array[i], @@ -99,6 +142,7 @@ ntru_encrypt_string( i, params); poly_delete_array(poly_array); + string_delete(compressed_msg); return enc_msg; } diff --git a/src/ntru_poly_ascii.c b/src/ntru_poly_ascii.c index 6300967..6caa8ea 100644 --- a/src/ntru_poly_ascii.c +++ b/src/ntru_poly_ascii.c @@ -239,21 +239,17 @@ poly_to_base64(const fmpz_poly_t poly, { string *result_string = ntru_malloc(sizeof(*result_string)); string *string_rep = NULL; - gchar *base64_string = NULL, - *tmp = NULL; + gchar *base64_string = NULL; string_rep = poly_to_ascii(poly, params); - tmp = g_base64_encode((const guchar *)string_rep->ptr, + base64_string = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len); - base64_string = g_base64_encode((const guchar *)tmp, - strlen(tmp)); result_string->ptr = base64_string; result_string->len = strlen(base64_string); string_delete(string_rep); - free(tmp); return result_string; } @@ -268,20 +264,17 @@ poly_arr_to_base64(const fmpz_poly_t **poly_array, string *string_rep; string *result_string = ntru_malloc(sizeof(*result_string)); - gchar *base64_string = NULL, - *tmp = NULL; + gchar *base64_string = NULL; string_rep = poly_arr_to_ascii(poly_array, poly_c, params); - tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len); - base64_string = g_base64_encode((const guchar *)tmp, - strlen(tmp)); + base64_string = g_base64_encode((const guchar *)string_rep->ptr, + string_rep->len); result_string->ptr = base64_string; result_string->len = strlen(base64_string); string_delete(string_rep); - free(tmp); return result_string; } From e29b54a8765178dad29770a9aa7bfbcbe9efb2fa Mon Sep 17 00:00:00 2001 From: hasufell Date: Sun, 22 Jun 2014 23:25:40 +0200 Subject: [PATCH 02/18] TRAVIS: fix build with lz4 --- .travis.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4f060ca..4eef998 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ env: global: secure: "BB9eiRNXC1PfNRLEifc0yQQZnM25zqaWNGVWN+atzT+NkxhVKyVr8+DoyPYaa8tOKybuSZhVkKnIiCd8iGMe3v+WyuHKFuhdiwdnEQhxbBlUCb9dDLEexI+J8QiYwC8AW/t6H9LWVwSka0RI3GJujJ8HKIBzW45I/j+1NlUte8U=" before_script: - - sudo apt-get install -qq libgmp-dev libmpfr-dev doxygen graphviz + - sudo apt-get install -qq libgmp-dev libmpfr-dev doxygen graphviz cmake - sudo apt-get remove doxygen - wget http://www.flintlib.org/flint-2.4.3.tar.gz - tar -xzf flint-2.4.3.tar.gz @@ -20,6 +20,13 @@ before_script: - make - sudo make install - cd .. + - wget http://dev.gentoo.org/~ryao/dist/lz4-0_p106.tar.xz + - tar -xJf lz4-0_p106.tar.xz + - cd lz4-0_p106/cmake + - cmake -DBUILD_LIBS=ON -DBUILD_SHARED_LIBS=ON . + - make + - sudo make install + - cd ../.. script: - make CC=clang - make doc From 9a57b5e7c18ad430c86510d6eb6a909431a96016 Mon Sep 17 00:00:00 2001 From: hasufell Date: Mon, 23 Jun 2014 17:42:25 +0200 Subject: [PATCH 03/18] DOC: random doxygen improvements --- include/ntru.h | 2 -- include/ntru_keypair.h | 12 ++++++++---- src/ntru_decrypt.h | 2 +- src/ntru_encrypt.h | 2 +- src/ntru_keypair.h | 11 +++++++---- src/ntru_poly.c | 2 +- src/ntru_poly.h | 8 +++----- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/ntru.h b/include/ntru.h index f15210d..c7deb77 100644 --- a/include/ntru.h +++ b/include/ntru.h @@ -108,8 +108,6 @@ string_delete(string *del_string); * fill with coefficients [out] * @param c array of polynomial coefficients, can be NULL * @param len size of the coefficient array, can be 0 - * @return newly allocated polynomial pointer, must be freed - * with fmpz_poly_clear() */ void poly_new(fmpz_poly_t new_poly, diff --git a/include/ntru_keypair.h b/include/ntru_keypair.h index 1dd9c96..ff54d1d 100644 --- a/include/ntru_keypair.h +++ b/include/ntru_keypair.h @@ -70,7 +70,9 @@ struct keypair { * consisting of public and private * components. * - * @param pair store private and public components here [out] + * @param pair store private and public components here (the + * polynomials inside the struct will be automatically + * initialized) [out] * @param f a random polynomial * @param g a random polynomial * @param params the NTRU context @@ -111,7 +113,7 @@ export_priv_key(char const * const filename, /** * Import the public key from a file. * - * @param pub where to save the public key [out] + * @param pub where to save the public key, must be initialized [out] * @param filename the file to get the public key from * @param params the NTRU context */ @@ -120,12 +122,14 @@ import_public_key(fmpz_poly_t pub, char const * const filename, const ntru_params *params); + /** * Import the private key from a file and store him * along with his inverse. * - * @param priv where to save the private key [out] - * @param priv_inv where to save the inverse of the private key [out] + * @param priv where to save the private key, must be initialized [out] + * @param priv_inv where to save the inverse of the private key, + * must be initialized [out] * @param filename the file to get the private key from * @param params the NTRU context */ diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 4e7ade4..d00b8d3 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -40,7 +40,7 @@ * Decryption of the given Polynom with the private key, its inverse * and the fitting ntru_params * - * @param out_tern the resulting ternary polynom [out] + * @param out_tern the resulting ternary polynom, must be initialized [out] * @param encr_msg encrypted polynomial with maximum length of N from * the given context * @param priv_key the polynomial containing the private key to decrypt diff --git a/src/ntru_encrypt.h b/src/ntru_encrypt.h index a02c192..6a82556 100644 --- a/src/ntru_encrypt.h +++ b/src/ntru_encrypt.h @@ -52,7 +52,7 @@ * q = large mod * * @param out the output poly which is in the range {0, q-1} - * (not ternary!) [out] + * (not ternary!), must be initialized [out] * @param msg_tern the message to encrypt, in ternary format * @param pub_key the public key * @param rnd the random poly (should have relatively small diff --git a/src/ntru_keypair.h b/src/ntru_keypair.h index 8bcbe32..269869f 100644 --- a/src/ntru_keypair.h +++ b/src/ntru_keypair.h @@ -67,7 +67,9 @@ struct keypair { * consisting of public and private * components. * - * @param pair store private and public components here [out] + * @param pair store private and public components here (the + * polynomials inside the struct will be automatically + * initialized) [out] * @param f a random polynomial * @param g a random polynomial * @param params the NTRU context @@ -108,7 +110,7 @@ export_priv_key(char const * const filename, /** * Import the public key from a file. * - * @param pub where to save the public key [out] + * @param pub where to save the public key, must be initialized [out] * @param filename the file to get the public key from * @param params the NTRU context */ @@ -121,8 +123,9 @@ import_public_key(fmpz_poly_t pub, * Import the private key from a file and store him * along with his inverse. * - * @param priv where to save the private key [out] - * @param priv_inv where to save the inverse of the private key [out] + * @param priv where to save the private key, must be initialized [out] + * @param priv_inv where to save the inverse of the private key, + * must be initialized [out] * @param filename the file to get the private key from * @param params the NTRU context */ diff --git a/src/ntru_poly.c b/src/ntru_poly.c index ac3b60d..d6aa574 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -47,7 +47,7 @@ * Find the inverse polynomial modulo a power of 2, * which is q. * - * @param Fq polynomial [out] + * @param Fq polynomial, must be initialized [out] * @param a polynomial to invert * @param params NTRU parameters */ diff --git a/src/ntru_poly.h b/src/ntru_poly.h index c324952..f40184a 100644 --- a/src/ntru_poly.h +++ b/src/ntru_poly.h @@ -60,8 +60,6 @@ fmpz_cmp_si_n(const fmpz_t f, slong g); * fill with coefficients [out] * @param c array of polynomial coefficients, can be NULL * @param len size of the coefficient array, can be 0 - * @return newly allocated polynomial pointer, must be freed - * with fmpz_poly_clear() */ void poly_new(fmpz_poly_t new_poly, @@ -169,7 +167,7 @@ fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h); * Starmultiplication, as follows: * c = a * b mod (x^N − 1) * - * @param c polynom [out] + * @param c polynom, must be initialized [out] * @param a polynom to multiply (can be the same as c) * @param b polynom to multiply * @param params NTRU parameters @@ -189,7 +187,7 @@ poly_starmultiply(fmpz_poly_t c, * See NTRU Cryptosystems Tech Report #014 "Almost Inverses * and Fast NTRU Key Creation." * - * @param Fq polynomial [out] + * @param Fq polynomial, must be initialized [out] * @param a polynomial to invert (is allowed to be the same as param Fq) * @param params NTRU parameters * @return true if invertible, false if not @@ -204,7 +202,7 @@ poly_inverse_poly_q(fmpz_poly_t Fq, * See NTRU Cryptosystems Tech Report #014 "Almost Inverses * and Fast NTRU Key Creation." * - * @param Fp polynomial [out] + * @param Fp polynomial, must be initialized [out] * @param a polynomial to invert * @param params NTRU parameters * @return true if invertible, false if not From 16677d84573ddc4b5f7b3c149fd419e1f7d28aa4 Mon Sep 17 00:00:00 2001 From: hasufell Date: Mon, 23 Jun 2014 18:17:09 +0200 Subject: [PATCH 04/18] DOC: random doxygen improvements --- src/ntru_rnd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ntru_rnd.h b/src/ntru_rnd.h index 4f9864d..01e51ec 100644 --- a/src/ntru_rnd.h +++ b/src/ntru_rnd.h @@ -55,7 +55,7 @@ get_urnd_int(void); * Get a random ternary polynomial with specified numbers * of 1 coefficients and -1 coefficients. * - * @param poly the resulting random polynomial [out] + * @param poly the resulting random polynomial, must be initialized [out] * @param params the NTRU context * @param num_ones the number of 1 coefficients * @param num_neg_ones the number of -1 coefficients From cd347938f495afae9f50f11f41759b4e1dbce3b7 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 16:04:46 +0200 Subject: [PATCH 05/18] PUB-API: fix write_file() declaration --- include/ntru.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/ntru.h b/include/ntru.h index c7deb77..bc567e3 100644 --- a/include/ntru.h +++ b/include/ntru.h @@ -33,6 +33,7 @@ #include #include +#include #include @@ -178,8 +179,9 @@ read_file(char const * const filename); * * @param wstring the string to write to the file * @param filename the name of the file to write to + * @return true for success or false for failure if fopen or fclose failed */ -void +bool write_file(string const *wstring, char const * const filename); From 1ed32d01932ee6e0dffaf571dfa52a03eb9742d9 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 16:05:37 +0200 Subject: [PATCH 06/18] FILE: improve error handling --- src/ntru_file.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ntru_file.c b/src/ntru_file.c index e2c26fd..a491659 100644 --- a/src/ntru_file.c +++ b/src/ntru_file.c @@ -108,6 +108,9 @@ write_file(string const *wstring, char const * const filename) { FILE *fp; + if (!wstring || !filename) + return false; + fp = fopen(filename, "w"); if (!fp) { From 772877e9920cef0c4781b62f4138af4770073c07 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 16:05:50 +0200 Subject: [PATCH 07/18] POLY: improve error handling --- src/ntru_poly.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ntru_poly.c b/src/ntru_poly.c index d6aa574..1d8a335 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -111,6 +111,9 @@ poly_new(fmpz_poly_t new_poly, int const * const c, const size_t len) { + if (!new_poly) + NTRU_ABORT_DEBUG("Unexpected NULL parameter in"); + fmpz_poly_init(new_poly); for (uint32_t i = 0; i < len; i++) From 470a10d36be332bb0b16753f9171f67838ac9ca4 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 16:06:23 +0200 Subject: [PATCH 08/18] BUILD: rm obsolete references --- src/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Makefile b/src/Makefile index d30ee32..eeb0d2a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -50,13 +50,13 @@ all: libpqc.a libpqc.so.$(LIBVER) libpqc.a: $(PQC_OBJS) $(PQC_HEADERS) $(AR) cru libpqc.a $(PQC_OBJS) -libpqc.so.$(LIBVER): libpqc.a $(PQC_HEADERS) $(LIBFLINT) +libpqc.so.$(LIBVER): libpqc.a $(PQC_HEADERS) $(CC) -shared $(CFLAGS) -Wl,-soname,libpqc$(SOVER) -o $@ $(LDFLAGS) \ - libpqc.a $(LIBFLINT) $(LIBS) + libpqc.a $(LIBS) -main: main.o libpqc.a $(LIBFLINT) +main: main.o libpqc.a $(CC) $(CFLAGS) -o $@ $(LDFLAGS) \ - main.o libpqc.a $(LIBFLINT) $(LIBS) + main.o libpqc.a $(LIBS) install: $(INSTALL_DIR) "$(DESTDIR)$(INSTALL_LIBDIR)" From c9fcb7aa9981d1fc9a728ace2cba936b5bb07948 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 22:39:37 +0200 Subject: [PATCH 09/18] POLY: name labels consistently --- src/ntru_poly.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ntru_poly.c b/src/ntru_poly.c index 1d8a335..03b0371 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -349,7 +349,7 @@ poly_inverse_poly_q(fmpz_poly_t Fq, k++; if (fmpz_poly_degree(f) == -1) - goto _cleanup; + goto cleanup; } if (fmpz_poly_degree(f) == 0) @@ -371,7 +371,7 @@ poly_inverse_poly_q(fmpz_poly_t Fq, b_last = fmpz_poly_get_coeff_ptr(b, params->N); if (fmpz_cmp_si_n(b_last, 0)) - goto _cleanup; + goto cleanup; /* Fq(x) = x^(N-k) * b(x) */ for (int i = params->N - 1; i >= 0; i--) { @@ -396,7 +396,7 @@ poly_inverse_poly_q(fmpz_poly_t Fq, else fmpz_poly_zero(Fq); -_cleanup: +cleanup: fmpz_poly_clear(a_tmp); fmpz_poly_clear(b); fmpz_poly_clear(c); From cef08a4ae04639ee12d63131e86469c751486407 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 22:40:08 +0200 Subject: [PATCH 10/18] POLY: fix infinite loop for non-invertible polynomials --- src/ntru_poly.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ntru_poly.c b/src/ntru_poly.c index 03b0371..a5386ce 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -352,6 +352,9 @@ poly_inverse_poly_q(fmpz_poly_t Fq, goto cleanup; } + if (fmpz_poly_is_zero(g) == 1) + goto cleanup; + if (fmpz_poly_degree(f) == 0) break; @@ -464,6 +467,9 @@ poly_inverse_poly_p(fmpz_poly_t Fp, goto cleanup; } + if (fmpz_poly_is_zero(g) == 1) + goto cleanup; + if (fmpz_poly_degree(f) == 0) break; From 05582c4bedc525f41c59f74fd9c857ed48498356 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 22:40:49 +0200 Subject: [PATCH 11/18] KEYGEN: improve error handling, be less fault tolerant --- src/ntru_keypair.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ntru_keypair.c b/src/ntru_keypair.c index 2fd22a9..8382117 100644 --- a/src/ntru_keypair.c +++ b/src/ntru_keypair.c @@ -55,8 +55,8 @@ ntru_create_keypair( Fp, pub; - if (!f || !g || !params) - goto _return; + if (!pair || !f || !g || !params) + NTRU_ABORT_DEBUG("Unexpected NULL parameters in"); fmpz_poly_init(Fq); fmpz_poly_init(Fp); @@ -86,7 +86,7 @@ _cleanup: fmpz_poly_clear(Fq); fmpz_poly_clear(Fp); fmpz_poly_clear(pub); -_return: + return retval; } From 85f671a46f38e92fcc6cebc50fd5a32b1afa2b1d Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 27 Jun 2014 22:41:11 +0200 Subject: [PATCH 12/18] PUB-API: fix syntax errors --- include/ntru_keypair.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ntru_keypair.h b/include/ntru_keypair.h index ff54d1d..bf33390 100644 --- a/include/ntru_keypair.h +++ b/include/ntru_keypair.h @@ -96,7 +96,7 @@ ntru_create_keypair( void export_public_key(char const * const filename, const fmpz_poly_t pub, - const ntru_params *params) + const ntru_params *params); /** * Export the private key to a file. @@ -108,7 +108,7 @@ export_public_key(char const * const filename, void export_priv_key(char const * const filename, const fmpz_poly_t priv, - const ntru_params *params) + const ntru_params *params); /** * Import the public key from a file. From 5e849d3d5056274f81923b1a2fbe38419a02e7db Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:44:33 +0200 Subject: [PATCH 13/18] KEYGEN: improve error handling Check for NULL-pointer arguments and add bool return value which indicates whether any of the file operations failed. --- include/ntru_keypair.h | 12 +++++++---- src/ntru_keypair.c | 49 +++++++++++++++++++++++++++++++----------- src/ntru_keypair.h | 12 +++++++---- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/include/ntru_keypair.h b/include/ntru_keypair.h index bf33390..c6cab36 100644 --- a/include/ntru_keypair.h +++ b/include/ntru_keypair.h @@ -92,8 +92,9 @@ ntru_create_keypair( * @param filename the file to save the public key into * @param pub the public key * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool export_public_key(char const * const filename, const fmpz_poly_t pub, const ntru_params *params); @@ -104,8 +105,9 @@ export_public_key(char const * const filename, * @param filename the file to save the private key into * @param priv the private key * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool export_priv_key(char const * const filename, const fmpz_poly_t priv, const ntru_params *params); @@ -116,8 +118,9 @@ export_priv_key(char const * const filename, * @param pub where to save the public key, must be initialized [out] * @param filename the file to get the public key from * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool import_public_key(fmpz_poly_t pub, char const * const filename, const ntru_params *params); @@ -132,8 +135,9 @@ import_public_key(fmpz_poly_t pub, * must be initialized [out] * @param filename the file to get the private key from * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, char const * const filename, diff --git a/src/ntru_keypair.c b/src/ntru_keypair.c index 8382117..e301863 100644 --- a/src/ntru_keypair.c +++ b/src/ntru_keypair.c @@ -92,43 +92,55 @@ _cleanup: /*------------------------------------------------------------------------*/ -void +bool export_public_key(char const * const filename, const fmpz_poly_t pub, const ntru_params *params) { string *pub_string; + bool retval = false; + + if (!filename || !pub || !params) + NTRU_ABORT_DEBUG("Unexpected NULL parameters in"); pub_string = poly_to_base64(pub, params); - write_file(pub_string, filename); + retval = write_file(pub_string, filename); string_delete(pub_string); + + return retval; } /*------------------------------------------------------------------------*/ -void +bool export_priv_key(char const * const filename, const fmpz_poly_t priv, const ntru_params *params) { string *priv_string; fmpz_poly_t priv_u; + bool retval = false; + + if (!filename || !priv || !params) + NTRU_ABORT_DEBUG("Unexpected NULL parameters in"); fmpz_poly_init(priv_u); fmpz_poly_set(priv_u, priv); fmpz_poly_mod_unsigned(priv_u, params->p); priv_string = poly_to_base64(priv_u, params); - write_file(priv_string, filename); + retval = write_file(priv_string, filename); fmpz_poly_clear(priv_u); string_delete(priv_string); + + return retval; } /*------------------------------------------------------------------------*/ -void +bool import_public_key(fmpz_poly_t pub, char const * const filename, const ntru_params *params) @@ -136,7 +148,12 @@ import_public_key(fmpz_poly_t pub, string *pub_string; fmpz_poly_t **imported; - pub_string = read_file(filename); + if (!pub || !filename || !params) + NTRU_ABORT_DEBUG("Unexpected NULL parameters in"); + + if (!(pub_string = read_file(filename))) + return false; + imported = base64_to_poly_arr(pub_string, params); /* if the array exceeds one element, then something @@ -149,25 +166,31 @@ import_public_key(fmpz_poly_t pub, string_delete(pub_string); poly_delete_array(imported); free(imported); + + return true; } /*------------------------------------------------------------------------*/ -void +bool import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, char const * const filename, const ntru_params *params) { - string *pub_string; + string *priv_string; fmpz_poly_t **imported, Fp; + if (!priv || !priv_inv || !filename || !params) + NTRU_ABORT_DEBUG("Unexpected NULL parameters in"); + + if (!(priv_string = read_file(filename))) + return false; + fmpz_poly_init(Fp); - pub_string = read_file(filename); - - imported = base64_to_poly_arr(pub_string, params); + imported = base64_to_poly_arr(priv_string, params); fmpz_poly_mod(**imported, params->p); /* if the array exceeds one element, then something @@ -186,9 +209,11 @@ import_priv_key(fmpz_poly_t priv, fmpz_poly_clear(Fp); cleanup: - string_delete(pub_string); + string_delete(priv_string); poly_delete_array(imported); free(imported); + + return true; } /*------------------------------------------------------------------------*/ diff --git a/src/ntru_keypair.h b/src/ntru_keypair.h index 269869f..7e02f61 100644 --- a/src/ntru_keypair.h +++ b/src/ntru_keypair.h @@ -89,8 +89,9 @@ ntru_create_keypair( * @param filename the file to save the public key into * @param pub the public key * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool export_public_key(char const * const filename, const fmpz_poly_t pub, const ntru_params *params); @@ -101,8 +102,9 @@ export_public_key(char const * const filename, * @param filename the file to save the private key into * @param priv the private key * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool export_priv_key(char const * const filename, const fmpz_poly_t priv, const ntru_params *params); @@ -113,8 +115,9 @@ export_priv_key(char const * const filename, * @param pub where to save the public key, must be initialized [out] * @param filename the file to get the public key from * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool import_public_key(fmpz_poly_t pub, char const * const filename, const ntru_params *params); @@ -128,8 +131,9 @@ import_public_key(fmpz_poly_t pub, * must be initialized [out] * @param filename the file to get the private key from * @param params the NTRU context + * @return true for success, false if any of the file operations failed */ -void +bool import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, char const * const filename, From 03f3901b81cb72d08017fa01c873efa4e092395c Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:47:47 +0200 Subject: [PATCH 14/18] ENC: fix possible memory error LZ4_compress might add a null-byte, so we have to account for that. --- src/ntru_encrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ntru_encrypt.c b/src/ntru_encrypt.c index 172c366..ab2fe06 100644 --- a/src/ntru_encrypt.c +++ b/src/ntru_encrypt.c @@ -63,7 +63,7 @@ get_compressed_str(const string *str) if (!str) NTRU_ABORT_DEBUG("Unexpected NULL parameters"); - max_output_size = str->len; + max_output_size = str->len + 1; compressed_str = ntru_malloc(sizeof(string)); compressed_str->ptr = ntru_malloc( sizeof(char) * max_output_size); From 913a58527901d93d9f0d05c2180e10f24e703033 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:48:12 +0200 Subject: [PATCH 15/18] POLY: fix segfault for non-invertible polynomials --- src/ntru_poly.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ntru_poly.c b/src/ntru_poly.c index a5386ce..22f0759 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -329,7 +329,8 @@ poly_inverse_poly_q(fmpz_poly_t Fq, fmpz_poly_zero(Fq); while (1) { - while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { + while (fmpz_poly_get_coeff_ptr(f, 0) && + fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); @@ -444,7 +445,8 @@ poly_inverse_poly_p(fmpz_poly_t Fp, fmpz_poly_zero(Fp); while (1) { - while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { + while (fmpz_poly_get_coeff_ptr(f, 0) && + fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); From fd978fcfec5efc6f4dcf32605a900835f4345217 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:48:54 +0200 Subject: [PATCH 16/18] FILE: improve error handling This also fixes various valgrind warnings. We explicitly check if we operate on a regular file instead of relying on error handling of fopen/open. --- src/ntru_file.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ntru_file.c b/src/ntru_file.c index a491659..d0fcb47 100644 --- a/src/ntru_file.c +++ b/src/ntru_file.c @@ -56,8 +56,19 @@ read_file(char const * const filename) ssize_t n; size_t file_length = 0; string *result_string; + struct stat s; + + if (!filename) + return NULL; fd = open(filename, O_RDONLY); + + /* check if this is a real file */ + if (fstat(fd, &s) == -1) + return NULL; + if (!S_ISREG(s.st_mode)) + return NULL; + file_length = lseek(fd, 0, SEEK_END) + 1; lseek(fd, 0, SEEK_SET); @@ -107,10 +118,17 @@ bool write_file(string const *wstring, char const * const filename) { FILE *fp; + struct stat s; if (!wstring || !filename) return false; + /* if "filename" already exists, we need to make sure + * it's a regular file */ + if (stat(filename, &s) == 0 && + !S_ISREG(s.st_mode)) + return false; + fp = fopen(filename, "w"); if (!fp) { From 5158736fab3dba6f3abd99ee015720e81c5087dc Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:54:55 +0200 Subject: [PATCH 17/18] TESTS: initial test suite implementation --- .gitignore | 2 + .travis.yml | 1 + Makefile | 7 + tests/Makefile | 47 +++++ tests/ntru_cunit.c | 206 ++++++++++++++++++++++ tests/ntru_cunit.h | 68 ++++++++ tests/ntru_decrypt_cunit.c | 76 +++++++++ tests/ntru_encrypt_cunit.c | 80 +++++++++ tests/ntru_file_cunit.c | 162 ++++++++++++++++++ tests/ntru_keypair_cunit.c | 341 +++++++++++++++++++++++++++++++++++++ tests/ntru_poly_cunit.c | 75 ++++++++ tests/test-file.txt | 1 + tests/to-decrypt.txt | 1 + tests/to-encrypt.txt | 1 + 14 files changed, 1068 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/ntru_cunit.c create mode 100644 tests/ntru_cunit.h create mode 100644 tests/ntru_decrypt_cunit.c create mode 100644 tests/ntru_encrypt_cunit.c create mode 100644 tests/ntru_file_cunit.c create mode 100644 tests/ntru_keypair_cunit.c create mode 100644 tests/ntru_poly_cunit.c create mode 100644 tests/test-file.txt create mode 100644 tests/to-decrypt.txt create mode 100644 tests/to-encrypt.txt 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 From f69a66e422cb6698e13718a4c95db8ca847d99f2 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 28 Jun 2014 00:57:22 +0200 Subject: [PATCH 18/18] TRAVIS: install missing cunit package --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4f89f20..9120f06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ env: global: secure: "BB9eiRNXC1PfNRLEifc0yQQZnM25zqaWNGVWN+atzT+NkxhVKyVr8+DoyPYaa8tOKybuSZhVkKnIiCd8iGMe3v+WyuHKFuhdiwdnEQhxbBlUCb9dDLEexI+J8QiYwC8AW/t6H9LWVwSka0RI3GJujJ8HKIBzW45I/j+1NlUte8U=" before_script: - - sudo apt-get install -qq libgmp-dev libmpfr-dev doxygen graphviz cmake + - sudo apt-get install -qq libgmp-dev libmpfr-dev doxygen graphviz cmake libcunit1 libcunit1-dev - sudo apt-get remove doxygen - wget http://www.flintlib.org/flint-2.4.3.tar.gz - tar -xzf flint-2.4.3.tar.gz