Annotation of src/usr.bin/ssh/cipher.c, Revision 1.96
1.96 ! djm 1: /* $OpenBSD: cipher.c,v 1.95 2014/01/27 19:18: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 <string.h>
1.81 deraadt 41: #include <stdarg.h>
1.91 djm 42: #include <stdio.h>
1.80 stevesk 43:
1.24 markus 44: #include "xmalloc.h"
1.42 markus 45: #include "log.h"
1.91 djm 46: #include "misc.h"
1.42 markus 47: #include "cipher.h"
1.95 markus 48: #include "buffer.h"
49: #include "digest.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");
1.96 ! djm 320: explicit_bzero(discard, 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.96 ! djm 405: explicit_bzero(&cc->cp_ctx, 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.41 markus 419: u_char digest[16];
1.32 markus 420:
1.95 markus 421: if (ssh_digest_memory(SSH_DIGEST_MD5, passphrase, strlen(passphrase),
422: digest, sizeof(digest)) < 0)
423: fatal("%s: md5 failed", __func__);
1.16 markus 424:
1.69 avsm 425: cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16 markus 426:
1.96 ! djm 427: explicit_bzero(digest, sizeof(digest));
1.52 markus 428: }
1.53 markus 429:
1.54 markus 430: /*
1.53 markus 431: * Exports an IV from the CipherContext required to export the key
432: * state back from the unprivileged child to the privileged parent
433: * process.
434: */
435:
436: int
1.66 jakob 437: cipher_get_keyiv_len(const CipherContext *cc)
1.53 markus 438: {
1.88 djm 439: const Cipher *c = cc->cipher;
1.53 markus 440: int ivlen;
441:
442: if (c->number == SSH_CIPHER_3DES)
443: ivlen = 24;
1.91 djm 444: else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
445: ivlen = 0;
1.53 markus 446: else
447: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
448: return (ivlen);
449: }
450:
451: void
452: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
453: {
1.88 djm 454: const Cipher *c = cc->cipher;
1.53 markus 455: int evplen;
456:
1.91 djm 457: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
458: if (len != 0)
459: fatal("%s: wrong iv length %d != %d", __func__, len, 0);
460: return;
461: }
462:
1.53 markus 463: switch (c->number) {
464: case SSH_CIPHER_SSH2:
465: case SSH_CIPHER_DES:
466: case SSH_CIPHER_BLOWFISH:
467: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76 djm 468: if (evplen <= 0)
1.53 markus 469: return;
1.76 djm 470: if ((u_int)evplen != len)
1.58 markus 471: fatal("%s: wrong iv length %d != %d", __func__,
1.53 markus 472: evplen, len);
1.85 markus 473: if (cipher_authlen(c)) {
474: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
475: len, iv))
476: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
477: } else
478: memcpy(iv, cc->evp.iv, len);
1.63 markus 479: break;
480: case SSH_CIPHER_3DES:
481: ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53 markus 482: break;
483: default:
1.58 markus 484: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 485: }
486: }
487:
488: void
489: cipher_set_keyiv(CipherContext *cc, u_char *iv)
490: {
1.88 djm 491: const Cipher *c = cc->cipher;
1.53 markus 492: int evplen = 0;
1.91 djm 493:
494: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
495: return;
1.53 markus 496:
497: switch (c->number) {
498: case SSH_CIPHER_SSH2:
499: case SSH_CIPHER_DES:
500: case SSH_CIPHER_BLOWFISH:
501: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
502: if (evplen == 0)
503: return;
1.85 markus 504: if (cipher_authlen(c)) {
505: if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
506: EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
507: fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
508: __func__);
509: } else
510: memcpy(cc->evp.iv, iv, evplen);
1.63 markus 511: break;
512: case SSH_CIPHER_3DES:
513: ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53 markus 514: break;
515: default:
1.58 markus 516: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 517: }
518: }
519:
520: #define EVP_X_STATE(evp) (evp).cipher_data
521: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
522:
523: int
1.66 jakob 524: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53 markus 525: {
1.88 djm 526: const Cipher *c = cc->cipher;
1.59 markus 527: int plen = 0;
1.53 markus 528:
1.87 djm 529: if (c->evptype == EVP_rc4) {
1.59 markus 530: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 531: if (dat == NULL)
1.59 markus 532: return (plen);
533: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 534: }
535: return (plen);
536: }
537:
538: void
539: cipher_set_keycontext(CipherContext *cc, u_char *dat)
540: {
1.88 djm 541: const Cipher *c = cc->cipher;
1.53 markus 542: int plen;
543:
1.87 djm 544: if (c->evptype == EVP_rc4) {
1.53 markus 545: plen = EVP_X_STATE_LEN(cc->evp);
546: memcpy(EVP_X_STATE(cc->evp), dat, plen);
547: }
1.1 deraadt 548: }