post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_ascii_poly.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_ascii_poly.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 
55 static char *
56 get_int_to_bin_str(uint8_t value);
57 
58 
59 /*------------------------------------------------------------------------*/
60 
61 static char *
62 get_int_to_bin_str(uint8_t value)
63 {
64  int i;
65  const size_t bin_string_size = ASCII_BITS + 1;
66  char *bin_string = ntru_malloc(sizeof(*bin_string) *
67  (bin_string_size)); /* account for trailing null-byte */
68 
69  /* terminate properly */
70  bin_string[bin_string_size - 1] = '\0';
71 
72  for (i = ASCII_BITS - 1; i >= 0; --i, value >>= 1)
73  bin_string[i] = (value & 1) + '0';
74 
75  return bin_string;
76 }
77 
78 /*------------------------------------------------------------------------*/
79 
80 fmpz_poly_t *
81 ascii_bin_to_bin_poly(const char *to_poly, const ntru_params *params)
82 {
83  uint32_t i = 0;
84  fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly));
85 
86  fmpz_poly_init(*new_poly);
87 
88  while (to_poly[i] && i < params->N) {
89  fmpz_poly_set_coeff_si(*new_poly,
90  i,
91  (to_poly[i] == '0') ? -1 : 1);
92  i++;
93  }
94 
95  return new_poly;
96 }
97 
98 /*------------------------------------------------------------------------*/
99 
100 fmpz_poly_t **
101 ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params)
102 {
103  char *cur = to_poly->ptr;
104  char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1));
105  uint32_t polyc = 0;
106  size_t out_len = 0;
107  fmpz_poly_t **poly_array;
108 
109  for (uint32_t i = 0; i < to_poly->len; i++) {
110  char *tmp_string = get_int_to_bin_str((int)(*cur));
111  memcpy(out + out_len, tmp_string, ASCII_BITS);
112  out_len += ASCII_BITS;
113  cur++;
114  free(tmp_string);
115  }
116  out[out_len] = '\0';
117 
118  poly_array = ntru_malloc(sizeof(**poly_array) *
119  (strlen(out) / params->N + 1));
120 
121  for (uint32_t i = 0; i < out_len; i += params->N) {
122  char chunk[params->N + 1];
123  size_t real_chunk_size;
124 
125  real_chunk_size =
126  (out_len - i > params->N) ? params->N : out_len - i;
127 
128  memcpy(chunk, out + i, real_chunk_size);
129  chunk[real_chunk_size] = '\0';
130 
131  poly_array[polyc] = ascii_bin_to_bin_poly(chunk, params);
132 
133  polyc++;
134  }
135 
136  free(out);
137 
138  poly_array[polyc] = NULL;
139 
140  return poly_array;
141 }
142 
143 /*------------------------------------------------------------------------*/
144 
145 fmpz_poly_t **
146 base64_to_poly_arr(const string *to_poly, const ntru_params *params)
147 {
148  uint32_t i = 0,
149  polyc = 0;
150  gsize out_len;
151  guchar *base64_decoded = NULL,
152  *base_tmp = NULL;
153  string *new_string = ntru_malloc(sizeof(*new_string));
154  fmpz_poly_t **poly_array;
155  char *tmp = ntru_malloc(sizeof(char) * (to_poly->len + 1));
156 
157  /* g_base64_decode() needs it null-terminated */
158  memcpy(tmp, to_poly->ptr, to_poly->len);
159  tmp[to_poly->len] = '\0';
160 
161  base_tmp = g_base64_decode((const gchar *)tmp, &out_len);
162 
163  /* g_base64_decode() needs it null-terminated */
164  REALLOC(tmp, sizeof(char) * (out_len + 1));
165  memcpy(tmp, base_tmp, out_len);
166  tmp[out_len] = '\0';
167 
168  base64_decoded = g_base64_decode((const gchar *)tmp, &out_len);
169 
170  new_string->ptr = (char *)base64_decoded;
171  new_string->len = (unsigned long)(out_len);
172 
173  poly_array = ntru_malloc(sizeof(**poly_array) *
174  (new_string->len / params->N));
175 
176  while (i < new_string->len) {
177  uint32_t j = 0;
178  fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly));
179 
180  fmpz_poly_init(*new_poly);
181 
182  while (j < params->N) {
183  fmpz_poly_set_coeff_si(*new_poly,
184  j,
185  (uint8_t)(base64_decoded[i]));
186  j++;
187  i++;
188  }
189 
190  /* fill the last poly with q (which is a non-standard
191  * coefficient) */
192  for (uint32_t k = j; k < params->N; k++) {
193  fmpz_poly_set_coeff_si(*new_poly,
194  k,
195  params->q);
196  }
197 
198  poly_array[polyc] = new_poly;
199  polyc++;
200  }
201 
202  poly_array[polyc] = NULL;
203 
204  string_delete(new_string);
205  free(base_tmp);
206  free(tmp);
207 
208  return poly_array;
209 }
fmpz_poly_t ** ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params)
#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
fmpz_poly_t ** base64_to_poly_arr(const string *to_poly, const ntru_params *params)
header for ntru_mem.c
NTRU parameters.
#define REALLOC(ptr, size)
Definition: ntru_mem.h:38
common macros/functions
static char * get_int_to_bin_str(uint8_t value)
void * ntru_malloc(size_t size)
Definition: ntru_mem.c:38
uint32_t q
Definition: ntru_params.h:52
header for ntru_string.c
#define CHAR_SIZE
Definition: ntru_common.h:37
char * ptr
Definition: ntru_string.h:49
fmpz_poly_t * ascii_bin_to_bin_poly(const char *to_poly, const ntru_params *params)
header for ntru_poly.c
header for ntru_ascii_poly.c