2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * 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".
13 * Copyright (c) 1999 Niels Provos. All rights reserved.
14 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
38 RCSID("$OpenBSD: cipher.c,v 1.42 2001/01/21 19:05:46 markus Exp $");
44 #include <openssl/md5.h>
49 none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
53 none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
57 none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
59 memcpy(dest, src, len);
64 des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
66 static int dowarn = 1;
68 error("Warning: use of DES is strongly discouraged "
69 "due to cryptographic weaknesses");
72 des_set_key((void *)key, cc->u.des.key);
75 des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
77 memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
80 des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
82 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
86 des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
88 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
94 des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
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);
101 des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
103 memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2));
104 memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3));
107 memcpy(cc->u.des3.iv3, (char *)iv, 8);
110 des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
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);
117 des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
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);
125 * This is used by SSH1:
127 * What kind of triple DES are these 2 routines?
129 * Why is there a redundant initialization vector?
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.
139 des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
141 des_set_key((void *) key, cc->u.des3.key1);
142 des_set_key((void *) (key+8), cc->u.des3.key2);
144 des_set_key((void *) key, cc->u.des3.key3);
146 des_set_key((void *) (key+16), cc->u.des3.key3);
149 des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
153 des_cblock *iv2 = &cc->u.des3.iv2;
154 des_cblock *iv3 = &cc->u.des3.iv3;
156 memcpy(&iv1, iv2, 8);
158 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);
163 des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
167 des_cblock *iv2 = &cc->u.des3.iv2;
168 des_cblock *iv3 = &cc->u.des3.iv3;
170 memcpy(&iv1, iv2, 8);
172 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);
179 blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
181 BF_set_key(&cc->u.bf.key, keylen, (u_char *)key);
184 blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
187 memset(cc->u.bf.iv, 0, 8);
189 memcpy(cc->u.bf.iv, (char *)iv, 8);
192 blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
195 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
199 blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
202 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
207 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
208 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
211 swap_bytes(const u_char *src, u_char *dst, int n)
215 /* Process 4 bytes every lap. */
216 for (n = n / 4; n > 0; n--) {
230 blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
233 swap_bytes(src, dest, len);
234 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
236 swap_bytes(dest, dest, len);
239 blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
242 swap_bytes(src, dest, len);
243 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
245 swap_bytes(dest, dest, len);
250 arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
252 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
255 arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
257 RC4(&cc->u.rc4, len, (u_char *)src, dest);
262 cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
264 CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key);
267 cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
270 fatal("no IV for %s.", cc->cipher->name);
271 memcpy(cc->u.cast.iv, (char *)iv, 8);
274 cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
276 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
280 cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
282 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
288 #define RIJNDAEL_BLOCKSIZE 16
290 rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
292 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);
296 rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
299 fatal("no IV for %s.", cc->cipher->name);
300 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
303 rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
306 rijndael_ctx *ctx = &cc->u.rijndael.enc;
307 u4byte *iv = cc->u.rijndael.iv;
309 u4byte *cprev, *cnow, *plain;
310 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
313 if (len % RIJNDAEL_BLOCKSIZE)
314 fatal("rijndael_cbc_encrypt: bad len %d", len);
315 cnow = (u4byte*) dest;
316 plain = (u4byte*) src;
318 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];
323 rijndael_encrypt(ctx, in, cnow);
326 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
330 rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
333 rijndael_ctx *ctx = &cc->u.rijndael.dec;
334 u4byte *iv = cc->u.rijndael.iv;
336 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
337 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
339 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
342 if (len % RIJNDAEL_BLOCKSIZE)
343 fatal("rijndael_cbc_decrypt: bad len %d", len);
344 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
345 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
346 rijndael_decrypt(ctx, cnow, plain);
347 ivp = (i == 1) ? iv : cnow-4;
353 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
358 SSH_CIPHER_NONE, 8, 0,
359 none_setkey, none_setiv,
360 none_crypt, none_crypt },
362 SSH_CIPHER_DES, 8, 8,
363 des_ssh1_setkey, des_ssh1_setiv,
364 des_ssh1_encrypt, des_ssh1_decrypt },
366 SSH_CIPHER_3DES, 8, 16,
367 des3_ssh1_setkey, des3_setiv,
368 des3_ssh1_encrypt, des3_ssh1_decrypt },
370 SSH_CIPHER_BLOWFISH, 8, 16,
371 blowfish_setkey, blowfish_setiv,
372 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
375 SSH_CIPHER_SSH2, 8, 24,
376 des3_setkey, des3_setiv,
377 des3_cbc_encrypt, des3_cbc_decrypt },
379 SSH_CIPHER_SSH2, 8, 16,
380 blowfish_setkey, blowfish_setiv,
381 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
383 SSH_CIPHER_SSH2, 8, 16,
384 cast_setkey, cast_setiv,
385 cast_cbc_encrypt, cast_cbc_decrypt },
387 SSH_CIPHER_SSH2, 8, 16,
388 arcfour_setkey, none_setiv,
389 arcfour_crypt, arcfour_crypt },
391 SSH_CIPHER_SSH2, 16, 16,
392 rijndael_setkey, rijndael_setiv,
393 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
395 SSH_CIPHER_SSH2, 16, 24,
396 rijndael_setkey, rijndael_setiv,
397 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
399 SSH_CIPHER_SSH2, 16, 32,
400 rijndael_setkey, rijndael_setiv,
401 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
403 SSH_CIPHER_SSH2, 16, 16,
404 rijndael_setkey, rijndael_setiv,
405 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
407 SSH_CIPHER_SSH2, 16, 24,
408 rijndael_setkey, rijndael_setiv,
409 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
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 },
418 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
424 cipher_mask_ssh1(int client)
427 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
428 mask |= 1 << SSH_CIPHER_BLOWFISH;
430 mask |= 1 << SSH_CIPHER_DES;
436 cipher_by_name(const char *name)
439 for (c = ciphers; c->name != NULL; c++)
440 if (strcasecmp(c->name, name) == 0)
446 cipher_by_number(int id)
449 for (c = ciphers; c->name != NULL; c++)
455 #define CIPHER_SEP ","
457 ciphers_valid(const char *names)
463 if (names == NULL || strcmp(names, "") == 0)
465 ciphers = cp = xstrdup(names);
466 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
467 (p = strsep(&cp, CIPHER_SEP))) {
468 c = cipher_by_name(p);
469 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
470 debug("bad cipher %s [%s]", p, names);
474 debug3("cipher ok: %s [%s]", p, names);
477 debug3("ciphers ok: [%s]", names);
483 * Parses the name of the cipher. Returns the number of the corresponding
484 * cipher, or -1 on error.
488 cipher_number(const char *name)
493 c = cipher_by_name(name);
494 return (c==NULL) ? -1 : c->number;
500 Cipher *c = cipher_by_number(id);
501 return (c==NULL) ? "<unknown>" : c->name;
505 cipher_init(CipherContext *cc, Cipher *cipher,
506 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
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);
515 cipher->setkey(cc, key, keylen);
516 cipher->setiv(cc, iv, ivlen);
520 cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
522 if (len % cc->cipher->block_size)
523 fatal("cipher_encrypt: bad plaintext length %d", len);
524 cc->cipher->encrypt(cc, dest, src, len);
528 cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
530 if (len % cc->cipher->block_size)
531 fatal("cipher_decrypt: bad ciphertext length %d", len);
532 cc->cipher->decrypt(cc, dest, src, len);
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.
541 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
542 const char *passphrase)
548 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
549 MD5_Final(digest, &md);
551 cipher_init(cc, cipher, digest, 16, NULL, 0);
553 memset(digest, 0, sizeof(digest));
554 memset(&md, 0, sizeof(md));