summaryrefslogtreecommitdiff
path: root/sbin/photurisd/modulus.c
diff options
context:
space:
mode:
authorNiels Provos <provos@cvs.openbsd.org>2000-12-15 01:58:28 +0000
committerNiels Provos <provos@cvs.openbsd.org>2000-12-15 01:58:28 +0000
commitc72bae0731852b536fecc600798b101c0d585837 (patch)
tree227ca6f97535de5c6836adea0b7f7ab29edba767 /sbin/photurisd/modulus.c
parentd64d9965935bc9dab6d6cb22352fc47cd826ae0a (diff)
more cleanup. remove last vestiges of gmp.
Diffstat (limited to 'sbin/photurisd/modulus.c')
-rw-r--r--sbin/photurisd/modulus.c239
1 files changed, 113 insertions, 126 deletions
diff --git a/sbin/photurisd/modulus.c b/sbin/photurisd/modulus.c
index 3ecd2be45e2..05671a8fb82 100644
--- a/sbin/photurisd/modulus.c
+++ b/sbin/photurisd/modulus.c
@@ -1,5 +1,5 @@
/*
- * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
+ * Copyright 1997-2000 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -45,46 +45,29 @@
#include "modulus.h"
#include "log.h"
-static struct moduli_cache *modob = NULL;
+TAILQ_HEAD(modlist, moduli_cache) modhead;
+
+void
+mod_init(void)
+{
+ TAILQ_INIT(&modhead);
+}
int
mod_insert(struct moduli_cache *ob)
{
- struct moduli_cache *tmp;
-
- ob->next = NULL;
-
- if(modob == NULL) {
- modob = ob;
- return 1;
- }
-
- tmp=modob;
- while(tmp->next!=NULL)
- tmp = tmp->next;
+ TAILQ_INSERT_TAIL(&modhead, ob, next);
- tmp->next = ob;
- return 1;
+ return (1);
}
int
mod_unlink(struct moduli_cache *ob)
{
- struct moduli_cache *tmp;
- if(modob == ob) {
- modob = ob->next;
- free(ob);
- return 1;
- }
+ TAILQ_REMOVE(&modhead, ob, next);
+ free(ob);
- for(tmp=modob; tmp!=NULL; tmp=tmp->next) {
- if(tmp->next==ob) {
- tmp->next=ob->next;
- free(ob);
- return 1;
- }
- }
- return 0;
+ return (0);
}
/*
@@ -96,69 +79,65 @@ mod_unlink(struct moduli_cache *ob)
void
mod_check_prime(int iter, int tm)
{
- struct moduli_cache *p = modob, *tmp;
- time_t now;
- int flag;
- BN_CTX *ctx;
-
-#ifdef DEBUG
- char *hex;
-#endif
-
- ctx = BN_CTX_new();
-
- now = time(NULL);
- while (p != NULL && (tm == 0 || (time(NULL) - now < tm))) {
- if (p->iterations < MOD_PRIME_MAX &&
- (p->status == MOD_UNUSED || p->status == MOD_COMPUTING)) {
-#ifdef DEBUG
- hex = BN_bn2hex(p->modulus);
- printf(" Checking 0x%s for primality: ", hex);
- fflush(stdout);
- free(hex);
-#endif
- flag = BN_is_prime(p->modulus, iter, NULL, ctx, NULL);
- if (!flag)
- log_print("found a non prime in mod_check_prime()");
-
- tmp = mod_find_modulus(p->modulus);
- while (tmp != NULL) {
- if (!flag) {
- tmp->status = MOD_NOTPRIME;
- tmp->lifetime = now + 2*MOD_TIMEOUT;
- } else {
- tmp->iterations += iter;
- if (tmp->iterations >= MOD_PRIME_MAX)
- tmp->status = MOD_PRIME;
- else
- tmp->status = MOD_COMPUTING;
- }
- tmp = mod_find_modulus_next(tmp, p->modulus);
- }
-#ifdef DEBUG
- if (!flag)
- printf("not prime\n");
- else if (p->iterations >= MOD_PRIME_MAX)
- printf("probably prime.\n");
- else
- printf("undecided.\n");
+ struct moduli_cache *p, *tmp, *next;
+ time_t now;
+ int flag;
+ BN_CTX *ctx;
+
+ ctx = BN_CTX_new();
+
+ now = time(NULL);
+ for (p = TAILQ_FIRST(&modhead);
+ p != NULL && (tm == 0 || (time(NULL) - now < tm)); p = next) {
+ next = TAILQ_NEXT(p, next);
+
+ if (p->iterations < MOD_PRIME_MAX &&
+ (p->status == MOD_UNUSED || p->status == MOD_COMPUTING)) {
+ flag = BN_is_prime(p->modulus, iter, NULL, ctx, NULL);
+ if (!flag)
+ log_print(__FUNCTION__": found a non prime");
+
+ tmp = mod_find_modulus(p->modulus);
+ while (tmp != NULL) {
+ if (!flag) {
+ tmp->status = MOD_NOTPRIME;
+ tmp->lifetime = now + 2*MOD_TIMEOUT;
+ } else {
+ tmp->iterations += iter;
+ if (tmp->iterations >= MOD_PRIME_MAX)
+ tmp->status = MOD_PRIME;
+ else
+ tmp->status = MOD_COMPUTING;
+ }
+ tmp = mod_find_modulus_next(tmp, p->modulus);
+ }
+#ifdef USE_DEBUG
+ {
+ char *hex, *msg;
+ if (!flag)
+ msg = "not prime.";
+ else if (p->iterations >= MOD_PRIME_MAX)
+ msg = "probably prime.";
+ else
+ msg = "undecided.";
+ hex = BN_bn2hex(p->modulus);
+ LOG_DBG((LOG_CRYPTO, 50, __FUNCTION__
+ ": check prime: %s: %s",
+ hex, msg));
+ free(hex);
+ }
#endif
- }
+ }
- if (p->status == MOD_NOTPRIME && p->lifetime < now) {
- struct moduli_cache *tmp;
-#ifdef DEBUG
- printf("Unlinking non prime modulus.\n");
-#endif
- tmp = p;
- p = p->next;
- mod_value_reset(tmp);
- mod_unlink(tmp);
- }
- p = p->next;
- }
+ if (p->status == MOD_NOTPRIME && p->lifetime < now) {
+ LOG_DBG((LOG_CRYPTO, 40, __FUNCTION__
+ ": unlinking non prime modulus"));
+ mod_value_reset(tmp);
+ mod_unlink(tmp);
+ }
+ }
- BN_CTX_free(ctx);
+ BN_CTX_free(ctx);
}
struct moduli_cache *
@@ -190,7 +169,7 @@ mod_new_modulus(BIGNUM *m)
tmp = mod_new_modgen(m, generator);
BN_clear_free(generator);
- return tmp;
+ return (tmp);
}
int
@@ -203,7 +182,7 @@ mod_value_reset(struct moduli_cache *ob)
if (ob->exchangevalue != NULL)
free(ob->exchangevalue);
- return 1;
+ return (1);
}
/* Find a proper modulus and generator in the queue.
@@ -214,27 +193,25 @@ struct moduli_cache *
mod_find_modgen_next(struct moduli_cache *ob, BIGNUM *modulus,
BIGNUM *generator)
{
- struct moduli_cache *tmp = ob;
-
- if (tmp == NULL)
- tmp = modob;
+ if (ob != NULL)
+ ob = TAILQ_NEXT(ob, next);
else
- tmp = tmp->next;
-
- while(tmp!=NULL) {
- if((BN_is_zero(generator) ||
- !BN_cmp(tmp->generator, generator)) &&
- (BN_is_zero(modulus) || !BN_cmp(modulus, tmp->modulus)))
- return tmp;
- tmp = tmp->next;
+ ob = TAILQ_FIRST(&modhead);
+
+ for ( ; ob; ob = TAILQ_NEXT(ob, next)) {
+ if ((BN_is_zero(generator) ||
+ !BN_cmp(ob->generator, generator)) &&
+ (BN_is_zero(modulus) || !BN_cmp(modulus, ob->modulus)))
+ break;
}
- return NULL;
+
+ return (ob);
}
struct moduli_cache *
mod_find_modgen(BIGNUM *modulus, BIGNUM *generator)
{
- return mod_find_modgen_next(NULL, modulus, generator);
+ return (mod_find_modgen_next(NULL, modulus, generator));
}
struct moduli_cache *
@@ -243,11 +220,14 @@ mod_find_generator_next(struct moduli_cache *ob, BIGNUM *generator)
struct moduli_cache *tmp;
BIGNUM *modulus;
- modulus = BN_new(); /* Is set to zero by init */
+ modulus = BN_new();
+ BN_zero(modulus);
+
tmp = mod_find_modgen_next(ob, modulus, generator);
- BN_clear_free(modulus);
+
+ BN_free(modulus);
- return tmp;
+ return (tmp);
}
struct moduli_cache *
@@ -256,11 +236,14 @@ mod_find_generator(BIGNUM *generator)
struct moduli_cache *tmp;
BIGNUM *modulus;
- modulus = BN_new(); /* Is set to zero by init */
+ modulus = BN_new();
+ BN_zero(modulus);
+
tmp = mod_find_modgen(modulus,generator);
- BN_clear_free(modulus);
+
+ BN_free(modulus);
- return tmp;
+ return (tmp);
}
struct moduli_cache *
@@ -269,11 +252,14 @@ mod_find_modulus_next(struct moduli_cache *ob, BIGNUM *modulus)
struct moduli_cache *tmp;
BIGNUM *generator;
- generator = BN_new(); /* Is set to zero by init */
+ generator = BN_new();
+ BN_zero(generator);
+
tmp = mod_find_modgen_next(ob, modulus, generator);
- BN_clear_free(generator);
+
+ BN_free(generator);
- return tmp;
+ return (tmp);
}
struct moduli_cache *
@@ -282,25 +268,26 @@ mod_find_modulus(BIGNUM *modulus)
struct moduli_cache *tmp;
BIGNUM *generator;
- generator = BN_new(); /* Is set to zero by init */
+ generator = BN_new();
+ BN_zero(generator);
+
tmp = mod_find_modgen(modulus,generator);
- BN_clear_free(generator);
- return tmp;
+ BN_free(generator);
+
+ return (tmp);
}
void
-mod_cleanup()
+mod_cleanup(void)
{
struct moduli_cache *p;
- struct moduli_cache *tmp = modob;
- while(tmp!=NULL) {
- p = tmp;
- mod_value_reset(tmp);
- tmp = tmp->next;
- free(p);
+
+ while ((p = TAILQ_FIRST(&modhead))) {
+ TAILQ_REMOVE(&modhead, p, next);
+ mod_value_reset(p);
+ free(p);
}
- modob = NULL;
}