2 * RSA implementation just sufficient for ssh client-side
5 * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
15 int makekey(unsigned char *data
, struct RSAKey
*result
,
16 unsigned char **keystr
, int order
) {
17 unsigned char *p
= data
;
23 result
->bits
= (result
->bits
<< 8) + *p
++;
28 * order=0 means exponent then modulus (the keys sent by the
29 * server). order=1 means modulus then exponent (the keys
30 * stored in a keyfile).
34 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
36 result
->bytes
= (((p
[0] << 8) + p
[1]) + 7) / 8;
37 if (keystr
) *keystr
= p
+2;
38 p
+= ssh1_read_bignum(p
, result ?
&result
->modulus
: NULL
);
40 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
45 int makeprivate(unsigned char *data
, struct RSAKey
*result
) {
46 return ssh1_read_bignum(data
, &result
->private_exponent
);
49 void rsaencrypt(unsigned char *data
, int length
, struct RSAKey
*key
) {
54 memmove(data
+key
->bytes
-length
, data
, length
);
58 for (i
= 2; i
< key
->bytes
-length
-1; i
++) {
60 data
[i
] = random_byte();
61 } while (data
[i
] == 0);
63 data
[key
->bytes
-length
-1] = 0;
65 b1
= bignum_from_bytes(data
, key
->bytes
);
67 b2
= modpow(b1
, key
->exponent
, key
->modulus
);
70 for (i
=key
->bytes
; i
-- ;) {
71 *p
++ = bignum_byte(b2
, i
);
78 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
) {
80 ret
= modpow(input
, key
->private_exponent
, key
->modulus
);
84 int rsastr_len(struct RSAKey
*key
) {
90 mdlen
= (ssh1_bignum_bitcount(md
)+15) / 16;
91 exlen
= (ssh1_bignum_bitcount(ex
)+15) / 16;
92 return 4 * (mdlen
+exlen
) + 20;
95 void rsastr_fmt(char *str
, struct RSAKey
*key
) {
97 int len
= 0, i
, nibbles
;
98 static const char hex
[] = "0123456789abcdef";
103 len
+= sprintf(str
+len
, "0x");
105 nibbles
= (3 + ssh1_bignum_bitcount(ex
))/4; if (nibbles
<1) nibbles
=1;
106 for (i
=nibbles
; i
-- ;)
107 str
[len
++] = hex
[(bignum_byte(ex
, i
/2) >> (4*(i
%2))) & 0xF];
109 len
+= sprintf(str
+len
, ",0x");
111 nibbles
= (3 + ssh1_bignum_bitcount(md
))/4; if (nibbles
<1) nibbles
=1;
112 for (i
=nibbles
; i
-- ;)
113 str
[len
++] = hex
[(bignum_byte(md
, i
/2) >> (4*(i
%2))) & 0xF];
119 * Generate a fingerprint string for the key. Compatible with the
120 * OpenSSH fingerprint code.
122 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
) {
123 struct MD5Context md5c
;
124 unsigned char digest
[16];
125 char buffer
[16*3+40];
129 numlen
= ssh1_bignum_length(key
->modulus
) - 2;
130 for (i
= numlen
; i
-- ;) {
131 unsigned char c
= bignum_byte(key
->modulus
, i
);
132 MD5Update(&md5c
, &c
, 1);
134 numlen
= ssh1_bignum_length(key
->exponent
) - 2;
135 for (i
= numlen
; i
-- ;) {
136 unsigned char c
= bignum_byte(key
->exponent
, i
);
137 MD5Update(&md5c
, &c
, 1);
139 MD5Final(digest
, &md5c
);
141 sprintf(buffer
, "%d ", ssh1_bignum_bitcount(key
->modulus
));
142 for (i
= 0; i
< 16; i
++)
143 sprintf(buffer
+strlen(buffer
), "%s%02x", i?
":":"", digest
[i
]);
144 strncpy(str
, buffer
, len
); str
[len
-1] = '\0';
146 if (key
->comment
&& slen
< len
-1) {
148 strncpy(str
+slen
+1, key
->comment
, len
-slen
-1);
153 void freersakey(struct RSAKey
*key
) {
154 if (key
->modulus
) freebn(key
->modulus
);
155 if (key
->exponent
) freebn(key
->exponent
);
156 if (key
->private_exponent
) freebn(key
->private_exponent
);
157 if (key
->comment
) sfree(key
->comment
);