Release 2.1.3.
[u/mdw/catacomb] / gdsa.c
CommitLineData
e9026a0a 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
e9026a0a 4 *
5 * Generalized version of DSA
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
e9026a0a 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.
45c0fd36 18 *
e9026a0a 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.
45c0fd36 23 *
e9026a0a 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
e9026a0a 30/*----- Header files ------------------------------------------------------*/
31
42ca150c 32#include "dsa.h"
e9026a0a 33#include "gdsa.h"
34#include "group.h"
35#include "ghash.h"
36#include "mpbarrett.h"
37#include "mprand.h"
38
39/*----- Main code ---------------------------------------------------------*/
40
41/* --- @gdsa_beginhash@ --- *
42 *
43 * Arguments: @const gdsa *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@.
49 */
50
51ghash *gdsa_beginhash(const gdsa *c) { return (GH_INIT(c->h)); }
52
53/* --- @gdsa_endhash@ --- *
54 *
55 * Arguments: @const gdsa *c@ = pointer to the context structure
56 * @ghash *h@ = the hashing context
57 *
58 * Returns: ---
59 *
60 * Use: Does any final thing that DSA wants to do when hashing a
45c0fd36 61 * message. (Actually, there's nothing.) The hashing context
e9026a0a 62 * isn't finalized.
63 */
64
f4535c64 65void gdsa_endhash(const gdsa *c, ghash *h) { ; }
e9026a0a 66
67/* --- @gdsa_sign@ --- *
68 *
69 * Arguments: @const gdsa *c@ = my context structure
70 * @gdsa_sig *s@ = where to put the signature (initialized)
71 * @const void *m@ = pointer to message hash
72 * @mp *k@ = random exponent for this message or null
73 *
74 * Returns: ---
75 *
76 * Use: Signs a message. Requires @g@, @u@, @h@, and @r@ if @k@ is
77 * null. This is a better idea than inventing @k@ yourself.
78 */
79
80void gdsa_sign(const gdsa *c, gdsa_sig *s, const void *m, mp *k)
81{
82 group *g = c->g;
c97fbcf9 83 mp *mr = dsa_h2n(MP_NEW, g->r, m, c->h->hashsz);
e9026a0a 84 ge *z = G_CREATE(g);
85 mp *sr = s->r, *ss = s->s;
86 mpbarrett b;
87
88 if (k) { MP_COPY(k); goto have_k; }
89new_k:
90 k = mprand_range(k, g->r, c->r, 0);
91have_k:
a69a3efd 92 if (MP_ZEROP(k)) goto new_k;
e9026a0a 93 G_EXP(g, z, g->g, k);
94 sr = G_TOINT(g, sr, z); assert(sr);
a69a3efd 95 if (MP_ZEROP(sr)) goto new_k;
e9026a0a 96
97 mp_div(0, &sr, sr, g->r);
98 mpbarrett_create(&b, g->r);
99 ss = mp_mul(ss, sr, c->u); ss = mpbarrett_reduce(&b, ss, ss);
100 ss = mp_add(ss, ss, mr); mp_div(0, &ss, ss, g->r);
b817bfc6 101 k = mp_modinv(k, k, g->r);
e9026a0a 102 ss = mp_mul(ss, ss, k); ss = mpbarrett_reduce(&b, ss, ss);
103 s->r = sr; s->s = ss;
104 mp_drop(k); mp_drop(mr); mpbarrett_destroy(&b); G_DESTROY(g, z);
105}
106
107/* --- @gdsa_verify@ --- *
108 *
109 * Arguments: @const gdsa *c@ = my context structure
110 * @const gdsa_sig *s@ = the signature to verify
111 * @const void *m@ = pointer to message hash
112 *
113 * Returns: Zero if OK, negative on failure.
114 *
115 * Use: Checks a signature on a message, Requires @g@, @p@ and @h@.
116 */
117
118int gdsa_verify(const gdsa *c, const gdsa_sig *s, const void *m)
119{
120 group *g = c->g;
121 group_expfactor e[2];
122 mpbarrett b;
b817bfc6 123 mp *h, *t;
e9026a0a 124 ge *w;
125 int rc = -1;
126
127 if (MP_CMP(s->r, <, MP_ONE) || MP_CMP(s->r, >=, g->r) ||
128 MP_CMP(s->s, <, MP_ONE) || MP_CMP(s->s, >=, g->r))
129 return (-1);
b817bfc6 130 mpbarrett_create(&b, g->r); h = mp_modinv(MP_NEW, s->s, g->r);
e9026a0a 131 e[0].base = g->g; e[1].base = c->p;
c97fbcf9 132 t = dsa_h2n(MP_NEW, g->r, m, c->h->hashsz); mp_div(0, &t, t, g->r);
e9026a0a 133 t = mp_mul(t, t, h); e[0].exp = t = mpbarrett_reduce(&b, t, t);
134 h = mp_mul(h, s->r, h); e[1].exp = h = mpbarrett_reduce(&b, h, h);
135 w = G_CREATE(g); G_MEXP(g, w, e, 2);
136 t = G_TOINT(g, t, w); if (!t) goto done;
137 mp_div(0, &t, t, g->r); if (MP_EQ(t, s->r)) rc = 0;
138done:
139 G_DESTROY(g, w); mp_drop(t); mp_drop(h); mpbarrett_destroy(&b);
140 return (rc);
141}
142
143/*----- Test rig ----------------------------------------------------------*/
144
145#ifdef TEST_RIG
146
147static group *getgroup(const char *p) {
148 group *g; qd_parse qd;
149 qd.p = p; qd.e = 0; g = group_parse(&qd);
150 if (g && !qd_eofp(&qd)) { G_DESTROYGROUP(g); g = 0; qd.e = "junk at eof"; }
151 if (!g) { fprintf(stderr, "bad group string `%.*s|%s': %s\n", qd.p - p,
152 p, qd.p, qd.e); exit(1); }
153 return (g);
154}
155
156static ge *getge(group *g, const char *p) {
157 ge *x = G_CREATE(g);
158 if (group_readstring(g, x, p, 0)) {
159 fprintf(stderr, "bad group element `%s'\n", p);
160 exit(1);
161 }
162 return (x);
163}
164
165static void showge(group *g, const char *p, ge *x) {
166 fprintf(stderr, "*** %s = ", p); group_writefile(g, x, stderr);
167 putc('\n', stderr);
168}
169
170static void showmp(const char *p, mp *x, int r) {
45c0fd36 171 fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r);
e9026a0a 172 putc('\n', stderr);
173}
174
175static int tsign(dstr *v)
176{
177 gdsa c;
178 gdsa_sig s, ss = GDSA_SIG_INIT;
179 ghash *h;
180 mp *k;
181 int ok = 1;
182
183 c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf);
184 c.u = *(mp **)v[2].buf; k = *(mp **)v[4].buf;
185 s.r = *(mp **)v[5].buf; s.s = *(mp **)v[6].buf;
186
187 h = gdsa_beginhash(&c);
188 GH_HASH(h, v[3].buf, v[3].len);
189 gdsa_endhash(&c, h);
190 gdsa_sign(&c, &ss, GH_DONE(h, 0), k);
191 if (!MP_EQ(s.r, ss.r) || !MP_EQ(s.s, ss.s)) {
192 ok = 0;
193 fprintf(stderr, "*** sign failed!\n");
194 fprintf(stderr, "*** group: %s\n", v[0].buf);
195 fprintf(stderr, "*** hash: %s\n", c.h->name);
196 showmp("private key", c.u, 16);
197 fprintf(stderr, "*** message: `%s'\n", v[3].buf);
198 showmp("computed r", ss.r, 16); showmp("computed s", ss.s, 16);
199 showmp("expected r", s.r, 16); showmp("expected s", s.s, 16);
200 }
201 mp_drop(s.r); mp_drop(s.s); mp_drop(ss.r); mp_drop(ss.s);
202 mp_drop(k); mp_drop(c.u); G_DESTROYGROUP(c.g); GH_DESTROY(h);
203 assert(mparena_count(MPARENA_GLOBAL) == 0);
204 return (ok);
205}
206
207static int tverify(dstr *v)
208{
209 gdsa c;
210 gdsa_sig s;
211 ghash *h;
212 int rc, erc;
213 int ok = 1;
214
215 c.g = getgroup(v[0].buf); c.h = ghash_byname(v[1].buf);
216 c.p = getge(c.g, v[2].buf);
217 s.r = *(mp **)v[4].buf; s.s = *(mp **)v[5].buf;
218 erc = *(int *)v[6].buf;
219
220 h = gdsa_beginhash(&c);
221 GH_HASH(h, v[3].buf, v[3].len);
222 gdsa_endhash(&c, h);
223 rc = gdsa_verify(&c, &s, GH_DONE(h, 0));
224 if (!rc != !erc) {
225 ok = 0;
226 fprintf(stderr, "*** verify failed!\n");
227 fprintf(stderr, "*** group: %s\n", v[0].buf);
228 fprintf(stderr, "*** hash: %s\n", c.h->name);
229 showge(c.g, "public key", c.p);
230 fprintf(stderr, "*** message: `%s'\n", v[3].buf);
231 showmp("sig r", s.r, 16); showmp("sig s", s.s, 16);
232 fprintf(stderr, "*** expected %s\n", !erc ? "pass" : "fail");
233 }
234 mp_drop(s.r); mp_drop(s.s); G_DESTROY(c.g, c.p); G_DESTROYGROUP(c.g);
235 GH_DESTROY(h);
236 assert(mparena_count(MPARENA_GLOBAL) == 0);
237 return (ok);
238}
239
240static const test_chunk tests[] = {
241 { "sign", tsign, { &type_string, &type_string, &type_mp, &type_string,
242 &type_mp, &type_mp, &type_mp } },
243 { "verify", tverify, { &type_string, &type_string, &type_string,
244 &type_string, &type_mp, &type_mp, &type_int } },
245 { 0 }
246};
247
248int main(int argc, char *argv[])
249{
250 sub_init();
251 test_run(argc, argv, tests, SRCDIR "/tests/gdsa");
252 return (0);
253}
254
255#endif
256
257/*----- That's all, folks -------------------------------------------------*/