blob: a7aacf284f2d93a730f2ceb9b7440c946e38860c [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 *
13 * Copyright (c) 1999 Niels Provos. All rights reserved.
14 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Ben Lindstrombba81212001-06-25 05:01:22 +000038RCSID("$OpenBSD: cipher.c,v 1.45 2001/06/23 15:12:18 itojun Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Miller78928792000-04-12 20:17:38 +100040#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
42#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller874d77b2000-10-14 16:23:11 +110046/* no encryption */
Ben Lindstrombba81212001-06-25 05:01:22 +000047static void
Damien Miller874d77b2000-10-14 16:23:11 +110048none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
49{
50}
Ben Lindstrombba81212001-06-25 05:01:22 +000051static void
Damien Miller874d77b2000-10-14 16:23:11 +110052none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
53{
54}
Ben Lindstrombba81212001-06-25 05:01:22 +000055static void
Damien Miller874d77b2000-10-14 16:23:11 +110056none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
57{
58 memcpy(dest, src, len);
59}
60
61/* DES */
Ben Lindstrombba81212001-06-25 05:01:22 +000062static void
Damien Miller874d77b2000-10-14 16:23:11 +110063des_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}
Ben Lindstrombba81212001-06-25 05:01:22 +000073static void
Damien Miller874d77b2000-10-14 16:23:11 +110074des_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}
Ben Lindstrombba81212001-06-25 05:01:22 +000078static void
Damien Miller874d77b2000-10-14 16:23:11 +110079des_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}
Ben Lindstrombba81212001-06-25 05:01:22 +000084static void
Damien Miller874d77b2000-10-14 16:23:11 +110085des_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 */
Ben Lindstrombba81212001-06-25 05:01:22 +000092static void
Damien Miller874d77b2000-10-14 16:23:11 +110093des3_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}
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Damien Miller874d77b2000-10-14 16:23:11 +1100100des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
101{
Ben Lindstroma3828d42001-06-05 20:50:16 +0000102 memset(cc->u.des3.iv1, 0, sizeof(cc->u.des3.iv1));
Damien Miller874d77b2000-10-14 16:23:11 +1100103 memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2));
104 memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3));
105 if (iv == NULL)
106 return;
107 memcpy(cc->u.des3.iv3, (char *)iv, 8);
108}
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
Damien Miller874d77b2000-10-14 16:23:11 +1100110des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
111{
112 des_ede3_cbc_encrypt(src, dest, len,
113 cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
114 &cc->u.des3.iv3, DES_ENCRYPT);
115}
Ben Lindstrombba81212001-06-25 05:01:22 +0000116static void
Damien Miller874d77b2000-10-14 16:23:11 +1100117des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
118{
119 des_ede3_cbc_encrypt(src, dest, len,
120 cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
121 &cc->u.des3.iv3, DES_DECRYPT);
122}
123
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124/*
Damien Miller78928792000-04-12 20:17:38 +1000125 * This is used by SSH1:
126 *
127 * What kind of triple DES are these 2 routines?
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 *
129 * Why is there a redundant initialization vector?
130 *
131 * If only iv3 was used, then, this would till effect have been
132 * outer-cbc. However, there is also a private iv1 == iv2 which
133 * perhaps makes differential analysis easier. On the other hand, the
134 * private iv1 probably makes the CRC-32 attack ineffective. This is a
135 * result of that there is no longer any known iv1 to use when
136 * choosing the X block.
137 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void
Damien Miller874d77b2000-10-14 16:23:11 +1100139des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
140{
141 des_set_key((void *) key, cc->u.des3.key1);
142 des_set_key((void *) (key+8), cc->u.des3.key2);
143 if (keylen <= 16)
144 des_set_key((void *) key, cc->u.des3.key3);
145 else
146 des_set_key((void *) (key+16), cc->u.des3.key3);
147}
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Miller874d77b2000-10-14 16:23:11 +1100149des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
150 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Ben Lindstroma3828d42001-06-05 20:50:16 +0000152 des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &cc->u.des3.iv1,
153 DES_ENCRYPT);
154 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2,
155 DES_DECRYPT);
156 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, &cc->u.des3.iv3,
157 DES_ENCRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158}
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
Damien Miller874d77b2000-10-14 16:23:11 +1100160des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
161 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162{
Ben Lindstroma3828d42001-06-05 20:50:16 +0000163 des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, &cc->u.des3.iv3,
164 DES_DECRYPT);
165 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2,
166 DES_ENCRYPT);
167 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &cc->u.des3.iv1,
168 DES_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Damien Miller874d77b2000-10-14 16:23:11 +1100171/* Blowfish */
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Damien Miller874d77b2000-10-14 16:23:11 +1100173blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
174{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000175 BF_set_key(&cc->u.bf.key, keylen, (u_char *)key);
Damien Miller874d77b2000-10-14 16:23:11 +1100176}
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static void
Damien Miller874d77b2000-10-14 16:23:11 +1100178blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
179{
180 if (iv == NULL)
181 memset(cc->u.bf.iv, 0, 8);
182 else
183 memcpy(cc->u.bf.iv, (char *)iv, 8);
184}
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static void
Damien Miller874d77b2000-10-14 16:23:11 +1100186blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
187 u_int len)
188{
189 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
190 BF_ENCRYPT);
191}
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Damien Miller874d77b2000-10-14 16:23:11 +1100193blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
194 u_int len)
195{
196 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
197 BF_DECRYPT);
198}
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200/*
Damien Miller78928792000-04-12 20:17:38 +1000201 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
203 */
Damien Miller95def091999-11-25 00:26:21 +1100204static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000205swap_bytes(const u_char *src, u_char *dst, int n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller69b69aa2000-10-28 14:19:58 +1100207 char c[4];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Miller69b69aa2000-10-28 14:19:58 +1100209 /* Process 4 bytes every lap. */
210 for (n = n / 4; n > 0; n--) {
211 c[3] = *src++;
212 c[2] = *src++;
213 c[1] = *src++;
214 c[0] = *src++;
Damien Miller95def091999-11-25 00:26:21 +1100215
Damien Miller69b69aa2000-10-28 14:19:58 +1100216 *dst++ = c[0];
217 *dst++ = c[1];
218 *dst++ = c[2];
219 *dst++ = c[3];
Damien Miller95def091999-11-25 00:26:21 +1100220 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221}
222
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller874d77b2000-10-14 16:23:11 +1100224blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
225 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller874d77b2000-10-14 16:23:11 +1100227 swap_bytes(src, dest, len);
228 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
229 BF_ENCRYPT);
230 swap_bytes(dest, dest, len);
231}
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static void
Damien Miller874d77b2000-10-14 16:23:11 +1100233blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
234 u_int len)
235{
236 swap_bytes(src, dest, len);
237 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
238 BF_DECRYPT);
239 swap_bytes(dest, dest, len);
240}
241
242/* alleged rc4 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Miller874d77b2000-10-14 16:23:11 +1100244arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
245{
246 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
247}
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static void
Damien Miller874d77b2000-10-14 16:23:11 +1100249arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
250{
251 RC4(&cc->u.rc4, len, (u_char *)src, dest);
252}
253
254/* CAST */
Ben Lindstrombba81212001-06-25 05:01:22 +0000255static void
Damien Miller874d77b2000-10-14 16:23:11 +1100256cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
257{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000258 CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key);
Damien Miller874d77b2000-10-14 16:23:11 +1100259}
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static void
Damien Miller874d77b2000-10-14 16:23:11 +1100261cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
262{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000263 if (iv == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +1100264 fatal("no IV for %s.", cc->cipher->name);
265 memcpy(cc->u.cast.iv, (char *)iv, 8);
266}
Ben Lindstrombba81212001-06-25 05:01:22 +0000267static void
Damien Miller874d77b2000-10-14 16:23:11 +1100268cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
269{
270 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
271 CAST_ENCRYPT);
272}
Ben Lindstrombba81212001-06-25 05:01:22 +0000273static void
Damien Miller874d77b2000-10-14 16:23:11 +1100274cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
275{
276 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
277 CAST_DECRYPT);
278}
279
280/* RIJNDAEL */
281
282#define RIJNDAEL_BLOCKSIZE 16
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Damien Miller874d77b2000-10-14 16:23:11 +1100284rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
285{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000286 rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1);
287 rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100288}
Ben Lindstrombba81212001-06-25 05:01:22 +0000289static void
Damien Miller874d77b2000-10-14 16:23:11 +1100290rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
291{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000292 if (iv == NULL)
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000293 fatal("no IV for %s.", cc->cipher->name);
294 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
Damien Miller874d77b2000-10-14 16:23:11 +1100295}
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
Damien Miller874d77b2000-10-14 16:23:11 +1100297rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
298 u_int len)
299{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000300 rijndael_ctx *ctx = &cc->u.rijndael.enc;
301 u4byte *iv = cc->u.rijndael.iv;
302 u4byte in[4];
303 u4byte *cprev, *cnow, *plain;
304 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100305 if (len == 0)
306 return;
307 if (len % RIJNDAEL_BLOCKSIZE)
308 fatal("rijndael_cbc_encrypt: bad len %d", len);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000309 cnow = (u4byte*) dest;
310 plain = (u4byte*) src;
Damien Miller874d77b2000-10-14 16:23:11 +1100311 cprev = iv;
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000312 for(i = 0; i < blocks; i++, plain+=4, cnow+=4) {
313 in[0] = plain[0] ^ cprev[0];
314 in[1] = plain[1] ^ cprev[1];
315 in[2] = plain[2] ^ cprev[2];
316 in[3] = plain[3] ^ cprev[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100317 rijndael_encrypt(ctx, in, cnow);
318 cprev = cnow;
319 }
320 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
321}
322
Ben Lindstrombba81212001-06-25 05:01:22 +0000323static void
Damien Miller874d77b2000-10-14 16:23:11 +1100324rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
325 u_int len)
326{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000327 rijndael_ctx *ctx = &cc->u.rijndael.dec;
328 u4byte *iv = cc->u.rijndael.iv;
329 u4byte ivsaved[4];
330 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
331 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
332 u4byte *ivp;
333 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100334 if (len == 0)
335 return;
336 if (len % RIJNDAEL_BLOCKSIZE)
337 fatal("rijndael_cbc_decrypt: bad len %d", len);
338 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000339 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
Damien Miller874d77b2000-10-14 16:23:11 +1100340 rijndael_decrypt(ctx, cnow, plain);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000341 ivp = (i == 1) ? iv : cnow-4;
342 plain[0] ^= ivp[0];
343 plain[1] ^= ivp[1];
344 plain[2] ^= ivp[2];
345 plain[3] ^= ivp[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100346 }
347 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
348}
349
350Cipher ciphers[] = {
351 { "none",
352 SSH_CIPHER_NONE, 8, 0,
353 none_setkey, none_setiv,
354 none_crypt, none_crypt },
355 { "des",
356 SSH_CIPHER_DES, 8, 8,
357 des_ssh1_setkey, des_ssh1_setiv,
358 des_ssh1_encrypt, des_ssh1_decrypt },
359 { "3des",
360 SSH_CIPHER_3DES, 8, 16,
361 des3_ssh1_setkey, des3_setiv,
362 des3_ssh1_encrypt, des3_ssh1_decrypt },
363 { "blowfish",
364 SSH_CIPHER_BLOWFISH, 8, 16,
365 blowfish_setkey, blowfish_setiv,
366 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
367
368 { "3des-cbc",
369 SSH_CIPHER_SSH2, 8, 24,
370 des3_setkey, des3_setiv,
371 des3_cbc_encrypt, des3_cbc_decrypt },
372 { "blowfish-cbc",
373 SSH_CIPHER_SSH2, 8, 16,
374 blowfish_setkey, blowfish_setiv,
375 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
376 { "cast128-cbc",
377 SSH_CIPHER_SSH2, 8, 16,
378 cast_setkey, cast_setiv,
379 cast_cbc_encrypt, cast_cbc_decrypt },
380 { "arcfour",
381 SSH_CIPHER_SSH2, 8, 16,
382 arcfour_setkey, none_setiv,
383 arcfour_crypt, arcfour_crypt },
384 { "aes128-cbc",
385 SSH_CIPHER_SSH2, 16, 16,
386 rijndael_setkey, rijndael_setiv,
387 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
388 { "aes192-cbc",
389 SSH_CIPHER_SSH2, 16, 24,
390 rijndael_setkey, rijndael_setiv,
391 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
392 { "aes256-cbc",
393 SSH_CIPHER_SSH2, 16, 32,
394 rijndael_setkey, rijndael_setiv,
395 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
396 { "rijndael128-cbc",
397 SSH_CIPHER_SSH2, 16, 16,
398 rijndael_setkey, rijndael_setiv,
399 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
400 { "rijndael192-cbc",
401 SSH_CIPHER_SSH2, 16, 24,
402 rijndael_setkey, rijndael_setiv,
403 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
404 { "rijndael256-cbc",
405 SSH_CIPHER_SSH2, 16, 32,
406 rijndael_setkey, rijndael_setiv,
407 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
408 { "rijndael-cbc@lysator.liu.se",
409 SSH_CIPHER_SSH2, 16, 32,
410 rijndael_setkey, rijndael_setiv,
411 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
Kevin Stevesef4eea92001-02-05 12:42:17 +0000412 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413};
414
Damien Miller874d77b2000-10-14 16:23:11 +1100415/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Ben Lindstrom46c16222000-12-22 01:43:59 +0000417u_int
Damien Miller874d77b2000-10-14 16:23:11 +1100418cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000420 u_int mask = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100421 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100422 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100423 if (client) {
424 mask |= 1 << SSH_CIPHER_DES;
425 }
Damien Miller1383bd82000-04-06 12:32:37 +1000426 return mask;
427}
Damien Miller874d77b2000-10-14 16:23:11 +1100428
429Cipher *
430cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000431{
Damien Miller874d77b2000-10-14 16:23:11 +1100432 Cipher *c;
433 for (c = ciphers; c->name != NULL; c++)
434 if (strcasecmp(c->name, name) == 0)
435 return c;
436 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000437}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller874d77b2000-10-14 16:23:11 +1100439Cipher *
440cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441{
Damien Miller874d77b2000-10-14 16:23:11 +1100442 Cipher *c;
443 for (c = ciphers; c->name != NULL; c++)
444 if (c->number == id)
445 return c;
446 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447}
448
Damien Miller78928792000-04-12 20:17:38 +1000449#define CIPHER_SEP ","
450int
451ciphers_valid(const char *names)
452{
Damien Miller874d77b2000-10-14 16:23:11 +1100453 Cipher *c;
Damien Miller37023962000-07-11 17:31:38 +1000454 char *ciphers, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000455 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000456
Damien Millerb1715dc2000-05-30 13:44:51 +1000457 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000458 return 0;
Damien Miller37023962000-07-11 17:31:38 +1000459 ciphers = cp = xstrdup(names);
Damien Miller874d77b2000-10-14 16:23:11 +1100460 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller37023962000-07-11 17:31:38 +1000461 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100462 c = cipher_by_name(p);
463 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
464 debug("bad cipher %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000465 xfree(ciphers);
466 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100467 } else {
Damien Miller50a41ed2000-10-16 12:14:42 +1100468 debug3("cipher ok: %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000469 }
470 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100471 debug3("ciphers ok: [%s]", names);
Damien Miller78928792000-04-12 20:17:38 +1000472 xfree(ciphers);
473 return 1;
474}
475
Damien Miller5428f641999-11-25 11:54:57 +1100476/*
477 * Parses the name of the cipher. Returns the number of the corresponding
478 * cipher, or -1 on error.
479 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
481int
482cipher_number(const char *name)
483{
Damien Miller874d77b2000-10-14 16:23:11 +1100484 Cipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000485 if (name == NULL)
486 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100487 c = cipher_by_name(name);
488 return (c==NULL) ? -1 : c->number;
489}
490
491char *
492cipher_name(int id)
493{
494 Cipher *c = cipher_by_number(id);
495 return (c==NULL) ? "<unknown>" : c->name;
496}
497
498void
499cipher_init(CipherContext *cc, Cipher *cipher,
500 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
501{
502 if (keylen < cipher->key_len)
503 fatal("cipher_init: key length %d is insufficient for %s.",
504 keylen, cipher->name);
505 if (iv != NULL && ivlen < cipher->block_size)
506 fatal("cipher_init: iv length %d is insufficient for %s.",
507 ivlen, cipher->name);
508 cc->cipher = cipher;
509 cipher->setkey(cc, key, keylen);
510 cipher->setiv(cc, iv, ivlen);
511}
512
513void
514cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
515{
516 if (len % cc->cipher->block_size)
517 fatal("cipher_encrypt: bad plaintext length %d", len);
518 cc->cipher->encrypt(cc, dest, src, len);
519}
520
521void
522cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
523{
524 if (len % cc->cipher->block_size)
525 fatal("cipher_decrypt: bad ciphertext length %d", len);
526 cc->cipher->decrypt(cc, dest, src, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527}
528
Damien Miller5428f641999-11-25 11:54:57 +1100529/*
530 * Selects the cipher, and keys if by computing the MD5 checksum of the
531 * passphrase and using the resulting 16 bytes as the key.
532 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller4af51302000-04-16 11:18:38 +1000534void
Damien Miller874d77b2000-10-14 16:23:11 +1100535cipher_set_key_string(CipherContext *cc, Cipher *cipher,
536 const char *passphrase)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537{
Damien Miller95def091999-11-25 00:26:21 +1100538 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000539 u_char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller95def091999-11-25 00:26:21 +1100541 MD5_Init(&md);
Damien Miller874d77b2000-10-14 16:23:11 +1100542 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +1100543 MD5_Final(digest, &md);
544
Damien Miller874d77b2000-10-14 16:23:11 +1100545 cipher_init(cc, cipher, digest, 16, NULL, 0);
Damien Miller95def091999-11-25 00:26:21 +1100546
547 memset(digest, 0, sizeof(digest));
548 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549}