Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: cipher.c,v 1.28 2026/09/21 21:30:59 christos Exp $	*/
      2 /* $OpenBSD: cipher.c,v 1.129 2026/06/19 05:26:04 djm Exp $ */
      3 
      4 /*
      5  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      6  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      7  *                    All rights reserved
      8  *
      9  * As far as I am concerned, the code I have written for this software
     10  * can be used freely for any purpose.  Any derived versions of this
     11  * software must be clearly marked as such, and if the derived work is
     12  * incompatible with the protocol description in the RFC file, it must be
     13  * called by a name other than "ssh" or "Secure Shell".
     14  *
     15  *
     16  * Copyright (c) 1999 Niels Provos.  All rights reserved.
     17  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notice, this list of conditions and the following disclaimer.
     24  * 2. Redistributions in binary form must reproduce the above copyright
     25  *    notice, this list of conditions and the following disclaimer in the
     26  *    documentation and/or other materials provided with the distribution.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include "includes.h"
     41 __RCSID("$NetBSD: cipher.c,v 1.28 2026/09/21 21:30:59 christos Exp $");
     42 #include <sys/types.h>
     43 
     44 #include <string.h>
     45 #include <stdarg.h>
     46 #include <stdio.h>
     47 
     48 #include "cipher.h"
     49 #include "misc.h"
     50 #include "sshbuf.h"
     51 #include "ssherr.h"
     52 
     53 #ifndef WITH_OPENSSL
     54 #define EVP_CIPHER_CTX void
     55 #endif
     56 
     57 // XXX: from libressl
     58 #ifdef WITH_OPENSSL
     59 #define HAVE_EVP_CIPHER_CTX_IV
     60 
     61 static int
     62 EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
     63 {
     64 	if (ctx == NULL)
     65 		return 0;
     66 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
     67 		return 0;
     68 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
     69 		return 0;
     70 	if (len > EVP_MAX_IV_LENGTH)
     71 		return 0; /* sanity check; shouldn't happen */
     72 	/*
     73 	 * Skip the memcpy entirely when the requested IV length is zero,
     74 	 * since the iv pointer may be NULL or invalid.
     75 	 */
     76 	if (len != 0) {
     77 		if (iv == NULL)
     78 			return 0;
     79 # ifdef HAVE_EVP_CIPHER_CTX_IV
     80 		memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
     81 # else
     82 		memcpy(iv, ctx->iv, len);
     83 # endif /* HAVE_EVP_CIPHER_CTX_IV */
     84 	}
     85 	return 1;
     86 }
     87 #endif
     88 // XXX: end
     89 
     90 struct sshcipher_ctx {
     91 	int	plaintext;
     92 	int	encrypt;
     93 	EVP_CIPHER_CTX *evp;
     94 	struct chachapoly_ctx *cp_ctx;
     95 	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
     96 	const struct sshcipher *cipher;
     97 };
     98 
     99 struct sshcipher {
    100 	const char	*name;
    101 	u_int	block_size;
    102 	u_int	key_len;
    103 	u_int	iv_len;		/* defaults to block_size */
    104 	u_int	auth_len;
    105 	u_int	flags;
    106 #define CFLAG_CBC		(1<<0)
    107 #define CFLAG_CHACHAPOLY	(1<<1)
    108 #define CFLAG_AESCTR		(1<<2)
    109 #define CFLAG_NONE		(1<<3)
    110 #define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
    111 #ifdef WITH_OPENSSL
    112 	const EVP_CIPHER	*(*evptype)(void);
    113 #else
    114 	void	*ignored;
    115 #endif
    116 };
    117 
    118 static const struct sshcipher ciphers[] = {
    119 #ifdef WITH_OPENSSL
    120 	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
    121 	{ "aes128-cbc",		16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
    122 	{ "aes192-cbc",		16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
    123 	{ "aes256-cbc",		16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
    124 	{ "aes128-ctr",		16, 16, 0, 0, 0, EVP_aes_128_ctr },
    125 	{ "aes192-ctr",		16, 24, 0, 0, 0, EVP_aes_192_ctr },
    126 	{ "aes256-ctr",		16, 32, 0, 0, 0, EVP_aes_256_ctr },
    127 	{ "aes128-gcm (at) openssh.com",
    128 				16, 16, 12, 16, 0, EVP_aes_128_gcm },
    129 	{ "aes256-gcm (at) openssh.com",
    130 				16, 32, 12, 16, 0, EVP_aes_256_gcm },
    131 #else
    132 	{ "aes128-ctr",		16, 16, 0, 0, CFLAG_AESCTR, NULL },
    133 	{ "aes192-ctr",		16, 24, 0, 0, CFLAG_AESCTR, NULL },
    134 	{ "aes256-ctr",		16, 32, 0, 0, CFLAG_AESCTR, NULL },
    135 #endif
    136 	{ "chacha20-poly1305 (at) openssh.com",
    137 				8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
    138 	{ "none",		8, 0, 0, 0, CFLAG_NONE, NULL },
    139 
    140 	{ NULL,			0, 0, 0, 0, 0, NULL }
    141 };
    142 
    143 /*--*/
    144 
    145 /* Returns a comma-separated list of supported ciphers. */
    146 char *
    147 cipher_alg_list(char sep, int auth_only)
    148 {
    149 	char *ret = NULL;
    150 	const struct sshcipher *c;
    151 	char sep_str[2] = {sep, '\0'};
    152 
    153 	for (c = ciphers; c->name != NULL; c++) {
    154 		if ((c->flags & CFLAG_INTERNAL) != 0)
    155 			continue;
    156 		if (auth_only && c->auth_len == 0)
    157 			continue;
    158 		xextendf(&ret, sep_str, "%s", c->name);
    159 	}
    160 	return ret;
    161 }
    162 
    163 const char *
    164 compression_alg_list(int compression)
    165 {
    166 #ifdef WITH_ZLIB
    167 	return compression ? "zlib (at) openssh.com,none" :
    168 	    "none,zlib (at) openssh.com";
    169 #else
    170 	return "none";
    171 #endif
    172 }
    173 
    174 u_int
    175 cipher_blocksize(const struct sshcipher *c)
    176 {
    177 	return (c->block_size);
    178 }
    179 
    180 u_int
    181 cipher_keylen(const struct sshcipher *c)
    182 {
    183 	return (c->key_len);
    184 }
    185 
    186 u_int
    187 cipher_seclen(const struct sshcipher *c)
    188 {
    189 	if (strcmp("3des-cbc", c->name) == 0)
    190 		return 14;
    191 	return cipher_keylen(c);
    192 }
    193 
    194 u_int
    195 cipher_authlen(const struct sshcipher *c)
    196 {
    197 	return (c->auth_len);
    198 }
    199 
    200 u_int
    201 cipher_ivlen(const struct sshcipher *c)
    202 {
    203 	/*
    204 	 * Default is cipher block size, except for chacha20+poly1305 that
    205 	 * needs no IV. XXX make iv_len == -1 default?
    206 	 */
    207 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
    208 	    c->iv_len : c->block_size;
    209 }
    210 
    211 u_int
    212 cipher_is_cbc(const struct sshcipher *c)
    213 {
    214 	return (c->flags & CFLAG_CBC) != 0;
    215 }
    216 
    217 u_int
    218 cipher_is_internal(const struct sshcipher *c)
    219 {
    220 	return (c->flags & CFLAG_INTERNAL) != 0;
    221 }
    222 
    223 u_int
    224 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
    225 {
    226 	return cc->plaintext;
    227 }
    228 
    229 const struct sshcipher *
    230 cipher_by_name(const char *name)
    231 {
    232 	const struct sshcipher *c;
    233 	for (c = ciphers; c->name != NULL; c++)
    234 		if (strcmp(c->name, name) == 0)
    235 			return c;
    236 	return NULL;
    237 }
    238 
    239 #define	CIPHER_SEP	","
    240 int
    241 ciphers_valid(const char *names)
    242 {
    243 	const struct sshcipher *c;
    244 	char *cipher_list, *cp;
    245 	char *p;
    246 	int found = 0;
    247 
    248 	if (names == NULL || strcmp(names, "") == 0)
    249 		return 0;
    250 	if ((cipher_list = cp = strdup(names)) == NULL)
    251 		return 0;
    252 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
    253 	    (p = strsep(&cp, CIPHER_SEP))) {
    254 		c = cipher_by_name(p);
    255 		if (c == NULL || (c->flags & (CFLAG_INTERNAL|CFLAG_NONE)) != 0) {
    256 			free(cipher_list);
    257 			return 0;
    258 		} else
    259 			found = 1;
    260 	}
    261 	free(cipher_list);
    262 	return found;
    263 }
    264 
    265 const char *
    266 cipher_warning_message(const struct sshcipher_ctx *cc)
    267 {
    268 	if (cc == NULL || cc->cipher == NULL)
    269 		return NULL;
    270 	/* XXX repurpose for CBC warning */
    271 	return NULL;
    272 }
    273 
    274 int
    275 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
    276     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
    277     int do_encrypt)
    278 {
    279 	struct sshcipher_ctx *cc = NULL;
    280 	int ret = SSH_ERR_INTERNAL_ERROR;
    281 #ifdef WITH_OPENSSL
    282 	const EVP_CIPHER *type;
    283 	int klen;
    284 #endif
    285 
    286 	*ccp = NULL;
    287 	if ((cc = calloc(1, sizeof(*cc))) == NULL)
    288 		return SSH_ERR_ALLOC_FAIL;
    289 
    290 	cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
    291 	cc->encrypt = do_encrypt;
    292 
    293 	if (keylen < cipher->key_len ||
    294 	    (iv != NULL && ivlen < cipher_ivlen(cipher))) {
    295 		ret = SSH_ERR_INVALID_ARGUMENT;
    296 		goto out;
    297 	}
    298 
    299 	cc->cipher = cipher;
    300 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    301 		cc->cp_ctx = chachapoly_new(key, keylen);
    302 		ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
    303 		goto out;
    304 	}
    305 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
    306 		ret = 0;
    307 		goto out;
    308 	}
    309 #ifndef WITH_OPENSSL
    310 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    311 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
    312 		aesctr_ivsetup(&cc->ac_ctx, iv);
    313 		ret = 0;
    314 		goto out;
    315 	}
    316 	ret = SSH_ERR_INVALID_ARGUMENT;
    317 	goto out;
    318 #else /* WITH_OPENSSL */
    319 	type = (*cipher->evptype)();
    320 	if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
    321 		ret = SSH_ERR_ALLOC_FAIL;
    322 		goto out;
    323 	}
    324 	if (EVP_CipherInit(cc->evp, type, NULL, (const u_char *)iv,
    325 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
    326 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    327 		goto out;
    328 	}
    329 	if (cipher_authlen(cipher) &&
    330 	    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
    331 	    -1, __UNCONST(iv)) <= 0) {
    332 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    333 		goto out;
    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 out;
    340 		}
    341 	}
    342 	/* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
    343 	   use EVP_CipherInit_ex for augmenting */
    344 	if (EVP_CipherInit_ex(cc->evp, NULL, NULL, __UNCONST(key), NULL, -1) == 0) {
    345 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    346 		goto out;
    347 	}
    348 	ret = 0;
    349 #endif /* WITH_OPENSSL */
    350  out:
    351 	if (ret == 0) {
    352 		/* success */
    353 		*ccp = cc;
    354 	} else {
    355 		if (cc != NULL) {
    356 #ifdef WITH_OPENSSL
    357 			EVP_CIPHER_CTX_free(cc->evp);
    358 #endif /* WITH_OPENSSL */
    359 			freezero(cc, sizeof(*cc));
    360 		}
    361 	}
    362 	return ret;
    363 }
    364 
    365 /*
    366  * cipher_crypt() operates as following:
    367  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
    368  * These bytes are treated as additional authenticated data for
    369  * authenticated encryption modes.
    370  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
    371  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
    372  * This tag is written on encryption and verified on decryption.
    373  * Both 'aadlen' and 'authlen' can be set to 0.
    374  */
    375 int
    376 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
    377    const u_char *src, u_int len, u_int aadlen, u_int authlen)
    378 {
    379 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    380 		return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
    381 		    len, aadlen, authlen, cc->encrypt);
    382 	}
    383 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
    384 		memcpy(dest, src, aadlen + len);
    385 		return 0;
    386 	}
    387 #ifndef WITH_OPENSSL
    388 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    389 		if (aadlen)
    390 			memcpy(dest, src, aadlen);
    391 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
    392 		    dest + aadlen, len);
    393 		return 0;
    394 	}
    395 	return SSH_ERR_INVALID_ARGUMENT;
    396 #else
    397 	if (authlen) {
    398 		u_char lastiv[1];
    399 
    400 		if (authlen != cipher_authlen(cc->cipher))
    401 			return SSH_ERR_INVALID_ARGUMENT;
    402 		/* increment IV */
    403 		if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
    404 		    1, lastiv) <= 0)
    405 			return SSH_ERR_LIBCRYPTO_ERROR;
    406 		/* set tag on decryption */
    407 		if (!cc->encrypt &&
    408 		    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
    409 		    authlen, __UNCONST(src + aadlen + len)) <= 0)
    410 			return SSH_ERR_LIBCRYPTO_ERROR;
    411 	}
    412 	if (aadlen) {
    413 		if (authlen &&
    414 		    EVP_Cipher(cc->evp, NULL, (const u_char *)src, aadlen) < 0)
    415 			return SSH_ERR_LIBCRYPTO_ERROR;
    416 		memcpy(dest, src, aadlen);
    417 	}
    418 	if (len % cc->cipher->block_size)
    419 		return SSH_ERR_INVALID_ARGUMENT;
    420 	if (EVP_Cipher(cc->evp, dest + aadlen, (const u_char *)src + aadlen,
    421 	    len) < 0)
    422 		return SSH_ERR_LIBCRYPTO_ERROR;
    423 	if (authlen) {
    424 		/* compute tag (on encrypt) or verify tag (on decrypt) */
    425 		if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
    426 			return cc->encrypt ?
    427 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
    428 		if (cc->encrypt &&
    429 		    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
    430 		    authlen, dest + aadlen + len) <= 0)
    431 			return SSH_ERR_LIBCRYPTO_ERROR;
    432 	}
    433 	return 0;
    434 #endif
    435 }
    436 
    437 /* Extract the packet length, including any decryption necessary beforehand */
    438 int
    439 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
    440     const u_char *cp, u_int len)
    441 {
    442 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    443 		return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
    444 		    cp, len);
    445 	if (len < 4)
    446 		return SSH_ERR_MESSAGE_INCOMPLETE;
    447 	*plenp = PEEK_U32(cp);
    448 	return 0;
    449 }
    450 
    451 void
    452 cipher_free(struct sshcipher_ctx *cc)
    453 {
    454 	if (cc == NULL)
    455 		return;
    456 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    457 		chachapoly_free(cc->cp_ctx);
    458 		cc->cp_ctx = NULL;
    459 	} else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
    460 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
    461 #ifdef WITH_OPENSSL
    462 	EVP_CIPHER_CTX_free(cc->evp);
    463 	cc->evp = NULL;
    464 #endif
    465 	freezero(cc, sizeof(*cc));
    466 }
    467 
    468 int
    469 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
    470 {
    471 #ifdef WITH_OPENSSL
    472 	const struct sshcipher *c = cc->cipher;
    473 	int evplen;
    474 #endif
    475 
    476 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    477 		if (len != 0)
    478 			return SSH_ERR_INVALID_ARGUMENT;
    479 		return 0;
    480 	}
    481 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    482 		if (len != sizeof(cc->ac_ctx.ctr))
    483 			return SSH_ERR_INVALID_ARGUMENT;
    484 		memcpy(iv, cc->ac_ctx.ctr, len);
    485 		return 0;
    486 	}
    487 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    488 		return 0;
    489 
    490 #ifdef WITH_OPENSSL
    491 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
    492 	if (evplen == 0)
    493 		return 0;
    494 	else if (evplen < 0)
    495 		return SSH_ERR_LIBCRYPTO_ERROR;
    496 	if ((size_t)evplen != len)
    497 		return SSH_ERR_INVALID_ARGUMENT;
    498 	if (cipher_authlen(c)) {
    499 		if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len,
    500 		    iv) <= 0)
    501 			return SSH_ERR_LIBCRYPTO_ERROR;
    502 	} else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0)
    503 		return SSH_ERR_LIBCRYPTO_ERROR;
    504 #endif
    505 	return 0;
    506 }
    507