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
00027
00028
00029
00030
00031 #include "polarssl/config.h"
00032
00033 #if defined(POLARSSL_ARC4_C)
00034
00035 #include "polarssl/arc4.h"
00036
00037 #if !defined(POLARSSL_ARC4_ALT)
00038
00039
00040
00041
00042 void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen )
00043 {
00044 int i, j, a;
00045 unsigned int k;
00046 unsigned char *m;
00047
00048 ctx->x = 0;
00049 ctx->y = 0;
00050 m = ctx->m;
00051
00052 for( i = 0; i < 256; i++ )
00053 m[i] = (unsigned char) i;
00054
00055 j = k = 0;
00056
00057 for( i = 0; i < 256; i++, k++ )
00058 {
00059 if( k >= keylen ) k = 0;
00060
00061 a = m[i];
00062 j = ( j + a + key[k] ) & 0xFF;
00063 m[i] = m[j];
00064 m[j] = (unsigned char) a;
00065 }
00066 }
00067
00068
00069
00070
00071 int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
00072 unsigned char *output )
00073 {
00074 int x, y, a, b;
00075 size_t i;
00076 unsigned char *m;
00077
00078 x = ctx->x;
00079 y = ctx->y;
00080 m = ctx->m;
00081
00082 for( i = 0; i < length; i++ )
00083 {
00084 x = ( x + 1 ) & 0xFF; a = m[x];
00085 y = ( y + a ) & 0xFF; b = m[y];
00086
00087 m[x] = (unsigned char) b;
00088 m[y] = (unsigned char) a;
00089
00090 output[i] = (unsigned char)
00091 ( input[i] ^ m[(unsigned char)( a + b )] );
00092 }
00093
00094 ctx->x = x;
00095 ctx->y = y;
00096
00097 return( 0 );
00098 }
00099
00100 #endif
00101
00102 #if defined(POLARSSL_SELF_TEST)
00103
00104 #include <string.h>
00105 #include <stdio.h>
00106
00107
00108
00109
00110
00111
00112 static const unsigned char arc4_test_key[3][8] =
00113 {
00114 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00115 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00116 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00117 };
00118
00119 static const unsigned char arc4_test_pt[3][8] =
00120 {
00121 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00122 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
00123 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00124 };
00125
00126 static const unsigned char arc4_test_ct[3][8] =
00127 {
00128 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
00129 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
00130 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
00131 };
00132
00133
00134
00135
00136 int arc4_self_test( int verbose )
00137 {
00138 int i;
00139 unsigned char ibuf[8];
00140 unsigned char obuf[8];
00141 arc4_context ctx;
00142
00143 for( i = 0; i < 3; i++ )
00144 {
00145 if( verbose != 0 )
00146 printf( " ARC4 test #%d: ", i + 1 );
00147
00148 memcpy( ibuf, arc4_test_pt[i], 8 );
00149
00150 arc4_setup( &ctx, arc4_test_key[i], 8 );
00151 arc4_crypt( &ctx, 8, ibuf, obuf );
00152
00153 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
00154 {
00155 if( verbose != 0 )
00156 printf( "failed\n" );
00157
00158 return( 1 );
00159 }
00160
00161 if( verbose != 0 )
00162 printf( "passed\n" );
00163 }
00164
00165 if( verbose != 0 )
00166 printf( "\n" );
00167
00168 return( 0 );
00169 }
00170
00171 #endif
00172
00173 #endif