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

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