blob: 639c1abbdfab5e548cd7e211bacbcd9bdd89a271 [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 Miller5f056372000-04-16 12:31:48 +100015RCSID("$Id: cipher.c,v 1.20 2000/04/16 02:31:50 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
21#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
23/*
Damien Miller78928792000-04-12 20:17:38 +100024 * This is used by SSH1:
25 *
26 * What kind of triple DES are these 2 routines?
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027 *
28 * Why is there a redundant initialization vector?
29 *
30 * If only iv3 was used, then, this would till effect have been
31 * outer-cbc. However, there is also a private iv1 == iv2 which
32 * perhaps makes differential analysis easier. On the other hand, the
33 * private iv1 probably makes the CRC-32 attack ineffective. This is a
34 * result of that there is no longer any known iv1 to use when
35 * choosing the X block.
36 */
37void
38SSH_3CBC_ENCRYPT(des_key_schedule ks1,
Damien Miller95def091999-11-25 00:26:21 +110039 des_key_schedule ks2, des_cblock * iv2,
40 des_key_schedule ks3, des_cblock * iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +110041 unsigned char *dest, unsigned char *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042 unsigned int len)
43{
Damien Miller95def091999-11-25 00:26:21 +110044 des_cblock iv1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller95def091999-11-25 00:26:21 +110046 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller95def091999-11-25 00:26:21 +110048 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110049 memcpy(&iv1, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
52 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110055 memcpy(iv3, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
58void
59SSH_3CBC_DECRYPT(des_key_schedule ks1,
Damien Miller95def091999-11-25 00:26:21 +110060 des_key_schedule ks2, des_cblock * iv2,
61 des_key_schedule ks3, des_cblock * iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +110062 unsigned char *dest, unsigned char *src,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063 unsigned int len)
64{
Damien Miller95def091999-11-25 00:26:21 +110065 des_cblock iv1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller95def091999-11-25 00:26:21 +110067 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Miller95def091999-11-25 00:26:21 +110069 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110070 memcpy(iv3, src + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller95def091999-11-25 00:26:21 +110072 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
Damien Miller98c7ad62000-03-09 21:27:49 +110073 memcpy(iv2, dest + len - 8, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
76 /* memcpy(&iv1, iv2, 8); */
77 /* Note how iv1 == iv2 on entry and exit. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
80/*
Damien Miller78928792000-04-12 20:17:38 +100081 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
83 */
Damien Miller95def091999-11-25 00:26:21 +110084static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
86{
Damien Miller95def091999-11-25 00:26:21 +110087 /* dst must be properly aligned. */
88 u_int32_t *dst = (u_int32_t *) dst_;
89 union {
90 u_int32_t i;
91 char c[4];
92 } t;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Process 8 bytes every lap. */
95 for (n = n / 8; n > 0; n--) {
96 t.c[3] = *src++;
97 t.c[2] = *src++;
98 t.c[1] = *src++;
99 t.c[0] = *src++;
100 *dst++ = t.i;
101
102 t.c[3] = *src++;
103 t.c[2] = *src++;
104 t.c[1] = *src++;
105 t.c[0] = *src++;
106 *dst++ = t.i;
107 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108}
109
Damien Miller5428f641999-11-25 11:54:57 +1100110/*
111 * Names of all encryption algorithms.
112 * These must match the numbers defined in cipher.h.
113 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114static char *cipher_names[] =
115{
Damien Miller95def091999-11-25 00:26:21 +1100116 "none",
117 "idea",
118 "des",
119 "3des",
120 "tss",
121 "rc4",
Damien Millerb38eff82000-04-01 11:09:21 +1000122 "blowfish",
123 "reserved",
124 "blowfish-cbc",
125 "3des-cbc",
126 "arcfour",
127 "cast128-cbc"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128};
129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Returns a bit mask indicating which ciphers are supported by this
132 * implementation. The bit mask has the corresponding bit set of each
133 * supported cipher.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller4af51302000-04-16 11:18:38 +1000136unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000137cipher_mask1()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Miller95def091999-11-25 00:26:21 +1100139 unsigned int mask = 0;
140 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
141 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller1383bd82000-04-06 12:32:37 +1000142 return mask;
143}
Damien Miller4af51302000-04-16 11:18:38 +1000144unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000145cipher_mask2()
146{
147 unsigned int mask = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000148 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
149 mask |= 1 << SSH_CIPHER_3DES_CBC;
150 mask |= 1 << SSH_CIPHER_ARCFOUR;
151 mask |= 1 << SSH_CIPHER_CAST128_CBC;
Damien Miller95def091999-11-25 00:26:21 +1100152 return mask;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153}
Damien Miller4af51302000-04-16 11:18:38 +1000154unsigned int
Damien Miller1383bd82000-04-06 12:32:37 +1000155cipher_mask()
156{
157 return cipher_mask1() | cipher_mask2();
158}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
160/* Returns the name of the cipher. */
161
Damien Miller95def091999-11-25 00:26:21 +1100162const char *
163cipher_name(int cipher)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
166 cipher_names[cipher] == NULL)
Damien Miller78928792000-04-12 20:17:38 +1000167 fatal("cipher_name: bad cipher name: %d", cipher);
Damien Miller95def091999-11-25 00:26:21 +1100168 return cipher_names[cipher];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Damien Miller78928792000-04-12 20:17:38 +1000171/* Returns 1 if the name of the ciphers are valid. */
172
173#define CIPHER_SEP ","
174int
175ciphers_valid(const char *names)
176{
177 char *ciphers;
178 char *p;
179 int i;
180
181 if (strcmp(names, "") == 0)
182 return 0;
183 ciphers = xstrdup(names);
184 for ((p = strtok(ciphers, CIPHER_SEP)); p; (p = strtok(NULL, CIPHER_SEP))) {
185 i = cipher_number(p);
186 if (i == -1 || !(cipher_mask2() & (1 << i))) {
187 xfree(ciphers);
188 return 0;
189 }
190 }
191 xfree(ciphers);
192 return 1;
193}
194
Damien Miller5428f641999-11-25 11:54:57 +1100195/*
196 * Parses the name of the cipher. Returns the number of the corresponding
197 * cipher, or -1 on error.
198 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
200int
201cipher_number(const char *name)
202{
Damien Miller95def091999-11-25 00:26:21 +1100203 int i;
204 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
205 if (strcmp(cipher_names[i], name) == 0 &&
206 (cipher_mask() & (1 << i)))
207 return i;
208 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
212 * Selects the cipher, and keys if by computing the MD5 checksum of the
213 * passphrase and using the resulting 16 bytes as the key.
214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller4af51302000-04-16 11:18:38 +1000216void
Damien Miller1383bd82000-04-06 12:32:37 +1000217cipher_set_key_string(CipherContext *context, int cipher, const char *passphrase)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218{
Damien Miller95def091999-11-25 00:26:21 +1100219 MD5_CTX md;
220 unsigned char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 MD5_Init(&md);
223 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
224 MD5_Final(digest, &md);
225
Damien Miller1383bd82000-04-06 12:32:37 +1000226 cipher_set_key(context, cipher, digest, 16);
Damien Miller95def091999-11-25 00:26:21 +1100227
228 memset(digest, 0, sizeof(digest));
229 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
232/* Selects the cipher to use and sets the key. */
233
Damien Miller4af51302000-04-16 11:18:38 +1000234void
Damien Miller1383bd82000-04-06 12:32:37 +1000235cipher_set_key(CipherContext *context, int cipher, const unsigned char *key,
236 int keylen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Damien Miller95def091999-11-25 00:26:21 +1100238 unsigned char padded[32];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Set cipher type. */
241 context->type = cipher;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 /* Get 32 bytes of key data. Pad if necessary. (So that code
244 below does not need to worry about key size). */
245 memset(padded, 0, sizeof(padded));
246 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller95def091999-11-25 00:26:21 +1100248 /* Initialize the initialization vector. */
249 switch (cipher) {
250 case SSH_CIPHER_NONE:
Damien Miller5428f641999-11-25 11:54:57 +1100251 /*
252 * Has to stay for authfile saving of private key with no
253 * passphrase
254 */
Damien Miller95def091999-11-25 00:26:21 +1100255 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 case SSH_CIPHER_3DES:
Damien Miller5428f641999-11-25 11:54:57 +1100258 /*
259 * Note: the least significant bit of each byte of key is
260 * parity, and must be ignored by the implementation. 16
261 * bytes of key are used (first and last keys are the same).
262 */
Damien Miller95def091999-11-25 00:26:21 +1100263 if (keylen < 16)
264 error("Key length %d is insufficient for 3DES.", keylen);
265 des_set_key((void *) padded, context->u.des3.key1);
266 des_set_key((void *) (padded + 8), context->u.des3.key2);
267 if (keylen <= 16)
268 des_set_key((void *) padded, context->u.des3.key3);
269 else
270 des_set_key((void *) (padded + 16), context->u.des3.key3);
271 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
272 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
273 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Miller95def091999-11-25 00:26:21 +1100275 case SSH_CIPHER_BLOWFISH:
Damien Millerb38eff82000-04-01 11:09:21 +1000276 if (keylen < 16)
277 error("Key length %d is insufficient for blowfish.", keylen);
Damien Miller95def091999-11-25 00:26:21 +1100278 BF_set_key(&context->u.bf.key, keylen, padded);
279 memset(context->u.bf.iv, 0, 8);
280 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Millerb38eff82000-04-01 11:09:21 +1000282 case SSH_CIPHER_3DES_CBC:
283 case SSH_CIPHER_BLOWFISH_CBC:
284 case SSH_CIPHER_ARCFOUR:
285 case SSH_CIPHER_CAST128_CBC:
286 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
287 break;
288
Damien Miller95def091999-11-25 00:26:21 +1100289 default:
290 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
291 }
292 memset(padded, 0, sizeof(padded));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293}
294
Damien Miller4af51302000-04-16 11:18:38 +1000295void
Damien Millerb38eff82000-04-01 11:09:21 +1000296cipher_set_key_iv(CipherContext * context, int cipher,
Damien Miller4af51302000-04-16 11:18:38 +1000297 const unsigned char *key, int keylen,
Damien Millerb38eff82000-04-01 11:09:21 +1000298 const unsigned char *iv, int ivlen)
299{
300 /* Set cipher type. */
301 context->type = cipher;
302
303 /* Initialize the initialization vector. */
304 switch (cipher) {
305 case SSH_CIPHER_NONE:
306 break;
307
308 case SSH_CIPHER_3DES:
309 case SSH_CIPHER_BLOWFISH:
310 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
311 break;
312
313 case SSH_CIPHER_3DES_CBC:
314 if (keylen < 24)
315 error("Key length %d is insufficient for 3des-cbc.", keylen);
316 des_set_key((void *) key, context->u.des3.key1);
317 des_set_key((void *) (key+8), context->u.des3.key2);
318 des_set_key((void *) (key+16), context->u.des3.key3);
319 if (ivlen < 8)
320 error("IV length %d is insufficient for 3des-cbc.", ivlen);
321 memcpy(context->u.des3.iv3, (char *)iv, 8);
322 break;
323
324 case SSH_CIPHER_BLOWFISH_CBC:
325 if (keylen < 16)
326 error("Key length %d is insufficient for blowfish.", keylen);
327 if (ivlen < 8)
328 error("IV length %d is insufficient for blowfish.", ivlen);
329 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
330 memcpy(context->u.bf.iv, (char *)iv, 8);
331 break;
332
333 case SSH_CIPHER_ARCFOUR:
334 if (keylen < 16)
335 error("Key length %d is insufficient for arcfour.", keylen);
336 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
337 break;
338
339 case SSH_CIPHER_CAST128_CBC:
340 if (keylen < 16)
341 error("Key length %d is insufficient for cast128.", keylen);
342 if (ivlen < 8)
343 error("IV length %d is insufficient for cast128.", ivlen);
344 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
345 memcpy(context->u.cast.iv, (char *)iv, 8);
346 break;
347
348 default:
349 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
350 }
351}
352
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353/* Encrypts data using the cipher. */
354
Damien Miller4af51302000-04-16 11:18:38 +1000355void
Damien Miller95def091999-11-25 00:26:21 +1100356cipher_encrypt(CipherContext *context, unsigned char *dest,
357 const unsigned char *src, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Damien Miller95def091999-11-25 00:26:21 +1100359 if ((len & 7) != 0)
360 fatal("cipher_encrypt: bad plaintext length %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Damien Miller95def091999-11-25 00:26:21 +1100362 switch (context->type) {
363 case SSH_CIPHER_NONE:
364 memcpy(dest, src, len);
365 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Miller95def091999-11-25 00:26:21 +1100367 case SSH_CIPHER_3DES:
368 SSH_3CBC_ENCRYPT(context->u.des3.key1,
369 context->u.des3.key2, &context->u.des3.iv2,
370 context->u.des3.key3, &context->u.des3.iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +1100371 dest, (unsigned char *) src, len);
Damien Miller95def091999-11-25 00:26:21 +1100372 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Damien Miller95def091999-11-25 00:26:21 +1100374 case SSH_CIPHER_BLOWFISH:
375 swap_bytes(src, dest, len);
376 BF_cbc_encrypt(dest, dest, len,
Damien Miller4af51302000-04-16 11:18:38 +1000377 &context->u.bf.key, context->u.bf.iv,
Damien Miller95def091999-11-25 00:26:21 +1100378 BF_ENCRYPT);
379 swap_bytes(dest, dest, len);
380 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381
Damien Millerb38eff82000-04-01 11:09:21 +1000382 case SSH_CIPHER_BLOWFISH_CBC:
383 BF_cbc_encrypt((void *)src, dest, len,
Damien Miller4af51302000-04-16 11:18:38 +1000384 &context->u.bf.key, context->u.bf.iv,
Damien Millerb38eff82000-04-01 11:09:21 +1000385 BF_ENCRYPT);
386 break;
387
388 case SSH_CIPHER_3DES_CBC:
389 des_ede3_cbc_encrypt(src, dest, len,
390 context->u.des3.key1, context->u.des3.key2,
391 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
392 break;
393
394 case SSH_CIPHER_ARCFOUR:
395 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
396 break;
397
398 case SSH_CIPHER_CAST128_CBC:
399 CAST_cbc_encrypt(src, dest, len,
400 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
401 break;
402
Damien Miller95def091999-11-25 00:26:21 +1100403 default:
404 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
405 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
Damien Miller95def091999-11-25 00:26:21 +1100407
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408/* Decrypts data using the cipher. */
409
Damien Miller4af51302000-04-16 11:18:38 +1000410void
Damien Miller95def091999-11-25 00:26:21 +1100411cipher_decrypt(CipherContext *context, unsigned char *dest,
412 const unsigned char *src, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413{
Damien Miller95def091999-11-25 00:26:21 +1100414 if ((len & 7) != 0)
415 fatal("cipher_decrypt: bad ciphertext length %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller95def091999-11-25 00:26:21 +1100417 switch (context->type) {
418 case SSH_CIPHER_NONE:
419 memcpy(dest, src, len);
420 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Miller95def091999-11-25 00:26:21 +1100422 case SSH_CIPHER_3DES:
Damien Miller95def091999-11-25 00:26:21 +1100423 SSH_3CBC_DECRYPT(context->u.des3.key1,
424 context->u.des3.key2, &context->u.des3.iv2,
425 context->u.des3.key3, &context->u.des3.iv3,
Damien Miller98c7ad62000-03-09 21:27:49 +1100426 dest, (unsigned char *) src, len);
Damien Miller95def091999-11-25 00:26:21 +1100427 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Damien Miller95def091999-11-25 00:26:21 +1100429 case SSH_CIPHER_BLOWFISH:
Damien Miller95def091999-11-25 00:26:21 +1100430 swap_bytes(src, dest, len);
431 BF_cbc_encrypt((void *) dest, dest, len,
432 &context->u.bf.key, context->u.bf.iv,
433 BF_DECRYPT);
434 swap_bytes(dest, dest, len);
435 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Millerb38eff82000-04-01 11:09:21 +1000437 case SSH_CIPHER_BLOWFISH_CBC:
438 BF_cbc_encrypt((void *) src, dest, len,
439 &context->u.bf.key, context->u.bf.iv,
440 BF_DECRYPT);
441 break;
442
443 case SSH_CIPHER_3DES_CBC:
444 des_ede3_cbc_encrypt(src, dest, len,
445 context->u.des3.key1, context->u.des3.key2,
446 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
447 break;
448
449 case SSH_CIPHER_ARCFOUR:
450 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
451 break;
452
453 case SSH_CIPHER_CAST128_CBC:
454 CAST_cbc_encrypt(src, dest, len,
455 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
456 break;
457
Damien Miller95def091999-11-25 00:26:21 +1100458 default:
459 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));
460 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}