Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: cipher.c,v 1.26 2025/12/28 09:39:35 nia Exp $	*/
      2 /* $OpenBSD: cipher.c,v 1.125 2025/09/02 11:08:34 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.26 2025/12/28 09:39:35 nia 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 #include "digest.h"
     53 
     54 #ifndef WITH_OPENSSL
     55 #define EVP_CIPHER_CTX void
     56 #endif
     57 
     58 // XXX: from libressl
     59 #ifdef WITH_OPENSSL
     60 #define HAVE_EVP_CIPHER_CTX_IV
     61 
     62 static int
     63 EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
     64 {
     65 	if (ctx == NULL)
     66 		return 0;
     67 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
     68 		return 0;
     69 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
     70 		return 0;
     71 	if (len > EVP_MAX_IV_LENGTH)
     72 		return 0; /* sanity check; shouldn't happen */
     73 	/*
     74 	 * Skip the memcpy entirely when the requested IV length is zero,
     75 	 * since the iv pointer may be NULL or invalid.
     76 	 */
     77 	if (len != 0) {
     78 		if (iv == NULL)
     79 			return 0;
     80 # ifdef HAVE_EVP_CIPHER_CTX_IV
     81 		memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
     82 # else
     83 		memcpy(iv, ctx->iv, len);
     84 # endif /* HAVE_EVP_CIPHER_CTX_IV */
     85 	}
     86 	return 1;
     87 }
     88 #endif
     89 // XXX: end
     90 
     91 struct sshcipher_ctx {
     92 	int	plaintext;
     93 	int	encrypt;
     94 	EVP_CIPHER_CTX *evp;
     95 	struct chachapoly_ctx *cp_ctx;
     96 	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
     97 	const struct sshcipher *cipher;
     98 };
     99 
    100 struct sshcipher {
    101 	const char	*name;
    102 	u_int	block_size;
    103 	u_int	key_len;
    104 	u_int	iv_len;		/* defaults to block_size */
    105 	u_int	auth_len;
    106 	u_int	flags;
    107 #define CFLAG_CBC		(1<<0)
    108 #define CFLAG_CHACHAPOLY	(1<<1)
    109 #define CFLAG_AESCTR		(1<<2)
    110 #define CFLAG_NONE		(1<<3)
    111 #define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
    112 #ifdef WITH_OPENSSL
    113 	const EVP_CIPHER	*(*evptype)(void);
    114 #else
    115 	void	*ignored;
    116 #endif
    117 };
    118 
    119 static const struct sshcipher ciphers[] = {
    120 #ifdef WITH_OPENSSL
    121 	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
    122 	{ "aes128-cbc",		16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
    123 	{ "aes192-cbc",		16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
    124 	{ "aes256-cbc",		16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
    125 	{ "aes128-ctr",		16, 16, 0, 0, 0, EVP_aes_128_ctr },
    126 	{ "aes192-ctr",		16, 24, 0, 0, 0, EVP_aes_192_ctr },
    127 	{ "aes256-ctr",		16, 32, 0, 0, 0, EVP_aes_256_ctr },
    128 	{ "aes128-gcm (at) openssh.com",
    129 				16, 16, 12, 16, 0, EVP_aes_128_gcm },
    130 	{ "aes256-gcm (at) openssh.com",
    131 				16, 32, 12, 16, 0, EVP_aes_256_gcm },
    132 #else
    133 	{ "aes128-ctr",		16, 16, 0, 0, CFLAG_AESCTR, NULL },
    134 	{ "aes192-ctr",		16, 24, 0, 0, CFLAG_AESCTR, NULL },
    135 	{ "aes256-ctr",		16, 32, 0, 0, CFLAG_AESCTR, NULL },
    136 #endif
    137 	{ "chacha20-poly1305 (at) openssh.com",
    138 				8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
    139 	{ "none",		8, 0, 0, 0, CFLAG_NONE, NULL },
    140 
    141 	{ NULL,			0, 0, 0, 0, 0, NULL }
    142 };
    143 
    144 /*--*/
    145 
    146 /* Returns a comma-separated list of supported ciphers. */
    147 char *
    148 cipher_alg_list(char sep, int auth_only)
    149 {
    150 	char *ret = NULL;
    151 	const struct sshcipher *c;
    152 	char sep_str[2] = {sep, '\0'};
    153 
    154 	for (c = ciphers; c->name != NULL; c++) {
    155 		if ((c->flags & CFLAG_INTERNAL) != 0)
    156 			continue;
    157 		if (auth_only && c->auth_len == 0)
    158 			continue;
    159 		xextendf(&ret, sep_str, "%s", c->name);
    160 	}
    161 	return ret;
    162 }
    163 
    164 const char *
    165 compression_alg_list(int compression)
    166 {
    167 #ifdef WITH_ZLIB
    168 	return compression ? "zlib (at) openssh.com,none" :
    169 	    "none,zlib (at) openssh.com";
    170 #else
    171 	return "none";
    172 #endif
    173 }
    174 
    175 u_int
    176 cipher_blocksize(const struct sshcipher *c)
    177 {
    178 	return (c->block_size);
    179 }
    180 
    181 u_int
    182 cipher_keylen(const struct sshcipher *c)
    183 {
    184 	return (c->key_len);
    185 }
    186 
    187 u_int
    188 cipher_seclen(const struct sshcipher *c)
    189 {
    190 	if (strcmp("3des-cbc", c->name) == 0)
    191 		return 14;
    192 	return cipher_keylen(c);
    193 }
    194 
    195 u_int
    196 cipher_authlen(const struct sshcipher *c)
    197 {
    198 	return (c->auth_len);
    199 }
    200 
    201 u_int
    202 cipher_ivlen(const struct sshcipher *c)
    203 {
    204 	/*
    205 	 * Default is cipher block size, except for chacha20+poly1305 that
    206 	 * needs no IV. XXX make iv_len == -1 default?
    207 	 */
    208 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
    209 	    c->iv_len : c->block_size;
    210 }
    211 
    212 u_int
    213 cipher_is_cbc(const struct sshcipher *c)
    214 {
    215 	return (c->flags & CFLAG_CBC) != 0;
    216 }
    217 
    218 u_int
    219 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
    220 {
    221 	return cc->plaintext;
    222 }
    223 
    224 const struct sshcipher *
    225 cipher_by_name(const char *name)
    226 {
    227 	const struct sshcipher *c;
    228 	for (c = ciphers; c->name != NULL; c++)
    229 		if (strcmp(c->name, name) == 0)
    230 			return c;
    231 	return NULL;
    232 }
    233 
    234 #define	CIPHER_SEP	","
    235 int
    236 ciphers_valid(const char *names)
    237 {
    238 	const struct sshcipher *c;
    239 	char *cipher_list, *cp;
    240 	char *p;
    241 
    242 	if (names == NULL || strcmp(names, "") == 0)
    243 		return 0;
    244 	if ((cipher_list = cp = strdup(names)) == NULL)
    245 		return 0;
    246 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
    247 	    (p = strsep(&cp, CIPHER_SEP))) {
    248 		c = cipher_by_name(p);
    249 		if (c == NULL || (c->flags & (CFLAG_INTERNAL|CFLAG_NONE)) != 0) {
    250 			free(cipher_list);
    251 			return 0;
    252 		}
    253 	}
    254 	free(cipher_list);
    255 	return 1;
    256 }
    257 
    258 const char *
    259 cipher_warning_message(const struct sshcipher_ctx *cc)
    260 {
    261 	if (cc == NULL || cc->cipher == NULL)
    262 		return NULL;
    263 	/* XXX repurpose for CBC warning */
    264 	return NULL;
    265 }
    266 
    267 int
    268 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
    269     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
    270     int do_encrypt)
    271 {
    272 	struct sshcipher_ctx *cc = NULL;
    273 	int ret = SSH_ERR_INTERNAL_ERROR;
    274 #ifdef WITH_OPENSSL
    275 	const EVP_CIPHER *type;
    276 	int klen;
    277 #endif
    278 
    279 	*ccp = NULL;
    280 	if ((cc = calloc(1, sizeof(*cc))) == NULL)
    281 		return SSH_ERR_ALLOC_FAIL;
    282 
    283 	cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
    284 	cc->encrypt = do_encrypt;
    285 
    286 	if (keylen < cipher->key_len ||
    287 	    (iv != NULL && ivlen < cipher_ivlen(cipher))) {
    288 		ret = SSH_ERR_INVALID_ARGUMENT;
    289 		goto out;
    290 	}
    291 
    292 	cc->cipher = cipher;
    293 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    294 		cc->cp_ctx = chachapoly_new(key, keylen);
    295 		ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
    296 		goto out;
    297 	}
    298 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
    299 		ret = 0;
    300 		goto out;
    301 	}
    302 #ifndef WITH_OPENSSL
    303 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    304 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
    305 		aesctr_ivsetup(&cc->ac_ctx, iv);
    306 		ret = 0;
    307 		goto out;
    308 	}
    309 	ret = SSH_ERR_INVALID_ARGUMENT;
    310 	goto out;
    311 #else /* WITH_OPENSSL */
    312 	type = (*cipher->evptype)();
    313 	if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
    314 		ret = SSH_ERR_ALLOC_FAIL;
    315 		goto out;
    316 	}
    317 	if (EVP_CipherInit(cc->evp, type, NULL, (const u_char *)iv,
    318 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
    319 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    320 		goto out;
    321 	}
    322 	if (cipher_authlen(cipher) &&
    323 	    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
    324 	    -1, __UNCONST(iv)) <= 0) {
    325 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    326 		goto out;
    327 	}
    328 	klen = EVP_CIPHER_CTX_key_length(cc->evp);
    329 	if (klen > 0 && keylen != (u_int)klen) {
    330 		if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
    331 			ret = SSH_ERR_LIBCRYPTO_ERROR;
    332 			goto out;
    333 		}
    334 	}
    335 	/* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
    336 	   use EVP_CipherInit_ex for augmenting */
    337 	if (EVP_CipherInit_ex(cc->evp, NULL, NULL, __UNCONST(key), NULL, -1) == 0) {
    338 		ret = SSH_ERR_LIBCRYPTO_ERROR;
    339 		goto out;
    340 	}
    341 	ret = 0;
    342 #endif /* WITH_OPENSSL */
    343  out:
    344 	if (ret == 0) {
    345 		/* success */
    346 		*ccp = cc;
    347 	} else {
    348 		if (cc != NULL) {
    349 #ifdef WITH_OPENSSL
    350 			EVP_CIPHER_CTX_free(cc->evp);
    351 #endif /* WITH_OPENSSL */
    352 			freezero(cc, sizeof(*cc));
    353 		}
    354 	}
    355 	return ret;
    356 }
    357 
    358 /*
    359  * cipher_crypt() operates as following:
    360  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
    361  * These bytes are treated as additional authenticated data for
    362  * authenticated encryption modes.
    363  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
    364  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
    365  * This tag is written on encryption and verified on decryption.
    366  * Both 'aadlen' and 'authlen' can be set to 0.
    367  */
    368 int
    369 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
    370    const u_char *src, u_int len, u_int aadlen, u_int authlen)
    371 {
    372 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    373 		return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
    374 		    len, aadlen, authlen, cc->encrypt);
    375 	}
    376 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
    377 		memcpy(dest, src, aadlen + len);
    378 		return 0;
    379 	}
    380 #ifndef WITH_OPENSSL
    381 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    382 		if (aadlen)
    383 			memcpy(dest, src, aadlen);
    384 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
    385 		    dest + aadlen, len);
    386 		return 0;
    387 	}
    388 	return SSH_ERR_INVALID_ARGUMENT;
    389 #else
    390 	if (authlen) {
    391 		u_char lastiv[1];
    392 
    393 		if (authlen != cipher_authlen(cc->cipher))
    394 			return SSH_ERR_INVALID_ARGUMENT;
    395 		/* increment IV */
    396 		if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
    397 		    1, lastiv) <= 0)
    398 			return SSH_ERR_LIBCRYPTO_ERROR;
    399 		/* set tag on decryption */
    400 		if (!cc->encrypt &&
    401 		    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
    402 		    authlen, __UNCONST(src + aadlen + len)) <= 0)
    403 			return SSH_ERR_LIBCRYPTO_ERROR;
    404 	}
    405 	if (aadlen) {
    406 		if (authlen &&
    407 		    EVP_Cipher(cc->evp, NULL, (const u_char *)src, aadlen) < 0)
    408 			return SSH_ERR_LIBCRYPTO_ERROR;
    409 		memcpy(dest, src, aadlen);
    410 	}
    411 	if (len % cc->cipher->block_size)
    412 		return SSH_ERR_INVALID_ARGUMENT;
    413 	if (EVP_Cipher(cc->evp, dest + aadlen, (const u_char *)src + aadlen,
    414 	    len) < 0)
    415 		return SSH_ERR_LIBCRYPTO_ERROR;
    416 	if (authlen) {
    417 		/* compute tag (on encrypt) or verify tag (on decrypt) */
    418 		if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
    419 			return cc->encrypt ?
    420 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
    421 		if (cc->encrypt &&
    422 		    EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
    423 		    authlen, dest + aadlen + len) <= 0)
    424 			return SSH_ERR_LIBCRYPTO_ERROR;
    425 	}
    426 	return 0;
    427 #endif
    428 }
    429 
    430 /* Extract the packet length, including any decryption necessary beforehand */
    431 int
    432 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
    433     const u_char *cp, u_int len)
    434 {
    435 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    436 		return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
    437 		    cp, len);
    438 	if (len < 4)
    439 		return SSH_ERR_MESSAGE_INCOMPLETE;
    440 	*plenp = PEEK_U32(cp);
    441 	return 0;
    442 }
    443 
    444 void
    445 cipher_free(struct sshcipher_ctx *cc)
    446 {
    447 	if (cc == NULL)
    448 		return;
    449 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    450 		chachapoly_free(cc->cp_ctx);
    451 		cc->cp_ctx = NULL;
    452 	} else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
    453 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
    454 #ifdef WITH_OPENSSL
    455 	EVP_CIPHER_CTX_free(cc->evp);
    456 	cc->evp = NULL;
    457 #endif
    458 	freezero(cc, sizeof(*cc));
    459 }
    460 
    461 int
    462 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
    463 {
    464 #ifdef WITH_OPENSSL
    465 	const struct sshcipher *c = cc->cipher;
    466 	int evplen;
    467 #endif
    468 
    469 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
    470 		if (len != 0)
    471 			return SSH_ERR_INVALID_ARGUMENT;
    472 		return 0;
    473 	}
    474 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
    475 		if (len != sizeof(cc->ac_ctx.ctr))
    476 			return SSH_ERR_INVALID_ARGUMENT;
    477 		memcpy(iv, cc->ac_ctx.ctr, len);
    478 		return 0;
    479 	}
    480 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    481 		return 0;
    482 
    483 #ifdef WITH_OPENSSL
    484 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
    485 	if (evplen == 0)
    486 		return 0;
    487 	else if (evplen < 0)
    488 		return SSH_ERR_LIBCRYPTO_ERROR;
    489 	if ((size_t)evplen != len)
    490 		return SSH_ERR_INVALID_ARGUMENT;
    491 	if (cipher_authlen(c)) {
    492 		if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len,
    493 		    iv) <= 0)
    494 			return SSH_ERR_LIBCRYPTO_ERROR;
    495 	} else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0)
    496 		return SSH_ERR_LIBCRYPTO_ERROR;
    497 #endif
    498 	return 0;
    499 }
    500 
    501 int
    502 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
    503 {
    504 #ifdef WITH_OPENSSL
    505 	const struct sshcipher *c = cc->cipher;
    506 	int evplen = 0;
    507 #endif
    508 
    509 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
    510 		return 0;
    511 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
    512 		return 0;
    513 
    514 #ifdef WITH_OPENSSL
    515 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
    516 	if (evplen <= 0)
    517 		return SSH_ERR_LIBCRYPTO_ERROR;
    518 	if ((size_t)evplen != len)
    519 		return SSH_ERR_INVALID_ARGUMENT;
    520 	if (cipher_authlen(c)) {
    521 		/* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
    522 		if (EVP_CIPHER_CTX_ctrl(cc->evp,
    523 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, __UNCONST(iv)) <= 0)
    524 			return SSH_ERR_LIBCRYPTO_ERROR;
    525 	} else
    526 		memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
    527 #endif
    528 	return 0;
    529 }
    530