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

1.1       deraadt     1: /*
1.17      deraadt     2:  *
                      3:  * cipher.c
                      4:  *
                      5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      6:  *
                      7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
                      9:  *
                     10:  * Created: Wed Apr 19 17:41:39 1995 ylo
                     11:  *
                     12:  */
1.1       deraadt    13:
                     14: #include "includes.h"
1.21    ! markus     15: RCSID("$Id: cipher.c,v 1.21 2000/03/22 13:40:45 markus Exp $");
1.1       deraadt    16:
                     17: #include "ssh.h"
                     18: #include "cipher.h"
1.8       deraadt    19:
1.12      deraadt    20: #include <ssl/md5.h>
1.1       deraadt    21:
                     22: /*
                     23:  * What kind of tripple DES are these 2 routines?
                     24:  *
                     25:  * Why is there a redundant initialization vector?
                     26:  *
                     27:  * If only iv3 was used, then, this would till effect have been
                     28:  * outer-cbc. However, there is also a private iv1 == iv2 which
                     29:  * perhaps makes differential analysis easier. On the other hand, the
                     30:  * private iv1 probably makes the CRC-32 attack ineffective. This is a
                     31:  * result of that there is no longer any known iv1 to use when
                     32:  * choosing the X block.
                     33:  */
                     34: void
                     35: SSH_3CBC_ENCRYPT(des_key_schedule ks1,
1.16      markus     36:                 des_key_schedule ks2, des_cblock * iv2,
                     37:                 des_key_schedule ks3, des_cblock * iv3,
1.19      markus     38:                 unsigned char *dest, unsigned char *src,
1.1       deraadt    39:                 unsigned int len)
                     40: {
1.16      markus     41:        des_cblock iv1;
1.1       deraadt    42:
1.16      markus     43:        memcpy(&iv1, iv2, 8);
1.1       deraadt    44:
1.16      markus     45:        des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
                     46:        memcpy(&iv1, dest + len - 8, 8);
1.1       deraadt    47:
1.16      markus     48:        des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
                     49:        memcpy(iv2, &iv1, 8);   /* Note how iv1 == iv2 on entry and exit. */
1.1       deraadt    50:
1.16      markus     51:        des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
                     52:        memcpy(iv3, dest + len - 8, 8);
1.1       deraadt    53: }
                     54:
                     55: void
                     56: SSH_3CBC_DECRYPT(des_key_schedule ks1,
1.16      markus     57:                 des_key_schedule ks2, des_cblock * iv2,
                     58:                 des_key_schedule ks3, des_cblock * iv3,
1.19      markus     59:                 unsigned char *dest, unsigned char *src,
1.1       deraadt    60:                 unsigned int len)
                     61: {
1.16      markus     62:        des_cblock iv1;
1.1       deraadt    63:
1.16      markus     64:        memcpy(&iv1, iv2, 8);
1.1       deraadt    65:
1.16      markus     66:        des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
                     67:        memcpy(iv3, src + len - 8, 8);
1.1       deraadt    68:
1.16      markus     69:        des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
                     70:        memcpy(iv2, dest + len - 8, 8);
1.1       deraadt    71:
1.16      markus     72:        des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
                     73:        /* memcpy(&iv1, iv2, 8); */
                     74:        /* Note how iv1 == iv2 on entry and exit. */
1.1       deraadt    75: }
                     76:
                     77: /*
                     78:  * SSH uses a variation on Blowfish, all bytes must be swapped before
                     79:  * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
                     80:  */
1.16      markus     81: static void
1.1       deraadt    82: swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
                     83: {
1.16      markus     84:        /* dst must be properly aligned. */
                     85:        u_int32_t *dst = (u_int32_t *) dst_;
                     86:        union {
                     87:                u_int32_t i;
                     88:                char c[4];
                     89:        } t;
                     90:
                     91:        /* Process 8 bytes every lap. */
                     92:        for (n = n / 8; n > 0; n--) {
                     93:                t.c[3] = *src++;
                     94:                t.c[2] = *src++;
                     95:                t.c[1] = *src++;
                     96:                t.c[0] = *src++;
                     97:                *dst++ = t.i;
                     98:
                     99:                t.c[3] = *src++;
                    100:                t.c[2] = *src++;
                    101:                t.c[1] = *src++;
                    102:                t.c[0] = *src++;
                    103:                *dst++ = t.i;
                    104:        }
1.1       deraadt   105: }
                    106:
1.18      markus    107: /*
                    108:  * Names of all encryption algorithms.
                    109:  * These must match the numbers defined in cipher.h.
                    110:  */
