1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * 6 * As far as I am concerned, the code I have written for this software 7 * can be used freely for any purpose. Any derived versions of this 8 * software must be clearly marked as such, and if the derived work is 9 * incompatible with the protocol description in the RFC file, it must be 10 * called by a name other than "ssh" or "Secure Shell". 11 * 12 * 13 * Copyright (c) 1999 Niels Provos. All rights reserved. 14 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include "includes.h" 38 RCSID("$OpenBSD: cipher.c,v 1.59 2002/06/19 18:01:00 markus Exp $"); 39 RCSID("$FreeBSD$"); 40 41 #include "xmalloc.h" 42 #include "log.h" 43 #include "cipher.h" 44 45 #include <openssl/md5.h> 46 47 #if OPENSSL_VERSION_NUMBER < 0x00907000L 48 #include "rijndael.h" 49 static const EVP_CIPHER *evp_rijndael(void); 50 #endif 51 static const EVP_CIPHER *evp_ssh1_3des(void); 52 static const EVP_CIPHER *evp_ssh1_bf(void); 53 54 struct Cipher { 55 char *name; 56 int number; /* for ssh1 only */ 57 u_int block_size; 58 u_int key_len; 59 const EVP_CIPHER *(*evptype)(void); 60 } ciphers[] = { 61 { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null }, 62 { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc }, 63 { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des }, 64 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf }, 65 66 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc }, 67 { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc }, 68 { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc }, 69 { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 }, 70 #if OPENSSL_VERSION_NUMBER < 0x00907000L 71 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael }, 72 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael }, 73 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael }, 74 { "rijndael-cbc@lysator.liu.se", 75 SSH_CIPHER_SSH2, 16, 32, evp_rijndael }, 76 #else 77 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc }, 78 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc }, 79 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc }, 80 { "rijndael-cbc@lysator.liu.se", 81 SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc }, 82 #endif 83 84 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL } 85 }; 86 87 /*--*/ 88 89 u_int 90 cipher_blocksize(Cipher *c) 91 { 92 return (c->block_size); 93 } 94 u_int 95 cipher_keylen(Cipher *c) 96 { 97 return (c->key_len); 98 } 99 u_int 100 cipher_get_number(Cipher *c) 101 { 102 return (c->number); 103 } 104 105 u_int 106 cipher_mask_ssh1(int client) 107 { 108 u_int mask = 0; 109 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ 110 mask |= 1 << SSH_CIPHER_BLOWFISH; 111 if (client) { 112 mask |= 1 << SSH_CIPHER_DES; 113 } 114 return mask; 115 } 116 117 Cipher * 118 cipher_by_name(const char *name) 119 { 120 Cipher *c; 121 for (c = ciphers; c->name != NULL; c++) 122 if (strcasecmp(c->name, name) == 0) 123 return c; 124 return NULL; 125 } 126 127 Cipher * 128 cipher_by_number(int id) 129 { 130 Cipher *c; 131 for (c = ciphers; c->name != NULL; c++) 132 if (c->number == id) 133 return c; 134 return NULL; 135 } 136 137 #define CIPHER_SEP "," 138 int 139 ciphers_valid(const char *names) 140 { 141 Cipher *c; 142 char *ciphers, *cp; 143 char *p; 144 145 if (names == NULL || strcmp(names, "") == 0) 146 return 0; 147 ciphers = cp = xstrdup(names); 148 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 149 (p = strsep(&cp, CIPHER_SEP))) { 150 c = cipher_by_name(p); 151 if (c == NULL || c->number != SSH_CIPHER_SSH2) { 152 debug("bad cipher %s [%s]", p, names); 153 xfree(ciphers); 154 return 0; 155 } else { 156 debug3("cipher ok: %s [%s]", p, names); 157 } 158 } 159 debug3("ciphers ok: [%s]", names); 160 xfree(ciphers); 161 return 1; 162 } 163 164 /* 165 * Parses the name of the cipher. Returns the number of the corresponding 166 * cipher, or -1 on error. 167 */ 168 169 int 170 cipher_number(const char *name) 171 { 172 Cipher *c; 173 if (name == NULL) 174 return -1; 175 c = cipher_by_name(name); 176 return (c==NULL) ? -1 : c->number; 177 } 178 179 char * 180 cipher_name(int id) 181 { 182 Cipher *c = cipher_by_number(id); 183 return (c==NULL) ? "<unknown>" : c->name; 184 } 185 186 void 187 cipher_init(CipherContext *cc, Cipher *cipher, 188 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, 189 int encrypt) 190 { 191 static int dowarn = 1; 192 const EVP_CIPHER *type; 193 int klen; 194 195 if (cipher->number == SSH_CIPHER_DES) { 196 if (dowarn) { 197 error("Warning: use of DES is strongly discouraged " 198 "due to cryptographic weaknesses"); 199 dowarn = 0; 200 } 201 if (keylen > 8) 202 keylen = 8; 203 } 204 cc->plaintext = (cipher->number == SSH_CIPHER_NONE); 205 206 if (keylen < cipher->key_len) 207 fatal("cipher_init: key length %d is insufficient for %s.", 208 keylen, cipher->name); 209 if (iv != NULL && ivlen < cipher->block_size) 210 fatal("cipher_init: iv length %d is insufficient for %s.", 211 ivlen, cipher->name); 212 cc->cipher = cipher; 213 214 type = (*cipher->evptype)(); 215 216 EVP_CIPHER_CTX_init(&cc->evp); 217 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, 218 (encrypt == CIPHER_ENCRYPT)) == 0) 219 fatal("cipher_init: EVP_CipherInit failed for %s", 220 cipher->name); 221 klen = EVP_CIPHER_CTX_key_length(&cc->evp); 222 if (klen > 0 && keylen != klen) { 223 debug("cipher_init: set keylen (%d -> %d)", klen, keylen); 224 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) 225 fatal("cipher_init: set keylen failed (%d -> %d)", 226 klen, keylen); 227 } 228 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) 229 fatal("cipher_init: EVP_CipherInit: set key failed for %s", 230 cipher->name); 231 } 232 233 void 234 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) 235 { 236 if (len % cc->cipher->block_size) 237 fatal("cipher_encrypt: bad plaintext length %d", len); 238 if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0) 239 fatal("evp_crypt: EVP_Cipher failed"); 240 } 241 242 void 243 cipher_cleanup(CipherContext *cc) 244 { 245 if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) 246 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed"); 247 } 248 249 /* 250 * Selects the cipher, and keys if by computing the MD5 checksum of the 251 * passphrase and using the resulting 16 bytes as the key. 252 */ 253 254 void 255 cipher_set_key_string(CipherContext *cc, Cipher *cipher, 256 const char *passphrase, int encrypt) 257 { 258 MD5_CTX md; 259 u_char digest[16]; 260 261 MD5_Init(&md); 262 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); 263 MD5_Final(digest, &md); 264 265 cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt); 266 267 memset(digest, 0, sizeof(digest)); 268 memset(&md, 0, sizeof(md)); 269 } 270 271 /* Implementations for other non-EVP ciphers */ 272 273 /* 274 * This is used by SSH1: 275 * 276 * What kind of triple DES are these 2 routines? 277 * 278 * Why is there a redundant initialization vector? 279 * 280 * If only iv3 was used, then, this would till effect have been 281 * outer-cbc. However, there is also a private iv1 == iv2 which 282 * perhaps makes differential analysis easier. On the other hand, the 283 * private iv1 probably makes the CRC-32 attack ineffective. This is a 284 * result of that there is no longer any known iv1 to use when 285 * choosing the X block. 286 */ 287 struct ssh1_3des_ctx 288 { 289 EVP_CIPHER_CTX k1, k2, k3; 290 }; 291 static int 292 ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, 293 int enc) 294 { 295 struct ssh1_3des_ctx *c; 296 u_char *k1, *k2, *k3; 297 298 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 299 c = xmalloc(sizeof(*c)); 300 EVP_CIPHER_CTX_set_app_data(ctx, c); 301 } 302 if (key == NULL) 303 return (1); 304 if (enc == -1) 305 enc = ctx->encrypt; 306 k1 = k2 = k3 = (u_char *) key; 307 k2 += 8; 308 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) { 309 if (enc) 310 k3 += 16; 311 else 312 k1 += 16; 313 } 314 EVP_CIPHER_CTX_init(&c->k1); 315 EVP_CIPHER_CTX_init(&c->k2); 316 EVP_CIPHER_CTX_init(&c->k3); 317 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 || 318 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 || 319 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) { 320 memset(c, 0, sizeof(*c)); 321 xfree(c); 322 EVP_CIPHER_CTX_set_app_data(ctx, NULL); 323 return (0); 324 } 325 return (1); 326 } 327 static int 328 ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len) 329 { 330 struct ssh1_3des_ctx *c; 331 332 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 333 error("ssh1_3des_cbc: no context"); 334 return (0); 335 } 336 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || 337 EVP_Cipher(&c->k2, dest, dest, len) == 0 || 338 EVP_Cipher(&c->k3, dest, dest, len) == 0) 339 return (0); 340 return (1); 341 } 342 static int 343 ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) 344 { 345 struct ssh1_3des_ctx *c; 346 347 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { 348 memset(c, 0, sizeof(*c)); 349 xfree(c); 350 EVP_CIPHER_CTX_set_app_data(ctx, NULL); 351 } 352 return (1); 353 } 354 static const EVP_CIPHER * 355 evp_ssh1_3des(void) 356 { 357 static EVP_CIPHER ssh1_3des; 358 359 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); 360 ssh1_3des.nid = NID_undef; 361 ssh1_3des.block_size = 8; 362 ssh1_3des.iv_len = 0; 363 ssh1_3des.key_len = 16; 364 ssh1_3des.init = ssh1_3des_init; 365 ssh1_3des.cleanup = ssh1_3des_cleanup; 366 ssh1_3des.do_cipher = ssh1_3des_cbc; 367 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; 368 return (&ssh1_3des); 369 } 370 371 /* 372 * SSH1 uses a variation on Blowfish, all bytes must be swapped before 373 * and after encryption/decryption. Thus the swap_bytes stuff (yuk). 374 */ 375 static void 376 swap_bytes(const u_char *src, u_char *dst, int n) 377 { 378 u_char c[4]; 379 380 /* Process 4 bytes every lap. */ 381 for (n = n / 4; n > 0; n--) { 382 c[3] = *src++; 383 c[2] = *src++; 384 c[1] = *src++; 385 c[0] = *src++; 386 387 *dst++ = c[0]; 388 *dst++ = c[1]; 389 *dst++ = c[2]; 390 *dst++ = c[3]; 391 } 392 } 393 static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL; 394 static int 395 bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len) 396 { 397 int ret; 398 399 swap_bytes(in, out, len); 400 ret = (*orig_bf)(ctx, out, out, len); 401 swap_bytes(out, out, len); 402 return (ret); 403 } 404 static const EVP_CIPHER * 405 evp_ssh1_bf(void) 406 { 407 static EVP_CIPHER ssh1_bf; 408 409 memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER)); 410 orig_bf = ssh1_bf.do_cipher; 411 ssh1_bf.nid = NID_undef; 412 ssh1_bf.do_cipher = bf_ssh1_cipher; 413 ssh1_bf.key_len = 32; 414 return (&ssh1_bf); 415 } 416 417 #if OPENSSL_VERSION_NUMBER < 0x00907000L 418 /* RIJNDAEL */ 419 #define RIJNDAEL_BLOCKSIZE 16 420 struct ssh_rijndael_ctx 421 { 422 rijndael_ctx r_ctx; 423 u_char r_iv[RIJNDAEL_BLOCKSIZE]; 424 }; 425 426 static int 427 ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, 428 int enc) 429 { 430 struct ssh_rijndael_ctx *c; 431 432 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 433 c = xmalloc(sizeof(*c)); 434 EVP_CIPHER_CTX_set_app_data(ctx, c); 435 } 436 if (key != NULL) { 437 if (enc == -1) 438 enc = ctx->encrypt; 439 rijndael_set_key(&c->r_ctx, (u_char *)key, 440 8*EVP_CIPHER_CTX_key_length(ctx), enc); 441 } 442 if (iv != NULL) 443 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE); 444 return (1); 445 } 446 static int 447 ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, 448 u_int len) 449 { 450 struct ssh_rijndael_ctx *c; 451 u_char buf[RIJNDAEL_BLOCKSIZE]; 452 u_char *cprev, *cnow, *plain, *ivp; 453 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; 454 455 if (len == 0) 456 return (1); 457 if (len % RIJNDAEL_BLOCKSIZE) 458 fatal("ssh_rijndael_cbc: bad len %d", len); 459 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 460 error("ssh_rijndael_cbc: no context"); 461 return (0); 462 } 463 if (ctx->encrypt) { 464 cnow = dest; 465 plain = (u_char *)src; 466 cprev = c->r_iv; 467 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE, 468 cnow+=RIJNDAEL_BLOCKSIZE) { 469 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) 470 buf[j] = plain[j] ^ cprev[j]; 471 rijndael_encrypt(&c->r_ctx, buf, cnow); 472 cprev = cnow; 473 } 474 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE); 475 } else { 476 cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE); 477 plain = dest+len-RIJNDAEL_BLOCKSIZE; 478 479 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE); 480 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE, 481 plain-=RIJNDAEL_BLOCKSIZE) { 482 rijndael_decrypt(&c->r_ctx, cnow, plain); 483 ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE; 484 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) 485 plain[j] ^= ivp[j]; 486 } 487 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE); 488 } 489 return (1); 490 } 491 static int 492 ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx) 493 { 494 struct ssh_rijndael_ctx *c; 495 496 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { 497 memset(c, 0, sizeof(*c)); 498 xfree(c); 499 EVP_CIPHER_CTX_set_app_data(ctx, NULL); 500 } 501 return (1); 502 } 503 static const EVP_CIPHER * 504 evp_rijndael(void) 505 { 506 static EVP_CIPHER rijndal_cbc; 507 508 memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER)); 509 rijndal_cbc.nid = NID_undef; 510 rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE; 511 rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE; 512 rijndal_cbc.key_len = 16; 513 rijndal_cbc.init = ssh_rijndael_init; 514 rijndal_cbc.cleanup = ssh_rijndael_cleanup; 515 rijndal_cbc.do_cipher = ssh_rijndael_cbc; 516 rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | 517 EVP_CIPH_ALWAYS_CALL_INIT; 518 return (&rijndal_cbc); 519 } 520 #endif 521 522 /* 523 * Exports an IV from the CipherContext required to export the key 524 * state back from the unprivileged child to the privileged parent 525 * process. 526 */ 527 528 int 529 cipher_get_keyiv_len(CipherContext *cc) 530 { 531 Cipher *c = cc->cipher; 532 int ivlen; 533 534 if (c->number == SSH_CIPHER_3DES) 535 ivlen = 24; 536 else 537 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp); 538 return (ivlen); 539 } 540 541 void 542 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len) 543 { 544 Cipher *c = cc->cipher; 545 u_char *civ = NULL; 546 int evplen; 547 548 switch (c->number) { 549 case SSH_CIPHER_SSH2: 550 case SSH_CIPHER_DES: 551 case SSH_CIPHER_BLOWFISH: 552 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 553 if (evplen == 0) 554 return; 555 if (evplen != len) 556 fatal("%s: wrong iv length %d != %d", __func__, 557 evplen, len); 558 559 #if OPENSSL_VERSION_NUMBER < 0x00907000L 560 if (c->evptype == evp_rijndael) { 561 struct ssh_rijndael_ctx *aesc; 562 563 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp); 564 if (aesc == NULL) 565 fatal("%s: no rijndael context", __func__); 566 civ = aesc->r_iv; 567 } else 568 #endif 569 { 570 civ = cc->evp.iv; 571 } 572 break; 573 case SSH_CIPHER_3DES: { 574 struct ssh1_3des_ctx *desc; 575 if (len != 24) 576 fatal("%s: bad 3des iv length: %d", __func__, len); 577 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); 578 if (desc == NULL) 579 fatal("%s: no 3des context", __func__); 580 debug3("%s: Copying 3DES IV", __func__); 581 memcpy(iv, desc->k1.iv, 8); 582 memcpy(iv + 8, desc->k2.iv, 8); 583 memcpy(iv + 16, desc->k3.iv, 8); 584 return; 585 } 586 default: 587 fatal("%s: bad cipher %d", __func__, c->number); 588 } 589 memcpy(iv, civ, len); 590 } 591 592 void 593 cipher_set_keyiv(CipherContext *cc, u_char *iv) 594 { 595 Cipher *c = cc->cipher; 596 u_char *div = NULL; 597 int evplen = 0; 598 599 switch (c->number) { 600 case SSH_CIPHER_SSH2: 601 case SSH_CIPHER_DES: 602 case SSH_CIPHER_BLOWFISH: 603 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 604 if (evplen == 0) 605 return; 606 607 #if OPENSSL_VERSION_NUMBER < 0x00907000L 608 if (c->evptype == evp_rijndael) { 609 struct ssh_rijndael_ctx *aesc; 610 611 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp); 612 if (aesc == NULL) 613 fatal("%s: no rijndael context", __func__); 614 div = aesc->r_iv; 615 } else 616 #endif 617 { 618 div = cc->evp.iv; 619 } 620 break; 621 case SSH_CIPHER_3DES: { 622 struct ssh1_3des_ctx *desc; 623 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); 624 if (desc == NULL) 625 fatal("%s: no 3des context", __func__); 626 debug3("%s: Installed 3DES IV", __func__); 627 memcpy(desc->k1.iv, iv, 8); 628 memcpy(desc->k2.iv, iv + 8, 8); 629 memcpy(desc->k3.iv, iv + 16, 8); 630 return; 631 } 632 default: 633 fatal("%s: bad cipher %d", __func__, c->number); 634 } 635 memcpy(div, iv, evplen); 636 } 637 638 #if OPENSSL_VERSION_NUMBER < 0x00907000L 639 #define EVP_X_STATE(evp) &(evp).c 640 #define EVP_X_STATE_LEN(evp) sizeof((evp).c) 641 #else 642 #define EVP_X_STATE(evp) (evp).cipher_data 643 #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size 644 #endif 645 646 int 647 cipher_get_keycontext(CipherContext *cc, u_char *dat) 648 { 649 Cipher *c = cc->cipher; 650 int plen = 0; 651 652 if (c->evptype == EVP_rc4) { 653 plen = EVP_X_STATE_LEN(cc->evp); 654 if (dat == NULL) 655 return (plen); 656 memcpy(dat, EVP_X_STATE(cc->evp), plen); 657 } 658 return (plen); 659 } 660 661 void 662 cipher_set_keycontext(CipherContext *cc, u_char *dat) 663 { 664 Cipher *c = cc->cipher; 665 int plen; 666 667 if (c->evptype == EVP_rc4) { 668 plen = EVP_X_STATE_LEN(cc->evp); 669 memcpy(EVP_X_STATE(cc->evp), dat, plen); 670 } 671 } 672