/home/dko/projects/mobilec/trunk/src/security/xyssl-0.9/library/arc4.c

Go to the documentation of this file.
00001 /*
00002  * An implementation of the ARCFOUR algorithm
00003  *
00004  * Copyright (C) 2006-2007 Christophe Devine
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 /*
00021  * The ARCFOUR algorithm was publicly disclosed on 94/09.
00022  *
00023  * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
00024  */
00025 
00026 #include "xyssl/config.h"
00027 
00028 #if defined(XYSSL_ARC4_C)
00029 
00030 #include "xyssl/arc4.h"
00031 
00032 /*
00033  * ARC4 key schedule
00034  */
00035 void arc4_setup( arc4_context *ctx, unsigned char *key, int keylen )
00036 {
00037 int i, j, k, a;
00038 unsigned char *m;
00039 
00040 ctx->x = 0;
00041 ctx->y = 0;
00042 m = ctx->m;
00043 
00044 for( i = 0; i < 256; i++ )
00045 m[i] = (unsigned char) i;
00046 
00047 j = k = 0;
00048 
00049 for( i = 0; i < 256; i++, k++ )
00050 {
00051 if( k >= keylen ) k = 0;
00052 
00053 a = m[i];
00054 j = ( j + a + key[k] ) & 0xFF;
00055 m[i] = m[j];
00056 m[j] = (unsigned char) a;
00057 }
00058 }
00059 
00060 /*
00061  * ARC4 cipher function
00062  */
00063 void arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
00064 {
00065 int i, x, y, a, b;
00066 unsigned char *m;
00067 
00068 x = ctx->x;
00069 y = ctx->y;
00070 m = ctx->m;
00071 
00072 for( i = 0; i < buflen; i++ )
00073 {
00074 x = ( x + 1 ) & 0xFF; a = m[x];
00075 y = ( y + a ) & 0xFF; b = m[y];
00076 
00077 m[x] = (unsigned char) b;
00078 m[y] = (unsigned char) a;
00079 
00080 buf[i] = (unsigned char)
00081 ( buf[i] ^ m[(unsigned char)( a + b )] );
00082 }
00083 
00084 ctx->x = x;
00085 ctx->y = y;
00086 }
00087 
00088 #if defined(XYSSL_SELF_TEST)
00089 
00090 #include <string.h>
00091 #include <stdio.h>
00092 
00093 /*
00094  * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
00095  *
00096  * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
00097  */
00098 static const unsigned char arc4_test_key[3][8] =
00099 {
00100 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00101 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00102 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00103 };
00104 
00105 static const unsigned char arc4_test_pt[3][8] =
00106 {
00107 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00108 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
00109 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00110 };
00111 
00112 static const unsigned char arc4_test_ct[3][8] =
00113 {
00114 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
00115 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
00116 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
00117 };
00118 
00119 /*
00120  * Checkup routine
00121  */
00122 int arc4_self_test( int verbose )
00123 {
00124 int i;
00125 unsigned char buf[8];
00126 arc4_context ctx;
00127 
00128 for( i = 0; i < 3; i++ )
00129 {
00130 if( verbose != 0 )
00131 printf( " ARC4 test #%d: ", i + 1 );
00132 
00133 memcpy( buf, arc4_test_pt[i], 8 );
00134 
00135 arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
00136 arc4_crypt( &ctx, buf, 8 );
00137 
00138 if( memcmp( buf, arc4_test_ct[i], 8 ) != 0 )
00139 {
00140 if( verbose != 0 )
00141 printf( "failed\n" );
00142 
00143 return( 1 );
00144 }
00145 
00146 if( verbose != 0 )
00147 printf( "passed\n" );
00148 }
00149 
00150 if( verbose != 0 )
00151 printf( "\n" );
00152 
00153 return( 0 );
00154 }
00155 
00156 #endif
00157 
00158 #endif

Generated on Tue Oct 28 17:03:22 2008 for Mobile-C by doxygen 1.5.5

AltStyle によって変換されたページ (->オリジナル) /