ssl/tls: add proper const attributes to functions, context members
[tropicssl.git] / library / arc4.c
blobad46002c30e6cbd5ed5bf26070bb6fd98fad015c
1 /*
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>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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"
48 * ARC4 key schedule
50 void arc4_setup(arc4_context * ctx, const unsigned char *key, int keylen)
52 int i, j, k, a;
53 unsigned char *m;
55 ctx->x = 0;
56 ctx->y = 0;
57 m = ctx->m;
59 for (i = 0; i < 256; i++)
60 m[i] = (unsigned char)i;
62 j = k = 0;
64 for (i = 0; i < 256; i++, k++) {
65 if (k >= keylen)
66 k = 0;
68 a = m[i];
69 j = (j + a + key[k]) & 0xFF;
70 m[i] = m[j];
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)
82 int i, x, y, a, b;
83 unsigned char *m;
85 x = ctx->x;
86 y = ctx->y;
87 m = ctx->m;
89 for (i = 0; i < buflen; i++) {
90 x = (x + 1) & 0xFF;
91 a = m[x];
92 y = (y + a) & 0xFF;
93 b = m[y];
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)]);
102 ctx->x = x;
103 ctx->y = y;
106 #if defined(TROPICSSL_SELF_TEST)
108 #include <string.h>
109 #include <stdio.h>
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}
135 * Checkup routine
137 int arc4_self_test(int verbose)
139 int i;
140 unsigned char inbuf[8];
141 unsigned char outbuf[8];
142 arc4_context ctx;
144 for (i = 0; i < 3; i++) {
145 if (verbose != 0)
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) {
154 if (verbose != 0)
155 printf("failed\n");
157 return (1);
160 if (verbose != 0)
161 printf("passed\n");
164 if (verbose != 0)
165 printf("\n");
167 return (0);
170 #endif
172 #endif