blob: 5350703efbf857e138138c3e4419525ff2cbc7db [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"
Kevin Steves2b725a02001-02-05 18:16:28 +000038RCSID("$OpenBSD: cipher.c,v 1.43 2001/02/04 15:32:23 stevesk 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
47/* no encryption */
48void
49none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
50{
51}
52void
53none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
54{
55}
56void
57none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
58{
59 memcpy(dest, src, len);
60}
61
62/* DES */
63void
64des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
65{
66 static int dowarn = 1;
67 if (dowarn) {
68 error("Warning: use of DES is strongly discouraged "
69 "due to cryptographic weaknesses");
70 dowarn = 0;
71 }
72 des_set_key((void *)key, cc->u.des.key);
73}
74void
75des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
76{
77 memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
78}
79void
80des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
81{
82 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
83 DES_ENCRYPT);
84}
85void
86des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
87{
88 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
89 DES_DECRYPT);
90}
91
92/* 3DES */
93void
94des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
95{
96 des_set_key((void *) key, cc->u.des3.key1);
97 des_set_key((void *) (key+8), cc->u.des3.key2);
98 des_set_key((void *) (key+16), cc->u.des3.key3);
99}
100void
101des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
102{
103 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}
109void
110des3_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}
116void
117des3_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 */
138void
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}
148void
149des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
150 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Damien Miller95def091999-11-25 00:26:21 +1100152 des_cblock iv1;
Damien Miller874d77b2000-10-14 16:23:11 +1100153 des_cblock *iv2 = &cc->u.des3.iv2;
154 des_cblock *iv3 = &cc->u.des3.iv3;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller95def091999-11-25 00:26:21 +1100156 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Ben Lindstromc72745a2000-12-02 19:03:54 +0000158 des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT);
159 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT);
160 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162void
Damien Miller874d77b2000-10-14 16:23:11 +1100163des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
164 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Damien Miller95def091999-11-25 00:26:21 +1100166 des_cblock iv1;
Damien Miller874d77b2000-10-14 16:23:11 +1100167 des_cblock *iv2 = &cc->u.des3.iv2;
168 des_cblock *iv3 = &cc->u.des3.iv3;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller95def091999-11-25 00:26:21 +1100170 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Ben Lindstromc72745a2000-12-02 19:03:54 +0000172 des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT);
173 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT);
174 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175}
176
Damien Miller874d77b2000-10-14 16:23:11 +1100177/* Blowfish */
178void
179blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
180{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000181 BF_set_key(&cc->u.bf.key, keylen, (u_char *)key);
Damien Miller874d77b2000-10-14 16:23:11 +1100182}
183void
184blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
185{
186 if (iv == NULL)
187 memset(cc->u.bf.iv, 0, 8);
188 else
189 memcpy(cc->u.bf.iv, (char *)iv, 8);
190}
191void
192blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
193 u_int len)
194{
195 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
196 BF_ENCRYPT);
197}
198void
199blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
200 u_int len)
201{
202 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
203 BF_DECRYPT);
204}
205
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206/*
Damien Miller78928792000-04-12 20:17:38 +1000207 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
209 */
Damien Miller95def091999-11-25 00:26:21 +1100210static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000211swap_bytes(const u_char *src, u_char *dst, int n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212{
Damien Miller69b69aa2000-10-28 14:19:58 +1100213 char c[4];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller69b69aa2000-10-28 14:19:58 +1100215 /* Process 4 bytes every lap. */
216 for (n = n / 4; n > 0; n--) {
217 c[3] = *src++;
218 c[2] = *src++;
219 c[1] = *src++;
220 c[0] = *src++;
Damien Miller95def091999-11-25 00:26:21 +1100221
Damien Miller69b69aa2000-10-28 14:19:58 +1100222 *dst++ = c[0];
223 *dst++ = c[1];
224 *dst++ = c[2];
225 *dst++ = c[3];
Damien Miller95def091999-11-25 00:26:21 +1100226 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller874d77b2000-10-14 16:23:11 +1100229void
230blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
231 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232{
Damien Miller874d77b2000-10-14 16:23:11 +1100233 swap_bytes(src, dest, len);
234 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
235 BF_ENCRYPT);
236 swap_bytes(dest, dest, len);
237}
238void
239blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
240 u_int len)
241{
242 swap_bytes(src, dest, len);
243 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
244 BF_DECRYPT);
245 swap_bytes(dest, dest, len);
246}
247
248/* alleged rc4 */
249void
250arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
251{
252 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
253}
254void
255arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
256{
257 RC4(&cc->u.rc4, len, (u_char *)src, dest);
258}
259
260/* CAST */
261void
262cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
263{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000264 CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key);
Damien Miller874d77b2000-10-14 16:23:11 +1100265}
266void
267cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
268{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000269 if (iv == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +1100270 fatal("no IV for %s.", cc->cipher->name);
271 memcpy(cc->u.cast.iv, (char *)iv, 8);
272}
273void
274cast_cbc_encrypt(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_ENCRYPT);
278}
279void
280cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
281{
282 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
283 CAST_DECRYPT);
284}
285
286/* RIJNDAEL */
287
288#define RIJNDAEL_BLOCKSIZE 16
289void
290rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
291{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000292 rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1);
293 rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100294}
295void
296rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
297{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000298 if (iv == NULL)
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000299 fatal("no IV for %s.", cc->cipher->name);
300 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
Damien Miller874d77b2000-10-14 16:23:11 +1100301}
302void
303rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
304 u_int len)
305{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000306 rijndael_ctx *ctx = &cc->u.rijndael.enc;
307 u4byte *iv = cc->u.rijndael.iv;
308 u4byte in[4];
309 u4byte *cprev, *cnow, *plain;
310 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100311 if (len == 0)
312 return;
313 if (len % RIJNDAEL_BLOCKSIZE)
314 fatal("rijndael_cbc_encrypt: bad len %d", len);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000315 cnow = (u4byte*) dest;
316 plain = (u4byte*) src;
Damien Miller874d77b2000-10-14 16:23:11 +1100317 cprev = iv;
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000318 for(i = 0; i < blocks; i++, plain+=4, cnow+=4) {
319 in[0] = plain[0] ^ cprev[0];
320 in[1] = plain[1] ^ cprev[1];
321 in[2] = plain[2] ^ cprev[2];
322 in[3] = plain[3] ^ cprev[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100323 rijndael_encrypt(ctx, in, cnow);
324 cprev = cnow;
325 }
326 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
327}
328
329void
330rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
331 u_int len)
332{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000333 rijndael_ctx *ctx = &cc->u.rijndael.dec;
334 u4byte *iv = cc->u.rijndael.iv;
335 u4byte ivsaved[4];
336 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
337 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
338 u4byte *ivp;
339 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100340 if (len == 0)
341 return;
342 if (len % RIJNDAEL_BLOCKSIZE)
343 fatal("rijndael_cbc_decrypt: bad len %d", len);
344 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000345 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
Damien Miller874d77b2000-10-14 16:23:11 +1100346 rijndael_decrypt(ctx, cnow, plain);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000347 ivp = (i == 1) ? iv : cnow-4;
348 plain[0] ^= ivp[0];
349 plain[1] ^= ivp[1];
350 plain[2] ^= ivp[2];
351 plain[3] ^= ivp[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100352 }
353 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
354}
355
356Cipher ciphers[] = {
357 { "none",
358 SSH_CIPHER_NONE, 8, 0,
359 none_setkey, none_setiv,
360 none_crypt, none_crypt },
361 { "des",
362 SSH_CIPHER_DES, 8, 8,
363 des_ssh1_setkey, des_ssh1_setiv,
364 des_ssh1_encrypt, des_ssh1_decrypt },
365 { "3des",
366 SSH_CIPHER_3DES, 8, 16,
367 des3_ssh1_setkey, des3_setiv,
368 des3_ssh1_encrypt, des3_ssh1_decrypt },
369 { "blowfish",
370 SSH_CIPHER_BLOWFISH, 8, 16,
371 blowfish_setkey, blowfish_setiv,
372 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
373
374 { "3des-cbc",
375 SSH_CIPHER_SSH2, 8, 24,
376 des3_setkey, des3_setiv,
377 des3_cbc_encrypt, des3_cbc_decrypt },
378 { "blowfish-cbc",
379 SSH_CIPHER_SSH2, 8, 16,
380 blowfish_setkey, blowfish_setiv,
381 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
382 { "cast128-cbc",
383 SSH_CIPHER_SSH2, 8, 16,
384 cast_setkey, cast_setiv,
385 cast_cbc_encrypt, cast_cbc_decrypt },
386 { "arcfour",
387 SSH_CIPHER_SSH2, 8, 16,
388 arcfour_setkey, none_setiv,
389 arcfour_crypt, arcfour_crypt },
390 { "aes128-cbc",
391 SSH_CIPHER_SSH2, 16, 16,
392 rijndael_setkey, rijndael_setiv,
393 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
394 { "aes192-cbc",
395 SSH_CIPHER_SSH2, 16, 24,
396 rijndael_setkey, rijndael_setiv,
397 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
398 { "aes256-cbc",
399 SSH_CIPHER_SSH2, 16, 32,
400 rijndael_setkey, rijndael_setiv,
401 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
402 { "rijndael128-cbc",
403 SSH_CIPHER_SSH2, 16, 16,
404 rijndael_setkey, rijndael_setiv,
405 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
406 { "rijndael192-cbc",
407 SSH_CIPHER_SSH2, 16, 24,
408 rijndael_setkey, rijndael_setiv,
409 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
410 { "rijndael256-cbc",
411 SSH_CIPHER_SSH2, 16, 32,
412 rijndael_setkey, rijndael_setiv,
413 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
414 { "rijndael-cbc@lysator.liu.se",
415 SSH_CIPHER_SSH2, 16, 32,
416 rijndael_setkey, rijndael_setiv,
417 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
Kevin Stevesef4eea92001-02-05 12:42:17 +0000418 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419};
420
Damien Miller874d77b2000-10-14 16:23:11 +1100421/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Ben Lindstrom46c16222000-12-22 01:43:59 +0000423u_int
Damien Miller874d77b2000-10-14 16:23:11 +1100424cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000426 u_int mask = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100427 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100428 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100429 if (client) {
430 mask |= 1 << SSH_CIPHER_DES;
431 }
Damien Miller1383bd82000-04-06 12:32:37 +1000432 return mask;
433}
Damien Miller874d77b2000-10-14 16:23:11 +1100434
435Cipher *
436cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000437{
Damien Miller874d77b2000-10-14 16:23:11 +1100438 Cipher *c;
439 for (c = ciphers; c->name != NULL; c++)
440 if (strcasecmp(c->name, name) == 0)
441 return c;
442 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000443}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller874d77b2000-10-14 16:23:11 +1100445Cipher *
446cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447{
Damien Miller874d77b2000-10-14 16:23:11 +1100448 Cipher *c;
449 for (c = ciphers; c->name != NULL; c++)
450 if (c->number == id)
451 return c;
452 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
454
Damien Miller78928792000-04-12 20:17:38 +1000455#define CIPHER_SEP ","
456int
457ciphers_valid(const char *names)
458{
Damien Miller874d77b2000-10-14 16:23:11 +1100459 Cipher *c;
Damien Miller37023962000-07-11 17:31:38 +1000460 char *ciphers, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000461 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000462
Damien Millerb1715dc2000-05-30 13:44:51 +1000463 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000464 return 0;
Damien Miller37023962000-07-11 17:31:38 +1000465 ciphers = cp = xstrdup(names);
Damien Miller874d77b2000-10-14 16:23:11 +1100466 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller37023962000-07-11 17:31:38 +1000467 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100468 c = cipher_by_name(p);
469 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
470 debug("bad cipher %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000471 xfree(ciphers);
472 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100473 } else {
Damien Miller50a41ed2000-10-16 12:14:42 +1100474 debug3("cipher ok: %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000475 }
476 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100477 debug3("ciphers ok: [%s]", names);
Damien Miller78928792000-04-12 20:17:38 +1000478 xfree(ciphers);
479 return 1;
480}
481
Damien Miller5428f641999-11-25 11:54:57 +1100482/*
483 * Parses the name of the cipher. Returns the number of the corresponding
484 * cipher, or -1 on error.
485 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
487int
488cipher_number(const char *name)
489{
Damien Miller874d77b2000-10-14 16:23:11 +1100490 Cipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000491 if (name == NULL)
492 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100493 c = cipher_by_name(name);
494 return (c==NULL) ? -1 : c->number;
495}
496
497char *
498cipher_name(int id)
499{
500 Cipher *c = cipher_by_number(id);
501 return (c==NULL) ? "<unknown>" : c->name;
502}
503
504void
505cipher_init(CipherContext *cc, Cipher *cipher,
506 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
507{
508 if (keylen < cipher->key_len)
509 fatal("cipher_init: key length %d is insufficient for %s.",
510 keylen, cipher->name);
511 if (iv != NULL && ivlen < cipher->block_size)
512 fatal("cipher_init: iv length %d is insufficient for %s.",
513 ivlen, cipher->name);
514 cc->cipher = cipher;
515 cipher->setkey(cc, key, keylen);
516 cipher->setiv(cc, iv, ivlen);
517}
518
519void
520cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
521{
522 if (len % cc->cipher->block_size)
523 fatal("cipher_encrypt: bad plaintext length %d", len);
524 cc->cipher->encrypt(cc, dest, src, len);
525}
526
527void
528cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
529{
530 if (len % cc->cipher->block_size)
531 fatal("cipher_decrypt: bad ciphertext length %d", len);
532 cc->cipher->decrypt(cc, dest, src, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533}
534
Damien Miller5428f641999-11-25 11:54:57 +1100535/*
536 * Selects the cipher, and keys if by computing the MD5 checksum of the
537 * passphrase and using the resulting 16 bytes as the key.
538 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller4af51302000-04-16 11:18:38 +1000540void
Damien Miller874d77b2000-10-14 16:23:11 +1100541cipher_set_key_string(CipherContext *cc, Cipher *cipher,
542 const char *passphrase)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543{
Damien Miller95def091999-11-25 00:26:21 +1100544 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000545 u_char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller95def091999-11-25 00:26:21 +1100547 MD5_Init(&md);
Damien Miller874d77b2000-10-14 16:23:11 +1100548 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +1100549 MD5_Final(digest, &md);
550
Damien Miller874d77b2000-10-14 16:23:11 +1100551 cipher_init(cc, cipher, digest, 16, NULL, 0);
Damien Miller95def091999-11-25 00:26:21 +1100552
553 memset(digest, 0, sizeof(digest));
554 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555}