1.1       deraadt   111: static char *cipher_names[] =
1.4       provos    112: {
1.16      markus    113:        "none",
                    114:        "idea",
                    115:        "des",
                    116:        "3des",
                    117:        "tss",
                    118:        "rc4",
1.21    ! markus    119:        "blowfish",
        !           120:        "reserved",
        !           121:        "blowfish-cbc",
        !           122:        "3des-cbc",
        !           123:        "arcfour",
        !           124:        "cast128-cbc"
1.1       deraadt   125: };
                    126:
1.18      markus    127: /*
                    128:  * Returns a bit mask indicating which ciphers are supported by this
                    129:  * implementation.  The bit mask has the corresponding bit set of each
                    130:  * supported cipher.
                    131:  */
1.1       deraadt   132:
1.16      markus    133: unsigned int
                    134: cipher_mask()
1.1       deraadt   135: {
1.16      markus    136:        unsigned int mask = 0;
                    137:        mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
                    138:        mask |= 1 << SSH_CIPHER_BLOWFISH;
1.21    ! markus    139:        mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
        !           140:        mask |= 1 << SSH_CIPHER_3DES_CBC;
        !           141:        mask |= 1 << SSH_CIPHER_ARCFOUR;
        !           142:        mask |= 1 << SSH_CIPHER_CAST128_CBC;
1.16      markus    143:        return mask;
1.1       deraadt   144: }
                    145:
                    146: /* Returns the name of the cipher. */
                    147:
1.16      markus    148: const char *
                    149: cipher_name(int cipher)
1.1       deraadt   150: {
1.16      markus    151:        if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
                    152:            cipher_names[cipher] == NULL)
                    153:                fatal("cipher_name: bad cipher number: %d", cipher);
                    154:        return cipher_names[cipher];
1.1       deraadt   155: }
                    156:
1.18      markus    157: /*
                    158:  * Parses the name of the cipher.  Returns the number of the corresponding
                    159:  * cipher, or -1 on error.
                    160:  */
1.1       deraadt   161:
1.4       provos    162: int
                    163: cipher_number(const char *name)
1.1       deraadt   164: {
1.16      markus    165:        int i;
                    166:        for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
                    167:                if (strcmp(cipher_names[i], name) == 0 &&
                    168:                    (cipher_mask() & (1 << i)))
                    169:                        return i;
                    170:        return -1;
1.1       deraadt   171: }
                    172:
1.18      markus    173: /*
                    174:  * Selects the cipher, and keys if by computing the MD5 checksum of the
                    175:  * passphrase and using the resulting 16 bytes as the key.
                    176:  */
1.1       deraadt   177:
1.16      markus    178: void
                    179: cipher_set_key_string(CipherContext *context, int cipher,
                    180:                      const char *passphrase, int for_encryption)
1.1       deraadt   181: {
1.16      markus    182:        MD5_CTX md;
                    183:        unsigned char digest[16];
                    184:
                    185:        MD5_Init(&md);
                    186:        MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
                    187:        MD5_Final(digest, &md);
                    188:
                    189:        cipher_set_key(context, cipher, digest, 16, for_encryption);
                    190:
                    191:        memset(digest, 0, sizeof(digest));
                    192:        memset(&md, 0, sizeof(md));
1.1       deraadt   193: }
                    194:
                    195: /* Selects the cipher to use and sets the key. */
                    196:
