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