3 * Release $Name: MATRIXSSL_1_8_8_OPEN $
5 * ARC4 stream cipher implementation
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"
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
;
52 ctx
->arc4
.byteCount
= 0;
53 state
= &ctx
->arc4
.state
[0];
55 for (counter
= 0; counter
< 256; counter
++) {
56 state
[counter
] = (unsigned char)counter
;
63 for (counter
= 0; counter
< 256; counter
++) {
64 index2
= (key
[index1
] + state
[counter
] + index2
) & 0xff;
67 state
[counter
] = state
[index2
];
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
;
80 ctx
->arc4
.byteCount
+= len
;
81 if (ctx
->arc4
.byteCount
> ARC4_MAX_BYTES
) {
87 state
= &ctx
->arc4
.state
[0];
88 for (counter
= 0; counter
< len
; counter
++) {
90 y
= (state
[x
] + y
) & 0xff;
96 xorIndex
= (state
[x
] + state
[y
]) & 0xff;
99 tmp
^= state
[xorIndex
];
107 #endif /* USE_ARC4 */
109 /******************************************************************************/