2 * RSA implementation just sufficient for ssh client-side
5 * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
16 #define GET_32BIT(cp) \
17 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
18 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
19 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
20 ((unsigned long)(unsigned char)(cp)[3]))
22 #define PUT_32BIT(cp, value) { \
23 (cp)[0] = (unsigned char)((value) >> 24); \
24 (cp)[1] = (unsigned char)((value) >> 16); \
25 (cp)[2] = (unsigned char)((value) >> 8); \
26 (cp)[3] = (unsigned char)(value); }
28 int makekey(unsigned char *data
, struct RSAKey
*result
,
29 unsigned char **keystr
, int order
)
31 unsigned char *p
= data
;
36 for (i
= 0; i
< 4; i
++)
37 result
->bits
= (result
->bits
<< 8) + *p
++;
42 * order=0 means exponent then modulus (the keys sent by the
43 * server). order=1 means modulus then exponent (the keys
44 * stored in a keyfile).
48 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
50 result
->bytes
= (((p
[0] << 8) + p
[1]) + 7) / 8;
53 p
+= ssh1_read_bignum(p
, result ?
&result
->modulus
: NULL
);
55 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
60 int makeprivate(unsigned char *data
, struct RSAKey
*result
)
62 return ssh1_read_bignum(data
, &result
->private_exponent
);
65 void rsaencrypt(unsigned char *data
, int length
, struct RSAKey
*key
)
71 memmove(data
+ key
->bytes
- length
, data
, length
);
75 for (i
= 2; i
< key
->bytes
- length
- 1; i
++) {
77 data
[i
] = random_byte();
78 } while (data
[i
] == 0);
80 data
[key
->bytes
- length
- 1] = 0;
82 b1
= bignum_from_bytes(data
, key
->bytes
);
84 b2
= modpow(b1
, key
->exponent
, key
->modulus
);
87 for (i
= key
->bytes
; i
--;) {
88 *p
++ = bignum_byte(b2
, i
);
95 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
)
98 ret
= modpow(input
, key
->private_exponent
, key
->modulus
);
102 int rsastr_len(struct RSAKey
*key
)
109 mdlen
= (bignum_bitcount(md
) + 15) / 16;
110 exlen
= (bignum_bitcount(ex
) + 15) / 16;
111 return 4 * (mdlen
+ exlen
) + 20;
114 void rsastr_fmt(char *str
, struct RSAKey
*key
)
117 int len
= 0, i
, nibbles
;
118 static const char hex
[] = "0123456789abcdef";
123 len
+= sprintf(str
+ len
, "0x");
125 nibbles
= (3 + bignum_bitcount(ex
)) / 4;
128 for (i
= nibbles
; i
--;)
129 str
[len
++] = hex
[(bignum_byte(ex
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
131 len
+= sprintf(str
+ len
, ",0x");
133 nibbles
= (3 + bignum_bitcount(md
)) / 4;
136 for (i
= nibbles
; i
--;)
137 str
[len
++] = hex
[(bignum_byte(md
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
143 * Generate a fingerprint string for the key. Compatible with the
144 * OpenSSH fingerprint code.
146 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
)
148 struct MD5Context md5c
;
149 unsigned char digest
[16];
150 char buffer
[16 * 3 + 40];
154 numlen
= ssh1_bignum_length(key
->modulus
) - 2;
155 for (i
= numlen
; i
--;) {
156 unsigned char c
= bignum_byte(key
->modulus
, i
);
157 MD5Update(&md5c
, &c
, 1);
159 numlen
= ssh1_bignum_length(key
->exponent
) - 2;
160 for (i
= numlen
; i
--;) {
161 unsigned char c
= bignum_byte(key
->exponent
, i
);
162 MD5Update(&md5c
, &c
, 1);
164 MD5Final(digest
, &md5c
);
166 sprintf(buffer
, "%d ", bignum_bitcount(key
->modulus
));
167 for (i
= 0; i
< 16; i
++)
168 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
170 strncpy(str
, buffer
, len
);
173 if (key
->comment
&& slen
< len
- 1) {
175 strncpy(str
+ slen
+ 1, key
->comment
, len
- slen
- 1);
181 * Verify that the public data in an RSA key matches the private
182 * data. We also check the private data itself: we ensure that p >
183 * q and that iqmp really is the inverse of q mod p.
185 int rsa_verify(struct RSAKey
*key
)
187 Bignum n
, ed
, pm1
, qm1
;
190 /* n must equal pq. */
191 n
= bigmul(key
->p
, key
->q
);
192 cmp
= bignum_cmp(n
, key
->modulus
);
197 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
198 pm1
= copybn(key
->p
);
200 ed
= modmul(key
->exponent
, key
->private_exponent
, pm1
);
201 cmp
= bignum_cmp(ed
, One
);
206 qm1
= copybn(key
->q
);
208 ed
= modmul(key
->exponent
, key
->private_exponent
, qm1
);
209 cmp
= bignum_cmp(ed
, One
);
217 if (bignum_cmp(key
->p
, key
->q
) <= 0)
221 * Ensure iqmp * q is congruent to 1, modulo p.
223 n
= modmul(key
->iqmp
, key
->q
, key
->p
);
224 cmp
= bignum_cmp(n
, One
);
232 /* Public key blob as used by Pageant: exponent before modulus. */
233 unsigned char *rsa_public_blob(struct RSAKey
*key
, int *len
)
238 length
= (ssh1_bignum_length(key
->modulus
) +
239 ssh1_bignum_length(key
->exponent
) + 4);
240 ret
= smalloc(length
);
242 PUT_32BIT(ret
, bignum_bitcount(key
->modulus
));
244 pos
+= ssh1_write_bignum(ret
+ pos
, key
->exponent
);
245 pos
+= ssh1_write_bignum(ret
+ pos
, key
->modulus
);
251 /* Given a public blob, determine its length. */
252 int rsa_public_blob_len(void *data
)
254 unsigned char *p
= (unsigned char *)data
;
257 p
+= 4; /* length word */
258 p
+= ssh1_read_bignum(p
, NULL
); /* exponent */
259 p
+= ssh1_read_bignum(p
, NULL
); /* modulus */
261 return p
- (unsigned char *)data
;
264 void freersakey(struct RSAKey
*key
)
267 freebn(key
->modulus
);
269 freebn(key
->exponent
);
270 if (key
->private_exponent
)
271 freebn(key
->private_exponent
);
276 /* ----------------------------------------------------------------------
277 * Implementation of the ssh-rsa signing key type.
280 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
285 *length
= GET_32BIT(*data
);
288 if (*datalen
< *length
)
294 static Bignum
getmp(char **data
, int *datalen
)
300 getstring(data
, datalen
, &p
, &length
);
303 b
= bignum_from_bytes(p
, length
);
307 static void *rsa2_newkey(char *data
, int len
)
313 rsa
= smalloc(sizeof(struct RSAKey
));
316 getstring(&data
, &len
, &p
, &slen
);
318 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
322 rsa
->exponent
= getmp(&data
, &len
);
323 rsa
->modulus
= getmp(&data
, &len
);
324 rsa
->private_exponent
= NULL
;
330 static void rsa2_freekey(void *key
)
332 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
337 static char *rsa2_fmtkey(void *key
)
339 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
343 len
= rsastr_len(rsa
);
349 static unsigned char *rsa2_public_blob(void *key
, int *len
)
351 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
352 int elen
, mlen
, bloblen
;
354 unsigned char *blob
, *p
;
356 elen
= (bignum_bitcount(rsa
->exponent
) + 8) / 8;
357 mlen
= (bignum_bitcount(rsa
->modulus
) + 8) / 8;
360 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
361 * (three length fields, 12+7=19).
363 bloblen
= 19 + elen
+ mlen
;
364 blob
= smalloc(bloblen
);
368 memcpy(p
, "ssh-rsa", 7);
373 *p
++ = bignum_byte(rsa
->exponent
, i
);
377 *p
++ = bignum_byte(rsa
->modulus
, i
);
378 assert(p
== blob
+ bloblen
);
383 static unsigned char *rsa2_private_blob(void *key
, int *len
)
385 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
386 int dlen
, plen
, qlen
, ulen
, bloblen
;
388 unsigned char *blob
, *p
;
390 dlen
= (bignum_bitcount(rsa
->private_exponent
) + 8) / 8;
391 plen
= (bignum_bitcount(rsa
->p
) + 8) / 8;
392 qlen
= (bignum_bitcount(rsa
->q
) + 8) / 8;
393 ulen
= (bignum_bitcount(rsa
->iqmp
) + 8) / 8;
396 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
399 bloblen
= 16 + dlen
+ plen
+ qlen
+ ulen
;
400 blob
= smalloc(bloblen
);
405 *p
++ = bignum_byte(rsa
->private_exponent
, i
);
409 *p
++ = bignum_byte(rsa
->p
, i
);
413 *p
++ = bignum_byte(rsa
->q
, i
);
417 *p
++ = bignum_byte(rsa
->iqmp
, i
);
418 assert(p
== blob
+ bloblen
);
423 static void *rsa2_createkey(unsigned char *pub_blob
, int pub_len
,
424 unsigned char *priv_blob
, int priv_len
)
427 char *pb
= (char *) priv_blob
;
429 rsa
= rsa2_newkey((char *) pub_blob
, pub_len
);
430 rsa
->private_exponent
= getmp(&pb
, &priv_len
);
431 rsa
->p
= getmp(&pb
, &priv_len
);
432 rsa
->q
= getmp(&pb
, &priv_len
);
433 rsa
->iqmp
= getmp(&pb
, &priv_len
);
435 if (!rsa_verify(rsa
)) {
443 static void *rsa2_openssh_createkey(unsigned char **blob
, int *len
)
445 char **b
= (char **) blob
;
448 rsa
= smalloc(sizeof(struct RSAKey
));
453 rsa
->modulus
= getmp(b
, len
);
454 rsa
->exponent
= getmp(b
, len
);
455 rsa
->private_exponent
= getmp(b
, len
);
456 rsa
->iqmp
= getmp(b
, len
);
457 rsa
->p
= getmp(b
, len
);
458 rsa
->q
= getmp(b
, len
);
460 if (!rsa
->modulus
|| !rsa
->exponent
|| !rsa
->private_exponent
||
461 !rsa
->iqmp
|| !rsa
->p
|| !rsa
->q
) {
463 sfree(rsa
->exponent
);
464 sfree(rsa
->private_exponent
);
475 static int rsa2_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
477 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
481 ssh2_bignum_length(rsa
->modulus
) +
482 ssh2_bignum_length(rsa
->exponent
) +
483 ssh2_bignum_length(rsa
->private_exponent
) +
484 ssh2_bignum_length(rsa
->iqmp
) +
485 ssh2_bignum_length(rsa
->p
) + ssh2_bignum_length(rsa
->q
);
492 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
493 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
496 ENC(rsa
->private_exponent
);
504 static char *rsa2_fingerprint(void *key
)
506 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
507 struct MD5Context md5c
;
508 unsigned char digest
[16], lenbuf
[4];
509 char buffer
[16 * 3 + 40];
514 MD5Update(&md5c
, "\0\0\0\7ssh-rsa", 11);
516 #define ADD_BIGNUM(bignum) \
517 numlen = (bignum_bitcount(bignum)+8)/8; \
518 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
519 for (i = numlen; i-- ;) { \
520 unsigned char c = bignum_byte(bignum, i); \
521 MD5Update(&md5c, &c, 1); \
523 ADD_BIGNUM(rsa
->exponent
);
524 ADD_BIGNUM(rsa
->modulus
);
527 MD5Final(digest
, &md5c
);
529 sprintf(buffer
, "ssh-rsa %d ", bignum_bitcount(rsa
->modulus
));
530 for (i
= 0; i
< 16; i
++)
531 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
533 ret
= smalloc(strlen(buffer
) + 1);
540 * This is the magic ASN.1/DER prefix that goes in the decoded
541 * signature, between the string of FFs and the actual SHA hash
542 * value. The meaning of it is:
544 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
546 * 30 21 -- a constructed SEQUENCE of length 0x21
547 * 30 09 -- a constructed sub-SEQUENCE of length 9
548 * 06 05 -- an object identifier, length 5
549 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
550 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
552 * 04 14 -- a primitive OCTET STRING of length 0x14
553 * [0x14 bytes of hash data follows]
555 * The object id in the middle there is listed as `id-sha1' in
556 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
557 * ASN module for PKCS #1) and its expanded form is as follows:
559 * id-sha1 OBJECT IDENTIFIER ::= {
560 * iso(1) identified-organization(3) oiw(14) secsig(3)
563 static unsigned char asn1_weird_stuff
[] = {
564 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
565 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
568 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
570 static int rsa2_verifysig(void *key
, char *sig
, int siglen
,
571 char *data
, int datalen
)
573 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
577 int bytes
, i
, j
, ret
;
578 unsigned char hash
[20];
580 getstring(&sig
, &siglen
, &p
, &slen
);
581 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
584 in
= getmp(&sig
, &siglen
);
585 out
= modpow(in
, rsa
->exponent
, rsa
->modulus
);
590 bytes
= bignum_bitcount(rsa
->modulus
) / 8;
591 /* Top (partial) byte should be zero. */
592 if (bignum_byte(out
, bytes
- 1) != 0)
594 /* First whole byte should be 1. */
595 if (bignum_byte(out
, bytes
- 2) != 1)
597 /* Most of the rest should be FF. */
598 for (i
= bytes
- 3; i
>= 20 + ASN1_LEN
; i
--) {
599 if (bignum_byte(out
, i
) != 0xFF)
602 /* Then we expect to see the asn1_weird_stuff. */
603 for (i
= 20 + ASN1_LEN
- 1, j
= 0; i
>= 20; i
--, j
++) {
604 if (bignum_byte(out
, i
) != asn1_weird_stuff
[j
])
607 /* Finally, we expect to see the SHA-1 hash of the signed data. */
608 SHA_Simple(data
, datalen
, hash
);
609 for (i
= 19, j
= 0; i
>= 0; i
--, j
++) {
610 if (bignum_byte(out
, i
) != hash
[j
])
617 unsigned char *rsa2_sign(void *key
, char *data
, int datalen
, int *siglen
)
619 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
620 unsigned char *bytes
;
622 unsigned char hash
[20];
626 SHA_Simple(data
, datalen
, hash
);
628 nbytes
= (bignum_bitcount(rsa
->modulus
) - 1) / 8;
629 bytes
= smalloc(nbytes
);
632 for (i
= 1; i
< nbytes
- 20 - ASN1_LEN
; i
++)
634 for (i
= nbytes
- 20 - ASN1_LEN
, j
= 0; i
< nbytes
- 20; i
++, j
++)
635 bytes
[i
] = asn1_weird_stuff
[j
];
636 for (i
= nbytes
- 20, j
= 0; i
< nbytes
; i
++, j
++)
639 in
= bignum_from_bytes(bytes
, nbytes
);
642 out
= modpow(in
, rsa
->private_exponent
, rsa
->modulus
);
645 nbytes
= (bignum_bitcount(out
) + 7) / 8;
646 bytes
= smalloc(4 + 7 + 4 + nbytes
);
648 memcpy(bytes
+ 4, "ssh-rsa", 7);
649 PUT_32BIT(bytes
+ 4 + 7, nbytes
);
650 for (i
= 0; i
< nbytes
; i
++)
651 bytes
[4 + 7 + 4 + i
] = bignum_byte(out
, nbytes
- 1 - i
);
654 *siglen
= 4 + 7 + 4 + nbytes
;
658 const struct ssh_signkey ssh_rsa
= {
665 rsa2_openssh_createkey
,