- Changed ARC4 to use seperate input/output buffer

This commit is contained in:
Paul Bakker 2010-03-21 15:42:15 +00:00
parent f3ccc68100
commit baad6504d4
6 changed files with 30 additions and 21 deletions

View file

@ -63,7 +63,8 @@ void arc4_setup( arc4_context *ctx, const unsigned char *key, int keylen )
/*
* ARC4 cipher function
*/
int arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
int arc4_crypt( arc4_context *ctx, int length, const unsigned char *input,
unsigned char *output )
{
int i, x, y, a, b;
unsigned char *m;
@ -72,7 +73,7 @@ int arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
y = ctx->y;
m = ctx->m;
for( i = 0; i < buflen; i++ )
for( i = 0; i < length; i++ )
{
x = ( x + 1 ) & 0xFF; a = m[x];
y = ( y + a ) & 0xFF; b = m[y];
@ -80,8 +81,8 @@ int arc4_crypt( arc4_context *ctx, unsigned char *buf, int buflen )
m[x] = (unsigned char) b;
m[y] = (unsigned char) a;
buf[i] = (unsigned char)
( buf[i] ^ m[(unsigned char)( a + b )] );
output[i] = (unsigned char)
( input[i] ^ m[(unsigned char)( a + b )] );
}
ctx->x = x;
@ -127,7 +128,8 @@ static const unsigned char arc4_test_ct[3][8] =
int arc4_self_test( int verbose )
{
int i;
unsigned char buf[8];
unsigned char ibuf[8];
unsigned char obuf[8];
arc4_context ctx;
for( i = 0; i < 3; i++ )
@ -135,12 +137,12 @@ int arc4_self_test( int verbose )
if( verbose != 0 )
printf( " ARC4 test #%d: ", i + 1 );
memcpy( buf, arc4_test_pt[i], 8 );
memcpy( ibuf, arc4_test_pt[i], 8 );
arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
arc4_crypt( &ctx, buf, 8 );
arc4_crypt( &ctx, 8, ibuf, obuf );
if( memcmp( buf, arc4_test_ct[i], 8 ) != 0 )
if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
{
if( verbose != 0 )
printf( "failed\n" );

View file

@ -531,7 +531,8 @@ static int ssl_encrypt_buf( ssl_context *ssl )
ssl->out_msg, ssl->out_msglen );
arc4_crypt( (arc4_context *) ssl->ctx_enc,
ssl->out_msg, ssl->out_msglen );
ssl->out_msglen, ssl->out_msg,
ssl->out_msg );
#else
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
#endif
@ -618,7 +619,8 @@ static int ssl_decrypt_buf( ssl_context *ssl )
#if defined(POLARSSL_ARC4_C)
padlen = 0;
arc4_crypt( (arc4_context *) ssl->ctx_dec,
ssl->in_msg, ssl->in_msglen );
ssl->in_msglen, ssl->in_msg,
ssl->in_msg );
#else
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
#endif