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

1.1       deraadt     1: /*
1.30      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.26      markus      5:  *
1.30      deraadt     6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
1.26      markus     11:  *
                     12:  *
1.30      deraadt    13:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
1.46      markus     14:  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
1.26      markus     15:  *
1.30      deraadt    16:  * Redistribution and use in source and binary forms, with or without
                     17:  * modification, are permitted provided that the following conditions
                     18:  * are met:
                     19:  * 1. Redistributions of source code must retain the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer.
                     21:  * 2. Redistributions in binary form must reproduce the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer in the
                     23:  *    documentation and/or other materials provided with the distribution.
1.26      markus     24:  *
1.30      deraadt    25:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     26:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     27:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     28:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     29:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     30:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     31:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     32:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     33:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     34:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.17      deraadt    35:  */
1.1       deraadt    36:
                     37: #include "includes.h"
1.71.2.2! brad       38: RCSID("$OpenBSD: cipher.c,v 1.77 2005/07/16 01:35:24 djm Exp $");
1.1       deraadt    39:
1.24      markus     40: #include "xmalloc.h"
1.42      markus     41: #include "log.h"
                     42: #include "cipher.h"
1.8       deraadt    43:
1.25      markus     44: #include <openssl/md5.h>
1.57      markus     45:
1.64      markus     46: extern const EVP_CIPHER *evp_ssh1_bf(void);
                     47: extern const EVP_CIPHER *evp_ssh1_3des(void);
                     48: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.65      markus     49: extern const EVP_CIPHER *evp_aes_128_ctr(void);
                     50: extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
