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