]> andersk Git - openssh.git/blob - cipher.c
- markus@cvs.openbsd.org 2000/12/06 23:10:39
[openssh.git] / cipher.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  *
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".
11  *
12  *
13  * Copyright (c) 1999 Niels Provos.  All rights reserved.
14  * Copyright (c) 1999,2000 Markus Friedl.  All rights reserved.
15  *
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.
24  *
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.
35  */
36
37 #include "includes.h"
38 RCSID("$OpenBSD: cipher.c,v 1.39 2000/12/06 23:05:42 markus Exp $");
39
40 #include "ssh.h"
41 #include "xmalloc.h"
42
43 #include <openssl/md5.h>
44
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         static int dowarn = 1;
66         if (dowarn) {
67                 error("Warning: use of DES is strongly discouraged "
68                     "due to cryptographic weaknesses");
69                 dowarn = 0;
70         }
71         des_set_key((void *)key, cc->u.des.key);
72 }
73 void
74 des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
75 {
76         memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv));
77 }
78 void
79 des_ssh1_encrypt(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_ENCRYPT);
83 }
84 void
85 des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
86 {
87         des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv,
88             DES_DECRYPT);
89 }
90
91 /* 3DES */
92 void
93 des3_setkey(CipherContext *cc, const u_char *key, u_int keylen)
94 {
95         des_set_key((void *) key, cc->u.des3.key1);
96         des_set_key((void *) (key+8), cc->u.des3.key2);
97         des_set_key((void *) (key+16), cc->u.des3.key3);
98 }
99 void
100 des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
101 {
102         memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2));
103         memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3));
104         if (iv == NULL)
105                 return;
106         memcpy(cc->u.des3.iv3, (char *)iv, 8);
107 }
108 void
109 des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
110 {
111         des_ede3_cbc_encrypt(src, dest, len,
112             cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
113             &cc->u.des3.iv3, DES_ENCRYPT);
114 }
115 void
116 des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
117 {
118         des_ede3_cbc_encrypt(src, dest, len,
119             cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3,
120             &cc->u.des3.iv3, DES_DECRYPT);
121 }
122
123 /*
124  * This is used by SSH1:
125  *
126  * What kind of triple DES are these 2 routines?
127  *
128  * Why is there a redundant initialization vector?
129  *
130  * If only iv3 was used, then, this would till effect have been
131  * outer-cbc. However, there is also a private iv1 == iv2 which
132  * perhaps makes differential analysis easier. On the other hand, the
133  * private iv1 probably makes the CRC-32 attack ineffective. This is a
134  * result of that there is no longer any known iv1 to use when
135  * choosing the X block.
136  */
137 void
138 des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen)
139 {
140         des_set_key((void *) key, cc->u.des3.key1);
141         des_set_key((void *) (key+8), cc->u.des3.key2);
142         if (keylen <= 16)
143                 des_set_key((void *) key, cc->u.des3.key3);
144         else
145                 des_set_key((void *) (key+16), cc->u.des3.key3);
146 }
147 void
148 des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
149     u_int len)
150 {
151         des_cblock iv1;
152         des_cblock *iv2 = &cc->u.des3.iv2;
153         des_cblock *iv3 = &cc->u.des3.iv3;
154
155         memcpy(&iv1, iv2, 8);
156
157         des_ncbc_encrypt(src,  dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT);
158         des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT);
159         des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT);
160 }
161 void
162 des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
163     u_int len)
164 {
165         des_cblock iv1;
166         des_cblock *iv2 = &cc->u.des3.iv2;
167         des_cblock *iv3 = &cc->u.des3.iv3;
168
169         memcpy(&iv1, iv2, 8);
170
171         des_ncbc_encrypt(src,  dest, len, cc->u.des3.key3, iv3, DES_DECRYPT);
172         des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT);
173         des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT);
174 }
175
176 /* Blowfish */
177 void
178 blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen)
179 {
180         BF_set_key(&cc->u.bf.key, keylen, (unsigned char *)key);
181 }
182 void
183 blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
184 {
185         if (iv == NULL)
186                 memset(cc->u.bf.iv, 0, 8);
187         else
188                 memcpy(cc->u.bf.iv, (char *)iv, 8);
189 }
190 void
191 blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
192      u_int len)
193 {
194         BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
195             BF_ENCRYPT);
196 }
197 void
198 blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
199      u_int len)
200 {
201         BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv,
202             BF_DECRYPT);
203 }
204
205 /*
206  * SSH1 uses a variation on Blowfish, all bytes must be swapped before
207  * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
208  */
209 static void
210 swap_bytes(const unsigned char *src, unsigned char *dst, int n)
211 {
212         char c[4];
213
214         /* Process 4 bytes every lap. */
215         for (n = n / 4; n > 0; n--) {
216                 c[3] = *src++;
217                 c[2] = *src++;
218                 c[1] = *src++;
219                 c[0] = *src++;
220
221                 *dst++ = c[0];
222                 *dst++ = c[1];
223                 *dst++ = c[2];
224                 *dst++ = c[3];
225         }
226 }
227
228 void
229 blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
230     u_int len)
231 {
232         swap_bytes(src, dest, len);
233         BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
234             BF_ENCRYPT);
235         swap_bytes(dest, dest, len);
236 }
237 void
238 blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
239     u_int len)
240 {
241         swap_bytes(src, dest, len);
242         BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv,
243             BF_DECRYPT);
244         swap_bytes(dest, dest, len);
245 }
246
247 /* alleged rc4 */
248 void
249 arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen)
250 {
251         RC4_set_key(&cc->u.rc4, keylen, (u_char *)key);
252 }
253 void
254 arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
255 {
256         RC4(&cc->u.rc4, len, (u_char *)src, dest);
257 }
258
259 /* CAST */
260 void
261 cast_setkey(CipherContext *cc, const u_char *key, u_int keylen)
262 {
263         CAST_set_key(&cc->u.cast.key, keylen, (unsigned char *) key);
264 }
265 void
266 cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
267 {
268         if (iv == NULL) 
269                 fatal("no IV for %s.", cc->cipher->name);
270         memcpy(cc->u.cast.iv, (char *)iv, 8);
271 }
272 void
273 cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
274 {
275         CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
276             CAST_ENCRYPT);
277 }
278 void
279 cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
280 {
281         CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv,
282             CAST_DECRYPT);
283 }
284
285 /* RIJNDAEL */
286
287 #define RIJNDAEL_BLOCKSIZE 16
288
289 void
290 rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
291 {
292         if (rijndael_makekey(&cc->u.rijndael.enc, RIJNDAEL_ENCRYPT,
293                     8*keylen, (char *)key) == -1)
294                 fatal("rijndael_setkey: RIJNDAEL_ENCRYPT");
295         if (rijndael_makekey(&cc->u.rijndael.dec, RIJNDAEL_DECRYPT,
296                     8*keylen, (char *)key) == -1)
297                 fatal("rijndael_setkey: RIJNDAEL_DECRYPT");
298 }
299 void
300 rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
301 {
302         if (iv == NULL || ivlen != RIJNDAEL_BLOCKSIZE) 
303                 fatal("bad/no IV for %s.", cc->cipher->name);
304         memcpy(cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
305 }
306
307 void
308 rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
309     u_int len)
310 {
311         rijndael_key *ctx = &cc->u.rijndael.enc;
312         u_char *iv = cc->u.rijndael.iv;
313         u_char in[RIJNDAEL_BLOCKSIZE];
314         u_char *cprev, *cnow, *plain;
315         int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
316         if (len == 0)
317                 return;
318         if (len % RIJNDAEL_BLOCKSIZE)
319                 fatal("rijndael_cbc_encrypt: bad len %d", len);
320         cnow  = dest;
321         plain = (u_char *) src;
322         cprev = iv;
323         for(i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
324             cnow+=RIJNDAEL_BLOCKSIZE) {
325                 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
326                         in[j] = plain[j] ^ cprev[j];
327                 rijndael_encrypt(ctx, in, cnow);
328                 cprev = cnow;
329         }
330         memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
331 }
332
333 void
334 rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
335     u_int len)
336 {
337         rijndael_key *ctx = &cc->u.rijndael.dec;
338         u_char *iv = cc->u.rijndael.iv;
339         u_char ivsaved[RIJNDAEL_BLOCKSIZE];
340         u_char *cnow  = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
341         u_char *plain = dest+len-RIJNDAEL_BLOCKSIZE;
342         u_char *ivp;
343         int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
344         if (len == 0)
345                 return;
346         if (len % RIJNDAEL_BLOCKSIZE)
347                 fatal("rijndael_cbc_decrypt: bad len %d", len);
348         memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
349         for(i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
350             plain-=RIJNDAEL_BLOCKSIZE) {
351                 rijndael_decrypt(ctx, cnow, plain);
352                 //rijndael_decrypt(cnow, plain, ctx->keySched, ctx->ROUNDS);
353                 ivp = (i == 1) ? iv : cnow-RIJNDAEL_BLOCKSIZE;
354                 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
355                         plain[j] ^= ivp[j];
356         }
357         memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
358 }
359
360 Cipher ciphers[] = {
361         { "none",
362                 SSH_CIPHER_NONE, 8, 0,
363                 none_setkey, none_setiv,
364                 none_crypt, none_crypt },
365         { "des",
366                 SSH_CIPHER_DES, 8, 8,
367                 des_ssh1_setkey, des_ssh1_setiv,
368                 des_ssh1_encrypt, des_ssh1_decrypt },
369         { "3des",
370                 SSH_CIPHER_3DES, 8, 16,
371                 des3_ssh1_setkey, des3_setiv,
372                 des3_ssh1_encrypt, des3_ssh1_decrypt },
373         { "blowfish",
374                 SSH_CIPHER_BLOWFISH, 8, 16,
375                 blowfish_setkey, blowfish_setiv,
376                 blowfish_ssh1_encrypt, blowfish_ssh1_decrypt },
377
378         { "3des-cbc",
379                 SSH_CIPHER_SSH2, 8, 24,
380                 des3_setkey, des3_setiv,
381                 des3_cbc_encrypt, des3_cbc_decrypt },
382         { "blowfish-cbc",
383                 SSH_CIPHER_SSH2, 8, 16,
384                 blowfish_setkey, blowfish_setiv,
385                 blowfish_cbc_encrypt, blowfish_cbc_decrypt },
386         { "cast128-cbc",
387                 SSH_CIPHER_SSH2, 8, 16,
388                 cast_setkey, cast_setiv,
389                 cast_cbc_encrypt, cast_cbc_decrypt },
390         { "arcfour",
391                 SSH_CIPHER_SSH2, 8, 16,
392                 arcfour_setkey, none_setiv,
393                 arcfour_crypt, arcfour_crypt },
394         { "aes128-cbc",
395                 SSH_CIPHER_SSH2, 16, 16,
396                 rijndael_setkey, rijndael_setiv,
397                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
398         { "aes192-cbc",
399                 SSH_CIPHER_SSH2, 16, 24,
400                 rijndael_setkey, rijndael_setiv,
401                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
402         { "aes256-cbc",
403                 SSH_CIPHER_SSH2, 16, 32,
404                 rijndael_setkey, rijndael_setiv,
405                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
406         { "rijndael128-cbc",
407                 SSH_CIPHER_SSH2, 16, 16,
408                 rijndael_setkey, rijndael_setiv,
409                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
410         { "rijndael192-cbc",
411                 SSH_CIPHER_SSH2, 16, 24,
412                 rijndael_setkey, rijndael_setiv,
413                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
414         { "rijndael256-cbc",
415                 SSH_CIPHER_SSH2, 16, 32,
416                 rijndael_setkey, rijndael_setiv,
417                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
418         { "rijndael-cbc@lysator.liu.se",
419                 SSH_CIPHER_SSH2, 16, 32,
420                 rijndael_setkey, rijndael_setiv,
421                 rijndael_cbc_encrypt, rijndael_cbc_decrypt },
422         { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL }
423 };
424
425 /*--*/
426
427 unsigned int
428 cipher_mask_ssh1(int client)
429 {
430         unsigned int mask = 0;
431         mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
432         mask |= 1 << SSH_CIPHER_BLOWFISH;
433         if (client) {
434                 mask |= 1 << SSH_CIPHER_DES;
435         }
436         return mask;
437 }
438
439 Cipher *
440 cipher_by_name(const char *name)
441 {
442         Cipher *c;
443         for (c = ciphers; c->name != NULL; c++)
444                 if (strcasecmp(c->name, name) == 0)
445                         return c;
446         return NULL;
447 }
448
449 Cipher *
450 cipher_by_number(int id)
451 {
452         Cipher *c;
453         for (c = ciphers; c->name != NULL; c++)
454                 if (c->number == id)
455                         return c;
456         return NULL;
457 }
458
459 #define CIPHER_SEP      ","
460 int
461 ciphers_valid(const char *names)
462 {
463         Cipher *c;
464         char *ciphers, *cp;
465         char *p;
466
467         if (names == NULL || strcmp(names, "") == 0)
468                 return 0;
469         ciphers = cp = xstrdup(names);
470         for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
471              (p = strsep(&cp, CIPHER_SEP))) {
472                 c = cipher_by_name(p);
473                 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
474                         debug("bad cipher %s [%s]", p, names);
475                         xfree(ciphers);
476                         return 0;
477                 } else {
478                         debug3("cipher ok: %s [%s]", p, names);
479                 }
480         }
481         debug3("ciphers ok: [%s]", names);
482         xfree(ciphers);
483         return 1;
484 }
485
486 /*
487  * Parses the name of the cipher.  Returns the number of the corresponding
488  * cipher, or -1 on error.
489  */
490
491 int
492 cipher_number(const char *name)
493 {
494         Cipher *c;
495         if (name == NULL)
496                 return -1;
497         c = cipher_by_name(name);
498         return (c==NULL) ? -1 : c->number;
499 }
500
501 char *
502 cipher_name(int id)
503 {
504         Cipher *c = cipher_by_number(id);
505         return (c==NULL) ? "<unknown>" : c->name;
506 }
507
508 void
509 cipher_init(CipherContext *cc, Cipher *cipher,
510     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
511 {
512         if (keylen < cipher->key_len)
513                 fatal("cipher_init: key length %d is insufficient for %s.",
514                     keylen, cipher->name);
515         if (iv != NULL && ivlen < cipher->block_size)
516                 fatal("cipher_init: iv length %d is insufficient for %s.",
517                     ivlen, cipher->name);
518         cc->cipher = cipher;
519         cipher->setkey(cc, key, keylen);
520         cipher->setiv(cc, iv, ivlen);
521 }
522
523 void
524 cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
525 {
526         if (len % cc->cipher->block_size)
527                 fatal("cipher_encrypt: bad plaintext length %d", len);
528         cc->cipher->encrypt(cc, dest, src, len);
529 }
530
531 void
532 cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
533 {
534         if (len % cc->cipher->block_size)
535                 fatal("cipher_decrypt: bad ciphertext length %d", len);
536         cc->cipher->decrypt(cc, dest, src, len);
537 }
538
539 /*
540  * Selects the cipher, and keys if by computing the MD5 checksum of the
541  * passphrase and using the resulting 16 bytes as the key.
542  */
543
544 void
545 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
546     const char *passphrase)
547 {
548         MD5_CTX md;
549         unsigned char digest[16];
550
551         MD5_Init(&md);
552         MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
553         MD5_Final(digest, &md);
554
555         cipher_init(cc, cipher, digest, 16, NULL, 0);
556
557         memset(digest, 0, sizeof(digest));
558         memset(&md, 0, sizeof(md));
559 }
This page took 0.199833 seconds and 5 git commands to generate.