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

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.
                     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.33      markus     38: RCSID("$OpenBSD: cipher.c,v 1.32 2000/10/11 20:27:23 markus Exp $");
1.1       deraadt    39:
                     40: #include "ssh.h"
1.24      markus     41: #include "xmalloc.h"
1.8       deraadt    42:
1.25      markus     43: #include <openssl/md5.h>
1.1       deraadt    44:
1.32      markus     45:
                     46: /* no encryption */
                     47: void
                     48: none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                     49: {
                     50: }
                     51: void
                     52: none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
                     53: {
                     54: }
                     55: void
                     56: none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                     57: {
                     58:        memcpy(dest, src, len);
                     59: }
                     60:
                     61: /* DES */
                     62: void
                     63: des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                     64: {
1.34    ! markus     65:        static int dowarn = 1;
        !            66:        if (dowarn) {
        !            67:                error("Warning: use of DES is strongly discouraged "
        !            68:                    "due to cryptographic weaknesses");
        !            69:                dowarn = 0;
        !            70:        }
1.32      markus     71:        des_set_key((void *)key, cc->u.des.key);
                     72: }
                     73: void
                     74: des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
                     75: {
                     76:        memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
                     77: }
                     78: void
                     79: des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                     80: {
                     81:        des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
                     82:            DES_ENCRYPT);
                     83: }
                     84: void
                     85: des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                     86: {
                     87:        des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
                     88:            DES_DECRYPT);
                     89: }
                     90:
                     91: /* 3DES */
                     92: void
                     93: des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                     94: {
                     95:        des_set_key((void *) key, cc->u.des3.key1);
                     96:        des_set_key((void *) (key+8), cc->u.des3.key2);
                     97:        des_set_key((void *) (key+16), cc->u.des3.key3);
                     98: }
                     99: void
                    100: des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
                    101: {
                    102:        memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2));
                    103:        memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3));
                    104:        if (iv == NULL)
                    105:                return;
                    106:        memcpy(cc->u.des3.iv3, (char *)iv, 8);
                    107: }
                    108: void
                    109: des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                    110: {
                    111:        des_ede3_cbc_encrypt(src, dest, len,
                    112:            cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
                    113:            &cc->u.des3.iv3, DES_ENCRYPT);
                    114: }
                    115: void
                    116: des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                    117: {
                    118:        des_ede3_cbc_encrypt(src, dest, len,
                    119:            cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
                    120:            &cc->u.des3.iv3, DES_DECRYPT);
                    121: }
                    122:
1.1       deraadt   123: /*
1.24      markus    124:  * This is used by SSH1:
                    125:  *
1.23      deraadt   126:  * What kind of triple DES are these 2 routines?
1.1       deraadt   127:  *
                    128:  * Why is there a redundant initialization vector?
                    129:  *
                    130:  * If only iv3 was used, then, this would till effect have been
                    131:  * outer-cbc. However, there is also a private iv1 == iv2 which
                    132:  * perhaps makes differential analysis easier. On the other hand, the
                    133:  * private iv1 probably makes the CRC-32 attack ineffective. This is a
                    134:  * result of that there is no longer any known iv1 to use when
                    135:  * choosing the X block.
                    136:  */
                    137: void
1.32      markus    138: des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                    139: {
                    140:        des_set_key((void *) key, cc->u.des3.key1);
                    141:        des_set_key((void *) (key+8), cc->u.des3.key2);
                    142:        if (keylen <= 16)
                    143:                des_set_key((void *) key, cc->u.des3.key3);
                    144:        else
                    145:                des_set_key((void *) (key+16), cc->u.des3.key3);
                    146: }
                    147: void
                    148: des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    149:     u_int len)
