Change interface for suggested destinations.
[u/mdw/catacomb] / dsa-verify.c
1 /* -*-c-*-
2 *
3 * $Id: dsa-verify.c,v 1.3 1999/12/10 23:18:38 mdw Exp $
4 *
5 * DSA signature verification
6 *
7 * (c) 1999 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: dsa-verify.c,v $
33 * Revision 1.3 1999/12/10 23:18:38 mdw
34 * Change interface for suggested destinations.
35 *
36 * Revision 1.2 1999/11/23 00:20:04 mdw
37 * Remove stray debugging code.
38 *
39 * Revision 1.1 1999/11/19 19:28:00 mdw
40 * Implementation of the Digital Signature Algorithm.
41 *
42 */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include "dsa.h"
47 #include "mp.h"
48 #include "mpmont.h"
49
50 /*----- Main code ---------------------------------------------------------*/
51
52 /* --- @dsa_vrfy@ --- *
53 *
54 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
55 * @mp *y@ = public verification key
56 * @mp *m@ = message which was signed
57 * @mp *r, *s@ = the signature
58 *
59 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
60 *
61 * Use: Verifies a DSA digital signature.
62 */
63
64 int dsa_vrfy(const dsa_param *dp, mp *y, mp *m, mp *r, mp *s)
65 {
66 mpmont pm, qm;
67 mp *w;
68 mpmont_factor f[2];
69 int ok;
70
71 /* --- Ensure that all of the signature bits are in range --- */
72
73 if ((r->f | s->f) & MP_NEG)
74 return (0);
75 if (MP_CMP(r, >=, dp->q) || MP_CMP(s, >=, dp->q))
76 return (0);
77
78 /* --- Set up Montgomery contexts --- */
79
80 mpmont_create(&pm, dp->p);
81 mpmont_create(&qm, dp->q);
82
83 /* --- Compute %$w = s^{-1} \bmod q$% --- */
84
85 {
86 mp *z = MP_NEW;
87 mp_gcd(0, 0, &z, dp->q, s);
88 w = mpmont_mul(&qm, MP_NEW, z, qm.r2);
89 mp_drop(z);
90 }
91
92 /* --- Compute %$wr%$ and %$wm$% --- */
93
94 f[0].exp = mpmont_mul(&qm, MP_NEW, w, m);
95 f[1].exp = mpmont_mul(&qm, MP_NEW, w, r);
96 mp_drop(w);
97 mpmont_destroy(&qm);
98
99 /* --- Do the exponentiation and take residue mod @q@ --- */
100
101 f[0].base = dp->g;
102 f[1].base = y;
103 w = mpmont_mexp(&pm, MP_NEW, f, 2);
104 mp_div(0, &w, w, dp->q);
105 ok = MP_CMP(w, ==, r);
106
107 /* --- Tidy up --- */
108
109 mp_drop(w);
110 mp_drop(f[0].exp);
111 mp_drop(f[1].exp);
112 mpmont_destroy(&pm);
113 return (ok);
114 }
115
116 /* --- @dsa_verify@ --- *
117 *
118 * Arguments: @const dsa_param *dp@ = pointer to DSA parameters
119 * @mp *y@ = public verification key
120 * @const void *m@ = pointer to message block
121 * @size_t msz@ = size of message block
122 * @const void *r@ = pointer to @r@ signature half
123 * @size_t rsz@ = size of @r@
124 * @const void *s@ = pointer to @s@ signature half
125 * @size_t ssz@ = size of @s@
126 *
127 * Returns: Zero if the signature is a forgery, nonzero if it's valid.
128 *
129 * Use: Verifies a DSA digital signature.
130 */
131
132 int dsa_verify(const dsa_param *dp, mp *y,
133 const void *m, size_t msz,
134 const void *r, size_t rsz,
135 const void *s, size_t ssz)
136 {
137 mp *mm = mp_loadb(MP_NEW, m, msz);
138 mp *rm = mp_loadb(MP_NEW, r, rsz);
139 mp *sm = mp_loadb(MP_NEW, s, ssz);
140 int ok = dsa_vrfy(dp, y, mm, rm, sm);
141 mp_drop(mm);
142 mp_drop(rm);
143 mp_drop(sm);
144 return (ok);
145 }
146
147 /*----- Test rig ----------------------------------------------------------*/
148
149 #ifdef TEST_RIG
150
151 #include <mLib/testrig.h>
152
153 #include "sha.h"
154
155 static int verify(int good, dstr *v)
156 {
157 dsa_param dp;
158 mp *y;
159 sha_ctx c;
160 octet hash[SHA_HASHSZ];
161 int ok = 1;
162 int rc;
163
164 dp.q = *(mp **)v[0].buf;
165 dp.p = *(mp **)v[1].buf;
166 dp.g = *(mp **)v[2].buf;
167 y = *(mp **)v[3].buf;
168
169 sha_init(&c);
170 sha_hash(&c, v[4].buf, v[4].len);
171 sha_done(&c, hash);
172
173 rc = dsa_verify(&dp, y, hash, sizeof(hash),
174 v[5].buf, v[5].len, v[6].buf, v[6].len);
175
176 if (!rc != !good) {
177 if (good)
178 fputs("\n*** verification failed", stderr);
179 else
180 fputs("\n*** verification succeeded", stderr);
181 fputs("\nq = ", stderr); mp_writefile(dp.q, stderr, 16);
182 fputs("\np = ", stderr); mp_writefile(dp.p, stderr, 16);
183 fputs("\ng = ", stderr); mp_writefile(dp.g, stderr, 16);
184 fputs("\ny = ", stderr); mp_writefile(y, stderr, 16);
185 fprintf(stderr, "\nmessage = `%s'", v[4].buf);
186 fputs("\nr = ", stderr); type_hex.dump(&v[5], stderr);
187 fputs("\ns = ", stderr); type_hex.dump(&v[6], stderr);
188 fputc('\n', stderr);
189 ok = 0;
190 }
191
192 mp_drop(dp.p);
193 mp_drop(dp.q);
194 mp_drop(dp.g);
195 mp_drop(y);
196 assert(mparena_count(MPARENA_GLOBAL) == 0);
197 return (ok);
198 }
199
200 static int vgood(dstr *v) { return verify(1, v); }
201 static int vbad(dstr *v) { return verify(0, v); }
202
203 static test_chunk tests[] = {
204 { "verify-good", vgood,
205 { &type_mp, &type_mp, &type_mp, &type_mp,
206 &type_string, &type_hex, &type_hex, 0 } },
207 { "verify-bad", vbad,
208 { &type_mp, &type_mp, &type_mp, &type_mp,
209 &type_string, &type_hex, &type_hex, 0 } },
210 { 0, 0, { 0 } }
211 };
212
213 int main(int argc, char *argv[])
214 {
215 sub_init();
216 test_run(argc, argv, tests, SRCDIR "/tests/dsa");
217 return (0);
218 }
219
220 #endif
221
222 /*----- That's all, folks -------------------------------------------------*/