Annotation of src/usr.bin/ssh/cipher.c, Revision 1.33
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: {
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: { "3des",
305: SSH_CIPHER_3DES, 8, 16,
306: des3_ssh1_setkey, des3_setiv,
307: des3_ssh1_encrypt, des3_ssh1_decrypt },
308: { "blowfish",
309: SSH_CIPHER_BLOWFISH, 8, 16,
310: blowfish_setkey, blowfish_setiv,
311: blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
312:
313: { "3des-cbc",
314: SSH_CIPHER_SSH2, 8, 24,
315: des3_setkey, des3_setiv,
316: des3_cbc_encrypt, des3_cbc_decrypt },
317: { "blowfish-cbc",
318: SSH_CIPHER_SSH2, 8, 16,
319: blowfish_setkey, blowfish_setiv,
320: blowfish_cbc_encrypt, blowfish_cbc_decrypt },
321: { "cast128-cbc",
322: SSH_CIPHER_SSH2, 8, 16,
323: cast_setkey, cast_setiv,
324: cast_cbc_encrypt, cast_cbc_decrypt },
325: { "arcfour",
326: SSH_CIPHER_SSH2, 8, 16,
327: arcfour_setkey, none_setiv,
328: arcfour_crypt, arcfour_crypt },
329: { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
330: };
331:
332: /*--*/
1.1 deraadt 333:
1.32 markus 334: unsigned int
335: cipher_mask1()
1.1 deraadt 336: {
1.32 markus 337: unsigned int mask = 0;
338: Cipher *c;
339: for (c = ciphers; c->name != NULL; c++) {
340: if (c->number > SSH_CIPHER_NONE)
341: mask |= 1 << c->number;
342: }
343: return mask;
1.1 deraadt 344: }
345:
1.32 markus 346: Cipher *
347: cipher_by_name(const char *name)
348: {
349: Cipher *c;
350: if (strcmp(name, "des") == 0)
351: error("Warning: use of DES is strongly discouraged "
352: "due to cryptographic weaknesses");
353: for (c = ciphers; c->name != NULL; c++)
354: if (strcasecmp(c->name, name) == 0)
355: return c;
356: return NULL;
357: }
358:
359: Cipher *
360: cipher_by_number(int id)
361: {
362: Cipher *c;
363: for (c = ciphers; c->name != NULL; c++)
364: if (c->number == id)
365: return c;
366: return NULL;
367: }
1.24 markus 368:
369: #define CIPHER_SEP ","
370: int
371: ciphers_valid(const char *names)
372: {
1.32 markus 373: Cipher *c;
1.29 ho 374: char *ciphers, *cp;
1.24 markus 375: char *p;
376:
1.27 markus 377: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 378: return 0;
1.29 ho 379: ciphers = cp = xstrdup(names);
1.32 markus 380: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.29 ho 381: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 382: c = cipher_by_name(p);
383: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
384: debug("bad cipher %s [%s]", p, names);
1.24 markus 385: xfree(ciphers);
386: return 0;
1.32 markus 387: } else {
388: debug("cipher ok: %s [%s]", p, names);
1.24 markus 389: }
390: }
1.32 markus 391: debug("ciphers ok: [%s]", names);
1.24 markus 392: xfree(ciphers);
393: return 1;
394: }
395:
1.18 markus 396: /*
397: * Parses the name of the cipher. Returns the number of the corresponding
398: * cipher, or -1 on error.
399: */
1.1 deraadt 400:
1.4 provos 401: int
402: cipher_number(const char *name)
1.1 deraadt 403: {
1.32 markus 404: Cipher *c;
1.27 markus 405: if (name == NULL)
406: return -1;
1.32 markus 407: c = cipher_by_name(name);
408: return (c==NULL) ? -1 : c->number;
1.1 deraadt 409: }
410:
1.32 markus 411: char *
412: cipher_name(int id)
1.1 deraadt 413: {
1.32 markus 414: Cipher *c = cipher_by_number(id);
415: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 416: }
417:
1.26 markus 418: void
1.32 markus 419: cipher_init(CipherContext *cc, Cipher *cipher,
420: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
1.16 markus 421: {
1.32 markus 422: if (keylen < cipher->key_len)
423: fatal("cipher_init: key length %d is insufficient for %s.",
424: keylen, cipher->name);
425: if (iv != NULL && ivlen < cipher->block_size)
426: fatal("cipher_init: iv length %d is insufficient for %s.",
427: ivlen, cipher->name);
428: cc->cipher = cipher;
429: cipher->setkey(cc, key, keylen);
430: cipher->setiv(cc, iv, ivlen);
1.1 deraadt 431: }
1.21 markus 432:
1.26 markus 433: void
1.32 markus 434: cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
435: {
436: if (len % cc->cipher->block_size)
437: fatal("cipher_encrypt: bad plaintext length %d", len);
438: cc->cipher->encrypt(cc, dest, src, len);
1.21 markus 439: }
440:
1.26 markus 441: void
1.32 markus 442: cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.16 markus 443: {
1.32 markus 444: if (len % cc->cipher->block_size)
445: fatal("cipher_decrypt: bad ciphertext length %d", len);
446: cc->cipher->decrypt(cc, dest, src, len);
1.16 markus 447: }
1.1 deraadt 448:
1.32 markus 449: /*
450: * Selects the cipher, and keys if by computing the MD5 checksum of the
451: * passphrase and using the resulting 16 bytes as the key.
452: */
1.1 deraadt 453:
1.26 markus 454: void
1.32 markus 455: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
456: const char *passphrase)
1.16 markus 457: {
1.32 markus 458: MD5_CTX md;
459: unsigned char digest[16];
460:
461: MD5_Init(&md);
462: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
463: MD5_Final(digest, &md);
1.16 markus 464:
1.32 markus 465: cipher_init(cc, cipher, digest, 16, NULL, 0);
1.16 markus 466:
1.32 markus 467: memset(digest, 0, sizeof(digest));
468: memset(&md, 0, sizeof(md));
1.1 deraadt 469: }