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