2 * mppe.c - interface MPPE to the PPP code.
4 * By Frank Cusack <fcusack@fcusack.com>.
5 * Copyright (c) 2002,2003,2004 Google, Inc.
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation is hereby granted, provided that the above copyright
11 * notice appears in all copies. This software is provided without any
12 * warranty, express or implied.
15 * 08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
16 * Only need extra skb padding on transmit, not receive.
17 * 06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
18 * Use Linux kernel 2.6 arc4 and sha1 routines rather than
20 * 2/15/04 - TS: added #include <version.h> and testing for Kernel
21 * version before using
22 * MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
26 #include "netif/ppp/ppp_opts.h"
27 #if PPP_SUPPORT && MPPE_SUPPORT /* don't build if not configured for use in lwipopts.h */
33 #include "netif/ppp/ppp_impl.h"
34 #include "netif/ppp/ccp.h"
35 #include "netif/ppp/mppe.h"
36 #include "netif/ppp/pppdebug.h"
37 #include "netif/ppp/pppcrypt.h"
39 #define SHA1_SIGNATURE_SIZE 20
41 /* ppp_mppe_state.bits definitions */
42 #define MPPE_BIT_A 0x80 /* Encryption table were (re)inititalized */
43 #define MPPE_BIT_B 0x40 /* MPPC only (not implemented) */
44 #define MPPE_BIT_C 0x20 /* MPPC only (not implemented) */
45 #define MPPE_BIT_D 0x10 /* This is an encrypted frame */
47 #define MPPE_BIT_FLUSHED MPPE_BIT_A
48 #define MPPE_BIT_ENCRYPTED MPPE_BIT_D
50 #define MPPE_BITS(p) ((p)[0] & 0xf0)
51 #define MPPE_CCOUNT(p) ((((p)[0] & 0x0f) << 8) + (p)[1])
52 #define MPPE_CCOUNT_SPACE 0x1000 /* The size of the ccount space */
54 #define MPPE_OVHD 2 /* MPPE overhead/packet */
55 #define SANITY_MAX 1600 /* Max bogon factor we will tolerate */
58 * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
59 * Well, not what's written there, but rather what they meant.
61 static void mppe_rekey(ppp_mppe_state
* state
, int initial_key
)
63 lwip_sha1_context sha1_ctx
;
64 u8_t sha1_digest
[SHA1_SIGNATURE_SIZE
];
67 * Key Derivation, from RFC 3078, RFC 3079.
68 * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
70 lwip_sha1_init(&sha1_ctx
);
71 lwip_sha1_starts(&sha1_ctx
);
72 lwip_sha1_update(&sha1_ctx
, state
->master_key
, state
->keylen
);
73 lwip_sha1_update(&sha1_ctx
, mppe_sha1_pad1
, SHA1_PAD_SIZE
);
74 lwip_sha1_update(&sha1_ctx
, state
->session_key
, state
->keylen
);
75 lwip_sha1_update(&sha1_ctx
, mppe_sha1_pad2
, SHA1_PAD_SIZE
);
76 lwip_sha1_finish(&sha1_ctx
, sha1_digest
);
77 lwip_sha1_free(&sha1_ctx
);
78 MEMCPY(state
->session_key
, sha1_digest
, state
->keylen
);
81 lwip_arc4_init(&state
->arc4
);
82 lwip_arc4_setup(&state
->arc4
, sha1_digest
, state
->keylen
);
83 lwip_arc4_crypt(&state
->arc4
, state
->session_key
, state
->keylen
);
84 lwip_arc4_free(&state
->arc4
);
86 if (state
->keylen
== 8) {
88 state
->session_key
[0] = 0xd1;
89 state
->session_key
[1] = 0x26;
90 state
->session_key
[2] = 0x9e;
92 lwip_arc4_init(&state
->arc4
);
93 lwip_arc4_setup(&state
->arc4
, state
->session_key
, state
->keylen
);
97 * Set key, used by MSCHAP before mppe_init() is actually called by CCP so we
98 * don't have to keep multiple copies of keys.
100 void mppe_set_key(ppp_pcb
*pcb
, ppp_mppe_state
*state
, u8_t
*key
) {
101 LWIP_UNUSED_ARG(pcb
);
102 MEMCPY(state
->master_key
, key
, MPPE_MAX_KEY_LEN
);
106 * Initialize (de)compressor state.
109 mppe_init(ppp_pcb
*pcb
, ppp_mppe_state
*state
, u8_t options
)
112 const u8_t
*debugstr
= (const u8_t
*)"mppe_comp_init";
113 if (&pcb
->mppe_decomp
== state
) {
114 debugstr
= (const u8_t
*)"mppe_decomp_init";
116 #endif /* PPP_DEBUG */
119 MEMCPY(state
->session_key
, state
->master_key
, sizeof(state
->master_key
));
121 if (options
& MPPE_OPT_128
)
123 else if (options
& MPPE_OPT_40
)
126 PPPDEBUG(LOG_DEBUG
, ("%s[%d]: unknown key length\n", debugstr
,
128 lcp_close(pcb
, "MPPE required but peer negotiation failed");
131 if (options
& MPPE_OPT_STATEFUL
)
134 /* Generate the initial session key. */
135 mppe_rekey(state
, 1);
140 char mkey
[sizeof(state
->master_key
) * 2 + 1];
141 char skey
[sizeof(state
->session_key
) * 2 + 1];
143 PPPDEBUG(LOG_DEBUG
, ("%s[%d]: initialized with %d-bit %s mode\n",
144 debugstr
, pcb
->netif
->num
, (state
->keylen
== 16) ? 128 : 40,
145 (state
->stateful
) ? "stateful" : "stateless"));
147 for (i
= 0; i
< (int)sizeof(state
->master_key
); i
++)
148 sprintf(mkey
+ i
* 2, "%02x", state
->master_key
[i
]);
149 for (i
= 0; i
< (int)sizeof(state
->session_key
); i
++)
150 sprintf(skey
+ i
* 2, "%02x", state
->session_key
[i
]);
152 ("%s[%d]: keys: master: %s initial session: %s\n",
153 debugstr
, pcb
->netif
->num
, mkey
, skey
));
155 #endif /* PPP_DEBUG */
158 * Initialize the coherency count. The initial value is not specified
159 * in RFC 3078, but we can make a reasonable assumption that it will
160 * start at 0. Setting it to the max here makes the comp/decomp code
161 * do the right thing (determined through experiment).
163 state
->ccount
= MPPE_CCOUNT_SPACE
- 1;
166 * Note that even though we have initialized the key table, we don't
167 * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1.
169 state
->bits
= MPPE_BIT_ENCRYPTED
;
173 * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
174 * tell the compressor to rekey. Note that we MUST NOT rekey for
175 * every CCP Reset-Request; we only rekey on the next xmit packet.
176 * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
177 * So, rekeying for every CCP Reset-Request is broken as the peer will not
178 * know how many times we've rekeyed. (If we rekey and THEN get another
179 * CCP Reset-Request, we must rekey again.)
181 void mppe_comp_reset(ppp_pcb
*pcb
, ppp_mppe_state
*state
)
183 LWIP_UNUSED_ARG(pcb
);
184 state
->bits
|= MPPE_BIT_FLUSHED
;
188 * Compress (encrypt) a packet.
189 * It's strange to call this a compressor, since the output is always
190 * MPPE_OVHD + 2 bytes larger than the input.
193 mppe_compress(ppp_pcb
*pcb
, ppp_mppe_state
*state
, struct pbuf
**pb
, u16_t protocol
)
199 LWIP_UNUSED_ARG(pcb
);
201 /* TCP stack requires that we don't change the packet payload, therefore we copy
202 * the whole packet before encryption.
204 np
= pbuf_alloc(PBUF_RAW
, MPPE_OVHD
+ sizeof(protocol
) + (*pb
)->tot_len
, PBUF_POOL
);
209 /* Hide MPPE header + protocol */
210 pbuf_header(np
, -(s16_t
)(MPPE_OVHD
+ sizeof(protocol
)));
212 if ((err
= pbuf_copy(np
, *pb
)) != ERR_OK
) {
217 /* Reveal MPPE header + protocol */
218 pbuf_header(np
, (s16_t
)(MPPE_OVHD
+ sizeof(protocol
)));
221 pl
= (u8_t
*)np
->payload
;
223 state
->ccount
= (state
->ccount
+ 1) % MPPE_CCOUNT_SPACE
;
224 PPPDEBUG(LOG_DEBUG
, ("mppe_compress[%d]: ccount %d\n", pcb
->netif
->num
, state
->ccount
));
225 /* FIXME: use PUT* macros */
226 pl
[0] = state
->ccount
>>8;
227 pl
[1] = state
->ccount
;
229 if (!state
->stateful
|| /* stateless mode */
230 ((state
->ccount
& 0xff) == 0xff) || /* "flag" packet */
231 (state
->bits
& MPPE_BIT_FLUSHED
)) { /* CCP Reset-Request */
233 if (state
->stateful
) {
234 PPPDEBUG(LOG_DEBUG
, ("mppe_compress[%d]: rekeying\n", pcb
->netif
->num
));
236 mppe_rekey(state
, 0);
237 state
->bits
|= MPPE_BIT_FLUSHED
;
239 pl
[0] |= state
->bits
;
240 state
->bits
&= ~MPPE_BIT_FLUSHED
; /* reset for next xmit */
244 /* FIXME: add PFC support */
245 pl
[0] = protocol
>> 8;
248 /* Hide MPPE header */
249 pbuf_header(np
, -(s16_t
)MPPE_OVHD
);
252 for (n
= np
; n
!= NULL
; n
= n
->next
) {
253 lwip_arc4_crypt(&state
->arc4
, (u8_t
*)n
->payload
, n
->len
);
254 if (n
->tot_len
== n
->len
) {
259 /* Reveal MPPE header */
260 pbuf_header(np
, (s16_t
)MPPE_OVHD
);
266 * We received a CCP Reset-Ack. Just ignore it.
268 void mppe_decomp_reset(ppp_pcb
*pcb
, ppp_mppe_state
*state
)
270 LWIP_UNUSED_ARG(pcb
);
271 LWIP_UNUSED_ARG(state
);
276 * Decompress (decrypt) an MPPE packet.
279 mppe_decompress(ppp_pcb
*pcb
, ppp_mppe_state
*state
, struct pbuf
**pb
)
281 struct pbuf
*n0
= *pb
, *n
;
287 if (n0
->len
< MPPE_OVHD
) {
289 ("mppe_decompress[%d]: short pkt (%d)\n",
290 pcb
->netif
->num
, n0
->len
));
291 state
->sanity_errors
+= 100;
295 pl
= (u8_t
*)n0
->payload
;
296 flushed
= MPPE_BITS(pl
) & MPPE_BIT_FLUSHED
;
297 ccount
= MPPE_CCOUNT(pl
);
298 PPPDEBUG(LOG_DEBUG
, ("mppe_decompress[%d]: ccount %d\n",
299 pcb
->netif
->num
, ccount
));
301 /* sanity checks -- terminate with extreme prejudice */
302 if (!(MPPE_BITS(pl
) & MPPE_BIT_ENCRYPTED
)) {
304 ("mppe_decompress[%d]: ENCRYPTED bit not set!\n",
306 state
->sanity_errors
+= 100;
309 if (!state
->stateful
&& !flushed
) {
310 PPPDEBUG(LOG_DEBUG
, ("mppe_decompress[%d]: FLUSHED bit not set in "
311 "stateless mode!\n", pcb
->netif
->num
));
312 state
->sanity_errors
+= 100;
315 if (state
->stateful
&& ((ccount
& 0xff) == 0xff) && !flushed
) {
316 PPPDEBUG(LOG_DEBUG
, ("mppe_decompress[%d]: FLUSHED bit not set on "
317 "flag packet!\n", pcb
->netif
->num
));
318 state
->sanity_errors
+= 100;
323 * Check the coherency count.
326 if (!state
->stateful
) {
327 /* Discard late packet */
328 if ((ccount
- state
->ccount
) % MPPE_CCOUNT_SPACE
> MPPE_CCOUNT_SPACE
/ 2) {
329 state
->sanity_errors
++;
333 /* RFC 3078, sec 8.1. Rekey for every packet. */
334 while (state
->ccount
!= ccount
) {
335 mppe_rekey(state
, 0);
336 state
->ccount
= (state
->ccount
+ 1) % MPPE_CCOUNT_SPACE
;
339 /* RFC 3078, sec 8.2. */
340 if (!state
->discard
) {
342 state
->ccount
= (state
->ccount
+ 1) % MPPE_CCOUNT_SPACE
;
343 if (ccount
!= state
->ccount
) {
345 * (ccount > state->ccount)
346 * Packet loss detected, enter the discard state.
347 * Signal the peer to rekey (by sending a CCP Reset-Request).
350 ccp_resetrequest(pcb
);
356 /* ccp.c will be silent (no additional CCP Reset-Requests). */
359 /* Rekey for every missed "flag" packet. */
360 while ((ccount
& ~0xff) !=
361 (state
->ccount
& ~0xff)) {
362 mppe_rekey(state
, 0);
365 256) % MPPE_CCOUNT_SPACE
;
370 state
->ccount
= ccount
;
372 * Another problem with RFC 3078 here. It implies that the
373 * peer need not send a Reset-Ack packet. But RFC 1962
374 * requires it. Hopefully, M$ does send a Reset-Ack; even
375 * though it isn't required for MPPE synchronization, it is
376 * required to reset CCP state.
381 mppe_rekey(state
, 0);
384 /* Hide MPPE header */
385 pbuf_header(n0
, -(s16_t
)(MPPE_OVHD
));
387 /* Decrypt the packet. */
388 for (n
= n0
; n
!= NULL
; n
= n
->next
) {
389 lwip_arc4_crypt(&state
->arc4
, (u8_t
*)n
->payload
, n
->len
);
390 if (n
->tot_len
== n
->len
) {
395 /* good packet credit */
396 state
->sanity_errors
>>= 1;
401 if (state
->sanity_errors
>= SANITY_MAX
) {
403 * Take LCP down if the peer is sending too many bogons.
404 * We don't want to do this for a single or just a few
405 * instances since it could just be due to packet corruption.
407 lcp_close(pcb
, "Too many MPPE errors");
412 #endif /* PPP_SUPPORT && MPPE_SUPPORT */