7cca0d81 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
65a22376 |
3 | #include <assert.h> |
7cca0d81 |
4 | |
e5574168 |
5 | #include "ssh.h" |
5c72ca61 |
6 | #include "misc.h" |
e5574168 |
7 | |
7cca0d81 |
8 | #define GET_32BIT(cp) \ |
9 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
10 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
11 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
12 | ((unsigned long)(unsigned char)(cp)[3])) |
13 | |
d5859615 |
14 | #define PUT_32BIT(cp, value) { \ |
15 | (cp)[0] = (unsigned char)((value) >> 24); \ |
16 | (cp)[1] = (unsigned char)((value) >> 16); \ |
17 | (cp)[2] = (unsigned char)((value) >> 8); \ |
18 | (cp)[3] = (unsigned char)(value); } |
19 | |
5c72ca61 |
20 | static void sha_mpint(SHA_State * s, Bignum b) |
21 | { |
5c72ca61 |
22 | unsigned char lenbuf[4]; |
23 | int len; |
24 | len = (bignum_bitcount(b) + 8) / 8; |
25 | PUT_32BIT(lenbuf, len); |
26 | SHA_Bytes(s, lenbuf, 4); |
27 | while (len-- > 0) { |
28 | lenbuf[0] = bignum_byte(b, len); |
29 | SHA_Bytes(s, lenbuf, 1); |
30 | } |
31 | memset(lenbuf, 0, sizeof(lenbuf)); |
32 | } |
33 | |
34 | static void sha512_mpint(SHA512_State * s, Bignum b) |
35 | { |
5c72ca61 |
36 | unsigned char lenbuf[4]; |
37 | int len; |
38 | len = (bignum_bitcount(b) + 8) / 8; |
39 | PUT_32BIT(lenbuf, len); |
40 | SHA512_Bytes(s, lenbuf, 4); |
41 | while (len-- > 0) { |
42 | lenbuf[0] = bignum_byte(b, len); |
43 | SHA512_Bytes(s, lenbuf, 1); |
44 | } |
45 | memset(lenbuf, 0, sizeof(lenbuf)); |
46 | } |
9c621433 |
47 | |
32874aea |
48 | static void getstring(char **data, int *datalen, char **p, int *length) |
49 | { |
7cca0d81 |
50 | *p = NULL; |
51 | if (*datalen < 4) |
32874aea |
52 | return; |
7cca0d81 |
53 | *length = GET_32BIT(*data); |
32874aea |
54 | *datalen -= 4; |
55 | *data += 4; |
7cca0d81 |
56 | if (*datalen < *length) |
32874aea |
57 | return; |
7cca0d81 |
58 | *p = *data; |
32874aea |
59 | *data += *length; |
60 | *datalen -= *length; |
7cca0d81 |
61 | } |
32874aea |
62 | static Bignum getmp(char **data, int *datalen) |
63 | { |
7cca0d81 |
64 | char *p; |
3709bfe9 |
65 | int length; |
7cca0d81 |
66 | Bignum b; |
67 | |
68 | getstring(data, datalen, &p, &length); |
69 | if (!p) |
32874aea |
70 | return NULL; |
7cca0d81 |
71 | if (p[0] & 0x80) |
32874aea |
72 | return NULL; /* negative mp */ |
3709bfe9 |
73 | b = bignum_from_bytes(p, length); |
7cca0d81 |
74 | return b; |
75 | } |
76 | |
32874aea |
77 | static Bignum get160(char **data, int *datalen) |
78 | { |
7cca0d81 |
79 | Bignum b; |
80 | |
3709bfe9 |
81 | b = bignum_from_bytes(*data, 20); |
32874aea |
82 | *data += 20; |
83 | *datalen -= 20; |
7cca0d81 |
84 | |
7cca0d81 |
85 | return b; |
86 | } |
87 | |
32874aea |
88 | static void *dss_newkey(char *data, int len) |
89 | { |
7cca0d81 |
90 | char *p; |
91 | int slen; |
e055a386 |
92 | struct dss_key *dss; |
93 | |
dcbde236 |
94 | dss = smalloc(sizeof(struct dss_key)); |
32874aea |
95 | if (!dss) |
96 | return NULL; |
7cca0d81 |
97 | getstring(&data, &len, &p, &slen); |
9c621433 |
98 | |
99 | #ifdef DEBUG_DSS |
100 | { |
32874aea |
101 | int i; |
102 | printf("key:"); |
103 | for (i = 0; i < len; i++) |
104 | printf(" %02x", (unsigned char) (data[i])); |
105 | printf("\n"); |
9c621433 |
106 | } |
107 | #endif |
108 | |
7cca0d81 |
109 | if (!p || memcmp(p, "ssh-dss", 7)) { |
dcbde236 |
110 | sfree(dss); |
e055a386 |
111 | return NULL; |
7cca0d81 |
112 | } |
e055a386 |
113 | dss->p = getmp(&data, &len); |
114 | dss->q = getmp(&data, &len); |
115 | dss->g = getmp(&data, &len); |
116 | dss->y = getmp(&data, &len); |
117 | |
118 | return dss; |
7cca0d81 |
119 | } |
120 | |
32874aea |
121 | static void dss_freekey(void *key) |
122 | { |
123 | struct dss_key *dss = (struct dss_key *) key; |
e055a386 |
124 | freebn(dss->p); |
125 | freebn(dss->q); |
126 | freebn(dss->g); |
127 | freebn(dss->y); |
dcbde236 |
128 | sfree(dss); |
e055a386 |
129 | } |
130 | |
32874aea |
131 | static char *dss_fmtkey(void *key) |
132 | { |
133 | struct dss_key *dss = (struct dss_key *) key; |
7cca0d81 |
134 | char *p; |
d5859615 |
135 | int len, i, pos, nibbles; |
136 | static const char hex[] = "0123456789abcdef"; |
e055a386 |
137 | if (!dss->p) |
32874aea |
138 | return NULL; |
139 | len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */ |
140 | len += 4 * (bignum_bitcount(dss->p) + 15) / 16; |
141 | len += 4 * (bignum_bitcount(dss->q) + 15) / 16; |
142 | len += 4 * (bignum_bitcount(dss->g) + 15) / 16; |
143 | len += 4 * (bignum_bitcount(dss->y) + 15) / 16; |
dcbde236 |
144 | p = smalloc(len); |
32874aea |
145 | if (!p) |
146 | return NULL; |
d5859615 |
147 | |
148 | pos = 0; |
32874aea |
149 | pos += sprintf(p + pos, "0x"); |
150 | nibbles = (3 + bignum_bitcount(dss->p)) / 4; |
151 | if (nibbles < 1) |
152 | nibbles = 1; |
153 | for (i = nibbles; i--;) |
154 | p[pos++] = |
155 | hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF]; |
156 | pos += sprintf(p + pos, ",0x"); |
157 | nibbles = (3 + bignum_bitcount(dss->q)) / 4; |
158 | if (nibbles < 1) |
159 | nibbles = 1; |
160 | for (i = nibbles; i--;) |
161 | p[pos++] = |
162 | hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF]; |
163 | pos += sprintf(p + pos, ",0x"); |
164 | nibbles = (3 + bignum_bitcount(dss->g)) / 4; |
165 | if (nibbles < 1) |
166 | nibbles = 1; |
167 | for (i = nibbles; i--;) |
168 | p[pos++] = |
169 | hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF]; |
170 | pos += sprintf(p + pos, ",0x"); |
171 | nibbles = (3 + bignum_bitcount(dss->y)) / 4; |
172 | if (nibbles < 1) |
173 | nibbles = 1; |
174 | for (i = nibbles; i--;) |
175 | p[pos++] = |
176 | hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF]; |
d5859615 |
177 | p[pos] = '\0'; |
7cca0d81 |
178 | return p; |
179 | } |
180 | |
32874aea |
181 | static char *dss_fingerprint(void *key) |
182 | { |
183 | struct dss_key *dss = (struct dss_key *) key; |
d5859615 |
184 | struct MD5Context md5c; |
185 | unsigned char digest[16], lenbuf[4]; |
32874aea |
186 | char buffer[16 * 3 + 40]; |
d5859615 |
187 | char *ret; |
188 | int numlen, i; |
189 | |
190 | MD5Init(&md5c); |
191 | MD5Update(&md5c, "\0\0\0\7ssh-dss", 11); |
192 | |
193 | #define ADD_BIGNUM(bignum) \ |
ddecd643 |
194 | numlen = (bignum_bitcount(bignum)+8)/8; \ |
d5859615 |
195 | PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ |
196 | for (i = numlen; i-- ;) { \ |
197 | unsigned char c = bignum_byte(bignum, i); \ |
198 | MD5Update(&md5c, &c, 1); \ |
199 | } |
e055a386 |
200 | ADD_BIGNUM(dss->p); |
201 | ADD_BIGNUM(dss->q); |
202 | ADD_BIGNUM(dss->g); |
203 | ADD_BIGNUM(dss->y); |
d5859615 |
204 | #undef ADD_BIGNUM |
205 | |
206 | MD5Final(digest, &md5c); |
207 | |
ddecd643 |
208 | sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p)); |
d5859615 |
209 | for (i = 0; i < 16; i++) |
32874aea |
210 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
211 | digest[i]); |
212 | ret = smalloc(strlen(buffer) + 1); |
d5859615 |
213 | if (ret) |
32874aea |
214 | strcpy(ret, buffer); |
d5859615 |
215 | return ret; |
216 | } |
217 | |
e055a386 |
218 | static int dss_verifysig(void *key, char *sig, int siglen, |
32874aea |
219 | char *data, int datalen) |
220 | { |
221 | struct dss_key *dss = (struct dss_key *) key; |
7cca0d81 |
222 | char *p; |
c5fbd713 |
223 | int slen; |
7cca0d81 |
224 | char hash[20]; |
59600f67 |
225 | Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v; |
7cca0d81 |
226 | int ret; |
227 | |
e055a386 |
228 | if (!dss->p) |
32874aea |
229 | return 0; |
7cca0d81 |
230 | |
9c621433 |
231 | #ifdef DEBUG_DSS |
232 | { |
32874aea |
233 | int i; |
234 | printf("sig:"); |
235 | for (i = 0; i < siglen; i++) |
236 | printf(" %02x", (unsigned char) (sig[i])); |
237 | printf("\n"); |
9c621433 |
238 | } |
239 | #endif |
7f7837c8 |
240 | /* |
241 | * Commercial SSH (2.0.13) and OpenSSH disagree over the format |
242 | * of a DSA signature. OpenSSH is in line with the IETF drafts: |
243 | * it uses a string "ssh-dss", followed by a 40-byte string |
244 | * containing two 160-bit integers end-to-end. Commercial SSH |
245 | * can't be bothered with the header bit, and considers a DSA |
246 | * signature blob to be _just_ the 40-byte string containing |
247 | * the two 160-bit integers. We tell them apart by measuring |
248 | * the length: length 40 means the commercial-SSH bug, anything |
249 | * else is assumed to be IETF-compliant. |
250 | */ |
32874aea |
251 | if (siglen != 40) { /* bug not present; read admin fields */ |
252 | getstring(&sig, &siglen, &p, &slen); |
253 | if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) { |
254 | return 0; |
255 | } |
256 | sig += 4, siglen -= 4; /* skip yet another length field */ |
7cca0d81 |
257 | } |
7cca0d81 |
258 | r = get160(&sig, &siglen); |
259 | s = get160(&sig, &siglen); |
260 | if (!r || !s) |
32874aea |
261 | return 0; |
7cca0d81 |
262 | |
263 | /* |
264 | * Step 1. w <- s^-1 mod q. |
265 | */ |
e055a386 |
266 | w = modinv(s, dss->q); |
7cca0d81 |
267 | |
268 | /* |
269 | * Step 2. u1 <- SHA(message) * w mod q. |
270 | */ |
7cca0d81 |
271 | SHA_Simple(data, datalen, hash); |
32874aea |
272 | p = hash; |
273 | slen = 20; |
274 | sha = get160(&p, &slen); |
e055a386 |
275 | u1 = modmul(sha, w, dss->q); |
7cca0d81 |
276 | |
277 | /* |
278 | * Step 3. u2 <- r * w mod q. |
279 | */ |
e055a386 |
280 | u2 = modmul(r, w, dss->q); |
7cca0d81 |
281 | |
282 | /* |
283 | * Step 4. v <- (g^u1 * y^u2 mod p) mod q. |
284 | */ |
e055a386 |
285 | gu1p = modpow(dss->g, u1, dss->p); |
e055a386 |
286 | yu2p = modpow(dss->y, u2, dss->p); |
e055a386 |
287 | gu1yu2p = modmul(gu1p, yu2p, dss->p); |
e055a386 |
288 | v = modmul(gu1yu2p, One, dss->q); |
7cca0d81 |
289 | |
290 | /* |
291 | * Step 5. v should now be equal to r. |
292 | */ |
293 | |
c5fbd713 |
294 | ret = !bignum_cmp(v, r); |
7cca0d81 |
295 | |
296 | freebn(w); |
7cca0d81 |
297 | freebn(sha); |
59600f67 |
298 | freebn(gu1p); |
299 | freebn(yu2p); |
300 | freebn(gu1yu2p); |
7cca0d81 |
301 | freebn(v); |
302 | freebn(r); |
303 | freebn(s); |
304 | |
305 | return ret; |
306 | } |
307 | |
32874aea |
308 | static unsigned char *dss_public_blob(void *key, int *len) |
309 | { |
310 | struct dss_key *dss = (struct dss_key *) key; |
65a22376 |
311 | int plen, qlen, glen, ylen, bloblen; |
312 | int i; |
313 | unsigned char *blob, *p; |
314 | |
32874aea |
315 | plen = (bignum_bitcount(dss->p) + 8) / 8; |
316 | qlen = (bignum_bitcount(dss->q) + 8) / 8; |
317 | glen = (bignum_bitcount(dss->g) + 8) / 8; |
318 | ylen = (bignum_bitcount(dss->y) + 8) / 8; |
65a22376 |
319 | |
320 | /* |
321 | * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total |
322 | * 27 + sum of lengths. (five length fields, 20+7=27). |
323 | */ |
32874aea |
324 | bloblen = 27 + plen + qlen + glen + ylen; |
65a22376 |
325 | blob = smalloc(bloblen); |
326 | p = blob; |
32874aea |
327 | PUT_32BIT(p, 7); |
328 | p += 4; |
329 | memcpy(p, "ssh-dss", 7); |
330 | p += 7; |
331 | PUT_32BIT(p, plen); |
332 | p += 4; |
333 | for (i = plen; i--;) |
334 | *p++ = bignum_byte(dss->p, i); |
335 | PUT_32BIT(p, qlen); |
336 | p += 4; |
337 | for (i = qlen; i--;) |
338 | *p++ = bignum_byte(dss->q, i); |
339 | PUT_32BIT(p, glen); |
340 | p += 4; |
341 | for (i = glen; i--;) |
342 | *p++ = bignum_byte(dss->g, i); |
343 | PUT_32BIT(p, ylen); |
344 | p += 4; |
345 | for (i = ylen; i--;) |
346 | *p++ = bignum_byte(dss->y, i); |
65a22376 |
347 | assert(p == blob + bloblen); |
348 | *len = bloblen; |
349 | return blob; |
350 | } |
351 | |
32874aea |
352 | static unsigned char *dss_private_blob(void *key, int *len) |
353 | { |
5c72ca61 |
354 | struct dss_key *dss = (struct dss_key *) key; |
355 | int xlen, bloblen; |
356 | int i; |
357 | unsigned char *blob, *p; |
5c72ca61 |
358 | |
359 | xlen = (bignum_bitcount(dss->x) + 8) / 8; |
360 | |
361 | /* |
7bedb13c |
362 | * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen. |
5c72ca61 |
363 | */ |
7bedb13c |
364 | bloblen = 4 + xlen; |
5c72ca61 |
365 | blob = smalloc(bloblen); |
366 | p = blob; |
367 | PUT_32BIT(p, xlen); |
368 | p += 4; |
369 | for (i = xlen; i--;) |
370 | *p++ = bignum_byte(dss->x, i); |
5c72ca61 |
371 | assert(p == blob + bloblen); |
372 | *len = bloblen; |
373 | return blob; |
65a22376 |
374 | } |
375 | |
376 | static void *dss_createkey(unsigned char *pub_blob, int pub_len, |
32874aea |
377 | unsigned char *priv_blob, int priv_len) |
378 | { |
5c72ca61 |
379 | struct dss_key *dss; |
380 | char *pb = (char *) priv_blob; |
381 | char *hash; |
382 | int hashlen; |
383 | SHA_State s; |
384 | unsigned char digest[20]; |
385 | Bignum ytest; |
386 | |
387 | dss = dss_newkey((char *) pub_blob, pub_len); |
388 | dss->x = getmp(&pb, &priv_len); |
5c72ca61 |
389 | |
390 | /* |
7bedb13c |
391 | * Check the obsolete hash in the old DSS key format. |
5c72ca61 |
392 | */ |
7bedb13c |
393 | hashlen = -1; |
394 | getstring(&pb, &priv_len, &hash, &hashlen); |
395 | if (hashlen == 20) { |
396 | SHA_Init(&s); |
397 | sha_mpint(&s, dss->p); |
398 | sha_mpint(&s, dss->q); |
399 | sha_mpint(&s, dss->g); |
400 | SHA_Final(&s, digest); |
401 | if (0 != memcmp(hash, digest, 20)) { |
402 | dss_freekey(dss); |
403 | return NULL; |
404 | } |
5c72ca61 |
405 | } |
406 | |
407 | /* |
408 | * Now ensure g^x mod p really is y. |
409 | */ |
410 | ytest = modpow(dss->g, dss->x, dss->p); |
411 | if (0 != bignum_cmp(ytest, dss->y)) { |
412 | dss_freekey(dss); |
413 | return NULL; |
414 | } |
415 | freebn(ytest); |
416 | |
417 | return dss; |
65a22376 |
418 | } |
419 | |
32874aea |
420 | static void *dss_openssh_createkey(unsigned char **blob, int *len) |
421 | { |
5c72ca61 |
422 | char **b = (char **) blob; |
423 | struct dss_key *dss; |
424 | |
425 | dss = smalloc(sizeof(struct dss_key)); |
426 | if (!dss) |
427 | return NULL; |
428 | |
429 | dss->p = getmp(b, len); |
430 | dss->q = getmp(b, len); |
431 | dss->g = getmp(b, len); |
432 | dss->y = getmp(b, len); |
433 | dss->x = getmp(b, len); |
434 | |
435 | if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x) { |
436 | sfree(dss->p); |
437 | sfree(dss->q); |
438 | sfree(dss->g); |
439 | sfree(dss->y); |
440 | sfree(dss->x); |
441 | sfree(dss); |
442 | return NULL; |
443 | } |
444 | |
445 | return dss; |
45cebe79 |
446 | } |
447 | |
32874aea |
448 | static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len) |
449 | { |
5c72ca61 |
450 | struct dss_key *dss = (struct dss_key *) key; |
451 | int bloblen, i; |
452 | |
453 | bloblen = |
454 | ssh2_bignum_length(dss->p) + |
455 | ssh2_bignum_length(dss->q) + |
456 | ssh2_bignum_length(dss->g) + |
457 | ssh2_bignum_length(dss->y) + |
458 | ssh2_bignum_length(dss->x); |
459 | |
460 | if (bloblen > len) |
461 | return bloblen; |
462 | |
463 | bloblen = 0; |
464 | #define ENC(x) \ |
465 | PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \ |
466 | for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i); |
467 | ENC(dss->p); |
468 | ENC(dss->q); |
469 | ENC(dss->g); |
470 | ENC(dss->y); |
471 | ENC(dss->x); |
472 | |
473 | return bloblen; |
ddecd643 |
474 | } |
475 | |
32874aea |
476 | unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen) |
477 | { |
5c72ca61 |
478 | /* |
479 | * The basic DSS signing algorithm is: |
480 | * |
481 | * - invent a random k between 1 and q-1 (exclusive). |
482 | * - Compute r = (g^k mod p) mod q. |
483 | * - Compute s = k^-1 * (hash + x*r) mod q. |
484 | * |
485 | * This has the dangerous properties that: |
486 | * |
487 | * - if an attacker in possession of the public key _and_ the |
488 | * signature (for example, the host you just authenticated |
489 | * to) can guess your k, he can reverse the computation of s |
490 | * and work out x = r^-1 * (s*k - hash) mod q. That is, he |
491 | * can deduce the private half of your key, and masquerade |
492 | * as you for as long as the key is still valid. |
493 | * |
494 | * - since r is a function purely of k and the public key, if |
495 | * the attacker only has a _range of possibilities_ for k |
496 | * it's easy for him to work through them all and check each |
497 | * one against r; he'll never be unsure of whether he's got |
498 | * the right one. |
499 | * |
500 | * - if you ever sign two different hashes with the same k, it |
501 | * will be immediately obvious because the two signatures |
502 | * will have the same r, and moreover an attacker in |
503 | * possession of both signatures (and the public key of |
504 | * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q, |
505 | * and from there deduce x as before. |
506 | * |
507 | * - the Bleichenbacher attack on DSA makes use of methods of |
508 | * generating k which are significantly non-uniformly |
509 | * distributed; in particular, generating a 160-bit random |
510 | * number and reducing it mod q is right out. |
511 | * |
512 | * For this reason we must be pretty careful about how we |
513 | * generate our k. Since this code runs on Windows, with no |
514 | * particularly good system entropy sources, we can't trust our |
515 | * RNG itself to produce properly unpredictable data. Hence, we |
516 | * use a totally different scheme instead. |
517 | * |
518 | * What we do is to take a SHA-512 (_big_) hash of the private |
519 | * key x, and then feed this into another SHA-512 hash that |
520 | * also includes the message hash being signed. That is: |
521 | * |
522 | * proto_k = SHA512 ( SHA512(x) || SHA160(message) ) |
523 | * |
524 | * This number is 512 bits long, so reducing it mod q won't be |
525 | * noticeably non-uniform. So |
526 | * |
527 | * k = proto_k mod q |
528 | * |
529 | * This has the interesting property that it's _deterministic_: |
530 | * signing the same hash twice with the same key yields the |
531 | * same signature. |
532 | * |
5ced2a02 |
533 | * Despite this determinism, it's still not predictable to an |
534 | * attacker, because in order to repeat the SHA-512 |
535 | * construction that created it, the attacker would have to |
536 | * know the private key value x - and by assumption he doesn't, |
537 | * because if he knew that he wouldn't be attacking k! |
538 | * |
539 | * (This trick doesn't, _per se_, protect against reuse of k. |
540 | * Reuse of k is left to chance; all it does is prevent |
541 | * _excessively high_ chances of reuse of k due to entropy |
542 | * problems.) |
5c72ca61 |
543 | * |
544 | * Thanks to Colin Plumb for the general idea of using x to |
545 | * ensure k is hard to guess, and to the Cambridge University |
546 | * Computer Security Group for helping to argue out all the |
547 | * fine details. |
548 | */ |
549 | struct dss_key *dss = (struct dss_key *) key; |
550 | SHA512_State ss; |
551 | unsigned char digest[20], digest512[64]; |
552 | Bignum proto_k, k, gkp, hash, kinv, hxr, r, s; |
553 | unsigned char *bytes; |
554 | int nbytes, i; |
555 | |
556 | SHA_Simple(data, datalen, digest); |
557 | |
558 | /* |
559 | * Hash some identifying text plus x. |
560 | */ |
561 | SHA512_Init(&ss); |
562 | SHA512_Bytes(&ss, "DSA deterministic k generator", 30); |
563 | sha512_mpint(&ss, dss->x); |
564 | SHA512_Final(&ss, digest512); |
565 | |
566 | /* |
567 | * Now hash that digest plus the message hash. |
568 | */ |
569 | SHA512_Init(&ss); |
570 | SHA512_Bytes(&ss, digest512, sizeof(digest512)); |
571 | SHA512_Bytes(&ss, digest, sizeof(digest)); |
572 | SHA512_Final(&ss, digest512); |
573 | |
574 | memset(&ss, 0, sizeof(ss)); |
575 | |
576 | /* |
577 | * Now convert the result into a bignum, and reduce it mod q. |
578 | */ |
579 | proto_k = bignum_from_bytes(digest512, 64); |
580 | k = bigmod(proto_k, dss->q); |
581 | freebn(proto_k); |
582 | |
583 | memset(digest512, 0, sizeof(digest512)); |
584 | |
585 | /* |
586 | * Now we have k, so just go ahead and compute the signature. |
587 | */ |
588 | gkp = modpow(dss->g, k, dss->p); /* g^k mod p */ |
589 | r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */ |
590 | freebn(gkp); |
591 | |
592 | hash = bignum_from_bytes(digest, 20); |
593 | kinv = modinv(k, dss->q); /* k^-1 mod q */ |
594 | hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */ |
595 | s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */ |
596 | freebn(hxr); |
597 | freebn(kinv); |
598 | freebn(hash); |
599 | |
600 | /* |
601 | * Signature blob is |
602 | * |
603 | * string "ssh-dss" |
604 | * string two 20-byte numbers r and s, end to end |
605 | * |
606 | * i.e. 4+7 + 4+40 bytes. |
607 | */ |
608 | nbytes = 4 + 7 + 4 + 40; |
609 | bytes = smalloc(nbytes); |
610 | PUT_32BIT(bytes, 7); |
611 | memcpy(bytes + 4, "ssh-dss", 7); |
612 | PUT_32BIT(bytes + 4 + 7, 40); |
613 | for (i = 0; i < 20; i++) { |
614 | bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i); |
615 | bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i); |
616 | } |
617 | freebn(r); |
618 | freebn(s); |
619 | |
620 | *siglen = nbytes; |
621 | return bytes; |
e055a386 |
622 | } |
623 | |
65a22376 |
624 | const struct ssh_signkey ssh_dss = { |
e055a386 |
625 | dss_newkey, |
626 | dss_freekey, |
7cca0d81 |
627 | dss_fmtkey, |
65a22376 |
628 | dss_public_blob, |
629 | dss_private_blob, |
630 | dss_createkey, |
45cebe79 |
631 | dss_openssh_createkey, |
ddecd643 |
632 | dss_openssh_fmtkey, |
d5859615 |
633 | dss_fingerprint, |
7cca0d81 |
634 | dss_verifysig, |
e055a386 |
635 | dss_sign, |
d5859615 |
636 | "ssh-dss", |
637 | "dss" |
e5574168 |
638 | }; |