xref: /titanic_50/usr/src/cmd/ssh/libssh/common/cipher.c (revision 6023a540d24df8406f1bd221f66d71e19332fafd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate  *                    All rights reserved
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
77c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
87c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
97c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
107c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Niels Provos.  All rights reserved.
147c478bd9Sstevel@tonic-gate  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
177c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
187c478bd9Sstevel@tonic-gate  * are met:
197c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
207c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
217c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
227c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
237c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
267c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
277c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
287c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
297c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
307c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
317c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
327c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
337c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
347c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
37ee5b3c37Sjp161948 /*
38*6023a540SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
39ee5b3c37Sjp161948  * Use is subject to license terms.
40ee5b3c37Sjp161948  */
41ee5b3c37Sjp161948 
427c478bd9Sstevel@tonic-gate #include "includes.h"
437c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: cipher.c,v 1.61 2002/07/12 15:50:17 markus Exp $");
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "xmalloc.h"
467c478bd9Sstevel@tonic-gate #include "log.h"
477c478bd9Sstevel@tonic-gate #include "cipher.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <openssl/md5.h>
507c478bd9Sstevel@tonic-gate 
51cd7d5fafSJan Pechanec /*
52cd7d5fafSJan Pechanec  * Symmetric ciphers can be offloaded to any engine through the EVP API only.
53cd7d5fafSJan Pechanec  * However, OpenSSL doesn't offer AES in counter mode through EVP. So, we must
54cd7d5fafSJan Pechanec  * define our own EVP functions.
55cd7d5fafSJan Pechanec  */
567c478bd9Sstevel@tonic-gate extern const EVP_CIPHER *evp_aes_128_ctr(void);
57cd7d5fafSJan Pechanec extern const EVP_CIPHER *evp_aes_192_ctr(void);
58cd7d5fafSJan Pechanec extern const EVP_CIPHER *evp_aes_256_ctr(void);
597c478bd9Sstevel@tonic-gate extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static const EVP_CIPHER *evp_ssh1_3des(void);
627c478bd9Sstevel@tonic-gate static const EVP_CIPHER *evp_ssh1_bf(void);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate struct Cipher {
657c478bd9Sstevel@tonic-gate 	char	*name;
667c478bd9Sstevel@tonic-gate 	int	number;		/* for ssh1 only */
677c478bd9Sstevel@tonic-gate 	u_int	block_size;
687c478bd9Sstevel@tonic-gate 	u_int	key_len;
69*6023a540SJan Pechanec 	u_int	discard_len;
707c478bd9Sstevel@tonic-gate 	const EVP_CIPHER	*(*evptype)(void);
717c478bd9Sstevel@tonic-gate } ciphers[] = {
72*6023a540SJan Pechanec 	{ "none",	  SSH_CIPHER_NONE,	8,  0,	  0, EVP_enc_null },
73*6023a540SJan Pechanec 	{ "des",	  SSH_CIPHER_DES,	8,  8,	  0, EVP_des_cbc },
74*6023a540SJan Pechanec 	{ "3des",	  SSH_CIPHER_3DES,	8, 16,	  0, evp_ssh1_3des },
75*6023a540SJan Pechanec 	{ "blowfish",	  SSH_CIPHER_BLOWFISH,  8, 32,	  0, evp_ssh1_bf },
76*6023a540SJan Pechanec 	{ "3des-cbc",	  SSH_CIPHER_SSH2,	8, 24,	  0, EVP_des_ede3_cbc },
77*6023a540SJan Pechanec 	{ "blowfish-cbc", SSH_CIPHER_SSH2,	8, 16,	  0, EVP_bf_cbc },
787c478bd9Sstevel@tonic-gate #ifdef SOLARIS_SSH_ENABLE_CAST5_128
79*6023a540SJan Pechanec 	{ "cast128-cbc",  SSH_CIPHER_SSH2,	8, 16,	  0, EVP_cast5_cbc },
807c478bd9Sstevel@tonic-gate #endif /* SOLARIS_SSH_ENABLE_CAST5_128 */
81*6023a540SJan Pechanec 	{ "arcfour",	  SSH_CIPHER_SSH2,	8, 16,	  0, EVP_rc4 },
82*6023a540SJan Pechanec 	{ "arcfour128",	  SSH_CIPHER_SSH2,	8, 16, 1536, EVP_rc4 },
83*6023a540SJan Pechanec 	{ "arcfour256",	  SSH_CIPHER_SSH2,	8, 32, 1536, EVP_rc4 },
84*6023a540SJan Pechanec 	{ "aes128-cbc",	  SSH_CIPHER_SSH2,     16, 16,	  0, EVP_aes_128_cbc },
85*6023a540SJan Pechanec 	{ "aes192-cbc",	  SSH_CIPHER_SSH2,     16, 24,	  0, EVP_aes_192_cbc },
86*6023a540SJan Pechanec 	{ "aes256-cbc",	  SSH_CIPHER_SSH2,     16, 32,	  0, EVP_aes_256_cbc },
87*6023a540SJan Pechanec 	{ "aes128-ctr",	  SSH_CIPHER_SSH2,     16, 16,	  0, evp_aes_128_ctr },
88*6023a540SJan Pechanec 	{ "aes192-ctr",	  SSH_CIPHER_SSH2,     16, 24,	  0, evp_aes_192_ctr },
89*6023a540SJan Pechanec 	{ "aes256-ctr",	  SSH_CIPHER_SSH2,     16, 32,	  0, evp_aes_256_ctr },
90*6023a540SJan Pechanec 	{ NULL,		  SSH_CIPHER_ILLEGAL,	0,  0,	  0, NULL }
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*--*/
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate u_int
cipher_blocksize(Cipher * c)967c478bd9Sstevel@tonic-gate cipher_blocksize(Cipher *c)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	return (c->block_size);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate u_int
cipher_keylen(Cipher * c)1027c478bd9Sstevel@tonic-gate cipher_keylen(Cipher *c)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	return (c->key_len);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate u_int
cipher_get_number(Cipher * c)1087c478bd9Sstevel@tonic-gate cipher_get_number(Cipher *c)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	return (c->number);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate u_int
cipher_mask_ssh1(int client)1147c478bd9Sstevel@tonic-gate cipher_mask_ssh1(int client)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	u_int mask = 0;
1177c478bd9Sstevel@tonic-gate 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
1187c478bd9Sstevel@tonic-gate 	mask |= 1 << SSH_CIPHER_BLOWFISH;
1197c478bd9Sstevel@tonic-gate 	if (client) {
1207c478bd9Sstevel@tonic-gate 		mask |= 1 << SSH_CIPHER_DES;
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	return mask;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate Cipher *
cipher_by_name(const char * name)1267c478bd9Sstevel@tonic-gate cipher_by_name(const char *name)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	Cipher *c;
1297c478bd9Sstevel@tonic-gate 	for (c = ciphers; c->name != NULL; c++)
1307c478bd9Sstevel@tonic-gate 		if (strcasecmp(c->name, name) == 0)
1317c478bd9Sstevel@tonic-gate 			return c;
1327c478bd9Sstevel@tonic-gate 	return NULL;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate Cipher *
cipher_by_number(int id)1367c478bd9Sstevel@tonic-gate cipher_by_number(int id)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	Cipher *c;
1397c478bd9Sstevel@tonic-gate 	for (c = ciphers; c->name != NULL; c++)
1407c478bd9Sstevel@tonic-gate 		if (c->number == id)
1417c478bd9Sstevel@tonic-gate 			return c;
1427c478bd9Sstevel@tonic-gate 	return NULL;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	CIPHER_SEP	","
1467c478bd9Sstevel@tonic-gate int
ciphers_valid(const char * names)1477c478bd9Sstevel@tonic-gate ciphers_valid(const char *names)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	Cipher *c;
1507c478bd9Sstevel@tonic-gate 	char *ciphers, *cp;
1517c478bd9Sstevel@tonic-gate 	char *p;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (names == NULL || strcmp(names, "") == 0)
1547c478bd9Sstevel@tonic-gate 		return 0;
1557c478bd9Sstevel@tonic-gate 	ciphers = cp = xstrdup(names);
1567c478bd9Sstevel@tonic-gate 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1577c478bd9Sstevel@tonic-gate 	    (p = strsep(&cp, CIPHER_SEP))) {
1587c478bd9Sstevel@tonic-gate 		c = cipher_by_name(p);
1597c478bd9Sstevel@tonic-gate 		if (c == NULL || c->number != SSH_CIPHER_SSH2) {
1607c478bd9Sstevel@tonic-gate 			debug("bad cipher %s [%s]", p, names);
1617c478bd9Sstevel@tonic-gate 			xfree(ciphers);
1627c478bd9Sstevel@tonic-gate 			return 0;
1637c478bd9Sstevel@tonic-gate 		} else {
1647c478bd9Sstevel@tonic-gate 			debug3("cipher ok: %s [%s]", p, names);
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	debug3("ciphers ok: [%s]", names);
1687c478bd9Sstevel@tonic-gate 	xfree(ciphers);
1697c478bd9Sstevel@tonic-gate 	return 1;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Parses the name of the cipher.  Returns the number of the corresponding
1747c478bd9Sstevel@tonic-gate  * cipher, or -1 on error.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int
cipher_number(const char * name)1787c478bd9Sstevel@tonic-gate cipher_number(const char *name)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	Cipher *c;
1817c478bd9Sstevel@tonic-gate 	if (name == NULL)
1827c478bd9Sstevel@tonic-gate 		return -1;
1837c478bd9Sstevel@tonic-gate 	c = cipher_by_name(name);
1847c478bd9Sstevel@tonic-gate 	return (c==NULL) ? -1 : c->number;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate char *
cipher_name(int id)1887c478bd9Sstevel@tonic-gate cipher_name(int id)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	Cipher *c = cipher_by_number(id);
1917c478bd9Sstevel@tonic-gate 	return (c==NULL) ? "<unknown>" : c->name;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate void
cipher_init(CipherContext * cc,Cipher * cipher,const u_char * key,u_int keylen,const u_char * iv,u_int ivlen,int encrypt)1957c478bd9Sstevel@tonic-gate cipher_init(CipherContext *cc, Cipher *cipher,
1967c478bd9Sstevel@tonic-gate     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1977c478bd9Sstevel@tonic-gate     int encrypt)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	static int dowarn = 1;
2007c478bd9Sstevel@tonic-gate 	const EVP_CIPHER *type;
2017c478bd9Sstevel@tonic-gate 	int klen;
202*6023a540SJan Pechanec 	u_char *junk, *discard;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (cipher->number == SSH_CIPHER_DES) {
2057c478bd9Sstevel@tonic-gate 		if (dowarn) {
2067c478bd9Sstevel@tonic-gate 			error("Warning: use of DES is strongly discouraged "
2077c478bd9Sstevel@tonic-gate 			    "due to cryptographic weaknesses");
2087c478bd9Sstevel@tonic-gate 			dowarn = 0;
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		if (keylen > 8)
2117c478bd9Sstevel@tonic-gate 			keylen = 8;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (keylen < cipher->key_len)
2167c478bd9Sstevel@tonic-gate 		fatal("cipher_init: key length %d is insufficient for %s.",
2177c478bd9Sstevel@tonic-gate 		    keylen, cipher->name);
2187c478bd9Sstevel@tonic-gate 	if (iv != NULL && ivlen < cipher->block_size)
2197c478bd9Sstevel@tonic-gate 		fatal("cipher_init: iv length %d is insufficient for %s.",
2207c478bd9Sstevel@tonic-gate 		    ivlen, cipher->name);
2217c478bd9Sstevel@tonic-gate 	cc->cipher = cipher;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	type = (*cipher->evptype)();
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&cc->evp);
226*6023a540SJan Pechanec 	/*
227*6023a540SJan Pechanec 	 * cc->evp is of type EVP_CIPHER_CTX and its key_len will be set to the
228*6023a540SJan Pechanec 	 * default value here for the cipher type. If the requested key length
229*6023a540SJan Pechanec 	 * is different from the default value we will call EVP_CipherInit()
230*6023a540SJan Pechanec 	 * again, see below.
231*6023a540SJan Pechanec 	 */
2327c478bd9Sstevel@tonic-gate 	if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
2337c478bd9Sstevel@tonic-gate 	    (encrypt == CIPHER_ENCRYPT)) == 0)
2347c478bd9Sstevel@tonic-gate 		fatal("cipher_init: EVP_CipherInit failed for %s",
2357c478bd9Sstevel@tonic-gate 		    cipher->name);
2367c478bd9Sstevel@tonic-gate 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
2377c478bd9Sstevel@tonic-gate 	if (klen > 0 && keylen != klen) {
2387c478bd9Sstevel@tonic-gate 		debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
2397c478bd9Sstevel@tonic-gate 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
2407c478bd9Sstevel@tonic-gate 			fatal("cipher_init: set keylen failed (%d -> %d)",
2417c478bd9Sstevel@tonic-gate 			    klen, keylen);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
2447c478bd9Sstevel@tonic-gate 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
2457c478bd9Sstevel@tonic-gate 		    cipher->name);
246*6023a540SJan Pechanec 
247*6023a540SJan Pechanec 	if (cipher->discard_len > 0) {
248*6023a540SJan Pechanec 		junk = xmalloc(cipher->discard_len);
249*6023a540SJan Pechanec 		discard = xmalloc(cipher->discard_len);
250*6023a540SJan Pechanec 		if (EVP_Cipher(&cc->evp, discard, junk,
251*6023a540SJan Pechanec 		    cipher->discard_len) == 0)
252*6023a540SJan Pechanec 			fatal("cipher_init: EVP_Cipher failed during discard");
253*6023a540SJan Pechanec 		memset(discard, 0, cipher->discard_len);
254*6023a540SJan Pechanec 		xfree(junk);
255*6023a540SJan Pechanec 		xfree(discard);
256*6023a540SJan Pechanec 	}
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate void
cipher_crypt(CipherContext * cc,u_char * dest,const u_char * src,u_int len)2607c478bd9Sstevel@tonic-gate cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	if (len % cc->cipher->block_size)
2637c478bd9Sstevel@tonic-gate 		fatal("cipher_encrypt: bad plaintext length %d", len);
2647c478bd9Sstevel@tonic-gate 	if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
2657c478bd9Sstevel@tonic-gate 		fatal("evp_crypt: EVP_Cipher failed");
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate void
cipher_cleanup(CipherContext * cc)2697c478bd9Sstevel@tonic-gate cipher_cleanup(CipherContext *cc)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
2727c478bd9Sstevel@tonic-gate 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Selects the cipher, and keys if by computing the MD5 checksum of the
2777c478bd9Sstevel@tonic-gate  * passphrase and using the resulting 16 bytes as the key.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate void
cipher_set_key_string(CipherContext * cc,Cipher * cipher,const char * passphrase,int encrypt)2817c478bd9Sstevel@tonic-gate cipher_set_key_string(CipherContext *cc, Cipher *cipher,
2827c478bd9Sstevel@tonic-gate     const char *passphrase, int encrypt)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	MD5_CTX md;
2857c478bd9Sstevel@tonic-gate 	u_char digest[16];
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	MD5_Init(&md);
2887c478bd9Sstevel@tonic-gate 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
2897c478bd9Sstevel@tonic-gate 	MD5_Final(digest, &md);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	memset(digest, 0, sizeof(digest));
2947c478bd9Sstevel@tonic-gate 	memset(&md, 0, sizeof(md));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /* Implementations for other non-EVP ciphers */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * This is used by SSH1:
3017c478bd9Sstevel@tonic-gate  *
3027c478bd9Sstevel@tonic-gate  * What kind of triple DES are these 2 routines?
3037c478bd9Sstevel@tonic-gate  *
3047c478bd9Sstevel@tonic-gate  * Why is there a redundant initialization vector?
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  * If only iv3 was used, then, this would till effect have been
3077c478bd9Sstevel@tonic-gate  * outer-cbc. However, there is also a private iv1 == iv2 which
3087c478bd9Sstevel@tonic-gate  * perhaps makes differential analysis easier. On the other hand, the
3097c478bd9Sstevel@tonic-gate  * private iv1 probably makes the CRC-32 attack ineffective. This is a
3107c478bd9Sstevel@tonic-gate  * result of that there is no longer any known iv1 to use when
3117c478bd9Sstevel@tonic-gate  * choosing the X block.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate struct ssh1_3des_ctx
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	EVP_CIPHER_CTX	k1, k2, k3;
3167c478bd9Sstevel@tonic-gate };
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static int
ssh1_3des_init(EVP_CIPHER_CTX * ctx,const u_char * key,const u_char * iv,int enc)3197c478bd9Sstevel@tonic-gate ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
3207c478bd9Sstevel@tonic-gate     int enc)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct ssh1_3des_ctx *c;
3237c478bd9Sstevel@tonic-gate 	u_char *k1, *k2, *k3;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
3267c478bd9Sstevel@tonic-gate 		c = xmalloc(sizeof(*c));
3277c478bd9Sstevel@tonic-gate 		EVP_CIPHER_CTX_set_app_data(ctx, c);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	if (key == NULL)
3307c478bd9Sstevel@tonic-gate 		return (1);
3317c478bd9Sstevel@tonic-gate 	if (enc == -1)
3327c478bd9Sstevel@tonic-gate 		enc = ctx->encrypt;
3337c478bd9Sstevel@tonic-gate 	k1 = k2 = k3 = (u_char *) key;
3347c478bd9Sstevel@tonic-gate 	k2 += 8;
3357c478bd9Sstevel@tonic-gate 	if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
3367c478bd9Sstevel@tonic-gate 		if (enc)
3377c478bd9Sstevel@tonic-gate 			k3 += 16;
3387c478bd9Sstevel@tonic-gate 		else
3397c478bd9Sstevel@tonic-gate 			k1 += 16;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&c->k1);
3427c478bd9Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&c->k2);
3437c478bd9Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&c->k3);
3447c478bd9Sstevel@tonic-gate 	if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
3457c478bd9Sstevel@tonic-gate 	    EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
3467c478bd9Sstevel@tonic-gate 	    EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
3477c478bd9Sstevel@tonic-gate 		memset(c, 0, sizeof(*c));
3487c478bd9Sstevel@tonic-gate 		xfree(c);
3497c478bd9Sstevel@tonic-gate 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
3507c478bd9Sstevel@tonic-gate 		return (0);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	return (1);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate static int
ssh1_3des_cbc(EVP_CIPHER_CTX * ctx,u_char * dest,const u_char * src,u_int len)3567c478bd9Sstevel@tonic-gate ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	struct ssh1_3des_ctx *c;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
3617c478bd9Sstevel@tonic-gate 		error("ssh1_3des_cbc: no context");
3627c478bd9Sstevel@tonic-gate 		return (0);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
3657c478bd9Sstevel@tonic-gate 	    EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
3667c478bd9Sstevel@tonic-gate 	    EVP_Cipher(&c->k3, dest, dest, len) == 0)
3677c478bd9Sstevel@tonic-gate 		return (0);
3687c478bd9Sstevel@tonic-gate 	return (1);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int
ssh1_3des_cleanup(EVP_CIPHER_CTX * ctx)3727c478bd9Sstevel@tonic-gate ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	struct ssh1_3des_ctx *c;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
3777c478bd9Sstevel@tonic-gate 		memset(c, 0, sizeof(*c));
3787c478bd9Sstevel@tonic-gate 		xfree(c);
3797c478bd9Sstevel@tonic-gate 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	return (1);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static const EVP_CIPHER *
evp_ssh1_3des(void)3857c478bd9Sstevel@tonic-gate evp_ssh1_3des(void)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	static EVP_CIPHER ssh1_3des;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
3907c478bd9Sstevel@tonic-gate 	ssh1_3des.nid = NID_undef;
3917c478bd9Sstevel@tonic-gate 	ssh1_3des.block_size = 8;
3927c478bd9Sstevel@tonic-gate 	ssh1_3des.iv_len = 0;
3937c478bd9Sstevel@tonic-gate 	ssh1_3des.key_len = 16;
3947c478bd9Sstevel@tonic-gate 	ssh1_3des.init = ssh1_3des_init;
3957c478bd9Sstevel@tonic-gate 	ssh1_3des.cleanup = ssh1_3des_cleanup;
3967c478bd9Sstevel@tonic-gate 	ssh1_3des.do_cipher = ssh1_3des_cbc;
3977c478bd9Sstevel@tonic-gate 	ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
3987c478bd9Sstevel@tonic-gate 	return (&ssh1_3des);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * SSH1 uses a variation on Blowfish, all bytes must be swapped before
4037c478bd9Sstevel@tonic-gate  * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate static void
swap_bytes(const u_char * src,u_char * dst,int n)4067c478bd9Sstevel@tonic-gate swap_bytes(const u_char *src, u_char *dst, int n)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	u_char c[4];
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* Process 4 bytes every lap. */
4117c478bd9Sstevel@tonic-gate 	for (n = n / 4; n > 0; n--) {
4127c478bd9Sstevel@tonic-gate 		c[3] = *src++;
4137c478bd9Sstevel@tonic-gate 		c[2] = *src++;
4147c478bd9Sstevel@tonic-gate 		c[1] = *src++;
4157c478bd9Sstevel@tonic-gate 		c[0] = *src++;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		*dst++ = c[0];
4187c478bd9Sstevel@tonic-gate 		*dst++ = c[1];
4197c478bd9Sstevel@tonic-gate 		*dst++ = c[2];
4207c478bd9Sstevel@tonic-gate 		*dst++ = c[3];
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static int
bf_ssh1_cipher(EVP_CIPHER_CTX * ctx,u_char * out,const u_char * in,u_int len)4277c478bd9Sstevel@tonic-gate bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	int ret;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	swap_bytes(in, out, len);
4327c478bd9Sstevel@tonic-gate 	ret = (*orig_bf)(ctx, out, out, len);
4337c478bd9Sstevel@tonic-gate 	swap_bytes(out, out, len);
4347c478bd9Sstevel@tonic-gate 	return (ret);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static const EVP_CIPHER *
evp_ssh1_bf(void)4387c478bd9Sstevel@tonic-gate evp_ssh1_bf(void)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	static EVP_CIPHER ssh1_bf;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
4437c478bd9Sstevel@tonic-gate 	orig_bf = ssh1_bf.do_cipher;
4447c478bd9Sstevel@tonic-gate 	ssh1_bf.nid = NID_undef;
4457c478bd9Sstevel@tonic-gate 	ssh1_bf.do_cipher = bf_ssh1_cipher;
4467c478bd9Sstevel@tonic-gate 	ssh1_bf.key_len = 32;
4477c478bd9Sstevel@tonic-gate 	return (&ssh1_bf);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Exports an IV from the CipherContext required to export the key
4527c478bd9Sstevel@tonic-gate  * state back from the unprivileged child to the privileged parent
4537c478bd9Sstevel@tonic-gate  * process.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate int
cipher_get_keyiv_len(CipherContext * cc)4577c478bd9Sstevel@tonic-gate cipher_get_keyiv_len(CipherContext *cc)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	Cipher *c = cc->cipher;
4607c478bd9Sstevel@tonic-gate 	int ivlen;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (c->number == SSH_CIPHER_3DES)
4637c478bd9Sstevel@tonic-gate 		ivlen = 24;
4647c478bd9Sstevel@tonic-gate 	else
4657c478bd9Sstevel@tonic-gate 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
4667c478bd9Sstevel@tonic-gate 	return (ivlen);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate void
cipher_get_keyiv(CipherContext * cc,u_char * iv,u_int len)4707c478bd9Sstevel@tonic-gate cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	Cipher *c = cc->cipher;
4737c478bd9Sstevel@tonic-gate 	u_char *civ = NULL;
4747c478bd9Sstevel@tonic-gate 	int evplen;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	switch (c->number) {
4777c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_SSH2:
4787c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_DES:
4797c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_BLOWFISH:
4807c478bd9Sstevel@tonic-gate 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
4817c478bd9Sstevel@tonic-gate 		if (evplen == 0)
4827c478bd9Sstevel@tonic-gate 			return;
4837c478bd9Sstevel@tonic-gate 		if (evplen != len)
4847c478bd9Sstevel@tonic-gate 			fatal("%s: wrong iv length %d != %d", __func__,
4857c478bd9Sstevel@tonic-gate 			    evplen, len);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (c->evptype == evp_aes_128_ctr) {
4887c478bd9Sstevel@tonic-gate 			ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
4897c478bd9Sstevel@tonic-gate 			return;
4907c478bd9Sstevel@tonic-gate 		} else {
4917c478bd9Sstevel@tonic-gate 			civ = cc->evp.iv;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		break;
4947c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_3DES: {
4957c478bd9Sstevel@tonic-gate 		struct ssh1_3des_ctx *desc;
4967c478bd9Sstevel@tonic-gate 		if (len != 24)
4977c478bd9Sstevel@tonic-gate 			fatal("%s: bad 3des iv length: %d", __func__, len);
4987c478bd9Sstevel@tonic-gate 		desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
4997c478bd9Sstevel@tonic-gate 		if (desc == NULL)
5007c478bd9Sstevel@tonic-gate 			fatal("%s: no 3des context", __func__);
5017c478bd9Sstevel@tonic-gate 		debug3("%s: Copying 3DES IV", __func__);
5027c478bd9Sstevel@tonic-gate 		memcpy(iv, desc->k1.iv, 8);
5037c478bd9Sstevel@tonic-gate 		memcpy(iv + 8, desc->k2.iv, 8);
5047c478bd9Sstevel@tonic-gate 		memcpy(iv + 16, desc->k3.iv, 8);
5057c478bd9Sstevel@tonic-gate 		return;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 	default:
5087c478bd9Sstevel@tonic-gate 		fatal("%s: bad cipher %d", __func__, c->number);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	memcpy(iv, civ, len);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate void
cipher_set_keyiv(CipherContext * cc,u_char * iv)5147c478bd9Sstevel@tonic-gate cipher_set_keyiv(CipherContext *cc, u_char *iv)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	Cipher *c = cc->cipher;
5177c478bd9Sstevel@tonic-gate 	u_char *div = NULL;
5187c478bd9Sstevel@tonic-gate 	int evplen = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	switch (c->number) {
5217c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_SSH2:
5227c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_DES:
5237c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_BLOWFISH:
5247c478bd9Sstevel@tonic-gate 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
5257c478bd9Sstevel@tonic-gate 		if (evplen == 0)
5267c478bd9Sstevel@tonic-gate 			return;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		if (c->evptype == evp_aes_128_ctr) {
5297c478bd9Sstevel@tonic-gate 			ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
5307c478bd9Sstevel@tonic-gate 			return;
5317c478bd9Sstevel@tonic-gate 		} else {
5327c478bd9Sstevel@tonic-gate 			div = cc->evp.iv;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 		break;
5357c478bd9Sstevel@tonic-gate 	case SSH_CIPHER_3DES: {
5367c478bd9Sstevel@tonic-gate 		struct ssh1_3des_ctx *desc;
5377c478bd9Sstevel@tonic-gate 		desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
5387c478bd9Sstevel@tonic-gate 		if (desc == NULL)
5397c478bd9Sstevel@tonic-gate 			fatal("%s: no 3des context", __func__);
5407c478bd9Sstevel@tonic-gate 		debug3("%s: Installed 3DES IV", __func__);
5417c478bd9Sstevel@tonic-gate 		memcpy(desc->k1.iv, iv, 8);
5427c478bd9Sstevel@tonic-gate 		memcpy(desc->k2.iv, iv + 8, 8);
5437c478bd9Sstevel@tonic-gate 		memcpy(desc->k3.iv, iv + 16, 8);
5447c478bd9Sstevel@tonic-gate 		return;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	default:
5477c478bd9Sstevel@tonic-gate 		fatal("%s: bad cipher %d", __func__, c->number);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	memcpy(div, iv, evplen);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate #if OPENSSL_VERSION_NUMBER < 0x00907000L
5537c478bd9Sstevel@tonic-gate #define EVP_X_STATE(evp)	&(evp).c
5547c478bd9Sstevel@tonic-gate #define EVP_X_STATE_LEN(evp)	sizeof((evp).c)
5557c478bd9Sstevel@tonic-gate #else
5567c478bd9Sstevel@tonic-gate #define EVP_X_STATE(evp)	(evp).cipher_data
5577c478bd9Sstevel@tonic-gate #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
5587c478bd9Sstevel@tonic-gate #endif
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate int
cipher_get_keycontext(CipherContext * cc,u_char * dat)5617c478bd9Sstevel@tonic-gate cipher_get_keycontext(CipherContext *cc, u_char *dat)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	int plen = 0;
5647c478bd9Sstevel@tonic-gate 	Cipher *c = cc->cipher;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (c->evptype == EVP_rc4) {
5677c478bd9Sstevel@tonic-gate 		plen = EVP_X_STATE_LEN(cc->evp);
5687c478bd9Sstevel@tonic-gate 		if (dat == NULL)
5697c478bd9Sstevel@tonic-gate 			return (plen);
5707c478bd9Sstevel@tonic-gate 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	return (plen);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate void
cipher_set_keycontext(CipherContext * cc,u_char * dat)5767c478bd9Sstevel@tonic-gate cipher_set_keycontext(CipherContext *cc, u_char *dat)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	Cipher *c = cc->cipher;
5797c478bd9Sstevel@tonic-gate 	int plen;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (c->evptype == EVP_rc4) {
5827c478bd9Sstevel@tonic-gate 		plen = EVP_X_STATE_LEN(cc->evp);
5837c478bd9Sstevel@tonic-gate 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate }
586