2 * RSA implementation just sufficient for ssh client-side
5 * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
12 #include <windows.h> // FIXME
13 #include "putty.h" // FIXME
18 int makekey(unsigned char *data, struct RSAKey *result,
19 unsigned char **keystr, int order) {
20 unsigned char *p = data;
26 result->bits = (result->bits << 8) + *p++;
31 * order=0 means exponent then modulus (the keys sent by the
32 * server). order=1 means modulus then exponent (the keys
33 * stored in a keyfile).
37 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
39 result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
40 if (keystr) *keystr = p+2;
41 p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
43 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
48 int makeprivate(unsigned char *data, struct RSAKey *result) {
49 return ssh1_read_bignum(data, &result->private_exponent);
52 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
57 memmove(data+key->bytes-length, data, length);
61 for (i = 2; i < key->bytes-length-1; i++) {
63 data[i] = random_byte();
64 } while (data[i] == 0);
66 data[key->bytes-length-1] = 0;
68 b1 = bignum_from_bytes(data, key->bytes);
70 b2 = modpow(b1, key->exponent, key->modulus);
73 for (i=key->bytes; i-- ;) {
74 *p++ = bignum_byte(b2, i);
81 Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
83 ret = modpow(input, key->private_exponent, key->modulus);
87 int rsastr_len(struct RSAKey *key) {
93 mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
94 exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
95 return 4 * (mdlen+exlen) + 20;
98 void rsastr_fmt(char *str, struct RSAKey *key) {
100 int len = 0, i, nibbles;
101 static const char hex[] = "0123456789abcdef";
106 len += sprintf(str+len, "0x");
108 nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
109 for (i=nibbles; i-- ;)
110 str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
112 len += sprintf(str+len, ",0x");
114 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
115 for (i=nibbles; i-- ;)
116 str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
122 * Generate a fingerprint string for the key. Compatible with the
123 * OpenSSH fingerprint code.
125 void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
126 struct MD5Context md5c;
127 unsigned char digest[16];
128 char buffer[16*3+40];
132 numlen = ssh1_bignum_length(key->modulus) - 2;
133 for (i = numlen; i-- ;) {
134 unsigned char c = bignum_byte(key->modulus, i);
135 MD5Update(&md5c, &c, 1);
137 numlen = ssh1_bignum_length(key->exponent) - 2;
138 for (i = numlen; i-- ;) {
139 unsigned char c = bignum_byte(key->exponent, i);
140 MD5Update(&md5c, &c, 1);
142 MD5Final(digest, &md5c);
144 sprintf(buffer, "%d ", ssh1_bignum_bitcount(key->modulus));
145 for (i = 0; i < 16; i++)
146 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
147 strncpy(str, buffer, len); str[len-1] = '\0';
149 if (key->comment && slen < len-1) {
151 strncpy(str+slen+1, key->comment, len-slen-1);
156 void freersakey(struct RSAKey *key) {
157 if (key->modulus) freebn(key->modulus);
158 if (key->exponent) freebn(key->exponent);
159 if (key->private_exponent) freebn(key->private_exponent);
160 if (key->comment) sfree(key->comment);
163 /* ----------------------------------------------------------------------
164 * Implementation of the ssh-rsa signing key type.
167 #define GET_32BIT(cp) \
168 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
169 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
170 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
171 ((unsigned long)(unsigned char)(cp)[3]))
173 #define PUT_32BIT(cp, value) { \
174 (cp)[0] = (unsigned char)((value) >> 24); \
175 (cp)[1] = (unsigned char)((value) >> 16); \
176 (cp)[2] = (unsigned char)((value) >> 8); \
177 (cp)[3] = (unsigned char)(value); }
179 static void getstring(char **data, int *datalen, char **p, int *length) {
183 *length = GET_32BIT(*data);
184 *datalen -= 4; *data += 4;
185 if (*datalen < *length)
188 *data += *length; *datalen -= *length;
190 static Bignum getmp(char **data, int *datalen) {
195 getstring(data, datalen, &p, &length);
198 b = bignum_from_bytes(p, length);
202 static void *rsa2_newkey(char *data, int len) {
207 rsa = smalloc(sizeof(struct RSAKey));
208 if (!rsa) return NULL;
209 getstring(&data, &len, &p, &slen);
211 if (!p || memcmp(p, "ssh-rsa", 7)) {
215 rsa->exponent = getmp(&data, &len);
216 rsa->modulus = getmp(&data, &len);
217 rsa->private_exponent = NULL;
223 static void rsa2_freekey(void *key) {
224 struct RSAKey *rsa = (struct RSAKey *)key;
229 static char *rsa2_fmtkey(void *key) {
230 struct RSAKey *rsa = (struct RSAKey *)key;
234 len = rsastr_len(rsa);
240 static unsigned char *rsa2_public_blob(void *key, int *len) {
241 struct RSAKey *rsa = (struct RSAKey *)key;
242 int elen, mlen, bloblen;
244 unsigned char *blob, *p;
246 elen = (ssh1_bignum_bitcount(rsa->exponent)+8)/8;
247 mlen = (ssh1_bignum_bitcount(rsa->modulus)+8)/8;
250 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
251 * (three length fields, 12+7=19).
253 bloblen = 19+elen+mlen;
254 blob = smalloc(bloblen);
256 PUT_32BIT(p, 7); p += 4;
257 memcpy(p, "ssh-rsa", 7); p += 7;
258 PUT_32BIT(p, elen); p += 4;
259 for (i = elen; i-- ;) *p++ = bignum_byte(rsa->exponent, i);
260 PUT_32BIT(p, mlen); p += 4;
261 for (i = mlen; i-- ;) *p++ = bignum_byte(rsa->modulus, i);
262 assert(p == blob + bloblen);
267 static unsigned char *rsa2_private_blob(void *key, int *len) {
268 struct RSAKey *rsa = (struct RSAKey *)key;
269 int dlen, plen, qlen, ulen, bloblen;
271 unsigned char *blob, *p;
273 dlen = (ssh1_bignum_bitcount(rsa->private_exponent)+8)/8;
274 plen = (ssh1_bignum_bitcount(rsa->p)+8)/8;
275 qlen = (ssh1_bignum_bitcount(rsa->q)+8)/8;
276 ulen = (ssh1_bignum_bitcount(rsa->iqmp)+8)/8;
279 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
282 bloblen = 16+dlen+plen+qlen+ulen;
283 blob = smalloc(bloblen);
285 PUT_32BIT(p, dlen); p += 4;
286 for (i = dlen; i-- ;) *p++ = bignum_byte(rsa->private_exponent, i);
287 PUT_32BIT(p, plen); p += 4;
288 for (i = plen; i-- ;) *p++ = bignum_byte(rsa->p, i);
289 PUT_32BIT(p, qlen); p += 4;
290 for (i = qlen; i-- ;) *p++ = bignum_byte(rsa->q, i);
291 PUT_32BIT(p, ulen); p += 4;
292 for (i = ulen; i-- ;) *p++ = bignum_byte(rsa->iqmp, i);
293 assert(p == blob + bloblen);
298 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
299 unsigned char *priv_blob, int priv_len) {
301 char *pb = (char *)priv_blob;
303 rsa = rsa2_newkey((char *)pub_blob, pub_len);
304 rsa->private_exponent = getmp(&pb, &priv_len);
305 rsa->p = getmp(&pb, &priv_len);
306 rsa->q = getmp(&pb, &priv_len);
307 rsa->iqmp = getmp(&pb, &priv_len);
312 static char *rsa2_fingerprint(void *key) {
313 struct RSAKey *rsa = (struct RSAKey *)key;
314 struct MD5Context md5c;
315 unsigned char digest[16], lenbuf[4];
316 char buffer[16*3+40];
321 MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
323 #define ADD_BIGNUM(bignum) \
324 numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
325 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
326 for (i = numlen; i-- ;) { \
327 unsigned char c = bignum_byte(bignum, i); \
328 MD5Update(&md5c, &c, 1); \
330 ADD_BIGNUM(rsa->exponent);
331 ADD_BIGNUM(rsa->modulus);
334 MD5Final(digest, &md5c);
336 sprintf(buffer, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa->modulus));
337 for (i = 0; i < 16; i++)
338 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
339 ret = smalloc(strlen(buffer)+1);
346 * This is the magic ASN.1/DER prefix that goes in the decoded
347 * signature, between the string of FFs and the actual SHA hash
348 * value. As closely as I can tell, the meaning of it is:
350 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
352 * 30 21 -- a constructed SEQUENCE of length 0x21
353 * 30 09 -- a constructed sub-SEQUENCE of length 9
354 * 06 05 -- an object identifier, length 5
357 * 04 14 -- a primitive OCTET STRING of length 0x14
358 * [0x14 bytes of hash data follows]
360 static unsigned char asn1_weird_stuff[] = {
361 0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
362 0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
365 static int rsa2_verifysig(void *key, char *sig, int siglen,
366 char *data, int datalen) {
367 struct RSAKey *rsa = (struct RSAKey *)key;
371 int bytes, i, j, ret;
372 unsigned char hash[20];
374 getstring(&sig, &siglen, &p, &slen);
375 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
378 in = getmp(&sig, &siglen);
379 out = modpow(in, rsa->exponent, rsa->modulus);
384 bytes = ssh1_bignum_bitcount(rsa->modulus) / 8;
385 /* Top (partial) byte should be zero. */
386 if (bignum_byte(out, bytes-1) != 0)
388 /* First whole byte should be 1. */
389 if (bignum_byte(out, bytes-2) != 1)
391 /* Most of the rest should be FF. */
392 for (i = bytes-3; i >= 20 + sizeof(asn1_weird_stuff); i--) {
393 if (bignum_byte(out, i) != 0xFF)
396 /* Then we expect to see the asn1_weird_stuff. */
397 for (i = 20 + sizeof(asn1_weird_stuff) - 1, j=0; i >= 20; i--,j++) {
398 if (bignum_byte(out, i) != asn1_weird_stuff[j])
401 /* Finally, we expect to see the SHA-1 hash of the signed data. */
402 SHA_Simple(data, datalen, hash);
403 for (i = 19, j=0; i >= 0; i--,j++) {
404 if (bignum_byte(out, i) != hash[j])
411 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) {
412 struct RSAKey *rsa = (struct RSAKey *)key;
413 unsigned char *bytes;
415 unsigned char hash[20];
419 SHA_Simple(data, datalen, hash);
421 nbytes = (ssh1_bignum_bitcount(rsa->modulus)-1) / 8;
422 bytes = smalloc(nbytes);
425 for (i = 1; i < nbytes-20-sizeof(asn1_weird_stuff); i++)
427 for (i = nbytes-20-sizeof(asn1_weird_stuff), j=0; i < nbytes-20; i++,j++)
428 bytes[i] = asn1_weird_stuff[j];
429 for (i = nbytes-20, j=0; i < nbytes; i++,j++)
432 in = bignum_from_bytes(bytes, nbytes);
435 out = modpow(in, rsa->private_exponent, rsa->modulus);
438 nbytes = (ssh1_bignum_bitcount(out)+7)/8;
439 bytes = smalloc(4+7+4+nbytes);
441 memcpy(bytes+4, "ssh-rsa", 7);
442 PUT_32BIT(bytes+4+7, nbytes);
443 for (i = 0; i < nbytes; i++)
444 bytes[4+7+4+i] = bignum_byte(out, nbytes-1-i);
447 *siglen = 4+7+4+nbytes;
451 const struct ssh_signkey ssh_rsa = {