From d498ddf1a85dd68bb1731a57f6dd08e390d0f4d7 Mon Sep 17 00:00:00 2001 From: malte Date: Sun, 18 May 2014 10:28:25 +0200 Subject: [PATCH] ASCII->POLY: written a function to convert a char* string into a polynom. The function returns a newly allocated poynom of the string size * 7 7 bits per ASCII symbol. the function need to be warped so the maximal poynom is corresponding to the NTRU context. --- src/ascii_poly.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ascii_poly.h | 27 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/ascii_poly.c create mode 100644 src/ascii_poly.h diff --git a/src/ascii_poly.c b/src/ascii_poly.c new file mode 100644 index 0000000..e33c54f --- /dev/null +++ b/src/ascii_poly.c @@ -0,0 +1,74 @@ +/* + * 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 + */ + +#include "mem.h" +#include "poly.h" +#include "ascii_poly.h" + +#include +#include +#include +#include +#include +#include + +#define ASCII_DIGIETS 7 + +/** + * Converts a string into a pb_poly of the size strlen(to_poly) * 7. + * 7 bit per ASCII symbol. + * + * @param to_poly the string + * @return the newly allocated polynom. + */ +pb_poly *ascii_to_poly(char *to_poly) +{ + size_t length = (strlen(to_poly) * ASCII_DIGIETS); + char *tmp_ptr = to_poly; + u_int8_t quotient, + i, + k, + binary_Number[ASCII_DIGIETS + 1]; + + mp_int *chara = ntru_malloc(sizeof(mp_int)); + init_integer(chara); + + pb_poly *poly = ntru_malloc(sizeof(pb_poly)); + init_polynom_size(poly, chara, length); + + for (u_int32_t j = 0; j < strlen(to_poly); j++) { + quotient = (u_int8_t) *tmp_ptr++; + k = ASCII_DIGIETS; + for (i = 1; i <= ASCII_DIGIETS; i++) { + binary_Number[k--] = quotient % 2; + quotient >>= 1; + } + for (i = 1; i <= ASCII_DIGIETS; i++) { + mp_set(&(poly->terms[((i - 1) + (j * ASCII_DIGIETS))]), + binary_Number[i]); + binary_Number[i] = 0; + poly->terms[i].sign = 0; + } + } + poly->used = (int) length; + mp_clear(chara); + return poly; +} \ No newline at end of file diff --git a/src/ascii_poly.h b/src/ascii_poly.h new file mode 100644 index 0000000..739801d --- /dev/null +++ b/src/ascii_poly.h @@ -0,0 +1,27 @@ +/* + * 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 + */ + +#ifndef ASCII_POLY_H_ +#define ASCII_POLY_H_ + +pb_poly *ascii_to_poly(char *to_poly); + +#endif /* ASCII_POLY_H_ */