27 #if PPP_SUPPORT && MPPE_SUPPORT 38 #if LWIP_INCLUDED_POLARSSL_SHA1 44 #if LWIP_INCLUDED_POLARSSL_ARC4 50 #define SHA1_SIGNATURE_SIZE 20 53 #define MPPE_BIT_A 0x80 54 #define MPPE_BIT_B 0x40 55 #define MPPE_BIT_C 0x20 56 #define MPPE_BIT_D 0x10 58 #define MPPE_BIT_FLUSHED MPPE_BIT_A 59 #define MPPE_BIT_ENCRYPTED MPPE_BIT_D 61 #define MPPE_BITS(p) ((p)[0] & 0xf0) 62 #define MPPE_CCOUNT(p) ((((p)[0] & 0x0f) << 8) + (p)[1]) 63 #define MPPE_CCOUNT_SPACE 0x1000 66 #define SANITY_MAX 1600 72 static void mppe_rekey(ppp_mppe_state * state,
int initial_key)
74 sha1_context sha1_ctx;
75 u8_t sha1_digest[SHA1_SIGNATURE_SIZE];
81 sha1_starts(&sha1_ctx);
82 sha1_update(&sha1_ctx, state->master_key, state->keylen);
83 sha1_update(&sha1_ctx, mppe_sha1_pad1, SHA1_PAD_SIZE);
84 sha1_update(&sha1_ctx, state->session_key, state->keylen);
85 sha1_update(&sha1_ctx, mppe_sha1_pad2, SHA1_PAD_SIZE);
86 sha1_finish(&sha1_ctx, sha1_digest);
87 MEMCPY(state->session_key, sha1_digest, state->keylen);
90 arc4_setup(&state->arc4, sha1_digest, state->keylen);
91 arc4_crypt(&state->arc4, state->session_key, state->keylen);
93 if (state->keylen == 8) {
95 state->session_key[0] = 0xd1;
96 state->session_key[1] = 0x26;
97 state->session_key[2] = 0x9e;
99 arc4_setup(&state->arc4, state->session_key, state->keylen);
106 void mppe_set_key(ppp_pcb *pcb, ppp_mppe_state *state,
u8_t *key) {
108 MEMCPY(state->master_key, key, MPPE_MAX_KEY_LEN);
115 mppe_init(ppp_pcb *pcb, ppp_mppe_state *state,
u8_t options)
118 const u8_t *debugstr = (
const u8_t*)
"mppe_comp_init";
119 if (&pcb->mppe_decomp == state) {
120 debugstr = (
const u8_t*)
"mppe_decomp_init";
125 MEMCPY(state->session_key, state->master_key,
sizeof(state->master_key));
127 if (options & MPPE_OPT_128)
129 else if (options & MPPE_OPT_40)
132 PPPDEBUG(LOG_DEBUG, (
"%s[%d]: unknown key length\n", debugstr,
134 lcp_close(pcb,
"MPPE required but peer negotiation failed");
137 if (options & MPPE_OPT_STATEFUL)
141 mppe_rekey(state, 1);
146 char mkey[
sizeof(state->master_key) * 2 + 1];
147 char skey[
sizeof(state->session_key) * 2 + 1];
149 PPPDEBUG(LOG_DEBUG, (
"%s[%d]: initialized with %d-bit %s mode\n",
150 debugstr, pcb->netif->num, (state->keylen == 16) ? 128 : 40,
151 (state->stateful) ?
"stateful" :
"stateless"));
153 for (i = 0; i < (int)
sizeof(state->master_key); i++)
154 sprintf(mkey + i * 2,
"%02x", state->master_key[i]);
155 for (i = 0; i < (int)
sizeof(state->session_key); i++)
156 sprintf(skey + i * 2,
"%02x", state->session_key[i]);
158 (
"%s[%d]: keys: master: %s initial session: %s\n",
159 debugstr, pcb->netif->num, mkey, skey));
169 state->ccount = MPPE_CCOUNT_SPACE - 1;
175 state->bits = MPPE_BIT_ENCRYPTED;
187 void mppe_comp_reset(ppp_pcb *pcb, ppp_mppe_state *state)
190 state->bits |= MPPE_BIT_FLUSHED;
199 mppe_compress(ppp_pcb *pcb, ppp_mppe_state *state,
struct pbuf **pb,
u16_t protocol)
229 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
230 PPPDEBUG(LOG_DEBUG, (
"mppe_compress[%d]: ccount %d\n", pcb->netif->num, state->ccount));
232 pl[0] = state->ccount>>8;
233 pl[1] = state->ccount;
235 if (!state->stateful ||
236 ((state->ccount & 0xff) == 0xff) ||
237 (state->bits & MPPE_BIT_FLUSHED)) {
239 if (state->stateful) {
240 PPPDEBUG(LOG_DEBUG, (
"mppe_compress[%d]: rekeying\n", pcb->netif->num));
242 mppe_rekey(state, 0);
243 state->bits |= MPPE_BIT_FLUSHED;
245 pl[0] |= state->bits;
246 state->bits &= ~MPPE_BIT_FLUSHED;
251 pl[0] = protocol >> 8;
258 for (n = np; n !=
NULL; n = n->
next) {
274 void mppe_decomp_reset(ppp_pcb *pcb, ppp_mppe_state *state)
285 mppe_decompress(ppp_pcb *pcb, ppp_mppe_state *state,
struct pbuf **pb)
287 struct pbuf *n0 = *pb, *n;
293 if (n0->
len < MPPE_OVHD) {
295 (
"mppe_decompress[%d]: short pkt (%d)\n",
296 pcb->netif->num, n0->
len));
297 state->sanity_errors += 100;
302 flushed = MPPE_BITS(pl) & MPPE_BIT_FLUSHED;
303 ccount = MPPE_CCOUNT(pl);
304 PPPDEBUG(LOG_DEBUG, (
"mppe_decompress[%d]: ccount %d\n",
305 pcb->netif->num, ccount));
308 if (!(MPPE_BITS(pl) & MPPE_BIT_ENCRYPTED)) {
310 (
"mppe_decompress[%d]: ENCRYPTED bit not set!\n",
312 state->sanity_errors += 100;
315 if (!state->stateful && !flushed) {
316 PPPDEBUG(LOG_DEBUG, (
"mppe_decompress[%d]: FLUSHED bit not set in " 317 "stateless mode!\n", pcb->netif->num));
318 state->sanity_errors += 100;
321 if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
322 PPPDEBUG(LOG_DEBUG, (
"mppe_decompress[%d]: FLUSHED bit not set on " 323 "flag packet!\n", pcb->netif->num));
324 state->sanity_errors += 100;
332 if (!state->stateful) {
334 if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE > MPPE_CCOUNT_SPACE / 2) {
335 state->sanity_errors++;
340 while (state->ccount != ccount) {
341 mppe_rekey(state, 0);
342 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
346 if (!state->discard) {
348 state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
349 if (ccount != state->ccount) {
356 ccp_resetrequest(pcb);
366 while ((ccount & ~0xff) !=
367 (state->ccount & ~0xff)) {
368 mppe_rekey(state, 0);
371 256) % MPPE_CCOUNT_SPACE;
376 state->ccount = ccount;
387 mppe_rekey(state, 0);
394 for (n = n0; n !=
NULL; n = n->next) {
395 arc4_crypt(&state->arc4, (
u8_t*)n->payload, n->len);
396 if (n->tot_len == n->len) {
402 state->sanity_errors >>= 1;
407 if (state->sanity_errors >= SANITY_MAX) {
413 lcp_close(pcb,
"Too many MPPE errors");
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define MEMCPY(dst, src, len)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u8_t pbuf_free(struct pbuf *p)
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
#define LWIP_UNUSED_ARG(x)