Home | History | Annotate | Line # | Download | only in dist
cipher.c revision 1.6
      1 /*	$NetBSD: cipher.c,v 1.6 2014/10/19 16:30:58 christos Exp $	*/
      2 /* $OpenBSD: cipher.c,v 1.99 2014/06/24 01:13:21 djm Exp $ */
      3 /*
      4  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      5  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      6  *                    All rights reserved
      7  *
      8  * As far as I am concerned, the code I have written for this software
      9  * can be used freely for any purpose.  Any derived versions of this
     10  * software must be clearly marked as such, and if the derived work is
     11  * incompatible with the protocol description in the RFC file, it must be
     12  * called by a name other than "ssh" or "Secure Shell".
     13  *
     14  *
     15  * Copyright (c) 1999 Niels Provos.  All rights reserved.
     16  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "includes.h"
     40 __RCSID("$NetBSD: cipher.c,v 1.6 2014/10/19 16:30:58 christos Exp $");
     41 #include <sys/types.h>
     42 
     43 #include <string.h>
     44 #include <stdarg.h>
     45 #include <stdio.h>
     46 
     47 #include "cipher.h"
     48 #include "misc.h"
     49 #include "sshbuf.h"
     50 #include "ssherr.h"
     51 #include "digest.h"
     52 
     53 #ifdef WITH_SSH1
     54 extern const EVP_CIPHER *evp_ssh1_bf(void);
     55 extern const EVP_CIPHER *evp_ssh1_3des(void);
     56 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
     57 #endif
     58 
     59 struct sshcipher {
     60 	const char	*name;
     61 	int	number;		/* for ssh1 only */
     62 	u_int	block_size;
     63 	u_int	key_len;
     64 	u_int	iv_len;		/* defaults to block_size */
     65 	u_int	auth_len;
     66 	u_int	discard_len;
     67 	u_int	flags;
     68 #define CFLAG_CBC		(1<<0)
     69 #define CFLAG_CHACHAPOLY	(1<<1)
     70 #define CFLAG_AESCTR		(1<<2)
     71 #define CFLAG_NONE		(1<<3)
     72 #ifdef WITH_OPENSSL
     73 	const EVP_CIPHER	*(*evptype)(void);
     74 #else
     75 	void	*ignored;
     76 #endif
     77 };
     78 
     79 static const struct sshcipher ciphers[] = {
     80 #ifdef WITH_SSH1
     81 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
     82 	{ "3des",	SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
     83 	{ "blowfish",	SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
     84 #endif
     85 #ifdef WITH_OPENSSL
     86 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
     87 	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
     88 	{ "blowfish-cbc",
     89 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
     90 	{ "cast128-cbc",
     91 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
     92 	{ "arcfour",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
     93 	{ "arcfour128",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
     94 	{ "arcfour256",	SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
     95 	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
     96 	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
     97 	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
     98 	{ "rijndael-cbc (at) lysator.liu.se",
     99 			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
    100 #ifdef AES_CTR_MT
    101 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, evp_aes_ctr_mt },
    102 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, evp_aes_ctr_mt },
    103 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, evp_aes_ctr_mt },
    104 #else
    105 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
    106 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
    107 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
    108 #endif
    109 	{ "aes128-gcm (at) openssh.com",
    110 			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
    111 	{ "aes256-gcm (at) openssh.com",
    112 			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
    113 #else
    114 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
    115 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
    116 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
    117 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
    118 #endif
    119 	{ "chacha20-poly1305 (at) openssh.com",
    120 			SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
    121 
    122 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
    123 };
    124 
    125 /*--*/
    126 
    127 /* Returns a comma-separated list of supported ciphers. */
    128 char *
    129 cipher_alg_list(char sep, int auth_only)
    130 {
    131 	char *tmp, *ret = NULL;
    132 	size_t nlen, rlen = 0;
    133 	const struct sshcipher *c;
    134 
    135 	for (c = ciphers; c->name != NULL; c++) {
    136 		if (c->number != SSH_CIPHER_SSH2)
    137 			continue;
    138 		if (auth_only && c->auth_len == 0)
    139 			continue;
    140 		if (ret != NULL)
    141 			ret[rlen++] = sep;
    142 		nlen = strlen(c->name);
    143 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
    144 			free(ret);
    145 			return NULL;
    146 		}
    147 		ret = tmp;
    148 		memcpy(ret + rlen, c->name, nlen + 1);
    149 		rlen += nlen;
    150 	}
    151 	return ret;
    152 }
    153 
    154 u_int
    155 cipher_blocksize(const struct sshcipher *c)
    156 {
    157 	return (c->block_size);
    158 }
    159 
    160 u_int
    161 cipher_keylen(const struct sshcipher *c)
    162 {
    163 	return (c->key_len);
    164 }
    165 
    166 u_int
    167 cipher_seclen(const struct sshcipher *c)
    168 {
    169 	if (strcmp("3des-cbc", c->name) == 0)
    170 		return 14;
    171 	return cipher_keylen(c);
    172 }
    173 
    174 u_int
    175 cipher_authlen(const struct sshcipher *c)
    176 {
    177 	return (c->auth_len);
    178 }
    179 
    180 u_int
    181 cipher_ivlen(const struct sshcipher *c)
    182 {
    183 	/*
    184 	 * Default is cipher block size, except for chacha20+poly1305 that
    185 	 * needs no IV. XXX make iv_len == -1 default?
    186 	 */
    187 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
    188 	    c->iv_len : c->block_size;
    189 }
    190 
    191 u_int
    192 cipher_get_number(const struct sshcipher *c)
    193 {
    194 	return (c->number);
    195 }
    196 
    197 u_int
    198 cipher_is_cbc(const struct sshcipher *c)
    199 {
    200 	return (c->flags & CFLAG_CBC) != 0;
    201 }
    202 
    203 u_int
    204 cipher_mask_ssh1(int client)
    205 {
    206 	u_int mask = 0;
    207 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
    208 	mask |= 1 << SSH_CIPHER_BLOWFISH;
    209 	if (client) {
    210 		mask |= 1 << SSH_CIPHER_DES;
    211 	}
    212 	return mask;
    213 }
    214 
    215 const struct sshcipher *
    216 cipher_by_name(const char *name)
    217 {
    218 	const struct sshcipher *c;
    219 	for (c = ciphers; c->name != NULL; c++)
    220 		if (strcmp(c->name, name) == 0)
    221 			return c;
    222 	return NULL;
    223 }
    224 
    225 const struct sshcipher *
    226 cipher_by_number(int id)
    227 {
    228 	const struct sshcipher *c;
    229 	for (c = ciphers; c->name != NULL; c++)
    230 		if (c->number == id)
    231 			return c;
    232 	return NULL;
    233 }
    234 
    235 #define	CIPHER_SEP	","
    236 int
    237 ciphers_valid(const char *names)
    238 {
    239 	const struct sshcipher *c;
    240 	char *cipher_list, *cp;
    241 	char *p;
    242 
    243 	if (names == NULL || strcmp(names, "") == 0)
    244 		return 0;
    245 	if ((cipher_list = cp = strdup(names)) == NULL)
    246 		return 0;
    247 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
    248 	    (p = strsep(&cp, CIPHER_SEP))) {
    249 		c = cipher_by_name(p);
    250 		if (c == NULL || (c->number != SSH_CIPHER_SSH2 &&
    251 c->number != SSH_CIPHER_NONE)) {
    252 			free(cipher_list);
    253 			return 0;
    254 		}
    255 	}
    256 	free(cipher_list);
    257 	return 1;
    258 }
    259 
    260 /*
    261  * Parses the name of the cipher.  Returns the number of the corresponding
    262  * cipher, or -1 on error.
    263  */
    264 
    265 int
    266 cipher_number(const char *name)
    267 {
    268 	const struct sshcipher *c;
    269 	if (name == NULL)
    270 		return -1;
    271 	for (c = ciphers; c->name != NULL; c++)
    272 		if (strcasecmp(c->name, name) == 0)
    273 			return c->number;
    274 	return -1;
    275 }
    276 
    277 const char *
    278 cipher_name(int id)
    279 {
    280 	const struct sshcipher *c = cipher_by_number(id);
    281 	return (c==NULL) ? "<unknown>" : c->name;
    282 }
    283 
    284 const char *
    285 cipher_warning_message(const struct sshcipher_ctx *cc)
    286 {
    287 	if (cc == NULL || cc->cipher == NULL)
    288 		return NULL;
    289 	if (cc->cipher->number == SSH_CIPHER_DES)
    290 		return "use of DES is strongly discouraged due to "
    291 		    "cryptographic weaknesses";
    292 	return NULL;
    293 }
    294 
    295 int
    296 cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
    297     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
    298     int do_encrypt)
    299 {
    300 #ifdef WITH_OPENSSL
    301 	int ret = SSH_ERR_INTERNAL_ERROR;
    302 	const EVP_CIPHER *type;
    303 	int klen;
    304 	u_char *junk, *discard;
    305 
    306 	if (cipher->number == SSH_CIPHER_DES) {
    307 		if (keylen > 8)
    308 			keylen = 8;
    309 	}
    310 #endif
    311 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
    312 	cc->encrypt = do_encrypt;
    313 
    314 	if (keylen < cipher->key_len ||
    315 	    (iv != NULL && ivlen < cipher_ivlen(cipher)))
    316 		return SSH_ERR_INVALID_ARGUMENT;
    317 
    318 	cc->cipher = cipher;
    319 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    320 		return chachapoly_init(&cc->cp_ctx, key, keylen);
    321 	}
    322 #ifndef WITH_OPENSSL
    323 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    324 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
    325 		aesctr_ivsetup(&cc->ac_ctx, iv);
    326 		return 0;
    327 	}
    328 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    329 		return 0;
    330 	return SSH_ERR_INVALID_ARGUMENT;
    331 #else
    332 	type = (*cipher->evptype)();
    333 	EVP_CIPHER_CTX_init(&cc->evp);
    334 	if (EVP_CipherInit(&cc->evp, type, NULL, __UNCONST(iv),
    335 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
    336 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    337 		goto bad;
    338 	}
    339 	if (cipher_authlen(cipher) &&
    340 	    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
    341 	    -1, __UNCONST(iv))) {
    342 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    343 		goto bad;
    344 	}
    345 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
    346 	if (klen > 0 && keylen != (u_int)klen) {
    347 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) {
    348 			ret = SSH_ERR_LIBCRYPTO_ERROR;
    349 			goto bad;
    350 		}
    351 	}
    352 	if (EVP_CipherInit(&cc->evp, NULL, __UNCONST(key), NULL, -1) == 0) {
    353 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    354 		goto bad;
    355 	}
    356 
    357 	if (cipher->discard_len > 0) {
    358 		if ((junk = malloc(cipher->discard_len)) == NULL ||
    359 		    (discard = malloc(cipher->discard_len)) == NULL) {
    360 			if (junk != NULL)
    361 				free(junk);
    362 			ret = SSH_ERR_ALLOC_FAIL;
    363 			goto bad;
    364 		}
    365 		ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len);
    366 		explicit_bzero(discard, cipher->discard_len);
    367 		free(junk);
    368 		free(discard);
    369 		if (ret != 1) {
    370 			ret = SSH_ERR_LIBCRYPTO_ERROR;
    371  bad:
    372 			EVP_CIPHER_CTX_cleanup(&cc->evp);
    373 			return ret;
    374 		}
    375 	}
    376 #endif
    377 	return 0;
    378 }
    379 
    380 /*
    381  * cipher_crypt() operates as following:
    382  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
    383  * Theses bytes are treated as additional authenticated data for
    384  * authenticated encryption modes.
    385  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
    386  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
    387  * This tag is written on encryption and verified on decryption.
    388  * Both 'aadlen' and 'authlen' can be set to 0.
    389  */
    390 int
    391 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
    392    const u_char *src, u_int len, u_int aadlen, u_int authlen)
    393 {
    394 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    395 		return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
    396 		    len, aadlen, authlen, cc->encrypt);
    397 	}
    398 #ifndef WITH_OPENSSL
    399 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    400 		if (aadlen)
    401 			memcpy(dest, src, aadlen);
    402 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
    403 		    dest + aadlen, len);
    404 		return 0;
    405 	}
    406 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
    407 		memcpy(dest, src, aadlen + len);
    408 		return 0;
    409 	}
    410 	return SSH_ERR_INVALID_ARGUMENT;
    411 #else
    412 	if (authlen) {
    413 		u_char lastiv[1];
    414 
    415 		if (authlen != cipher_authlen(cc->cipher))
    416 			return SSH_ERR_INVALID_ARGUMENT;
    417 		/* increment IV */
    418 		if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    419 		    1, lastiv))
    420 			return SSH_ERR_LIBCRYPTO_ERROR;
    421 		/* set tag on decyption */
    422 		if (!cc->encrypt &&
    423 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
    424 		    authlen, __UNCONST(src + aadlen + len)))
    425 			return SSH_ERR_LIBCRYPTO_ERROR;
    426 	}
    427 	if (aadlen) {
    428 		if (authlen &&
    429 		    EVP_Cipher(&cc->evp, NULL, (const u_char *)src, aadlen) < 0)
    430 			return SSH_ERR_LIBCRYPTO_ERROR;
    431 		memcpy(dest, src, aadlen);
    432 	}
    433 	if (len % cc->cipher->block_size)
    434 		return SSH_ERR_INVALID_ARGUMENT;
    435 	if (EVP_Cipher(&cc->evp, dest + aadlen, (const u_char *)src + aadlen,
    436 	    len) < 0)
    437 		return SSH_ERR_LIBCRYPTO_ERROR;
    438 	if (authlen) {
    439 		/* compute tag (on encrypt) or verify tag (on decrypt) */
    440 		if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
    441 			return cc->encrypt ?
    442 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
    443 		if (cc->encrypt &&
    444 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
    445 		    authlen, dest + aadlen + len))
    446 			return SSH_ERR_LIBCRYPTO_ERROR;
    447 	}
    448 	return 0;
    449 #endif
    450 }
    451 
    452 /* Extract the packet length, including any decryption necessary beforehand */
    453 int
    454 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
    455     const u_char *cp, u_int len)
    456 {
    457 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    458 		return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
    459 		    cp, len);
    460 	if (len < 4)
    461 		return SSH_ERR_MESSAGE_INCOMPLETE;
    462 	*plenp = get_u32(cp);
    463 	return 0;
    464 }
    465 
    466 int
    467 cipher_cleanup(struct sshcipher_ctx *cc)
    468 {
    469 	if (cc == NULL || cc->cipher == NULL)
    470 		return 0;
    471 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    472 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
    473 	else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
    474 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
    475 #ifdef WITH_OPENSSL
    476 	else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
    477 		return SSH_ERR_LIBCRYPTO_ERROR;
    478 #endif
    479 	return 0;
    480 }
    481 
    482 /*
    483  * Selects the cipher, and keys if by computing the MD5 checksum of the
    484  * passphrase and using the resulting 16 bytes as the key.
    485  */
    486 int
    487 cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
    488     const char *passphrase, int do_encrypt)
    489 {
    490 	u_char digest[16];
    491 	int r = SSH_ERR_INTERNAL_ERROR;
    492 
    493 	if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
    494 	    passphrase, strlen(passphrase),
    495 	    digest, sizeof(digest))) != 0)
    496 		goto out;
    497 
    498 	r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
    499  out:
    500 	explicit_bzero(digest, sizeof(digest));
    501 	return r;
    502 }
    503 
    504 /*
    505  * Exports an IV from the sshcipher_ctx required to export the key
    506  * state back from the unprivileged child to the privileged parent
    507  * process.
    508  */
    509 int
    510 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
    511 {
    512 	const struct sshcipher *c = cc->cipher;
    513 	int ivlen = 0;
    514 
    515 	if (c->number == SSH_CIPHER_3DES)
    516 		ivlen = 24;
    517 	else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    518 		ivlen = 0;
    519 #ifdef WITH_OPENSSL
    520 	else
    521 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    522 #endif /* WITH_OPENSSL */
    523 	return (ivlen);
    524 }
    525 
    526 int
    527 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
    528 {
    529 	const struct sshcipher *c = cc->cipher;
    530 #ifdef WITH_OPENSSL
    531  	int evplen;
    532 #endif
    533 
    534 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    535 		if (len != 0)
    536 			return SSH_ERR_INVALID_ARGUMENT;
    537 		return 0;
    538 	}
    539 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    540 		return 0;
    541 
    542 	switch (c->number) {
    543 #ifdef WITH_OPENSSL
    544 	case SSH_CIPHER_NONE:
    545 	case SSH_CIPHER_SSH2:
    546 	case SSH_CIPHER_DES:
    547 	case SSH_CIPHER_BLOWFISH:
    548 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    549 		if (evplen == 0)
    550 			return 0;
    551 		else if (evplen < 0)
    552 			return SSH_ERR_LIBCRYPTO_ERROR;
    553 		if ((u_int)evplen != len)
    554 			return SSH_ERR_INVALID_ARGUMENT;
    555 		if (cipher_authlen(c)) {
    556 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    557 			   len, iv))
    558 			       return SSH_ERR_LIBCRYPTO_ERROR;
    559 		} else
    560 			memcpy(iv, cc->evp.iv, len);
    561 		break;
    562 #endif
    563 #ifdef WITH_SSH1
    564 	case SSH_CIPHER_3DES:
    565 		return ssh1_3des_iv(&cc->evp, 0, iv, 24);
    566 #endif
    567 	default:
    568 		return SSH_ERR_INVALID_ARGUMENT;
    569 	}
    570 	return 0;
    571 }
    572 
    573 int
    574 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
    575 {
    576 	const struct sshcipher *c = cc->cipher;
    577 #ifdef WITH_OPENSSL
    578  	int evplen = 0;
    579 #endif
    580 
    581 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    582 		return 0;
    583 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    584 		return 0;
    585 
    586 	switch (c->number) {
    587 #ifdef WITH_OPENSSL
    588 	case SSH_CIPHER_NONE:
    589 	case SSH_CIPHER_SSH2:
    590 	case SSH_CIPHER_DES:
    591 	case SSH_CIPHER_BLOWFISH:
    592 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    593 		if (evplen <= 0)
    594 			return SSH_ERR_LIBCRYPTO_ERROR;
    595 		if (cipher_authlen(c)) {
    596 			/* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
    597 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
    598 			    EVP_CTRL_GCM_SET_IV_FIXED, -1, __UNCONST(iv)))
    599 				return SSH_ERR_LIBCRYPTO_ERROR;
    600 		} else
    601 			memcpy(cc->evp.iv, iv, evplen);
    602 		break;
    603 #endif
    604 #ifdef WITH_SSH1
    605 	case SSH_CIPHER_3DES:
    606 		return ssh1_3des_iv(&cc->evp, 1, __UNCONST(iv), 24);
    607 #endif
    608 	default:
    609 		return SSH_ERR_INVALID_ARGUMENT;
    610 	}
    611 	return 0;
    612 }
    613 
    614 #ifdef WITH_OPENSSL
    615 #define EVP_X_STATE(evp)	(evp).cipher_data
    616 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
    617 #endif
    618 
    619 int
    620 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
    621 {
    622 #ifdef WITH_OPENSSL
    623 	const struct sshcipher *c = cc->cipher;
    624 	int plen = 0;
    625 
    626 	if (c->evptype == EVP_rc4) {
    627 		plen = EVP_X_STATE_LEN(cc->evp);
    628 		if (dat == NULL)
    629 			return (plen);
    630 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
    631 	}
    632 	return (plen);
    633 #else
    634 	return 0;
    635 #endif
    636 }
    637 
    638 void
    639 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
    640 {
    641 #ifdef WITH_OPENSSL
    642 	const struct sshcipher *c = cc->cipher;
    643 	int plen;
    644 
    645 	if (c->evptype == EVP_rc4) {
    646 		plen = EVP_X_STATE_LEN(cc->evp);
    647 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
    648 	}
    649 #endif
    650 }
    651