Update DSS implementation to use new bignum routines
[u/mdw/putty] / ssh.h
CommitLineData
37508af4 1#include <string.h>
2
e5574168 3/*
4 * Useful thing.
5 */
6#ifndef lenof
7#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
8#endif
9
bea1ef5f 10#define SSH_CIPHER_IDEA 1
9697bfd2 11#define SSH_CIPHER_DES 2
bea1ef5f 12#define SSH_CIPHER_3DES 3
13#define SSH_CIPHER_BLOWFISH 6
14
e5574168 15#ifdef MSCRYPTOAPI
16#define APIEXTRA 8
17#else
18#define APIEXTRA 0
19#endif
20
7cca0d81 21/*
22 * A Bignum is stored as a sequence of `unsigned short' words. The
23 * first tells how many remain; the remaining ones are digits, LS
24 * first.
25 */
26typedef unsigned short *Bignum;
27
374330e2 28struct RSAKey {
29 int bits;
30 int bytes;
8f203108 31#ifdef MSCRYPTOAPI
32 unsigned long exponent;
33 unsigned char *modulus;
34#else
7cca0d81 35 Bignum modulus;
36 Bignum exponent;
37 Bignum private_exponent;
8f203108 38#endif
5c58ad2d 39 char *comment;
374330e2 40};
41
9400cf6f 42struct RSAAux {
43 Bignum p;
44 Bignum q;
45 Bignum iqmp;
46};
47
374330e2 48int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 49 unsigned char **keystr, int order);
50int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 51void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 52Bignum rsadecrypt(Bignum input, struct RSAKey *key);
53void rsasign(unsigned char *data, int length, struct RSAKey *key);
54void rsasanitise(struct RSAKey *key);
374330e2 55int rsastr_len(struct RSAKey *key);
56void rsastr_fmt(char *str, struct RSAKey *key);
1c2a93c4 57void rsa_fingerprint(char *str, int len, struct RSAKey *key);
5c58ad2d 58void freersakey(struct RSAKey *key);
374330e2 59
60typedef unsigned int word32;
61typedef unsigned int uint32;
62
8160fbfc 63unsigned long crc32(const void *s, size_t len);
374330e2 64
48672163 65typedef struct {
66 uint32 h[4];
67} MD5_Core_State;
68
374330e2 69struct MD5Context {
8f203108 70#ifdef MSCRYPTOAPI
71 unsigned long hHash;
72#else
48672163 73 MD5_Core_State core;
bb093ca7 74 unsigned char block[64];
48672163 75 int blkused;
76 uint32 lenhi, lenlo;
8f203108 77#endif
374330e2 78};
79
80void MD5Init(struct MD5Context *context);
81void MD5Update(struct MD5Context *context, unsigned char const *buf,
82 unsigned len);
83void MD5Final(unsigned char digest[16], struct MD5Context *context);
84
e5574168 85typedef struct {
86 uint32 h[5];
87 unsigned char block[64];
88 int blkused;
89 uint32 lenhi, lenlo;
90} SHA_State;
91
92void SHA_Init(SHA_State *s);
93void SHA_Bytes(SHA_State *s, void *p, int len);
94void SHA_Final(SHA_State *s, unsigned char *output);
7cca0d81 95void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 96
374330e2 97struct ssh_cipher {
d39f364a 98 void (*sesskey)(unsigned char *key); /* for ssh 1 */
99 void (*setcsiv)(unsigned char *key); /* for ssh 2 */
100 void (*setcskey)(unsigned char *key); /* for ssh 2 */
101 void (*setsciv)(unsigned char *key); /* for ssh 2 */
102 void (*setsckey)(unsigned char *key); /* for ssh 2 */
374330e2 103 void (*encrypt)(unsigned char *blk, int len);
104 void (*decrypt)(unsigned char *blk, int len);
e5574168 105 char *name;
106 int blksize;
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
129struct ssh_hostkey {
7cca0d81 130 void (*setkey)(char *data, int len);
131 char *(*fmtkey)(void);
d5859615 132 char *(*fingerprint)(void);
7cca0d81 133 int (*verifysig)(char *sig, int siglen, char *data, int datalen);
e5574168 134 char *name;
d5859615 135 char *keytype; /* for host key cache */
e5574168 136};
137
138struct ssh_compress {
139 char *name;
374330e2 140};
141
8f203108 142#ifndef MSCRYPTOAPI
374330e2 143void SHATransform(word32 *digest, word32 *data);
8f203108 144#endif
374330e2 145
146int random_byte(void);
147void random_add_noise(void *noise, int length);
6e522441 148void random_add_heavynoise(void *noise, int length);
c5e9c988 149
150void logevent (char *);
e5574168 151
e5574168 152Bignum newbn(int length);
7cca0d81 153Bignum copybn(Bignum b);
9400cf6f 154Bignum bignum_from_short(unsigned short n);
e5574168 155void freebn(Bignum b);
156void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result);
7cca0d81 157void modmul(Bignum a, Bignum b, Bignum mod, Bignum result);
158void decbn(Bignum n);
159extern Bignum Zero, One;
160int ssh1_read_bignum(unsigned char *data, Bignum *result);
5c58ad2d 161int ssh1_bignum_bitcount(Bignum bn);
162int ssh1_bignum_length(Bignum bn);
163int bignum_byte(Bignum bn, int i);
9400cf6f 164int bignum_bit(Bignum bn, int i);
165void bignum_set_bit(Bignum bn, int i, int value);
5c58ad2d 166int ssh1_write_bignum(void *data, Bignum bn);
9400cf6f 167Bignum biggcd(Bignum a, Bignum b);
168unsigned short bignum_mod_short(Bignum number, unsigned short modulus);
169Bignum bignum_add_long(Bignum number, unsigned long addend);
170Bignum bigmul(Bignum a, Bignum b);
171Bignum modinv(Bignum number, Bignum modulus);
172Bignum bignum_rshift(Bignum number, int shift);
8c3cd914 173int bignum_cmp(Bignum a, Bignum b);
6e522441 174char *bignum_decimal(Bignum x);
e5574168 175
176Bignum dh_create_e(void);
177Bignum dh_find_K(Bignum f);
7cca0d81 178
6e522441 179int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
180 char *passphrase);
a52f067e 181int rsakey_encrypted(char *filename, char **comment);
7cca0d81 182
6e522441 183int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux,
184 char *passphrase);
185
7cca0d81 186void des3_decrypt_pubkey(unsigned char *key,
5c58ad2d 187 unsigned char *blk, int len);
6e522441 188void des3_encrypt_pubkey(unsigned char *key,
189 unsigned char *blk, int len);
9400cf6f 190
191/*
192 * For progress updates in the key generation utility.
193 */
194typedef void (*progfn_t)(void *param, int phase, int progress);
195
196int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
197 progfn_t pfn, void *pfnparam);
198Bignum primegen(int bits, int modulus, int residue,
199 int phase, progfn_t pfn, void *pfnparam);