Uprating of the passphrase pixie.
[u/mdw/catacomb] / pgen-safe.c
index a8c5ac7..115f7b4 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pgen-safe.c,v 1.2 2000/02/12 18:21:03 mdw Exp $
+ * $Id: pgen-safe.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
  *
  * Safe prime generation
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: pgen-safe.c,v $
- * Revision 1.2  2000/02/12 18:21:03  mdw
- * Overhaul of key management (again).
- *
- * Revision 1.1  1999/12/22 16:01:34  mdw
- * Find `safe' primes (i.e., %$p = 2q + 1$%).
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "mp.h"
@@ -119,17 +108,27 @@ int pgen_safejump(int rq, pgen_event *ev, void *p)
      *
      * The jump in @j.q@ is congruent to 2 (mod 4); see @strongprime_setup@.
      * If @p@ is initially 1 (mod 4) then add @j.q@.  Then double @j.q@ to
-     * ensure that the step is 0 (mod 4).
+     * ensure that the step is 0 (mod 4).  Ensure that @jq@ and @q@ don't
+     * have any common factors.
      */
 
     case PGEN_BEGIN: {
       mp *p = ev->m;
       mp *q;
+      mp *g = MP_NEW;
       if ((p->v[0] & 3) != 3)
        p = mp_add(p, p, j->jq.m);
+      q = mp_lsr(MP_NEW, p, 1);
+      mp_gcd(&g, 0, 0, p, j->jq.m);
+      if (MP_CMP(g, >, MP_ONE)) {
+       ev->m = p;
+       mp_drop(q);
+       mp_drop(g);
+       return (PGEN_ABORT);
+      }
+      mp_drop(g);
       rc = pfilt_create(&j->p, p);
       pfilt_muladd(&j->jp, &j->jq, 2, 0);
-      q = mp_lsr(MP_NEW, p, 1);
       qrc = pfilt_create(&j->q, q);
       mp_drop(p);
       mp_drop(q);