Annotation of src/usr.bin/ssh/cipher.c, Revision 1.10
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.10 ! provos 15: RCSID("$Id: cipher.c,v 1.9 1999/09/30 18:37:45 provos 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: {
1.10 ! provos 152: if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
! 153: cipher_names[cipher] == NULL)
1.1 deraadt 154: fatal("cipher_name: bad cipher number: %d", cipher);
155: return cipher_names[cipher];
156: }
157:
158: /* Parses the name of the cipher. Returns the number of the corresponding
159: cipher, or -1 on error. */
160:
1.4 provos 161: int
162: cipher_number(const char *name)
1.1 deraadt 163: {
164: int i;
165: for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
1.9 provos 166: if (cipher_names[i] != NULL && strcmp(cipher_names[i], name) == 0)
1.1 deraadt 167: return i;
168: return -1;
169: }
170:
171: /* Selects the cipher, and keys if by computing the MD5 checksum of the
172: passphrase and using the resulting 16 bytes as the key. */
173:
174: void cipher_set_key_string(CipherContext *context, int cipher,
175: const char *passphrase, int for_encryption)
176: {
1.8 deraadt 177: MD5_CTX md;
1.1 deraadt 178: unsigned char digest[16];
179:
180: MD5Init(&md);
181: MD5Update(&md, (const unsigned char *)passphrase, strlen(passphrase));
182: MD5Final(digest, &md);
183:
184: cipher_set_key(context, cipher, digest, 16, for_encryption);
185:
186: memset(digest, 0, sizeof(digest));
187: memset(&md, 0, sizeof(md));
188: }
189:
190: /* Selects the cipher to use and sets the key. */
191:
192: void cipher_set_key(CipherContext *context, int cipher,
193: const unsigned char *key, int keylen, int for_encryption)
194: {
195: unsigned char padded[32];
196:
197: /* Set cipher type. */
198: context->type = cipher;
199:
200: /* Get 32 bytes of key data. Pad if necessary. (So that code below does
201: not need to worry about key size). */
202: memset(padded, 0, sizeof(padded));
203: memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
204:
205: /* Initialize the initialization vector. */
206: switch (cipher)
207: {
208: case SSH_CIPHER_NONE:
1.7 provos 209: /* Has to stay for authfile saving of private key with no passphrase */
1.1 deraadt 210: break;
211:
212: case SSH_CIPHER_3DES:
213: /* Note: the least significant bit of each byte of key is parity,
214: and must be ignored by the implementation. 16 bytes of key are
215: used (first and last keys are the same). */
216: if (keylen < 16)
217: error("Key length %d is insufficient for 3DES.", keylen);
218: des_set_key((void*)padded, context->u.des3.key1);
219: des_set_key((void*)(padded + 8), context->u.des3.key2);
220: if (keylen <= 16)
221: des_set_key((void*)padded, context->u.des3.key3);
222: else
223: des_set_key((void*)(padded + 16), context->u.des3.key3);
224: memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
225: memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
226: break;
227:
228: case SSH_CIPHER_BLOWFISH:
229: BF_set_key(&context->u.bf.key, keylen, padded);
230: memset(context->u.bf.iv, 0, 8);
231: break;
232:
233: default:
234: fatal("cipher_set_key: unknown cipher: %d", cipher);
235: }
236: memset(padded, 0, sizeof(padded));
237: }
238:
239: /* Encrypts data using the cipher. */
240:
241: void cipher_encrypt(CipherContext *context, unsigned char *dest,
242: const unsigned char *src, unsigned int len)
243: {
244: assert((len & 7) == 0);
245:
246: switch (context->type)
247: {
248: case SSH_CIPHER_NONE:
249: memcpy(dest, src, len);
250: break;
251:
252: case SSH_CIPHER_3DES:
253: SSH_3CBC_ENCRYPT(context->u.des3.key1,
254: context->u.des3.key2, &context->u.des3.iv2,
255: context->u.des3.key3, &context->u.des3.iv3,
256: dest, (void*)src, len);
257: break;
258:
259: case SSH_CIPHER_BLOWFISH:
260: swap_bytes(src, dest, len);
261: BF_cbc_encrypt(dest, dest, len,
262: &context->u.bf.key, context->u.bf.iv, BF_ENCRYPT);
263: swap_bytes(dest, dest, len);
264: break;
265:
266: default:
267: fatal("cipher_encrypt: unknown cipher: %d", context->type);
268: }
269: }
270:
271: /* Decrypts data using the cipher. */
272:
273: void cipher_decrypt(CipherContext *context, unsigned char *dest,
274: const unsigned char *src, unsigned int len)
275: {
276: assert((len & 7) == 0);
277:
278: switch (context->type)
279: {
280: case SSH_CIPHER_NONE:
281: memcpy(dest, src, len);
282: break;
283:
284: case SSH_CIPHER_3DES:
285: /* CRC-32 attack? */
286: SSH_3CBC_DECRYPT(context->u.des3.key1,
287: context->u.des3.key2, &context->u.des3.iv2,
288: context->u.des3.key3, &context->u.des3.iv3,
289: dest, (void*)src, len);
290: break;
291:
292: case SSH_CIPHER_BLOWFISH:
293: detect_cbc_attack(src, len);
294: swap_bytes(src, dest, len);
295: BF_cbc_encrypt((void*)dest, dest, len,
296: &context->u.bf.key, context->u.bf.iv, BF_DECRYPT);
297: swap_bytes(dest, dest, len);
298: break;
299:
300: default:
301: fatal("cipher_decrypt: unknown cipher: %d", context->type);
302: }
303: }