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