r17727: Start pulling in changes for 3.0.23c
[Samba/gbeck.git] / source / lib / arc4.c
blob03ca54c324be02b55823b77e6231c12d41ebae84
1 /*
2 Unix SMB/CIFS implementation.
4 An implementation of arc4.
6 Copyright (C) Jeremy Allison 2005.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 /*****************************************************************
26 Initialize state for an arc4 crypt/decrpyt.
27 arc4 state is 258 bytes - last 2 bytes are the index bytes.
28 *****************************************************************/
30 void smb_arc4_init(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
32 size_t ind;
33 unsigned char j = 0;
35 for (ind = 0; ind < 256; ind++) {
36 arc4_state_out[ind] = (unsigned char)ind;
39 for( ind = 0; ind < 256; ind++) {
40 unsigned char tc;
42 j += (arc4_state_out[ind] + key[ind%keylen]);
44 tc = arc4_state_out[ind];
45 arc4_state_out[ind] = arc4_state_out[j];
46 arc4_state_out[j] = tc;
48 arc4_state_out[256] = 0;
49 arc4_state_out[257] = 0;
52 /*****************************************************************
53 Do the arc4 crypt/decrpyt.
54 arc4 state is 258 bytes - last 2 bytes are the index bytes.
55 *****************************************************************/
57 void smb_arc4_crypt(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
59 unsigned char index_i = arc4_state_inout[256];
60 unsigned char index_j = arc4_state_inout[257];
61 size_t ind;
63 for( ind = 0; ind < len; ind++) {
64 unsigned char tc;
65 unsigned char t;
67 index_i++;
68 index_j += arc4_state_inout[index_i];
70 tc = arc4_state_inout[index_i];
71 arc4_state_inout[index_i] = arc4_state_inout[index_j];
72 arc4_state_inout[index_j] = tc;
74 t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
75 data[ind] = data[ind] ^ arc4_state_inout[t];
78 arc4_state_inout[256] = index_i;
79 arc4_state_inout[257] = index_j;