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