0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 #include <crypto/arc4.h>
0046 #include <crypto/hash.h>
0047 #include <linux/err.h>
0048 #include <linux/fips.h>
0049 #include <linux/module.h>
0050 #include <linux/kernel.h>
0051 #include <linux/init.h>
0052 #include <linux/types.h>
0053 #include <linux/slab.h>
0054 #include <linux/string.h>
0055 #include <linux/mm.h>
0056 #include <linux/ppp_defs.h>
0057 #include <linux/ppp-comp.h>
0058 #include <linux/scatterlist.h>
0059 #include <asm/unaligned.h>
0060
0061 #include "ppp_mppe.h"
0062
0063 MODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
0064 MODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
0065 MODULE_LICENSE("Dual BSD/GPL");
0066 MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
0067 MODULE_VERSION("1.0.2");
0068
0069 #define SHA1_PAD_SIZE 40
0070
0071
0072
0073
0074
0075
0076 struct sha_pad {
0077 unsigned char sha_pad1[SHA1_PAD_SIZE];
0078 unsigned char sha_pad2[SHA1_PAD_SIZE];
0079 };
0080 static struct sha_pad *sha_pad;
0081
0082 static inline void sha_pad_init(struct sha_pad *shapad)
0083 {
0084 memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
0085 memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
0086 }
0087
0088
0089
0090
0091 struct ppp_mppe_state {
0092 struct arc4_ctx arc4;
0093 struct shash_desc *sha1;
0094 unsigned char *sha1_digest;
0095 unsigned char master_key[MPPE_MAX_KEY_LEN];
0096 unsigned char session_key[MPPE_MAX_KEY_LEN];
0097 unsigned keylen;
0098
0099
0100
0101 unsigned char bits;
0102 unsigned ccount;
0103 unsigned stateful;
0104 int discard;
0105 int sanity_errors;
0106 int unit;
0107 int debug;
0108 struct compstat stats;
0109 };
0110
0111
0112 #define MPPE_BIT_A 0x80
0113 #define MPPE_BIT_B 0x40
0114 #define MPPE_BIT_C 0x20
0115 #define MPPE_BIT_D 0x10
0116
0117 #define MPPE_BIT_FLUSHED MPPE_BIT_A
0118 #define MPPE_BIT_ENCRYPTED MPPE_BIT_D
0119
0120 #define MPPE_BITS(p) ((p)[4] & 0xf0)
0121 #define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
0122 #define MPPE_CCOUNT_SPACE 0x1000
0123
0124 #define MPPE_OVHD 2
0125 #define SANITY_MAX 1600
0126
0127
0128
0129
0130
0131 static void get_new_key_from_sha(struct ppp_mppe_state * state)
0132 {
0133 crypto_shash_init(state->sha1);
0134 crypto_shash_update(state->sha1, state->master_key,
0135 state->keylen);
0136 crypto_shash_update(state->sha1, sha_pad->sha_pad1,
0137 sizeof(sha_pad->sha_pad1));
0138 crypto_shash_update(state->sha1, state->session_key,
0139 state->keylen);
0140 crypto_shash_update(state->sha1, sha_pad->sha_pad2,
0141 sizeof(sha_pad->sha_pad2));
0142 crypto_shash_final(state->sha1, state->sha1_digest);
0143 }
0144
0145
0146
0147
0148
0149 static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
0150 {
0151 get_new_key_from_sha(state);
0152 if (!initial_key) {
0153 arc4_setkey(&state->arc4, state->sha1_digest, state->keylen);
0154 arc4_crypt(&state->arc4, state->session_key, state->sha1_digest,
0155 state->keylen);
0156 } else {
0157 memcpy(state->session_key, state->sha1_digest, state->keylen);
0158 }
0159 if (state->keylen == 8) {
0160
0161 state->session_key[0] = 0xd1;
0162 state->session_key[1] = 0x26;
0163 state->session_key[2] = 0x9e;
0164 }
0165 arc4_setkey(&state->arc4, state->session_key, state->keylen);
0166 }
0167
0168
0169
0170
0171 static void *mppe_alloc(unsigned char *options, int optlen)
0172 {
0173 struct ppp_mppe_state *state;
0174 struct crypto_shash *shash;
0175 unsigned int digestsize;
0176
0177 if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
0178 options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
0179 fips_enabled)
0180 goto out;
0181
0182 state = kzalloc(sizeof(*state), GFP_KERNEL);
0183 if (state == NULL)
0184 goto out;
0185
0186
0187 shash = crypto_alloc_shash("sha1", 0, 0);
0188 if (IS_ERR(shash))
0189 goto out_free;
0190
0191 state->sha1 = kmalloc(sizeof(*state->sha1) +
0192 crypto_shash_descsize(shash),
0193 GFP_KERNEL);
0194 if (!state->sha1) {
0195 crypto_free_shash(shash);
0196 goto out_free;
0197 }
0198 state->sha1->tfm = shash;
0199
0200 digestsize = crypto_shash_digestsize(shash);
0201 if (digestsize < MPPE_MAX_KEY_LEN)
0202 goto out_free;
0203
0204 state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
0205 if (!state->sha1_digest)
0206 goto out_free;
0207
0208
0209 memcpy(state->master_key, &options[CILEN_MPPE],
0210 sizeof(state->master_key));
0211 memcpy(state->session_key, state->master_key,
0212 sizeof(state->master_key));
0213
0214
0215
0216
0217
0218
0219 return (void *)state;
0220
0221 out_free:
0222 kfree(state->sha1_digest);
0223 if (state->sha1) {
0224 crypto_free_shash(state->sha1->tfm);
0225 kfree_sensitive(state->sha1);
0226 }
0227 kfree(state);
0228 out:
0229 return NULL;
0230 }
0231
0232
0233
0234
0235 static void mppe_free(void *arg)
0236 {
0237 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0238 if (state) {
0239 kfree(state->sha1_digest);
0240 crypto_free_shash(state->sha1->tfm);
0241 kfree_sensitive(state->sha1);
0242 kfree_sensitive(state);
0243 }
0244 }
0245
0246
0247
0248
0249 static int
0250 mppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
0251 const char *debugstr)
0252 {
0253 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0254 unsigned char mppe_opts;
0255
0256 if (optlen != CILEN_MPPE ||
0257 options[0] != CI_MPPE || options[1] != CILEN_MPPE)
0258 return 0;
0259
0260 MPPE_CI_TO_OPTS(&options[2], mppe_opts);
0261 if (mppe_opts & MPPE_OPT_128)
0262 state->keylen = 16;
0263 else if (mppe_opts & MPPE_OPT_40)
0264 state->keylen = 8;
0265 else {
0266 printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
0267 unit);
0268 return 0;
0269 }
0270 if (mppe_opts & MPPE_OPT_STATEFUL)
0271 state->stateful = 1;
0272
0273
0274 mppe_rekey(state, 1);
0275
0276 if (debug) {
0277 printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
0278 debugstr, unit, (state->keylen == 16) ? 128 : 40,
0279 (state->stateful) ? "stateful" : "stateless");
0280 printk(KERN_DEBUG
0281 "%s[%d]: keys: master: %*phN initial session: %*phN\n",
0282 debugstr, unit,
0283 (int)sizeof(state->master_key), state->master_key,
0284 (int)sizeof(state->session_key), state->session_key);
0285 }
0286
0287
0288
0289
0290
0291
0292
0293 state->ccount = MPPE_CCOUNT_SPACE - 1;
0294
0295
0296
0297
0298
0299 state->bits = MPPE_BIT_ENCRYPTED;
0300
0301 state->unit = unit;
0302 state->debug = debug;
0303
0304 return 1;
0305 }
0306
0307 static int
0308 mppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
0309 int hdrlen, int debug)
0310 {
0311
0312 return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
0313 }
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 static void mppe_comp_reset(void *arg)
0325 {
0326 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0327
0328 state->bits |= MPPE_BIT_FLUSHED;
0329 }
0330
0331
0332
0333
0334
0335
0336 static int
0337 mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
0338 int isize, int osize)
0339 {
0340 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0341 int proto;
0342
0343
0344
0345
0346 proto = PPP_PROTOCOL(ibuf);
0347 if (proto < 0x0021 || proto > 0x00fa)
0348 return 0;
0349
0350
0351 if (osize < isize + MPPE_OVHD + 2) {
0352
0353 printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
0354 "(have: %d need: %d)\n", state->unit,
0355 osize, osize + MPPE_OVHD + 2);
0356 return -1;
0357 }
0358
0359 osize = isize + MPPE_OVHD + 2;
0360
0361
0362
0363
0364 obuf[0] = PPP_ADDRESS(ibuf);
0365 obuf[1] = PPP_CONTROL(ibuf);
0366 put_unaligned_be16(PPP_COMP, obuf + 2);
0367 obuf += PPP_HDRLEN;
0368
0369 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
0370 if (state->debug >= 7)
0371 printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
0372 state->ccount);
0373 put_unaligned_be16(state->ccount, obuf);
0374
0375 if (!state->stateful ||
0376 ((state->ccount & 0xff) == 0xff) ||
0377 (state->bits & MPPE_BIT_FLUSHED)) {
0378
0379 if (state->debug && state->stateful)
0380 printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
0381 state->unit);
0382 mppe_rekey(state, 0);
0383 state->bits |= MPPE_BIT_FLUSHED;
0384 }
0385 obuf[0] |= state->bits;
0386 state->bits &= ~MPPE_BIT_FLUSHED;
0387
0388 obuf += MPPE_OVHD;
0389 ibuf += 2;
0390 isize -= 2;
0391
0392 arc4_crypt(&state->arc4, obuf, ibuf, isize);
0393
0394 state->stats.unc_bytes += isize;
0395 state->stats.unc_packets++;
0396 state->stats.comp_bytes += osize;
0397 state->stats.comp_packets++;
0398
0399 return osize;
0400 }
0401
0402
0403
0404
0405
0406 static void mppe_comp_stats(void *arg, struct compstat *stats)
0407 {
0408 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0409
0410 *stats = state->stats;
0411 }
0412
0413 static int
0414 mppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
0415 int hdrlen, int mru, int debug)
0416 {
0417
0418 return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
0419 }
0420
0421
0422
0423
0424 static void mppe_decomp_reset(void *arg)
0425 {
0426
0427 return;
0428 }
0429
0430
0431
0432
0433 static int
0434 mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
0435 int osize)
0436 {
0437 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0438 unsigned ccount;
0439 int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
0440
0441 if (isize <= PPP_HDRLEN + MPPE_OVHD) {
0442 if (state->debug)
0443 printk(KERN_DEBUG
0444 "mppe_decompress[%d]: short pkt (%d)\n",
0445 state->unit, isize);
0446 return DECOMP_ERROR;
0447 }
0448
0449
0450
0451
0452
0453
0454
0455 if (osize < isize - MPPE_OVHD - 1) {
0456 printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
0457 "(have: %d need: %d)\n", state->unit,
0458 osize, isize - MPPE_OVHD - 1);
0459 return DECOMP_ERROR;
0460 }
0461 osize = isize - MPPE_OVHD - 2;
0462
0463 ccount = MPPE_CCOUNT(ibuf);
0464 if (state->debug >= 7)
0465 printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
0466 state->unit, ccount);
0467
0468
0469 if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
0470 printk(KERN_DEBUG
0471 "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
0472 state->unit);
0473 state->sanity_errors += 100;
0474 goto sanity_error;
0475 }
0476 if (!state->stateful && !flushed) {
0477 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
0478 "stateless mode!\n", state->unit);
0479 state->sanity_errors += 100;
0480 goto sanity_error;
0481 }
0482 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
0483 printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
0484 "flag packet!\n", state->unit);
0485 state->sanity_errors += 100;
0486 goto sanity_error;
0487 }
0488
0489
0490
0491
0492
0493 if (!state->stateful) {
0494
0495 if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
0496 > MPPE_CCOUNT_SPACE / 2) {
0497 state->sanity_errors++;
0498 goto sanity_error;
0499 }
0500
0501
0502 while (state->ccount != ccount) {
0503 mppe_rekey(state, 0);
0504 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
0505 }
0506 } else {
0507
0508 if (!state->discard) {
0509
0510 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
0511 if (ccount != state->ccount) {
0512
0513
0514
0515
0516
0517 state->discard = 1;
0518 return DECOMP_ERROR;
0519 }
0520 } else {
0521
0522 if (!flushed) {
0523
0524 return DECOMP_ERROR;
0525 } else {
0526
0527 while ((ccount & ~0xff) !=
0528 (state->ccount & ~0xff)) {
0529 mppe_rekey(state, 0);
0530 state->ccount =
0531 (state->ccount +
0532 256) % MPPE_CCOUNT_SPACE;
0533 }
0534
0535
0536 state->discard = 0;
0537 state->ccount = ccount;
0538
0539
0540
0541
0542
0543
0544
0545 }
0546 }
0547 if (flushed)
0548 mppe_rekey(state, 0);
0549 }
0550
0551
0552
0553
0554
0555 obuf[0] = PPP_ADDRESS(ibuf);
0556 obuf[1] = PPP_CONTROL(ibuf);
0557 obuf += 2;
0558 ibuf += PPP_HDRLEN + MPPE_OVHD;
0559 isize -= PPP_HDRLEN + MPPE_OVHD;
0560
0561
0562
0563
0564
0565
0566 arc4_crypt(&state->arc4, obuf, ibuf, 1);
0567
0568
0569
0570
0571
0572
0573 if ((obuf[0] & 0x01) != 0) {
0574 obuf[1] = obuf[0];
0575 obuf[0] = 0;
0576 obuf++;
0577 osize++;
0578 }
0579
0580
0581 arc4_crypt(&state->arc4, obuf + 1, ibuf + 1, isize - 1);
0582
0583 state->stats.unc_bytes += osize;
0584 state->stats.unc_packets++;
0585 state->stats.comp_bytes += isize;
0586 state->stats.comp_packets++;
0587
0588
0589 state->sanity_errors >>= 1;
0590
0591 return osize;
0592
0593 sanity_error:
0594 if (state->sanity_errors < SANITY_MAX)
0595 return DECOMP_ERROR;
0596 else
0597
0598
0599
0600
0601 return DECOMP_FATALERROR;
0602 }
0603
0604
0605
0606
0607
0608
0609
0610 static void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
0611 {
0612 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
0613
0614 if (state->debug &&
0615 (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
0616 printk(KERN_DEBUG
0617 "mppe_incomp[%d]: incompressible (unencrypted) data! "
0618 "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
0619
0620 state->stats.inc_bytes += icnt;
0621 state->stats.inc_packets++;
0622 state->stats.unc_bytes += icnt;
0623 state->stats.unc_packets++;
0624 }
0625
0626
0627
0628
0629
0630
0631
0632
0633 static struct compressor ppp_mppe = {
0634 .compress_proto = CI_MPPE,
0635 .comp_alloc = mppe_alloc,
0636 .comp_free = mppe_free,
0637 .comp_init = mppe_comp_init,
0638 .comp_reset = mppe_comp_reset,
0639 .compress = mppe_compress,
0640 .comp_stat = mppe_comp_stats,
0641 .decomp_alloc = mppe_alloc,
0642 .decomp_free = mppe_free,
0643 .decomp_init = mppe_decomp_init,
0644 .decomp_reset = mppe_decomp_reset,
0645 .decompress = mppe_decompress,
0646 .incomp = mppe_incomp,
0647 .decomp_stat = mppe_comp_stats,
0648 .owner = THIS_MODULE,
0649 .comp_extra = MPPE_PAD,
0650 };
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 static int __init ppp_mppe_init(void)
0661 {
0662 int answer;
0663 if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC))
0664 return -ENODEV;
0665
0666 sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
0667 if (!sha_pad)
0668 return -ENOMEM;
0669 sha_pad_init(sha_pad);
0670
0671 answer = ppp_register_compressor(&ppp_mppe);
0672
0673 if (answer == 0)
0674 printk(KERN_INFO "PPP MPPE Compression module registered\n");
0675 else
0676 kfree(sha_pad);
0677
0678 return answer;
0679 }
0680
0681 static void __exit ppp_mppe_cleanup(void)
0682 {
0683 ppp_unregister_compressor(&ppp_mppe);
0684 kfree(sha_pad);
0685 }
0686
0687 module_init(ppp_mppe_init);
0688 module_exit(ppp_mppe_cleanup);