[BACK]Return to cipher.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/cipher.c, Revision 1.110

1.110   ! djm         1: /* $OpenBSD: cipher.c,v 1.109 2018/02/07 02:06:50 jsing 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
1.109     jsing     307:                        EVP_CIPHER_CTX_free(cc->evp);
1.102     djm       308: #endif /* WITH_OPENSSL */
                    309:                        explicit_bzero(cc, sizeof(*cc));
                    310:                        free(cc);
1.99      djm       311:                }
1.74      djm       312:        }
1.102     djm       313:        return ret;
1.1       deraadt   314: }
1.21      markus    315:
1.83      markus    316: /*
                    317:  * cipher_crypt() operates as following:
                    318:  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
                    319:  * Theses bytes are treated as additional authenticated data for
                    320:  * authenticated encryption modes.
                    321:  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85      markus    322:  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
                    323:  * This tag is written on encryption and verified on decryption.
1.83      markus    324:  * Both 'aadlen' and 'authlen' can be set to 0.
                    325:  */
1.93      markus    326: int
1.99      djm       327: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
                    328:    const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32      markus    329: {
1.99      djm       330:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    331:                return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
                    332:                    len, aadlen, authlen, cc->encrypt);
                    333:        }
1.104     djm       334:        if ((cc->cipher->flags & CFLAG_NONE) != 0) {
                    335:                memcpy(dest, src, aadlen + len);
                    336:                return 0;
                    337:        }
1.98      markus    338: #ifndef WITH_OPENSSL
                    339:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    340:                if (aadlen)
                    341:                        memcpy(dest, src, aadlen);
                    342:                aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
                    343:                    dest + aadlen, len);
                    344:                return 0;
                    345:        }
1.99      djm       346:        return SSH_ERR_INVALID_ARGUMENT;
1.98      markus    347: #else
1.85      markus    348:        if (authlen) {
                    349:                u_char lastiv[1];
                    350:
                    351:                if (authlen != cipher_authlen(cc->cipher))
1.99      djm       352:                        return SSH_ERR_INVALID_ARGUMENT;
1.85      markus    353:                /* increment IV */
1.102     djm       354:                if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85      markus    355:                    1, lastiv))
1.99      djm       356:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    357:                /* set tag on decyption */
                    358:                if (!cc->encrypt &&
1.102     djm       359:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
1.85      markus    360:                    authlen, (u_char *)src + aadlen + len))
1.99      djm       361:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    362:        }
                    363:        if (aadlen) {
                    364:                if (authlen &&
1.102     djm       365:                    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99      djm       366:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.83      markus    367:                memcpy(dest, src, aadlen);
1.85      markus    368:        }
1.32      markus    369:        if (len % cc->cipher->block_size)
1.99      djm       370:                return SSH_ERR_INVALID_ARGUMENT;
1.102     djm       371:        if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
1.83      markus    372:            len) < 0)
1.99      djm       373:                return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    374:        if (authlen) {
                    375:                /* compute tag (on encrypt) or verify tag (on decrypt) */
1.102     djm       376:                if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
1.99      djm       377:                        return cc->encrypt ?
                    378:                            SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85      markus    379:                if (cc->encrypt &&
1.102     djm       380:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
1.85      markus    381:                    authlen, dest + aadlen + len))
1.99      djm       382:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    383:        }
1.93      markus    384:        return 0;
1.98      markus    385: #endif
1.21      markus    386: }
                    387:
1.91      djm       388: /* Extract the packet length, including any decryption necessary beforehand */
                    389: int
1.99      djm       390: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91      djm       391:     const u_char *cp, u_int len)
                    392: {
                    393:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    394:                return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
                    395:                    cp, len);
                    396:        if (len < 4)
1.99      djm       397:                return SSH_ERR_MESSAGE_INCOMPLETE;
1.91      djm       398:        *plenp = get_u32(cp);
                    399:        return 0;
                    400: }
                    401:
1.102     djm       402: void
                    403: cipher_free(struct sshcipher_ctx *cc)
