eolib 0.6.1
A core C library for writing applications related to Endless Online
Loading...
Searching...
No Matches
encrypt.c
Go to the documentation of this file.
1#include "eolib/data.h"
2#include "eolib/encrypt.h"
3#include "eolib/rng.h"
4
6{
7 /* See encrypt.h for formula reference. */
8 challenge++;
9 return 110905 + ((challenge % 9) + 1) * ((11092004 - challenge) % (((challenge % 11) + 1) * 119)) * 119 + (challenge % 2004);
10}
11
12void eo_swap_multiples(uint8_t *data, size_t length, uint8_t multiple)
13{
14 size_t sequence_length = 0;
15 size_t i;
16
17 for (i = 0; i < length; i++)
18 {
19 if (data[i] % multiple == 0)
20 {
21 sequence_length++;
22 }
23 else
24 {
25 if (sequence_length > 1)
26 {
27 size_t start = i - sequence_length;
28 size_t end = start + sequence_length - 1;
29 size_t j;
30
31 for (j = 0; j < sequence_length / 2; j++)
32 {
33 uint8_t temp = data[start + j];
34 data[start + j] = data[end - j];
35 data[end - j] = temp;
36 }
37 }
38
39 sequence_length = 0;
40 }
41 }
42
43 /* Flush a trailing sequence at the end of the buffer */
44 if (sequence_length > 1)
45 {
46 size_t start = length - sequence_length;
47 size_t end = start + sequence_length - 1;
48 size_t j;
49
50 for (j = 0; j < sequence_length / 2; j++)
51 {
52 uint8_t temp = data[start + j];
53 data[start + j] = data[end - j];
54 data[end - j] = temp;
55 }
56 }
57}
58
60{
61 return (uint8_t)(eo_rand_range(6, 12));
62}
63
64void eo_encrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
65{
66 size_t big_half;
67 size_t i;
68
69 if (length < 2 || (data[0] == EO_BREAK_BYTE && data[1] == EO_BREAK_BYTE))
70 {
71 return;
72 }
73
74 eo_swap_multiples(data, length, swap_multiple);
75
76 /* ceiling div */
77 big_half = (length + 1) / 2;
78
79 for (i = 0; i < length; i++)
80 {
81 size_t next;
82 size_t j;
83 uint8_t temp;
84
85 next = (i < big_half) ? (i * 2) : ((length - 1 - i) * 2 + 1);
86 if (next == i)
87 {
88 if ((data[i] & 0x7F) != 0)
89 {
90 data[i] ^= 0x80;
91 }
92 continue;
93 }
94
95 j = next;
96 while (j != i && j > i)
97 {
98 j = (j < big_half) ? (j * 2) : ((length - 1 - j) * 2 + 1);
99 }
100 if (j != i)
101 {
102 continue;
103 }
104
105 temp = data[i];
106 j = next;
107 while (j != i)
108 {
109 uint8_t swap = data[j];
110 if ((temp & 0x7F) != 0)
111 {
112 temp ^= 0x80;
113 }
114 data[j] = temp;
115 temp = swap;
116 j = (j < big_half) ? (j * 2) : ((length - 1 - j) * 2 + 1);
117 }
118
119 if ((temp & 0x7F) != 0)
120 {
121 temp ^= 0x80;
122 }
123 data[i] = temp;
124 }
125}
126
127void eo_decrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
128{
129 size_t i;
130
131 if (length < 2 || (data[0] == EO_BREAK_BYTE && data[1] == EO_BREAK_BYTE))
132 {
133 return;
134 }
135
136 for (i = 0; i < length; i++)
137 {
138 size_t next;
139 size_t j;
140 uint8_t temp;
141
142 next = (i % 2 == 0) ? (i / 2) : (length - 1 - (i - 1) / 2);
143 if (next == i)
144 {
145 if ((data[i] & 0x7F) != 0)
146 {
147 data[i] ^= 0x80;
148 }
149 continue;
150 }
151
152 j = next;
153 while (j != i && j > i)
154 {
155 j = (j % 2 == 0) ? (j / 2) : (length - 1 - (j - 1) / 2);
156 }
157 if (j != i)
158 {
159 continue;
160 }
161
162 temp = data[i];
163 j = next;
164 while (j != i)
165 {
166 uint8_t swap = data[j];
167 if ((temp & 0x7F) != 0)
168 {
169 temp ^= 0x80;
170 }
171 data[j] = temp;
172 temp = swap;
173 j = (j % 2 == 0) ? (j / 2) : (length - 1 - (j - 1) / 2);
174 }
175
176 if ((temp & 0x7F) != 0)
177 {
178 temp ^= 0x80;
179 }
180 data[i] = temp;
181 }
182
183 eo_swap_multiples(data, length, swap_multiple);
184}
#define EO_BREAK_BYTE
Definition data.h:20
void eo_swap_multiples(uint8_t *data, size_t length, uint8_t multiple)
Definition encrypt.c:12
void eo_encrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
Definition encrypt.c:64
uint8_t eo_generate_swap_multiple(void)
Definition encrypt.c:59
void eo_decrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
Definition encrypt.c:127
int32_t eo_server_verification_hash(int32_t challenge)
Definition encrypt.c:5
uint32_t eo_rand_range(uint32_t min, uint32_t max)
Definition rng.c:38
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78