5 * Copyright (c) 1996-2000 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $
44 * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type.
46 * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or
47 * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_mppc.h>
62 #include "opt_netgraph.h"
64 #if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION)
65 #error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION
68 #ifdef NG_SEPARATE_MALLOC
69 MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node ");
71 #define M_NETGRAPH_MPPC M_NETGRAPH
74 #ifdef NETGRAPH_MPPC_COMPRESSION
75 /* XXX this file doesn't exist yet, but hopefully someday it will... */
78 #ifdef NETGRAPH_MPPC_ENCRYPTION
79 #include <crypto/rc4/rc4.h>
81 #include <crypto/sha1.h>
83 /* Decompression blowup */
84 #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */
85 #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */
87 /* MPPC/MPPE header length */
91 #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8)
94 * When packets are lost with MPPE, we may have to re-key arbitrarily
95 * many times to 'catch up' to the new jumped-ahead sequence number.
96 * Since this can be expensive, we pose a limit on how many re-keyings
97 * we will do at one time to avoid a possible D.O.S. vulnerability.
98 * This should instead be a configurable parameter.
100 #define MPPE_MAX_REKEY 1000
102 /* MPPC packet header bits */
103 #define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */
104 #define MPPC_FLAG_RESTART 0x4000 /* compress history restart */
105 #define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */
106 #define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */
107 #define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */
109 #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */
110 #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */
112 #define MPPC_COMP_OK 0x05
113 #define MPPC_DECOMP_OK 0x05
115 /* Per direction info */
117 struct ng_mppc_config cfg; /* configuration */
118 hook_p hook; /* netgraph hook */
119 u_int16_t cc:12; /* coherency count */
120 u_char flushed; /* clean history (xmit only) */
121 #ifdef NETGRAPH_MPPC_COMPRESSION
122 u_char *history; /* compression history */
124 #ifdef NETGRAPH_MPPC_ENCRYPTION
125 u_char key[MPPE_KEY_LEN]; /* session key */
126 struct rc4_state rc4; /* rc4 state */
130 /* Node private data */
131 struct ng_mppc_private {
132 struct ng_mppc_dir xmit; /* compress/encrypt config */
133 struct ng_mppc_dir recv; /* decompress/decrypt config */
134 ng_ID_t ctrlnode; /* path to controlling node */
136 typedef struct ng_mppc_private *priv_p;
138 /* Netgraph node methods */
139 static ng_constructor_t ng_mppc_constructor;
140 static ng_rcvmsg_t ng_mppc_rcvmsg;
141 static ng_shutdown_t ng_mppc_shutdown;
142 static ng_newhook_t ng_mppc_newhook;
143 static ng_rcvdata_t ng_mppc_rcvdata;
144 static ng_disconnect_t ng_mppc_disconnect;
146 /* Helper functions */
147 static int ng_mppc_compress(node_p node,
148 struct mbuf *m, struct mbuf **resultp);
149 static int ng_mppc_decompress(node_p node,
150 struct mbuf *m, struct mbuf **resultp);
151 static void ng_mppc_getkey(const u_char *h, u_char *h2, int len);
152 static void ng_mppc_updatekey(u_int32_t bits,
153 u_char *key0, u_char *key, struct rc4_state *rc4);
154 static void ng_mppc_reset_req(node_p node);
156 /* Node type descriptor */
157 static struct ng_type ng_mppc_typestruct = {
171 NETGRAPH_INIT(mppc, &ng_mppc_typestruct);
173 /* Fixed bit pattern to weaken keysize down to 40 or 56 bits */
174 static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e };
176 #define ERROUT(x) do { error = (x); goto done; } while (0)
178 /************************************************************************
180 ************************************************************************/
183 * Node type constructor
186 ng_mppc_constructor(node_p node)
190 /* Allocate private structure */
191 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO);
195 NG_NODE_SET_PRIVATE(node, priv);
202 * Give our OK for a hook to be added
205 ng_mppc_newhook(node_p node, hook_p hook, const char *name)
207 const priv_p priv = NG_NODE_PRIVATE(node);
210 /* Check hook name */
211 if (strcmp(name, NG_MPPC_HOOK_COMP) == 0)
212 hookPtr = &priv->xmit.hook;
213 else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0)
214 hookPtr = &priv->recv.hook;
218 /* See if already connected */
219 if (*hookPtr != NULL)
228 * Receive a control message
231 ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook)
233 const priv_p priv = NG_NODE_PRIVATE(node);
234 struct ng_mesg *resp = NULL;
238 NGI_GET_MSG(item, msg);
239 switch (msg->header.typecookie) {
240 case NGM_MPPC_COOKIE:
241 switch (msg->header.cmd) {
242 case NGM_MPPC_CONFIG_COMP:
243 case NGM_MPPC_CONFIG_DECOMP:
245 struct ng_mppc_config *const cfg
246 = (struct ng_mppc_config *)msg->data;
248 msg->header.cmd == NGM_MPPC_CONFIG_COMP;
249 struct ng_mppc_dir *const d = isComp ?
250 &priv->xmit : &priv->recv;
252 /* Check configuration */
253 if (msg->header.arglen != sizeof(*cfg))
256 if ((cfg->bits & ~MPPC_VALID_BITS) != 0)
258 #ifndef NETGRAPH_MPPC_COMPRESSION
259 if ((cfg->bits & MPPC_BIT) != 0)
260 ERROUT(EPROTONOSUPPORT);
262 #ifndef NETGRAPH_MPPC_ENCRYPTION
263 if ((cfg->bits & MPPE_BITS) != 0)
264 ERROUT(EPROTONOSUPPORT);
269 /* Save return address so we can send reset-req's */
271 priv->ctrlnode = NGI_RETADDR(item);
273 /* Configuration is OK, reset to it */
276 #ifdef NETGRAPH_MPPC_COMPRESSION
277 /* Initialize state buffers for compression */
278 if (d->history != NULL) {
279 FREE(d->history, M_NETGRAPH_MPPC);
282 if ((cfg->bits & MPPC_BIT) != 0) {
283 MALLOC(d->history, u_char *,
284 isComp ? MPPC_SizeOfCompressionHistory() :
285 MPPC_SizeOfDecompressionHistory(),
286 M_NETGRAPH_MPPC, M_NOWAIT);
287 if (d->history == NULL)
290 MPPC_InitCompressionHistory(d->history);
292 MPPC_InitDecompressionHistory(
298 #ifdef NETGRAPH_MPPC_ENCRYPTION
299 /* Generate initial session keys for encryption */
300 if ((cfg->bits & MPPE_BITS) != 0) {
301 const int keylen = KEYLEN(cfg->bits);
303 bcopy(cfg->startkey, d->key, keylen);
304 ng_mppc_getkey(cfg->startkey, d->key, keylen);
305 if ((cfg->bits & MPPE_40) != 0)
306 bcopy(&ng_mppe_weakenkey, d->key, 3);
307 else if ((cfg->bits & MPPE_56) != 0)
308 bcopy(&ng_mppe_weakenkey, d->key, 1);
309 rc4_init(&d->rc4, d->key, keylen);
313 /* Initialize other state */
319 case NGM_MPPC_RESETREQ:
320 ng_mppc_reset_req(node);
333 NG_RESPOND_MSG(error, node, item, resp);
339 * Receive incoming data on our hook.
342 ng_mppc_rcvdata(hook_p hook, item_p item)
344 const node_p node = NG_HOOK_NODE(hook);
345 const priv_p priv = NG_NODE_PRIVATE(node);
351 /* Compress and/or encrypt */
352 if (hook == priv->xmit.hook) {
353 if (!priv->xmit.cfg.enable) {
358 if ((error = ng_mppc_compress(node, m, &out)) != 0) {
364 NG_FWD_NEW_DATA(error, item, priv->xmit.hook, out);
368 /* Decompress and/or decrypt */
369 if (hook == priv->recv.hook) {
370 if (!priv->recv.cfg.enable) {
375 if ((error = ng_mppc_decompress(node, m, &out)) != 0) {
378 if (error == EINVAL && priv->ctrlnode != 0) {
381 /* Need to send a reset-request */
382 NG_MKMESSAGE(msg, NGM_MPPC_COOKIE,
383 NGM_MPPC_RESETREQ, 0, M_NOWAIT);
386 NG_SEND_MSG_ID(error, node, msg,
392 NG_FWD_NEW_DATA(error, item, priv->recv.hook, out);
397 panic("%s: unknown hook", __func__);
398 #ifdef RESTARTABLE_PANICS
407 ng_mppc_shutdown(node_p node)
409 const priv_p priv = NG_NODE_PRIVATE(node);
411 /* Take down netgraph node */
412 #ifdef NETGRAPH_MPPC_COMPRESSION
413 if (priv->xmit.history != NULL)
414 FREE(priv->xmit.history, M_NETGRAPH_MPPC);
415 if (priv->recv.history != NULL)
416 FREE(priv->recv.history, M_NETGRAPH_MPPC);
418 bzero(priv, sizeof(*priv));
419 FREE(priv, M_NETGRAPH_MPPC);
420 NG_NODE_SET_PRIVATE(node, NULL);
421 NG_NODE_UNREF(node); /* let the node escape */
429 ng_mppc_disconnect(hook_p hook)
431 const node_p node = NG_HOOK_NODE(hook);
432 const priv_p priv = NG_NODE_PRIVATE(node);
434 /* Zero out hook pointer */
435 if (hook == priv->xmit.hook)
436 priv->xmit.hook = NULL;
437 if (hook == priv->recv.hook)
438 priv->recv.hook = NULL;
440 /* Go away if no longer connected */
441 if ((NG_NODE_NUMHOOKS(node) == 0)
442 && NG_NODE_IS_VALID(node))
443 ng_rmnode_self(node);
447 /************************************************************************
449 ************************************************************************/
452 * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
453 * The original mbuf is not free'd.
456 ng_mppc_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
458 const priv_p priv = NG_NODE_PRIVATE(node);
459 struct ng_mppc_dir *const d = &priv->xmit;
460 u_char *inbuf, *outbuf;
468 header |= MPPC_FLAG_FLUSHED;
472 /* Work with contiguous regions of memory */
473 inlen = m->m_pkthdr.len;
474 MALLOC(inbuf, u_char *, inlen, M_NETGRAPH_MPPC, M_NOWAIT);
477 m_copydata(m, 0, inlen, (caddr_t)inbuf);
478 if ((d->cfg.bits & MPPC_BIT) != 0)
479 outlen = MPPC_MAX_BLOWUP(inlen);
481 outlen = MPPC_HDRLEN + inlen;
482 MALLOC(outbuf, u_char *, outlen, M_NETGRAPH_MPPC, M_NOWAIT);
483 if (outbuf == NULL) {
484 FREE(inbuf, M_NETGRAPH_MPPC);
488 /* Compress "inbuf" into "outbuf" (if compression enabled) */
489 #ifdef NETGRAPH_MPPC_COMPRESSION
490 if ((d->cfg.bits & MPPC_BIT) != 0) {
491 u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS;
492 u_char *source, *dest;
493 u_long sourceCnt, destCnt;
496 /* Prepare to compress */
499 dest = outbuf + MPPC_HDRLEN;
500 destCnt = outlen - MPPC_HDRLEN;
501 if ((d->cfg.bits & MPPE_STATELESS) == 0)
502 flags |= MPPC_SAVE_HISTORY;
505 rtn = MPPC_Compress(&source, &dest, &sourceCnt,
506 &destCnt, d->history, flags, 0);
508 /* Check return value */
509 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
510 if ((rtn & MPPC_EXPANDED) == 0
511 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) {
513 header |= MPPC_FLAG_COMPRESSED;
514 if ((rtn & MPPC_RESTART_HISTORY) != 0)
515 header |= MPPC_FLAG_RESTART;
517 d->flushed = (rtn & MPPC_EXPANDED) != 0
518 || (flags & MPPC_SAVE_HISTORY) == 0;
522 /* If we did not compress this packet, copy it to output buffer */
523 if ((header & MPPC_FLAG_COMPRESSED) == 0) {
524 bcopy(inbuf, outbuf + MPPC_HDRLEN, inlen);
525 outlen = MPPC_HDRLEN + inlen;
527 FREE(inbuf, M_NETGRAPH_MPPC);
529 /* Always set the flushed bit in stateless mode */
530 if ((d->cfg.bits & MPPE_STATELESS) != 0)
531 header |= MPPC_FLAG_FLUSHED;
533 /* Now encrypt packet (if encryption enabled) */
534 #ifdef NETGRAPH_MPPC_ENCRYPTION
535 if ((d->cfg.bits & MPPE_BITS) != 0) {
537 /* Set header bits; need to reset key if we say we did */
538 header |= MPPC_FLAG_ENCRYPTED;
539 if ((header & MPPC_FLAG_FLUSHED) != 0)
540 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
542 /* Update key if it's time */
543 if ((d->cfg.bits & MPPE_STATELESS) != 0
544 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
545 ng_mppc_updatekey(d->cfg.bits,
546 d->cfg.startkey, d->key, &d->rc4);
550 rc4_crypt(&d->rc4, outbuf + MPPC_HDRLEN,
551 outbuf + MPPC_HDRLEN, outlen - MPPC_HDRLEN);
555 /* Update sequence number */
559 *((u_int16_t *)outbuf) = htons(header);
561 /* Return packet in an mbuf */
562 *resultp = m_devget((caddr_t)outbuf, outlen, 0, NULL, NULL);
563 FREE(outbuf, M_NETGRAPH_MPPC);
564 return (*resultp == NULL ? ENOBUFS : 0);
568 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
569 * The original mbuf is not free'd.
572 ng_mppc_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
574 const priv_p priv = NG_NODE_PRIVATE(node);
575 struct ng_mppc_dir *const d = &priv->recv;
576 u_int16_t header, cc;
581 /* Pull off header */
582 if (m->m_pkthdr.len < MPPC_HDRLEN)
584 m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header);
585 header = ntohs(header);
586 cc = (header & MPPC_CCOUNT_MASK);
588 /* Copy payload into a contiguous region of memory */
589 len = m->m_pkthdr.len - MPPC_HDRLEN;
590 MALLOC(buf, u_char *, len, M_NETGRAPH_MPPC, M_NOWAIT);
593 m_copydata(m, MPPC_HDRLEN, len, (caddr_t)buf);
595 /* Check for an unexpected jump in the sequence number */
596 numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK);
598 /* If flushed bit set, we can always handle packet */
599 if ((header & MPPC_FLAG_FLUSHED) != 0) {
600 #ifdef NETGRAPH_MPPC_COMPRESSION
601 if (d->history != NULL)
602 MPPC_InitDecompressionHistory(d->history);
604 #ifdef NETGRAPH_MPPC_ENCRYPTION
605 if ((d->cfg.bits & MPPE_BITS) != 0) {
608 /* How many times are we going to have to re-key? */
609 rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ?
610 numLost : (numLost / (MPPE_UPDATE_MASK + 1));
611 if (rekey > MPPE_MAX_REKEY) {
612 log(LOG_ERR, "%s: too many (%d) packets"
613 " dropped, disabling node %p!",
614 __func__, numLost, node);
615 priv->recv.cfg.enable = 0;
619 /* Re-key as necessary to catch up to peer */
620 while (d->cc != cc) {
621 if ((d->cfg.bits & MPPE_STATELESS) != 0
622 || (d->cc & MPPE_UPDATE_MASK)
623 == MPPE_UPDATE_FLAG) {
624 ng_mppc_updatekey(d->cfg.bits,
625 d->cfg.startkey, d->key, &d->rc4);
630 /* Reset key (except in stateless mode, see below) */
631 if ((d->cfg.bits & MPPE_STATELESS) == 0)
632 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
635 d->cc = cc; /* skip over lost seq numbers */
636 numLost = 0; /* act like no packets were lost */
639 /* Can't decode non-sequential packets without a flushed bit */
644 if ((header & MPPC_FLAG_ENCRYPTED) != 0) {
646 /* Are we not expecting encryption? */
647 if ((d->cfg.bits & MPPE_BITS) == 0) {
648 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
649 __func__, "encrypted");
653 #ifdef NETGRAPH_MPPC_ENCRYPTION
654 /* Update key if it's time (always in stateless mode) */
655 if ((d->cfg.bits & MPPE_STATELESS) != 0
656 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) {
657 ng_mppc_updatekey(d->cfg.bits,
658 d->cfg.startkey, d->key, &d->rc4);
662 rc4_crypt(&d->rc4, buf, buf, len);
666 /* Are we expecting encryption? */
667 if ((d->cfg.bits & MPPE_BITS) != 0) {
668 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
669 __func__, "unencrypted");
674 /* Update coherency count for next time (12 bit arithmetic) */
677 /* Check for unexpected compressed packet */
678 if ((header & MPPC_FLAG_COMPRESSED) != 0
679 && (d->cfg.bits & MPPC_BIT) == 0) {
680 log(LOG_ERR, "%s: rec'd unexpectedly %s packet",
681 __func__, "compressed");
683 FREE(buf, M_NETGRAPH_MPPC);
687 #ifdef NETGRAPH_MPPC_COMPRESSION
688 /* Decompress packet */
689 if ((header & MPPC_FLAG_COMPRESSED) != 0) {
690 int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS;
691 u_char *decompbuf, *source, *dest;
692 u_long sourceCnt, destCnt;
695 /* Allocate a buffer for decompressed data */
696 MALLOC(decompbuf, u_char *, MPPC_DECOMP_BUFSIZE
697 + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT);
698 if (decompbuf == NULL) {
699 FREE(buf, M_NETGRAPH_MPPC);
702 decomplen = MPPC_DECOMP_BUFSIZE;
704 /* Prepare to decompress */
709 if ((header & MPPC_FLAG_RESTART) != 0)
710 flags |= MPPC_RESTART_HISTORY;
713 rtn = MPPC_Decompress(&source, &dest,
714 &sourceCnt, &destCnt, d->history, flags);
716 /* Check return value */
717 KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__));
718 if ((rtn & MPPC_DEST_EXHAUSTED) != 0
719 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) {
720 log(LOG_ERR, "%s: decomp returned 0x%x",
722 FREE(decompbuf, M_NETGRAPH_MPPC);
726 /* Replace compressed data with decompressed data */
727 FREE(buf, M_NETGRAPH_MPPC);
729 len = decomplen - destCnt;
733 /* Return result in an mbuf */
734 *resultp = m_devget((caddr_t)buf, len, 0, NULL, NULL);
735 FREE(buf, M_NETGRAPH_MPPC);
736 return (*resultp == NULL ? ENOBUFS : 0);
740 * The peer has sent us a CCP ResetRequest, so reset our transmit state.
743 ng_mppc_reset_req(node_p node)
745 const priv_p priv = NG_NODE_PRIVATE(node);
746 struct ng_mppc_dir *const d = &priv->xmit;
748 #ifdef NETGRAPH_MPPC_COMPRESSION
749 if (d->history != NULL)
750 MPPC_InitCompressionHistory(d->history);
752 #ifdef NETGRAPH_MPPC_ENCRYPTION
753 if ((d->cfg.bits & MPPE_STATELESS) == 0)
754 rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits));
760 * Generate a new encryption key
763 ng_mppc_getkey(const u_char *h, u_char *h2, int len)
765 static const u_char pad1[10] =
766 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
767 static const u_char pad2[10] =
768 { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, };
773 bzero(&hash, sizeof(hash));
775 SHA1Update(&c, h, len);
776 for (k = 0; k < 4; k++)
777 SHA1Update(&c, pad1, sizeof(pad2));
778 SHA1Update(&c, h2, len);
779 for (k = 0; k < 4; k++)
780 SHA1Update(&c, pad2, sizeof(pad2));
782 bcopy(hash, h2, len);
786 * Update the encryption key
789 ng_mppc_updatekey(u_int32_t bits,
790 u_char *key0, u_char *key, struct rc4_state *rc4)
792 const int keylen = KEYLEN(bits);
794 ng_mppc_getkey(key0, key, keylen);
795 rc4_init(rc4, key, keylen);
796 rc4_crypt(rc4, key, key, keylen);
797 if ((bits & MPPE_40) != 0)
798 bcopy(&ng_mppe_weakenkey, key, 3);
799 else if ((bits & MPPE_56) != 0)
800 bcopy(&ng_mppe_weakenkey, key, 1);
801 rc4_init(rc4, key, keylen);