1.1       deraadt    51:
1.51      markus     52: struct Cipher {
                     53:        char    *name;
                     54:        int     number;         /* for ssh1 only */
                     55:        u_int   block_size;
                     56:        u_int   key_len;
1.71.2.2! brad       57:        u_int   discard_len;
1.56      markus     58:        const EVP_CIPHER        *(*evptype)(void);
1.52      markus     59: } ciphers[] = {
1.71.2.2! brad       60:        { "none",               SSH_CIPHER_NONE, 8, 0, 0, EVP_enc_null },
        !            61:        { "des",                SSH_CIPHER_DES, 8, 8, 0, EVP_des_cbc },
        !            62:        { "3des",               SSH_CIPHER_3DES, 8, 16, 0, evp_ssh1_3des },
        !            63:        { "blowfish",           SSH_CIPHER_BLOWFISH, 8, 32, 0, evp_ssh1_bf },
        !            64:
        !            65:        { "3des-cbc",           SSH_CIPHER_SSH2, 8, 24, 0, EVP_des_ede3_cbc },
        !            66:        { "blowfish-cbc",       SSH_CIPHER_SSH2, 8, 16, 0, EVP_bf_cbc },
        !            67:        { "cast128-cbc",        SSH_CIPHER_SSH2, 8, 16, 0, EVP_cast5_cbc },
        !            68:        { "arcfour",            SSH_CIPHER_SSH2, 8, 16, 0, EVP_rc4 },
        !            69:        { "arcfour128",         SSH_CIPHER_SSH2, 8, 16, 1536, EVP_rc4 },
        !            70:        { "arcfour256",         SSH_CIPHER_SSH2, 8, 32, 1536, EVP_rc4 },
        !            71:        { "aes128-cbc",         SSH_CIPHER_SSH2, 16, 16, 0, EVP_aes_128_cbc },
        !            72:        { "aes192-cbc",         SSH_CIPHER_SSH2, 16, 24, 0, EVP_aes_192_cbc },
        !            73:        { "aes256-cbc",         SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
1.57      markus     74:        { "rijndael-cbc@lysator.liu.se",
1.71.2.2! brad       75:                                SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
        !            76:        { "aes128-ctr",         SSH_CIPHER_SSH2, 16, 16, 0, evp_aes_128_ctr },
        !            77:        { "aes192-ctr",         SSH_CIPHER_SSH2, 16, 24, 0, evp_aes_128_ctr },
        !            78:        { "aes256-ctr",         SSH_CIPHER_SSH2, 16, 32, 0, evp_aes_128_ctr },
        !            79:        { "acss@openssh.org",   SSH_CIPHER_SSH2, 16, 5, 0, EVP_acss },
1.47      markus     80:
1.71.2.2! brad       81:        { NULL,                 SSH_CIPHER_INVALID, 0, 0, 0, NULL }
1.32      markus     82: };
                     83:
                     84: /*--*/
1.1       deraadt    85:
1.54      markus     86: u_int
1.66      jakob      87: cipher_blocksize(const Cipher *c)
1.51      markus     88: {
                     89:        return (c->block_size);
                     90: }
1.60      deraadt    91:
1.54      markus     92: u_int
1.66      jakob      93: cipher_keylen(const Cipher *c)
1.51      markus     94: {
                     95:        return (c->key_len);
                     96: }
1.60      deraadt    97:
1.54      markus     98: u_int
1.66      jakob      99: cipher_get_number(const Cipher *c)
1.53      markus    100: {
                    101:        return (c->number);
                    102: }
1.51      markus    103:
1.41      markus    104: u_int
1.34      markus    105: cipher_mask_ssh1(int client)
1.1       deraadt   106: {
1.41      markus    107:        u_int mask = 0;
1.48      deraadt   108:        mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
1.34      markus    109:        mask |= 1 << SSH_CIPHER_BLOWFISH;
                    110:        if (client) {
                    111:                mask |= 1 << SSH_CIPHER_DES;
1.32      markus    112:        }
                    113:        return mask;
1.1       deraadt   114: }
                    115:
1.32      markus    116: Cipher *
                    117: cipher_by_name(const char *name)
                    118: {
                    119:        Cipher *c;
                    120:        for (c = ciphers; c->name != NULL; c++)
1.71.2.1  brad      121:                if (strcmp(c->name, name) == 0)
1.32      markus    122:                        return c;
                    123:        return NULL;
                    124: }
                    125:
                    126: Cipher *
                    127: cipher_by_number(int id)
                    128: {
                    129:        Cipher *c;
                    130:        for (c = ciphers; c->name != NULL; c++)
                    131:                if (c->number == id)
                    132:                        return c;
                    133:        return NULL;
                    134: }
1.24      markus    135:
                    136: #define        CIPHER_SEP      ","
                    137: int
                    138: ciphers_valid(const char *names)
                    139: {
1.32      markus    140:        Cipher *c;
1.69      avsm      141:        char *cipher_list, *cp;
1.24      markus    142:        char *p;
                    143:
1.27      markus    144:        if (names == NULL || strcmp(names, "") == 0)
1.24      markus    145:                return 0;
1.69      avsm      146:        cipher_list = cp = xstrdup(names);
1.32      markus    147:        for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48      deraadt   148:            (p = strsep(&cp, CIPHER_SEP))) {
1.32      markus    149:                c = cipher_by_name(p);
                    150:                if (c == NULL || c->number != SSH_CIPHER_SSH2) {
                    151:                        debug("bad cipher %s [%s]", p, names);
1.69      avsm      152:                        xfree(cipher_list);
1.24      markus    153:                        return 0;
1.32      markus    154:                } else {
1.36      markus    155:                        debug3("cipher ok: %s [%s]", p, names);
1.24      markus    156:                }
                    157:        }
1.36      markus    158:        debug3("ciphers ok: [%s]", names);
1.69      avsm      159:        xfree(cipher_list);
1.24      markus    160:        return 1;
                    161: }
                    162:
1.18      markus    163: /*
                    164:  * Parses the name of the cipher.  Returns the number of the corresponding
                    165:  * cipher, or -1 on error.
                    166:  */
1.1       deraadt   167:
1.4       provos    168: int
                    169: cipher_number(const char *name)
1.1       deraadt   170: {
1.32      markus    171:        Cipher *c;
1.27      markus    172:        if (name == NULL)
                    173:                return -1;
1.71.2.1  brad      174:        for (c = ciphers; c->name != NULL; c++)
                    175:                if (strcasecmp(c->name, name) == 0)
                    176:                        return c->number;
                    177:        return -1;
1.1       deraadt   178: }
                    179:
1.32      markus    180: char *
                    181: cipher_name(int id)
1.1       deraadt   182: {
1.32      markus    183:        Cipher *c = cipher_by_number(id);
                    184:        return (c==NULL) ? "<unknown>" : c->name;
1.1       deraadt   185: }
                    186:
1.26      markus    187: void
1.52      markus    188: cipher_init(CipherContext *cc, Cipher *cipher,
                    189:     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69      avsm      190:     int do_encrypt)
1.16      markus    191: {
1.52      markus    192:        static int dowarn = 1;
                    193:        const EVP_CIPHER *type;
                    194:        int klen;
1.71.2.2! brad      195:        u_char *junk, *discard;
1.52      markus    196:
                    197:        if (cipher->number == SSH_CIPHER_DES) {
                    198:                if (dowarn) {
                    199:                        error("Warning: use of DES is strongly discouraged "
                    200:                            "due to cryptographic weaknesses");
                    201:                        dowarn = 0;
                    202:                }
                    203:                if (keylen > 8)
                    204:                        keylen = 8;
                    205:        }
                    206:        cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
                    207:
1.32      markus    208:        if (keylen < cipher->key_len)
                    209:                fatal("cipher_init: key length %d is insufficient for %s.",
                    210:                    keylen, cipher->name);
                    211:        if (iv != NULL && ivlen < cipher->block_size)
                    212:                fatal("cipher_init: iv length %d is insufficient for %s.",
                    213:                    ivlen, cipher->name);
                    214:        cc->cipher = cipher;
1.52      markus    215:
                    216:        type = (*cipher->evptype)();
                    217:
                    218:        EVP_CIPHER_CTX_init(&cc->evp);
                    219:        if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69      avsm      220:            (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52      markus    221:                fatal("cipher_init: EVP_CipherInit failed for %s",
                    222:                    cipher->name);
                    223:        klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.71.2.2! brad      224:        if (klen > 0 && keylen != (u_int)klen) {
1.62      markus    225:                debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52      markus    226:                if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
                    227:                        fatal("cipher_init: set keylen failed (%d -> %d)",
                    228:                            klen, keylen);
                    229:        }
                    230:        if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
                    231:                fatal("cipher_init: EVP_CipherInit: set key failed for %s",
                    232:                    cipher->name);
1.71.2.2! brad      233:
        !           234:        if (cipher->discard_len > 0) {
        !           235:                junk = xmalloc(cipher->discard_len);
        !           236:                discard = xmalloc(cipher->discard_len);
        !           237:                if (EVP_Cipher(&cc->evp, discard, junk,
        !           238:                    cipher->discard_len) == 0)
        !           239:                        fatal("evp_crypt: EVP_Cipher failed during discard");
        !           240:                memset(discard, 0, cipher->discard_len);
        !           241:                xfree(junk);
        !           242:                xfree(discard);
        !           243:        }
1.1       deraadt   244: }
1.21      markus    245:
1.26      markus    246: void
1.51      markus    247: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.32      markus    248: {
                    249:        if (len % cc->cipher->block_size)
                    250:                fatal("cipher_encrypt: bad plaintext length %d", len);
1.52      markus    251:        if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
                    252:                fatal("evp_crypt: EVP_Cipher failed");
1.21      markus    253: }
                    254:
1.26      markus    255: void
1.51      markus    256: cipher_cleanup(CipherContext *cc)
1.16      markus    257: {
1.52      markus    258:        if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
                    259:                error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16      markus    260: }
1.1       deraadt   261:
1.32      markus    262: /*
                    263:  * Selects the cipher, and keys if by computing the MD5 checksum of the
                    264:  * passphrase and using the resulting 16 bytes as the key.
                    265:  */
1.1       deraadt   266:
1.26      markus    267: void
1.32      markus    268: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.69      avsm      269:     const char *passphrase, int do_encrypt)
1.16      markus    270: {
1.32      markus    271:        MD5_CTX md;
1.41      markus    272:        u_char digest[16];
1.32      markus    273:
                    274:        MD5_Init(&md);
                    275:        MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
                    276:        MD5_Final(digest, &md);
1.16      markus    277:
1.69      avsm      278:        cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16      markus    279:
1.32      markus    280:        memset(digest, 0, sizeof(digest));
                    281:        memset(&md, 0, sizeof(md));
1.52      markus    282: }
1.53      markus    283:
1.54      markus    284: /*
1.53      markus    285:  * Exports an IV from the CipherContext required to export the key
                    286:  * state back from the unprivileged child to the privileged parent
                    287:  * process.
                    288:  */
                    289:
                    290: int
1.66      jakob     291: cipher_get_keyiv_len(const CipherContext *cc)
1.53      markus    292: {
                    293:        Cipher *c = cc->cipher;
                    294:        int ivlen;
                    295:
                    296:        if (c->number == SSH_CIPHER_3DES)
                    297:                ivlen = 24;
                    298:        else
                    299:                ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    300:        return (ivlen);
                    301: }
                    302:
                    303: void
                    304: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
                    305: {
                    306:        Cipher *c = cc->cipher;
                    307:        int evplen;
                    308:
                    309:        switch (c->number) {
                    310:        case SSH_CIPHER_SSH2:
                    311:        case SSH_CIPHER_DES:
                    312:        case SSH_CIPHER_BLOWFISH:
                    313:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.71.2.2! brad      314:                if (evplen <= 0)
1.53      markus    315:                        return;
1.71.2.2! brad      316:                if ((u_int)evplen != len)
1.58      markus    317:                        fatal("%s: wrong iv length %d != %d", __func__,
1.53      markus    318:                            evplen, len);
1.65      markus    319:                if (c->evptype == evp_aes_128_ctr)
                    320:                        ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
                    321:                else
1.63      markus    322:                        memcpy(iv, cc->evp.iv, len);
                    323:                break;
                    324:        case SSH_CIPHER_3DES:
                    325:                ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53      markus    326:                break;
                    327:        default:
1.58      markus    328:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    329:        }
                    330: }
                    331:
                    332: void
                    333: cipher_set_keyiv(CipherContext *cc, u_char *iv)
                    334: {
                    335:        Cipher *c = cc->cipher;
                    336:        int evplen = 0;
                    337:
                    338:        switch (c->number) {
                    339:        case SSH_CIPHER_SSH2:
                    340:        case SSH_CIPHER_DES:
                    341:        case SSH_CIPHER_BLOWFISH:
                    342:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    343:                if (evplen == 0)
                    344:                        return;
1.65      markus    345:                if (c->evptype == evp_aes_128_ctr)
                    346:                        ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
                    347:                else
1.63      markus    348:                        memcpy(cc->evp.iv, iv, evplen);
                    349:                break;
                    350:        case SSH_CIPHER_3DES:
                    351:                ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53      markus    352:                break;
                    353:        default:
1.58      markus    354:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    355:        }
                    356: }
                    357:
                    358: #define EVP_X_STATE(evp)       (evp).cipher_data
                    359: #define EVP_X_STATE_LEN(evp)   (evp).cipher->ctx_size
                    360:
                    361: int
1.66      jakob     362: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53      markus    363: {
                    364:        Cipher *c = cc->cipher;
1.59      markus    365:        int plen = 0;
1.53      markus    366:
1.67      hshoexer  367:        if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.59      markus    368:                plen = EVP_X_STATE_LEN(cc->evp);
1.53      markus    369:                if (dat == NULL)
1.59      markus    370:                        return (plen);
                    371:                memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53      markus    372:        }
                    373:        return (plen);
                    374: }
                    375:
                    376: void
                    377: cipher_set_keycontext(CipherContext *cc, u_char *dat)
                    378: {
                    379:        Cipher *c = cc->cipher;
                    380:        int plen;
                    381:
1.67      hshoexer  382:        if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.53      markus    383:                plen = EVP_X_STATE_LEN(cc->evp);
                    384:                memcpy(EVP_X_STATE(cc->evp), dat, plen);
                    385:        }
1.1       deraadt   386: }