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