blob: c7725bec0debd0a39a0437a7a48b770c7f21f770 [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.
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
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/arc4.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37/*
38 * ARC4 key schedule
39 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000040void arc4_setup( arc4_context *ctx, const unsigned char *key, int keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +000041{
42 int i, j, k, a;
43 unsigned char *m;
44
45 ctx->x = 0;
46 ctx->y = 0;
47 m = ctx->m;
48
49 for( i = 0; i < 256; i++ )
50 m[i] = (unsigned char) i;
51
52 j = k = 0;
53
54 for( i = 0; i < 256; i++, k++ )
55 {
56 if( k >= keylen ) k = 0;
57
58 a = m[i];
59 j = ( j + a + key[k] ) & 0xFF;
60 m[i] = m[j];
61 m[j] = (unsigned char) a;
62 }
63}
64
65/*
66 * ARC4 cipher function
67 */
Paul Bakkerbaad6502010-03-21 15:42:15 +000068int arc4_crypt( arc4_context *ctx, int length, const unsigned char *input,
69 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
71 int i, x, y, a, b;
72 unsigned char *m;
73
74 x = ctx->x;
75 y = ctx->y;
76 m = ctx->m;
77
Paul Bakkerbaad6502010-03-21 15:42:15 +000078 for( i = 0; i < length; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +000079 {
80 x = ( x + 1 ) & 0xFF; a = m[x];
81 y = ( y + a ) & 0xFF; b = m[y];
82
83 m[x] = (unsigned char) b;
84 m[y] = (unsigned char) a;
85
Paul Bakkerbaad6502010-03-21 15:42:15 +000086 output[i] = (unsigned char)
87 ( input[i] ^ m[(unsigned char)( a + b )] );
Paul Bakker5121ce52009-01-03 21:22:43 +000088 }
89
90 ctx->x = x;
91 ctx->y = y;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000092
93 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95
Paul Bakker40e46942009-01-03 21:51:57 +000096#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98#include <string.h>
99#include <stdio.h>
100
101/*
102 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
103 *
104 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
105 */
106static const unsigned char arc4_test_key[3][8] =
107{
108 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
109 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
110 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
111};
112
113static const unsigned char arc4_test_pt[3][8] =
114{
115 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
116 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
117 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
118};
119
120static const unsigned char arc4_test_ct[3][8] =
121{
122 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
123 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
124 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
125};
126
127/*
128 * Checkup routine
129 */
130int arc4_self_test( int verbose )
131{
132 int i;
Paul Bakkerbaad6502010-03-21 15:42:15 +0000133 unsigned char ibuf[8];
134 unsigned char obuf[8];
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 arc4_context ctx;
136
137 for( i = 0; i < 3; i++ )
138 {
139 if( verbose != 0 )
140 printf( " ARC4 test #%d: ", i + 1 );
141
Paul Bakkerbaad6502010-03-21 15:42:15 +0000142 memcpy( ibuf, arc4_test_pt[i], 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
Paul Bakkerbaad6502010-03-21 15:42:15 +0000145 arc4_crypt( &ctx, 8, ibuf, obuf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Paul Bakkerbaad6502010-03-21 15:42:15 +0000147 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 {
149 if( verbose != 0 )
150 printf( "failed\n" );
151
152 return( 1 );
153 }
154
155 if( verbose != 0 )
156 printf( "passed\n" );
157 }
158
159 if( verbose != 0 )
160 printf( "\n" );
161
162 return( 0 );
163}
164
165#endif
166
167#endif