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

1.106   ! djm         1: /* $OpenBSD: cipher.c,v 1.105 2017/05/01 00:03:18 djm Exp $ */
1.1       deraadt     2: /*
1.30      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.26      markus      6:  *
1.30      deraadt     7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
1.26      markus     12:  *
                     13:  *
1.30      deraadt    14:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
1.46      markus     15:  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
1.26      markus     16:  *
1.30      deraadt    17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
1.26      markus     25:  *
1.30      deraadt    26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.17      deraadt    36:  */
1.1       deraadt    37:
1.81      deraadt    38: #include <sys/types.h>
1.1       deraadt    39:
1.80      stevesk    40: #include <string.h>
1.81      deraadt    41: #include <stdarg.h>
1.91      djm        42: #include <stdio.h>
1.80      stevesk    43:
1.99      djm        44: #include "cipher.h"
1.91      djm        45: #include "misc.h"
1.99      djm        46: #include "sshbuf.h"
                     47: #include "ssherr.h"
1.95      markus     48: #include "digest.h"
1.57      markus     49:
1.1       deraadt    50:
1.102     djm        51: struct sshcipher_ctx {
                     52:        int     plaintext;
                     53:        int     encrypt;
                     54:        EVP_CIPHER_CTX *evp;
                     55:        struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
                     56:        struct aesctr_ctx ac_ctx; /* XXX union with evp? */
                     57:        const struct sshcipher *cipher;
                     58: };
                     59:
1.99      djm        60: struct sshcipher {
1.51      markus     61:        char    *name;
                     62:        u_int   block_size;
                     63:        u_int   key_len;
1.85      markus     64:        u_int   iv_len;         /* defaults to block_size */
                     65:        u_int   auth_len;
1.74      djm        66:        u_int   discard_len;
1.91      djm        67:        u_int   flags;
                     68: #define CFLAG_CBC              (1<<0)
                     69: #define CFLAG_CHACHAPOLY       (1<<1)
1.98      markus     70: #define CFLAG_AESCTR           (1<<2)
                     71: #define CFLAG_NONE             (1<<3)
1.104     djm        72: #define CFLAG_INTERNAL         CFLAG_NONE /* Don't use "none" for packets */
1.98      markus     73: #ifdef WITH_OPENSSL
1.56      markus     74:        const EVP_CIPHER        *(*evptype)(void);
1.98      markus     75: #else
                     76:        void    *ignored;
                     77: #endif
1.88      djm        78: };
                     79:
