]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ssh.h
Remove the last lingering knowledge, outside sshbn.c, of the
[PuTTY.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 (*setcsiv)(unsigned char *key);   /* for ssh 2 */
99     void (*setcskey)(unsigned char *key);   /* for ssh 2 */
100     void (*setsciv)(unsigned char *key);   /* for ssh 2 */
101     void (*setsckey)(unsigned char *key);   /* for ssh 2 */
102     void (*encrypt)(unsigned char *blk, int len);
103     void (*decrypt)(unsigned char *blk, int len);
104     char *name;
105     int blksize;
106 };
107
108 struct ssh_mac {
109     void (*setcskey)(unsigned char *key);
110     void (*setsckey)(unsigned char *key);
111     void (*generate)(unsigned char *blk, int len, unsigned long seq);
112     int (*verify)(unsigned char *blk, int len, unsigned long seq);
113     char *name;
114     int len;
115 };
116
117 struct ssh_kex {
118     /*
119      * Plugging in another KEX algorithm requires structural chaos,
120      * so it's hard to abstract them into nice little structures
121      * like this. Hence, for the moment, this is just a
122      * placeholder. I claim justification in the fact that OpenSSH
123      * does this too :-)
124      */
125     char *name;
126 };
127
128 struct ssh_signkey {
129     void *(*newkey)(char *data, int len);
130     void (*freekey)(void *key);
131     char *(*fmtkey)(void *key);
132     char *(*fingerprint)(void *key);
133     int (*verifysig)(void *key, char *sig, int siglen,
134                      char *data, int datalen);
135     int (*sign)(void *key, char *sig, int siglen,
136                 char *data, int datalen);
137     char *name;
138     char *keytype;                     /* for host key cache */
139 };
140
141 struct ssh_compress {
142     char *name;
143     void (*compress_init)(void);
144     int (*compress)(unsigned char *block, int len,
145                     unsigned char **outblock, int *outlen);
146     void (*decompress_init)(void);
147     int (*decompress)(unsigned char *block, int len,
148                       unsigned char **outblock, int *outlen);
149 };
150
151 #ifndef MSCRYPTOAPI
152 void SHATransform(word32 *digest, word32 *data);
153 #endif
154
155 int random_byte(void);
156 void random_add_noise(void *noise, int length);
157 void random_add_heavynoise(void *noise, int length);
158
159 void logevent (char *);
160
161 Bignum copybn(Bignum b);
162 Bignum bn_power_2(int n);
163 void bn_restore_invariant(Bignum b);
164 Bignum bignum_from_short(unsigned short n);
165 void freebn(Bignum b);
166 Bignum modpow(Bignum base, Bignum exp, Bignum mod);
167 Bignum modmul(Bignum a, Bignum b, Bignum mod);
168 void decbn(Bignum n);
169 extern Bignum Zero, One;
170 Bignum bignum_from_bytes(unsigned char *data, int nbytes);
171 int ssh1_read_bignum(unsigned char *data, Bignum *result);
172 int ssh1_bignum_bitcount(Bignum bn);
173 int ssh1_bignum_length(Bignum bn);
174 int bignum_byte(Bignum bn, int i);
175 int bignum_bit(Bignum bn, int i);
176 void bignum_set_bit(Bignum bn, int i, int value);
177 int ssh1_write_bignum(void *data, Bignum bn);
178 Bignum biggcd(Bignum a, Bignum b);
179 unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
180 Bignum bignum_add_long(Bignum number, unsigned long addend);
181 Bignum bigmul(Bignum a, Bignum b);
182 Bignum modinv(Bignum number, Bignum modulus);
183 Bignum bignum_bitmask(Bignum number);
184 Bignum bignum_rshift(Bignum number, int shift);
185 int bignum_cmp(Bignum a, Bignum b);
186 char *bignum_decimal(Bignum x);
187
188 void dh_setup_group1(void);
189 void dh_cleanup(void);
190 Bignum dh_create_e(void);
191 Bignum dh_find_K(Bignum f);
192
193 int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
194                char *passphrase);
195 int rsakey_encrypted(char *filename, char **comment);
196
197 int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
198                char *passphrase);
199
200 void des3_decrypt_pubkey(unsigned char *key,
201                          unsigned char *blk, int len);
202 void des3_encrypt_pubkey(unsigned char *key,
203                          unsigned char *blk, int len);
204
205 /*
206  * For progress updates in the key generation utility.
207  */
208 typedef void (*progfn_t)(void *param, int phase, int progress);
209
210 int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
211                  progfn_t pfn, void *pfnparam);
212 Bignum primegen(int bits, int modulus, int residue,
213                 int phase, progfn_t pfn, void *pfnparam);
214
215 /*
216  * zlib compression.
217  */
218 void zlib_compress_init(void);
219 void zlib_decompress_init(void);
220 int zlib_compress_block(unsigned char *block, int len,
221                         unsigned char **outblock, int *outlen);
222 int zlib_decompress_block(unsigned char *block, int len,
223                           unsigned char **outblock, int *outlen);