Fix an uninitialized variable found by the IBM Checker
[Samba.git] / source / lib / arc4.c
blobaf2564b6c0f84d4e42084a1bc3b9bbda86a47046
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 /*****************************************************************
25 Initialize state for an arc4 crypt/decrpyt.
26 arc4 state is 258 bytes - last 2 bytes are the index bytes.
27 *****************************************************************/
29 void smb_arc4_init(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
31 size_t ind;
32 unsigned char j = 0;
34 for (ind = 0; ind < 256; ind++) {
35 arc4_state_out[ind] = (unsigned char)ind;
38 for( ind = 0; ind < 256; ind++) {
39 unsigned char tc;
41 j += (arc4_state_out[ind] + key[ind%keylen]);
43 tc = arc4_state_out[ind];
44 arc4_state_out[ind] = arc4_state_out[j];
45 arc4_state_out[j] = tc;
47 arc4_state_out[256] = 0;
48 arc4_state_out[257] = 0;
51 /*****************************************************************
52 Do the arc4 crypt/decrpyt.
53 arc4 state is 258 bytes - last 2 bytes are the index bytes.
54 *****************************************************************/
56 void smb_arc4_crypt(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
58 unsigned char index_i = arc4_state_inout[256];
59 unsigned char index_j = arc4_state_inout[257];
60 size_t ind;
62 for( ind = 0; ind < len; ind++) {
63 unsigned char tc;
64 unsigned char t;
66 index_i++;
67 index_j += arc4_state_inout[index_i];
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;
73 t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
74 data[ind] = data[ind] ^ arc4_state_inout[t];
77 arc4_state_inout[256] = index_i;
78 arc4_state_inout[257] = index_j;