1*10465441SEvalZero /*
2*10465441SEvalZero * An implementation of the ARCFOUR algorithm
3*10465441SEvalZero *
4*10465441SEvalZero * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5*10465441SEvalZero *
6*10465441SEvalZero * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
7*10465441SEvalZero *
8*10465441SEvalZero * All rights reserved.
9*10465441SEvalZero *
10*10465441SEvalZero * Redistribution and use in source and binary forms, with or without
11*10465441SEvalZero * modification, are permitted provided that the following conditions
12*10465441SEvalZero * are met:
13*10465441SEvalZero *
14*10465441SEvalZero * * Redistributions of source code must retain the above copyright
15*10465441SEvalZero * notice, this list of conditions and the following disclaimer.
16*10465441SEvalZero * * Redistributions in binary form must reproduce the above copyright
17*10465441SEvalZero * notice, this list of conditions and the following disclaimer in the
18*10465441SEvalZero * documentation and/or other materials provided with the distribution.
19*10465441SEvalZero * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20*10465441SEvalZero * may be used to endorse or promote products derived from this software
21*10465441SEvalZero * without specific prior written permission.
22*10465441SEvalZero *
23*10465441SEvalZero * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*10465441SEvalZero * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*10465441SEvalZero * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26*10465441SEvalZero * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*10465441SEvalZero * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*10465441SEvalZero * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*10465441SEvalZero * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*10465441SEvalZero * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*10465441SEvalZero * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*10465441SEvalZero * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*10465441SEvalZero * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*10465441SEvalZero */
35*10465441SEvalZero /*
36*10465441SEvalZero * The ARCFOUR algorithm was publicly disclosed on 94/09.
37*10465441SEvalZero *
38*10465441SEvalZero * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
39*10465441SEvalZero */
40*10465441SEvalZero
41*10465441SEvalZero #include "netif/ppp/ppp_opts.h"
42*10465441SEvalZero #if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_ARC4
43*10465441SEvalZero
44*10465441SEvalZero #include "netif/ppp/polarssl/arc4.h"
45*10465441SEvalZero /*
46*10465441SEvalZero * ARC4 key schedule
47*10465441SEvalZero */
arc4_setup(arc4_context * ctx,unsigned char * key,int keylen)48*10465441SEvalZero void arc4_setup( arc4_context *ctx, unsigned char *key, int keylen )
49*10465441SEvalZero {
50*10465441SEvalZero int i, j, k, a;
51*10465441SEvalZero unsigned char *m;
52*10465441SEvalZero
53*10465441SEvalZero ctx->x = 0;
54*10465441SEvalZero ctx->y = 0;
55*10465441SEvalZero m = ctx->m;
56*10465441SEvalZero
57*10465441SEvalZero for( i = 0; i < 256; i++ )
58*10465441SEvalZero m[i] = (unsigned char) i;
59*10465441SEvalZero
60*10465441SEvalZero j = k = 0;
61*10465441SEvalZero
62*10465441SEvalZero for( i = 0; i < 256; i++, k++ )
63*10465441SEvalZero {
64*10465441SEvalZero if( k >= keylen ) k = 0;
65*10465441SEvalZero
66*10465441SEvalZero a = m[i];
67*10465441SEvalZero j = ( j + a + key[k] ) & 0xFF;
68*10465441SEvalZero m[i] = m[j];
69*10465441SEvalZero m[j] = (unsigned char) a;
70*10465441SEvalZero }
71*10465441SEvalZero }
72*10465441SEvalZero
73*10465441SEvalZero /*
74*10465441SEvalZero * ARC4 cipher function
75*10465441SEvalZero */
arc4_crypt(arc4_context * ctx,unsigned char * buf,int buflen)76*10465441SEvalZero void arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
77*10465441SEvalZero {
78*10465441SEvalZero int i, x, y, a, b;
79*10465441SEvalZero unsigned char *m;
80*10465441SEvalZero
81*10465441SEvalZero x = ctx->x;
82*10465441SEvalZero y = ctx->y;
83*10465441SEvalZero m = ctx->m;
84*10465441SEvalZero
85*10465441SEvalZero for( i = 0; i < buflen; i++ )
86*10465441SEvalZero {
87*10465441SEvalZero x = ( x + 1 ) & 0xFF; a = m[x];
88*10465441SEvalZero y = ( y + a ) & 0xFF; b = m[y];
89*10465441SEvalZero
90*10465441SEvalZero m[x] = (unsigned char) b;
91*10465441SEvalZero m[y] = (unsigned char) a;
92*10465441SEvalZero
93*10465441SEvalZero buf[i] = (unsigned char)
94*10465441SEvalZero ( buf[i] ^ m[(unsigned char)( a + b )] );
95*10465441SEvalZero }
96*10465441SEvalZero
97*10465441SEvalZero ctx->x = x;
98*10465441SEvalZero ctx->y = y;
99*10465441SEvalZero }
100*10465441SEvalZero
101*10465441SEvalZero #endif /* PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES */
102