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