xref: /freebsd/crypto/openssh/moduli.c (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
1*4d3fc8b0SEd Maste /* $OpenBSD: moduli.c,v 1.39 2023/03/02 06:41:56 dtucker Exp $ */
2d95e11bfSDag-Erling Smørgrav /*
3d95e11bfSDag-Erling Smørgrav  * Copyright 1994 Phil Karn <karn@qualcomm.com>
4d95e11bfSDag-Erling Smørgrav  * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com>
5d95e11bfSDag-Erling Smørgrav  * Copyright 2000 Niels Provos <provos@citi.umich.edu>
6d95e11bfSDag-Erling Smørgrav  * All rights reserved.
7d95e11bfSDag-Erling Smørgrav  *
8d95e11bfSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9d95e11bfSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10d95e11bfSDag-Erling Smørgrav  * are met:
11d95e11bfSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
12d95e11bfSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
13d95e11bfSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
14d95e11bfSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
15d95e11bfSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
16d95e11bfSDag-Erling Smørgrav  *
17d95e11bfSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d95e11bfSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d95e11bfSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d95e11bfSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d95e11bfSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d95e11bfSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d95e11bfSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d95e11bfSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d95e11bfSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d95e11bfSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d95e11bfSDag-Erling Smørgrav  */
28d95e11bfSDag-Erling Smørgrav 
29d95e11bfSDag-Erling Smørgrav /*
30d95e11bfSDag-Erling Smørgrav  * Two-step process to generate safe primes for DHGEX
31d95e11bfSDag-Erling Smørgrav  *
32d95e11bfSDag-Erling Smørgrav  *  Sieve candidates for "safe" primes,
33d95e11bfSDag-Erling Smørgrav  *  suitable for use as Diffie-Hellman moduli;
34d95e11bfSDag-Erling Smørgrav  *  that is, where q = (p-1)/2 is also prime.
35d95e11bfSDag-Erling Smørgrav  *
36d95e11bfSDag-Erling Smørgrav  * First step: generate candidate primes (memory intensive)
37d95e11bfSDag-Erling Smørgrav  * Second step: test primes' safety (processor intensive)
38d95e11bfSDag-Erling Smørgrav  */
39d95e11bfSDag-Erling Smørgrav 
40d95e11bfSDag-Erling Smørgrav #include "includes.h"
41761efaa7SDag-Erling Smørgrav 
42bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
43bc5531deSDag-Erling Smørgrav 
44761efaa7SDag-Erling Smørgrav #include <sys/types.h>
45d95e11bfSDag-Erling Smørgrav 
46d95e11bfSDag-Erling Smørgrav #include <openssl/bn.h>
47d4af9e69SDag-Erling Smørgrav #include <openssl/dh.h>
48d95e11bfSDag-Erling Smørgrav 
49462c32cbSDag-Erling Smørgrav #include <errno.h>
50761efaa7SDag-Erling Smørgrav #include <stdio.h>
51761efaa7SDag-Erling Smørgrav #include <stdlib.h>
52761efaa7SDag-Erling Smørgrav #include <string.h>
53761efaa7SDag-Erling Smørgrav #include <stdarg.h>
54761efaa7SDag-Erling Smørgrav #include <time.h>
55462c32cbSDag-Erling Smørgrav #include <unistd.h>
56bc5531deSDag-Erling Smørgrav #include <limits.h>
57761efaa7SDag-Erling Smørgrav 
58761efaa7SDag-Erling Smørgrav #include "xmalloc.h"
59d4af9e69SDag-Erling Smørgrav #include "dh.h"
60761efaa7SDag-Erling Smørgrav #include "log.h"
61f7167e0eSDag-Erling Smørgrav #include "misc.h"
62761efaa7SDag-Erling Smørgrav 
634a421b63SDag-Erling Smørgrav #include "openbsd-compat/openssl-compat.h"
644a421b63SDag-Erling Smørgrav 
65d95e11bfSDag-Erling Smørgrav /*
66d95e11bfSDag-Erling Smørgrav  * File output defines
67d95e11bfSDag-Erling Smørgrav  */
68d95e11bfSDag-Erling Smørgrav 
69d95e11bfSDag-Erling Smørgrav /* need line long enough for largest moduli plus headers */
70d95e11bfSDag-Erling Smørgrav #define QLINESIZE		(100+8192)
71d95e11bfSDag-Erling Smørgrav 
72efcad6b7SDag-Erling Smørgrav /*
73efcad6b7SDag-Erling Smørgrav  * Size: decimal.
74d95e11bfSDag-Erling Smørgrav  * Specifies the number of the most significant bit (0 to M).
75efcad6b7SDag-Erling Smørgrav  * WARNING: internally, usually 1 to N.
76d95e11bfSDag-Erling Smørgrav  */
77d95e11bfSDag-Erling Smørgrav #define QSIZE_MINIMUM		(511)
78d95e11bfSDag-Erling Smørgrav 
79d95e11bfSDag-Erling Smørgrav /*
80d95e11bfSDag-Erling Smørgrav  * Prime sieving defines
81d95e11bfSDag-Erling Smørgrav  */
82d95e11bfSDag-Erling Smørgrav 
83d95e11bfSDag-Erling Smørgrav /* Constant: assuming 8 bit bytes and 32 bit words */
84d95e11bfSDag-Erling Smørgrav #define SHIFT_BIT	(3)
85d95e11bfSDag-Erling Smørgrav #define SHIFT_BYTE	(2)
86d95e11bfSDag-Erling Smørgrav #define SHIFT_WORD	(SHIFT_BIT+SHIFT_BYTE)
87d95e11bfSDag-Erling Smørgrav #define SHIFT_MEGABYTE	(20)
88d95e11bfSDag-Erling Smørgrav #define SHIFT_MEGAWORD	(SHIFT_MEGABYTE-SHIFT_BYTE)
89d95e11bfSDag-Erling Smørgrav 
90d95e11bfSDag-Erling Smørgrav /*
91d74d50a8SDag-Erling Smørgrav  * Using virtual memory can cause thrashing.  This should be the largest
92d74d50a8SDag-Erling Smørgrav  * number that is supported without a large amount of disk activity --
93d74d50a8SDag-Erling Smørgrav  * that would increase the run time from hours to days or weeks!
94d74d50a8SDag-Erling Smørgrav  */
95d74d50a8SDag-Erling Smørgrav #define LARGE_MINIMUM	(8UL)	/* megabytes */
96d74d50a8SDag-Erling Smørgrav 
97d74d50a8SDag-Erling Smørgrav /*
98d74d50a8SDag-Erling Smørgrav  * Do not increase this number beyond the unsigned integer bit size.
99d74d50a8SDag-Erling Smørgrav  * Due to a multiple of 4, it must be LESS than 128 (yielding 2**30 bits).
100d74d50a8SDag-Erling Smørgrav  */
101d74d50a8SDag-Erling Smørgrav #define LARGE_MAXIMUM	(127UL)	/* megabytes */
102d74d50a8SDag-Erling Smørgrav 
103d74d50a8SDag-Erling Smørgrav /*
104d95e11bfSDag-Erling Smørgrav  * Constant: when used with 32-bit integers, the largest sieve prime
105d95e11bfSDag-Erling Smørgrav  * has to be less than 2**32.
106d95e11bfSDag-Erling Smørgrav  */
107d95e11bfSDag-Erling Smørgrav #define SMALL_MAXIMUM	(0xffffffffUL)
108d95e11bfSDag-Erling Smørgrav 
109d95e11bfSDag-Erling Smørgrav /* Constant: can sieve all primes less than 2**32, as 65537**2 > 2**32-1. */
110d95e11bfSDag-Erling Smørgrav #define TINY_NUMBER	(1UL<<16)
111d95e11bfSDag-Erling Smørgrav 
112d95e11bfSDag-Erling Smørgrav /* Ensure enough bit space for testing 2*q. */
113d95e11bfSDag-Erling Smørgrav #define TEST_MAXIMUM	(1UL<<16)
114d95e11bfSDag-Erling Smørgrav #define TEST_MINIMUM	(QSIZE_MINIMUM + 1)
115d95e11bfSDag-Erling Smørgrav /* real TEST_MINIMUM	(1UL << (SHIFT_WORD - TEST_POWER)) */
116d95e11bfSDag-Erling Smørgrav #define TEST_POWER	(3)	/* 2**n, n < SHIFT_WORD */
117d95e11bfSDag-Erling Smørgrav 
118d95e11bfSDag-Erling Smørgrav /* bit operations on 32-bit words */
119d95e11bfSDag-Erling Smørgrav #define BIT_CLEAR(a,n)	((a)[(n)>>SHIFT_WORD] &= ~(1L << ((n) & 31)))
120d95e11bfSDag-Erling Smørgrav #define BIT_SET(a,n)	((a)[(n)>>SHIFT_WORD] |= (1L << ((n) & 31)))
121d95e11bfSDag-Erling Smørgrav #define BIT_TEST(a,n)	((a)[(n)>>SHIFT_WORD] & (1L << ((n) & 31)))
122d95e11bfSDag-Erling Smørgrav 
123d95e11bfSDag-Erling Smørgrav /*
124d95e11bfSDag-Erling Smørgrav  * Prime testing defines
125d95e11bfSDag-Erling Smørgrav  */
126d95e11bfSDag-Erling Smørgrav 
127d74d50a8SDag-Erling Smørgrav /* Minimum number of primality tests to perform */
128d74d50a8SDag-Erling Smørgrav #define TRIAL_MINIMUM	(4)
129d74d50a8SDag-Erling Smørgrav 
130d95e11bfSDag-Erling Smørgrav /*
131d95e11bfSDag-Erling Smørgrav  * Sieving data (XXX - move to struct)
132d95e11bfSDag-Erling Smørgrav  */
133d95e11bfSDag-Erling Smørgrav 
134d95e11bfSDag-Erling Smørgrav /* sieve 2**16 */
135d95e11bfSDag-Erling Smørgrav static u_int32_t *TinySieve, tinybits;
136d95e11bfSDag-Erling Smørgrav 
137d95e11bfSDag-Erling Smørgrav /* sieve 2**30 in 2**16 parts */
138d95e11bfSDag-Erling Smørgrav static u_int32_t *SmallSieve, smallbits, smallbase;
139d95e11bfSDag-Erling Smørgrav 
140d95e11bfSDag-Erling Smørgrav /* sieve relative to the initial value */
141d95e11bfSDag-Erling Smørgrav static u_int32_t *LargeSieve, largewords, largetries, largenumbers;
142d95e11bfSDag-Erling Smørgrav static u_int32_t largebits, largememory;	/* megabytes */
143d95e11bfSDag-Erling Smørgrav static BIGNUM *largebase;
144d95e11bfSDag-Erling Smørgrav 
145043840dfSDag-Erling Smørgrav int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
146462c32cbSDag-Erling Smørgrav int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
147462c32cbSDag-Erling Smørgrav     unsigned long);
148d95e11bfSDag-Erling Smørgrav 
149d95e11bfSDag-Erling Smørgrav /*
150d95e11bfSDag-Erling Smørgrav  * print moduli out in consistent form,
151d95e11bfSDag-Erling Smørgrav  */
152d95e11bfSDag-Erling Smørgrav static int
qfileout(FILE * ofile,u_int32_t otype,u_int32_t otests,u_int32_t otries,u_int32_t osize,u_int32_t ogenerator,BIGNUM * omodulus)153d95e11bfSDag-Erling Smørgrav qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries,
154d95e11bfSDag-Erling Smørgrav     u_int32_t osize, u_int32_t ogenerator, BIGNUM * omodulus)
155d95e11bfSDag-Erling Smørgrav {
156d95e11bfSDag-Erling Smørgrav 	struct tm *gtm;
157d95e11bfSDag-Erling Smørgrav 	time_t time_now;
158d95e11bfSDag-Erling Smørgrav 	int res;
159d95e11bfSDag-Erling Smørgrav 
160d95e11bfSDag-Erling Smørgrav 	time(&time_now);
161d95e11bfSDag-Erling Smørgrav 	gtm = gmtime(&time_now);
16219261079SEd Maste 	if (gtm == NULL)
16319261079SEd Maste 		return -1;
164d95e11bfSDag-Erling Smørgrav 
165d95e11bfSDag-Erling Smørgrav 	res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
166d95e11bfSDag-Erling Smørgrav 	    gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday,
167d95e11bfSDag-Erling Smørgrav 	    gtm->tm_hour, gtm->tm_min, gtm->tm_sec,
168d95e11bfSDag-Erling Smørgrav 	    otype, otests, otries, osize, ogenerator);
169d95e11bfSDag-Erling Smørgrav 
170d95e11bfSDag-Erling Smørgrav 	if (res < 0)
171d95e11bfSDag-Erling Smørgrav 		return (-1);
172d95e11bfSDag-Erling Smørgrav 
173d95e11bfSDag-Erling Smørgrav 	if (BN_print_fp(ofile, omodulus) < 1)
174d95e11bfSDag-Erling Smørgrav 		return (-1);
175d95e11bfSDag-Erling Smørgrav 
176d95e11bfSDag-Erling Smørgrav 	res = fprintf(ofile, "\n");
177d95e11bfSDag-Erling Smørgrav 	fflush(ofile);
178d95e11bfSDag-Erling Smørgrav 
179d95e11bfSDag-Erling Smørgrav 	return (res > 0 ? 0 : -1);
180d95e11bfSDag-Erling Smørgrav }
181d95e11bfSDag-Erling Smørgrav 
182d95e11bfSDag-Erling Smørgrav 
183d95e11bfSDag-Erling Smørgrav /*
184d95e11bfSDag-Erling Smørgrav  ** Sieve p's and q's with small factors
185d95e11bfSDag-Erling Smørgrav  */
186d95e11bfSDag-Erling Smørgrav static void
sieve_large(u_int32_t s32)18738a52bd3SEd Maste sieve_large(u_int32_t s32)
188d95e11bfSDag-Erling Smørgrav {
18938a52bd3SEd Maste 	u_int64_t r, u, s = s32;
190d95e11bfSDag-Erling Smørgrav 
19138a52bd3SEd Maste 	debug3("sieve_large %u", s32);
192d95e11bfSDag-Erling Smørgrav 	largetries++;
193d95e11bfSDag-Erling Smørgrav 	/* r = largebase mod s */
19438a52bd3SEd Maste 	r = BN_mod_word(largebase, s32);
195d95e11bfSDag-Erling Smørgrav 	if (r == 0)
196d95e11bfSDag-Erling Smørgrav 		u = 0; /* s divides into largebase exactly */
197d95e11bfSDag-Erling Smørgrav 	else
198d95e11bfSDag-Erling Smørgrav 		u = s - r; /* largebase+u is first entry divisible by s */
199d95e11bfSDag-Erling Smørgrav 
20038a52bd3SEd Maste 	if (u < largebits * 2ULL) {
201d95e11bfSDag-Erling Smørgrav 		/*
202d95e11bfSDag-Erling Smørgrav 		 * The sieve omits p's and q's divisible by 2, so ensure that
203d95e11bfSDag-Erling Smørgrav 		 * largebase+u is odd. Then, step through the sieve in
204d95e11bfSDag-Erling Smørgrav 		 * increments of 2*s
205d95e11bfSDag-Erling Smørgrav 		 */
206d95e11bfSDag-Erling Smørgrav 		if (u & 0x1)
207d95e11bfSDag-Erling Smørgrav 			u += s; /* Make largebase+u odd, and u even */
208d95e11bfSDag-Erling Smørgrav 
209d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of 2*s */
210d95e11bfSDag-Erling Smørgrav 		for (u /= 2; u < largebits; u += s)
211d95e11bfSDag-Erling Smørgrav 			BIT_SET(LargeSieve, u);
212d95e11bfSDag-Erling Smørgrav 	}
213d95e11bfSDag-Erling Smørgrav 
214d95e11bfSDag-Erling Smørgrav 	/* r = p mod s */
215d95e11bfSDag-Erling Smørgrav 	r = (2 * r + 1) % s;
216d95e11bfSDag-Erling Smørgrav 	if (r == 0)
217d95e11bfSDag-Erling Smørgrav 		u = 0; /* s divides p exactly */
218d95e11bfSDag-Erling Smørgrav 	else
219d95e11bfSDag-Erling Smørgrav 		u = s - r; /* p+u is first entry divisible by s */
220d95e11bfSDag-Erling Smørgrav 
22138a52bd3SEd Maste 	if (u < largebits * 4ULL) {
222d95e11bfSDag-Erling Smørgrav 		/*
223d95e11bfSDag-Erling Smørgrav 		 * The sieve omits p's divisible by 4, so ensure that
224d95e11bfSDag-Erling Smørgrav 		 * largebase+u is not. Then, step through the sieve in
225d95e11bfSDag-Erling Smørgrav 		 * increments of 4*s
226d95e11bfSDag-Erling Smørgrav 		 */
227d95e11bfSDag-Erling Smørgrav 		while (u & 0x3) {
228d95e11bfSDag-Erling Smørgrav 			if (SMALL_MAXIMUM - u < s)
229d95e11bfSDag-Erling Smørgrav 				return;
230d95e11bfSDag-Erling Smørgrav 			u += s;
231d95e11bfSDag-Erling Smørgrav 		}
232d95e11bfSDag-Erling Smørgrav 
233d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of 4*s */
234d95e11bfSDag-Erling Smørgrav 		for (u /= 4; u < largebits; u += s)
235d95e11bfSDag-Erling Smørgrav 			BIT_SET(LargeSieve, u);
236d95e11bfSDag-Erling Smørgrav 	}
237d95e11bfSDag-Erling Smørgrav }
238d95e11bfSDag-Erling Smørgrav 
239d95e11bfSDag-Erling Smørgrav /*
240d74d50a8SDag-Erling Smørgrav  * list candidates for Sophie-Germain primes (where q = (p-1)/2)
241d95e11bfSDag-Erling Smørgrav  * to standard output.
242d95e11bfSDag-Erling Smørgrav  * The list is checked against small known primes (less than 2**30).
243d95e11bfSDag-Erling Smørgrav  */
244d95e11bfSDag-Erling Smørgrav int
gen_candidates(FILE * out,u_int32_t memory,u_int32_t power,BIGNUM * start)245043840dfSDag-Erling Smørgrav gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
246d95e11bfSDag-Erling Smørgrav {
247d95e11bfSDag-Erling Smørgrav 	BIGNUM *q;
248d95e11bfSDag-Erling Smørgrav 	u_int32_t j, r, s, t;
249d95e11bfSDag-Erling Smørgrav 	u_int32_t smallwords = TINY_NUMBER >> 6;
250d95e11bfSDag-Erling Smørgrav 	u_int32_t tinywords = TINY_NUMBER >> 6;
251d95e11bfSDag-Erling Smørgrav 	time_t time_start, time_stop;
252043840dfSDag-Erling Smørgrav 	u_int32_t i;
253043840dfSDag-Erling Smørgrav 	int ret = 0;
254d95e11bfSDag-Erling Smørgrav 
255d95e11bfSDag-Erling Smørgrav 	largememory = memory;
256d95e11bfSDag-Erling Smørgrav 
257d74d50a8SDag-Erling Smørgrav 	if (memory != 0 &&
258d74d50a8SDag-Erling Smørgrav 	    (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
259d74d50a8SDag-Erling Smørgrav 		error("Invalid memory amount (min %ld, max %ld)",
260d74d50a8SDag-Erling Smørgrav 		    LARGE_MINIMUM, LARGE_MAXIMUM);
261d74d50a8SDag-Erling Smørgrav 		return (-1);
262d74d50a8SDag-Erling Smørgrav 	}
263d74d50a8SDag-Erling Smørgrav 
264d95e11bfSDag-Erling Smørgrav 	/*
265d95e11bfSDag-Erling Smørgrav 	 * Set power to the length in bits of the prime to be generated.
266d95e11bfSDag-Erling Smørgrav 	 * This is changed to 1 less than the desired safe prime moduli p.
267d95e11bfSDag-Erling Smørgrav 	 */
268d95e11bfSDag-Erling Smørgrav 	if (power > TEST_MAXIMUM) {
269d95e11bfSDag-Erling Smørgrav 		error("Too many bits: %u > %lu", power, TEST_MAXIMUM);
270d95e11bfSDag-Erling Smørgrav 		return (-1);
271d95e11bfSDag-Erling Smørgrav 	} else if (power < TEST_MINIMUM) {
272d95e11bfSDag-Erling Smørgrav 		error("Too few bits: %u < %u", power, TEST_MINIMUM);
273d95e11bfSDag-Erling Smørgrav 		return (-1);
274d95e11bfSDag-Erling Smørgrav 	}
275d95e11bfSDag-Erling Smørgrav 	power--; /* decrement before squaring */
276d95e11bfSDag-Erling Smørgrav 
277d95e11bfSDag-Erling Smørgrav 	/*
278d95e11bfSDag-Erling Smørgrav 	 * The density of ordinary primes is on the order of 1/bits, so the
279d95e11bfSDag-Erling Smørgrav 	 * density of safe primes should be about (1/bits)**2. Set test range
280d95e11bfSDag-Erling Smørgrav 	 * to something well above bits**2 to be reasonably sure (but not
281d95e11bfSDag-Erling Smørgrav 	 * guaranteed) of catching at least one safe prime.
282d95e11bfSDag-Erling Smørgrav 	 */
283d95e11bfSDag-Erling Smørgrav 	largewords = ((power * power) >> (SHIFT_WORD - TEST_POWER));
284d95e11bfSDag-Erling Smørgrav 
285d95e11bfSDag-Erling Smørgrav 	/*
286d95e11bfSDag-Erling Smørgrav 	 * Need idea of how much memory is available. We don't have to use all
287d95e11bfSDag-Erling Smørgrav 	 * of it.
288d95e11bfSDag-Erling Smørgrav 	 */
289d95e11bfSDag-Erling Smørgrav 	if (largememory > LARGE_MAXIMUM) {
290d95e11bfSDag-Erling Smørgrav 		logit("Limited memory: %u MB; limit %lu MB",
291d95e11bfSDag-Erling Smørgrav 		    largememory, LARGE_MAXIMUM);
292d95e11bfSDag-Erling Smørgrav 		largememory = LARGE_MAXIMUM;
293d95e11bfSDag-Erling Smørgrav 	}
294d95e11bfSDag-Erling Smørgrav 
295d95e11bfSDag-Erling Smørgrav 	if (largewords <= (largememory << SHIFT_MEGAWORD)) {
296d95e11bfSDag-Erling Smørgrav 		logit("Increased memory: %u MB; need %u bytes",
297d95e11bfSDag-Erling Smørgrav 		    largememory, (largewords << SHIFT_BYTE));
298d95e11bfSDag-Erling Smørgrav 		largewords = (largememory << SHIFT_MEGAWORD);
299d95e11bfSDag-Erling Smørgrav 	} else if (largememory > 0) {
300d95e11bfSDag-Erling Smørgrav 		logit("Decreased memory: %u MB; want %u bytes",
301d95e11bfSDag-Erling Smørgrav 		    largememory, (largewords << SHIFT_BYTE));
302d95e11bfSDag-Erling Smørgrav 		largewords = (largememory << SHIFT_MEGAWORD);
303d95e11bfSDag-Erling Smørgrav 	}
304d95e11bfSDag-Erling Smørgrav 
305761efaa7SDag-Erling Smørgrav 	TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
306d95e11bfSDag-Erling Smørgrav 	tinybits = tinywords << SHIFT_WORD;
307d95e11bfSDag-Erling Smørgrav 
308761efaa7SDag-Erling Smørgrav 	SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
309d95e11bfSDag-Erling Smørgrav 	smallbits = smallwords << SHIFT_WORD;
310d95e11bfSDag-Erling Smørgrav 
311d95e11bfSDag-Erling Smørgrav 	/*
312d95e11bfSDag-Erling Smørgrav 	 * dynamically determine available memory
313d95e11bfSDag-Erling Smørgrav 	 */
314d95e11bfSDag-Erling Smørgrav 	while ((LargeSieve = calloc(largewords, sizeof(u_int32_t))) == NULL)
315d95e11bfSDag-Erling Smørgrav 		largewords -= (1L << (SHIFT_MEGAWORD - 2)); /* 1/4 MB chunks */
316d95e11bfSDag-Erling Smørgrav 
317d95e11bfSDag-Erling Smørgrav 	largebits = largewords << SHIFT_WORD;
318d95e11bfSDag-Erling Smørgrav 	largenumbers = largebits * 2;	/* even numbers excluded */
319d95e11bfSDag-Erling Smørgrav 
320d95e11bfSDag-Erling Smørgrav 	/* validation check: count the number of primes tried */
321d95e11bfSDag-Erling Smørgrav 	largetries = 0;
32292eb0aa1SDag-Erling Smørgrav 	if ((q = BN_new()) == NULL)
32392eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
324d95e11bfSDag-Erling Smørgrav 
325d95e11bfSDag-Erling Smørgrav 	/*
326d95e11bfSDag-Erling Smørgrav 	 * Generate random starting point for subprime search, or use
327d95e11bfSDag-Erling Smørgrav 	 * specified parameter.
328d95e11bfSDag-Erling Smørgrav 	 */
32992eb0aa1SDag-Erling Smørgrav 	if ((largebase = BN_new()) == NULL)
33092eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
33192eb0aa1SDag-Erling Smørgrav 	if (start == NULL) {
33292eb0aa1SDag-Erling Smørgrav 		if (BN_rand(largebase, power, 1, 1) == 0)
33392eb0aa1SDag-Erling Smørgrav 			fatal("BN_rand failed");
33492eb0aa1SDag-Erling Smørgrav 	} else {
33592eb0aa1SDag-Erling Smørgrav 		if (BN_copy(largebase, start) == NULL)
33692eb0aa1SDag-Erling Smørgrav 			fatal("BN_copy: failed");
33792eb0aa1SDag-Erling Smørgrav 	}
338d95e11bfSDag-Erling Smørgrav 
339d95e11bfSDag-Erling Smørgrav 	/* ensure odd */
34092eb0aa1SDag-Erling Smørgrav 	if (BN_set_bit(largebase, 0) == 0)
34192eb0aa1SDag-Erling Smørgrav 		fatal("BN_set_bit: failed");
342d95e11bfSDag-Erling Smørgrav 
343d95e11bfSDag-Erling Smørgrav 	time(&time_start);
344d95e11bfSDag-Erling Smørgrav 
345d95e11bfSDag-Erling Smørgrav 	logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start),
346d95e11bfSDag-Erling Smørgrav 	    largenumbers, power);
347d95e11bfSDag-Erling Smørgrav 	debug2("start point: 0x%s", BN_bn2hex(largebase));
348d95e11bfSDag-Erling Smørgrav 
349d95e11bfSDag-Erling Smørgrav 	/*
350d95e11bfSDag-Erling Smørgrav 	 * TinySieve
351d95e11bfSDag-Erling Smørgrav 	 */
352d95e11bfSDag-Erling Smørgrav 	for (i = 0; i < tinybits; i++) {
353d95e11bfSDag-Erling Smørgrav 		if (BIT_TEST(TinySieve, i))
354d95e11bfSDag-Erling Smørgrav 			continue; /* 2*i+3 is composite */
355d95e11bfSDag-Erling Smørgrav 
356d95e11bfSDag-Erling Smørgrav 		/* The next tiny prime */
357d95e11bfSDag-Erling Smørgrav 		t = 2 * i + 3;
358d95e11bfSDag-Erling Smørgrav 
359d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of t */
360d95e11bfSDag-Erling Smørgrav 		for (j = i + t; j < tinybits; j += t)
361d95e11bfSDag-Erling Smørgrav 			BIT_SET(TinySieve, j);
362d95e11bfSDag-Erling Smørgrav 
363d95e11bfSDag-Erling Smørgrav 		sieve_large(t);
364d95e11bfSDag-Erling Smørgrav 	}
365d95e11bfSDag-Erling Smørgrav 
366d95e11bfSDag-Erling Smørgrav 	/*
367d95e11bfSDag-Erling Smørgrav 	 * Start the small block search at the next possible prime. To avoid
368d95e11bfSDag-Erling Smørgrav 	 * fencepost errors, the last pass is skipped.
369d95e11bfSDag-Erling Smørgrav 	 */
370d95e11bfSDag-Erling Smørgrav 	for (smallbase = TINY_NUMBER + 3;
371d95e11bfSDag-Erling Smørgrav 	    smallbase < (SMALL_MAXIMUM - TINY_NUMBER);
372d95e11bfSDag-Erling Smørgrav 	    smallbase += TINY_NUMBER) {
373d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < tinybits; i++) {
374d95e11bfSDag-Erling Smørgrav 			if (BIT_TEST(TinySieve, i))
375d95e11bfSDag-Erling Smørgrav 				continue; /* 2*i+3 is composite */
376d95e11bfSDag-Erling Smørgrav 
377d95e11bfSDag-Erling Smørgrav 			/* The next tiny prime */
378d95e11bfSDag-Erling Smørgrav 			t = 2 * i + 3;
379d95e11bfSDag-Erling Smørgrav 			r = smallbase % t;
380d95e11bfSDag-Erling Smørgrav 
381d95e11bfSDag-Erling Smørgrav 			if (r == 0) {
382d95e11bfSDag-Erling Smørgrav 				s = 0; /* t divides into smallbase exactly */
383d95e11bfSDag-Erling Smørgrav 			} else {
384d95e11bfSDag-Erling Smørgrav 				/* smallbase+s is first entry divisible by t */
385d95e11bfSDag-Erling Smørgrav 				s = t - r;
386d95e11bfSDag-Erling Smørgrav 			}
387d95e11bfSDag-Erling Smørgrav 
388d95e11bfSDag-Erling Smørgrav 			/*
389d95e11bfSDag-Erling Smørgrav 			 * The sieve omits even numbers, so ensure that
390d95e11bfSDag-Erling Smørgrav 			 * smallbase+s is odd. Then, step through the sieve
391d95e11bfSDag-Erling Smørgrav 			 * in increments of 2*t
392d95e11bfSDag-Erling Smørgrav 			 */
393d95e11bfSDag-Erling Smørgrav 			if (s & 1)
394d95e11bfSDag-Erling Smørgrav 				s += t; /* Make smallbase+s odd, and s even */
395d95e11bfSDag-Erling Smørgrav 
396d95e11bfSDag-Erling Smørgrav 			/* Mark all multiples of 2*t */
397d95e11bfSDag-Erling Smørgrav 			for (s /= 2; s < smallbits; s += t)
398d95e11bfSDag-Erling Smørgrav 				BIT_SET(SmallSieve, s);
399d95e11bfSDag-Erling Smørgrav 		}
400d95e11bfSDag-Erling Smørgrav 
401d95e11bfSDag-Erling Smørgrav 		/*
402d95e11bfSDag-Erling Smørgrav 		 * SmallSieve
403d95e11bfSDag-Erling Smørgrav 		 */
404d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < smallbits; i++) {
405d95e11bfSDag-Erling Smørgrav 			if (BIT_TEST(SmallSieve, i))
406d95e11bfSDag-Erling Smørgrav 				continue; /* 2*i+smallbase is composite */
407d95e11bfSDag-Erling Smørgrav 
408d95e11bfSDag-Erling Smørgrav 			/* The next small prime */
409d95e11bfSDag-Erling Smørgrav 			sieve_large((2 * i) + smallbase);
410d95e11bfSDag-Erling Smørgrav 		}
411d95e11bfSDag-Erling Smørgrav 
412d95e11bfSDag-Erling Smørgrav 		memset(SmallSieve, 0, smallwords << SHIFT_BYTE);
413d95e11bfSDag-Erling Smørgrav 	}
414d95e11bfSDag-Erling Smørgrav 
415d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
416d95e11bfSDag-Erling Smørgrav 
41747dd1d1bSDag-Erling Smørgrav 	logit("%.24s Sieved with %u small primes in %lld seconds",
41847dd1d1bSDag-Erling Smørgrav 	    ctime(&time_stop), largetries, (long long)(time_stop - time_start));
419d95e11bfSDag-Erling Smørgrav 
420d95e11bfSDag-Erling Smørgrav 	for (j = r = 0; j < largebits; j++) {
421d95e11bfSDag-Erling Smørgrav 		if (BIT_TEST(LargeSieve, j))
422d95e11bfSDag-Erling Smørgrav 			continue; /* Definitely composite, skip */
423d95e11bfSDag-Erling Smørgrav 
424d95e11bfSDag-Erling Smørgrav 		debug2("test q = largebase+%u", 2 * j);
42592eb0aa1SDag-Erling Smørgrav 		if (BN_set_word(q, 2 * j) == 0)
42692eb0aa1SDag-Erling Smørgrav 			fatal("BN_set_word failed");
42792eb0aa1SDag-Erling Smørgrav 		if (BN_add(q, q, largebase) == 0)
42892eb0aa1SDag-Erling Smørgrav 			fatal("BN_add failed");
429d4af9e69SDag-Erling Smørgrav 		if (qfileout(out, MODULI_TYPE_SOPHIE_GERMAIN,
430d4af9e69SDag-Erling Smørgrav 		    MODULI_TESTS_SIEVE, largetries,
431d4af9e69SDag-Erling Smørgrav 		    (power - 1) /* MSB */, (0), q) == -1) {
432d95e11bfSDag-Erling Smørgrav 			ret = -1;
433d95e11bfSDag-Erling Smørgrav 			break;
434d95e11bfSDag-Erling Smørgrav 		}
435d95e11bfSDag-Erling Smørgrav 
436d95e11bfSDag-Erling Smørgrav 		r++; /* count q */
437d95e11bfSDag-Erling Smørgrav 	}
438d95e11bfSDag-Erling Smørgrav 
439d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
440d95e11bfSDag-Erling Smørgrav 
441e4a9863fSDag-Erling Smørgrav 	free(LargeSieve);
442e4a9863fSDag-Erling Smørgrav 	free(SmallSieve);
443e4a9863fSDag-Erling Smørgrav 	free(TinySieve);
444d95e11bfSDag-Erling Smørgrav 
445d95e11bfSDag-Erling Smørgrav 	logit("%.24s Found %u candidates", ctime(&time_stop), r);
446d95e11bfSDag-Erling Smørgrav 
447d95e11bfSDag-Erling Smørgrav 	return (ret);
448d95e11bfSDag-Erling Smørgrav }
449d95e11bfSDag-Erling Smørgrav 
450462c32cbSDag-Erling Smørgrav static void
write_checkpoint(char * cpfile,u_int32_t lineno)451462c32cbSDag-Erling Smørgrav write_checkpoint(char *cpfile, u_int32_t lineno)
452462c32cbSDag-Erling Smørgrav {
453462c32cbSDag-Erling Smørgrav 	FILE *fp;
454bc5531deSDag-Erling Smørgrav 	char tmp[PATH_MAX];
455*4d3fc8b0SEd Maste 	int r, writeok, closeok;
456462c32cbSDag-Erling Smørgrav 
457462c32cbSDag-Erling Smørgrav 	r = snprintf(tmp, sizeof(tmp), "%s.XXXXXXXXXX", cpfile);
45819261079SEd Maste 	if (r < 0 || r >= PATH_MAX) {
459462c32cbSDag-Erling Smørgrav 		logit("write_checkpoint: temp pathname too long");
460462c32cbSDag-Erling Smørgrav 		return;
461462c32cbSDag-Erling Smørgrav 	}
462462c32cbSDag-Erling Smørgrav 	if ((r = mkstemp(tmp)) == -1) {
463462c32cbSDag-Erling Smørgrav 		logit("mkstemp(%s): %s", tmp, strerror(errno));
464462c32cbSDag-Erling Smørgrav 		return;
465462c32cbSDag-Erling Smørgrav 	}
466462c32cbSDag-Erling Smørgrav 	if ((fp = fdopen(r, "w")) == NULL) {
467462c32cbSDag-Erling Smørgrav 		logit("write_checkpoint: fdopen: %s", strerror(errno));
468bc5531deSDag-Erling Smørgrav 		unlink(tmp);
469462c32cbSDag-Erling Smørgrav 		close(r);
470462c32cbSDag-Erling Smørgrav 		return;
471462c32cbSDag-Erling Smørgrav 	}
472*4d3fc8b0SEd Maste 	writeok = (fprintf(fp, "%lu\n", (unsigned long)lineno) > 0);
473*4d3fc8b0SEd Maste 	closeok = (fclose(fp) == 0);
474*4d3fc8b0SEd Maste 	if (writeok && closeok && rename(tmp, cpfile) == 0) {
475462c32cbSDag-Erling Smørgrav 		debug3("wrote checkpoint line %lu to '%s'",
476462c32cbSDag-Erling Smørgrav 		    (unsigned long)lineno, cpfile);
477*4d3fc8b0SEd Maste 	} else {
478462c32cbSDag-Erling Smørgrav 		logit("failed to write to checkpoint file '%s': %s", cpfile,
479462c32cbSDag-Erling Smørgrav 		    strerror(errno));
480*4d3fc8b0SEd Maste 		(void)unlink(tmp);
481*4d3fc8b0SEd Maste 	}
482462c32cbSDag-Erling Smørgrav }
483462c32cbSDag-Erling Smørgrav 
484462c32cbSDag-Erling Smørgrav static unsigned long
read_checkpoint(char * cpfile)485462c32cbSDag-Erling Smørgrav read_checkpoint(char *cpfile)
486462c32cbSDag-Erling Smørgrav {
487462c32cbSDag-Erling Smørgrav 	FILE *fp;
488462c32cbSDag-Erling Smørgrav 	unsigned long lineno = 0;
489462c32cbSDag-Erling Smørgrav 
490462c32cbSDag-Erling Smørgrav 	if ((fp = fopen(cpfile, "r")) == NULL)
491462c32cbSDag-Erling Smørgrav 		return 0;
492462c32cbSDag-Erling Smørgrav 	if (fscanf(fp, "%lu\n", &lineno) < 1)
493462c32cbSDag-Erling Smørgrav 		logit("Failed to load checkpoint from '%s'", cpfile);
494462c32cbSDag-Erling Smørgrav 	else
495462c32cbSDag-Erling Smørgrav 		logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno);
496462c32cbSDag-Erling Smørgrav 	fclose(fp);
497462c32cbSDag-Erling Smørgrav 	return lineno;
498462c32cbSDag-Erling Smørgrav }
499462c32cbSDag-Erling Smørgrav 
500f7167e0eSDag-Erling Smørgrav static unsigned long
count_lines(FILE * f)501f7167e0eSDag-Erling Smørgrav count_lines(FILE *f)
502f7167e0eSDag-Erling Smørgrav {
503f7167e0eSDag-Erling Smørgrav 	unsigned long count = 0;
504f7167e0eSDag-Erling Smørgrav 	char lp[QLINESIZE + 1];
505f7167e0eSDag-Erling Smørgrav 
506f7167e0eSDag-Erling Smørgrav 	if (fseek(f, 0, SEEK_SET) != 0) {
507f7167e0eSDag-Erling Smørgrav 		debug("input file is not seekable");
508f7167e0eSDag-Erling Smørgrav 		return ULONG_MAX;
509f7167e0eSDag-Erling Smørgrav 	}
510f7167e0eSDag-Erling Smørgrav 	while (fgets(lp, QLINESIZE + 1, f) != NULL)
511f7167e0eSDag-Erling Smørgrav 		count++;
512f7167e0eSDag-Erling Smørgrav 	rewind(f);
513f7167e0eSDag-Erling Smørgrav 	debug("input file has %lu lines", count);
514f7167e0eSDag-Erling Smørgrav 	return count;
515f7167e0eSDag-Erling Smørgrav }
516f7167e0eSDag-Erling Smørgrav 
517f7167e0eSDag-Erling Smørgrav static char *
fmt_time(time_t seconds)518f7167e0eSDag-Erling Smørgrav fmt_time(time_t seconds)
519f7167e0eSDag-Erling Smørgrav {
520f7167e0eSDag-Erling Smørgrav 	int day, hr, min;
521f7167e0eSDag-Erling Smørgrav 	static char buf[128];
522f7167e0eSDag-Erling Smørgrav 
523f7167e0eSDag-Erling Smørgrav 	min = (seconds / 60) % 60;
524f7167e0eSDag-Erling Smørgrav 	hr = (seconds / 60 / 60) % 24;
525f7167e0eSDag-Erling Smørgrav 	day = seconds / 60 / 60 / 24;
526f7167e0eSDag-Erling Smørgrav 	if (day > 0)
527f7167e0eSDag-Erling Smørgrav 		snprintf(buf, sizeof buf, "%dd %d:%02d", day, hr, min);
528f7167e0eSDag-Erling Smørgrav 	else
529f7167e0eSDag-Erling Smørgrav 		snprintf(buf, sizeof buf, "%d:%02d", hr, min);
530f7167e0eSDag-Erling Smørgrav 	return buf;
531f7167e0eSDag-Erling Smørgrav }
532f7167e0eSDag-Erling Smørgrav 
533f7167e0eSDag-Erling Smørgrav static void
print_progress(unsigned long start_lineno,unsigned long current_lineno,unsigned long end_lineno)534f7167e0eSDag-Erling Smørgrav print_progress(unsigned long start_lineno, unsigned long current_lineno,
535f7167e0eSDag-Erling Smørgrav     unsigned long end_lineno)
536f7167e0eSDag-Erling Smørgrav {
537f7167e0eSDag-Erling Smørgrav 	static time_t time_start, time_prev;
538f7167e0eSDag-Erling Smørgrav 	time_t time_now, elapsed;
539f7167e0eSDag-Erling Smørgrav 	unsigned long num_to_process, processed, remaining, percent, eta;
540f7167e0eSDag-Erling Smørgrav 	double time_per_line;
541f7167e0eSDag-Erling Smørgrav 	char *eta_str;
542f7167e0eSDag-Erling Smørgrav 
543f7167e0eSDag-Erling Smørgrav 	time_now = monotime();
544f7167e0eSDag-Erling Smørgrav 	if (time_start == 0) {
545f7167e0eSDag-Erling Smørgrav 		time_start = time_prev = time_now;
546f7167e0eSDag-Erling Smørgrav 		return;
547f7167e0eSDag-Erling Smørgrav 	}
548f7167e0eSDag-Erling Smørgrav 	/* print progress after 1m then once per 5m */
549f7167e0eSDag-Erling Smørgrav 	if (time_now - time_prev < 5 * 60)
550f7167e0eSDag-Erling Smørgrav 		return;
551f7167e0eSDag-Erling Smørgrav 	time_prev = time_now;
552f7167e0eSDag-Erling Smørgrav 	elapsed = time_now - time_start;
553f7167e0eSDag-Erling Smørgrav 	processed = current_lineno - start_lineno;
554f7167e0eSDag-Erling Smørgrav 	remaining = end_lineno - current_lineno;
555f7167e0eSDag-Erling Smørgrav 	num_to_process = end_lineno - start_lineno;
556f7167e0eSDag-Erling Smørgrav 	time_per_line = (double)elapsed / processed;
557f7167e0eSDag-Erling Smørgrav 	/* if we don't know how many we're processing just report count+time */
558f7167e0eSDag-Erling Smørgrav 	time(&time_now);
559f7167e0eSDag-Erling Smørgrav 	if (end_lineno == ULONG_MAX) {
560f7167e0eSDag-Erling Smørgrav 		logit("%.24s processed %lu in %s", ctime(&time_now),
561f7167e0eSDag-Erling Smørgrav 		    processed, fmt_time(elapsed));
562f7167e0eSDag-Erling Smørgrav 		return;
563f7167e0eSDag-Erling Smørgrav 	}
564f7167e0eSDag-Erling Smørgrav 	percent = 100 * processed / num_to_process;
565f7167e0eSDag-Erling Smørgrav 	eta = time_per_line * remaining;
566f7167e0eSDag-Erling Smørgrav 	eta_str = xstrdup(fmt_time(eta));
567f7167e0eSDag-Erling Smørgrav 	logit("%.24s processed %lu of %lu (%lu%%) in %s, ETA %s",
568f7167e0eSDag-Erling Smørgrav 	    ctime(&time_now), processed, num_to_process, percent,
569f7167e0eSDag-Erling Smørgrav 	    fmt_time(elapsed), eta_str);
570f7167e0eSDag-Erling Smørgrav 	free(eta_str);
571f7167e0eSDag-Erling Smørgrav }
572f7167e0eSDag-Erling Smørgrav 
573d95e11bfSDag-Erling Smørgrav /*
574d95e11bfSDag-Erling Smørgrav  * perform a Miller-Rabin primality test
575d95e11bfSDag-Erling Smørgrav  * on the list of candidates
576d95e11bfSDag-Erling Smørgrav  * (checking both q and p)
577d95e11bfSDag-Erling Smørgrav  * The result is a list of so-call "safe" primes
578d95e11bfSDag-Erling Smørgrav  */
579d95e11bfSDag-Erling Smørgrav int
prime_test(FILE * in,FILE * out,u_int32_t trials,u_int32_t generator_wanted,char * checkpoint_file,unsigned long start_lineno,unsigned long num_lines)580462c32cbSDag-Erling Smørgrav prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
581462c32cbSDag-Erling Smørgrav     char *checkpoint_file, unsigned long start_lineno, unsigned long num_lines)
582d95e11bfSDag-Erling Smørgrav {
583d95e11bfSDag-Erling Smørgrav 	BIGNUM *q, *p, *a;
584d95e11bfSDag-Erling Smørgrav 	char *cp, *lp;
585d95e11bfSDag-Erling Smørgrav 	u_int32_t count_in = 0, count_out = 0, count_possible = 0;
586d95e11bfSDag-Erling Smørgrav 	u_int32_t generator_known, in_tests, in_tries, in_type, in_size;
587462c32cbSDag-Erling Smørgrav 	unsigned long last_processed = 0, end_lineno;
588d95e11bfSDag-Erling Smørgrav 	time_t time_start, time_stop;
58919261079SEd Maste 	int res, is_prime;
590d95e11bfSDag-Erling Smørgrav 
591d74d50a8SDag-Erling Smørgrav 	if (trials < TRIAL_MINIMUM) {
592d74d50a8SDag-Erling Smørgrav 		error("Minimum primality trials is %d", TRIAL_MINIMUM);
593d74d50a8SDag-Erling Smørgrav 		return (-1);
594d74d50a8SDag-Erling Smørgrav 	}
595d74d50a8SDag-Erling Smørgrav 
596f7167e0eSDag-Erling Smørgrav 	if (num_lines == 0)
597f7167e0eSDag-Erling Smørgrav 		end_lineno = count_lines(in);
598f7167e0eSDag-Erling Smørgrav 	else
599f7167e0eSDag-Erling Smørgrav 		end_lineno = start_lineno + num_lines;
600f7167e0eSDag-Erling Smørgrav 
601d95e11bfSDag-Erling Smørgrav 	time(&time_start);
602d95e11bfSDag-Erling Smørgrav 
60392eb0aa1SDag-Erling Smørgrav 	if ((p = BN_new()) == NULL)
60492eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
60592eb0aa1SDag-Erling Smørgrav 	if ((q = BN_new()) == NULL)
60692eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
607d95e11bfSDag-Erling Smørgrav 
608d95e11bfSDag-Erling Smørgrav 	debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
609d95e11bfSDag-Erling Smørgrav 	    ctime(&time_start), trials, generator_wanted);
610d95e11bfSDag-Erling Smørgrav 
611462c32cbSDag-Erling Smørgrav 	if (checkpoint_file != NULL)
612462c32cbSDag-Erling Smørgrav 		last_processed = read_checkpoint(checkpoint_file);
613ca86bcf2SDag-Erling Smørgrav 	last_processed = start_lineno = MAXIMUM(last_processed, start_lineno);
614f7167e0eSDag-Erling Smørgrav 	if (end_lineno == ULONG_MAX)
615f7167e0eSDag-Erling Smørgrav 		debug("process from line %lu from pipe", last_processed);
616462c32cbSDag-Erling Smørgrav 	else
617f7167e0eSDag-Erling Smørgrav 		debug("process from line %lu to line %lu", last_processed,
618f7167e0eSDag-Erling Smørgrav 		    end_lineno);
619462c32cbSDag-Erling Smørgrav 
620d95e11bfSDag-Erling Smørgrav 	res = 0;
621d95e11bfSDag-Erling Smørgrav 	lp = xmalloc(QLINESIZE + 1);
622462c32cbSDag-Erling Smørgrav 	while (fgets(lp, QLINESIZE + 1, in) != NULL && count_in < end_lineno) {
623d95e11bfSDag-Erling Smørgrav 		count_in++;
624462c32cbSDag-Erling Smørgrav 		if (count_in <= last_processed) {
625f7167e0eSDag-Erling Smørgrav 			debug3("skipping line %u, before checkpoint or "
626f7167e0eSDag-Erling Smørgrav 			    "specified start line", count_in);
627462c32cbSDag-Erling Smørgrav 			continue;
628462c32cbSDag-Erling Smørgrav 		}
629f7167e0eSDag-Erling Smørgrav 		if (checkpoint_file != NULL)
630462c32cbSDag-Erling Smørgrav 			write_checkpoint(checkpoint_file, count_in);
631f7167e0eSDag-Erling Smørgrav 		print_progress(start_lineno, count_in, end_lineno);
632d4af9e69SDag-Erling Smørgrav 		if (strlen(lp) < 14 || *lp == '!' || *lp == '#') {
633d95e11bfSDag-Erling Smørgrav 			debug2("%10u: comment or short line", count_in);
634d95e11bfSDag-Erling Smørgrav 			continue;
635d95e11bfSDag-Erling Smørgrav 		}
636d95e11bfSDag-Erling Smørgrav 
637d95e11bfSDag-Erling Smørgrav 		/* XXX - fragile parser */
638d95e11bfSDag-Erling Smørgrav 		/* time */
639d95e11bfSDag-Erling Smørgrav 		cp = &lp[14];	/* (skip) */
640d95e11bfSDag-Erling Smørgrav 
641d95e11bfSDag-Erling Smørgrav 		/* type */
642d95e11bfSDag-Erling Smørgrav 		in_type = strtoul(cp, &cp, 10);
643d95e11bfSDag-Erling Smørgrav 
644d95e11bfSDag-Erling Smørgrav 		/* tests */
645d95e11bfSDag-Erling Smørgrav 		in_tests = strtoul(cp, &cp, 10);
646d95e11bfSDag-Erling Smørgrav 
647d4af9e69SDag-Erling Smørgrav 		if (in_tests & MODULI_TESTS_COMPOSITE) {
648d95e11bfSDag-Erling Smørgrav 			debug2("%10u: known composite", count_in);
649d95e11bfSDag-Erling Smørgrav 			continue;
650d95e11bfSDag-Erling Smørgrav 		}
651efcad6b7SDag-Erling Smørgrav 
652d95e11bfSDag-Erling Smørgrav 		/* tries */
653d95e11bfSDag-Erling Smørgrav 		in_tries = strtoul(cp, &cp, 10);
654d95e11bfSDag-Erling Smørgrav 
655d95e11bfSDag-Erling Smørgrav 		/* size (most significant bit) */
656d95e11bfSDag-Erling Smørgrav 		in_size = strtoul(cp, &cp, 10);
657d95e11bfSDag-Erling Smørgrav 
658d95e11bfSDag-Erling Smørgrav 		/* generator (hex) */
659d95e11bfSDag-Erling Smørgrav 		generator_known = strtoul(cp, &cp, 16);
660d95e11bfSDag-Erling Smørgrav 
661d95e11bfSDag-Erling Smørgrav 		/* Skip white space */
662d95e11bfSDag-Erling Smørgrav 		cp += strspn(cp, " ");
663d95e11bfSDag-Erling Smørgrav 
664d95e11bfSDag-Erling Smørgrav 		/* modulus (hex) */
665d95e11bfSDag-Erling Smørgrav 		switch (in_type) {
666d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SOPHIE_GERMAIN:
667d74d50a8SDag-Erling Smørgrav 			debug2("%10u: (%u) Sophie-Germain", count_in, in_type);
668d95e11bfSDag-Erling Smørgrav 			a = q;
66992eb0aa1SDag-Erling Smørgrav 			if (BN_hex2bn(&a, cp) == 0)
67092eb0aa1SDag-Erling Smørgrav 				fatal("BN_hex2bn failed");
671d95e11bfSDag-Erling Smørgrav 			/* p = 2*q + 1 */
67292eb0aa1SDag-Erling Smørgrav 			if (BN_lshift(p, q, 1) == 0)
67392eb0aa1SDag-Erling Smørgrav 				fatal("BN_lshift failed");
67492eb0aa1SDag-Erling Smørgrav 			if (BN_add_word(p, 1) == 0)
67592eb0aa1SDag-Erling Smørgrav 				fatal("BN_add_word failed");
676d95e11bfSDag-Erling Smørgrav 			in_size += 1;
677d95e11bfSDag-Erling Smørgrav 			generator_known = 0;
678d95e11bfSDag-Erling Smørgrav 			break;
679d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_UNSTRUCTURED:
680d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SAFE:
681d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SCHNORR:
682d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_STRONG:
683d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_UNKNOWN:
684d95e11bfSDag-Erling Smørgrav 			debug2("%10u: (%u)", count_in, in_type);
685d95e11bfSDag-Erling Smørgrav 			a = p;
68692eb0aa1SDag-Erling Smørgrav 			if (BN_hex2bn(&a, cp) == 0)
68792eb0aa1SDag-Erling Smørgrav 				fatal("BN_hex2bn failed");
688d95e11bfSDag-Erling Smørgrav 			/* q = (p-1) / 2 */
68992eb0aa1SDag-Erling Smørgrav 			if (BN_rshift(q, p, 1) == 0)
69092eb0aa1SDag-Erling Smørgrav 				fatal("BN_rshift failed");
691d95e11bfSDag-Erling Smørgrav 			break;
692efcad6b7SDag-Erling Smørgrav 		default:
693efcad6b7SDag-Erling Smørgrav 			debug2("Unknown prime type");
694efcad6b7SDag-Erling Smørgrav 			break;
695d95e11bfSDag-Erling Smørgrav 		}
696d95e11bfSDag-Erling Smørgrav 
697d95e11bfSDag-Erling Smørgrav 		/*
698d95e11bfSDag-Erling Smørgrav 		 * due to earlier inconsistencies in interpretation, check
699d95e11bfSDag-Erling Smørgrav 		 * the proposed bit size.
700d95e11bfSDag-Erling Smørgrav 		 */
701043840dfSDag-Erling Smørgrav 		if ((u_int32_t)BN_num_bits(p) != (in_size + 1)) {
702d95e11bfSDag-Erling Smørgrav 			debug2("%10u: bit size %u mismatch", count_in, in_size);
703d95e11bfSDag-Erling Smørgrav 			continue;
704d95e11bfSDag-Erling Smørgrav 		}
705d95e11bfSDag-Erling Smørgrav 		if (in_size < QSIZE_MINIMUM) {
706d95e11bfSDag-Erling Smørgrav 			debug2("%10u: bit size %u too short", count_in, in_size);
707d95e11bfSDag-Erling Smørgrav 			continue;
708d95e11bfSDag-Erling Smørgrav 		}
709d95e11bfSDag-Erling Smørgrav 
710d4af9e69SDag-Erling Smørgrav 		if (in_tests & MODULI_TESTS_MILLER_RABIN)
711d95e11bfSDag-Erling Smørgrav 			in_tries += trials;
712d95e11bfSDag-Erling Smørgrav 		else
713d95e11bfSDag-Erling Smørgrav 			in_tries = trials;
714efcad6b7SDag-Erling Smørgrav 
715d95e11bfSDag-Erling Smørgrav 		/*
716d95e11bfSDag-Erling Smørgrav 		 * guess unknown generator
717d95e11bfSDag-Erling Smørgrav 		 */
718d95e11bfSDag-Erling Smørgrav 		if (generator_known == 0) {
719d95e11bfSDag-Erling Smørgrav 			if (BN_mod_word(p, 24) == 11)
720d95e11bfSDag-Erling Smørgrav 				generator_known = 2;
721d95e11bfSDag-Erling Smørgrav 			else {
722d95e11bfSDag-Erling Smørgrav 				u_int32_t r = BN_mod_word(p, 10);
723d95e11bfSDag-Erling Smørgrav 
724efcad6b7SDag-Erling Smørgrav 				if (r == 3 || r == 7)
725d95e11bfSDag-Erling Smørgrav 					generator_known = 5;
726d95e11bfSDag-Erling Smørgrav 			}
727d95e11bfSDag-Erling Smørgrav 		}
728d95e11bfSDag-Erling Smørgrav 		/*
729d95e11bfSDag-Erling Smørgrav 		 * skip tests when desired generator doesn't match
730d95e11bfSDag-Erling Smørgrav 		 */
731d95e11bfSDag-Erling Smørgrav 		if (generator_wanted > 0 &&
732d95e11bfSDag-Erling Smørgrav 		    generator_wanted != generator_known) {
733d95e11bfSDag-Erling Smørgrav 			debug2("%10u: generator %d != %d",
734d95e11bfSDag-Erling Smørgrav 			    count_in, generator_known, generator_wanted);
735d95e11bfSDag-Erling Smørgrav 			continue;
736d95e11bfSDag-Erling Smørgrav 		}
737d95e11bfSDag-Erling Smørgrav 
738efcad6b7SDag-Erling Smørgrav 		/*
739efcad6b7SDag-Erling Smørgrav 		 * Primes with no known generator are useless for DH, so
740efcad6b7SDag-Erling Smørgrav 		 * skip those.
741efcad6b7SDag-Erling Smørgrav 		 */
742efcad6b7SDag-Erling Smørgrav 		if (generator_known == 0) {
743efcad6b7SDag-Erling Smørgrav 			debug2("%10u: no known generator", count_in);
744efcad6b7SDag-Erling Smørgrav 			continue;
745efcad6b7SDag-Erling Smørgrav 		}
746efcad6b7SDag-Erling Smørgrav 
747d95e11bfSDag-Erling Smørgrav 		count_possible++;
748d95e11bfSDag-Erling Smørgrav 
749d95e11bfSDag-Erling Smørgrav 		/*
750d95e11bfSDag-Erling Smørgrav 		 * The (1/4)^N performance bound on Miller-Rabin is
751d95e11bfSDag-Erling Smørgrav 		 * extremely pessimistic, so don't spend a lot of time
752d95e11bfSDag-Erling Smørgrav 		 * really verifying that q is prime until after we know
753d95e11bfSDag-Erling Smørgrav 		 * that p is also prime. A single pass will weed out the
754d95e11bfSDag-Erling Smørgrav 		 * vast majority of composite q's.
755d95e11bfSDag-Erling Smørgrav 		 */
75619261079SEd Maste 		is_prime = BN_is_prime_ex(q, 1, NULL, NULL);
75719261079SEd Maste 		if (is_prime < 0)
75819261079SEd Maste 			fatal("BN_is_prime_ex failed");
75919261079SEd Maste 		if (is_prime == 0) {
760efcad6b7SDag-Erling Smørgrav 			debug("%10u: q failed first possible prime test",
761d95e11bfSDag-Erling Smørgrav 			    count_in);
762d95e11bfSDag-Erling Smørgrav 			continue;
763d95e11bfSDag-Erling Smørgrav 		}
764d95e11bfSDag-Erling Smørgrav 
765d95e11bfSDag-Erling Smørgrav 		/*
766d95e11bfSDag-Erling Smørgrav 		 * q is possibly prime, so go ahead and really make sure
767d95e11bfSDag-Erling Smørgrav 		 * that p is prime. If it is, then we can go back and do
768d95e11bfSDag-Erling Smørgrav 		 * the same for q. If p is composite, chances are that
769d95e11bfSDag-Erling Smørgrav 		 * will show up on the first Rabin-Miller iteration so it
770d95e11bfSDag-Erling Smørgrav 		 * doesn't hurt to specify a high iteration count.
771d95e11bfSDag-Erling Smørgrav 		 */
77219261079SEd Maste 		is_prime = BN_is_prime_ex(p, trials, NULL, NULL);
77319261079SEd Maste 		if (is_prime < 0)
77419261079SEd Maste 			fatal("BN_is_prime_ex failed");
77519261079SEd Maste 		if (is_prime == 0) {
776efcad6b7SDag-Erling Smørgrav 			debug("%10u: p is not prime", count_in);
777d95e11bfSDag-Erling Smørgrav 			continue;
778d95e11bfSDag-Erling Smørgrav 		}
779d95e11bfSDag-Erling Smørgrav 		debug("%10u: p is almost certainly prime", count_in);
780d95e11bfSDag-Erling Smørgrav 
781d95e11bfSDag-Erling Smørgrav 		/* recheck q more rigorously */
78219261079SEd Maste 		is_prime = BN_is_prime_ex(q, trials - 1, NULL, NULL);
78319261079SEd Maste 		if (is_prime < 0)
78419261079SEd Maste 			fatal("BN_is_prime_ex failed");
78519261079SEd Maste 		if (is_prime == 0) {
786d95e11bfSDag-Erling Smørgrav 			debug("%10u: q is not prime", count_in);
787d95e11bfSDag-Erling Smørgrav 			continue;
788d95e11bfSDag-Erling Smørgrav 		}
789d95e11bfSDag-Erling Smørgrav 		debug("%10u: q is almost certainly prime", count_in);
790d95e11bfSDag-Erling Smørgrav 
791d4af9e69SDag-Erling Smørgrav 		if (qfileout(out, MODULI_TYPE_SAFE,
792d4af9e69SDag-Erling Smørgrav 		    in_tests | MODULI_TESTS_MILLER_RABIN,
793d95e11bfSDag-Erling Smørgrav 		    in_tries, in_size, generator_known, p)) {
794d95e11bfSDag-Erling Smørgrav 			res = -1;
795d95e11bfSDag-Erling Smørgrav 			break;
796d95e11bfSDag-Erling Smørgrav 		}
797d95e11bfSDag-Erling Smørgrav 
798d95e11bfSDag-Erling Smørgrav 		count_out++;
799d95e11bfSDag-Erling Smørgrav 	}
800d95e11bfSDag-Erling Smørgrav 
801d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
802e4a9863fSDag-Erling Smørgrav 	free(lp);
803d95e11bfSDag-Erling Smørgrav 	BN_free(p);
804d95e11bfSDag-Erling Smørgrav 	BN_free(q);
805d95e11bfSDag-Erling Smørgrav 
806462c32cbSDag-Erling Smørgrav 	if (checkpoint_file != NULL)
807462c32cbSDag-Erling Smørgrav 		unlink(checkpoint_file);
808462c32cbSDag-Erling Smørgrav 
809d95e11bfSDag-Erling Smørgrav 	logit("%.24s Found %u safe primes of %u candidates in %ld seconds",
810d95e11bfSDag-Erling Smørgrav 	    ctime(&time_stop), count_out, count_possible,
811d95e11bfSDag-Erling Smørgrav 	    (long) (time_stop - time_start));
812d95e11bfSDag-Erling Smørgrav 
813d95e11bfSDag-Erling Smørgrav 	return (res);
814d95e11bfSDag-Erling Smørgrav }
815bc5531deSDag-Erling Smørgrav 
816bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
817