1.16      markus    197: void
                    198: cipher_set_key(CipherContext *context, int cipher,
                    199:               const unsigned char *key, int keylen, int for_encryption)
                    200: {
                    201:        unsigned char padded[32];
                    202:
                    203:        /* Set cipher type. */
                    204:        context->type = cipher;
                    205:
                    206:        /* Get 32 bytes of key data.  Pad if necessary.  (So that code
                    207:           below does not need to worry about key size). */
                    208:        memset(padded, 0, sizeof(padded));
                    209:        memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
                    210:
                    211:        /* Initialize the initialization vector. */
                    212:        switch (cipher) {
                    213:        case SSH_CIPHER_NONE:
1.18      markus    214:                /*
                    215:                 * Has to stay for authfile saving of private key with no
                    216:                 * passphrase
                    217:                 */
1.16      markus    218:                break;
                    219:
                    220:        case SSH_CIPHER_3DES:
1.18      markus    221:                /*
                    222:                 * Note: the least significant bit of each byte of key is
                    223:                 * parity, and must be ignored by the implementation.  16
                    224:                 * bytes of key are used (first and last keys are the same).
                    225:                 */
1.16      markus    226:                if (keylen < 16)
                    227:                        error("Key length %d is insufficient for 3DES.", keylen);
                    228:                des_set_key((void *) padded, context->u.des3.key1);
                    229:                des_set_key((void *) (padded + 8), context->u.des3.key2);
                    230:                if (keylen <= 16)
                    231:                        des_set_key((void *) padded, context->u.des3.key3);
                    232:                else
                    233:                        des_set_key((void *) (padded + 16), context->u.des3.key3);
                    234:                memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
                    235:                memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
                    236:                break;
                    237:
                    238:        case SSH_CIPHER_BLOWFISH:
1.21    ! markus    239:                if (keylen < 16)
        !           240:                        error("Key length %d is insufficient for blowfish.", keylen);
1.16      markus    241:                BF_set_key(&context->u.bf.key, keylen, padded);
                    242:                memset(context->u.bf.iv, 0, 8);
                    243:                break;
                    244:
1.21    ! markus    245:        case SSH_CIPHER_3DES_CBC:
        !           246:        case SSH_CIPHER_BLOWFISH_CBC:
        !           247:        case SSH_CIPHER_ARCFOUR:
        !           248:        case SSH_CIPHER_CAST128_CBC:
        !           249:                fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
        !           250:                break;
        !           251:
1.16      markus    252:        default:
                    253:                fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
                    254:        }
                    255:        memset(padded, 0, sizeof(padded));
1.1       deraadt   256: }
                    257:
1.21    ! markus    258:
        !           259: void
        !           260: cipher_set_key_iv(CipherContext * context, int cipher,
        !           261:     const unsigned char *key, int keylen,
        !           262:     const unsigned char *iv, int ivlen)
        !           263: {
        !           264:        /* Set cipher type. */
        !           265:        context->type = cipher;
        !           266:
        !           267:        /* Initialize the initialization vector. */
        !           268:        switch (cipher) {
        !           269:        case SSH_CIPHER_NONE:
        !           270:                break;
        !           271:
        !           272:        case SSH_CIPHER_3DES:
        !           273:        case SSH_CIPHER_BLOWFISH:
        !           274:                fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
        !           275:                break;
        !           276:
        !           277:        case SSH_CIPHER_3DES_CBC:
        !           278:                if (keylen < 24)
        !           279:                        error("Key length %d is insufficient for 3des-cbc.", keylen);
        !           280:                des_set_key((void *) key, context->u.des3.key1);
        !           281:                des_set_key((void *) (key+8), context->u.des3.key2);
        !           282:                des_set_key((void *) (key+16), context->u.des3.key3);
        !           283:                if (ivlen < 8)
        !           284:                        error("IV length %d is insufficient for 3des-cbc.", ivlen);
        !           285:                memcpy(context->u.des3.iv3, (char *)iv, 8);
        !           286:                break;
        !           287:
        !           288:        case SSH_CIPHER_BLOWFISH_CBC:
        !           289:                if (keylen < 16)
        !           290:                        error("Key length %d is insufficient for blowfish.", keylen);
        !           291:                if (ivlen < 8)
        !           292:                        error("IV length %d is insufficient for blowfish.", ivlen);
        !           293:                BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
        !           294:                memcpy(context->u.bf.iv, (char *)iv, 8);
        !           295:                break;
        !           296:
        !           297:        case SSH_CIPHER_ARCFOUR:
        !           298:                if (keylen < 16)
        !           299:                        error("Key length %d is insufficient for arcfour.", keylen);
        !           300:                RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
        !           301:                break;
        !           302:
        !           303:        case SSH_CIPHER_CAST128_CBC:
        !           304:                if (keylen < 16)
        !           305:                        error("Key length %d is insufficient for cast128.", keylen);
        !           306:                if (ivlen < 8)
        !           307:                        error("IV length %d is insufficient for cast128.", ivlen);
        !           308:                CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
        !           309:                memcpy(context->u.cast.iv, (char *)iv, 8);
        !           310:                break;
        !           311:
        !           312:        default:
        !           313:                fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
        !           314:        }
        !           315: }
        !           316:
1.1       deraadt   317: /* Encrypts data using the cipher. */
                    318:
