Oops - free the key after removing it!
[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
42int makekey(unsigned char *data, struct RSAKey *result,
7cca0d81 43 unsigned char **keystr, int order);
44int makeprivate(unsigned char *data, struct RSAKey *result);
374330e2 45void rsaencrypt(unsigned char *data, int length, struct RSAKey *key);
7cca0d81 46Bignum rsadecrypt(Bignum input, struct RSAKey *key);
47void rsasign(unsigned char *data, int length, struct RSAKey *key);
48void rsasanitise(struct RSAKey *key);
374330e2 49int rsastr_len(struct RSAKey *key);
50void rsastr_fmt(char *str, struct RSAKey *key);
5c58ad2d 51void freersakey(struct RSAKey *key);
374330e2 52
53typedef unsigned int word32;
54typedef unsigned int uint32;
55
8160fbfc 56unsigned long crc32(const void *s, size_t len);
374330e2 57
48672163 58typedef struct {
59 uint32 h[4];
60} MD5_Core_State;
61
374330e2 62struct MD5Context {
8f203108 63#ifdef MSCRYPTOAPI
64 unsigned long hHash;
65#else
48672163 66 MD5_Core_State core;
bb093ca7 67 unsigned char block[64];
48672163 68 int blkused;
69 uint32 lenhi, lenlo;
8f203108 70#endif
374330e2 71};
72
73void MD5Init(struct MD5Context *context);
74void MD5Update(struct MD5Context *context, unsigned char const *buf,
75 unsigned len);
76void MD5Final(unsigned char digest[16], struct MD5Context *context);
77
e5574168 78typedef struct {
79 uint32 h[5];
80 unsigned char block[64];
81 int blkused;
82 uint32 lenhi, lenlo;
83} SHA_State;
84
85void SHA_Init(SHA_State *s);
86void SHA_Bytes(SHA_State *s, void *p, int len);
87void SHA_Final(SHA_State *s, unsigned char *output);
7cca0d81 88void SHA_Simple(void *p, int len, unsigned char *output);
e5574168 89
374330e2 90struct ssh_cipher {
d39f364a 91 void (*sesskey)(unsigned char *key); /* for ssh 1 */
92 void (*setcsiv)(unsigned char *key); /* for ssh 2 */
93 void (*setcskey)(unsigned char *key); /* for ssh 2 */
94 void (*setsciv)(unsigned char *key); /* for ssh 2 */
95 void (*setsckey)(unsigned char *key); /* for ssh 2 */
374330e2 96 void (*encrypt)(unsigned char *blk, int len);
97 void (*decrypt)(unsigned char *blk, int len);
e5574168 98 char *name;
99 int blksize;
100};
101
102struct ssh_mac {
d39f364a 103 void (*setcskey)(unsigned char *key);
104 void (*setsckey)(unsigned char *key);
e5574168 105 void (*generate)(unsigned char *blk, int len, unsigned long seq);
106 int (*verify)(unsigned char *blk, int len, unsigned long seq);
107 char *name;
108 int len;
109};
110
111struct ssh_kex {
7cca0d81 112 /*
113 * Plugging in another KEX algorithm requires structural chaos,
114 * so it's hard to abstract them into nice little structures
115 * like this. Hence, for the moment, this is just a
116 * placeholder. I claim justification in the fact that OpenSSH
117 * does this too :-)
118 */
e5574168 119 char *name;
120};
121
122struct ssh_hostkey {
7cca0d81 123 void (*setkey)(char *data, int len);
124 char *(*fmtkey)(void);
125 int (*verifysig)(char *sig, int siglen, char *data, int datalen);
e5574168 126 char *name;
127};
128
129struct ssh_compress {
130 char *name;
374330e2 131};
132
8f203108 133#ifndef MSCRYPTOAPI
374330e2 134void SHATransform(word32 *digest, word32 *data);
8f203108 135#endif
374330e2 136
137int random_byte(void);
138void random_add_noise(void *noise, int length);
c5e9c988 139
140void logevent (char *);
e5574168 141
e5574168 142Bignum newbn(int length);
7cca0d81 143Bignum copybn(Bignum b);
e5574168 144void freebn(Bignum b);
145void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result);
7cca0d81 146void modmul(Bignum a, Bignum b, Bignum mod, Bignum result);
147void decbn(Bignum n);
148extern Bignum Zero, One;
149int ssh1_read_bignum(unsigned char *data, Bignum *result);
5c58ad2d 150int ssh1_bignum_bitcount(Bignum bn);
151int ssh1_bignum_length(Bignum bn);
152int bignum_byte(Bignum bn, int i);
153int ssh1_write_bignum(void *data, Bignum bn);
e5574168 154
155Bignum dh_create_e(void);
156Bignum dh_find_K(Bignum f);
7cca0d81 157
158int loadrsakey(char *filename, struct RSAKey *key, char *passphrase);
a52f067e 159int rsakey_encrypted(char *filename, char **comment);
7cca0d81 160
161void des3_decrypt_pubkey(unsigned char *key,
5c58ad2d 162 unsigned char *blk, int len);