new polarssl
[syren.git] / src / libpolarssl / arc4.c
blob087854f42631d8b37918a2f4a49e29b7072240f7
1 /*
2 * An implementation of the ARCFOUR algorithm
4 * Copyright (C) 2006-2014, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
9 * All rights reserved.
11 * 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.
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.
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.
26 * The ARCFOUR algorithm was publicly disclosed on 94/09.
28 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
31 #if !defined(POLARSSL_CONFIG_FILE)
32 #include "config.h"
33 #else
34 #include POLARSSL_CONFIG_FILE
35 #endif
37 #if defined(POLARSSL_ARC4_C)
39 #include "arc4.h"
41 #if defined(POLARSSL_PLATFORM_C)
42 #include "platform.h"
43 #else
44 #define polarssl_printf printf
45 #endif
47 #if !defined(POLARSSL_ARC4_ALT)
50 * ARC4 key schedule
52 void arc4_setup( arc4_context *ctx, const unsigned char *key,
53 unsigned int keylen )
55 int i, j, a;
56 unsigned int k;
57 unsigned char *m;
59 ctx->x = 0;
60 ctx->y = 0;
61 m = ctx->m;
63 for( i = 0; i < 256; i++ )
64 m[i] = (unsigned char) i;
66 j = k = 0;
68 for( i = 0; i < 256; i++, k++ )
70 if( k >= keylen ) k = 0;
72 a = m[i];
73 j = ( j + a + key[k] ) & 0xFF;
74 m[i] = m[j];
75 m[j] = (unsigned char) a;
80 * ARC4 cipher function
82 int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
83 unsigned char *output )
85 int x, y, a, b;
86 size_t i;
87 unsigned char *m;
89 x = ctx->x;
90 y = ctx->y;
91 m = ctx->m;
93 for( i = 0; i < length; i++ )
95 x = ( x + 1 ) & 0xFF; a = m[x];
96 y = ( y + a ) & 0xFF; b = m[y];
98 m[x] = (unsigned char) b;
99 m[y] = (unsigned char) a;
101 output[i] = (unsigned char)
102 ( input[i] ^ m[(unsigned char)( a + b )] );
105 ctx->x = x;
106 ctx->y = y;
108 return( 0 );
111 #endif /* !POLARSSL_ARC4_ALT */
113 #if defined(POLARSSL_SELF_TEST)
115 #include <string.h>
116 #include <stdio.h>
119 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
121 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
123 static const unsigned char arc4_test_key[3][8] =
125 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
126 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
127 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
130 static const unsigned char arc4_test_pt[3][8] =
132 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
133 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
134 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
137 static const unsigned char arc4_test_ct[3][8] =
139 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
140 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
141 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
145 * Checkup routine
147 int arc4_self_test( int verbose )
149 int i;
150 unsigned char ibuf[8];
151 unsigned char obuf[8];
152 arc4_context ctx;
154 for( i = 0; i < 3; i++ )
156 if( verbose != 0 )
157 polarssl_printf( " ARC4 test #%d: ", i + 1 );
159 memcpy( ibuf, arc4_test_pt[i], 8 );
161 arc4_setup( &ctx, arc4_test_key[i], 8 );
162 arc4_crypt( &ctx, 8, ibuf, obuf );
164 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
166 if( verbose != 0 )
167 polarssl_printf( "failed\n" );
169 return( 1 );
172 if( verbose != 0 )
173 polarssl_printf( "passed\n" );
176 if( verbose != 0 )
177 polarssl_printf( "\n" );
179 return( 0 );
182 #endif /* POLARSSL_SELF_TEST */
184 #endif /* POLARSSL_ARC4_C */