1.1       deraadt   150: {
1.16      markus    151:        des_cblock iv1;
1.32      markus    152:        des_cblock *iv2 = &cc->u.des3.iv2;
                    153:        des_cblock *iv3 = &cc->u.des3.iv3;
1.1       deraadt   154:
1.16      markus    155:        memcpy(&iv1, iv2, 8);
1.1       deraadt   156:
1.32      markus    157:        des_cbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT);
1.16      markus    158:        memcpy(&iv1, dest + len - 8, 8);
1.1       deraadt   159:
1.32      markus    160:        des_cbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT);
1.16      markus    161:        memcpy(iv2, &iv1, 8);   /* Note how iv1 == iv2 on entry and exit. */
1.1       deraadt   162:
1.32      markus    163:        des_cbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT);
1.16      markus    164:        memcpy(iv3, dest + len - 8, 8);
1.1       deraadt   165: }
                    166: void
1.32      markus    167: des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    168:     u_int len)
1.1       deraadt   169: {
1.16      markus    170:        des_cblock iv1;
1.32      markus    171:        des_cblock *iv2 = &cc->u.des3.iv2;
                    172:        des_cblock *iv3 = &cc->u.des3.iv3;
1.1       deraadt   173:
1.16      markus    174:        memcpy(&iv1, iv2, 8);
1.1       deraadt   175:
1.32      markus    176:        des_cbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT);
1.16      markus    177:        memcpy(iv3, src + len - 8, 8);
1.1       deraadt   178:
1.32      markus    179:        des_cbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT);
1.16      markus    180:        memcpy(iv2, dest + len - 8, 8);
1.1       deraadt   181:
1.32      markus    182:        des_cbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT);
1.16      markus    183:        /* memcpy(&iv1, iv2, 8); */
                    184:        /* Note how iv1 == iv2 on entry and exit. */
1.1       deraadt   185: }
                    186:
1.32      markus    187: /* Blowfish */
                    188: void
                    189: blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                    190: {
                    191:        BF_set_key(&cc->u.bf.key, keylen, (unsigned char *)key);
                    192: }
                    193: void
                    194: blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
                    195: {
                    196:        if (iv == NULL)
                    197:                memset(cc->u.bf.iv, 0, 8);
                    198:        else
                    199:                memcpy(cc->u.bf.iv, (char *)iv, 8);
                    200: }
                    201: void
                    202: blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    203:      u_int len)
                    204: {
                    205:        BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
                    206:            BF_ENCRYPT);
                    207: }
                    208: void
                    209: blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    210:      u_int len)
                    211: {
                    212:        BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
                    213:            BF_DECRYPT);
                    214: }
                    215:
1.1       deraadt   216: /*
1.24      markus    217:  * SSH1 uses a variation on Blowfish, all bytes must be swapped before
1.1       deraadt   218:  * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
                    219:  */
1.16      markus    220: static void
1.1       deraadt   221: swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
                    222: {
1.16      markus    223:        /* dst must be properly aligned. */
                    224:        u_int32_t *dst = (u_int32_t *) dst_;
                    225:        union {
                    226:                u_int32_t i;
                    227:                char c[4];
                    228:        } t;
                    229:
                    230:        /* Process 8 bytes every lap. */
                    231:        for (n = n / 8; n > 0; n--) {
                    232:                t.c[3] = *src++;
                    233:                t.c[2] = *src++;
                    234:                t.c[1] = *src++;
                    235:                t.c[0] = *src++;
                    236:                *dst++ = t.i;
                    237:
                    238:                t.c[3] = *src++;
                    239:                t.c[2] = *src++;
                    240:                t.c[1] = *src++;
                    241:                t.c[0] = *src++;
                    242:                *dst++ = t.i;
                    243:        }
1.1       deraadt   244: }
                    245:
1.32      markus    246: void
                    247: blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    248:     u_int len)
                    249: {
                    250:        swap_bytes(src, dest, len);
                    251:        BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
                    252:            BF_ENCRYPT);
                    253:        swap_bytes(dest, dest, len);
                    254: }
                    255: void
                    256: blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
                    257:     u_int len)
1.4       provos    258: {
1.32      markus    259:        swap_bytes(src, dest, len);
                    260:        BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
                    261:            BF_DECRYPT);
                    262:        swap_bytes(dest, dest, len);
                    263: }
1.1       deraadt   264:
1.32      markus    265: /* alleged rc4 */
                    266: void
                    267: arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                    268: {
                    269:        RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
                    270: }
                    271: void
                    272: arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                    273: {
                    274:        RC4(&cc->u.rc4, len, (u_char *)src, dest);
                    275: }
