]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshrsa.c
Fix various trivial compiler warnings
[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     unsigned char *p = data;
19     int i;
20
21     if (result) {
22         result->bits = 0;
23         for (i=0; i<4; i++)
24             result->bits = (result->bits << 8) + *p++;
25     } else
26         p += 4;
27
28     /*
29      * order=0 means exponent then modulus (the keys sent by the
30      * server). order=1 means modulus then exponent (the keys
31      * stored in a keyfile).
32      */
33
34     if (order == 0)
35         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
36     if (result)
37         result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
38     if (keystr) *keystr = p+2;
39     p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
40     if (order == 1)
41         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
42
43     return p - data;
44 }
45
46 int makeprivate(unsigned char *data, struct RSAKey *result) {
47     return ssh1_read_bignum(data, &result->private_exponent);
48 }
49
50 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
51     Bignum b1, b2;
52     int i;
53     unsigned char *p;
54
55     memmove(data+key->bytes-length, data, length);
56     data[0] = 0;
57     data[1] = 2;
58
59     for (i = 2; i < key->bytes-length-1; i++) {
60         do {
61             data[i] = random_byte();
62         } while (data[i] == 0);
63     }
64     data[key->bytes-length-1] = 0;
65
66     b1 = bignum_from_bytes(data, key->bytes);
67
68     b2 = modpow(b1, key->exponent, key->modulus);
69
70     p = data;
71     for (i=key->bytes; i-- ;) {
72         *p++ = bignum_byte(b2, i);
73     }
74
75     freebn(b1);
76     freebn(b2);
77 }
78
79 Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
80     Bignum ret;
81     ret = modpow(input, key->private_exponent, key->modulus);
82     return ret;
83 }
84
85 int rsastr_len(struct RSAKey *key) {
86     Bignum md, ex;
87     int mdlen, exlen;
88
89     md = key->modulus;
90     ex = key->exponent;
91     mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
92     exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
93     return 4 * (mdlen+exlen) + 20;
94 }
95
96 void rsastr_fmt(char *str, struct RSAKey *key) {
97     Bignum md, ex;
98     int len = 0, i, nibbles;
99     static const char hex[] = "0123456789abcdef";
100
101     md = key->modulus;
102     ex = key->exponent;
103
104     len += sprintf(str+len, "0x");
105
106     nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
107     for (i=nibbles; i-- ;)
108         str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
109
110     len += sprintf(str+len, ",0x");
111
112     nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
113     for (i=nibbles; i-- ;)
114         str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
115
116     str[len] = '\0';
117 }
118
119 /*
120  * Generate a fingerprint string for the key. Compatible with the
121  * OpenSSH fingerprint code.
122  */
123 void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
124     struct MD5Context md5c;
125     unsigned char digest[16];
126     char buffer[16*3+40];
127     int numlen, slen, i;
128
129     MD5Init(&md5c);
130     numlen = ssh1_bignum_length(key->modulus) - 2;
131     for (i = numlen; i-- ;) {
132         unsigned char c = bignum_byte(key->modulus, i);
133         MD5Update(&md5c, &c, 1);
134     }
135     numlen = ssh1_bignum_length(key->exponent) - 2;
136     for (i = numlen; i-- ;) {
137         unsigned char c = bignum_byte(key->exponent, i);
138         MD5Update(&md5c, &c, 1);
139     }
140     MD5Final(digest, &md5c);
141
142     sprintf(buffer, "%d ", ssh1_bignum_bitcount(key->modulus));
143     for (i = 0; i < 16; i++)
144         sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
145     strncpy(str, buffer, len); str[len-1] = '\0';
146     slen = strlen(str);
147     if (key->comment && slen < len-1) {
148         str[slen] = ' ';
149         strncpy(str+slen+1, key->comment, len-slen-1);
150         str[len-1] = '\0';
151     }
152 }
153
154 void freersakey(struct RSAKey *key) {
155     if (key->modulus) freebn(key->modulus);
156     if (key->exponent) freebn(key->exponent);
157     if (key->private_exponent) freebn(key->private_exponent);
158     if (key->comment) sfree(key->comment);
159 }
160
161 /* ----------------------------------------------------------------------
162  * Implementation of the ssh-rsa signing key type. 
163  */
164
165 #define GET_32BIT(cp) \
166     (((unsigned long)(unsigned char)(cp)[0] << 24) | \
167     ((unsigned long)(unsigned char)(cp)[1] << 16) | \
168     ((unsigned long)(unsigned char)(cp)[2] << 8) | \
169     ((unsigned long)(unsigned char)(cp)[3]))
170
171 #define PUT_32BIT(cp, value) { \
172     (cp)[0] = (unsigned char)((value) >> 24); \
173     (cp)[1] = (unsigned char)((value) >> 16); \
174     (cp)[2] = (unsigned char)((value) >> 8); \
175     (cp)[3] = (unsigned char)(value); }
176
177 static void getstring(char **data, int *datalen, char **p, int *length) {
178     *p = NULL;
179     if (*datalen < 4)
180         return;
181     *length = GET_32BIT(*data);
182     *datalen -= 4; *data += 4;
183     if (*datalen < *length)
184         return;
185     *p = *data;
186     *data += *length; *datalen -= *length;
187 }
188 static Bignum getmp(char **data, int *datalen) {
189     char *p;
190     int length;
191     Bignum b;
192
193     getstring(data, datalen, &p, &length);
194     if (!p)
195         return NULL;
196     b = bignum_from_bytes(p, length);
197     return b;
198 }
199
200 static void *rsa2_newkey(char *data, int len) {
201     char *p;
202     int slen;
203     struct RSAKey *rsa;
204
205     rsa = smalloc(sizeof(struct RSAKey));
206     if (!rsa) return NULL;
207     getstring(&data, &len, &p, &slen);
208
209     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
210         sfree(rsa);
211         return NULL;
212     }
213     rsa->exponent = getmp(&data, &len);
214     rsa->modulus = getmp(&data, &len);
215     rsa->private_exponent = NULL;
216     rsa->comment = NULL;
217
218     return rsa;
219 }
220
221 static void rsa2_freekey(void *key) {
222     struct RSAKey *rsa = (struct RSAKey *)key;
223     freersakey(rsa);
224     sfree(rsa);
225 }
226
227 static char *rsa2_fmtkey(void *key) {
228     struct RSAKey *rsa = (struct RSAKey *)key;
229     char *p;
230     int len;
231     
232     len = rsastr_len(rsa);
233     p = smalloc(len);
234     rsastr_fmt(p, rsa);
235     return p;
236 }
237
238 static unsigned char *rsa2_public_blob(void *key, int *len) {
239     struct RSAKey *rsa = (struct RSAKey *)key;
240     int elen, mlen, bloblen;
241     int i;
242     unsigned char *blob, *p;
243
244     elen = (ssh1_bignum_bitcount(rsa->exponent)+8)/8;
245     mlen = (ssh1_bignum_bitcount(rsa->modulus)+8)/8;
246
247     /*
248      * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
249      * (three length fields, 12+7=19).
250      */
251     bloblen = 19+elen+mlen;
252     blob = smalloc(bloblen);
253     p = blob;
254     PUT_32BIT(p, 7); p += 4;
255     memcpy(p, "ssh-rsa", 7); p += 7;
256     PUT_32BIT(p, elen); p += 4;
257     for (i = elen; i-- ;) *p++ = bignum_byte(rsa->exponent, i);
258     PUT_32BIT(p, mlen); p += 4;
259     for (i = mlen; i-- ;) *p++ = bignum_byte(rsa->modulus, i);
260     assert(p == blob + bloblen);
261     *len = bloblen;
262     return blob;
263 }
264
265 static unsigned char *rsa2_private_blob(void *key, int *len) {
266     struct RSAKey *rsa = (struct RSAKey *)key;
267     int dlen, plen, qlen, ulen, bloblen;
268     int i;
269     unsigned char *blob, *p;
270
271     dlen = (ssh1_bignum_bitcount(rsa->private_exponent)+8)/8;
272     plen = (ssh1_bignum_bitcount(rsa->p)+8)/8;
273     qlen = (ssh1_bignum_bitcount(rsa->q)+8)/8;
274     ulen = (ssh1_bignum_bitcount(rsa->iqmp)+8)/8;
275
276     /*
277      * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
278      * sum of lengths.
279      */
280     bloblen = 16+dlen+plen+qlen+ulen;
281     blob = smalloc(bloblen);
282     p = blob;
283     PUT_32BIT(p, dlen); p += 4;
284     for (i = dlen; i-- ;) *p++ = bignum_byte(rsa->private_exponent, i);
285     PUT_32BIT(p, plen); p += 4;
286     for (i = plen; i-- ;) *p++ = bignum_byte(rsa->p, i);
287     PUT_32BIT(p, qlen); p += 4;
288     for (i = qlen; i-- ;) *p++ = bignum_byte(rsa->q, i);
289     PUT_32BIT(p, ulen); p += 4;
290     for (i = ulen; i-- ;) *p++ = bignum_byte(rsa->iqmp, i);
291     assert(p == blob + bloblen);
292     *len = bloblen;
293     return blob;
294 }
295
296 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
297                             unsigned char *priv_blob, int priv_len) {
298     struct RSAKey *rsa;
299     char *pb = (char *)priv_blob;
300     
301     rsa = rsa2_newkey((char *)pub_blob, pub_len);
302     rsa->private_exponent = getmp(&pb, &priv_len);
303     rsa->p = getmp(&pb, &priv_len);
304     rsa->q = getmp(&pb, &priv_len);
305     rsa->iqmp = getmp(&pb, &priv_len);
306
307     return rsa;
308 }
309
310 static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
311     char **b = (char **)blob;
312     struct RSAKey *rsa;
313
314     rsa = smalloc(sizeof(struct RSAKey));
315     if (!rsa) return NULL;
316     rsa->comment = NULL;
317
318     rsa->modulus = getmp(b, len);
319     rsa->exponent = getmp(b, len);
320     rsa->private_exponent = getmp(b, len);
321     rsa->iqmp = getmp(b, len);
322     rsa->p = getmp(b, len);
323     rsa->q = getmp(b, len);
324
325     if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
326         !rsa->iqmp || !rsa->p || !rsa->q) {
327         sfree(rsa->modulus);
328         sfree(rsa->exponent);
329         sfree(rsa->private_exponent);
330         sfree(rsa->iqmp);
331         sfree(rsa->p);
332         sfree(rsa->q);
333         sfree(rsa);
334         return NULL;
335     }
336
337     return rsa;
338 }
339
340 static char *rsa2_fingerprint(void *key) {
341     struct RSAKey *rsa = (struct RSAKey *)key;
342     struct MD5Context md5c;
343     unsigned char digest[16], lenbuf[4];
344     char buffer[16*3+40];
345     char *ret;
346     int numlen, i;
347
348     MD5Init(&md5c);
349     MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
350
351 #define ADD_BIGNUM(bignum) \
352     numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
353     PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
354     for (i = numlen; i-- ;) { \
355         unsigned char c = bignum_byte(bignum, i); \
356         MD5Update(&md5c, &c, 1); \
357     }
358     ADD_BIGNUM(rsa->exponent);
359     ADD_BIGNUM(rsa->modulus);
360 #undef ADD_BIGNUM
361
362     MD5Final(digest, &md5c);
363
364     sprintf(buffer, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa->modulus));
365     for (i = 0; i < 16; i++)
366         sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
367     ret = smalloc(strlen(buffer)+1);
368     if (ret)
369         strcpy(ret, buffer);
370     return ret;
371 }
372
373 /*
374  * This is the magic ASN.1/DER prefix that goes in the decoded
375  * signature, between the string of FFs and the actual SHA hash
376  * value. As closely as I can tell, the meaning of it is:
377  * 
378  * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
379  * 
380  * 30 21 -- a constructed SEQUENCE of length 0x21
381  *    30 09 -- a constructed sub-SEQUENCE of length 9
382  *       06 05 -- an object identifier, length 5
383  *          2B 0E 03 02 1A -- 
384  *       05 00 -- NULL
385  *    04 14 -- a primitive OCTET STRING of length 0x14
386  *       [0x14 bytes of hash data follows]
387  */
388 static unsigned char asn1_weird_stuff[] = {
389     0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
390     0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
391 };
392
393 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
394
395 static int rsa2_verifysig(void *key, char *sig, int siglen,
396                          char *data, int datalen) {
397     struct RSAKey *rsa = (struct RSAKey *)key;
398     Bignum in, out;
399     char *p;
400     int slen;
401     int bytes, i, j, ret;
402     unsigned char hash[20];
403
404     getstring(&sig, &siglen, &p, &slen);
405     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
406         return 0;
407     }
408     in = getmp(&sig, &siglen);
409     out = modpow(in, rsa->exponent, rsa->modulus);
410     freebn(in);
411
412     ret = 1;
413
414     bytes = ssh1_bignum_bitcount(rsa->modulus) / 8;
415     /* Top (partial) byte should be zero. */
416     if (bignum_byte(out, bytes-1) != 0)
417         ret = 0;
418     /* First whole byte should be 1. */
419     if (bignum_byte(out, bytes-2) != 1)
420         ret = 0;
421     /* Most of the rest should be FF. */
422     for (i = bytes-3; i >= 20 + ASN1_LEN; i--) {
423         if (bignum_byte(out, i) != 0xFF)
424             ret = 0;
425     }
426     /* Then we expect to see the asn1_weird_stuff. */
427     for (i = 20 + ASN1_LEN - 1, j=0; i >= 20; i--,j++) {
428         if (bignum_byte(out, i) != asn1_weird_stuff[j])
429             ret = 0;
430     }
431     /* Finally, we expect to see the SHA-1 hash of the signed data. */
432     SHA_Simple(data, datalen, hash);
433     for (i = 19, j=0; i >= 0; i--,j++) {
434         if (bignum_byte(out, i) != hash[j])
435             ret = 0;
436     }
437
438     return ret;
439 }
440
441 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) {
442     struct RSAKey *rsa = (struct RSAKey *)key;
443     unsigned char *bytes;
444     int nbytes;
445     unsigned char hash[20];
446     Bignum in, out;
447     int i, j;
448
449     SHA_Simple(data, datalen, hash);
450
451     nbytes = (ssh1_bignum_bitcount(rsa->modulus)-1) / 8;
452     bytes = smalloc(nbytes);
453
454     bytes[0] = 1;
455     for (i = 1; i < nbytes-20-ASN1_LEN; i++)
456         bytes[i] = 0xFF;
457     for (i = nbytes-20-ASN1_LEN, j=0; i < nbytes-20; i++,j++)
458         bytes[i] = asn1_weird_stuff[j];
459     for (i = nbytes-20, j=0; i < nbytes; i++,j++)
460         bytes[i] = hash[j];
461
462     in = bignum_from_bytes(bytes, nbytes);
463     sfree(bytes);
464
465     out = modpow(in, rsa->private_exponent, rsa->modulus);
466     freebn(in);
467
468     nbytes = (ssh1_bignum_bitcount(out)+7)/8;
469     bytes = smalloc(4+7+4+nbytes);
470     PUT_32BIT(bytes, 7);
471     memcpy(bytes+4, "ssh-rsa", 7);
472     PUT_32BIT(bytes+4+7, nbytes);
473     for (i = 0; i < nbytes; i++)
474         bytes[4+7+4+i] = bignum_byte(out, nbytes-1-i);
475     freebn(out);
476
477     *siglen = 4+7+4+nbytes;
478     return bytes;
479 }
480
481 const struct ssh_signkey ssh_rsa = {
482     rsa2_newkey,
483     rsa2_freekey,
484     rsa2_fmtkey,
485     rsa2_public_blob,
486     rsa2_private_blob,
487     rsa2_createkey,
488     rsa2_openssh_createkey,
489     rsa2_fingerprint,
490     rsa2_verifysig,
491     rsa2_sign,
492     "ssh-rsa",
493     "rsa2"
494 };