cc-hash.c (fhash): The FILE name may be null.
[u/mdw/catacomb] / dh-param.c
index 66bee09..e1e5374 100644 (file)
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: dh-param.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ * $Id$
  *
  * Reading Diffie-Hellman parameters
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: dh-param.c,v $
- * Revision 1.1  2004/04/01 12:50:09  mdw
- * Add cyclic group abstraction, with test code.  Separate off exponentation
- * functions for better static linking.  Fix a buttload of bugs on the way.
- * Generally ensure that negative exponents do inversion correctly.  Add
- * table of standard prime-field subgroups.  (Binary field subgroups are
- * currently unimplemented but easy to add if anyone ever finds a good one.)
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "dh.h"
 #include "ptab.h"
+#include "bintab.h"
 
 /*----- Main code ---------------------------------------------------------*/
 
-/* --- @dh_parse@ --- *
+/* ---- @dh_infofromdata@ --- *
+ *
+ * Arguments:  @dh_param *dp@ = parameters to fill in
+ *             @pdata *pd@ = packed data structure
+ *
+ * Returns:    ---
+ *
+ * Use:                Fills in a parameters structure from a packed data block.
+ */
+
+void dh_infofromdata(dh_param *dp, pdata *pd)
+  { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; }
+
+/* --- @dh_parse@, @dhbin_parse@ --- *
  *
  * Arguments:  @qd_parse *qd@ = parser context
  *             @dh_param *dp@ = parameters to fill in
  *             separated by commas.
  */
 
-static void getinfo(dh_param *dp, pdata *pd)
-  { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; }
+static int parse(qd_parse *qd, gprime_param *dp)
+{
+  mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW;
+
+  if ((p = qd_getmp(qd)) == 0) goto fail;
+  qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail;
+  qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail;
+  dp->p = p; dp->q = q; dp->g = g;
+  return (0);
+fail:
+  mp_drop(p); mp_drop(q); mp_drop(g);
+  return (-1);
+}
 
 int dh_parse(qd_parse *qd, dh_param *dp)
 {
-  mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW;
   const pentry *pe;
 
   for (pe = ptab; pe->name; pe++) {
     if (qd_enum(qd, pe->name) >= 0) {
-      getinfo(dp, pe->data);
+      dh_infofromdata(dp, pe->data);
       goto found;
     }
   }
-  if ((p = qd_getmp(qd)) == 0) goto fail;
-  qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail;
-  qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail;
-  dp->p = p; dp->q = q; dp->g = g;
+  if (parse(qd, dp))
+    return (-1);
 found:
   return (0);
+}
 
-fail:
-  mp_drop(p); mp_drop(q); mp_drop(g);
-  return (-1);
+int dhbin_parse(qd_parse *qd, gbin_param *gb)
+{
+  const binentry *be;
+
+  for (be = bintab; be->name; be++) {
+    if (qd_enum(qd, be->name) >= 0) {
+      dh_infofromdata(gb, be->data);
+      goto found;
+    }
+  }
+  if (parse(qd, gb))
+    return (-1);
+found:
+  return (0);
 }
 
 /*----- Test rig ----------------------------------------------------------*/
@@ -90,33 +112,59 @@ fail:
 
 #include "fibrand.h"
 
-int main(void)
+int main(int argc, char *argv[])
 {
   const pentry *pe;
+  const binentry *be;
   const char *e;
-  int ok = 1;
+  int ok = 1, aok = 1;
   grand *gr;
 
   gr = fibrand_create(0);
-  fputs("checking standard prime fields: ", stdout);
+  fputs("checking standard prime groups:", stdout);
+  fflush(stdout);
   for (pe = ptab; pe->name; pe++) {
     dh_param dp;
     group *g;
-    getinfo(&dp, pe->data);
+    dh_infofromdata(&dp, pe->data);
     g = group_prime(&dp);
+    if (mp_bits(dp.p) > 2048 &&
+       (!argv[1] || strcmp(argv[1], "keen") != 0)) {
+      printf(" [%s skipped]", pe->name);
+      fflush(stdout);
+      continue;
+    }
     e = G_CHECK(g, gr);
     G_DESTROYGROUP(g);
     dh_paramfree(&dp);
     if (e) {
-      fprintf(stderr, "\n*** group %s fails: %s\n", pe->name, e);
-      ok = 0;
-    }
-    putchar('.');
+      printf(" [%s failed: %s]", pe->name, e);
+      ok = aok = 0;
+    } else
+      printf(" %s", pe->name);
     fflush(stdout);
   }
-  gr->ops->destroy(gr);
   fputs(ok ? " ok\n" : " failed\n", stdout);
-  return (!ok);
+  ok = 1;
+  fputs("checking standard binary groups:", stdout);
+  for (be = bintab; be->name; be++) {
+    gbin_param gb;
+    group *g;
+    dh_infofromdata(&gb, be->data);
+    g = group_binary(&gb);
+    e = G_CHECK(g, gr);
+    G_DESTROYGROUP(g);
+    dh_paramfree(&gb);
+    if (e) {
+      printf(" [%s failed: %s]", be->name, e);
+      ok = aok = 0;
+    } else
+      printf(" %s", be->name);
+    fflush(stdout);
+  }
+  fputs(ok ? " ok\n" : " failed\n", stdout);
+  gr->ops->destroy(gr);
+  return (!aok);
 }
 
 #endif