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.61 2002/07/12 15:50:17 markus Exp $");
39 RCSID("$FreeBSD: src/crypto/openssh/cipher.c,v 1.2.2.6 2003/02/03 17:31:06 des Exp $");
40 RCSID("$DragonFly: src/crypto/openssh/Attic/cipher.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
46 #include <openssl/md5.h>
48 #if OPENSSL_VERSION_NUMBER < 0x00906000L
50 #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
53 #if OPENSSL_VERSION_NUMBER < 0x00907000L
55 static const EVP_CIPHER *evp_rijndael(void);
57 static const EVP_CIPHER *evp_ssh1_3des(void);
58 static const EVP_CIPHER *evp_ssh1_bf(void);
62 int number; /* for ssh1 only */
65 const EVP_CIPHER *(*evptype)(void);
67 { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
68 { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
69 { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
70 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
72 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
73 { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
74 { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
75 { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
76 #if OPENSSL_VERSION_NUMBER < 0x00907000L
77 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
78 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
79 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
80 { "rijndael-cbc@lysator.liu.se",
81 SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
83 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc },
84 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc },
85 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
86 { "rijndael-cbc@lysator.liu.se",
87 SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
90 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL }
96 cipher_blocksize(Cipher *c)
98 return (c->block_size);
102 cipher_keylen(Cipher *c)
108 cipher_get_number(Cipher *c)
114 cipher_mask_ssh1(int client)
117 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
118 mask |= 1 << SSH_CIPHER_BLOWFISH;
120 mask |= 1 << SSH_CIPHER_DES;
126 cipher_by_name(const char *name)
129 for (c = ciphers; c->name != NULL; c++)
130 if (strcasecmp(c->name, name) == 0)
136 cipher_by_number(int id)
139 for (c = ciphers; c->name != NULL; c++)
145 #define CIPHER_SEP ","
147 ciphers_valid(const char *names)
153 if (names == NULL || strcmp(names, "") == 0)
155 ciphers = cp = xstrdup(names);
156 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
157 (p = strsep(&cp, CIPHER_SEP))) {
158 c = cipher_by_name(p);
159 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
160 debug("bad cipher %s [%s]", p, names);
164 debug3("cipher ok: %s [%s]", p, names);
167 debug3("ciphers ok: [%s]", names);
173 * Parses the name of the cipher. Returns the number of the corresponding
174 * cipher, or -1 on error.
178 cipher_number(const char *name)
183 c = cipher_by_name(name);
184 return (c==NULL) ? -1 : c->number;
190 Cipher *c = cipher_by_number(id);
191 return (c==NULL) ? "<unknown>" : c->name;
195 cipher_init(CipherContext *cc, Cipher *cipher,
196 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
199 static int dowarn = 1;
203 const EVP_CIPHER *type;
207 if (cipher->number == SSH_CIPHER_DES) {
209 error("Warning: use of DES is strongly discouraged "
210 "due to cryptographic weaknesses");
216 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
218 if (keylen < cipher->key_len)
219 fatal("cipher_init: key length %d is insufficient for %s.",
220 keylen, cipher->name);
221 if (iv != NULL && ivlen < cipher->block_size)
222 fatal("cipher_init: iv length %d is insufficient for %s.",
223 ivlen, cipher->name);
226 type = (*cipher->evptype)();
228 EVP_CIPHER_CTX_init(&cc->evp);
230 if (type->key_len > 0 && type->key_len != keylen) {
231 debug("cipher_init: set keylen (%d -> %d)",
232 type->key_len, keylen);
233 type->key_len = keylen;
235 EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
236 (encrypt == CIPHER_ENCRYPT));
238 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
239 (encrypt == CIPHER_ENCRYPT)) == 0)
240 fatal("cipher_init: EVP_CipherInit failed for %s",
242 klen = EVP_CIPHER_CTX_key_length(&cc->evp);
243 if (klen > 0 && keylen != klen) {
244 debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
245 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
246 fatal("cipher_init: set keylen failed (%d -> %d)",
249 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
250 fatal("cipher_init: EVP_CipherInit: set key failed for %s",
256 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
258 if (len % cc->cipher->block_size)
259 fatal("cipher_encrypt: bad plaintext length %d", len);
261 EVP_Cipher(&cc->evp, dest, (u_char *)src, len);
263 if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
264 fatal("evp_crypt: EVP_Cipher failed");
269 cipher_cleanup(CipherContext *cc)
272 EVP_CIPHER_CTX_cleanup(&cc->evp);
274 if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
275 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
280 * Selects the cipher, and keys if by computing the MD5 checksum of the
281 * passphrase and using the resulting 16 bytes as the key.
285 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
286 const char *passphrase, int encrypt)
292 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
293 MD5_Final(digest, &md);
295 cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
297 memset(digest, 0, sizeof(digest));
298 memset(&md, 0, sizeof(md));
301 /* Implementations for other non-EVP ciphers */
304 * This is used by SSH1:
306 * What kind of triple DES are these 2 routines?
308 * Why is there a redundant initialization vector?
310 * If only iv3 was used, then, this would till effect have been
311 * outer-cbc. However, there is also a private iv1 == iv2 which
312 * perhaps makes differential analysis easier. On the other hand, the
313 * private iv1 probably makes the CRC-32 attack ineffective. This is a
314 * result of that there is no longer any known iv1 to use when
315 * choosing the X block.
319 EVP_CIPHER_CTX k1, k2, k3;
323 ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
326 struct ssh1_3des_ctx *c;
327 u_char *k1, *k2, *k3;
329 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
330 c = xmalloc(sizeof(*c));
331 EVP_CIPHER_CTX_set_app_data(ctx, c);
337 k1 = k2 = k3 = (u_char *) key;
339 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
345 EVP_CIPHER_CTX_init(&c->k1);
346 EVP_CIPHER_CTX_init(&c->k2);
347 EVP_CIPHER_CTX_init(&c->k3);
349 EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
350 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
351 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
353 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
354 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
355 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
356 memset(c, 0, sizeof(*c));
358 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
366 ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
368 struct ssh1_3des_ctx *c;
370 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
371 error("ssh1_3des_cbc: no context");
375 EVP_Cipher(&c->k1, dest, (u_char *)src, len);
376 EVP_Cipher(&c->k2, dest, dest, len);
377 EVP_Cipher(&c->k3, dest, dest, len);
379 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
380 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
381 EVP_Cipher(&c->k3, dest, dest, len) == 0)
388 ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
390 struct ssh1_3des_ctx *c;
392 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
393 memset(c, 0, sizeof(*c));
395 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
400 static const EVP_CIPHER *
403 static EVP_CIPHER ssh1_3des;
405 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
406 ssh1_3des.nid = NID_undef;
407 ssh1_3des.block_size = 8;
408 ssh1_3des.iv_len = 0;
409 ssh1_3des.key_len = 16;
410 ssh1_3des.init = ssh1_3des_init;
411 ssh1_3des.cleanup = ssh1_3des_cleanup;
412 ssh1_3des.do_cipher = ssh1_3des_cbc;
414 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
420 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
421 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
424 swap_bytes(const u_char *src, u_char *dst, int n)
428 /* Process 4 bytes every lap. */
429 for (n = n / 4; n > 0; n--) {
443 static void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key,
444 const unsigned char *iv, int enc)
447 memcpy (&(ctx->oiv[0]), iv, 8);
448 memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8);
450 BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx),
454 static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
457 bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
461 swap_bytes(in, out, len);
462 ret = (*orig_bf)(ctx, out, out, len);
463 swap_bytes(out, out, len);
467 static const EVP_CIPHER *
470 static EVP_CIPHER ssh1_bf;
472 memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
473 orig_bf = ssh1_bf.do_cipher;
474 ssh1_bf.nid = NID_undef;
476 ssh1_bf.init = bf_ssh1_init;
478 ssh1_bf.do_cipher = bf_ssh1_cipher;
479 ssh1_bf.key_len = 32;
483 #if OPENSSL_VERSION_NUMBER < 0x00907000L
485 #define RIJNDAEL_BLOCKSIZE 16
486 struct ssh_rijndael_ctx
489 u_char r_iv[RIJNDAEL_BLOCKSIZE];
493 ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
496 struct ssh_rijndael_ctx *c;
498 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
499 c = xmalloc(sizeof(*c));
500 EVP_CIPHER_CTX_set_app_data(ctx, c);
505 rijndael_set_key(&c->r_ctx, (u_char *)key,
506 8*EVP_CIPHER_CTX_key_length(ctx), enc);
509 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
514 ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
517 struct ssh_rijndael_ctx *c;
518 u_char buf[RIJNDAEL_BLOCKSIZE];
519 u_char *cprev, *cnow, *plain, *ivp;
520 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
524 if (len % RIJNDAEL_BLOCKSIZE)
525 fatal("ssh_rijndael_cbc: bad len %d", len);
526 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
527 error("ssh_rijndael_cbc: no context");
532 plain = (u_char *)src;
534 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
535 cnow+=RIJNDAEL_BLOCKSIZE) {
536 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
537 buf[j] = plain[j] ^ cprev[j];
538 rijndael_encrypt(&c->r_ctx, buf, cnow);
541 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
543 cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
544 plain = dest+len-RIJNDAEL_BLOCKSIZE;
546 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
547 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
548 plain-=RIJNDAEL_BLOCKSIZE) {
549 rijndael_decrypt(&c->r_ctx, cnow, plain);
550 ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
551 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
554 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
560 ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
562 struct ssh_rijndael_ctx *c;
564 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
565 memset(c, 0, sizeof(*c));
567 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
572 static const EVP_CIPHER *
575 static EVP_CIPHER rijndal_cbc;
577 memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
578 rijndal_cbc.nid = NID_undef;
579 rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
580 rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
581 rijndal_cbc.key_len = 16;
582 rijndal_cbc.init = ssh_rijndael_init;
583 rijndal_cbc.cleanup = ssh_rijndael_cleanup;
584 rijndal_cbc.do_cipher = ssh_rijndael_cbc;
586 rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
587 EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
589 return (&rijndal_cbc);
594 * Exports an IV from the CipherContext required to export the key
595 * state back from the unprivileged child to the privileged parent
600 cipher_get_keyiv_len(CipherContext *cc)
602 Cipher *c = cc->cipher;
605 if (c->number == SSH_CIPHER_3DES)
608 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
613 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
615 Cipher *c = cc->cipher;
620 case SSH_CIPHER_SSH2:
622 case SSH_CIPHER_BLOWFISH:
623 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
627 fatal("%s: wrong iv length %d != %d", __func__,
630 #if OPENSSL_VERSION_NUMBER < 0x00907000L
631 if (c->evptype == evp_rijndael) {
632 struct ssh_rijndael_ctx *aesc;
634 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
636 fatal("%s: no rijndael context", __func__);
644 case SSH_CIPHER_3DES: {
645 struct ssh1_3des_ctx *desc;
647 fatal("%s: bad 3des iv length: %d", __func__, len);
648 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
650 fatal("%s: no 3des context", __func__);
651 debug3("%s: Copying 3DES IV", __func__);
652 memcpy(iv, desc->k1.iv, 8);
653 memcpy(iv + 8, desc->k2.iv, 8);
654 memcpy(iv + 16, desc->k3.iv, 8);
658 fatal("%s: bad cipher %d", __func__, c->number);
660 memcpy(iv, civ, len);
664 cipher_set_keyiv(CipherContext *cc, u_char *iv)
666 Cipher *c = cc->cipher;
671 case SSH_CIPHER_SSH2:
673 case SSH_CIPHER_BLOWFISH:
674 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
678 #if OPENSSL_VERSION_NUMBER < 0x00907000L
679 if (c->evptype == evp_rijndael) {
680 struct ssh_rijndael_ctx *aesc;
682 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
684 fatal("%s: no rijndael context", __func__);
692 case SSH_CIPHER_3DES: {
693 struct ssh1_3des_ctx *desc;
694 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
696 fatal("%s: no 3des context", __func__);
697 debug3("%s: Installed 3DES IV", __func__);
698 memcpy(desc->k1.iv, iv, 8);
699 memcpy(desc->k2.iv, iv + 8, 8);
700 memcpy(desc->k3.iv, iv + 16, 8);
704 fatal("%s: bad cipher %d", __func__, c->number);
706 memcpy(div, iv, evplen);
709 #if OPENSSL_VERSION_NUMBER < 0x00907000L
710 #define EVP_X_STATE(evp) &(evp).c
711 #define EVP_X_STATE_LEN(evp) sizeof((evp).c)
713 #define EVP_X_STATE(evp) (evp).cipher_data
714 #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
718 cipher_get_keycontext(CipherContext *cc, u_char *dat)
720 Cipher *c = cc->cipher;
723 if (c->evptype == EVP_rc4) {
724 plen = EVP_X_STATE_LEN(cc->evp);
727 memcpy(dat, EVP_X_STATE(cc->evp), plen);
733 cipher_set_keycontext(CipherContext *cc, u_char *dat)
735 Cipher *c = cc->cipher;
738 if (c->evptype == EVP_rc4) {
739 plen = EVP_X_STATE_LEN(cc->evp);
740 memcpy(EVP_X_STATE(cc->evp), dat, plen);