summaryrefslogtreecommitdiff
path: root/cipher.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-10-14 16:23:11 +1100
committerDamien Miller <djm@mindrot.org>2000-10-14 16:23:11 +1100
commit874d77bb134a21a5cf625956b60173376a993ba8 (patch)
tree93dd73b2ff1fbf0ad5f3978a2c4e0d8438a0bf7c /cipher.c
parent89d9796fbedef4eed6956a2c095c7cc25330c28d (diff)
- (djm) Big OpenBSD sync:
- markus@cvs.openbsd.org 2000/09/30 10:27:44 [log.c] allow loglevel debug - markus@cvs.openbsd.org 2000/10/03 11:59:57 [packet.c] hmac->mac - markus@cvs.openbsd.org 2000/10/03 12:03:03 [auth-krb4.c auth-passwd.c auth-rh-rsa.c auth-rhosts.c auth-rsa.c auth1.c] move fake-auth from auth1.c to individual auth methods, disables s/key in debug-msg - markus@cvs.openbsd.org 2000/10/03 12:16:48 ssh.c do not resolve canonname, i have no idea why this was added oin ossh - markus@cvs.openbsd.org 2000/10/09 15:30:44 ssh-keygen.1 ssh-keygen.c -X now reads private ssh.com DSA keys, too. - markus@cvs.openbsd.org 2000/10/09 15:32:34 auth-options.c clear options on every call. - markus@cvs.openbsd.org 2000/10/09 15:51:00 authfd.c authfd.h interop with ssh-agent2, from <res@shore.net> - markus@cvs.openbsd.org 2000/10/10 14:20:45 compat.c use rexexp for version string matching - provos@cvs.openbsd.org 2000/10/10 22:02:18 [kex.c kex.h myproposal.h ssh.h ssh2.h sshconnect2.c sshd.c dh.c dh.h] First rough implementation of the diffie-hellman group exchange. The client can ask the server for bigger groups to perform the diffie-hellman in, thus increasing the attack complexity when using ciphers with longer keys. University of Windsor provided network, T the company. - markus@cvs.openbsd.org 2000/10/11 13:59:52 [auth-rsa.c auth2.c] clear auth options unless auth sucessfull - markus@cvs.openbsd.org 2000/10/11 14:00:27 [auth-options.h] clear auth options unless auth sucessfull - markus@cvs.openbsd.org 2000/10/11 14:03:27 [scp.1 scp.c] support 'scp -o' with help from mouring@pconline.com - markus@cvs.openbsd.org 2000/10/11 14:11:35 [dh.c] Wall - markus@cvs.openbsd.org 2000/10/11 14:14:40 [auth.h auth2.c readconf.c readconf.h readpass.c servconf.c servconf.h] [ssh.h sshconnect2.c sshd_config auth2-skey.c cli.c cli.h] add support for s/key (kbd-interactive) to ssh2, based on work by mkiernan@avantgo.com and me - markus@cvs.openbsd.org 2000/10/11 14:27:24 [auth.c auth1.c auth2.c authfile.c cipher.c cipher.h kex.c kex.h] [myproposal.h packet.c readconf.c session.c ssh.c ssh.h sshconnect1.c] [sshconnect2.c sshd.c] new cipher framework - markus@cvs.openbsd.org 2000/10/11 14:45:21 [cipher.c] remove DES - markus@cvs.openbsd.org 2000/10/12 03:59:20 [cipher.c cipher.h sshconnect1.c sshconnect2.c sshd.c] enable DES in SSH-1 clients only - markus@cvs.openbsd.org 2000/10/12 08:21:13 [kex.h packet.c] remove unused - markus@cvs.openbsd.org 2000/10/13 12:34:46 [sshd.c] Kludge for F-Secure Macintosh < 1.0.2; appro@fy.chalmers.se - markus@cvs.openbsd.org 2000/10/13 12:59:15 [cipher.c cipher.h myproposal.h rijndael.c rijndael.h] rijndael/aes support - markus@cvs.openbsd.org 2000/10/13 13:10:54 [sshd.8] more info about -V - markus@cvs.openbsd.org 2000/10/13 13:12:02 [myproposal.h] prefer no compression
Diffstat (limited to 'cipher.c')
-rw-r--r--cipher.c707
1 files changed, 396 insertions, 311 deletions
diff --git a/cipher.c b/cipher.c
index c7985a79c..226e4256a 100644
--- a/cipher.c
+++ b/cipher.c
@@ -35,14 +35,91 @@
35 */ 35 */
36 36
37#include "includes.h" 37#include "includes.h"
38RCSID("$OpenBSD: cipher.c,v 1.31 2000/09/12 00:38:32 deraadt Exp $"); 38RCSID("$OpenBSD: cipher.c,v 1.35 2000/10/13 18:59:13 markus Exp $");
39 39
40#include "ssh.h" 40#include "ssh.h"
41#include "cipher.h"
42#include "xmalloc.h" 41#include "xmalloc.h"
43 42
44#include <openssl/md5.h> 43#include <openssl/md5.h>
45 44
45
46/* no encryption */
47void
48none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
49{
50}
51void
52none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
53{
54}
55void
56none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
57{
58 memcpy(dest, src, len);
59}
60
61/* DES */
62void
63des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
64{
65 static int dowarn = 1;
66 if (dowarn) {
67 error("Warning: use of DES is strongly discouraged "
68 "due to cryptographic weaknesses");
69 dowarn = 0;
70 }
71 des_set_key((void *)key, cc->u.des.key);
72}
73void
74des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
75{
76 memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
77}
78void
79des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
80{
81 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
82 DES_ENCRYPT);
83}
84void
85des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
86{
87 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
88 DES_DECRYPT);
89}
90
91/* 3DES */
92void
93des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
94{
95 des_set_key((void *) key, cc->u.des3.key1);
96 des_set_key((void *) (key+8), cc->u.des3.key2);
97 des_set_key((void *) (key+16), cc->u.des3.key3);
98}
99void
100des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
101{
102 memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2));
103 memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3));
104 if (iv == NULL)
105 return;
106 memcpy(cc->u.des3.iv3, (char *)iv, 8);
107}
108void
109des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
110{
111 des_ede3_cbc_encrypt(src, dest, len,
112 cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
113 &cc->u.des3.iv3, DES_ENCRYPT);
114}
115void
116des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
117{
118 des_ede3_cbc_encrypt(src, dest, len,
119 cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
120 &cc->u.des3.iv3, DES_DECRYPT);
121}
122
46/* 123/*
47 * This is used by SSH1: 124 * This is used by SSH1:
48 * 125 *
@@ -58,48 +135,84 @@ RCSID("$OpenBSD: cipher.c,v 1.31 2000/09/12 00:38:32 deraadt Exp $");
58 * choosing the X block. 135 * choosing the X block.
59 */ 136 */
60void 137void
61SSH_3CBC_ENCRYPT(des_key_schedule ks1, 138des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
62 des_key_schedule ks2, des_cblock * iv2, 139{
63 des_key_schedule ks3, des_cblock * iv3, 140 des_set_key((void *) key, cc->u.des3.key1);
64 unsigned char *dest, unsigned char *src, 141 des_set_key((void *) (key+8), cc->u.des3.key2);
65 unsigned int len) 142 if (keylen <= 16)
143 des_set_key((void *) key, cc->u.des3.key3);
144 else
145 des_set_key((void *) (key+16), cc->u.des3.key3);
146}
147void
148des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
149 u_int len)
66{ 150{
67 des_cblock iv1; 151 des_cblock iv1;
152 des_cblock *iv2 = &cc->u.des3.iv2;
153 des_cblock *iv3 = &cc->u.des3.iv3;
68 154
69 memcpy(&iv1, iv2, 8); 155 memcpy(&iv1, iv2, 8);
70 156
71 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT); 157 des_cbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT);
72 memcpy(&iv1, dest + len - 8, 8); 158 memcpy(&iv1, dest + len - 8, 8);
73 159
74 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT); 160 des_cbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT);
75 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */ 161 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
76 162
77 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT); 163 des_cbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT);
78 memcpy(iv3, dest + len - 8, 8); 164 memcpy(iv3, dest + len - 8, 8);
79} 165}
80
81void 166void
82SSH_3CBC_DECRYPT(des_key_schedule ks1, 167des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
83 des_key_schedule ks2, des_cblock * iv2, 168 u_int len)
84 des_key_schedule ks3, des_cblock * iv3,
85 unsigned char *dest, unsigned char *src,
86 unsigned int len)
87{ 169{
88 des_cblock iv1; 170 des_cblock iv1;
171 des_cblock *iv2 = &cc->u.des3.iv2;
172 des_cblock *iv3 = &cc->u.des3.iv3;
89 173
90 memcpy(&iv1, iv2, 8); 174 memcpy(&iv1, iv2, 8);
91 175
92 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT); 176 des_cbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT);
93 memcpy(iv3, src + len - 8, 8); 177 memcpy(iv3, src + len - 8, 8);
94 178
95 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT); 179 des_cbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT);
96 memcpy(iv2, dest + len - 8, 8); 180 memcpy(iv2, dest + len - 8, 8);
97 181
98 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT); 182 des_cbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT);
99 /* memcpy(&iv1, iv2, 8); */ 183 /* memcpy(&iv1, iv2, 8); */
100 /* Note how iv1 == iv2 on entry and exit. */ 184 /* Note how iv1 == iv2 on entry and exit. */
101} 185}
102 186
187/* Blowfish */
188void
189blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
190{
191 BF_set_key(&cc->u.bf.key, keylen, (unsigned char *)key);
192}
193void
194blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
195{
196 if (iv == NULL)
197 memset(cc->u.bf.iv, 0, 8);
198 else
199 memcpy(cc->u.bf.iv, (char *)iv, 8);
200}
201void
202blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
203 u_int len)
204{
205 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
206 BF_ENCRYPT);
207}
208void
209blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
210 u_int len)
211{
212 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
213 BF_DECRYPT);
214}
215
103/* 216/*
104 * SSH1 uses a variation on Blowfish, all bytes must be swapped before 217 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
105 * and after encryption/decryption. Thus the swap_bytes stuff (yuk). 218 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
@@ -130,88 +243,255 @@ swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
130 } 243 }
131} 244}
132 245
133/* 246void
134 * Names of all encryption algorithms. 247blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
135 * These must match the numbers defined in cipher.h. 248 u_int len)
136 */ 249{
137static char *cipher_names[] = 250 swap_bytes(src, dest, len);
138{ 251 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
139 "none", 252 BF_ENCRYPT);
140 "idea", 253 swap_bytes(dest, dest, len);
141 "des", 254}
142 "3des", 255void
143 "tss", 256blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
144 "rc4", /* Alleged RC4 */ 257 u_int len)
145 "blowfish", 258{
146 "reserved", 259 swap_bytes(src, dest, len);
147 "blowfish-cbc", 260 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
148 "3des-cbc", 261 BF_DECRYPT);
149 "arcfour", 262 swap_bytes(dest, dest, len);
150 "cast128-cbc" 263}
151};
152 264
153/* 265/* alleged rc4 */
154 * Returns a bit mask indicating which ciphers are supported by this 266void
155 * implementation. The bit mask has the corresponding bit set of each 267arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
156 * supported cipher. 268{
157 */ 269 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
270}
271void
272arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
273{
274 RC4(&cc->u.rc4, len, (u_char *)src, dest);
275}
158 276
159unsigned int 277/* CAST */
160cipher_mask1() 278void
279cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
161{ 280{
162 unsigned int mask = 0; 281 CAST_set_key(&cc->u.cast.key, keylen, (unsigned char *) key);
163 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ 282}
164 mask |= 1 << SSH_CIPHER_BLOWFISH; 283void
165 return mask; 284cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
285{
286 if (iv == NULL)
287 fatal("no IV for %s.", cc->cipher->name);
288 memcpy(cc->u.cast.iv, (char *)iv, 8);
289}
290void
291cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
292{
293 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
294 CAST_ENCRYPT);
295}
296void
297cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
298{
299 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
300 CAST_DECRYPT);
301}
302
303/* RIJNDAEL */
304
305#define RIJNDAEL_BLOCKSIZE 16
306void
307rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
308{
309 rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1);
310 rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0);
311}
312void
313rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
314{
315 if (iv == NULL)
316 fatal("no IV for %s.", cc->cipher->name);
317 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
166} 318}
319void
320rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
321 u_int len)
322{
323 rijndael_ctx *ctx = &cc->u.rijndael.enc;
324 u4byte *iv = cc->u.rijndael.iv;
325 u4byte in[4];
326 u4byte *cprev, *cnow, *plain;
327 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
328 if (len == 0)
329 return;
330 if (len % RIJNDAEL_BLOCKSIZE)
331 fatal("rijndael_cbc_encrypt: bad len %d", len);
332 cnow = (u4byte*) dest;
333 plain = (u4byte*) src;
334 cprev = iv;
335 for(i = 0; i < blocks; i++, plain+=4, cnow+=4) {
336 in[0] = plain[0] ^ cprev[0];
337 in[1] = plain[1] ^ cprev[1];
338 in[2] = plain[2] ^ cprev[2];
339 in[3] = plain[3] ^ cprev[3];
340 rijndael_encrypt(ctx, in, cnow);
341 cprev = cnow;
342 }
343 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
344}
345
346void
347rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
348 u_int len)
349{
350 rijndael_ctx *ctx = &cc->u.rijndael.dec;
351 u4byte *iv = cc->u.rijndael.iv;
352 u4byte ivsaved[4];
353 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
354 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
355 u4byte *ivp;
356 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
357 if (len == 0)
358 return;
359 if (len % RIJNDAEL_BLOCKSIZE)
360 fatal("rijndael_cbc_decrypt: bad len %d", len);
361 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
362 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
363 rijndael_decrypt(ctx, cnow, plain);
364 ivp = (i == 1) ? iv : cnow-4;
365 plain[0] ^= ivp[0];
366 plain[1] ^= ivp[1];
367 plain[2] ^= ivp[2];
368 plain[3] ^= ivp[3];
369 }
370 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
371}
372
373Cipher ciphers[] = {
374 { "none",
375 SSH_CIPHER_NONE, 8, 0,
376 none_setkey, none_setiv,
377 none_crypt, none_crypt },
378 { "des",
379 SSH_CIPHER_DES, 8, 8,
380 des_ssh1_setkey, des_ssh1_setiv,
381 des_ssh1_encrypt, des_ssh1_decrypt },
382 { "3des",
383 SSH_CIPHER_3DES, 8, 16,
384 des3_ssh1_setkey, des3_setiv,
385 des3_ssh1_encrypt, des3_ssh1_decrypt },
386 { "blowfish",
387 SSH_CIPHER_BLOWFISH, 8, 16,
388 blowfish_setkey, blowfish_setiv,
389 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
390
391 { "3des-cbc",
392 SSH_CIPHER_SSH2, 8, 24,
393 des3_setkey, des3_setiv,
394 des3_cbc_encrypt, des3_cbc_decrypt },
395 { "blowfish-cbc",
396 SSH_CIPHER_SSH2, 8, 16,
397 blowfish_setkey, blowfish_setiv,
398 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
399 { "cast128-cbc",
400 SSH_CIPHER_SSH2, 8, 16,
401 cast_setkey, cast_setiv,
402 cast_cbc_encrypt, cast_cbc_decrypt },
403 { "arcfour",
404 SSH_CIPHER_SSH2, 8, 16,
405 arcfour_setkey, none_setiv,
406 arcfour_crypt, arcfour_crypt },
407 { "aes128-cbc",
408 SSH_CIPHER_SSH2, 16, 16,
409 rijndael_setkey, rijndael_setiv,
410 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
411 { "aes192-cbc",
412 SSH_CIPHER_SSH2, 16, 24,
413 rijndael_setkey, rijndael_setiv,
414 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
415 { "aes256-cbc",
416 SSH_CIPHER_SSH2, 16, 32,
417 rijndael_setkey, rijndael_setiv,
418 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
419 { "rijndael128-cbc",
420 SSH_CIPHER_SSH2, 16, 16,
421 rijndael_setkey, rijndael_setiv,
422 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
423 { "rijndael192-cbc",
424 SSH_CIPHER_SSH2, 16, 24,
425 rijndael_setkey, rijndael_setiv,
426 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
427 { "rijndael256-cbc",
428 SSH_CIPHER_SSH2, 16, 32,
429 rijndael_setkey, rijndael_setiv,
430 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
431 { "rijndael-cbc@lysator.liu.se",
432 SSH_CIPHER_SSH2, 16, 32,
433 rijndael_setkey, rijndael_setiv,
434 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
435 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
436};
437
438/*--*/
439
167unsigned int 440unsigned int
168cipher_mask2() 441cipher_mask_ssh1(int client)
169{ 442{
170 unsigned int mask = 0; 443 unsigned int mask = 0;
171 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC; 444 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
172 mask |= 1 << SSH_CIPHER_3DES_CBC; 445 mask |= 1 << SSH_CIPHER_BLOWFISH;
173 mask |= 1 << SSH_CIPHER_ARCFOUR; 446 if (client) {
174 mask |= 1 << SSH_CIPHER_CAST128_CBC; 447 mask |= 1 << SSH_CIPHER_DES;
448 }
175 return mask; 449 return mask;
176} 450}
177unsigned int 451
178cipher_mask() 452Cipher *
453cipher_by_name(const char *name)
179{ 454{
180 return cipher_mask1() | cipher_mask2(); 455 Cipher *c;
456 for (c = ciphers; c->name != NULL; c++)
457 if (strcasecmp(c->name, name) == 0)
458 return c;
459 return NULL;
181} 460}
182 461
183/* Returns the name of the cipher. */ 462Cipher *
184 463cipher_by_number(int id)
185const char *
186cipher_name(int cipher)
187{ 464{
188 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) || 465 Cipher *c;
189 cipher_names[cipher] == NULL) 466 for (c = ciphers; c->name != NULL; c++)
190 fatal("cipher_name: bad cipher name: %d", cipher); 467 if (c->number == id)
191 return cipher_names[cipher]; 468 return c;
469 return NULL;
192} 470}
193 471
194/* Returns 1 if the name of the ciphers are valid. */
195
196#define CIPHER_SEP "," 472#define CIPHER_SEP ","
197int 473int
198ciphers_valid(const char *names) 474ciphers_valid(const char *names)
199{ 475{
476 Cipher *c;
200 char *ciphers, *cp; 477 char *ciphers, *cp;
201 char *p; 478 char *p;
202 int i;
203 479
204 if (names == NULL || strcmp(names, "") == 0) 480 if (names == NULL || strcmp(names, "") == 0)
205 return 0; 481 return 0;
206 ciphers = cp = xstrdup(names); 482 ciphers = cp = xstrdup(names);
207 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 483 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
208 (p = strsep(&cp, CIPHER_SEP))) { 484 (p = strsep(&cp, CIPHER_SEP))) {
209 i = cipher_number(p); 485 c = cipher_by_name(p);
210 if (i == -1 || !(cipher_mask2() & (1 << i))) { 486 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
487 debug("bad cipher %s [%s]", p, names);
211 xfree(ciphers); 488 xfree(ciphers);
212 return 0; 489 return 0;
490 } else {
491 debug("cipher ok: %s [%s]", p, names);
213 } 492 }
214 } 493 }
494 debug("ciphers ok: [%s]", names);
215 xfree(ciphers); 495 xfree(ciphers);
216 return 1; 496 return 1;
217} 497}
@@ -224,14 +504,49 @@ ciphers_valid(const char *names)
224int 504int
225cipher_number(const char *name) 505cipher_number(const char *name)
226{ 506{
227 int i; 507 Cipher *c;
228 if (name == NULL) 508 if (name == NULL)
229 return -1; 509 return -1;
230 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++) 510 c = cipher_by_name(name);
231 if (strcmp(cipher_names[i], name) == 0 && 511 return (c==NULL) ? -1 : c->number;
232 (cipher_mask() & (1 << i))) 512}
233 return i; 513
234 return -1; 514char *
515cipher_name(int id)
516{
517 Cipher *c = cipher_by_number(id);
518 return (c==NULL) ? "<unknown>" : c->name;
519}
520
521void
522cipher_init(CipherContext *cc, Cipher *cipher,
523 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
524{
525 if (keylen < cipher->key_len)
526 fatal("cipher_init: key length %d is insufficient for %s.",
527 keylen, cipher->name);
528 if (iv != NULL && ivlen < cipher->block_size)
529 fatal("cipher_init: iv length %d is insufficient for %s.",
530 ivlen, cipher->name);
531 cc->cipher = cipher;
532 cipher->setkey(cc, key, keylen);
533 cipher->setiv(cc, iv, ivlen);
534}
535
536void
537cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
538{
539 if (len % cc->cipher->block_size)
540 fatal("cipher_encrypt: bad plaintext length %d", len);
541 cc->cipher->encrypt(cc, dest, src, len);
542}
543
544void
545cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
546{
547 if (len % cc->cipher->block_size)
548 fatal("cipher_decrypt: bad ciphertext length %d", len);
549 cc->cipher->decrypt(cc, dest, src, len);
235} 550}
236 551
237/* 552/*
@@ -240,248 +555,18 @@ cipher_number(const char *name)
240 */ 555 */
241 556
242void 557void
243cipher_set_key_string(CipherContext *context, int cipher, const char *passphrase) 558cipher_set_key_string(CipherContext *cc, Cipher *cipher,
559 const char *passphrase)
244{ 560{
245 MD5_CTX md; 561 MD5_CTX md;
246 unsigned char digest[16]; 562 unsigned char digest[16];
247 563
248 MD5_Init(&md); 564 MD5_Init(&md);
249 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase)); 565 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
250 MD5_Final(digest, &md); 566 MD5_Final(digest, &md);
251 567
252 cipher_set_key(context, cipher, digest, 16); 568 cipher_init(cc, cipher, digest, 16, NULL, 0);
253 569
254 memset(digest, 0, sizeof(digest)); 570 memset(digest, 0, sizeof(digest));
255 memset(&md, 0, sizeof(md)); 571 memset(&md, 0, sizeof(md));
256} 572}
257
258/* Selects the cipher to use and sets the key. */
259
260void
261cipher_set_key(CipherContext *context, int cipher, const unsigned char *key,
262 int keylen)
263{
264 unsigned char padded[32];
265
266 /* Set cipher type. */
267 context->type = cipher;
268
269 /* Get 32 bytes of key data. Pad if necessary. (So that code
270 below does not need to worry about key size). */
271 memset(padded, 0, sizeof(padded));
272 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
273
274 /* Initialize the initialization vector. */
275 switch (cipher) {
276 case SSH_CIPHER_NONE:
277 /*
278 * Has to stay for authfile saving of private key with no
279 * passphrase
280 */
281 break;
282
283 case SSH_CIPHER_3DES:
284 /*
285 * Note: the least significant bit of each byte of key is
286 * parity, and must be ignored by the implementation. 16
287 * bytes of key are used (first and last keys are the same).
288 */
289 if (keylen < 16)
290 error("Key length %d is insufficient for 3DES.", keylen);
291 des_set_key((void *) padded, context->u.des3.key1);
292 des_set_key((void *) (padded + 8), context->u.des3.key2);
293 if (keylen <= 16)
294 des_set_key((void *) padded, context->u.des3.key3);
295 else
296 des_set_key((void *) (padded + 16), context->u.des3.key3);
297 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
298 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
299 break;
300
301 case SSH_CIPHER_BLOWFISH:
302 if (keylen < 16)
303 error("Key length %d is insufficient for blowfish.", keylen);
304 BF_set_key(&context->u.bf.key, keylen, padded);
305 memset(context->u.bf.iv, 0, 8);
306 break;
307
308 case SSH_CIPHER_3DES_CBC:
309 case SSH_CIPHER_BLOWFISH_CBC:
310 case SSH_CIPHER_ARCFOUR:
311 case SSH_CIPHER_CAST128_CBC:
312 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
313 break;
314
315 default:
316 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
317 }
318 memset(padded, 0, sizeof(padded));
319}
320
321void
322cipher_set_key_iv(CipherContext * context, int cipher,
323 const unsigned char *key, int keylen,
324 const unsigned char *iv, int ivlen)
325{
326 /* Set cipher type. */
327 context->type = cipher;
328
329 /* Initialize the initialization vector. */
330 switch (cipher) {
331 case SSH_CIPHER_NONE:
332 break;
333
334 case SSH_CIPHER_3DES:
335 case SSH_CIPHER_BLOWFISH:
336 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
337 break;
338
339 case SSH_CIPHER_3DES_CBC:
340 if (keylen < 24)
341 error("Key length %d is insufficient for 3des-cbc.", keylen);
342 des_set_key((void *) key, context->u.des3.key1);
343 des_set_key((void *) (key+8), context->u.des3.key2);
344 des_set_key((void *) (key+16), context->u.des3.key3);
345 if (ivlen < 8)
346 error("IV length %d is insufficient for 3des-cbc.", ivlen);
347 memcpy(context->u.des3.iv3, (char *)iv, 8);
348 break;
349
350 case SSH_CIPHER_BLOWFISH_CBC:
351 if (keylen < 16)
352 error("Key length %d is insufficient for blowfish.", keylen);
353 if (ivlen < 8)
354 error("IV length %d is insufficient for blowfish.", ivlen);
355 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
356 memcpy(context->u.bf.iv, (char *)iv, 8);
357 break;
358
359 case SSH_CIPHER_ARCFOUR:
360 if (keylen < 16)
361 error("Key length %d is insufficient for arcfour.", keylen);
362 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
363 break;
364
365 case SSH_CIPHER_CAST128_CBC:
366 if (keylen < 16)
367 error("Key length %d is insufficient for cast128.", keylen);
368 if (ivlen < 8)
369 error("IV length %d is insufficient for cast128.", ivlen);
370 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
371 memcpy(context->u.cast.iv, (char *)iv, 8);
372 break;
373
374 default:
375 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
376 }
377}
378
379/* Encrypts data using the cipher. */
380
381void
382cipher_encrypt(CipherContext *context, unsigned char *dest,
383 const unsigned char *src, unsigned int len)
384{
385 if ((len & 7) != 0)
386 fatal("cipher_encrypt: bad plaintext length %d", len);
387
388 switch (context->type) {
389 case SSH_CIPHER_NONE:
390 memcpy(dest, src, len);
391 break;
392
393 case SSH_CIPHER_3DES:
394 SSH_3CBC_ENCRYPT(context->u.des3.key1,
395 context->u.des3.key2, &context->u.des3.iv2,
396 context->u.des3.key3, &context->u.des3.iv3,
397 dest, (unsigned char *) src, len);
398 break;
399
400 case SSH_CIPHER_BLOWFISH:
401 swap_bytes(src, dest, len);
402 BF_cbc_encrypt(dest, dest, len,
403 &context->u.bf.key, context->u.bf.iv,
404 BF_ENCRYPT);
405 swap_bytes(dest, dest, len);
406 break;
407
408 case SSH_CIPHER_BLOWFISH_CBC:
409 BF_cbc_encrypt((void *)src, dest, len,
410 &context->u.bf.key, context->u.bf.iv,
411 BF_ENCRYPT);
412 break;
413
414 case SSH_CIPHER_3DES_CBC:
415 des_ede3_cbc_encrypt(src, dest, len,
416 context->u.des3.key1, context->u.des3.key2,
417 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
418 break;
419
420 case SSH_CIPHER_ARCFOUR:
421 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
422 break;
423
424 case SSH_CIPHER_CAST128_CBC:
425 CAST_cbc_encrypt(src, dest, len,
426 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
427 break;
428
429 default:
430 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
431 }
432}
433
434/* Decrypts data using the cipher. */
435
436void
437cipher_decrypt(CipherContext *context, unsigned char *dest,
438 const unsigned char *src, unsigned int len)
439{
440 if ((len & 7) != 0)
441 fatal("cipher_decrypt: bad ciphertext length %d", len);
442
443 switch (context->type) {
444 case SSH_CIPHER_NONE:
445 memcpy(dest, src, len);
446 break;
447
448 case SSH_CIPHER_3DES:
449 SSH_3CBC_DECRYPT(context->u.des3.key1,
450 context->u.des3.key2, &context->u.des3.iv2,
451 context->u.des3.key3, &context->u.des3.iv3,
452 dest, (unsigned char *) src, len);
453 break;
454
455 case SSH_CIPHER_BLOWFISH:
456 swap_bytes(src, dest, len);
457 BF_cbc_encrypt((void *) dest, dest, len,
458 &context->u.bf.key, context->u.bf.iv,
459 BF_DECRYPT);
460 swap_bytes(dest, dest, len);
461 break;
462
463 case SSH_CIPHER_BLOWFISH_CBC:
464 BF_cbc_encrypt((void *) src, dest, len,
465 &context->u.bf.key, context->u.bf.iv,
466 BF_DECRYPT);
467 break;
468
469 case SSH_CIPHER_3DES_CBC:
470 des_ede3_cbc_encrypt(src, dest, len,
471 context->u.des3.key1, context->u.des3.key2,
472 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
473 break;
474
475 case SSH_CIPHER_ARCFOUR:
476 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
477 break;
478
479 case SSH_CIPHER_CAST128_CBC:
480 CAST_cbc_encrypt(src, dest, len,
481 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
482 break;
483
484 default:
485 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));
486 }
487}