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

1.116   ! djm         1: /* $OpenBSD: cipher.c,v 1.115 2020/02/26 13:40:09 jsg 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 */
1.115     jsg       323:                        freezero(cc, sizeof(*cc));
1.99      djm       324:                }
1.74      djm       325:        }
1.102     djm       326:        return ret;
1.1       deraadt   327: }
1.21      markus    328:
1.83      markus    329: /*
                    330:  * cipher_crypt() operates as following:
                    331:  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
1.116   ! djm       332:  * These bytes are treated as additional authenticated data for
1.83      markus    333:  * authenticated encryption modes.
                    334:  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85      markus    335:  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
                    336:  * This tag is written on encryption and verified on decryption.
1.83      markus    337:  * Both 'aadlen' and 'authlen' can be set to 0.
                    338:  */
1.93      markus    339: int
1.99      djm       340: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
                    341:    const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32      markus    342: {
1.99      djm       343:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    344:                return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
                    345:                    len, aadlen, authlen, cc->encrypt);
                    346:        }
1.104     djm       347:        if ((cc->cipher->flags & CFLAG_NONE) != 0) {
                    348:                memcpy(dest, src, aadlen + len);
                    349:                return 0;
                    350:        }
1.98      markus    351: #ifndef WITH_OPENSSL
                    352:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    353:                if (aadlen)
                    354:                        memcpy(dest, src, aadlen);
                    355:                aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
                    356:                    dest + aadlen, len);
                    357:                return 0;
                    358:        }
1.99      djm       359:        return SSH_ERR_INVALID_ARGUMENT;
1.98      markus    360: #else
1.85      markus    361:        if (authlen) {
                    362:                u_char lastiv[1];
                    363:
                    364:                if (authlen != cipher_authlen(cc->cipher))
1.99      djm       365:                        return SSH_ERR_INVALID_ARGUMENT;
1.85      markus    366:                /* increment IV */
1.102     djm       367:                if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85      markus    368:                    1, lastiv))
1.99      djm       369:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    370:                /* set tag on decyption */
                    371:                if (!cc->encrypt &&
1.102     djm       372:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
1.85      markus    373:                    authlen, (u_char *)src + aadlen + len))
1.99      djm       374:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    375:        }
                    376:        if (aadlen) {
                    377:                if (authlen &&
1.102     djm       378:                    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99      djm       379:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.83      markus    380:                memcpy(dest, src, aadlen);
1.85      markus    381:        }
1.32      markus    382:        if (len % cc->cipher->block_size)
1.99      djm       383:                return SSH_ERR_INVALID_ARGUMENT;
1.102     djm       384:        if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
1.83      markus    385:            len) < 0)
1.99      djm       386:                return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    387:        if (authlen) {
                    388:                /* compute tag (on encrypt) or verify tag (on decrypt) */
1.102     djm       389:                if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
1.99      djm       390:                        return cc->encrypt ?
                    391:                            SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85      markus    392:                if (cc->encrypt &&
1.102     djm       393:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
1.85      markus    394:                    authlen, dest + aadlen + len))
1.99      djm       395:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    396:        }
1.93      markus    397:        return 0;
1.98      markus    398: #endif
1.21      markus    399: }
                    400:
1.91      djm       401: /* Extract the packet length, including any decryption necessary beforehand */
                    402: int
1.99      djm       403: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91      djm       404:     const u_char *cp, u_int len)
                    405: {
                    406:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    407:                return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
                    408:                    cp, len);
                    409:        if (len < 4)
1.99      djm       410:                return SSH_ERR_MESSAGE_INCOMPLETE;
1.111     markus    411:        *plenp = PEEK_U32(cp);
1.91      djm       412:        return 0;
                    413: }
                    414:
1.102     djm       415: void
                    416: cipher_free(struct sshcipher_ctx *cc)
1.16      markus    417: {
1.102     djm       418:        if (cc == NULL)
                    419:                return;
1.91      djm       420:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96      djm       421:                explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98      markus    422:        else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
                    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: