- (dtucker) [configure.ac] Use a proper AC_CHECK_DECL for BROKEN_GETADDRINFO
[openssh-git.git] / moduli.c
blobf737cb3f517d2063ac30d4774da4e0d06dcc6677
1 /* $OpenBSD: moduli.c,v 1.21 2008/06/26 09:19:40 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.
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.
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.
30 * Two-step process to generate safe primes for DHGEX
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.
36 * First step: generate candidate primes (memory intensive)
37 * Second step: test primes' safety (processor intensive)
40 #include "includes.h"
42 #include <sys/types.h>
44 #include <openssl/bn.h>
45 #include <openssl/dh.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdarg.h>
51 #include <time.h>
53 #include "xmalloc.h"
54 #include "dh.h"
55 #include "log.h"
58 * File output defines
61 /* need line long enough for largest moduli plus headers */
62 #define QLINESIZE (100+8192)
65 * Size: decimal.
66 * Specifies the number of the most significant bit (0 to M).
67 * WARNING: internally, usually 1 to N.
69 #define QSIZE_MINIMUM (511)
72 * Prime sieving defines
75 /* Constant: assuming 8 bit bytes and 32 bit words */
76 #define SHIFT_BIT (3)
77 #define SHIFT_BYTE (2)
78 #define SHIFT_WORD (SHIFT_BIT+SHIFT_BYTE)
79 #define SHIFT_MEGABYTE (20)
80 #define SHIFT_MEGAWORD (SHIFT_MEGABYTE-SHIFT_BYTE)
83 * Using virtual memory can cause thrashing. This should be the largest
84 * number that is supported without a large amount of disk activity --
85 * that would increase the run time from hours to days or weeks!
87 #define LARGE_MINIMUM (8UL) /* megabytes */
90 * Do not increase this number beyond the unsigned integer bit size.
91 * Due to a multiple of 4, it must be LESS than 128 (yielding 2**30 bits).
93 #define LARGE_MAXIMUM (127UL) /* megabytes */
96 * Constant: when used with 32-bit integers, the largest sieve prime
97 * has to be less than 2**32.
99 #define SMALL_MAXIMUM (0xffffffffUL)
101 /* Constant: can sieve all primes less than 2**32, as 65537**2 > 2**32-1. */
102 #define TINY_NUMBER (1UL<<16)
104 /* Ensure enough bit space for testing 2*q. */
105 #define TEST_MAXIMUM (1UL<<16)
106 #define TEST_MINIMUM (QSIZE_MINIMUM + 1)
107 /* real TEST_MINIMUM (1UL << (SHIFT_WORD - TEST_POWER)) */
108 #define TEST_POWER (3) /* 2**n, n < SHIFT_WORD */
110 /* bit operations on 32-bit words */
111 #define BIT_CLEAR(a,n) ((a)[(n)>>SHIFT_WORD] &= ~(1L << ((n) & 31)))
112 #define BIT_SET(a,n) ((a)[(n)>>SHIFT_WORD] |= (1L << ((n) & 31)))
113 #define BIT_TEST(a,n) ((a)[(n)>>SHIFT_WORD] & (1L << ((n) & 31)))
116 * Prime testing defines
119 /* Minimum number of primality tests to perform */
120 #define TRIAL_MINIMUM (4)
123 * Sieving data (XXX - move to struct)
126 /* sieve 2**16 */
127 static u_int32_t *TinySieve, tinybits;
129 /* sieve 2**30 in 2**16 parts */
130 static u_int32_t *SmallSieve, smallbits, smallbase;
132 /* sieve relative to the initial value */
133 static u_int32_t *LargeSieve, largewords, largetries, largenumbers;
134 static u_int32_t largebits, largememory; /* megabytes */
135 static BIGNUM *largebase;
137 int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
138 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
141 * print moduli out in consistent form,
143 static int
144 qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries,
145 u_int32_t osize, u_int32_t ogenerator, BIGNUM * omodulus)
147 struct tm *gtm;
148 time_t time_now;
149 int res;
151 time(&time_now);
152 gtm = gmtime(&time_now);
154 res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
155 gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday,
156 gtm->tm_hour, gtm->tm_min, gtm->tm_sec,
157 otype, otests, otries, osize, ogenerator);
159 if (res < 0)
160 return (-1);
162 if (BN_print_fp(ofile, omodulus) < 1)
163 return (-1);
165 res = fprintf(ofile, "\n");
166 fflush(ofile);
168 return (res > 0 ? 0 : -1);
173 ** Sieve p's and q's with small factors
175 static void
176 sieve_large(u_int32_t s)
178 u_int32_t r, u;
180 debug3("sieve_large %u", s);
181 largetries++;
182 /* r = largebase mod s */
183 r = BN_mod_word(largebase, s);
184 if (r == 0)
185 u = 0; /* s divides into largebase exactly */
186 else
187 u = s - r; /* largebase+u is first entry divisible by s */
189 if (u < largebits * 2) {
191 * The sieve omits p's and q's divisible by 2, so ensure that
192 * largebase+u is odd. Then, step through the sieve in
193 * increments of 2*s
195 if (u & 0x1)
196 u += s; /* Make largebase+u odd, and u even */
198 /* Mark all multiples of 2*s */
199 for (u /= 2; u < largebits; u += s)
200 BIT_SET(LargeSieve, u);
203 /* r = p mod s */
204 r = (2 * r + 1) % s;
205 if (r == 0)
206 u = 0; /* s divides p exactly */
207 else
208 u = s - r; /* p+u is first entry divisible by s */
210 if (u < largebits * 4) {
212 * The sieve omits p's divisible by 4, so ensure that
213 * largebase+u is not. Then, step through the sieve in
214 * increments of 4*s
216 while (u & 0x3) {
217 if (SMALL_MAXIMUM - u < s)
218 return;
219 u += s;
222 /* Mark all multiples of 4*s */
223 for (u /= 4; u < largebits; u += s)
224 BIT_SET(LargeSieve, u);
229 * list candidates for Sophie-Germain primes (where q = (p-1)/2)
230 * to standard output.
231 * The list is checked against small known primes (less than 2**30).
234 gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
236 BIGNUM *q;
237 u_int32_t j, r, s, t;
238 u_int32_t smallwords = TINY_NUMBER >> 6;
239 u_int32_t tinywords = TINY_NUMBER >> 6;
240 time_t time_start, time_stop;
241 u_int32_t i;
242 int ret = 0;
244 largememory = memory;
246 if (memory != 0 &&
247 (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
248 error("Invalid memory amount (min %ld, max %ld)",
249 LARGE_MINIMUM, LARGE_MAXIMUM);
250 return (-1);
254 * Set power to the length in bits of the prime to be generated.
255 * This is changed to 1 less than the desired safe prime moduli p.
257 if (power > TEST_MAXIMUM) {
258 error("Too many bits: %u > %lu", power, TEST_MAXIMUM);
259 return (-1);
260 } else if (power < TEST_MINIMUM) {
261 error("Too few bits: %u < %u", power, TEST_MINIMUM);
262 return (-1);
264 power--; /* decrement before squaring */
267 * The density of ordinary primes is on the order of 1/bits, so the
268 * density of safe primes should be about (1/bits)**2. Set test range
269 * to something well above bits**2 to be reasonably sure (but not
270 * guaranteed) of catching at least one safe prime.
272 largewords = ((power * power) >> (SHIFT_WORD - TEST_POWER));
275 * Need idea of how much memory is available. We don't have to use all
276 * of it.
278 if (largememory > LARGE_MAXIMUM) {
279 logit("Limited memory: %u MB; limit %lu MB",
280 largememory, LARGE_MAXIMUM);
281 largememory = LARGE_MAXIMUM;
284 if (largewords <= (largememory << SHIFT_MEGAWORD)) {
285 logit("Increased memory: %u MB; need %u bytes",
286 largememory, (largewords << SHIFT_BYTE));
287 largewords = (largememory << SHIFT_MEGAWORD);
288 } else if (largememory > 0) {
289 logit("Decreased memory: %u MB; want %u bytes",
290 largememory, (largewords << SHIFT_BYTE));
291 largewords = (largememory << SHIFT_MEGAWORD);
294 TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
295 tinybits = tinywords << SHIFT_WORD;
297 SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
298 smallbits = smallwords << SHIFT_WORD;
301 * dynamically determine available memory
303 while ((LargeSieve = calloc(largewords, sizeof(u_int32_t))) == NULL)
304 largewords -= (1L << (SHIFT_MEGAWORD - 2)); /* 1/4 MB chunks */
306 largebits = largewords << SHIFT_WORD;
307 largenumbers = largebits * 2; /* even numbers excluded */
309 /* validation check: count the number of primes tried */
310 largetries = 0;
311 if ((q = BN_new()) == NULL)
312 fatal("BN_new failed");
315 * Generate random starting point for subprime search, or use
316 * specified parameter.
318 if ((largebase = BN_new()) == NULL)
319 fatal("BN_new failed");
320 if (start == NULL) {
321 if (BN_rand(largebase, power, 1, 1) == 0)
322 fatal("BN_rand failed");
323 } else {
324 if (BN_copy(largebase, start) == NULL)
325 fatal("BN_copy: failed");
328 /* ensure odd */
329 if (BN_set_bit(largebase, 0) == 0)
330 fatal("BN_set_bit: failed");
332 time(&time_start);
334 logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start),
335 largenumbers, power);
336 debug2("start point: 0x%s", BN_bn2hex(largebase));
339 * TinySieve
341 for (i = 0; i < tinybits; i++) {
342 if (BIT_TEST(TinySieve, i))
343 continue; /* 2*i+3 is composite */
345 /* The next tiny prime */
346 t = 2 * i + 3;
348 /* Mark all multiples of t */
349 for (j = i + t; j < tinybits; j += t)
350 BIT_SET(TinySieve, j);
352 sieve_large(t);
356 * Start the small block search at the next possible prime. To avoid
357 * fencepost errors, the last pass is skipped.
359 for (smallbase = TINY_NUMBER + 3;
360 smallbase < (SMALL_MAXIMUM - TINY_NUMBER);
361 smallbase += TINY_NUMBER) {
362 for (i = 0; i < tinybits; i++) {
363 if (BIT_TEST(TinySieve, i))
364 continue; /* 2*i+3 is composite */
366 /* The next tiny prime */
367 t = 2 * i + 3;
368 r = smallbase % t;
370 if (r == 0) {
371 s = 0; /* t divides into smallbase exactly */
372 } else {
373 /* smallbase+s is first entry divisible by t */
374 s = t - r;
378 * The sieve omits even numbers, so ensure that
379 * smallbase+s is odd. Then, step through the sieve
380 * in increments of 2*t
382 if (s & 1)
383 s += t; /* Make smallbase+s odd, and s even */
385 /* Mark all multiples of 2*t */
386 for (s /= 2; s < smallbits; s += t)
387 BIT_SET(SmallSieve, s);
391 * SmallSieve
393 for (i = 0; i < smallbits; i++) {
394 if (BIT_TEST(SmallSieve, i))
395 continue; /* 2*i+smallbase is composite */
397 /* The next small prime */
398 sieve_large((2 * i) + smallbase);
401 memset(SmallSieve, 0, smallwords << SHIFT_BYTE);
404 time(&time_stop);
406 logit("%.24s Sieved with %u small primes in %ld seconds",
407 ctime(&time_stop), largetries, (long) (time_stop - time_start));
409 for (j = r = 0; j < largebits; j++) {
410 if (BIT_TEST(LargeSieve, j))
411 continue; /* Definitely composite, skip */
413 debug2("test q = largebase+%u", 2 * j);
414 if (BN_set_word(q, 2 * j) == 0)
415 fatal("BN_set_word failed");
416 if (BN_add(q, q, largebase) == 0)
417 fatal("BN_add failed");
418 if (qfileout(out, MODULI_TYPE_SOPHIE_GERMAIN,
419 MODULI_TESTS_SIEVE, largetries,
420 (power - 1) /* MSB */, (0), q) == -1) {
421 ret = -1;
422 break;
425 r++; /* count q */
428 time(&time_stop);
430 xfree(LargeSieve);
431 xfree(SmallSieve);
432 xfree(TinySieve);
434 logit("%.24s Found %u candidates", ctime(&time_stop), r);
436 return (ret);
440 * perform a Miller-Rabin primality test
441 * on the list of candidates
442 * (checking both q and p)
443 * The result is a list of so-call "safe" primes
446 prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted)
448 BIGNUM *q, *p, *a;
449 BN_CTX *ctx;
450 char *cp, *lp;
451 u_int32_t count_in = 0, count_out = 0, count_possible = 0;
452 u_int32_t generator_known, in_tests, in_tries, in_type, in_size;
453 time_t time_start, time_stop;
454 int res;
456 if (trials < TRIAL_MINIMUM) {
457 error("Minimum primality trials is %d", TRIAL_MINIMUM);
458 return (-1);
461 time(&time_start);
463 if ((p = BN_new()) == NULL)
464 fatal("BN_new failed");
465 if ((q = BN_new()) == NULL)
466 fatal("BN_new failed");
467 if ((ctx = BN_CTX_new()) == NULL)
468 fatal("BN_CTX_new failed");
470 debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
471 ctime(&time_start), trials, generator_wanted);
473 res = 0;
474 lp = xmalloc(QLINESIZE + 1);
475 while (fgets(lp, QLINESIZE + 1, in) != NULL) {
476 count_in++;
477 if (strlen(lp) < 14 || *lp == '!' || *lp == '#') {
478 debug2("%10u: comment or short line", count_in);
479 continue;
482 /* XXX - fragile parser */
483 /* time */
484 cp = &lp[14]; /* (skip) */
486 /* type */
487 in_type = strtoul(cp, &cp, 10);
489 /* tests */
490 in_tests = strtoul(cp, &cp, 10);
492 if (in_tests & MODULI_TESTS_COMPOSITE) {
493 debug2("%10u: known composite", count_in);
494 continue;
497 /* tries */
498 in_tries = strtoul(cp, &cp, 10);
500 /* size (most significant bit) */
501 in_size = strtoul(cp, &cp, 10);
503 /* generator (hex) */
504 generator_known = strtoul(cp, &cp, 16);
506 /* Skip white space */
507 cp += strspn(cp, " ");
509 /* modulus (hex) */
510 switch (in_type) {
511 case MODULI_TYPE_SOPHIE_GERMAIN:
512 debug2("%10u: (%u) Sophie-Germain", count_in, in_type);
513 a = q;
514 if (BN_hex2bn(&a, cp) == 0)
515 fatal("BN_hex2bn failed");
516 /* p = 2*q + 1 */
517 if (BN_lshift(p, q, 1) == 0)
518 fatal("BN_lshift failed");
519 if (BN_add_word(p, 1) == 0)
520 fatal("BN_add_word failed");
521 in_size += 1;
522 generator_known = 0;
523 break;
524 case MODULI_TYPE_UNSTRUCTURED:
525 case MODULI_TYPE_SAFE:
526 case MODULI_TYPE_SCHNORR:
527 case MODULI_TYPE_STRONG:
528 case MODULI_TYPE_UNKNOWN:
529 debug2("%10u: (%u)", count_in, in_type);
530 a = p;
531 if (BN_hex2bn(&a, cp) == 0)
532 fatal("BN_hex2bn failed");
533 /* q = (p-1) / 2 */
534 if (BN_rshift(q, p, 1) == 0)
535 fatal("BN_rshift failed");
536 break;
537 default:
538 debug2("Unknown prime type");
539 break;
543 * due to earlier inconsistencies in interpretation, check
544 * the proposed bit size.
546 if ((u_int32_t)BN_num_bits(p) != (in_size + 1)) {
547 debug2("%10u: bit size %u mismatch", count_in, in_size);
548 continue;
550 if (in_size < QSIZE_MINIMUM) {
551 debug2("%10u: bit size %u too short", count_in, in_size);
552 continue;
555 if (in_tests & MODULI_TESTS_MILLER_RABIN)
556 in_tries += trials;
557 else
558 in_tries = trials;
561 * guess unknown generator
563 if (generator_known == 0) {
564 if (BN_mod_word(p, 24) == 11)
565 generator_known = 2;
566 else if (BN_mod_word(p, 12) == 5)
567 generator_known = 3;
568 else {
569 u_int32_t r = BN_mod_word(p, 10);
571 if (r == 3 || r == 7)
572 generator_known = 5;
576 * skip tests when desired generator doesn't match
578 if (generator_wanted > 0 &&
579 generator_wanted != generator_known) {
580 debug2("%10u: generator %d != %d",
581 count_in, generator_known, generator_wanted);
582 continue;
586 * Primes with no known generator are useless for DH, so
587 * skip those.
589 if (generator_known == 0) {
590 debug2("%10u: no known generator", count_in);
591 continue;
594 count_possible++;
597 * The (1/4)^N performance bound on Miller-Rabin is
598 * extremely pessimistic, so don't spend a lot of time
599 * really verifying that q is prime until after we know
600 * that p is also prime. A single pass will weed out the
601 * vast majority of composite q's.
603 if (BN_is_prime(q, 1, NULL, ctx, NULL) <= 0) {
604 debug("%10u: q failed first possible prime test",
605 count_in);
606 continue;
610 * q is possibly prime, so go ahead and really make sure
611 * that p is prime. If it is, then we can go back and do
612 * the same for q. If p is composite, chances are that
613 * will show up on the first Rabin-Miller iteration so it
614 * doesn't hurt to specify a high iteration count.
616 if (!BN_is_prime(p, trials, NULL, ctx, NULL)) {
617 debug("%10u: p is not prime", count_in);
618 continue;
620 debug("%10u: p is almost certainly prime", count_in);
622 /* recheck q more rigorously */
623 if (!BN_is_prime(q, trials - 1, NULL, ctx, NULL)) {
624 debug("%10u: q is not prime", count_in);
625 continue;
627 debug("%10u: q is almost certainly prime", count_in);
629 if (qfileout(out, MODULI_TYPE_SAFE,
630 in_tests | MODULI_TESTS_MILLER_RABIN,
631 in_tries, in_size, generator_known, p)) {
632 res = -1;
633 break;
636 count_out++;
639 time(&time_stop);
640 xfree(lp);
641 BN_free(p);
642 BN_free(q);
643 BN_CTX_free(ctx);
645 logit("%.24s Found %u safe primes of %u candidates in %ld seconds",
646 ctime(&time_stop), count_out, count_possible,
647 (long) (time_stop - time_start));
649 return (res);