xref: /netbsd-src/usr.bin/moduli/qsafe/qsafe.c (revision 96cef9518936fd4efe47205412ab2e50a5f85a0d)
1*96cef951Schristos /* $NetBSD: qsafe.c,v 1.4 2018/02/06 19:32:49 christos Exp $ */
2e31e6187Selad 
3e31e6187Selad /*-
4e31e6187Selad  * Copyright 1994 Phil Karn <karn@qualcomm.com>
5e31e6187Selad  * Copyright 1996-1998, 2003 William Allen206 Simpson <wsimpson@greendragon.com>
6e31e6187Selad  * Copyright 2000 Niels Provos <provos@citi.umich.edu>
7e31e6187Selad  * All rights reserved.
8e31e6187Selad  *
9e31e6187Selad  * Redistribution and use in source and binary forms, with or without
10e31e6187Selad  * modification, are permitted provided that the following conditions
11e31e6187Selad  * are met:
12e31e6187Selad  * 1. Redistributions of source code must retain the above copyright
13e31e6187Selad  *    notice, this list of conditions and the following disclaimer.
14e31e6187Selad  * 2. Redistributions in binary form must reproduce the above copyright
15e31e6187Selad  *    notice, this list of conditions and the following disclaimer in the
16e31e6187Selad  *    documentation and/or other materials provided with the distribution.
17e31e6187Selad  *
18e31e6187Selad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19e31e6187Selad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20e31e6187Selad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21e31e6187Selad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22e31e6187Selad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23e31e6187Selad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24e31e6187Selad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25e31e6187Selad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26e31e6187Selad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27e31e6187Selad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e31e6187Selad  */
29e31e6187Selad 
30e31e6187Selad /*
31e31e6187Selad  * Test probable "safe" primes,
32e31e6187Selad  *
33e31e6187Selad  *  suitable for use as Diffie-Hellman moduli;
34e31e6187Selad  *  that is, where q = (p-1)/2 is also prime.
35e31e6187Selad  *
36e31e6187Selad  * This is the second of two steps.
37e31e6187Selad  * This step is processor intensive.
38e31e6187Selad  *
39e31e6187Selad  * 1996 May     William Allen Simpson
40e31e6187Selad  *              extracted from earlier code by Phil Karn, April 1994.
41e31e6187Selad  *              read large prime candidates list (q),
42e31e6187Selad  *              and check prime probability of (p).
43e31e6187Selad  * 1998 May     William Allen Simpson
44e31e6187Selad  *              parameterized.
45e31e6187Selad  *              optionally limit to a single generator.
46e31e6187Selad  * 2000 Dec     Niels Provos
47e31e6187Selad  *              convert from GMP to openssl BN.
48e31e6187Selad  * 2003 Jun     William Allen Simpson
49e31e6187Selad  *              change outfile definition slightly to match openssh mistake.
50e31e6187Selad  *              move common file i/o to own file for better documentation.
51e31e6187Selad  *              redo debugprint again.
52e31e6187Selad  */
53e31e6187Selad 
54e31e6187Selad #include <stdlib.h>
55e31e6187Selad #include <stdio.h>
56e31e6187Selad #include <string.h>
57e31e6187Selad #include <time.h>
58e31e6187Selad #include <openssl/bn.h>
59e31e6187Selad #include "qfile.h"
60e31e6187Selad 
61e31e6187Selad /* define DEBUGPRINT     1 */
62e31e6187Selad #define TRIAL_MINIMUM           (4)
63e31e6187Selad 
646d5539deSjoerg __dead static void     usage(void);
65e31e6187Selad 
66e31e6187Selad /*
67e31e6187Selad  * perform a Miller-Rabin primality test
68e31e6187Selad  * on the list of candidates
69e31e6187Selad  * (checking both q and p)
70e31e6187Selad  * from standard input.
71e31e6187Selad  * The result is a list of so-call "safe" primes
72e31e6187Selad  * to standard output,
73e31e6187Selad  */
74e31e6187Selad int
main(int argc,char * argv[])75e31e6187Selad main(int argc, char *argv[])
76e31e6187Selad {
77e31e6187Selad 	BIGNUM         *q, *p, *a;
78e31e6187Selad 	BN_CTX         *ctx;
79e31e6187Selad 	char           *cp;
80e31e6187Selad 	char           *lp;
81e31e6187Selad 	uint32_t        count_in = 0;
82e31e6187Selad 	uint32_t        count_out = 0;
83e31e6187Selad 	uint32_t        count_possible = 0;
84e31e6187Selad 	uint32_t        generator_known;
85e31e6187Selad 	uint32_t        generator_wanted = 0;
86e31e6187Selad 	uint32_t        in_tests;
87e31e6187Selad 	uint32_t        in_tries;
88e31e6187Selad 	uint32_t        in_type;
89e31e6187Selad 	uint32_t        in_size;
90e31e6187Selad 	int             trials;
91e31e6187Selad 	time_t          time_start;
92e31e6187Selad 	time_t          time_stop;
93e31e6187Selad 
94e31e6187Selad 	setprogname(argv[0]);
95e31e6187Selad 
96e31e6187Selad 	if (argc < 2) {
97e31e6187Selad 		usage();
98e31e6187Selad 	}
99e31e6187Selad 
100e31e6187Selad 	if ((trials = strtoul(argv[1], NULL, 10)) < TRIAL_MINIMUM) {
101e31e6187Selad 		trials = TRIAL_MINIMUM;
102e31e6187Selad 	}
103e31e6187Selad 
104e31e6187Selad 	if (argc > 2) {
105e31e6187Selad 		generator_wanted = strtoul(argv[2], NULL, 16);
106e31e6187Selad 	}
107e31e6187Selad 
108e31e6187Selad 	time(&time_start);
109e31e6187Selad 
110e31e6187Selad 	p = BN_new();
111e31e6187Selad 	q = BN_new();
112e31e6187Selad 	ctx = BN_CTX_new();
113e31e6187Selad 
114e31e6187Selad 	(void)fprintf(stderr,
115e31e6187Selad 		"%.24s Final %d Miller-Rabin trials (%x generator)\n",
116e31e6187Selad 		ctime(&time_start), trials, generator_wanted);
117e31e6187Selad 
118e31e6187Selad 	lp = (char *) malloc((unsigned long) QLINESIZE + 1);
119e31e6187Selad 
120e31e6187Selad 	while (fgets(lp, QLINESIZE, stdin) != NULL) {
121e31e6187Selad 		size_t          ll = strlen(lp);
122e31e6187Selad 
123e31e6187Selad 		count_in++;
124e31e6187Selad 
125e31e6187Selad 		if (ll < 14 || *lp == '!' || *lp == '#') {
126e31e6187Selad #ifdef  DEBUGPRINT
127e31e6187Selad 			(void)fprintf(stderr, "%10lu: comment or short"
128e31e6187Selad 				      " line\n", count_in);
129e31e6187Selad #endif
130e31e6187Selad 			continue;
131e31e6187Selad 		}
132e31e6187Selad 
133e31e6187Selad 		/* time */
134e31e6187Selad 		cp = &lp[14];	/* (skip) */
135e31e6187Selad 
136e31e6187Selad 		/* type */
137e31e6187Selad 		in_type = strtoul(cp, &cp, 10);
138e31e6187Selad 
139e31e6187Selad 		/* tests */
140e31e6187Selad 		in_tests = strtoul(cp, &cp, 10);
141e31e6187Selad 		if (in_tests & QTEST_COMPOSITE) {
142e31e6187Selad #ifdef  DEBUGPRINT
143e31e6187Selad 			(void)fprintf(stderr, "%10lu: known composite\n",
144e31e6187Selad 				count_in);
145e31e6187Selad #endif
146e31e6187Selad 			continue;
147e31e6187Selad 		}
148e31e6187Selad 
149e31e6187Selad 		/* tries */
150e31e6187Selad 		in_tries = (uint32_t) strtoul(cp, &cp, 10);
151e31e6187Selad 
152e31e6187Selad 		/* size (most significant bit) */
153e31e6187Selad 		in_size = (uint32_t) strtoul(cp, &cp, 10);
154e31e6187Selad 
155e31e6187Selad 		/* generator (hex) */
156e31e6187Selad 		generator_known = (uint32_t) strtoul(cp, &cp, 16);
157e31e6187Selad 
158e31e6187Selad 		/* Skip white space */
159e31e6187Selad 		cp += strspn(cp, " ");
160e31e6187Selad 
161e31e6187Selad 		/* modulus (hex) */
162e31e6187Selad 		switch (in_type) {
163e31e6187Selad 		case QTYPE_SOPHIE_GERMAINE:
164e31e6187Selad #ifdef  DEBUGPRINT
165e31e6187Selad 			(void)fprintf(stderr, "%10lu: (%lu) "
166e31e6187Selad 				      "Sophie-Germaine\n", count_in,
167e31e6187Selad 				      in_type);
168e31e6187Selad #endif
169e31e6187Selad 
170e31e6187Selad 			a = q;
171e31e6187Selad 			BN_hex2bn(&a, cp);
172e31e6187Selad 			/* p = 2*q + 1 */
173e31e6187Selad 			BN_lshift(p, q, 1);
174e31e6187Selad 			BN_add_word(p, 1UL);
175e31e6187Selad 			in_size += 1;
176e31e6187Selad 			generator_known = 0;
177e31e6187Selad 
178e31e6187Selad 			break;
179e31e6187Selad 
180e31e6187Selad 		default:
181e31e6187Selad #ifdef  DEBUGPRINT
182e31e6187Selad 			(void)fprintf(stderr, "%10lu: (%lu)\n",
183e31e6187Selad 				      count_in,	in_type);
184e31e6187Selad #endif
185e31e6187Selad 			a = p;
186e31e6187Selad 			BN_hex2bn(&a, cp);
187e31e6187Selad 			/* q = (p-1) / 2 */
188e31e6187Selad 			BN_rshift(q, p, 1);
189e31e6187Selad 
190e31e6187Selad 			break;
191e31e6187Selad 		}
192e31e6187Selad 
193e31e6187Selad 		/*
194e31e6187Selad 		 * due to earlier inconsistencies in interpretation, check the
195e31e6187Selad 		 * proposed bit size.
196e31e6187Selad 		 */
197a0935173Slukem 		if ((uint32_t)BN_num_bits(p) != (in_size + 1)) {
198e31e6187Selad #ifdef  DEBUGPRINT
199e31e6187Selad 			(void)fprintf(stderr, "%10lu: bit size %ul "
200e31e6187Selad 				      "mismatch\n", count_in, in_size);
201e31e6187Selad #endif
202e31e6187Selad 			continue;
203e31e6187Selad 		}
204e31e6187Selad 
205e31e6187Selad 		if (in_size < QSIZE_MINIMUM) {
206e31e6187Selad #ifdef  DEBUGPRINT
207e31e6187Selad 			(void)fprintf(stderr, "%10lu: bit size %ul "
208e31e6187Selad 				      "too short\n", count_in, in_size);
209e31e6187Selad #endif
210e31e6187Selad 			continue;
211e31e6187Selad 		}
212e31e6187Selad 
213e31e6187Selad 		if (in_tests & QTEST_MILLER_RABIN)
214e31e6187Selad 			in_tries += trials;
215e31e6187Selad 		else
216e31e6187Selad 			in_tries = trials;
217e31e6187Selad 
218e31e6187Selad 		/*
219e31e6187Selad 		 * guess unknown generator
220e31e6187Selad 		 */
221e31e6187Selad 		if (generator_known == 0) {
222e31e6187Selad 			if (BN_mod_word(p, 24UL) == 11)
223e31e6187Selad 				generator_known = 2;
224e31e6187Selad 			else if (BN_mod_word(p, 12UL) == 5)
225e31e6187Selad 				generator_known = 3;
226e31e6187Selad 			else {
227e31e6187Selad 				BN_ULONG        r = BN_mod_word(p, 10UL);
228e31e6187Selad 
229e31e6187Selad 				if (r == 3 || r == 7) {
230e31e6187Selad 					generator_known = 5;
231e31e6187Selad 				}
232e31e6187Selad 			}
233e31e6187Selad 		}
234e31e6187Selad 
235e31e6187Selad 		/*
236e31e6187Selad 		 * skip tests when desired generator doesn't match
237e31e6187Selad 		 */
238e31e6187Selad 		if (generator_wanted > 0 &&
239e31e6187Selad 		    generator_wanted != generator_known) {
240e31e6187Selad #ifdef  DEBUGPRINT
241e31e6187Selad 			(void)fprintf(stderr,
242e31e6187Selad 				      "%10lu: generator %ld != %ld\n",
243e31e6187Selad 				count_in, generator_known, generator_wanted);
244e31e6187Selad #endif
245e31e6187Selad 			continue;
246e31e6187Selad 		}
247e31e6187Selad 
248e31e6187Selad 		count_possible++;
249e31e6187Selad 
250e31e6187Selad 		/*
251e31e6187Selad 		 * The (1/4)^N performance bound on Miller-Rabin is extremely
252e31e6187Selad 		 * pessimistic, so don't spend a lot of time really verifying
253e31e6187Selad 		 * that q is prime until after we know that p is also prime. A
254e31e6187Selad 		 * single pass will weed out the vast majority of composite
255e31e6187Selad 		 * q's.
256e31e6187Selad 		 */
257*96cef951Schristos 		if (BN_is_prime_ex(q, 1, ctx, NULL) <= 0) {
258e31e6187Selad #ifdef  DEBUGPRINT
259e31e6187Selad 			(void)fprintf(stderr, "%10lu: q failed first "
260e31e6187Selad 				      "possible prime test\n", count_in);
261e31e6187Selad #endif
262e31e6187Selad 			continue;
263e31e6187Selad 		}
264e31e6187Selad 
265e31e6187Selad 		/*
266e31e6187Selad 		 * q is possibly prime, so go ahead and really make sure that
267e31e6187Selad 		 * p is prime. If it is, then we can go back and do the same
268e31e6187Selad 		 * for q. If p is composite, chances are that will show up on
269e31e6187Selad 		 * the first Rabin-Miller iteration so it doesn't hurt to
270e31e6187Selad 		 * specify a high iteration count.
271e31e6187Selad 		 */
272*96cef951Schristos 		if (!BN_is_prime_ex(p, trials, ctx, NULL)) {
273e31e6187Selad #ifdef  DEBUGPRINT
274e31e6187Selad 			(void)fprintf(stderr, "%10lu: p is not prime\n",
275e31e6187Selad 				      count_in);
276e31e6187Selad #endif
277e31e6187Selad 			continue;
278e31e6187Selad 		}
279e31e6187Selad 
280e31e6187Selad #ifdef  DEBUGPRINT
281e31e6187Selad 		(void)fprintf(stderr, "%10lu: p is almost certainly "
282e31e6187Selad 			      "prime\n", count_in);
283e31e6187Selad #endif
284e31e6187Selad 
285e31e6187Selad 		/* recheck q more rigorously */
286*96cef951Schristos 		if (!BN_is_prime_ex(q, trials - 1, ctx, NULL)) {
287e31e6187Selad #ifdef  DEBUGPRINT
288e31e6187Selad 			(void)fprintf(stderr, "%10lu: q is not prime\n",
289e31e6187Selad 				      count_in);
290e31e6187Selad #endif
291e31e6187Selad 			continue;
292e31e6187Selad 		}
293e31e6187Selad #ifdef  DEBUGPRINT
294e31e6187Selad 		fprintf(stderr, "%10lu: q is almost certainly prime\n",
295e31e6187Selad 			count_in);
296e31e6187Selad #endif
297e31e6187Selad 		if (0 > qfileout(stdout,
298e31e6187Selad 				 QTYPE_SAFE,
299e31e6187Selad 				 (in_tests | QTEST_MILLER_RABIN),
300e31e6187Selad 				 in_tries,
301e31e6187Selad 				 in_size,
302e31e6187Selad 				 generator_known,
303e31e6187Selad 				 p)) {
304e31e6187Selad 			break;
305e31e6187Selad 		}
306e31e6187Selad 		count_out++;
307e31e6187Selad #ifdef  DEBUGPRINT
308e31e6187Selad 		fflush(stderr);
309e31e6187Selad 		fflush(stdout);
310e31e6187Selad #endif
311e31e6187Selad 	}
312e31e6187Selad 
313e31e6187Selad 	time(&time_stop);
314e31e6187Selad 	free(lp);
315e31e6187Selad 	BN_free(p);
316e31e6187Selad 	BN_free(q);
317e31e6187Selad 	BN_CTX_free(ctx);
318e31e6187Selad 	fflush(stdout);		/* fclose(stdout); */
319e31e6187Selad 	/* fclose(stdin); */
320e31e6187Selad 	(void)fprintf(stderr,
321e31e6187Selad 	   "%.24s Found %u safe primes of %u candidates in %lu seconds\n",
322e31e6187Selad 		ctime(&time_stop), count_out, count_possible,
323e31e6187Selad 		(long) (time_stop - time_start));
324e31e6187Selad 
325e31e6187Selad 	return (0);
326e31e6187Selad }
327e31e6187Selad 
328e31e6187Selad static void
usage(void)329e31e6187Selad usage(void)
330e31e6187Selad {
331e31e6187Selad 	(void)fprintf(stderr, "Usage: %s <trials> [generator]\n",
332e31e6187Selad 		      getprogname());
333e31e6187Selad 	exit(1);
334e31e6187Selad }
335