Merge branch 'compression'

This commit is contained in:
hasufell 2014-06-28 01:06:58 +02:00
commit 62f5b93039
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
28 changed files with 1284 additions and 62 deletions

2
.gitignore vendored
View File

@ -30,6 +30,8 @@ install/
*.pdf
*.dec
*.enc
tests/test-file.out
tests/ntru_cunit
# tmp files

View File

@ -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 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
@ -20,8 +20,16 @@ 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 CC=clang check
- make doc
after_script:
- ./update-gh-pages.sh

View File

@ -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

View File

@ -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

View File

@ -31,6 +31,7 @@ Further work is based on <a href="http://www.math.uni-hamburg.de/home/kuehn/mold
This library was written for Linux systems. Support for windows will not be added.
\* <a href="http://www.flintlib.org">FLINT-2.4.3 or later</a> (compiled with gmp and mpfr)
\* <a href="https://developer.gnome.org/glib/stable/">glib-2.0</a>
\* <a href="https://code.google.com/p/lz4">lz4</a>
\* <a href="http://www.freedesktop.org/wiki/Software/pkg-config/">pkg-config</a> (for the build only)
\section install_sec Installation

View File

@ -33,6 +33,7 @@
#include <fmpz_poly.h>
#include <fmpz.h>
#include <stdbool.h>
#include <stdint.h>
@ -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);

View File

@ -92,11 +92,12 @@ 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)
const ntru_params *params);
/**
* Export the private key to a file.
@ -104,11 +105,12 @@ 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)
const ntru_params *params);
/**
* Import the public key from a file.
@ -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,

View File

@ -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)
@ -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)"

View File

@ -21,6 +21,7 @@ Further work is based on <a href="http://www.math.uni-hamburg.de/home/kuehn/mold
This library was written for Linux systems. Support for windows will not be added.
\* <a href="http://www.flintlib.org">FLINT-2.4.3 or later</a> (compiled with gmp and mpfr)
\* <a href="https://developer.gnome.org/glib/stable/">glib-2.0</a>
\* <a href="https://code.google.com/p/lz4">lz4</a>
\* <a href="http://www.freedesktop.org/wiki/Software/pkg-config/">pkg-config</a> (for the build only)
\section install_sec Installation

View File

@ -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;

View File

@ -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 <lz4.h>
#include <stdbool.h>
#include <string.h>
@ -40,6 +42,49 @@
#include <fmpz.h>
/**
* 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;
}
/*------------------------------------------------------------------------*/

View File

@ -34,12 +34,52 @@
#include "ntru_poly_ascii.h"
#include "ntru_string.h"
#include <lz4.h>
#include <string.h>
#include <fmpz_poly.h>
#include <fmpz.h>
/**
* 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 + 1;
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;
}

View File

@ -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,6 +118,16 @@ 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");

View File

@ -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,49 +86,61 @@ _cleanup:
fmpz_poly_clear(Fq);
fmpz_poly_clear(Fp);
fmpz_poly_clear(pub);
_return:
return retval;
}
/*------------------------------------------------------------------------*/
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;
}
/*------------------------------------------------------------------------*/

View File

@ -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,

View File

@ -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++)
@ -326,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);
@ -346,9 +350,12 @@ poly_inverse_poly_q(fmpz_poly_t Fq,
k++;
if (fmpz_poly_degree(f) == -1)
goto _cleanup;
goto cleanup;
}
if (fmpz_poly_is_zero(g) == 1)
goto cleanup;
if (fmpz_poly_degree(f) == 0)
break;
@ -368,7 +375,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--) {
@ -393,7 +400,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);
@ -438,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);
@ -461,6 +469,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;

View File

@ -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;
}

47
tests/Makefile Normal file
View File

@ -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

206
tests/ntru_cunit.c Normal file
View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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();
}

68
tests/ntru_cunit.h Normal file
View File

@ -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);

View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* 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, &params);
enc_string = read_file("to-decrypt.txt");
dec_string = ntru_decrypt_string(enc_string, pair.priv, pair.priv_inv,
&params);
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);
}

View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* 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, &params);
clear_string = read_file("to-encrypt.txt");
enc_string = ntru_encrypt_string(clear_string, pair.pub,
rnd, &params);
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);
}

162
tests/ntru_file_cunit.c Normal file
View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* 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));
}

341
tests/ntru_keypair_cunit.c Normal file
View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* 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, &params));
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, &params));
}
/**
* 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, &params);
export_public_key("pub.key", pair.pub, &params);
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, &params);
export_public_key(".", pair.pub, &params);
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, &params);
export_priv_key("priv.key", pair.pub, &params);
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, &params);
export_priv_key(".", pair.pub, &params);
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, &params);
export_public_key("pub.key", pair.pub, &params);
import_public_key(pub, "pub.key", &params);
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, &params);
export_public_key("pub.key", pair.pub, &params);
import_public_key(pub, "foo", &params);
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, &params);
export_priv_key("priv.key", pair.priv, &params);
import_priv_key(priv, priv_inv, "priv.key", &params);
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, &params);
export_priv_key("priv.key", pair.priv, &params);
import_priv_key(priv, priv_inv, ".", &params);
remove("priv.key");
CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(priv, pair.priv));
}

75
tests/ntru_poly_cunit.c Normal file
View File

@ -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 <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fmpz_poly.h>
#include <fmpz.h>
/**
* 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));
}

1
tests/test-file.txt Normal file
View File

@ -0,0 +1 @@
This test file is a test file.

1
tests/to-decrypt.txt Normal file
View File

@ -0,0 +1 @@
EAobFg4PHQYZBhEOChkYDg8fBhkGEw4KGRgODx0GGQYREAoZGA4PHQYbBBEODBsWDhEdBhkEERAKGxYQDx0IGwQTDgoZGA4RHQgZBBMQChkWDg8dCBkGEQ==

1
tests/to-encrypt.txt Normal file
View File

@ -0,0 +1 @@
BLAHFASEL