1.99      djm        80: static const struct sshcipher ciphers[] = {
1.98      markus     81: #ifdef WITH_OPENSSL
1.104     djm        82:        { "3des-cbc",           8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
                     83:        { "blowfish-cbc",       8, 16, 0, 0, 0, 1, EVP_bf_cbc },
                     84:        { "cast128-cbc",        8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
                     85:        { "arcfour",            8, 16, 0, 0, 0, 0, EVP_rc4 },
                     86:        { "arcfour128",         8, 16, 0, 0, 1536, 0, EVP_rc4 },
                     87:        { "arcfour256",         8, 32, 0, 0, 1536, 0, EVP_rc4 },
                     88:        { "aes128-cbc",         16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
                     89:        { "aes192-cbc",         16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
                     90:        { "aes256-cbc",         16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
1.57      markus     91:        { "rijndael-cbc@lysator.liu.se",
1.104     djm        92:                                16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
                     93:        { "aes128-ctr",         16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
                     94:        { "aes192-ctr",         16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
                     95:        { "aes256-ctr",         16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
1.85      markus     96:        { "aes128-gcm@openssh.com",
1.104     djm        97:                                16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
1.85      markus     98:        { "aes256-gcm@openssh.com",
1.104     djm        99:                                16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.98      markus    100: #else
1.104     djm       101:        { "aes128-ctr",         16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
                    102:        { "aes192-ctr",         16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
                    103:        { "aes256-ctr",         16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
1.98      markus    104: #endif
1.91      djm       105:        { "chacha20-poly1305@openssh.com",
1.104     djm       106:                                8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
                    107:        { "none",               8, 0, 0, 0, 0, CFLAG_NONE, NULL },
1.47      markus    108:
1.104     djm       109:        { NULL,                 0, 0, 0, 0, 0, 0, NULL }
1.32      markus    110: };
                    111:
                    112: /*--*/
1.1       deraadt   113:
1.99      djm       114: /* Returns a comma-separated list of supported ciphers. */
1.88      djm       115: char *
1.91      djm       116: cipher_alg_list(char sep, int auth_only)
1.88      djm       117: {
1.99      djm       118:        char *tmp, *ret = NULL;
1.88      djm       119:        size_t nlen, rlen = 0;
1.99      djm       120:        const struct sshcipher *c;
1.88      djm       121:
                    122:        for (c = ciphers; c->name != NULL; c++) {
1.104     djm       123:                if ((c->flags & CFLAG_INTERNAL) != 0)
1.88      djm       124:                        continue;
1.91      djm       125:                if (auth_only && c->auth_len == 0)
                    126:                        continue;
1.88      djm       127:                if (ret != NULL)
1.90      dtucker   128:                        ret[rlen++] = sep;
1.88      djm       129:                nlen = strlen(c->name);
1.99      djm       130:                if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
                    131:                        free(ret);
                    132:                        return NULL;
                    133:                }
                    134:                ret = tmp;
1.88      djm       135:                memcpy(ret + rlen, c->name, nlen + 1);
                    136:                rlen += nlen;
                    137:        }
                    138:        return ret;
                    139: }
                    140:
1.54      markus    141: u_int
1.99      djm       142: cipher_blocksize(const struct sshcipher *c)
1.51      markus    143: {
                    144:        return (c->block_size);
                    145: }
1.60      deraadt   146:
1.54      markus    147: u_int
1.99      djm       148: cipher_keylen(const struct sshcipher *c)
1.51      markus    149: {
                    150:        return (c->key_len);
1.94      dtucker   151: }
                    152:
                    153: u_int
1.99      djm       154: cipher_seclen(const struct sshcipher *c)
1.94      dtucker   155: {
                    156:        if (strcmp("3des-cbc", c->name) == 0)
                    157:                return 14;
                    158:        return cipher_keylen(c);
1.51      markus    159: }
1.60      deraadt   160:
1.54      markus    161: u_int
1.99      djm       162: cipher_authlen(const struct sshcipher *c)
1.85      markus    163: {
                    164:        return (c->auth_len);
                    165: }
                    166:
                    167: u_int
1.99      djm       168: cipher_ivlen(const struct sshcipher *c)
1.85      markus    169: {
1.91      djm       170:        /*
                    171:         * Default is cipher block size, except for chacha20+poly1305 that
                    172:         * needs no IV. XXX make iv_len == -1 default?
                    173:         */
                    174:        return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
                    175:            c->iv_len : c->block_size;
1.85      markus    176: }
                    177:
                    178: u_int
1.99      djm       179: cipher_is_cbc(const struct sshcipher *c)
1.82      markus    180: {
1.91      djm       181:        return (c->flags & CFLAG_CBC) != 0;
1.53      markus    182: }
1.51      markus    183:
1.41      markus    184: u_int
1.102     djm       185: cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
                    186: {
                    187:        return cc->plaintext;
                    188: }
                    189:
1.99      djm       190: const struct sshcipher *
1.32      markus    191: cipher_by_name(const char *name)
                    192: {
1.99      djm       193:        const struct sshcipher *c;
1.32      markus    194:        for (c = ciphers; c->name != NULL; c++)
1.73      djm       195:                if (strcmp(c->name, name) == 0)
1.32      markus    196:                        return c;
                    197:        return NULL;
                    198: }
                    199:
1.24      markus    200: #define        CIPHER_SEP      ","
                    201: int
                    202: ciphers_valid(const char *names)
                    203: {
1.99      djm       204:        const struct sshcipher *c;
1.69      avsm      205:        char *cipher_list, *cp;
1.24      markus    206:        char *p;
                    207:
1.27      markus    208:        if (names == NULL || strcmp(names, "") == 0)
1.24      markus    209:                return 0;
1.99      djm       210:        if ((cipher_list = cp = strdup(names)) == NULL)
                    211:                return 0;
1.32      markus    212:        for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48      deraadt   213:            (p = strsep(&cp, CIPHER_SEP))) {
1.32      markus    214:                c = cipher_by_name(p);
1.104     djm       215:                if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
1.89      djm       216:                        free(cipher_list);
1.24      markus    217:                        return 0;
                    218:                }
                    219:        }
1.89      djm       220:        free(cipher_list);
1.24      markus    221:        return 1;
                    222: }
                    223:
1.99      djm       224: const char *
                    225: cipher_warning_message(const struct sshcipher_ctx *cc)
                    226: {
                    227:        if (cc == NULL || cc->cipher == NULL)
                    228:                return NULL;
1.104     djm       229:        /* XXX repurpose for CBC warning */
1.99      djm       230:        return NULL;
                    231: }
                    232:
                    233: int
1.102     djm       234: cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
1.52      markus    235:     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69      avsm      236:     int do_encrypt)
1.16      markus    237: {
1.102     djm       238:        struct sshcipher_ctx *cc = NULL;
                    239:        int ret = SSH_ERR_INTERNAL_ERROR;
1.98      markus    240: #ifdef WITH_OPENSSL
1.52      markus    241:        const EVP_CIPHER *type;
                    242:        int klen;
1.74      djm       243:        u_char *junk, *discard;
1.102     djm       244: #endif
                    245:
                    246:        *ccp = NULL;
                    247:        if ((cc = calloc(sizeof(*cc), 1)) == NULL)
                    248:                return SSH_ERR_ALLOC_FAIL;
1.52      markus    249:
1.105     djm       250:        cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
1.85      markus    251:        cc->encrypt = do_encrypt;
1.52      markus    252:
1.99      djm       253:        if (keylen < cipher->key_len ||
1.102     djm       254:            (iv != NULL && ivlen < cipher_ivlen(cipher))) {
                    255:                ret = SSH_ERR_INVALID_ARGUMENT;
                    256:                goto out;
                    257:        }
1.99      djm       258:
1.32      markus    259:        cc->cipher = cipher;
1.91      djm       260:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
1.102     djm       261:                ret = chachapoly_init(&cc->cp_ctx, key, keylen);
                    262:                goto out;
1.91      djm       263:        }
1.104     djm       264:        if ((cc->cipher->flags & CFLAG_NONE) != 0) {
                    265:                ret = 0;
                    266:                goto out;
                    267:        }
1.98      markus    268: #ifndef WITH_OPENSSL
                    269:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    270:                aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
                    271:                aesctr_ivsetup(&cc->ac_ctx, iv);
1.102     djm       272:                ret = 0;
                    273:                goto out;
                    274:        }
                    275:        ret = SSH_ERR_INVALID_ARGUMENT;
                    276:        goto out;
                    277: #else /* WITH_OPENSSL */
1.52      markus    278:        type = (*cipher->evptype)();
1.102     djm       279:        if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
                    280:                ret = SSH_ERR_ALLOC_FAIL;
                    281:                goto out;
                    282:        }
                    283:        if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
1.99      djm       284:            (do_encrypt == CIPHER_ENCRYPT)) == 0) {
                    285:                ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102     djm       286:                goto out;
1.99      djm       287:        }
1.85      markus    288:        if (cipher_authlen(cipher) &&
1.102     djm       289:            !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
1.99      djm       290:            -1, (u_char *)iv)) {
                    291:                ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102     djm       292:                goto out;
1.99      djm       293:        }
1.102     djm       294:        klen = EVP_CIPHER_CTX_key_length(cc->evp);
1.76      djm       295:        if (klen > 0 && keylen != (u_int)klen) {
1.102     djm       296:                if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
1.99      djm       297:                        ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102     djm       298:                        goto out;
1.99      djm       299:                }
                    300:        }
1.102     djm       301:        if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
1.99      djm       302:                ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102     djm       303:                goto out;
1.99      djm       304:        }
1.74      djm       305:
1.77      djm       306:        if (cipher->discard_len > 0) {
1.99      djm       307:                if ((junk = malloc(cipher->discard_len)) == NULL ||
                    308:                    (discard = malloc(cipher->discard_len)) == NULL) {
1.101     mmcc      309:                        free(junk);
1.99      djm       310:                        ret = SSH_ERR_ALLOC_FAIL;
1.102     djm       311:                        goto out;
1.99      djm       312:                }
1.102     djm       313:                ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
1.96      djm       314:                explicit_bzero(discard, cipher->discard_len);
1.89      djm       315:                free(junk);
                    316:                free(discard);
1.99      djm       317:                if (ret != 1) {
                    318:                        ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102     djm       319:                        goto out;
                    320:                }
                    321:        }
                    322:        ret = 0;
                    323: #endif /* WITH_OPENSSL */
                    324:  out:
                    325:        if (ret == 0) {
                    326:                /* success */
                    327:                *ccp = cc;
                    328:        } else {
                    329:                if (cc != NULL) {
                    330: #ifdef WITH_OPENSSL
                    331:                        if (cc->evp != NULL)
                    332:                                EVP_CIPHER_CTX_free(cc->evp);
                    333: #endif /* WITH_OPENSSL */
                    334:                        explicit_bzero(cc, sizeof(*cc));
                    335:                        free(cc);
1.99      djm       336:                }
1.74      djm       337:        }
1.102     djm       338:        return ret;
1.1       deraadt   339: }
1.21      markus    340:
1.83      markus    341: /*
                    342:  * cipher_crypt() operates as following:
                    343:  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
                    344:  * Theses bytes are treated as additional authenticated data for
                    345:  * authenticated encryption modes.
                    346:  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85      markus    347:  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
                    348:  * This tag is written on encryption and verified on decryption.
1.83      markus    349:  * Both 'aadlen' and 'authlen' can be set to 0.
                    350:  */
1.93      markus    351: int
1.99      djm       352: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
                    353:    const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32      markus    354: {
1.99      djm       355:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    356:                return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
                    357:                    len, aadlen, authlen, cc->encrypt);
                    358:        }
1.104     djm       359:        if ((cc->cipher->flags & CFLAG_NONE) != 0) {
                    360:                memcpy(dest, src, aadlen + len);
                    361:                return 0;
                    362:        }
1.98      markus    363: #ifndef WITH_OPENSSL
                    364:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    365:                if (aadlen)
                    366:                        memcpy(dest, src, aadlen);
                    367:                aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
                    368:                    dest + aadlen, len);
                    369:                return 0;
                    370:        }
