Add AES support in SSH2. Not yet complete: there's no way to select
[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
3709bfe9 23#ifndef BIGNUM_INTERNAL
24typedef void *Bignum;
25#endif
7cca0d81 26
374330e2 27struct 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;
8f203108 37#endif
5c58ad2d 38 char *comment;
374330e2 39};
40
9400cf6f 41struct RSAAux {
42 Bignum p;
43 Bignum q;
44 Bignum iqmp;
45};
46
374330e2 47int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 48 unsigned char **keystr, int order);
49int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 50void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 51Bignum rsadecrypt(Bignum input, struct RSAKey *key);
52void rsasign(unsigned char *data, int length, struct RSAKey *key);
53void rsasanitise(struct RSAKey *key);
374330e2 54int rsastr_len(struct RSAKey *key);
55void rsastr_fmt(char *str, struct RSAKey *key);
1c2a93c4 56void rsa_fingerprint(char *str, int len, struct RSAKey *key);
5c58ad2d 57void freersakey(struct RSAKey *key);
374330e2 58
59typedef unsigned int word32;
60typedef unsigned int uint32;
61
8160fbfc 62unsigned long crc32(const void *s, size_t len);
374330e2 63
48672163 64typedef struct {
65 uint32 h[4];
66} MD5_Core_State;
67
374330e2 68struct MD5Context {
8f203108 69#ifdef MSCRYPTOAPI
70 unsigned long hHash;
71#else
48672163 72 MD5_Core_State core;
bb093ca7 73 unsigned char block[64];
48672163 74 int blkused;
75 uint32 lenhi, lenlo;
8f203108 76#endif
374330e2 77};
78
79void MD5Init(struct MD5Context *context);
80void MD5Update(struct MD5Context *context, unsigned char const *buf,
81 unsigned len);
82void MD5Final(unsigned char digest[16], struct MD5Context *context);
83
e5574168 84typedef struct {
85 uint32 h[5];
86 unsigned char block[64];
87 int blkused;
88 uint32 lenhi, lenlo;
89} SHA_State;
90
91void SHA_Init(SHA_State *s);
92void SHA_Bytes(SHA_State *s, void *p, int len);
93void SHA_Final(SHA_State *s, unsigned char *output);
7cca0d81 94void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 95
374330e2 96struct ssh_cipher {
d39f364a 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 */
374330e2 102 void (*encrypt)(unsigned char *blk, int len);
103 void (*decrypt)(unsigned char *blk, int len);
e5574168 104 char *name;
105 int blksize;
d2a0e0be 106 int keylen;
e5574168 107};
108
109struct ssh_mac {
d39f364a 110 void (*setcskey)(unsigned char *key);
111 void (*setsckey)(unsigned char *key);
e5574168 112 void (*generate)(unsigned char *blk, int len, unsigned long seq);
113 int (*verify)(unsigned char *blk, int len, unsigned long seq);
114 char *name;
115 int len;
116};
117
118struct ssh_kex {
7cca0d81 119 /*
120 * Plugging in another KEX algorithm requires structural chaos,
121 * so it's hard to abstract them into nice little structures
122 * like this. Hence, for the moment, this is just a
123 * placeholder. I claim justification in the fact that OpenSSH
124 * does this too :-)
125 */
e5574168 126 char *name;
127};
128
e055a386 129struct ssh_signkey {
130 void *(*newkey)(char *data, int len);
131 void (*freekey)(void *key);
132 char *(*fmtkey)(void *key);
133 char *(*fingerprint)(void *key);
134 int (*verifysig)(void *key, char *sig, int siglen,
135 char *data, int datalen);
136 int (*sign)(void *key, char *sig, int siglen,
137 char *data, int datalen);
e5574168 138 char *name;
d5859615 139 char *keytype; /* for host key cache */
e5574168 140};
141
142struct ssh_compress {
143 char *name;
4ba9b64b 144 void (*compress_init)(void);
145 int (*compress)(unsigned char *block, int len,
146 unsigned char **outblock, int *outlen);
147 void (*decompress_init)(void);
148 int (*decompress)(unsigned char *block, int len,
149 unsigned char **outblock, int *outlen);
374330e2 150};
151
8f203108 152#ifndef MSCRYPTOAPI
374330e2 153void SHATransform(word32 *digest, word32 *data);
8f203108 154#endif
374330e2 155
156int random_byte(void);
157void random_add_noise(void *noise, int length);
6e522441 158void random_add_heavynoise(void *noise, int length);
c5e9c988 159
160void logevent (char *);
e5574168 161
7cca0d81 162Bignum copybn(Bignum b);
3709bfe9 163Bignum bn_power_2(int n);
164void bn_restore_invariant(Bignum b);
9400cf6f 165Bignum bignum_from_short(unsigned short n);
e5574168 166void freebn(Bignum b);
59600f67 167Bignum modpow(Bignum base, Bignum exp, Bignum mod);
168Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 169void decbn(Bignum n);
170extern Bignum Zero, One;
3709bfe9 171Bignum bignum_from_bytes(unsigned char *data, int nbytes);
7cca0d81 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);
3709bfe9 184Bignum bignum_bitmask(Bignum number);
9400cf6f 185Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 186int bignum_cmp(Bignum a, Bignum b);
6e522441 187char *bignum_decimal(Bignum x);
e5574168 188
3709bfe9 189void dh_setup_group1(void);
a92dd380 190void dh_setup_group(Bignum pval, Bignum gval);
3709bfe9 191void dh_cleanup(void);
e5574168 192Bignum dh_create_e(void);
193Bignum dh_find_K(Bignum f);
7cca0d81 194
6e522441 195int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
196 char *passphrase);
a52f067e 197int rsakey_encrypted(char *filename, char **comment);
7cca0d81 198
6e522441 199int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
200 char *passphrase);
201
7cca0d81 202void des3_decrypt_pubkey(unsigned char *key,
5c58ad2d 203 unsigned char *blk, int len);
6e522441 204void des3_encrypt_pubkey(unsigned char *key,
205 unsigned char *blk, int len);
9400cf6f 206
207/*
208 * For progress updates in the key generation utility.
209 */
210typedef void (*progfn_t)(void *param, int phase, int progress);
211
212int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
213 progfn_t pfn, void *pfnparam);
214Bignum primegen(int bits, int modulus, int residue,
215 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 216
217/*
218 * zlib compression.
219 */
220void zlib_compress_init(void);
221void zlib_decompress_init(void);
222int zlib_compress_block(unsigned char *block, int len,
223 unsigned char **outblock, int *outlen);
224int zlib_decompress_block(unsigned char *block, int len,
225 unsigned char **outblock, int *outlen);