1.16      markus    404: {
1.102     djm       405:        if (cc == NULL)
                    406:                return;
1.91      djm       407:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96      djm       408:                explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98      markus    409:        else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
                    410:                explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
                    411: #ifdef WITH_OPENSSL
1.109     jsing     412:        EVP_CIPHER_CTX_free(cc->evp);
                    413:        cc->evp = NULL;
1.98      markus    414: #endif
1.102     djm       415:        explicit_bzero(cc, sizeof(*cc));
                    416:        free(cc);
1.52      markus    417: }
1.53      markus    418:
1.54      markus    419: /*
1.99      djm       420:  * Exports an IV from the sshcipher_ctx required to export the key
1.53      markus    421:  * state back from the unprivileged child to the privileged parent
                    422:  * process.
                    423:  */
                    424: int
1.99      djm       425: cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
1.53      markus    426: {
1.99      djm       427:        const struct sshcipher *c = cc->cipher;
1.53      markus    428:
1.104     djm       429:        if ((c->flags & CFLAG_CHACHAPOLY) != 0)
                    430:                return 0;
                    431:        else if ((c->flags & CFLAG_AESCTR) != 0)
                    432:                return sizeof(cc->ac_ctx.ctr);
1.98      markus    433: #ifdef WITH_OPENSSL
1.104     djm       434:        return EVP_CIPHER_CTX_iv_length(cc->evp);
                    435: #else
                    436:        return 0;
                    437: #endif
1.53      markus    438: }
                    439:
1.99      djm       440: int
                    441: cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
1.53      markus    442: {
1.108     djm       443: #ifdef WITH_OPENSSL
1.99      djm       444:        const struct sshcipher *c = cc->cipher;
1.110   ! djm       445:        int evplen;
1.98      markus    446: #endif
1.53      markus    447:
1.91      djm       448:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    449:                if (len != 0)
1.99      djm       450:                        return SSH_ERR_INVALID_ARGUMENT;
1.100     djm       451:                return 0;
                    452:        }
                    453:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    454:                if (len != sizeof(cc->ac_ctx.ctr))
                    455:                        return SSH_ERR_INVALID_ARGUMENT;
                    456:                memcpy(iv, cc->ac_ctx.ctr, len);
1.99      djm       457:                return 0;
1.91      djm       458:        }
1.98      markus    459:        if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99      djm       460:                return 0;
1.91      djm       461:
1.98      markus    462: #ifdef WITH_OPENSSL
1.104     djm       463:        evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
                    464:        if (evplen == 0)
                    465:                return 0;
                    466:        else if (evplen < 0)
                    467:                return SSH_ERR_LIBCRYPTO_ERROR;
                    468:        if ((u_int)evplen != len)
                    469:                return SSH_ERR_INVALID_ARGUMENT;
                    470:        if (cipher_authlen(c)) {
                    471:                if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
                    472:                   len, iv))
                    473:                       return SSH_ERR_LIBCRYPTO_ERROR;
                    474:        } else
                    475:                memcpy(iv, cc->evp->iv, len);
1.98      markus    476: #endif
1.99      djm       477:        return 0;
1.53      markus    478: }
                    479:
1.99      djm       480: int
                    481: cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
1.53      markus    482: {
1.108     djm       483: #ifdef WITH_OPENSSL
1.99      djm       484:        const struct sshcipher *c = cc->cipher;
1.110   ! djm       485:        int evplen = 0;
1.98      markus    486: #endif
1.91      djm       487:
                    488:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.99      djm       489:                return 0;
1.98      markus    490:        if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99      djm       491:                return 0;
1.53      markus    492:
1.98      markus    493: #ifdef WITH_OPENSSL
1.104     djm       494:        evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
                    495:        if (evplen <= 0)
                    496:                return SSH_ERR_LIBCRYPTO_ERROR;
                    497:        if (cipher_authlen(c)) {
                    498:                /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
                    499:                if (!EVP_CIPHER_CTX_ctrl(cc->evp,
                    500:                    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
1.99      djm       501:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.104     djm       502:        } else
                    503:                memcpy(cc->evp->iv, iv, evplen);
1.98      markus    504: #endif
1.99      djm       505:        return 0;
1.53      markus    506: }
                    507: