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