chiark / gitweb /
math/, pub/: Take a more consistent approach to prime-generation failures.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 26 Jun 2016 10:44:33 +0000 (11:44 +0100)
  * Don't have `strongprime_setup' assert just because the requested
    size is too small.

  * Fix `strongprime' itself, so that it leaves its destination in a
    predictable state (specifically, it's unmolested) if it fails.

  * Remove the retry loops from `bbs_gen' and `rsa_gen'.  Now,
    downstream failures are consistently propagated.

math/strongprime.c
pub/bbs-gen.c
pub/rsa-gen.c

index 60a7cc75310b9c926aadb4afc6c3a206a04689bb..a82bfad090b44bc0bc7c660f5fdbb43e13634927 100644 (file)
@@ -82,7 +82,7 @@ mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
 
   /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
 
-  assert(((void)"nbits too small in strongprime_setup", nbits/2 > BITSLOP));
+  if (nbits/2 <= BITSLOP) return (0);
   nb = nbits/2 - BITSLOP;
   c.step = 1;
 
@@ -188,16 +188,20 @@ fail_s:
 mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
                unsigned n, pgen_proc *event, void *ectx)
 {
+  mp *p;
   pfilt f;
   pgen_jumpctx j;
   rabin rb;
 
-  d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
+  if (d) mp_copy(d);
+  p = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
+  if (!p) { mp_drop(d); return (0); }
   j.j = &f;
-  d = pgen(name, d, d, event, ectx, n, pgen_jump, &j,
+  p = pgen(name, p, p, event, ectx, n, pgen_jump, &j,
           rabin_iters(nbits), pgen_test, &rb);
   pfilt_destroy(&f);
-  return (d);
+  mp_drop(d);
+  return (p);
 }
 
 /*----- That's all, folks -------------------------------------------------*/
index e9e6922e316711cfd93b466de9292108a03fef94..f57683f10fb57c96ae1cc88f20cf5fd0fc6d4f9a 100644 (file)
@@ -69,18 +69,13 @@ int bbs_gen(bbs_priv *bp, unsigned nbits, grand *r, unsigned n,
 
   /* --- Generate @p@ --- */
 
-again:
   if ((x = strongprime_setup("p", x, &jp, nb, r, n, event, ectx)) == 0)
     goto fail_x;
   j.j = &jp;
   bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_jump, &j,
               rabin_iters(nb), pgen_test, &rb);
   pfilt_destroy(&jp);
-  if (!bp->p) {
-    if (n)
-      goto fail_p;
-    goto again;
-  }
+  if (!bp->p) goto fail_p;
 
   /* --- Generate @q@ --- */
 
@@ -98,12 +93,7 @@ again:
   pfilt_destroy(&g.jp);
   mp_drop(g.r);
   mp_drop(g.g);
-  if (!bp->q) {
-    if (n)
-      goto fail_q;
-    mp_drop(bp->p);
-    goto again;
-  }
+  if (!bp->q) goto fail_q;
 
   /* --- Compute @n@ --- */
 
index c9a2da604a459e62560bbde0ff824b93a378f293..c12be18ae3a02d5282a01ccecf7adca217163c27 100644 (file)
@@ -73,7 +73,6 @@ int rsa_gen(rsa_priv *rp, unsigned nbits, grand *r, unsigned n,
    * conservative about that sort of thing.
    */
 
-again:
   if ((rp->p = strongprime("p", MP_NEWSEC, nbits/2, r, n, event, ectx)) == 0)
     goto fail_p;
 
@@ -106,10 +105,7 @@ again:
     mp_drop(g.r);
     if (!q) {
       mp_drop(g.g);
-      if (n)
-       goto fail_q;
-      mp_drop(rp->p);
-      goto again;
+      goto fail_q;
     }
     rp->q = q;
   }
@@ -125,10 +121,7 @@ again:
       MP_LEN(phi) * 4 < MP_LEN(rp->q) * 3) {
     mp_drop(rp->p);
     mp_drop(g.g);
-    if (n)
-      goto fail_q;
-    mp_drop(rp->q);
-    goto again;
+    goto fail_q;
   }
 
   if (MP_NEGP(phi)) {