source: branches/samba-3.0/source/lib/arc4.c@ 392

Last change on this file since 392 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 2.4 KB
Line 
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25/*****************************************************************
26 Initialize state for an arc4 crypt/decrpyt.
27 arc4 state is 258 bytes - last 2 bytes are the index bytes.
28*****************************************************************/
29
30void smb_arc4_init(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
31{
32 size_t ind;
33 unsigned char j = 0;
34
35 for (ind = 0; ind < 256; ind++) {
36 arc4_state_out[ind] = (unsigned char)ind;
37 }
38
39 for( ind = 0; ind < 256; ind++) {
40 unsigned char tc;
41
42 j += (arc4_state_out[ind] + key[ind%keylen]);
43
44 tc = arc4_state_out[ind];
45 arc4_state_out[ind] = arc4_state_out[j];
46 arc4_state_out[j] = tc;
47 }
48 arc4_state_out[256] = 0;
49 arc4_state_out[257] = 0;
50}
51
52/*****************************************************************
53 Do the arc4 crypt/decrpyt.
54 arc4 state is 258 bytes - last 2 bytes are the index bytes.
55*****************************************************************/
56
57void smb_arc4_crypt(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
58{
59 unsigned char index_i = arc4_state_inout[256];
60 unsigned char index_j = arc4_state_inout[257];
61 size_t ind;
62
63 for( ind = 0; ind < len; ind++) {
64 unsigned char tc;
65 unsigned char t;
66
67 index_i++;
68 index_j += arc4_state_inout[index_i];
69
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;
73
74 t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
75 data[ind] = data[ind] ^ arc4_state_inout[t];
76 }
77
78 arc4_state_inout[256] = index_i;
79 arc4_state_inout[257] = index_j;
80}
Note: See TracBrowser for help on using the repository browser.