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