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; |
9400cf6f |
37 | Bignum p; |
38 | Bignum q; |
39 | Bignum iqmp; |
65a22376 |
40 | #endif |
41 | char *comment; |
9400cf6f |
42 | }; |
43 | |
374330e2 |
44 | int makekey(unsigned char *data, struct RSAKey *result, |
7cca0d81 |
45 | unsigned char **keystr, int order); |
46 | int makeprivate(unsigned char *data, struct RSAKey *result); |
374330e2 |
47 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
7cca0d81 |
48 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
49 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
50 | void rsasanitise(struct RSAKey *key); |
374330e2 |
51 | int rsastr_len(struct RSAKey *key); |
52 | void rsastr_fmt(char *str, struct RSAKey *key); |
1c2a93c4 |
53 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
5c58ad2d |
54 | void freersakey(struct RSAKey *key); |
374330e2 |
55 | |
56 | typedef unsigned int word32; |
57 | typedef unsigned int uint32; |
58 | |
8160fbfc |
59 | unsigned long crc32(const void *s, size_t len); |
374330e2 |
60 | |
48672163 |
61 | typedef struct { |
62 | uint32 h[4]; |
63 | } MD5_Core_State; |
64 | |
374330e2 |
65 | struct MD5Context { |
8f203108 |
66 | #ifdef MSCRYPTOAPI |
67 | unsigned long hHash; |
68 | #else |
48672163 |
69 | MD5_Core_State core; |
bb093ca7 |
70 | unsigned char block[64]; |
48672163 |
71 | int blkused; |
72 | uint32 lenhi, lenlo; |
8f203108 |
73 | #endif |
374330e2 |
74 | }; |
75 | |
76 | void MD5Init(struct MD5Context *context); |
77 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
78 | unsigned len); |
79 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
80 | |
e5574168 |
81 | typedef struct { |
82 | uint32 h[5]; |
83 | unsigned char block[64]; |
84 | int blkused; |
85 | uint32 lenhi, lenlo; |
86 | } SHA_State; |
87 | |
88 | void SHA_Init(SHA_State *s); |
89 | void SHA_Bytes(SHA_State *s, void *p, int len); |
90 | void SHA_Final(SHA_State *s, unsigned char *output); |
7cca0d81 |
91 | void SHA_Simple(void *p, int len, unsigned char *output); |
e5574168 |
92 | |
374330e2 |
93 | struct ssh_cipher { |
d39f364a |
94 | void (*sesskey)(unsigned char *key); /* for ssh 1 */ |
0a3f1d48 |
95 | void (*encrypt)(unsigned char *blk, int len); |
96 | void (*decrypt)(unsigned char *blk, int len); |
97 | int blksize; |
98 | }; |
99 | |
100 | struct ssh2_cipher { |
d39f364a |
101 | void (*setcsiv)(unsigned char *key); /* for ssh 2 */ |
102 | void (*setcskey)(unsigned char *key); /* for ssh 2 */ |
103 | void (*setsciv)(unsigned char *key); /* for ssh 2 */ |
104 | void (*setsckey)(unsigned char *key); /* for ssh 2 */ |
374330e2 |
105 | void (*encrypt)(unsigned char *blk, int len); |
106 | void (*decrypt)(unsigned char *blk, int len); |
e5574168 |
107 | char *name; |
108 | int blksize; |
d2a0e0be |
109 | int keylen; |
e5574168 |
110 | }; |
111 | |
0a3f1d48 |
112 | struct ssh2_ciphers { |
113 | int nciphers; |
65a22376 |
114 | const struct ssh2_cipher *const *list; |
0a3f1d48 |
115 | }; |
116 | |
e5574168 |
117 | struct ssh_mac { |
d39f364a |
118 | void (*setcskey)(unsigned char *key); |
119 | void (*setsckey)(unsigned char *key); |
e5574168 |
120 | void (*generate)(unsigned char *blk, int len, unsigned long seq); |
121 | int (*verify)(unsigned char *blk, int len, unsigned long seq); |
122 | char *name; |
123 | int len; |
124 | }; |
125 | |
126 | struct ssh_kex { |
7cca0d81 |
127 | /* |
128 | * Plugging in another KEX algorithm requires structural chaos, |
129 | * so it's hard to abstract them into nice little structures |
130 | * like this. Hence, for the moment, this is just a |
131 | * placeholder. I claim justification in the fact that OpenSSH |
132 | * does this too :-) |
133 | */ |
e5574168 |
134 | char *name; |
135 | }; |
136 | |
e055a386 |
137 | struct ssh_signkey { |
138 | void *(*newkey)(char *data, int len); |
139 | void (*freekey)(void *key); |
140 | char *(*fmtkey)(void *key); |
65a22376 |
141 | unsigned char *(*public_blob)(void *key, int *len); |
142 | unsigned char *(*private_blob)(void *key, int *len); |
143 | void *(*createkey)(unsigned char *pub_blob, int pub_len, |
144 | unsigned char *priv_blob, int priv_len); |
45cebe79 |
145 | void *(*openssh_createkey)(unsigned char **blob, int *len); |
e055a386 |
146 | char *(*fingerprint)(void *key); |
147 | int (*verifysig)(void *key, char *sig, int siglen, |
148 | char *data, int datalen); |
65a22376 |
149 | unsigned char *(*sign)(void *key, char *data, int datalen, int *siglen); |
e5574168 |
150 | char *name; |
d5859615 |
151 | char *keytype; /* for host key cache */ |
e5574168 |
152 | }; |
153 | |
154 | struct ssh_compress { |
155 | char *name; |
4ba9b64b |
156 | void (*compress_init)(void); |
157 | int (*compress)(unsigned char *block, int len, |
158 | unsigned char **outblock, int *outlen); |
159 | void (*decompress_init)(void); |
160 | int (*decompress)(unsigned char *block, int len, |
161 | unsigned char **outblock, int *outlen); |
6e9e9520 |
162 | int (*disable_compression)(void); |
374330e2 |
163 | }; |
164 | |
65a22376 |
165 | struct ssh2_userkey { |
166 | const struct ssh_signkey *alg; /* the key algorithm */ |
167 | void *data; /* the key data */ |
168 | char *comment; /* the key comment */ |
169 | }; |
170 | |
171 | extern const struct ssh_cipher ssh_3des; |
172 | extern const struct ssh_cipher ssh_des; |
173 | extern const struct ssh_cipher ssh_blowfish_ssh1; |
174 | extern const struct ssh2_ciphers ssh2_3des; |
175 | extern const struct ssh2_ciphers ssh2_aes; |
176 | extern const struct ssh2_ciphers ssh2_blowfish; |
177 | extern const struct ssh_kex ssh_diffiehellman; |
178 | extern const struct ssh_kex ssh_diffiehellman_gex; |
179 | extern const struct ssh_signkey ssh_dss; |
180 | extern const struct ssh_signkey ssh_rsa; |
181 | extern const struct ssh_mac ssh_md5; |
182 | extern const struct ssh_mac ssh_sha1; |
183 | extern const struct ssh_mac ssh_sha1_buggy; |
184 | |
8f203108 |
185 | #ifndef MSCRYPTOAPI |
374330e2 |
186 | void SHATransform(word32 *digest, word32 *data); |
8f203108 |
187 | #endif |
374330e2 |
188 | |
189 | int random_byte(void); |
190 | void random_add_noise(void *noise, int length); |
6e522441 |
191 | void random_add_heavynoise(void *noise, int length); |
c5e9c988 |
192 | |
193 | void logevent (char *); |
e5574168 |
194 | |
7cca0d81 |
195 | Bignum copybn(Bignum b); |
3709bfe9 |
196 | Bignum bn_power_2(int n); |
197 | void bn_restore_invariant(Bignum b); |
9400cf6f |
198 | Bignum bignum_from_short(unsigned short n); |
e5574168 |
199 | void freebn(Bignum b); |
59600f67 |
200 | Bignum modpow(Bignum base, Bignum exp, Bignum mod); |
201 | Bignum modmul(Bignum a, Bignum b, Bignum mod); |
7cca0d81 |
202 | void decbn(Bignum n); |
203 | extern Bignum Zero, One; |
3709bfe9 |
204 | Bignum bignum_from_bytes(unsigned char *data, int nbytes); |
7cca0d81 |
205 | int ssh1_read_bignum(unsigned char *data, Bignum *result); |
5c58ad2d |
206 | int ssh1_bignum_bitcount(Bignum bn); |
207 | int ssh1_bignum_length(Bignum bn); |
208 | int bignum_byte(Bignum bn, int i); |
9400cf6f |
209 | int bignum_bit(Bignum bn, int i); |
210 | void bignum_set_bit(Bignum bn, int i, int value); |
5c58ad2d |
211 | int ssh1_write_bignum(void *data, Bignum bn); |
9400cf6f |
212 | Bignum biggcd(Bignum a, Bignum b); |
213 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
214 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
215 | Bignum bigmul(Bignum a, Bignum b); |
216 | Bignum modinv(Bignum number, Bignum modulus); |
3709bfe9 |
217 | Bignum bignum_bitmask(Bignum number); |
9400cf6f |
218 | Bignum bignum_rshift(Bignum number, int shift); |
8c3cd914 |
219 | int bignum_cmp(Bignum a, Bignum b); |
6e522441 |
220 | char *bignum_decimal(Bignum x); |
e5574168 |
221 | |
3709bfe9 |
222 | void dh_setup_group1(void); |
a92dd380 |
223 | void dh_setup_group(Bignum pval, Bignum gval); |
3709bfe9 |
224 | void dh_cleanup(void); |
e5574168 |
225 | Bignum dh_create_e(void); |
226 | Bignum dh_find_K(Bignum f); |
7cca0d81 |
227 | |
65a22376 |
228 | int loadrsakey(char *filename, struct RSAKey *key, char *passphrase); |
a52f067e |
229 | int rsakey_encrypted(char *filename, char **comment); |
7cca0d81 |
230 | |
65a22376 |
231 | int saversakey(char *filename, struct RSAKey *key, char *passphrase); |
232 | |
233 | void base64_encode_atom(unsigned char *data, int n, char *out); |
234 | |
235 | /* ssh2_load_userkey can return this as an error */ |
236 | extern struct ssh2_userkey ssh2_wrong_passphrase; |
237 | #define SSH2_WRONG_PASSPHRASE (&ssh2_wrong_passphrase) |
238 | |
239 | int ssh2_userkey_encrypted(char *filename, char **comment); |
240 | struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase); |
241 | char *ssh2_userkey_loadpub(char *filename, char **algorithm, int *pub_blob_len); |
242 | int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, char *passphrase); |
243 | |
244 | int keyfile_version(char *filename); |
6e522441 |
245 | |
65a22376 |
246 | void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
247 | void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
248 | void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
249 | void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); |
9400cf6f |
250 | |
251 | /* |
252 | * For progress updates in the key generation utility. |
253 | */ |
254 | typedef void (*progfn_t)(void *param, int phase, int progress); |
255 | |
65a22376 |
256 | int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, void *pfnparam); |
9400cf6f |
257 | Bignum primegen(int bits, int modulus, int residue, |
258 | int phase, progfn_t pfn, void *pfnparam); |
4ba9b64b |
259 | |
260 | /* |
261 | * zlib compression. |
262 | */ |
263 | void zlib_compress_init(void); |
264 | void zlib_decompress_init(void); |
265 | int zlib_compress_block(unsigned char *block, int len, |
266 | unsigned char **outblock, int *outlen); |
267 | int zlib_decompress_block(unsigned char *block, int len, |
268 | unsigned char **outblock, int *outlen); |
1983e559 |
269 | |
270 | /* |
271 | * SSH1 agent messages. |
272 | */ |
273 | #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1 |
274 | #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2 |
275 | #define SSH1_AGENTC_RSA_CHALLENGE 3 |
276 | #define SSH1_AGENT_RSA_RESPONSE 4 |
277 | #define SSH1_AGENTC_ADD_RSA_IDENTITY 7 |
278 | #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8 |
279 | #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */ |
280 | |
281 | /* |
282 | * Messages common to SSH1 and OpenSSH's SSH2. |
283 | */ |
284 | #define SSH_AGENT_FAILURE 5 |
285 | #define SSH_AGENT_SUCCESS 6 |
286 | |
287 | /* |
288 | * OpenSSH's SSH2 agent messages. |
289 | */ |
290 | #define SSH2_AGENTC_REQUEST_IDENTITIES 11 |
291 | #define SSH2_AGENT_IDENTITIES_ANSWER 12 |
292 | #define SSH2_AGENTC_SIGN_REQUEST 13 |
293 | #define SSH2_AGENT_SIGN_RESPONSE 14 |
294 | #define SSH2_AGENTC_ADD_IDENTITY 17 |
295 | #define SSH2_AGENTC_REMOVE_IDENTITY 18 |
296 | #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19 |