Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
rc4.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19//
20// rc4.cpp
21// RC4 encryption / decryption
22//
23#include "rc4.h"
24#include <memory.h>
25#include <stdio.h>
26
27static unsigned char RC4_Temp_Byte;
28#define RC4_SWAP_BYTE(a,b) RC4_Temp_Byte=a; a=b; b=RC4_Temp_Byte
29
30//
31// Used to init the key state vector in key setup
32//
33static unsigned char RC4_Table_Init[]={
34 0, 1, 2, 3, 4,
35 5, 6, 7, 8, 9,
36 10, 11, 12, 13, 14,
37 15, 16, 17, 18, 19,
38 20, 21, 22, 23, 24,
39 25, 26, 27, 28, 29,
40 30, 31, 32, 33, 34,
41 35, 36, 37, 38, 39,
42 40, 41, 42, 43, 44,
43 45, 46, 47, 48, 49,
44 50, 51, 52, 53, 54,
45 55, 56, 57, 58, 59,
46 60, 61, 62, 63, 64,
47 65, 66, 67, 68, 69,
48 70, 71, 72, 73, 74,
49 75, 76, 77, 78, 79,
50 80, 81, 82, 83, 84,
51 85, 86, 87, 88, 89,
52 90, 91, 92, 93, 94,
53 95, 96, 97, 98, 99,
54 100, 101, 102, 103, 104,
55 105, 106, 107, 108, 109,
56 110, 111, 112, 113, 114,
57 115, 116, 117, 118, 119,
58 120, 121, 122, 123, 124,
59 125, 126, 127, 128, 129,
60 130, 131, 132, 133, 134,
61 135, 136, 137, 138, 139,
62 140, 141, 142, 143, 144,
63 145, 146, 147, 148, 149,
64 150, 151, 152, 153, 154,
65 155, 156, 157, 158, 159,
66 160, 161, 162, 163, 164,
67 165, 166, 167, 168, 169,
68 170, 171, 172, 173, 174,
69 175, 176, 177, 178, 179,
70 180, 181, 182, 183, 184,
71 185, 186, 187, 188, 189,
72 190, 191, 192, 193, 194,
73 195, 196, 197, 198, 199,
74 200, 201, 202, 203, 204,
75 205, 206, 207, 208, 209,
76 210, 211, 212, 213, 214,
77 215, 216, 217, 218, 219,
78 220, 221, 222, 223, 224,
79 225, 226, 227, 228, 229,
80 230, 231, 232, 233, 234,
81 235, 236, 237, 238, 239,
82 240, 241, 242, 243, 244,
83 245, 246, 247, 248, 249,
84 250, 251, 252, 253, 254,
85 255
86};
87
88
89
90
91
92//
93// Don't rely on this to zero the key
94//
96{
97 memset(Key.State, 0, 256);
98 Key.X=0;
99 Key.Y=0;
100}
101
102
103
104//
105// Setup the encryption key. This must be called before you encrypt/decrypt!
106//
107void RC4Class::Prepare_Key(const unsigned char *key_data_ptr, int key_data_len)
108{
109 switch(key_data_len) {
110 case 8:
111 Prepare_Key_8bytes(key_data_ptr);
112 break;
113
114 case 16:
115 Prepare_Key_16bytes(key_data_ptr);
116 break;
117
118 default:
119 {
120 unsigned char index1;
121 unsigned char index2;
122 unsigned char *state;
123 unsigned short counter;
124
125 state = &Key.State[0];
126 memcpy(state, RC4_Table_Init, 256);
127
128 Key.X = 0;
129 Key.Y = 0;
130 index1 = 0;
131 index2 = 0;
132 for (counter = 0; counter < 256; counter++) {
133 index2 = (key_data_ptr[index1] + state[counter] + index2);
134 RC4_SWAP_BYTE(state[counter], state[index2]);
135 index1 = (unsigned char)((index1 + 1) % key_data_len);
136 }
137 }
138 } // switch
139}
140
141
143 unsigned char *state;
144 state = &Key.State[0];
145 for (int i=0; i<256; i+=5) {
146 printf(" %3d %3d %3d %3d %3d\n",state[i], state[i+1], state[i+2], state[i+3], state[i+4]);
147 }
148 printf("X = %d Y = %d\n\n",Key.X,Key.Y);
149}
150
151
152
153//
154// RC4 in standard mode.
155//
156// This will XOR the buffer with the RC4 stream (like a one time pad).
157//
158void RC4Class::RC4(unsigned char *buffer_ptr, int buffer_len)
159{
160 unsigned char x;
161 unsigned char y;
162 unsigned char *state;
163 unsigned char *buffer_end=buffer_ptr+buffer_len;
164 unsigned char *buffer_cur=buffer_ptr;
165
166 x = Key.X;
167 y = Key.Y;
168
169 state = &Key.State[0];
170 while(buffer_cur != buffer_end) {
171 x++;
172 y = (unsigned char)(y + state[x]);
173 RC4_SWAP_BYTE(state[x], state[y]);
174 *buffer_cur ^= state[(state[x] + state[y]) & 255];
175 ++buffer_cur;
176 }
177
178 Key.X = x;
179 Key.Y = y;
180}
181
182
183
184
185//
186// Copy state & key
187//
189 if (this == &other)
190 return(*this);
191
192 Key.X=other.Key.X;
193 Key.Y=other.Key.Y;
194 memcpy(Key.State, other.Key.State, 256);
195
196 return(*this);
197}
198
199
200
202
203
204
205//
206// Setup the encryption key. This must be called before you encrypt/decrypt!
207// This version assumes the key len is 8 bytes (64 bits).
208//
209void RC4Class::Prepare_Key_8bytes(const unsigned char *key_data_ptr)
210{
211 unsigned char index1;
212 unsigned char index2;
213 unsigned char *state;
214 unsigned short counter;
215
216 state = &Key.State[0];
217 memcpy(state, RC4_Table_Init, 256);
218
219 Key.X = 0;
220 Key.Y = 0;
221 index1 = 0;
222 index2 = 0;
223 for (counter = 0; counter < 256; counter++) {
224 index2 = (unsigned char)(key_data_ptr[index1] + state[counter] + index2);
225 RC4_SWAP_BYTE(state[counter], state[index2]);
226 ++index1;
227 index1 &= 0x07;
228 }
229}
230
231
232
233//
234// Setup the encryption key. This must be called before you encrypt/decrypt!
235// This version assumes the key len is 16 bytes (128 bits).
236//
237void RC4Class::Prepare_Key_16bytes(const unsigned char *key_data_ptr)
238{
239 unsigned char index1;
240 unsigned char index2;
241 unsigned char *state;
242 unsigned short counter;
243
244 state = &Key.State[0];
245 memcpy(state, RC4_Table_Init, 256);
246
247 Key.X = 0;
248 Key.Y = 0;
249 index1 = 0;
250 index2 = 0;
251 for (counter = 0; counter < 256; counter++) {
252 index2 = (unsigned char)(key_data_ptr[index1] + state[counter] + index2);
253 RC4_SWAP_BYTE(state[counter], state[index2]);
254 ++index1;
255 index1 &= 0x0F;
256 }
257}
RC4Class()
Definition rc4.cpp:95
void Print_State(void)
Definition rc4.cpp:142
void Prepare_Key(const unsigned char *key_data_ptr, int key_data_len)
Definition rc4.cpp:107
RC4Class & operator=(const RC4Class &other)
Definition rc4.cpp:188
void RC4(unsigned char *buffer_ptr, int buffer_len)
Definition rc4.cpp:158
int counter
Definition patch.cpp:410
#define RC4_SWAP_BYTE(a, b)
Definition rc4.cpp:28