Support subgroups of binary fields.
[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"
3688eb75 34#include "bintab.h"
34e4f738 35
36/*----- Main code ---------------------------------------------------------*/
37
3688eb75 38/* --- @dh_parse@, @dhbin_parse@ --- *
34e4f738 39 *
40 * Arguments: @qd_parse *qd@ = parser context
41 * @dh_param *dp@ = parameters to fill in
42 *
43 * Returns: Zero if OK, nonzero on error.
44 *
45 * Use: Parses a prime group string. This is either one of the
46 * standard group strings, or a %$p$%, %$q$%, %$g$% triple
47 * separated by commas.
48 */
49
50static void getinfo(dh_param *dp, pdata *pd)
51 { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; }
52
3688eb75 53static int parse(qd_parse *qd, gprime_param *dp)
34e4f738 54{
55 mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW;
3688eb75 56
57 if ((p = qd_getmp(qd)) == 0) goto fail;
58 qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail;
59 qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail;
60 dp->p = p; dp->q = q; dp->g = g;
61 return (0);
62fail:
63 mp_drop(p); mp_drop(q); mp_drop(g);
64 return (-1);
65}
66
67int dh_parse(qd_parse *qd, dh_param *dp)
68{
34e4f738 69 const pentry *pe;
70
71 for (pe = ptab; pe->name; pe++) {
72 if (qd_enum(qd, pe->name) >= 0) {
73 getinfo(dp, pe->data);
74 goto found;
75 }
76 }
3688eb75 77 if (parse(qd, dp))
78 return (-1);
34e4f738 79found:
80 return (0);
3688eb75 81}
34e4f738 82
3688eb75 83int dhbin_parse(qd_parse *qd, gbin_param *gb)
84{
85 const binentry *be;
86
87 for (be = bintab; be->name; be++) {
88 if (qd_enum(qd, be->name) >= 0) {
89 getinfo(gb, be->data);
90 goto found;
91 }
92 }
93 if (parse(qd, gb))
94 return (-1);
95found:
96 return (0);
34e4f738 97}
98
99/*----- Test rig ----------------------------------------------------------*/
100
101#ifdef TEST_RIG
102
103#include "fibrand.h"
104
f94b972d 105int main(int argc, char *argv[])
34e4f738 106{
107 const pentry *pe;
3688eb75 108 const binentry *be;
34e4f738 109 const char *e;
110 int ok = 1;
111 grand *gr;
112
113 gr = fibrand_create(0);
3688eb75 114 fputs("checking standard prime groups...\n", stdout);
34e4f738 115 for (pe = ptab; pe->name; pe++) {
116 dh_param dp;
117 group *g;
118 getinfo(&dp, pe->data);
f94b972d 119 printf(" %s: ", pe->name);
34e4f738 120 g = group_prime(&dp);
f94b972d 121 if (mp_bits(dp.q) > 2048 &&
122 (!argv[1] || strcmp(argv[1], "keen") != 0)) {
123 fputs("skipping\n", stdout);
124 continue;
125 }
126 fflush(stdout);
34e4f738 127 e = G_CHECK(g, gr);
128 G_DESTROYGROUP(g);
129 dh_paramfree(&dp);
130 if (e) {
f94b972d 131 printf("fails: %s\n", e);
34e4f738 132 ok = 0;
f94b972d 133 } else
134 fputs("ok\n", stdout);
34e4f738 135 }
3688eb75 136 fputs("checking standard binary groups...\n", stdout);
137 for (be = bintab; be->name; be++) {
138 gbin_param gb;
139 group *g;
140 getinfo(&gb, be->data);
141 printf(" %s: ", be->name);
142 g = group_binary(&gb);
143 fflush(stdout);
144 e = G_CHECK(g, gr);
145 G_DESTROYGROUP(g);
146 dh_paramfree(&gb);
147 if (e) {
148 printf("fails: %s\n", e);
149 ok = 0;
150 } else
151 fputs("ok\n", stdout);
152 }
34e4f738 153 gr->ops->destroy(gr);
f94b972d 154 if (ok)
155 fputs("all ok\n", stdout);
34e4f738 156 return (!ok);
157}
158
159#endif
160
161/*----- That's all, folks -------------------------------------------------*/