Correct a little typo.
[kugel-rb.git] / rbutil / ipodpatcher / arc4.c
blob75b1862b89f8f1d44d4c8798f0179dcb64712ce6
1 /*
2 * arc4.c
3 * Release $Name: MATRIXSSL_1_8_3_OPEN $
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
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 "arc4.h"
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.
39 #define ARC4_MAX_BYTES 0x40000000
41 /******************************************************************************/
43 SSL_RSA_WITH_RC4_* cipher callbacks
45 void matrixArc4Init(struct rc4_key_t *ctx, unsigned char *key, int32_t keylen)
47 unsigned char index1, index2, tmp, *state;
48 int16_t counter;
50 ctx->byteCount = 0;
51 state = &ctx->state[0];
53 for (counter = 0; counter < 256; counter++) {
54 state[counter] = (unsigned char)counter;
56 ctx->x = 0;
57 ctx->y = 0;
58 index1 = 0;
59 index2 = 0;
61 for (counter = 0; counter < 256; counter++) {
62 index2 = (key[index1] + state[counter] + index2) & 0xff;
64 tmp = state[counter];
65 state[counter] = state[index2];
66 state[index2] = tmp;
68 index1 = (index1 + 1) % keylen;
72 int32_t matrixArc4(struct rc4_key_t *ctx, unsigned char *in,
73 unsigned char *out, int32_t len)
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. */
81 ctx->byteCount += len;
82 if (ctx->byteCount > ARC4_MAX_BYTES) {
83 return -1;
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;
93 tmp = state[x];
94 state[x] = state[y];
95 state[y] = tmp;
97 xorIndex = (state[x] + state[y]) & 0xff;
99 tmp = in[counter];
100 tmp ^= state[xorIndex];
101 out[counter] = tmp;
103 ctx->x = x;
104 ctx->y = y;
105 return len;
108 /*****************************************************************************/