summaryrefslogtreecommitdiffstats
path: root/moduli/qsafe/qsafe.c
blob: 99c1e8c1629215aa9a9b69448e0a15be2d29c146 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* $NetBSD: qsafe.c,v 1.1 2006/01/19 23:23:58 elad Exp $ */

/*-
 * Copyright 1994 Phil Karn <karn@qualcomm.com>
 * Copyright 1996-1998, 2003 William Allen206 Simpson <wsimpson@greendragon.com>
 * Copyright 2000 Niels Provos <provos@citi.umich.edu>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Test probable "safe" primes,
 *
 *  suitable for use as Diffie-Hellman moduli;
 *  that is, where q = (p-1)/2 is also prime.
 *
 * This is the second of two steps.
 * This step is processor intensive.
 *
 * 1996 May     William Allen Simpson
 *              extracted from earlier code by Phil Karn, April 1994.
 *              read large prime candidates list (q),
 *              and check prime probability of (p).
 * 1998 May     William Allen Simpson
 *              parameterized.
 *              optionally limit to a single generator.
 * 2000 Dec     Niels Provos
 *              convert from GMP to openssl BN.
 * 2003 Jun     William Allen Simpson
 *              change outfile definition slightly to match openssh mistake.
 *              move common file i/o to own file for better documentation.
 *              redo debugprint again.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <openssl/bn.h>
#include "qfile.h"

/* define DEBUGPRINT     1 */
#define TRIAL_MINIMUM           (4)

static void     usage(void);

/*
 * perform a Miller-Rabin primality test
 * on the list of candidates
 * (checking both q and p)
 * from standard input.
 * The result is a list of so-call "safe" primes
 * to standard output,
 */
int
main(int argc, char *argv[])
{
	BIGNUM         *q, *p, *a;
	BN_CTX         *ctx;
	char           *cp;
	char           *lp;
	uint32_t        count_in = 0;
	uint32_t        count_out = 0;
	uint32_t        count_possible = 0;
	uint32_t        generator_known;
	uint32_t        generator_wanted = 0;
	uint32_t        in_tests;
	uint32_t        in_tries;
	uint32_t        in_type;
	uint32_t        in_size;
	int             trials;
	time_t          time_start;
	time_t          time_stop;

	setprogname(argv[0]);

	if (argc < 2) {
		usage();
	}

	if ((trials = strtoul(argv[1], NULL, 10)) < TRIAL_MINIMUM) {
		trials = TRIAL_MINIMUM;
	}

	if (argc > 2) {
		generator_wanted = strtoul(argv[2], NULL, 16);
	}

	time(&time_start);

	p = BN_new();
	q = BN_new();
	ctx = BN_CTX_new();

	(void)fprintf(stderr,
		"%.24s Final %d Miller-Rabin trials (%x generator)\n",
		ctime(&time_start), trials, generator_wanted);

	lp = (char *) malloc((unsigned long) QLINESIZE + 1);

	while (fgets(lp, QLINESIZE, stdin) != NULL) {
		size_t          ll = strlen(lp);

		count_in++;

		if (ll < 14 || *lp == '!' || *lp == '#') {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: comment or short"
				      " line\n", count_in);
#endif
			continue;
		}

		/* time */
		cp = &lp[14];	/* (skip) */

		/* type */
		in_type = strtoul(cp, &cp, 10);

		/* tests */
		in_tests = strtoul(cp, &cp, 10);
		if (in_tests & QTEST_COMPOSITE) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: known composite\n",
				count_in);
#endif
			continue;
		}

		/* tries */
		in_tries = (uint32_t) strtoul(cp, &cp, 10);

		/* size (most significant bit) */
		in_size = (uint32_t) strtoul(cp, &cp, 10);

		/* generator (hex) */
		generator_known = (uint32_t) strtoul(cp, &cp, 16);

		/* Skip white space */
		cp += strspn(cp, " ");

		/* modulus (hex) */
		switch (in_type) {
		case QTYPE_SOPHIE_GERMAINE:
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: (%lu) "
				      "Sophie-Germaine\n", count_in,
				      in_type);
