X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/1ba83484ee5bb486da9aa958576de4bc29ef0c1d..34e4f738bcba58e6d8c4cabbb0b3232a65b42a9d:/dh-param.c diff --git a/dh-param.c b/dh-param.c new file mode 100644 index 0000000..66bee09 --- /dev/null +++ b/dh-param.c @@ -0,0 +1,124 @@ +/* -*-c-*- + * + * $Id: dh-param.c,v 1.1 2004/04/01 12:50:09 mdw Exp $ + * + * Reading Diffie-Hellman parameters + * + * (c) 2004 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * 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" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @dh_parse@ --- * + * + * Arguments: @qd_parse *qd@ = parser context + * @dh_param *dp@ = parameters to fill in + * + * Returns: Zero if OK, nonzero on error. + * + * Use: Parses a prime group string. This is either one of the + * standard group strings, or a %$p$%, %$q$%, %$g$% triple + * separated by commas. + */ + +static void getinfo(dh_param *dp, pdata *pd) + { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; } + +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); + 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; +found: + return (0); + +fail: + mp_drop(p); mp_drop(q); mp_drop(g); + return (-1); +} + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#include "fibrand.h" + +int main(void) +{ + const pentry *pe; + const char *e; + int ok = 1; + grand *gr; + + gr = fibrand_create(0); + fputs("checking standard prime fields: ", stdout); + for (pe = ptab; pe->name; pe++) { + dh_param dp; + group *g; + getinfo(&dp, pe->data); + g = group_prime(&dp); + 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('.'); + fflush(stdout); + } + gr->ops->destroy(gr); + fputs(ok ? " ok\n" : " failed\n", stdout); + return (!ok); +} + +#endif + +/*----- That's all, folks -------------------------------------------------*/