Annotation of src/usr.bin/ssh/cipher.c, Revision 1.107
1.107 ! djm 1: /* $OpenBSD: cipher.c,v 1.106 2017/05/04 01:33:21 djm 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.99 djm 44: #include "cipher.h"
1.91 djm 45: #include "misc.h"
1.99 djm 46: #include "sshbuf.h"
47: #include "ssherr.h"
1.95 markus 48: #include "digest.h"
1.57 markus 49:
1.1 deraadt 50:
1.102 djm 51: struct sshcipher_ctx {
52: int plaintext;
53: int encrypt;
54: EVP_CIPHER_CTX *evp;
55: struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
56: struct aesctr_ctx ac_ctx; /* XXX union with evp? */
57: const struct sshcipher *cipher;
58: };
59:
1.99 djm 60: struct sshcipher {
1.51 markus 61: char *name;
62: u_int block_size;
63: u_int key_len;
1.85 markus 64: u_int iv_len; /* defaults to block_size */
65: u_int auth_len;
1.91 djm 66: u_int flags;
67: #define CFLAG_CBC (1<<0)
68: #define CFLAG_CHACHAPOLY (1<<1)
1.98 markus 69: #define CFLAG_AESCTR (1<<2)
70: #define CFLAG_NONE (1<<3)
1.104 djm 71: #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
1.98 markus 72: #ifdef WITH_OPENSSL
1.56 markus 73: const EVP_CIPHER *(*evptype)(void);
1.98 markus 74: #else
75: void *ignored;
76: #endif
1.88 djm 77: };
78:
1.99 djm 79: static const struct sshcipher ciphers[] = {
1.98 markus 80: #ifdef WITH_OPENSSL
1.107 ! djm 81: { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
! 82: { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
! 83: { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
! 84: { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
1.57 markus 85: { "rijndael-cbc@lysator.liu.se",
1.107 ! djm 86: 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
! 87: { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
! 88: { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
! 89: { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
1.85 markus 90: { "aes128-gcm@openssh.com",
1.107 ! djm 91: 16, 16, 12, 16, 0, EVP_aes_128_gcm },
1.85 markus 92: { "aes256-gcm@openssh.com",
1.107 ! djm 93: 16, 32, 12, 16, 0, EVP_aes_256_gcm },
1.98 markus 94: #else
1.107 ! djm 95: { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
! 96: { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
! 97: { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
1.98 markus 98: #endif
1.91 djm 99: { "chacha20-poly1305@openssh.com",
1.107 ! djm 100: 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
! 101: { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
1.47 markus 102:
1.107 ! djm 103: { NULL, 0, 0, 0, 0, 0, NULL }
1.32 markus 104: };
105:
106: /*--*/
1.1 deraadt 107:
1.99 djm 108: /* Returns a comma-separated list of supported ciphers. */
1.88 djm 109: char *
1.91 djm 110: cipher_alg_list(char sep, int auth_only)
1.88 djm 111: {
1.99 djm 112: char *tmp, *ret = NULL;
1.88 djm 113: size_t nlen, rlen = 0;
1.99 djm 114: const struct sshcipher *c;
1.88 djm 115:
116: for (c = ciphers; c->name != NULL; c++) {
1.104 djm 117: if ((c->flags & CFLAG_INTERNAL) != 0)
1.88 djm 118: continue;
1.91 djm 119: if (auth_only && c->auth_len == 0)
120: continue;
1.88 djm 121: if (ret != NULL)
1.90 dtucker 122: ret[rlen++] = sep;
1.88 djm 123: nlen = strlen(c->name);
1.99 djm 124: if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
125: free(ret);
126: return NULL;
127: }
128: ret = tmp;
1.88 djm 129: memcpy(ret + rlen, c->name, nlen + 1);
130: rlen += nlen;
131: }
132: return ret;
133: }
134:
1.54 markus 135: u_int
1.99 djm 136: cipher_blocksize(const struct sshcipher *c)
1.51 markus 137: {
138: return (c->block_size);
139: }
1.60 deraadt 140:
1.54 markus 141: u_int
1.99 djm 142: cipher_keylen(const struct sshcipher *c)
1.51 markus 143: {
144: return (c->key_len);
1.94 dtucker 145: }
146:
147: u_int
1.99 djm 148: cipher_seclen(const struct sshcipher *c)
1.94 dtucker 149: {
150: if (strcmp("3des-cbc", c->name) == 0)
151: return 14;
152: return cipher_keylen(c);
1.51 markus 153: }
1.60 deraadt 154:
1.54 markus 155: u_int
1.99 djm 156: cipher_authlen(const struct sshcipher *c)
1.85 markus 157: {
158: return (c->auth_len);
159: }
160:
161: u_int
1.99 djm 162: cipher_ivlen(const struct sshcipher *c)
1.85 markus 163: {
1.91 djm 164: /*
165: * Default is cipher block size, except for chacha20+poly1305 that
166: * needs no IV. XXX make iv_len == -1 default?
167: */
168: return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
169: c->iv_len : c->block_size;
1.85 markus 170: }
171:
172: u_int
1.99 djm 173: cipher_is_cbc(const struct sshcipher *c)
1.82 markus 174: {
1.91 djm 175: return (c->flags & CFLAG_CBC) != 0;
1.53 markus 176: }
1.51 markus 177:
1.41 markus 178: u_int
1.102 djm 179: cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
180: {
181: return cc->plaintext;
182: }
183:
1.99 djm 184: const struct sshcipher *
1.32 markus 185: cipher_by_name(const char *name)
186: {
1.99 djm 187: const struct sshcipher *c;
1.32 markus 188: for (c = ciphers; c->name != NULL; c++)
1.73 djm 189: if (strcmp(c->name, name) == 0)
1.32 markus 190: return c;
191: return NULL;
192: }
193:
1.24 markus 194: #define CIPHER_SEP ","
195: int
196: ciphers_valid(const char *names)
197: {
1.99 djm 198: const struct sshcipher *c;
1.69 avsm 199: char *cipher_list, *cp;
1.24 markus 200: char *p;
201:
1.27 markus 202: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 203: return 0;
1.99 djm 204: if ((cipher_list = cp = strdup(names)) == NULL)
205: return 0;
1.32 markus 206: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 207: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 208: c = cipher_by_name(p);
1.104 djm 209: if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
1.89 djm 210: free(cipher_list);
1.24 markus 211: return 0;
212: }
213: }
1.89 djm 214: free(cipher_list);
1.24 markus 215: return 1;
216: }
217:
1.99 djm 218: const char *
219: cipher_warning_message(const struct sshcipher_ctx *cc)
220: {
221: if (cc == NULL || cc->cipher == NULL)
222: return NULL;
1.104 djm 223: /* XXX repurpose for CBC warning */
1.99 djm 224: return NULL;
225: }
226:
227: int
1.102 djm 228: cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
1.52 markus 229: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69 avsm 230: int do_encrypt)
1.16 markus 231: {
1.102 djm 232: struct sshcipher_ctx *cc = NULL;
233: int ret = SSH_ERR_INTERNAL_ERROR;
1.98 markus 234: #ifdef WITH_OPENSSL
1.52 markus 235: const EVP_CIPHER *type;
236: int klen;
1.102 djm 237: #endif
238:
239: *ccp = NULL;
240: if ((cc = calloc(sizeof(*cc), 1)) == NULL)
241: return SSH_ERR_ALLOC_FAIL;
1.52 markus 242:
1.105 djm 243: cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
1.85 markus 244: cc->encrypt = do_encrypt;
1.52 markus 245:
1.99 djm 246: if (keylen < cipher->key_len ||
1.102 djm 247: (iv != NULL && ivlen < cipher_ivlen(cipher))) {
248: ret = SSH_ERR_INVALID_ARGUMENT;
249: goto out;
250: }
1.99 djm 251:
1.32 markus 252: cc->cipher = cipher;
1.91 djm 253: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
1.102 djm 254: ret = chachapoly_init(&cc->cp_ctx, key, keylen);
255: goto out;
1.91 djm 256: }
1.104 djm 257: if ((cc->cipher->flags & CFLAG_NONE) != 0) {
258: ret = 0;
259: goto out;
260: }
1.98 markus 261: #ifndef WITH_OPENSSL
262: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
263: aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
264: aesctr_ivsetup(&cc->ac_ctx, iv);
1.102 djm 265: ret = 0;
266: goto out;
267: }
268: ret = SSH_ERR_INVALID_ARGUMENT;
269: goto out;
270: #else /* WITH_OPENSSL */
1.52 markus 271: type = (*cipher->evptype)();
1.102 djm 272: if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
273: ret = SSH_ERR_ALLOC_FAIL;
274: goto out;
275: }
276: if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
1.99 djm 277: (do_encrypt == CIPHER_ENCRYPT)) == 0) {
278: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 279: goto out;
1.99 djm 280: }
1.85 markus 281: if (cipher_authlen(cipher) &&
1.102 djm 282: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
1.99 djm 283: -1, (u_char *)iv)) {
284: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 285: goto out;
1.99 djm 286: }
1.102 djm 287: klen = EVP_CIPHER_CTX_key_length(cc->evp);
1.76 djm 288: if (klen > 0 && keylen != (u_int)klen) {
1.102 djm 289: if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
1.99 djm 290: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 291: goto out;
1.99 djm 292: }
293: }
1.102 djm 294: if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
1.99 djm 295: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 296: goto out;
1.99 djm 297: }
1.102 djm 298: ret = 0;
299: #endif /* WITH_OPENSSL */
300: out:
301: if (ret == 0) {
302: /* success */
303: *ccp = cc;
304: } else {
305: if (cc != NULL) {
306: #ifdef WITH_OPENSSL
307: if (cc->evp != NULL)
308: EVP_CIPHER_CTX_free(cc->evp);
309: #endif /* WITH_OPENSSL */
310: explicit_bzero(cc, sizeof(*cc));
311: free(cc);
1.99 djm 312: }
1.74 djm 313: }
1.102 djm 314: return ret;
1.1 deraadt 315: }
1.21 markus 316:
1.83 markus 317: /*
318: * cipher_crypt() operates as following:
319: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
320: * Theses bytes are treated as additional authenticated data for
321: * authenticated encryption modes.
322: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 markus 323: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
324: * This tag is written on encryption and verified on decryption.
1.83 markus 325: * Both 'aadlen' and 'authlen' can be set to 0.
326: */
1.93 markus 327: int
1.99 djm 328: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
329: const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32 markus 330: {
1.99 djm 331: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
332: return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
333: len, aadlen, authlen, cc->encrypt);
334: }
1.104 djm 335: if ((cc->cipher->flags & CFLAG_NONE) != 0) {
336: memcpy(dest, src, aadlen + len);
337: return 0;
338: }
1.98 markus 339: #ifndef WITH_OPENSSL
340: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
341: if (aadlen)
342: memcpy(dest, src, aadlen);
343: aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
344: dest + aadlen, len);
345: return 0;
346: }
1.99 djm 347: return SSH_ERR_INVALID_ARGUMENT;
1.98 markus 348: #else
1.85 markus 349: if (authlen) {
350: u_char lastiv[1];
351:
352: if (authlen != cipher_authlen(cc->cipher))
1.99 djm 353: return SSH_ERR_INVALID_ARGUMENT;
1.85 markus 354: /* increment IV */
1.102 djm 355: if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85 markus 356: 1, lastiv))
1.99 djm 357: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 358: /* set tag on decyption */
359: if (!cc->encrypt &&
1.102 djm 360: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
1.85 markus 361: authlen, (u_char *)src + aadlen + len))
1.99 djm 362: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 363: }
364: if (aadlen) {
365: if (authlen &&
1.102 djm 366: EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99 djm 367: return SSH_ERR_LIBCRYPTO_ERROR;
1.83 markus 368: memcpy(dest, src, aadlen);
1.85 markus 369: }
1.32 markus 370: if (len % cc->cipher->block_size)
1.99 djm 371: return SSH_ERR_INVALID_ARGUMENT;
1.102 djm 372: if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
1.83 markus 373: len) < 0)
1.99 djm 374: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 375: if (authlen) {
376: /* compute tag (on encrypt) or verify tag (on decrypt) */
1.102 djm 377: if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
1.99 djm 378: return cc->encrypt ?
379: SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85 markus 380: if (cc->encrypt &&
1.102 djm 381: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
1.85 markus 382: authlen, dest + aadlen + len))
1.99 djm 383: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 384: }
1.93 markus 385: return 0;
1.98 markus 386: #endif
1.21 markus 387: }
388:
1.91 djm 389: /* Extract the packet length, including any decryption necessary beforehand */
390: int
1.99 djm 391: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91 djm 392: const u_char *cp, u_int len)
393: {
394: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
395: return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
396: cp, len);
397: if (len < 4)
1.99 djm 398: return SSH_ERR_MESSAGE_INCOMPLETE;
1.91 djm 399: *plenp = get_u32(cp);
400: return 0;
401: }
402:
1.102 djm 403: void
404: cipher_free(struct sshcipher_ctx *cc)
1.16 markus 405: {
1.102 djm 406: if (cc == NULL)
407: return;
1.91 djm 408: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96 djm 409: explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98 markus 410: else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
411: explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
412: #ifdef WITH_OPENSSL
1.102 djm 413: if (cc->evp != NULL) {
414: EVP_CIPHER_CTX_free(cc->evp);
415: cc->evp = NULL;
416: }
1.98 markus 417: #endif
1.102 djm 418: explicit_bzero(cc, sizeof(*cc));
419: free(cc);
1.52 markus 420: }
1.53 markus 421:
1.54 markus 422: /*
1.99 djm 423: * Exports an IV from the sshcipher_ctx required to export the key
1.53 markus 424: * state back from the unprivileged child to the privileged parent
425: * process.
426: */
427: int
1.99 djm 428: cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
1.53 markus 429: {
1.99 djm 430: const struct sshcipher *c = cc->cipher;
1.53 markus 431:
1.104 djm 432: if ((c->flags & CFLAG_CHACHAPOLY) != 0)
433: return 0;
434: else if ((c->flags & CFLAG_AESCTR) != 0)
435: return sizeof(cc->ac_ctx.ctr);
1.98 markus 436: #ifdef WITH_OPENSSL
1.104 djm 437: return EVP_CIPHER_CTX_iv_length(cc->evp);
438: #else
439: return 0;
440: #endif
1.53 markus 441: }
442:
1.99 djm 443: int
444: cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
1.53 markus 445: {
1.99 djm 446: const struct sshcipher *c = cc->cipher;
1.98 markus 447: #ifdef WITH_OPENSSL
1.99 djm 448: int evplen;
1.98 markus 449: #endif
1.53 markus 450:
1.91 djm 451: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
452: if (len != 0)
1.99 djm 453: return SSH_ERR_INVALID_ARGUMENT;
1.100 djm 454: return 0;
455: }
456: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
457: if (len != sizeof(cc->ac_ctx.ctr))
458: return SSH_ERR_INVALID_ARGUMENT;
459: memcpy(iv, cc->ac_ctx.ctr, len);
1.99 djm 460: return 0;
1.91 djm 461: }
1.98 markus 462: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 463: return 0;
1.91 djm 464:
1.98 markus 465: #ifdef WITH_OPENSSL
1.104 djm 466: evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
467: if (evplen == 0)
468: return 0;
469: else if (evplen < 0)
470: return SSH_ERR_LIBCRYPTO_ERROR;
471: if ((u_int)evplen != len)
472: return SSH_ERR_INVALID_ARGUMENT;
473: if (cipher_authlen(c)) {
474: if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
475: len, iv))
476: return SSH_ERR_LIBCRYPTO_ERROR;
477: } else
478: memcpy(iv, cc->evp->iv, len);
1.98 markus 479: #endif
1.99 djm 480: return 0;
1.53 markus 481: }
482:
1.99 djm 483: int
484: cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
1.53 markus 485: {
1.99 djm 486: const struct sshcipher *c = cc->cipher;
1.98 markus 487: #ifdef WITH_OPENSSL
1.99 djm 488: int evplen = 0;
1.98 markus 489: #endif
1.91 djm 490:
491: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.99 djm 492: return 0;
1.98 markus 493: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 494: return 0;
1.53 markus 495:
1.98 markus 496: #ifdef WITH_OPENSSL
1.104 djm 497: evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
498: if (evplen <= 0)
499: return SSH_ERR_LIBCRYPTO_ERROR;
500: if (cipher_authlen(c)) {
501: /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
502: if (!EVP_CIPHER_CTX_ctrl(cc->evp,
503: EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
1.99 djm 504: return SSH_ERR_LIBCRYPTO_ERROR;
1.104 djm 505: } else
506: memcpy(cc->evp->iv, iv, evplen);
1.98 markus 507: #endif
1.99 djm 508: return 0;
1.53 markus 509: }
510: