xref: /freebsd/crypto/openssh/moduli.c (revision 47dd1d1b619cc035b82b49a91a25544309ff95ae)
1*47dd1d1bSDag-Erling Smørgrav /* $OpenBSD: moduli.c,v 1.32 2017/12/08 03:45:52 deraadt 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
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);
162d95e11bfSDag-Erling Smørgrav 
163d95e11bfSDag-Erling Smørgrav 	res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
164d95e11bfSDag-Erling Smørgrav 	    gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday,
165d95e11bfSDag-Erling Smørgrav 	    gtm->tm_hour, gtm->tm_min, gtm->tm_sec,
166d95e11bfSDag-Erling Smørgrav 	    otype, otests, otries, osize, ogenerator);
167d95e11bfSDag-Erling Smørgrav 
168d95e11bfSDag-Erling Smørgrav 	if (res < 0)
169d95e11bfSDag-Erling Smørgrav 		return (-1);
170d95e11bfSDag-Erling Smørgrav 
171d95e11bfSDag-Erling Smørgrav 	if (BN_print_fp(ofile, omodulus) < 1)
172d95e11bfSDag-Erling Smørgrav 		return (-1);
173d95e11bfSDag-Erling Smørgrav 
174d95e11bfSDag-Erling Smørgrav 	res = fprintf(ofile, "\n");
175d95e11bfSDag-Erling Smørgrav 	fflush(ofile);
176d95e11bfSDag-Erling Smørgrav 
177d95e11bfSDag-Erling Smørgrav 	return (res > 0 ? 0 : -1);
178d95e11bfSDag-Erling Smørgrav }
179d95e11bfSDag-Erling Smørgrav 
180d95e11bfSDag-Erling Smørgrav 
181d95e11bfSDag-Erling Smørgrav /*
182d95e11bfSDag-Erling Smørgrav  ** Sieve p's and q's with small factors
183d95e11bfSDag-Erling Smørgrav  */
184d95e11bfSDag-Erling Smørgrav static void
185d95e11bfSDag-Erling Smørgrav sieve_large(u_int32_t s)
186d95e11bfSDag-Erling Smørgrav {
187d95e11bfSDag-Erling Smørgrav 	u_int32_t r, u;
188d95e11bfSDag-Erling Smørgrav 
189efcad6b7SDag-Erling Smørgrav 	debug3("sieve_large %u", s);
190d95e11bfSDag-Erling Smørgrav 	largetries++;
191d95e11bfSDag-Erling Smørgrav 	/* r = largebase mod s */
192d95e11bfSDag-Erling Smørgrav 	r = BN_mod_word(largebase, s);
193d95e11bfSDag-Erling Smørgrav 	if (r == 0)
194d95e11bfSDag-Erling Smørgrav 		u = 0; /* s divides into largebase exactly */
195d95e11bfSDag-Erling Smørgrav 	else
196d95e11bfSDag-Erling Smørgrav 		u = s - r; /* largebase+u is first entry divisible by s */
197d95e11bfSDag-Erling Smørgrav 
198d95e11bfSDag-Erling Smørgrav 	if (u < largebits * 2) {
199d95e11bfSDag-Erling Smørgrav 		/*
200d95e11bfSDag-Erling Smørgrav 		 * The sieve omits p's and q's divisible by 2, so ensure that
201d95e11bfSDag-Erling Smørgrav 		 * largebase+u is odd. Then, step through the sieve in
202d95e11bfSDag-Erling Smørgrav 		 * increments of 2*s
203d95e11bfSDag-Erling Smørgrav 		 */
204d95e11bfSDag-Erling Smørgrav 		if (u & 0x1)
205d95e11bfSDag-Erling Smørgrav 			u += s; /* Make largebase+u odd, and u even */
206d95e11bfSDag-Erling Smørgrav 
207d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of 2*s */
208d95e11bfSDag-Erling Smørgrav 		for (u /= 2; u < largebits; u += s)
209d95e11bfSDag-Erling Smørgrav 			BIT_SET(LargeSieve, u);
210d95e11bfSDag-Erling Smørgrav 	}
211d95e11bfSDag-Erling Smørgrav 
212d95e11bfSDag-Erling Smørgrav 	/* r = p mod s */
213d95e11bfSDag-Erling Smørgrav 	r = (2 * r + 1) % s;
214d95e11bfSDag-Erling Smørgrav 	if (r == 0)
215d95e11bfSDag-Erling Smørgrav 		u = 0; /* s divides p exactly */
216d95e11bfSDag-Erling Smørgrav 	else
217d95e11bfSDag-Erling Smørgrav 		u = s - r; /* p+u is first entry divisible by s */
218d95e11bfSDag-Erling Smørgrav 
219d95e11bfSDag-Erling Smørgrav 	if (u < largebits * 4) {
220d95e11bfSDag-Erling Smørgrav 		/*
221d95e11bfSDag-Erling Smørgrav 		 * The sieve omits p's divisible by 4, so ensure that
222d95e11bfSDag-Erling Smørgrav 		 * largebase+u is not. Then, step through the sieve in
223d95e11bfSDag-Erling Smørgrav 		 * increments of 4*s
224d95e11bfSDag-Erling Smørgrav 		 */
225d95e11bfSDag-Erling Smørgrav 		while (u & 0x3) {
226d95e11bfSDag-Erling Smørgrav 			if (SMALL_MAXIMUM - u < s)
227d95e11bfSDag-Erling Smørgrav 				return;
228d95e11bfSDag-Erling Smørgrav 			u += s;
229d95e11bfSDag-Erling Smørgrav 		}
230d95e11bfSDag-Erling Smørgrav 
231d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of 4*s */
232d95e11bfSDag-Erling Smørgrav 		for (u /= 4; u < largebits; u += s)
233d95e11bfSDag-Erling Smørgrav 			BIT_SET(LargeSieve, u);
234d95e11bfSDag-Erling Smørgrav 	}
235d95e11bfSDag-Erling Smørgrav }
236d95e11bfSDag-Erling Smørgrav 
237d95e11bfSDag-Erling Smørgrav /*
238d74d50a8SDag-Erling Smørgrav  * list candidates for Sophie-Germain primes (where q = (p-1)/2)
239d95e11bfSDag-Erling Smørgrav  * to standard output.
240d95e11bfSDag-Erling Smørgrav  * The list is checked against small known primes (less than 2**30).
241d95e11bfSDag-Erling Smørgrav  */
242d95e11bfSDag-Erling Smørgrav int
243043840dfSDag-Erling Smørgrav gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
244d95e11bfSDag-Erling Smørgrav {
245d95e11bfSDag-Erling Smørgrav 	BIGNUM *q;
246d95e11bfSDag-Erling Smørgrav 	u_int32_t j, r, s, t;
247d95e11bfSDag-Erling Smørgrav 	u_int32_t smallwords = TINY_NUMBER >> 6;
248d95e11bfSDag-Erling Smørgrav 	u_int32_t tinywords = TINY_NUMBER >> 6;
249d95e11bfSDag-Erling Smørgrav 	time_t time_start, time_stop;
250043840dfSDag-Erling Smørgrav 	u_int32_t i;
251043840dfSDag-Erling Smørgrav 	int ret = 0;
252d95e11bfSDag-Erling Smørgrav 
253d95e11bfSDag-Erling Smørgrav 	largememory = memory;
254d95e11bfSDag-Erling Smørgrav 
255d74d50a8SDag-Erling Smørgrav 	if (memory != 0 &&
256d74d50a8SDag-Erling Smørgrav 	    (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
257d74d50a8SDag-Erling Smørgrav 		error("Invalid memory amount (min %ld, max %ld)",
258d74d50a8SDag-Erling Smørgrav 		    LARGE_MINIMUM, LARGE_MAXIMUM);
259d74d50a8SDag-Erling Smørgrav 		return (-1);
260d74d50a8SDag-Erling Smørgrav 	}
261d74d50a8SDag-Erling Smørgrav 
262d95e11bfSDag-Erling Smørgrav 	/*
263d95e11bfSDag-Erling Smørgrav 	 * Set power to the length in bits of the prime to be generated.
264d95e11bfSDag-Erling Smørgrav 	 * This is changed to 1 less than the desired safe prime moduli p.
265d95e11bfSDag-Erling Smørgrav 	 */
266d95e11bfSDag-Erling Smørgrav 	if (power > TEST_MAXIMUM) {
267d95e11bfSDag-Erling Smørgrav 		error("Too many bits: %u > %lu", power, TEST_MAXIMUM);
268d95e11bfSDag-Erling Smørgrav 		return (-1);
269d95e11bfSDag-Erling Smørgrav 	} else if (power < TEST_MINIMUM) {
270d95e11bfSDag-Erling Smørgrav 		error("Too few bits: %u < %u", power, TEST_MINIMUM);
271d95e11bfSDag-Erling Smørgrav 		return (-1);
272d95e11bfSDag-Erling Smørgrav 	}
273d95e11bfSDag-Erling Smørgrav 	power--; /* decrement before squaring */
274d95e11bfSDag-Erling Smørgrav 
275d95e11bfSDag-Erling Smørgrav 	/*
276d95e11bfSDag-Erling Smørgrav 	 * The density of ordinary primes is on the order of 1/bits, so the
277d95e11bfSDag-Erling Smørgrav 	 * density of safe primes should be about (1/bits)**2. Set test range
278d95e11bfSDag-Erling Smørgrav 	 * to something well above bits**2 to be reasonably sure (but not
279d95e11bfSDag-Erling Smørgrav 	 * guaranteed) of catching at least one safe prime.
280d95e11bfSDag-Erling Smørgrav 	 */
281d95e11bfSDag-Erling Smørgrav 	largewords = ((power * power) >> (SHIFT_WORD - TEST_POWER));
282d95e11bfSDag-Erling Smørgrav 
283d95e11bfSDag-Erling Smørgrav 	/*
284d95e11bfSDag-Erling Smørgrav 	 * Need idea of how much memory is available. We don't have to use all
285d95e11bfSDag-Erling Smørgrav 	 * of it.
286d95e11bfSDag-Erling Smørgrav 	 */
287d95e11bfSDag-Erling Smørgrav 	if (largememory > LARGE_MAXIMUM) {
288d95e11bfSDag-Erling Smørgrav 		logit("Limited memory: %u MB; limit %lu MB",
289d95e11bfSDag-Erling Smørgrav 		    largememory, LARGE_MAXIMUM);
290d95e11bfSDag-Erling Smørgrav 		largememory = LARGE_MAXIMUM;
291d95e11bfSDag-Erling Smørgrav 	}
292d95e11bfSDag-Erling Smørgrav 
293d95e11bfSDag-Erling Smørgrav 	if (largewords <= (largememory << SHIFT_MEGAWORD)) {
294d95e11bfSDag-Erling Smørgrav 		logit("Increased memory: %u MB; need %u bytes",
295d95e11bfSDag-Erling Smørgrav 		    largememory, (largewords << SHIFT_BYTE));
296d95e11bfSDag-Erling Smørgrav 		largewords = (largememory << SHIFT_MEGAWORD);
297d95e11bfSDag-Erling Smørgrav 	} else if (largememory > 0) {
298d95e11bfSDag-Erling Smørgrav 		logit("Decreased memory: %u MB; want %u bytes",
299d95e11bfSDag-Erling Smørgrav 		    largememory, (largewords << SHIFT_BYTE));
300d95e11bfSDag-Erling Smørgrav 		largewords = (largememory << SHIFT_MEGAWORD);
301d95e11bfSDag-Erling Smørgrav 	}
302d95e11bfSDag-Erling Smørgrav 
303761efaa7SDag-Erling Smørgrav 	TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
304d95e11bfSDag-Erling Smørgrav 	tinybits = tinywords << SHIFT_WORD;
305d95e11bfSDag-Erling Smørgrav 
306761efaa7SDag-Erling Smørgrav 	SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
307d95e11bfSDag-Erling Smørgrav 	smallbits = smallwords << SHIFT_WORD;
308d95e11bfSDag-Erling Smørgrav 
309d95e11bfSDag-Erling Smørgrav 	/*
310d95e11bfSDag-Erling Smørgrav 	 * dynamically determine available memory
311d95e11bfSDag-Erling Smørgrav 	 */
312d95e11bfSDag-Erling Smørgrav 	while ((LargeSieve = calloc(largewords, sizeof(u_int32_t))) == NULL)
313d95e11bfSDag-Erling Smørgrav 		largewords -= (1L << (SHIFT_MEGAWORD - 2)); /* 1/4 MB chunks */
314d95e11bfSDag-Erling Smørgrav 
315d95e11bfSDag-Erling Smørgrav 	largebits = largewords << SHIFT_WORD;
316d95e11bfSDag-Erling Smørgrav 	largenumbers = largebits * 2;	/* even numbers excluded */
317d95e11bfSDag-Erling Smørgrav 
318d95e11bfSDag-Erling Smørgrav 	/* validation check: count the number of primes tried */
319d95e11bfSDag-Erling Smørgrav 	largetries = 0;
32092eb0aa1SDag-Erling Smørgrav 	if ((q = BN_new()) == NULL)
32192eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
322d95e11bfSDag-Erling Smørgrav 
323d95e11bfSDag-Erling Smørgrav 	/*
324d95e11bfSDag-Erling Smørgrav 	 * Generate random starting point for subprime search, or use
325d95e11bfSDag-Erling Smørgrav 	 * specified parameter.
326d95e11bfSDag-Erling Smørgrav 	 */
32792eb0aa1SDag-Erling Smørgrav 	if ((largebase = BN_new()) == NULL)
32892eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
32992eb0aa1SDag-Erling Smørgrav 	if (start == NULL) {
33092eb0aa1SDag-Erling Smørgrav 		if (BN_rand(largebase, power, 1, 1) == 0)
33192eb0aa1SDag-Erling Smørgrav 			fatal("BN_rand failed");
33292eb0aa1SDag-Erling Smørgrav 	} else {
33392eb0aa1SDag-Erling Smørgrav 		if (BN_copy(largebase, start) == NULL)
33492eb0aa1SDag-Erling Smørgrav 			fatal("BN_copy: failed");
33592eb0aa1SDag-Erling Smørgrav 	}
336d95e11bfSDag-Erling Smørgrav 
337d95e11bfSDag-Erling Smørgrav 	/* ensure odd */
33892eb0aa1SDag-Erling Smørgrav 	if (BN_set_bit(largebase, 0) == 0)
33992eb0aa1SDag-Erling Smørgrav 		fatal("BN_set_bit: failed");
340d95e11bfSDag-Erling Smørgrav 
341d95e11bfSDag-Erling Smørgrav 	time(&time_start);
342d95e11bfSDag-Erling Smørgrav 
343d95e11bfSDag-Erling Smørgrav 	logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start),
344d95e11bfSDag-Erling Smørgrav 	    largenumbers, power);
345d95e11bfSDag-Erling Smørgrav 	debug2("start point: 0x%s", BN_bn2hex(largebase));
346d95e11bfSDag-Erling Smørgrav 
347d95e11bfSDag-Erling Smørgrav 	/*
348d95e11bfSDag-Erling Smørgrav 	 * TinySieve
349d95e11bfSDag-Erling Smørgrav 	 */
350d95e11bfSDag-Erling Smørgrav 	for (i = 0; i < tinybits; i++) {
351d95e11bfSDag-Erling Smørgrav 		if (BIT_TEST(TinySieve, i))
352d95e11bfSDag-Erling Smørgrav 			continue; /* 2*i+3 is composite */
353d95e11bfSDag-Erling Smørgrav 
354d95e11bfSDag-Erling Smørgrav 		/* The next tiny prime */
355d95e11bfSDag-Erling Smørgrav 		t = 2 * i + 3;
356d95e11bfSDag-Erling Smørgrav 
357d95e11bfSDag-Erling Smørgrav 		/* Mark all multiples of t */
358d95e11bfSDag-Erling Smørgrav 		for (j = i + t; j < tinybits; j += t)
359d95e11bfSDag-Erling Smørgrav 			BIT_SET(TinySieve, j);
360d95e11bfSDag-Erling Smørgrav 
361d95e11bfSDag-Erling Smørgrav 		sieve_large(t);
362d95e11bfSDag-Erling Smørgrav 	}
363d95e11bfSDag-Erling Smørgrav 
364d95e11bfSDag-Erling Smørgrav 	/*
365d95e11bfSDag-Erling Smørgrav 	 * Start the small block search at the next possible prime. To avoid
366d95e11bfSDag-Erling Smørgrav 	 * fencepost errors, the last pass is skipped.
367d95e11bfSDag-Erling Smørgrav 	 */
368d95e11bfSDag-Erling Smørgrav 	for (smallbase = TINY_NUMBER + 3;
369d95e11bfSDag-Erling Smørgrav 	    smallbase < (SMALL_MAXIMUM - TINY_NUMBER);
370d95e11bfSDag-Erling Smørgrav 	    smallbase += TINY_NUMBER) {
371d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < tinybits; i++) {
372d95e11bfSDag-Erling Smørgrav 			if (BIT_TEST(TinySieve, i))
373d95e11bfSDag-Erling Smørgrav 				continue; /* 2*i+3 is composite */
374d95e11bfSDag-Erling Smørgrav 
375d95e11bfSDag-Erling Smørgrav 			/* The next tiny prime */
376d95e11bfSDag-Erling Smørgrav 			t = 2 * i + 3;
377d95e11bfSDag-Erling Smørgrav 			r = smallbase % t;
378d95e11bfSDag-Erling Smørgrav 
379d95e11bfSDag-Erling Smørgrav 			if (r == 0) {
380d95e11bfSDag-Erling Smørgrav 				s = 0; /* t divides into smallbase exactly */
381d95e11bfSDag-Erling Smørgrav 			} else {
382d95e11bfSDag-Erling Smørgrav 				/* smallbase+s is first entry divisible by t */
383d95e11bfSDag-Erling Smørgrav 				s = t - r;
384d95e11bfSDag-Erling Smørgrav 			}
385d95e11bfSDag-Erling Smørgrav 
386d95e11bfSDag-Erling Smørgrav 			/*
387d95e11bfSDag-Erling Smørgrav 			 * The sieve omits even numbers, so ensure that
388d95e11bfSDag-Erling Smørgrav 			 * smallbase+s is odd. Then, step through the sieve
389d95e11bfSDag-Erling Smørgrav 			 * in increments of 2*t
390d95e11bfSDag-Erling Smørgrav 			 */
391d95e11bfSDag-Erling Smørgrav 			if (s & 1)
392d95e11bfSDag-Erling Smørgrav 				s += t; /* Make smallbase+s odd, and s even */
393d95e11bfSDag-Erling Smørgrav 
394d95e11bfSDag-Erling Smørgrav 			/* Mark all multiples of 2*t */
395d95e11bfSDag-Erling Smørgrav 			for (s /= 2; s < smallbits; s += t)
396d95e11bfSDag-Erling Smørgrav 				BIT_SET(SmallSieve, s);
397d95e11bfSDag-Erling Smørgrav 		}
398d95e11bfSDag-Erling Smørgrav 
399d95e11bfSDag-Erling Smørgrav 		/*
400d95e11bfSDag-Erling Smørgrav 		 * SmallSieve
401d95e11bfSDag-Erling Smørgrav 		 */
402d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < smallbits; i++) {
403d95e11bfSDag-Erling Smørgrav 			if (BIT_TEST(SmallSieve, i))
404d95e11bfSDag-Erling Smørgrav 				continue; /* 2*i+smallbase is composite */
405d95e11bfSDag-Erling Smørgrav 
406d95e11bfSDag-Erling Smørgrav 			/* The next small prime */
407d95e11bfSDag-Erling Smørgrav 			sieve_large((2 * i) + smallbase);
408d95e11bfSDag-Erling Smørgrav 		}
409d95e11bfSDag-Erling Smørgrav 
410d95e11bfSDag-Erling Smørgrav 		memset(SmallSieve, 0, smallwords << SHIFT_BYTE);
411d95e11bfSDag-Erling Smørgrav 	}
412d95e11bfSDag-Erling Smørgrav 
413d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
414d95e11bfSDag-Erling Smørgrav 
415*47dd1d1bSDag-Erling Smørgrav 	logit("%.24s Sieved with %u small primes in %lld seconds",
416*47dd1d1bSDag-Erling Smørgrav 	    ctime(&time_stop), largetries, (long long)(time_stop - time_start));
417d95e11bfSDag-Erling Smørgrav 
418d95e11bfSDag-Erling Smørgrav 	for (j = r = 0; j < largebits; j++) {
419d95e11bfSDag-Erling Smørgrav 		if (BIT_TEST(LargeSieve, j))
420d95e11bfSDag-Erling Smørgrav 			continue; /* Definitely composite, skip */
421d95e11bfSDag-Erling Smørgrav 
422d95e11bfSDag-Erling Smørgrav 		debug2("test q = largebase+%u", 2 * j);
42392eb0aa1SDag-Erling Smørgrav 		if (BN_set_word(q, 2 * j) == 0)
42492eb0aa1SDag-Erling Smørgrav 			fatal("BN_set_word failed");
42592eb0aa1SDag-Erling Smørgrav 		if (BN_add(q, q, largebase) == 0)
42692eb0aa1SDag-Erling Smørgrav 			fatal("BN_add failed");
427d4af9e69SDag-Erling Smørgrav 		if (qfileout(out, MODULI_TYPE_SOPHIE_GERMAIN,
428d4af9e69SDag-Erling Smørgrav 		    MODULI_TESTS_SIEVE, largetries,
429d4af9e69SDag-Erling Smørgrav 		    (power - 1) /* MSB */, (0), q) == -1) {
430d95e11bfSDag-Erling Smørgrav 			ret = -1;
431d95e11bfSDag-Erling Smørgrav 			break;
432d95e11bfSDag-Erling Smørgrav 		}
433d95e11bfSDag-Erling Smørgrav 
434d95e11bfSDag-Erling Smørgrav 		r++; /* count q */
435d95e11bfSDag-Erling Smørgrav 	}
436d95e11bfSDag-Erling Smørgrav 
437d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
438d95e11bfSDag-Erling Smørgrav 
439e4a9863fSDag-Erling Smørgrav 	free(LargeSieve);
440e4a9863fSDag-Erling Smørgrav 	free(SmallSieve);
441e4a9863fSDag-Erling Smørgrav 	free(TinySieve);
442d95e11bfSDag-Erling Smørgrav 
443d95e11bfSDag-Erling Smørgrav 	logit("%.24s Found %u candidates", ctime(&time_stop), r);
444d95e11bfSDag-Erling Smørgrav 
445d95e11bfSDag-Erling Smørgrav 	return (ret);
446d95e11bfSDag-Erling Smørgrav }
447d95e11bfSDag-Erling Smørgrav 
448462c32cbSDag-Erling Smørgrav static void
449462c32cbSDag-Erling Smørgrav write_checkpoint(char *cpfile, u_int32_t lineno)
450462c32cbSDag-Erling Smørgrav {
451462c32cbSDag-Erling Smørgrav 	FILE *fp;
452bc5531deSDag-Erling Smørgrav 	char tmp[PATH_MAX];
453462c32cbSDag-Erling Smørgrav 	int r;
454462c32cbSDag-Erling Smørgrav 
455462c32cbSDag-Erling Smørgrav 	r = snprintf(tmp, sizeof(tmp), "%s.XXXXXXXXXX", cpfile);
456bc5531deSDag-Erling Smørgrav 	if (r == -1 || r >= PATH_MAX) {
457462c32cbSDag-Erling Smørgrav 		logit("write_checkpoint: temp pathname too long");
458462c32cbSDag-Erling Smørgrav 		return;
459462c32cbSDag-Erling Smørgrav 	}
460462c32cbSDag-Erling Smørgrav 	if ((r = mkstemp(tmp)) == -1) {
461462c32cbSDag-Erling Smørgrav 		logit("mkstemp(%s): %s", tmp, strerror(errno));
462462c32cbSDag-Erling Smørgrav 		return;
463462c32cbSDag-Erling Smørgrav 	}
464462c32cbSDag-Erling Smørgrav 	if ((fp = fdopen(r, "w")) == NULL) {
465462c32cbSDag-Erling Smørgrav 		logit("write_checkpoint: fdopen: %s", strerror(errno));
466bc5531deSDag-Erling Smørgrav 		unlink(tmp);
467462c32cbSDag-Erling Smørgrav 		close(r);
468462c32cbSDag-Erling Smørgrav 		return;
469462c32cbSDag-Erling Smørgrav 	}
470462c32cbSDag-Erling Smørgrav 	if (fprintf(fp, "%lu\n", (unsigned long)lineno) > 0 && fclose(fp) == 0
471462c32cbSDag-Erling Smørgrav 	    && rename(tmp, cpfile) == 0)
472462c32cbSDag-Erling Smørgrav 		debug3("wrote checkpoint line %lu to '%s'",
473462c32cbSDag-Erling Smørgrav 		    (unsigned long)lineno, cpfile);
474462c32cbSDag-Erling Smørgrav 	else
475462c32cbSDag-Erling Smørgrav 		logit("failed to write to checkpoint file '%s': %s", cpfile,
476462c32cbSDag-Erling Smørgrav 		    strerror(errno));
477462c32cbSDag-Erling Smørgrav }
478462c32cbSDag-Erling Smørgrav 
479462c32cbSDag-Erling Smørgrav static unsigned long
480462c32cbSDag-Erling Smørgrav read_checkpoint(char *cpfile)
481462c32cbSDag-Erling Smørgrav {
482462c32cbSDag-Erling Smørgrav 	FILE *fp;
483462c32cbSDag-Erling Smørgrav 	unsigned long lineno = 0;
484462c32cbSDag-Erling Smørgrav 
485462c32cbSDag-Erling Smørgrav 	if ((fp = fopen(cpfile, "r")) == NULL)
486462c32cbSDag-Erling Smørgrav 		return 0;
487462c32cbSDag-Erling Smørgrav 	if (fscanf(fp, "%lu\n", &lineno) < 1)
488462c32cbSDag-Erling Smørgrav 		logit("Failed to load checkpoint from '%s'", cpfile);
489462c32cbSDag-Erling Smørgrav 	else
490462c32cbSDag-Erling Smørgrav 		logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno);
491462c32cbSDag-Erling Smørgrav 	fclose(fp);
492462c32cbSDag-Erling Smørgrav 	return lineno;
493462c32cbSDag-Erling Smørgrav }
494462c32cbSDag-Erling Smørgrav 
495f7167e0eSDag-Erling Smørgrav static unsigned long
496f7167e0eSDag-Erling Smørgrav count_lines(FILE *f)
497f7167e0eSDag-Erling Smørgrav {
498f7167e0eSDag-Erling Smørgrav 	unsigned long count = 0;
499f7167e0eSDag-Erling Smørgrav 	char lp[QLINESIZE + 1];
500f7167e0eSDag-Erling Smørgrav 
501f7167e0eSDag-Erling Smørgrav 	if (fseek(f, 0, SEEK_SET) != 0) {
502f7167e0eSDag-Erling Smørgrav 		debug("input file is not seekable");
503f7167e0eSDag-Erling Smørgrav 		return ULONG_MAX;
504f7167e0eSDag-Erling Smørgrav 	}
505f7167e0eSDag-Erling Smørgrav 	while (fgets(lp, QLINESIZE + 1, f) != NULL)
506f7167e0eSDag-Erling Smørgrav 		count++;
507f7167e0eSDag-Erling Smørgrav 	rewind(f);
508f7167e0eSDag-Erling Smørgrav 	debug("input file has %lu lines", count);
509f7167e0eSDag-Erling Smørgrav 	return count;
510f7167e0eSDag-Erling Smørgrav }
511f7167e0eSDag-Erling Smørgrav 
512f7167e0eSDag-Erling Smørgrav static char *
513f7167e0eSDag-Erling Smørgrav fmt_time(time_t seconds)
514f7167e0eSDag-Erling Smørgrav {
515f7167e0eSDag-Erling Smørgrav 	int day, hr, min;
516f7167e0eSDag-Erling Smørgrav 	static char buf[128];
517f7167e0eSDag-Erling Smørgrav 
518f7167e0eSDag-Erling Smørgrav 	min = (seconds / 60) % 60;
519f7167e0eSDag-Erling Smørgrav 	hr = (seconds / 60 / 60) % 24;
520f7167e0eSDag-Erling Smørgrav 	day = seconds / 60 / 60 / 24;
521f7167e0eSDag-Erling Smørgrav 	if (day > 0)
522f7167e0eSDag-Erling Smørgrav 		snprintf(buf, sizeof buf, "%dd %d:%02d", day, hr, min);
523f7167e0eSDag-Erling Smørgrav 	else
524f7167e0eSDag-Erling Smørgrav 		snprintf(buf, sizeof buf, "%d:%02d", hr, min);
525f7167e0eSDag-Erling Smørgrav 	return buf;
526f7167e0eSDag-Erling Smørgrav }
527f7167e0eSDag-Erling Smørgrav 
528f7167e0eSDag-Erling Smørgrav static void
529f7167e0eSDag-Erling Smørgrav print_progress(unsigned long start_lineno, unsigned long current_lineno,
530f7167e0eSDag-Erling Smørgrav     unsigned long end_lineno)
531f7167e0eSDag-Erling Smørgrav {
532f7167e0eSDag-Erling Smørgrav 	static time_t time_start, time_prev;
533f7167e0eSDag-Erling Smørgrav 	time_t time_now, elapsed;
534f7167e0eSDag-Erling Smørgrav 	unsigned long num_to_process, processed, remaining, percent, eta;
535f7167e0eSDag-Erling Smørgrav 	double time_per_line;
536f7167e0eSDag-Erling Smørgrav 	char *eta_str;
537f7167e0eSDag-Erling Smørgrav 
538f7167e0eSDag-Erling Smørgrav 	time_now = monotime();
539f7167e0eSDag-Erling Smørgrav 	if (time_start == 0) {
540f7167e0eSDag-Erling Smørgrav 		time_start = time_prev = time_now;
541f7167e0eSDag-Erling Smørgrav 		return;
542f7167e0eSDag-Erling Smørgrav 	}
543f7167e0eSDag-Erling Smørgrav 	/* print progress after 1m then once per 5m */
544f7167e0eSDag-Erling Smørgrav 	if (time_now - time_prev < 5 * 60)
545f7167e0eSDag-Erling Smørgrav 		return;
546f7167e0eSDag-Erling Smørgrav 	time_prev = time_now;
547f7167e0eSDag-Erling Smørgrav 	elapsed = time_now - time_start;
548f7167e0eSDag-Erling Smørgrav 	processed = current_lineno - start_lineno;
549f7167e0eSDag-Erling Smørgrav 	remaining = end_lineno - current_lineno;
550f7167e0eSDag-Erling Smørgrav 	num_to_process = end_lineno - start_lineno;
551f7167e0eSDag-Erling Smørgrav 	time_per_line = (double)elapsed / processed;
552f7167e0eSDag-Erling Smørgrav 	/* if we don't know how many we're processing just report count+time */
553f7167e0eSDag-Erling Smørgrav 	time(&time_now);
554f7167e0eSDag-Erling Smørgrav 	if (end_lineno == ULONG_MAX) {
555f7167e0eSDag-Erling Smørgrav 		logit("%.24s processed %lu in %s", ctime(&time_now),
556f7167e0eSDag-Erling Smørgrav 		    processed, fmt_time(elapsed));
557f7167e0eSDag-Erling Smørgrav 		return;
558f7167e0eSDag-Erling Smørgrav 	}
559f7167e0eSDag-Erling Smørgrav 	percent = 100 * processed / num_to_process;
560f7167e0eSDag-Erling Smørgrav 	eta = time_per_line * remaining;
561f7167e0eSDag-Erling Smørgrav 	eta_str = xstrdup(fmt_time(eta));
562f7167e0eSDag-Erling Smørgrav 	logit("%.24s processed %lu of %lu (%lu%%) in %s, ETA %s",
563f7167e0eSDag-Erling Smørgrav 	    ctime(&time_now), processed, num_to_process, percent,
564f7167e0eSDag-Erling Smørgrav 	    fmt_time(elapsed), eta_str);
565f7167e0eSDag-Erling Smørgrav 	free(eta_str);
566f7167e0eSDag-Erling Smørgrav }
567f7167e0eSDag-Erling Smørgrav 
568d95e11bfSDag-Erling Smørgrav /*
569d95e11bfSDag-Erling Smørgrav  * perform a Miller-Rabin primality test
570d95e11bfSDag-Erling Smørgrav  * on the list of candidates
571d95e11bfSDag-Erling Smørgrav  * (checking both q and p)
572d95e11bfSDag-Erling Smørgrav  * The result is a list of so-call "safe" primes
573d95e11bfSDag-Erling Smørgrav  */
574d95e11bfSDag-Erling Smørgrav int
575462c32cbSDag-Erling Smørgrav prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
576462c32cbSDag-Erling Smørgrav     char *checkpoint_file, unsigned long start_lineno, unsigned long num_lines)
577d95e11bfSDag-Erling Smørgrav {
578d95e11bfSDag-Erling Smørgrav 	BIGNUM *q, *p, *a;
579d95e11bfSDag-Erling Smørgrav 	BN_CTX *ctx;
580d95e11bfSDag-Erling Smørgrav 	char *cp, *lp;
581d95e11bfSDag-Erling Smørgrav 	u_int32_t count_in = 0, count_out = 0, count_possible = 0;
582d95e11bfSDag-Erling Smørgrav 	u_int32_t generator_known, in_tests, in_tries, in_type, in_size;
583462c32cbSDag-Erling Smørgrav 	unsigned long last_processed = 0, end_lineno;
584d95e11bfSDag-Erling Smørgrav 	time_t time_start, time_stop;
585d95e11bfSDag-Erling Smørgrav 	int res;
586d95e11bfSDag-Erling Smørgrav 
587d74d50a8SDag-Erling Smørgrav 	if (trials < TRIAL_MINIMUM) {
588d74d50a8SDag-Erling Smørgrav 		error("Minimum primality trials is %d", TRIAL_MINIMUM);
589d74d50a8SDag-Erling Smørgrav 		return (-1);
590d74d50a8SDag-Erling Smørgrav 	}
591d74d50a8SDag-Erling Smørgrav 
592f7167e0eSDag-Erling Smørgrav 	if (num_lines == 0)
593f7167e0eSDag-Erling Smørgrav 		end_lineno = count_lines(in);
594f7167e0eSDag-Erling Smørgrav 	else
595f7167e0eSDag-Erling Smørgrav 		end_lineno = start_lineno + num_lines;
596f7167e0eSDag-Erling Smørgrav 
597d95e11bfSDag-Erling Smørgrav 	time(&time_start);
598d95e11bfSDag-Erling Smørgrav 
59992eb0aa1SDag-Erling Smørgrav 	if ((p = BN_new()) == NULL)
60092eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
60192eb0aa1SDag-Erling Smørgrav 	if ((q = BN_new()) == NULL)
60292eb0aa1SDag-Erling Smørgrav 		fatal("BN_new failed");
60392eb0aa1SDag-Erling Smørgrav 	if ((ctx = BN_CTX_new()) == NULL)
60492eb0aa1SDag-Erling Smørgrav 		fatal("BN_CTX_new failed");
605d95e11bfSDag-Erling Smørgrav 
606d95e11bfSDag-Erling Smørgrav 	debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
607d95e11bfSDag-Erling Smørgrav 	    ctime(&time_start), trials, generator_wanted);
608d95e11bfSDag-Erling Smørgrav 
609462c32cbSDag-Erling Smørgrav 	if (checkpoint_file != NULL)
610462c32cbSDag-Erling Smørgrav 		last_processed = read_checkpoint(checkpoint_file);
611ca86bcf2SDag-Erling Smørgrav 	last_processed = start_lineno = MAXIMUM(last_processed, start_lineno);
612f7167e0eSDag-Erling Smørgrav 	if (end_lineno == ULONG_MAX)
613f7167e0eSDag-Erling Smørgrav 		debug("process from line %lu from pipe", last_processed);
614462c32cbSDag-Erling Smørgrav 	else
615f7167e0eSDag-Erling Smørgrav 		debug("process from line %lu to line %lu", last_processed,
616f7167e0eSDag-Erling Smørgrav 		    end_lineno);
617462c32cbSDag-Erling Smørgrav 
618d95e11bfSDag-Erling Smørgrav 	res = 0;
619d95e11bfSDag-Erling Smørgrav 	lp = xmalloc(QLINESIZE + 1);
620462c32cbSDag-Erling Smørgrav 	while (fgets(lp, QLINESIZE + 1, in) != NULL && count_in < end_lineno) {
621d95e11bfSDag-Erling Smørgrav 		count_in++;
622462c32cbSDag-Erling Smørgrav 		if (count_in <= last_processed) {
623f7167e0eSDag-Erling Smørgrav 			debug3("skipping line %u, before checkpoint or "
624f7167e0eSDag-Erling Smørgrav 			    "specified start line", count_in);
625462c32cbSDag-Erling Smørgrav 			continue;
626462c32cbSDag-Erling Smørgrav 		}
627f7167e0eSDag-Erling Smørgrav 		if (checkpoint_file != NULL)
628462c32cbSDag-Erling Smørgrav 			write_checkpoint(checkpoint_file, count_in);
629f7167e0eSDag-Erling Smørgrav 		print_progress(start_lineno, count_in, end_lineno);
630d4af9e69SDag-Erling Smørgrav 		if (strlen(lp) < 14 || *lp == '!' || *lp == '#') {
631d95e11bfSDag-Erling Smørgrav 			debug2("%10u: comment or short line", count_in);
632d95e11bfSDag-Erling Smørgrav 			continue;
633d95e11bfSDag-Erling Smørgrav 		}
634d95e11bfSDag-Erling Smørgrav 
635d95e11bfSDag-Erling Smørgrav 		/* XXX - fragile parser */
636d95e11bfSDag-Erling Smørgrav 		/* time */
637d95e11bfSDag-Erling Smørgrav 		cp = &lp[14];	/* (skip) */
638d95e11bfSDag-Erling Smørgrav 
639d95e11bfSDag-Erling Smørgrav 		/* type */
640d95e11bfSDag-Erling Smørgrav 		in_type = strtoul(cp, &cp, 10);
641d95e11bfSDag-Erling Smørgrav 
642d95e11bfSDag-Erling Smørgrav 		/* tests */
643d95e11bfSDag-Erling Smørgrav 		in_tests = strtoul(cp, &cp, 10);
644d95e11bfSDag-Erling Smørgrav 
645d4af9e69SDag-Erling Smørgrav 		if (in_tests & MODULI_TESTS_COMPOSITE) {
646d95e11bfSDag-Erling Smørgrav 			debug2("%10u: known composite", count_in);
647d95e11bfSDag-Erling Smørgrav 			continue;
648d95e11bfSDag-Erling Smørgrav 		}
649efcad6b7SDag-Erling Smørgrav 
650d95e11bfSDag-Erling Smørgrav 		/* tries */
651d95e11bfSDag-Erling Smørgrav 		in_tries = strtoul(cp, &cp, 10);
652d95e11bfSDag-Erling Smørgrav 
653d95e11bfSDag-Erling Smørgrav 		/* size (most significant bit) */
654d95e11bfSDag-Erling Smørgrav 		in_size = strtoul(cp, &cp, 10);
655d95e11bfSDag-Erling Smørgrav 
656d95e11bfSDag-Erling Smørgrav 		/* generator (hex) */
657d95e11bfSDag-Erling Smørgrav 		generator_known = strtoul(cp, &cp, 16);
658d95e11bfSDag-Erling Smørgrav 
659d95e11bfSDag-Erling Smørgrav 		/* Skip white space */
660d95e11bfSDag-Erling Smørgrav 		cp += strspn(cp, " ");
661d95e11bfSDag-Erling Smørgrav 
662d95e11bfSDag-Erling Smørgrav 		/* modulus (hex) */
663d95e11bfSDag-Erling Smørgrav 		switch (in_type) {
664d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SOPHIE_GERMAIN:
665d74d50a8SDag-Erling Smørgrav 			debug2("%10u: (%u) Sophie-Germain", count_in, in_type);
666d95e11bfSDag-Erling Smørgrav 			a = q;
66792eb0aa1SDag-Erling Smørgrav 			if (BN_hex2bn(&a, cp) == 0)
66892eb0aa1SDag-Erling Smørgrav 				fatal("BN_hex2bn failed");
669d95e11bfSDag-Erling Smørgrav 			/* p = 2*q + 1 */
67092eb0aa1SDag-Erling Smørgrav 			if (BN_lshift(p, q, 1) == 0)
67192eb0aa1SDag-Erling Smørgrav 				fatal("BN_lshift failed");
67292eb0aa1SDag-Erling Smørgrav 			if (BN_add_word(p, 1) == 0)
67392eb0aa1SDag-Erling Smørgrav 				fatal("BN_add_word failed");
674d95e11bfSDag-Erling Smørgrav 			in_size += 1;
675d95e11bfSDag-Erling Smørgrav 			generator_known = 0;
676d95e11bfSDag-Erling Smørgrav 			break;
677d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_UNSTRUCTURED:
678d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SAFE:
679d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_SCHNORR:
680d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_STRONG:
681d4af9e69SDag-Erling Smørgrav 		case MODULI_TYPE_UNKNOWN:
682d95e11bfSDag-Erling Smørgrav 			debug2("%10u: (%u)", count_in, in_type);
683d95e11bfSDag-Erling Smørgrav 			a = p;
68492eb0aa1SDag-Erling Smørgrav 			if (BN_hex2bn(&a, cp) == 0)
68592eb0aa1SDag-Erling Smørgrav 				fatal("BN_hex2bn failed");
686d95e11bfSDag-Erling Smørgrav 			/* q = (p-1) / 2 */
68792eb0aa1SDag-Erling Smørgrav 			if (BN_rshift(q, p, 1) == 0)
68892eb0aa1SDag-Erling Smørgrav 				fatal("BN_rshift failed");
689d95e11bfSDag-Erling Smørgrav 			break;
690efcad6b7SDag-Erling Smørgrav 		default:
691efcad6b7SDag-Erling Smørgrav 			debug2("Unknown prime type");
692efcad6b7SDag-Erling Smørgrav 			break;
693d95e11bfSDag-Erling Smørgrav 		}
694d95e11bfSDag-Erling Smørgrav 
695d95e11bfSDag-Erling Smørgrav 		/*
696d95e11bfSDag-Erling Smørgrav 		 * due to earlier inconsistencies in interpretation, check
697d95e11bfSDag-Erling Smørgrav 		 * the proposed bit size.
698d95e11bfSDag-Erling Smørgrav 		 */
699043840dfSDag-Erling Smørgrav 		if ((u_int32_t)BN_num_bits(p) != (in_size + 1)) {
700d95e11bfSDag-Erling Smørgrav 			debug2("%10u: bit size %u mismatch", count_in, in_size);
701d95e11bfSDag-Erling Smørgrav 			continue;
702d95e11bfSDag-Erling Smørgrav 		}
703d95e11bfSDag-Erling Smørgrav 		if (in_size < QSIZE_MINIMUM) {
704d95e11bfSDag-Erling Smørgrav 			debug2("%10u: bit size %u too short", count_in, in_size);
705d95e11bfSDag-Erling Smørgrav 			continue;
706d95e11bfSDag-Erling Smørgrav 		}
707d95e11bfSDag-Erling Smørgrav 
708d4af9e69SDag-Erling Smørgrav 		if (in_tests & MODULI_TESTS_MILLER_RABIN)
709d95e11bfSDag-Erling Smørgrav 			in_tries += trials;
710d95e11bfSDag-Erling Smørgrav 		else
711d95e11bfSDag-Erling Smørgrav 			in_tries = trials;
712efcad6b7SDag-Erling Smørgrav 
713d95e11bfSDag-Erling Smørgrav 		/*
714d95e11bfSDag-Erling Smørgrav 		 * guess unknown generator
715d95e11bfSDag-Erling Smørgrav 		 */
716d95e11bfSDag-Erling Smørgrav 		if (generator_known == 0) {
717d95e11bfSDag-Erling Smørgrav 			if (BN_mod_word(p, 24) == 11)
718d95e11bfSDag-Erling Smørgrav 				generator_known = 2;
719d95e11bfSDag-Erling Smørgrav 			else if (BN_mod_word(p, 12) == 5)
720d95e11bfSDag-Erling Smørgrav 				generator_known = 3;
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 		 */
7564a421b63SDag-Erling Smørgrav 		if (BN_is_prime_ex(q, 1, ctx, NULL) <= 0) {
757efcad6b7SDag-Erling Smørgrav 			debug("%10u: q failed first possible prime test",
758d95e11bfSDag-Erling Smørgrav 			    count_in);
759d95e11bfSDag-Erling Smørgrav 			continue;
760d95e11bfSDag-Erling Smørgrav 		}
761d95e11bfSDag-Erling Smørgrav 
762d95e11bfSDag-Erling Smørgrav 		/*
763d95e11bfSDag-Erling Smørgrav 		 * q is possibly prime, so go ahead and really make sure
764d95e11bfSDag-Erling Smørgrav 		 * that p is prime. If it is, then we can go back and do
765d95e11bfSDag-Erling Smørgrav 		 * the same for q. If p is composite, chances are that
766d95e11bfSDag-Erling Smørgrav 		 * will show up on the first Rabin-Miller iteration so it
767d95e11bfSDag-Erling Smørgrav 		 * doesn't hurt to specify a high iteration count.
768d95e11bfSDag-Erling Smørgrav 		 */
7694a421b63SDag-Erling Smørgrav 		if (!BN_is_prime_ex(p, trials, ctx, NULL)) {
770efcad6b7SDag-Erling Smørgrav 			debug("%10u: p is not prime", count_in);
771d95e11bfSDag-Erling Smørgrav 			continue;
772d95e11bfSDag-Erling Smørgrav 		}
773d95e11bfSDag-Erling Smørgrav 		debug("%10u: p is almost certainly prime", count_in);
774d95e11bfSDag-Erling Smørgrav 
775d95e11bfSDag-Erling Smørgrav 		/* recheck q more rigorously */
7764a421b63SDag-Erling Smørgrav 		if (!BN_is_prime_ex(q, trials - 1, ctx, NULL)) {
777d95e11bfSDag-Erling Smørgrav 			debug("%10u: q is not prime", count_in);
778d95e11bfSDag-Erling Smørgrav 			continue;
779d95e11bfSDag-Erling Smørgrav 		}
780d95e11bfSDag-Erling Smørgrav 		debug("%10u: q is almost certainly prime", count_in);
781d95e11bfSDag-Erling Smørgrav 
782d4af9e69SDag-Erling Smørgrav 		if (qfileout(out, MODULI_TYPE_SAFE,
783d4af9e69SDag-Erling Smørgrav 		    in_tests | MODULI_TESTS_MILLER_RABIN,
784d95e11bfSDag-Erling Smørgrav 		    in_tries, in_size, generator_known, p)) {
785d95e11bfSDag-Erling Smørgrav 			res = -1;
786d95e11bfSDag-Erling Smørgrav 			break;
787d95e11bfSDag-Erling Smørgrav 		}
788d95e11bfSDag-Erling Smørgrav 
789d95e11bfSDag-Erling Smørgrav 		count_out++;
790d95e11bfSDag-Erling Smørgrav 	}
791d95e11bfSDag-Erling Smørgrav 
792d95e11bfSDag-Erling Smørgrav 	time(&time_stop);
793e4a9863fSDag-Erling Smørgrav 	free(lp);
794d95e11bfSDag-Erling Smørgrav 	BN_free(p);
795d95e11bfSDag-Erling Smørgrav 	BN_free(q);
796d95e11bfSDag-Erling Smørgrav 	BN_CTX_free(ctx);
797d95e11bfSDag-Erling Smørgrav 
798462c32cbSDag-Erling Smørgrav 	if (checkpoint_file != NULL)
799462c32cbSDag-Erling Smørgrav 		unlink(checkpoint_file);
800462c32cbSDag-Erling Smørgrav 
801d95e11bfSDag-Erling Smørgrav 	logit("%.24s Found %u safe primes of %u candidates in %ld seconds",
802d95e11bfSDag-Erling Smørgrav 	    ctime(&time_stop), count_out, count_possible,
803d95e11bfSDag-Erling Smørgrav 	    (long) (time_stop - time_start));
804d95e11bfSDag-Erling Smørgrav 
805d95e11bfSDag-Erling Smørgrav 	return (res);
806d95e11bfSDag-Erling Smørgrav }
807bc5531deSDag-Erling Smørgrav 
808bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
809