1.1       deraadt   276:
1.32      markus    277: /* CAST */
                    278: void
                    279: cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
                    280: {
                    281:        CAST_set_key(&cc->u.cast.key, keylen, (unsigned char *) key);
                    282: }
                    283: void
                    284: cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
1.1       deraadt   285: {
1.32      markus    286:        if (iv == NULL)
                    287:                fatal("no IV for %s.", cc->cipher->name);
                    288:        memcpy(cc->u.cast.iv, (char *)iv, 8);
1.22      markus    289: }
1.32      markus    290: void
                    291: cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.22      markus    292: {
1.32      markus    293:        CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
                    294:            CAST_ENCRYPT);
1.1       deraadt   295: }
1.32      markus    296: void
                    297: cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.22      markus    298: {
1.32      markus    299:        CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
                    300:            CAST_DECRYPT);
1.22      markus    301: }
1.1       deraadt   302:
1.32      markus    303: /*--*/
                    304:
                    305: Cipher ciphers[] = {
                    306:        { "none",
                    307:                SSH_CIPHER_NONE, 8, 0,
                    308:                none_setkey, none_setiv,
                    309:                none_crypt, none_crypt },
1.34    ! markus    310:        { "des",
        !           311:                SSH_CIPHER_DES, 8, 8,
        !           312:                des_ssh1_setkey, des_ssh1_setiv,
        !           313:                des_ssh1_encrypt, des_ssh1_decrypt },
1.32      markus    314:        { "3des",
                    315:                SSH_CIPHER_3DES, 8, 16,
                    316:                des3_ssh1_setkey, des3_setiv,
                    317:                des3_ssh1_encrypt, des3_ssh1_decrypt },
                    318:        { "blowfish",
                    319:                SSH_CIPHER_BLOWFISH, 8, 16,
                    320:                blowfish_setkey, blowfish_setiv,
                    321:                blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
                    322:
                    323:        { "3des-cbc",
                    324:                SSH_CIPHER_SSH2, 8, 24,
                    325:                des3_setkey, des3_setiv,
                    326:                des3_cbc_encrypt, des3_cbc_decrypt },
                    327:        { "blowfish-cbc",
                    328:                SSH_CIPHER_SSH2, 8, 16,
                    329:                blowfish_setkey, blowfish_setiv,
                    330:                blowfish_cbc_encrypt, blowfish_cbc_decrypt },
                    331:        { "cast128-cbc",
                    332:                SSH_CIPHER_SSH2, 8, 16,
                    333:                cast_setkey, cast_setiv,
                    334:                cast_cbc_encrypt, cast_cbc_decrypt },
                    335:        { "arcfour",
                    336:                SSH_CIPHER_SSH2, 8, 16,
                    337:                arcfour_setkey, none_setiv,
                    338:                arcfour_crypt, arcfour_crypt },
                    339:         { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
                    340: };
                    341:
                    342: /*--*/
1.1       deraadt   343:
1.32      markus    344: unsigned int
1.34    ! markus    345: cipher_mask_ssh1(int client)
1.1       deraadt   346: {
1.32      markus    347:        unsigned int mask = 0;
1.34    ! markus    348:        mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
        !           349:        mask |= 1 << SSH_CIPHER_BLOWFISH;
        !           350:        if (client) {
        !           351:                mask |= 1 << SSH_CIPHER_DES;
1.32      markus    352:        }
                    353:        return mask;
1.1       deraadt   354: }
                    355:
1.32      markus    356: Cipher *
                    357: cipher_by_name(const char *name)
                    358: {
                    359:        Cipher *c;
                    360:        for (c = ciphers; c->name != NULL; c++)
                    361:                if (strcasecmp(c->name, name) == 0)
                    362:                        return c;
                    363:        return NULL;
                    364: }
                    365:
                    366: Cipher *
                    367: cipher_by_number(int id)
                    368: {
                    369:        Cipher *c;
                    370:        for (c = ciphers; c->name != NULL; c++)
                    371:                if (c->number == id)
                    372:                        return c;
                    373:        return NULL;
                    374: }
1.24      markus    375:
                    376: #define        CIPHER_SEP      ","
                    377: int
                    378: ciphers_valid(const char *names)
                    379: {
1.32      markus    380:        Cipher *c;
1.29      ho        381:        char *ciphers, *cp;
1.24      markus    382:        char *p;
                    383:
1.27      markus    384:        if (names == NULL || strcmp(names, "") == 0)
1.24      markus    385:                return 0;
1.29      ho        386:        ciphers = cp = xstrdup(names);
1.32      markus    387:        for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.29      ho        388:             (p = strsep(&cp, CIPHER_SEP))) {
1.32      markus    389:                c = cipher_by_name(p);
                    390:                if (c == NULL || c->number != SSH_CIPHER_SSH2) {
                    391:                        debug("bad cipher %s [%s]", p, names);
1.24      markus    392:                        xfree(ciphers);
                    393:                        return 0;
1.32      markus    394:                } else {
                    395:                        debug("cipher ok: %s [%s]", p, names);
1.24      markus    396:                }
                    397:        }
1.32      markus    398:        debug("ciphers ok: [%s]", names);
1.24      markus    399:        xfree(ciphers);
                    400:        return 1;
                    401: }
                    402:
1.18      markus    403: /*
                    404:  * Parses the name of the cipher.  Returns the number of the corresponding
                    405:  * cipher, or -1 on error.
                    406:  */
1.1       deraadt   407:
1.4       provos    408: int
                    409: cipher_number(const char *name)
1.1       deraadt   410: {
1.32      markus    411:        Cipher *c;
1.27      markus    412:        if (name == NULL)
                    413:                return -1;
1.32      markus    414:        c = cipher_by_name(name);
                    415:        return (c==NULL) ? -1 : c->number;
1.1       deraadt   416: }
                    417:
1.32      markus    418: char *
                    419: cipher_name(int id)
1.1       deraadt   420: {
1.32      markus    421:        Cipher *c = cipher_by_number(id);
                    422:        return (c==NULL) ? "<unknown>" : c->name;
1.1       deraadt   423: }
                    424:
1.26      markus    425: void
1.32      markus    426: cipher_init(CipherContext *cc, Cipher *cipher,
                    427:     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
1.16      markus    428: {
1.32      markus    429:        if (keylen < cipher->key_len)
                    430:                fatal("cipher_init: key length %d is insufficient for %s.",
                    431:                    keylen, cipher->name);
                    432:        if (iv != NULL && ivlen < cipher->block_size)
                    433:                fatal("cipher_init: iv length %d is insufficient for %s.",
                    434:                    ivlen, cipher->name);
                    435:        cc->cipher = cipher;
                    436:        cipher->setkey(cc, key, keylen);
                    437:        cipher->setiv(cc, iv, ivlen);
1.1       deraadt   438: }
1.21      markus    439:
1.26      markus    440: void
1.32      markus    441: cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
                    442: {
                    443:        if (len % cc->cipher->block_size)
                    444:                fatal("cipher_encrypt: bad plaintext length %d", len);
                    445:        cc->cipher->encrypt(cc, dest, src, len);
1.21      markus    446: }
                    447:
1.26      markus    448: void
1.32      markus    449: cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.16      markus    450: {
1.32      markus    451:        if (len % cc->cipher->block_size)
                    452:                fatal("cipher_decrypt: bad ciphertext length %d", len);
                    453:        cc->cipher->decrypt(cc, dest, src, len);
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.32      markus    462: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
                    463:     const char *passphrase)
1.16      markus    464: {
1.32      markus    465:        MD5_CTX md;
                    466:        unsigned char digest[16];
                    467:
                    468:        MD5_Init(&md);
                    469:        MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
                    470:        MD5_Final(digest, &md);
1.16      markus    471:
1.32      markus    472:        cipher_init(cc, cipher, digest, 16, NULL, 0);
1.16      markus    473:
1.32      markus    474:        memset(digest, 0, sizeof(digest));
                    475:        memset(&md, 0, sizeof(md));
1.1       deraadt   476: }