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

1.80    ! stevesk     1: /* $OpenBSD: cipher.c,v 1.79 2006/03/25 13:17:01 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:
                     38: #include "includes.h"
                     39:
1.80    ! stevesk    40: #include <openssl/md5.h>
        !            41:
        !            42: #include <string.h>
        !            43:
1.24      markus     44: #include "xmalloc.h"
1.42      markus     45: #include "log.h"
                     46: #include "cipher.h"
1.57      markus     47:
1.64      markus     48: extern const EVP_CIPHER *evp_ssh1_bf(void);
                     49: extern const EVP_CIPHER *evp_ssh1_3des(void);
                     50: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.65      markus     51: extern const EVP_CIPHER *evp_aes_128_ctr(void);
                     52: extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
1.1       deraadt    53:
1.51      markus     54: struct Cipher {
                     55:        char    *name;
                     56:        int     number;         /* for ssh1 only */
                     57:        u_int   block_size;
                     58:        u_int   key_len;
1.74      djm        59:        u_int   discard_len;
1.56      markus     60:        const EVP_CIPHER        *(*evptype)(void);
1.52      markus     61: } ciphers[] = {
1.74      djm        62:        { "none",               SSH_CIPHER_NONE, 8, 0, 0, EVP_enc_null },
                     63:        { "des",                SSH_CIPHER_DES, 8, 8, 0, EVP_des_cbc },
                     64:        { "3des",               SSH_CIPHER_3DES, 8, 16, 0, evp_ssh1_3des },
                     65:        { "blowfish",           SSH_CIPHER_BLOWFISH, 8, 32, 0, evp_ssh1_bf },
                     66:
                     67:        { "3des-cbc",           SSH_CIPHER_SSH2, 8, 24, 0, EVP_des_ede3_cbc },
                     68:        { "blowfish-cbc",       SSH_CIPHER_SSH2, 8, 16, 0, EVP_bf_cbc },
                     69:        { "cast128-cbc",        SSH_CIPHER_SSH2, 8, 16, 0, EVP_cast5_cbc },
                     70:        { "arcfour",            SSH_CIPHER_SSH2, 8, 16, 0, EVP_rc4 },
                     71:        { "arcfour128",         SSH_CIPHER_SSH2, 8, 16, 1536, EVP_rc4 },
                     72:        { "arcfour256",         SSH_CIPHER_SSH2, 8, 32, 1536, EVP_rc4 },
                     73:        { "aes128-cbc",         SSH_CIPHER_SSH2, 16, 16, 0, EVP_aes_128_cbc },
                     74:        { "aes192-cbc",         SSH_CIPHER_SSH2, 16, 24, 0, EVP_aes_192_cbc },
                     75:        { "aes256-cbc",         SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
1.57      markus     76:        { "rijndael-cbc@lysator.liu.se",
1.74      djm        77:                                SSH_CIPHER_SSH2, 16, 32, 0, EVP_aes_256_cbc },
                     78:        { "aes128-ctr",         SSH_CIPHER_SSH2, 16, 16, 0, evp_aes_128_ctr },
                     79:        { "aes192-ctr",         SSH_CIPHER_SSH2, 16, 24, 0, evp_aes_128_ctr },
                     80:        { "aes256-ctr",         SSH_CIPHER_SSH2, 16, 32, 0, evp_aes_128_ctr },
                     81:        { "acss@openssh.org",   SSH_CIPHER_SSH2, 16, 5, 0, EVP_acss },
1.47      markus     82:
1.75      dtucker    83:        { NULL,                 SSH_CIPHER_INVALID, 0, 0, 0, NULL }
1.32      markus     84: };
                     85:
                     86: /*--*/
1.1       deraadt    87:
1.54      markus     88: u_int
1.66      jakob      89: cipher_blocksize(const Cipher *c)
1.51      markus     90: {
                     91:        return (c->block_size);
                     92: }
1.60      deraadt    93:
1.54      markus     94: u_int
1.66      jakob      95: cipher_keylen(const Cipher *c)
1.51      markus     96: {
                     97:        return (c->key_len);
                     98: }
1.60      deraadt    99:
1.54      markus    100: u_int
1.66      jakob     101: cipher_get_number(const Cipher *c)
1.53      markus    102: {
                    103:        return (c->number);
                    104: }
1.51      markus    105:
1.41      markus    106: u_int
1.34      markus    107: cipher_mask_ssh1(int client)
1.1       deraadt   108: {
1.41      markus    109:        u_int mask = 0;
1.48      deraadt   110:        mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
1.34      markus    111:        mask |= 1 << SSH_CIPHER_BLOWFISH;
                    112:        if (client) {
                    113:                mask |= 1 << SSH_CIPHER_DES;
1.32      markus    114:        }
                    115:        return mask;
1.1       deraadt   116: }
                    117:
1.32      markus    118: Cipher *
                    119: cipher_by_name(const char *name)
                    120: {
                    121:        Cipher *c;
                    122:        for (c = ciphers; c->name != NULL; c++)
1.73      djm       123:                if (strcmp(c->name, name) == 0)
1.32      markus    124:                        return c;
                    125:        return NULL;
                    126: }
                    127:
                    128: Cipher *
                    129: cipher_by_number(int id)
                    130: {
                    131:        Cipher *c;
                    132:        for (c = ciphers; c->name != NULL; c++)
                    133:                if (c->number == id)
                    134:                        return c;
                    135:        return NULL;
                    136: }
1.24      markus    137:
                    138: #define        CIPHER_SEP      ","
                    139: int
                    140: ciphers_valid(const char *names)
                    141: {
1.32      markus    142:        Cipher *c;
1.69      avsm      143:        char *cipher_list, *cp;
1.24      markus    144:        char *p;
                    145:
1.27      markus    146:        if (names == NULL || strcmp(names, "") == 0)
1.24      markus    147:                return 0;
1.69      avsm      148:        cipher_list = cp = xstrdup(names);
1.32      markus    149:        for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48      deraadt   150:            (p = strsep(&cp, CIPHER_SEP))) {
1.32      markus    151:                c = cipher_by_name(p);
                    152:                if (c == NULL || c->number != SSH_CIPHER_SSH2) {
                    153:                        debug("bad cipher %s [%s]", p, names);
1.69      avsm      154:                        xfree(cipher_list);
1.24      markus    155:                        return 0;
1.32      markus    156:                } else {
1.36      markus    157:                        debug3("cipher ok: %s [%s]", p, names);
1.24      markus    158:                }
                    159:        }
1.36      markus    160:        debug3("ciphers ok: [%s]", names);
1.69      avsm      161:        xfree(cipher_list);
1.24      markus    162:        return 1;
                    163: }
                    164:
