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