Eliminate clone-and-hack of DES key expansion and parity setting.
[u/mdw/catacomb] / dh-param.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
f94b972d 3 * $Id$
34e4f738 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
34e4f738 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
49static void getinfo(dh_param *dp, pdata *pd)
50 { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; }
51
52int 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;
67found:
68 return (0);
69
70fail:
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
f94b972d 81int main(int argc, char *argv[])
34e4f738 82{
83 const pentry *pe;
84 const char *e;
85 int ok = 1;
86 grand *gr;
87
88 gr = fibrand_create(0);
f94b972d 89 fputs("checking standard prime fields...\n", stdout);
34e4f738 90 for (pe = ptab; pe->name; pe++) {
91 dh_param dp;
92 group *g;
93 getinfo(&dp, pe->data);
f94b972d 94 printf(" %s: ", pe->name);
34e4f738 95 g = group_prime(&dp);
f94b972d 96 if (mp_bits(dp.q) > 2048 &&
97 (!argv[1] || strcmp(argv[1], "keen") != 0)) {
98 fputs("skipping\n", stdout);
99 continue;
100 }
101 fflush(stdout);
34e4f738 102 e = G_CHECK(g, gr);
103 G_DESTROYGROUP(g);
104 dh_paramfree(&dp);
105 if (e) {
f94b972d 106 printf("fails: %s\n", e);
34e4f738 107 ok = 0;
f94b972d 108 } else
109 fputs("ok\n", stdout);
34e4f738 110 }
111 gr->ops->destroy(gr);
f94b972d 112 if (ok)
113 fputs("all ok\n", stdout);
34e4f738 114 return (!ok);
115}
116
117#endif
118
119/*----- That's all, folks -------------------------------------------------*/