xref: /openssh-portable/cipher.c (revision 0c378ff6)
1 /* $OpenBSD: cipher.c,v 1.106 2017/05/04 01:33:21 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 "includes.h"
39 
40 #include <sys/types.h>
41 
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 
46 #include "cipher.h"
47 #include "misc.h"
48 #include "sshbuf.h"
49 #include "ssherr.h"
50 #include "digest.h"
51 
52 #include "openbsd-compat/openssl-compat.h"
53 
54 
55 struct sshcipher_ctx {
56 	int	plaintext;
57 	int	encrypt;
58 	EVP_CIPHER_CTX *evp;
59 	struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
60 	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
61 	const struct sshcipher *cipher;
62 };
63 
64 struct sshcipher {
65 	char	*name;
66 	u_int	block_size;
67 	u_int	key_len;
68 	u_int	iv_len;		/* defaults to block_size */
69 	u_int	auth_len;
70 	u_int	discard_len;
71 	u_int	flags;
72 #define CFLAG_CBC		(1<<0)
73 #define CFLAG_CHACHAPOLY	(1<<1)
74 #define CFLAG_AESCTR		(1<<2)
75 #define CFLAG_NONE		(1<<3)
76 #define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
77 #ifdef WITH_OPENSSL
78 	const EVP_CIPHER	*(*evptype)(void);
79 #else
80 	void	*ignored;
81 #endif
82 };
83 
84 static const struct sshcipher ciphers[] = {
85 #ifdef WITH_OPENSSL
86 	{ "3des-cbc",		8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
87 # ifndef OPENSSL_NO_BF
88 	{ "blowfish-cbc",	8, 16, 0, 0, 0, 1, EVP_bf_cbc },
89 # endif /* OPENSSL_NO_BF */
90 # ifndef OPENSSL_NO_CAST
91 	{ "cast128-cbc",	8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
92 # endif /* OPENSSL_NO_CAST */
93 # ifndef OPENSSL_NO_RC4
94 	{ "arcfour",		8, 16, 0, 0, 0, 0, EVP_rc4 },
95 	{ "arcfour128",		8, 16, 0, 0, 1536, 0, EVP_rc4 },
96 	{ "arcfour256",		8, 32, 0, 0, 1536, 0, EVP_rc4 },
97 # endif /* OPENSSL_NO_RC4 */
98 	{ "aes128-cbc",		16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
99 	{ "aes192-cbc",		16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
100 	{ "aes256-cbc",		16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
101 	{ "rijndael-cbc@lysator.liu.se",
102 				16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
103 	{ "aes128-ctr",		16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
104 	{ "aes192-ctr",		16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
105 	{ "aes256-ctr",		16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
106 # ifdef OPENSSL_HAVE_EVPGCM
107 	{ "aes128-gcm@openssh.com",
108 				16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
109 	{ "aes256-gcm@openssh.com",
110 				16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
111 # endif /* OPENSSL_HAVE_EVPGCM */
112 #else
113 	{ "aes128-ctr",		16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
114 	{ "aes192-ctr",		16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
115 	{ "aes256-ctr",		16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
116 #endif
117 	{ "chacha20-poly1305@openssh.com",
118 				8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
119 	{ "none",		8, 0, 0, 0, 0, CFLAG_NONE, NULL },
120 
121 	{ NULL,			0, 0, 0, 0, 0, 0, NULL }
122 };
123 
124 /*--*/
125 
126 /* Returns a comma-separated list of supported ciphers. */
127 char *
128 cipher_alg_list(char sep, int auth_only)
129 {
130 	char *tmp, *ret = NULL;
131 	size_t nlen, rlen = 0;
132 	const struct sshcipher *c;
133 
134 	for (c = ciphers; c->name != NULL; c++) {
135 		if ((c->flags & CFLAG_INTERNAL) != 0)
136 			continue;
137 		if (auth_only && c->auth_len == 0)
138 			continue;
139 		if (ret != NULL)
140 			ret[rlen++] = sep;
141 		nlen = strlen(c->name);
142 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
143 			free(ret);
144 			return NULL;
145 		}
146 		ret = tmp;
147 		memcpy(ret + rlen, c->name, nlen + 1);
148 		rlen += nlen;
149 	}
150 	return ret;
151 }
152 
153 u_int
154 cipher_blocksize(const struct sshcipher *c)
155 {
156 	return (c->block_size);
157 }
158 
159 u_int
160 cipher_keylen(const struct sshcipher *c)
161 {
162 	return (c->key_len);
163 }
164 
165 u_int
166 cipher_seclen(const struct sshcipher *c)
167 {
168 	if (strcmp("3des-cbc", c->name) == 0)
169 		return 14;
170 	return cipher_keylen(c);
171 }
172 
173 u_int
174 cipher_authlen(const struct sshcipher *c)
175 {
176 	return (c->auth_len);
177 }
178 
179 u_int
180 cipher_ivlen(const struct sshcipher *c)
181 {
182 	/*
183 	 * Default is cipher block size, except for chacha20+poly1305 that
184 	 * needs no IV. XXX make iv_len == -1 default?
185 	 */
186 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
187 	    c->iv_len : c->block_size;
188 }
189 
190 u_int
191 cipher_is_cbc(const struct sshcipher *c)
192 {
193 	return (c->flags & CFLAG_CBC) != 0;
194 }
195 
196 u_int
197 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
198 {
199 	return cc->plaintext;
200 }
201 
202 const struct sshcipher *
203 cipher_by_name(const char *name)
204 {
205 	const struct sshcipher *c;
206 	for (c = ciphers; c->name != NULL; c++)
207 		if (strcmp(c->name, name) == 0)
208 			return c;
209 	return NULL;
210 }
211 
212 #define	CIPHER_SEP	","
213 int
214 ciphers_valid(const char *names)
215 {
216 	const struct sshcipher *c;
217 	char *cipher_list, *cp;
218 	char *p;
219 
220 	if (names == NULL || strcmp(names, "") == 0)
221 		return 0;
222 	if ((cipher_list = cp = strdup(names)) == NULL)
223 		return 0;
224 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
225 	    (p = strsep(&cp, CIPHER_SEP))) {
226 		c = cipher_by_name(p);
227 		if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
228 			free(cipher_list);
229 			return 0;
230 		}
231 	}
232 	free(cipher_list);
233 	return 1;
234 }
235 
236 const char *
237 cipher_warning_message(const struct sshcipher_ctx *cc)
238 {
239 	if (cc == NULL || cc->cipher == NULL)
240 		return NULL;
241 	/* XXX repurpose for CBC warning */
242 	return NULL;
243 }
244 
245 int
246 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
247     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
248     int do_encrypt)
249 {
250 	struct sshcipher_ctx *cc = NULL;
251 	int ret = SSH_ERR_INTERNAL_ERROR;
252 #ifdef WITH_OPENSSL
253 	const EVP_CIPHER *type;
254 	int klen;
255 	u_char *junk, *discard;
256 #endif
257 
258 	*ccp = NULL;
259 	if ((cc = calloc(sizeof(*cc), 1)) == NULL)
260 		return SSH_ERR_ALLOC_FAIL;
261 
262 	cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
263 	cc->encrypt = do_encrypt;
264 
265 	if (keylen < cipher->key_len ||
266 	    (iv != NULL && ivlen < cipher_ivlen(cipher))) {
267 		ret = SSH_ERR_INVALID_ARGUMENT;
268 		goto out;
269 	}
270 
271 	cc->cipher = cipher;
272 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
273 		ret = chachapoly_init(&cc->cp_ctx, key, keylen);
274 		goto out;
275 	}
276 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
277 		ret = 0;
278 		goto out;
279 	}
280 #ifndef WITH_OPENSSL
281 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
282 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
283 		aesctr_ivsetup(&cc->ac_ctx, iv);
284 		ret = 0;
285 		goto out;
286 	}
287 	ret = SSH_ERR_INVALID_ARGUMENT;
288 	goto out;
289 #else /* WITH_OPENSSL */
290 	type = (*cipher->evptype)();
291 	if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
292 		ret = SSH_ERR_ALLOC_FAIL;
293 		goto out;
294 	}
295 	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
296 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
297 		ret = SSH_ERR_LIBCRYPTO_ERROR;
298 		goto out;
299 	}
300 	if (cipher_authlen(cipher) &&
301 	    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
302 	    -1, (u_char *)iv)) {
303 		ret = SSH_ERR_LIBCRYPTO_ERROR;
304 		goto out;
305 	}
306 	klen = EVP_CIPHER_CTX_key_length(cc->evp);
307 	if (klen > 0 && keylen != (u_int)klen) {
308 		if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
309 			ret = SSH_ERR_LIBCRYPTO_ERROR;
310 			goto out;
311 		}
312 	}
313 	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
314 		ret = SSH_ERR_LIBCRYPTO_ERROR;
315 		goto out;
316 	}
317 
318 	if (cipher->discard_len > 0) {
319 		if ((junk = malloc(cipher->discard_len)) == NULL ||
320 		    (discard = malloc(cipher->discard_len)) == NULL) {
321 			free(junk);
322 			ret = SSH_ERR_ALLOC_FAIL;
323 			goto out;
324 		}
325 		ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
326 		explicit_bzero(discard, cipher->discard_len);
327 		free(junk);
328 		free(discard);
329 		if (ret != 1) {
330 			ret = SSH_ERR_LIBCRYPTO_ERROR;
331 			goto out;
332 		}
333 	}
334 	ret = 0;
335 #endif /* WITH_OPENSSL */
336  out:
337 	if (ret == 0) {
338 		/* success */
339 		*ccp = cc;
340 	} else {
341 		if (cc != NULL) {
342 #ifdef WITH_OPENSSL
343 			if (cc->evp != NULL)
344 				EVP_CIPHER_CTX_free(cc->evp);
345 #endif /* WITH_OPENSSL */
346 			explicit_bzero(cc, sizeof(*cc));
347 			free(cc);
348 		}
349 	}
350 	return ret;
351 }
352 
353 /*
354  * cipher_crypt() operates as following:
355  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
356  * Theses bytes are treated as additional authenticated data for
357  * authenticated encryption modes.
358  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
359  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
360  * This tag is written on encryption and verified on decryption.
361  * Both 'aadlen' and 'authlen' can be set to 0.
362  */
363 int
364 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
365    const u_char *src, u_int len, u_int aadlen, u_int authlen)
366 {
367 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
368 		return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
369 		    len, aadlen, authlen, cc->encrypt);
370 	}
371 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
372 		memcpy(dest, src, aadlen + len);
373 		return 0;
374 	}
375 #ifndef WITH_OPENSSL
376 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
377 		if (aadlen)
378 			memcpy(dest, src, aadlen);
379 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
380 		    dest + aadlen, len);
381 		return 0;
382 	}
383 	return SSH_ERR_INVALID_ARGUMENT;
384 #else
385 	if (authlen) {
386 		u_char lastiv[1];
387 
388 		if (authlen != cipher_authlen(cc->cipher))
389 			return SSH_ERR_INVALID_ARGUMENT;
390 		/* increment IV */
391 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
392 		    1, lastiv))
393 			return SSH_ERR_LIBCRYPTO_ERROR;
394 		/* set tag on decyption */
395 		if (!cc->encrypt &&
396 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
397 		    authlen, (u_char *)src + aadlen + len))
398 			return SSH_ERR_LIBCRYPTO_ERROR;
399 	}
400 	if (aadlen) {
401 		if (authlen &&
402 		    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
403 			return SSH_ERR_LIBCRYPTO_ERROR;
404 		memcpy(dest, src, aadlen);
405 	}
406 	if (len % cc->cipher->block_size)
407 		return SSH_ERR_INVALID_ARGUMENT;
408 	if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
409 	    len) < 0)
410 		return SSH_ERR_LIBCRYPTO_ERROR;
411 	if (authlen) {
412 		/* compute tag (on encrypt) or verify tag (on decrypt) */
413 		if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
414 			return cc->encrypt ?
415 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
416 		if (cc->encrypt &&
417 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
418 		    authlen, dest + aadlen + len))
419 			return SSH_ERR_LIBCRYPTO_ERROR;
420 	}
421 	return 0;
422 #endif
423 }
424 
425 /* Extract the packet length, including any decryption necessary beforehand */
426 int
427 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
428     const u_char *cp, u_int len)
429 {
430 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
431 		return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
432 		    cp, len);
433 	if (len < 4)
434 		return SSH_ERR_MESSAGE_INCOMPLETE;
435 	*plenp = get_u32(cp);
436 	return 0;
437 }
438 
439 void
440 cipher_free(struct sshcipher_ctx *cc)
441 {
442 	if (cc == NULL)
443 		return;
444 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
445 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
446 	else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
447 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
448 #ifdef WITH_OPENSSL
449 	if (cc->evp != NULL) {
450 		EVP_CIPHER_CTX_free(cc->evp);
451 		cc->evp = NULL;
452 	}
453 #endif
454 	explicit_bzero(cc, sizeof(*cc));
455 	free(cc);
456 }
457 
458 /*
459  * Exports an IV from the sshcipher_ctx required to export the key
460  * state back from the unprivileged child to the privileged parent
461  * process.
462  */
463 int
464 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
465 {
466 	const struct sshcipher *c = cc->cipher;
467 
468 	if ((c->flags & CFLAG_CHACHAPOLY) != 0)
469 		return 0;
470 	else if ((c->flags & CFLAG_AESCTR) != 0)
471 		return sizeof(cc->ac_ctx.ctr);
472 #ifdef WITH_OPENSSL
473 	return EVP_CIPHER_CTX_iv_length(cc->evp);
474 #else
475 	return 0;
476 #endif
477 }
478 
479 int
480 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
481 {
482 	const struct sshcipher *c = cc->cipher;
483 #ifdef WITH_OPENSSL
484  	int evplen;
485 #endif
486 
487 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
488 		if (len != 0)
489 			return SSH_ERR_INVALID_ARGUMENT;
490 		return 0;
491 	}
492 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
493 		if (len != sizeof(cc->ac_ctx.ctr))
494 			return SSH_ERR_INVALID_ARGUMENT;
495 		memcpy(iv, cc->ac_ctx.ctr, len);
496 		return 0;
497 	}
498 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
499 		return 0;
500 
501 #ifdef WITH_OPENSSL
502 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
503 	if (evplen == 0)
504 		return 0;
505 	else if (evplen < 0)
506 		return SSH_ERR_LIBCRYPTO_ERROR;
507 	if ((u_int)evplen != len)
508 		return SSH_ERR_INVALID_ARGUMENT;
509 #ifndef OPENSSL_HAVE_EVPCTR
510 	if (c->evptype == evp_aes_128_ctr)
511 		ssh_aes_ctr_iv(cc->evp, 0, iv, len);
512 	else
513 #endif
514 	if (cipher_authlen(c)) {
515 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
516 		   len, iv))
517 		       return SSH_ERR_LIBCRYPTO_ERROR;
518 	} else
519 		memcpy(iv, cc->evp->iv, len);
520 #endif
521 	return 0;
522 }
523 
524 int
525 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
526 {
527 	const struct sshcipher *c = cc->cipher;
528 #ifdef WITH_OPENSSL
529  	int evplen = 0;
530 #endif
531 
532 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
533 		return 0;
534 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
535 		return 0;
536 
537 #ifdef WITH_OPENSSL
538 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
539 	if (evplen <= 0)
540 		return SSH_ERR_LIBCRYPTO_ERROR;
541 #ifndef OPENSSL_HAVE_EVPCTR
542 	/* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
543 	if (c->evptype == evp_aes_128_ctr)
544 		ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
545 	else
546 #endif
547 	if (cipher_authlen(c)) {
548 		/* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
549 		if (!EVP_CIPHER_CTX_ctrl(cc->evp,
550 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
551 			return SSH_ERR_LIBCRYPTO_ERROR;
552 	} else
553 		memcpy(cc->evp->iv, iv, evplen);
554 #endif
555 	return 0;
556 }
557 
558 #ifdef WITH_OPENSSL
559 #define EVP_X_STATE(evp)	(evp)->cipher_data
560 #define EVP_X_STATE_LEN(evp)	(evp)->cipher->ctx_size
561 #endif
562 
563 int
564 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
565 {
566 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
567 	const struct sshcipher *c = cc->cipher;
568 	int plen = 0;
569 
570 	if (c->evptype == EVP_rc4) {
571 		plen = EVP_X_STATE_LEN(cc->evp);
572 		if (dat == NULL)
573 			return (plen);
574 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
575 	}
576 	return (plen);
577 #else
578 	return 0;
579 #endif
580 }
581 
582 void
583 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
584 {
585 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
586 	const struct sshcipher *c = cc->cipher;
587 	int plen;
588 
589 	if (c->evptype == EVP_rc4) {
590 		plen = EVP_X_STATE_LEN(cc->evp);
591 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
592 	}
593 #endif
594 }
595