blob: 75b1862b89f8f1d44d4c8798f0179dcb64712ce6 [file] [log] [blame]
Dave Chapman6e797152007-09-08 23:27:49 +00001/*
2 * arc4.c
3 * Release $Name: MATRIXSSL_1_8_3_OPEN $
4 *
5 * ARC4 stream cipher implementation
6 */
7/*
8 * Copyright (c) PeerSec Networks, 2002-2007. All Rights Reserved.
9 * The latest version of this code is available at http://www.matrixssl.org
10 *
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.
15 *
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
20 *
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.
24 *
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
29 */
30/******************************************************************************/
31
32#include "arc4.h"
33
34/*
35 Some accounts, such as O'Reilly's Secure Programming Cookbook say that no
36 more than 2^30 bytes should be processed without rekeying, so we
37 enforce that limit here. FYI, this is equal to 1GB of data transferred.
38*/
39#define ARC4_MAX_BYTES 0x40000000
40
41/******************************************************************************/
42/*
43 SSL_RSA_WITH_RC4_* cipher callbacks
44 */
45void matrixArc4Init(struct rc4_key_t *ctx, unsigned char *key, int32_t keylen)
46{
47 unsigned char index1, index2, tmp, *state;
48 int16_t counter;
49
50 ctx->byteCount = 0;
51 state = &ctx->state[0];
52
53 for (counter = 0; counter < 256; counter++) {
54 state[counter] = (unsigned char)counter;
55 }
56 ctx->x = 0;
57 ctx->y = 0;
58 index1 = 0;
59 index2 = 0;
60
61 for (counter = 0; counter < 256; counter++) {
62 index2 = (key[index1] + state[counter] + index2) & 0xff;
63
64 tmp = state[counter];
65 state[counter] = state[index2];
66 state[index2] = tmp;
67
68 index1 = (index1 + 1) % keylen;
69 }
70}
71
72int32_t matrixArc4(struct rc4_key_t *ctx, unsigned char *in,
73 unsigned char *out, int32_t len)
74{
75 unsigned char x, y, *state, xorIndex, tmp;
76 int counter; /* NOTE BY DAVE CHAPMAN: This was a short in
77 the original code, which caused a segfault
78 when attempting to process data > 32767
79 bytes. */
80
81 ctx->byteCount += len;
82 if (ctx->byteCount > ARC4_MAX_BYTES) {
83 return -1;
84 }
85
86 x = ctx->x;
87 y = ctx->y;
88 state = &ctx->state[0];
89 for (counter = 0; counter < len; counter++) {
90 x = (x + 1) & 0xff;
91 y = (state[x] + y) & 0xff;
92
93 tmp = state[x];
94 state[x] = state[y];
95 state[y] = tmp;
96
97 xorIndex = (state[x] + state[y]) & 0xff;
98
99 tmp = in[counter];
100 tmp ^= state[xorIndex];
101 out[counter] = tmp;
102 }
103 ctx->x = x;
104 ctx->y = y;
105 return len;
106}
107
108/*****************************************************************************/