1.99      djm       371:        return SSH_ERR_INVALID_ARGUMENT;
1.98      markus    372: #else
1.85      markus    373:        if (authlen) {
                    374:                u_char lastiv[1];
                    375:
                    376:                if (authlen != cipher_authlen(cc->cipher))
1.99      djm       377:                        return SSH_ERR_INVALID_ARGUMENT;
1.85      markus    378:                /* increment IV */
1.102     djm       379:                if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85      markus    380:                    1, lastiv))
1.99      djm       381:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    382:                /* set tag on decyption */
                    383:                if (!cc->encrypt &&
1.102     djm       384:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
1.85      markus    385:                    authlen, (u_char *)src + aadlen + len))
1.99      djm       386:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    387:        }
                    388:        if (aadlen) {
                    389:                if (authlen &&
1.102     djm       390:                    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99      djm       391:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.83      markus    392:                memcpy(dest, src, aadlen);
1.85      markus    393:        }
1.32      markus    394:        if (len % cc->cipher->block_size)
1.99      djm       395:                return SSH_ERR_INVALID_ARGUMENT;
1.102     djm       396:        if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
1.83      markus    397:            len) < 0)
1.99      djm       398:                return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    399:        if (authlen) {
                    400:                /* compute tag (on encrypt) or verify tag (on decrypt) */
1.102     djm       401:                if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
1.99      djm       402:                        return cc->encrypt ?
                    403:                            SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85      markus    404:                if (cc->encrypt &&
1.102     djm       405:                    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
1.85      markus    406:                    authlen, dest + aadlen + len))
1.99      djm       407:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.85      markus    408:        }
1.93      markus    409:        return 0;
1.98      markus    410: #endif
1.21      markus    411: }
                    412:
