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