xref: /dragonfly/crypto/openssh/cipher.c (revision ee116499)
150a69bb5SSascha Wildner /* $OpenBSD: cipher.c,v 1.119 2021/04/03 06:18:40 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos  * Author: Tatu Ylonen <ylo@cs.hut.fi>
418de8d7fSPeter Avalos  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
518de8d7fSPeter Avalos  *                    All rights reserved
618de8d7fSPeter Avalos  *
718de8d7fSPeter Avalos  * As far as I am concerned, the code I have written for this software
818de8d7fSPeter Avalos  * can be used freely for any purpose.  Any derived versions of this
918de8d7fSPeter Avalos  * software must be clearly marked as such, and if the derived work is
1018de8d7fSPeter Avalos  * incompatible with the protocol description in the RFC file, it must be
1118de8d7fSPeter Avalos  * called by a name other than "ssh" or "Secure Shell".
1218de8d7fSPeter Avalos  *
1318de8d7fSPeter Avalos  *
1418de8d7fSPeter Avalos  * Copyright (c) 1999 Niels Provos.  All rights reserved.
1518de8d7fSPeter Avalos  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
1618de8d7fSPeter Avalos  *
1718de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
1818de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
1918de8d7fSPeter Avalos  * are met:
2018de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
2118de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
2218de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
2318de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
2418de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
2518de8d7fSPeter Avalos  *
2618de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2718de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2818de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2918de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3018de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3118de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3218de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3318de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3418de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3518de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3618de8d7fSPeter Avalos  */
3718de8d7fSPeter Avalos 
3818de8d7fSPeter Avalos #include "includes.h"
3918de8d7fSPeter Avalos 
4018de8d7fSPeter Avalos #include <sys/types.h>
4118de8d7fSPeter Avalos 
4218de8d7fSPeter Avalos #include <string.h>
4318de8d7fSPeter Avalos #include <stdarg.h>
4436e94dc5SPeter Avalos #include <stdio.h>
4518de8d7fSPeter Avalos 
4618de8d7fSPeter Avalos #include "cipher.h"
4736e94dc5SPeter Avalos #include "misc.h"
4836e94dc5SPeter Avalos #include "sshbuf.h"
4936e94dc5SPeter Avalos #include "ssherr.h"
5036e94dc5SPeter Avalos #include "digest.h"
5118de8d7fSPeter Avalos 
5218de8d7fSPeter Avalos #include "openbsd-compat/openssl-compat.h"
5318de8d7fSPeter Avalos 
540cbfa66cSDaniel Fojt #ifndef WITH_OPENSSL
550cbfa66cSDaniel Fojt #define EVP_CIPHER_CTX void
560cbfa66cSDaniel Fojt #endif
57ce74bacaSMatthew Dillon 
58ce74bacaSMatthew Dillon struct sshcipher_ctx {
59ce74bacaSMatthew Dillon 	int	plaintext;
60ce74bacaSMatthew Dillon 	int	encrypt;
61ce74bacaSMatthew Dillon 	EVP_CIPHER_CTX *evp;
620cbfa66cSDaniel Fojt 	struct chachapoly_ctx *cp_ctx;
63ce74bacaSMatthew Dillon 	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
64ce74bacaSMatthew Dillon 	const struct sshcipher *cipher;
65ce74bacaSMatthew Dillon };
6618de8d7fSPeter Avalos 
6736e94dc5SPeter Avalos struct sshcipher {
6818de8d7fSPeter Avalos 	char	*name;
6918de8d7fSPeter Avalos 	u_int	block_size;
7018de8d7fSPeter Avalos 	u_int	key_len;
7136e94dc5SPeter Avalos 	u_int	iv_len;		/* defaults to block_size */
7236e94dc5SPeter Avalos 	u_int	auth_len;
7336e94dc5SPeter Avalos 	u_int	flags;
7436e94dc5SPeter Avalos #define CFLAG_CBC		(1<<0)
7536e94dc5SPeter Avalos #define CFLAG_CHACHAPOLY	(1<<1)
7636e94dc5SPeter Avalos #define CFLAG_AESCTR		(1<<2)
7736e94dc5SPeter Avalos #define CFLAG_NONE		(1<<3)
78ce74bacaSMatthew Dillon #define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
7936e94dc5SPeter Avalos #ifdef WITH_OPENSSL
8018de8d7fSPeter Avalos 	const EVP_CIPHER	*(*evptype)(void);
8136e94dc5SPeter Avalos #else
8236e94dc5SPeter Avalos 	void	*ignored;
8318de8d7fSPeter Avalos #endif
8436e94dc5SPeter Avalos };
8536e94dc5SPeter Avalos 
8636e94dc5SPeter Avalos static const struct sshcipher ciphers[] = {
8736e94dc5SPeter Avalos #ifdef WITH_OPENSSL
88664f4763Szrj #ifndef OPENSSL_NO_DES
89ce74bacaSMatthew Dillon 	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
90664f4763Szrj #endif
91ce74bacaSMatthew Dillon 	{ "aes128-cbc",		16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
92ce74bacaSMatthew Dillon 	{ "aes192-cbc",		16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
93ce74bacaSMatthew Dillon 	{ "aes256-cbc",		16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
94ce74bacaSMatthew Dillon 	{ "aes128-ctr",		16, 16, 0, 0, 0, EVP_aes_128_ctr },
95ce74bacaSMatthew Dillon 	{ "aes192-ctr",		16, 24, 0, 0, 0, EVP_aes_192_ctr },
96ce74bacaSMatthew Dillon 	{ "aes256-ctr",		16, 32, 0, 0, 0, EVP_aes_256_ctr },
9736e94dc5SPeter Avalos 	{ "aes128-gcm@openssh.com",
98ce74bacaSMatthew Dillon 				16, 16, 12, 16, 0, EVP_aes_128_gcm },
9936e94dc5SPeter Avalos 	{ "aes256-gcm@openssh.com",
100ce74bacaSMatthew Dillon 				16, 32, 12, 16, 0, EVP_aes_256_gcm },
101ce74bacaSMatthew Dillon #else
102ce74bacaSMatthew Dillon 	{ "aes128-ctr",		16, 16, 0, 0, CFLAG_AESCTR, NULL },
103ce74bacaSMatthew Dillon 	{ "aes192-ctr",		16, 24, 0, 0, CFLAG_AESCTR, NULL },
104ce74bacaSMatthew Dillon 	{ "aes256-ctr",		16, 32, 0, 0, CFLAG_AESCTR, NULL },
105ce74bacaSMatthew Dillon #endif
10636e94dc5SPeter Avalos 	{ "chacha20-poly1305@openssh.com",
107ce74bacaSMatthew Dillon 				8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
108ce74bacaSMatthew Dillon 	{ "none",		8, 0, 0, 0, CFLAG_NONE, NULL },
10936e94dc5SPeter Avalos 
110ce74bacaSMatthew Dillon 	{ NULL,			0, 0, 0, 0, 0, NULL }
11118de8d7fSPeter Avalos };
11218de8d7fSPeter Avalos 
11318de8d7fSPeter Avalos /*--*/
11418de8d7fSPeter Avalos 
11536e94dc5SPeter Avalos /* Returns a comma-separated list of supported ciphers. */
11636e94dc5SPeter Avalos char *
cipher_alg_list(char sep,int auth_only)11736e94dc5SPeter Avalos cipher_alg_list(char sep, int auth_only)
11836e94dc5SPeter Avalos {
11936e94dc5SPeter Avalos 	char *tmp, *ret = NULL;
12036e94dc5SPeter Avalos 	size_t nlen, rlen = 0;
12136e94dc5SPeter Avalos 	const struct sshcipher *c;
12236e94dc5SPeter Avalos 
12336e94dc5SPeter Avalos 	for (c = ciphers; c->name != NULL; c++) {
124ce74bacaSMatthew Dillon 		if ((c->flags & CFLAG_INTERNAL) != 0)
12536e94dc5SPeter Avalos 			continue;
12636e94dc5SPeter Avalos 		if (auth_only && c->auth_len == 0)
12736e94dc5SPeter Avalos 			continue;
12836e94dc5SPeter Avalos 		if (ret != NULL)
12936e94dc5SPeter Avalos 			ret[rlen++] = sep;
13036e94dc5SPeter Avalos 		nlen = strlen(c->name);
13136e94dc5SPeter Avalos 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
13236e94dc5SPeter Avalos 			free(ret);
13336e94dc5SPeter Avalos 			return NULL;
13436e94dc5SPeter Avalos 		}
13536e94dc5SPeter Avalos 		ret = tmp;
13636e94dc5SPeter Avalos 		memcpy(ret + rlen, c->name, nlen + 1);
13736e94dc5SPeter Avalos 		rlen += nlen;
13836e94dc5SPeter Avalos 	}
13936e94dc5SPeter Avalos 	return ret;
14036e94dc5SPeter Avalos }
14136e94dc5SPeter Avalos 
1420cbfa66cSDaniel Fojt const char *
compression_alg_list(int compression)1430cbfa66cSDaniel Fojt compression_alg_list(int compression)
1440cbfa66cSDaniel Fojt {
1450cbfa66cSDaniel Fojt #ifdef WITH_ZLIB
1460cbfa66cSDaniel Fojt 	return compression ? "zlib@openssh.com,zlib,none" :
1470cbfa66cSDaniel Fojt 	    "none,zlib@openssh.com,zlib";
1480cbfa66cSDaniel Fojt #else
1490cbfa66cSDaniel Fojt 	return "none";
1500cbfa66cSDaniel Fojt #endif
1510cbfa66cSDaniel Fojt }
1520cbfa66cSDaniel Fojt 
15318de8d7fSPeter Avalos u_int
cipher_blocksize(const struct sshcipher * c)15436e94dc5SPeter Avalos cipher_blocksize(const struct sshcipher *c)
15518de8d7fSPeter Avalos {
15618de8d7fSPeter Avalos 	return (c->block_size);
15718de8d7fSPeter Avalos }
15818de8d7fSPeter Avalos 
15918de8d7fSPeter Avalos u_int
cipher_keylen(const struct sshcipher * c)16036e94dc5SPeter Avalos cipher_keylen(const struct sshcipher *c)
16118de8d7fSPeter Avalos {
16218de8d7fSPeter Avalos 	return (c->key_len);
16318de8d7fSPeter Avalos }
16418de8d7fSPeter Avalos 
16518de8d7fSPeter Avalos u_int
cipher_seclen(const struct sshcipher * c)16636e94dc5SPeter Avalos cipher_seclen(const struct sshcipher *c)
16736e94dc5SPeter Avalos {
16836e94dc5SPeter Avalos 	if (strcmp("3des-cbc", c->name) == 0)
16936e94dc5SPeter Avalos 		return 14;
17036e94dc5SPeter Avalos 	return cipher_keylen(c);
17136e94dc5SPeter Avalos }
17236e94dc5SPeter Avalos 
17336e94dc5SPeter Avalos u_int
cipher_authlen(const struct sshcipher * c)17436e94dc5SPeter Avalos cipher_authlen(const struct sshcipher *c)
17536e94dc5SPeter Avalos {
17636e94dc5SPeter Avalos 	return (c->auth_len);
17736e94dc5SPeter Avalos }
17836e94dc5SPeter Avalos 
17936e94dc5SPeter Avalos u_int
cipher_ivlen(const struct sshcipher * c)18036e94dc5SPeter Avalos cipher_ivlen(const struct sshcipher *c)
18136e94dc5SPeter Avalos {
18236e94dc5SPeter Avalos 	/*
18336e94dc5SPeter Avalos 	 * Default is cipher block size, except for chacha20+poly1305 that
18436e94dc5SPeter Avalos 	 * needs no IV. XXX make iv_len == -1 default?
18536e94dc5SPeter Avalos 	 */
18636e94dc5SPeter Avalos 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
18736e94dc5SPeter Avalos 	    c->iv_len : c->block_size;
18836e94dc5SPeter Avalos }
18936e94dc5SPeter Avalos 
19036e94dc5SPeter Avalos u_int
cipher_is_cbc(const struct sshcipher * c)19136e94dc5SPeter Avalos cipher_is_cbc(const struct sshcipher *c)
192cb5eb4f1SPeter Avalos {
19336e94dc5SPeter Avalos 	return (c->flags & CFLAG_CBC) != 0;
194cb5eb4f1SPeter Avalos }
195cb5eb4f1SPeter Avalos 
196cb5eb4f1SPeter Avalos u_int
cipher_ctx_is_plaintext(struct sshcipher_ctx * cc)197ce74bacaSMatthew Dillon cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
19818de8d7fSPeter Avalos {
199ce74bacaSMatthew Dillon 	return cc->plaintext;
20018de8d7fSPeter Avalos }
20118de8d7fSPeter Avalos 
20236e94dc5SPeter Avalos const struct sshcipher *
cipher_by_name(const char * name)20318de8d7fSPeter Avalos cipher_by_name(const char *name)
20418de8d7fSPeter Avalos {
20536e94dc5SPeter Avalos 	const struct sshcipher *c;
20618de8d7fSPeter Avalos 	for (c = ciphers; c->name != NULL; c++)
20718de8d7fSPeter Avalos 		if (strcmp(c->name, name) == 0)
20818de8d7fSPeter Avalos 			return c;
20918de8d7fSPeter Avalos 	return NULL;
21018de8d7fSPeter Avalos }
21118de8d7fSPeter Avalos 
21218de8d7fSPeter Avalos #define	CIPHER_SEP	","
21318de8d7fSPeter Avalos int
ciphers_valid(const char * names)21418de8d7fSPeter Avalos ciphers_valid(const char *names)
21518de8d7fSPeter Avalos {
21636e94dc5SPeter Avalos 	const struct sshcipher *c;
21718de8d7fSPeter Avalos 	char *cipher_list, *cp;
21818de8d7fSPeter Avalos 	char *p;
21918de8d7fSPeter Avalos 
22018de8d7fSPeter Avalos 	if (names == NULL || strcmp(names, "") == 0)
22118de8d7fSPeter Avalos 		return 0;
22236e94dc5SPeter Avalos 	if ((cipher_list = cp = strdup(names)) == NULL)
22336e94dc5SPeter Avalos 		return 0;
22418de8d7fSPeter Avalos 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
22518de8d7fSPeter Avalos 	    (p = strsep(&cp, CIPHER_SEP))) {
22618de8d7fSPeter Avalos 		c = cipher_by_name(p);
227ce74bacaSMatthew Dillon 		if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
22836e94dc5SPeter Avalos 			free(cipher_list);
22918de8d7fSPeter Avalos 			return 0;
23018de8d7fSPeter Avalos 		}
23118de8d7fSPeter Avalos 	}
23236e94dc5SPeter Avalos 	free(cipher_list);
23318de8d7fSPeter Avalos 	return 1;
23418de8d7fSPeter Avalos }
23518de8d7fSPeter Avalos 
23636e94dc5SPeter Avalos const char *
cipher_warning_message(const struct sshcipher_ctx * cc)23736e94dc5SPeter Avalos cipher_warning_message(const struct sshcipher_ctx *cc)
23836e94dc5SPeter Avalos {
23936e94dc5SPeter Avalos 	if (cc == NULL || cc->cipher == NULL)
24036e94dc5SPeter Avalos 		return NULL;
241ce74bacaSMatthew Dillon 	/* XXX repurpose for CBC warning */
24236e94dc5SPeter Avalos 	return NULL;
24336e94dc5SPeter Avalos }
24436e94dc5SPeter Avalos 
24536e94dc5SPeter Avalos int
cipher_init(struct sshcipher_ctx ** ccp,const struct sshcipher * cipher,const u_char * key,u_int keylen,const u_char * iv,u_int ivlen,int do_encrypt)246ce74bacaSMatthew Dillon cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
24718de8d7fSPeter Avalos     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
24818de8d7fSPeter Avalos     int do_encrypt)
24918de8d7fSPeter Avalos {
250ce74bacaSMatthew Dillon 	struct sshcipher_ctx *cc = NULL;
25136e94dc5SPeter Avalos 	int ret = SSH_ERR_INTERNAL_ERROR;
252ce74bacaSMatthew Dillon #ifdef WITH_OPENSSL
25318de8d7fSPeter Avalos 	const EVP_CIPHER *type;
25418de8d7fSPeter Avalos 	int klen;
25536e94dc5SPeter Avalos #endif
256ce74bacaSMatthew Dillon 
257ce74bacaSMatthew Dillon 	*ccp = NULL;
258ce74bacaSMatthew Dillon 	if ((cc = calloc(sizeof(*cc), 1)) == NULL)
259ce74bacaSMatthew Dillon 		return SSH_ERR_ALLOC_FAIL;
260ce74bacaSMatthew Dillon 
261ce74bacaSMatthew Dillon 	cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
26236e94dc5SPeter Avalos 	cc->encrypt = do_encrypt;
26318de8d7fSPeter Avalos 
26436e94dc5SPeter Avalos 	if (keylen < cipher->key_len ||
265ce74bacaSMatthew Dillon 	    (iv != NULL && ivlen < cipher_ivlen(cipher))) {
266ce74bacaSMatthew Dillon 		ret = SSH_ERR_INVALID_ARGUMENT;
267ce74bacaSMatthew Dillon 		goto out;
268ce74bacaSMatthew Dillon 	}
26936e94dc5SPeter Avalos 
27018de8d7fSPeter Avalos 	cc->cipher = cipher;
27136e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
2720cbfa66cSDaniel Fojt 		cc->cp_ctx = chachapoly_new(key, keylen);
2730cbfa66cSDaniel Fojt 		ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
274ce74bacaSMatthew Dillon 		goto out;
275ce74bacaSMatthew Dillon 	}
276ce74bacaSMatthew Dillon 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
277ce74bacaSMatthew Dillon 		ret = 0;
278ce74bacaSMatthew Dillon 		goto out;
27918de8d7fSPeter Avalos 	}
28036e94dc5SPeter Avalos #ifndef WITH_OPENSSL
28136e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
28236e94dc5SPeter Avalos 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
28336e94dc5SPeter Avalos 		aesctr_ivsetup(&cc->ac_ctx, iv);
284ce74bacaSMatthew Dillon 		ret = 0;
285ce74bacaSMatthew Dillon 		goto out;
28636e94dc5SPeter Avalos 	}
287ce74bacaSMatthew Dillon 	ret = SSH_ERR_INVALID_ARGUMENT;
288ce74bacaSMatthew Dillon 	goto out;
289ce74bacaSMatthew Dillon #else /* WITH_OPENSSL */
29036e94dc5SPeter Avalos 	type = (*cipher->evptype)();
291ce74bacaSMatthew Dillon 	if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
292ce74bacaSMatthew Dillon 		ret = SSH_ERR_ALLOC_FAIL;
293ce74bacaSMatthew Dillon 		goto out;
294ce74bacaSMatthew Dillon 	}
295ce74bacaSMatthew Dillon 	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
29636e94dc5SPeter Avalos 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
29736e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
298ce74bacaSMatthew Dillon 		goto out;
29936e94dc5SPeter Avalos 	}
30036e94dc5SPeter Avalos 	if (cipher_authlen(cipher) &&
301ce74bacaSMatthew Dillon 	    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
30236e94dc5SPeter Avalos 	    -1, (u_char *)iv)) {
30336e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
304ce74bacaSMatthew Dillon 		goto out;
30536e94dc5SPeter Avalos 	}
306ce74bacaSMatthew Dillon 	klen = EVP_CIPHER_CTX_key_length(cc->evp);
30718de8d7fSPeter Avalos 	if (klen > 0 && keylen != (u_int)klen) {
308ce74bacaSMatthew Dillon 		if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
30936e94dc5SPeter Avalos 			ret = SSH_ERR_LIBCRYPTO_ERROR;
310ce74bacaSMatthew Dillon 			goto out;
31118de8d7fSPeter Avalos 		}
31236e94dc5SPeter Avalos 	}
313ce74bacaSMatthew Dillon 	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
31436e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
315ce74bacaSMatthew Dillon 		goto out;
31636e94dc5SPeter Avalos 	}
317ce74bacaSMatthew Dillon 	ret = 0;
318ce74bacaSMatthew Dillon #endif /* WITH_OPENSSL */
319ce74bacaSMatthew Dillon  out:
320ce74bacaSMatthew Dillon 	if (ret == 0) {
321ce74bacaSMatthew Dillon 		/* success */
322ce74bacaSMatthew Dillon 		*ccp = cc;
323ce74bacaSMatthew Dillon 	} else {
324ce74bacaSMatthew Dillon 		if (cc != NULL) {
325ce74bacaSMatthew Dillon #ifdef WITH_OPENSSL
326ce74bacaSMatthew Dillon 			EVP_CIPHER_CTX_free(cc->evp);
327ce74bacaSMatthew Dillon #endif /* WITH_OPENSSL */
3280cbfa66cSDaniel Fojt 			freezero(cc, sizeof(*cc));
32918de8d7fSPeter Avalos 		}
330ce74bacaSMatthew Dillon 	}
33136e94dc5SPeter Avalos 	return ret;
33236e94dc5SPeter Avalos }
33318de8d7fSPeter Avalos 
33436e94dc5SPeter Avalos /*
33536e94dc5SPeter Avalos  * cipher_crypt() operates as following:
33636e94dc5SPeter Avalos  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
3370cbfa66cSDaniel Fojt  * These bytes are treated as additional authenticated data for
33836e94dc5SPeter Avalos  * authenticated encryption modes.
33936e94dc5SPeter Avalos  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
34036e94dc5SPeter Avalos  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
34136e94dc5SPeter Avalos  * This tag is written on encryption and verified on decryption.
34236e94dc5SPeter Avalos  * Both 'aadlen' and 'authlen' can be set to 0.
34336e94dc5SPeter Avalos  */
34436e94dc5SPeter Avalos int
cipher_crypt(struct sshcipher_ctx * cc,u_int seqnr,u_char * dest,const u_char * src,u_int len,u_int aadlen,u_int authlen)34536e94dc5SPeter Avalos cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
34636e94dc5SPeter Avalos    const u_char *src, u_int len, u_int aadlen, u_int authlen)
34718de8d7fSPeter Avalos {
34836e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
3490cbfa66cSDaniel Fojt 		return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
35036e94dc5SPeter Avalos 		    len, aadlen, authlen, cc->encrypt);
35136e94dc5SPeter Avalos 	}
352ce74bacaSMatthew Dillon 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
353ce74bacaSMatthew Dillon 		memcpy(dest, src, aadlen + len);
354ce74bacaSMatthew Dillon 		return 0;
355ce74bacaSMatthew Dillon 	}
35636e94dc5SPeter Avalos #ifndef WITH_OPENSSL
35736e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
35836e94dc5SPeter Avalos 		if (aadlen)
35936e94dc5SPeter Avalos 			memcpy(dest, src, aadlen);
36036e94dc5SPeter Avalos 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
36136e94dc5SPeter Avalos 		    dest + aadlen, len);
36236e94dc5SPeter Avalos 		return 0;
36336e94dc5SPeter Avalos 	}
36436e94dc5SPeter Avalos 	return SSH_ERR_INVALID_ARGUMENT;
36536e94dc5SPeter Avalos #else
36636e94dc5SPeter Avalos 	if (authlen) {
36736e94dc5SPeter Avalos 		u_char lastiv[1];
36836e94dc5SPeter Avalos 
36936e94dc5SPeter Avalos 		if (authlen != cipher_authlen(cc->cipher))
37036e94dc5SPeter Avalos 			return SSH_ERR_INVALID_ARGUMENT;
37136e94dc5SPeter Avalos 		/* increment IV */
372ce74bacaSMatthew Dillon 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
37336e94dc5SPeter Avalos 		    1, lastiv))
37436e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
37536e94dc5SPeter Avalos 		/* set tag on decyption */
37636e94dc5SPeter Avalos 		if (!cc->encrypt &&
377ce74bacaSMatthew Dillon 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
37836e94dc5SPeter Avalos 		    authlen, (u_char *)src + aadlen + len))
37936e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
38036e94dc5SPeter Avalos 	}
38136e94dc5SPeter Avalos 	if (aadlen) {
38236e94dc5SPeter Avalos 		if (authlen &&
383ce74bacaSMatthew Dillon 		    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
38436e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
38536e94dc5SPeter Avalos 		memcpy(dest, src, aadlen);
38636e94dc5SPeter Avalos 	}
38718de8d7fSPeter Avalos 	if (len % cc->cipher->block_size)
38836e94dc5SPeter Avalos 		return SSH_ERR_INVALID_ARGUMENT;
389ce74bacaSMatthew Dillon 	if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
39036e94dc5SPeter Avalos 	    len) < 0)
39136e94dc5SPeter Avalos 		return SSH_ERR_LIBCRYPTO_ERROR;
39236e94dc5SPeter Avalos 	if (authlen) {
39336e94dc5SPeter Avalos 		/* compute tag (on encrypt) or verify tag (on decrypt) */
394ce74bacaSMatthew Dillon 		if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
39536e94dc5SPeter Avalos 			return cc->encrypt ?
39636e94dc5SPeter Avalos 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
39736e94dc5SPeter Avalos 		if (cc->encrypt &&
398ce74bacaSMatthew Dillon 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
39936e94dc5SPeter Avalos 		    authlen, dest + aadlen + len))
40036e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
40136e94dc5SPeter Avalos 	}
40236e94dc5SPeter Avalos 	return 0;
40336e94dc5SPeter Avalos #endif
40418de8d7fSPeter Avalos }
40518de8d7fSPeter Avalos 
40636e94dc5SPeter Avalos /* Extract the packet length, including any decryption necessary beforehand */
40736e94dc5SPeter Avalos int
cipher_get_length(struct sshcipher_ctx * cc,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)40836e94dc5SPeter Avalos cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
40936e94dc5SPeter Avalos     const u_char *cp, u_int len)
41018de8d7fSPeter Avalos {
41136e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
4120cbfa66cSDaniel Fojt 		return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
41336e94dc5SPeter Avalos 		    cp, len);
41436e94dc5SPeter Avalos 	if (len < 4)
41536e94dc5SPeter Avalos 		return SSH_ERR_MESSAGE_INCOMPLETE;
416664f4763Szrj 	*plenp = PEEK_U32(cp);
41736e94dc5SPeter Avalos 	return 0;
41836e94dc5SPeter Avalos }
41936e94dc5SPeter Avalos 
420ce74bacaSMatthew Dillon void
cipher_free(struct sshcipher_ctx * cc)421ce74bacaSMatthew Dillon cipher_free(struct sshcipher_ctx *cc)
42236e94dc5SPeter Avalos {
423ce74bacaSMatthew Dillon 	if (cc == NULL)
424ce74bacaSMatthew Dillon 		return;
4250cbfa66cSDaniel Fojt 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
4260cbfa66cSDaniel Fojt 		chachapoly_free(cc->cp_ctx);
4270cbfa66cSDaniel Fojt 		cc->cp_ctx = NULL;
4280cbfa66cSDaniel Fojt 	} else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
42936e94dc5SPeter Avalos 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
43036e94dc5SPeter Avalos #ifdef WITH_OPENSSL
431ce74bacaSMatthew Dillon 	EVP_CIPHER_CTX_free(cc->evp);
432ce74bacaSMatthew Dillon 	cc->evp = NULL;
433ce74bacaSMatthew Dillon #endif
4340cbfa66cSDaniel Fojt 	freezero(cc, sizeof(*cc));
43518de8d7fSPeter Avalos }
43618de8d7fSPeter Avalos 
43718de8d7fSPeter Avalos /*
43836e94dc5SPeter Avalos  * Exports an IV from the sshcipher_ctx required to export the key
43918de8d7fSPeter Avalos  * state back from the unprivileged child to the privileged parent
44018de8d7fSPeter Avalos  * process.
44118de8d7fSPeter Avalos  */
44218de8d7fSPeter Avalos int
cipher_get_keyiv_len(const struct sshcipher_ctx * cc)44336e94dc5SPeter Avalos cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
44418de8d7fSPeter Avalos {
44536e94dc5SPeter Avalos 	const struct sshcipher *c = cc->cipher;
44618de8d7fSPeter Avalos 
447ce74bacaSMatthew Dillon 	if ((c->flags & CFLAG_CHACHAPOLY) != 0)
448ce74bacaSMatthew Dillon 		return 0;
449ce74bacaSMatthew Dillon 	else if ((c->flags & CFLAG_AESCTR) != 0)
450ce74bacaSMatthew Dillon 		return sizeof(cc->ac_ctx.ctr);
45136e94dc5SPeter Avalos #ifdef WITH_OPENSSL
452ce74bacaSMatthew Dillon 	return EVP_CIPHER_CTX_iv_length(cc->evp);
453ce74bacaSMatthew Dillon #else
454ce74bacaSMatthew Dillon 	return 0;
455ce74bacaSMatthew Dillon #endif
45618de8d7fSPeter Avalos }
45718de8d7fSPeter Avalos 
45836e94dc5SPeter Avalos int
cipher_get_keyiv(struct sshcipher_ctx * cc,u_char * iv,size_t len)459664f4763Szrj cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
46018de8d7fSPeter Avalos {
46136e94dc5SPeter Avalos #ifdef WITH_OPENSSL
462664f4763Szrj 	const struct sshcipher *c = cc->cipher;
46318de8d7fSPeter Avalos 	int evplen;
46418de8d7fSPeter Avalos #endif
46518de8d7fSPeter Avalos 
46636e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
46736e94dc5SPeter Avalos 		if (len != 0)
46836e94dc5SPeter Avalos 			return SSH_ERR_INVALID_ARGUMENT;
46936e94dc5SPeter Avalos 		return 0;
47036e94dc5SPeter Avalos 	}
471e9778795SPeter Avalos 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
472e9778795SPeter Avalos 		if (len != sizeof(cc->ac_ctx.ctr))
473e9778795SPeter Avalos 			return SSH_ERR_INVALID_ARGUMENT;
474e9778795SPeter Avalos 		memcpy(iv, cc->ac_ctx.ctr, len);
475e9778795SPeter Avalos 		return 0;
476e9778795SPeter Avalos 	}
47736e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
47836e94dc5SPeter Avalos 		return 0;
47918de8d7fSPeter Avalos 
48036e94dc5SPeter Avalos #ifdef WITH_OPENSSL
481ce74bacaSMatthew Dillon 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
48218de8d7fSPeter Avalos 	if (evplen == 0)
48336e94dc5SPeter Avalos 		return 0;
48436e94dc5SPeter Avalos 	else if (evplen < 0)
48536e94dc5SPeter Avalos 		return SSH_ERR_LIBCRYPTO_ERROR;
486664f4763Szrj 	if ((size_t)evplen != len)
48736e94dc5SPeter Avalos 		return SSH_ERR_INVALID_ARGUMENT;
48836e94dc5SPeter Avalos 	if (cipher_authlen(c)) {
489ce74bacaSMatthew Dillon 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
49036e94dc5SPeter Avalos 		    len, iv))
49136e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
492664f4763Szrj 	} else if (!EVP_CIPHER_CTX_get_iv(cc->evp, iv, len))
493664f4763Szrj 		return SSH_ERR_LIBCRYPTO_ERROR;
49436e94dc5SPeter Avalos #endif
49536e94dc5SPeter Avalos 	return 0;
49618de8d7fSPeter Avalos }
49718de8d7fSPeter Avalos 
49836e94dc5SPeter Avalos int
cipher_set_keyiv(struct sshcipher_ctx * cc,const u_char * iv,size_t len)499664f4763Szrj cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
50036e94dc5SPeter Avalos {
50136e94dc5SPeter Avalos #ifdef WITH_OPENSSL
502664f4763Szrj 	const struct sshcipher *c = cc->cipher;
50336e94dc5SPeter Avalos 	int evplen = 0;
50436e94dc5SPeter Avalos #endif
50536e94dc5SPeter Avalos 
50636e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
50736e94dc5SPeter Avalos 		return 0;
50836e94dc5SPeter Avalos 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
50936e94dc5SPeter Avalos 		return 0;
51036e94dc5SPeter Avalos 
51136e94dc5SPeter Avalos #ifdef WITH_OPENSSL
512ce74bacaSMatthew Dillon 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
51336e94dc5SPeter Avalos 	if (evplen <= 0)
51436e94dc5SPeter Avalos 		return SSH_ERR_LIBCRYPTO_ERROR;
515664f4763Szrj 	if ((size_t)evplen != len)
516664f4763Szrj 		return SSH_ERR_INVALID_ARGUMENT;
51736e94dc5SPeter Avalos 	if (cipher_authlen(c)) {
51836e94dc5SPeter Avalos 		/* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
519ce74bacaSMatthew Dillon 		if (!EVP_CIPHER_CTX_ctrl(cc->evp,
52036e94dc5SPeter Avalos 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
52136e94dc5SPeter Avalos 			return SSH_ERR_LIBCRYPTO_ERROR;
522664f4763Szrj 	} else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
523664f4763Szrj 		return SSH_ERR_LIBCRYPTO_ERROR;
52436e94dc5SPeter Avalos #endif
52536e94dc5SPeter Avalos 	return 0;
52636e94dc5SPeter Avalos }
527