2 * RSA implementation just sufficient for ssh client-side
5 * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
16 int makekey(unsigned char *data
, struct RSAKey
*result
,
17 unsigned char **keystr
, int order
)
19 unsigned char *p
= data
;
24 for (i
= 0; i
< 4; i
++)
25 result
->bits
= (result
->bits
<< 8) + *p
++;
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).
36 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
38 result
->bytes
= (((p
[0] << 8) + p
[1]) + 7) / 8;
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
)
50 return ssh1_read_bignum(data
, &result
->private_exponent
);
53 void rsaencrypt(unsigned char *data
, int length
, struct RSAKey
*key
)
59 memmove(data
+ key
->bytes
- length
, data
, length
);
63 for (i
= 2; i
< key
->bytes
- length
- 1; i
++) {
65 data
[i
] = random_byte();
66 } while (data
[i
] == 0);
68 data
[key
->bytes
- length
- 1] = 0;
70 b1
= bignum_from_bytes(data
, key
->bytes
);
72 b2
= modpow(b1
, key
->exponent
, key
->modulus
);
75 for (i
= key
->bytes
; i
--;) {
76 *p
++ = bignum_byte(b2
, i
);
83 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
)
86 ret
= modpow(input
, key
->private_exponent
, key
->modulus
);
90 int rsastr_len(struct RSAKey
*key
)
97 mdlen
= (bignum_bitcount(md
) + 15) / 16;
98 exlen
= (bignum_bitcount(ex
) + 15) / 16;
99 return 4 * (mdlen
+ exlen
) + 20;
102 void rsastr_fmt(char *str
, struct RSAKey
*key
)
105 int len
= 0, i
, nibbles
;
106 static const char hex
[] = "0123456789abcdef";
111 len
+= sprintf(str
+ len
, "0x");
113 nibbles
= (3 + bignum_bitcount(ex
)) / 4;
116 for (i
= nibbles
; i
--;)
117 str
[len
++] = hex
[(bignum_byte(ex
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
119 len
+= sprintf(str
+ len
, ",0x");
121 nibbles
= (3 + bignum_bitcount(md
)) / 4;
124 for (i
= nibbles
; i
--;)
125 str
[len
++] = hex
[(bignum_byte(md
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
131 * Generate a fingerprint string for the key. Compatible with the
132 * OpenSSH fingerprint code.
134 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
)
136 struct MD5Context md5c
;
137 unsigned char digest
[16];
138 char buffer
[16 * 3 + 40];
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);
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);
152 MD5Final(digest
, &md5c
);
154 sprintf(buffer
, "%d ", bignum_bitcount(key
->modulus
));
155 for (i
= 0; i
< 16; i
++)
156 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
158 strncpy(str
, buffer
, len
);
161 if (key
->comment
&& slen
< len
- 1) {
163 strncpy(str
+ slen
+ 1, key
->comment
, len
- slen
- 1);
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.
173 int rsa_verify(struct RSAKey
*key
)
175 Bignum n
, ed
, pm1
, qm1
;
178 /* n must equal pq. */
179 n
= bigmul(key
->p
, key
->q
);
180 cmp
= bignum_cmp(n
, key
->modulus
);
185 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
186 pm1
= copybn(key
->p
);
188 ed
= modmul(key
->exponent
, key
->private_exponent
, pm1
);
189 cmp
= bignum_cmp(ed
, One
);
194 qm1
= copybn(key
->q
);
196 ed
= modmul(key
->exponent
, key
->private_exponent
, qm1
);
197 cmp
= bignum_cmp(ed
, One
);
205 if (bignum_cmp(key
->p
, key
->q
) <= 0)
209 * Ensure iqmp * q is congruent to 1, modulo p.
211 n
= modmul(key
->iqmp
, key
->q
, key
->p
);
212 cmp
= bignum_cmp(n
, One
);
220 void freersakey(struct RSAKey
*key
)
223 freebn(key
->modulus
);
225 freebn(key
->exponent
);
226 if (key
->private_exponent
)
227 freebn(key
->private_exponent
);
232 /* ----------------------------------------------------------------------
233 * Implementation of the ssh-rsa signing key type.
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]))
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); }
248 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
253 *length
= GET_32BIT(*data
);
256 if (*datalen
< *length
)
262 static Bignum
getmp(char **data
, int *datalen
)
268 getstring(data
, datalen
, &p
, &length
);
271 b
= bignum_from_bytes(p
, length
);
275 static void *rsa2_newkey(char *data
, int len
)
281 rsa
= smalloc(sizeof(struct RSAKey
));
284 getstring(&data
, &len
, &p
, &slen
);
286 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
290 rsa
->exponent
= getmp(&data
, &len
);
291 rsa
->modulus
= getmp(&data
, &len
);
292 rsa
->private_exponent
= NULL
;
298 static void rsa2_freekey(void *key
)
300 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
305 static char *rsa2_fmtkey(void *key
)
307 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
311 len
= rsastr_len(rsa
);
317 static unsigned char *rsa2_public_blob(void *key
, int *len
)
319 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
320 int elen
, mlen
, bloblen
;
322 unsigned char *blob
, *p
;
324 elen
= (bignum_bitcount(rsa
->exponent
) + 8) / 8;
325 mlen
= (bignum_bitcount(rsa
->modulus
) + 8) / 8;
328 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
329 * (three length fields, 12+7=19).
331 bloblen
= 19 + elen
+ mlen
;
332 blob
= smalloc(bloblen
);
336 memcpy(p
, "ssh-rsa", 7);
341 *p
++ = bignum_byte(rsa
->exponent
, i
);
345 *p
++ = bignum_byte(rsa
->modulus
, i
);
346 assert(p
== blob
+ bloblen
);
351 static unsigned char *rsa2_private_blob(void *key
, int *len
)
353 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
354 int dlen
, plen
, qlen
, ulen
, bloblen
;
356 unsigned char *blob
, *p
;
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;
364 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
367 bloblen
= 16 + dlen
+ plen
+ qlen
+ ulen
;
368 blob
= smalloc(bloblen
);
373 *p
++ = bignum_byte(rsa
->private_exponent
, i
);
377 *p
++ = bignum_byte(rsa
->p
, i
);
381 *p
++ = bignum_byte(rsa
->q
, i
);
385 *p
++ = bignum_byte(rsa
->iqmp
, i
);
386 assert(p
== blob
+ bloblen
);
391 static void *rsa2_createkey(unsigned char *pub_blob
, int pub_len
,
392 unsigned char *priv_blob
, int priv_len
)
395 char *pb
= (char *) priv_blob
;
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
);
403 if (!rsa_verify(rsa
)) {
411 static void *rsa2_openssh_createkey(unsigned char **blob
, int *len
)
413 char **b
= (char **) blob
;
416 rsa
= smalloc(sizeof(struct RSAKey
));
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
);
428 if (!rsa
->modulus
|| !rsa
->exponent
|| !rsa
->private_exponent
||
429 !rsa
->iqmp
|| !rsa
->p
|| !rsa
->q
) {
431 sfree(rsa
->exponent
);
432 sfree(rsa
->private_exponent
);
443 static int rsa2_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
445 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
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
);
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);
464 ENC(rsa
->private_exponent
);
472 static char *rsa2_fingerprint(void *key
)
474 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
475 struct MD5Context md5c
;
476 unsigned char digest
[16], lenbuf
[4];
477 char buffer
[16 * 3 + 40];
482 MD5Update(&md5c
, "\0\0\0\7ssh-rsa", 11);
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); \
491 ADD_BIGNUM(rsa
->exponent
);
492 ADD_BIGNUM(rsa
->modulus
);
495 MD5Final(digest
, &md5c
);
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 ?
":" : "",
501 ret
= smalloc(strlen(buffer
) + 1);
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:
512 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
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)
520 * 04 14 -- a primitive OCTET STRING of length 0x14
521 * [0x14 bytes of hash data follows]
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:
527 * id-sha1 OBJECT IDENTIFIER ::= {
528 * iso(1) identified-organization(3) oiw(14) secsig(3)
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,
536 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
538 static int rsa2_verifysig(void *key
, char *sig
, int siglen
,
539 char *data
, int datalen
)
541 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
545 int bytes
, i
, j
, ret
;
546 unsigned char hash
[20];
548 getstring(&sig
, &siglen
, &p
, &slen
);
549 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
552 in
= getmp(&sig
, &siglen
);
553 out
= modpow(in
, rsa
->exponent
, rsa
->modulus
);
558 bytes
= bignum_bitcount(rsa
->modulus
) / 8;
559 /* Top (partial) byte should be zero. */
560 if (bignum_byte(out
, bytes
- 1) != 0)
562 /* First whole byte should be 1. */
563 if (bignum_byte(out
, bytes
- 2) != 1)
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)
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
])
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
])
585 unsigned char *rsa2_sign(void *key
, char *data
, int datalen
, int *siglen
)
587 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
588 unsigned char *bytes
;
590 unsigned char hash
[20];
594 SHA_Simple(data
, datalen
, hash
);
596 nbytes
= (bignum_bitcount(rsa
->modulus
) - 1) / 8;
597 bytes
= smalloc(nbytes
);
600 for (i
= 1; i
< nbytes
- 20 - ASN1_LEN
; i
++)
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
++)
607 in
= bignum_from_bytes(bytes
, nbytes
);
610 out
= modpow(in
, rsa
->private_exponent
, rsa
->modulus
);
613 nbytes
= (bignum_bitcount(out
) + 7) / 8;
614 bytes
= smalloc(4 + 7 + 4 + nbytes
);
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
);
622 *siglen
= 4 + 7 + 4 + nbytes
;
626 const struct ssh_signkey ssh_rsa
= {
633 rsa2_openssh_createkey
,