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

1.92    ! djm         1: /* $OpenBSD: cipher.c,v 1.91 2013/11/21 00:45:44 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 <openssl/md5.h>
                     41:
                     42: #include <string.h>
1.81      deraadt    43: #include <stdarg.h>
1.91      djm        44: #include <stdio.h>
1.80      stevesk    45:
1.24      markus     46: #include "xmalloc.h"
1.42      markus     47: #include "log.h"
1.91      djm        48: #include "misc.h"
1.42      markus     49: #include "cipher.h"
1.57      markus     50:
1.64      markus     51: extern const EVP_CIPHER *evp_ssh1_bf(void);
                     52: extern const EVP_CIPHER *evp_ssh1_3des(void);
                     53: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.1       deraadt    54:
1.51      markus     55: struct Cipher {
                     56:        char    *name;
                     57:        int     number;         /* for ssh1 only */
                     58:        u_int   block_size;
                     59:        u_int   key_len;
1.85      markus     60:        u_int   iv_len;         /* defaults to block_size */
                     61:        u_int   auth_len;
1.74      djm        62:        u_int   discard_len;
1.91      djm        63:        u_int   flags;
                     64: #define CFLAG_CBC              (1<<0)
                     65: #define CFLAG_CHACHAPOLY       (1<<1)
