blob: f737cb3f517d2063ac30d4774da4e0d06dcc6677 [file] [log] [blame]
Damien Miller2e9cf492008-06-29 22:47:04 +10001/* $OpenBSD: moduli.c,v 1.21 2008/06/26 09:19:40 djm Exp $ */
Darren Tuckerb2f9d412003-08-02 23:51:38 +10002/*
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"
Damien Miller5598b4f2006-07-24 14:09:40 +100041
42#include <sys/types.h>
Darren Tuckerb2f9d412003-08-02 23:51:38 +100043
44#include <openssl/bn.h>
Damien Miller2e9cf492008-06-29 22:47:04 +100045#include <openssl/dh.h>
Darren Tuckerb2f9d412003-08-02 23:51:38 +100046
Damien Millera7a73ee2006-08-05 11:37:59 +100047#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100048#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100049#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100050#include <stdarg.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100051#include <time.h>
52
53#include "xmalloc.h"
Damien Miller2e9cf492008-06-29 22:47:04 +100054#include "dh.h"
Damien Miller5598b4f2006-07-24 14:09:40 +100055#include "log.h"
56
Darren Tuckerb2f9d412003-08-02 23:51:38 +100057/*
58 * File output defines
59 */
60
61/* need line long enough for largest moduli plus headers */
Darren Tuckerfc959702004-07-17 16:12:08 +100062#define QLINESIZE (100+8192)
Darren Tuckerb2f9d412003-08-02 23:51:38 +100063
Darren Tucker06930c72003-12-31 11:34:51 +110064/*
65 * Size: decimal.
Darren Tuckerb2f9d412003-08-02 23:51:38 +100066 * Specifies the number of the most significant bit (0 to M).
Darren Tucker06930c72003-12-31 11:34:51 +110067 * WARNING: internally, usually 1 to N.
Darren Tuckerb2f9d412003-08-02 23:51:38 +100068 */
Darren Tuckerfc959702004-07-17 16:12:08 +100069#define QSIZE_MINIMUM (511)
Darren Tuckerb2f9d412003-08-02 23:51:38 +100070
71/*
72 * Prime sieving defines
73 */
74
75/* Constant: assuming 8 bit bytes and 32 bit words */
Darren Tuckerfc959702004-07-17 16:12:08 +100076#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)
Darren Tuckerb2f9d412003-08-02 23:51:38 +100081
82/*
Darren Tucker770fc012004-05-13 16:24:32 +100083 * 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!
86 */
Darren Tuckerfc959702004-07-17 16:12:08 +100087#define LARGE_MINIMUM (8UL) /* megabytes */
Darren Tucker770fc012004-05-13 16:24:32 +100088
89/*
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).
92 */
Darren Tuckerfc959702004-07-17 16:12:08 +100093#define LARGE_MAXIMUM (127UL) /* megabytes */
Darren Tucker770fc012004-05-13 16:24:32 +100094
95/*
Darren Tuckerb2f9d412003-08-02 23:51:38 +100096 * Constant: when used with 32-bit integers, the largest sieve prime
97 * has to be less than 2**32.
98 */
Darren Tuckerfc959702004-07-17 16:12:08 +100099#define SMALL_MAXIMUM (0xffffffffUL)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000100
101/* Constant: can sieve all primes less than 2**32, as 65537**2 > 2**32-1. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000102#define TINY_NUMBER (1UL<<16)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000103
104/* Ensure enough bit space for testing 2*q. */
Damien Miller0dc1bef2005-07-17 17:22:45 +1000105#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 */
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000109
110/* bit operations on 32-bit words */
Damien Miller0dc1bef2005-07-17 17:22:45 +1000111#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)))
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000114
115/*
116 * Prime testing defines
117 */
118
Darren Tucker770fc012004-05-13 16:24:32 +1000119/* Minimum number of primality tests to perform */
Damien Miller0dc1bef2005-07-17 17:22:45 +1000120#define TRIAL_MINIMUM (4)
Darren Tucker770fc012004-05-13 16:24:32 +1000121
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000122/*
123 * Sieving data (XXX - move to struct)
124 */
125
126/* sieve 2**16 */
127static u_int32_t *TinySieve, tinybits;
128
129/* sieve 2**30 in 2**16 parts */
130static u_int32_t *SmallSieve, smallbits, smallbase;
131
132/* sieve relative to the initial value */
133static u_int32_t *LargeSieve, largewords, largetries, largenumbers;
134static u_int32_t largebits, largememory; /* megabytes */
135static BIGNUM *largebase;
136
Damien Millerb089fb52005-05-26 12:16:18 +1000137int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tuckere4ab1152004-05-24 10:14:24 +1000138int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000139
140/*
141 * print moduli out in consistent form,
142 */
143static int
144qfileout(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)
146{
147 struct tm *gtm;
148 time_t time_now;
149 int res;
150
151 time(&time_now);
152 gtm = gmtime(&time_now);
Damien Miller787b2ec2003-11-21 23:56:47 +1100153
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000154 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);
158
159 if (res < 0)
160 return (-1);
161
162 if (BN_print_fp(ofile, omodulus) < 1)
163 return (-1);
164
165 res = fprintf(ofile, "\n");
166 fflush(ofile);
167
168 return (res > 0 ? 0 : -1);
169}
170
171
172/*
173 ** Sieve p's and q's with small factors
174 */
175static void
176sieve_large(u_int32_t s)
177{
178 u_int32_t r, u;
179
Darren Tucker06930c72003-12-31 11:34:51 +1100180 debug3("sieve_large %u", s);
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000181 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 */
188
189 if (u < largebits * 2) {
190 /*
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
194 */
195 if (u & 0x1)
196 u += s; /* Make largebase+u odd, and u even */
197
198 /* Mark all multiples of 2*s */
199 for (u /= 2; u < largebits; u += s)
200 BIT_SET(LargeSieve, u);
201 }
202
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 */
209
210 if (u < largebits * 4) {
211 /*
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
215 */
216 while (u & 0x3) {
217 if (SMALL_MAXIMUM - u < s)
218 return;
219 u += s;
220 }
221
222 /* Mark all multiples of 4*s */
223 for (u /= 4; u < largebits; u += s)
224 BIT_SET(LargeSieve, u);
225 }
226}
227
228/*
Darren Tucker47abce42004-05-02 22:09:00 +1000229 * list candidates for Sophie-Germain primes (where q = (p-1)/2)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000230 * to standard output.
231 * The list is checked against small known primes (less than 2**30).
232 */
233int
Damien Millerb089fb52005-05-26 12:16:18 +1000234gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000235{
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;
Damien Millerb089fb52005-05-26 12:16:18 +1000241 u_int32_t i;
242 int ret = 0;
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000243
244 largememory = memory;
245
Darren Tucker770fc012004-05-13 16:24:32 +1000246 if (memory != 0 &&
Damien Miller0dc1bef2005-07-17 17:22:45 +1000247 (memory < LARGE_MINIMUM || memory > LARGE_MAXIMUM)) {
Darren Tucker770fc012004-05-13 16:24:32 +1000248 error("Invalid memory amount (min %ld, max %ld)",
249 LARGE_MINIMUM, LARGE_MAXIMUM);
250 return (-1);
251 }
252
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000253 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100254 * 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.
256 */
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000257 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);
263 }
264 power--; /* decrement before squaring */
265
266 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100267 * 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.
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000271 */
272 largewords = ((power * power) >> (SHIFT_WORD - TEST_POWER));
273
274 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100275 * Need idea of how much memory is available. We don't have to use all
276 * of it.
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000277 */
278 if (largememory > LARGE_MAXIMUM) {
279 logit("Limited memory: %u MB; limit %lu MB",
280 largememory, LARGE_MAXIMUM);
281 largememory = LARGE_MAXIMUM;
282 }
283
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);
292 }
293
Damien Miller07d86be2006-03-26 14:19:21 +1100294 TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000295 tinybits = tinywords << SHIFT_WORD;
296
Damien Miller07d86be2006-03-26 14:19:21 +1100297 SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000298 smallbits = smallwords << SHIFT_WORD;
299
300 /*
301 * dynamically determine available memory
302 */
303 while ((LargeSieve = calloc(largewords, sizeof(u_int32_t))) == NULL)
304 largewords -= (1L << (SHIFT_MEGAWORD - 2)); /* 1/4 MB chunks */
305
306 largebits = largewords << SHIFT_WORD;
307 largenumbers = largebits * 2; /* even numbers excluded */
308
309 /* validation check: count the number of primes tried */
310 largetries = 0;
Darren Tucker0bc85572006-11-07 23:14:41 +1100311 if ((q = BN_new()) == NULL)
312 fatal("BN_new failed");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000313
314 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100315 * Generate random starting point for subprime search, or use
316 * specified parameter.
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000317 */
Darren Tucker0bc85572006-11-07 23:14:41 +1100318 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");
326 }
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000327
328 /* ensure odd */
Darren Tucker0bc85572006-11-07 23:14:41 +1100329 if (BN_set_bit(largebase, 0) == 0)
330 fatal("BN_set_bit: failed");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000331
332 time(&time_start);
333
Damien Millera8e06ce2003-11-21 23:48:55 +1100334 logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start),
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000335 largenumbers, power);
336 debug2("start point: 0x%s", BN_bn2hex(largebase));
337
338 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100339 * TinySieve
340 */
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000341 for (i = 0; i < tinybits; i++) {
342 if (BIT_TEST(TinySieve, i))
343 continue; /* 2*i+3 is composite */
344
345 /* The next tiny prime */
346 t = 2 * i + 3;
347
348 /* Mark all multiples of t */
349 for (j = i + t; j < tinybits; j += t)
350 BIT_SET(TinySieve, j);
351
352 sieve_large(t);
353 }
354
355 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100356 * Start the small block search at the next possible prime. To avoid
357 * fencepost errors, the last pass is skipped.
358 */
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000359 for (smallbase = TINY_NUMBER + 3;
Damien Miller0dc1bef2005-07-17 17:22:45 +1000360 smallbase < (SMALL_MAXIMUM - TINY_NUMBER);
361 smallbase += TINY_NUMBER) {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000362 for (i = 0; i < tinybits; i++) {
363 if (BIT_TEST(TinySieve, i))
364 continue; /* 2*i+3 is composite */
365
366 /* The next tiny prime */
367 t = 2 * i + 3;
368 r = smallbase % t;
369
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;
375 }
376
377 /*
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
381 */
382 if (s & 1)
383 s += t; /* Make smallbase+s odd, and s even */
384
385 /* Mark all multiples of 2*t */
386 for (s /= 2; s < smallbits; s += t)
387 BIT_SET(SmallSieve, s);
388 }
389
390 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100391 * SmallSieve
392 */
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000393 for (i = 0; i < smallbits; i++) {
394 if (BIT_TEST(SmallSieve, i))
395 continue; /* 2*i+smallbase is composite */
396
397 /* The next small prime */
398 sieve_large((2 * i) + smallbase);
399 }
400
401 memset(SmallSieve, 0, smallwords << SHIFT_BYTE);
402 }
403
404 time(&time_stop);
405
406 logit("%.24s Sieved with %u small primes in %ld seconds",
407 ctime(&time_stop), largetries, (long) (time_stop - time_start));
408
409 for (j = r = 0; j < largebits; j++) {
410 if (BIT_TEST(LargeSieve, j))
411 continue; /* Definitely composite, skip */
412
413 debug2("test q = largebase+%u", 2 * j);
Darren Tucker0bc85572006-11-07 23:14:41 +1100414 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");
Damien Miller2e9cf492008-06-29 22:47:04 +1000418 if (qfileout(out, MODULI_TYPE_SOPHIE_GERMAIN,
419 MODULI_TESTS_SIEVE, largetries,
420 (power - 1) /* MSB */, (0), q) == -1) {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000421 ret = -1;
422 break;
423 }
424
425 r++; /* count q */
426 }
427
428 time(&time_stop);
429
430 xfree(LargeSieve);
431 xfree(SmallSieve);
432 xfree(TinySieve);
433
434 logit("%.24s Found %u candidates", ctime(&time_stop), r);
435
436 return (ret);
437}
438
439/*
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
444 */
445int
Darren Tucker770fc012004-05-13 16:24:32 +1000446prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000447{
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;
455
Darren Tucker770fc012004-05-13 16:24:32 +1000456 if (trials < TRIAL_MINIMUM) {
457 error("Minimum primality trials is %d", TRIAL_MINIMUM);
458 return (-1);
459 }
460
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000461 time(&time_start);
462
Darren Tucker0bc85572006-11-07 23:14:41 +1100463 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");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000469
470 debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
471 ctime(&time_start), trials, generator_wanted);
472
473 res = 0;
474 lp = xmalloc(QLINESIZE + 1);
Darren Tucker90aaed42007-02-25 20:38:55 +1100475 while (fgets(lp, QLINESIZE + 1, in) != NULL) {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000476 count_in++;
Darren Tucker90aaed42007-02-25 20:38:55 +1100477 if (strlen(lp) < 14 || *lp == '!' || *lp == '#') {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000478 debug2("%10u: comment or short line", count_in);
479 continue;
480 }
481
482 /* XXX - fragile parser */
483 /* time */
484 cp = &lp[14]; /* (skip) */
485
486 /* type */
487 in_type = strtoul(cp, &cp, 10);
488
489 /* tests */
490 in_tests = strtoul(cp, &cp, 10);
491
Damien Miller2e9cf492008-06-29 22:47:04 +1000492 if (in_tests & MODULI_TESTS_COMPOSITE) {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000493 debug2("%10u: known composite", count_in);
494 continue;
495 }
Darren Tucker06930c72003-12-31 11:34:51 +1100496
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000497 /* tries */
498 in_tries = strtoul(cp, &cp, 10);
499
500 /* size (most significant bit) */
501 in_size = strtoul(cp, &cp, 10);
502
503 /* generator (hex) */
504 generator_known = strtoul(cp, &cp, 16);
505
506 /* Skip white space */
507 cp += strspn(cp, " ");
508
509 /* modulus (hex) */
510 switch (in_type) {
Damien Miller2e9cf492008-06-29 22:47:04 +1000511 case MODULI_TYPE_SOPHIE_GERMAIN:
Darren Tucker47abce42004-05-02 22:09:00 +1000512 debug2("%10u: (%u) Sophie-Germain", count_in, in_type);
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000513 a = q;
Darren Tucker0bc85572006-11-07 23:14:41 +1100514 if (BN_hex2bn(&a, cp) == 0)
515 fatal("BN_hex2bn failed");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000516 /* p = 2*q + 1 */
Darren Tucker0bc85572006-11-07 23:14:41 +1100517 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");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000521 in_size += 1;
522 generator_known = 0;
523 break;
Damien Miller2e9cf492008-06-29 22:47:04 +1000524 case MODULI_TYPE_UNSTRUCTURED:
525 case MODULI_TYPE_SAFE:
526 case MODULI_TYPE_SCHNORR:
527 case MODULI_TYPE_STRONG:
528 case MODULI_TYPE_UNKNOWN:
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000529 debug2("%10u: (%u)", count_in, in_type);
530 a = p;
Darren Tucker0bc85572006-11-07 23:14:41 +1100531 if (BN_hex2bn(&a, cp) == 0)
532 fatal("BN_hex2bn failed");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000533 /* q = (p-1) / 2 */
Darren Tucker0bc85572006-11-07 23:14:41 +1100534 if (BN_rshift(q, p, 1) == 0)
535 fatal("BN_rshift failed");
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000536 break;
Darren Tucker06930c72003-12-31 11:34:51 +1100537 default:
538 debug2("Unknown prime type");
539 break;
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000540 }
541
542 /*
543 * due to earlier inconsistencies in interpretation, check
544 * the proposed bit size.
545 */
Damien Millerb089fb52005-05-26 12:16:18 +1000546 if ((u_int32_t)BN_num_bits(p) != (in_size + 1)) {
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000547 debug2("%10u: bit size %u mismatch", count_in, in_size);
548 continue;
549 }
550 if (in_size < QSIZE_MINIMUM) {
551 debug2("%10u: bit size %u too short", count_in, in_size);
552 continue;
553 }
554
Damien Miller2e9cf492008-06-29 22:47:04 +1000555 if (in_tests & MODULI_TESTS_MILLER_RABIN)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000556 in_tries += trials;
557 else
558 in_tries = trials;
Darren Tucker06930c72003-12-31 11:34:51 +1100559
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000560 /*
561 * guess unknown generator
562 */
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);
570
Darren Tucker06930c72003-12-31 11:34:51 +1100571 if (r == 3 || r == 7)
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000572 generator_known = 5;
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000573 }
574 }
575 /*
576 * skip tests when desired generator doesn't match
577 */
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;
583 }
584
Darren Tucker5cd9d442003-12-10 00:54:38 +1100585 /*
586 * Primes with no known generator are useless for DH, so
587 * skip those.
588 */
589 if (generator_known == 0) {
590 debug2("%10u: no known generator", count_in);
591 continue;
592 }
593
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000594 count_possible++;
595
596 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100597 * 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
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000601 * vast majority of composite q's.
602 */
603 if (BN_is_prime(q, 1, NULL, ctx, NULL) <= 0) {
Darren Tucker06930c72003-12-31 11:34:51 +1100604 debug("%10u: q failed first possible prime test",
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000605 count_in);
606 continue;
607 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100608
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000609 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100610 * 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
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000613 * will show up on the first Rabin-Miller iteration so it
614 * doesn't hurt to specify a high iteration count.
615 */
616 if (!BN_is_prime(p, trials, NULL, ctx, NULL)) {
Darren Tucker06930c72003-12-31 11:34:51 +1100617 debug("%10u: p is not prime", count_in);
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000618 continue;
619 }
620 debug("%10u: p is almost certainly prime", count_in);
621
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;
626 }
627 debug("%10u: q is almost certainly prime", count_in);
628
Damien Miller2e9cf492008-06-29 22:47:04 +1000629 if (qfileout(out, MODULI_TYPE_SAFE,
630 in_tests | MODULI_TESTS_MILLER_RABIN,
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000631 in_tries, in_size, generator_known, p)) {
632 res = -1;
633 break;
634 }
635
636 count_out++;
637 }
638
639 time(&time_stop);
640 xfree(lp);
641 BN_free(p);
642 BN_free(q);
643 BN_CTX_free(ctx);
644
645 logit("%.24s Found %u safe primes of %u candidates in %ld seconds",
Damien Millera8e06ce2003-11-21 23:48:55 +1100646 ctime(&time_stop), count_out, count_possible,
Darren Tuckerb2f9d412003-08-02 23:51:38 +1000647 (long) (time_stop - time_start));
648
649 return (res);
650}