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