]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ffc/ffc_params_generate.c
fix provider signatures
[thirdparty/openssl.git] / crypto / ffc / ffc_params_generate.c
CommitLineData
f11f86f6
SL
1/*
2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/*
11 * For the prime check..
12 * FIPS 186-4 Section C.3 Table C.1
13 * Returns the minimum number of Miller Rabin iterations for a L,N pair
14 * (where L = len(p), N = len(q))
15 * L N Min
16 * 1024 160 40
17 * 2048 224 56
18 * 2048 256 56
19 * 3072 256 64
20 *
21 * BN_check_prime() uses:
22 * 64 iterations for L <= 2048 OR
23 * 128 iterations for L > 2048
24 * So this satisfies the requirement.
25 */
26
27#include <string.h> /* memset */
28#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
29#include <openssl/rand.h>
8da42c8b
DDO
30#include <openssl/err.h>
31#include <openssl/dherr.h>
32#include <openssl/dsaerr.h>
f11f86f6
SL
33#include "crypto/bn.h"
34#include "internal/ffc.h"
35
36/*
37 * Verify that the passed in L, N pair for DH or DSA is valid.
38 * Returns 0 if invalid, otherwise it returns the security strength.
39 */
49ed5ba8
SL
40
41#ifdef FIPS_MODULE
42static int ffc_validate_LN(size_t L, size_t N, int type, int verify)
43{
44 if (type == FFC_PARAM_TYPE_DH) {
45 /* Valid DH L,N parameters from SP800-56Ar3 5.5.1 Table 1 */
46 if (L == 2048 && (N == 224 || N == 256))
47 return 112;
48# ifndef OPENSSL_NO_DH
49 DHerr(0, DH_R_BAD_FFC_PARAMETERS);
50# endif
51 } else if (type == FFC_PARAM_TYPE_DSA) {
52 /* Valid DSA L,N parameters from FIPS 186-4 Section 4.2 */
53 /* In fips mode 1024/160 can only be used for verification */
54 if (verify && L == 1024 && N == 160)
55 return 80;
56 if (L == 2048 && (N == 224 || N == 256))
57 return 112;
58 if (L == 3072 && N == 256)
59 return 128;
60# ifndef OPENSSL_NO_DSA
61 DSAerr(0, DSA_R_BAD_FFC_PARAMETERS);
62# endif
63 }
64 return 0;
65}
66#else
b8237707 67static int ffc_validate_LN(size_t L, size_t N, int type, int verify)
f11f86f6 68{
b8237707 69 if (type == FFC_PARAM_TYPE_DH) {
b8237707
SL
70 /* Allow legacy 1024/160 in non fips mode */
71 if (L == 1024 && N == 160)
72 return 80;
f11f86f6
SL
73 /* Valid DH L,N parameters from SP800-56Ar3 5.5.1 Table 1 */
74 if (L == 2048 && (N == 224 || N == 256))
75 return 112;
49ed5ba8 76# ifndef OPENSSL_NO_DH
8da42c8b 77 DHerr(0, DH_R_BAD_FFC_PARAMETERS);
49ed5ba8 78# endif
f11f86f6 79 } else if (type == FFC_PARAM_TYPE_DSA) {
49ed5ba8
SL
80 if (L == 1024 && N == 160)
81 return 80;
f11f86f6
SL
82 if (L == 2048 && (N == 224 || N == 256))
83 return 112;
f11f86f6
SL
84 if (L == 3072 && N == 256)
85 return 128;
49ed5ba8 86# ifndef OPENSSL_NO_DSA
8da42c8b 87 DSAerr(0, DSA_R_BAD_FFC_PARAMETERS);
49ed5ba8 88# endif
f11f86f6
SL
89 }
90 return 0;
91}
49ed5ba8 92#endif /* FIPS_MODULE */
f11f86f6
SL
93
94/* FIPS186-4 A.2.1 Unverifiable Generation of Generator g */
95static int generate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, BIGNUM *g,
96 BIGNUM *hbn, const BIGNUM *p,
97 const BIGNUM *e,const BIGNUM *pm1,
98 int *hret)
99{
100 int h = 2;
101
102 /* Step (2): choose h (where 1 < h)*/
103 if (!BN_set_word(hbn, h))
104 return 0;
105
106 for (;;) {
107 /* Step (3): g = h^e % p */
108 if (!BN_mod_exp_mont(g, hbn, e, p, ctx, mont))
109 return 0;
110 /* Step (4): Finish if g > 1 */
111 if (BN_cmp(g, BN_value_one()) > 0)
112 break;
113
114 /* Step (2) Choose any h in the range 1 < h < (p-1) */
115 if (!BN_add_word(hbn, 1) || BN_cmp(hbn, pm1) >= 0)
116 return 0;
117 ++h;
118 }
119 *hret = h;
120 return 1;
121}
122
123/*
124 * FIPS186-4 A.2 Generation of canonical generator g.
125 *
126 * It requires the following values as input:
127 * 'evpmd' digest, 'p' prime, 'e' cofactor, gindex and seed.
128 * tmp is a passed in temporary BIGNUM.
129 * mont is used in a BN_mod_exp_mont() with a modulus of p.
130 * Returns a value in g.
131 */
132static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont,
133 const EVP_MD *evpmd, BIGNUM *g, BIGNUM *tmp,
134 const BIGNUM *p, const BIGNUM *e,
135 int gindex, unsigned char *seed, size_t seedlen)
136{
137 int ret = 0;
138 int counter = 1;
139 unsigned char md[EVP_MAX_MD_SIZE];
140 EVP_MD_CTX *mctx = NULL;
141 int mdsize;
142
075b1f2f
SL
143 mdsize = EVP_MD_size(evpmd);
144 if (mdsize <= 0)
145 return 0;
146
f11f86f6
SL
147 mctx = EVP_MD_CTX_new();
148 if (mctx == NULL)
075b1f2f 149 return 0;
f11f86f6 150
f11f86f6
SL
151 /*
152 * A.2.3 Step (4) & (5)
153 * A.2.4 Step (6) & (7)
154 * counter = 0; counter += 1
155 */
156 for (counter = 1; counter <= 0xFFFF; ++counter) {
157 /*
158 * A.2.3 Step (7) & (8) & (9)
159 * A.2.4 Step (9) & (10) & (11)
160 * W = Hash(seed || "ggen" || index || counter)
161 * g = W^e % p
162 */
163 static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
164
165 md[0] = (unsigned char)(gindex & 0xff);
166 md[1] = (unsigned char)((counter >> 8) & 0xff);
167 md[2] = (unsigned char)(counter & 0xff);
168 if (!EVP_DigestInit_ex(mctx, evpmd, NULL)
169 || !EVP_DigestUpdate(mctx, seed, seedlen)
170 || !EVP_DigestUpdate(mctx, ggen, sizeof(ggen))
171 || !EVP_DigestUpdate(mctx, md, 3)
172 || !EVP_DigestFinal_ex(mctx, md, NULL)
173 || (BN_bin2bn(md, mdsize, tmp) == NULL)
174 || !BN_mod_exp_mont(g, tmp, e, p, ctx, mont))
075b1f2f 175 break; /* exit on failure */
f11f86f6
SL
176 /*
177 * A.2.3 Step (10)
178 * A.2.4 Step (12)
179 * Found a value for g if (g >= 2)
180 */
181 if (BN_cmp(g, BN_value_one()) > 0) {
182 ret = 1;
183 break; /* found g */
184 }
185 }
f11f86f6
SL
186 EVP_MD_CTX_free(mctx);
187 return ret;
188}
189
190/* Generation of p is the same for FIPS 186-4 & FIPS 186-2 */
191static int generate_p(BN_CTX *ctx, const EVP_MD *evpmd, int max_counter, int n,
192 unsigned char *buf, size_t buf_len, const BIGNUM *q,
193 BIGNUM *p, int L, BN_GENCB *cb, int *counter,
194 int *res)
195{
196 int ret = -1;
197 int i, j, k, r;
198 unsigned char md[EVP_MAX_MD_SIZE];
199 int mdsize;
200 BIGNUM *W, *X, *tmp, *c, *test;
201
202 BN_CTX_start(ctx);
203 W = BN_CTX_get(ctx);
204 X = BN_CTX_get(ctx);
205 c = BN_CTX_get(ctx);
206 test = BN_CTX_get(ctx);
207 tmp = BN_CTX_get(ctx);
208 if (tmp == NULL)
209 goto err;
210
211 if (!BN_lshift(test, BN_value_one(), L - 1))
212 goto err;
213
214 mdsize = EVP_MD_size(evpmd);
215 if (mdsize <= 0)
216 goto err;
217
218 /* A.1.1.2 Step (10) AND
219 * A.1.1.2 Step (12)
220 * offset = 1 (this is handled below)
221 */
222 /*
223 * A.1.1.2 Step (11) AND
224 * A.1.1.3 Step (13)
225 */
226 for (i = 0; i <= max_counter; i++) {
227 if ((i != 0) && !BN_GENCB_call(cb, 0, i))
228 goto err;
229
230 BN_zero(W);
231 /* seed_tmp buffer contains "seed + offset - 1" */
232 for (j = 0; j <= n; j++) {
233 /* obtain "seed + offset + j" by incrementing by 1: */
234 for (k = (int)buf_len - 1; k >= 0; k--) {
235 buf[k]++;
236 if (buf[k] != 0)
237 break;
238 }
239 /*
240 * A.1.1.2 Step (11.1) AND
241 * A.1.1.3 Step (13.1)
242 * tmp = V(j) = Hash((seed + offset + j) % 2^seedlen)
243 */
244 if (!EVP_Digest(buf, buf_len, md, NULL, evpmd, NULL)
245 || (BN_bin2bn(md, mdsize, tmp) == NULL)
246 /*
247 * A.1.1.2 Step (11.2)
248 * A.1.1.3 Step (13.2)
249 * W += V(j) * 2^(outlen * j)
250 */
251 || !BN_lshift(tmp, tmp, (mdsize << 3) * j)
252 || !BN_add(W, W, tmp))
253 goto err;
254 }
255
256 /*
257 * A.1.1.2 Step (11.3) AND
258 * A.1.1.3 Step (13.3)
259 * X = W + 2^(L-1) where W < 2^(L-1)
260 */
261 if (!BN_mask_bits(W, L - 1)
262 || !BN_copy(X, W)
263 || !BN_add(X, X, test)
264 /*
265 * A.1.1.2 Step (11.4) AND
266 * A.1.1.3 Step (13.4)
267 * c = X mod 2q
268 */
269 || !BN_lshift1(tmp, q)
270 || !BN_mod(c, X, tmp, ctx)
271 /*
272 * A.1.1.2 Step (11.5) AND
273 * A.1.1.3 Step (13.5)
274 * p = X - (c - 1)
275 */
276 || !BN_sub(tmp, c, BN_value_one())
277 || !BN_sub(p, X, tmp))
278 goto err;
279
280 /*
281 * A.1.1.2 Step (11.6) AND
282 * A.1.1.3 Step (13.6)
283 * if (p < 2 ^ (L-1)) continue
284 * This makes sure the top bit is set.
285 */
286 if (BN_cmp(p, test) >= 0) {
287 /*
288 * A.1.1.2 Step (11.7) AND
289 * A.1.1.3 Step (13.7)
290 * Test if p is prime
291 * (This also makes sure the bottom bit is set)
292 */
293 r = BN_check_prime(p, ctx, cb);
294 /* A.1.1.2 Step (11.8) : Return if p is prime */
295 if (r > 0) {
296 *counter = i;
297 ret = 1; /* return success */
298 goto err;
299 }
300 if (r != 0)
301 goto err;
302 }
303 /* Step (11.9) : offset = offset + n + 1 is done auto-magically */
304 }
305 /* No prime P found */
306 ret = 0;
307 *res |= FFC_CHECK_P_NOT_PRIME;
308err:
309 BN_CTX_end(ctx);
310 return ret;
311}
312
313static int generate_q_fips186_4(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd,
314 int qsize, unsigned char *seed, size_t seedlen,
315 int generate_seed, int *retm, int *res,
316 BN_GENCB *cb)
317{
318 int ret = 0, r;
319 int m = *retm;
320 unsigned char md[EVP_MAX_MD_SIZE];
321 int mdsize = EVP_MD_size(evpmd);
322 unsigned char *pmd;
323 OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);
324
325 /* find q */
326 for (;;) {
327 if(!BN_GENCB_call(cb, 0, m++))
328 goto err;
329
330 /* A.1.1.2 Step (5) : generate seed with size seed_len */
331 if (generate_seed
332 && RAND_bytes_ex(libctx, seed, (int)seedlen) < 0)
333 goto err;
334 /*
335 * A.1.1.2 Step (6) AND
336 * A.1.1.3 Step (7)
337 * U = Hash(seed) % (2^(N-1))
338 */
339 if (!EVP_Digest(seed, seedlen, md, NULL, evpmd, NULL))
340 goto err;
341 /* Take least significant bits of md */
342 if (mdsize > qsize)
343 pmd = md + mdsize - qsize;
344 else
345 pmd = md;
346 if (mdsize < qsize)
347 memset(md + mdsize, 0, qsize - mdsize);
348
349 /*
350 * A.1.1.2 Step (7) AND
351 * A.1.1.3 Step (8)
352 * q = U + 2^(N-1) + (1 - U %2) (This sets top and bottom bits)
353 */
354 pmd[0] |= 0x80;
355 pmd[qsize-1] |= 0x01;
356 if (!BN_bin2bn(pmd, qsize, q))
357 goto err;
358
359 /*
360 * A.1.1.2 Step (8) AND
361 * A.1.1.3 Step (9)
362 * Test if q is prime
363 */
364 r = BN_check_prime(q, ctx, cb);
365 if (r > 0) {
366 ret = 1;
367 goto err;
368 }
369 /*
370 * A.1.1.3 Step (9) : If the provided seed didn't produce a prime q
371 * return an error.
372 */
373 if (!generate_seed) {
374 *res |= FFC_CHECK_Q_NOT_PRIME;
375 goto err;
376 }
377 if (r != 0)
378 goto err;
379 /* A.1.1.2 Step (9) : if q is not prime, try another q */
380 }
381err:
382 *retm = m;
383 return ret;
384}
385
386static int generate_q_fips186_2(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd,
387 unsigned char *buf, unsigned char *seed,
388 size_t qsize, int generate_seed, int *retm,
389 int *res, BN_GENCB *cb)
390{
391 unsigned char buf2[EVP_MAX_MD_SIZE];
392 unsigned char md[EVP_MAX_MD_SIZE];
393 int i, r, ret = 0, m = *retm;
394 OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);
395
396 /* find q */
397 for (;;) {
398 /* step 1 */
399 if (!BN_GENCB_call(cb, 0, m++))
400 goto err;
401
402 if (generate_seed && RAND_bytes_ex(libctx, seed, (int)qsize) <= 0)
403 goto err;
404
405 memcpy(buf, seed, qsize);
406 memcpy(buf2, seed, qsize);
407
408 /* precompute "SEED + 1" for step 7: */
409 for (i = (int)qsize - 1; i >= 0; i--) {
410 buf[i]++;
411 if (buf[i] != 0)
412 break;
413 }
414
415 /* step 2 */
416 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
417 goto err;
418 if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
419 goto err;
420 for (i = 0; i < (int)qsize; i++)
421 md[i] ^= buf2[i];
422
423 /* step 3 */
424 md[0] |= 0x80;
425 md[qsize - 1] |= 0x01;
426 if (!BN_bin2bn(md, (int)qsize, q))
427 goto err;
428
429 /* step 4 */
430 r = BN_check_prime(q, ctx, cb);
431 if (r > 0) {
432 /* Found a prime */
433 ret = 1;
434 goto err;
435 }
436 if (r != 0)
437 goto err; /* Exit if error */
438 /* Try another iteration if it wasnt prime - was in old code.. */
439 generate_seed = 1;
440 }
441err:
442 *retm = m;
443 return ret;
444}
445
4f2271d5 446static const char *default_mdname(size_t N)
f11f86f6 447{
f11f86f6 448 if (N == 160)
4f2271d5 449 return "SHA1";
f11f86f6 450 else if (N == 224)
4f2271d5 451 return "SHA-224";
f11f86f6 452 else if (N == 256)
4f2271d5
SL
453 return "SHA-256";
454 return NULL;
f11f86f6
SL
455}
456
457/*
458 * FIPS 186-4 FFC parameter generation (as defined in Appendix A).
459 * The same code is used for validation (when validate_flags != 0)
460 *
461 * The primes p & q are generated/validated using:
462 * A.1.1.2 Generation of probable primes p & q using approved hash.
463 * A.1.1.3 Validation of generated probable primes
464 *
465 * Generator 'g' has 2 types in FIPS 186-4:
466 * (1) A.2.1 unverifiable generation of generator g.
467 * A.2.2 Assurance of the validity of unverifiable generator g.
468 * (2) A.2.3 Verifiable Canonical Generation of the generator g.
469 * A.2.4 Validation for Canonical Generation of the generator g.
470 *
471 * Notes:
472 * (1) is only a partial validation of g, The validation of (2) requires
473 * the seed and index used during generation as input.
474 *
475 * params: used to pass in values for generation and validation.
4f2271d5
SL
476 * params->md: is the digest to use, If this value is NULL, then the digest is
477 * chosen using the value of N.
478 * params->flags:
479 * For validation one of:
480 * -FFC_PARAM_FLAG_VALIDATE_PQ
481 * -FFC_PARAM_FLAG_VALIDATE_G
482 * -FFC_PARAM_FLAG_VALIDATE_ALL
f11f86f6
SL
483 * For generation of p & q:
484 * - This is skipped if p & q are passed in.
485 * - If the seed is passed in then generation of p & q uses this seed (and if
486 * this fails an error will occur).
487 * - Otherwise the seed is generated, and values of p & q are generated and
488 * the value of seed and counter are optionally returned.
489 * For the generation of g (after the generation of p, q):
490 * - If the seed has been generated or passed in and a valid gindex is passed
491 * in then canonical generation of g is used otherwise unverifiable
492 * generation of g is chosen.
493 * For validation of p & q:
494 * - p, q, and the seed and counter used for generation must be passed in.
495 * For validation of g:
496 * - For a partial validation : p, q and g are required.
497 * - For a canonical validation : the gindex and seed used for generation are
498 * also required.
4f2271d5 499 * mode: The mode - either FFC_PARAM_MODE_GENERATE or FFC_PARAM_MODE_VERIFY.
f11f86f6
SL
500 * type: The key type - FFC_PARAM_TYPE_DSA or FFC_PARAM_TYPE_DH.
501 * L: is the size of the prime p in bits (e.g 2048)
502 * N: is the size of the prime q in bits (e.g 256)
f11f86f6
SL
503 * res: A returned failure reason (One of FFC_CHECK_XXXX),
504 * or 0 for general failures.
505 * cb: A callback (can be NULL) that is called during different phases
506 *
507 * Returns:
4f2271d5
SL
508 * - FFC_PARAM_RET_STATUS_FAILED: if there was an error, or validation failed.
509 * - FFC_PARAM_RET_STATUS_SUCCESS if the generation or validation succeeded.
510 * - FFC_PARAM_RET_STATUS_UNVERIFIABLE_G if the validation of G succeeded,
f11f86f6
SL
511 * but G is unverifiable.
512 */
8083fd3a 513int ffc_params_FIPS186_4_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
4f2271d5 514 int mode, int type, size_t L, size_t N,
8083fd3a 515 int *res, BN_GENCB *cb)
f11f86f6 516{
4f2271d5 517 int ok = FFC_PARAM_RET_STATUS_FAILED;
f11f86f6
SL
518 unsigned char *seed = NULL, *seed_tmp = NULL;
519 int mdsize, counter = 0, pcounter = 0, r = 0;
520 size_t seedlen = 0;
521 BIGNUM *tmp, *pm1, *e, *test;
522 BIGNUM *g = NULL, *q = NULL, *p = NULL;
523 BN_MONT_CTX *mont = NULL;
4f2271d5 524 int n = 0, m = 0, qsize;
b03ec3b5 525 int canonical_g = 0, hret = 0;
f11f86f6
SL
526 BN_CTX *ctx = NULL;
527 EVP_MD_CTX *mctx = NULL;
4f2271d5
SL
528 EVP_MD *md = NULL;
529 int verify = (mode == FFC_PARAM_MODE_VERIFY);
530 unsigned int flags = verify ? params->flags : 0;
63794b04 531 const char *def_name;
f11f86f6
SL
532
533 *res = 0;
534
4f2271d5
SL
535 if (params->mdname != NULL) {
536 md = EVP_MD_fetch(libctx, params->mdname, params->mdprops);
537 } else {
9beffaf6 538 if (N == 0)
4f2271d5 539 N = (L >= 2048 ? SHA256_DIGEST_LENGTH : SHA_DIGEST_LENGTH) * 8;
63794b04 540 def_name = default_mdname(N);
b8237707
SL
541 if (def_name == NULL) {
542 *res = FFC_CHECK_INVALID_Q_VALUE;
63794b04 543 goto err;
b8237707 544 }
63794b04 545 md = EVP_MD_fetch(libctx, def_name, NULL);
4f2271d5
SL
546 }
547 if (md == NULL)
548 goto err;
549 mdsize = EVP_MD_size(md);
550 if (mdsize <= 0)
551 goto err;
552
9beffaf6 553 if (N == 0)
4f2271d5
SL
554 N = mdsize * 8;
555 qsize = N >> 3;
556
f11f86f6
SL
557 /*
558 * A.1.1.2 Step (1) AND
559 * A.1.1.3 Step (3)
560 * Check that the L,N pair is an acceptable pair.
561 */
b8237707 562 if (L <= N || !ffc_validate_LN(L, N, type, verify)) {
f11f86f6
SL
563 *res = FFC_CHECK_BAD_LN_PAIR;
564 goto err;
565 }
566
567 mctx = EVP_MD_CTX_new();
568 if (mctx == NULL)
569 goto err;
570
f11f86f6
SL
571 if ((ctx = BN_CTX_new_ex(libctx)) == NULL)
572 goto err;
573
574 BN_CTX_start(ctx);
575 g = BN_CTX_get(ctx);
576 pm1 = BN_CTX_get(ctx);
577 e = BN_CTX_get(ctx);
578 test = BN_CTX_get(ctx);
579 tmp = BN_CTX_get(ctx);
580 if (tmp == NULL)
581 goto err;
582
583 seedlen = params->seedlen;
584 if (seedlen == 0)
585 seedlen = (size_t)mdsize;
586 /* If the seed was passed in - use this value as the seed */
587 if (params->seed != NULL)
588 seed = params->seed;
589
4f2271d5 590 if (!verify) {
f11f86f6
SL
591 /* For generation: p & q must both be NULL or NON-NULL */
592 if ((params->p == NULL) != (params->q == NULL)) {
593 *res = FFC_CHECK_INVALID_PQ;
594 goto err;
595 }
596 } else {
597 /* Validation of p,q requires seed and counter to be valid */
4f2271d5 598 if ((flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0) {
f11f86f6
SL
599 if (seed == NULL || params->pcounter < 0) {
600 *res = FFC_CHECK_MISSING_SEED_OR_COUNTER;
601 goto err;
602 }
603 }
4f2271d5 604 if ((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0) {
f11f86f6
SL
605 /* validation of g also requires g to be set */
606 if (params->g == NULL) {
607 *res = FFC_CHECK_INVALID_G;
608 goto err;
609 }
610 }
611 }
612
613 /*
614 * If p & q are passed in and
615 * validate_flags = 0 then skip the generation of PQ.
616 * validate_flags = VALIDATE_G then also skip the validation of PQ.
617 */
4f2271d5 618 if (params->p != NULL && ((flags & FFC_PARAM_FLAG_VALIDATE_PQ) == 0)) {
f11f86f6
SL
619 /* p and q already exists so only generate g */
620 p = params->p;
621 q = params->q;
622 goto g_only;
623 /* otherwise fall thru to validate p & q */
624 }
625
626 /* p & q will be used for generation and validation */
627 p = BN_CTX_get(ctx);
628 q = BN_CTX_get(ctx);
629 if (q == NULL)
630 goto err;
631
632 /*
633 * A.1.1.2 Step (2) AND
634 * A.1.1.3 Step (6)
635 * Return invalid if seedlen < N
636 */
637 if ((seedlen * 8) < N) {
638 *res = FFC_CHECK_INVALID_SEED_SIZE;
639 goto err;
640 }
641
642 seed_tmp = OPENSSL_malloc(seedlen);
643 if (seed_tmp == NULL)
644 goto err;
645
646 if (seed == NULL) {
647 /* Validation requires the seed to be supplied */
4f2271d5 648 if (verify) {
f11f86f6
SL
649 *res = FFC_CHECK_MISSING_SEED_OR_COUNTER;
650 goto err;
651 }
652 /* if the seed is not supplied then alloc a seed buffer */
653 seed = OPENSSL_malloc(seedlen);
654 if (seed == NULL)
655 goto err;
656 }
657
658 /* A.1.1.2 Step (11): max loop count = 4L - 1 */
659 counter = 4 * L - 1;
660 /* Validation requires the counter to be supplied */
4f2271d5 661 if (verify) {
f11f86f6
SL
662 /* A.1.1.3 Step (4) : if (counter > (4L -1)) return INVALID */
663 if (params->pcounter > counter) {
664 *res = FFC_CHECK_INVALID_COUNTER;
665 goto err;
666 }
667 counter = params->pcounter;
668 }
669
670 /*
671 * A.1.1.2 Step (3) AND
672 * A.1.1.3 Step (10)
673 * n = floor(L / hash_outlen) - 1
674 */
675 n = (L - 1 ) / (mdsize << 3);
676
677 /* Calculate 2^(L-1): Used in step A.1.1.2 Step (11.3) */
678 if (!BN_lshift(test, BN_value_one(), L - 1))
679 goto err;
680
681 for (;;) {
4f2271d5 682 if (!generate_q_fips186_4(ctx, q, md, qsize, seed, seedlen,
f11f86f6
SL
683 seed != params->seed, &m, res, cb))
684 goto err;
685 /* A.1.1.3 Step (9): Verify that q matches the expected value */
4f2271d5 686 if (verify && (BN_cmp(q, params->q) != 0)) {
f11f86f6
SL
687 *res = FFC_CHECK_Q_MISMATCH;
688 goto err;
689 }
690 if(!BN_GENCB_call(cb, 2, 0))
691 goto err;
692 if(!BN_GENCB_call(cb, 3, 0))
693 goto err;
694
695 memcpy(seed_tmp, seed, seedlen);
4f2271d5
SL
696 r = generate_p(ctx, md, counter, n, seed_tmp, seedlen, q, p, L,
697 cb, &pcounter, res);
f11f86f6
SL
698 if (r > 0)
699 break; /* found p */
700 if (r < 0)
701 goto err;
702 /*
703 * A.1.1.3 Step (14):
704 * If we get here we failed to get a p for the given seed. If the
705 * seed is not random then it needs to fail (as it will always fail).
706 */
707 if (seed == params->seed) {
708 *res = FFC_CHECK_P_NOT_PRIME;
709 goto err;
710 }
711 }
712 if(!BN_GENCB_call(cb, 2, 1))
713 goto err;
714 /*
715 * Gets here if we found p.
716 * A.1.1.3 Step (14): return error if i != counter OR computed_p != known_p.
717 */
4f2271d5 718 if (verify && (pcounter != counter || (BN_cmp(p, params->p) != 0)))
f11f86f6
SL
719 goto err;
720
721 /* If validating p & q only then skip the g validation test */
4f2271d5 722 if ((flags & FFC_PARAM_FLAG_VALIDATE_ALL) == FFC_PARAM_FLAG_VALIDATE_PQ)
f11f86f6
SL
723 goto pass;
724g_only:
725 if ((mont = BN_MONT_CTX_new()) == NULL)
726 goto err;
727 if (!BN_MONT_CTX_set(mont, p, ctx))
728 goto err;
729
4f2271d5 730 if (((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0)
f11f86f6
SL
731 && !ffc_params_validate_unverifiable_g(ctx, mont, p, q, params->g,
732 tmp, res))
733 goto err;
734
735 /*
736 * A.2.1 Step (1) AND
737 * A.2.3 Step (3) AND
738 * A.2.4 Step (5)
739 * e = (p - 1) / q (i.e- Cofactor 'e' is given by p = q * e + 1)
740 */
741 if (!(BN_sub(pm1, p, BN_value_one()) && BN_div(e, NULL, pm1, q, ctx)))
742 goto err;
743
744 /* Canonical g requires a seed and index to be set */
745 if ((seed != NULL) && (params->gindex != FFC_UNVERIFIABLE_GINDEX)) {
746 canonical_g = 1;
4f2271d5 747 if (!generate_canonical_g(ctx, mont, md, g, tmp, p, e,
f11f86f6
SL
748 params->gindex, seed, seedlen)) {
749 *res = FFC_CHECK_INVALID_G;
750 goto err;
751 }
752 /* A.2.4 Step (13): Return valid if computed_g == g */
4f2271d5 753 if (verify && BN_cmp(g, params->g) != 0) {
f11f86f6
SL
754 *res = FFC_CHECK_G_MISMATCH;
755 goto err;
756 }
4f2271d5 757 } else if (!verify) {
f11f86f6
SL
758 if (!generate_unverifiable_g(ctx, mont, g, tmp, p, e, pm1, &hret))
759 goto err;
760 }
761
762 if (!BN_GENCB_call(cb, 3, 1))
763 goto err;
764
4f2271d5 765 if (!verify) {
f11f86f6
SL
766 if (p != params->p) {
767 BN_free(params->p);
768 params->p = BN_dup(p);
769 }
770 if (q != params->q) {
771 BN_free(params->q);
772 params->q = BN_dup(q);
773 }
774 if (g != params->g) {
775 BN_free(params->g);
776 params->g = BN_dup(g);
777 }
778 if (params->p == NULL || params->q == NULL || params->g == NULL)
779 goto err;
780 if (!ffc_params_set_validate_params(params, seed, seedlen, pcounter))
781 goto err;
782 params->h = hret;
783 }
784pass:
4f2271d5 785 if ((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0 && (canonical_g == 0))
f11f86f6 786 /* Return for the case where g is partially valid */
4f2271d5 787 ok = FFC_PARAM_RET_STATUS_UNVERIFIABLE_G;
f11f86f6 788 else
4f2271d5 789 ok = FFC_PARAM_RET_STATUS_SUCCESS;
f11f86f6
SL
790err:
791 if (seed != params->seed)
792 OPENSSL_free(seed);
793 OPENSSL_free(seed_tmp);
075b1f2f 794 if (ctx != NULL)
f11f86f6
SL
795 BN_CTX_end(ctx);
796 BN_CTX_free(ctx);
797 BN_MONT_CTX_free(mont);
f11f86f6 798 EVP_MD_CTX_free(mctx);
4f2271d5 799 EVP_MD_free(md);
f11f86f6
SL
800 return ok;
801}
802
b8237707 803/* Note this function is only used for verification in fips mode */
8083fd3a 804int ffc_params_FIPS186_2_gen_verify(OPENSSL_CTX *libctx, FFC_PARAMS *params,
4f2271d5 805 int mode, int type, size_t L, size_t N,
8083fd3a 806 int *res, BN_GENCB *cb)
f11f86f6 807{
4f2271d5 808 int ok = FFC_PARAM_RET_STATUS_FAILED;
f11f86f6
SL
809 unsigned char seed[SHA256_DIGEST_LENGTH];
810 unsigned char buf[SHA256_DIGEST_LENGTH];
811 BIGNUM *r0, *test, *tmp, *g = NULL, *q = NULL, *p = NULL;
812 BN_MONT_CTX *mont = NULL;
4f2271d5
SL
813 EVP_MD *md = NULL;
814 size_t qsize;
f11f86f6
SL
815 int n = 0, m = 0;
816 int counter = 0, pcounter = 0, use_random_seed;
817 int rv;
818 BN_CTX *ctx = NULL;
819 int hret = -1;
f11f86f6
SL
820 unsigned char *seed_in = params->seed;
821 size_t seed_len = params->seedlen;
4f2271d5
SL
822 int verify = (mode == FFC_PARAM_MODE_VERIFY);
823 unsigned int flags = verify ? params->flags : 0;
b8237707 824 const char *def_name;
f11f86f6
SL
825
826 *res = 0;
4f2271d5
SL
827
828 if (params->mdname != NULL) {
829 md = EVP_MD_fetch(libctx, params->mdname, params->mdprops);
830 } else {
9beffaf6 831 if (N == 0)
4f2271d5 832 N = (L >= 2048 ? SHA256_DIGEST_LENGTH : SHA_DIGEST_LENGTH) * 8;
b8237707
SL
833 def_name = default_mdname(N);
834 if (def_name == NULL) {
835 *res = FFC_CHECK_INVALID_Q_VALUE;
836 goto err;
837 }
838 md = EVP_MD_fetch(libctx, def_name, NULL);
4f2271d5
SL
839 }
840 if (md == NULL)
841 goto err;
9beffaf6 842 if (N == 0)
4f2271d5
SL
843 N = EVP_MD_size(md) * 8;
844 qsize = N >> 3;
845
f11f86f6 846 /*
b8237707
SL
847 * The original spec allowed L = 512 + 64*j (j = 0.. 8)
848 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
849 * says that 512 can be used for legacy verification.
f11f86f6 850 */
b8237707 851 if (L < 512) {
f11f86f6 852 *res = FFC_CHECK_BAD_LN_PAIR;
4f2271d5 853 goto err;
f11f86f6 854 }
f11f86f6
SL
855 if (qsize != SHA_DIGEST_LENGTH
856 && qsize != SHA224_DIGEST_LENGTH
857 && qsize != SHA256_DIGEST_LENGTH) {
858 /* invalid q size */
859 *res = FFC_CHECK_INVALID_Q_VALUE;
4f2271d5 860 goto err;
f11f86f6
SL
861 }
862
f11f86f6
SL
863 L = (L + 63) / 64 * 64;
864
865 if (seed_in != NULL) {
866 if (seed_len < qsize) {
867 *res = FFC_CHECK_INVALID_SEED_SIZE;
4f2271d5 868 goto err;
f11f86f6 869 }
4f2271d5
SL
870 /* Only consume as much seed as is expected. */
871 if (seed_len > qsize)
f11f86f6 872 seed_len = qsize;
f11f86f6
SL
873 memcpy(seed, seed_in, seed_len);
874 }
875
876 ctx = BN_CTX_new_ex(libctx);
877 if (ctx == NULL)
878 goto err;
879
880 BN_CTX_start(ctx);
881
882 r0 = BN_CTX_get(ctx);
883 g = BN_CTX_get(ctx);
884 q = BN_CTX_get(ctx);
885 p = BN_CTX_get(ctx);
886 tmp = BN_CTX_get(ctx);
887 test = BN_CTX_get(ctx);
888 if (test == NULL)
889 goto err;
890
891 if (!BN_lshift(test, BN_value_one(), L - 1))
892 goto err;
893
4f2271d5 894 if (!verify) {
f11f86f6
SL
895 /* For generation: p & q must both be NULL or NON-NULL */
896 if ((params->p != NULL) != (params->q != NULL)) {
897 *res = FFC_CHECK_INVALID_PQ;
898 goto err;
899 }
900 } else {
4f2271d5 901 if ((flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0) {
f11f86f6
SL
902 /* Validation of p,q requires seed and counter to be valid */
903 if (seed_in == NULL || params->pcounter < 0) {
904 *res = FFC_CHECK_MISSING_SEED_OR_COUNTER;
905 goto err;
906 }
907 }
4f2271d5 908 if ((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0) {
f11f86f6
SL
909 /* validation of g also requires g to be set */
910 if (params->g == NULL) {
911 *res = FFC_CHECK_INVALID_G;
912 goto err;
913 }
914 }
915 }
916
4f2271d5 917 if (params->p != NULL && ((flags & FFC_PARAM_FLAG_VALIDATE_PQ) == 0)) {
f11f86f6
SL
918 /* p and q already exists so only generate g */
919 p = params->p;
920 q = params->q;
921 goto g_only;
922 /* otherwise fall thru to validate p and q */
923 }
924
925 use_random_seed = (seed_in == NULL);
926 for (;;) {
4f2271d5 927 if (!generate_q_fips186_2(ctx, q, md, buf, seed, qsize,
f11f86f6
SL
928 use_random_seed, &m, res, cb))
929 goto err;
930
931 if (!BN_GENCB_call(cb, 2, 0))
932 goto err;
933 if (!BN_GENCB_call(cb, 3, 0))
934 goto err;
935
936 /* step 6 */
937 n = (L - 1) / 160;
938 counter = 4 * L - 1; /* Was 4096 */
939 /* Validation requires the counter to be supplied */
4f2271d5 940 if (verify) {
f11f86f6
SL
941 if (params->pcounter > counter) {
942 *res = FFC_CHECK_INVALID_COUNTER;
943 goto err;
944 }
945 counter = params->pcounter;
946 }
947
4f2271d5 948 rv = generate_p(ctx, md, counter, n, buf, qsize, q, p, L, cb,
f11f86f6
SL
949 &pcounter, res);
950 if (rv > 0)
951 break; /* found it */
952 if (rv == -1)
953 goto err;
954 /* This is what the old code did - probably not a good idea! */
955 use_random_seed = 1;
956 }
957
958 if (!BN_GENCB_call(cb, 2, 1))
959 goto err;
960
4f2271d5 961 if (verify) {
f11f86f6
SL
962 if (pcounter != counter) {
963 *res = FFC_CHECK_COUNTER_MISMATCH;
964 goto err;
965 }
966 if (BN_cmp(p, params->p) != 0) {
967 *res = FFC_CHECK_P_MISMATCH;
968 goto err;
969 }
970 }
971 /* If validating p & q only then skip the g validation test */
4f2271d5 972 if ((flags & FFC_PARAM_FLAG_VALIDATE_ALL) == FFC_PARAM_FLAG_VALIDATE_PQ)
f11f86f6
SL
973 goto pass;
974g_only:
975 if ((mont = BN_MONT_CTX_new()) == NULL)
976 goto err;
977 if (!BN_MONT_CTX_set(mont, p, ctx))
978 goto err;
979
4f2271d5 980 if (!verify) {
f11f86f6
SL
981 /* We now need to generate g */
982 /* set test = p - 1 */
983 if (!BN_sub(test, p, BN_value_one()))
984 goto err;
985 /* Set r0 = (p - 1) / q */
986 if (!BN_div(r0, NULL, test, q, ctx))
987 goto err;
988 if (!generate_unverifiable_g(ctx, mont, g, tmp, p, r0, test, &hret))
989 goto err;
4f2271d5 990 } else if (((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0)
f11f86f6
SL
991 && !ffc_params_validate_unverifiable_g(ctx, mont, p, q,
992 params->g, tmp, res)) {
993 goto err;
994 }
995
996 if (!BN_GENCB_call(cb, 3, 1))
997 goto err;
998
4f2271d5 999 if (!verify) {
f11f86f6
SL
1000 if (p != params->p) {
1001 BN_free(params->p);
1002 params->p = BN_dup(p);
1003 }
1004 if (q != params->q) {
1005 BN_free(params->q);
1006 params->q = BN_dup(q);
1007 }
1008 if (g != params->g) {
1009 BN_free(params->g);
1010 params->g = BN_dup(g);
1011 }
1012 if (params->p == NULL || params->q == NULL || params->g == NULL)
1013 goto err;
1014 if (!ffc_params_set_validate_params(params, seed, qsize, pcounter))
1015 goto err;
1016 params->h = hret;
1017 }
1018pass:
4f2271d5
SL
1019 if ((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0)
1020 ok = FFC_PARAM_RET_STATUS_UNVERIFIABLE_G;
f11f86f6 1021 else
4f2271d5 1022 ok = FFC_PARAM_RET_STATUS_SUCCESS;
f11f86f6
SL
1023err:
1024 if (ctx != NULL)
1025 BN_CTX_end(ctx);
1026 BN_CTX_free(ctx);
f11f86f6 1027 BN_MONT_CTX_free(mont);
4f2271d5 1028 EVP_MD_free(md);
f11f86f6
SL
1029 return ok;
1030}
1031
1032int ffc_params_FIPS186_4_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
1033 int type, size_t L, size_t N,
4f2271d5 1034 int *res, BN_GENCB *cb)
f11f86f6 1035{
4f2271d5
SL
1036 return ffc_params_FIPS186_4_gen_verify(libctx, params,
1037 FFC_PARAM_MODE_GENERATE,
1038 type, L, N, res, cb);
f11f86f6
SL
1039}
1040
1041/* This should no longer be used in FIPS mode */
1042int ffc_params_FIPS186_2_generate(OPENSSL_CTX *libctx, FFC_PARAMS *params,
1043 int type, size_t L, size_t N,
4f2271d5 1044 int *res, BN_GENCB *cb)
f11f86f6 1045{
4f2271d5
SL
1046 return ffc_params_FIPS186_2_gen_verify(libctx, params,
1047 FFC_PARAM_MODE_GENERATE,
1048 type, L, N, res, cb);
f11f86f6 1049}