summaryrefslogtreecommitdiff
path: root/moduli.c
diff options
context:
space:
mode:
authorAlistair Delva <adelva@google.com>2020-08-21 00:00:13 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2020-08-21 00:00:13 +0000
commited358b3546c776c1c677fd88eb8f716cf6187510 (patch)
tree3c6134bcb2cda4b9dccc57b4a8b997a945aab62d /moduli.c
parent22246b08952d746a7cc5a292570636cf4277598f (diff)
parent44a1065de8a58c51a021243a28bfa01e87822e4f (diff)
Merge changes I934c73d4,I28cdc9a0,I9e734da9,I3c079d86
* changes: UPSTREAM: depend UPSTREAM: upstream: avoid possible NULL deref; from Pedro Martelletto Revert "upstream: fix compilation with DEBUG_KEXDH; bz#3160 ok dtucker@" Merge upstream-master into master
Diffstat (limited to 'moduli.c')
-rw-r--r--moduli.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/moduli.c b/moduli.c
index ed1bdc94..8dd36b1c 100644
--- a/moduli.c
+++ b/moduli.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: moduli.c,v 1.30 2015/01/20 23:14:00 deraadt Exp $ */
+/* $OpenBSD: moduli.c,v 1.37 2019/11/15 06:00:20 djm Exp $ */
/*
* Copyright 1994 Phil Karn <karn@qualcomm.com>
* Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com>
@@ -41,7 +41,6 @@
#ifdef WITH_OPENSSL
-#include <sys/param.h> /* MAX */
#include <sys/types.h>
#include <openssl/bn.h>
@@ -160,6 +159,8 @@ qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries,
time(&time_now);
gtm = gmtime(&time_now);
+ if (gtm == NULL)
+ return -1;
res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday,
@@ -413,8 +414,8 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
time(&time_stop);
- logit("%.24s Sieved with %u small primes in %ld seconds",
- ctime(&time_stop), largetries, (long) (time_stop - time_start));
+ logit("%.24s Sieved with %u small primes in %lld seconds",
+ ctime(&time_stop), largetries, (long long)(time_stop - time_start));
for (j = r = 0; j < largebits; j++) {
if (BIT_TEST(LargeSieve, j))
@@ -454,7 +455,7 @@ write_checkpoint(char *cpfile, u_int32_t lineno)
int r;
r = snprintf(tmp, sizeof(tmp), "%s.XXXXXXXXXX", cpfile);
- if (r == -1 || r >= PATH_MAX) {
+ if (r < 0 || r >= PATH_MAX) {
logit("write_checkpoint: temp pathname too long");
return;
}
@@ -577,13 +578,12 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
char *checkpoint_file, unsigned long start_lineno, unsigned long num_lines)
{
BIGNUM *q, *p, *a;
- BN_CTX *ctx;
char *cp, *lp;
u_int32_t count_in = 0, count_out = 0, count_possible = 0;
u_int32_t generator_known, in_tests, in_tries, in_type, in_size;
unsigned long last_processed = 0, end_lineno;
time_t time_start, time_stop;
- int res;
+ int res, is_prime;
if (trials < TRIAL_MINIMUM) {
error("Minimum primality trials is %d", TRIAL_MINIMUM);
@@ -601,15 +601,13 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
fatal("BN_new failed");
if ((q = BN_new()) == NULL)
fatal("BN_new failed");
- if ((ctx = BN_CTX_new()) == NULL)
- fatal("BN_CTX_new failed");
debug2("%.24s Final %u Miller-Rabin trials (%x generator)",
ctime(&time_start), trials, generator_wanted);
if (checkpoint_file != NULL)
last_processed = read_checkpoint(checkpoint_file);
- last_processed = start_lineno = MAX(last_processed, start_lineno);
+ last_processed = start_lineno = MAXIMUM(last_processed, start_lineno);
if (end_lineno == ULONG_MAX)
debug("process from line %lu from pipe", last_processed);
else
@@ -717,8 +715,6 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
if (generator_known == 0) {
if (BN_mod_word(p, 24) == 11)
generator_known = 2;
- else if (BN_mod_word(p, 12) == 5)
- generator_known = 3;
else {
u_int32_t r = BN_mod_word(p, 10);
@@ -754,7 +750,10 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
* that p is also prime. A single pass will weed out the
* vast majority of composite q's.
*/
- if (BN_is_prime_ex(q, 1, ctx, NULL) <= 0) {
+ is_prime = BN_is_prime_ex(q, 1, NULL, NULL);
+ if (is_prime < 0)
+ fatal("BN_is_prime_ex failed");
+ if (is_prime == 0) {
debug("%10u: q failed first possible prime test",
count_in);
continue;
@@ -767,14 +766,20 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
* will show up on the first Rabin-Miller iteration so it
* doesn't hurt to specify a high iteration count.
*/
- if (!BN_is_prime_ex(p, trials, ctx, NULL)) {
+ is_prime = BN_is_prime_ex(p, trials, NULL, NULL);
+ if (is_prime < 0)
+ fatal("BN_is_prime_ex failed");
+ if (is_prime == 0) {
debug("%10u: p is not prime", count_in);
continue;
}
debug("%10u: p is almost certainly prime", count_in);
/* recheck q more rigorously */
- if (!BN_is_prime_ex(q, trials - 1, ctx, NULL)) {
+ is_prime = BN_is_prime_ex(q, trials - 1, NULL, NULL);
+ if (is_prime < 0)
+ fatal("BN_is_prime_ex failed");
+ if (is_prime == 0) {
debug("%10u: q is not prime", count_in);
continue;
}
@@ -794,7 +799,6 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted,
free(lp);
BN_free(p);
BN_free(q);
- BN_CTX_free(ctx);
if (checkpoint_file != NULL)
unlink(checkpoint_file);