1 /* $OpenBSD: cipher.c,v 1.99 2014/06/24 01:13:21 djm 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 "includes.h" 39 40 #include <sys/types.h> 41 42 #include <string.h> 43 #include <stdarg.h> 44 #include <stdio.h> 45 46 #include "cipher.h" 47 #include "misc.h" 48 #include "sshbuf.h" 49 #include "ssherr.h" 50 #include "digest.h" 51 52 #ifdef WITH_SSH1 53 extern const EVP_CIPHER *evp_ssh1_bf(void); 54 extern const EVP_CIPHER *evp_ssh1_3des(void); 55 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); 56 #endif 57 58 struct sshcipher { 59 char *name; 60 int number; /* for ssh1 only */ 61 u_int block_size; 62 u_int key_len; 63 u_int iv_len; /* defaults to block_size */ 64 u_int auth_len; 65 u_int discard_len; 66 u_int flags; 67 #define CFLAG_CBC (1<<0) 68 #define CFLAG_CHACHAPOLY (1<<1) 69 #define CFLAG_AESCTR (1<<2) 70 #define CFLAG_NONE (1<<3) 71 #ifdef WITH_OPENSSL 72 const EVP_CIPHER *(*evptype)(void); 73 #else 74 void *ignored; 75 #endif 76 }; 77 78 static const struct sshcipher ciphers[] = { 79 #ifdef WITH_SSH1 80 { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc }, 81 { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des }, 82 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf }, 83 #endif /* WITH_SSH1 */ 84 #ifdef WITH_OPENSSL 85 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null }, 86 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc }, 87 { "blowfish-cbc", 88 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc }, 89 { "cast128-cbc", 90 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc }, 91 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 }, 92 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 }, 93 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 }, 94 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc }, 95 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc }, 96 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, 97 { "rijndael-cbc@lysator.liu.se", 98 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, 99 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr }, 100 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr }, 101 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr }, 102 # ifdef OPENSSL_HAVE_EVPGCM 103 { "aes128-gcm@openssh.com", 104 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm }, 105 { "aes256-gcm@openssh.com", 106 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm }, 107 # endif /* OPENSSL_HAVE_EVPGCM */ 108 #else /* WITH_OPENSSL */ 109 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL }, 110 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL }, 111 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL }, 112 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL }, 113 #endif /* WITH_OPENSSL */ 114 { "chacha20-poly1305@openssh.com", 115 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL }, 116 117 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL } 118 }; 119 120 /*--*/ 121 122 /* Returns a comma-separated list of supported ciphers. */ 123 char * 124 cipher_alg_list(char sep, int auth_only) 125 { 126 char *tmp, *ret = NULL; 127 size_t nlen, rlen = 0; 128 const struct sshcipher *c; 129 130 for (c = ciphers; c->name != NULL; c++) { 131 if (c->number != SSH_CIPHER_SSH2) 132 continue; 133 if (auth_only && c->auth_len == 0) 134 continue; 135 if (ret != NULL) 136 ret[rlen++] = sep; 137 nlen = strlen(c->name); 138 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { 139 free(ret); 140 return NULL; 141 } 142 ret = tmp; 143 memcpy(ret + rlen, c->name, nlen + 1); 144 rlen += nlen; 145 } 146 return ret; 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_get_number(const struct sshcipher *c) 188 { 189 return (c->number); 190 } 191 192 u_int 193 cipher_is_cbc(const struct sshcipher *c) 194 { 195 return (c->flags & CFLAG_CBC) != 0; 196 } 197 198 u_int 199 cipher_mask_ssh1(int client) 200 { 201 u_int mask = 0; 202 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ 203 mask |= 1 << SSH_CIPHER_BLOWFISH; 204 if (client) { 205 mask |= 1 << SSH_CIPHER_DES; 206 } 207 return mask; 208 } 209 210 const struct sshcipher * 211 cipher_by_name(const char *name) 212 { 213 const struct sshcipher *c; 214 for (c = ciphers; c->name != NULL; c++) 215 if (strcmp(c->name, name) == 0) 216 return c; 217 return NULL; 218 } 219 220 const struct sshcipher * 221 cipher_by_number(int id) 222 { 223 const struct sshcipher *c; 224 for (c = ciphers; c->name != NULL; c++) 225 if (c->number == id) 226 return c; 227 return NULL; 228 } 229 230 #define CIPHER_SEP "," 231 int 232 ciphers_valid(const char *names) 233 { 234 const struct sshcipher *c; 235 char *cipher_list, *cp; 236 char *p; 237 238 if (names == NULL || strcmp(names, "") == 0) 239 return 0; 240 if ((cipher_list = cp = strdup(names)) == NULL) 241 return 0; 242 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 243 (p = strsep(&cp, CIPHER_SEP))) { 244 c = cipher_by_name(p); 245 if (c == NULL || c->number != SSH_CIPHER_SSH2) { 246 free(cipher_list); 247 return 0; 248 } 249 } 250 free(cipher_list); 251 return 1; 252 } 253 254 /* 255 * Parses the name of the cipher. Returns the number of the corresponding 256 * cipher, or -1 on error. 257 */ 258 259 int 260 cipher_number(const char *name) 261 { 262 const struct sshcipher *c; 263 if (name == NULL) 264 return -1; 265 for (c = ciphers; c->name != NULL; c++) 266 if (strcasecmp(c->name, name) == 0) 267 return c->number; 268 return -1; 269 } 270 271 char * 272 cipher_name(int id) 273 { 274 const struct sshcipher *c = cipher_by_number(id); 275 return (c==NULL) ? "<unknown>" : c->name; 276 } 277 278 const char * 279 cipher_warning_message(const struct sshcipher_ctx *cc) 280 { 281 if (cc == NULL || cc->cipher == NULL) 282 return NULL; 283 if (cc->cipher->number == SSH_CIPHER_DES) 284 return "use of DES is strongly discouraged due to " 285 "cryptographic weaknesses"; 286 return NULL; 287 } 288 289 int 290 cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher, 291 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, 292 int do_encrypt) 293 { 294 #ifdef WITH_OPENSSL 295 int ret = SSH_ERR_INTERNAL_ERROR; 296 const EVP_CIPHER *type; 297 int klen; 298 u_char *junk, *discard; 299 300 if (cipher->number == SSH_CIPHER_DES) { 301 if (keylen > 8) 302 keylen = 8; 303 } 304 #endif 305 cc->plaintext = (cipher->number == SSH_CIPHER_NONE); 306 cc->encrypt = do_encrypt; 307 308 if (keylen < cipher->key_len || 309 (iv != NULL && ivlen < cipher_ivlen(cipher))) 310 return SSH_ERR_INVALID_ARGUMENT; 311 312 cc->cipher = cipher; 313 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 314 return chachapoly_init(&cc->cp_ctx, key, keylen); 315 } 316 #ifndef WITH_OPENSSL 317 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 318 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); 319 aesctr_ivsetup(&cc->ac_ctx, iv); 320 return 0; 321 } 322 if ((cc->cipher->flags & CFLAG_NONE) != 0) 323 return 0; 324 return SSH_ERR_INVALID_ARGUMENT; 325 #else 326 type = (*cipher->evptype)(); 327 EVP_CIPHER_CTX_init(&cc->evp); 328 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, 329 (do_encrypt == CIPHER_ENCRYPT)) == 0) { 330 ret = SSH_ERR_LIBCRYPTO_ERROR; 331 goto bad; 332 } 333 if (cipher_authlen(cipher) && 334 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, 335 -1, (u_char *)iv)) { 336 ret = SSH_ERR_LIBCRYPTO_ERROR; 337 goto bad; 338 } 339 klen = EVP_CIPHER_CTX_key_length(&cc->evp); 340 if (klen > 0 && keylen != (u_int)klen) { 341 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) { 342 ret = SSH_ERR_LIBCRYPTO_ERROR; 343 goto bad; 344 } 345 } 346 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) { 347 ret = SSH_ERR_LIBCRYPTO_ERROR; 348 goto bad; 349 } 350 351 if (cipher->discard_len > 0) { 352 if ((junk = malloc(cipher->discard_len)) == NULL || 353 (discard = malloc(cipher->discard_len)) == NULL) { 354 if (junk != NULL) 355 free(junk); 356 ret = SSH_ERR_ALLOC_FAIL; 357 goto bad; 358 } 359 ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len); 360 explicit_bzero(discard, cipher->discard_len); 361 free(junk); 362 free(discard); 363 if (ret != 1) { 364 ret = SSH_ERR_LIBCRYPTO_ERROR; 365 bad: 366 EVP_CIPHER_CTX_cleanup(&cc->evp); 367 return ret; 368 } 369 } 370 #endif 371 return 0; 372 } 373 374 /* 375 * cipher_crypt() operates as following: 376 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. 377 * Theses bytes are treated as additional authenticated data for 378 * authenticated encryption modes. 379 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. 380 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. 381 * This tag is written on encryption and verified on decryption. 382 * Both 'aadlen' and 'authlen' can be set to 0. 383 */ 384 int 385 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, 386 const u_char *src, u_int len, u_int aadlen, u_int authlen) 387 { 388 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 389 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, 390 len, aadlen, authlen, cc->encrypt); 391 } 392 #ifndef WITH_OPENSSL 393 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 394 if (aadlen) 395 memcpy(dest, src, aadlen); 396 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, 397 dest + aadlen, len); 398 return 0; 399 } 400 if ((cc->cipher->flags & CFLAG_NONE) != 0) { 401 memcpy(dest, src, aadlen + len); 402 return 0; 403 } 404 return SSH_ERR_INVALID_ARGUMENT; 405 #else 406 if (authlen) { 407 u_char lastiv[1]; 408 409 if (authlen != cipher_authlen(cc->cipher)) 410 return SSH_ERR_INVALID_ARGUMENT; 411 /* increment IV */ 412 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN, 413 1, lastiv)) 414 return SSH_ERR_LIBCRYPTO_ERROR; 415 /* set tag on decyption */ 416 if (!cc->encrypt && 417 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG, 418 authlen, (u_char *)src + aadlen + len)) 419 return SSH_ERR_LIBCRYPTO_ERROR; 420 } 421 if (aadlen) { 422 if (authlen && 423 EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0) 424 return SSH_ERR_LIBCRYPTO_ERROR; 425 memcpy(dest, src, aadlen); 426 } 427 if (len % cc->cipher->block_size) 428 return SSH_ERR_INVALID_ARGUMENT; 429 if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen, 430 len) < 0) 431 return SSH_ERR_LIBCRYPTO_ERROR; 432 if (authlen) { 433 /* compute tag (on encrypt) or verify tag (on decrypt) */ 434 if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) 435 return cc->encrypt ? 436 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; 437 if (cc->encrypt && 438 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG, 439 authlen, dest + aadlen + len)) 440 return SSH_ERR_LIBCRYPTO_ERROR; 441 } 442 return 0; 443 #endif 444 } 445 446 /* Extract the packet length, including any decryption necessary beforehand */ 447 int 448 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr, 449 const u_char *cp, u_int len) 450 { 451 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 452 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr, 453 cp, len); 454 if (len < 4) 455 return SSH_ERR_MESSAGE_INCOMPLETE; 456 *plenp = get_u32(cp); 457 return 0; 458 } 459 460 int 461 cipher_cleanup(struct sshcipher_ctx *cc) 462 { 463 if (cc == NULL || cc->cipher == NULL) 464 return 0; 465 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 466 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx)); 467 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) 468 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); 469 #ifdef WITH_OPENSSL 470 else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) 471 return SSH_ERR_LIBCRYPTO_ERROR; 472 #endif 473 return 0; 474 } 475 476 /* 477 * Selects the cipher, and keys if by computing the MD5 checksum of the 478 * passphrase and using the resulting 16 bytes as the key. 479 */ 480 int 481 cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher, 482 const char *passphrase, int do_encrypt) 483 { 484 u_char digest[16]; 485 int r = SSH_ERR_INTERNAL_ERROR; 486 487 if ((r = ssh_digest_memory(SSH_DIGEST_MD5, 488 passphrase, strlen(passphrase), 489 digest, sizeof(digest))) != 0) 490 goto out; 491 492 r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt); 493 out: 494 explicit_bzero(digest, sizeof(digest)); 495 return r; 496 } 497 498 /* 499 * Exports an IV from the sshcipher_ctx required to export the key 500 * state back from the unprivileged child to the privileged parent 501 * process. 502 */ 503 int 504 cipher_get_keyiv_len(const struct sshcipher_ctx *cc) 505 { 506 const struct sshcipher *c = cc->cipher; 507 int ivlen = 0; 508 509 if (c->number == SSH_CIPHER_3DES) 510 ivlen = 24; 511 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 512 ivlen = 0; 513 #ifdef WITH_OPENSSL 514 else 515 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp); 516 #endif /* WITH_OPENSSL */ 517 return (ivlen); 518 } 519 520 int 521 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len) 522 { 523 const struct sshcipher *c = cc->cipher; 524 #ifdef WITH_OPENSSL 525 int evplen; 526 #endif 527 528 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 529 if (len != 0) 530 return SSH_ERR_INVALID_ARGUMENT; 531 return 0; 532 } 533 if ((cc->cipher->flags & CFLAG_NONE) != 0) 534 return 0; 535 536 switch (c->number) { 537 #ifdef WITH_OPENSSL 538 case SSH_CIPHER_SSH2: 539 case SSH_CIPHER_DES: 540 case SSH_CIPHER_BLOWFISH: 541 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 542 if (evplen == 0) 543 return 0; 544 else if (evplen < 0) 545 return SSH_ERR_LIBCRYPTO_ERROR; 546 if ((u_int)evplen != len) 547 return SSH_ERR_INVALID_ARGUMENT; 548 if (cipher_authlen(c)) { 549 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN, 550 len, iv)) 551 return SSH_ERR_LIBCRYPTO_ERROR; 552 } else 553 memcpy(iv, cc->evp.iv, len); 554 break; 555 #endif 556 #ifdef WITH_SSH1 557 case SSH_CIPHER_3DES: 558 return ssh1_3des_iv(&cc->evp, 0, iv, 24); 559 #endif 560 default: 561 return SSH_ERR_INVALID_ARGUMENT; 562 } 563 return 0; 564 } 565 566 int 567 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv) 568 { 569 const struct sshcipher *c = cc->cipher; 570 #ifdef WITH_OPENSSL 571 int evplen = 0; 572 #endif 573 574 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 575 return 0; 576 if ((cc->cipher->flags & CFLAG_NONE) != 0) 577 return 0; 578 579 switch (c->number) { 580 #ifdef WITH_OPENSSL 581 case SSH_CIPHER_SSH2: 582 case SSH_CIPHER_DES: 583 case SSH_CIPHER_BLOWFISH: 584 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); 585 if (evplen <= 0) 586 return SSH_ERR_LIBCRYPTO_ERROR; 587 if (cipher_authlen(c)) { 588 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ 589 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, 590 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv)) 591 return SSH_ERR_LIBCRYPTO_ERROR; 592 } else 593 memcpy(cc->evp.iv, iv, evplen); 594 break; 595 #endif 596 #ifdef WITH_SSH1 597 case SSH_CIPHER_3DES: 598 return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24); 599 #endif 600 default: 601 return SSH_ERR_INVALID_ARGUMENT; 602 } 603 return 0; 604 } 605 606 #ifdef WITH_OPENSSL 607 #define EVP_X_STATE(evp) (evp).cipher_data 608 #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size 609 #endif 610 611 int 612 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat) 613 { 614 #ifdef WITH_OPENSSL 615 const struct sshcipher *c = cc->cipher; 616 int plen = 0; 617 618 if (c->evptype == EVP_rc4) { 619 plen = EVP_X_STATE_LEN(cc->evp); 620 if (dat == NULL) 621 return (plen); 622 memcpy(dat, EVP_X_STATE(cc->evp), plen); 623 } 624 return (plen); 625 #else 626 return 0; 627 #endif 628 } 629 630 void 631 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat) 632 { 633 #ifdef WITH_OPENSSL 634 const struct sshcipher *c = cc->cipher; 635 int plen; 636 637 if (c->evptype == EVP_rc4) { 638 plen = EVP_X_STATE_LEN(cc->evp); 639 memcpy(EVP_X_STATE(cc->evp), dat, plen); 640 } 641 #endif 642 } 643