Debian Codegraph @

cipher/sntrup761.c

raw
1 /* sntrup761.c - Streamlined NTRU Prime sntrup761 key-encapsulation method
2 * Copyright (C) 2023 Simon Josefsson <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt 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
14 * GNU 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 program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 *
20 * For a description of the algorithm, see:
21 * https://ntruprime.cr.yp.to/
22 */
23
24 /*
25 * Derived from public domain source, written by (in alphabetical order):
26 * - Daniel J. Bernstein
27 * - Chitchanok Chuengsatiansup
28 * - Tanja Lange
29 * - Christine van Vredendaal
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "sntrup761.h"
37 #include "const-time.h"
38
39 /* from supercop-20201130/crypto_sort/int32/portable4/int32_minmax.inc */
40 #define int32_MINMAX(a,b) \
41 do { \
42 int64_t ab = (int64_t)b ^ (int64_t)a; \
43 int64_t c = (int64_t)b - (int64_t)a; \
44 c ^= ab & (c ^ b); \
45 c = ct_ulong_gen_mask((uint64_t)c >> 63); \
46 c &= ab; \
47 a ^= c; \
48 b ^= c; \
49 } while(0)
50
51 /* from supercop-20201130/crypto_sort/int32/portable4/sort.c */
52 static void
53 crypto_sort_int32 (void *array, long long n)
54 {
55 long long top, p, q, r, i, j;
56 int32_t *x = array;
57
58 if (n < 2)
59 return;
60 top = 1;
61 while (top < n - top)
62 top += top;
63
64 for (p = top; p >= 1; p >>= 1)
65 {
66 i = 0;
67 while (i + 2 * p <= n)
68 {
69 for (j = i; j < i + p; ++j)
70 int32_MINMAX (x[j], x[j + p]);
71 i += 2 * p;
72 }
73 for (j = i; j < n - p; ++j)
74 int32_MINMAX (x[j], x[j + p]);
75
76 i = 0;
77 j = 0;
78 for (q = top; q > p; q >>= 1)
79 {
80 if (j != i)
81 for (;;)
82 {
83 int32_t a;
84
85 if (j == n - q)
86 goto done;
87 a = x[j + p];
88 for (r = q; r > p; r >>= 1)
89 int32_MINMAX (a, x[j + r]);
90 x[j + p] = a;
91 ++j;
92 if (j == i + p)
93 {
94 i += 2 * p;
95 break;
96 }
97 }
98 while (i + p <= n - q)
99 {
100 for (j = i; j < i + p; ++j)
101 {
102 int32_t a = x[j + p];
103 for (r = q; r > p; r >>= 1)
104 int32_MINMAX (a, x[j + r]);
105 x[j + p] = a;
106 }
107 i += 2 * p;
108 }
109 /* now i + p > n - q */
110 j = i;
111 while (j < n - q)
112 {
113 int32_t a = x[j + p];
114 for (r = q; r > p; r >>= 1)
115 int32_MINMAX (a, x[j + r]);
116 x[j + p] = a;
117 ++j;
118 }
119
120 done:;
121 }
122 }
123 }
124
125 /* from supercop-20201130/crypto_sort/uint32/useint32/sort.c */
126
127 /* can save time by vectorizing xor loops */
128 /* can save time by integrating xor loops with int32_sort */
129
130 static void
131 crypto_sort_uint32 (void *array, long long n)
132 {
133 uint32_t *x = array;
134 long long j;
135 for (j = 0; j < n; ++j)
136 x[j] ^= 0x80000000;
137 crypto_sort_int32 (array, n);
138 for (j = 0; j < n; ++j)
139 x[j] ^= 0x80000000;
140 }
141
142 /* from supercop-20201130/crypto_kem/sntrup761/ref/uint32.c */
143
144 /*
145 CPU division instruction typically takes time depending on x.
146 This software is designed to take time independent of x.
147 Time still varies depending on m; user must ensure that m is constant.
148 Time also varies on CPUs where multiplication is variable-time.
149 There could be more CPU issues.
150 There could also be compiler issues.
151 */
152
153 static void
154 uint32_divmod_uint14 (uint32_t * q, uint16_t * r, uint32_t x, uint16_t m)
155 {
156 uint32_t v = 0x80000000;
157 uint32_t qpart;
158 uint32_t mask;
159
160 v /= m;
161
162 /* caller guarantees m > 0 */
163 /* caller guarantees m < 16384 */
164 /* vm <= 2^31 <= vm+m-1 */
165 /* xvm <= 2^31 x <= xvm+x(m-1) */
166
167 *q = 0;
168
169 qpart = (x * (uint64_t) v) >> 31;
170 /* 2^31 qpart <= xv <= 2^31 qpart + 2^31-1 */
171 /* 2^31 qpart m <= xvm <= 2^31 qpart m + (2^31-1)m */
172 /* 2^31 qpart m <= 2^31 x <= 2^31 qpart m + (2^31-1)m + x(m-1) */
173 /* 0 <= 2^31 newx <= (2^31-1)m + x(m-1) */
174 /* 0 <= newx <= (1-1/2^31)m + x(m-1)/2^31 */
175 /* 0 <= newx <= (1-1/2^31)(2^14-1) + (2^32-1)((2^14-1)-1)/2^31 */
176
177 x -= qpart * m;
178 *q += qpart;
179 /* x <= 49146 */
180
181 qpart = (x * (uint64_t) v) >> 31;
182 /* 0 <= newx <= (1-1/2^31)m + x(m-1)/2^31 */
183 /* 0 <= newx <= m + 49146(2^14-1)/2^31 */
184 /* 0 <= newx <= m + 0.4 */
185 /* 0 <= newx <= m */
186
187 x -= qpart * m;
188 *q += qpart;
189 /* x <= m */
190
191 x -= m;
192 *q += 1;
193 mask = ct_ulong_gen_mask(x >> 31);
194 x += mask & (uint32_t) m;
195 *q += mask;
196 /* x < m */
197
198 *r = x;
199 }
200
201
202 static uint16_t
203 uint32_mod_uint14 (uint32_t x, uint16_t m)
204 {
205 uint32_t q;
206 uint16_t r;
207 uint32_divmod_uint14 (&q, &r, x, m);
208 return r;
209 }
210
211 /* from supercop-20201130/crypto_kem/sntrup761/ref/int32.c */
212
213 static void
214 int32_divmod_uint14 (int32_t * q, uint16_t * r, int32_t x, uint16_t m)
215 {
216 uint32_t uq, uq2;
217 uint16_t ur, ur2;
218 uint32_t mask;
219
220 uint32_divmod_uint14 (&uq, &ur, 0x80000000 + (uint32_t) x, m);
221 uint32_divmod_uint14 (&uq2, &ur2, 0x80000000, m);
222 ur -= ur2;
223 uq -= uq2;
224 mask = ct_ulong_gen_mask(ur >> 15);
225 ur += mask & m;
226 uq += mask;
227 *r = ur;
228 *q = uq;
229 }
230
231
232 static uint16_t
233 int32_mod_uint14 (int32_t x, uint16_t m)
234 {
235 int32_t q;
236 uint16_t r;
237 int32_divmod_uint14 (&q, &r, x, m);
238 return r;
239 }
240
241 /* from supercop-20201130/crypto_kem/sntrup761/ref/paramsmenu.h */
242 #define p 761
243 #define q 4591
244 #define Rounded_bytes 1007
245 #define Rq_bytes 1158
246 #define w 286
247
248 /* from supercop-20201130/crypto_kem/sntrup761/ref/Decode.h */
249
250 /* Decode(R,s,M,len) */
251 /* assumes 0 < M[i] < 16384 */
252 /* produces 0 <= R[i] < M[i] */
253
254 /* from supercop-20201130/crypto_kem/sntrup761/ref/Decode.c */
255
256 static void
257 Decode (uint16_t * out, const unsigned char *S, const uint16_t * M,
258 long long len)
259 {
260 if (len == 1)
261 {
262 if (M[0] == 1)
263 *out = 0;
264 else if (M[0] <= 256)
265 *out = uint32_mod_uint14 (S[0], M[0]);
266 else
267 *out = uint32_mod_uint14 (S[0] + (((uint16_t) S[1]) << 8), M[0]);
268 }
269 if (len > 1)
270 {
271 uint16_t R2[(len + 1) / 2];
272 uint16_t M2[(len + 1) / 2];
273 uint16_t bottomr[len / 2];
274 uint32_t bottomt[len / 2];
275 long long i;
276 for (i = 0; i < len - 1; i += 2)
277 {
278 uint32_t m = M[i] * (uint32_t) M[i + 1];
279 if (m > 256 * 16383)
280 {
281 bottomt[i / 2] = 256 * 256;
282 bottomr[i / 2] = S[0] + 256 * S[1];
283 S += 2;
284 M2[i / 2] = (((m + 255) >> 8) + 255) >> 8;
285 }
286 else if (m >= 16384)
287 {
288 bottomt[i / 2] = 256;
289 bottomr[i / 2] = S[0];
290 S += 1;
291 M2[i / 2] = (m + 255) >> 8;
292 }
293 else
294 {
295 bottomt[i / 2] = 1;
296 bottomr[i / 2] = 0;
297 M2[i / 2] = m;
298 }
299 }
300 if (i < len)
301 M2[i / 2] = M[i];
302 Decode (R2, S, M2, (len + 1) / 2);
303 for (i = 0; i < len - 1; i += 2)
304 {
305 uint32_t r = bottomr[i / 2];
306 uint32_t r1;
307 uint16_t r0;
308 r += bottomt[i / 2] * R2[i / 2];
309 uint32_divmod_uint14 (&r1, &r0, r, M[i]);
310 r1 = uint32_mod_uint14 (r1, M[i + 1]); /* only needed for invalid inputs */
311 *out++ = r0;
312 *out++ = r1;
313 }
314 if (i < len)
315 *out++ = R2[i / 2];
316 }
317 }
318
319 /* from supercop-20201130/crypto_kem/sntrup761/ref/Encode.h */
320
321 /* Encode(s,R,M,len) */
322 /* assumes 0 <= R[i] < M[i] < 16384 */
323
324 /* from supercop-20201130/crypto_kem/sntrup761/ref/Encode.c */
325
326 /* 0 <= R[i] < M[i] < 16384 */
327 static void
328 Encode (unsigned char *out, const uint16_t * R, const uint16_t * M,
329 long long len)
330 {
331 if (len == 1)
332 {
333 uint16_t r = R[0];
334 uint16_t m = M[0];
335 while (m > 1)
336 {
337 *out++ = r;
338 r >>= 8;
339 m = (m + 255) >> 8;
340 }
341 }
342 if (len > 1)
343 {
344 uint16_t R2[(len + 1) / 2];
345 uint16_t M2[(len + 1) / 2];
346 long long i;
347 for (i = 0; i < len - 1; i += 2)
348 {
349 uint32_t m0 = M[i];
350 uint32_t r = R[i] + R[i + 1] * m0;
351 uint32_t m = M[i + 1] * m0;
352 while (m >= 16384)
353 {
354 *out++ = r;
355 r >>= 8;
356 m = (m + 255) >> 8;
357 }
358 R2[i / 2] = r;
359 M2[i / 2] = m;
360 }
361 if (i < len)
362 {
363 R2[i / 2] = R[i];
364 M2[i / 2] = M[i];
365 }
366 Encode (out, R2, M2, (len + 1) / 2);
367 }
368 }
369
370 /* from supercop-20201130/crypto_kem/sntrup761/ref/kem.c */
371
372 /* ----- masks */
373
374 /* return -1 if x!=0; else return 0 */
375 static int
376 int16_t_nonzero_mask (int16_t x)
377 {
378 uint16_t u = x; /* 0, else 1...65535 */
379 uint32_t v = u; /* 0, else 1...65535 */
380 v = -v; /* 0, else 2^32-65535...2^32-1 */
381 v >>= 31; /* 0, else 1 */
382 return ct_ulong_gen_mask(v); /* 0, else -1 */
383 }
384
385 /* return -1 if x<0; otherwise return 0 */
386 static int
387 int16_t_negative_mask (int16_t x)
388 {
389 uint16_t u = x;
390 u >>= 15;
391 return (int)ct_ulong_gen_mask(u);
392 }
393
394 /* ----- arithmetic mod 3 */
395
396 typedef int8_t small;
397
398 /* F3 is always represented as -1,0,1 */
399 /* so ZZ_fromF3 is a no-op */
400
401 /* x must not be close to top int16_t */
402 static small
403 F3_freeze (int16_t x)
404 {
405 return int32_mod_uint14 (x + 1, 3) - 1;
406 }
407
408 /* ----- arithmetic mod q */
409
410 #define q12 ((q-1)/2)
411 typedef int16_t Fq;
412 /* always represented as -q12...q12 */
413 /* so ZZ_fromFq is a no-op */
414
415 /* x must not be close to top int32 */
416 static Fq
417 Fq_freeze (int32_t x)
418 {
419 return int32_mod_uint14 (x + q12, q) - q12;
420 }
421
422 static Fq
423 Fq_recip (Fq a1)
424 {
425 int i = 1;
426 Fq ai = a1;
427
428 while (i < q - 2)
429 {
430 ai = Fq_freeze (a1 * (int32_t) ai);
431 i += 1;
432 }
433 return ai;
434 }
435
436 /* ----- small polynomials */
437
438 /* 0 if Weightw_is(r), else -1 */
439 static int
440 Weightw_mask (small * r)
441 {
442 int weight = 0;
443 int i;
444
445 for (i = 0; i < p; ++i)
446 weight += r[i] & 1;
447 return int16_t_nonzero_mask (weight - w);
448 }
449
450 /* R3_fromR(R_fromRq(r)) */
451 static void
452 R3_fromRq (small * out, const Fq * r)
453 {
454 int i;
455 for (i = 0; i < p; ++i)
456 out[i] = F3_freeze (r[i]);
457 }
458
459 /* h = f*g in the ring R3 */
460 static void
461 R3_mult (small * h, const small * f, const small * g)
462 {
463 small fg[p + p - 1];
464 small result;
465 int i, j;
466
467 for (i = 0; i < p; ++i)
468 {
469 result = 0;
470 for (j = 0; j <= i; ++j)
471 result = F3_freeze (result + f[j] * g[i - j]);
472 fg[i] = result;
473 }
474 for (i = p; i < p + p - 1; ++i)
475 {
476 result = 0;
477 for (j = i - p + 1; j < p; ++j)
478 result = F3_freeze (result + f[j] * g[i - j]);
479 fg[i] = result;
480 }
481
482 for (i = p + p - 2; i >= p; --i)
483 {
484 fg[i - p] = F3_freeze (fg[i - p] + fg[i]);
485 fg[i - p + 1] = F3_freeze (fg[i - p + 1] + fg[i]);
486 }
487
488 for (i = 0; i < p; ++i)
489 h[i] = fg[i];
490 }
491
492 /* returns 0 if recip succeeded; else -1 */
493 static int
494 R3_recip (small * out, const small * in)
495 {
496 small f[p + 1], g[p + 1], v[p + 1], r[p + 1];
497 int i, loop, delta;
498 int sign, swap, t;
499
500 for (i = 0; i < p + 1; ++i)
501 v[i] = 0;
502 for (i = 0; i < p + 1; ++i)
503 r[i] = 0;
504 r[0] = 1;
505 for (i = 0; i < p; ++i)
506 f[i] = 0;
507 f[0] = 1;
508 f[p - 1] = f[p] = -1;
509 for (i = 0; i < p; ++i)
510 g[p - 1 - i] = in[i];
511 g[p] = 0;
512
513 delta = 1;
514
515 for (loop = 0; loop < 2 * p - 1; ++loop)
516 {
517 for (i = p; i > 0; --i)
518 v[i] = v[i - 1];
519 v[0] = 0;
520
521 sign = -g[0] * f[0];
522 swap = int16_t_negative_mask (-delta) & int16_t_nonzero_mask (g[0]);
523 delta ^= swap & (delta ^ -delta);
524 delta += 1;
525
526 for (i = 0; i < p + 1; ++i)
527 {
528 t = swap & (f[i] ^ g[i]);
529 f[i] ^= t;
530 g[i] ^= t;
531 t = swap & (v[i] ^ r[i]);
532 v[i] ^= t;
533 r[i] ^= t;
534 }
535
536 for (i = 0; i < p + 1; ++i)
537 g[i] = F3_freeze (g[i] + sign * f[i]);
538 for (i = 0; i < p + 1; ++i)
539 r[i] = F3_freeze (r[i] + sign * v[i]);
540
541 for (i = 0; i < p; ++i)
542 g[i] = g[i + 1];
543 g[p] = 0;
544 }
545
546 sign = f[0];
547 for (i = 0; i < p; ++i)
548 out[i] = sign * v[p - 1 - i];
549
550 return int16_t_nonzero_mask (delta);
551 }
552
553 /* ----- polynomials mod q */
554
555 /* h = f*g in the ring Rq */
556 static void
557 Rq_mult_small (Fq * h, const Fq * f, const small * g)
558 {
559 Fq fg[p + p - 1];
560 Fq result;
561 int i, j;
562
563 for (i = 0; i < p; ++i)
564 {
565 result = 0;
566 for (j = 0; j <= i; ++j)
567 result = Fq_freeze (result + f[j] * (int32_t) g[i - j]);
568 fg[i] = result;
569 }
570 for (i = p; i < p + p - 1; ++i)
571 {
572 result = 0;
573 for (j = i - p + 1; j < p; ++j)
574 result = Fq_freeze (result + f[j] * (int32_t) g[i - j]);
575 fg[i] = result;
576 }
577
578 for (i = p + p - 2; i >= p; --i)
579 {
580 fg[i - p] = Fq_freeze (fg[i - p] + fg[i]);
581 fg[i - p + 1] = Fq_freeze (fg[i - p + 1] + fg[i]);
582 }
583
584 for (i = 0; i < p; ++i)
585 h[i] = fg[i];
586 }
587
588 /* h = 3f in Rq */
589 static void
590 Rq_mult3 (Fq * h, const Fq * f)
591 {
592 int i;
593
594 for (i = 0; i < p; ++i)
595 h[i] = Fq_freeze (3 * f[i]);
596 }
597
598 /* out = 1/(3*in) in Rq */
599 /* returns 0 if recip succeeded; else -1 */
600 static int
601 Rq_recip3 (Fq * out, const small * in)
602 {
603 Fq f[p + 1], g[p + 1], v[p + 1], r[p + 1];
604 int i, loop, delta;
605 int swap, t;
606 int32_t f0, g0;
607 Fq scale;
608
609 for (i = 0; i < p + 1; ++i)
610 v[i] = 0;
611 for (i = 0; i < p + 1; ++i)
612 r[i] = 0;
613 r[0] = Fq_recip (3);
614 for (i = 0; i < p; ++i)
615 f[i] = 0;
616 f[0] = 1;
617 f[p - 1] = f[p] = -1;
618 for (i = 0; i < p; ++i)
619 g[p - 1 - i] = in[i];
620 g[p] = 0;
621
622 delta = 1;
623
624 for (loop = 0; loop < 2 * p - 1; ++loop)
625 {
626 for (i = p; i > 0; --i)
627 v[i] = v[i - 1];
628 v[0] = 0;
629
630 swap = int16_t_negative_mask (-delta) & int16_t_nonzero_mask (g[0]);
631 delta ^= swap & (delta ^ -delta);
632 delta += 1;
633
634 for (i = 0; i < p + 1; ++i)
635 {
636 t = swap & (f[i] ^ g[i]);
637 f[i] ^= t;
638 g[i] ^= t;
639 t = swap & (v[i] ^ r[i]);
640 v[i] ^= t;
641 r[i] ^= t;
642 }
643
644 f0 = f[0];
645 g0 = g[0];
646 for (i = 0; i < p + 1; ++i)
647 g[i] = Fq_freeze (f0 * g[i] - g0 * f[i]);
648 for (i = 0; i < p + 1; ++i)
649 r[i] = Fq_freeze (f0 * r[i] - g0 * v[i]);
650
651 for (i = 0; i < p; ++i)
652 g[i] = g[i + 1];
653 g[p] = 0;
654 }
655
656 scale = Fq_recip (f[0]);
657 for (i = 0; i < p; ++i)
658 out[i] = Fq_freeze (scale * (int32_t) v[p - 1 - i]);
659
660 return int16_t_nonzero_mask (delta);
661 }
662
663 /* ----- rounded polynomials mod q */
664
665 static void
666 Round (Fq * out, const Fq * a)
667 {
668 int i;
669 for (i = 0; i < p; ++i)
670 out[i] = a[i] - F3_freeze (a[i]);
671 }
672
673 /* ----- sorting to generate short polynomial */
674
675 static void
676 Short_fromlist (small * out, const uint32_t * in)
677 {
678 uint32_t L[p];
679 int i;
680
681 for (i = 0; i < w; ++i)
682 L[i] = in[i] & (uint32_t) - 2;
683 for (i = w; i < p; ++i)
684 L[i] = (in[i] & (uint32_t) - 3) | 1;
685 crypto_sort_uint32 (L, p);
686 for (i = 0; i < p; ++i)
687 out[i] = (L[i] & 3) - 1;
688 }
689
690 /* ----- underlying hash function */
691
692 #define Hash_bytes 32
693
694 /* e.g., b = 0 means out = Hash0(in) */
695 static void
696 Hash_prefix (unsigned char *out, int b, const unsigned char *in, int inlen)
697 {
698 unsigned char x[inlen + 1];
699 unsigned char h[64];
700 int i;
701
702 x[0] = b;
703 for (i = 0; i < inlen; ++i)
704 x[i + 1] = in[i];
705 crypto_hash_sha512 (h, x, inlen + 1);
706 for (i = 0; i < 32; ++i)
707 out[i] = h[i];
708 }
709
710 /* ----- higher-level randomness */
711
712 static uint32_t
713 urandom32 (void *random_ctx, sntrup761_random_func * random)
714 {
715 unsigned char c[4];
716 uint32_t out[4];
717
718 random (random_ctx, 4, c);
719 out[0] = (uint32_t) c[0];
720 out[1] = ((uint32_t) c[1]) << 8;
721 out[2] = ((uint32_t) c[2]) << 16;
722 out[3] = ((uint32_t) c[3]) << 24;
723 return out[0] + out[1] + out[2] + out[3];
724 }
725
726 static void
727 Short_random (small * out, void *random_ctx, sntrup761_random_func * random)
728 {
729 uint32_t L[p];
730 int i;
731
732 for (i = 0; i < p; ++i)
733 L[i] = urandom32 (random_ctx, random);
734 Short_fromlist (out, L);
735 }
736
737 static void
738 Small_random (small * out, void *random_ctx, sntrup761_random_func * random)
739 {
740 int i;
741
742 for (i = 0; i < p; ++i)
743 out[i] = (((urandom32 (random_ctx, random) & 0x3fffffff) * 3) >> 30) - 1;
744 }
745
746 /* ----- Streamlined NTRU Prime Core */
747
748 /* h,(f,ginv) = KeyGen() */
749 static void
750 KeyGen (Fq * h, small * f, small * ginv, void *random_ctx,
751 sntrup761_random_func * random)
752 {
753 small g[p];
754 Fq finv[p];
755
756 for (;;)
757 {
758 Small_random (g, random_ctx, random);
759 if (R3_recip (ginv, g) == 0)
760 break;
761 }
762 Short_random (f, random_ctx, random);
763 Rq_recip3 (finv, f); /* always works */
764 Rq_mult_small (h, finv, g);
765 }
766
767 /* c = Encrypt(r,h) */
768 static void
769 Encrypt (Fq * c, const small * r, const Fq * h)
770 {
771 Fq hr[p];
772
773 Rq_mult_small (hr, h, r);
774 Round (c, hr);
775 }
776
777 /* r = Decrypt(c,(f,ginv)) */
778 static void
779 Decrypt (small * r, const Fq * c, const small * f, const small * ginv)
780 {
781 Fq cf[p];
782 Fq cf3[p];
783 small e[p];
784 small ev[p];
785 int mask;
786 int i;
787
788 Rq_mult_small (cf, c, f);
789 Rq_mult3 (cf3, cf);
790 R3_fromRq (e, cf3);
791 R3_mult (ev, e, ginv);
792
793 mask = Weightw_mask (ev); /* 0 if weight w, else -1 */
794 for (i = 0; i < w; ++i)
795 r[i] = ((ev[i] ^ 1) & ~mask) ^ 1;
796 for (i = w; i < p; ++i)
797 r[i] = ev[i] & ~mask;
798 }
799
800 /* ----- encoding small polynomials (including short polynomials) */
801
802 #define Small_bytes ((p+3)/4)
803
804 /* these are the only functions that rely on p mod 4 = 1 */
805
806 static void
807 Small_encode (unsigned char *s, const small * f)
808 {
809 small x;
810 int i;
811
812 for (i = 0; i < p / 4; ++i)
813 {
814 x = *f++ + 1;
815 x += (*f++ + 1) << 2;
816 x += (*f++ + 1) << 4;
817 x += (*f++ + 1) << 6;
818 *s++ = x;
819 }
820 x = *f++ + 1;
821 *s++ = x;
822 }
823
824 static void
825 Small_decode (small * f, const unsigned char *s)
826 {
827 unsigned char x;
828 int i;
829
830 for (i = 0; i < p / 4; ++i)
831 {
832 x = *s++;
833 *f++ = ((small) (x & 3)) - 1;
834 x >>= 2;
835 *f++ = ((small) (x & 3)) - 1;
836 x >>= 2;
837 *f++ = ((small) (x & 3)) - 1;
838 x >>= 2;
839 *f++ = ((small) (x & 3)) - 1;
840 }
841 x = *s++;
842 *f++ = ((small) (x & 3)) - 1;
843 }
844
845 /* ----- encoding general polynomials */
846
847 static void
848 Rq_encode (unsigned char *s, const Fq * r)
849 {
850 uint16_t R[p], M[p];
851 int i;
852
853 for (i = 0; i < p; ++i)
854 R[i] = r[i] + q12;
855 for (i = 0; i < p; ++i)
856 M[i] = q;
857 Encode (s, R, M, p);
858 }
859
860 static void
861 Rq_decode (Fq * r, const unsigned char *s)
862 {
863 uint16_t R[p], M[p];
864 int i;
865
866 for (i = 0; i < p; ++i)
867 M[i] = q;
868 Decode (R, s, M, p);
869 for (i = 0; i < p; ++i)
870 r[i] = ((Fq) R[i]) - q12;
871 }
872
873 /* ----- encoding rounded polynomials */
874
875 static void
876 Rounded_encode (unsigned char *s, const Fq * r)
877 {
878 uint16_t R[p], M[p];
879 int i;
880
881 for (i = 0; i < p; ++i)
882 R[i] = ((r[i] + q12) * 10923) >> 15;
883 for (i = 0; i < p; ++i)
884 M[i] = (q + 2) / 3;
885 Encode (s, R, M, p);
886 }
887
888 static void
889 Rounded_decode (Fq * r, const unsigned char *s)
890 {
891 uint16_t R[p], M[p];
892 int i;
893
894 for (i = 0; i < p; ++i)
895 M[i] = (q + 2) / 3;
896 Decode (R, s, M, p);
897 for (i = 0; i < p; ++i)
898 r[i] = R[i] * 3 - q12;
899 }
900
901 /* ----- Streamlined NTRU Prime Core plus encoding */
902
903 typedef small Inputs[p]; /* passed by reference */
904 #define Inputs_random Short_random
905 #define Inputs_encode Small_encode
906 #define Inputs_bytes Small_bytes
907
908 #define Ciphertexts_bytes Rounded_bytes
909 #define SecretKeys_bytes (2*Small_bytes)
910 #define PublicKeys_bytes Rq_bytes
911
912 /* pk,sk = ZKeyGen() */
913 static void
914 ZKeyGen (unsigned char *pk, unsigned char *sk, void *random_ctx,
915 sntrup761_random_func * random)
916 {
917 Fq h[p];
918 small f[p], v[p];
919
920 KeyGen (h, f, v, random_ctx, random);
921 Rq_encode (pk, h);
922 Small_encode (sk, f);
923 sk += Small_bytes;
924 Small_encode (sk, v);
925 }
926
927 /* C = ZEncrypt(r,pk) */
928 static void
929 ZEncrypt (unsigned char *C, const Inputs r, const unsigned char *pk)
930 {
931 Fq h[p];
932 Fq c[p];
933 Rq_decode (h, pk);
934 Encrypt (c, r, h);
935 Rounded_encode (C, c);
936 }
937
938 /* r = ZDecrypt(C,sk) */
939 static void
940 ZDecrypt (Inputs r, const unsigned char *C, const unsigned char *sk)
941 {
942 small f[p], v[p];
943 Fq c[p];
944
945 Small_decode (f, sk);
946 sk += Small_bytes;
947 Small_decode (v, sk);
948 Rounded_decode (c, C);
949 Decrypt (r, c, f, v);
950 }
951
952 /* ----- confirmation hash */
953
954 #define Confirm_bytes 32
955
956 /* h = HashConfirm(r,pk,cache); cache is Hash4(pk) */
957 static void
958 HashConfirm (unsigned char *h, const unsigned char *r,
959 /* const unsigned char *pk, */ const unsigned char *cache)
960 {
961 unsigned char x[Hash_bytes * 2];
962 int i;
963
964 Hash_prefix (x, 3, r, Inputs_bytes);
965 for (i = 0; i < Hash_bytes; ++i)
966 x[Hash_bytes + i] = cache[i];
967 Hash_prefix (h, 2, x, sizeof x);
968 }
969
970 /* ----- session-key hash */
971
972 /* k = HashSession(b,y,z) */
973 static void
974 HashSession (unsigned char *k, int b, const unsigned char *y,
975 const unsigned char *z)
976 {
977 unsigned char x[Hash_bytes + Ciphertexts_bytes + Confirm_bytes];
978 int i;
979
980 Hash_prefix (x, 3, y, Inputs_bytes);
981 for (i = 0; i < Ciphertexts_bytes + Confirm_bytes; ++i)
982 x[Hash_bytes + i] = z[i];
983 Hash_prefix (k, b, x, sizeof x);
984 }
985
986 /* ----- Streamlined NTRU Prime */
987
988 /* pk,sk = KEM_KeyGen() */
989 void
990 sntrup761_keypair (unsigned char *pk, unsigned char *sk, void *random_ctx,
991 sntrup761_random_func * random)
992 {
993 int i;
994
995 ZKeyGen (pk, sk, random_ctx, random);
996 sk += SecretKeys_bytes;
997 for (i = 0; i < PublicKeys_bytes; ++i)
998 *sk++ = pk[i];
999 random (random_ctx, Inputs_bytes, sk);
1000 sk += Inputs_bytes;
1001 Hash_prefix (sk, 4, pk, PublicKeys_bytes);
1002 }
1003
1004 /* c,r_enc = Hide(r,pk,cache); cache is Hash4(pk) */
1005 static void
1006 Hide (unsigned char *c, unsigned char *r_enc, const Inputs r,
1007 const unsigned char *pk, const unsigned char *cache)
1008 {
1009 Inputs_encode (r_enc, r);
1010 ZEncrypt (c, r, pk);
1011 c += Ciphertexts_bytes;
1012 HashConfirm (c, r_enc, cache);
1013 }
1014
1015 /* c,k = Encap(pk) */
1016 void
1017 sntrup761_enc (unsigned char *c, unsigned char *k, const unsigned char *pk,
1018 void *random_ctx, sntrup761_random_func * random)
1019 {
1020 Inputs r;
1021 unsigned char r_enc[Inputs_bytes];
1022 unsigned char cache[Hash_bytes];
1023
1024 Hash_prefix (cache, 4, pk, PublicKeys_bytes);
1025 Inputs_random (r, random_ctx, random);
1026 Hide (c, r_enc, r, pk, cache);
1027 HashSession (k, 1, r_enc, c);
1028 }
1029
1030 /* 0 if matching ciphertext+confirm, else -1 */
1031 static int
1032 Ciphertexts_diff_mask (const unsigned char *c, const unsigned char *c2)
1033 {
1034 int len = Ciphertexts_bytes + Confirm_bytes;
1035 return ct_ulong_gen_mask(_gcry_ct_not_memequal(c, c2, len));
1036 }
1037
1038 /* k = Decap(c,sk) */
1039 void
1040 sntrup761_dec (unsigned char *k, const unsigned char *c, const unsigned char *sk)
1041 {
1042 const unsigned char *pk = sk + SecretKeys_bytes;
1043 const unsigned char *rho = pk + PublicKeys_bytes;
1044 const unsigned char *cache = rho + Inputs_bytes;
1045 Inputs r;
1046 unsigned char r_enc[Inputs_bytes];
1047 unsigned char tmp[Inputs_bytes];
1048 unsigned char cnew[Ciphertexts_bytes + Confirm_bytes];
1049 int mask;
1050 int i;
1051
1052 ZDecrypt (r, c, sk);
1053 Hide (cnew, r_enc, r, pk, cache);
1054 mask = Ciphertexts_diff_mask (c, cnew);
1055 for (i = 0; i < Inputs_bytes; ++i)
1056 tmp[i] = r_enc[i] ^ rho[i];
1057 _gcry_ct_memmov_cond (r_enc, tmp, Inputs_bytes, mask & 1);
1058 HashSession (k, 1 + mask, r_enc, c);
1059 }