#endif

			a = q;
			BN_hex2bn(&a, cp);
			/* p = 2*q + 1 */
			BN_lshift(p, q, 1);
			BN_add_word(p, 1UL);
			in_size += 1;
			generator_known = 0;

			break;

		default:
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: (%lu)\n",
				      count_in,	in_type);
#endif
			a = p;
			BN_hex2bn(&a, cp);
			/* q = (p-1) / 2 */
			BN_rshift(q, p, 1);

			break;
		}

		/*
		 * due to earlier inconsistencies in interpretation, check the
		 * proposed bit size.
		 */
		if (BN_num_bits(p) != (in_size + 1)) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: bit size %ul "
				      "mismatch\n", count_in, in_size);
#endif
			continue;
		}

		if (in_size < QSIZE_MINIMUM) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: bit size %ul "
				      "too short\n", count_in, in_size);
#endif
			continue;
		}

		if (in_tests & QTEST_MILLER_RABIN)
			in_tries += trials;
		else
			in_tries = trials;

		/*
		 * guess unknown generator
		 */
		if (generator_known == 0) {
			if (BN_mod_word(p, 24UL) == 11)
				generator_known = 2;
			else if (BN_mod_word(p, 12UL) == 5)
				generator_known = 3;
			else {
				BN_ULONG        r = BN_mod_word(p, 10UL);

				if (r == 3 || r == 7) {
					generator_known = 5;
				}
			}
		}

		/*
		 * skip tests when desired generator doesn't match
		 */
		if (generator_wanted > 0 &&
		    generator_wanted != generator_known) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr,
				      "%10lu: generator %ld != %ld\n",
				count_in, generator_known, generator_wanted);
#endif
			continue;
		}

		count_possible++;

		/*
		 * The (1/4)^N performance bound on Miller-Rabin is extremely
		 * pessimistic, so don't spend a lot of time really verifying
		 * that q is prime until after we know that p is also prime. A
		 * single pass will weed out the vast majority of composite
		 * q's.
		 */
		if (BN_is_prime(q, 1, NULL, ctx, NULL) <= 0) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: q failed first "
				      "possible prime test\n", count_in);
#endif
			continue;
		}

		/*
		 * q is possibly prime, so go ahead and really make sure that
		 * p is prime. If it is, then we can go back and do the same
		 * for q. If p is composite, chances are that will show up on
		 * the first Rabin-Miller iteration so it doesn't hurt to
		 * specify a high iteration count.
		 */
		if (!BN_is_prime(p, trials, NULL, ctx, NULL)) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: p is not prime\n",
				      count_in);
#endif
			continue;
		}

#ifdef  DEBUGPRINT
		(void)fprintf(stderr, "%10lu: p is almost certainly "
			      "prime\n", count_in);
#endif

		/* recheck q more rigorously */
		if (!BN_is_prime(q, trials - 1, NULL, ctx, NULL)) {
#ifdef  DEBUGPRINT
			(void)fprintf(stderr, "%10lu: q is not prime\n",
				      count_in);
#endif
			continue;
		}
#ifdef  DEBUGPRINT
		fprintf(stderr, "%10lu: q is almost certainly prime\n",
			count_in);
#endif
		if (0 > qfileout(stdout,
				 QTYPE_SAFE,
				 (in_tests | QTEST_MILLER_RABIN),
				 in_tries,
				 in_size,
				 generator_known,
				 p)) {
			break;
		}
		count_out++;
#ifdef  DEBUGPRINT
		fflush(stderr);
		fflush(stdout);
#endif
	}

	time(&time_stop);
	free(lp);
	BN_free(p);
	BN_free(q);
	BN_CTX_free(ctx);
	fflush(stdout);		/* fclose(stdout); */
	/* fclose(stdin); */
	(void)fprintf(stderr,
	   "%.24s Found %u safe primes of %u candidates in %lu seconds\n",
		ctime(&time_stop), count_out, count_possible,
		(long) (time_stop - time_start));

	return (0);
}

static void
usage(void)
{
	(void)fprintf(stderr, "Usage: %s <trials> [generator]\n",
		      getprogname());
	exit(1);
}