blob: 052aa328f0c249dd2ce64d6244aac6f5ac701b2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The ARCFOUR algorithm was publicly disclosed on 94/09.
50 *
51 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
52 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <string.h>
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_SELF_TEST)
67#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010069#else
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define mbedtls_printf printf
72#endif /* MBEDTLS_PLATFORM_C */
73#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_ARC4_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020076
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010079 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080}
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088{
89 if( ctx == NULL )
90 return;
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020093}
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095/*
96 * ARC4 key schedule
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020099 unsigned int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100{
Paul Bakker23986e52011-04-24 08:57:21 +0000101 int i, j, a;
102 unsigned int k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 unsigned char *m;
104
105 ctx->x = 0;
106 ctx->y = 0;
107 m = ctx->m;
108
109 for( i = 0; i < 256; i++ )
110 m[i] = (unsigned char) i;
111
112 j = k = 0;
113
114 for( i = 0; i < 256; i++, k++ )
115 {
116 if( k >= keylen ) k = 0;
117
118 a = m[i];
119 j = ( j + a + key[k] ) & 0xFF;
120 m[i] = m[j];
121 m[j] = (unsigned char) a;
122 }
123}
124
125/*
126 * ARC4 cipher function
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
Paul Bakkerbaad6502010-03-21 15:42:15 +0000129 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130{
Paul Bakker23986e52011-04-24 08:57:21 +0000131 int x, y, a, b;
132 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 unsigned char *m;
134
135 x = ctx->x;
136 y = ctx->y;
137 m = ctx->m;
138
Paul Bakkerbaad6502010-03-21 15:42:15 +0000139 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 {
141 x = ( x + 1 ) & 0xFF; a = m[x];
142 y = ( y + a ) & 0xFF; b = m[y];
143
144 m[x] = (unsigned char) b;
145 m[y] = (unsigned char) a;
146
Paul Bakkerbaad6502010-03-21 15:42:15 +0000147 output[i] = (unsigned char)
148 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 }
150
151 ctx->x = x;
152 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000153
154 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155}
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* !MBEDTLS_ARC4_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000160/*
161 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
162 *
163 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
164 */
165static const unsigned char arc4_test_key[3][8] =
166{
167 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
168 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
169 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
170};
171
172static const unsigned char arc4_test_pt[3][8] =
173{
174 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
175 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
176 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
177};
178
179static const unsigned char arc4_test_ct[3][8] =
180{
181 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
182 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
183 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
184};
185
186/*
187 * Checkup routine
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_arc4_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200191 int i, ret = 0;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000192 unsigned char ibuf[8];
193 unsigned char obuf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_arc4_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 for( i = 0; i < 3; i++ )
199 {
200 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " ARC4 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Paul Bakkerbaad6502010-03-21 15:42:15 +0000203 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
206 mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Paul Bakkerbaad6502010-03-21 15:42:15 +0000208 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 {
210 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200213 ret = 1;
214 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 }
216
217 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 }
220
221 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200226
227 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_ARC4_C */