37508af4 |
1 | #include <string.h> |
2 | |
dcbde236 |
3 | #include "puttymem.h" |
4 | |
e5574168 |
5 | /* |
6 | * Useful thing. |
7 | */ |
8 | #ifndef lenof |
9 | #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) |
10 | #endif |
11 | |
bea1ef5f |
12 | #define SSH_CIPHER_IDEA 1 |
9697bfd2 |
13 | #define SSH_CIPHER_DES 2 |
bea1ef5f |
14 | #define SSH_CIPHER_3DES 3 |
15 | #define SSH_CIPHER_BLOWFISH 6 |
16 | |
e5574168 |
17 | #ifdef MSCRYPTOAPI |
18 | #define APIEXTRA 8 |
19 | #else |
20 | #define APIEXTRA 0 |
21 | #endif |
22 | |
3709bfe9 |
23 | #ifndef BIGNUM_INTERNAL |
24 | typedef void *Bignum; |
25 | #endif |
7cca0d81 |
26 | |
374330e2 |
27 | struct RSAKey { |
28 | int bits; |
29 | int bytes; |
8f203108 |
30 | #ifdef MSCRYPTOAPI |
31 | unsigned long exponent; |
32 | unsigned char *modulus; |
33 | #else |
7cca0d81 |
34 | Bignum modulus; |
35 | Bignum exponent; |
36 | Bignum private_exponent; |
9400cf6f |
37 | Bignum p; |
38 | Bignum q; |
39 | Bignum iqmp; |
65a22376 |
40 | #endif |
41 | char *comment; |
9400cf6f |
42 | }; |
43 | |
374330e2 |
44 | int makekey(unsigned char *data, struct RSAKey *result, |
7cca0d81 |
45 | unsigned char **keystr, int order); |
46 | int makeprivate(unsigned char *data, struct RSAKey *result); |
374330e2 |
47 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
7cca0d81 |
48 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
49 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
50 | void rsasanitise(struct RSAKey *key); |
374330e2 |
51 | int rsastr_len(struct RSAKey *key); |
52 | void rsastr_fmt(char *str, struct RSAKey *key); |
1c2a93c4 |
53 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
014970c8 |
54 | int rsa_verify(struct RSAKey *key); |
5c58ad2d |
55 | void freersakey(struct RSAKey *key); |
374330e2 |
56 | |
57 | typedef unsigned int word32; |
58 | typedef unsigned int uint32; |
59 | |
8160fbfc |
60 | unsigned long crc32(const void *s, size_t len); |
374330e2 |
61 | |
48672163 |
62 | typedef struct { |
63 | uint32 h[4]; |
64 | } MD5_Core_State; |
65 | |
374330e2 |
66 | struct MD5Context { |
8f203108 |
67 | #ifdef MSCRYPTOAPI |
68 | unsigned long hHash; |
69 | #else |
48672163 |
70 | MD5_Core_State core; |
bb093ca7 |
71 | unsigned char block[64]; |
48672163 |
72 | int blkused; |
73 | uint32 lenhi, lenlo; |
8f203108 |
74 | #endif |
374330e2 |
75 | }; |
76 | |
77 | void MD5Init(struct MD5Context *context); |
78 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
32874aea |
79 | unsigned len); |
374330e2 |
80 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
81 | |
e5574168 |
82 | typedef struct { |
83 | uint32 h[5]; |
84 | unsigned char block[64]; |
85 | int blkused; |
86 | uint32 lenhi, lenlo; |
87 | } SHA_State; |
88 | |
32874aea |
89 | void SHA_Init(SHA_State * s); |
90 | void SHA_Bytes(SHA_State * s, void *p, int len); |
91 | void SHA_Final(SHA_State * s, unsigned char *output); |
7cca0d81 |
92 | void SHA_Simple(void *p, int len, unsigned char *output); |
e5574168 |
93 | |
374330e2 |
94 | struct ssh_cipher { |
32874aea |
95 | void (*sesskey) (unsigned char *key); /* for ssh 1 */ |
96 | void (*encrypt) (unsigned char *blk, int len); |
97 | void (*decrypt) (unsigned char *blk, int len); |
0a3f1d48 |
98 | int blksize; |
99 | }; |
100 | |
101 | struct ssh2_cipher { |
32874aea |
102 | void (*setcsiv) (unsigned char *key); /* for ssh 2 */ |
103 | void (*setcskey) (unsigned char *key); /* for ssh 2 */ |
104 | void (*setsciv) (unsigned char *key); /* for ssh 2 */ |
105 | void (*setsckey) (unsigned char *key); /* for ssh 2 */ |
106 | void (*encrypt) (unsigned char *blk, int len); |
107 | void (*decrypt) (unsigned char *blk, int len); |
e5574168 |
108 | char *name; |
109 | int blksize; |
d2a0e0be |
110 | int keylen; |
e5574168 |
111 | }; |
112 | |
0a3f1d48 |
113 | struct ssh2_ciphers { |
114 | int nciphers; |
65a22376 |
115 | const struct ssh2_cipher *const *list; |
0a3f1d48 |
116 | }; |
117 | |
e5574168 |
118 | struct ssh_mac { |
32874aea |
119 | void (*setcskey) (unsigned char *key); |
120 | void (*setsckey) (unsigned char *key); |
121 | void (*generate) (unsigned char *blk, int len, unsigned long seq); |
122 | int (*verify) (unsigned char *blk, int len, unsigned long seq); |
e5574168 |
123 | char *name; |
124 | int len; |
125 | }; |
126 | |
127 | struct ssh_kex { |
7cca0d81 |
128 | /* |
129 | * Plugging in another KEX algorithm requires structural chaos, |
130 | * so it's hard to abstract them into nice little structures |
131 | * like this. Hence, for the moment, this is just a |
132 | * placeholder. I claim justification in the fact that OpenSSH |
133 | * does this too :-) |
134 | */ |
e5574168 |
135 | char *name; |
136 | }; |
137 | |
e055a386 |
138 | struct ssh_signkey { |
32874aea |
139 | void *(*newkey) (char *data, int len); |
140 | void (*freekey) (void *key); |
141 | char *(*fmtkey) (void *key); |
142 | unsigned char *(*public_blob) (void *key, int *len); |
143 | unsigned char *(*private_blob) (void *key, int *len); |
144 | void *(*createkey) (unsigned char *pub_blob, int pub_len, |
145 | unsigned char *priv_blob, int priv_len); |
146 | void *(*openssh_createkey) (unsigned char **blob, int *len); |
147 | int (*openssh_fmtkey) (void *key, unsigned char *blob, int len); |
148 | char *(*fingerprint) (void *key); |
149 | int (*verifysig) (void *key, char *sig, int siglen, |
150 | char *data, int datalen); |
151 | unsigned char *(*sign) (void *key, char *data, int datalen, |
152 | int *siglen); |
e5574168 |
153 | char *name; |
32874aea |
154 | char *keytype; /* for host key cache */ |
e5574168 |
155 | }; |
156 | |
157 | struct ssh_compress { |
158 | char *name; |
32874aea |
159 | void (*compress_init) (void); |
160 | int (*compress) (unsigned char *block, int len, |
161 | unsigned char **outblock, int *outlen); |
162 | void (*decompress_init) (void); |
163 | int (*decompress) (unsigned char *block, int len, |
164 | unsigned char **outblock, int *outlen); |
165 | int (*disable_compression) (void); |
374330e2 |
166 | }; |
167 | |
65a22376 |
168 | struct ssh2_userkey { |
169 | const struct ssh_signkey *alg; /* the key algorithm */ |
170 | void *data; /* the key data */ |
171 | char *comment; /* the key comment */ |
172 | }; |
173 | |
174 | extern const struct ssh_cipher ssh_3des; |
175 | extern const struct ssh_cipher ssh_des; |
176 | extern const struct ssh_cipher ssh_blowfish_ssh1; |
177 | extern const struct ssh2_ciphers ssh2_3des; |
178 | extern const struct ssh2_ciphers ssh2_aes; |
179 | extern const struct ssh2_ciphers ssh2_blowfish; |
180 | extern const struct ssh_kex ssh_diffiehellman; |
181 | extern const struct ssh_kex ssh_diffiehellman_gex; |
182 | extern const struct ssh_signkey ssh_dss; |
183 | extern const struct ssh_signkey ssh_rsa; |
184 | extern const struct ssh_mac ssh_md5; |
185 | extern const struct ssh_mac ssh_sha1; |
186 | extern const struct ssh_mac ssh_sha1_buggy; |
187 | |
900a4ee6 |
188 | /* |
189 | * PuTTY version number formatted as an SSH version string. |
190 | */ |
191 | extern char sshver[]; |
192 | |
8f203108 |
193 | #ifndef MSCRYPTOAPI |
32874aea |
194 | void SHATransform(word32 * digest, word32 * data); |
8f203108 |
195 | #endif |
374330e2 |
196 | |
197 | int random_byte(void); |
198 | void random_add_noise(void *noise, int length); |
6e522441 |
199 | void random_add_heavynoise(void *noise, int length); |
c5e9c988 |
200 | |
32874aea |
201 | void logevent(char *); |
e5574168 |
202 | |
7cca0d81 |
203 | Bignum copybn(Bignum b); |
3709bfe9 |
204 | Bignum bn_power_2(int n); |
205 | void bn_restore_invariant(Bignum b); |
9400cf6f |
206 | Bignum bignum_from_short(unsigned short n); |
e5574168 |
207 | void freebn(Bignum b); |
59600f67 |
208 | Bignum modpow(Bignum base, Bignum exp, Bignum mod); |
209 | Bignum modmul(Bignum a, Bignum b, Bignum mod); |
7cca0d81 |
210 | void decbn(Bignum n); |
211 | extern Bignum Zero, One; |
3709bfe9 |
212 | Bignum bignum_from_bytes(unsigned char *data, int nbytes); |
32874aea |
213 | int ssh1_read_bignum(unsigned char *data, Bignum * result); |
ddecd643 |
214 | int bignum_bitcount(Bignum bn); |
5c58ad2d |
215 | int ssh1_bignum_length(Bignum bn); |
260f3dec |
216 | int ssh2_bignum_length(Bignum bn); |
5c58ad2d |
217 | int bignum_byte(Bignum bn, int i); |
9400cf6f |
218 | int bignum_bit(Bignum bn, int i); |
219 | void bignum_set_bit(Bignum bn, int i, int value); |
5c58ad2d |
220 | int ssh1_write_bignum(void *data, Bignum bn); |
9400cf6f |
221 | Bignum biggcd(Bignum a, Bignum b); |
222 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
223 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
224 | Bignum bigmul(Bignum a, Bignum b); |
225 | Bignum modinv(Bignum number, Bignum modulus); |
3709bfe9 |
226 | Bignum bignum_bitmask(Bignum number); |
9400cf6f |
227 | Bignum bignum_rshift(Bignum number, int shift); |
8c3cd914 |
228 | int bignum_cmp(Bignum a, Bignum b); |
6e522441 |
229 | char *bignum_decimal(Bignum x); |
e5574168 |
230 | |
3709bfe9 |
231 | void dh_setup_group1(void); |
a92dd380 |
232 | void dh_setup_group(Bignum pval, Bignum gval); |
3709bfe9 |
233 | void dh_cleanup(void); |
7bd5a860 |
234 | Bignum dh_create_e(int nbits); |
e5574168 |
235 | Bignum dh_find_K(Bignum f); |
7cca0d81 |
236 | |
65a22376 |
237 | int loadrsakey(char *filename, struct RSAKey *key, char *passphrase); |
a52f067e |
238 | int rsakey_encrypted(char *filename, char **comment); |
7cca0d81 |
239 | |
65a22376 |
240 | int saversakey(char *filename, struct RSAKey *key, char *passphrase); |
241 | |
242 | void base64_encode_atom(unsigned char *data, int n, char *out); |
243 | |
244 | /* ssh2_load_userkey can return this as an error */ |
245 | extern struct ssh2_userkey ssh2_wrong_passphrase; |
246 | #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase) |
247 | |
248 | int ssh2_userkey_encrypted(char *filename, char **comment); |
249 | struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase); |
32874aea |
250 | char *ssh2_userkey_loadpub(char *filename, char **algorithm, |
251 | int *pub_blob_len); |
252 | int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, |
253 | char *passphrase); |
65a22376 |
254 | |
255 | int keyfile_version(char *filename); |
6e522441 |
256 | |
65a22376 |
257 | void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
258 | void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
32874aea |
259 | void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, |
260 | int len); |
261 | void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, |
262 | int len); |
9400cf6f |
263 | |
264 | /* |
265 | * For progress updates in the key generation utility. |
266 | */ |
32874aea |
267 | typedef void (*progfn_t) (void *param, int phase, int progress); |
9400cf6f |
268 | |
32874aea |
269 | int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, |
270 | void *pfnparam); |
271 | Bignum primegen(int bits, int modulus, int residue, int phase, |
272 | progfn_t pfn, void *pfnparam); |
4ba9b64b |
273 | |
274 | /* |
275 | * zlib compression. |
276 | */ |
277 | void zlib_compress_init(void); |
278 | void zlib_decompress_init(void); |
279 | int zlib_compress_block(unsigned char *block, int len, |
280 | unsigned char **outblock, int *outlen); |
281 | int zlib_decompress_block(unsigned char *block, int len, |
282 | unsigned char **outblock, int *outlen); |
1983e559 |
283 | |
284 | /* |
285 | * SSH1 agent messages. |
286 | */ |
287 | #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1 |
288 | #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2 |
289 | #define SSH1_AGENTC_RSA_CHALLENGE 3 |
290 | #define SSH1_AGENT_RSA_RESPONSE 4 |
291 | #define SSH1_AGENTC_ADD_RSA_IDENTITY 7 |
292 | #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8 |
32874aea |
293 | #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */ |
1983e559 |
294 | |
295 | /* |
296 | * Messages common to SSH1 and OpenSSH's SSH2. |
297 | */ |
298 | #define SSH_AGENT_FAILURE 5 |
299 | #define SSH_AGENT_SUCCESS 6 |
300 | |
301 | /* |
302 | * OpenSSH's SSH2 agent messages. |
303 | */ |
304 | #define SSH2_AGENTC_REQUEST_IDENTITIES 11 |
305 | #define SSH2_AGENT_IDENTITIES_ANSWER 12 |
306 | #define SSH2_AGENTC_SIGN_REQUEST 13 |
307 | #define SSH2_AGENT_SIGN_RESPONSE 14 |
308 | #define SSH2_AGENTC_ADD_IDENTITY 17 |
309 | #define SSH2_AGENTC_REMOVE_IDENTITY 18 |
310 | #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19 |