2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* An implementation of the ARCFOUR algorithm
|
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ARCFOUR algorithm was publicly disclosed on 94/09.
|
|
|
|
*
|
|
|
|
* http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
|
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/arc4.h"
|
2018-04-17 14:51:09 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 21:50:26 +00:00
|
|
|
#else
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdio.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2014-02-01 21:50:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_ARC4_ALT)
|
2013-06-24 17:20:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
|
2014-06-18 09:12:03 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
|
2014-06-18 09:12:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
|
2014-06-18 09:12:03 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
|
2014-06-18 09:12:03 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* ARC4 key schedule
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
|
2014-05-01 12:18:25 +00:00
|
|
|
unsigned int keylen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int i, j, a;
|
|
|
|
unsigned int k;
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
ctx->x = 0;
|
|
|
|
ctx->y = 0;
|
|
|
|
m = ctx->m;
|
|
|
|
|
|
|
|
for( i = 0; i < 256; i++ )
|
|
|
|
m[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
j = k = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < 256; i++, k++ )
|
|
|
|
{
|
|
|
|
if( k >= keylen ) k = 0;
|
|
|
|
|
|
|
|
a = m[i];
|
|
|
|
j = ( j + a + key[k] ) & 0xFF;
|
|
|
|
m[i] = m[j];
|
|
|
|
m[j] = (unsigned char) a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ARC4 cipher function
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
|
2010-03-21 15:42:15 +00:00
|
|
|
unsigned char *output )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int x, y, a, b;
|
|
|
|
size_t i;
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
x = ctx->x;
|
|
|
|
y = ctx->y;
|
|
|
|
m = ctx->m;
|
|
|
|
|
2010-03-21 15:42:15 +00:00
|
|
|
for( i = 0; i < length; i++ )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
x = ( x + 1 ) & 0xFF; a = m[x];
|
|
|
|
y = ( y + a ) & 0xFF; b = m[y];
|
|
|
|
|
|
|
|
m[x] = (unsigned char) b;
|
|
|
|
m[y] = (unsigned char) a;
|
|
|
|
|
2010-03-21 15:42:15 +00:00
|
|
|
output[i] = (unsigned char)
|
|
|
|
( input[i] ^ m[(unsigned char)( a + b )] );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->x = x;
|
|
|
|
ctx->y = y;
|
2010-03-18 21:21:02 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* !MBEDTLS_ARC4_ALT */
|
2013-06-24 17:20:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
|
|
|
|
*
|
|
|
|
* http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
|
|
|
|
*/
|
|
|
|
static const unsigned char arc4_test_key[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char arc4_test_pt[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char arc4_test_ct[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
|
|
|
|
{ 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
|
|
|
|
{ 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_arc4_self_test( int verbose )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2014-06-18 09:12:03 +00:00
|
|
|
int i, ret = 0;
|
2010-03-21 15:42:15 +00:00
|
|
|
unsigned char ibuf[8];
|
|
|
|
unsigned char obuf[8];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_arc4_context ctx;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_arc4_init( &ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ARC4 test #%d: ", i + 1 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2010-03-21 15:42:15 +00:00
|
|
|
memcpy( ibuf, arc4_test_pt[i], 8 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
|
|
|
|
mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2010-03-21 15:42:15 +00:00
|
|
|
if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-06-18 09:12:03 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "passed\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-06-18 09:12:03 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_arc4_free( &ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_ARC4_C */
|