1 /* $OpenBSD: cipher.c,v 1.97 2014/02/07 06:55:54 djm Exp $ */ 2 /* $FreeBSD$ */ 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 * 14 * 15 * Copyright (c) 1999 Niels Provos. All rights reserved. 16 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include "includes.h" 40 __RCSID("$FreeBSD$"); 41 42 #include <sys/types.h> 43 44 #include <string.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 48 #include "xmalloc.h" 49 #include "log.h" 50 #include "misc.h" 51 #include "cipher.h" 52 #include "buffer.h" 53 #include "digest.h" 54 55 /* compatibility with old or broken OpenSSL versions */ 56 #include "openbsd-compat/openssl-compat.h" 57 58 extern const EVP_CIPHER *evp_ssh1_bf(void); 59 extern const EVP_CIPHER *evp_ssh1_3des(void); 60 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); 61 62 struct Cipher { 63 char *name; 64 int number; /* for ssh1 only */ 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 discard_len; 70 u_int flags; 71 #define CFLAG_CBC (1<<0) 72 #define CFLAG_CHACHAPOLY (1<<1) 73 const EVP_CIPHER *(*evptype)(void); 74 }; 75 76 static const struct Cipher ciphers[] = { 77 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null }, 78 { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc }, 79 { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des }, 80 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf }, 81 82 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc }, 83 { "blowfish-cbc", 84 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc }, 85 { "cast128-cbc", 86 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc }, 87 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 }, 88 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 }, 89 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 }, 90 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc }, 91 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc }, 92 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, 93 { "rijndael-cbc@lysator.liu.se", 94 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, 95 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr }, 96 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr }, 97 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr }, 98 #ifdef OPENSSL_HAVE_EVPGCM 99 { "aes128-gcm@openssh.com", 100 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm }, 101 { "aes256-gcm@openssh.com", 102 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm }, 103 #endif 104 { "chacha20-poly1305@openssh.com", 105 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL }, 106 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL } 107 }; 108 109 /*--*/ 110 111 /* Returns a list of supported ciphers separated by the specified char. */ 112 char * 113 cipher_alg_list(char sep, int auth_only) 114 { 115 char *ret = NULL; 116 size_t nlen, rlen = 0; 117 const Cipher *c; 118 119 for (c = ciphers; c->name != NULL; c++) { 120 if (c->number != SSH_CIPHER_SSH2) 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 ret = xrealloc(ret, 1, rlen + nlen + 2); 128 memcpy(ret + rlen, c->name, nlen + 1); 129 rlen += nlen; 130 } 131 return ret; 132 } 133 134 u_int 135 cipher_blocksize(const Cipher *c) 136 { 137 return (c->block_size); 138 } 139 140 u_int 141 cipher_keylen(const Cipher *c) 142 { 143 return (c->key_len); 144 } 145 146 u_int 147 cipher_seclen(const Cipher *c) 148 { 149 if (strcmp("3des-cbc", c->name) == 0) 150 return 14; 151 return cipher_keylen(c); 152 } 153 154 u_int 155 cipher_authlen(const Cipher *c) 156 { 157 return (c->auth_len); 158 } 159 160 u_int 161 cipher_ivlen(const Cipher *c) 162 { 163 /* 164 * Default is cipher block size, except for chacha20+poly1305 that 165 * needs no IV. XXX make iv_len == -1 default? 166 */ 167 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ? 168 c->iv_len : c->block_size; 169 } 170 171 u_int 172 cipher_get_number(const Cipher *c) 173 { 174 return (c->number); 175 } 176 177 u_int 178 cipher_is_cbc(const Cipher *c) 179 { 180 return (c->flags & CFLAG_CBC) != 0; 181 } 182 183 u_int 184 cipher_mask_ssh1(int client) 185 { 186 u_int mask = 0; 187 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ 188 mask |= 1 << SSH_CIPHER_BLOWFISH; 189 if (client) { 190 mask |= 1 << SSH_CIPHER_DES; 191 } 192 return mask; 193 } 194 195 const Cipher * 196 cipher_by_name(const char *name) 197 { 198 const Cipher *c; 199 for (c = ciphers; c->name != NULL; c++) 200 if (strcmp(c->name, name) == 0) 201 return c; 202 return NULL; 203 } 204 205 const Cipher * 206 cipher_by_number(int id) 207 { 208 const Cipher *c; 209 for (c = ciphers; c->name != NULL; c++) 210 if (c->number == id) 211 return c; 212 return NULL; 213 } 214 215 #define CIPHER_SEP "," 216 int 217 ciphers_valid(const char *names) 218 { 219 const Cipher *c; 220 char *cipher_list, *cp; 221 char *p; 222 223 if (names == NULL || strcmp(names, "") == 0) 224 return 0; 225 cipher_list = cp = xstrdup(names); 226 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 227 (p = strsep(&cp, CIPHER_SEP))) { 228 c = cipher_by_name(p); 229 #ifdef NONE_CIPHER_ENABLED 230 if (c == NULL || (c->number != SSH_CIPHER_SSH2 && 231 c->number != SSH_CIPHER_NONE)) { 232 #else 233 if (c == NULL || (c->number != SSH_CIPHER_SSH2)) { 234 #endif 235 debug("bad cipher %s [%s]", p, names); 236 free(cipher_list); 237 return 0; 238 } 239 } 240 debug3("ciphers ok: [%s]", names); 241 free(cipher_list); 242 return 1; 243 } 244 245 /* 246 * Parses the name of the cipher. Returns the number of the corresponding 247 * cipher, or -1 on error. 248 */ 249 250 int 251 cipher_number(const char *name) 252 { 253 const Cipher *c; 254 if (name == NULL) 255 return -1; 256 for (c = ciphers; c->name != NULL; c++) 257 if (strcasecmp(c->name, name) == 0) 258 return c->number; 259 return -1; 260 } 261 262 char * 263 cipher_name(int id) 264 { 265 const Cipher *c = cipher_by_number(id); 266 return (c==NULL) ? "<unknown>" : c->name; 267 } 268 269 void 270 cipher_init(CipherContext *cc, const Cipher *cipher, 271 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, 272 int do_encrypt) 273 { 274 static int dowarn = 1; 275 #ifdef SSH_OLD_EVP 276 EVP_CIPHER *type; 277 #else 278 const EVP_CIPHER *type; 279 int klen; 280 #endif 281 u_char *junk, *discard; 282 283 if (cipher->number == SSH_CIPHER_DES) { 284 if (dowarn) { 285 error("Warning: use of DES is strongly discouraged " 286 "due to cryptographic weaknesses"); 287 dowarn = 0; 288 } 289 if (keylen > 8) 290 keylen = 8; 291 } 292 cc->plaintext = (cipher->number == SSH_CIPHER_NONE); 293 cc->encrypt = do_encrypt; 294 295 if (keylen < cipher->key_len) 296 fatal("cipher_init: key length %d is insufficient for %s.", 297 keylen, cipher->name); 298 if (iv != NULL && ivlen < cipher_ivlen(cipher)) 299 fatal("cipher_init: iv length %d is insufficient for %s.", 300 ivlen, cipher->name); 301 cc->cipher = cipher; 302 303 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 304 chachapoly_init(&cc->cp_ctx, key, keylen); 305 return; 306 } 307 type = (*cipher->evptype)(); 308 EVP_CIPHER_CTX_init(&cc->evp); 309 #ifdef SSH_OLD_EVP 310 if (type->key_len > 0 && type->key_len != keylen) { 311 debug("cipher_init: set keylen (%d -> %d)", 312 type->key_len, keylen); 313 type->key_len = keylen; 314 } 315 EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv, 316 (do_encrypt == CIPHER_ENCRYPT)); 317 #else 318 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, 319 (do_encrypt == CIPHER_ENCRYPT)) == 0) 320 fatal("cipher_init: EVP_CipherInit failed for %s", 321 cipher->name); 322 if (cipher_authlen(cipher) && 323 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, 324 -1, (u_char *)iv)) 325 fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s", 326 cipher->name); 327 klen = EVP_CIPHER_CTX_key_length(&cc->evp); 328 if (klen > 0 && keylen != (u_int)klen) { 329 debug2("cipher_init: set keylen (%d -> %d)", klen, keylen); 330 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) 331 fatal("cipher_init: set keylen failed (%d -> %d)", 332 klen, keylen); 333 } 334 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) 335 fatal("cipher_init: EVP_CipherInit: set key failed for %s", 336 cipher->name); 337 #endif 338 339 if (cipher->discard_len > 0) { 340 junk = xmalloc(cipher->discard_len); 341 discard = xmalloc(cipher->discard_len); 342 if (EVP_Cipher(&cc->evp, discard, junk, 343 cipher->discard_len) == 0) 344 fatal("evp_crypt: EVP_Cipher failed during discard"); 345 explicit_bzero(discard, cipher->discard_len); 346 free(junk); 347 free(discard); 348 } 349 } 350 351 /* 352 * cipher_crypt() operates as following: 353 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. 354 * Theses bytes are treated as additional authenticated data for 355 * authenticated encryption modes. 356 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. 357 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. 358 * This tag is written on encryption and verified on decryption. 359 * Both 'aadlen' and 'authlen' can be set to 0. 360 * cipher_crypt() returns 0 on success and -1 if the decryption integrity 361 * check fails. 362 */ 363 int 364 cipher_crypt(CipherContext *cc, u_int seqnr, u_char *dest, const u_char *src, 365 u_int len, u_int aadlen, u_int authlen) 366 { 367 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 368 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, len, 369 aadlen, authlen, cc->encrypt); 370 if (authlen) { 371 u_char lastiv[1]; 372 373 if (authlen != cipher_authlen(cc->cipher)) 374 fatal("%s: authlen mismatch %d", __func__, authlen); 375 /* increment IV */ 376 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN, 377 1, lastiv)) 378 fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__); 379 /* set tag on decyption */ 380 if (!cc->encrypt && 381 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG, 382 authlen, (u_char *)src + aadlen + len)) 383 fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__); 384 } 385 if (aadlen) { 386 if (authlen && 387 EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0) 388 fatal("%s: EVP_Cipher(aad) failed", __func__); 389 memcpy(dest, src, aadlen); 390 } 391 if (len % cc->cipher->block_size) 392 fatal("%s: bad plaintext length %d", __func__, len); 393 if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen, 394 len) < 0) 395 fatal("%s: EVP_Cipher failed", __func__); 396 if (authlen) { 397 /* compute tag (on encrypt) or verify tag (on decrypt) */ 398 if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) { 399 if (cc->encrypt) 400 fatal("%s: EVP_Cipher(final) failed", __func__); 401 else 402 return -1; 403 } 404 if (cc->encrypt && 405 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG, 406 authlen, dest + aadlen + len)) 407 fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__); 408 } 409 return 0; 410 } 411 412 /* Extract the packet length, including any decryption necessary beforehand */ 413 int 414 cipher_get_length(CipherContext *cc, u_int *plenp, u_int seqnr, 415 const u_char *cp, u_int len) 416 { 417 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 418 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr, 419 cp, len); 420 if (len < 4) 421 return -1; 422 *plenp = get_u32(cp); 423 return 0; 424 } 425 426 void 427 cipher_cleanup(CipherContext *cc) 428 { 429 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 430 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx)); 431 else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) 432 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed"); 433 } 434 435 /* 436 * Selects the cipher, and keys if by computing the MD5 checksum of the 437 * passphrase and using the resulting 16 bytes as the key. 438 */ 439 440 void 441 cipher_set_key_string(CipherContext *cc, const Cipher *cipher, 442 const char *passphrase, int do_encrypt) 443 { 444 u_char digest[16]; 445 446 if (ssh_digest_memory(SSH_DIGEST_MD5, passphrase, strlen(passphrase), 447 digest, sizeof(digest)) < 0) 448 fatal("%s: md5 failed", __func__); 449 450 cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt); 451 452 explicit_bzero(digest, sizeof(digest)); 453 } 454 455 /* 456 * Exports an IV from the CipherContext required to export the key 457 * state back from the unprivileged child to the privileged parent 458 * process. 459 */ 460 461 int 462 cipher_get_keyiv_len(const CipherContext *cc) 463 { 464 const Cipher *c = cc->cipher; 465 int ivlen; 466 467 if (c->number == SSH_CIPHER_3DES) 468 ivlen = 24; 469 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 470 ivlen = 0; 471 else 472 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp); 473 return (ivlen); 474 } 475 476 void 477 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len) 478 { 479 const Cipher *c = cc->cipher; 480 int evplen; 481 482 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 483 if (len != 0) 484 fatal("%s: wrong iv length %d != %d", __func__, len, 0); 485 return; 486 } 487 488 switch (c->number) { 489 #ifdef NONE_CIPHER_ENABLED 490 case SSH_CIPHER_NONE: 491 #endif 492 case SSH_CIPHER_SSH2: 493 case SSH_CIPHER_DES: 494 case SSH_CIPHER_BLOWFISH: 495 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 496 if (evplen <= 0) 497 return; 498 if ((u_int)evplen != len) 499 fatal("%s: wrong iv length %d != %d", __func__, 500 evplen, len); 501 #ifdef USE_BUILTIN_RIJNDAEL 502 if (c->evptype == evp_rijndael) 503 ssh_rijndael_iv(&cc->evp, 0, iv, len); 504 else 505 #endif 506 #ifndef OPENSSL_HAVE_EVPCTR 507 if (c->evptype == evp_aes_128_ctr) 508 ssh_aes_ctr_iv(&cc->evp, 0, iv, len); 509 else 510 #endif 511 memcpy(iv, cc->evp.iv, len); 512 break; 513 case SSH_CIPHER_3DES: 514 ssh1_3des_iv(&cc->evp, 0, iv, 24); 515 break; 516 default: 517 fatal("%s: bad cipher %d", __func__, c->number); 518 } 519 } 520 521 void 522 cipher_set_keyiv(CipherContext *cc, u_char *iv) 523 { 524 const Cipher *c = cc->cipher; 525 int evplen = 0; 526 527 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 528 return; 529 530 switch (c->number) { 531 #ifdef NONE_CIPHER_ENABLED 532 case SSH_CIPHER_NONE: 533 #endif 534 case SSH_CIPHER_SSH2: 535 case SSH_CIPHER_DES: 536 case SSH_CIPHER_BLOWFISH: 537 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 538 if (evplen == 0) 539 return; 540 #ifdef USE_BUILTIN_RIJNDAEL 541 if (c->evptype == evp_rijndael) 542 ssh_rijndael_iv(&cc->evp, 1, iv, evplen); 543 else 544 #endif 545 #ifndef OPENSSL_HAVE_EVPCTR 546 if (c->evptype == evp_aes_128_ctr) 547 ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen); 548 else 549 #endif 550 memcpy(cc->evp.iv, iv, evplen); 551 break; 552 case SSH_CIPHER_3DES: 553 ssh1_3des_iv(&cc->evp, 1, iv, 24); 554 break; 555 default: 556 fatal("%s: bad cipher %d", __func__, c->number); 557 } 558 } 559 560 int 561 cipher_get_keycontext(const CipherContext *cc, u_char *dat) 562 { 563 const Cipher *c = cc->cipher; 564 int plen = 0; 565 566 if (c->evptype == EVP_rc4) { 567 plen = EVP_X_STATE_LEN(cc->evp); 568 if (dat == NULL) 569 return (plen); 570 memcpy(dat, EVP_X_STATE(cc->evp), plen); 571 } 572 return (plen); 573 } 574 575 void 576 cipher_set_keycontext(CipherContext *cc, u_char *dat) 577 { 578 const Cipher *c = cc->cipher; 579 int plen; 580 581 if (c->evptype == EVP_rc4) { 582 plen = EVP_X_STATE_LEN(cc->evp); 583 memcpy(EVP_X_STATE(cc->evp), dat, plen); 584 } 585 } 586