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 |
24 | typedef void *Bignum; |
25 | #endif |
7cca0d81 |
26 | |
374330e2 |
27 | struct 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 |
41 | struct RSAAux { |
42 | Bignum p; |
43 | Bignum q; |
44 | Bignum iqmp; |
45 | }; |
46 | |
374330e2 |
47 | int makekey(unsigned char *data, struct RSAKey *result, |
7cca0d81 |
48 | unsigned char **keystr, int order); |
49 | int makeprivate(unsigned char *data, struct RSAKey *result); |
374330e2 |
50 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
7cca0d81 |
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); |
374330e2 |
54 | int rsastr_len(struct RSAKey *key); |
55 | void rsastr_fmt(char *str, struct RSAKey *key); |
1c2a93c4 |
56 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
5c58ad2d |
57 | void freersakey(struct RSAKey *key); |
374330e2 |
58 | |
59 | typedef unsigned int word32; |
60 | typedef unsigned int uint32; |
61 | |
8160fbfc |
62 | unsigned long crc32(const void *s, size_t len); |
374330e2 |
63 | |
48672163 |
64 | typedef struct { |
65 | uint32 h[4]; |
66 | } MD5_Core_State; |
67 | |
374330e2 |
68 | struct 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 | |
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 | |
e5574168 |
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); |
7cca0d81 |
94 | void SHA_Simple(void *p, int len, unsigned char *output); |
e5574168 |
95 | |
374330e2 |
96 | struct 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 | |
103 | struct 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 |
115 | struct ssh2_ciphers { |
116 | int nciphers; |
117 | struct ssh2_cipher **list; |
118 | }; |
119 | |
e5574168 |
120 | struct 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 | |
129 | struct 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 |
140 | struct 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 | |
153 | struct 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 |
164 | void SHATransform(word32 *digest, word32 *data); |
8f203108 |
165 | #endif |
374330e2 |
166 | |
167 | int random_byte(void); |
168 | void random_add_noise(void *noise, int length); |
6e522441 |
169 | void random_add_heavynoise(void *noise, int length); |
c5e9c988 |
170 | |
171 | void logevent (char *); |
e5574168 |
172 | |
7cca0d81 |
173 | Bignum copybn(Bignum b); |
3709bfe9 |
174 | Bignum bn_power_2(int n); |
175 | void bn_restore_invariant(Bignum b); |
9400cf6f |
176 | Bignum bignum_from_short(unsigned short n); |
e5574168 |
177 | void freebn(Bignum b); |
59600f67 |
178 | Bignum modpow(Bignum base, Bignum exp, Bignum mod); |
179 | Bignum modmul(Bignum a, Bignum b, Bignum mod); |
7cca0d81 |
180 | void decbn(Bignum n); |
181 | extern Bignum Zero, One; |
3709bfe9 |
182 | Bignum bignum_from_bytes(unsigned char *data, int nbytes); |
7cca0d81 |
183 | int ssh1_read_bignum(unsigned char *data, Bignum *result); |
5c58ad2d |
184 | int ssh1_bignum_bitcount(Bignum bn); |
185 | int ssh1_bignum_length(Bignum bn); |
186 | int bignum_byte(Bignum bn, int i); |
9400cf6f |
187 | int bignum_bit(Bignum bn, int i); |
188 | void bignum_set_bit(Bignum bn, int i, int value); |
5c58ad2d |
189 | int ssh1_write_bignum(void *data, Bignum bn); |
9400cf6f |
190 | Bignum biggcd(Bignum a, Bignum b); |
191 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
192 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
193 | Bignum bigmul(Bignum a, Bignum b); |
194 | Bignum modinv(Bignum number, Bignum modulus); |
3709bfe9 |
195 | Bignum bignum_bitmask(Bignum number); |
9400cf6f |
196 | Bignum bignum_rshift(Bignum number, int shift); |
8c3cd914 |
197 | int bignum_cmp(Bignum a, Bignum b); |
6e522441 |
198 | char *bignum_decimal(Bignum x); |
e5574168 |
199 | |
3709bfe9 |
200 | void dh_setup_group1(void); |
a92dd380 |
201 | void dh_setup_group(Bignum pval, Bignum gval); |
3709bfe9 |
202 | void dh_cleanup(void); |
e5574168 |
203 | Bignum dh_create_e(void); |
204 | Bignum dh_find_K(Bignum f); |
7cca0d81 |
205 | |
6e522441 |
206 | int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
207 | char *passphrase); |
a52f067e |
208 | int rsakey_encrypted(char *filename, char **comment); |
7cca0d81 |
209 | |
6e522441 |
210 | int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
211 | char *passphrase); |
212 | |
7cca0d81 |
213 | void des3_decrypt_pubkey(unsigned char *key, |
5c58ad2d |
214 | unsigned char *blk, int len); |
6e522441 |
215 | void 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 | */ |
221 | typedef void (*progfn_t)(void *param, int phase, int progress); |
222 | |
223 | int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits, |
224 | progfn_t pfn, void *pfnparam); |
225 | Bignum primegen(int bits, int modulus, int residue, |
226 | int phase, progfn_t pfn, void *pfnparam); |
4ba9b64b |
227 | |
228 | /* |
229 | * zlib compression. |
230 | */ |
231 | void zlib_compress_init(void); |
232 | void zlib_decompress_init(void); |
233 | int zlib_compress_block(unsigned char *block, int len, |
234 | unsigned char **outblock, int *outlen); |
235 | int zlib_decompress_block(unsigned char *block, int len, |
236 | unsigned char **outblock, int *outlen); |