Stop accidental subsystem attempts
[u/mdw/putty] / ssh.h
CommitLineData
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
7cca0d81 23/*
24 * A Bignum is stored as a sequence of `unsigned short' words. The
25 * first tells how many remain; the remaining ones are digits, LS
26 * first.
27 */
28typedef unsigned short *Bignum;
29
374330e2 30struct RSAKey {
31 int bits;
32 int bytes;
8f203108 33#ifdef MSCRYPTOAPI
34 unsigned long exponent;
35 unsigned char *modulus;
36#else
7cca0d81 37 Bignum modulus;
38 Bignum exponent;
39 Bignum private_exponent;
8f203108 40#endif
5c58ad2d 41 char *comment;
374330e2 42};
43
9400cf6f 44struct RSAAux {
45 Bignum p;
46 Bignum q;
47 Bignum iqmp;
48};
49
374330e2 50int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 51 unsigned char **keystr, int order);
52int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 53void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 54Bignum rsadecrypt(Bignum input, struct RSAKey *key);
55void rsasign(unsigned char *data, int length, struct RSAKey *key);
56void rsasanitise(struct RSAKey *key);
374330e2 57int rsastr_len(struct RSAKey *key);
58void rsastr_fmt(char *str, struct RSAKey *key);
1c2a93c4 59void rsa_fingerprint(char *str, int len, struct RSAKey *key);
5c58ad2d 60void freersakey(struct RSAKey *key);
374330e2 61
62typedef unsigned int word32;
63typedef unsigned int uint32;
64
8160fbfc 65unsigned long crc32(const void *s, size_t len);
374330e2 66
48672163 67typedef struct {
68 uint32 h[4];
69} MD5_Core_State;
70
374330e2 71struct MD5Context {
8f203108 72#ifdef MSCRYPTOAPI
73 unsigned long hHash;
74#else
48672163 75 MD5_Core_State core;
bb093ca7 76 unsigned char block[64];
48672163 77 int blkused;
78 uint32 lenhi, lenlo;
8f203108 79#endif
374330e2 80};
81
82void MD5Init(struct MD5Context *context);
83void MD5Update(struct MD5Context *context, unsigned char const *buf,
84 unsigned len);
85void MD5Final(unsigned char digest[16], struct MD5Context *context);
86
e5574168 87typedef struct {
88 uint32 h[5];
89 unsigned char block[64];
90 int blkused;
91 uint32 lenhi, lenlo;
92} SHA_State;
93
94void SHA_Init(SHA_State *s);
95void SHA_Bytes(SHA_State *s, void *p, int len);
96void SHA_Final(SHA_State *s, unsigned char *output);
7cca0d81 97void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 98
374330e2 99struct ssh_cipher {
d39f364a 100 void (*sesskey)(unsigned char *key); /* for ssh 1 */
101 void (*setcsiv)(unsigned char *key); /* for ssh 2 */
102 void (*setcskey)(unsigned char *key); /* for ssh 2 */
103 void (*setsciv)(unsigned char *key); /* for ssh 2 */
104 void (*setsckey)(unsigned char *key); /* for ssh 2 */
374330e2 105 void (*encrypt)(unsigned char *blk, int len);
106 void (*decrypt)(unsigned char *blk, int len);
e5574168 107 char *name;
108 int blksize;
109};
110
111struct ssh_mac {
d39f364a 112 void (*setcskey)(unsigned char *key);
113 void (*setsckey)(unsigned char *key);
e5574168 114 void (*generate)(unsigned char *blk, int len, unsigned long seq);
115 int (*verify)(unsigned char *blk, int len, unsigned long seq);
116 char *name;
117 int len;
118};
119
120struct ssh_kex {
7cca0d81 121 /*
122 * Plugging in another KEX algorithm requires structural chaos,
123 * so it's hard to abstract them into nice little structures
124 * like this. Hence, for the moment, this is just a
125 * placeholder. I claim justification in the fact that OpenSSH
126 * does this too :-)
127 */
e5574168 128 char *name;
129};
130
e055a386 131struct ssh_signkey {
132 void *(*newkey)(char *data, int len);
133 void (*freekey)(void *key);
134 char *(*fmtkey)(void *key);
135 char *(*fingerprint)(void *key);
136 int (*verifysig)(void *key, char *sig, int siglen,
137 char *data, int datalen);
138 int (*sign)(void *key, char *sig, int siglen,
139 char *data, int datalen);
e5574168 140 char *name;
d5859615 141 char *keytype; /* for host key cache */
e5574168 142};
143
144struct ssh_compress {
145 char *name;
4ba9b64b 146 void (*compress_init)(void);
147 int (*compress)(unsigned char *block, int len,
148 unsigned char **outblock, int *outlen);
149 void (*decompress_init)(void);
150 int (*decompress)(unsigned char *block, int len,
151 unsigned char **outblock, int *outlen);
374330e2 152};
153
8f203108 154#ifndef MSCRYPTOAPI
374330e2 155void SHATransform(word32 *digest, word32 *data);
8f203108 156#endif
374330e2 157
158int random_byte(void);
159void random_add_noise(void *noise, int length);
6e522441 160void random_add_heavynoise(void *noise, int length);
c5e9c988 161
162void logevent (char *);
e5574168 163
e5574168 164Bignum newbn(int length);
7cca0d81 165Bignum copybn(Bignum b);
9400cf6f 166Bignum bignum_from_short(unsigned short n);
e5574168 167void freebn(Bignum b);
59600f67 168Bignum modpow(Bignum base, Bignum exp, Bignum mod);
169Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 170void decbn(Bignum n);
171extern Bignum Zero, One;
172int ssh1_read_bignum(unsigned char *data, Bignum *result);
5c58ad2d 173int ssh1_bignum_bitcount(Bignum bn);
174int ssh1_bignum_length(Bignum bn);
175int bignum_byte(Bignum bn, int i);
9400cf6f 176int bignum_bit(Bignum bn, int i);
177void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 178int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 179Bignum biggcd(Bignum a, Bignum b);
180unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
181Bignum bignum_add_long(Bignum number, unsigned long addend);
182Bignum bigmul(Bignum a, Bignum b);
183Bignum modinv(Bignum number, Bignum modulus);
184Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 185int bignum_cmp(Bignum a, Bignum b);
6e522441 186char *bignum_decimal(Bignum x);
e5574168 187
188Bignum dh_create_e(void);
189Bignum dh_find_K(Bignum f);
7cca0d81 190
6e522441 191int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
192 char *passphrase);
a52f067e 193int rsakey_encrypted(char *filename, char **comment);
7cca0d81 194
6e522441 195int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
196 char *passphrase);
197
7cca0d81 198void des3_decrypt_pubkey(unsigned char *key,
5c58ad2d 199 unsigned char *blk, int len);
6e522441 200void des3_encrypt_pubkey(unsigned char *key,
201 unsigned char *blk, int len);
9400cf6f 202
203/*
204 * For progress updates in the key generation utility.
205 */
206typedef void (*progfn_t)(void *param, int phase, int progress);
207
208int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
209 progfn_t pfn, void *pfnparam);
210Bignum primegen(int bits, int modulus, int residue,
211 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 212
213/*
214 * zlib compression.
215 */
216void zlib_compress_init(void);
217void zlib_decompress_init(void);
218int zlib_compress_block(unsigned char *block, int len,
219 unsigned char **outblock, int *outlen);
220int zlib_decompress_block(unsigned char *block, int len,
221 unsigned char **outblock, int *outlen);