1.18      markus    165: /*
                    166:  * Parses the name of the cipher.  Returns the number of the corresponding
                    167:  * cipher, or -1 on error.
                    168:  */
1.1       deraadt   169:
1.4       provos    170: int
                    171: cipher_number(const char *name)
1.1       deraadt   172: {
1.32      markus    173:        Cipher *c;
1.27      markus    174:        if (name == NULL)
                    175:                return -1;
1.73      djm       176:        for (c = ciphers; c->name != NULL; c++)
                    177:                if (strcasecmp(c->name, name) == 0)
                    178:                        return c->number;
                    179:        return -1;
1.1       deraadt   180: }
                    181:
1.32      markus    182: char *
                    183: cipher_name(int id)
1.1       deraadt   184: {
1.32      markus    185:        Cipher *c = cipher_by_number(id);
                    186:        return (c==NULL) ? "<unknown>" : c->name;
1.1       deraadt   187: }
                    188:
1.26      markus    189: void
1.52      markus    190: cipher_init(CipherContext *cc, Cipher *cipher,
                    191:     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69      avsm      192:     int do_encrypt)
1.16      markus    193: {
1.52      markus    194:        static int dowarn = 1;
                    195:        const EVP_CIPHER *type;
                    196:        int klen;
1.74      djm       197:        u_char *junk, *discard;
1.52      markus    198:
                    199:        if (cipher->number == SSH_CIPHER_DES) {
                    200:                if (dowarn) {
                    201:                        error("Warning: use of DES is strongly discouraged "
                    202:                            "due to cryptographic weaknesses");
                    203:                        dowarn = 0;
                    204:                }
                    205:                if (keylen > 8)
                    206:                        keylen = 8;
                    207:        }
                    208:        cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
                    209:
1.32      markus    210:        if (keylen < cipher->key_len)
                    211:                fatal("cipher_init: key length %d is insufficient for %s.",
                    212:                    keylen, cipher->name);
                    213:        if (iv != NULL && ivlen < cipher->block_size)
                    214:                fatal("cipher_init: iv length %d is insufficient for %s.",
                    215:                    ivlen, cipher->name);
                    216:        cc->cipher = cipher;
1.52      markus    217:
                    218:        type = (*cipher->evptype)();
                    219:
                    220:        EVP_CIPHER_CTX_init(&cc->evp);
                    221:        if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69      avsm      222:            (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52      markus    223:                fatal("cipher_init: EVP_CipherInit failed for %s",
                    224:                    cipher->name);
                    225:        klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.76      djm       226:        if (klen > 0 && keylen != (u_int)klen) {
1.62      markus    227:                debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52      markus    228:                if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
                    229:                        fatal("cipher_init: set keylen failed (%d -> %d)",
                    230:                            klen, keylen);
                    231:        }
                    232:        if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
                    233:                fatal("cipher_init: EVP_CipherInit: set key failed for %s",
                    234:                    cipher->name);
1.74      djm       235:
1.77      djm       236:        if (cipher->discard_len > 0) {
1.74      djm       237:                junk = xmalloc(cipher->discard_len);
                    238:                discard = xmalloc(cipher->discard_len);
                    239:                if (EVP_Cipher(&cc->evp, discard, junk,
                    240:                    cipher->discard_len) == 0)
                    241:                        fatal("evp_crypt: EVP_Cipher failed during discard");
                    242:                memset(discard, 0, cipher->discard_len);
                    243:                xfree(junk);
                    244:                xfree(discard);
                    245:        }
1.1       deraadt   246: }
1.21      markus    247:
1.26      markus    248: void
1.51      markus    249: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.32      markus    250: {
                    251:        if (len % cc->cipher->block_size)
                    252:                fatal("cipher_encrypt: bad plaintext length %d", len);
1.52      markus    253:        if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
                    254:                fatal("evp_crypt: EVP_Cipher failed");
1.21      markus    255: }
                    256:
1.26      markus    257: void
1.51      markus    258: cipher_cleanup(CipherContext *cc)
1.16      markus    259: {
1.52      markus    260:        if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
                    261:                error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16      markus    262: }
1.1       deraadt   263:
1.32      markus    264: /*
                    265:  * Selects the cipher, and keys if by computing the MD5 checksum of the
                    266:  * passphrase and using the resulting 16 bytes as the key.
                    267:  */
1.1       deraadt   268:
1.26      markus    269: void
1.32      markus    270: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.69      avsm      271:     const char *passphrase, int do_encrypt)
1.16      markus    272: {
1.32      markus    273:        MD5_CTX md;
1.41      markus    274:        u_char digest[16];
1.32      markus    275:
                    276:        MD5_Init(&md);
                    277:        MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
                    278:        MD5_Final(digest, &md);
1.16      markus    279:
1.69      avsm      280:        cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16      markus    281:
1.32      markus    282:        memset(digest, 0, sizeof(digest));
                    283:        memset(&md, 0, sizeof(md));
1.52      markus    284: }
1.53      markus    285:
1.54      markus    286: /*
1.53      markus    287:  * Exports an IV from the CipherContext required to export the key
                    288:  * state back from the unprivileged child to the privileged parent
                    289:  * process.
                    290:  */
                    291:
                    292: int
