]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshrsa.c
Run entire source base through GNU indent to tidy up the varying
[PuTTY.git] / sshrsa.c
1 /*
2  * RSA implementation just sufficient for ssh client-side
3  * initialisation step
4  *
5  * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12
13 #include "ssh.h"
14
15
16 int makekey(unsigned char *data, struct RSAKey *result,
17             unsigned char **keystr, int order)
18 {
19     unsigned char *p = data;
20     int i;
21
22     if (result) {
23         result->bits = 0;
24         for (i = 0; i < 4; i++)
25             result->bits = (result->bits << 8) + *p++;
26     } else
27         p += 4;
28
29     /*
30      * order=0 means exponent then modulus (the keys sent by the
31      * server). order=1 means modulus then exponent (the keys
32      * stored in a keyfile).
33      */
34
35     if (order == 0)
36         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
37     if (result)
38         result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
39     if (keystr)
40         *keystr = p + 2;
41     p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
42     if (order == 1)
43         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
44
45     return p - data;
46 }
47
48 int makeprivate(unsigned char *data, struct RSAKey *result)
49 {
50     return ssh1_read_bignum(data, &result->private_exponent);
51 }
52
53 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
54 {
55     Bignum b1, b2;
56     int i;
57     unsigned char *p;
58
59     memmove(data + key->bytes - length, data, length);
60     data[0] = 0;
61     data[1] = 2;
62
63     for (i = 2; i < key->bytes - length - 1; i++) {
64         do {
65             data[i] = random_byte();
66         } while (data[i] == 0);
67     }
68     data[key->bytes - length - 1] = 0;
69
70     b1 = bignum_from_bytes(data, key->bytes);
71
72     b2 = modpow(b1, key->exponent, key->modulus);
73
74     p = data;
75     for (i = key->bytes; i--;) {
76         *p++ = bignum_byte(b2, i);
77     }
78
79     freebn(b1);
80     freebn(b2);
81 }
82
83 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
84 {
85     Bignum ret;
86     ret = modpow(input, key->private_exponent, key->modulus);
87     return ret;
88 }
89
90 int rsastr_len(struct RSAKey *key)
91 {
92     Bignum md, ex;
93     int mdlen, exlen;
94
95     md = key->modulus;
96     ex = key->exponent;
97     mdlen = (bignum_bitcount(md) + 15) / 16;
98     exlen = (bignum_bitcount(ex) + 15) / 16;
99     return 4 * (mdlen + exlen) + 20;
100 }
101
102 void rsastr_fmt(char *str, struct RSAKey *key)
103 {
104     Bignum md, ex;
105     int len = 0, i, nibbles;
106     static const char hex[] = "0123456789abcdef";
107
108     md = key->modulus;
109     ex = key->exponent;
110
111     len += sprintf(str + len, "0x");
112
113     nibbles = (3 + bignum_bitcount(ex)) / 4;
114     if (nibbles < 1)
115         nibbles = 1;
116     for (i = nibbles; i--;)
117         str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
118
119     len += sprintf(str + len, ",0x");
120
121     nibbles = (3 + bignum_bitcount(md)) / 4;
122     if (nibbles < 1)
123         nibbles = 1;
124     for (i = nibbles; i--;)
125         str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
126
127     str[len] = '\0';
128 }
129
130 /*
131  * Generate a fingerprint string for the key. Compatible with the
132  * OpenSSH fingerprint code.
133  */
134 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
135 {
136     struct MD5Context md5c;
137     unsigned char digest[16];
138     char buffer[16 * 3 + 40];
139     int numlen, slen, i;
140
141     MD5Init(&md5c);
142     numlen = ssh1_bignum_length(key->modulus) - 2;
143     for (i = numlen; i--;) {
144         unsigned char c = bignum_byte(key->modulus, i);
145         MD5Update(&md5c, &c, 1);
146     }
147     numlen = ssh1_bignum_length(key->exponent) - 2;
148     for (i = numlen; i--;) {
149         unsigned char c = bignum_byte(key->exponent, i);
150         MD5Update(&md5c, &c, 1);
151     }
152     MD5Final(digest, &md5c);
153
154     sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
155     for (i = 0; i < 16; i++)
156         sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
157                 digest[i]);
158     strncpy(str, buffer, len);
159     str[len - 1] = '\0';
160     slen = strlen(str);
161     if (key->comment && slen < len - 1) {
162         str[slen] = ' ';
163         strncpy(str + slen + 1, key->comment, len - slen - 1);
164         str[len - 1] = '\0';
165     }
166 }
167
168 /*
169  * Verify that the public data in an RSA key matches the private
170  * data. We also check the private data itself: we ensure that p >
171  * q and that iqmp really is the inverse of q mod p.
172  */
173 int rsa_verify(struct RSAKey *key)
174 {
175     Bignum n, ed, pm1, qm1;
176     int cmp;
177
178     /* n must equal pq. */
179     n = bigmul(key->p, key->q);
180     cmp = bignum_cmp(n, key->modulus);
181     freebn(n);
182     if (cmp != 0)
183         return 0;
184
185     /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
186     pm1 = copybn(key->p);
187     decbn(pm1);
188     ed = modmul(key->exponent, key->private_exponent, pm1);
189     cmp = bignum_cmp(ed, One);
190     sfree(ed);
191     if (cmp != 0)
192         return 0;
193
194     qm1 = copybn(key->q);
195     decbn(qm1);
196     ed = modmul(key->exponent, key->private_exponent, qm1);
197     cmp = bignum_cmp(ed, One);
198     sfree(ed);
199     if (cmp != 0)
200         return 0;
201
202     /*
203      * Ensure p > q.
204      */
205     if (bignum_cmp(key->p, key->q) <= 0)
206         return 0;
207
208     /*
209      * Ensure iqmp * q is congruent to 1, modulo p.
210      */
211     n = modmul(key->iqmp, key->q, key->p);
212     cmp = bignum_cmp(n, One);
213     sfree(n);
214     if (cmp != 0)
215         return 0;
216
217     return 1;
218 }
219
220 void freersakey(struct RSAKey *key)
221 {
222     if (key->modulus)
223         freebn(key->modulus);
224     if (key->exponent)
225         freebn(key->exponent);
226     if (key->private_exponent)
227         freebn(key->private_exponent);
228     if (key->comment)
229         sfree(key->comment);
230 }
231
232 /* ----------------------------------------------------------------------
233  * Implementation of the ssh-rsa signing key type. 
234  */
235
236 #define GET_32BIT(cp) \
237     (((unsigned long)(unsigned char)(cp)[0] << 24) | \
238     ((unsigned long)(unsigned char)(cp)[1] << 16) | \
239     ((unsigned long)(unsigned char)(cp)[2] << 8) | \
240     ((unsigned long)(unsigned char)(cp)[3]))
241
242 #define PUT_32BIT(cp, value) { \
243     (cp)[0] = (unsigned char)((value) >> 24); \
244     (cp)[1] = (unsigned char)((value) >> 16); \
245     (cp)[2] = (unsigned char)((value) >> 8); \
246     (cp)[3] = (unsigned char)(value); }
247
248 static void getstring(char **data, int *datalen, char **p, int *length)
249 {
250     *p = NULL;
251     if (*datalen < 4)
252         return;
253     *length = GET_32BIT(*data);
254     *datalen -= 4;
255     *data += 4;
256     if (*datalen < *length)
257         return;
258     *p = *data;
259     *data += *length;
260     *datalen -= *length;
261 }
262 static Bignum getmp(char **data, int *datalen)
263 {
264     char *p;
265     int length;
266     Bignum b;
267
268     getstring(data, datalen, &p, &length);
269     if (!p)
270         return NULL;
271     b = bignum_from_bytes(p, length);
272     return b;
273 }
274
275 static void *rsa2_newkey(char *data, int len)
276 {
277     char *p;
278     int slen;
279     struct RSAKey *rsa;
280
281     rsa = smalloc(sizeof(struct RSAKey));
282     if (!rsa)
283         return NULL;
284     getstring(&data, &len, &p, &slen);
285
286     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
287         sfree(rsa);
288         return NULL;
289     }
290     rsa->exponent = getmp(&data, &len);
291     rsa->modulus = getmp(&data, &len);
292     rsa->private_exponent = NULL;
293     rsa->comment = NULL;
294
295     return rsa;
296 }
297
298 static void rsa2_freekey(void *key)
299 {
300     struct RSAKey *rsa = (struct RSAKey *) key;
301     freersakey(rsa);
302     sfree(rsa);
303 }
304
305 static char *rsa2_fmtkey(void *key)
306 {
307     struct RSAKey *rsa = (struct RSAKey *) key;
308     char *p;
309     int len;
310
311     len = rsastr_len(rsa);
312     p = smalloc(len);
313     rsastr_fmt(p, rsa);
314     return p;
315 }
316
317 static unsigned char *rsa2_public_blob(void *key, int *len)
318 {
319     struct RSAKey *rsa = (struct RSAKey *) key;
320     int elen, mlen, bloblen;
321     int i;
322     unsigned char *blob, *p;
323
324     elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
325     mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
326
327     /*
328      * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
329      * (three length fields, 12+7=19).
330      */
331     bloblen = 19 + elen + mlen;
332     blob = smalloc(bloblen);
333     p = blob;
334     PUT_32BIT(p, 7);
335     p += 4;
336     memcpy(p, "ssh-rsa", 7);
337     p += 7;
338     PUT_32BIT(p, elen);
339     p += 4;
340     for (i = elen; i--;)
341         *p++ = bignum_byte(rsa->exponent, i);
342     PUT_32BIT(p, mlen);
343     p += 4;
344     for (i = mlen; i--;)
345         *p++ = bignum_byte(rsa->modulus, i);
346     assert(p == blob + bloblen);
347     *len = bloblen;
348     return blob;
349 }
350
351 static unsigned char *rsa2_private_blob(void *key, int *len)
352 {
353     struct RSAKey *rsa = (struct RSAKey *) key;
354     int dlen, plen, qlen, ulen, bloblen;
355     int i;
356     unsigned char *blob, *p;
357
358     dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
359     plen = (bignum_bitcount(rsa->p) + 8) / 8;
360     qlen = (bignum_bitcount(rsa->q) + 8) / 8;
361     ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
362
363     /*
364      * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
365      * sum of lengths.
366      */
367     bloblen = 16 + dlen + plen + qlen + ulen;
368     blob = smalloc(bloblen);
369     p = blob;
370     PUT_32BIT(p, dlen);
371     p += 4;
372     for (i = dlen; i--;)
373         *p++ = bignum_byte(rsa->private_exponent, i);
374     PUT_32BIT(p, plen);
375     p += 4;
376     for (i = plen; i--;)
377         *p++ = bignum_byte(rsa->p, i);
378     PUT_32BIT(p, qlen);
379     p += 4;
380     for (i = qlen; i--;)
381         *p++ = bignum_byte(rsa->q, i);
382     PUT_32BIT(p, ulen);
383     p += 4;
384     for (i = ulen; i--;)
385         *p++ = bignum_byte(rsa->iqmp, i);
386     assert(p == blob + bloblen);
387     *len = bloblen;
388     return blob;
389 }
390
391 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
392                             unsigned char *priv_blob, int priv_len)
393 {
394     struct RSAKey *rsa;
395     char *pb = (char *) priv_blob;
396
397     rsa = rsa2_newkey((char *) pub_blob, pub_len);
398     rsa->private_exponent = getmp(&pb, &priv_len);
399     rsa->p = getmp(&pb, &priv_len);
400     rsa->q = getmp(&pb, &priv_len);
401     rsa->iqmp = getmp(&pb, &priv_len);
402
403     if (!rsa_verify(rsa)) {
404         rsa2_freekey(rsa);
405         return NULL;
406     }
407
408     return rsa;
409 }
410
411 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
412 {
413     char **b = (char **) blob;
414     struct RSAKey *rsa;
415
416     rsa = smalloc(sizeof(struct RSAKey));
417     if (!rsa)
418         return NULL;
419     rsa->comment = NULL;
420
421     rsa->modulus = getmp(b, len);
422     rsa->exponent = getmp(b, len);
423     rsa->private_exponent = getmp(b, len);
424     rsa->iqmp = getmp(b, len);
425     rsa->p = getmp(b, len);
426     rsa->q = getmp(b, len);
427
428     if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
429         !rsa->iqmp || !rsa->p || !rsa->q) {
430         sfree(rsa->modulus);
431         sfree(rsa->exponent);
432         sfree(rsa->private_exponent);
433         sfree(rsa->iqmp);
434         sfree(rsa->p);
435         sfree(rsa->q);
436         sfree(rsa);
437         return NULL;
438     }
439
440     return rsa;
441 }
442
443 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
444 {
445     struct RSAKey *rsa = (struct RSAKey *) key;
446     int bloblen, i;
447
448     bloblen =
449         ssh2_bignum_length(rsa->modulus) +
450         ssh2_bignum_length(rsa->exponent) +
451         ssh2_bignum_length(rsa->private_exponent) +
452         ssh2_bignum_length(rsa->iqmp) +
453         ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
454
455     if (bloblen > len)
456         return bloblen;
457
458     bloblen = 0;
459 #define ENC(x) \
460     PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
461     for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
462     ENC(rsa->modulus);
463     ENC(rsa->exponent);
464     ENC(rsa->private_exponent);
465     ENC(rsa->iqmp);
466     ENC(rsa->p);
467     ENC(rsa->q);
468
469     return bloblen;
470 }
471
472 static char *rsa2_fingerprint(void *key)
473 {
474     struct RSAKey *rsa = (struct RSAKey *) key;
475     struct MD5Context md5c;
476     unsigned char digest[16], lenbuf[4];
477     char buffer[16 * 3 + 40];
478     char *ret;
479     int numlen, i;
480
481     MD5Init(&md5c);
482     MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
483
484 #define ADD_BIGNUM(bignum) \
485     numlen = (bignum_bitcount(bignum)+8)/8; \
486     PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
487     for (i = numlen; i-- ;) { \
488         unsigned char c = bignum_byte(bignum, i); \
489         MD5Update(&md5c, &c, 1); \
490     }
491     ADD_BIGNUM(rsa->exponent);
492     ADD_BIGNUM(rsa->modulus);
493 #undef ADD_BIGNUM
494
495     MD5Final(digest, &md5c);
496
497     sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
498     for (i = 0; i < 16; i++)
499         sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
500                 digest[i]);
501     ret = smalloc(strlen(buffer) + 1);
502     if (ret)
503         strcpy(ret, buffer);
504     return ret;
505 }
506
507 /*
508  * This is the magic ASN.1/DER prefix that goes in the decoded
509  * signature, between the string of FFs and the actual SHA hash
510  * value. The meaning of it is:
511  * 
512  * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
513  * 
514  * 30 21 -- a constructed SEQUENCE of length 0x21
515  *    30 09 -- a constructed sub-SEQUENCE of length 9
516  *       06 05 -- an object identifier, length 5
517  *          2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
518  *                            (the 1,3 comes from 0x2B = 43 = 40*1+3)
519  *       05 00 -- NULL
520  *    04 14 -- a primitive OCTET STRING of length 0x14
521  *       [0x14 bytes of hash data follows]
522  * 
523  * The object id in the middle there is listed as `id-sha1' in
524  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
525  * ASN module for PKCS #1) and its expanded form is as follows:
526  * 
527  * id-sha1                OBJECT IDENTIFIER ::= {
528  *    iso(1) identified-organization(3) oiw(14) secsig(3)
529  *    algorithms(2) 26 }
530  */
531 static unsigned char asn1_weird_stuff[] = {
532     0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
533     0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
534 };
535
536 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
537
538 static int rsa2_verifysig(void *key, char *sig, int siglen,
539                           char *data, int datalen)
540 {
541     struct RSAKey *rsa = (struct RSAKey *) key;
542     Bignum in, out;
543     char *p;
544     int slen;
545     int bytes, i, j, ret;
546     unsigned char hash[20];
547
548     getstring(&sig, &siglen, &p, &slen);
549     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
550         return 0;
551     }
552     in = getmp(&sig, &siglen);
553     out = modpow(in, rsa->exponent, rsa->modulus);
554     freebn(in);
555
556     ret = 1;
557
558     bytes = bignum_bitcount(rsa->modulus) / 8;
559     /* Top (partial) byte should be zero. */
560     if (bignum_byte(out, bytes - 1) != 0)
561         ret = 0;
562     /* First whole byte should be 1. */
563     if (bignum_byte(out, bytes - 2) != 1)
564         ret = 0;
565     /* Most of the rest should be FF. */
566     for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
567         if (bignum_byte(out, i) != 0xFF)
568             ret = 0;
569     }
570     /* Then we expect to see the asn1_weird_stuff. */
571     for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
572         if (bignum_byte(out, i) != asn1_weird_stuff[j])
573             ret = 0;
574     }
575     /* Finally, we expect to see the SHA-1 hash of the signed data. */
576     SHA_Simple(data, datalen, hash);
577     for (i = 19, j = 0; i >= 0; i--, j++) {
578         if (bignum_byte(out, i) != hash[j])
579             ret = 0;
580     }
581
582     return ret;
583 }
584
585 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen)
586 {
587     struct RSAKey *rsa = (struct RSAKey *) key;
588     unsigned char *bytes;
589     int nbytes;
590     unsigned char hash[20];
591     Bignum in, out;
592     int i, j;
593
594     SHA_Simple(data, datalen, hash);
595
596     nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
597     bytes = smalloc(nbytes);
598
599     bytes[0] = 1;
600     for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
601         bytes[i] = 0xFF;
602     for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
603         bytes[i] = asn1_weird_stuff[j];
604     for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
605         bytes[i] = hash[j];
606
607     in = bignum_from_bytes(bytes, nbytes);
608     sfree(bytes);
609
610     out = modpow(in, rsa->private_exponent, rsa->modulus);
611     freebn(in);
612
613     nbytes = (bignum_bitcount(out) + 7) / 8;
614     bytes = smalloc(4 + 7 + 4 + nbytes);
615     PUT_32BIT(bytes, 7);
616     memcpy(bytes + 4, "ssh-rsa", 7);
617     PUT_32BIT(bytes + 4 + 7, nbytes);
618     for (i = 0; i < nbytes; i++)
619         bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
620     freebn(out);
621
622     *siglen = 4 + 7 + 4 + nbytes;
623     return bytes;
624 }
625
626 const struct ssh_signkey ssh_rsa = {
627     rsa2_newkey,
628     rsa2_freekey,
629     rsa2_fmtkey,
630     rsa2_public_blob,
631     rsa2_private_blob,
632     rsa2_createkey,
633     rsa2_openssh_createkey,
634     rsa2_openssh_fmtkey,
635     rsa2_fingerprint,
636     rsa2_verifysig,
637     rsa2_sign,
638     "ssh-rsa",
639     "rsa2"
640 };