Annotation of src/usr.bin/ssh/cipher.c, Revision 1.94
1.94 ! dtucker 1: /* $OpenBSD: cipher.c,v 1.93 2013/12/06 13:34:54 markus Exp $ */
1.1 deraadt 2: /*
1.30 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.26 markus 6: *
1.30 deraadt 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".
1.26 markus 12: *
13: *
1.30 deraadt 14: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.46 markus 15: * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
1.26 markus 16: *
1.30 deraadt 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.
1.26 markus 25: *
1.30 deraadt 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.
1.17 deraadt 36: */
1.1 deraadt 37:
1.81 deraadt 38: #include <sys/types.h>
1.1 deraadt 39:
1.80 stevesk 40: #include <openssl/md5.h>
41:
42: #include <string.h>
1.81 deraadt 43: #include <stdarg.h>
1.91 djm 44: #include <stdio.h>
1.80 stevesk 45:
1.24 markus 46: #include "xmalloc.h"
1.42 markus 47: #include "log.h"
1.91 djm 48: #include "misc.h"
1.42 markus 49: #include "cipher.h"
1.57 markus 50:
1.64 markus 51: extern const EVP_CIPHER *evp_ssh1_bf(void);
52: extern const EVP_CIPHER *evp_ssh1_3des(void);
53: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.1 deraadt 54:
1.51 markus 55: struct Cipher {
56: char *name;
57: int number; /* for ssh1 only */
58: u_int block_size;
59: u_int key_len;
1.85 markus 60: u_int iv_len; /* defaults to block_size */
61: u_int auth_len;
1.74 djm 62: u_int discard_len;
1.91 djm 63: u_int flags;
64: #define CFLAG_CBC (1<<0)
65: #define CFLAG_CHACHAPOLY (1<<1)
1.56 markus 66: const EVP_CIPHER *(*evptype)(void);
1.88 djm 67: };
68:
69: static const struct Cipher ciphers[] = {
1.85 markus 70: { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
71: { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
72: { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
73: { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
74:
75: { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
76: { "blowfish-cbc",
77: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
78: { "cast128-cbc",
79: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
80: { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
81: { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
82: { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
83: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
84: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
85: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
1.57 markus 86: { "rijndael-cbc@lysator.liu.se",
1.85 markus 87: SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
88: { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
89: { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
90: { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
91: { "aes128-gcm@openssh.com",
92: SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
93: { "aes256-gcm@openssh.com",
94: SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.91 djm 95: { "chacha20-poly1305@openssh.com",
96: SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
1.47 markus 97:
1.85 markus 98: { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
1.32 markus 99: };
100:
101: /*--*/
1.1 deraadt 102:
1.90 dtucker 103: /* Returns a list of supported ciphers separated by the specified char. */
1.88 djm 104: char *
1.91 djm 105: cipher_alg_list(char sep, int auth_only)
1.88 djm 106: {
107: char *ret = NULL;
108: size_t nlen, rlen = 0;
109: const Cipher *c;
110:
111: for (c = ciphers; c->name != NULL; c++) {
112: if (c->number != SSH_CIPHER_SSH2)
113: continue;
1.91 djm 114: if (auth_only && c->auth_len == 0)
115: continue;
1.88 djm 116: if (ret != NULL)
1.90 dtucker 117: ret[rlen++] = sep;
1.88 djm 118: nlen = strlen(c->name);
119: ret = xrealloc(ret, 1, rlen + nlen + 2);
120: memcpy(ret + rlen, c->name, nlen + 1);
121: rlen += nlen;
122: }
123: return ret;
124: }
125:
1.54 markus 126: u_int
1.66 jakob 127: cipher_blocksize(const Cipher *c)
1.51 markus 128: {
129: return (c->block_size);
130: }
1.60 deraadt 131:
1.54 markus 132: u_int
1.66 jakob 133: cipher_keylen(const Cipher *c)
1.51 markus 134: {
135: return (c->key_len);
1.94 ! dtucker 136: }
! 137:
! 138: u_int
! 139: cipher_seclen(const Cipher *c)
! 140: {
! 141: if (strcmp("3des-cbc", c->name) == 0)
! 142: return 14;
! 143: return cipher_keylen(c);
1.51 markus 144: }
1.60 deraadt 145:
1.54 markus 146: u_int
1.85 markus 147: cipher_authlen(const Cipher *c)
148: {
149: return (c->auth_len);
150: }
151:
152: u_int
153: cipher_ivlen(const Cipher *c)
154: {
1.91 djm 155: /*
156: * Default is cipher block size, except for chacha20+poly1305 that
157: * needs no IV. XXX make iv_len == -1 default?
158: */
159: return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
160: c->iv_len : c->block_size;
1.85 markus 161: }
162:
163: u_int
1.66 jakob 164: cipher_get_number(const Cipher *c)
1.53 markus 165: {
166: return (c->number);
1.82 markus 167: }
168:
169: u_int
170: cipher_is_cbc(const Cipher *c)
171: {
1.91 djm 172: return (c->flags & CFLAG_CBC) != 0;
1.53 markus 173: }
1.51 markus 174:
1.41 markus 175: u_int
1.34 markus 176: cipher_mask_ssh1(int client)
1.1 deraadt 177: {
1.41 markus 178: u_int mask = 0;
1.48 deraadt 179: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 180: mask |= 1 << SSH_CIPHER_BLOWFISH;
181: if (client) {
182: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 183: }
184: return mask;
1.1 deraadt 185: }
186:
1.88 djm 187: const Cipher *
1.32 markus 188: cipher_by_name(const char *name)
189: {
1.88 djm 190: const Cipher *c;
1.32 markus 191: for (c = ciphers; c->name != NULL; c++)
1.73 djm 192: if (strcmp(c->name, name) == 0)
1.32 markus 193: return c;
194: return NULL;
195: }
196:
1.88 djm 197: const Cipher *
1.32 markus 198: cipher_by_number(int id)
199: {
1.88 djm 200: const Cipher *c;
1.32 markus 201: for (c = ciphers; c->name != NULL; c++)
202: if (c->number == id)
203: return c;
204: return NULL;
205: }
1.24 markus 206:
207: #define CIPHER_SEP ","
208: int
209: ciphers_valid(const char *names)
210: {
1.88 djm 211: const Cipher *c;
1.69 avsm 212: char *cipher_list, *cp;
1.24 markus 213: char *p;
214:
1.27 markus 215: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 216: return 0;
1.69 avsm 217: cipher_list = cp = xstrdup(names);
1.32 markus 218: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 219: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 220: c = cipher_by_name(p);
221: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
222: debug("bad cipher %s [%s]", p, names);
1.89 djm 223: free(cipher_list);
1.24 markus 224: return 0;
1.32 markus 225: } else {
1.36 markus 226: debug3("cipher ok: %s [%s]", p, names);
1.24 markus 227: }
228: }
1.36 markus 229: debug3("ciphers ok: [%s]", names);
1.89 djm 230: free(cipher_list);
1.24 markus 231: return 1;
232: }
233:
1.18 markus 234: /*
235: * Parses the name of the cipher. Returns the number of the corresponding
236: * cipher, or -1 on error.
237: */
1.1 deraadt 238:
1.4 provos 239: int
240: cipher_number(const char *name)
1.1 deraadt 241: {
1.88 djm 242: const Cipher *c;
1.27 markus 243: if (name == NULL)
244: return -1;
1.73 djm 245: for (c = ciphers; c->name != NULL; c++)
246: if (strcasecmp(c->name, name) == 0)
247: return c->number;
248: return -1;
1.1 deraadt 249: }
250:
1.32 markus 251: char *
252: cipher_name(int id)
1.1 deraadt 253: {
1.88 djm 254: const Cipher *c = cipher_by_number(id);
1.32 markus 255: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 256: }
257:
1.26 markus 258: void
1.88 djm 259: cipher_init(CipherContext *cc, const Cipher *cipher,
1.52 markus 260: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69 avsm 261: int do_encrypt)
1.16 markus 262: {
1.52 markus 263: static int dowarn = 1;
264: const EVP_CIPHER *type;
265: int klen;
1.74 djm 266: u_char *junk, *discard;
1.52 markus 267:
268: if (cipher->number == SSH_CIPHER_DES) {
269: if (dowarn) {
270: error("Warning: use of DES is strongly discouraged "
271: "due to cryptographic weaknesses");
272: dowarn = 0;
273: }
274: if (keylen > 8)
275: keylen = 8;
276: }
277: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
1.85 markus 278: cc->encrypt = do_encrypt;
1.52 markus 279:
1.32 markus 280: if (keylen < cipher->key_len)
281: fatal("cipher_init: key length %d is insufficient for %s.",
282: keylen, cipher->name);
1.85 markus 283: if (iv != NULL && ivlen < cipher_ivlen(cipher))
1.32 markus 284: fatal("cipher_init: iv length %d is insufficient for %s.",
285: ivlen, cipher->name);
286: cc->cipher = cipher;
1.52 markus 287:
1.91 djm 288: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
289: chachapoly_init(&cc->cp_ctx, key, keylen);
290: return;
291: }
1.52 markus 292: type = (*cipher->evptype)();
293: EVP_CIPHER_CTX_init(&cc->evp);
294: if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69 avsm 295: (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52 markus 296: fatal("cipher_init: EVP_CipherInit failed for %s",
297: cipher->name);
1.85 markus 298: if (cipher_authlen(cipher) &&
299: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
300: -1, (u_char *)iv))
301: fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
302: cipher->name);
1.52 markus 303: klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.76 djm 304: if (klen > 0 && keylen != (u_int)klen) {
1.62 markus 305: debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52 markus 306: if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
307: fatal("cipher_init: set keylen failed (%d -> %d)",
308: klen, keylen);
309: }
310: if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
311: fatal("cipher_init: EVP_CipherInit: set key failed for %s",
312: cipher->name);
1.74 djm 313:
1.77 djm 314: if (cipher->discard_len > 0) {
1.74 djm 315: junk = xmalloc(cipher->discard_len);
316: discard = xmalloc(cipher->discard_len);
317: if (EVP_Cipher(&cc->evp, discard, junk,
318: cipher->discard_len) == 0)
319: fatal("evp_crypt: EVP_Cipher failed during discard");
320: memset(discard, 0, cipher->discard_len);
1.89 djm 321: free(junk);
322: free(discard);
1.74 djm 323: }
1.1 deraadt 324: }
1.21 markus 325:
1.83 markus 326: /*
327: * cipher_crypt() operates as following:
328: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
329: * Theses bytes are treated as additional authenticated data for
330: * authenticated encryption modes.
331: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 markus 332: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
333: * This tag is written on encryption and verified on decryption.
1.83 markus 334: * Both 'aadlen' and 'authlen' can be set to 0.
1.93 markus 335: * cipher_crypt() returns 0 on success and -1 if the decryption integrity
336: * check fails.
1.83 markus 337: */
1.93 markus 338: int
1.91 djm 339: cipher_crypt(CipherContext *cc, u_int seqnr, u_char *dest, const u_char *src,
1.85 markus 340: u_int len, u_int aadlen, u_int authlen)
1.32 markus 341: {
1.93 markus 342: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
343: return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, len,
344: aadlen, authlen, cc->encrypt);
1.85 markus 345: if (authlen) {
346: u_char lastiv[1];
347:
348: if (authlen != cipher_authlen(cc->cipher))
349: fatal("%s: authlen mismatch %d", __func__, authlen);
350: /* increment IV */
351: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
352: 1, lastiv))
353: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
354: /* set tag on decyption */
355: if (!cc->encrypt &&
356: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
357: authlen, (u_char *)src + aadlen + len))
358: fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
359: }
360: if (aadlen) {
361: if (authlen &&
362: EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
363: fatal("%s: EVP_Cipher(aad) failed", __func__);
1.83 markus 364: memcpy(dest, src, aadlen);
1.85 markus 365: }
1.32 markus 366: if (len % cc->cipher->block_size)
1.83 markus 367: fatal("%s: bad plaintext length %d", __func__, len);
368: if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
369: len) < 0)
370: fatal("%s: EVP_Cipher failed", __func__);
1.85 markus 371: if (authlen) {
372: /* compute tag (on encrypt) or verify tag (on decrypt) */
1.86 djm 373: if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
374: if (cc->encrypt)
375: fatal("%s: EVP_Cipher(final) failed", __func__);
376: else
1.93 markus 377: return -1;
1.86 djm 378: }
1.85 markus 379: if (cc->encrypt &&
380: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
381: authlen, dest + aadlen + len))
382: fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
383: }
1.93 markus 384: return 0;
1.21 markus 385: }
386:
1.91 djm 387: /* Extract the packet length, including any decryption necessary beforehand */
388: int
389: cipher_get_length(CipherContext *cc, u_int *plenp, u_int seqnr,
390: const u_char *cp, u_int len)
391: {
392: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
393: return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
394: cp, len);
395: if (len < 4)
396: return -1;
397: *plenp = get_u32(cp);
398: return 0;
399: }
400:
1.26 markus 401: void
1.51 markus 402: cipher_cleanup(CipherContext *cc)
1.16 markus 403: {
1.91 djm 404: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.92 djm 405: memset(&cc->cp_ctx, 0, sizeof(cc->cp_ctx));
1.91 djm 406: else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
1.52 markus 407: error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16 markus 408: }
1.1 deraadt 409:
1.32 markus 410: /*
411: * Selects the cipher, and keys if by computing the MD5 checksum of the
412: * passphrase and using the resulting 16 bytes as the key.
413: */
1.1 deraadt 414:
1.26 markus 415: void
1.88 djm 416: cipher_set_key_string(CipherContext *cc, const Cipher *cipher,
1.69 avsm 417: const char *passphrase, int do_encrypt)
1.16 markus 418: {
1.32 markus 419: MD5_CTX md;
1.41 markus 420: u_char digest[16];
1.32 markus 421:
422: MD5_Init(&md);
423: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
424: MD5_Final(digest, &md);
1.16 markus 425:
1.69 avsm 426: cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16 markus 427:
1.32 markus 428: memset(digest, 0, sizeof(digest));
429: memset(&md, 0, sizeof(md));
1.52 markus 430: }
1.53 markus 431:
1.54 markus 432: /*
1.53 markus 433: * Exports an IV from the CipherContext required to export the key
434: * state back from the unprivileged child to the privileged parent
435: * process.
436: */
437:
438: int
1.66 jakob 439: cipher_get_keyiv_len(const CipherContext *cc)
1.53 markus 440: {
1.88 djm 441: const Cipher *c = cc->cipher;
1.53 markus 442: int ivlen;
443:
444: if (c->number == SSH_CIPHER_3DES)
445: ivlen = 24;
1.91 djm 446: else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
447: ivlen = 0;
1.53 markus 448: else
449: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
450: return (ivlen);
451: }
452:
453: void
454: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
455: {
1.88 djm 456: const Cipher *c = cc->cipher;
1.53 markus 457: int evplen;
458:
1.91 djm 459: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
460: if (len != 0)
461: fatal("%s: wrong iv length %d != %d", __func__, len, 0);
462: return;
463: }
464:
1.53 markus 465: switch (c->number) {
466: case SSH_CIPHER_SSH2:
467: case SSH_CIPHER_DES:
468: case SSH_CIPHER_BLOWFISH:
469: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76 djm 470: if (evplen <= 0)
1.53 markus 471: return;
1.76 djm 472: if ((u_int)evplen != len)
1.58 markus 473: fatal("%s: wrong iv length %d != %d", __func__,
1.53 markus 474: evplen, len);
1.85 markus 475: if (cipher_authlen(c)) {
476: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
477: len, iv))
478: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
479: } else
480: memcpy(iv, cc->evp.iv, len);
1.63 markus 481: break;
482: case SSH_CIPHER_3DES:
483: ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53 markus 484: break;
485: default:
1.58 markus 486: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 487: }
488: }
489:
490: void
491: cipher_set_keyiv(CipherContext *cc, u_char *iv)
492: {
1.88 djm 493: const Cipher *c = cc->cipher;
1.53 markus 494: int evplen = 0;
1.91 djm 495:
496: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
497: return;
1.53 markus 498:
499: switch (c->number) {
500: case SSH_CIPHER_SSH2:
501: case SSH_CIPHER_DES:
502: case SSH_CIPHER_BLOWFISH:
503: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
504: if (evplen == 0)
505: return;
1.85 markus 506: if (cipher_authlen(c)) {
507: if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
508: EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
509: fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
510: __func__);
511: } else
512: memcpy(cc->evp.iv, iv, evplen);
1.63 markus 513: break;
514: case SSH_CIPHER_3DES:
515: ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53 markus 516: break;
517: default:
1.58 markus 518: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 519: }
520: }
521:
522: #define EVP_X_STATE(evp) (evp).cipher_data
523: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
524:
525: int
1.66 jakob 526: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53 markus 527: {
1.88 djm 528: const Cipher *c = cc->cipher;
1.59 markus 529: int plen = 0;
1.53 markus 530:
1.87 djm 531: if (c->evptype == EVP_rc4) {
1.59 markus 532: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 533: if (dat == NULL)
1.59 markus 534: return (plen);
535: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 536: }
537: return (plen);
538: }
539:
540: void
541: cipher_set_keycontext(CipherContext *cc, u_char *dat)
542: {
1.88 djm 543: const Cipher *c = cc->cipher;
1.53 markus 544: int plen;
545:
1.87 djm 546: if (c->evptype == EVP_rc4) {
1.53 markus 547: plen = EVP_X_STATE_LEN(cc->evp);
548: memcpy(EVP_X_STATE(cc->evp), dat, plen);
549: }
1.1 deraadt 550: }