1.56      markus     66:        const EVP_CIPHER        *(*evptype)(void);
1.88      djm        67: };
                     68:
                     69: static const struct Cipher ciphers[] = {
1.85      markus     70:        { "none",       SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
                     71:        { "des",        SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
                     72:        { "3des",       SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
                     73:        { "blowfish",   SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
                     74:
                     75:        { "3des-cbc",   SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
                     76:        { "blowfish-cbc",
                     77:                        SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
                     78:        { "cast128-cbc",
                     79:                        SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
                     80:        { "arcfour",    SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
                     81:        { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
                     82:        { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
                     83:        { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
                     84:        { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
                     85:        { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
1.57      markus     86:        { "rijndael-cbc@lysator.liu.se",
1.85      markus     87:                        SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
                     88:        { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
                     89:        { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
                     90:        { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
                     91:        { "aes128-gcm@openssh.com",
                     92:                        SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
                     93:        { "aes256-gcm@openssh.com",
                     94:                        SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.91      djm        95:        { "chacha20-poly1305@openssh.com",
                     96:                        SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
1.47      markus     97:
1.85      markus     98:        { NULL,         SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
1.32      markus     99: };
                    100:
                    101: /*--*/
1.1       deraadt   102:
1.90      dtucker   103: /* Returns a list of supported ciphers separated by the specified char. */
1.88      djm       104: char *
1.91      djm       105: cipher_alg_list(char sep, int auth_only)
1.88      djm       106: {
                    107:        char *ret = NULL;
                    108:        size_t nlen, rlen = 0;
                    109:        const Cipher *c;
                    110:
                    111:        for (c = ciphers; c->name != NULL; c++) {
                    112:                if (c->number != SSH_CIPHER_SSH2)
                    113:                        continue;
1.91      djm       114:                if (auth_only && c->auth_len == 0)
                    115:                        continue;
1.88      djm       116:                if (ret != NULL)
1.90      dtucker   117:                        ret[rlen++] = sep;
1.88      djm       118:                nlen = strlen(c->name);
                    119:                ret = xrealloc(ret, 1, rlen + nlen + 2);
                    120:                memcpy(ret + rlen, c->name, nlen + 1);
                    121:                rlen += nlen;
                    122:        }
                    123:        return ret;
                    124: }
                    125:
1.54      markus    126: u_int
1.66      jakob     127: cipher_blocksize(const Cipher *c)
1.51      markus    128: {
                    129:        return (c->block_size);
                    130: }
1.60      deraadt   131:
1.54      markus    132: u_int
1.66      jakob     133: cipher_keylen(const Cipher *c)
1.51      markus    134: {
                    135:        return (c->key_len);
                    136: }
1.60      deraadt   137:
1.54      markus    138: u_int
1.85      markus    139: cipher_authlen(const Cipher *c)
                    140: {
                    141:        return (c->auth_len);
                    142: }
                    143:
                    144: u_int
                    145: cipher_ivlen(const Cipher *c)
                    146: {
1.91      djm       147:        /*
                    148:         * Default is cipher block size, except for chacha20+poly1305 that
                    149:         * needs no IV. XXX make iv_len == -1 default?
                    150:         */
                    151:        return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
                    152:            c->iv_len : c->block_size;
1.85      markus    153: }
                    154:
                    155: u_int
1.66      jakob     156: cipher_get_number(const Cipher *c)
1.53      markus    157: {
                    158:        return (c->number);
1.82      markus    159: }
                    160:
                    161: u_int
                    162: cipher_is_cbc(const Cipher *c)
                    163: {
1.91      djm       164:        return (c->flags & CFLAG_CBC) != 0;
1.53      markus    165: }
1.51      markus    166:
1.41      markus    167: u_int
1.34      markus    168: cipher_mask_ssh1(int client)
1.1       deraadt   169: {
1.41      markus    170:        u_int mask = 0;
1.48      deraadt   171:        mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
1.34      markus    172:        mask |= 1 << SSH_CIPHER_BLOWFISH;
                    173:        if (client) {
                    174:                mask |= 1 << SSH_CIPHER_DES;
1.32      markus    175:        }
                    176:        return mask;
1.1       deraadt   177: }
                    178:
1.88      djm       179: const Cipher *
1.32      markus    180: cipher_by_name(const char *name)
                    181: {
1.88      djm       182:        const Cipher *c;
1.32      markus    183:        for (c = ciphers; c->name != NULL; c++)
1.73      djm       184:                if (strcmp(c->name, name) == 0)
1.32      markus    185:                        return c;
                    186:        return NULL;
                    187: }
                    188:
1.88      djm       189: const Cipher *
1.32      markus    190: cipher_by_number(int id)
                    191: {
1.88      djm       192:        const Cipher *c;
1.32      markus    193:        for (c = ciphers; c->name != NULL; c++)
                    194:                if (c->number == id)
                    195:                        return c;
                    196:        return NULL;
                    197: }
1.24      markus    198:
                    199: #define        CIPHER_SEP      ","
                    200: int
                    201: ciphers_valid(const char *names)
                    202: {
1.88      djm       203:        const Cipher *c;
1.69      avsm      204:        char *cipher_list, *cp;
1.24      markus    205:        char *p;
                    206:
1.27      markus    207:        if (names == NULL || strcmp(names, "") == 0)
1.24      markus    208:                return 0;
1.69      avsm      209:        cipher_list = cp = xstrdup(names);
1.32      markus    210:        for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48      deraadt   211:            (p = strsep(&cp, CIPHER_SEP))) {
1.32      markus    212:                c = cipher_by_name(p);
                    213:                if (c == NULL || c->number != SSH_CIPHER_SSH2) {
                    214:                        debug("bad cipher %s [%s]", p, names);
1.89      djm       215:                        free(cipher_list);
1.24      markus    216:                        return 0;
1.32      markus    217:                } else {
1.36      markus    218:                        debug3("cipher ok: %s [%s]", p, names);
1.24      markus    219:                }
                    220:        }
1.36      markus    221:        debug3("ciphers ok: [%s]", names);
1.89      djm       222:        free(cipher_list);
1.24      markus    223:        return 1;
                    224: }
                    225:
1.18      markus    226: /*
                    227:  * Parses the name of the cipher.  Returns the number of the corresponding
                    228:  * cipher, or -1 on error.
                    229:  */
1.1       deraadt   230:
1.4       provos    231: int
                    232: cipher_number(const char *name)
1.1       deraadt   233: {
1.88      djm       234:        const Cipher *c;
1.27      markus    235:        if (name == NULL)
                    236:                return -1;
1.73      djm       237:        for (c = ciphers; c->name != NULL; c++)
                    238:                if (strcasecmp(c->name, name) == 0)
                    239:                        return c->number;
                    240:        return -1;
1.1       deraadt   241: }
                    242:
1.32      markus    243: char *
                    244: cipher_name(int id)
1.1       deraadt   245: {
1.88      djm       246:        const Cipher *c = cipher_by_number(id);
1.32      markus    247:        return (c==NULL) ? "<unknown>" : c->name;
1.1       deraadt   248: }
                    249:
1.26      markus    250: void
1.88      djm       251: cipher_init(CipherContext *cc, const Cipher *cipher,
1.52      markus    252:     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69      avsm      253:     int do_encrypt)
1.16      markus    254: {
1.52      markus    255:        static int dowarn = 1;
                    256:        const EVP_CIPHER *type;
                    257:        int klen;
1.74      djm       258:        u_char *junk, *discard;
1.52      markus    259:
                    260:        if (cipher->number == SSH_CIPHER_DES) {
                    261:                if (dowarn) {
                    262:                        error("Warning: use of DES is strongly discouraged "
                    263:                            "due to cryptographic weaknesses");
                    264:                        dowarn = 0;
                    265:                }
                    266:                if (keylen > 8)
                    267:                        keylen = 8;
                    268:        }
                    269:        cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
1.85      markus    270:        cc->encrypt = do_encrypt;
1.52      markus    271:
1.32      markus    272:        if (keylen < cipher->key_len)
                    273:                fatal("cipher_init: key length %d is insufficient for %s.",
                    274:                    keylen, cipher->name);
1.85      markus    275:        if (iv != NULL && ivlen < cipher_ivlen(cipher))
1.32      markus    276:                fatal("cipher_init: iv length %d is insufficient for %s.",
                    277:                    ivlen, cipher->name);
                    278:        cc->cipher = cipher;
1.52      markus    279:
1.91      djm       280:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    281:                chachapoly_init(&cc->cp_ctx, key, keylen);
                    282:                return;
                    283:        }
1.52      markus    284:        type = (*cipher->evptype)();
                    285:        EVP_CIPHER_CTX_init(&cc->evp);
                    286:        if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69      avsm      287:            (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52      markus    288:                fatal("cipher_init: EVP_CipherInit failed for %s",
                    289:                    cipher->name);
1.85      markus    290:        if (cipher_authlen(cipher) &&
                    291:            !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
                    292:            -1, (u_char *)iv))
                    293:                fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
                    294:                    cipher->name);
1.52      markus    295:        klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.76      djm       296:        if (klen > 0 && keylen != (u_int)klen) {
1.62      markus    297:                debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52      markus    298:                if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
                    299:                        fatal("cipher_init: set keylen failed (%d -> %d)",
                    300:                            klen, keylen);
                    301:        }
                    302:        if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
                    303:                fatal("cipher_init: EVP_CipherInit: set key failed for %s",
                    304:                    cipher->name);
1.74      djm       305:
1.77      djm       306:        if (cipher->discard_len > 0) {
1.74      djm       307:                junk = xmalloc(cipher->discard_len);
                    308:                discard = xmalloc(cipher->discard_len);
                    309:                if (EVP_Cipher(&cc->evp, discard, junk,
                    310:                    cipher->discard_len) == 0)
                    311:                        fatal("evp_crypt: EVP_Cipher failed during discard");
                    312:                memset(discard, 0, cipher->discard_len);
1.89      djm       313:                free(junk);
                    314:                free(discard);
1.74      djm       315:        }
1.1       deraadt   316: }
1.21      markus    317:
1.83      markus    318: /*
                    319:  * cipher_crypt() operates as following:
                    320:  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
                    321:  * Theses bytes are treated as additional authenticated data for
                    322:  * authenticated encryption modes.
                    323:  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85      markus    324:  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
                    325:  * This tag is written on encryption and verified on decryption.
1.83      markus    326:  * Both 'aadlen' and 'authlen' can be set to 0.
                    327:  */
1.26      markus    328: void
1.91      djm       329: cipher_crypt(CipherContext *cc, u_int seqnr, u_char *dest, const u_char *src,
1.85      markus    330:     u_int len, u_int aadlen, u_int authlen)
1.32      markus    331: {
1.91      djm       332:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    333:                if (chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, len, aadlen,
                    334:                    authlen, cc->encrypt) != 0)
                    335:                        fatal("Decryption integrity check failed");
                    336:                return;
                    337:        }
1.85      markus    338:        if (authlen) {
                    339:                u_char lastiv[1];
                    340:
                    341:                if (authlen != cipher_authlen(cc->cipher))
                    342:                        fatal("%s: authlen mismatch %d", __func__, authlen);
                    343:                /* increment IV */
                    344:                if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
                    345:                    1, lastiv))
                    346:                        fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
                    347:                /* set tag on decyption */
                    348:                if (!cc->encrypt &&
                    349:                    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
                    350:                    authlen, (u_char *)src + aadlen + len))
                    351:                        fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
                    352:        }
                    353:        if (aadlen) {
                    354:                if (authlen &&
                    355:                    EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
                    356:                        fatal("%s: EVP_Cipher(aad) failed", __func__);
1.83      markus    357:                memcpy(dest, src, aadlen);
1.85      markus    358:        }
1.32      markus    359:        if (len % cc->cipher->block_size)
1.83      markus    360:                fatal("%s: bad plaintext length %d", __func__, len);
                    361:        if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
                    362:            len) < 0)
                    363:                fatal("%s: EVP_Cipher failed", __func__);
1.85      markus    364:        if (authlen) {
                    365:                /* compute tag (on encrypt) or verify tag (on decrypt) */
1.86      djm       366:                if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
                    367:                        if (cc->encrypt)
                    368:                                fatal("%s: EVP_Cipher(final) failed", __func__);
                    369:                        else
                    370:                                fatal("Decryption integrity check failed");
                    371:                }
1.85      markus    372:                if (cc->encrypt &&
                    373:                    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
                    374:                    authlen, dest + aadlen + len))
                    375:                        fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
                    376:        }
1.21      markus    377: }
                    378:
1.91      djm       379: /* Extract the packet length, including any decryption necessary beforehand */
                    380: int
                    381: cipher_get_length(CipherContext *cc, u_int *plenp, u_int seqnr,
                    382:     const u_char *cp, u_int len)
                    383: {
                    384:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    385:                return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
                    386:                    cp, len);
                    387:        if (len < 4)
                    388:                return -1;
                    389:        *plenp = get_u32(cp);
                    390:        return 0;
                    391: }
                    392:
1.26      markus    393: void
1.51      markus    394: cipher_cleanup(CipherContext *cc)
1.16      markus    395: {
1.91      djm       396:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.92    ! djm       397:                memset(&cc->cp_ctx, 0, sizeof(cc->cp_ctx));
1.91      djm       398:        else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
1.52      markus    399:                error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16      markus    400: }
1.1       deraadt   401:
1.32      markus    402: /*
                    403:  * Selects the cipher, and keys if by computing the MD5 checksum of the
                    404:  * passphrase and using the resulting 16 bytes as the key.
                    405:  */
1.1       deraadt   406:
1.26      markus    407: void
1.88      djm       408: cipher_set_key_string(CipherContext *cc, const Cipher *cipher,
1.69      avsm      409:     const char *passphrase, int do_encrypt)
1.16      markus    410: {
1.32      markus    411:        MD5_CTX md;
1.41      markus    412:        u_char digest[16];
1.32      markus    413:
                    414:        MD5_Init(&md);
                    415:        MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
                    416:        MD5_Final(digest, &md);
1.16      markus    417:
1.69      avsm      418:        cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16      markus    419:
1.32      markus    420:        memset(digest, 0, sizeof(digest));
                    421:        memset(&md, 0, sizeof(md));
1.52      markus    422: }
1.53      markus    423:
1.54      markus    424: /*
1.53      markus    425:  * Exports an IV from the CipherContext required to export the key
                    426:  * state back from the unprivileged child to the privileged parent
                    427:  * process.
                    428:  */
                    429:
                    430: int
1.66      jakob     431: cipher_get_keyiv_len(const CipherContext *cc)
1.53      markus    432: {
1.88      djm       433:        const Cipher *c = cc->cipher;
1.53      markus    434:        int ivlen;
                    435:
                    436:        if (c->number == SSH_CIPHER_3DES)
                    437:                ivlen = 24;
1.91      djm       438:        else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    439:                ivlen = 0;
1.53      markus    440:        else
                    441:                ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    442:        return (ivlen);
                    443: }
                    444:
                    445: void
                    446: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
                    447: {
1.88      djm       448:        const Cipher *c = cc->cipher;
1.53      markus    449:        int evplen;
                    450:
1.91      djm       451:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
                    452:                if (len != 0)
                    453:                        fatal("%s: wrong iv length %d != %d", __func__, len, 0);
                    454:                return;
                    455:        }
                    456:
1.53      markus    457:        switch (c->number) {
                    458:        case SSH_CIPHER_SSH2:
                    459:        case SSH_CIPHER_DES:
                    460:        case SSH_CIPHER_BLOWFISH:
                    461:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76      djm       462:                if (evplen <= 0)
1.53      markus    463:                        return;
1.76      djm       464:                if ((u_int)evplen != len)
1.58      markus    465:                        fatal("%s: wrong iv length %d != %d", __func__,
1.53      markus    466:                            evplen, len);
1.85      markus    467:                if (cipher_authlen(c)) {
                    468:                        if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
                    469:                           len, iv))
                    470:                               fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
                    471:                } else
                    472:                        memcpy(iv, cc->evp.iv, len);
1.63      markus    473:                break;
                    474:        case SSH_CIPHER_3DES:
                    475:                ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53      markus    476:                break;
                    477:        default:
1.58      markus    478:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    479:        }
                    480: }
                    481:
                    482: void
                    483: cipher_set_keyiv(CipherContext *cc, u_char *iv)
                    484: {
1.88      djm       485:        const Cipher *c = cc->cipher;
1.53      markus    486:        int evplen = 0;
1.91      djm       487:
                    488:        if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
                    489:                return;
1.53      markus    490:
                    491:        switch (c->number) {
                    492:        case SSH_CIPHER_SSH2:
                    493:        case SSH_CIPHER_DES:
                    494:        case SSH_CIPHER_BLOWFISH:
                    495:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    496:                if (evplen == 0)
                    497:                        return;
1.85      markus    498:                if (cipher_authlen(c)) {
                    499:                        if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
                    500:                            EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
                    501:                                fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
                    502:                                    __func__);
                    503:                } else
                    504:                        memcpy(cc->evp.iv, iv, evplen);
1.63      markus    505:                break;
                    506:        case SSH_CIPHER_3DES:
                    507:                ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53      markus    508:                break;
                    509:        default:
1.58      markus    510:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    511:        }
                    512: }
                    513:
                    514: #define EVP_X_STATE(evp)       (evp).cipher_data
                    515: #define EVP_X_STATE_LEN(evp)   (evp).cipher->ctx_size
                    516:
                    517: int
1.66      jakob     518: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53      markus    519: {
1.88      djm       520:        const Cipher *c = cc->cipher;
1.59      markus    521:        int plen = 0;
1.53      markus    522:
1.87      djm       523:        if (c->evptype == EVP_rc4) {
1.59      markus    524:                plen = EVP_X_STATE_LEN(cc->evp);
1.53      markus    525:                if (dat == NULL)
1.59      markus    526:                        return (plen);
                    527:                memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53      markus    528:        }
                    529:        return (plen);
                    530: }
                    531:
                    532: void
                    533: cipher_set_keycontext(CipherContext *cc, u_char *dat)
                    534: {
1.88      djm       535:        const Cipher *c = cc->cipher;
1.53      markus    536:        int plen;
                    537:
1.87      djm       538:        if (c->evptype == EVP_rc4) {
1.53      markus    539:                plen = EVP_X_STATE_LEN(cc->evp);
                    540:                memcpy(EVP_X_STATE(cc->evp), dat, plen);
                    541:        }
1.1       deraadt   542: }