1.91      djm       413: /* Extract the packet length, including any decryption necessary beforehand */
                    414: int
1.99      djm       415: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91      djm       416:     const u_char *cp, u_int len)
                    417: {
                    418:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    419:                return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
                    420:                    cp, len);
                    421:        if (len < 4)
1.99      djm       422:                return SSH_ERR_MESSAGE_INCOMPLETE;
1.91      djm       423:        *plenp = get_u32(cp);
                    424:        return 0;
                    425: }
                    426:
1.102     djm       427: void
                    428: cipher_free(struct sshcipher_ctx *cc)
1.16      markus    429: {
1.102     djm       430:        if (cc == NULL)
                    431:                return;
1.91      djm       432:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96      djm       433:                explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98      markus    434:        else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
                    435:                explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
                    436: #ifdef WITH_OPENSSL
1.102     djm       437:        if (cc->evp != NULL) {
                    438:                EVP_CIPHER_CTX_free(cc->evp);
                    439:                cc->evp = NULL;
                    440:        }
1.98      markus    441: #endif
1.102     djm       442:        explicit_bzero(cc, sizeof(*cc));
                    443:        free(cc);
1.52      markus    444: }
1.53      markus    445:
1.54      markus    446: /*
1.99      djm       447:  * Exports an IV from the sshcipher_ctx required to export the key
1.53      markus    448:  * state back from the unprivileged child to the privileged parent
                    449:  * process.
                    450:  */
                    451: int
1.99      djm       452: cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
1.53      markus    453: {
1.99      djm       454:        const struct sshcipher *c = cc->cipher;
1.53      markus    455:
1.104     djm       456:        if ((c->flags & CFLAG_CHACHAPOLY) != 0)
                    457:                return 0;
                    458:        else if ((c->flags & CFLAG_AESCTR) != 0)
                    459:                return sizeof(cc->ac_ctx.ctr);
1.98      markus    460: #ifdef WITH_OPENSSL
1.104     djm       461:        return EVP_CIPHER_CTX_iv_length(cc->evp);
                    462: #else
                    463:        return 0;
                    464: #endif
1.53      markus    465: }
                    466:
