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