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
|| slen
!= 7 || 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 void *rsa2_openssh_createkey(unsigned char **blob
, int *len
) {
313 char **b
= (char **)blob
;
318 rsa
= smalloc(sizeof(struct RSAKey
));
319 if (!rsa
) return NULL
;
322 rsa
->modulus
= getmp(b
, len
);
323 rsa
->exponent
= getmp(b
, len
);
324 rsa
->private_exponent
= getmp(b
, len
);
325 rsa
->iqmp
= getmp(b
, len
);
326 rsa
->p
= getmp(b
, len
);
327 rsa
->q
= getmp(b
, len
);
329 if (!rsa
->modulus
|| !rsa
->exponent
|| !rsa
->private_exponent
||
330 !rsa
->iqmp
|| !rsa
->p
|| !rsa
->q
) {
332 sfree(rsa
->exponent
);
333 sfree(rsa
->private_exponent
);
344 static char *rsa2_fingerprint(void *key
) {
345 struct RSAKey
*rsa
= (struct RSAKey
*)key
;
346 struct MD5Context md5c
;
347 unsigned char digest
[16], lenbuf
[4];
348 char buffer
[16*3+40];
353 MD5Update(&md5c
, "\0\0\0\7ssh-rsa", 11);
355 #define ADD_BIGNUM(bignum) \
356 numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
357 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
358 for (i = numlen; i-- ;) { \
359 unsigned char c = bignum_byte(bignum, i); \
360 MD5Update(&md5c, &c, 1); \
362 ADD_BIGNUM(rsa
->exponent
);
363 ADD_BIGNUM(rsa
->modulus
);
366 MD5Final(digest
, &md5c
);
368 sprintf(buffer
, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa
->modulus
));
369 for (i
= 0; i
< 16; i
++)
370 sprintf(buffer
+strlen(buffer
), "%s%02x", i?
":":"", digest
[i
]);
371 ret
= smalloc(strlen(buffer
)+1);
378 * This is the magic ASN.1/DER prefix that goes in the decoded
379 * signature, between the string of FFs and the actual SHA hash
380 * value. As closely as I can tell, the meaning of it is:
382 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
384 * 30 21 -- a constructed SEQUENCE of length 0x21
385 * 30 09 -- a constructed sub-SEQUENCE of length 9
386 * 06 05 -- an object identifier, length 5
389 * 04 14 -- a primitive OCTET STRING of length 0x14
390 * [0x14 bytes of hash data follows]
392 static unsigned char asn1_weird_stuff
[] = {
393 0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
394 0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
397 static int rsa2_verifysig(void *key
, char *sig
, int siglen
,
398 char *data
, int datalen
) {
399 struct RSAKey
*rsa
= (struct RSAKey
*)key
;
403 int bytes
, i
, j
, ret
;
404 unsigned char hash
[20];
406 getstring(&sig
, &siglen
, &p
, &slen
);
407 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
410 in
= getmp(&sig
, &siglen
);
411 out
= modpow(in
, rsa
->exponent
, rsa
->modulus
);
416 bytes
= ssh1_bignum_bitcount(rsa
->modulus
) / 8;
417 /* Top (partial) byte should be zero. */
418 if (bignum_byte(out
, bytes
-1) != 0)
420 /* First whole byte should be 1. */
421 if (bignum_byte(out
, bytes
-2) != 1)
423 /* Most of the rest should be FF. */
424 for (i
= bytes
-3; i
>= 20 + sizeof(asn1_weird_stuff
); i
--) {
425 if (bignum_byte(out
, i
) != 0xFF)
428 /* Then we expect to see the asn1_weird_stuff. */
429 for (i
= 20 + sizeof(asn1_weird_stuff
) - 1, j
=0; i
>= 20; i
--,j
++) {
430 if (bignum_byte(out
, i
) != asn1_weird_stuff
[j
])
433 /* Finally, we expect to see the SHA-1 hash of the signed data. */
434 SHA_Simple(data
, datalen
, hash
);
435 for (i
= 19, j
=0; i
>= 0; i
--,j
++) {
436 if (bignum_byte(out
, i
) != hash
[j
])
443 unsigned char *rsa2_sign(void *key
, char *data
, int datalen
, int *siglen
) {
444 struct RSAKey
*rsa
= (struct RSAKey
*)key
;
445 unsigned char *bytes
;
447 unsigned char hash
[20];
451 SHA_Simple(data
, datalen
, hash
);
453 nbytes
= (ssh1_bignum_bitcount(rsa
->modulus
)-1) / 8;
454 bytes
= smalloc(nbytes
);
457 for (i
= 1; i
< nbytes
-20-sizeof(asn1_weird_stuff
); i
++)
459 for (i
= nbytes
-20-sizeof(asn1_weird_stuff
), j
=0; i
< nbytes
-20; i
++,j
++)
460 bytes
[i
] = asn1_weird_stuff
[j
];
461 for (i
= nbytes
-20, j
=0; i
< nbytes
; i
++,j
++)
464 in
= bignum_from_bytes(bytes
, nbytes
);
467 out
= modpow(in
, rsa
->private_exponent
, rsa
->modulus
);
470 nbytes
= (ssh1_bignum_bitcount(out
)+7)/8;
471 bytes
= smalloc(4+7+4+nbytes
);
473 memcpy(bytes
+4, "ssh-rsa", 7);
474 PUT_32BIT(bytes
+4+7, nbytes
);
475 for (i
= 0; i
< nbytes
; i
++)
476 bytes
[4+7+4+i
] = bignum_byte(out
, nbytes
-1-i
);
479 *siglen
= 4+7+4+nbytes
;
483 const struct ssh_signkey ssh_rsa
= {
490 rsa2_openssh_createkey
,