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