blob: ef0e7f89a5eaf9b62267f4879207dea6b1f21854 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ARCFOUR algorithm was publicly disclosed on 94/09.
24 *
25 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
26 */
27
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
31#include POLARSSL_CONFIG_FILE
32#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#if defined(POLARSSL_PLATFORM_C)
39#include "polarssl/platform.h"
40#else
41#define polarssl_printf printf
42#endif
43
Paul Bakker90995b52013-06-24 19:20:35 +020044#if !defined(POLARSSL_ARC4_ALT)
45
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020046/* Implementation that should never be optimized out by the compiler */
47static void polarssl_zeroize( void *v, size_t n ) {
48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
51void arc4_init( arc4_context *ctx )
52{
53 memset( ctx, 0, sizeof( arc4_context ) );
54}
55
56void arc4_free( arc4_context *ctx )
57{
58 if( ctx == NULL )
59 return;
60
61 polarssl_zeroize( ctx, sizeof( arc4_context ) );
62}
63
Paul Bakker5121ce52009-01-03 21:22:43 +000064/*
65 * ARC4 key schedule
66 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020067void arc4_setup( arc4_context *ctx, const unsigned char *key,
68 unsigned int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Paul Bakker23986e52011-04-24 08:57:21 +000070 int i, j, a;
71 unsigned int k;
Paul Bakker5121ce52009-01-03 21:22:43 +000072 unsigned char *m;
73
74 ctx->x = 0;
75 ctx->y = 0;
76 m = ctx->m;
77
78 for( i = 0; i < 256; i++ )
79 m[i] = (unsigned char) i;
80
81 j = k = 0;
82
83 for( i = 0; i < 256; i++, k++ )
84 {
85 if( k >= keylen ) k = 0;
86
87 a = m[i];
88 j = ( j + a + key[k] ) & 0xFF;
89 m[i] = m[j];
90 m[j] = (unsigned char) a;
91 }
92}
93
94/*
95 * ARC4 cipher function
96 */
Paul Bakker23986e52011-04-24 08:57:21 +000097int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
Paul Bakkerbaad6502010-03-21 15:42:15 +000098 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Paul Bakker23986e52011-04-24 08:57:21 +0000100 int x, y, a, b;
101 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 unsigned char *m;
103
104 x = ctx->x;
105 y = ctx->y;
106 m = ctx->m;
107
Paul Bakkerbaad6502010-03-21 15:42:15 +0000108 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 {
110 x = ( x + 1 ) & 0xFF; a = m[x];
111 y = ( y + a ) & 0xFF; b = m[y];
112
113 m[x] = (unsigned char) b;
114 m[y] = (unsigned char) a;
115
Paul Bakkerbaad6502010-03-21 15:42:15 +0000116 output[i] = (unsigned char)
117 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 }
119
120 ctx->x = x;
121 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000122
123 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
125
Paul Bakker90995b52013-06-24 19:20:35 +0200126#endif /* !POLARSSL_ARC4_ALT */
127
Paul Bakker40e46942009-01-03 21:51:57 +0000128#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130#include <string.h>
131#include <stdio.h>
132
133/*
134 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
135 *
136 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
137 */
138static const unsigned char arc4_test_key[3][8] =
139{
140 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
141 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
142 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
143};
144
145static const unsigned char arc4_test_pt[3][8] =
146{
147 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
148 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
149 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
150};
151
152static const unsigned char arc4_test_ct[3][8] =
153{
154 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
155 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
156 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
157};
158
159/*
160 * Checkup routine
161 */
162int arc4_self_test( int verbose )
163{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200164 int i, ret = 0;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000165 unsigned char ibuf[8];
166 unsigned char obuf[8];
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 arc4_context ctx;
168
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200169 arc4_init( &ctx );
170
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 for( i = 0; i < 3; i++ )
172 {
173 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100174 polarssl_printf( " ARC4 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Paul Bakkerbaad6502010-03-21 15:42:15 +0000176 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Paul Bakker3c2122f2013-06-24 19:03:14 +0200178 arc4_setup( &ctx, arc4_test_key[i], 8 );
Paul Bakkerbaad6502010-03-21 15:42:15 +0000179 arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Paul Bakkerbaad6502010-03-21 15:42:15 +0000181 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 {
183 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100184 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200186 ret = 1;
187 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 }
189
190 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100191 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 }
193
194 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100195 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200197exit:
198 arc4_free( &ctx );
199
200 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201}
202
Paul Bakker9af723c2014-05-01 13:03:14 +0200203#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker9af723c2014-05-01 13:03:14 +0200205#endif /* POLARSSL_ARC4_C */