34e4f738 |
1 | /* -*-c-*- |
34e4f738 |
2 | * |
3 | * Reading Diffie-Hellman parameters |
4 | * |
5 | * (c) 2004 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
34e4f738 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
34e4f738 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
34e4f738 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
34e4f738 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include "dh.h" |
31 | #include "ptab.h" |
3688eb75 |
32 | #include "bintab.h" |
34e4f738 |
33 | |
34 | /*----- Main code ---------------------------------------------------------*/ |
35 | |
7b6d64f1 |
36 | /* ---- @dh_infofromdata@ --- * |
37 | * |
38 | * Arguments: @dh_param *dp@ = parameters to fill in |
39 | * @pdata *pd@ = packed data structure |
40 | * |
41 | * Returns: --- |
42 | * |
43 | * Use: Fills in a parameters structure from a packed data block. |
44 | */ |
45 | |
46 | void dh_infofromdata(dh_param *dp, pdata *pd) |
47 | { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; } |
48 | |
3688eb75 |
49 | /* --- @dh_parse@, @dhbin_parse@ --- * |
34e4f738 |
50 | * |
51 | * Arguments: @qd_parse *qd@ = parser context |
52 | * @dh_param *dp@ = parameters to fill in |
53 | * |
54 | * Returns: Zero if OK, nonzero on error. |
55 | * |
56 | * Use: Parses a prime group string. This is either one of the |
57 | * standard group strings, or a %$p$%, %$q$%, %$g$% triple |
58 | * separated by commas. |
59 | */ |
60 | |
3688eb75 |
61 | static int parse(qd_parse *qd, gprime_param *dp) |
34e4f738 |
62 | { |
63 | mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW; |
3688eb75 |
64 | |
65 | if ((p = qd_getmp(qd)) == 0) goto fail; |
66 | qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail; |
67 | qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail; |
68 | dp->p = p; dp->q = q; dp->g = g; |
69 | return (0); |
70 | fail: |
71 | mp_drop(p); mp_drop(q); mp_drop(g); |
72 | return (-1); |
73 | } |
74 | |
75 | int dh_parse(qd_parse *qd, dh_param *dp) |
76 | { |
34e4f738 |
77 | const pentry *pe; |
78 | |
79 | for (pe = ptab; pe->name; pe++) { |
80 | if (qd_enum(qd, pe->name) >= 0) { |
7b6d64f1 |
81 | dh_infofromdata(dp, pe->data); |
34e4f738 |
82 | goto found; |
83 | } |
84 | } |
3688eb75 |
85 | if (parse(qd, dp)) |
86 | return (-1); |
34e4f738 |
87 | found: |
88 | return (0); |
3688eb75 |
89 | } |
34e4f738 |
90 | |
3688eb75 |
91 | int dhbin_parse(qd_parse *qd, gbin_param *gb) |
92 | { |
93 | const binentry *be; |
94 | |
95 | for (be = bintab; be->name; be++) { |
96 | if (qd_enum(qd, be->name) >= 0) { |
7b6d64f1 |
97 | dh_infofromdata(gb, be->data); |
3688eb75 |
98 | goto found; |
99 | } |
100 | } |
101 | if (parse(qd, gb)) |
102 | return (-1); |
103 | found: |
104 | return (0); |
34e4f738 |
105 | } |
106 | |
107 | /*----- Test rig ----------------------------------------------------------*/ |
108 | |
109 | #ifdef TEST_RIG |
110 | |
111 | #include "fibrand.h" |
112 | |
f94b972d |
113 | int main(int argc, char *argv[]) |
34e4f738 |
114 | { |
115 | const pentry *pe; |
3688eb75 |
116 | const binentry *be; |
34e4f738 |
117 | const char *e; |
7b6d64f1 |
118 | int ok = 1, aok = 1; |
34e4f738 |
119 | grand *gr; |
120 | |
121 | gr = fibrand_create(0); |
7b6d64f1 |
122 | fputs("checking standard prime groups:", stdout); |
123 | fflush(stdout); |
34e4f738 |
124 | for (pe = ptab; pe->name; pe++) { |
125 | dh_param dp; |
126 | group *g; |
7b6d64f1 |
127 | dh_infofromdata(&dp, pe->data); |
34e4f738 |
128 | g = group_prime(&dp); |
47b41095 |
129 | if (mp_bits(dp.p) > 2048 && |
f94b972d |
130 | (!argv[1] || strcmp(argv[1], "keen") != 0)) { |
7b6d64f1 |
131 | printf(" [%s skipped]", pe->name); |
132 | fflush(stdout); |
f94b972d |
133 | continue; |
134 | } |
34e4f738 |
135 | e = G_CHECK(g, gr); |
136 | G_DESTROYGROUP(g); |
137 | dh_paramfree(&dp); |
138 | if (e) { |
7b6d64f1 |
139 | printf(" [%s failed: %s]", pe->name, e); |
140 | ok = aok = 0; |
f94b972d |
141 | } else |
7b6d64f1 |
142 | printf(" %s", pe->name); |
143 | fflush(stdout); |
34e4f738 |
144 | } |
7b6d64f1 |
145 | fputs(ok ? " ok\n" : " failed\n", stdout); |
146 | ok = 1; |
147 | fputs("checking standard binary groups:", stdout); |
3688eb75 |
148 | for (be = bintab; be->name; be++) { |
149 | gbin_param gb; |
150 | group *g; |
7b6d64f1 |
151 | dh_infofromdata(&gb, be->data); |
3688eb75 |
152 | g = group_binary(&gb); |
3688eb75 |
153 | e = G_CHECK(g, gr); |
154 | G_DESTROYGROUP(g); |
155 | dh_paramfree(&gb); |
156 | if (e) { |
7b6d64f1 |
157 | printf(" [%s failed: %s]", be->name, e); |
158 | ok = aok = 0; |
3688eb75 |
159 | } else |
7b6d64f1 |
160 | printf(" %s", be->name); |
161 | fflush(stdout); |
3688eb75 |
162 | } |
7b6d64f1 |
163 | fputs(ok ? " ok\n" : " failed\n", stdout); |
34e4f738 |
164 | gr->ops->destroy(gr); |
7b6d64f1 |
165 | return (!aok); |
34e4f738 |
166 | } |
167 | |
168 | #endif |
169 | |
170 | /*----- That's all, folks -------------------------------------------------*/ |