xref: /third_party/lwip/src/netif/ppp/polarssl/arc4.c (revision 195972f6)
1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci *  An implementation of the ARCFOUR algorithm
3195972f6Sopenharmony_ci *
4195972f6Sopenharmony_ci *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
5195972f6Sopenharmony_ci *
6195972f6Sopenharmony_ci *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
7195972f6Sopenharmony_ci *
8195972f6Sopenharmony_ci *  All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci *  Redistribution and use in source and binary forms, with or without
11195972f6Sopenharmony_ci *  modification, are permitted provided that the following conditions
12195972f6Sopenharmony_ci *  are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci *    * Redistributions of source code must retain the above copyright
15195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci *    * Redistributions in binary form must reproduce the above copyright
17195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer in the
18195972f6Sopenharmony_ci *      documentation and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci *    * Neither the names of PolarSSL or XySSL nor the names of its contributors
20195972f6Sopenharmony_ci *      may be used to endorse or promote products derived from this software
21195972f6Sopenharmony_ci *      without specific prior written permission.
22195972f6Sopenharmony_ci *
23195972f6Sopenharmony_ci *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24195972f6Sopenharmony_ci *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25195972f6Sopenharmony_ci *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26195972f6Sopenharmony_ci *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27195972f6Sopenharmony_ci *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28195972f6Sopenharmony_ci *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29195972f6Sopenharmony_ci *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30195972f6Sopenharmony_ci *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31195972f6Sopenharmony_ci *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32195972f6Sopenharmony_ci *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33195972f6Sopenharmony_ci *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34195972f6Sopenharmony_ci */
35195972f6Sopenharmony_ci/*
36195972f6Sopenharmony_ci *  The ARCFOUR algorithm was publicly disclosed on 94/09.
37195972f6Sopenharmony_ci *
38195972f6Sopenharmony_ci *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
39195972f6Sopenharmony_ci */
40195972f6Sopenharmony_ci
41195972f6Sopenharmony_ci#include "netif/ppp/ppp_opts.h"
42195972f6Sopenharmony_ci#if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_ARC4
43195972f6Sopenharmony_ci
44195972f6Sopenharmony_ci#include "netif/ppp/polarssl/arc4.h"
45195972f6Sopenharmony_ci/*
46195972f6Sopenharmony_ci * ARC4 key schedule
47195972f6Sopenharmony_ci */
48195972f6Sopenharmony_civoid arc4_setup( arc4_context *ctx, unsigned char *key, int keylen )
49195972f6Sopenharmony_ci{
50195972f6Sopenharmony_ci    int i, j, k, a;
51195972f6Sopenharmony_ci    unsigned char *m;
52195972f6Sopenharmony_ci
53195972f6Sopenharmony_ci    ctx->x = 0;
54195972f6Sopenharmony_ci    ctx->y = 0;
55195972f6Sopenharmony_ci    m = ctx->m;
56195972f6Sopenharmony_ci
57195972f6Sopenharmony_ci    for( i = 0; i < 256; i++ )
58195972f6Sopenharmony_ci        m[i] = (unsigned char) i;
59195972f6Sopenharmony_ci
60195972f6Sopenharmony_ci    j = k = 0;
61195972f6Sopenharmony_ci
62195972f6Sopenharmony_ci    for( i = 0; i < 256; i++, k++ )
63195972f6Sopenharmony_ci    {
64195972f6Sopenharmony_ci        if( k >= keylen ) k = 0;
65195972f6Sopenharmony_ci
66195972f6Sopenharmony_ci        a = m[i];
67195972f6Sopenharmony_ci        j = ( j + a + key[k] ) & 0xFF;
68195972f6Sopenharmony_ci        m[i] = m[j];
69195972f6Sopenharmony_ci        m[j] = (unsigned char) a;
70195972f6Sopenharmony_ci    }
71195972f6Sopenharmony_ci}
72195972f6Sopenharmony_ci
73195972f6Sopenharmony_ci/*
74195972f6Sopenharmony_ci * ARC4 cipher function
75195972f6Sopenharmony_ci */
76195972f6Sopenharmony_civoid arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
77195972f6Sopenharmony_ci{
78195972f6Sopenharmony_ci    int i, x, y, a, b;
79195972f6Sopenharmony_ci    unsigned char *m;
80195972f6Sopenharmony_ci
81195972f6Sopenharmony_ci    x = ctx->x;
82195972f6Sopenharmony_ci    y = ctx->y;
83195972f6Sopenharmony_ci    m = ctx->m;
84195972f6Sopenharmony_ci
85195972f6Sopenharmony_ci    for( i = 0; i < buflen; i++ )
86195972f6Sopenharmony_ci    {
87195972f6Sopenharmony_ci        x = ( x + 1 ) & 0xFF; a = m[x];
88195972f6Sopenharmony_ci        y = ( y + a ) & 0xFF; b = m[y];
89195972f6Sopenharmony_ci
90195972f6Sopenharmony_ci        m[x] = (unsigned char) b;
91195972f6Sopenharmony_ci        m[y] = (unsigned char) a;
92195972f6Sopenharmony_ci
93195972f6Sopenharmony_ci        buf[i] = (unsigned char)
94195972f6Sopenharmony_ci            ( buf[i] ^ m[(unsigned char)( a + b )] );
95195972f6Sopenharmony_ci    }
96195972f6Sopenharmony_ci
97195972f6Sopenharmony_ci    ctx->x = x;
98195972f6Sopenharmony_ci    ctx->y = y;
99195972f6Sopenharmony_ci}
100195972f6Sopenharmony_ci
101195972f6Sopenharmony_ci#endif /* PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES */
102