Only store frame number and not a pointer to frame_data structure in seq_analysis_item_t
[metze/wireshark/wip.git] / wsutil / rc4.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    a partial implementation of RC4 designed for use in the
5    SMB authentication protocol
6
7    Copyright (C) Andrew Tridgell 1998
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "rc4.h"
29
30 /* Perform RC4 on a block of data using specified key.  "data" is a pointer
31    to the block to be processed.  Output is written to same memory as input,
32    so caller may need to make a copy before calling this function, since
33    the input will be overwritten.
34
35    Taken from Samba source code.  Modified to allow us to maintain state
36    between calls to crypt_rc4.
37 */
38
39 void crypt_rc4_init(rc4_state_struct *rc4_state,
40                     const unsigned char *key, int key_len)
41 {
42   int ind;
43   unsigned char j = 0;
44   unsigned char *s_box;
45
46   memset(rc4_state, 0, sizeof(rc4_state_struct));
47   s_box = rc4_state->s_box;
48
49   for (ind = 0; ind < 256; ind++)
50   {
51     s_box[ind] = (unsigned char)ind;
52   }
53
54   for( ind = 0; ind < 256; ind++)
55   {
56      unsigned char tc;
57
58      j += (s_box[ind] + key[ind%key_len]);
59
60      tc = s_box[ind];
61      s_box[ind] = s_box[j];
62      s_box[j] = tc;
63   }
64
65 }
66
67 void crypt_rc4(rc4_state_struct *rc4_state, unsigned char *data, int data_len)
68 {
69   unsigned char *s_box;
70   unsigned char index_i;
71   unsigned char index_j;
72   int ind;
73
74   /* retrieve current state from the state struct (so we can resume where
75      we left off) */
76   index_i = rc4_state->index_i;
77   index_j = rc4_state->index_j;
78   s_box = rc4_state->s_box;
79
80   for( ind = 0; ind < data_len; ind++)
81   {
82     unsigned char tc;
83     unsigned char t;
84
85     index_i++;
86     index_j += s_box[index_i];
87
88     tc = s_box[index_i];
89     s_box[index_i] = s_box[index_j];
90     s_box[index_j] = tc;
91
92     t = s_box[index_i] + s_box[index_j];
93     data[ind] = data[ind] ^ s_box[t];
94   }
95
96   /* Store the updated state */
97   rc4_state->index_i = index_i;
98   rc4_state->index_j = index_j;
99 }
100
101 /*
102  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
103  *
104  * Local Variables:
105  * c-basic-offset: 2
106  * tab-width: 8
107  * indent-tabs-mode: nil
108  * End:
109  *
110  * ex: set shiftwidth=2 tabstop=8 expandtab:
111  * :indentSize=2:tabSize=8:noTabs=true:
112  */