]> asedeno.scripts.mit.edu Git - PuTTY_svn.git/blob - ssh.h
aafee4e305d6a0f0ca644ec5f8743a198acd5e76
[PuTTY_svn.git] / ssh.h
1 #include <string.h>
2
3 #include "puttymem.h"
4
5 /*
6  * Useful thing.
7  */
8 #ifndef lenof
9 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
10 #endif
11
12 #define SSH_CIPHER_IDEA         1
13 #define SSH_CIPHER_DES          2
14 #define SSH_CIPHER_3DES         3
15 #define SSH_CIPHER_BLOWFISH     6
16
17 #ifdef MSCRYPTOAPI
18 #define APIEXTRA 8
19 #else
20 #define APIEXTRA 0
21 #endif
22
23 #ifndef BIGNUM_INTERNAL
24 typedef void *Bignum;
25 #endif
26
27 struct RSAKey {
28     int bits;
29     int bytes;
30 #ifdef MSCRYPTOAPI
31     unsigned long exponent;
32     unsigned char *modulus;
33 #else
34     Bignum modulus;
35     Bignum exponent;
36     Bignum private_exponent;
37 #endif
38     char *comment;
39 };
40
41 struct RSAAux {
42     Bignum p;
43     Bignum q;
44     Bignum iqmp;
45 };
46
47 int makekey(unsigned char *data, struct RSAKey *result,
48             unsigned char **keystr, int order);
49 int makeprivate(unsigned char *data, struct RSAKey *result);
50 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
51 Bignum rsadecrypt(Bignum input, struct RSAKey *key);
52 void rsasign(unsigned char *data, int length, struct RSAKey *key);
53 void rsasanitise(struct RSAKey *key);
54 int rsastr_len(struct RSAKey *key);
55 void rsastr_fmt(char *str, struct RSAKey *key);
56 void rsa_fingerprint(char *str, int len, struct RSAKey *key);
57 void freersakey(struct RSAKey *key);
58
59 typedef unsigned int word32;
60 typedef unsigned int uint32;
61
62 unsigned long crc32(const void *s, size_t len);
63
64 typedef struct {
65     uint32 h[4];
66 } MD5_Core_State;
67
68 struct MD5Context {
69 #ifdef MSCRYPTOAPI
70     unsigned long hHash;
71 #else
72     MD5_Core_State core;
73     unsigned char block[64];
74     int blkused;
75     uint32 lenhi, lenlo;
76 #endif
77 };
78
79 void MD5Init(struct MD5Context *context);
80 void MD5Update(struct MD5Context *context, unsigned char const *buf,
81                unsigned len);
82 void MD5Final(unsigned char digest[16], struct MD5Context *context);
83
84 typedef struct {
85     uint32 h[5];
86     unsigned char block[64];
87     int blkused;
88     uint32 lenhi, lenlo;
89 } SHA_State;
90
91 void SHA_Init(SHA_State *s);
92 void SHA_Bytes(SHA_State *s, void *p, int len);
93 void SHA_Final(SHA_State *s, unsigned char *output);
94 void SHA_Simple(void *p, int len, unsigned char *output);
95
96 struct ssh_cipher {
97     void (*sesskey)(unsigned char *key);   /* for ssh 1 */
98     void (*encrypt)(unsigned char *blk, int len);
99     void (*decrypt)(unsigned char *blk, int len);
100     int blksize;
101 };
102
103 struct ssh2_cipher {
104     void (*setcsiv)(unsigned char *key);   /* for ssh 2 */
105     void (*setcskey)(unsigned char *key);   /* for ssh 2 */
106     void (*setsciv)(unsigned char *key);   /* for ssh 2 */
107     void (*setsckey)(unsigned char *key);   /* for ssh 2 */
108     void (*encrypt)(unsigned char *blk, int len);
109     void (*decrypt)(unsigned char *blk, int len);
110     char *name;
111     int blksize;
112     int keylen;
113 };
114
115 struct ssh2_ciphers {
116     int nciphers;
117     struct ssh2_cipher **list;
118 };
119
120 struct ssh_mac {
121     void (*setcskey)(unsigned char *key);
122     void (*setsckey)(unsigned char *key);
123     void (*generate)(unsigned char *blk, int len, unsigned long seq);
124     int (*verify)(unsigned char *blk, int len, unsigned long seq);
125     char *name;
126     int len;
127 };
128
129 struct ssh_kex {
130     /*
131      * Plugging in another KEX algorithm requires structural chaos,
132      * so it's hard to abstract them into nice little structures
133      * like this. Hence, for the moment, this is just a
134      * placeholder. I claim justification in the fact that OpenSSH
135      * does this too :-)
136      */
137     char *name;
138 };
139
140 struct ssh_signkey {
141     void *(*newkey)(char *data, int len);
142     void (*freekey)(void *key);
143     char *(*fmtkey)(void *key);
144     char *(*fingerprint)(void *key);
145     int (*verifysig)(void *key, char *sig, int siglen,
146                      char *data, int datalen);
147     int (*sign)(void *key, char *sig, int siglen,
148                 char *data, int datalen);
149     char *name;
150     char *keytype;                     /* for host key cache */
151 };
152
153 struct ssh_compress {
154     char *name;
155     void (*compress_init)(void);
156     int (*compress)(unsigned char *block, int len,
157                     unsigned char **outblock, int *outlen);
158     void (*decompress_init)(void);
159     int (*decompress)(unsigned char *block, int len,
160                       unsigned char **outblock, int *outlen);
161 };
162
163 #ifndef MSCRYPTOAPI
164 void SHATransform(word32 *digest, word32 *data);
165 #endif
166
167 int random_byte(void);
168 void random_add_noise(void *noise, int length);
169 void random_add_heavynoise(void *noise, int length);
170
171 void logevent (char *);
172
173 Bignum copybn(Bignum b);
174 Bignum bn_power_2(int n);
175 void bn_restore_invariant(Bignum b);
176 Bignum bignum_from_short(unsigned short n);
177 void freebn(Bignum b);
178 Bignum modpow(Bignum base, Bignum exp, Bignum mod);
179 Bignum modmul(Bignum a, Bignum b, Bignum mod);
180 void decbn(Bignum n);
181 extern Bignum Zero, One;
182 Bignum bignum_from_bytes(unsigned char *data, int nbytes);
183 int ssh1_read_bignum(unsigned char *data, Bignum *result);
184 int ssh1_bignum_bitcount(Bignum bn);
185 int ssh1_bignum_length(Bignum bn);
186 int bignum_byte(Bignum bn, int i);
187 int bignum_bit(Bignum bn, int i);
188 void bignum_set_bit(Bignum bn, int i, int value);
189 int ssh1_write_bignum(void *data, Bignum bn);
190 Bignum biggcd(Bignum a, Bignum b);
191 unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
192 Bignum bignum_add_long(Bignum number, unsigned long addend);
193 Bignum bigmul(Bignum a, Bignum b);
194 Bignum modinv(Bignum number, Bignum modulus);
195 Bignum bignum_bitmask(Bignum number);
196 Bignum bignum_rshift(Bignum number, int shift);
197 int bignum_cmp(Bignum a, Bignum b);
198 char *bignum_decimal(Bignum x);
199
200 void dh_setup_group1(void);
201 void dh_setup_group(Bignum pval, Bignum gval);
202 void dh_cleanup(void);
203 Bignum dh_create_e(void);
204 Bignum dh_find_K(Bignum f);
205
206 int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
207                char *passphrase);
208 int rsakey_encrypted(char *filename, char **comment);
209
210 int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
211                char *passphrase);
212
213 void des3_decrypt_pubkey(unsigned char *key,
214                          unsigned char *blk, int len);
215 void des3_encrypt_pubkey(unsigned char *key,
216                          unsigned char *blk, int len);
217
218 /*
219  * For progress updates in the key generation utility.
220  */
221 typedef void (*progfn_t)(void *param, int phase, int progress);
222
223 int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
224                  progfn_t pfn, void *pfnparam);
225 Bignum primegen(int bits, int modulus, int residue,
226                 int phase, progfn_t pfn, void *pfnparam);
227
228 /*
229  * zlib compression.
230  */
231 void zlib_compress_init(void);
232 void zlib_decompress_init(void);
233 int zlib_compress_block(unsigned char *block, int len,
234                         unsigned char **outblock, int *outlen);
235 int zlib_decompress_block(unsigned char *block, int len,
236                           unsigned char **outblock, int *outlen);