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