[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.118

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