00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #include "xyssl/config.h"
00027 
00028 #if defined(XYSSL_ARC4_C)
00029 
00030 #include "xyssl/arc4.h"
00031 
00032 
00033 
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 
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 
00095 
00096 
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 
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