a4cc871a4753a2c852faee718a7e2b3624517f16
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_poly.c
1 /*
2 * Copyright (C) 2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2009-2013 Security Innovation
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "ntru_poly.h"
19 #include "ntru_mgf1.h"
20
21 #include <utils/debug.h>
22 #include <utils/test.h>
23
24 typedef struct private_ntru_poly_t private_ntru_poly_t;
25 typedef struct indices_len_t indices_len_t;
26
27 /**
28 * Stores number of +1 and -1 coefficients
29 */
30 struct indices_len_t {
31 int p;
32 int m;
33 };
34
35 /**
36 * Private data of an ntru_poly_t object.
37 */
38 struct private_ntru_poly_t {
39
40 /**
41 * Public ntru_poly_t interface.
42 */
43 ntru_poly_t public;
44
45 /**
46 * Ring dimension equal to the number of polynomial coefficients
47 */
48 uint16_t N;
49
50 /**
51 * Large modulus
52 */
53 uint16_t q;
54
55 /**
56 * Array containing the indices of the non-zero coefficients
57 */
58 uint16_t *indices;
59
60 /**
61 * Number of sparse polynomials
62 */
63 int num_polynomials;
64
65 /**
66 * Number of nonzero coefficients for up to 3 sparse polynomials
67 */
68 indices_len_t indices_len[3];
69
70 };
71
72 METHOD(ntru_poly_t, get_size, size_t,
73 private_ntru_poly_t *this)
74 {
75 int n;
76 size_t size = 0;
77
78 for (n = 0; n < this->num_polynomials; n++)
79 {
80 size += this->indices_len[n].p + this->indices_len[n].m;
81 }
82 return size;
83 }
84
85 METHOD(ntru_poly_t, get_indices, uint16_t*,
86 private_ntru_poly_t *this)
87 {
88 return this->indices;
89 }
90 /**
91 * Multiplication of polynomial a with a sparse polynomial b given by
92 * the indices of its +1 and -1 coefficients results in polynomial c.
93 * This is a convolution operation
94 */
95 static void ring_mult_i(uint16_t *a, indices_len_t len, uint16_t *indices,
96 uint16_t N, uint16_t mod_q_mask, uint16_t *t,
97 uint16_t *c)
98 {
99 int i, j, k;
100
101 /* initialize temporary array t */
102 for (k = 0; k < N; k++)
103 {
104 t[k] = 0;
105 }
106
107 /* t[(i+k)%N] = sum i=0 through N-1 of a[i], for b[k] = -1 */
108 for (j = len.p; j < len.p + len.m; j++)
109 {
110 k = indices[j];
111 for (i = 0; k < N; ++i, ++k)
112 {
113 t[k] += a[i];
114 }
115 for (k = 0; i < N; ++i, ++k)
116 {
117 t[k] += a[i];
118 }
119 }
120
121 /* t[(i+k)%N] = -(sum i=0 through N-1 of a[i] for b[k] = -1) */
122 for (k = 0; k < N; k++)
123 {
124 t[k] = -t[k];
125 }
126
127 /* t[(i+k)%N] += sum i=0 through N-1 of a[i] for b[k] = +1 */
128 for (j = 0; j < len.p; j++)
129 {
130 k = indices[j];
131 for (i = 0; k < N; ++i, ++k)
132 {
133 t[k] += a[i];
134 }
135 for (k = 0; i < N; ++i, ++k)
136 {
137 t[k] += a[i];
138 }
139 }
140
141 /* c = (a * b) mod q */
142 for (k = 0; k < N; k++)
143 {
144 c[k] = t[k] & mod_q_mask;
145 }
146 }
147
148 METHOD(ntru_poly_t, ring_mult, void,
149 private_ntru_poly_t *this, uint16_t *a, uint16_t *c)
150 {
151 uint16_t *t1, *t2;
152 uint16_t *bi = this->indices;
153 uint16_t mod_q_mask = this->q - 1;
154 int i;
155
156 /* allocate temporary array t1 */
157 t1 = malloc(this->N * sizeof(uint16_t));
158
159 if (this->num_polynomials == 1)
160 {
161 ring_mult_i(a, this->indices_len[0], bi, this->N, mod_q_mask, t1, c);
162 }
163 else
164 {
165 /* allocate temporary array t2 */
166 t2 = malloc(this->N * sizeof(uint16_t));
167
168 /* t1 = a * b1 */
169 ring_mult_i(a, this->indices_len[0], bi, this->N, mod_q_mask, t1, t1);
170
171 /* t1 = (a * b1) * b2 */
172 bi += this->indices_len[0].p + this->indices_len[0].m;
173 ring_mult_i(t1, this->indices_len[1], bi, this->N, mod_q_mask, t2, t1);
174
175 /* t2 = a * b3 */
176 bi += this->indices_len[1].p + this->indices_len[1].m;
177 ring_mult_i(a, this->indices_len[2], bi, this->N, mod_q_mask, t2, t2);
178
179 /* c = (a * b1 * b2) + (a * b3) */
180 for (i = 0; i < this->N; i++)
181 {
182 c[i] = (t1[i] + t2[i]) & mod_q_mask;
183 }
184 free(t2);
185 }
186 free(t1);
187 }
188
189 METHOD(ntru_poly_t, destroy, void,
190 private_ntru_poly_t *this)
191 {
192 memwipe(this->indices, sizeof(uint16_t) * get_size(this));
193 free(this->indices);
194 free(this);
195 }
196
197 /*
198 * Described in header.
199 */
200 ntru_poly_t *ntru_poly_create(hash_algorithm_t alg, chunk_t seed,
201 uint8_t c_bits, uint16_t N, uint16_t q,
202 uint32_t indices_len_p, uint32_t indices_len_m,
203 bool is_product_form)
204 {
205 private_ntru_poly_t *this;
206 size_t hash_len, octet_count = 0, i;
207 uint8_t octets[HASH_SIZE_SHA512], *used, num_left = 0, num_needed;
208 uint16_t index, limit, left = 0;
209 int n, num_indices, index_i = 0;
210 ntru_mgf1_t *mgf1;
211
212 DBG2(DBG_LIB, "MGF1 is seeded with %u bytes", seed.len);
213 mgf1 = ntru_mgf1_create(alg, seed, TRUE);
214 if (!mgf1)
215 {
216 return NULL;
217 }
218 i = hash_len = mgf1->get_hash_size(mgf1);
219
220 INIT(this,
221 .public = {
222 .get_size = _get_size,
223 .get_indices = _get_indices,
224 .ring_mult = _ring_mult,
225 .destroy = _destroy,
226 },
227 .N = N,
228 .q = q,
229 );
230
231 if (is_product_form)
232 {
233 this->num_polynomials = 3;
234 for (n = 0; n < 3; n++)
235 {
236 this->indices_len[n].p = 0xff & indices_len_p;
237 this->indices_len[n].m = 0xff & indices_len_m;
238 indices_len_p >>= 8;
239 indices_len_m >>= 8;
240 }
241 }
242 else
243 {
244 this->num_polynomials = 1;
245 this->indices_len[0].p = indices_len_p;
246 this->indices_len[0].m = indices_len_m;
247 }
248 this->indices = malloc(sizeof(uint16_t) * get_size(this)),
249
250 used = malloc(N);
251 limit = N * ((1 << c_bits) / N);
252
253 /* generate indices for all polynomials */
254 for (n = 0; n < this->num_polynomials; n++)
255 {
256 memset(used, 0, N);
257 num_indices = this->indices_len[n].p + this->indices_len[n].m;
258
259 /* generate indices for a single polynomial */
260 while (num_indices)
261 {
262 /* generate a random candidate index with a size of c_bits */
263 do
264 {
265 /* use any leftover bits first */
266 index = num_left ? left << (c_bits - num_left) : 0;
267
268 /* get the rest of the bits needed from new octets */
269 num_needed = c_bits - num_left;
270
271 while (num_needed)
272 {
273 if (i == hash_len)
274 {
275 /* get another block from MGF1 */
276 if (!mgf1->get_mask(mgf1, hash_len, octets))
277 {
278 mgf1->destroy(mgf1);
279 destroy(this);
280 free(used);
281 return NULL;
282 }
283 octet_count += hash_len;
284 i = 0;
285 }
286 left = octets[i++];
287
288 if (num_needed <= 8)
289 {
290 /* all bits needed to fill the index are in this octet */
291 index |= left >> (8 - num_needed);
292 num_left = 8 - num_needed;
293 num_needed = 0;
294 left &= 0xff >> (8 - num_left);
295 }
296 else
297 {
298 /* more than one octet will be needed */
299 index |= left << (num_needed - 8);
300 num_needed -= 8;
301 }
302 }
303 }
304 while (index >= limit);
305
306 /* form index and check if unique */
307 index %= N;
308 if (!used[index])
309 {
310 used[index] = 1;
311 this->indices[index_i++] = index;
312 num_indices--;
313 }
314 }
315 }
316
317 DBG2(DBG_LIB, "MGF1 generates %u octets to derive %u indices",
318 octet_count, get_size(this));
319 mgf1->destroy(mgf1);
320 free(used);
321
322 return &this->public;
323 }
324
325 EXPORT_FUNCTION_FOR_TESTS(ntru, ntru_poly_create);