1 /* $NetBSD: cipher.c,v 1.27 2026/04/08 18:58:40 christos Exp $ */ 2 /* $OpenBSD: cipher.c,v 1.126 2026/02/14 00:18:34 jsg 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.27 2026/04/08 18:58:40 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_ctx_is_plaintext(struct sshcipher_ctx *cc) 219 { 220 return cc->plaintext; 221 } 222 223 const struct sshcipher * 224 cipher_by_name(const char *name) 225 { 226 const struct sshcipher *c; 227 for (c = ciphers; c->name != NULL; c++) 228 if (strcmp(c->name, name) == 0) 229 return c; 230 return NULL; 231 } 232 233 #define CIPHER_SEP "," 234 int 235 ciphers_valid(const char *names) 236 { 237 const struct sshcipher *c; 238 char *cipher_list, *cp; 239 char *p; 240 241 if (names == NULL || strcmp(names, "") == 0) 242 return 0; 243 if ((cipher_list = cp = strdup(names)) == NULL) 244 return 0; 245 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 246 (p = strsep(&cp, CIPHER_SEP))) { 247 c = cipher_by_name(p); 248 if (c == NULL || (c->flags & (CFLAG_INTERNAL|CFLAG_NONE)) != 0) { 249 free(cipher_list); 250 return 0; 251 } 252 } 253 free(cipher_list); 254 return 1; 255 } 256 257 const char * 258 cipher_warning_message(const struct sshcipher_ctx *cc) 259 { 260 if (cc == NULL || cc->cipher == NULL) 261 return NULL; 262 /* XXX repurpose for CBC warning */ 263 return NULL; 264 } 265 266 int 267 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher, 268 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, 269 int do_encrypt) 270 { 271 struct sshcipher_ctx *cc = NULL; 272 int ret = SSH_ERR_INTERNAL_ERROR; 273 #ifdef WITH_OPENSSL 274 const EVP_CIPHER *type; 275 int klen; 276 #endif 277 278 *ccp = NULL; 279 if ((cc = calloc(1, sizeof(*cc))) == NULL) 280 return SSH_ERR_ALLOC_FAIL; 281 282 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0; 283 cc->encrypt = do_encrypt; 284 285 if (keylen < cipher->key_len || 286 (iv != NULL && ivlen < cipher_ivlen(cipher))) { 287 ret = SSH_ERR_INVALID_ARGUMENT; 288 goto out; 289 } 290 291 cc->cipher = cipher; 292 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 293 cc->cp_ctx = chachapoly_new(key, keylen); 294 ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT; 295 goto out; 296 } 297 if ((cc->cipher->flags & CFLAG_NONE) != 0) { 298 ret = 0; 299 goto out; 300 } 301 #ifndef WITH_OPENSSL 302 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 303 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); 304 aesctr_ivsetup(&cc->ac_ctx, iv); 305 ret = 0; 306 goto out; 307 } 308 ret = SSH_ERR_INVALID_ARGUMENT; 309 goto out; 310 #else /* WITH_OPENSSL */ 311 type = (*cipher->evptype)(); 312 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) { 313 ret = SSH_ERR_ALLOC_FAIL; 314 goto out; 315 } 316 if (EVP_CipherInit(cc->evp, type, NULL, (const u_char *)iv, 317 (do_encrypt == CIPHER_ENCRYPT)) == 0) { 318 ret = SSH_ERR_LIBCRYPTO_ERROR; 319 goto out; 320 } 321 if (cipher_authlen(cipher) && 322 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, 323 -1, __UNCONST(iv)) <= 0) { 324 ret = SSH_ERR_LIBCRYPTO_ERROR; 325 goto out; 326 } 327 klen = EVP_CIPHER_CTX_key_length(cc->evp); 328 if (klen > 0 && keylen != (u_int)klen) { 329 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) { 330 ret = SSH_ERR_LIBCRYPTO_ERROR; 331 goto out; 332 } 333 } 334 /* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups; 335 use EVP_CipherInit_ex for augmenting */ 336 if (EVP_CipherInit_ex(cc->evp, NULL, NULL, __UNCONST(key), NULL, -1) == 0) { 337 ret = SSH_ERR_LIBCRYPTO_ERROR; 338 goto out; 339 } 340 ret = 0; 341 #endif /* WITH_OPENSSL */ 342 out: 343 if (ret == 0) { 344 /* success */ 345 *ccp = cc; 346 } else { 347 if (cc != NULL) { 348 #ifdef WITH_OPENSSL 349 EVP_CIPHER_CTX_free(cc->evp); 350 #endif /* WITH_OPENSSL */ 351 freezero(cc, sizeof(*cc)); 352 } 353 } 354 return ret; 355 } 356 357 /* 358 * cipher_crypt() operates as following: 359 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. 360 * These bytes are treated as additional authenticated data for 361 * authenticated encryption modes. 362 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. 363 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. 364 * This tag is written on encryption and verified on decryption. 365 * Both 'aadlen' and 'authlen' can be set to 0. 366 */ 367 int 368 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, 369 const u_char *src, u_int len, u_int aadlen, u_int authlen) 370 { 371 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 372 return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src, 373 len, aadlen, authlen, cc->encrypt); 374 } 375 if ((cc->cipher->flags & CFLAG_NONE) != 0) { 376 memcpy(dest, src, aadlen + len); 377 return 0; 378 } 379 #ifndef WITH_OPENSSL 380 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 381 if (aadlen) 382 memcpy(dest, src, aadlen); 383 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, 384 dest + aadlen, len); 385 return 0; 386 } 387 return SSH_ERR_INVALID_ARGUMENT; 388 #else 389 if (authlen) { 390 u_char lastiv[1]; 391 392 if (authlen != cipher_authlen(cc->cipher)) 393 return SSH_ERR_INVALID_ARGUMENT; 394 /* increment IV */ 395 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, 396 1, lastiv) <= 0) 397 return SSH_ERR_LIBCRYPTO_ERROR; 398 /* set tag on decryption */ 399 if (!cc->encrypt && 400 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG, 401 authlen, __UNCONST(src + aadlen + len)) <= 0) 402 return SSH_ERR_LIBCRYPTO_ERROR; 403 } 404 if (aadlen) { 405 if (authlen && 406 EVP_Cipher(cc->evp, NULL, (const u_char *)src, aadlen) < 0) 407 return SSH_ERR_LIBCRYPTO_ERROR; 408 memcpy(dest, src, aadlen); 409 } 410 if (len % cc->cipher->block_size) 411 return SSH_ERR_INVALID_ARGUMENT; 412 if (EVP_Cipher(cc->evp, dest + aadlen, (const u_char *)src + aadlen, 413 len) < 0) 414 return SSH_ERR_LIBCRYPTO_ERROR; 415 if (authlen) { 416 /* compute tag (on encrypt) or verify tag (on decrypt) */ 417 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0) 418 return cc->encrypt ? 419 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; 420 if (cc->encrypt && 421 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG, 422 authlen, dest + aadlen + len) <= 0) 423 return SSH_ERR_LIBCRYPTO_ERROR; 424 } 425 return 0; 426 #endif 427 } 428 429 /* Extract the packet length, including any decryption necessary beforehand */ 430 int 431 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr, 432 const u_char *cp, u_int len) 433 { 434 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 435 return chachapoly_get_length(cc->cp_ctx, plenp, seqnr, 436 cp, len); 437 if (len < 4) 438 return SSH_ERR_MESSAGE_INCOMPLETE; 439 *plenp = PEEK_U32(cp); 440 return 0; 441 } 442 443 void 444 cipher_free(struct sshcipher_ctx *cc) 445 { 446 if (cc == NULL) 447 return; 448 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 449 chachapoly_free(cc->cp_ctx); 450 cc->cp_ctx = NULL; 451 } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) 452 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); 453 #ifdef WITH_OPENSSL 454 EVP_CIPHER_CTX_free(cc->evp); 455 cc->evp = NULL; 456 #endif 457 freezero(cc, sizeof(*cc)); 458 } 459 460 int 461 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len) 462 { 463 #ifdef WITH_OPENSSL 464 const struct sshcipher *c = cc->cipher; 465 int evplen; 466 #endif 467 468 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 469 if (len != 0) 470 return SSH_ERR_INVALID_ARGUMENT; 471 return 0; 472 } 473 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 474 if (len != sizeof(cc->ac_ctx.ctr)) 475 return SSH_ERR_INVALID_ARGUMENT; 476 memcpy(iv, cc->ac_ctx.ctr, len); 477 return 0; 478 } 479 if ((cc->cipher->flags & CFLAG_NONE) != 0) 480 return 0; 481 482 #ifdef WITH_OPENSSL 483 evplen = EVP_CIPHER_CTX_iv_length(cc->evp); 484 if (evplen == 0) 485 return 0; 486 else if (evplen < 0) 487 return SSH_ERR_LIBCRYPTO_ERROR; 488 if ((size_t)evplen != len) 489 return SSH_ERR_INVALID_ARGUMENT; 490 if (cipher_authlen(c)) { 491 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len, 492 iv) <= 0) 493 return SSH_ERR_LIBCRYPTO_ERROR; 494 } else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0) 495 return SSH_ERR_LIBCRYPTO_ERROR; 496 #endif 497 return 0; 498 } 499 500 int 501 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len) 502 { 503 #ifdef WITH_OPENSSL 504 const struct sshcipher *c = cc->cipher; 505 int evplen = 0; 506 #endif 507 508 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 509 return 0; 510 if ((cc->cipher->flags & CFLAG_NONE) != 0) 511 return 0; 512 513 #ifdef WITH_OPENSSL 514 evplen = EVP_CIPHER_CTX_iv_length(cc->evp); 515 if (evplen <= 0) 516 return SSH_ERR_LIBCRYPTO_ERROR; 517 if ((size_t)evplen != len) 518 return SSH_ERR_INVALID_ARGUMENT; 519 if (cipher_authlen(c)) { 520 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ 521 if (EVP_CIPHER_CTX_ctrl(cc->evp, 522 EVP_CTRL_GCM_SET_IV_FIXED, -1, __UNCONST(iv)) <= 0) 523 return SSH_ERR_LIBCRYPTO_ERROR; 524 } else 525 memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen); 526 #endif 527 return 0; 528 } 529