eolib 0.5.0
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
5int32_t eo_server_verification_hash(int32_t challenge)
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 for (size_t i = 0; i < length; i++)
16 {
17 if (data[i] % multiple == 0)
18 {
19 sequence_length++;
20 }
21 else
22 {
23 if (sequence_length > 1)
24 {
25 size_t start = i - sequence_length;
26 size_t end = start + sequence_length - 1;
27 for (size_t j = 0; j < sequence_length / 2; j++)
28 {
29 uint8_t temp = data[start + j];
30 data[start + j] = data[end - j];
31 data[end - j] = temp;
32 }
33 }
34
35 sequence_length = 0;
36 }
37 }
38
39 // Flush a trailing sequence at the end of the buffer
40 if (sequence_length > 1)
41 {
42 size_t start = length - sequence_length;
43 size_t end = start + sequence_length - 1;
44 for (size_t j = 0; j < sequence_length / 2; j++)
45 {
46 uint8_t temp = data[start + j];
47 data[start + j] = data[end - j];
48 data[end - j] = temp;
49 }
50 }
51}
52
54{
55 return (uint8_t)(eo_rand_range(6, 12));
56}
57
58void eo_encrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
59{
60 if (length < 2 || (data[0] == EO_BREAK_BYTE && data[1] == EO_BREAK_BYTE))
61 {
62 return;
63 }
64
65 eo_swap_multiples(data, length, swap_multiple);
66
67 // ceiling div
68 size_t big_half = (length + 1) / 2;
69
70 for (size_t i = 0; i < length; i++)
71 {
72 size_t next = (i < big_half) ? (i * 2) : ((length - 1 - i) * 2 + 1);
73 if (next == i)
74 {
75 if ((data[i] & 0x7F) != 0)
76 {
77 data[i] ^= 0x80;
78 }
79 continue;
80 }
81
82 size_t j = next;
83 while (j != i && j > i)
84 {
85 j = (j < big_half) ? (j * 2) : ((length - 1 - j) * 2 + 1);
86 }
87 if (j != i)
88 {
89 continue;
90 }
91
92 uint8_t temp = data[i];
93 j = next;
94 while (j != i)
95 {
96 uint8_t swap = data[j];
97 if ((temp & 0x7F) != 0)
98 {
99 temp ^= 0x80;
100 }
101 data[j] = temp;
102 temp = swap;
103 j = (j < big_half) ? (j * 2) : ((length - 1 - j) * 2 + 1);
104 }
105
106 if ((temp & 0x7F) != 0)
107 {
108 temp ^= 0x80;
109 }
110 data[i] = temp;
111 }
112}
113
114void eo_decrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
115{
116 if (length < 2 || (data[0] == EO_BREAK_BYTE && data[1] == EO_BREAK_BYTE))
117 {
118 return;
119 }
120
121 for (size_t i = 0; i < length; i++)
122 {
123 size_t next = (i % 2 == 0) ? (i / 2) : (length - 1 - (i - 1) / 2);
124 if (next == i)
125 {
126 if ((data[i] & 0x7F) != 0)
127 {
128 data[i] ^= 0x80;
129 }
130 continue;
131 }
132
133 size_t j = next;
134 while (j != i && j > i)
135 {
136 j = (j % 2 == 0) ? (j / 2) : (length - 1 - (j - 1) / 2);
137 }
138 if (j != i)
139 {
140 continue;
141 }
142
143 uint8_t temp = data[i];
144 j = next;
145 while (j != i)
146 {
147 uint8_t swap = data[j];
148 if ((temp & 0x7F) != 0)
149 {
150 temp ^= 0x80;
151 }
152 data[j] = temp;
153 temp = swap;
154 j = (j % 2 == 0) ? (j / 2) : (length - 1 - (j - 1) / 2);
155 }
156
157 if ((temp & 0x7F) != 0)
158 {
159 temp ^= 0x80;
160 }
161 data[i] = temp;
162 }
163
164 eo_swap_multiples(data, length, swap_multiple);
165}
#define EO_BREAK_BYTE
Definition data.h:21
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:58
uint8_t eo_generate_swap_multiple()
Definition encrypt.c:53
void eo_decrypt_packet(uint8_t *data, size_t length, uint8_t swap_multiple)
Definition encrypt.c:114
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