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