blob: d722c56ea8f13978b43d74fc9b272b0ec3907d5d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ARCFOUR algorithm was publicly disclosed on 94/09.
27 *
28 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker7dc4c442014-02-01 22:50:26 +010041#if defined(POLARSSL_PLATFORM_C)
42#include "polarssl/platform.h"
43#else
44#define polarssl_printf printf
45#endif
46
Paul Bakker90995b52013-06-24 19:20:35 +020047#if !defined(POLARSSL_ARC4_ALT)
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049/*
50 * ARC4 key schedule
51 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020052void arc4_setup( arc4_context *ctx, const unsigned char *key,
53 unsigned int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000054{
Paul Bakker23986e52011-04-24 08:57:21 +000055 int i, j, a;
56 unsigned int k;
Paul Bakker5121ce52009-01-03 21:22:43 +000057 unsigned char *m;
58
59 ctx->x = 0;
60 ctx->y = 0;
61 m = ctx->m;
62
63 for( i = 0; i < 256; i++ )
64 m[i] = (unsigned char) i;
65
66 j = k = 0;
67
68 for( i = 0; i < 256; i++, k++ )
69 {
70 if( k >= keylen ) k = 0;
71
72 a = m[i];
73 j = ( j + a + key[k] ) & 0xFF;
74 m[i] = m[j];
75 m[j] = (unsigned char) a;
76 }
77}
78
79/*
80 * ARC4 cipher function
81 */
Paul Bakker23986e52011-04-24 08:57:21 +000082int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
Paul Bakkerbaad6502010-03-21 15:42:15 +000083 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
Paul Bakker23986e52011-04-24 08:57:21 +000085 int x, y, a, b;
86 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000087 unsigned char *m;
88
89 x = ctx->x;
90 y = ctx->y;
91 m = ctx->m;
92
Paul Bakkerbaad6502010-03-21 15:42:15 +000093 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +000094 {
95 x = ( x + 1 ) & 0xFF; a = m[x];
96 y = ( y + a ) & 0xFF; b = m[y];
97
98 m[x] = (unsigned char) b;
99 m[y] = (unsigned char) a;
100
Paul Bakkerbaad6502010-03-21 15:42:15 +0000101 output[i] = (unsigned char)
102 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 }
104
105 ctx->x = x;
106 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000107
108 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109}
110
Paul Bakker90995b52013-06-24 19:20:35 +0200111#endif /* !POLARSSL_ARC4_ALT */
112
Paul Bakker40e46942009-01-03 21:51:57 +0000113#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115#include <string.h>
116#include <stdio.h>
117
118/*
119 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
120 *
121 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
122 */
123static const unsigned char arc4_test_key[3][8] =
124{
125 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
126 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
127 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
128};
129
130static const unsigned char arc4_test_pt[3][8] =
131{
132 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
133 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
134 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
135};
136
137static const unsigned char arc4_test_ct[3][8] =
138{
139 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
140 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
141 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
142};
143
144/*
145 * Checkup routine
146 */
147int arc4_self_test( int verbose )
148{
149 int i;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000150 unsigned char ibuf[8];
151 unsigned char obuf[8];
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 arc4_context ctx;
153
154 for( i = 0; i < 3; i++ )
155 {
156 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100157 polarssl_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
Paul Bakker3c2122f2013-06-24 19:03:14 +0200161 arc4_setup( &ctx, arc4_test_key[i], 8 );
Paul Bakkerbaad6502010-03-21 15:42:15 +0000162 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 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100167 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 return( 1 );
170 }
171
172 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100173 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 }
175
176 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100177 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 return( 0 );
180}
181
Paul Bakker9af723c2014-05-01 13:03:14 +0200182#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker9af723c2014-05-01 13:03:14 +0200184#endif /* POLARSSL_ARC4_C */