2 * An implementation of the ARCFOUR algorithm
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The ARCFOUR algorithm was publicly disclosed on 94/09.
24 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "mbedtls/config.h"
30 #include MBEDTLS_CONFIG_FILE
33 #if defined(MBEDTLS_ARC4_C)
35 #include "mbedtls/arc4.h"
36 #include "mbedtls/platform_util.h"
40 #if defined(MBEDTLS_SELF_TEST)
41 #if defined(MBEDTLS_PLATFORM_C)
42 #include "mbedtls/platform.h"
45 #define mbedtls_printf printf
46 #endif /* MBEDTLS_PLATFORM_C */
47 #endif /* MBEDTLS_SELF_TEST */
49 #if !defined(MBEDTLS_ARC4_ALT)
51 void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
53 memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
56 void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
61 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
67 void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
78 for( i = 0; i < 256; i++ )
79 m[i] = (unsigned char) i;
83 for( i = 0; i < 256; i++, k++ )
85 if( k >= keylen ) k = 0;
88 j = ( j + a + key[k] ) & 0xFF;
90 m[j] = (unsigned char) a;
95 * ARC4 cipher function
97 int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
98 unsigned char *output )
108 for( i = 0; i < length; i++ )
110 x = ( x + 1 ) & 0xFF; a = m[x];
111 y = ( y + a ) & 0xFF; b = m[y];
113 m[x] = (unsigned char) b;
114 m[y] = (unsigned char) a;
116 output[i] = (unsigned char)
117 ( input[i] ^ m[(unsigned char)( a + b )] );
126 #endif /* !MBEDTLS_ARC4_ALT */
128 #if defined(MBEDTLS_SELF_TEST)
130 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
132 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
134 static const unsigned char arc4_test_key[3][8] =
136 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
137 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
138 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
141 static const unsigned char arc4_test_pt[3][8] =
143 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
144 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
145 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
148 static const unsigned char arc4_test_ct[3][8] =
150 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
151 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
152 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
158 int mbedtls_arc4_self_test( int verbose )
161 unsigned char ibuf[8];
162 unsigned char obuf[8];
163 mbedtls_arc4_context ctx;
165 mbedtls_arc4_init( &ctx );
167 for( i = 0; i < 3; i++ )
170 mbedtls_printf( " ARC4 test #%d: ", i + 1 );
172 memcpy( ibuf, arc4_test_pt[i], 8 );
174 mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
175 mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
177 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
180 mbedtls_printf( "failed\n" );
187 mbedtls_printf( "passed\n" );
191 mbedtls_printf( "\n" );
194 mbedtls_arc4_free( &ctx );
199 #endif /* MBEDTLS_SELF_TEST */
201 #endif /* MBEDTLS_ARC4_C */