3 * Generalized version of KCDSA
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
33 #include "mpbarrett.h"
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @gkcdsa_beginhash@ --- *
40 * Arguments: @const gkcdsa *c@ = pointer to the context structure
42 * Returns: A hashing context for you to hash the message.
44 * Use: Initializes a hash function correctly for you to hash a
45 * message. Requires @h@, @g@ and @p@.
48 ghash *gkcdsa_beginhash(const gkcdsa *c)
50 ghash *h = GH_INIT(c->h);
51 mp *v = G_TOINT(c->g, MP_NEW, c->p);
52 size_t sz = c->h->bufsz;
53 void *p = xmalloc(sz);
54 if (/*ouch*/ !v) memset(p, 0, sz);
55 else mp_storeb(v, p, sz);
61 /* --- @gkcdsa_endhash@ --- *
63 * Arguments: @const gkcdsa *c@ = pointer to the context structure
64 * @ghash *h@ = the hashing context
68 * Use: Does any final thing that KCDSA wants to do when hashing a
69 * message. (Actually, there's nothing.) The hashing context
73 void gkcdsa_endhash(const gkcdsa *c, ghash *h) { ; }
77 * Arguments: @group *g@ = abstract group
78 * @const gchash *hc@ = hash class
79 * @ge *w@ = a group element
81 * Returns: A hash context, with the hash of @w@ in it.
84 static ghash *hashge(group *g, const gchash *hc, ge *w)
97 rc = G_TORAW(g, &b, w);
100 GH_HASH(h, BBASE(&b), BLEN(&b));
105 /* --- @gkcdsa_sign@ --- *
107 * Arguments: @const gkcdsa *c@ = my context structure
108 * @gkcdsa_sig *s@ = where to put the signature (initialized)
109 * @const void *m@ = pointer to message hash
110 * @mp *k@ = random exponent for this message or null
114 * Use: Signs a message. Requires @g@, @u@, @h@, and @r@ if @k@ is
115 * null. This is a better idea than inventing @k@ yourself.
118 void gkcdsa_sign(const gkcdsa *c, gkcdsa_sig *s, const void *m, mp *k)
123 size_t hsz = c->h->hashsz;
126 if (k) { MP_COPY(k); goto have_k; }
128 k = mprand_range(k, g->r, c->r, 0);
130 if (MP_ZEROP(k)) goto new_k;
131 G_EXP(g, z, g->g, k);
132 if (!s->r) s->r = xmalloc(hsz);
133 h = hashge(g, c->h, z); GH_DONE(h, s->r);
135 x = mp_loadb(s->s, m, hsz);
136 y = mp_loadb(MP_NEW, s->r, hsz);
138 mp_div(0, &x, x, g->r);
139 x = mp_sub(x, g->r, x);
141 if (MP_CMP(x, >=, g->r)) x = mp_sub(x, x, g->r);
142 x = mp_mul(x, x, c->u); mp_div(0, &x, x, g->r);
144 mp_drop(k); mp_drop(y); GH_DESTROY(h); G_DESTROY(g, z);
147 /* --- @gkcdsa_verify@ --- *
149 * Arguments: @const gkcdsa *c@ = my context structure
150 * @const gkcdsa_sig *s@ = the signature to verify
151 * @const void *m@ = pointer to message hash
153 * Returns: Zero if OK, negative on failure.
155 * Use: Checks a signature on a message, Requires @g@, @p@, @h@.
158 int gkcdsa_verify(const gkcdsa *c, const gkcdsa_sig *s, const void *m)
161 size_t hsz = c->h->hashsz;
162 group_expfactor e[2];
169 if (MP_CMP(s->s, <, MP_ONE) || MP_CMP(s->s, >=, g->r))
171 x = mp_loadb(MP_NEW, m, hsz); y = mp_loadb(MP_NEW, s->r, hsz);
172 x = mp_xor(x, x, y); mp_div(0, &x, x, g->r);
173 e[0].base = c->p; e[0].exp = s->s;
174 e[1].base = g->g; e[1].exp = x;
175 z = G_CREATE(g); G_MEXP(g, z, e, 2);
176 h = hashge(g, c->h, z); p = GH_DONE(h, 0);
177 if (memcmp(p, s->r, hsz) == 0) rc = 0;
178 mp_drop(x); mp_drop(y); G_DESTROY(g, z); GH_DESTROY(h);
182 /*----- Test rig ----------------------------------------------------------*/
186 static group *getgroup(const char *p) {
187 group *g; qd_parse qd;
188 qd.p = p; qd.e = 0; g = group_parse(&qd);
189 if (g && !qd_eofp(&qd)) { G_DESTROYGROUP(g); g = 0; qd.e = "junk at eof"; }
191 fprintf(stderr, "bad group string `%.*s|%s': %s\n",
192 (int)(qd.p - p), p, qd.p, qd.e);
198 static ge *getge(group *g, const char *p) {
200 if (group_readstring(g, x, p, 0)) {
201 fprintf(stderr, "bad group element `%s'\n", p);
207 static void showge(group *g, const char *p, ge *x) {
208 fprintf(stderr, "*** %s = ", p); group_writefile(g, x, stderr);
212 static void showmp(const char *p, mp *x, int r) {
213 fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r);
217 static int tsign(dstr *v)
220 gkcdsa_sig s, ss = GKCDSA_SIG_INIT;
227 c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf);
228 c.u = *(mp **)v[2].buf; k = *(mp **)v[4].buf;
229 s.r = (octet *)v[5].buf; s.s = *(mp **)v[6].buf;
230 DENSURE(&d, c.h->hashsz); d.len = c.h->hashsz; memset(d.buf, 0, d.len);
231 ss.r = (octet *)d.buf;
233 x = mp_modinv(MP_NEW, c.u, c.g->r);
234 c.p = G_CREATE(c.g); G_EXP(c.g, c.p, c.g->g, x);
235 h = gkcdsa_beginhash(&c);
236 GH_HASH(h, v[3].buf, v[3].len);
237 gkcdsa_endhash(&c, h);
238 gkcdsa_sign(&c, &ss, GH_DONE(h, 0), k);
239 if (memcmp(s.r, ss.r, c.h->hashsz) || !MP_EQ(s.s, ss.s)) {
241 fprintf(stderr, "*** sign failed!\n");
242 fprintf(stderr, "*** group: %s\n", v[0].buf);
243 fprintf(stderr, "*** hash: %s\n", c.h->name);
244 showmp("private key", c.u, 16);
245 showge(c.g, "public key", c.p);
246 fprintf(stderr, "*** message: `%s'\n", v[3].buf);
247 fprintf(stderr, "*** computed r = ");
248 type_hex.dump(&d, stderr); putc('\n', stderr);
249 showmp("computed s", ss.s, 16);
250 fprintf(stderr, "*** computed r = ");
251 type_hex.dump(&v[5], stderr); putc('\n', stderr);
252 showmp("expected s", s.s, 16);
254 mp_drop(s.s); dstr_destroy(&d); mp_drop(ss.s); mp_drop(x); mp_drop(k);
255 mp_drop(c.u); G_DESTROY(c.g, c.p); G_DESTROYGROUP(c.g); GH_DESTROY(h);
256 assert(mparena_count(MPARENA_GLOBAL) == 0);
260 static int tverify(dstr *v)
268 c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf);
269 c.p = getge(c.g, v[2].buf);
270 s.r = (octet *)v[4].buf; s.s = *(mp **)v[5].buf;
271 erc = *(int *)v[6].buf;
273 h = gkcdsa_beginhash(&c);
274 GH_HASH(h, v[3].buf, v[3].len);
275 gkcdsa_endhash(&c, h);
276 rc = gkcdsa_verify(&c, &s, GH_DONE(h, 0));
279 fprintf(stderr, "*** verify failed!\n");
280 fprintf(stderr, "*** group: %s\n", v[0].buf);
281 fprintf(stderr, "*** hash: %s\n", c.h->name);
282 showge(c.g, "public key", c.p);
283 fprintf(stderr, "*** message: `%s'\n", v[3].buf);
284 fprintf(stderr, "*** sig r = ");
285 type_hex.dump(&v[4], stderr); putc('\n', stderr);
286 showmp("sig s", s.s, 16);
287 fprintf(stderr, "*** expected %s\n", !erc ? "pass" : "fail");
289 mp_drop(s.s); G_DESTROY(c.g, c.p); G_DESTROYGROUP(c.g);
291 assert(mparena_count(MPARENA_GLOBAL) == 0);
295 static const test_chunk tests[] = {
296 { "sign", tsign, { &type_string, &type_string, &type_mp, &type_string,
297 &type_mp, &type_hex, &type_mp } },
298 { "verify", tverify, { &type_string, &type_string, &type_string,
299 &type_string, &type_hex, &type_mp, &type_int } },
303 int main(int argc, char *argv[])
306 test_run(argc, argv, tests, SRCDIR "/t/gkcdsa");
312 /*----- That's all, folks -------------------------------------------------*/