blob: 93ea4dc7c8797dd571bdd755f6b393154783934e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * An implementation of the ARCFOUR algorithm
3 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker5121ce52009-01-03 21:22:43 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21/*
22 * The ARCFOUR algorithm was publicly disclosed on 94/09.
23 *
24 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
25 */
26
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
33/*
34 * ARC4 key schedule
35 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000036void arc4_setup( arc4_context *ctx, const unsigned char *key, int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000037{
38 int i, j, k, a;
39 unsigned char *m;
40
41 ctx->x = 0;
42 ctx->y = 0;
43 m = ctx->m;
44
45 for( i = 0; i < 256; i++ )
46 m[i] = (unsigned char) i;
47
48 j = k = 0;
49
50 for( i = 0; i < 256; i++, k++ )
51 {
52 if( k >= keylen ) k = 0;
53
54 a = m[i];
55 j = ( j + a + key[k] ) & 0xFF;
56 m[i] = m[j];
57 m[j] = (unsigned char) a;
58 }
59}
60
61/*
62 * ARC4 cipher function
63 */
Paul Bakkerbaad6502010-03-21 15:42:15 +000064int arc4_crypt( arc4_context *ctx, int length, const unsigned char *input,
65 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
67 int i, x, y, a, b;
68 unsigned char *m;
69
70 x = ctx->x;
71 y = ctx->y;
72 m = ctx->m;
73
Paul Bakkerbaad6502010-03-21 15:42:15 +000074 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +000075 {
76 x = ( x + 1 ) & 0xFF; a = m[x];
77 y = ( y + a ) & 0xFF; b = m[y];
78
79 m[x] = (unsigned char) b;
80 m[y] = (unsigned char) a;
81
Paul Bakkerbaad6502010-03-21 15:42:15 +000082 output[i] = (unsigned char)
83 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 }
85
86 ctx->x = x;
87 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000088
89 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
91
Paul Bakker40e46942009-01-03 21:51:57 +000092#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94#include <string.h>
95#include <stdio.h>
96
97/*
98 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
99 *
100 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
101 */
102static const unsigned char arc4_test_key[3][8] =
103{
104 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
105 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
106 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
107};
108
109static const unsigned char arc4_test_pt[3][8] =
110{
111 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
112 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
113 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
114};
115
116static const unsigned char arc4_test_ct[3][8] =
117{
118 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
119 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
120 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
121};
122
123/*
124 * Checkup routine
125 */
126int arc4_self_test( int verbose )
127{
128 int i;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000129 unsigned char ibuf[8];
130 unsigned char obuf[8];
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 arc4_context ctx;
132
133 for( i = 0; i < 3; i++ )
134 {
135 if( verbose != 0 )
136 printf( " ARC4 test #%d: ", i + 1 );
137
Paul Bakkerbaad6502010-03-21 15:42:15 +0000138 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
Paul Bakkerbaad6502010-03-21 15:42:15 +0000141 arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Paul Bakkerbaad6502010-03-21 15:42:15 +0000143 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 {
145 if( verbose != 0 )
146 printf( "failed\n" );
147
148 return( 1 );
149 }
150
151 if( verbose != 0 )
152 printf( "passed\n" );
153 }
154
155 if( verbose != 0 )
156 printf( "\n" );
157
158 return( 0 );
159}
160
161#endif
162
163#endif