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