Annotation of src/usr.bin/ssh/moduli.c, Revision 1.23
1.23 ! dtucker 1: /* $OpenBSD: moduli.c,v 1.22 2010/11/10 01:33:07 djm Exp $ */
1.1 djm 2: /*
3: * Copyright 1994 Phil Karn <karn@qualcomm.com>
4: * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com>
5: * Copyright 2000 Niels Provos <provos@citi.umich.edu>
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28:
29: /*
30: * Two-step process to generate safe primes for DHGEX
31: *
32: * Sieve candidates for "safe" primes,
33: * suitable for use as Diffie-Hellman moduli;
34: * that is, where q = (p-1)/2 is also prime.
35: *
36: * First step: generate candidate primes (memory intensive)
37: * Second step: test primes' safety (processor intensive)
38: */
39:
1.14 stevesk 40: #include <sys/types.h>
41:
42: #include <openssl/bn.h>
1.21 djm 43: #include <openssl/dh.h>
1.14 stevesk 44:
1.17 stevesk 45: #include <stdio.h>
1.16 stevesk 46: #include <stdlib.h>
1.15 stevesk 47: #include <string.h>
1.18 deraadt 48: #include <stdarg.h>
1.14 stevesk 49: #include <time.h>
1.23 ! dtucker 50: #include <unistd.h>
1.14 stevesk 51:
1.1 djm 52: #include "xmalloc.h"
1.21 djm 53: #include "dh.h"
1.1 djm 54: #include "log.h"
55:
56: /*
57: * File output defines
58: */
59:
60: /* need line long enough for largest moduli plus headers */
1.9 deraadt 61: #define QLINESIZE (100+8192)
1.1 djm 62:
1.5 djm 63: /*
64: * Size: decimal.
1.1 djm 65: * Specifies the number of the most significant bit (0 to M).
1.5 djm 66: * WARNING: internally, usually 1 to N.
1.1 djm 67: */
1.9 deraadt 68: #define QSIZE_MINIMUM (511)
1.1 djm 69:
70: /*
71: * Prime sieving defines
72: */
73:
74: /* Constant: assuming 8 bit bytes and 32 bit words */
1.9 deraadt 75: #define SHIFT_BIT (3)
76: #define SHIFT_BYTE (2)
77: #define SHIFT_WORD (SHIFT_BIT+SHIFT_BYTE)
78: #define SHIFT_MEGABYTE (20)
79: #define SHIFT_MEGAWORD (SHIFT_MEGABYTE-SHIFT_BYTE)
1.1 djm 80:
81: /*
1.7 djm 82: * Using virtual memory can cause thrashing. This should be the largest
83: * number that is supported without a large amount of disk activity --
84: * that would increase the run time from hours to days or weeks!
85: */
1.9 deraadt 86: #define LARGE_MINIMUM (8UL) /* megabytes */
1.7 djm 87:
88: /*
89: * Do not increase this number beyond the unsigned integer bit size.
90: * Due to a multiple of 4, it must be LESS than 128 (yielding 2**30 bits).
91: */
1.9 deraadt 92: #define LARGE_MAXIMUM (127UL) /* megabytes */
1.7 djm 93:
94: /*
1.1 djm 95: * Constant: when used with 32-bit integers, the largest sieve prime
96: * has to be less than 2**32.
97: */
1.9 deraadt 98: #define SMALL_MAXIMUM (0xffffffffUL)
1.1 djm 99:
100: /* Constant: can sieve all primes less than 2**32, as 65537**2 > 2**32-1. */
1.9 deraadt 101: #define TINY_NUMBER (1UL<<16)
1.1 djm 102:
103: /* Ensure enough bit space for testing 2*q. */
1.12 djm 104: #define TEST_MAXIMUM (1UL<<16)
105: #define TEST_MINIMUM (QSIZE_MINIMUM + 1)
106: /* real TEST_MINIMUM (1UL << (SHIFT_WORD - TEST_POWER)) */
107: #define TEST_POWER (3) /* 2**n, n < SHIFT_WORD */
1.1 djm 108:
109: /* bit operations on 32-bit words */
1.12 djm 110: #define BIT_CLEAR(a,n) ((a)[(n)>>SHIFT_WORD] &= ~(1L << ((n) & 31)))
111: #define BIT_SET(a,n) ((a)[(n)>>SHIFT_WORD] |= (1L << ((n) & 31)))
112: #define BIT_TEST(a,n) ((a)[(n)>>SHIFT_WORD] & (1L << ((n) & 31)))
1.1 djm 113:
114: /*
115: * Prime testing defines
116: */
117:
1.7 djm 118: /* Minimum number of primality tests to perform */
1.12 djm 119: #define TRIAL_MINIMUM (4)
1.7 djm 120:
1.1 djm 121: /*
122: * Sieving data (XXX - move to struct)
123: */
124:
125: /* sieve 2**16 */
126: static u_int32_t *TinySieve, tinybits;
127:
128: /* sieve 2**30 in 2**16 parts */
129: static u_int32_t *SmallSieve, smallbits, smallbase;
130:
131: /* sieve relative to the initial value */
132: static u_int32_t *LargeSieve, largewords, largetries, largenumbers;
133: static u_int32_t largebits, largememory; /* megabytes */
134: static BIGNUM *largebase;
135:
1.11 avsm 136: int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
1.23 ! dtucker 137: int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *);
1.1 djm 138:
139: /*
140: * print moduli out in consistent form,
141: */
142: static int
143: qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries,
144: u_int32_t osize, u_int32_t ogenerator, BIGNUM * omodulus)
145: {
146: struct tm *gtm;
147: time_t time_now;
148: int res;
149:
150: time(&time_now);
151: gtm = gmtime(&time_now);
1.2 djm 152:
1.1 djm 153: res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
154: gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday,
155: gtm->tm_hour, gtm->tm_min, gtm->tm_sec,
156: otype, otests, otries, osize, ogenerator);
157:
158: if (res < 0)
159: return (-1);
160:
161: if (BN_print_fp(ofile, omodulus) < 1)
162: return (-1);
163:
164: res = fprintf(ofile, "\n");
165: fflush(ofile);
166:
167: return (res > 0 ? 0 : -1);
168: }
169:
170:
171: /*
172: ** Sieve p's and q's with small factors
173: */
174: static void
175: sieve_large(u_int32_t s)
176: {
177: u_int32_t r, u;
178:
1.5 djm 179: debug3("sieve_large %u", s);
1.1 djm 180: largetries++;
181: /* r = largebase mod s */
182: r = BN_mod_word(largebase, s);
183: if (r == 0)
184: u = 0; /* s divides into largebase exactly */
185: else
186: u = s - r; /* largebase+u is first entry divisible by s */
187:
188: if (u < largebits * 2) {
189: /*
190: * The sieve omits p's and q's divisible by 2, so ensure that
191: * largebase+u is odd. Then, step through the sieve in
192: * increments of 2*s
193: */
194: if (u & 0x1)
195: u += s; /* Make largebase+u odd, and u even */
196:
197: /* Mark all multiples of 2*s */
198: for (u /= 2; u < largebits; u += s)
199: BIT_SET(LargeSieve, u);
200: }
201:
202: /* r = p mod s */
203: r = (2 * r + 1) % s;
204: if (r == 0)
205: u = 0; /* s divides p exactly */
206: else
207: u = s - r; /* p+u is first entry divisible by s */
208:
209: if (u < largebits * 4) {
210: /*
211: * The sieve omits p's divisible by 4, so ensure that
212: * largebase+u is not. Then, step through the sieve in
213: * increments of 4*s
214: */
215: while (u & 0x3) {
216: if (SMALL_MAXIMUM - u < s)
217: return;
218: u += s;
219: }
220:
221: /* Mark all multiples of 4*s */
222: for (u /= 4; u < largebits; u += s)
223: BIT_SET(LargeSieve, u);
224: }
225: }
226:
227: /*
1.6 djm 228: * list candidates for Sophie-Germain primes (where q = (p-1)/2)
1.1 djm 229: * to standard output.
230: * The list is checked against small known primes (less than 2**30).
231: */
232: int
1.11 avsm 233: gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
1.1 djm 234: {
235: BIGNUM *q;
236: u_int32_t j, r, s, t;
237: u_int32_t smallwords = TINY_NUMBER >> 6;
238: u_int32_t tinywords = TINY_NUMBER >> 6;
239: time_t time_start, time_stop;
1.11 avsm 240: u_int32_t i;
241: int ret = 0;
1.1 djm 242:
243: largememory = memory;
244:
1.7 djm 245: if (memory != 0 &&
1.12 djm 246: (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
1.7 djm 247: error("Invalid memory amount (min %ld, max %ld)",
248: LARGE_MINIMUM, LARGE_MAXIMUM);
249: return (-1);
250: }
251:
1.1 djm 252: /*
1.2 djm 253: * Set power to the length in bits of the prime to be generated.
254: * This is changed to 1 less than the desired safe prime moduli p.
255: */
1.1 djm 256: if (power > TEST_MAXIMUM) {
257: error("Too many bits: %u > %lu", power, TEST_MAXIMUM);
258: return (-1);
259: } else if (power < TEST_MINIMUM) {
260: error("Too few bits: %u < %u", power, TEST_MINIMUM);
261: return (-1);
262: }
263: power--; /* decrement before squaring */
264:
265: /*
1.2 djm 266: * The density of ordinary primes is on the order of 1/bits, so the
267: * density of safe primes should be about (1/bits)**2. Set test range
268: * to something well above bits**2 to be reasonably sure (but not
269: * guaranteed) of catching at least one safe prime.
1.1 djm 270: */
271: largewords = ((power * power) >> (SHIFT_WORD - TEST_POWER));
272:
273: /*
1.2 djm 274: * Need idea of how much memory is available. We don't have to use all
275: * of it.
1.1 djm 276: */
277: if (largememory > LARGE_MAXIMUM) {
278: logit("Limited memory: %u MB; limit %lu MB",
279: largememory, LARGE_MAXIMUM);
280: largememory = LARGE_MAXIMUM;
281: }
282:
283: if (largewords <= (largememory << SHIFT_MEGAWORD)) {
284: logit("Increased memory: %u MB; need %u bytes",
285: largememory, (largewords << SHIFT_BYTE));
286: largewords = (largememory << SHIFT_MEGAWORD);
287: } else if (largememory > 0) {
288: logit("Decreased memory: %u MB; want %u bytes",
289: largememory, (largewords << SHIFT_BYTE));
290: largewords = (largememory << SHIFT_MEGAWORD);
291: }
292:
1.13 djm 293: TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
1.1 djm 294: tinybits = tinywords << SHIFT_WORD;
295:
1.13 djm 296: SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
1.1 djm 297: smallbits = smallwords << SHIFT_WORD;
298:
299: /*
300: * dynamically determine available memory
301: */
302: while ((LargeSieve = calloc(largewords, sizeof(u_int32_t))) == NULL)
303: largewords -= (1L << (SHIFT_MEGAWORD - 2)); /* 1/4 MB chunks */
304:
305: largebits = largewords << SHIFT_WORD;
306: largenumbers = largebits * 2; /* even numbers excluded */
307:
308: /* validation check: count the number of primes tried */
309: largetries = 0;
1.19 markus 310: if ((q = BN_new()) == NULL)
311: fatal("BN_new failed");
1.1 djm 312:
313: /*
1.2 djm 314: * Generate random starting point for subprime search, or use
315: * specified parameter.
1.1 djm 316: */
1.19 markus 317: if ((largebase = BN_new()) == NULL)
318: fatal("BN_new failed");
319: if (start == NULL) {
320: if (BN_rand(largebase, power, 1, 1) == 0)
321: fatal("BN_rand failed");
322: } else {
323: if (BN_copy(largebase, start) == NULL)
324: fatal("BN_copy: failed");
325: }
1.1 djm 326:
327: /* ensure odd */
1.19 markus 328: if (BN_set_bit(largebase, 0) == 0)
329: fatal("BN_set_bit: failed");
1.1 djm 330:
331: time(&time_start);
332:
1.2 djm 333: logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start),
1.1 djm 334: largenumbers, power);
335: debug2("start point: 0x%s", BN_bn2hex(largebase));
336:
337: /*
1.2 djm 338: * TinySieve
339: */
1.1 djm 340: for (i = 0; i < tinybits; i++) {
341: if (BIT_TEST(TinySieve, i))
342: continue; /* 2*i+3 is composite */
343:
344: /* The next tiny prime */
345: t = 2 * i + 3;
346:
347: /* Mark all multiples of t */
348: for (j = i + t; j < tinybits; j += t)
349: BIT_SET(TinySieve, j);
350:
351: sieve_large(t);
352: }
353:
354: /*
1.2 djm 355: * Start the small block search at the next possible prime. To avoid
356: * fencepost errors, the last pass is skipped.
357: */
1.1 djm 358: for (smallbase = TINY_NUMBER + 3;
1.12 djm 359: smallbase < (SMALL_MAXIMUM - TINY_NUMBER);
360: smallbase += TINY_NUMBER) {
1.1 djm 361: for (i = 0; i < tinybits; i++) {
362: if (BIT_TEST(TinySieve, i))
363: continue; /* 2*i+3 is composite */
364:
365: /* The next tiny prime */
366: t = 2 * i + 3;
367: r = smallbase % t;
368:
369: if (r == 0) {
370: s = 0; /* t divides into smallbase exactly */
371: } else {
372: /* smallbase+s is first entry divisible by t */
373: s = t - r;
374: }
375:
376: /*
377: * The sieve omits even numbers, so ensure that
378: * smallbase+s is odd. Then, step through the sieve
379: * in increments of 2*t
380: */
381: if (s & 1)
382: s += t; /* Make smallbase+s odd, and s even */
383:
384: /* Mark all multiples of 2*t */
385: for (s /= 2; s < smallbits; s += t)
386: BIT_SET(SmallSieve, s);
387: }
388:
389: /*
1.2 djm 390: * SmallSieve
391: */
1.1 djm 392: for (i = 0; i < smallbits; i++) {
393: if (BIT_TEST(SmallSieve, i))
394: continue; /* 2*i+smallbase is composite */
395:
396: /* The next small prime */
397: sieve_large((2 * i) + smallbase);
398: }
399:
400: memset(SmallSieve, 0, smallwords << SHIFT_BYTE);
401: }
402:
403: time(&time_stop);
404:
405: logit("%.24s Sieved with %u small primes in %ld seconds",
406: ctime(&time_stop), largetries, (long) (time_stop - time_start));
407:
408: for (j = r = 0; j < largebits; j++) {
409: if (BIT_TEST(LargeSieve, j))
410: continue; /* Definitely composite, skip */
411:
412: debug2("test q = largebase+%u", 2 * j);
1.19 markus 413: if (BN_set_word(q, 2 * j) == 0)
414: fatal("BN_set_word failed");
415: if (BN_add(q, q, largebase) == 0)
416: fatal("BN_add failed");
1.21 djm 417: if (qfileout(out, MODULI_TYPE_SOPHIE_GERMAIN,
418: MODULI_TESTS_SIEVE, largetries,
419: (power - 1) /* MSB */, (0), q) == -1) {
1.1 djm 420: ret = -1;
421: break;
422: }
423:
424: r++; /* count q */
425: }
426:
427: time(&time_stop);
428:
429: xfree(LargeSieve);
430: xfree(SmallSieve);
431: xfree(TinySieve);
432:
433: logit("%.24s Found %u candidates", ctime(&time_stop), r);
434:
435: return (ret);
436: }
437:
1.23 ! dtucker 438: static void
! 439: write_checkpoint(char *cpfile, u_int32_t lineno)
! 440: {
! 441: FILE *fp;
! 442: char tmpfile[MAXPATHLEN];
! 443: int r;
! 444:
! 445: r = snprintf(tmpfile, sizeof(tmpfile), "%s.XXXXXXXXXX", cpfile);
! 446: if (r == -1 || r >= MAXPATHLEN) {
! 447: logit("write_checkpoint: temp pathname too long");
! 448: return;
! 449: }
! 450: if ((r = mkstemp(tmpfile)) == -1) {
! 451: logit("mkstemp(%s): %s", tmpfile, strerror(errno));
! 452: return;
! 453: }
! 454: if ((fp = fdopen(r, "w")) == NULL) {
! 455: logit("write_checkpoint: fdopen: %s", strerror(errno));
! 456: close(r);
! 457: return;
! 458: }
! 459: if (fprintf(fp, "%lu\n", (unsigned long)lineno) > 0 && fclose(fp) == 0
! 460: && rename(tmpfile, cpfile) == 0)
! 461: debug3("wrote checkpoint line %lu to '%s'",
! 462: (unsigned long)lineno, cpfile);
! 463: else
! 464: logit("failed to write to checkpoint file '%s': %s", cpfile,
! 465: strerror(errno));
! 466: }
! 467:
! 468: static unsigned long
! 469: read_checkpoint(char *cpfile)
! 470: {
! 471: FILE *fp;
! 472: unsigned long lineno = 0;
! 473:
! 474: if ((fp = fopen(cpfile, "r")) == NULL)
! 475: return 0;
! 476: if (fscanf(fp, "%lu\n", &lineno) < 1)
! 477: logit("Failed to load checkpoint from '%s'", cpfile);
! 478: else
! 479: logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno);
! 480: fclose(fp);
! 481: return lineno;
! 482: }
! 483:
1.1 djm 484: /*
485: * perform a Miller-Rabin primality test
486: * on the list of candidates
487: * (checking both q and p)
488: * The result is a list of so-call "safe" primes
489: */
490: int
1.23 ! dtucker 491: prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
! 492: char *checkpoint_file)
1.1 djm 493: {
494: BIGNUM *q, *p, *a;
495: BN_CTX *ctx;
496: char *cp, *lp;
497: u_int32_t count_in = 0, count_out = 0, count_possible = 0;
498: u_int32_t generator_known, in_tests, in_tries, in_type, in_size;
1.23 ! dtucker 499: unsigned long last_processed = 0;
1.1 djm 500: time_t time_start, time_stop;
501: int res;
1.7 djm 502:
503: if (trials < TRIAL_MINIMUM) {
504: error("Minimum primality trials is %d", TRIAL_MINIMUM);
505: return (-1);
506: }
1.1 djm 507:
508: time(&time_start);
509:
1.19 markus 510: if ((p = BN_new()) == NULL)
511: fatal("BN_new failed");
512: if ((q = BN_new()) == NULL)
513: fatal("BN_new failed");
514: if ((ctx = BN_CTX_new()) == NULL)
515: fatal("BN_CTX_new failed");
1.1 djm 516:
517: debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
518: ctime(&time_start), trials, generator_wanted);
519:
1.23 ! dtucker 520: if (checkpoint_file != NULL)
! 521: last_processed = read_checkpoint(checkpoint_file);
! 522:
1.1 djm 523: res = 0;
524: lp = xmalloc(QLINESIZE + 1);
1.20 ray 525: while (fgets(lp, QLINESIZE + 1, in) != NULL) {
1.1 djm 526: count_in++;
1.23 ! dtucker 527: if (checkpoint_file != NULL) {
! 528: if (count_in <= last_processed) {
! 529: debug3("skipping line %u, before checkpoint",
! 530: count_in);
! 531: continue;
! 532: }
! 533: write_checkpoint(checkpoint_file, count_in);
! 534: }
1.20 ray 535: if (strlen(lp) < 14 || *lp == '!' || *lp == '#') {
1.1 djm 536: debug2("%10u: comment or short line", count_in);
537: continue;
538: }
539:
540: /* XXX - fragile parser */
541: /* time */
542: cp = &lp[14]; /* (skip) */
543:
544: /* type */
545: in_type = strtoul(cp, &cp, 10);
546:
547: /* tests */
548: in_tests = strtoul(cp, &cp, 10);
549:
1.21 djm 550: if (in_tests & MODULI_TESTS_COMPOSITE) {
1.1 djm 551: debug2("%10u: known composite", count_in);
552: continue;
553: }
1.5 djm 554:
1.1 djm 555: /* tries */
556: in_tries = strtoul(cp, &cp, 10);
557:
558: /* size (most significant bit) */
559: in_size = strtoul(cp, &cp, 10);
560:
561: /* generator (hex) */
562: generator_known = strtoul(cp, &cp, 16);
563:
564: /* Skip white space */
565: cp += strspn(cp, " ");
566:
567: /* modulus (hex) */
568: switch (in_type) {
1.21 djm 569: case MODULI_TYPE_SOPHIE_GERMAIN:
1.6 djm 570: debug2("%10u: (%u) Sophie-Germain", count_in, in_type);
1.1 djm 571: a = q;
1.19 markus 572: if (BN_hex2bn(&a, cp) == 0)
573: fatal("BN_hex2bn failed");
1.1 djm 574: /* p = 2*q + 1 */
1.19 markus 575: if (BN_lshift(p, q, 1) == 0)
576: fatal("BN_lshift failed");
577: if (BN_add_word(p, 1) == 0)
578: fatal("BN_add_word failed");
1.1 djm 579: in_size += 1;
580: generator_known = 0;
581: break;
1.21 djm 582: case MODULI_TYPE_UNSTRUCTURED:
583: case MODULI_TYPE_SAFE:
584: case MODULI_TYPE_SCHNORR:
585: case MODULI_TYPE_STRONG:
586: case MODULI_TYPE_UNKNOWN:
1.1 djm 587: debug2("%10u: (%u)", count_in, in_type);
588: a = p;
1.19 markus 589: if (BN_hex2bn(&a, cp) == 0)
590: fatal("BN_hex2bn failed");
1.1 djm 591: /* q = (p-1) / 2 */
1.19 markus 592: if (BN_rshift(q, p, 1) == 0)
593: fatal("BN_rshift failed");
1.1 djm 594: break;
1.5 djm 595: default:
596: debug2("Unknown prime type");
597: break;
1.1 djm 598: }
599:
600: /*
601: * due to earlier inconsistencies in interpretation, check
602: * the proposed bit size.
603: */
1.11 avsm 604: if ((u_int32_t)BN_num_bits(p) != (in_size + 1)) {
1.1 djm 605: debug2("%10u: bit size %u mismatch", count_in, in_size);
606: continue;
607: }
608: if (in_size < QSIZE_MINIMUM) {
609: debug2("%10u: bit size %u too short", count_in, in_size);
610: continue;
611: }
612:
1.21 djm 613: if (in_tests & MODULI_TESTS_MILLER_RABIN)
1.1 djm 614: in_tries += trials;
615: else
616: in_tries = trials;
1.5 djm 617:
1.1 djm 618: /*
619: * guess unknown generator
620: */
621: if (generator_known == 0) {
622: if (BN_mod_word(p, 24) == 11)
623: generator_known = 2;
624: else if (BN_mod_word(p, 12) == 5)
625: generator_known = 3;
626: else {
627: u_int32_t r = BN_mod_word(p, 10);
628:
1.5 djm 629: if (r == 3 || r == 7)
1.1 djm 630: generator_known = 5;
631: }
632: }
633: /*
634: * skip tests when desired generator doesn't match
635: */
636: if (generator_wanted > 0 &&
637: generator_wanted != generator_known) {
638: debug2("%10u: generator %d != %d",
639: count_in, generator_known, generator_wanted);
1.4 dtucker 640: continue;
641: }
642:
643: /*
644: * Primes with no known generator are useless for DH, so
645: * skip those.
646: */
647: if (generator_known == 0) {
648: debug2("%10u: no known generator", count_in);
1.1 djm 649: continue;
650: }
651:
652: count_possible++;
653:
654: /*
1.2 djm 655: * The (1/4)^N performance bound on Miller-Rabin is
656: * extremely pessimistic, so don't spend a lot of time
657: * really verifying that q is prime until after we know
658: * that p is also prime. A single pass will weed out the
1.1 djm 659: * vast majority of composite q's.
660: */
1.22 djm 661: if (BN_is_prime_ex(q, 1, ctx, NULL) <= 0) {
1.5 djm 662: debug("%10u: q failed first possible prime test",
1.1 djm 663: count_in);
664: continue;
665: }
1.2 djm 666:
1.1 djm 667: /*
1.2 djm 668: * q is possibly prime, so go ahead and really make sure
669: * that p is prime. If it is, then we can go back and do
670: * the same for q. If p is composite, chances are that
1.1 djm 671: * will show up on the first Rabin-Miller iteration so it
672: * doesn't hurt to specify a high iteration count.
673: */
1.22 djm 674: if (!BN_is_prime_ex(p, trials, ctx, NULL)) {
1.5 djm 675: debug("%10u: p is not prime", count_in);
1.1 djm 676: continue;
677: }
678: debug("%10u: p is almost certainly prime", count_in);
679:
680: /* recheck q more rigorously */
1.22 djm 681: if (!BN_is_prime_ex(q, trials - 1, ctx, NULL)) {
1.1 djm 682: debug("%10u: q is not prime", count_in);
683: continue;
684: }
685: debug("%10u: q is almost certainly prime", count_in);
686:
1.21 djm 687: if (qfileout(out, MODULI_TYPE_SAFE,
688: in_tests | MODULI_TESTS_MILLER_RABIN,
1.1 djm 689: in_tries, in_size, generator_known, p)) {
690: res = -1;
691: break;
692: }
693:
694: count_out++;
695: }
696:
697: time(&time_stop);
698: xfree(lp);
699: BN_free(p);
700: BN_free(q);
701: BN_CTX_free(ctx);
1.23 ! dtucker 702:
! 703: if (checkpoint_file != NULL)
! 704: unlink(checkpoint_file);
1.1 djm 705:
706: logit("%.24s Found %u safe primes of %u candidates in %ld seconds",
1.2 djm 707: ctime(&time_stop), count_out, count_possible,
1.1 djm 708: (long) (time_stop - time_start));
709:
710: return (res);
711: }