blob: 5fb94242b9f055e6ed20d794de0d525fea9c00a6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +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
Paul Bakker40e46942009-01-03 21:51:57 +000028#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34/*
35 * ARC4 key schedule
36 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000037void arc4_setup( arc4_context *ctx, const unsigned char *key, int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000038{
39 int i, j, k, a;
40 unsigned char *m;
41
42 ctx->x = 0;
43 ctx->y = 0;
44 m = ctx->m;
45
46 for( i = 0; i < 256; i++ )
47 m[i] = (unsigned char) i;
48
49 j = k = 0;
50
51 for( i = 0; i < 256; i++, k++ )
52 {
53 if( k >= keylen ) k = 0;
54
55 a = m[i];
56 j = ( j + a + key[k] ) & 0xFF;
57 m[i] = m[j];
58 m[j] = (unsigned char) a;
59 }
60}
61
62/*
63 * ARC4 cipher function
64 */
Paul Bakkerbaad6502010-03-21 15:42:15 +000065int arc4_crypt( arc4_context *ctx, int length, const unsigned char *input,
66 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 int i, x, y, a, b;
69 unsigned char *m;
70
71 x = ctx->x;
72 y = ctx->y;
73 m = ctx->m;
74
Paul Bakkerbaad6502010-03-21 15:42:15 +000075 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +000076 {
77 x = ( x + 1 ) & 0xFF; a = m[x];
78 y = ( y + a ) & 0xFF; b = m[y];
79
80 m[x] = (unsigned char) b;
81 m[y] = (unsigned char) a;
82
Paul Bakkerbaad6502010-03-21 15:42:15 +000083 output[i] = (unsigned char)
84 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +000085 }
86
87 ctx->x = x;
88 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000089
90 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
Paul Bakker40e46942009-01-03 21:51:57 +000093#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95#include <string.h>
96#include <stdio.h>
97
98/*
99 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
100 *
101 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
102 */
103static const unsigned char arc4_test_key[3][8] =
104{
105 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
106 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
107 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
108};
109
110static const unsigned char arc4_test_pt[3][8] =
111{
112 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
113 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
114 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
115};
116
117static const unsigned char arc4_test_ct[3][8] =
118{
119 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
120 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
121 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
122};
123
124/*
125 * Checkup routine
126 */
127int arc4_self_test( int verbose )
128{
129 int i;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000130 unsigned char ibuf[8];
131 unsigned char obuf[8];
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 arc4_context ctx;
133
134 for( i = 0; i < 3; i++ )
135 {
136 if( verbose != 0 )
137 printf( " ARC4 test #%d: ", i + 1 );
138
Paul Bakkerbaad6502010-03-21 15:42:15 +0000139 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
Paul Bakkerbaad6502010-03-21 15:42:15 +0000142 arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Paul Bakkerbaad6502010-03-21 15:42:15 +0000144 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 {
146 if( verbose != 0 )
147 printf( "failed\n" );
148
149 return( 1 );
150 }
151
152 if( verbose != 0 )
153 printf( "passed\n" );
154 }
155
156 if( verbose != 0 )
157 printf( "\n" );
158
159 return( 0 );
160}
161
162#endif
163
164#endif