1.16      markus    319: void
                    320: cipher_encrypt(CipherContext *context, unsigned char *dest,
                    321:               const unsigned char *src, unsigned int len)
                    322: {
                    323:        if ((len & 7) != 0)
                    324:                fatal("cipher_encrypt: bad plaintext length %d", len);
                    325:
                    326:        switch (context->type) {
                    327:        case SSH_CIPHER_NONE:
                    328:                memcpy(dest, src, len);
                    329:                break;
                    330:
                    331:        case SSH_CIPHER_3DES:
                    332:                SSH_3CBC_ENCRYPT(context->u.des3.key1,
                    333:                                 context->u.des3.key2, &context->u.des3.iv2,
                    334:                                 context->u.des3.key3, &context->u.des3.iv3,
1.19      markus    335:                                 dest, (unsigned char *) src, len);
1.16      markus    336:                break;
                    337:
                    338:        case SSH_CIPHER_BLOWFISH:
                    339:                swap_bytes(src, dest, len);
                    340:                BF_cbc_encrypt(dest, dest, len,
                    341:                               &context->u.bf.key, context->u.bf.iv,
                    342:                               BF_ENCRYPT);
                    343:                swap_bytes(dest, dest, len);
                    344:                break;
                    345:
1.21    ! markus    346:        case SSH_CIPHER_BLOWFISH_CBC:
        !           347:                BF_cbc_encrypt((void *)src, dest, len,
        !           348:                               &context->u.bf.key, context->u.bf.iv,
        !           349:                               BF_ENCRYPT);
        !           350:                break;
        !           351:
        !           352:        case SSH_CIPHER_3DES_CBC:
        !           353:                des_ede3_cbc_encrypt(src, dest, len,
        !           354:                    context->u.des3.key1, context->u.des3.key2,
        !           355:                    context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
        !           356:                break;
        !           357:
        !           358:        case SSH_CIPHER_ARCFOUR:
        !           359:                RC4(&context->u.rc4, len, (unsigned char *)src, dest);
        !           360:                break;
        !           361:
        !           362:        case SSH_CIPHER_CAST128_CBC:
        !           363:                CAST_cbc_encrypt(src, dest, len,
        !           364:                    &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
        !           365:                break;
        !           366:
1.16      markus    367:        default:
                    368:                fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
                    369:        }
                    370: }
1.1       deraadt   371:
                    372: /* Decrypts data using the cipher. */
                    373:
1.16      markus    374: void
                    375: cipher_decrypt(CipherContext *context, unsigned char *dest,
                    376:               const unsigned char *src, unsigned int len)
                    377: {
                    378:        if ((len & 7) != 0)
                    379:                fatal("cipher_decrypt: bad ciphertext length %d", len);
                    380:
                    381:        switch (context->type) {
                    382:        case SSH_CIPHER_NONE:
                    383:                memcpy(dest, src, len);
                    384:                break;
                    385:
                    386:        case SSH_CIPHER_3DES:
                    387:                SSH_3CBC_DECRYPT(context->u.des3.key1,
                    388:                                 context->u.des3.key2, &context->u.des3.iv2,
                    389:                                 context->u.des3.key3, &context->u.des3.iv3,
1.19      markus    390:                                 dest, (unsigned char *) src, len);
1.16      markus    391:                break;
                    392:
                    393:        case SSH_CIPHER_BLOWFISH:
                    394:                swap_bytes(src, dest, len);
                    395:                BF_cbc_encrypt((void *) dest, dest, len,
                    396:                               &context->u.bf.key, context->u.bf.iv,
                    397:                               BF_DECRYPT);
                    398:                swap_bytes(dest, dest, len);
1.21    ! markus    399:                break;
        !           400:
        !           401:        case SSH_CIPHER_BLOWFISH_CBC:
        !           402:                BF_cbc_encrypt((void *) src, dest, len,
        !           403:                               &context->u.bf.key, context->u.bf.iv,
        !           404:                               BF_DECRYPT);
        !           405:                break;
        !           406:
        !           407:        case SSH_CIPHER_3DES_CBC:
        !           408:                des_ede3_cbc_encrypt(src, dest, len,
        !           409:                    context->u.des3.key1, context->u.des3.key2,
        !           410:                    context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
        !           411:                break;
        !           412:
        !           413:        case SSH_CIPHER_ARCFOUR:
        !           414:                RC4(&context->u.rc4, len, (unsigned char *)src, dest);
        !           415:                break;
        !           416:
        !           417:        case SSH_CIPHER_CAST128_CBC:
        !           418:                CAST_cbc_encrypt(src, dest, len,
        !           419:                    &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
1.16      markus    420:                break;
                    421:
                    422:        default:
                    423:                fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));
                    424:        }
1.1       deraadt   425: }