diff --git a/src/Makefile b/src/Makefile index 163f175..7cf0db2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,8 @@ PQC_SOURCES = poly.c \ keypair.c \ ascii_poly.c \ file.c \ - ntru_string.c + ntru_string.c \ + poly_ascii.c PQC_OBJS = $(patsubst %.c, %.o, $(PQC_SOURCES)) @@ -22,7 +23,8 @@ PQC_HEADERS = err.h \ ascii_poly.h \ common.h \ file.h \ - ntru_string.h + ntru_string.h \ + poly_ascii.h # libs LIBS += -L. -lgmp -lmpfr -lflint $(shell $(PKG_CONFIG) --libs glib-2.0) -lm diff --git a/src/ascii_poly.c b/src/ascii_poly.c index 3ff1c88..d244c01 100644 --- a/src/ascii_poly.c +++ b/src/ascii_poly.c @@ -21,9 +21,9 @@ /** * @file ascii_poly.c - * This file allows to convert between ascii strings - * and polynomials. - * @brief asci->poly and poly->ascii + * This file allows to convert ascii strings, including + * base64 encoded ones, to polynomials. + * @brief ascii to polynomials */ #include "ascii_poly.h" @@ -55,25 +55,6 @@ static char * get_int_to_bin_str(uint8_t value); -/** - * Converts a binary representation of multiple concatenated - * integers to the corresponding array of ascii chars, which - * is NULL-terminated. - * - * It reads in 8bit chunks, as in: - * - * 10110101|00111100|01011001 => 90|60|89 => "Zptr = int_string; - result->len = i; - - free(int_arr); - - return result; -} - -/*------------------------------------------------------------------------*/ - fmpz_poly_t * ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx) { @@ -205,108 +141,6 @@ ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx) /*------------------------------------------------------------------------*/ -string * -bin_poly_to_ascii(const fmpz_poly_t poly, - const ntru_context *ctx) -{ - string *result_string = ntru_malloc(sizeof(*result_string)); - char *binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); - uint32_t i = 0; - - for (uint32_t j = 0; j < ctx->N; j++) { - fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, j); - - if (coeff) { - if (!fmpz_cmp_si(coeff, 1)) - binary_rep[i] = '1'; - else if (!fmpz_cmp_si(coeff, -1)) - binary_rep[i] = '0'; - } else { - break; - } - - i++; - } - - result_string->ptr = binary_rep; - result_string->len = i; - - return result_string; -} - -/*------------------------------------------------------------------------*/ - -string * -bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, - const ntru_context *ctx) -{ - fmpz_poly_t *ascii_poly; - char *binary_rep = NULL; - size_t string_len = 0; - string *ascii_string = NULL; - size_t old_length = 0, - new_length; - - /* - * parse the polynomial coefficients into a string - */ - binary_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1)); - while ((ascii_poly = (fmpz_poly_t *)*bin_poly_arr++)) { - string *single_poly_string = NULL; - - new_length = CHAR_SIZE * (ctx->N); - - REALLOC(binary_rep, - old_length + - new_length + - 1); /* trailing null byte */ - - old_length += new_length; - - single_poly_string = bin_poly_to_ascii(*ascii_poly, ctx); - - memcpy(binary_rep + string_len, - single_poly_string->ptr, - single_poly_string->len); - - string_len += single_poly_string->len; - - string_delete(single_poly_string); - } - binary_rep[string_len] = '\0'; - - ascii_string = get_bin_arr_to_ascii(binary_rep); - - free(binary_rep); - - return ascii_string; -} - -/*------------------------------------------------------------------------*/ - -string * -poly_to_ascii(const fmpz_poly_t poly, - const ntru_context *ctx) -{ - string *result_string = ntru_malloc(sizeof(*result_string)); - char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); - - for (uint32_t j = 0; j < ctx->N; j++) { - uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j); - if (coeff == ctx->q) - string_rep[j] = '\0'; - else - string_rep[j] = (char)coeff; - } - - result_string->ptr = string_rep; - result_string->len = ctx->N; - - return result_string; -} - -/*------------------------------------------------------------------------*/ - fmpz_poly_t ** base64_to_poly_arr(const string *to_poly, const ntru_context *ctx) { @@ -372,84 +206,3 @@ base64_to_poly_arr(const string *to_poly, const ntru_context *ctx) return poly_array; } - -/*------------------------------------------------------------------------*/ - -string * -poly_to_base64(const fmpz_poly_t poly, - const ntru_context *ctx) -{ - string *result_string = ntru_malloc(sizeof(*result_string)); - string *string_rep = NULL; - gchar *base64_string = NULL, - *tmp = NULL; - - string_rep = poly_to_ascii(poly, ctx); - - tmp = 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; -} - -/*------------------------------------------------------------------------*/ - -string * -poly_arr_to_base64(fmpz_poly_t **poly_array, - const ntru_context *ctx) -{ - fmpz_poly_t *ascii_poly; - char *string_rep = NULL; - size_t string_len = 0; - string *result_string = ntru_malloc(sizeof(*result_string)); - size_t old_length = 0, - new_length; - gchar *base64_string = NULL, - *tmp = NULL; - - /* - * parse the polynomial coefficients into a string - */ - string_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1)); - while ((ascii_poly = *poly_array++)) { - string *poly_str; - - poly_str = poly_to_ascii(*ascii_poly, ctx); - - new_length = CHAR_SIZE * poly_str->len; - REALLOC(string_rep, - old_length + - new_length); - old_length += new_length; - - memcpy(string_rep + string_len, - poly_str->ptr, - poly_str->len); - string_len += poly_str->len; - - string_delete(poly_str); - } - - tmp = g_base64_encode((const guchar *)string_rep, string_len); - base64_string = g_base64_encode((const guchar *)tmp, - strlen(tmp)); - - result_string->ptr = base64_string; - result_string->len = strlen(base64_string); - - free(string_rep); - free(tmp); - - return result_string; -} - -/*------------------------------------------------------------------------*/ diff --git a/src/ascii_poly.h b/src/ascii_poly.h index 43d54e4..d767374 100644 --- a/src/ascii_poly.h +++ b/src/ascii_poly.h @@ -75,69 +75,6 @@ ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx); fmpz_poly_t ** ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx); -/** - * Convert a single binary polynomial back to a real string. - * The polynomial coefficients represent a binary format of the - * ascii string with the following mapping: - * - * 1 => 1 - * - * -1 => 0 - * - * 2 => 0 - * - * The 2's are only used for filling up the rest of the polynomial, - * so they will just end up as '\0's at the end of the string and will - * not confuse the result. - * - * @param poly the binary polynomial to convert - * @param ctx the NTRUEncrypt context - * @return the real string, newly allocated - */ -string * -bin_poly_to_ascii(const fmpz_poly_t poly, - const ntru_context *ctx); - -/** - * Convert an array of binary polynomials back to a real string. - * The polynomial coefficients represent a binary format of the - * ascii string with the following mapping: - * - * 1 => 1 - * - * -1 => 0 - * - * 2 => 0 - * - * The 2's are only used for filling up the last polynomial, so they will - * just end up as '\0's at the end of the string and will not confuse - * the result. - * - * @param bin_poly_arr the array of polynomials - * @param ctx the NTRUEncrypt context - * @return the real string, newly allocated - */ -string * -bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, - const ntru_context *ctx); - -/** - * Convert a single polynom back to a real string which is - * ascii encoded (full 256 C char spectrum). - * The polynomial coefficients are expected to be in the range - * [0, q-1] and will be casted back to chars without any mapping. - * - * Trailing q coefficients are only used for filling up the rest of - * a polynomial with '\0', so they will not confuse the result. - * - * @param poly the polynomial to convert - * @param ctx the NTRUEncrypt context - * @return the real string, newly allocated - */ -string * -poly_to_ascii(const fmpz_poly_t poly, - const ntru_context *ctx); - /** * Convert an base64 encoded string to an array of polyomials with * coefficients which are expected to be in the range [0, q-1]. @@ -156,38 +93,5 @@ poly_to_ascii(const fmpz_poly_t poly, fmpz_poly_t ** base64_to_poly_arr(const string *to_poly, const ntru_context *ctx); -/** - * Convert a single polynom back to a real string which is - * base64 encoded. - * The polynomial coefficients are expected to be in the range - * [0, q-1] and will be casted back to chars without any mapping. - * - * Trailing q coefficients are only used for filling up the rest of - * a polynomial with '\0', so they will not confuse the result. - * - * @param poly the polynomial to convert - * @param ctx the NTRUEncrypt context - * @return the real string, newly allocated - */ -string * -poly_to_base64(const fmpz_poly_t poly, - const ntru_context *ctx); - -/** - * Convert an array of polynomials back to a real string which - * is base64 encoded. - * The polynomial coefficients are expected to be in the range - * [0, q-1] and will be casted back to chars without any mapping. - * - * Trailing q coefficients are only used for filling up the rest of - * a polynomial with '\0', so they will not confuse the result. - * - * @param poly_arr the array of polynomials - * @param ctx the NTRUEncrypt context - * @return the real string, newly allocated - */ -string * -poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_context *ctx); - #endif /* NTRU_ASCII_POLY_H_ */ diff --git a/src/decrypt.c b/src/decrypt.c index 4d31092..d4df368 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -29,6 +29,7 @@ #include "ascii_poly.h" #include "decrypt.h" #include "ntru_string.h" +#include "poly_ascii.h" #include #include diff --git a/src/encrypt.c b/src/encrypt.c index 504b329..4eda6b6 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -30,6 +30,7 @@ #include "encrypt.h" #include "mem.h" #include "ntru_string.h" +#include "poly_ascii.h" #include diff --git a/src/keypair.c b/src/keypair.c index a2eb9a1..fc453ea 100644 --- a/src/keypair.c +++ b/src/keypair.c @@ -32,6 +32,7 @@ #include "keypair.h" #include "ntru_string.h" #include "poly.h" +#include "poly_ascii.h" #include #include diff --git a/src/poly_ascii.c b/src/poly_ascii.c new file mode 100644 index 0000000..5e70018 --- /dev/null +++ b/src/poly_ascii.c @@ -0,0 +1,292 @@ +/* + * 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 poly_ascii.c + * This file allows to convert polynomials to + * ascii strings, including base64 encoded. + * @brief polynomials to acii + */ + +#include "poly_ascii.h" +#include "common.h" +#include "context.h" +#include "mem.h" +#include "ntru_string.h" +#include "poly.h" + +#include + +#include +#include +#include + +#include +#include + + +/** + * Converts a binary representation of multiple concatenated + * integers to the corresponding array of ascii chars, which + * is NULL-terminated. + * + * It reads in 8bit chunks, as in: + * + * 10110101|00111100|01011001 => 90|60|89 => "Zptr = int_string; + result->len = i; + + free(int_arr); + + return result; +} + +/*------------------------------------------------------------------------*/ + +string * +bin_poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx) +{ + string *result_string = ntru_malloc(sizeof(*result_string)); + char *binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); + uint32_t i = 0; + + for (uint32_t j = 0; j < ctx->N; j++) { + fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, j); + + if (coeff) { + if (!fmpz_cmp_si(coeff, 1)) + binary_rep[i] = '1'; + else if (!fmpz_cmp_si(coeff, -1)) + binary_rep[i] = '0'; + } else { + break; + } + + i++; + } + + result_string->ptr = binary_rep; + result_string->len = i; + + return result_string; +} + +/*------------------------------------------------------------------------*/ + +string * +bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, + const ntru_context *ctx) +{ + fmpz_poly_t *ascii_poly; + char *binary_rep = NULL; + size_t string_len = 0; + string *ascii_string = NULL; + size_t old_length = 0, + new_length; + + /* + * parse the polynomial coefficients into a string + */ + binary_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1)); + while ((ascii_poly = (fmpz_poly_t *)*bin_poly_arr++)) { + string *single_poly_string = NULL; + + new_length = CHAR_SIZE * (ctx->N); + + REALLOC(binary_rep, + old_length + + new_length + + 1); /* trailing null byte */ + + old_length += new_length; + + single_poly_string = bin_poly_to_ascii(*ascii_poly, ctx); + + memcpy(binary_rep + string_len, + single_poly_string->ptr, + single_poly_string->len); + + string_len += single_poly_string->len; + + string_delete(single_poly_string); + } + binary_rep[string_len] = '\0'; + + ascii_string = get_bin_arr_to_ascii(binary_rep); + + free(binary_rep); + + return ascii_string; +} + +/*------------------------------------------------------------------------*/ + +string * +poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx) +{ + string *result_string = ntru_malloc(sizeof(*result_string)); + char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); + + for (uint32_t j = 0; j < ctx->N; j++) { + uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j); + if (coeff == ctx->q) + string_rep[j] = '\0'; + else + string_rep[j] = (char)coeff; + } + + result_string->ptr = string_rep; + result_string->len = ctx->N; + + return result_string; +} + +/*------------------------------------------------------------------------*/ + +string * +poly_to_base64(const fmpz_poly_t poly, + const ntru_context *ctx) +{ + string *result_string = ntru_malloc(sizeof(*result_string)); + string *string_rep = NULL; + gchar *base64_string = NULL, + *tmp = NULL; + + string_rep = poly_to_ascii(poly, ctx); + + tmp = 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; +} + +/*------------------------------------------------------------------------*/ + +string * +poly_arr_to_base64(fmpz_poly_t **poly_array, + const ntru_context *ctx) +{ + fmpz_poly_t *ascii_poly; + char *string_rep = NULL; + size_t string_len = 0; + string *result_string = ntru_malloc(sizeof(*result_string)); + size_t old_length = 0, + new_length; + gchar *base64_string = NULL, + *tmp = NULL; + + /* + * parse the polynomial coefficients into a string + */ + string_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1)); + while ((ascii_poly = *poly_array++)) { + string *poly_str; + + poly_str = poly_to_ascii(*ascii_poly, ctx); + + new_length = CHAR_SIZE * poly_str->len; + REALLOC(string_rep, + old_length + + new_length); + old_length += new_length; + + memcpy(string_rep + string_len, + poly_str->ptr, + poly_str->len); + string_len += poly_str->len; + + string_delete(poly_str); + } + + tmp = g_base64_encode((const guchar *)string_rep, string_len); + base64_string = g_base64_encode((const guchar *)tmp, + strlen(tmp)); + + result_string->ptr = base64_string; + result_string->len = strlen(base64_string); + + free(string_rep); + free(tmp); + + return result_string; +} + +/*------------------------------------------------------------------------*/ diff --git a/src/poly_ascii.h b/src/poly_ascii.h new file mode 100644 index 0000000..df76746 --- /dev/null +++ b/src/poly_ascii.h @@ -0,0 +1,137 @@ +/* + * 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 poly_ascii.h + * Header for the internal API of poly_ascii.c. + * @brief header for poly_ascii.c + */ + +#ifndef NTRU_POLY_ASCII_H_ +#define NTRU_POLY_ASCII_H_ + + +#include "common.h" +#include "context.h" +#include "ntru_string.h" + +#include +#include + + +/** + * Convert a single binary polynomial back to a real string. + * The polynomial coefficients represent a binary format of the + * ascii string with the following mapping: + * + * 1 => 1 + * + * -1 => 0 + * + * 2 => 0 + * + * The 2's are only used for filling up the rest of the polynomial, + * so they will just end up as '\0's at the end of the string and will + * not confuse the result. + * + * @param poly the binary polynomial to convert + * @param ctx the NTRUEncrypt context + * @return the real string, newly allocated + */ +string * +bin_poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx); + +/** + * Convert an array of binary polynomials back to a real string. + * The polynomial coefficients represent a binary format of the + * ascii string with the following mapping: + * + * 1 => 1 + * + * -1 => 0 + * + * 2 => 0 + * + * The 2's are only used for filling up the last polynomial, so they will + * just end up as '\0's at the end of the string and will not confuse + * the result. + * + * @param bin_poly_arr the array of polynomials + * @param ctx the NTRUEncrypt context + * @return the real string, newly allocated + */ +string * +bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, + const ntru_context *ctx); + +/** + * Convert a single polynom back to a real string which is + * ascii encoded (full 256 C char spectrum). + * The polynomial coefficients are expected to be in the range + * [0, q-1] and will be casted back to chars without any mapping. + * + * Trailing q coefficients are only used for filling up the rest of + * a polynomial with '\0', so they will not confuse the result. + * + * @param poly the polynomial to convert + * @param ctx the NTRUEncrypt context + * @return the real string, newly allocated + */ +string * +poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx); + +/** + * Convert a single polynom back to a real string which is + * base64 encoded. + * The polynomial coefficients are expected to be in the range + * [0, q-1] and will be casted back to chars without any mapping. + * + * Trailing q coefficients are only used for filling up the rest of + * a polynomial with '\0', so they will not confuse the result. + * + * @param poly the polynomial to convert + * @param ctx the NTRUEncrypt context + * @return the real string, newly allocated + */ +string * +poly_to_base64(const fmpz_poly_t poly, + const ntru_context *ctx); + +/** + * Convert an array of polynomials back to a real string which + * is base64 encoded. + * The polynomial coefficients are expected to be in the range + * [0, q-1] and will be casted back to chars without any mapping. + * + * Trailing q coefficients are only used for filling up the rest of + * a polynomial with '\0', so they will not confuse the result. + * + * @param poly_arr the array of polynomials + * @param ctx the NTRUEncrypt context + * @return the real string, newly allocated + */ +string * +poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_context *ctx); + + +#endif /* NTRU_POLY_ASCII_H_ */