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

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