3 * Release $Name: MATRIXSSL_1_8_3_OPEN $
5 * ARC4 stream cipher implementation
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 /******************************************************************************/
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
;
51 state
= &ctx
->state
[0];
53 for (counter
= 0; counter
< 256; counter
++) {
54 state
[counter
] = (unsigned char)counter
;
61 for (counter
= 0; counter
< 256; counter
++) {
62 index2
= (key
[index1
] + state
[counter
] + index2
) & 0xff;
65 state
[counter
] = state
[index2
];
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
81 ctx
->byteCount
+= len
;
82 if (ctx
->byteCount
> ARC4_MAX_BYTES
) {
88 state
= &ctx
->state
[0];
89 for (counter
= 0; counter
< len
; counter
++) {
91 y
= (state
[x
] + y
) & 0xff;
97 xorIndex
= (state
[x
] + state
[y
]) & 0xff;
100 tmp
^= state
[xorIndex
];
108 /*****************************************************************************/