| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: group-parse.c,v 1.1 2004/04/01 12:50:09 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.1 2004/04/01 12:50:09 mdw |
| 33 | * Add cyclic group abstraction, with test code. Separate off exponentation |
| 34 | * functions for better static linking. Fix a buttload of bugs on the way. |
| 35 | * Generally ensure that negative exponents do inversion correctly. Add |
| 36 | * table of standard prime-field subgroups. (Binary field subgroups are |
| 37 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /*----- Header files ------------------------------------------------------*/ |
| 42 | |
| 43 | #include "group.h" |
| 44 | #include "dh.h" |
| 45 | |
| 46 | /*----- Main code ---------------------------------------------------------*/ |
| 47 | |
| 48 | /* --- @group_parse@ --- * |
| 49 | * |
| 50 | * Arguments: @qd_parse *qd@ = quick-and-dirty parser |
| 51 | * |
| 52 | * Returns: Group pointer, or null for failure. |
| 53 | * |
| 54 | * Use: Parses a group description and returns the group. This has |
| 55 | * the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and |
| 56 | * SPEC is the appropriate kind of group specification of the |
| 57 | * given type. |
| 58 | */ |
| 59 | |
| 60 | group *group_parse(qd_parse *qd) |
| 61 | { |
| 62 | group *g = 0; |
| 63 | |
| 64 | switch (qd_enum(qd, "prime,ec")) { |
| 65 | case 0: { |
| 66 | dh_param dp; |
| 67 | qd_delim(qd, '{'); |
| 68 | if (dh_parse(qd, &dp)) break; |
| 69 | qd_delim(qd, '}'); |
| 70 | g = group_prime(&dp); |
| 71 | dh_paramfree(&dp); |
| 72 | } break; |
| 73 | case 1: { |
| 74 | ec_info ei; |
| 75 | qd_delim(qd, '{'); |
| 76 | if (ec_infoparse(qd, &ei)) break; |
| 77 | qd_delim(qd, '}'); |
| 78 | g = group_ec(&ei); |
| 79 | } break; |
| 80 | } |
| 81 | return (g); |
| 82 | } |
| 83 | |
| 84 | /* --- @group_fromstring@ --- * |
| 85 | * |
| 86 | * Arguments: @const char *p@ = pointer to string to read |
| 87 | * @group **gg@ = where to put the group pointer |
| 88 | * |
| 89 | * Returns: Null if OK, or an error string. |
| 90 | * |
| 91 | * Use: Parses a group spec from a string, and returns the group. |
| 92 | */ |
| 93 | |
| 94 | const char *group_fromstring(const char *p, group **gg) |
| 95 | { |
| 96 | group *g; |
| 97 | qd_parse qd; |
| 98 | |
| 99 | qd.p = p; |
| 100 | qd.e = 0; |
| 101 | if ((g = group_parse(&qd)) == 0) return (qd.e); |
| 102 | if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); } |
| 103 | *gg = g; |
| 104 | return (0); |
| 105 | } |
| 106 | |
| 107 | /*----- That's all, folks -------------------------------------------------*/ |