More options for bignum debugging
[u/mdw/putty] / sshdss.c
CommitLineData
7cca0d81 1#include <stdio.h>
2#include <stdlib.h>
3
e5574168 4#include "ssh.h"
5
7cca0d81 6#define GET_32BIT(cp) \
7 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
8 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
9 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
10 ((unsigned long)(unsigned char)(cp)[3]))
11
d5859615 12#define PUT_32BIT(cp, value) { \
13 (cp)[0] = (unsigned char)((value) >> 24); \
14 (cp)[1] = (unsigned char)((value) >> 16); \
15 (cp)[2] = (unsigned char)((value) >> 8); \
16 (cp)[3] = (unsigned char)(value); }
17
9c621433 18#if 0
c5fbd713 19#define DEBUG_DSS
9c621433 20#else
21#define diagbn(x,y)
22#endif
23
7cca0d81 24static void getstring(char **data, int *datalen, char **p, int *length) {
25 *p = NULL;
26 if (*datalen < 4)
27 return;
28 *length = GET_32BIT(*data);
29 *datalen -= 4; *data += 4;
30 if (*datalen < *length)
31 return;
32 *p = *data;
33 *data += *length; *datalen -= *length;
34}
35static Bignum getmp(char **data, int *datalen) {
36 char *p;
3709bfe9 37 int length;
7cca0d81 38 Bignum b;
39
40 getstring(data, datalen, &p, &length);
41 if (!p)
42 return NULL;
43 if (p[0] & 0x80)
44 return NULL; /* negative mp */
3709bfe9 45 b = bignum_from_bytes(p, length);
7cca0d81 46 return b;
47}
48
49static Bignum get160(char **data, int *datalen) {
7cca0d81 50 Bignum b;
51
3709bfe9 52 b = bignum_from_bytes(*data, 20);
7cca0d81 53 *data += 20; *datalen -= 20;
54
7cca0d81 55 return b;
56}
57
e055a386 58struct dss_key {
59 Bignum p, q, g, y;
60};
7cca0d81 61
e055a386 62static void *dss_newkey(char *data, int len) {
7cca0d81 63 char *p;
64 int slen;
e055a386 65 struct dss_key *dss;
66
dcbde236 67 dss = smalloc(sizeof(struct dss_key));
e055a386 68 if (!dss) return NULL;
7cca0d81 69 getstring(&data, &len, &p, &slen);
9c621433 70
71#ifdef DEBUG_DSS
72 {
73 int i;
74 printf("key:");
75 for (i=0;i<len;i++)
76 printf(" %02x", (unsigned char)(data[i]));
77 printf("\n");
78 }
79#endif
80
7cca0d81 81 if (!p || memcmp(p, "ssh-dss", 7)) {
dcbde236 82 sfree(dss);
e055a386 83 return NULL;
7cca0d81 84 }
e055a386 85 dss->p = getmp(&data, &len);
86 dss->q = getmp(&data, &len);
87 dss->g = getmp(&data, &len);
88 dss->y = getmp(&data, &len);
89
90 return dss;
7cca0d81 91}
92
e055a386 93static void dss_freekey(void *key) {
94 struct dss_key *dss = (struct dss_key *)key;
95 freebn(dss->p);
96 freebn(dss->q);
97 freebn(dss->g);
98 freebn(dss->y);
dcbde236 99 sfree(dss);
e055a386 100}
101
102static char *dss_fmtkey(void *key) {
103 struct dss_key *dss = (struct dss_key *)key;
7cca0d81 104 char *p;
d5859615 105 int len, i, pos, nibbles;
106 static const char hex[] = "0123456789abcdef";
e055a386 107 if (!dss->p)
7cca0d81 108 return NULL;
d5859615 109 len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
3709bfe9 110 len += 4 * (ssh1_bignum_bitcount(dss->p)+15)/16;
111 len += 4 * (ssh1_bignum_bitcount(dss->q)+15)/16;
112 len += 4 * (ssh1_bignum_bitcount(dss->g)+15)/16;
113 len += 4 * (ssh1_bignum_bitcount(dss->y)+15)/16;
dcbde236 114 p = smalloc(len);
7cca0d81 115 if (!p) return NULL;
d5859615 116
117 pos = 0;
118 pos += sprintf(p+pos, "0x");
e055a386 119 nibbles = (3 + ssh1_bignum_bitcount(dss->p))/4; if (nibbles<1) nibbles=1;
d5859615 120 for (i=nibbles; i-- ;)
e055a386 121 p[pos++] = hex[(bignum_byte(dss->p, i/2) >> (4*(i%2))) & 0xF];
3aa0b691 122 pos += sprintf(p+pos, ",0x");
e055a386 123 nibbles = (3 + ssh1_bignum_bitcount(dss->q))/4; if (nibbles<1) nibbles=1;
d5859615 124 for (i=nibbles; i-- ;)
e055a386 125 p[pos++] = hex[(bignum_byte(dss->q, i/2) >> (4*(i%2))) & 0xF];
3aa0b691 126 pos += sprintf(p+pos, ",0x");
e055a386 127 nibbles = (3 + ssh1_bignum_bitcount(dss->g))/4; if (nibbles<1) nibbles=1;
d5859615 128 for (i=nibbles; i-- ;)
e055a386 129 p[pos++] = hex[(bignum_byte(dss->g, i/2) >> (4*(i%2))) & 0xF];
3aa0b691 130 pos += sprintf(p+pos, ",0x");
e055a386 131 nibbles = (3 + ssh1_bignum_bitcount(dss->y))/4; if (nibbles<1) nibbles=1;
d5859615 132 for (i=nibbles; i-- ;)
e055a386 133 p[pos++] = hex[(bignum_byte(dss->y, i/2) >> (4*(i%2))) & 0xF];
d5859615 134 p[pos] = '\0';
7cca0d81 135 return p;
136}
137
e055a386 138static char *dss_fingerprint(void *key) {
139 struct dss_key *dss = (struct dss_key *)key;
d5859615 140 struct MD5Context md5c;
141 unsigned char digest[16], lenbuf[4];
142 char buffer[16*3+40];
143 char *ret;
144 int numlen, i;
145
146 MD5Init(&md5c);
147 MD5Update(&md5c, "\0\0\0\7ssh-dss", 11);
148
149#define ADD_BIGNUM(bignum) \
150 numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
151 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
152 for (i = numlen; i-- ;) { \
153 unsigned char c = bignum_byte(bignum, i); \
154 MD5Update(&md5c, &c, 1); \
155 }
e055a386 156 ADD_BIGNUM(dss->p);
157 ADD_BIGNUM(dss->q);
158 ADD_BIGNUM(dss->g);
159 ADD_BIGNUM(dss->y);
d5859615 160#undef ADD_BIGNUM
161
162 MD5Final(digest, &md5c);
163
5d03233b 164 sprintf(buffer, "ssh-dss %d ", ssh1_bignum_bitcount(dss->p));
d5859615 165 for (i = 0; i < 16; i++)
166 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
dcbde236 167 ret = smalloc(strlen(buffer)+1);
d5859615 168 if (ret)
169 strcpy(ret, buffer);
170 return ret;
171}
172
e055a386 173static int dss_verifysig(void *key, char *sig, int siglen,
174 char *data, int datalen) {
175 struct dss_key *dss = (struct dss_key *)key;
7cca0d81 176 char *p;
c5fbd713 177 int slen;
7cca0d81 178 char hash[20];
59600f67 179 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
7cca0d81 180 int ret;
181
e055a386 182 if (!dss->p)
7cca0d81 183 return 0;
184
9c621433 185#ifdef DEBUG_DSS
186 {
187 int i;
188 printf("sig:");
189 for (i=0;i<siglen;i++)
3709bfe9 190 printf(" %02x", (unsigned char)(sig[i]));
9c621433 191 printf("\n");
192 }
193#endif
7f7837c8 194 /*
195 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
196 * of a DSA signature. OpenSSH is in line with the IETF drafts:
197 * it uses a string "ssh-dss", followed by a 40-byte string
198 * containing two 160-bit integers end-to-end. Commercial SSH
199 * can't be bothered with the header bit, and considers a DSA
200 * signature blob to be _just_ the 40-byte string containing
201 * the two 160-bit integers. We tell them apart by measuring
202 * the length: length 40 means the commercial-SSH bug, anything
203 * else is assumed to be IETF-compliant.
204 */
205 if (siglen != 40) { /* bug not present; read admin fields */
206 getstring(&sig, &siglen, &p, &slen);
efa4a6f2 207 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
7f7837c8 208 return 0;
209 }
210 sig += 4, siglen -= 4; /* skip yet another length field */
7cca0d81 211 }
e055a386 212 diagbn("p=", dss->p);
213 diagbn("q=", dss->q);
214 diagbn("g=", dss->g);
215 diagbn("y=", dss->y);
7cca0d81 216 r = get160(&sig, &siglen);
9c621433 217 diagbn("r=", r);
7cca0d81 218 s = get160(&sig, &siglen);
9c621433 219 diagbn("s=", s);
7cca0d81 220 if (!r || !s)
221 return 0;
222
223 /*
224 * Step 1. w <- s^-1 mod q.
225 */
e055a386 226 w = modinv(s, dss->q);
9c621433 227 diagbn("w=", w);
7cca0d81 228
229 /*
230 * Step 2. u1 <- SHA(message) * w mod q.
231 */
7cca0d81 232 SHA_Simple(data, datalen, hash);
233 p = hash; slen = 20; sha = get160(&p, &slen);
9c621433 234 diagbn("sha=", sha);
e055a386 235 u1 = modmul(sha, w, dss->q);
9c621433 236 diagbn("u1=", u1);
7cca0d81 237
238 /*
239 * Step 3. u2 <- r * w mod q.
240 */
e055a386 241 u2 = modmul(r, w, dss->q);
9c621433 242 diagbn("u2=", u2);
7cca0d81 243
244 /*
245 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
246 */
e055a386 247 gu1p = modpow(dss->g, u1, dss->p);
59600f67 248 diagbn("gu1p=", gu1p);
e055a386 249 yu2p = modpow(dss->y, u2, dss->p);
59600f67 250 diagbn("yu2p=", yu2p);
e055a386 251 gu1yu2p = modmul(gu1p, yu2p, dss->p);
59600f67 252 diagbn("gu1yu2p=", gu1yu2p);
e055a386 253 v = modmul(gu1yu2p, One, dss->q);
9c621433 254 diagbn("gu1yu2q=v=", v);
255 diagbn("r=", r);
7cca0d81 256
257 /*
258 * Step 5. v should now be equal to r.
259 */
260
c5fbd713 261 ret = !bignum_cmp(v, r);
7cca0d81 262
263 freebn(w);
7cca0d81 264 freebn(sha);
59600f67 265 freebn(gu1p);
266 freebn(yu2p);
267 freebn(gu1yu2p);
7cca0d81 268 freebn(v);
269 freebn(r);
270 freebn(s);
271
272 return ret;
273}
274
e055a386 275int dss_sign(void *key, char *sig, int siglen,
276 char *data, int datalen) {
277 return 0; /* do nothing */
278}
279
280struct ssh_signkey ssh_dss = {
281 dss_newkey,
282 dss_freekey,
7cca0d81 283 dss_fmtkey,
d5859615 284 dss_fingerprint,
7cca0d81 285 dss_verifysig,
e055a386 286 dss_sign,
d5859615 287 "ssh-dss",
288 "dss"
e5574168 289};