1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/err.h>
47#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/init.h>
50#include <linux/types.h>
51#include <linux/slab.h>
52#include <linux/string.h>
53#include <linux/crypto.h>
54#include <linux/mm.h>
55#include <linux/ppp_defs.h>
56#include <linux/ppp-comp.h>
57#include <linux/scatterlist.h>
58#include <asm/unaligned.h>
59
60#include "ppp_mppe.h"
61
62MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
63MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
64MODULE_LICENSE("Dual BSD/GPL");
65MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
66MODULE_VERSION("1.0.2");
67
68static unsigned int
69setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
70{
71 sg_set_buf(sg, address, length);
72 return length;
73}
74
75#define SHA1_PAD_SIZE 40
76
77
78
79
80
81
82struct sha_pad {
83 unsigned char sha_pad1[SHA1_PAD_SIZE];
84 unsigned char sha_pad2[SHA1_PAD_SIZE];
85};
86static struct sha_pad *sha_pad;
87
88static inline void sha_pad_init(struct sha_pad *shapad)
89{
90 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
91 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
92}
93
94
95
96
97struct ppp_mppe_state {
98 struct crypto_blkcipher *arc4;
99 struct crypto_hash *sha1;
100 unsigned char *sha1_digest;
101 unsigned char master_key[MPPE_MAX_KEY_LEN];
102 unsigned char session_key[MPPE_MAX_KEY_LEN];
103 unsigned keylen;
104
105
106
107 unsigned char bits;
108 unsigned ccount;
109 unsigned stateful;
110 int discard;
111 int sanity_errors;
112 int unit;
113 int debug;
114 struct compstat stats;
115};
116
117
118#define MPPE_BIT_A 0x80
119#define MPPE_BIT_B 0x40
120#define MPPE_BIT_C 0x20
121#define MPPE_BIT_D 0x10
122
123#define MPPE_BIT_FLUSHED MPPE_BIT_A
124#define MPPE_BIT_ENCRYPTED MPPE_BIT_D
125
126#define MPPE_BITS(p) ((p)[4] & 0xf0)
127#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
128#define MPPE_CCOUNT_SPACE 0x1000
129
130#define MPPE_OVHD 2
131#define SANITY_MAX 1600
132
133
134
135
136
137static void get_new_key_from_sha(struct ppp_mppe_state * state)
138{
139 struct hash_desc desc;
140 struct scatterlist sg[4];
141 unsigned int nbytes;
142
143 sg_init_table(sg, 4);
144
145 nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
146 nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
147 sizeof(sha_pad->sha_pad1));
148 nbytes += setup_sg(&sg[2], state->session_key, state->keylen);
149 nbytes += setup_sg(&sg[3], sha_pad->sha_pad2,
150 sizeof(sha_pad->sha_pad2));
151
152 desc.tfm = state->sha1;
153 desc.flags = 0;
154
155 crypto_hash_digest(&desc, sg, nbytes, state->sha1_digest);
156}
157
158
159
160
161
162static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
163{
164 struct scatterlist sg_in[1], sg_out[1];
165 struct blkcipher_desc desc = { .tfm = state->arc4 };
166
167 get_new_key_from_sha(state);
168 if (!initial_key) {
169 crypto_blkcipher_setkey(state->arc4, state->sha1_digest,
170 state->keylen);
171 sg_init_table(sg_in, 1);
172 sg_init_table(sg_out, 1);
173 setup_sg(sg_in, state->sha1_digest, state->keylen);
174 setup_sg(sg_out, state->session_key, state->keylen);
175 if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
176 state->keylen) != 0) {
177 printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
178 }
179 } else {
180 memcpy(state->session_key, state->sha1_digest, state->keylen);
181 }
182 if (state->keylen == 8) {
183
184 state->session_key[0] = 0xd1;
185 state->session_key[1] = 0x26;
186 state->session_key[2] = 0x9e;
187 }
188 crypto_blkcipher_setkey(state->arc4, state->session_key, state->keylen);
189}
190
191
192
193
194static void *mppe_alloc(unsigned char *options, int optlen)
195{
196 struct ppp_mppe_state *state;
197 unsigned int digestsize;
198
199 if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
200 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
201 goto out;
202
203 state = kzalloc(sizeof(*state), GFP_KERNEL);
204 if (state == NULL)
205 goto out;
206
207
208 state->arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
209 if (IS_ERR(state->arc4)) {
210 state->arc4 = NULL;
211 goto out_free;
212 }
213
214 state->sha1 = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
215 if (IS_ERR(state->sha1)) {
216 state->sha1 = NULL;
217 goto out_free;
218 }
219
220 digestsize = crypto_hash_digestsize(state->sha1);
221 if (digestsize < MPPE_MAX_KEY_LEN)
222 goto out_free;
223
224 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
225 if (!state->sha1_digest)
226 goto out_free;
227
228
229 memcpy(state->master_key, &options[CILEN_MPPE],
230 sizeof(state->master_key));
231 memcpy(state->session_key, state->master_key,
232 sizeof(state->master_key));
233
234
235
236
237
238
239 return (void *)state;
240
241 out_free:
242 if (state->sha1_digest)
243 kfree(state->sha1_digest);
244 if (state->sha1)
245 crypto_free_hash(state->sha1);
246 if (state->arc4)
247 crypto_free_blkcipher(state->arc4);
248 kfree(state);
249 out:
250 return NULL;
251}
252
253
254
255
256static void mppe_free(void *arg)
257{
258 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
259 if (state) {
260 if (state->sha1_digest)
261 kfree(state->sha1_digest);
262 if (state->sha1)
263 crypto_free_hash(state->sha1);
264 if (state->arc4)
265 crypto_free_blkcipher(state->arc4);
266 kfree(state);
267 }
268}
269
270
271
272
273static int
274mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
275 const char *debugstr)
276{
277 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
278 unsigned char mppe_opts;
279
280 if (optlen != CILEN_MPPE ||
281 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
282 return 0;
283
284 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
285 if (mppe_opts & MPPE_OPT_128)
286 state->keylen = 16;
287 else if (mppe_opts & MPPE_OPT_40)
288 state->keylen = 8;
289 else {
290 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
291 unit);
292 return 0;
293 }
294 if (mppe_opts & MPPE_OPT_STATEFUL)
295 state->stateful = 1;
296
297
298 mppe_rekey(state, 1);
299
300 if (debug) {
301 int i;
302 char mkey[sizeof(state->master_key) * 2 + 1];
303 char skey[sizeof(state->session_key) * 2 + 1];
304
305 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
306 debugstr, unit, (state->keylen == 16) ? 128 : 40,
307 (state->stateful) ? "stateful" : "stateless");
308
309 for (i = 0; i < sizeof(state->master_key); i++)
310 sprintf(mkey + i * 2, "%02x", state->master_key[i]);
311 for (i = 0; i < sizeof(state->session_key); i++)
312 sprintf(skey + i * 2, "%02x", state->session_key[i]);
313 printk(KERN_DEBUG
314 "%s[%d]: keys: master: %s initial session: %s\n",
315 debugstr, unit, mkey, skey);
316 }
317
318
319
320
321
322
323
324 state->ccount = MPPE_CCOUNT_SPACE - 1;
325
326
327
328
329
330 state->bits = MPPE_BIT_ENCRYPTED;
331
332 state->unit = unit;
333 state->debug = debug;
334
335 return 1;
336}
337
338static int
339mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
340 int hdrlen, int debug)
341{
342
343 return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
344}
345
346
347
348
349
350
351
352
353
354
355static void mppe_comp_reset(void *arg)
356{
357 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
358
359 state->bits |= MPPE_BIT_FLUSHED;
360}
361
362
363
364
365
366
367static int
368mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
369 int isize, int osize)
370{
371 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
372 struct blkcipher_desc desc = { .tfm = state->arc4 };
373 int proto;
374 struct scatterlist sg_in[1], sg_out[1];
375
376
377
378
379 proto = PPP_PROTOCOL(ibuf);
380 if (proto < 0x0021 || proto > 0x00fa)
381 return 0;
382
383
384 if (osize < isize + MPPE_OVHD + 2) {
385
386 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
387 "(have: %d need: %d)\n", state->unit,
388 osize, osize + MPPE_OVHD + 2);
389 return -1;
390 }
391
392 osize = isize + MPPE_OVHD + 2;
393
394
395
396
397 obuf[0] = PPP_ADDRESS(ibuf);
398 obuf[1] = PPP_CONTROL(ibuf);
399 put_unaligned_be16(PPP_COMP, obuf + 2);
400 obuf += PPP_HDRLEN;
401
402 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
403 if (state->debug >= 7)
404 printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
405 state->ccount);
406 put_unaligned_be16(state->ccount, obuf);
407
408 if (!state->stateful ||
409 ((state->ccount & 0xff) == 0xff) ||
410 (state->bits & MPPE_BIT_FLUSHED)) {
411
412 if (state->debug && state->stateful)
413 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
414 state->unit);
415 mppe_rekey(state, 0);
416 state->bits |= MPPE_BIT_FLUSHED;
417 }
418 obuf[0] |= state->bits;
419 state->bits &= ~MPPE_BIT_FLUSHED;
420
421 obuf += MPPE_OVHD;
422 ibuf += 2;
423 isize -= 2;
424
425
426 sg_init_table(sg_in, 1);
427 sg_init_table(sg_out, 1);
428 setup_sg(sg_in, ibuf, isize);
429 setup_sg(sg_out, obuf, osize);
430 if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in, isize) != 0) {
431 printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
432 return -1;
433 }
434
435 state->stats.unc_bytes += isize;
436 state->stats.unc_packets++;
437 state->stats.comp_bytes += osize;
438 state->stats.comp_packets++;
439
440 return osize;
441}
442
443
444
445
446
447static void mppe_comp_stats(void *arg, struct compstat *stats)
448{
449 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
450
451 *stats = state->stats;
452}
453
454static int
455mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
456 int hdrlen, int mru, int debug)
457{
458
459 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
460}
461
462
463
464
465static void mppe_decomp_reset(void *arg)
466{
467
468 return;
469}
470
471
472
473
474static int
475mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
476 int osize)
477{
478 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
479 struct blkcipher_desc desc = { .tfm = state->arc4 };
480 unsigned ccount;
481 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
482 int sanity = 0;
483 struct scatterlist sg_in[1], sg_out[1];
484
485 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
486 if (state->debug)
487 printk(KERN_DEBUG
488 "mppe_decompress[%d]: short pkt (%d)\n",
489 state->unit, isize);
490 return DECOMP_ERROR;
491 }
492
493
494
495
496
497
498
499 if (osize < isize - MPPE_OVHD - 1) {
500 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
501 "(have: %d need: %d)\n", state->unit,
502 osize, isize - MPPE_OVHD - 1);
503 return DECOMP_ERROR;
504 }
505 osize = isize - MPPE_OVHD - 2;
506
507 ccount = MPPE_CCOUNT(ibuf);
508 if (state->debug >= 7)
509 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
510 state->unit, ccount);
511
512
513 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
514 printk(KERN_DEBUG
515 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
516 state->unit);
517 state->sanity_errors += 100;
518 sanity = 1;
519 }
520 if (!state->stateful && !flushed) {
521 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
522 "stateless mode!\n", state->unit);
523 state->sanity_errors += 100;
524 sanity = 1;
525 }
526 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
527 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
528 "flag packet!\n", state->unit);
529 state->sanity_errors += 100;
530 sanity = 1;
531 }
532
533 if (sanity) {
534 if (state->sanity_errors < SANITY_MAX)
535 return DECOMP_ERROR;
536 else
537
538
539
540
541
542 return DECOMP_FATALERROR;
543 }
544
545
546
547
548
549 if (!state->stateful) {
550
551 while (state->ccount != ccount) {
552 mppe_rekey(state, 0);
553 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
554 }
555 } else {
556
557 if (!state->discard) {
558
559 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
560 if (ccount != state->ccount) {
561
562
563
564
565
566 state->discard = 1;
567 return DECOMP_ERROR;
568 }
569 } else {
570
571 if (!flushed) {
572
573 return DECOMP_ERROR;
574 } else {
575
576 while ((ccount & ~0xff) !=
577 (state->ccount & ~0xff)) {
578 mppe_rekey(state, 0);
579 state->ccount =
580 (state->ccount +
581 256) % MPPE_CCOUNT_SPACE;
582 }
583
584
585 state->discard = 0;
586 state->ccount = ccount;
587
588
589
590
591
592
593
594 }
595 }
596 if (flushed)
597 mppe_rekey(state, 0);
598 }
599
600
601
602
603
604 obuf[0] = PPP_ADDRESS(ibuf);
605 obuf[1] = PPP_CONTROL(ibuf);
606 obuf += 2;
607 ibuf += PPP_HDRLEN + MPPE_OVHD;
608 isize -= PPP_HDRLEN + MPPE_OVHD;
609
610
611
612
613
614
615 sg_init_table(sg_in, 1);
616 sg_init_table(sg_out, 1);
617 setup_sg(sg_in, ibuf, 1);
618 setup_sg(sg_out, obuf, 1);
619 if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, 1) != 0) {
620 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
621 return DECOMP_ERROR;
622 }
623
624
625
626
627
628
629 if ((obuf[0] & 0x01) != 0) {
630 obuf[1] = obuf[0];
631 obuf[0] = 0;
632 obuf++;
633 osize++;
634 }
635
636
637 setup_sg(sg_in, ibuf + 1, isize - 1);
638 setup_sg(sg_out, obuf + 1, osize - 1);
639 if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, isize - 1)) {
640 printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
641 return DECOMP_ERROR;
642 }
643
644 state->stats.unc_bytes += osize;
645 state->stats.unc_packets++;
646 state->stats.comp_bytes += isize;
647 state->stats.comp_packets++;
648
649
650 state->sanity_errors >>= 1;
651
652 return osize;
653}
654
655
656
657
658
659
660
661static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
662{
663 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
664
665 if (state->debug &&
666 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
667 printk(KERN_DEBUG
668 "mppe_incomp[%d]: incompressible (unencrypted) data! "
669 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
670
671 state->stats.inc_bytes += icnt;
672 state->stats.inc_packets++;
673 state->stats.unc_bytes += icnt;
674 state->stats.unc_packets++;
675}
676
677
678
679
680
681
682
683
684static struct compressor ppp_mppe = {
685 .compress_proto = CI_MPPE,
686 .comp_alloc = mppe_alloc,
687 .comp_free = mppe_free,
688 .comp_init = mppe_comp_init,
689 .comp_reset = mppe_comp_reset,
690 .compress = mppe_compress,
691 .comp_stat = mppe_comp_stats,
692 .decomp_alloc = mppe_alloc,
693 .decomp_free = mppe_free,
694 .decomp_init = mppe_decomp_init,
695 .decomp_reset = mppe_decomp_reset,
696 .decompress = mppe_decompress,
697 .incomp = mppe_incomp,
698 .decomp_stat = mppe_comp_stats,
699 .owner = THIS_MODULE,
700 .comp_extra = MPPE_PAD,
701};
702
703
704
705
706
707
708
709
710
711static int __init ppp_mppe_init(void)
712{
713 int answer;
714 if (!(crypto_has_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC) &&
715 crypto_has_hash("sha1", 0, CRYPTO_ALG_ASYNC)))
716 return -ENODEV;
717
718 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
719 if (!sha_pad)
720 return -ENOMEM;
721 sha_pad_init(sha_pad);
722
723 answer = ppp_register_compressor(&ppp_mppe);
724
725 if (answer == 0)
726 printk(KERN_INFO "PPP MPPE Compression module registered\n");
727 else
728 kfree(sha_pad);
729
730 return answer;
731}
732
733static void __exit ppp_mppe_cleanup(void)
734{
735 ppp_unregister_compressor(&ppp_mppe);
736 kfree(sha_pad);
737}
738
739module_init(ppp_mppe_init);
740module_exit(ppp_mppe_cleanup);
741