General robustification.
[u/mdw/catacomb] / group-parse.c
1 /* -*-c-*-
2 *
3 * $Id: group-parse.c,v 1.2 2004/04/03 03:32:05 mdw Exp $
4 *
5 * Parse group description strings
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: group-parse.c,v $
32 * Revision 1.2 2004/04/03 03:32:05 mdw
33 * General robustification.
34 *
35 * Revision 1.1 2004/04/01 12:50:09 mdw
36 * Add cyclic group abstraction, with test code. Separate off exponentation
37 * functions for better static linking. Fix a buttload of bugs on the way.
38 * Generally ensure that negative exponents do inversion correctly. Add
39 * table of standard prime-field subgroups. (Binary field subgroups are
40 * currently unimplemented but easy to add if anyone ever finds a good one.)
41 *
42 */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include "group.h"
47 #include "dh.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @group_parse@ --- *
52 *
53 * Arguments: @qd_parse *qd@ = quick-and-dirty parser
54 *
55 * Returns: Group pointer, or null for failure.
56 *
57 * Use: Parses a group description and returns the group. This has
58 * the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and
59 * SPEC is the appropriate kind of group specification of the
60 * given type.
61 */
62
63 group *group_parse(qd_parse *qd)
64 {
65 group *g = 0;
66
67 switch (qd_enum(qd, "prime,ec")) {
68 case 0: {
69 dh_param dp;
70 qd_delim(qd, '{');
71 if (dh_parse(qd, &dp)) break;
72 qd_delim(qd, '}');
73 g = group_prime(&dp);
74 dh_paramfree(&dp);
75 } break;
76 case 1: {
77 ec_info ei;
78 qd_delim(qd, '{');
79 if (ec_infoparse(qd, &ei)) break;
80 qd_delim(qd, '}');
81 g = group_ec(&ei);
82 } break;
83 }
84 if (!g) qd->e = "bad group parameters";
85 return (g);
86 }
87
88 /* --- @group_fromstring@ --- *
89 *
90 * Arguments: @const char *p@ = pointer to string to read
91 * @group **gg@ = where to put the group pointer
92 *
93 * Returns: Null if OK, or an error string.
94 *
95 * Use: Parses a group spec from a string, and returns the group.
96 */
97
98 const char *group_fromstring(const char *p, group **gg)
99 {
100 group *g;
101 qd_parse qd;
102
103 qd.p = p;
104 qd.e = 0;
105 if ((g = group_parse(&qd)) == 0) return (qd.e);
106 if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); }
107 *gg = g;
108 return (0);
109 }
110
111 /*----- That's all, folks -------------------------------------------------*/