blob: 89e3c279fcc52179b403a2e1885a043297bc34f7 [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 Lindstrom46c16222000-12-22 01:43:59 +000038RCSID("$OpenBSD: cipher.c,v 1.41 2000/12/19 23:17:56 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "ssh.h"
Damien Miller78928792000-04-12 20:17:38 +100041#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller874d77b2000-10-14 16:23:11 +110045
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
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123/*
Damien Miller78928792000-04-12 20:17:38 +1000124 * This is used by SSH1:
125 *
126 * What kind of triple DES are these 2 routines?
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127 *
128 * Why is there a redundant initialization vector?
129 *
130 * If only iv3 was used, then, this would till effect have been
131 * outer-cbc. However, there is also a private iv1 == iv2 which
132 * perhaps makes differential analysis easier. On the other hand, the
133 * private iv1 probably makes the CRC-32 attack ineffective. This is a
134 * result of that there is no longer any known iv1 to use when
135 * choosing the X block.
136 */
137void
Damien Miller874d77b2000-10-14 16:23:11 +1100138des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
139{
140 des_set_key((void *) key, cc->u.des3.key1);
141 des_set_key((void *) (key+8), cc->u.des3.key2);
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)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Damien Miller95def091999-11-25 00:26:21 +1100151 des_cblock iv1;
Damien Miller874d77b2000-10-14 16:23:11 +1100152 des_cblock *iv2 = &cc->u.des3.iv2;
153 des_cblock *iv3 = &cc->u.des3.iv3;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Ben Lindstromc72745a2000-12-02 19:03:54 +0000157 des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT);
158 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT);
159 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161void
Damien Miller874d77b2000-10-14 16:23:11 +1100162des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
163 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 des_cblock iv1;
Damien Miller874d77b2000-10-14 16:23:11 +1100166 des_cblock *iv2 = &cc->u.des3.iv2;
167 des_cblock *iv3 = &cc->u.des3.iv3;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169 memcpy(&iv1, iv2, 8);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Ben Lindstromc72745a2000-12-02 19:03:54 +0000171 des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT);
172 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT);
173 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Miller874d77b2000-10-14 16:23:11 +1100176/* Blowfish */
177void
178blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
179{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000180 BF_set_key(&cc->u.bf.key, keylen, (u_char *)key);
Damien Miller874d77b2000-10-14 16:23:11 +1100181}
182void
183blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
184{
185 if (iv == NULL)
186 memset(cc->u.bf.iv, 0, 8);
187 else
188 memcpy(cc->u.bf.iv, (char *)iv, 8);
189}
190void
191blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
192 u_int len)
193{
194 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
195 BF_ENCRYPT);
196}
197void
198blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
199 u_int len)
200{
201 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
202 BF_DECRYPT);
203}
204
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205/*
Damien Miller78928792000-04-12 20:17:38 +1000206 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
208 */
Damien Miller95def091999-11-25 00:26:21 +1100209static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000210swap_bytes(const u_char *src, u_char *dst, int n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Damien Miller69b69aa2000-10-28 14:19:58 +1100212 char c[4];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller69b69aa2000-10-28 14:19:58 +1100214 /* Process 4 bytes every lap. */
215 for (n = n / 4; n > 0; n--) {
216 c[3] = *src++;
217 c[2] = *src++;
218 c[1] = *src++;
219 c[0] = *src++;
Damien Miller95def091999-11-25 00:26:21 +1100220
Damien Miller69b69aa2000-10-28 14:19:58 +1100221 *dst++ = c[0];
222 *dst++ = c[1];
223 *dst++ = c[2];
224 *dst++ = c[3];
Damien Miller95def091999-11-25 00:26:21 +1100225 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226}
227
Damien Miller874d77b2000-10-14 16:23:11 +1100228void
229blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
230 u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Damien Miller874d77b2000-10-14 16:23:11 +1100232 swap_bytes(src, dest, len);
233 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
234 BF_ENCRYPT);
235 swap_bytes(dest, dest, len);
236}
237void
238blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
239 u_int len)
240{
241 swap_bytes(src, dest, len);
242 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
243 BF_DECRYPT);
244 swap_bytes(dest, dest, len);
245}
246
247/* alleged rc4 */
248void
249arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
250{
251 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
252}
253void
254arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
255{
256 RC4(&cc->u.rc4, len, (u_char *)src, dest);
257}
258
259/* CAST */
260void
261cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
262{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000263 CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key);
Damien Miller874d77b2000-10-14 16:23:11 +1100264}
265void
266cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
267{
268 if (iv == NULL)
269 fatal("no IV for %s.", cc->cipher->name);
270 memcpy(cc->u.cast.iv, (char *)iv, 8);
271}
272void
273cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
274{
275 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
276 CAST_ENCRYPT);
277}
278void
279cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
280{
281 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
282 CAST_DECRYPT);
283}
284
285/* RIJNDAEL */
286
287#define RIJNDAEL_BLOCKSIZE 16
288void
289rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
290{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000291 rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1);
292 rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100293}
294void
295rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
296{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000297 if (iv == NULL)
298 fatal("no IV for %s.", cc->cipher->name);
299 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
Damien Miller874d77b2000-10-14 16:23:11 +1100300}
301void
302rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
303 u_int len)
304{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000305 rijndael_ctx *ctx = &cc->u.rijndael.enc;
306 u4byte *iv = cc->u.rijndael.iv;
307 u4byte in[4];
308 u4byte *cprev, *cnow, *plain;
309 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100310 if (len == 0)
311 return;
312 if (len % RIJNDAEL_BLOCKSIZE)
313 fatal("rijndael_cbc_encrypt: bad len %d", len);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000314 cnow = (u4byte*) dest;
315 plain = (u4byte*) src;
Damien Miller874d77b2000-10-14 16:23:11 +1100316 cprev = iv;
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000317 for(i = 0; i < blocks; i++, plain+=4, cnow+=4) {
318 in[0] = plain[0] ^ cprev[0];
319 in[1] = plain[1] ^ cprev[1];
320 in[2] = plain[2] ^ cprev[2];
321 in[3] = plain[3] ^ cprev[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100322 rijndael_encrypt(ctx, in, cnow);
323 cprev = cnow;
324 }
325 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
326}
327
328void
329rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
330 u_int len)
331{
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000332 rijndael_ctx *ctx = &cc->u.rijndael.dec;
333 u4byte *iv = cc->u.rijndael.iv;
334 u4byte ivsaved[4];
335 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
336 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
337 u4byte *ivp;
338 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
Damien Miller874d77b2000-10-14 16:23:11 +1100339 if (len == 0)
340 return;
341 if (len % RIJNDAEL_BLOCKSIZE)
342 fatal("rijndael_cbc_decrypt: bad len %d", len);
343 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000344 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
Damien Miller874d77b2000-10-14 16:23:11 +1100345 rijndael_decrypt(ctx, cnow, plain);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000346 ivp = (i == 1) ? iv : cnow-4;
347 plain[0] ^= ivp[0];
348 plain[1] ^= ivp[1];
349 plain[2] ^= ivp[2];
350 plain[3] ^= ivp[3];
Damien Miller874d77b2000-10-14 16:23:11 +1100351 }
352 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
353}
354
355Cipher ciphers[] = {
356 { "none",
357 SSH_CIPHER_NONE, 8, 0,
358 none_setkey, none_setiv,
359 none_crypt, none_crypt },
360 { "des",
361 SSH_CIPHER_DES, 8, 8,
362 des_ssh1_setkey, des_ssh1_setiv,
363 des_ssh1_encrypt, des_ssh1_decrypt },
364 { "3des",
365 SSH_CIPHER_3DES, 8, 16,
366 des3_ssh1_setkey, des3_setiv,
367 des3_ssh1_encrypt, des3_ssh1_decrypt },
368 { "blowfish",
369 SSH_CIPHER_BLOWFISH, 8, 16,
370 blowfish_setkey, blowfish_setiv,
371 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
372
373 { "3des-cbc",
374 SSH_CIPHER_SSH2, 8, 24,
375 des3_setkey, des3_setiv,
376 des3_cbc_encrypt, des3_cbc_decrypt },
377 { "blowfish-cbc",
378 SSH_CIPHER_SSH2, 8, 16,
379 blowfish_setkey, blowfish_setiv,
380 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
381 { "cast128-cbc",
382 SSH_CIPHER_SSH2, 8, 16,
383 cast_setkey, cast_setiv,
384 cast_cbc_encrypt, cast_cbc_decrypt },
385 { "arcfour",
386 SSH_CIPHER_SSH2, 8, 16,
387 arcfour_setkey, none_setiv,
388 arcfour_crypt, arcfour_crypt },
389 { "aes128-cbc",
390 SSH_CIPHER_SSH2, 16, 16,
391 rijndael_setkey, rijndael_setiv,
392 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
393 { "aes192-cbc",
394 SSH_CIPHER_SSH2, 16, 24,
395 rijndael_setkey, rijndael_setiv,
396 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
397 { "aes256-cbc",
398 SSH_CIPHER_SSH2, 16, 32,
399 rijndael_setkey, rijndael_setiv,
400 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
401 { "rijndael128-cbc",
402 SSH_CIPHER_SSH2, 16, 16,
403 rijndael_setkey, rijndael_setiv,
404 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
405 { "rijndael192-cbc",
406 SSH_CIPHER_SSH2, 16, 24,
407 rijndael_setkey, rijndael_setiv,
408 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
409 { "rijndael256-cbc",
410 SSH_CIPHER_SSH2, 16, 32,
411 rijndael_setkey, rijndael_setiv,
412 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
413 { "rijndael-cbc@lysator.liu.se",
414 SSH_CIPHER_SSH2, 16, 32,
415 rijndael_setkey, rijndael_setiv,
416 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
417 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418};
419
Damien Miller874d77b2000-10-14 16:23:11 +1100420/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Ben Lindstrom46c16222000-12-22 01:43:59 +0000422u_int
Damien Miller874d77b2000-10-14 16:23:11 +1100423cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000425 u_int mask = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100426 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100427 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100428 if (client) {
429 mask |= 1 << SSH_CIPHER_DES;
430 }
Damien Miller1383bd82000-04-06 12:32:37 +1000431 return mask;
432}
Damien Miller874d77b2000-10-14 16:23:11 +1100433
434Cipher *
435cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000436{
Damien Miller874d77b2000-10-14 16:23:11 +1100437 Cipher *c;
438 for (c = ciphers; c->name != NULL; c++)
439 if (strcasecmp(c->name, name) == 0)
440 return c;
441 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000442}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Damien Miller874d77b2000-10-14 16:23:11 +1100444Cipher *
445cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446{
Damien Miller874d77b2000-10-14 16:23:11 +1100447 Cipher *c;
448 for (c = ciphers; c->name != NULL; c++)
449 if (c->number == id)
450 return c;
451 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452}
453
Damien Miller78928792000-04-12 20:17:38 +1000454#define CIPHER_SEP ","
455int
456ciphers_valid(const char *names)
457{
Damien Miller874d77b2000-10-14 16:23:11 +1100458 Cipher *c;
Damien Miller37023962000-07-11 17:31:38 +1000459 char *ciphers, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000460 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000461
Damien Millerb1715dc2000-05-30 13:44:51 +1000462 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000463 return 0;
Damien Miller37023962000-07-11 17:31:38 +1000464 ciphers = cp = xstrdup(names);
Damien Miller874d77b2000-10-14 16:23:11 +1100465 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller37023962000-07-11 17:31:38 +1000466 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100467 c = cipher_by_name(p);
468 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
469 debug("bad cipher %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000470 xfree(ciphers);
471 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100472 } else {
Damien Miller50a41ed2000-10-16 12:14:42 +1100473 debug3("cipher ok: %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000474 }
475 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100476 debug3("ciphers ok: [%s]", names);
Damien Miller78928792000-04-12 20:17:38 +1000477 xfree(ciphers);
478 return 1;
479}
480
Damien Miller5428f641999-11-25 11:54:57 +1100481/*
482 * Parses the name of the cipher. Returns the number of the corresponding
483 * cipher, or -1 on error.
484 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485
486int
487cipher_number(const char *name)
488{
Damien Miller874d77b2000-10-14 16:23:11 +1100489 Cipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000490 if (name == NULL)
491 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100492 c = cipher_by_name(name);
493 return (c==NULL) ? -1 : c->number;
494}
495
496char *
497cipher_name(int id)
498{
499 Cipher *c = cipher_by_number(id);
500 return (c==NULL) ? "<unknown>" : c->name;
501}
502
503void
504cipher_init(CipherContext *cc, Cipher *cipher,
505 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
506{
507 if (keylen < cipher->key_len)
508 fatal("cipher_init: key length %d is insufficient for %s.",
509 keylen, cipher->name);
510 if (iv != NULL && ivlen < cipher->block_size)
511 fatal("cipher_init: iv length %d is insufficient for %s.",
512 ivlen, cipher->name);
513 cc->cipher = cipher;
514 cipher->setkey(cc, key, keylen);
515 cipher->setiv(cc, iv, ivlen);
516}
517
518void
519cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
520{
521 if (len % cc->cipher->block_size)
522 fatal("cipher_encrypt: bad plaintext length %d", len);
523 cc->cipher->encrypt(cc, dest, src, len);
524}
525
526void
527cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
528{
529 if (len % cc->cipher->block_size)
530 fatal("cipher_decrypt: bad ciphertext length %d", len);
531 cc->cipher->decrypt(cc, dest, src, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532}
533
Damien Miller5428f641999-11-25 11:54:57 +1100534/*
535 * Selects the cipher, and keys if by computing the MD5 checksum of the
536 * passphrase and using the resulting 16 bytes as the key.
537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller4af51302000-04-16 11:18:38 +1000539void
Damien Miller874d77b2000-10-14 16:23:11 +1100540cipher_set_key_string(CipherContext *cc, Cipher *cipher,
541 const char *passphrase)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542{
Damien Miller95def091999-11-25 00:26:21 +1100543 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000544 u_char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Miller95def091999-11-25 00:26:21 +1100546 MD5_Init(&md);
Damien Miller874d77b2000-10-14 16:23:11 +1100547 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +1100548 MD5_Final(digest, &md);
549
Damien Miller874d77b2000-10-14 16:23:11 +1100550 cipher_init(cc, cipher, digest, 16, NULL, 0);
Damien Miller95def091999-11-25 00:26:21 +1100551
552 memset(digest, 0, sizeof(digest));
553 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554}