Merge branch 'master' of ssh://git.samba.org/data/git/samba into selftest
[kai/samba.git] / source3 / lib / arc4.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    An implementation of arc4.
5
6    Copyright (C) Jeremy Allison 2005.
7    
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 /*****************************************************************
25  Initialize state for an arc4 crypt/decrpyt.
26  arc4 state is 258 bytes - last 2 bytes are the index bytes.
27 *****************************************************************/
28
29 void smb_arc4_init(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
30 {
31         size_t ind;
32         unsigned char j = 0;
33
34         for (ind = 0; ind < 256; ind++) {
35                 arc4_state_out[ind] = (unsigned char)ind;
36         }
37
38         for( ind = 0; ind < 256; ind++) {
39                 unsigned char tc;
40
41                 j += (arc4_state_out[ind] + key[ind%keylen]);
42
43                 tc = arc4_state_out[ind];
44                 arc4_state_out[ind] = arc4_state_out[j];
45                 arc4_state_out[j] = tc;
46         }
47         arc4_state_out[256] = 0;
48         arc4_state_out[257] = 0;
49 }
50
51 /*****************************************************************
52  Do the arc4 crypt/decrpyt.
53  arc4 state is 258 bytes - last 2 bytes are the index bytes.
54 *****************************************************************/
55
56 void smb_arc4_crypt(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
57 {
58         unsigned char index_i = arc4_state_inout[256];
59         unsigned char index_j = arc4_state_inout[257];
60         size_t ind;
61
62         for( ind = 0; ind < len; ind++) {
63                 unsigned char tc;
64                 unsigned char t;
65
66                 index_i++;
67                 index_j += arc4_state_inout[index_i];
68
69                 tc = arc4_state_inout[index_i];
70                 arc4_state_inout[index_i] = arc4_state_inout[index_j];
71                 arc4_state_inout[index_j] = tc;
72
73                 t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
74                 data[ind] = data[ind] ^ arc4_state_inout[t];
75         }
76
77         arc4_state_inout[256] = index_i;
78         arc4_state_inout[257] = index_j;
79 }