More options for bignum debugging
[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 */
0a3f1d48 98 void (*encrypt)(unsigned char *blk, int len);
99 void (*decrypt)(unsigned char *blk, int len);
100 int blksize;
101};
102
103struct ssh2_cipher {
d39f364a 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 */
374330e2 108 void (*encrypt)(unsigned char *blk, int len);
109 void (*decrypt)(unsigned char *blk, int len);
e5574168 110 char *name;
111 int blksize;
d2a0e0be 112 int keylen;
e5574168 113};
114
0a3f1d48 115struct ssh2_ciphers {
116 int nciphers;
117 struct ssh2_cipher **list;
118};
119
e5574168 120struct ssh_mac {
d39f364a 121 void (*setcskey)(unsigned char *key);
122 void (*setsckey)(unsigned char *key);
e5574168 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
129struct ssh_kex {
7cca0d81 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 */
e5574168 137 char *name;
138};
139
e055a386 140struct 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);
e5574168 149 char *name;
d5859615 150 char *keytype; /* for host key cache */
e5574168 151};
152
153struct ssh_compress {
154 char *name;
4ba9b64b 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);
374330e2 161};
162
8f203108 163#ifndef MSCRYPTOAPI
374330e2 164void SHATransform(word32 *digest, word32 *data);
8f203108 165#endif
374330e2 166
167int random_byte(void);
168void random_add_noise(void *noise, int length);
6e522441 169void random_add_heavynoise(void *noise, int length);
c5e9c988 170
171void logevent (char *);
e5574168 172
7cca0d81 173Bignum copybn(Bignum b);
3709bfe9 174Bignum bn_power_2(int n);
175void bn_restore_invariant(Bignum b);
9400cf6f 176Bignum bignum_from_short(unsigned short n);
e5574168 177void freebn(Bignum b);
59600f67 178Bignum modpow(Bignum base, Bignum exp, Bignum mod);
179Bignum modmul(Bignum a, Bignum b, Bignum mod);
7cca0d81 180void decbn(Bignum n);
181extern Bignum Zero, One;
3709bfe9 182Bignum bignum_from_bytes(unsigned char *data, int nbytes);
7cca0d81 183int ssh1_read_bignum(unsigned char *data, Bignum *result);
5c58ad2d 184int ssh1_bignum_bitcount(Bignum bn);
185int ssh1_bignum_length(Bignum bn);
186int bignum_byte(Bignum bn, int i);
9400cf6f 187int bignum_bit(Bignum bn, int i);
188void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 189int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 190Bignum biggcd(Bignum a, Bignum b);
191unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
192Bignum bignum_add_long(Bignum number, unsigned long addend);
193Bignum bigmul(Bignum a, Bignum b);
194Bignum modinv(Bignum number, Bignum modulus);
3709bfe9 195Bignum bignum_bitmask(Bignum number);
9400cf6f 196Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 197int bignum_cmp(Bignum a, Bignum b);
6e522441 198char *bignum_decimal(Bignum x);
e5574168 199
3709bfe9 200void dh_setup_group1(void);
a92dd380 201void dh_setup_group(Bignum pval, Bignum gval);
3709bfe9 202void dh_cleanup(void);
e5574168 203Bignum dh_create_e(void);
204Bignum dh_find_K(Bignum f);
7cca0d81 205
6e522441 206int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
207 char *passphrase);
a52f067e 208int rsakey_encrypted(char *filename, char **comment);
7cca0d81 209
6e522441 210int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
211 char *passphrase);
212
7cca0d81 213void des3_decrypt_pubkey(unsigned char *key,
5c58ad2d 214 unsigned char *blk, int len);
6e522441 215void des3_encrypt_pubkey(unsigned char *key,
216 unsigned char *blk, int len);
9400cf6f 217
218/*
219 * For progress updates in the key generation utility.
220 */
221typedef void (*progfn_t)(void *param, int phase, int progress);
222
223int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
224 progfn_t pfn, void *pfnparam);
225Bignum primegen(int bits, int modulus, int residue,
226 int phase, progfn_t pfn, void *pfnparam);
4ba9b64b 227
228/*
229 * zlib compression.
230 */
231void zlib_compress_init(void);
232void zlib_decompress_init(void);
233int zlib_compress_block(unsigned char *block, int len,
234 unsigned char **outblock, int *outlen);
235int zlib_decompress_block(unsigned char *block, int len,
236 unsigned char **outblock, int *outlen);