SSH2 MACs now use dynamically allocated contexts.
[u/mdw/putty] / ssh.h
CommitLineData
37508af4 1#include <string.h>
2
dcbde236 3#include "puttymem.h"
d74d141c 4#include "network.h"
5c72ca61 5#include "int64.h"
dcbde236 6
2871113a 7struct ssh_channel;
8
5471d09a 9extern void sshfwd_close(struct ssh_channel *c);
10extern int sshfwd_write(struct ssh_channel *c, char *, int);
11extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize);
12
e5574168 13/*
14 * Useful thing.
15 */
16#ifndef lenof
17#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
18#endif
19
bea1ef5f 20#define SSH_CIPHER_IDEA 1
9697bfd2 21#define SSH_CIPHER_DES 2
bea1ef5f 22#define SSH_CIPHER_3DES 3
23#define SSH_CIPHER_BLOWFISH 6
24
e5574168 25#ifdef MSCRYPTOAPI
26#define APIEXTRA 8
27#else
28#define APIEXTRA 0
29#endif
30
3709bfe9 31#ifndef BIGNUM_INTERNAL
32typedef void *Bignum;
33#endif
7cca0d81 34
374330e2 35struct RSAKey {
36 int bits;
37 int bytes;
8f203108 38#ifdef MSCRYPTOAPI
39 unsigned long exponent;
40 unsigned char *modulus;
41#else
7cca0d81 42 Bignum modulus;
43 Bignum exponent;
44 Bignum private_exponent;
9400cf6f 45 Bignum p;
46 Bignum q;
47 Bignum iqmp;
65a22376 48#endif
49 char *comment;
9400cf6f 50};
51
5c72ca61 52struct dss_key {
53 Bignum p, q, g, y, x;
54};
55
374330e2 56int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 57 unsigned char **keystr, int order);
58int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 59void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 60Bignum rsadecrypt(Bignum input, struct RSAKey *key);
61void rsasign(unsigned char *data, int length, struct RSAKey *key);
62void rsasanitise(struct RSAKey *key);
374330e2 63int rsastr_len(struct RSAKey *key);
64void rsastr_fmt(char *str, struct RSAKey *key);
1c2a93c4 65void rsa_fingerprint(char *str, int len, struct RSAKey *key);
014970c8 66int rsa_verify(struct RSAKey *key);
3f2d010c 67unsigned char *rsa_public_blob(struct RSAKey *key, int *len);
68int rsa_public_blob_len(void *data);
5c58ad2d 69void freersakey(struct RSAKey *key);
374330e2 70
71typedef unsigned int word32;
72typedef unsigned int uint32;
73
8160fbfc 74unsigned long crc32(const void *s, size_t len);
9a3a93a5 75unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
76
77/* SSH CRC compensation attack detector */
78int detect_attack(unsigned char *buf, uint32 len, unsigned char *IV);
374330e2 79
48672163 80typedef struct {
81 uint32 h[4];
82} MD5_Core_State;
83
374330e2 84struct MD5Context {
8f203108 85#ifdef MSCRYPTOAPI
86 unsigned long hHash;
87#else
48672163 88 MD5_Core_State core;
bb093ca7 89 unsigned char block[64];
48672163 90 int blkused;
91 uint32 lenhi, lenlo;
8f203108 92#endif
374330e2 93};
94
95void MD5Init(struct MD5Context *context);
96void MD5Update(struct MD5Context *context, unsigned char const *buf,
32874aea 97 unsigned len);
374330e2 98void MD5Final(unsigned char digest[16], struct MD5Context *context);
99
e5574168 100typedef struct {
101 uint32 h[5];
102 unsigned char block[64];
103 int blkused;
104 uint32 lenhi, lenlo;
105} SHA_State;
32874aea 106void SHA_Init(SHA_State * s);
107void SHA_Bytes(SHA_State * s, void *p, int len);
108void SHA_Final(SHA_State * s, unsigned char *output);
7cca0d81 109void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 110
5c72ca61 111void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
112 unsigned char *output);
113
114typedef struct {
115 uint64 h[8];
116 unsigned char block[128];
117 int blkused;
118 uint32 len[4];
119} SHA512_State;
120void SHA512_Init(SHA512_State * s);
121void SHA512_Bytes(SHA512_State * s, const void *p, int len);
122void SHA512_Final(SHA512_State * s, unsigned char *output);
123void SHA512_Simple(const void *p, int len, unsigned char *output);
124
374330e2 125struct ssh_cipher {
371e569c 126 void *(*make_context)(void);
127 void (*free_context)(void *);
128 void (*sesskey) (void *, unsigned char *key); /* for ssh 1 */
129 void (*encrypt) (void *, unsigned char *blk, int len);
130 void (*decrypt) (void *, unsigned char *blk, int len);
0a3f1d48 131 int blksize;
371e569c 132 char *text_name;
0a3f1d48 133};
134
135struct ssh2_cipher {
371e569c 136 void *(*make_context)(void);
137 void (*free_context)(void *);
138 void (*setiv) (void *, unsigned char *key); /* for ssh 2 */
139 void (*setkey) (void *, unsigned char *key);/* for ssh 2 */
140 void (*encrypt) (void *, unsigned char *blk, int len);
141 void (*decrypt) (void *, unsigned char *blk, int len);
e5574168 142 char *name;
143 int blksize;
d2a0e0be 144 int keylen;
371e569c 145 char *text_name;
e5574168 146};
147
0a3f1d48 148struct ssh2_ciphers {
149 int nciphers;
65a22376 150 const struct ssh2_cipher *const *list;
0a3f1d48 151};
152
e5574168 153struct ssh_mac {
e0e1a00d 154 void *(*make_context)(void);
155 void (*free_context)(void *);
156 void (*setkey) (void *, unsigned char *key);
157 void (*generate) (void *, unsigned char *blk, int len, unsigned long seq);
158 int (*verify) (void *, unsigned char *blk, int len, unsigned long seq);
e5574168 159 char *name;
160 int len;
161};
162
163struct ssh_kex {
7cca0d81 164 /*
165 * Plugging in another KEX algorithm requires structural chaos,
166 * so it's hard to abstract them into nice little structures
167 * like this. Hence, for the moment, this is just a
168 * placeholder. I claim justification in the fact that OpenSSH
169 * does this too :-)
170 */
e5574168 171 char *name;
172};
173
e055a386 174struct ssh_signkey {
32874aea 175 void *(*newkey) (char *data, int len);
176 void (*freekey) (void *key);
177 char *(*fmtkey) (void *key);
178 unsigned char *(*public_blob) (void *key, int *len);
179 unsigned char *(*private_blob) (void *key, int *len);
180 void *(*createkey) (unsigned char *pub_blob, int pub_len,
181 unsigned char *priv_blob, int priv_len);
182 void *(*openssh_createkey) (unsigned char **blob, int *len);
183 int (*openssh_fmtkey) (void *key, unsigned char *blob, int len);
184 char *(*fingerprint) (void *key);
185 int (*verifysig) (void *key, char *sig, int siglen,
186 char *data, int datalen);
187 unsigned char *(*sign) (void *key, char *data, int datalen,
188 int *siglen);
e5574168 189 char *name;
32874aea 190 char *keytype; /* for host key cache */
e5574168 191};
192
193struct ssh_compress {
194 char *name;
32874aea 195 void (*compress_init) (void);
196 int (*compress) (unsigned char *block, int len,
197 unsigned char **outblock, int *outlen);
198 void (*decompress_init) (void);
199 int (*decompress) (unsigned char *block, int len,
200 unsigned char **outblock, int *outlen);
201 int (*disable_compression) (void);
374330e2 202};
203
65a22376 204struct ssh2_userkey {
205 const struct ssh_signkey *alg; /* the key algorithm */
206 void *data; /* the key data */
207 char *comment; /* the key comment */
208};
209
210extern const struct ssh_cipher ssh_3des;
211extern const struct ssh_cipher ssh_des;
212extern const struct ssh_cipher ssh_blowfish_ssh1;
213extern const struct ssh2_ciphers ssh2_3des;
0d7c43a6 214extern const struct ssh2_ciphers ssh2_des;
65a22376 215extern const struct ssh2_ciphers ssh2_aes;
216extern const struct ssh2_ciphers ssh2_blowfish;
217extern const struct ssh_kex ssh_diffiehellman;
218extern const struct ssh_kex ssh_diffiehellman_gex;
219extern const struct ssh_signkey ssh_dss;
220extern const struct ssh_signkey ssh_rsa;
221extern const struct ssh_mac ssh_md5;
222extern const struct ssh_mac ssh_sha1;
223extern const struct ssh_mac ssh_sha1_buggy;
224
900a4ee6 225/*
226 * PuTTY version number formatted as an SSH version string.
227 */
228extern char sshver[];
229
fd5e5847 230/*
231 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
232 * that fails. This variable is the means by which scp.c can reach
233 * into the SSH code and find out which one it got.
234 */
51470298 235extern int ssh_fallback_cmd(void *handle);
fd5e5847 236
8f203108 237#ifndef MSCRYPTOAPI
32874aea 238void SHATransform(word32 * digest, word32 * data);
8f203108 239#endif
374330e2 240
241int random_byte(void);
242void random_add_noise(void *noise, int length);
6e522441 243void random_add_heavynoise(void *noise, int length);
c5e9c988 244
32874aea 245void logevent(char *);
51470298 246
247/* Allocate and register a new channel for port forwarding */
248void *new_sock_channel(void *handle, Socket s);
249void ssh_send_port_open(void *handle, void *channel,
250 char *hostname, int port, char *org);
e5574168 251
7cca0d81 252Bignum copybn(Bignum b);
3709bfe9 253Bignum bn_power_2(int n);
254void bn_restore_invariant(Bignum b);
5c72ca61 255Bignum bignum_from_long(unsigned long n);
e5574168 256void freebn(Bignum b);
59600f67 257Bignum modpow(Bignum base, Bignum exp, Bignum mod);
258Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 259void decbn(Bignum n);
260extern Bignum Zero, One;
3709bfe9 261Bignum bignum_from_bytes(unsigned char *data, int nbytes);
32874aea 262int ssh1_read_bignum(unsigned char *data, Bignum * result);
ddecd643 263int bignum_bitcount(Bignum bn);
5c58ad2d 264int ssh1_bignum_length(Bignum bn);
260f3dec 265int ssh2_bignum_length(Bignum bn);
5c58ad2d 266int bignum_byte(Bignum bn, int i);
9400cf6f 267int bignum_bit(Bignum bn, int i);
268void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 269int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 270Bignum biggcd(Bignum a, Bignum b);
271unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
272Bignum bignum_add_long(Bignum number, unsigned long addend);
273Bignum bigmul(Bignum a, Bignum b);
5c72ca61 274Bignum bigmuladd(Bignum a, Bignum b, Bignum addend);
275Bignum bigdiv(Bignum a, Bignum b);
276Bignum bigmod(Bignum a, Bignum b);
9400cf6f 277Bignum modinv(Bignum number, Bignum modulus);
3709bfe9 278Bignum bignum_bitmask(Bignum number);
9400cf6f 279Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 280int bignum_cmp(Bignum a, Bignum b);
6e522441 281char *bignum_decimal(Bignum x);
e5574168 282
3709bfe9 283void dh_setup_group1(void);
a92dd380 284void dh_setup_group(Bignum pval, Bignum gval);
3709bfe9 285void dh_cleanup(void);
7bd5a860 286Bignum dh_create_e(int nbits);
e5574168 287Bignum dh_find_K(Bignum f);
7cca0d81 288
65a22376 289int loadrsakey(char *filename, struct RSAKey *key, char *passphrase);
a52f067e 290int rsakey_encrypted(char *filename, char **comment);
3f2d010c 291int rsakey_pubblob(char *filename, void **blob, int *bloblen);
7cca0d81 292
65a22376 293int saversakey(char *filename, struct RSAKey *key, char *passphrase);
294
295void base64_encode_atom(unsigned char *data, int n, char *out);
296
297/* ssh2_load_userkey can return this as an error */
298extern struct ssh2_userkey ssh2_wrong_passphrase;
299#define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase)
300
301int ssh2_userkey_encrypted(char *filename, char **comment);
302struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase);
32874aea 303char *ssh2_userkey_loadpub(char *filename, char **algorithm,
304 int *pub_blob_len);
305int ssh2_save_userkey(char *filename, struct ssh2_userkey *key,
306 char *passphrase);
65a22376 307
231ee168 308enum {
309 SSH_KEYTYPE_UNOPENABLE,
310 SSH_KEYTYPE_UNKNOWN,
311 SSH_KEYTYPE_SSH1, SSH_KEYTYPE_SSH2,
312 SSH_KEYTYPE_OPENSSH, SSH_KEYTYPE_SSHCOM
313};
314int key_type(char *filename);
315char *key_type_to_str(int type);
6e522441 316
9dda6459 317int import_possible(int type);
318int import_target_type(int type);
319int import_encrypted(char *filename, int type, char **comment);
320int import_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase);
321struct ssh2_userkey *import_ssh2(char *filename, int type, char *passphrase);
4801c01c 322int export_ssh1(char *filename, int type, struct RSAKey *key,char *passphrase);
323int export_ssh2(char *filename, int type,
324 struct ssh2_userkey *key, char *passphrase);
9dda6459 325
65a22376 326void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
327void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
9dda6459 328void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
329 unsigned char *blk, int len);
330void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
331 unsigned char *blk, int len);
32874aea 332void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
333 int len);
334void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
335 int len);
9400cf6f 336
337/*
338 * For progress updates in the key generation utility.
339 */
7c7f6893 340#define PROGFN_INITIALISE 1
341#define PROGFN_LIN_PHASE 2
342#define PROGFN_EXP_PHASE 3
343#define PROGFN_PHASE_EXTENT 4
344#define PROGFN_READY 5
345#define PROGFN_PROGRESS 6
5c72ca61 346typedef void (*progfn_t) (void *param, int action, int phase, int progress);
9400cf6f 347
32874aea 348int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
349 void *pfnparam);
5c72ca61 350int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
351 void *pfnparam);
352Bignum primegen(int bits, int modulus, int residue, Bignum factor,
353 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 354
d74d141c 355
4ba9b64b 356/*
357 * zlib compression.
358 */
359void zlib_compress_init(void);
360void zlib_decompress_init(void);
361int zlib_compress_block(unsigned char *block, int len,
362 unsigned char **outblock, int *outlen);
363int zlib_decompress_block(unsigned char *block, int len,
364 unsigned char **outblock, int *outlen);
1983e559 365
366/*
367 * SSH1 agent messages.
368 */
369#define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
370#define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
371#define SSH1_AGENTC_RSA_CHALLENGE 3
372#define SSH1_AGENT_RSA_RESPONSE 4
373#define SSH1_AGENTC_ADD_RSA_IDENTITY 7
374#define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
32874aea 375#define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
1983e559 376
377/*
378 * Messages common to SSH1 and OpenSSH's SSH2.
379 */
380#define SSH_AGENT_FAILURE 5
381#define SSH_AGENT_SUCCESS 6
382
383/*
384 * OpenSSH's SSH2 agent messages.
385 */
386#define SSH2_AGENTC_REQUEST_IDENTITIES 11
387#define SSH2_AGENT_IDENTITIES_ANSWER 12
388#define SSH2_AGENTC_SIGN_REQUEST 13
389#define SSH2_AGENT_SIGN_RESPONSE 14
390#define SSH2_AGENTC_ADD_IDENTITY 17
391#define SSH2_AGENTC_REMOVE_IDENTITY 18
392#define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
7bedb13c 393
394/*
395 * Need this to warn about support for the original SSH2 keyfile
396 * format.
397 */
398void old_keyfile_warning(void);