-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneat.c
More file actions
249 lines (208 loc) · 7.91 KB
/
neat.c
File metadata and controls
249 lines (208 loc) · 7.91 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define SWAP(A, B) { uint16_t T; T = A; A = B; B = T; }
typedef struct neat_key_struct_s {
uint16_t enc_round_keys[13][4];
uint16_t dec_round_keys[13][4];
uint8_t enc_round_xcons[12][8];
uint8_t dec_round_xcons[12][8];
} neat_key_struct_t;
const uint16_t key_const[] = {
0xFD56, 0xC7D1, 0xE36C, 0xA2DC
};
const uint8_t fixed_xcon[] = {
0, 1, 0, 1, 2, 3, 2, 3
};
const uint8_t xbox[] = {
0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 3, 2, 0, 1, 1, 0, 2, 3, 2, 3, 0, 1, 1, 0, 2, 3, 3, 2,
0, 1, 0, 2, 1, 3, 3, 2, 0, 1, 2, 0, 1, 3, 2, 3, 0, 1, 2, 0, 1,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,
1, 3, 2, 0, 1, 3, 2, 0, 1, 3, 0, 3, 1, 2, 0, 2, 1, 3, 0, 3, 1, 2, 2, 0, 1, 3, 3, 0, 1, 2, 0, 2,
1, 3, 3, 0, 1, 2, 2, 0, 1, 3, 1, 2, 0, 3, 0, 2, 1, 3, 1, 2, 0, 3, 2, 0, 1, 3, 2, 1, 0, 3, 0, 2,
1, 3, 2, 1, 0, 3, 2, 0, 1, 3, 1, 3, 0, 2, 0, 2, 1, 3, 1, 3, 0, 2, 2, 0, 1, 3, 3, 1, 0, 2, 0, 2,
1, 3, 3, 1, 0, 2, 2, 0, 1, 3, 2, 3, 0, 1, 2, 0, 1, 3, 3, 2, 0, 1, 0, 2, 2, 3, 0, 1, 2, 3, 0, 1,
2, 3, 0, 1, 2, 3, 1, 0, 2, 3, 1, 0, 2, 3, 1, 0, 2, 3, 0, 2, 1, 3, 0, 1, 2, 3, 0, 2, 1, 3, 1, 0,
2, 3, 2, 0, 1, 3, 0, 1, 2, 3, 2, 0, 1, 3, 1, 0, 2, 3, 0, 3, 1, 2, 0, 1, 2, 3, 0, 3, 1, 2, 1, 0,
2, 3, 3, 0, 1, 2, 0, 1, 2, 3, 3, 0, 1, 2, 1, 0, 2, 3, 1, 2, 0, 3, 0, 1, 2, 3, 1, 2, 0, 3, 1, 0,
2, 3, 2, 1, 0, 3, 0, 1, 2, 3, 2, 1, 0, 3, 1, 0, 2, 3, 1, 3, 0, 2, 0, 1, 2, 3, 1, 3, 0, 2, 1, 0,
2, 3, 3, 1, 0, 2, 1, 0, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 1, 0, 2, 3, 3, 2, 0, 1, 1, 0
};
uint16_t mul(uint32_t a, uint32_t b) {
if (a == 0)
a = 0x10000;
if (b == 0)
b = 0x10000;
return (a * b) % 0x10001;
}
uint16_t inv_mul(uint16_t a) {
// calc multiplicative inverse with extended euclidean algorithm
if (a <= 1)
return a;
int m = 0x10001, x = 0, x0 = 1, q, tmp;
while (a) {
q = m / a, tmp = x;
x = x0, x0 = tmp - q * x0;
tmp = m, m = a;
a = tmp - q * a;
}
return (x + 0x10001) % 0x10001;
}
uint16_t rotate_left (uint16_t n, uint16_t d) {
d &= 0xF;
return (n << d) | (n >> (16 - d));
}
uint16_t rotate_right (uint16_t n, uint16_t d) {
d &= 0xF;
return (n << (16 - d)) | (n >> d);
}
void f_function(uint16_t* x, const uint16_t* k) {
x[1] ^= x[0];
x[2] ^= x[3];
x[0] += x[2];
x[3] += x[1];
x[1] = mul(x[1], k[0]);
x[2] = mul(x[2], k[1]);
x[0] = rotate_left(x[0], x[1]);
x[3] = rotate_left(x[3], x[2]);
x[1] += x[3];
x[2] += x[0];
}
void inv_f_function(uint16_t* x, const uint16_t* k) {
x[1] -= x[3];
x[2] -= x[0];
x[0] = rotate_right(x[0], x[1]);
x[3] = rotate_right(x[3], x[2]);
x[1] = mul(x[1], k[2]);
x[2] = mul(x[2], k[3]);
x[0] -= x[2];
x[3] -= x[1];
x[1] ^= x[0];
x[2] ^= x[3];
}
void xor_layer(uint16_t* x, uint8_t* xcon) {
x[xcon[0]] ^= x[xcon[2] + 4];
x[xcon[1]] ^= x[xcon[3] + 4];
x[xcon[4] + 4] ^= x[xcon[6]];
x[xcon[5] + 4] ^= x[xcon[7]];
}
void round_function(uint16_t* x, uint16_t* rk, uint8_t* xcon) {
f_function(x, rk);
inv_f_function(x+4, rk);
xor_layer(x, xcon);
}
void neat_enc_key_schedule(neat_key_struct_t* ks, uint8_t* key) {
uint16_t x[8] = {0x0000, };
uint16_t pos = -1;
uint8_t xcon[8] = {0x0, };
// block to uint16
for (int i = 0; i < 16; i += 2)
x[i / 2] = key[i + 1] | (key[i] << 8);
// generate 12 round keys
for (int i = 0; i < 12; i += 2) {
round_function(&x, &key_const, &fixed_xcon);
memcpy(ks->enc_round_keys[i], x, 8);
memcpy(ks->enc_round_keys[i + 1], x + 4, 8);
}
// generate 1 round keys
round_function(&x, &key_const, &fixed_xcon);
memcpy(ks->enc_round_keys[12], x, 8);
// generate 12 round xcon
for (int i = 0; i < 12; ++i) {
pos = ((ks->enc_round_keys[i][0] ^ ks->enc_round_keys[i][3]) & 0x7F) << 3;
memcpy(ks->enc_round_xcons[i], &xbox[pos], 16);
}
}
void neat_dec_key_schedule(neat_key_struct_t* ks, uint8_t* key) {
// generate 13 dec round keys
for (int i = 0; i < 13; ++i) {
ks->dec_round_keys[i][0] = inv_mul(ks->enc_round_keys[12 - i][2]);
ks->dec_round_keys[i][1] = inv_mul(ks->enc_round_keys[12 - i][3]);
ks->dec_round_keys[i][2] = inv_mul(ks->enc_round_keys[12 - i][0]);
ks->dec_round_keys[i][3] = inv_mul(ks->enc_round_keys[12 - i][1]);
}
// generate 12 dec round xcon
for (int i = 0; i < 12; ++i) {
memcpy(ks->dec_round_xcons[i], ks->enc_round_xcons[11 - i] + 4, 4);
memcpy(ks->dec_round_xcons[i] + 4, ks->enc_round_xcons[11 - i], 4);
}
}
void neat_init_key_schedule(neat_key_struct_t* ks, uint8_t* key) {
neat_enc_key_schedule(ks, key);
neat_dec_key_schedule(ks, key);
}
void neat_crypt(uint8_t* xb, uint16_t* rk, uint8_t* rx) {
uint16_t x[8] = {0x0000, };
// block to uint16
for(int i = 0; i < 16; i += 2)
x[i / 2] = xb[i] | (xb[i + 1] << 8);
// 12 full rounds
for(int i = 0; i < 12; ++i)
round_function(x, &rk[i*4], &rx[i*8]);
// half round
f_function(&x[0], &rk[48]);
inv_f_function(&x[4], &rk[48]);
// final swap
for (int i = 0; i < 4; ++i)
SWAP(x[i], x[i + 4]);
// convert back to bytes
for (int i = 0; i < 16; i += 2) {
xb[i] = x[i / 2];
xb[i + 1] = x[i / 2] >> 8;
}
}
void neat_encrypt(uint8_t* xb, const neat_key_struct_t* ks) {
neat_crypt(xb, &ks->enc_round_keys, &ks->enc_round_xcons);
}
void neat_decrypt(uint8_t* xb, const neat_key_struct_t* ks) {
neat_crypt(xb, &ks->dec_round_keys, &ks->dec_round_xcons);
}
void show_as_hex(uint8_t* x) {
for (int i = 0; i < 16; ++i)
printf("%02X", x[i]);
printf("\n");
}
void show_as_str(uint8_t* x) {
for (int i = 0; i < 16; ++i)
printf("%c", x[i]);
printf("\n");
}
int main (int argc, char* argv[]) {
uint8_t key[16] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50 };
uint8_t x[16] = { 0x4d, 0x34, 0x4b, 0x45, 0x5f, 0x4e, 0x45, 0x34,
0x54, 0x5f, 0x4e, 0x45, 0x34, 0x54, 0x45, 0x52 };
printf("[*] key:\t");
show_as_hex(&key);
neat_key_struct_t ks;
neat_init_key_schedule(&ks, key);
printf("[*] plaintext:\t");
show_as_hex(&x);
neat_encrypt(x, &ks);
printf("[*] ciphertext:\t");
show_as_hex(&x);
neat_decrypt(x, &ks);
printf("[*] recovered:\t");
show_as_str(&x);
}
// EOF