2 * An implementation of the ARCFOUR algorithm
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The ARCFOUR algorithm was publicly disclosed on 94/09.
38 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
41 #include "tropicssl/config.h"
43 #if defined(TROPICSSL_ARC4_C)
45 #include "tropicssl/arc4.h"
50 void arc4_setup(arc4_context
* ctx
, const unsigned char *key
, int keylen
)
59 for (i
= 0; i
< 256; i
++)
60 m
[i
] = (unsigned char)i
;
64 for (i
= 0; i
< 256; i
++, k
++) {
69 j
= (j
+ a
+ key
[k
]) & 0xFF;
71 m
[j
] = (unsigned char)a
;
76 * ARC4 cipher function
78 void arc4_crypt(arc4_context
* ctx
, int buflen
,
79 const unsigned char *input
,
80 unsigned char *output
)
89 for (i
= 0; i
< buflen
; i
++) {
95 m
[x
] = (unsigned char)b
;
96 m
[y
] = (unsigned char)a
;
98 output
[i
] = (unsigned char)
99 (input
[i
] ^ m
[(unsigned char)(a
+ b
)]);
106 #if defined(TROPICSSL_SELF_TEST)
112 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
114 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
116 static const unsigned char arc4_test_key
[3][8] = {
117 {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
118 {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
119 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
122 static const unsigned char arc4_test_pt
[3][8] = {
123 {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
124 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
125 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
128 static const unsigned char arc4_test_ct
[3][8] = {
129 {0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96},
130 {0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79},
131 {0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A}
137 int arc4_self_test(int verbose
)
140 unsigned char inbuf
[8];
141 unsigned char outbuf
[8];
144 for (i
= 0; i
< 3; i
++) {
146 printf(" ARC4 test #%d: ", i
+ 1);
148 memcpy(inbuf
, arc4_test_pt
[i
], 8);
150 arc4_setup(&ctx
, arc4_test_key
[i
], 8);
151 arc4_crypt(&ctx
, 8, inbuf
, outbuf
);
153 if (memcmp(outbuf
, arc4_test_ct
[i
], 8) != 0) {