post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_poly_ascii.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 FH Bielefeld
3  *
4  * This file is part of a FH Bielefeld project.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21 
29 #include "ntru_poly_ascii.h"
30 #include "ntru_common.h"
31 #include "ntru_mem.h"
32 #include "ntru_params.h"
33 #include "ntru_poly.h"
34 #include "ntru_string.h"
35 
36 #include <glib.h>
37 
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include <fmpz_poly.h>
43 #include <fmpz.h>
44 
45 
62 static string *
63 get_bin_arr_to_ascii(const char *binary_rep);
64 
65 
66 /*------------------------------------------------------------------------*/
67 
68 static string *
69 get_bin_arr_to_ascii(const char *binary_rep)
70 {
71  size_t int_arr_size = 0;
72  uint8_t *int_arr = NULL;
73  uint32_t i = 0;
74  char *int_string = NULL;
75  string *result = ntru_malloc(sizeof(*result));
76 
77  if (!binary_rep || !*binary_rep)
78  return NULL;
79 
80  int_arr_size = strlen(binary_rep) / ASCII_BITS + 1;
81  int_arr = ntru_malloc(sizeof(*int_arr) * int_arr_size);
82 
83  while (*binary_rep) {
84  int_arr[i] = 0;
85 
86  /* convert one binary integer to real integer */
87  for (uint32_t j = 0; j < ASCII_BITS && *binary_rep; j++) {
88  if (*binary_rep == '1')
89  int_arr[i] = int_arr[i] * 2 + 1;
90  else if (*binary_rep == '0')
91  int_arr[i] *= 2;
92  binary_rep++;
93  }
94 
95  i++; /* amount of real integers */
96  }
97 
98  int_string = ntru_malloc(CHAR_SIZE * (i + 1));
99 
100  for (uint32_t j = 0; j < i; j++)
101  int_string[j] = (char) int_arr[j];
102 
103  result->ptr = int_string;
104  result->len = i;
105 
106  free(int_arr);
107 
108  return result;
109 }
110 
111 /*------------------------------------------------------------------------*/
112 
113 string *
114 bin_poly_to_ascii(const fmpz_poly_t poly,
115  const ntru_params *params)
116 {
117  string *result_string = ntru_malloc(sizeof(*result_string));
118  char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N));
119  uint32_t i = 0;
120 
121  for (i = 0; i < params->N; i++) {
122  fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, i);
123 
124  if (coeff) {
125  if (!fmpz_cmp_si(coeff, 1))
126  binary_rep[i] = '1';
127  else if (!fmpz_cmp_si(coeff, -1))
128  binary_rep[i] = '0';
129  } else {
130  break;
131  }
132  }
133 
134  result_string->ptr = binary_rep;
135  result_string->len = i;
136 
137  return result_string;
138 }
139 
140 /*------------------------------------------------------------------------*/
141 
142 string *
143 bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr,
144  const uint32_t poly_c,
145  const ntru_params *params)
146 {
147  char *binary_rep = NULL;
148  size_t string_len = 0;
149  string *ascii_string = NULL;
150 
151  /*
152  * parse the polynomial coefficients into a string
153  */
154  binary_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1));
155  for (uint32_t i = 0; i < poly_c; i++) {
156  string *single_poly_string = NULL;
157 
158  single_poly_string = bin_poly_to_ascii(*bin_poly_arr[i], params);
159 
160  memcpy(binary_rep + string_len,
161  single_poly_string->ptr,
162  single_poly_string->len);
163 
164  string_len += single_poly_string->len;
165 
166  string_delete(single_poly_string);
167  }
168  binary_rep[string_len] = '\0';
169 
170  ascii_string = get_bin_arr_to_ascii(binary_rep);
171 
172  free(binary_rep);
173 
174  return ascii_string;
175 }
176 
177 /*------------------------------------------------------------------------*/
178 
179 string *
180 poly_to_ascii(const fmpz_poly_t poly,
181  const ntru_params *params)
182 {
183  string *result_string = ntru_malloc(sizeof(*result_string));
184  char *string_rep = ntru_malloc(CHAR_SIZE * (params->N));
185 
186  for (uint32_t j = 0; j < params->N; j++) {
187  uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
188  if (coeff == params->q)
189  string_rep[j] = '\0';
190  else
191  string_rep[j] = (char)coeff;
192  }
193 
194  result_string->ptr = string_rep;
195  result_string->len = params->N;
196 
197  return result_string;
198 }
199 
200 /*------------------------------------------------------------------------*/
201 
202 string *
203 poly_arr_to_ascii(const fmpz_poly_t **poly_array,
204  const uint32_t poly_c,
205  const ntru_params *params)
206 {
207  char *string_rep = NULL;
208  size_t string_len = 0;
209  string *result_string = ntru_malloc(sizeof(*result_string));
210 
211  /*
212  * parse the polynomial coefficients into a string
213  */
214  string_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1));
215  for (uint32_t i = 0; i < poly_c; i++) {
216  string *poly_str;
217 
218  poly_str = poly_to_ascii(*poly_array[i], params);
219 
220  memcpy(string_rep + string_len,
221  poly_str->ptr,
222  poly_str->len);
223  string_len += poly_str->len;
224 
225  string_delete(poly_str);
226  }
227 
228  result_string->ptr = string_rep;
229  result_string->len = string_len;
230 
231  return result_string;
232 }
233 
234 /*------------------------------------------------------------------------*/
235 
236 string *
237 poly_to_base64(const fmpz_poly_t poly,
238  const ntru_params *params)
239 {
240  string *result_string = ntru_malloc(sizeof(*result_string));
241  string *string_rep = NULL;
242  gchar *base64_string = NULL,
243  *tmp = NULL;
244 
245  string_rep = poly_to_ascii(poly, params);
246 
247  tmp = g_base64_encode((const guchar *)string_rep->ptr,
248  string_rep->len);
249  base64_string = g_base64_encode((const guchar *)tmp,
250  strlen(tmp));
251 
252  result_string->ptr = base64_string;
253  result_string->len = strlen(base64_string);
254 
255  string_delete(string_rep);
256  free(tmp);
257 
258  return result_string;
259 }
260 
261 /*------------------------------------------------------------------------*/
262 
263 string *
264 poly_arr_to_base64(const fmpz_poly_t **poly_array,
265  const uint32_t poly_c,
266  const ntru_params *params)
267 {
268  string *string_rep;
269  string *result_string = ntru_malloc(sizeof(*result_string));
270 
271  gchar *base64_string = NULL,
272  *tmp = NULL;
273 
274  string_rep = poly_arr_to_ascii(poly_array, poly_c, params);
275 
276  tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len);
277  base64_string = g_base64_encode((const guchar *)tmp,
278  strlen(tmp));
279 
280  result_string->ptr = base64_string;
281  result_string->len = strlen(base64_string);
282 
283  string_delete(string_rep);
284  free(tmp);
285 
286  return result_string;
287 }
288 
289 /*------------------------------------------------------------------------*/
#define ASCII_BITS
Definition: ntru_common.h:38
void string_delete(string *del_string)
Definition: ntru_string.c:47
size_t len
Definition: ntru_string.h:53
uint32_t N
Definition: ntru_params.h:48
string * poly_to_ascii(const fmpz_poly_t poly, const ntru_params *params)
string * poly_arr_to_base64(const fmpz_poly_t **poly_array, const uint32_t poly_c, const ntru_params *params)
header for ntru_mem.c
static string * get_bin_arr_to_ascii(const char *binary_rep)
header for ntru_poly_ascii.c
NTRU parameters.
common macros/functions
string * poly_to_base64(const fmpz_poly_t poly, const ntru_params *params)
string * bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr, const uint32_t poly_c, const ntru_params *params)
string * bin_poly_to_ascii(const fmpz_poly_t poly, const ntru_params *params)
void * ntru_malloc(size_t size)
Definition: ntru_mem.c:38
uint32_t q
Definition: ntru_params.h:52
string * poly_arr_to_ascii(const fmpz_poly_t **poly_array, const uint32_t poly_c, const ntru_params *params)
header for ntru_string.c
#define CHAR_SIZE
Definition: ntru_common.h:37
char * ptr
Definition: ntru_string.h:49
header for ntru_poly.c