-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpurge.c
227 lines (188 loc) · 7.41 KB
/
purge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* purge.c
* 512-bit block encryption algorithm
* Author Kucheruavyu Ilya ([email protected])
* 05/27/2015 Ukraine Kharkiv
* _ _ _ _
* | | (_|_) |
* | | _____ _ _| |__ __ _
* | |/ / _ \| | | '_ \ / _` |
* | < (_) | | | |_) | (_| |
* |_|\_\___/| |_|_.__/ \__,_|
* _/ |
* |__/
**/
#include "purge.h"
#include <string.h>
#define PURGE_DECRYPT_SPEEDUP // cause more memory usage, and attack on buffer with round keys
#pragma GCC push_options
#pragma GCC optimize ("O0")
typedef uint8_t byte;
#define forAll(iterator, count) for(iterator = 0; iterator < (count); ++iterator)
// 64 circular shift (rotates)
#define rotateLeft(data, shift) (((data) << shift) | ((data) >> (64 - shift)))
#define rotateRight(data, shift) (((data) >> shift) | ((data) << (64 - shift)))
#define sawSwap(array) temp = array[7]; \
array[7] = array[2]; \
array[2] = array[5]; \
array[5] = array[0]; \
array[0] = temp; \
temp = 0
#define sawUnswap(array) temp = array[0]; \
array[0] = array[5]; \
array[5] = array[2]; \
array[2] = array[7]; \
array[7] = temp; \
temp = 0
#define roundKeyXor(data, key) data[0] ^= key[0]; \
data[1] ^= key[1]; \
data[2] ^= key[2]; \
data[3] ^= key[3]; \
data[4] ^= key[4]; \
data[5] ^= key[5]; \
data[6] ^= key[6]; \
data[7] ^= key[7]
uint64_t substitute(uint64_t data, const byte *block) {
union bytesTo64 {
uint64_t number;
byte bytes[8];
};
union bytesTo64 wrapper;
wrapper.number = data;
wrapper.bytes[0] = block[wrapper.bytes[0]];
wrapper.bytes[1] = block[wrapper.bytes[1]];
wrapper.bytes[2] = block[wrapper.bytes[2]];
wrapper.bytes[3] = block[wrapper.bytes[3]];
wrapper.bytes[4] = block[wrapper.bytes[4]];
wrapper.bytes[5] = block[wrapper.bytes[5]];
wrapper.bytes[6] = block[wrapper.bytes[6]];
wrapper.bytes[7] = block[wrapper.bytes[7]];
return wrapper.number;
}
void rotateBytes(byte *data, byte count) {
if(count % purgeBytesCount) {
byte diff = (byte) (purgeBytesCount - count);
byte temp[purgeBytesCount];
memcpy((byte*) temp, data, count);
memcpy( data, data + count, diff);
memcpy( data + diff, (byte*) temp, count);
memset((byte*) temp, 0, purgeBytesCount);
}
}
void reverseRotateBytes(byte *data, byte count) {
if(count % purgeBytesCount) {
byte temp[purgeBytesCount];
byte diff = (byte) (purgeBytesCount - count);
memcpy(temp, data + diff, count);
memcpy(data + count, data, diff);
memcpy(data, temp, count);
memset(temp, 0, purgeBytesCount);
}
}
void roundKeyForStep(uint64_t key[8], uint8_t step) {
byte iterator;
uint64_t temp;
forAll(iterator, step) {
sawSwap(key);
key[1] = substitute(key[1], purgeSubstitutionBlock);
key[3] = substitute(key[3], purgeSubstitutionBlock);
key[4] = substitute(key[4], purgeSubstitutionBlock);
key[6] = substitute(key[6], purgeSubstitutionBlock);
key[0] += purgeKeyАmplificationConstans[0];
key[1] += purgeKeyАmplificationConstans[1];
key[2] += purgeKeyАmplificationConstans[2];
key[3] += purgeKeyАmplificationConstans[3];
key[4] += purgeKeyАmplificationConstans[4];
key[5] += purgeKeyАmplificationConstans[5];
key[6] += purgeKeyАmplificationConstans[6];
key[7] += purgeKeyАmplificationConstans[7];
rotateBytes((byte *) key, (byte) (iterator % (purgeBytesCount / 2) + 1));
}
memset(&iterator, 0, 1);
}
#ifndef PURGE_DECRYPT_SPEEDUP
void decryptRoundKey(uint64_t key[8], uint8_t step) {
byte iterator;
uint64_t temp;
for(iterator = (byte) (step - 1); iterator != 255; --iterator) {
reverseRotateBytes((byte *) key, (byte) (iterator % (purgeBytesCount / 2) + 1));
key[0] -= purgeKeyАmplificationConstans[0];
key[1] -= purgeKeyАmplificationConstans[1];
key[2] -= purgeKeyАmplificationConstans[2];
key[3] -= purgeKeyАmplificationConstans[3];
key[4] -= purgeKeyАmplificationConstans[4];
key[5] -= purgeKeyАmplificationConstans[5];
key[6] -= purgeKeyАmplificationConstans[6];
key[7] -= purgeKeyАmplificationConstans[7];
key[1] = substitute(key[1], purgeReverseSubstitutionBlock);
key[3] = substitute(key[3], purgeReverseSubstitutionBlock);
key[4] = substitute(key[4], purgeReverseSubstitutionBlock);
key[6] = substitute(key[6], purgeReverseSubstitutionBlock);
sawUnswap(key);
}
memset(&iterator, 0, 1);
}
#endif
void purgeEncrypt(uint64_t data[8], uint64_t key[8]) {
uint64_t temp;
byte iterator;
forAll(iterator, purgeRoundsCount) { // 63 rounds [0 : 62]
// round
data[0] = rotateLeft(data[0], 7);
data[1] ^= purgePalindromeMask;
data[2] ^= purgePalindromeMaskReverse;
data[3] = rotateLeft(data[3], 13);
data[4] += data[3];
data[5] += data[0];
data[6] += purgeMask;
data[7] += purgeMaskReverse;
roundKeyForStep(key, (uint8_t) (iterator + 1));
roundKeyXor(data, key);
sawSwap(data);
data[1] = substitute(data[1], purgeSubstitutionBlock);
data[3] = substitute(data[3], purgeSubstitutionBlock);
data[4] = substitute(data[4], purgeSubstitutionBlock);
data[6] = substitute(data[6], purgeSubstitutionBlock);
rotateBytes((byte *) data, (byte)(iterator % (purgeBytesCount / 2) + 1));
}
memset((byte *)key, 0, purgeBytesCount);
}
void purgeDecrypt(uint64_t data[8], uint64_t key[8]) {
uint64_t temp;
byte iterator;
#ifdef PURGE_DECRYPT_SPEEDUP
uint64_t roundKeys[purgeRoundsCount][8];
#endif
// get final key
forAll(iterator, purgeRoundsCount) {
roundKeyForStep(key, (uint8_t) (iterator + 1));
#ifdef PURGE_DECRYPT_SPEEDUP
memcpy(roundKeys[iterator], key, purgeBytesCount);
#endif
}
for(iterator = purgeRoundsCount - 1; iterator != 255; --iterator) {
reverseRotateBytes((byte *) data, (byte)(iterator % (purgeBytesCount / 2) + 1));
data[1] = substitute(data[1], purgeReverseSubstitutionBlock);
data[3] = substitute(data[3], purgeReverseSubstitutionBlock);
data[4] = substitute(data[4], purgeReverseSubstitutionBlock);
data[6] = substitute(data[6], purgeReverseSubstitutionBlock);
sawUnswap(data);
#ifdef PURGE_DECRYPT_SPEEDUP
roundKeyXor(data, roundKeys[iterator]);
#else
roundKeyXor(data, key);
decryptRoundKey(key, (uint8_t) (iterator + 1));
#endif
// round
data[7] -= purgeMaskReverse;
data[6] -= purgeMask;
data[5] -= data[0];
data[4] -= data[3];
data[3] = rotateRight(data[3], 13);
data[2] ^= purgePalindromeMaskReverse;
data[1] ^= purgePalindromeMask;
data[0] = rotateRight(data[0], 7);
}
memset((byte *) key, 0, purgeBytesCount);
}
#pragma GCC pop_options