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.46 2001/06/25 08:25:36 markus Exp $");
44 #include <openssl/md5.h>
48 none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
52 none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
56 none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
58 memcpy(dest, src, len);
63 des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
65 static int dowarn = 1;
67 error("Warning: use of DES is strongly discouraged "
68 "due to cryptographic weaknesses");
71 des_set_key((void *)key, cc->u.des.key);
74 des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
76 memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
79 des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
81 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
85 des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
87 des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
93 des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
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);
100 des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
102 memset(cc->u.des3.iv1, 0, sizeof(cc->u.des3.iv1));
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,
152 des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &cc->u.des3.iv1,
154 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2,
156 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, &cc->u.des3.iv3,
160 des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
163 des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, &cc->u.des3.iv3,
165 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2,
167 des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &cc->u.des3.iv1,
173 blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
175 BF_set_key(&cc->u.bf.key, keylen, (u_char *)key);
178 blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
181 memset(cc->u.bf.iv, 0, 8);
183 memcpy(cc->u.bf.iv, (char *)iv, 8);
186 blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
189 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
193 blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
196 BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
201 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
202 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
205 swap_bytes(const u_char *src, u_char *dst, int n)
209 /* Process 4 bytes every lap. */
210 for (n = n / 4; n > 0; n--) {
224 blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
227 swap_bytes(src, dest, len);
228 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
230 swap_bytes(dest, dest, len);
233 blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
236 swap_bytes(src, dest, len);
237 BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
239 swap_bytes(dest, dest, len);
244 arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
246 RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
249 arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
251 RC4(&cc->u.rc4, len, (u_char *)src, dest);
256 cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
258 CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key);
261 cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
264 fatal("no IV for %s.", cc->cipher->name);
265 memcpy(cc->u.cast.iv, (char *)iv, 8);
268 cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
270 CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
274 cast_cbc_decrypt(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,
282 #define RIJNDAEL_BLOCKSIZE 16
284 rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
286 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);
290 rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
293 fatal("no IV for %s.", cc->cipher->name);
294 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
297 rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
300 rijndael_ctx *ctx = &cc->u.rijndael.enc;
301 u4byte *iv = cc->u.rijndael.iv;
303 u4byte *cprev, *cnow, *plain;
304 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
307 if (len % RIJNDAEL_BLOCKSIZE)
308 fatal("rijndael_cbc_encrypt: bad len %d", len);
309 cnow = (u4byte*) dest;
310 plain = (u4byte*) src;
312 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];
317 rijndael_encrypt(ctx, in, cnow);
320 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
324 rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
327 rijndael_ctx *ctx = &cc->u.rijndael.dec;
328 u4byte *iv = cc->u.rijndael.iv;
330 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);
331 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);
333 int i, blocks = len / RIJNDAEL_BLOCKSIZE;
336 if (len % RIJNDAEL_BLOCKSIZE)
337 fatal("rijndael_cbc_decrypt: bad len %d", len);
338 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
339 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {
340 rijndael_decrypt(ctx, cnow, plain);
341 ivp = (i == 1) ? iv : cnow-4;
347 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
352 SSH_CIPHER_NONE, 8, 0,
353 none_setkey, none_setiv,
354 none_crypt, none_crypt },
356 SSH_CIPHER_DES, 8, 8,
357 des_ssh1_setkey, des_ssh1_setiv,
358 des_ssh1_encrypt, des_ssh1_decrypt },
360 SSH_CIPHER_3DES, 8, 16,
361 des3_ssh1_setkey, des3_setiv,
362 des3_ssh1_encrypt, des3_ssh1_decrypt },
364 SSH_CIPHER_BLOWFISH, 8, 16,
365 blowfish_setkey, blowfish_setiv,
366 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
369 SSH_CIPHER_SSH2, 8, 24,
370 des3_setkey, des3_setiv,
371 des3_cbc_encrypt, des3_cbc_decrypt },
373 SSH_CIPHER_SSH2, 8, 16,
374 blowfish_setkey, blowfish_setiv,
375 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
377 SSH_CIPHER_SSH2, 8, 16,
378 cast_setkey, cast_setiv,
379 cast_cbc_encrypt, cast_cbc_decrypt },
381 SSH_CIPHER_SSH2, 8, 16,
382 arcfour_setkey, none_setiv,
383 arcfour_crypt, arcfour_crypt },
385 SSH_CIPHER_SSH2, 16, 16,
386 rijndael_setkey, rijndael_setiv,
387 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
389 SSH_CIPHER_SSH2, 16, 24,
390 rijndael_setkey, rijndael_setiv,
391 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
393 SSH_CIPHER_SSH2, 16, 32,
394 rijndael_setkey, rijndael_setiv,
395 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
397 SSH_CIPHER_SSH2, 16, 16,
398 rijndael_setkey, rijndael_setiv,
399 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
401 SSH_CIPHER_SSH2, 16, 24,
402 rijndael_setkey, rijndael_setiv,
403 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
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 },
412 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
418 cipher_mask_ssh1(int client)
421 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
422 mask |= 1 << SSH_CIPHER_BLOWFISH;
424 mask |= 1 << SSH_CIPHER_DES;
430 cipher_by_name(const char *name)
433 for (c = ciphers; c->name != NULL; c++)
434 if (strcasecmp(c->name, name) == 0)
440 cipher_by_number(int id)
443 for (c = ciphers; c->name != NULL; c++)
449 #define CIPHER_SEP ","
451 ciphers_valid(const char *names)
457 if (names == NULL || strcmp(names, "") == 0)
459 ciphers = cp = xstrdup(names);
460 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
461 (p = strsep(&cp, CIPHER_SEP))) {
462 c = cipher_by_name(p);
463 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
464 debug("bad cipher %s [%s]", p, names);
468 debug3("cipher ok: %s [%s]", p, names);
471 debug3("ciphers ok: [%s]", names);
477 * Parses the name of the cipher. Returns the number of the corresponding
478 * cipher, or -1 on error.
482 cipher_number(const char *name)
487 c = cipher_by_name(name);
488 return (c==NULL) ? -1 : c->number;
494 Cipher *c = cipher_by_number(id);
495 return (c==NULL) ? "<unknown>" : c->name;
499 cipher_init(CipherContext *cc, Cipher *cipher,
500 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
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);
509 cipher->setkey(cc, key, keylen);
510 cipher->setiv(cc, iv, ivlen);
514 cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
516 if (len % cc->cipher->block_size)
517 fatal("cipher_encrypt: bad plaintext length %d", len);
518 cc->cipher->encrypt(cc, dest, src, len);
522 cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
524 if (len % cc->cipher->block_size)
525 fatal("cipher_decrypt: bad ciphertext length %d", len);
526 cc->cipher->decrypt(cc, dest, src, len);
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.
535 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
536 const char *passphrase)
542 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
543 MD5_Final(digest, &md);
545 cipher_init(cc, cipher, digest, 16, NULL, 0);
547 memset(digest, 0, sizeof(digest));
548 memset(&md, 0, sizeof(md));