-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagma.c
More file actions
424 lines (355 loc) · 12.8 KB
/
magma.c
File metadata and controls
424 lines (355 loc) · 12.8 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#define _CRT_SECURE_NO_WARNINGS
#include "magma.h"
/* 1.2.643.7.1.2.5.1.1 */
magma_subst_block gost28147_TC26ParamSetZ = {
{0x1, 0x7, 0xe, 0xd, 0x0, 0x5, 0x8, 0x3, 0x4, 0xf, 0xa, 0x6, 0x9, 0xc,
0xb, 0x2}
,
{0x8, 0xe, 0x2, 0x5, 0x6, 0x9, 0x1, 0xc, 0xf, 0x4, 0xb, 0x0, 0xd, 0xa,
0x3, 0x7}
,
{0x5, 0xd, 0xf, 0x6, 0x9, 0x2, 0xc, 0xa, 0xb, 0x7, 0x8, 0x1, 0x4, 0x3,
0xe, 0x0}
,
{0x7, 0xf, 0x5, 0xa, 0x8, 0x1, 0x6, 0xd, 0x0, 0x9, 0x3, 0xe, 0xb, 0x4,
0x2, 0xc}
,
{0xc, 0x8, 0x2, 0x1, 0xd, 0x4, 0xf, 0x6, 0x7, 0x0, 0xa, 0x5, 0x3, 0xe,
0x9, 0xb}
,
{0xb, 0x3, 0x5, 0x8, 0x2, 0xf, 0xa, 0xd, 0xe, 0x1, 0x7, 0x4, 0xc, 0x9,
0x6, 0x0}
,
{0x6, 0x8, 0x2, 0x3, 0x9, 0xa, 0x5, 0xc, 0x1, 0xe, 0x4, 0x7, 0xb, 0xd,
0x0, 0xf}
,
{0xc, 0x4, 0x6, 0x2, 0xa, 0x5, 0xb, 0x9, 0xe, 0x8, 0xd, 0x7, 0x0, 0x3,
0xf, 0x1}
};
/* Set key into context without key mask */
void magma_key_nomask(magma_ctx* c, const byte* k);
/* Intermediate function used for calculate hash */
void magma_enc_with_key(magma_ctx* c, const byte* key, const byte* inblock, byte* outblock);
/* Initialization of magma_ctx subst blocks*/
void kboxinit(magma_ctx* c, const magma_subst_block* b)
{
int i;
for (i = 0; i < 256; i++) {
c->k87[i] = (word32)(b->k8[i >> 4] << 4 | b->k7[i & 15]) << 24;
c->k65[i] = (b->k6[i >> 4] << 4 | b->k5[i & 15]) << 16;
c->k43[i] = (b->k4[i >> 4] << 4 | b->k3[i & 15]) << 8;
c->k21[i] = b->k2[i >> 4] << 4 | b->k1[i & 15];
}
}
/* Part of magma 28147 algorithm moved into separate function */
static word32 f(magma_ctx* c, word32 x)
{
x = c->k87[x >> 24 & 255] | c->k65[x >> 16 & 255] |
c->k43[x >> 8 & 255] | c->k21[x & 255];
/* Rotate left 11 bits */
return x << 11 | x >> (32 - 11);
}
/* Low-level encryption routine - encrypts one 64 bit block*/
void magmacrypt(magma_ctx* c, const byte* in, byte* out)
{
register word32 n1, n2;
n1 = in[7 - 0] | (in[7 - 1] << 8) | (in[7 - 2] << 16) | ((word32)in[7 - 3] << 24);
n2 = in[7 - 4] | (in[7 - 5] << 8) | (in[7 - 6] << 16) | ((word32)in[7 - 7] << 24);
n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
out[7 - 0] = (byte)(n2 & 0xff);
out[7 - 1] = (byte)((n2 >> 8) & 0xff);
out[7 - 2] = (byte)((n2 >> 16) & 0xff);
out[7 - 3] = (byte)(n2 >> 24);
out[7 - 4] = (byte)(n1 & 0xff);
out[7 - 5] = (byte)((n1 >> 8) & 0xff);
out[7 - 6] = (byte)((n1 >> 16) & 0xff);
out[7 - 7] = (byte)(n1 >> 24);
}
/* Low-level decryption routine. Decrypts one 64-bit block */
void magmadecrypt(magma_ctx* c, const byte* in, byte* out)
{
register word32 n1, n2;
n1 = in[7 - 0] | (in[7 - 1] << 8) | (in[7 - 2] << 16) | ((word32)in[7 - 3] << 24);
n2 = in[7 - 4] | (in[7 - 5] << 8) | (in[7 - 6] << 16) | ((word32)in[7 - 7] << 24);
n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
out[7 - 0] = (byte)(n2 & 0xff);
out[7 - 1] = (byte)((n2 >> 8) & 0xff);
out[7 - 2] = (byte)((n2 >> 16) & 0xff);
out[7 - 3] = (byte)(n2 >> 24);
out[7 - 4] = (byte)(n1 & 0xff);
out[7 - 5] = (byte)((n1 >> 8) & 0xff);
out[7 - 6] = (byte)((n1 >> 16) & 0xff);
out[7 - 7] = (byte)(n1 >> 24);
}
/* (low-level) Encrypts several blocks in ECB mode */
void magma_enc(magma_ctx* c, const byte* clear, byte* cipher, int blocks)
{
int i;
for (i = 0; i < blocks; i++) {
magmacrypt(c, clear, cipher);
clear += 8;
cipher += 8;
}
}
/* (low-level) Decrypts several blocks in ECB mode */
void magma_dec(magma_ctx* c, const byte* cipher, byte* clear, int blocks)
{
int i;
for (i = 0; i < blocks; i++) {
magmadecrypt(c, cipher, clear);
clear += 8;
cipher += 8;
}
}
/* Encrypts one block using specified key */
void magma_enc_with_key(magma_ctx* c, const byte* key, const byte* inblock,
byte* outblock)
{
magma_key_nomask(c, key);
magmacrypt(c, inblock, outblock);
}
void magma_dec_with_key(magma_ctx* c, const byte* key, const byte* inblock,
byte* outblock)
{
magma_key_nomask(c, key);
magmadecrypt(c, inblock, outblock);
}
static void magma_key_impl(magma_ctx* c, const byte* k)
{
int i, j;
for (i = 0, j = 0; i < 8; ++i, j += 4) {
c->key[i] =
(k[j + 3] | (k[j + 2] << 8) | (k[j + 1] << 16) | ((word32)k[j] <<
24)) - c->mask[i];
}
}
/* Set 256 bit key into context without key mask */
void magma_key_nomask(magma_ctx* c, const byte* k)
{
memset(c->mask, 0, sizeof(c->mask));
magma_key_impl(c, k);
}
/* (high-level) Encrypts several blocks in ECB mode */
void magma_enc_ecb(const byte* key, const byte* inblock, byte* outblock, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
magma_key_nomask(&c, key);
magma_enc(&c, inblock, outblock, blocks);
}
/* (high-level) Decrypts several blocks in ECB mode */
void magma_dec_ecb(const byte* key, const byte* inblock, byte* outblock, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
magma_key_nomask(&c, key);
magma_dec(&c, inblock, outblock, blocks);
}
/* (low-level) Encrypt several full blocks in CBC mode */
void magma_enc_cbc_low(magma_ctx* c, const byte* key, const byte* iv, const byte* in, byte* out, int size) {
const byte* in_ptr = in;
byte* out_ptr = out;
byte iv_temp[8];
memcpy(iv_temp, iv, 8);
magma_key_nomask(c, key);
while (size > 0) {
for (int i = 0; i < 8; i++) {
out_ptr[i] = iv_temp[i] ^ in_ptr[i];
}
magmacrypt(c, out_ptr, out_ptr);
memcpy(iv_temp, out_ptr, 8);
out_ptr += 8;
in_ptr += 8;
size -= 8;
}
}
//for ECB decryption based version
void magma_enc_cbc_inv_low(magma_ctx* c, const byte* key, const byte* iv, const byte* in, byte* out, int size) {
const byte* in_ptr = in;
byte* out_ptr = out;
byte iv_temp[8];
memcpy(iv_temp, iv, 8);
magma_key_nomask(c, key);
while (size > 0) {
for (int i = 0; i < 8; i++) {
out_ptr[i] = iv_temp[i] ^ in_ptr[i];
}
magmadecrypt(c, out_ptr, out_ptr);
memcpy(iv_temp, out_ptr, 8);
out_ptr += 8;
in_ptr += 8;
size -= 8;
}
}
/* (low-level) Decrypt several full blocks in CBC mode */
void magma_dec_cbc_low(magma_ctx* c, const byte* key, const byte* iv, const byte* in, byte* out, int size) {
byte b[8];
byte d[8];
const byte* in_ptr = in;
byte* out_ptr = out;
byte iv_temp[8];
memcpy(iv_temp, iv, 8);
magma_key_nomask(c, key);
while (size > 0) {
magmadecrypt(c, in_ptr, b);
memcpy(d, in_ptr, 8);
for (int i = 0; i < 8; i++) {
out_ptr[i] = iv_temp[i] ^ b[i];
}
memcpy(iv_temp, d, 8);
out_ptr += 8;
in_ptr += 8;
size -= 8;
}
}
//for ECB decryption based version
void magma_dec_cbc_inv_low(magma_ctx* c, const byte* key, const byte* iv, const byte* in, byte* out, int size) {
byte b[8];
byte d[8];
const byte* in_ptr = in;
byte* out_ptr = out;
byte iv_temp[8];
memcpy(iv_temp, iv, 8);
magma_key_nomask(c, key);
while (size > 0) {
magmacrypt(c, in_ptr, b);
memcpy(d, in_ptr, 8);
for (int i = 0; i < 8; i++) {
out_ptr[i] = iv_temp[i] ^ b[i];
}
memcpy(iv_temp, d, 8);
out_ptr += 8;
in_ptr += 8;
size -= 8;
}
}
/* (high-level) Encrypt several full blocks in CBC mode */
void magma_enc_cbc(const byte* key, const byte* iv, const byte* in, byte* out, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
memcpy(out, iv, 8);
magma_enc_cbc_low(&c, key, iv, in, out + 8, blocks * 8);
}
void magma_enc_cbc_inv(const byte* key, const byte* iv, const byte* in, byte* out, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
memcpy(out, iv, 8);
magma_enc_cbc_inv_low(&c, key, iv, in, out + 8, blocks * 8);
}
/* (high-level) Decrypt several full blocks in CBC mode */
void magma_dec_cbc(const byte* key, const byte* in, byte* out, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
uint8_t iv[8];
memcpy(iv, in, 8);
magma_dec_cbc_low(&c, key, iv, in + 8, out, blocks * 8);
}
void magma_dec_cbc_inv(const byte* key, const byte* in, byte* out, int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
const uint8_t* iv = in;
magma_dec_cbc_inv_low(&c, key, iv, in + 8, out, blocks * 8);
}
/* MAC calculating */
void get_mac(magma_ctx* ctx, byte* buffer, byte* buf2, int n_key)
{
byte r[8] = { 0 };
magmacrypt(ctx, r, r);
int f = r[0] & 128;
for (int j = 0; j <= n_key; j++) { //n_key = 0 or 1
for (int i = 0; i < 7; i++)
{
r[i] = r[i] << 1;
if (r[i + 1] & 128)
r[i] ^= 1;
}
r[7] <<= 1;
if (f) r[7] &= 0x1b;
}
for (int i = 0; i < 8; i++) buffer[i] ^= r[i] ^ buf2[i];
magmacrypt(ctx, buffer, buffer);
}
void magma_mac_low(magma_ctx* ctx, int mac_len, const uint8_t* data, uint8_t* mac, unsigned int data_len)
{
byte buffer[8] = { 0 };
byte buf2[8] = { 0 };
int i;
int n_key = 0;
for (i = 0; i + 8 < data_len; i += 8) {
for (int k = 0; k < 8; k++) {
buffer[k] ^= data[i + k];
}
magmacrypt(ctx, buffer, buffer);
}
if (i + 8 > data_len) n_key++;
memcpy(buf2, data + i, data_len - i);
get_mac(ctx, buffer, buf2, n_key);
memcpy(mac, buffer, 8);
}
/* High-level MAC computation */
void magma_mac(const byte* key, const byte* indata, byte* outdata, const int blocks) {
magma_ctx c;
kboxinit(&c, &gost28147_TC26ParamSetZ);
magma_key_nomask(&c, key);
magma_mac_low(&c, 64, indata, outdata, blocks * 8);
}