blob: edc50dcf459639e1e650855a0087f86eb820c3bc [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * cipher.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Wed Apr 19 17:41:39 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Miller4af51302000-04-16 11:18:38 +100015RCSID("$Id: cipher.c,v 1.19 2000/04/16 01:18:41 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
18#include "cipher.h"
Damien Miller78928792000-04-12 20:17:38 +100019#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller7f6ea021999-10-28 13:25:17 +100021#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include <openssl/md5.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100023#endif
24#ifdef HAVE_SSL
25#include <ssl/md5.h>
26#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
28/*
Damien Miller78928792000-04-12 20:17:38 +100029 * This is used by SSH1:
30 *
31 * What kind of triple DES are these 2 routines?
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032 *
33 * Why is there a redundant initialization vector?
34 *
35 * If only iv3 was used, then, this would till effect have been
36 * outer-cbc. However, there is also a private iv1 == iv2 which
37 * perhaps makes differential analysis easier. On the other hand, the
38 * private iv1 probably makes the CRC-32 attack ineffective. This is a
39 * result of that there is no longer any known iv1 to use when
40 * choosing the X block.
41 */
42void
43SSH_3CBC_ENCRYPT(des_key_schedule ks1,
Damien Miller95def091999-11-25 00:26:21 +110044 des_key_schedule ks2, des_cblock * iv2,
45 des_key_schedule ks3, des_cblock * iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +110046 unsigned char *dest, unsigned char *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047 unsigned int len)
48{
Damien Miller95def091999-11-25 00:26:21 +110049 des_cblock iv1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller95def091999-11-25 00:26:21 +110053 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110054 memcpy(&iv1, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller95def091999-11-25 00:26:21 +110056 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
57 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Damien Miller95def091999-11-25 00:26:21 +110059 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110060 memcpy(iv3, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061}
62
63void
64SSH_3CBC_DECRYPT(des_key_schedule ks1,
Damien Miller95def091999-11-25 00:26:21 +110065 des_key_schedule ks2, des_cblock * iv2,
66 des_key_schedule ks3, des_cblock * iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +110067 unsigned char *dest, unsigned char *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068 unsigned int len)
69{
Damien Miller95def091999-11-25 00:26:21 +110070 des_cblock iv1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller95def091999-11-25 00:26:21 +110072 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Damien Miller95def091999-11-25 00:26:21 +110074 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110075 memcpy(iv3, src + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller95def091999-11-25 00:26:21 +110077 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110078 memcpy(iv2, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
81 /* memcpy(&iv1, iv2, 8); */
82 /* Note how iv1 == iv2 on entry and exit. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083}
84
85/*
Damien Miller78928792000-04-12 20:17:38 +100086 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
88 */
Damien Miller95def091999-11-25 00:26:21 +110089static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
91{
Damien Miller95def091999-11-25 00:26:21 +110092 /* dst must be properly aligned. */
93 u_int32_t *dst = (u_int32_t *) dst_;
94 union {
95 u_int32_t i;
96 char c[4];
97 } t;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* Process 8 bytes every lap. */
100 for (n = n / 8; n > 0; n--) {
101 t.c[3] = *src++;
102 t.c[2] = *src++;
103 t.c[1] = *src++;
104 t.c[0] = *src++;
105 *dst++ = t.i;
106
107 t.c[3] = *src++;
108 t.c[2] = *src++;
109 t.c[1] = *src++;
110 t.c[0] = *src++;
111 *dst++ = t.i;
112 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113}
114
Damien Miller5428f641999-11-25 11:54:57 +1100115/*
116 * Names of all encryption algorithms.
117 * These must match the numbers defined in cipher.h.
118 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119static char *cipher_names[] =
120{
Damien Miller95def091999-11-25 00:26:21 +1100121 "none",
122 "idea",
123 "des",
124 "3des",
125 "tss",
126 "rc4",
Damien Millerb38eff82000-04-01 11:09:21 +1000127 "blowfish",
128 "reserved",
129 "blowfish-cbc",
130 "3des-cbc",
131 "arcfour",
132 "cast128-cbc"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133};
134
Damien Miller5428f641999-11-25 11:54:57 +1100135/*
136 * Returns a bit mask indicating which ciphers are supported by this
137 * implementation. The bit mask has the corresponding bit set of each
138 * supported cipher.
139 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller4af51302000-04-16 11:18:38 +1000141unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000142cipher_mask1()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller95def091999-11-25 00:26:21 +1100144 unsigned int mask = 0;
145 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
146 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller1383bd82000-04-06 12:32:37 +1000147 return mask;
148}
Damien Miller4af51302000-04-16 11:18:38 +1000149unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000150cipher_mask2()
151{
152 unsigned int mask = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000153 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
154 mask |= 1 << SSH_CIPHER_3DES_CBC;
155 mask |= 1 << SSH_CIPHER_ARCFOUR;
156 mask |= 1 << SSH_CIPHER_CAST128_CBC;
Damien Miller95def091999-11-25 00:26:21 +1100157 return mask;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158}
Damien Miller4af51302000-04-16 11:18:38 +1000159unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000160cipher_mask()
161{
162 return cipher_mask1() | cipher_mask2();
163}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
165/* Returns the name of the cipher. */
166
Damien Miller95def091999-11-25 00:26:21 +1100167const char *
168cipher_name(int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169{
Damien Miller95def091999-11-25 00:26:21 +1100170 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
171 cipher_names[cipher] == NULL)
Damien Miller78928792000-04-12 20:17:38 +1000172 fatal("cipher_name: bad cipher name: %d", cipher);
Damien Miller95def091999-11-25 00:26:21 +1100173 return cipher_names[cipher];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Miller78928792000-04-12 20:17:38 +1000176/* Returns 1 if the name of the ciphers are valid. */
177
178#define CIPHER_SEP ","
179int
180ciphers_valid(const char *names)
181{
182 char *ciphers;
183 char *p;
184 int i;
185
186 if (strcmp(names, "") == 0)
187 return 0;
188 ciphers = xstrdup(names);
189 for ((p = strtok(ciphers, CIPHER_SEP)); p; (p = strtok(NULL, CIPHER_SEP))) {
190 i = cipher_number(p);
191 if (i == -1 || !(cipher_mask2() & (1 << i))) {
192 xfree(ciphers);
193 return 0;
194 }
195 }
196 xfree(ciphers);
197 return 1;
198}
199
Damien Miller5428f641999-11-25 11:54:57 +1100200/*
201 * Parses the name of the cipher. Returns the number of the corresponding
202 * cipher, or -1 on error.
203 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
205int
206cipher_number(const char *name)
207{
Damien Miller95def091999-11-25 00:26:21 +1100208 int i;
209 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
210 if (strcmp(cipher_names[i], name) == 0 &&
211 (cipher_mask() & (1 << i)))
212 return i;
213 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214}
215
Damien Miller5428f641999-11-25 11:54:57 +1100216/*
217 * Selects the cipher, and keys if by computing the MD5 checksum of the
218 * passphrase and using the resulting 16 bytes as the key.
219 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Damien Miller4af51302000-04-16 11:18:38 +1000221void
Damien Miller1383bd82000-04-06 12:32:37 +1000222cipher_set_key_string(CipherContext *context, int cipher, const char *passphrase)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Damien Miller95def091999-11-25 00:26:21 +1100224 MD5_CTX md;
225 unsigned char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226
Damien Miller95def091999-11-25 00:26:21 +1100227 MD5_Init(&md);
228 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
229 MD5_Final(digest, &md);
230
Damien Miller1383bd82000-04-06 12:32:37 +1000231 cipher_set_key(context, cipher, digest, 16);
Damien Miller95def091999-11-25 00:26:21 +1100232
233 memset(digest, 0, sizeof(digest));
234 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
237/* Selects the cipher to use and sets the key. */
238
Damien Miller4af51302000-04-16 11:18:38 +1000239void
Damien Miller1383bd82000-04-06 12:32:37 +1000240cipher_set_key(CipherContext *context, int cipher, const unsigned char *key,
241 int keylen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242{
Damien Miller95def091999-11-25 00:26:21 +1100243 unsigned char padded[32];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller95def091999-11-25 00:26:21 +1100245 /* Set cipher type. */
246 context->type = cipher;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller95def091999-11-25 00:26:21 +1100248 /* Get 32 bytes of key data. Pad if necessary. (So that code
249 below does not need to worry about key size). */
250 memset(padded, 0, sizeof(padded));
251 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Initialize the initialization vector. */
254 switch (cipher) {
255 case SSH_CIPHER_NONE:
Damien Miller5428f641999-11-25 11:54:57 +1100256 /*
257 * Has to stay for authfile saving of private key with no
258 * passphrase
259 */
Damien Miller95def091999-11-25 00:26:21 +1100260 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 case SSH_CIPHER_3DES:
Damien Miller5428f641999-11-25 11:54:57 +1100263 /*
264 * Note: the least significant bit of each byte of key is
265 * parity, and must be ignored by the implementation. 16
266 * bytes of key are used (first and last keys are the same).
267 */
Damien Miller95def091999-11-25 00:26:21 +1100268 if (keylen < 16)
269 error("Key length %d is insufficient for 3DES.", keylen);
270 des_set_key((void *) padded, context->u.des3.key1);
271 des_set_key((void *) (padded + 8), context->u.des3.key2);
272 if (keylen <= 16)
273 des_set_key((void *) padded, context->u.des3.key3);
274 else
275 des_set_key((void *) (padded + 16), context->u.des3.key3);
276 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
277 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
278 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller95def091999-11-25 00:26:21 +1100280 case SSH_CIPHER_BLOWFISH:
Damien Millerb38eff82000-04-01 11:09:21 +1000281 if (keylen < 16)
282 error("Key length %d is insufficient for blowfish.", keylen);
Damien Miller95def091999-11-25 00:26:21 +1100283 BF_set_key(&context->u.bf.key, keylen, padded);
284 memset(context->u.bf.iv, 0, 8);
285 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Millerb38eff82000-04-01 11:09:21 +1000287 case SSH_CIPHER_3DES_CBC:
288 case SSH_CIPHER_BLOWFISH_CBC:
289 case SSH_CIPHER_ARCFOUR:
290 case SSH_CIPHER_CAST128_CBC:
291 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
292 break;
293
Damien Miller95def091999-11-25 00:26:21 +1100294 default:
295 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
296 }
297 memset(padded, 0, sizeof(padded));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Damien Miller4af51302000-04-16 11:18:38 +1000300void
Damien Millerb38eff82000-04-01 11:09:21 +1000301cipher_set_key_iv(CipherContext * context, int cipher,
Damien Miller4af51302000-04-16 11:18:38 +1000302 const unsigned char *key, int keylen,
Damien Millerb38eff82000-04-01 11:09:21 +1000303 const unsigned char *iv, int ivlen)
304{
305 /* Set cipher type. */
306 context->type = cipher;
307
308 /* Initialize the initialization vector. */
309 switch (cipher) {
310 case SSH_CIPHER_NONE:
311 break;
312
313 case SSH_CIPHER_3DES:
314 case SSH_CIPHER_BLOWFISH:
315 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
316 break;
317
318 case SSH_CIPHER_3DES_CBC:
319 if (keylen < 24)
320 error("Key length %d is insufficient for 3des-cbc.", keylen);
321 des_set_key((void *) key, context->u.des3.key1);
322 des_set_key((void *) (key+8), context->u.des3.key2);
323 des_set_key((void *) (key+16), context->u.des3.key3);
324 if (ivlen < 8)
325 error("IV length %d is insufficient for 3des-cbc.", ivlen);
326 memcpy(context->u.des3.iv3, (char *)iv, 8);
327 break;
328
329 case SSH_CIPHER_BLOWFISH_CBC:
330 if (keylen < 16)
331 error("Key length %d is insufficient for blowfish.", keylen);
332 if (ivlen < 8)
333 error("IV length %d is insufficient for blowfish.", ivlen);
334 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
335 memcpy(context->u.bf.iv, (char *)iv, 8);
336 break;
337
338 case SSH_CIPHER_ARCFOUR:
339 if (keylen < 16)
340 error("Key length %d is insufficient for arcfour.", keylen);
341 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
342 break;
343
344 case SSH_CIPHER_CAST128_CBC:
345 if (keylen < 16)
346 error("Key length %d is insufficient for cast128.", keylen);
347 if (ivlen < 8)
348 error("IV length %d is insufficient for cast128.", ivlen);
349 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
350 memcpy(context->u.cast.iv, (char *)iv, 8);
351 break;
352
353 default:
354 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
355 }
356}
357
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358/* Encrypts data using the cipher. */
359
Damien Miller4af51302000-04-16 11:18:38 +1000360void
Damien Miller95def091999-11-25 00:26:21 +1100361cipher_encrypt(CipherContext *context, unsigned char *dest,
362 const unsigned char *src, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363{
Damien Miller95def091999-11-25 00:26:21 +1100364 if ((len & 7) != 0)
365 fatal("cipher_encrypt: bad plaintext length %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Miller95def091999-11-25 00:26:21 +1100367 switch (context->type) {
368 case SSH_CIPHER_NONE:
369 memcpy(dest, src, len);
370 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Damien Miller95def091999-11-25 00:26:21 +1100372 case SSH_CIPHER_3DES:
373 SSH_3CBC_ENCRYPT(context->u.des3.key1,
374 context->u.des3.key2, &context->u.des3.iv2,
375 context->u.des3.key3, &context->u.des3.iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +1100376 dest, (unsigned char *) src, len);
Damien Miller95def091999-11-25 00:26:21 +1100377 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378
Damien Miller95def091999-11-25 00:26:21 +1100379 case SSH_CIPHER_BLOWFISH:
380 swap_bytes(src, dest, len);
381 BF_cbc_encrypt(dest, dest, len,
Damien Miller4af51302000-04-16 11:18:38 +1000382 &context->u.bf.key, context->u.bf.iv,
Damien Miller95def091999-11-25 00:26:21 +1100383 BF_ENCRYPT);
384 swap_bytes(dest, dest, len);
385 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386
Damien Millerb38eff82000-04-01 11:09:21 +1000387 case SSH_CIPHER_BLOWFISH_CBC:
388 BF_cbc_encrypt((void *)src, dest, len,
Damien Miller4af51302000-04-16 11:18:38 +1000389 &context->u.bf.key, context->u.bf.iv,
Damien Millerb38eff82000-04-01 11:09:21 +1000390 BF_ENCRYPT);
391 break;
392
393 case SSH_CIPHER_3DES_CBC:
394 des_ede3_cbc_encrypt(src, dest, len,
395 context->u.des3.key1, context->u.des3.key2,
396 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
397 break;
398
399 case SSH_CIPHER_ARCFOUR:
400 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
401 break;
402
403 case SSH_CIPHER_CAST128_CBC:
404 CAST_cbc_encrypt(src, dest, len,
405 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
406 break;
407
Damien Miller95def091999-11-25 00:26:21 +1100408 default:
409 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
410 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411}
Damien Miller95def091999-11-25 00:26:21 +1100412
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413/* Decrypts data using the cipher. */
414
Damien Miller4af51302000-04-16 11:18:38 +1000415void
Damien Miller95def091999-11-25 00:26:21 +1100416cipher_decrypt(CipherContext *context, unsigned char *dest,
417 const unsigned char *src, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418{
Damien Miller95def091999-11-25 00:26:21 +1100419 if ((len & 7) != 0)
420 fatal("cipher_decrypt: bad ciphertext length %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Miller95def091999-11-25 00:26:21 +1100422 switch (context->type) {
423 case SSH_CIPHER_NONE:
424 memcpy(dest, src, len);
425 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Damien Miller95def091999-11-25 00:26:21 +1100427 case SSH_CIPHER_3DES:
Damien Miller95def091999-11-25 00:26:21 +1100428 SSH_3CBC_DECRYPT(context->u.des3.key1,
429 context->u.des3.key2, &context->u.des3.iv2,
430 context->u.des3.key3, &context->u.des3.iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +1100431 dest, (unsigned char *) src, len);
Damien Miller95def091999-11-25 00:26:21 +1100432 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 case SSH_CIPHER_BLOWFISH:
Damien Miller95def091999-11-25 00:26:21 +1100435 swap_bytes(src, dest, len);
436 BF_cbc_encrypt((void *) dest, dest, len,
437 &context->u.bf.key, context->u.bf.iv,
438 BF_DECRYPT);
439 swap_bytes(dest, dest, len);
440 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Damien Millerb38eff82000-04-01 11:09:21 +1000442 case SSH_CIPHER_BLOWFISH_CBC:
443 BF_cbc_encrypt((void *) src, dest, len,
444 &context->u.bf.key, context->u.bf.iv,
445 BF_DECRYPT);
446 break;
447
448 case SSH_CIPHER_3DES_CBC:
449 des_ede3_cbc_encrypt(src, dest, len,
450 context->u.des3.key1, context->u.des3.key2,
451 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
452 break;
453
454 case SSH_CIPHER_ARCFOUR:
455 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
456 break;
457
458 case SSH_CIPHER_CAST128_CBC:
459 CAST_cbc_encrypt(src, dest, len,
460 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
461 break;
462
Damien Miller95def091999-11-25 00:26:21 +1100463 default:
464 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));
465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}