blob: dcc13d539afe67fea0e3fe4f8cdc7070ed055205 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The ARCFOUR algorithm was publicly disclosed on 94/09.
21 *
22 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/arc4.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <string.h>
33
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_ARC4_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020041}
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020044{
45 if( ctx == NULL )
46 return;
47
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020049}
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
52 * ARC4 key schedule
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020055 unsigned int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
Paul Bakker23986e52011-04-24 08:57:21 +000057 int i, j, a;
58 unsigned int k;
Paul Bakker5121ce52009-01-03 21:22:43 +000059 unsigned char *m;
60
61 ctx->x = 0;
62 ctx->y = 0;
63 m = ctx->m;
64
65 for( i = 0; i < 256; i++ )
66 m[i] = (unsigned char) i;
67
68 j = k = 0;
69
70 for( i = 0; i < 256; i++, k++ )
71 {
72 if( k >= keylen ) k = 0;
73
74 a = m[i];
75 j = ( j + a + key[k] ) & 0xFF;
76 m[i] = m[j];
77 m[j] = (unsigned char) a;
78 }
79}
80
81/*
82 * ARC4 cipher function
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
Paul Bakkerbaad6502010-03-21 15:42:15 +000085 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
Paul Bakker23986e52011-04-24 08:57:21 +000087 int x, y, a, b;
88 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000089 unsigned char *m;
90
91 x = ctx->x;
92 y = ctx->y;
93 m = ctx->m;
94
Paul Bakkerbaad6502010-03-21 15:42:15 +000095 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +000096 {
97 x = ( x + 1 ) & 0xFF; a = m[x];
98 y = ( y + a ) & 0xFF; b = m[y];
99
100 m[x] = (unsigned char) b;
101 m[y] = (unsigned char) a;
102
Paul Bakkerbaad6502010-03-21 15:42:15 +0000103 output[i] = (unsigned char)
104 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
107 ctx->x = x;
108 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109
110 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* !MBEDTLS_ARC4_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000116/*
117 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
118 *
119 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
120 */
121static const unsigned char arc4_test_key[3][8] =
122{
123 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
124 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
125 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
126};
127
128static const unsigned char arc4_test_pt[3][8] =
129{
130 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
131 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
132 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
133};
134
135static const unsigned char arc4_test_ct[3][8] =
136{
137 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
138 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
139 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
140};
141
142/*
143 * Checkup routine
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_arc4_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200147 int i, ret = 0;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000148 unsigned char ibuf[8];
149 unsigned char obuf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_arc4_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 for( i = 0; i < 3; i++ )
155 {
156 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " ARC4 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Paul Bakkerbaad6502010-03-21 15:42:15 +0000159 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
162 mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakkerbaad6502010-03-21 15:42:15 +0000164 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 {
166 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200169 ret = 1;
170 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 }
172
173 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 }
176
177 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200180exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200182
183 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_ARC4_C */