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