64f0b063d6e82ecfc170f75c2168c19e9f3cb85c
[strongswan.git] / src / libstrongswan / plugins / bliss / bliss_private_key.c
1 /*
2 * Copyright (C) 2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "bliss_private_key.h"
17 #include "bliss_public_key.h"
18 #include "bliss_param_set.h"
19 #include "bliss_utils.h"
20 #include "bliss_sampler.h"
21 #include "bliss_signature.h"
22 #include "bliss_bitpacker.h"
23 #include "bliss_fft.h"
24
25 #include <crypto/mgf1/mgf1_bitspender.h>
26 #include <asn1/asn1.h>
27 #include <asn1/asn1_parser.h>
28 #include <asn1/oid.h>
29
30 #define _GNU_SOURCE
31 #include <stdlib.h>
32
33 typedef struct private_bliss_private_key_t private_bliss_private_key_t;
34
35 #define SECRET_KEY_TRIALS_MAX 50
36
37 /**
38 * Private data of a bliss_private_key_t object.
39 */
40 struct private_bliss_private_key_t {
41 /**
42 * Public interface for this signer.
43 */
44 bliss_private_key_t public;
45
46 /**
47 * BLISS signature parameter set
48 */
49 bliss_param_set_t *set;
50
51 /**
52 * BLISS secret key S1 (coefficients of polynomial f)
53 */
54 int8_t *s1;
55
56 /**
57 * BLISS secret key S2 (coefficients of polynomial 2g + 1)
58 */
59 int8_t *s2;
60
61 /**
62 * NTT of BLISS public key a (coefficients of polynomial (2g + 1)/f)
63 */
64 uint32_t *A;
65
66 /**
67 * reference count
68 */
69 refcount_t ref;
70 };
71
72 METHOD(private_key_t, get_type, key_type_t,
73 private_bliss_private_key_t *this)
74 {
75 return KEY_BLISS;
76 }
77
78 /**
79 * Multiply secret vector s with binary challenge vector c
80 */
81 static void multiply_by_c(int8_t *s, int n, uint16_t *c_indices,
82 uint16_t kappa, int32_t *product)
83 {
84 int i, j, index;
85
86 for (i = 0; i < n; i++)
87 {
88 product[i] = 0;
89
90 for (j = 0; j < kappa; j++)
91 {
92 index = c_indices[j];
93 if (i - index < 0)
94 {
95 product[i] -= s[i - index + n];
96 }
97 else
98 {
99 product[i] += s[i - index];
100 }
101 }
102 }
103 }
104
105 /**
106 * BLISS-B GreedySC algorithm
107 */
108 static void greedy_sc(int8_t *s1, int8_t *s2, int n, uint16_t *c_indices,
109 uint16_t kappa, int32_t *v1, int32_t *v2)
110 {
111 int i, j, index;
112 int32_t sign;
113
114 for (i = 0; i < n; i++)
115 {
116 v1[i] = v2[i] = 0;
117 }
118 for (j = 0; j < kappa; j++)
119 {
120 index = c_indices[j];
121 sign = 0;
122
123 for (i = 0; i < index; i++)
124 {
125 sign -= (v1[i] * s1[i - index + n] + v2[i] * s2[i - index + n]);
126 }
127 for (i = index; i < n; i++)
128 {
129 sign += (v1[i] * s1[i - index] + v2[i] * s2[i - index]);
130 }
131 for (i = 0; i < index; i++)
132 {
133 if (sign > 0)
134 {
135 v1[i] += s1[i - index + n];
136 v2[i] += s2[i - index + n];
137 }
138 else
139 {
140 v1[i] -= s1[i - index + n];
141 v2[i] -= s2[i - index + n];
142 }
143 }
144 for (i = index; i < n; i++)
145 {
146 if (sign > 0)
147 {
148 v1[i] -= s1[i - index];
149 v2[i] -= s2[i - index];
150 }
151 else
152 {
153 v1[i] += s1[i - index];
154 v2[i] += s2[i - index];
155 }
156 }
157 }
158 }
159
160 /**
161 * Compute a BLISS signature based on a SHA-512 hash
162 */
163 static bool sign_bliss_with_sha512(private_bliss_private_key_t *this,
164 chunk_t data, chunk_t *signature)
165 {
166 rng_t *rng;
167 hash_algorithm_t alg;
168 hasher_t *hasher;
169 bliss_fft_t *fft;
170 bliss_signature_t *sig;
171 bliss_sampler_t *sampler = NULL;
172 uint8_t seed_buf[32], data_hash_buf[HASH_SIZE_SHA512];
173 uint16_t q, q2, p, p2, *c_indices, tests = 0;
174 uint32_t *ay;
175 int32_t *y1, *y2, *z1, *z2, *u, *s1c, *s2c;
176 int32_t y1_min = 0, y1i, y1_max = 0, y2_min = 0, y2i, y2_max = 0;
177 int32_t scalar, norm, ui;
178 int16_t *ud, *uz2d, *z2d, value;
179 int i, n;
180 size_t seed_len;
181 double mean1 = 0, mean2 = 0, sigma1 = 0, sigma2 = 0;
182 chunk_t seed;
183 chunk_t data_hash = { data_hash_buf, sizeof(data_hash_buf) };
184 bool accepted, positive, success = FALSE, use_bliss_b;
185
186 /* Initialize signature */
187 *signature = chunk_empty;
188
189 /* Create data hash */
190 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA512);
191 if (!hasher)
192 {
193 return FALSE;
194 }
195 if (!hasher->get_hash(hasher, data, data_hash_buf))
196 {
197 hasher->destroy(hasher);
198 return FALSE;
199 }
200
201 /* Set MGF1 hash algorithm and seed length based on security strength */
202 if (this->set->strength > 160)
203 {
204 alg = HASH_SHA256;
205 seed_len = HASH_SIZE_SHA256;
206 }
207 else
208 {
209 alg = HASH_SHA1;
210 seed_len = HASH_SIZE_SHA1;
211 }
212 seed = chunk_create(seed_buf, seed_len);
213
214 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
215 if (!rng)
216 {
217 hasher->destroy(hasher);
218 return FALSE;
219 }
220
221 /* Initialize a couple of needed variables */
222 n = this->set->n;
223 q = this->set->q;
224 p = this->set->p;
225 q2 = 2 * q;
226 p2 = p / 2;
227 ay = malloc(n * sizeof(uint32_t));
228 z2 = malloc(n * sizeof(int32_t));
229 s1c = malloc(n * sizeof(int32_t));
230 s2c = malloc(n * sizeof(int32_t));
231 u = malloc(n * sizeof(int32_t));
232 uz2d = malloc(n * sizeof(int16_t));
233
234 sig = bliss_signature_create(this->set);
235 sig->get_parameters(sig, &z1, &z2d, &c_indices);
236 y1 = z1;
237 y2 = z2;
238 ud = z2d;
239
240 fft = bliss_fft_create(this->set->fft_params);
241
242 /* Use of the enhanced BLISS-B signature algorithm? */
243 switch (this->set->id)
244 {
245 case BLISS_I:
246 case BLISS_II:
247 case BLISS_III:
248 case BLISS_IV:
249 use_bliss_b = FALSE;
250 break;
251 case BLISS_B_I:
252 case BLISS_B_II:
253 case BLISS_B_III:
254 case BLISS_B_IV:
255 use_bliss_b = TRUE;
256 break;
257 }
258
259 while (true)
260 {
261 tests++;
262
263 if (!rng->get_bytes(rng, seed_len, seed_buf))
264 {
265 goto end;
266 }
267 DESTROY_IF(sampler);
268
269 sampler = bliss_sampler_create(alg, seed, this->set);
270 if (!sampler)
271 {
272 goto end;
273 }
274
275 /* Gaussian sampling for vectors y1 and y2 */
276 for (i = 0; i < n; i++)
277 {
278 if (!sampler->gaussian(sampler, &y1i) ||
279 !sampler->gaussian(sampler, &y2i))
280 {
281 goto end;
282 }
283 y1[i] = y1i;
284 y2[i] = y2i;
285
286 /* Collect statistical data on rejection sampling */
287 if (i == 0)
288 {
289 y1_min = y1_max = y1i;
290 y2_min = y2_max = y2i;
291 }
292 else
293 {
294 if (y1i < y1_min)
295 {
296 y1_min = y1i;
297 }
298 else if (y1i > y1_max)
299 {
300 y1_max = y1i;
301 }
302 if (y2i < y2_min)
303 {
304 y2_min = y2i;
305 }
306 else if (y2i > y2_max)
307 {
308 y2_max = y2i;
309 }
310 }
311 mean1 += y1i;
312 mean2 += y2i;
313 sigma1 += y1i * y1i;
314 sigma2 += y2i * y2i;
315
316 ay[i] = y1i < 0 ? q + y1i : y1i;
317 }
318
319 /* Compute statistics on vectors y1 and y2 */
320 mean1 /= n;
321 mean2 /= n;
322 sigma1 /= n;
323 sigma2 /= n;
324 sigma2 -= mean1 * mean1;
325 sigma2 -= mean2 * mean2;
326 DBG2(DBG_LIB, "y1 = %d..%d (sigma2 = %5.0f, mean = %4.1f)",
327 y1_min, y1_max, sigma1, mean1);
328 DBG2(DBG_LIB, "y2 = %d..%d (sigma2 = %5.0f, mean = %4.1f)",
329 y2_min, y2_max, sigma2, mean2);
330
331 fft->transform(fft, ay, ay, FALSE);
332
333 for (i = 0; i < n; i++)
334 {
335 ay[i] = (this->A[i] * ay[i]) % q;
336 }
337 fft->transform(fft, ay, ay, TRUE);
338
339 for (i = 0; i < n; i++)
340 {
341 ui = 2 * this->set->q2_inv * (int32_t)ay[i] + y2[i];
342 u[i] = ((ui < 0) ? q2 + ui : ui) % q2;
343 }
344 bliss_utils_round_and_drop(this->set, u, ud);
345
346 /* Detailed debugging information */
347 DBG3(DBG_LIB, " i u[i] ud[i]");
348 for (i = 0; i < n; i++)
349 {
350 DBG3(DBG_LIB, "%3d %6d %4d", i, u[i], ud[i]);
351 }
352
353 if (!bliss_utils_generate_c(hasher, data_hash, ud, n, this->set->kappa,
354 c_indices))
355 {
356 goto end;
357 }
358
359 if (use_bliss_b)
360 {
361 /* Compute v = (s1c, s2c) with the GreedySC algorithm */
362 greedy_sc(this->s1, this->s2, n, c_indices, this->set->kappa,
363 s1c, s2c);
364
365 /* Compute norm = ||v||^2 = ||Sc'||^2 */
366 norm = bliss_utils_scalar_product(s1c, s1c, n) +
367 bliss_utils_scalar_product(s2c, s2c, n);
368
369 /* Just in case. ||v||^2 <= P_max should always be fulfilled */
370 if (norm > this->set->p_max)
371 {
372 goto end;
373 }
374 }
375 else
376 {
377 /* Compute s*c */
378 multiply_by_c(this->s1, n, c_indices, this->set->kappa, s1c);
379 multiply_by_c(this->s2, n, c_indices, this->set->kappa, s2c);
380
381 /* Compute norm = |Sc||^2 */
382 norm = bliss_utils_scalar_product(s1c, s1c, n) +
383 bliss_utils_scalar_product(s2c, s2c, n);
384 }
385
386 if (!sampler->bernoulli_exp(sampler, this->set->M - norm, &accepted))
387 {
388 goto end;
389 }
390 if (use_bliss_b)
391 {
392 DBG2(DBG_LIB, "norm2(s1*c') + norm2(s2*c') = %u (%u max), %s",
393 norm, this->set->p_max, accepted ? "accepted" : "rejected");
394
395 }
396 else
397 {
398 DBG2(DBG_LIB, "norm2(s1*c) + norm2(s2*c) = %u, %s",
399 norm, accepted ? "accepted" : "rejected");
400 }
401 if (!accepted)
402 {
403 continue;
404 }
405
406 /* Compute z */
407 if (!sampler->sign(sampler, &positive))
408 {
409 goto end;
410 }
411 for (i = 0; i < n; i++)
412 {
413 if (positive)
414 {
415 z1[i] = y1[i] + s1c[i];
416 z2[i] = y2[i] + s2c[i];
417 }
418 else
419 {
420 z1[i] = y1[i] - s1c[i];
421 z2[i] = y2[i] - s2c[i];
422 }
423 }
424 /* Reject with probability 1/cosh(scalar/sigma^2) */
425 scalar = bliss_utils_scalar_product(z1, s1c, n) +
426 bliss_utils_scalar_product(z2, s2c, n);
427
428 if (!sampler->bernoulli_cosh(sampler, scalar, &accepted))
429 {
430 goto end;
431 }
432 DBG2(DBG_LIB, "scalar(z1,s1*c) + scalar(z2,s2*c) = %d, %s",
433 scalar, accepted ? "accepted" : "rejected");
434 if (!accepted)
435 {
436 continue;
437 }
438
439 /* Compute z2 with dropped bits */
440 for (i = 0; i < n; i++)
441 {
442 u[i] -= z2[i];
443 if (u[i] < 0)
444 {
445 u[i] += q2;
446 }
447 else if (u[i] >= q2)
448 {
449 u[i] -= q2;
450 }
451 }
452 bliss_utils_round_and_drop(this->set, u, uz2d);
453
454 for (i = 0; i < n; i++)
455 {
456 value = ud[i] - uz2d[i];
457 if (value <= -p2)
458 {
459 value += p;
460 }
461 else if (value > p2)
462 {
463 value -= p;
464 }
465 z2d[i] = value;
466 }
467
468 if (!bliss_utils_check_norms(this->set, z1, z2d))
469 {
470 continue;
471 }
472
473 *signature = sig->get_encoding(sig);
474 if (signature->len == 0)
475 {
476 DBG1(DBG_LIB, "inefficient Huffman coding of signature");
477 continue;
478 }
479 DBG2(DBG_LIB, "signature generation needed %u round%s", tests,
480 (tests == 1) ? "" : "s");
481 break;
482 }
483 success = TRUE;
484
485 end:
486 /* cleanup */
487 DESTROY_IF(sampler);
488 hasher->destroy(hasher);
489 sig->destroy(sig);
490 fft->destroy(fft);
491 rng->destroy(rng);
492 memwipe(s1c, n * sizeof(int32_t));
493 memwipe(s2c, n * sizeof(int32_t));
494 free(s1c);
495 free(s2c);
496 free(ay);
497 free(z2);
498 free(u);
499 free(uz2d);
500
501 return success;
502 }
503
504 METHOD(private_key_t, sign, bool,
505 private_bliss_private_key_t *this, signature_scheme_t scheme,
506 chunk_t data, chunk_t *signature)
507 {
508 switch (scheme)
509 {
510 case SIGN_BLISS_WITH_SHA512:
511 return sign_bliss_with_sha512(this, data, signature);
512 default:
513 DBG1(DBG_LIB, "signature scheme %N not supported with BLISS",
514 signature_scheme_names, scheme);
515 return FALSE;
516 }
517 }
518
519 METHOD(private_key_t, decrypt, bool,
520 private_bliss_private_key_t *this, encryption_scheme_t scheme,
521 chunk_t crypto, chunk_t *plain)
522 {
523 DBG1(DBG_LIB, "encryption scheme %N not supported",
524 encryption_scheme_names, scheme);
525 return FALSE;
526 }
527
528 METHOD(private_key_t, get_keysize, int,
529 private_bliss_private_key_t *this)
530 {
531 return this->set->strength;
532 }
533
534 METHOD(private_key_t, get_public_key, public_key_t*,
535 private_bliss_private_key_t *this)
536 {
537 public_key_t *public;
538 chunk_t pubkey;
539
540 pubkey = bliss_public_key_info_encode(this->set->oid, this->A, this->set);
541 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_BLISS,
542 BUILD_BLOB_ASN1_DER, pubkey, BUILD_END);
543 free(pubkey.ptr);
544
545 return public;
546 }
547
548 METHOD(private_key_t, get_encoding, bool,
549 private_bliss_private_key_t *this, cred_encoding_type_t type,
550 chunk_t *encoding)
551 {
552 switch (type)
553 {
554 case PRIVKEY_ASN1_DER:
555 case PRIVKEY_PEM:
556 {
557 chunk_t s1, s2, pubkey;
558 bliss_bitpacker_t *packer;
559 size_t s_bits;
560 int8_t value;
561 bool success = TRUE;
562 int i;
563
564 pubkey = bliss_public_key_encode(this->A, this->set);
565
566 /* Use either 2 or 3 bits per array element */
567 s_bits = 2 + (this->set->non_zero2 > 0);
568
569 /* Encode secret s1 */
570 packer = bliss_bitpacker_create(s_bits * this->set->n);
571 for (i = 0; i < this->set->n; i++)
572 {
573 packer->write_bits(packer, this->s1[i], s_bits);
574 }
575 s1 = packer->extract_buf(packer);
576 packer->destroy(packer);
577
578 /* Encode secret s2 */
579 packer = bliss_bitpacker_create(s_bits * this->set->n);
580 for (i = 0; i < this->set->n; i++)
581 {
582 value = this->s2[i];
583 if (i == 0)
584 {
585 value -= 1;
586 }
587 value /= 2;
588 packer->write_bits(packer, value, s_bits);
589 }
590 s2 = packer->extract_buf(packer);
591 packer->destroy(packer);
592
593 *encoding = asn1_wrap(ASN1_SEQUENCE, "mmss",
594 asn1_build_known_oid(this->set->oid),
595 asn1_bitstring("m", pubkey),
596 asn1_bitstring("m", s1),
597 asn1_bitstring("m", s2)
598 );
599 if (type == PRIVKEY_PEM)
600 {
601 chunk_t asn1_encoding = *encoding;
602
603 success = lib->encoding->encode(lib->encoding, PRIVKEY_PEM,
604 NULL, encoding, CRED_PART_BLISS_PRIV_ASN1_DER,
605 asn1_encoding, CRED_PART_END);
606 chunk_clear(&asn1_encoding);
607 }
608 return success;
609 }
610 default:
611 return FALSE;
612 }
613 }
614
615 METHOD(private_key_t, get_fingerprint, bool,
616 private_bliss_private_key_t *this, cred_encoding_type_t type, chunk_t *fp)
617 {
618 bool success;
619
620 if (lib->encoding->get_cache(lib->encoding, type, this, fp))
621 {
622 return TRUE;
623 }
624 success = bliss_public_key_fingerprint(this->set->oid, this->A,
625 this->set, type, fp);
626 if (success)
627 {
628 lib->encoding->cache(lib->encoding, type, this, *fp);
629 }
630 return success;
631 }
632
633 METHOD(private_key_t, get_ref, private_key_t*,
634 private_bliss_private_key_t *this)
635 {
636 ref_get(&this->ref);
637 return &this->public.key;
638 }
639
640 METHOD(private_key_t, destroy, void,
641 private_bliss_private_key_t *this)
642 {
643 if (ref_put(&this->ref))
644 {
645 lib->encoding->clear_cache(lib->encoding, this);
646 memwipe(this->s1, this->set->n * sizeof(int8_t));
647 memwipe(this->s2, this->set->n * sizeof(int8_t));
648 free(this->s1);
649 free(this->s2);
650 free(this->A);
651 free(this);
652 }
653 }
654
655 /**
656 * Internal generic constructor
657 */
658 static private_bliss_private_key_t *bliss_private_key_create_empty(void)
659 {
660 private_bliss_private_key_t *this;
661
662 INIT(this,
663 .public = {
664 .key = {
665 .get_type = _get_type,
666 .sign = _sign,
667 .decrypt = _decrypt,
668 .get_keysize = _get_keysize,
669 .get_public_key = _get_public_key,
670 .equals = private_key_equals,
671 .belongs_to = private_key_belongs_to,
672 .get_fingerprint = _get_fingerprint,
673 .has_fingerprint = private_key_has_fingerprint,
674 .get_encoding = _get_encoding,
675 .get_ref = _get_ref,
676 .destroy = _destroy,
677 },
678 },
679 .ref = 1,
680 );
681 return this;
682 }
683
684 /**
685 * Compute the scalar product of a vector x with a negative wrapped vector y
686 */
687 static int16_t wrapped_product(int8_t *x, int8_t *y, int n, int shift)
688 {
689 int16_t product = 0;
690 int i;
691
692 for (i = 0; i < n - shift; i++)
693 {
694 product += x[i] * y[i + shift];
695 }
696 for (i = n - shift; i < n; i++)
697 {
698 product -= x[i] * y[i + shift - n];
699 }
700 return product;
701 }
702
703 /**
704 * Apply a negative wrapped rotation to a vector x
705 */
706 static void wrap(int16_t *x, int n, int shift, int16_t *x_wrapped)
707 {
708 int i;
709
710 for (i = 0; i < n - shift; i++)
711 {
712 x_wrapped[i + shift] = x[i];
713 }
714 for (i = n - shift; i < n; i++)
715 {
716 x_wrapped[i + shift - n] = -x[i];
717 }
718 }
719
720 /**
721 * int16_t compare function needed for qsort()
722 */
723 static int compare(const int16_t *a, const int16_t *b)
724 {
725 int16_t temp = *a - *b;
726
727 if (temp > 0)
728 {
729 return 1;
730 }
731 else if (temp < 0)
732 {
733 return -1;
734 }
735 else
736 {
737 return 0;
738 }
739 }
740
741 /**
742 * Compute the Nk(S) norm of S = (s1, s2)
743 */
744 static uint32_t nks_norm(int8_t *s1, int8_t *s2, int n, uint16_t kappa)
745 {
746 int16_t t[n], t_wrapped[n], max_kappa[n];
747 uint32_t nks = 0;
748 int i, j;
749
750 for (i = 0; i < n; i++)
751 {
752 t[i] = wrapped_product(s1, s1, n, i) + wrapped_product(s2, s2, n, i);
753 }
754
755 for (i = 0; i < n; i++)
756 {
757 wrap(t, n, i, t_wrapped);
758 qsort(t_wrapped, n, sizeof(int16_t), (__compar_fn_t)compare);
759 max_kappa[i] = 0;
760
761 for (j = 1; j <= kappa; j++)
762 {
763 max_kappa[i] += t_wrapped[n - j];
764 }
765 }
766 qsort(max_kappa, n, sizeof(int16_t), (__compar_fn_t)compare);
767
768 for (i = 1; i <= kappa; i++)
769 {
770 nks += max_kappa[n - i];
771 }
772 return nks;
773 }
774
775 /**
776 * Compute the inverse x1 of x modulo q as x^(-1) = x^(q-2) mod q
777 */
778 static uint32_t invert(uint32_t x, uint16_t q)
779 {
780 uint32_t x1, x2;
781 uint16_t q2;
782 int i, i_max;
783
784 q2 = q - 2;
785 x1 = (q2 & 1) ? x : 1;
786 x2 = x;
787 i_max = 15;
788
789 while ((q2 & (1 << i_max)) == 0)
790 {
791 i_max--;
792 }
793 for (i = 1; i <= i_max; i++)
794 {
795 x2 = (x2 * x2) % q;
796
797 if (q2 & (1 << i))
798 {
799 x1 = (x1 * x2) % q;
800 }
801 }
802
803 return x1;
804 }
805
806 /**
807 * Create a vector with sparse and small coefficients from seed
808 */
809 static int8_t* create_vector_from_seed(private_bliss_private_key_t *this,
810 hash_algorithm_t alg, chunk_t seed)
811 {
812 mgf1_bitspender_t *bitspender;
813 uint32_t index, sign;
814 int8_t *vector;
815 int non_zero;
816
817 bitspender = mgf1_bitspender_create(alg, seed, FALSE);
818 if (!bitspender)
819 {
820 return NULL;
821 }
822
823 vector = malloc(sizeof(int8_t) * this->set->n);
824 memset(vector, 0x00, this->set->n);
825
826 non_zero = this->set->non_zero1;
827 while (non_zero)
828 {
829 if (!bitspender->get_bits(bitspender, this->set->n_bits, &index))
830 {
831 free(vector);
832 return NULL;
833 }
834 if (vector[index] != 0)
835 {
836 continue;
837 }
838
839 if (!bitspender->get_bits(bitspender, 1, &sign))
840 {
841 free(vector);
842 return NULL;
843 }
844 vector[index] = sign ? 1 : -1;
845 non_zero--;
846 }
847
848 non_zero = this->set->non_zero2;
849 while (non_zero)
850 {
851 if (!bitspender->get_bits(bitspender, this->set->n_bits, &index))
852 {
853 free(vector);
854 return NULL;
855 }
856 if (vector[index] != 0)
857 {
858 continue;
859 }
860
861 if (!bitspender->get_bits(bitspender, 1, &sign))
862 {
863 free(vector);
864 return NULL;
865 }
866 vector[index] = sign ? 2 : -2;
867 non_zero--;
868 }
869 bitspender->destroy(bitspender);
870
871 return vector;
872 }
873
874 /**
875 * Generate the secret key S = (s1, s2) fulfilling the Nk(S) norm
876 */
877 static bool create_secret(private_bliss_private_key_t *this, rng_t *rng,
878 int8_t **s1, int8_t **s2, int *trials)
879 {
880 uint8_t seed_buf[32];
881 uint8_t *f, *g;
882 uint32_t l2_norm, nks;
883 int i, n;
884 chunk_t seed;
885 size_t seed_len;
886 hash_algorithm_t alg;
887
888 n = this->set->n;
889 *s1 = NULL;
890 *s2 = NULL;
891
892 /* Set MGF1 hash algorithm and seed length based on security strength */
893 if (this->set->strength > 160)
894 {
895 alg = HASH_SHA256;
896 seed_len = HASH_SIZE_SHA256;
897 }
898 else
899 {
900 alg = HASH_SHA1;
901 seed_len = HASH_SIZE_SHA1;
902 }
903 seed = chunk_create(seed_buf, seed_len);
904
905 while (*trials < SECRET_KEY_TRIALS_MAX)
906 {
907 (*trials)++;
908
909 if (!rng->get_bytes(rng, seed_len, seed_buf))
910 {
911 return FALSE;
912 }
913 f = create_vector_from_seed(this, alg, seed);
914 if (f == NULL)
915 {
916 return FALSE;
917 }
918 if (!rng->get_bytes(rng, seed_len, seed_buf))
919 {
920 free(f);
921 return FALSE;
922 }
923 g = create_vector_from_seed(this, alg, seed);
924 if (g == NULL)
925 {
926 free(f);
927 return FALSE;
928 }
929
930 /* Compute 2g + 1 */
931 for (i = 0; i < n; i++)
932 {
933 g[i] *= 2;
934 }
935 g[0] += 1;
936
937 l2_norm = wrapped_product(f, f, n, 0) + wrapped_product(g, g, n, 0);
938 nks = nks_norm(f, g, n, this->set->kappa);
939
940 switch (this->set->id)
941 {
942 case BLISS_I:
943 case BLISS_II:
944 case BLISS_III:
945 case BLISS_IV:
946 DBG2(DBG_LIB, "l2 norm of s1||s2: %d, Nk(S): %u (%u max)",
947 l2_norm, nks, this->set->nks_max);
948 if (nks < this->set->nks_max)
949 {
950 *s1 = f;
951 *s2 = g;
952 return TRUE;
953 }
954 free(f);
955 free(g);
956 break;
957 case BLISS_B_I:
958 case BLISS_B_II:
959 case BLISS_B_III:
960 case BLISS_B_IV:
961 DBG2(DBG_LIB, "l2 norm of s1||s2: %d, Nk(S): %u",
962 l2_norm, nks);
963 *s1 = f;
964 *s2 = g;
965 return TRUE;
966 }
967 }
968
969 return FALSE;
970 }
971
972 /**
973 * See header.
974 */
975 bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
976 {
977 private_bliss_private_key_t *this;
978 u_int key_size = BLISS_B_I;
979 int i, n, trials = 0;
980 uint32_t *S1, *S2, *a;
981 uint16_t q;
982 bool success = FALSE;
983 bliss_param_set_t *set;
984 bliss_fft_t *fft;
985 rng_t *rng;
986
987 while (TRUE)
988 {
989 switch (va_arg(args, builder_part_t))
990 {
991 case BUILD_KEY_SIZE:
992 key_size = va_arg(args, u_int);
993 continue;
994 case BUILD_END:
995 break;
996 default:
997 return NULL;
998 }
999 break;
1000 }
1001
1002 if (lib->settings->get_bool(lib->settings, "%s.plugins.bliss.use_bliss_b",
1003 TRUE, lib->ns))
1004 {
1005 switch (key_size)
1006 {
1007 case BLISS_I:
1008 key_size = BLISS_B_I;
1009 break;
1010 case BLISS_II:
1011 key_size = BLISS_B_II;
1012 break;
1013 case BLISS_III:
1014 key_size = BLISS_B_III;
1015 break;
1016 case BLISS_IV:
1017 key_size = BLISS_B_IV;
1018 break;
1019 default:
1020 break;
1021 }
1022 }
1023
1024 /* Only BLISS or BLISS-B types I, III, or IV are currently supported */
1025 set = bliss_param_set_get_by_id(key_size);
1026 if (!set)
1027 {
1028 DBG1(DBG_LIB, "BLISS parameter set %u not supported", key_size);
1029 return NULL;
1030 }
1031
1032 /* Some shortcuts for often used variables */
1033 n = set->n;
1034 q = set->q;
1035
1036 if (set->fft_params->n != n || set->fft_params->q != q)
1037 {
1038 DBG1(DBG_LIB, "FFT parameters do not match BLISS parameters");
1039 return NULL;
1040 }
1041 this = bliss_private_key_create_empty();
1042 this->set = set;
1043
1044 /* We derive the public key from the private key using the FFT */
1045 fft = bliss_fft_create(set->fft_params);
1046
1047 /* Some vectors needed to derive the publi key */
1048 S1 = malloc(n * sizeof(uint32_t));
1049 S2 = malloc(n * sizeof(uint32_t));
1050 a = malloc(n * sizeof(uint32_t));
1051 this->A = malloc(n * sizeof(uint32_t));
1052
1053 /* Instantiate a true random generator */
1054 rng = lib->crypto->create_rng(lib->crypto, RNG_TRUE);
1055
1056 /* Loop until we have an invertible polynomial s1 */
1057 do
1058 {
1059 if (!create_secret(this, rng, &this->s1, &this->s2, &trials))
1060 {
1061 break;
1062 }
1063
1064 /* Convert signed arrays to unsigned arrays before FFT */
1065 for (i = 0; i < n; i++)
1066 {
1067 S1[i] = (this->s1[i] < 0) ? this->s1[i] + q : this->s1[i];
1068 S2[i] = (this->s2[i] > 0) ? q - this->s2[i] : -this->s2[i];
1069 }
1070 fft->transform(fft, S1, S1, FALSE);
1071 fft->transform(fft, S2, S2, FALSE);
1072
1073 success = TRUE;
1074 for (i = 0; i < n; i++)
1075 {
1076 if (S1[i] == 0)
1077 {
1078 DBG1(DBG_LIB, "S1[%d] is zero - s1 is not invertible", i);
1079 free(this->s1);
1080 free(this->s2);
1081 this->s1 = NULL;
1082 this->s2 = NULL;
1083 success = FALSE;
1084 break;
1085 }
1086 this->A[i] = invert(S1[i], q);
1087 this->A[i] = (S2[i] * this->A[i]) % q;
1088 }
1089 }
1090 while (!success && trials < SECRET_KEY_TRIALS_MAX);
1091
1092 DBG1(DBG_LIB, "secret key generation %s after %d trial%s",
1093 success ? "succeeded" : "failed", trials, (trials == 1) ? "" : "s");
1094
1095 if (success)
1096 {
1097 fft->transform(fft, this->A, a, TRUE);
1098
1099 DBG4(DBG_LIB, " i f g a F G A");
1100 for (i = 0; i < n; i++)
1101 {
1102 DBG4(DBG_LIB, "%4d %3d %3d %5u %5u %5u %5u",
1103 i, this->s1[i], this->s2[i], a[i], S1[i], S2[i], this->A[i]);
1104 }
1105 }
1106 else
1107 {
1108 destroy(this);
1109 }
1110
1111 /* Cleanup */
1112 fft->destroy(fft);
1113 rng->destroy(rng);
1114 memwipe(S1, n * sizeof(uint32_t));
1115 memwipe(S2, n * sizeof(uint32_t));
1116 free(S1);
1117 free(S2);
1118 free(a);
1119
1120 return success ? &this->public : NULL;
1121 }
1122
1123 /**
1124 * ASN.1 definition of a BLISS private key
1125 */
1126 static const asn1Object_t privkeyObjects[] = {
1127 { 0, "BLISSPrivateKey", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
1128 { 1, "keyType", ASN1_OID, ASN1_BODY }, /* 1 */
1129 { 1, "public", ASN1_BIT_STRING, ASN1_BODY }, /* 2 */
1130 { 1, "secret1", ASN1_BIT_STRING, ASN1_BODY }, /* 3 */
1131 { 1, "secret2", ASN1_BIT_STRING, ASN1_BODY }, /* 4 */
1132 { 0, "exit", ASN1_EOC, ASN1_EXIT }
1133 };
1134 #define PRIV_KEY_TYPE 1
1135 #define PRIV_KEY_PUBLIC 2
1136 #define PRIV_KEY_SECRET1 3
1137 #define PRIV_KEY_SECRET2 4
1138
1139 /**
1140 * See header.
1141 */
1142 bliss_private_key_t *bliss_private_key_load(key_type_t type, va_list args)
1143 {
1144 private_bliss_private_key_t *this;
1145 chunk_t key = chunk_empty, object;
1146 bliss_bitpacker_t *packer;
1147 asn1_parser_t *parser;
1148 size_t s_bits = 0;
1149 int8_t s, s_min, s_max;
1150 uint32_t s_sign = 0x02, s_mask = 0xfffffffc, value;
1151 bool success = FALSE;
1152 int objectID, oid, i;
1153
1154 while (TRUE)
1155 {
1156 switch (va_arg(args, builder_part_t))
1157 {
1158 case BUILD_BLOB_ASN1_DER:
1159 key = va_arg(args, chunk_t);
1160 continue;
1161 case BUILD_END:
1162 break;
1163 default:
1164 return NULL;
1165 }
1166 break;
1167 }
1168
1169 if (key.len == 0)
1170 {
1171 return NULL;
1172 }
1173 this = bliss_private_key_create_empty();
1174
1175 parser = asn1_parser_create(privkeyObjects, key);
1176 parser->set_flags(parser, FALSE, TRUE);
1177
1178 while (parser->iterate(parser, &objectID, &object))
1179 {
1180 switch (objectID)
1181 {
1182 case PRIV_KEY_TYPE:
1183 oid = asn1_known_oid(object);
1184 if (oid == OID_UNKNOWN)
1185 {
1186 goto end;
1187 }
1188 this->set = bliss_param_set_get_by_oid(oid);
1189 if (this->set == NULL)
1190 {
1191 goto end;
1192 }
1193 if (lib->settings->get_bool(lib->settings,
1194 "%s.plugins.bliss.use_bliss_b",TRUE, lib->ns))
1195 {
1196 switch (this->set->id)
1197 {
1198 case BLISS_I:
1199 this->set = bliss_param_set_get_by_id(BLISS_B_I);
1200 break;
1201 case BLISS_III:
1202 this->set = bliss_param_set_get_by_id(BLISS_B_III);
1203 break;
1204 case BLISS_IV:
1205 this->set = bliss_param_set_get_by_id(BLISS_B_IV);
1206 break;
1207 default:
1208 break;
1209 }
1210 }
1211 if (this->set->non_zero2)
1212 {
1213 s_min = -2;
1214 s_max = 2;
1215 s_bits = 3;
1216 }
1217 else
1218 {
1219 s_min = -1;
1220 s_max = 1;
1221 s_bits = 2;
1222 }
1223 s_sign = 1 << (s_bits - 1);
1224 s_mask = ((1 << (32 - s_bits)) - 1) << s_bits;
1225 break;
1226 case PRIV_KEY_PUBLIC:
1227 if (!bliss_public_key_from_asn1(object, this->set, &this->A))
1228 {
1229 goto end;
1230 }
1231 break;
1232 case PRIV_KEY_SECRET1:
1233 if (object.len != 1 + (s_bits * this->set->n + 7)/8)
1234 {
1235 goto end;
1236 }
1237 this->s1 = malloc(this->set->n);
1238
1239 /* Skip unused bits octet */
1240 object = chunk_skip(object, 1);
1241 packer = bliss_bitpacker_create_from_data(object);
1242 for (i = 0; i < this->set->n; i++)
1243 {
1244 packer->read_bits(packer, &value, s_bits);
1245 s = (value & s_sign) ? value | s_mask : value;
1246 if (s < s_min || s > s_max)
1247 {
1248 packer->destroy(packer);
1249 goto end;
1250 }
1251 this->s1[i] = s;
1252 }
1253 packer->destroy(packer);
1254 break;
1255 case PRIV_KEY_SECRET2:
1256 if (object.len != 1 + (s_bits * this->set->n + 7)/8)
1257 {
1258 goto end;
1259 }
1260 this->s2 = malloc(this->set->n);
1261
1262 /* Skip unused bits octet */
1263 object = chunk_skip(object, 1);
1264 packer = bliss_bitpacker_create_from_data(object);
1265 for (i = 0; i < this->set->n; i++)
1266 {
1267 packer->read_bits(packer, &value, s_bits);
1268 s = (value & s_sign) ? value | s_mask : value;
1269 if (s < s_min || s > s_max)
1270 {
1271 packer->destroy(packer);
1272 goto end;
1273 }
1274 this->s2[i] = 2 * s;
1275 if (i == 0)
1276 {
1277 this->s2[0] += 1;
1278 }
1279 }
1280 packer->destroy(packer);
1281 break;
1282 }
1283 }
1284 success = parser->success(parser);
1285
1286 end:
1287 parser->destroy(parser);
1288 if (!success)
1289 {
1290 destroy(this);
1291 return NULL;
1292 }
1293
1294 return &this->public;
1295 }
1296