Commit | Line | Data |
---|---|---|
e9026a0a | 1 | /* -*-c-*- |
e9026a0a | 2 | * |
3 | * Generalized version of KCDSA | |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
e9026a0a | 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 | * |
e9026a0a | 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 | * |
e9026a0a | 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 | ||
e9026a0a | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
77fdf13a | 30 | #include "dsa.h" |
e9026a0a | 31 | #include "gkcdsa.h" |
32 | #include "group.h" | |
33 | #include "ghash.h" | |
34 | #include "mpbarrett.h" | |
35 | #include "mprand.h" | |
36 | ||
37 | /*----- Main code ---------------------------------------------------------*/ | |
38 | ||
f4535c64 | 39 | /* --- @gkcdsa_beginhash@ --- * |
e9026a0a | 40 | * |
f4535c64 | 41 | * Arguments: @const gkcdsa *c@ = pointer to the context structure |
e9026a0a | 42 | * |
43 | * Returns: A hashing context for you to hash the message. | |
44 | * | |
45 | * Use: Initializes a hash function correctly for you to hash a | |
46 | * message. Requires @h@, @g@ and @p@. | |
47 | */ | |
48 | ||
49 | ghash *gkcdsa_beginhash(const gkcdsa *c) | |
50 | { | |
51 | ghash *h = GH_INIT(c->h); | |
52 | mp *v = G_TOINT(c->g, MP_NEW, c->p); | |
53 | size_t sz = c->h->bufsz; | |
54 | void *p = xmalloc(sz); | |
e9026a0a | 55 | if (/*ouch*/ !v) memset(p, 0, sz); |
56 | else mp_storeb(v, p, sz); | |
57 | GH_HASH(h, p, sz); | |
58 | mp_drop(v); xfree(p); | |
59 | return (h); | |
60 | } | |
61 | ||
62 | /* --- @gkcdsa_endhash@ --- * | |
63 | * | |
64 | * Arguments: @const gkcdsa *c@ = pointer to the context structure | |
65 | * @ghash *h@ = the hashing context | |
66 | * | |
67 | * Returns: --- | |
68 | * | |
69 | * Use: Does any final thing that KCDSA wants to do when hashing a | |
45c0fd36 | 70 | * message. (Actually, there's nothing.) The hashing context |
e9026a0a | 71 | * isn't finalized. |
72 | */ | |
73 | ||
f4535c64 | 74 | void gkcdsa_endhash(const gkcdsa *c, ghash *h) { ; } |
e9026a0a | 75 | |
76 | /* --- @hashge@ --- * | |
77 | * | |
78 | * Arguments: @group *g@ = abstract group | |
79 | * @const gchash *hc@ = hash class | |
80 | * @ge *w@ = a group element | |
81 | * | |
82 | * Returns: A hash context, with the hash of @w@ in it. | |
83 | */ | |
84 | ||
85 | static ghash *hashge(group *g, const gchash *hc, ge *w) | |
86 | { | |
87 | ghash *h; | |
88 | size_t sz; | |
89 | void *p; | |
90 | buf b; | |
91 | int rc; | |
92 | ||
93 | sz = hc->hashsz; | |
94 | if (sz < g->noctets) | |
95 | sz = g->noctets; | |
96 | p = xmalloc(sz); | |
97 | buf_init(&b, p, sz); | |
98 | rc = G_TORAW(g, &b, w); | |
99 | assert(rc == 0); | |
100 | h = GH_INIT(hc); | |
101 | GH_HASH(h, BBASE(&b), BLEN(&b)); | |
102 | xfree(p); | |
103 | return (h); | |
104 | } | |
105 | ||
106 | /* --- @gkcdsa_sign@ --- * | |
107 | * | |
108 | * Arguments: @const gkcdsa *c@ = my context structure | |
109 | * @gkcdsa_sig *s@ = where to put the signature (initialized) | |
110 | * @const void *m@ = pointer to message hash | |
111 | * @mp *k@ = random exponent for this message or null | |
112 | * | |
113 | * Returns: --- | |
114 | * | |
115 | * Use: Signs a message. Requires @g@, @u@, @h@, and @r@ if @k@ is | |
116 | * null. This is a better idea than inventing @k@ yourself. | |
117 | */ | |
118 | ||
119 | void gkcdsa_sign(const gkcdsa *c, gkcdsa_sig *s, const void *m, mp *k) | |
120 | { | |
121 | group *g = c->g; | |
122 | mp *x, *y; | |
123 | ge *z = G_CREATE(g); | |
124 | size_t hsz = c->h->hashsz; | |
125 | ghash *h; | |
126 | ||
127 | if (k) { MP_COPY(k); goto have_k; } | |
128 | new_k: | |
77fdf13a | 129 | k = dsa_nonce(k, g->r, c->u, m, c->h, c->r); |
e9026a0a | 130 | have_k: |
a69a3efd | 131 | if (MP_ZEROP(k)) goto new_k; |
e9026a0a | 132 | G_EXP(g, z, g->g, k); |
133 | if (!s->r) s->r = xmalloc(hsz); | |
134 | h = hashge(g, c->h, z); GH_DONE(h, s->r); | |
135 | ||
136 | x = mp_loadb(s->s, m, hsz); | |
137 | y = mp_loadb(MP_NEW, s->r, hsz); | |
138 | x = mp_xor(x, x, y); | |
139 | mp_div(0, &x, x, g->r); | |
140 | x = mp_sub(x, g->r, x); | |
141 | x = mp_add(x, x, k); | |
142 | if (MP_CMP(x, >=, g->r)) x = mp_sub(x, x, g->r); | |
143 | x = mp_mul(x, x, c->u); mp_div(0, &x, x, g->r); | |
144 | s->s = x; | |
145 | mp_drop(k); mp_drop(y); GH_DESTROY(h); G_DESTROY(g, z); | |
146 | } | |
147 | ||
148 | /* --- @gkcdsa_verify@ --- * | |
149 | * | |
150 | * Arguments: @const gkcdsa *c@ = my context structure | |
151 | * @const gkcdsa_sig *s@ = the signature to verify | |
152 | * @const void *m@ = pointer to message hash | |
153 | * | |
154 | * Returns: Zero if OK, negative on failure. | |
155 | * | |
156 | * Use: Checks a signature on a message, Requires @g@, @p@, @h@. | |
157 | */ | |
158 | ||
159 | int gkcdsa_verify(const gkcdsa *c, const gkcdsa_sig *s, const void *m) | |
160 | { | |
161 | group *g = c->g; | |
162 | size_t hsz = c->h->hashsz; | |
163 | group_expfactor e[2]; | |
164 | mp *x, *y; | |
165 | ghash *h; | |
166 | ge *z; | |
167 | octet *p; | |
168 | int rc = -1; | |
169 | ||
170 | if (MP_CMP(s->s, <, MP_ONE) || MP_CMP(s->s, >=, g->r)) | |
171 | return (-1); | |
172 | x = mp_loadb(MP_NEW, m, hsz); y = mp_loadb(MP_NEW, s->r, hsz); | |
173 | x = mp_xor(x, x, y); mp_div(0, &x, x, g->r); | |
174 | e[0].base = c->p; e[0].exp = s->s; | |
175 | e[1].base = g->g; e[1].exp = x; | |
176 | z = G_CREATE(g); G_MEXP(g, z, e, 2); | |
177 | h = hashge(g, c->h, z); p = GH_DONE(h, 0); | |
178 | if (memcmp(p, s->r, hsz) == 0) rc = 0; | |
179 | mp_drop(x); mp_drop(y); G_DESTROY(g, z); GH_DESTROY(h); | |
180 | return (rc); | |
181 | } | |
182 | ||
183 | /*----- Test rig ----------------------------------------------------------*/ | |
184 | ||
185 | #ifdef TEST_RIG | |
186 | ||
77fdf13a MW |
187 | #include "rand.h" |
188 | ||
e9026a0a | 189 | static group *getgroup(const char *p) { |
190 | group *g; qd_parse qd; | |
191 | qd.p = p; qd.e = 0; g = group_parse(&qd); | |
192 | if (g && !qd_eofp(&qd)) { G_DESTROYGROUP(g); g = 0; qd.e = "junk at eof"; } | |
bb77b1d1 MW |
193 | if (!g) { |
194 | fprintf(stderr, "bad group string `%.*s|%s': %s\n", | |
195 | (int)(qd.p - p), p, qd.p, qd.e); | |
196 | exit(1); | |
197 | } | |
e9026a0a | 198 | return (g); |
199 | } | |
200 | ||
201 | static ge *getge(group *g, const char *p) { | |
202 | ge *x = G_CREATE(g); | |
203 | if (group_readstring(g, x, p, 0)) { | |
204 | fprintf(stderr, "bad group element `%s'\n", p); | |
205 | exit(1); | |
206 | } | |
207 | return (x); | |
208 | } | |
209 | ||
210 | static void showge(group *g, const char *p, ge *x) { | |
211 | fprintf(stderr, "*** %s = ", p); group_writefile(g, x, stderr); | |
212 | putc('\n', stderr); | |
213 | } | |
214 | ||
215 | static void showmp(const char *p, mp *x, int r) { | |
45c0fd36 | 216 | fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r); |
e9026a0a | 217 | putc('\n', stderr); |
218 | } | |
219 | ||
220 | static int tsign(dstr *v) | |
221 | { | |
222 | gdsa c; | |
223 | gkcdsa_sig s, ss = GKCDSA_SIG_INIT; | |
224 | ghash *h; | |
77fdf13a | 225 | octet *m; |
e9026a0a | 226 | mp *k; |
227 | dstr d = DSTR_INIT; | |
228 | mp *x; | |
229 | int ok = 1; | |
230 | ||
231 | c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf); | |
232 | c.u = *(mp **)v[2].buf; k = *(mp **)v[4].buf; | |
233 | s.r = (octet *)v[5].buf; s.s = *(mp **)v[6].buf; | |
234 | DENSURE(&d, c.h->hashsz); d.len = c.h->hashsz; memset(d.buf, 0, d.len); | |
235 | ss.r = (octet *)d.buf; | |
236 | ||
b817bfc6 | 237 | x = mp_modinv(MP_NEW, c.u, c.g->r); |
e9026a0a | 238 | c.p = G_CREATE(c.g); G_EXP(c.g, c.p, c.g->g, x); |
239 | h = gkcdsa_beginhash(&c); | |
240 | GH_HASH(h, v[3].buf, v[3].len); | |
241 | gkcdsa_endhash(&c, h); | |
242 | gkcdsa_sign(&c, &ss, GH_DONE(h, 0), k); | |
77fdf13a | 243 | GH_DESTROY(h); |
e9026a0a | 244 | if (memcmp(s.r, ss.r, c.h->hashsz) || !MP_EQ(s.s, ss.s)) { |
245 | ok = 0; | |
246 | fprintf(stderr, "*** sign failed!\n"); | |
247 | fprintf(stderr, "*** group: %s\n", v[0].buf); | |
248 | fprintf(stderr, "*** hash: %s\n", c.h->name); | |
249 | showmp("private key", c.u, 16); | |
250 | showge(c.g, "public key", c.p); | |
251 | fprintf(stderr, "*** message: `%s'\n", v[3].buf); | |
252 | fprintf(stderr, "*** computed r = "); | |
253 | type_hex.dump(&d, stderr); putc('\n', stderr); | |
254 | showmp("computed s", ss.s, 16); | |
c4e7958a | 255 | fprintf(stderr, "*** expected r = "); |
e9026a0a | 256 | type_hex.dump(&v[5], stderr); putc('\n', stderr); |
257 | showmp("expected s", s.s, 16); | |
258 | } | |
77fdf13a MW |
259 | |
260 | c.r = &rand_global; | |
261 | h = gkcdsa_beginhash(&c); | |
262 | GH_HASH(h, v[3].buf, v[3].len); | |
263 | m = GH_DONE(h, 0); | |
264 | GH_DESTROY(h); | |
265 | gkcdsa_sign(&c, &ss, m, 0); | |
266 | if (gkcdsa_verify(&c, &ss, m)) { | |
267 | ok = 0; | |
268 | fprintf(stderr, "*** sign cross-check failed!\n"); | |
269 | fprintf(stderr, "*** group: %s\n", v[0].buf); | |
270 | fprintf(stderr, "*** hash: %s\n", c.h->name); | |
271 | showmp("private key", c.u, 16); | |
272 | showge(c.g, "public key", c.p); | |
273 | fprintf(stderr, "*** message: `%s'\n", v[3].buf); | |
274 | fprintf(stderr, "*** computed r = "); | |
275 | type_hex.dump(&d, stderr); putc('\n', stderr); | |
276 | showmp("computed s", ss.s, 16); | |
277 | } | |
278 | ||
279 | mp_drop(s.s); mp_drop(x); mp_drop(k); dstr_destroy(&d); mp_drop(ss.s); | |
280 | mp_drop(c.u); G_DESTROY(c.g, c.p); G_DESTROYGROUP(c.g); | |
e9026a0a | 281 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
282 | return (ok); | |
283 | } | |
284 | ||
285 | static int tverify(dstr *v) | |
286 | { | |
287 | gkcdsa c; | |
288 | gkcdsa_sig s; | |
289 | ghash *h; | |
290 | int rc, erc; | |
291 | int ok = 1; | |
292 | ||
293 | c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf); | |
294 | c.p = getge(c.g, v[2].buf); | |
295 | s.r = (octet *)v[4].buf; s.s = *(mp **)v[5].buf; | |
296 | erc = *(int *)v[6].buf; | |
297 | ||
298 | h = gkcdsa_beginhash(&c); | |
299 | GH_HASH(h, v[3].buf, v[3].len); | |
300 | gkcdsa_endhash(&c, h); | |
301 | rc = gkcdsa_verify(&c, &s, GH_DONE(h, 0)); | |
302 | if (!rc != !erc) { | |
303 | ok = 0; | |
304 | fprintf(stderr, "*** verify failed!\n"); | |
305 | fprintf(stderr, "*** group: %s\n", v[0].buf); | |
306 | fprintf(stderr, "*** hash: %s\n", c.h->name); | |
307 | showge(c.g, "public key", c.p); | |
308 | fprintf(stderr, "*** message: `%s'\n", v[3].buf); | |
309 | fprintf(stderr, "*** sig r = "); | |
310 | type_hex.dump(&v[4], stderr); putc('\n', stderr); | |
311 | showmp("sig s", s.s, 16); | |
312 | fprintf(stderr, "*** expected %s\n", !erc ? "pass" : "fail"); | |
313 | } | |
314 | mp_drop(s.s); G_DESTROY(c.g, c.p); G_DESTROYGROUP(c.g); | |
315 | GH_DESTROY(h); | |
316 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
317 | return (ok); | |
318 | } | |
319 | ||
320 | static const test_chunk tests[] = { | |
321 | { "sign", tsign, { &type_string, &type_string, &type_mp, &type_string, | |
322 | &type_mp, &type_hex, &type_mp } }, | |
323 | { "verify", tverify, { &type_string, &type_string, &type_string, | |
324 | &type_string, &type_hex, &type_mp, &type_int } }, | |
325 | { 0 } | |
326 | }; | |
327 | ||
328 | int main(int argc, char *argv[]) | |
329 | { | |
330 | sub_init(); | |
0f00dc4c | 331 | test_run(argc, argv, tests, SRCDIR "/t/gkcdsa"); |
e9026a0a | 332 | return (0); |
333 | } | |
334 | ||
335 | #endif | |
336 | ||
337 | /*----- That's all, folks -------------------------------------------------*/ |