diff options
author | Colin Watson <cjwatson@debian.org> | 2012-05-17 13:04:02 +0100 |
---|---|---|
committer | Colin Watson <cjwatson@debian.org> | 2012-05-17 13:04:02 +0100 |
commit | dd5ed53e20d218607260916a6b04d1c8c5b3d88f (patch) | |
tree | c59b4dbcc610f10700945f885adf3bddc2542c26 /moduli.c | |
parent | 8241a65bf12ac53c1b7304bba7ce739aad80b8b8 (diff) | |
parent | b9bc38990c5eb5d99e28ca5af6d3491fd4a0060a (diff) |
merge 6.0p1
Diffstat (limited to 'moduli.c')
-rw-r--r-- | moduli.c | 71 |
1 files changed, 68 insertions, 3 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: moduli.c,v 1.22 2010/11/10 01:33:07 djm Exp $ */ | 1 | /* $OpenBSD: moduli.c,v 1.25 2011/10/19 00:06:10 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright 1994 Phil Karn <karn@qualcomm.com> | 3 | * Copyright 1994 Phil Karn <karn@qualcomm.com> |
4 | * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com> | 4 | * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com> |
@@ -39,16 +39,19 @@ | |||
39 | 39 | ||
40 | #include "includes.h" | 40 | #include "includes.h" |
41 | 41 | ||
42 | #include <sys/param.h> | ||
42 | #include <sys/types.h> | 43 | #include <sys/types.h> |
43 | 44 | ||
44 | #include <openssl/bn.h> | 45 | #include <openssl/bn.h> |
45 | #include <openssl/dh.h> | 46 | #include <openssl/dh.h> |
46 | 47 | ||
48 | #include <errno.h> | ||
47 | #include <stdio.h> | 49 | #include <stdio.h> |
48 | #include <stdlib.h> | 50 | #include <stdlib.h> |
49 | #include <string.h> | 51 | #include <string.h> |
50 | #include <stdarg.h> | 52 | #include <stdarg.h> |
51 | #include <time.h> | 53 | #include <time.h> |
54 | #include <unistd.h> | ||
52 | 55 | ||
53 | #include "xmalloc.h" | 56 | #include "xmalloc.h" |
54 | #include "dh.h" | 57 | #include "dh.h" |
@@ -137,7 +140,7 @@ static u_int32_t largebits, largememory; /* megabytes */ | |||
137 | static BIGNUM *largebase; | 140 | static BIGNUM *largebase; |
138 | 141 | ||
139 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); | 142 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); |
140 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t); | 143 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *); |
141 | 144 | ||
142 | /* | 145 | /* |
143 | * print moduli out in consistent form, | 146 | * print moduli out in consistent form, |
@@ -438,6 +441,52 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start) | |||
438 | return (ret); | 441 | return (ret); |
439 | } | 442 | } |
440 | 443 | ||
444 | static void | ||
445 | write_checkpoint(char *cpfile, u_int32_t lineno) | ||
446 | { | ||
447 | FILE *fp; | ||
448 | char tmp[MAXPATHLEN]; | ||
449 | int r; | ||
450 | |||
451 | r = snprintf(tmp, sizeof(tmp), "%s.XXXXXXXXXX", cpfile); | ||
452 | if (r == -1 || r >= MAXPATHLEN) { | ||
453 | logit("write_checkpoint: temp pathname too long"); | ||
454 | return; | ||
455 | } | ||
456 | if ((r = mkstemp(tmp)) == -1) { | ||
457 | logit("mkstemp(%s): %s", tmp, strerror(errno)); | ||
458 | return; | ||
459 | } | ||
460 | if ((fp = fdopen(r, "w")) == NULL) { | ||
461 | logit("write_checkpoint: fdopen: %s", strerror(errno)); | ||
462 | close(r); | ||
463 | return; | ||
464 | } | ||
465 | if (fprintf(fp, "%lu\n", (unsigned long)lineno) > 0 && fclose(fp) == 0 | ||
466 | && rename(tmp, cpfile) == 0) | ||
467 | debug3("wrote checkpoint line %lu to '%s'", | ||
468 | (unsigned long)lineno, cpfile); | ||
469 | else | ||
470 | logit("failed to write to checkpoint file '%s': %s", cpfile, | ||
471 | strerror(errno)); | ||
472 | } | ||
473 | |||
474 | static unsigned long | ||
475 | read_checkpoint(char *cpfile) | ||
476 | { | ||
477 | FILE *fp; | ||
478 | unsigned long lineno = 0; | ||
479 | |||
480 | if ((fp = fopen(cpfile, "r")) == NULL) | ||
481 | return 0; | ||
482 | if (fscanf(fp, "%lu\n", &lineno) < 1) | ||
483 | logit("Failed to load checkpoint from '%s'", cpfile); | ||
484 | else | ||
485 | logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno); | ||
486 | fclose(fp); | ||
487 | return lineno; | ||
488 | } | ||
489 | |||
441 | /* | 490 | /* |
442 | * perform a Miller-Rabin primality test | 491 | * perform a Miller-Rabin primality test |
443 | * on the list of candidates | 492 | * on the list of candidates |
@@ -445,13 +494,15 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start) | |||
445 | * The result is a list of so-call "safe" primes | 494 | * The result is a list of so-call "safe" primes |
446 | */ | 495 | */ |
447 | int | 496 | int |
448 | prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | 497 | prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted, |
498 | char *checkpoint_file) | ||
449 | { | 499 | { |
450 | BIGNUM *q, *p, *a; | 500 | BIGNUM *q, *p, *a; |
451 | BN_CTX *ctx; | 501 | BN_CTX *ctx; |
452 | char *cp, *lp; | 502 | char *cp, *lp; |
453 | u_int32_t count_in = 0, count_out = 0, count_possible = 0; | 503 | u_int32_t count_in = 0, count_out = 0, count_possible = 0; |
454 | u_int32_t generator_known, in_tests, in_tries, in_type, in_size; | 504 | u_int32_t generator_known, in_tests, in_tries, in_type, in_size; |
505 | unsigned long last_processed = 0; | ||
455 | time_t time_start, time_stop; | 506 | time_t time_start, time_stop; |
456 | int res; | 507 | int res; |
457 | 508 | ||
@@ -472,10 +523,21 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | |||
472 | debug2("%.24s Final %u Miller-Rabin trials (%x generator)", | 523 | debug2("%.24s Final %u Miller-Rabin trials (%x generator)", |
473 | ctime(&time_start), trials, generator_wanted); | 524 | ctime(&time_start), trials, generator_wanted); |
474 | 525 | ||
526 | if (checkpoint_file != NULL) | ||
527 | last_processed = read_checkpoint(checkpoint_file); | ||
528 | |||
475 | res = 0; | 529 | res = 0; |
476 | lp = xmalloc(QLINESIZE + 1); | 530 | lp = xmalloc(QLINESIZE + 1); |
477 | while (fgets(lp, QLINESIZE + 1, in) != NULL) { | 531 | while (fgets(lp, QLINESIZE + 1, in) != NULL) { |
478 | count_in++; | 532 | count_in++; |
533 | if (checkpoint_file != NULL) { | ||
534 | if (count_in <= last_processed) { | ||
535 | debug3("skipping line %u, before checkpoint", | ||
536 | count_in); | ||
537 | continue; | ||
538 | } | ||
539 | write_checkpoint(checkpoint_file, count_in); | ||
540 | } | ||
479 | if (strlen(lp) < 14 || *lp == '!' || *lp == '#') { | 541 | if (strlen(lp) < 14 || *lp == '!' || *lp == '#') { |
480 | debug2("%10u: comment or short line", count_in); | 542 | debug2("%10u: comment or short line", count_in); |
481 | continue; | 543 | continue; |
@@ -644,6 +706,9 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | |||
644 | BN_free(q); | 706 | BN_free(q); |
645 | BN_CTX_free(ctx); | 707 | BN_CTX_free(ctx); |
646 | 708 | ||
709 | if (checkpoint_file != NULL) | ||
710 | unlink(checkpoint_file); | ||
711 | |||
647 | logit("%.24s Found %u safe primes of %u candidates in %ld seconds", | 712 | logit("%.24s Found %u safe primes of %u candidates in %ld seconds", |
648 | ctime(&time_stop), count_out, count_possible, | 713 | ctime(&time_stop), count_out, count_possible, |
649 | (long) (time_stop - time_start)); | 714 | (long) (time_stop - time_start)); |