1.66      jakob     293: cipher_get_keyiv_len(const CipherContext *cc)
1.53      markus    294: {
                    295:        Cipher *c = cc->cipher;
                    296:        int ivlen;
                    297:
                    298:        if (c->number == SSH_CIPHER_3DES)
                    299:                ivlen = 24;
                    300:        else
                    301:                ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    302:        return (ivlen);
                    303: }
                    304:
                    305: void
                    306: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
                    307: {
                    308:        Cipher *c = cc->cipher;
                    309:        int evplen;
                    310:
                    311:        switch (c->number) {
                    312:        case SSH_CIPHER_SSH2:
                    313:        case SSH_CIPHER_DES:
                    314:        case SSH_CIPHER_BLOWFISH:
                    315:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76      djm       316:                if (evplen <= 0)
1.53      markus    317:                        return;
1.76      djm       318:                if ((u_int)evplen != len)
1.58      markus    319:                        fatal("%s: wrong iv length %d != %d", __func__,
1.53      markus    320:                            evplen, len);
1.65      markus    321:                if (c->evptype == evp_aes_128_ctr)
                    322:                        ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
                    323:                else
1.63      markus    324:                        memcpy(iv, cc->evp.iv, len);
                    325:                break;
                    326:        case SSH_CIPHER_3DES:
                    327:                ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53      markus    328:                break;
                    329:        default:
1.58      markus    330:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    331:        }
                    332: }
                    333:
                    334: void
                    335: cipher_set_keyiv(CipherContext *cc, u_char *iv)
                    336: {
                    337:        Cipher *c = cc->cipher;
                    338:        int evplen = 0;
                    339:
                    340:        switch (c->number) {
                    341:        case SSH_CIPHER_SSH2:
                    342:        case SSH_CIPHER_DES:
                    343:        case SSH_CIPHER_BLOWFISH:
                    344:                evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
                    345:                if (evplen == 0)
                    346:                        return;
1.65      markus    347:                if (c->evptype == evp_aes_128_ctr)
                    348:                        ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
                    349:                else
1.63      markus    350:                        memcpy(cc->evp.iv, iv, evplen);
                    351:                break;
                    352:        case SSH_CIPHER_3DES:
                    353:                ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53      markus    354:                break;
                    355:        default:
1.58      markus    356:                fatal("%s: bad cipher %d", __func__, c->number);
1.53      markus    357:        }
                    358: }
                    359:
                    360: #define EVP_X_STATE(evp)       (evp).cipher_data
                    361: #define EVP_X_STATE_LEN(evp)   (evp).cipher->ctx_size
                    362:
                    363: int
1.66      jakob     364: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53      markus    365: {
                    366:        Cipher *c = cc->cipher;
1.59      markus    367:        int plen = 0;
1.53      markus    368:
1.67      hshoexer  369:        if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.59      markus    370:                plen = EVP_X_STATE_LEN(cc->evp);
1.53      markus    371:                if (dat == NULL)
1.59      markus    372:                        return (plen);
                    373:                memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53      markus    374:        }
                    375:        return (plen);
                    376: }
                    377:
                    378: void
                    379: cipher_set_keycontext(CipherContext *cc, u_char *dat)
                    380: {
                    381:        Cipher *c = cc->cipher;
                    382:        int plen;
                    383:
1.67      hshoexer  384:        if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.53      markus    385:                plen = EVP_X_STATE_LEN(cc->evp);
                    386:                memcpy(EVP_X_STATE(cc->evp), dat, plen);
                    387:        }
1.1       deraadt   388: }