Add cyclic group abstraction, with test code. Separate off exponentation
[u/mdw/catacomb] / group-parse.c
diff --git a/group-parse.c b/group-parse.c
new file mode 100644 (file)
index 0000000..47998c6
--- /dev/null
@@ -0,0 +1,107 @@
+/* -*-c-*-
+ *
+ * $Id: group-parse.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ *
+ * Parse group description strings
+ *
+ * (c) 2004 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Trivial IP Encryption (TrIPE).
+ *
+ * TrIPE is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * TrIPE 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with TrIPE; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: group-parse.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 "group.h"
+#include "dh.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @group_parse@ --- *
+ *
+ * Arguments:  @qd_parse *qd@ = quick-and-dirty parser
+ *
+ * Returns:    Group pointer, or null for failure.
+ *
+ * Use:                Parses a group description and returns the group.  This has
+ *             the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and
+ *             SPEC is the appropriate kind of group specification of the
+ *             given type.
+ */
+
+group *group_parse(qd_parse *qd)
+{
+  group *g = 0;
+
+  switch (qd_enum(qd, "prime,ec")) {
+    case 0: {
+      dh_param dp;
+      qd_delim(qd, '{');
+      if (dh_parse(qd, &dp)) break;
+      qd_delim(qd, '}');
+      g = group_prime(&dp);
+      dh_paramfree(&dp);
+    } break;
+    case 1: {
+      ec_info ei;
+      qd_delim(qd, '{');
+      if (ec_infoparse(qd, &ei)) break;
+      qd_delim(qd, '}');
+      g = group_ec(&ei);
+    } break;
+  }
+  return (g);
+}
+
+/* --- @group_fromstring@ --- *
+ *
+ * Arguments:  @const char *p@ = pointer to string to read
+ *             @group **gg@ = where to put the group pointer
+ *
+ * Returns:    Null if OK, or an error string.
+ *
+ * Use:                Parses a group spec from a string, and returns the group.
+ */
+
+const char *group_fromstring(const char *p, group **gg)
+{
+  group *g;
+  qd_parse qd;
+
+  qd.p = p;
+  qd.e = 0;
+  if ((g = group_parse(&qd)) == 0) return (qd.e);
+  if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); }
+  *gg = g;
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/