Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_arc4.c Source File

lwip_arc4.c

00001 /*
00002  *  An implementation of the ARCFOUR algorithm
00003  *
00004  *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
00005  *
00006  *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *  
00014  *    * Redistributions of source code must retain the above copyright
00015  *      notice, this list of conditions and the following disclaimer.
00016  *    * Redistributions in binary form must reproduce the above copyright
00017  *      notice, this list of conditions and the following disclaimer in the
00018  *      documentation and/or other materials provided with the distribution.
00019  *    * Neither the names of PolarSSL or XySSL nor the names of its contributors
00020  *      may be used to endorse or promote products derived from this software
00021  *      without specific prior written permission.
00022  *  
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00029  *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 /*
00036  *  The ARCFOUR algorithm was publicly disclosed on 94/09.
00037  *
00038  *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
00039  */
00040 
00041 #include "netif/ppp/ppp_opts.h"
00042 #if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_ARC4
00043 
00044 #include "netif/ppp/polarssl/arc4.h"
00045 /*
00046  * ARC4 key schedule
00047  */
00048 void arc4_setup( arc4_context *ctx, unsigned char *key, int keylen )
00049 {
00050     int i, j, k, a;
00051     unsigned char *m;
00052 
00053     ctx->x  = 0;
00054     ctx->y  = 0;
00055     m = ctx->m ;
00056 
00057     for( i = 0; i < 256; i++ )
00058         m[i] = (unsigned char) i;
00059 
00060     j = k = 0;
00061 
00062     for( i = 0; i < 256; i++, k++ )
00063     {
00064         if( k >= keylen ) k = 0;
00065 
00066         a = m[i];
00067         j = ( j + a + key[k] ) & 0xFF;
00068         m[i] = m[j];
00069         m[j] = (unsigned char) a;
00070     }
00071 }
00072 
00073 /*
00074  * ARC4 cipher function
00075  */
00076 void arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
00077 {
00078     int i, x, y, a, b;
00079     unsigned char *m;
00080 
00081     x = ctx->x ;
00082     y = ctx->y ;
00083     m = ctx->m ;
00084 
00085     for( i = 0; i < buflen; i++ )
00086     {
00087         x = ( x + 1 ) & 0xFF; a = m[x];
00088         y = ( y + a ) & 0xFF; b = m[y];
00089 
00090         m[x] = (unsigned char) b;
00091         m[y] = (unsigned char) a;
00092 
00093         buf[i] = (unsigned char)
00094             ( buf[i] ^ m[(unsigned char)( a + b )] );
00095     }
00096 
00097     ctx->x  = x;
00098     ctx->y  = y;
00099 }
00100 
00101 #endif /* PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES */