Annotation of src/usr.bin/ssh/cipher.c, Revision 1.9
1.1 deraadt 1: /*
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: */
13:
14: #include "includes.h"
1.9 ! provos 15: RCSID("$Id: cipher.c,v 1.8 1999/09/30 17:08:52 deraadt Exp $");
1.1 deraadt 16:
17: #include "ssh.h"
18: #include "cipher.h"
1.8 deraadt 19:
20: #include <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,
36: des_key_schedule ks2, des_cblock *iv2,
37: des_key_schedule ks3, des_cblock *iv3,
38: void *dest, void *src,
39: unsigned int len)
40: {
41: des_cblock iv1;
42:
43: memcpy(&iv1, iv2, 8);
44:
45: des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
46: memcpy(&iv1, dest + len - 8, 8);
47:
48: des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
49: memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
50:
51: des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
52: memcpy(iv3, dest + len - 8, 8);
53: }
54:
55: void
56: SSH_3CBC_DECRYPT(des_key_schedule ks1,
57: des_key_schedule ks2, des_cblock *iv2,
58: des_key_schedule ks3, des_cblock *iv3,
59: void *dest, void *src,
60: unsigned int len)
61: {
62: des_cblock iv1;
63:
64: memcpy(&iv1, iv2, 8);
65:
66: des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
67: memcpy(iv3, src + len - 8, 8);
68:
69: des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
70: memcpy(iv2, dest + len - 8, 8);
71:
72: des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
73: /* memcpy(&iv1, iv2, 8); */ /* Note how iv1 == iv2 on entry and exit. */
74: }
75:
76: /*
77: * SSH uses a variation on Blowfish, all bytes must be swapped before
78: * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
79: */
80: static
81: void
82: swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
83: {
1.5 deraadt 84: u_int32_t *dst = (u_int32_t *)dst_; /* dst must be properly aligned. */
1.1 deraadt 85: union {
1.5 deraadt 86: u_int32_t i;
1.1 deraadt 87: char c[4];
88: } t;
89:
90: /* assert((n & 7) == 0); */
91:
92: /* Process 8 bytes every lap. */
93: for (n = n / 8; n > 0; n--)
94: {
95: t.c[3] = *src++;
96: t.c[2] = *src++;
97: t.c[1] = *src++;
98: t.c[0] = *src++;
99: *dst++ = t.i;
100:
101: t.c[3] = *src++;
102: t.c[2] = *src++;
103: t.c[1] = *src++;
104: t.c[0] = *src++;
105: *dst++ = t.i;
106: }
107: }
108:
109: void (*cipher_attack_detected)(const char *fmt, ...) = fatal;
110:
111: static inline
112: void
113: detect_cbc_attack(const unsigned char *src,
114: unsigned int len)
115: {
116: return;
117:
118: log("CRC-32 CBC insertion attack detected");
119: cipher_attack_detected("CRC-32 CBC insertion attack detected");
120: }
121:
122: /* Names of all encryption algorithms. These must match the numbers defined
123: int cipher.h. */
124: static char *cipher_names[] =
1.4 provos 125: {
1.9 ! provos 126: NULL, /* no none */
! 127: NULL, /* no idea */
! 128: NULL, /* no des */
1.1 deraadt 129: "3des",
1.9 ! provos 130: NULL, /* no tss */
! 131: NULL, /* no rc4 */
1.1 deraadt 132: "blowfish"
133: };
134:
135: /* Returns a bit mask indicating which ciphers are supported by this
136: implementation. The bit mask has the corresponding bit set of each
137: supported cipher. */
138:
139: unsigned int cipher_mask()
140: {
141: unsigned int mask = 0;
142: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
143: mask |= 1 << SSH_CIPHER_BLOWFISH;
144: return mask;
145: }
146:
147: /* Returns the name of the cipher. */
148:
1.4 provos 149: const
150: char *cipher_name(int cipher)
1.1 deraadt 151: {
152: if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]))
153: fatal("cipher_name: bad cipher number: %d", cipher);
154: return cipher_names[cipher];
155: }
156:
157: /* Parses the name of the cipher. Returns the number of the corresponding
158: cipher, or -1 on error. */
159:
1.4 provos 160: int
161: cipher_number(const char *name)
1.1 deraadt 162: {
163: int i;
164: for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
1.9 ! provos 165: if (cipher_names[i] != NULL && strcmp(cipher_names[i], name) == 0)
1.1 deraadt 166: return i;
167: return -1;
168: }
169:
170: /* Selects the cipher, and keys if by computing the MD5 checksum of the
171: passphrase and using the resulting 16 bytes as the key. */
172:
173: void cipher_set_key_string(CipherContext *context, int cipher,
174: const char *passphrase, int for_encryption)
175: {
1.8 deraadt 176: MD5_CTX md;
1.1 deraadt 177: unsigned char digest[16];
178:
179: MD5Init(&md);
180: MD5Update(&md, (const unsigned char *)passphrase, strlen(passphrase));
181: MD5Final(digest, &md);
182:
183: cipher_set_key(context, cipher, digest, 16, for_encryption);
184:
185: memset(digest, 0, sizeof(digest));
186: memset(&md, 0, sizeof(md));
187: }
188:
189: /* Selects the cipher to use and sets the key. */
190:
191: void cipher_set_key(CipherContext *context, int cipher,
192: const unsigned char *key, int keylen, int for_encryption)
193: {
194: unsigned char padded[32];
195:
196: /* Set cipher type. */
197: context->type = cipher;
198:
199: /* Get 32 bytes of key data. Pad if necessary. (So that code below does
200: not need to worry about key size). */
201: memset(padded, 0, sizeof(padded));
202: memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
203:
204: /* Initialize the initialization vector. */
205: switch (cipher)
206: {
207: case SSH_CIPHER_NONE:
1.7 provos 208: /* Has to stay for authfile saving of private key with no passphrase */
1.1 deraadt 209: break;
210:
211: case SSH_CIPHER_3DES:
212: /* Note: the least significant bit of each byte of key is parity,
213: and must be ignored by the implementation. 16 bytes of key are
214: used (first and last keys are the same). */
215: if (keylen < 16)
216: error("Key length %d is insufficient for 3DES.", keylen);
217: des_set_key((void*)padded, context->u.des3.key1);
218: des_set_key((void*)(padded + 8), context->u.des3.key2);
219: if (keylen <= 16)
220: des_set_key((void*)padded, context->u.des3.key3);
221: else
222: des_set_key((void*)(padded + 16), context->u.des3.key3);
223: memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
224: memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
225: break;
226:
227: case SSH_CIPHER_BLOWFISH:
228: BF_set_key(&context->u.bf.key, keylen, padded);
229: memset(context->u.bf.iv, 0, 8);
230: break;
231:
232: default:
233: fatal("cipher_set_key: unknown cipher: %d", cipher);
234: }
235: memset(padded, 0, sizeof(padded));
236: }
237:
238: /* Encrypts data using the cipher. */
239:
240: void cipher_encrypt(CipherContext *context, unsigned char *dest,
241: const unsigned char *src, unsigned int len)
242: {
243: assert((len & 7) == 0);
244:
245: switch (context->type)
246: {
247: case SSH_CIPHER_NONE:
248: memcpy(dest, src, len);
249: break;
250:
251: case SSH_CIPHER_3DES:
252: SSH_3CBC_ENCRYPT(context->u.des3.key1,
253: context->u.des3.key2, &context->u.des3.iv2,
254: context->u.des3.key3, &context->u.des3.iv3,
255: dest, (void*)src, len);
256: break;
257:
258: case SSH_CIPHER_BLOWFISH:
259: swap_bytes(src, dest, len);
260: BF_cbc_encrypt(dest, dest, len,
261: &context->u.bf.key, context->u.bf.iv, BF_ENCRYPT);
262: swap_bytes(dest, dest, len);
263: break;
264:
265: default:
266: fatal("cipher_encrypt: unknown cipher: %d", context->type);
267: }
268: }
269:
270: /* Decrypts data using the cipher. */
271:
272: void cipher_decrypt(CipherContext *context, unsigned char *dest,
273: const unsigned char *src, unsigned int len)
274: {
275: assert((len & 7) == 0);
276:
277: switch (context->type)
278: {
279: case SSH_CIPHER_NONE:
280: memcpy(dest, src, len);
281: break;
282:
283: case SSH_CIPHER_3DES:
284: /* CRC-32 attack? */
285: SSH_3CBC_DECRYPT(context->u.des3.key1,
286: context->u.des3.key2, &context->u.des3.iv2,
287: context->u.des3.key3, &context->u.des3.iv3,
288: dest, (void*)src, len);
289: break;
290:
291: case SSH_CIPHER_BLOWFISH:
292: detect_cbc_attack(src, len);
293: swap_bytes(src, dest, len);
294: BF_cbc_encrypt((void*)dest, dest, len,
295: &context->u.bf.key, context->u.bf.iv, BF_DECRYPT);
296: swap_bytes(dest, dest, len);
297: break;
298:
299: default:
300: fatal("cipher_decrypt: unknown cipher: %d", context->type);
301: }
302: }