Tomato 1.26
[tomato.git] / release / src / router / matrixssl / src / crypto / peersec / arc4.c
blob4e39553d53d38864575b29709c7fbdd46df3ba34
1 /*
2 * arc4.c
3 * Release $Name: MATRIXSSL_1_8_8_OPEN $
5 * ARC4 stream cipher implementation
6 */
7 /*
8 * Copyright (c) PeerSec Networks, 2002-2009. All Rights Reserved.
9 * The latest version of this code is available at http://www.matrixssl.org
11 * This software is open source; 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 General Public License does NOT permit incorporating this software
17 * into proprietary programs. If you are unable to comply with the GPL, a
18 * commercial license for this software may be purchased from PeerSec Networks
19 * at http://www.peersec.com
21 * This program is distributed in WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * http://www.gnu.org/copyleft/gpl.html
30 /******************************************************************************/
32 #include "../cryptoLayer.h"
34 #ifdef USE_ARC4
37 Some accounts, such as O'Reilly's Secure Programming Cookbook say that no
38 more than 2^30 bytes should be processed without rekeying, so we
39 enforce that limit here. FYI, this is equal to 1GB of data transferred.
41 #define ARC4_MAX_BYTES 0x40000000
43 /******************************************************************************/
45 SSL_RSA_WITH_RC4_* cipher callbacks
47 void matrixArc4Init(sslCipherContext_t *ctx, unsigned char *key, int32 keylen)
49 unsigned char index1, index2, tmp, *state;
50 short counter;
52 ctx->arc4.byteCount = 0;
53 state = &ctx->arc4.state[0];
55 for (counter = 0; counter < 256; counter++) {
56 state[counter] = (unsigned char)counter;
58 ctx->arc4.x = 0;
59 ctx->arc4.y = 0;
60 index1 = 0;
61 index2 = 0;
63 for (counter = 0; counter < 256; counter++) {
64 index2 = (key[index1] + state[counter] + index2) & 0xff;
66 tmp = state[counter];
67 state[counter] = state[index2];
68 state[index2] = tmp;
70 index1 = (index1 + 1) % keylen;
74 int32 matrixArc4(sslCipherContext_t *ctx, unsigned char *in,
75 unsigned char *out, int32 len)
77 unsigned char x, y, *state, xorIndex, tmp;
78 short counter;
80 ctx->arc4.byteCount += len;
81 if (ctx->arc4.byteCount > ARC4_MAX_BYTES) {
82 return -1;
85 x = ctx->arc4.x;
86 y = ctx->arc4.y;
87 state = &ctx->arc4.state[0];
88 for (counter = 0; counter < len; counter++) {
89 x = (x + 1) & 0xff;
90 y = (state[x] + y) & 0xff;
92 tmp = state[x];
93 state[x] = state[y];
94 state[y] = tmp;
96 xorIndex = (state[x] + state[y]) & 0xff;
98 tmp = in[counter];
99 tmp ^= state[xorIndex];
100 out[counter] = tmp;
102 ctx->arc4.x = x;
103 ctx->arc4.y = y;
104 return len;
107 #endif /* USE_ARC4 */
109 /******************************************************************************/