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