| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: dh-param.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Reading Diffie-Hellman parameters |
| 6 | * |
| 7 | * (c) 2004 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb 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 Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #include "dh.h" |
| 33 | #include "ptab.h" |
| 34 | |
| 35 | /*----- Main code ---------------------------------------------------------*/ |
| 36 | |
| 37 | /* --- @dh_parse@ --- * |
| 38 | * |
| 39 | * Arguments: @qd_parse *qd@ = parser context |
| 40 | * @dh_param *dp@ = parameters to fill in |
| 41 | * |
| 42 | * Returns: Zero if OK, nonzero on error. |
| 43 | * |
| 44 | * Use: Parses a prime group string. This is either one of the |
| 45 | * standard group strings, or a %$p$%, %$q$%, %$g$% triple |
| 46 | * separated by commas. |
| 47 | */ |
| 48 | |
| 49 | static void getinfo(dh_param *dp, pdata *pd) |
| 50 | { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; } |
| 51 | |
| 52 | int dh_parse(qd_parse *qd, dh_param *dp) |
| 53 | { |
| 54 | mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW; |
| 55 | const pentry *pe; |
| 56 | |
| 57 | for (pe = ptab; pe->name; pe++) { |
| 58 | if (qd_enum(qd, pe->name) >= 0) { |
| 59 | getinfo(dp, pe->data); |
| 60 | goto found; |
| 61 | } |
| 62 | } |
| 63 | if ((p = qd_getmp(qd)) == 0) goto fail; |
| 64 | qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail; |
| 65 | qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail; |
| 66 | dp->p = p; dp->q = q; dp->g = g; |
| 67 | found: |
| 68 | return (0); |
| 69 | |
| 70 | fail: |
| 71 | mp_drop(p); mp_drop(q); mp_drop(g); |
| 72 | return (-1); |
| 73 | } |
| 74 | |
| 75 | /*----- Test rig ----------------------------------------------------------*/ |
| 76 | |
| 77 | #ifdef TEST_RIG |
| 78 | |
| 79 | #include "fibrand.h" |
| 80 | |
| 81 | int main(void) |
| 82 | { |
| 83 | const pentry *pe; |
| 84 | const char *e; |
| 85 | int ok = 1; |
| 86 | grand *gr; |
| 87 | |
| 88 | gr = fibrand_create(0); |
| 89 | fputs("checking standard prime fields: ", stdout); |
| 90 | for (pe = ptab; pe->name; pe++) { |
| 91 | dh_param dp; |
| 92 | group *g; |
| 93 | getinfo(&dp, pe->data); |
| 94 | g = group_prime(&dp); |
| 95 | e = G_CHECK(g, gr); |
| 96 | G_DESTROYGROUP(g); |
| 97 | dh_paramfree(&dp); |
| 98 | if (e) { |
| 99 | fprintf(stderr, "\n*** group %s fails: %s\n", pe->name, e); |
| 100 | ok = 0; |
| 101 | } |
| 102 | putchar('.'); |
| 103 | fflush(stdout); |
| 104 | } |
| 105 | gr->ops->destroy(gr); |
| 106 | fputs(ok ? " ok\n" : " failed\n", stdout); |
| 107 | return (!ok); |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | /*----- That's all, folks -------------------------------------------------*/ |