Annotation of src/usr.bin/ssh/cipher.c, Revision 1.37.2.6
1.1 deraadt 1: /*
1.30 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.26 markus 5: *
1.30 deraadt 6: * As far as I am concerned, the code I have written for this software
7: * can be used freely for any purpose. Any derived versions of this
8: * software must be clearly marked as such, and if the derived work is
9: * incompatible with the protocol description in the RFC file, it must be
10: * called by a name other than "ssh" or "Secure Shell".
1.26 markus 11: *
12: *
1.30 deraadt 13: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.37.2.5 miod 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.37.2.6! brad 38: RCSID("$OpenBSD: cipher.c,v 1.52 2002/02/18 13:05:32 markus Exp $");
1.1 deraadt 39:
1.24 markus 40: #include "xmalloc.h"
1.37.2.1 jason 41: #include "log.h"
42: #include "cipher.h"
1.8 deraadt 43:
1.37.2.6! brad 44: #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
! 45:
1.25 markus 46: #include <openssl/md5.h>
1.37.2.6! brad 47: #include "rijndael.h"
1.1 deraadt 48:
1.37.2.6! brad 49: static EVP_CIPHER *evp_ssh1_3des(void);
! 50: static EVP_CIPHER *evp_ssh1_bf(void);
! 51: static EVP_CIPHER *evp_rijndael(void);
! 52:
! 53: struct Cipher {
! 54: char *name;
! 55: int number; /* for ssh1 only */
! 56: u_int block_size;
! 57: u_int key_len;
! 58: EVP_CIPHER *(*evptype)(void);
! 59: } ciphers[] = {
! 60: { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
! 61: { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
! 62: { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
! 63: { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
! 64:
! 65: { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
! 66: { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
! 67: { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
! 68: { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
! 69: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
! 70: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
! 71: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
1.32 markus 72:
1.37.2.6! brad 73: { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL }
! 74: };
1.1 deraadt 75:
1.37.2.6! brad 76: /*--*/
1.35 markus 77:
1.37.2.6! brad 78: u_int
! 79: cipher_blocksize(Cipher *c)
1.35 markus 80: {
1.37.2.6! brad 81: return (c->block_size);
1.35 markus 82: }
1.37.2.6! brad 83: u_int
! 84: cipher_keylen(Cipher *c)
1.35 markus 85: {
1.37.2.6! brad 86: return (c->key_len);
1.35 markus 87: }
1.1 deraadt 88:
1.37.2.1 jason 89: u_int
1.34 markus 90: cipher_mask_ssh1(int client)
1.1 deraadt 91: {
1.37.2.1 jason 92: u_int mask = 0;
1.37.2.6! brad 93: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 94: mask |= 1 << SSH_CIPHER_BLOWFISH;
95: if (client) {
96: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 97: }
98: return mask;
1.1 deraadt 99: }
100:
1.32 markus 101: Cipher *
102: cipher_by_name(const char *name)
103: {
104: Cipher *c;
105: for (c = ciphers; c->name != NULL; c++)
106: if (strcasecmp(c->name, name) == 0)
107: return c;
108: return NULL;
109: }
110:
111: Cipher *
112: cipher_by_number(int id)
113: {
114: Cipher *c;
115: for (c = ciphers; c->name != NULL; c++)
116: if (c->number == id)
117: return c;
118: return NULL;
119: }
1.24 markus 120:
121: #define CIPHER_SEP ","
122: int
123: ciphers_valid(const char *names)
124: {
1.32 markus 125: Cipher *c;
1.29 ho 126: char *ciphers, *cp;
1.24 markus 127: char *p;
128:
1.27 markus 129: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 130: return 0;
1.29 ho 131: ciphers = cp = xstrdup(names);
1.32 markus 132: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.37.2.6! brad 133: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 134: c = cipher_by_name(p);
135: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
136: debug("bad cipher %s [%s]", p, names);
1.24 markus 137: xfree(ciphers);
138: return 0;
1.32 markus 139: } else {
1.36 markus 140: debug3("cipher ok: %s [%s]", p, names);
1.24 markus 141: }
142: }
1.36 markus 143: debug3("ciphers ok: [%s]", names);
1.24 markus 144: xfree(ciphers);
145: return 1;
146: }
147:
1.18 markus 148: /*
149: * Parses the name of the cipher. Returns the number of the corresponding
150: * cipher, or -1 on error.
151: */
1.1 deraadt 152:
1.4 provos 153: int
154: cipher_number(const char *name)
1.1 deraadt 155: {
1.32 markus 156: Cipher *c;
1.27 markus 157: if (name == NULL)
158: return -1;
1.32 markus 159: c = cipher_by_name(name);
160: return (c==NULL) ? -1 : c->number;
1.1 deraadt 161: }
162:
1.32 markus 163: char *
164: cipher_name(int id)
1.1 deraadt 165: {
1.32 markus 166: Cipher *c = cipher_by_number(id);
167: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 168: }
169:
1.26 markus 170: void
1.32 markus 171: cipher_init(CipherContext *cc, Cipher *cipher,
1.37.2.6! brad 172: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
! 173: int encrypt)
1.16 markus 174: {
1.37.2.6! brad 175: static int dowarn = 1;
! 176: const EVP_CIPHER *type;
! 177: int klen;
! 178:
! 179: if (cipher->number == SSH_CIPHER_DES) {
! 180: if (dowarn) {
! 181: error("Warning: use of DES is strongly discouraged "
! 182: "due to cryptographic weaknesses");
! 183: dowarn = 0;
! 184: }
! 185: if (keylen > 8)
! 186: keylen = 8;
! 187: }
! 188: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
! 189:
1.32 markus 190: if (keylen < cipher->key_len)
191: fatal("cipher_init: key length %d is insufficient for %s.",
192: keylen, cipher->name);
193: if (iv != NULL && ivlen < cipher->block_size)
194: fatal("cipher_init: iv length %d is insufficient for %s.",
195: ivlen, cipher->name);
196: cc->cipher = cipher;
1.37.2.6! brad 197:
! 198: type = (*cipher->evptype)();
! 199:
! 200: EVP_CIPHER_CTX_init(&cc->evp);
! 201: EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
! 202: (encrypt == CIPHER_ENCRYPT));
1.1 deraadt 203: }
1.21 markus 204:
1.26 markus 205: void
1.37.2.6! brad 206: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.32 markus 207: {
208: if (len % cc->cipher->block_size)
209: fatal("cipher_encrypt: bad plaintext length %d", len);
1.37.2.6! brad 210: EVP_Cipher(&cc->evp, dest, (u_char *)src, len);
1.21 markus 211: }
212:
1.26 markus 213: void
1.37.2.6! brad 214: cipher_cleanup(CipherContext *cc)
1.16 markus 215: {
1.37.2.6! brad 216: EVP_CIPHER_CTX_cleanup(&cc->evp);
1.16 markus 217: }
1.1 deraadt 218:
1.32 markus 219: /*
220: * Selects the cipher, and keys if by computing the MD5 checksum of the
221: * passphrase and using the resulting 16 bytes as the key.
222: */
1.1 deraadt 223:
1.26 markus 224: void
1.32 markus 225: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.37.2.6! brad 226: const char *passphrase, int encrypt)
1.16 markus 227: {
1.32 markus 228: MD5_CTX md;
1.37.2.1 jason 229: u_char digest[16];
1.32 markus 230:
231: MD5_Init(&md);
232: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
233: MD5_Final(digest, &md);
1.16 markus 234:
1.37.2.6! brad 235: cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
1.16 markus 236:
1.32 markus 237: memset(digest, 0, sizeof(digest));
238: memset(&md, 0, sizeof(md));
1.37.2.6! brad 239: }
! 240:
! 241: /* Implementations for other non-EVP ciphers */
! 242:
! 243: /*
! 244: * This is used by SSH1:
! 245: *
! 246: * What kind of triple DES are these 2 routines?
! 247: *
! 248: * Why is there a redundant initialization vector?
! 249: *
! 250: * If only iv3 was used, then, this would till effect have been
! 251: * outer-cbc. However, there is also a private iv1 == iv2 which
! 252: * perhaps makes differential analysis easier. On the other hand, the
! 253: * private iv1 probably makes the CRC-32 attack ineffective. This is a
! 254: * result of that there is no longer any known iv1 to use when
! 255: * choosing the X block.
! 256: */
! 257: struct ssh1_3des_ctx
! 258: {
! 259: EVP_CIPHER_CTX k1, k2, k3;
! 260: };
! 261: static int
! 262: ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
! 263: int enc)
! 264: {
! 265: struct ssh1_3des_ctx *c;
! 266: u_char *k1, *k2, *k3;
! 267:
! 268: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
! 269: c = xmalloc(sizeof(*c));
! 270: EVP_CIPHER_CTX_set_app_data(ctx, c);
! 271: }
! 272: if (key == NULL)
! 273: return (1);
! 274: if (enc == -1)
! 275: enc = ctx->encrypt;
! 276: k1 = k2 = k3 = (u_char *) key;
! 277: k2 += 8;
! 278: if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
! 279: if (enc)
! 280: k3 += 16;
! 281: else
! 282: k1 += 16;
! 283: }
! 284: EVP_CIPHER_CTX_init(&c->k1);
! 285: EVP_CIPHER_CTX_init(&c->k2);
! 286: EVP_CIPHER_CTX_init(&c->k3);
! 287: EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
! 288: EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
! 289: EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
! 290: return (1);
! 291: }
! 292: static int
! 293: ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
! 294: {
! 295: struct ssh1_3des_ctx *c;
! 296:
! 297: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
! 298: error("ssh1_3des_cbc: no context");
! 299: return (0);
! 300: }
! 301: EVP_Cipher(&c->k1, dest, (u_char *)src, len);
! 302: EVP_Cipher(&c->k2, dest, dest, len);
! 303: EVP_Cipher(&c->k3, dest, dest, len);
! 304: return (1);
! 305: }
! 306: static int
! 307: ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
! 308: {
! 309: struct ssh1_3des_ctx *c;
! 310:
! 311: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
! 312: memset(c, 0, sizeof(*c));
! 313: xfree(c);
! 314: EVP_CIPHER_CTX_set_app_data(ctx, NULL);
! 315: }
! 316: return (1);
! 317: }
! 318: static EVP_CIPHER *
! 319: evp_ssh1_3des(void)
! 320: {
! 321: static EVP_CIPHER ssh1_3des;
! 322:
! 323: memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
! 324: ssh1_3des.nid = NID_undef;
! 325: ssh1_3des.block_size = 8;
! 326: ssh1_3des.iv_len = 0;
! 327: ssh1_3des.key_len = 16;
! 328: ssh1_3des.init = ssh1_3des_init;
! 329: ssh1_3des.cleanup = ssh1_3des_cleanup;
! 330: ssh1_3des.do_cipher = ssh1_3des_cbc;
! 331: return (&ssh1_3des);
! 332: }
! 333:
! 334: /*
! 335: * SSH1 uses a variation on Blowfish, all bytes must be swapped before
! 336: * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
! 337: */
! 338: static void
! 339: swap_bytes(const u_char *src, u_char *dst, int n)
! 340: {
! 341: u_char c[4];
! 342:
! 343: /* Process 4 bytes every lap. */
! 344: for (n = n / 4; n > 0; n--) {
! 345: c[3] = *src++;
! 346: c[2] = *src++;
! 347: c[1] = *src++;
! 348: c[0] = *src++;
! 349:
! 350: *dst++ = c[0];
! 351: *dst++ = c[1];
! 352: *dst++ = c[2];
! 353: *dst++ = c[3];
! 354: }
! 355: }
! 356: static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
! 357: static int
! 358: bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
! 359: {
! 360: int ret;
! 361:
! 362: swap_bytes(in, out, len);
! 363: ret = (*orig_bf)(ctx, out, out, len);
! 364: swap_bytes(out, out, len);
! 365: return (ret);
! 366: }
! 367: static EVP_CIPHER *
! 368: evp_ssh1_bf(void)
! 369: {
! 370: static EVP_CIPHER ssh1_bf;
! 371:
! 372: memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
! 373: orig_bf = ssh1_bf.do_cipher;
! 374: ssh1_bf.nid = NID_undef;
! 375: ssh1_bf.do_cipher = bf_ssh1_cipher;
! 376: ssh1_bf.key_len = 32;
! 377: return (&ssh1_bf);
! 378: }
! 379:
! 380: /* RIJNDAEL */
! 381: #define RIJNDAEL_BLOCKSIZE 16
! 382: struct ssh_rijndael_ctx
! 383: {
! 384: rijndael_ctx r_ctx;
! 385: u_char r_iv[RIJNDAEL_BLOCKSIZE];
! 386: };
! 387:
! 388: static int
! 389: ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
! 390: int enc)
! 391: {
! 392: struct ssh_rijndael_ctx *c;
! 393:
! 394: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
! 395: c = xmalloc(sizeof(*c));
! 396: EVP_CIPHER_CTX_set_app_data(ctx, c);
! 397: }
! 398: if (key != NULL) {
! 399: if (enc == -1)
! 400: enc = ctx->encrypt;
! 401: rijndael_set_key(&c->r_ctx, (u_char *)key,
! 402: 8*EVP_CIPHER_CTX_key_length(ctx), enc);
! 403: }
! 404: if (iv != NULL)
! 405: memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
! 406: return (1);
! 407: }
! 408: static int
! 409: ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
! 410: u_int len)
! 411: {
! 412: struct ssh_rijndael_ctx *c;
! 413: u_char buf[RIJNDAEL_BLOCKSIZE];
! 414: u_char *cprev, *cnow, *plain, *ivp;
! 415: int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
! 416:
! 417: if (len == 0)
! 418: return (1);
! 419: if (len % RIJNDAEL_BLOCKSIZE)
! 420: fatal("ssh_rijndael_cbc: bad len %d", len);
! 421: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
! 422: error("ssh_rijndael_cbc: no context");
! 423: return (0);
! 424: }
! 425: if (ctx->encrypt) {
! 426: cnow = dest;
! 427: plain = (u_char *)src;
! 428: cprev = c->r_iv;
! 429: for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
! 430: cnow+=RIJNDAEL_BLOCKSIZE) {
! 431: for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
! 432: buf[j] = plain[j] ^ cprev[j];
! 433: rijndael_encrypt(&c->r_ctx, buf, cnow);
! 434: cprev = cnow;
! 435: }
! 436: memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
! 437: } else {
! 438: cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
! 439: plain = dest+len-RIJNDAEL_BLOCKSIZE;
! 440:
! 441: memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
! 442: for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
! 443: plain-=RIJNDAEL_BLOCKSIZE) {
! 444: rijndael_decrypt(&c->r_ctx, cnow, plain);
! 445: ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
! 446: for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
! 447: plain[j] ^= ivp[j];
! 448: }
! 449: memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
! 450: }
! 451: return (1);
! 452: }
! 453: static int
! 454: ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
! 455: {
! 456: struct ssh_rijndael_ctx *c;
! 457:
! 458: if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
! 459: memset(c, 0, sizeof(*c));
! 460: xfree(c);
! 461: EVP_CIPHER_CTX_set_app_data(ctx, NULL);
! 462: }
! 463: return (1);
! 464: }
! 465: static EVP_CIPHER *
! 466: evp_rijndael(void)
! 467: {
! 468: static EVP_CIPHER rijndal_cbc;
! 469:
! 470: memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
! 471: rijndal_cbc.nid = NID_undef;
! 472: rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
! 473: rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
! 474: rijndal_cbc.key_len = 16;
! 475: rijndal_cbc.init = ssh_rijndael_init;
! 476: rijndal_cbc.cleanup = ssh_rijndael_cleanup;
! 477: rijndal_cbc.do_cipher = ssh_rijndael_cbc;
! 478: return (&rijndal_cbc);
1.1 deraadt 479: }