1.99      djm       467: int
                    468: cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
1.53      markus    469: {
1.99      djm       470:        const struct sshcipher *c = cc->cipher;
1.98      markus    471: #ifdef WITH_OPENSSL
1.99      djm       472:        int evplen;
1.98      markus    473: #endif
1.53      markus    474:
1.91      djm       475:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    476:                if (len != 0)
1.99      djm       477:                        return SSH_ERR_INVALID_ARGUMENT;
1.100     djm       478:                return 0;
                    479:        }
                    480:        if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
                    481:                if (len != sizeof(cc->ac_ctx.ctr))
                    482:                        return SSH_ERR_INVALID_ARGUMENT;
                    483:                memcpy(iv, cc->ac_ctx.ctr, len);
1.99      djm       484:                return 0;
1.91      djm       485:        }
1.98      markus    486:        if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99      djm       487:                return 0;
1.91      djm       488:
1.98      markus    489: #ifdef WITH_OPENSSL
1.104     djm       490:        evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
                    491:        if (evplen == 0)
                    492:                return 0;
                    493:        else if (evplen < 0)
                    494:                return SSH_ERR_LIBCRYPTO_ERROR;
                    495:        if ((u_int)evplen != len)
                    496:                return SSH_ERR_INVALID_ARGUMENT;
                    497:        if (cipher_authlen(c)) {
                    498:                if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
                    499:                   len, iv))
                    500:                       return SSH_ERR_LIBCRYPTO_ERROR;
                    501:        } else
                    502:                memcpy(iv, cc->evp->iv, len);
1.98      markus    503: #endif
1.99      djm       504:        return 0;
1.53      markus    505: }
                    506:
1.99      djm       507: int
                    508: cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
1.53      markus    509: {
1.99      djm       510:        const struct sshcipher *c = cc->cipher;
1.98      markus    511: #ifdef WITH_OPENSSL
1.99      djm       512:        int evplen = 0;
1.98      markus    513: #endif
1.91      djm       514:
                    515:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.99      djm       516:                return 0;
1.98      markus    517:        if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99      djm       518:                return 0;
1.53      markus    519:
1.98      markus    520: #ifdef WITH_OPENSSL
1.104     djm       521:        evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
                    522:        if (evplen <= 0)
                    523:                return SSH_ERR_LIBCRYPTO_ERROR;
                    524:        if (cipher_authlen(c)) {
                    525:                /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
                    526:                if (!EVP_CIPHER_CTX_ctrl(cc->evp,
                    527:                    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
1.99      djm       528:                        return SSH_ERR_LIBCRYPTO_ERROR;
1.104     djm       529:        } else
                    530:                memcpy(cc->evp->iv, iv, evplen);
1.98      markus    531: #endif
1.99      djm       532:        return 0;
1.53      markus    533: }
                    534:
1.98      markus    535: #ifdef WITH_OPENSSL
1.102     djm       536: #define EVP_X_STATE(evp)       (evp)->cipher_data
                    537: #define EVP_X_STATE_LEN(evp)   (evp)->cipher->ctx_size
1.98      markus    538: #endif
1.53      markus    539:
                    540: int
1.99      djm       541: cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
1.53      markus    542: {
1.98      markus    543: #ifdef WITH_OPENSSL
1.99      djm       544:        const struct sshcipher *c = cc->cipher;
1.59      markus    545:        int plen = 0;
1.53      markus    546:
1.87      djm       547:        if (c->evptype == EVP_rc4) {
1.59      markus    548:                plen = EVP_X_STATE_LEN(cc->evp);
1.53      markus    549:                if (dat == NULL)
1.59      markus    550:                        return (plen);
                    551:                memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53      markus    552:        }
                    553:        return (plen);
1.98      markus    554: #else
1.99      djm       555:        return 0;
1.98      markus    556: #endif
1.53      markus    557: }
                    558:
                    559: void
1.99      djm       560: cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
1.53      markus    561: {
1.98      markus    562: #ifdef WITH_OPENSSL
1.99      djm       563:        const struct sshcipher *c = cc->cipher;
1.53      markus    564:        int plen;
                    565:
1.87      djm       566:        if (c->evptype == EVP_rc4) {
1.53      markus    567:                plen = EVP_X_STATE_LEN(cc->evp);
                    568:                memcpy(EVP_X_STATE(cc->evp), dat, plen);
                    569:        }
1.98      markus    570: #endif
1.1       deraadt   571: }