-
Notifications
You must be signed in to change notification settings - Fork 1
/
kuznyechik.c
376 lines (366 loc) · 20 KB
/
kuznyechik.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
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
#include <stdint.h>
#ifdef __ARM_NEON
#include <arm_neon.h>
#elif defined(__ARM_FEATURE_MVE) // M-profile Vector Extension
#include <arm_mve.h>
#else
typedef uint8_t uint8x16_t __attribute__((__vector_size__(16)));
//vld1q_u8()
static inline uint8x16_t vld1q_u8(const uint8_t *p) {
return *(uint8x16_t*)p;
}
static inline uint8x16_t vdupq_n_u8(uint8_t a) {
return (uint8x16_t){a,a,a,a, a,a,a,a, a,a,a,a, a,a,a,a}; ;//{15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
}
#endif
static inline uint8x16_t REV128(uint8x16_t v) {
return __builtin_shuffle(v, (uint8x16_t){15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0});
}
static const uint8x16_t log_LMT[16] = {// 0x00 0xFF
{0x00, 0x2D, 0xAF, 0xBC, 0x04, 0x64, 0xBF, 0xA7, 0xE6, 0x51, 0x38, 0xD4, 0x82, 0xD0, 0x8D, 0xF3},
{0x2D, 0xE8, 0xB7, 0x46, 0xE9, 0x8D, 0xCA, 0x1F, 0xF3, 0x85, 0x08, 0x28, 0x25, 0x3A, 0x4A, 0x6D},
{0x05, 0xDA, 0x38, 0x13, 0x38, 0x38, 0xB8, 0xEE, 0x30, 0x57, 0x01, 0xBC, 0x3D, 0xA1, 0x78, 0xEE},
{0x8A, 0x78, 0xEF, 0x59, 0xCA, 0x4C, 0x29, 0xA2, 0xC5, 0x59, 0x98, 0x7B, 0x97, 0x7F, 0xA5, 0xE2},
{0x04, 0xEA, 0x7A, 0xFD, 0xFD, 0xCB, 0x2A, 0x00, 0x66, 0xDB, 0x87, 0x00, 0x43, 0xC6, 0x70, 0xFC},
{0xEC, 0x93, 0x1C, 0xB7, 0xD1, 0x2E, 0xD8, 0x30, 0xF2, 0xAB, 0x39, 0x1E, 0xF6, 0xA1, 0xE6, 0xF6},
{0xA3, 0x72, 0xBA, 0x4F, 0x81, 0xF7, 0x31, 0xD4, 0x19, 0x2E, 0xFE, 0xC5, 0x0B, 0x4B, 0xB7, 0x63},
{0x00, 0x31, 0xA1, 0xF5, 0x21, 0xAF, 0x03, 0x35, 0xC5, 0x5C, 0x89, 0x93, 0xBA, 0x67, 0x69, 0x3C},
{0xC2, 0xC9, 0x9C, 0x19, 0x04, 0x8B, 0xF6, 0x43, 0x62, 0x45, 0xF3, 0x5A, 0xC4, 0x53, 0xC1, 0x2A},
{0x00, 0xC8, 0x71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B, 0x55, 0xAA, 0x97, 0xED, 0x19, 0x32, 0x64, 0xC8, 0x53, 0xA6, 0x8F, 0xDD, 0x79, 0xF2, 0x27,
0x4E, 0x9C, 0xFB, 0x35, 0x6A, 0xD4, 0x6B, 0xD6, 0x6F, 0xDE, 0x7F, 0xFE, 0x3F, 0x7E, 0xFC, 0x3B,
0x76, 0xEC, 0x1B, 0x36, 0x6C, 0xD8, 0x73, 0xE6, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0x23, 0x46, 0x8C,
0xDB, 0x75, 0xEA, 0x17, 0x2E, 0x5C, 0xB8, 0xB3, 0xA5, 0x89, 0xD1, 0x61, 0xC2, 0x47, 0x8E, 0xDF,
0x7D, 0xFA, 0x37, 0x6E, 0xDC, 0x7B, 0xF6, 0x2F, 0x5E, 0xBC, 0xBB, 0xB5, 0xA9, 0x91, 0xE1, 0x01,
};
#define SYM 255 // специальный символ, добавляется для исключения проверки
static inline uint8_t mod255(unsigned v) {
return v + (v>>8);
// return (v*0x10102u)>>(16);
}
static uint8x16_t LS4(uint8x16_t a)
{
/* const uint8x16_t exp_hi = {0x01, 0x1F, 0x96, 0x2A, 0xA0, 0x3D, 0xCE, 0x2D, 0xFD, 0x59, 0x18, 0xCB, 0x4E, 0x76, 0xDB, 0x7D};
const uint8x16_t exp_lo = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xC3, 0x45, 0x8A, 0xD7, 0x6D, 0xDA, 0x77, 0xEE};
const uint8x16_t mask = vdupq_n_u8(0xF); */
uint8x16_t v = {0};
int i;
for(i=0;i<16;i++){
uint8_t a8 = log_sbox[a[i]];
if (a8!=SYM) {// LOG0
#if 0// todo сделать для MVE через умножение полиномов p8
uint8x16_t q = vdupq_n_u8(a8);
#else
int j;
for (j=0;j<16;j++){
v[j] ^= exp_[mod255(a8 + log_LMT[i][j])];// %255
}
#endif
}
}
return v;
}
typedef struct {
uint8x16_t K[10];
} KuznCtx;
uint8x16_t kuzn_encrypt(KuznCtx* ctx, const uint8x16_t a)
{
uint8x16_t s = ( a ^ ctx->K[0]);
int i;
for (i=0; i<9; i++){
s = (LS4(s) ^ ctx->K[i+1]);
}
return s;
}
static const uint8x16_t Cx [32] = {
{0x01, 0x94, 0x84, 0xdd, 0x10, 0xbd, 0x27, 0x5d, 0xb8, 0x7a, 0x48, 0x6c, 0x72, 0x76, 0xa2, 0x6e },
{0x02, 0xeb, 0xcb, 0x79, 0x20, 0xb9, 0x4e, 0xba, 0xb3, 0xf4, 0x90, 0xd8, 0xe4, 0xec, 0x87, 0xdc },
{0x03, 0x7f, 0x4f, 0xa4, 0x30, 0x04, 0x69, 0xe7, 0x0b, 0x8e, 0xd8, 0xb4, 0x96, 0x9a, 0x25, 0xb2 },
{0x04, 0x15, 0x55, 0xf2, 0x40, 0xb1, 0x9c, 0xb7, 0xa5, 0x2b, 0xe3, 0x73, 0x0b, 0x1b, 0xcd, 0x7b },
{0x05, 0x81, 0xd1, 0x2f, 0x50, 0x0c, 0xbb, 0xea, 0x1d, 0x51, 0xab, 0x1f, 0x79, 0x6d, 0x6f, 0x15 },
{0x06, 0xfe, 0x9e, 0x8b, 0x60, 0x08, 0xd2, 0x0d, 0x16, 0xdf, 0x73, 0xab, 0xef, 0xf7, 0x4a, 0xa7 },
{0x07, 0x6a, 0x1a, 0x56, 0x70, 0xb5, 0xf5, 0x50, 0xae, 0xa5, 0x3b, 0xc7, 0x9d, 0x81, 0xe8, 0xc9 },
{0x08, 0x2a, 0xaa, 0x27, 0x80, 0xa1, 0xfb, 0xad, 0x89, 0x56, 0x05, 0xe6, 0x16, 0x36, 0x59, 0xf6 },
{0x09, 0xbe, 0x2e, 0xfa, 0x90, 0x1c, 0xdc, 0xf0, 0x31, 0x2c, 0x4d, 0x8a, 0x64, 0x40, 0xfb, 0x98 },
{0x0a, 0xc1, 0x61, 0x5e, 0xa0, 0x18, 0xb5, 0x17, 0x3a, 0xa2, 0x95, 0x3e, 0xf2, 0xda, 0xde, 0x2a },
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 0xe7, 0x94, 0xc7, 0x73, 0x79, 0xa4, 0xc3, 0x9b, 0xf8, 0x88 },
{0x17, 0x3e, 0x8d, 0x18, 0xb3, 0x34, 0xc0, 0xc9, 0x7f, 0x09, 0x31, 0xc8, 0xb1, 0xed, 0x5a, 0xe6 },
{0x18, 0x7e, 0x3d, 0x69, 0x43, 0x20, 0xce, 0x34, 0x58, 0xfa, 0x0f, 0xe9, 0x3a, 0x5a, 0xeb, 0xd9 },
{0x19, 0xea, 0xb9, 0xb4, 0x53, 0x9d, 0xe9, 0x69, 0xe0, 0x80, 0x47, 0x85, 0x48, 0x2c, 0x49, 0xb7 },
{0x1a, 0x95, 0xf6, 0x10, 0x63, 0x99, 0x80, 0x8e, 0xeb, 0x0e, 0x9f, 0x31, 0xde, 0xb6, 0x6c, 0x05 },
{0x1b, 0x01, 0x72, 0xcd, 0x73, 0x24, 0xa7, 0xd3, 0x53, 0x74, 0xd7, 0x5d, 0xac, 0xc0, 0xce, 0x6b },
{0x1c, 0x6b, 0x68, 0x9b, 0x03, 0x91, 0x52, 0x83, 0xfd, 0xd1, 0xec, 0x9a, 0x31, 0x41, 0x26, 0xa2 },
{0x1d, 0xff, 0xec, 0x46, 0x13, 0x2c, 0x75, 0xde, 0x45, 0xab, 0xa4, 0xf6, 0x43, 0x37, 0x84, 0xcc },
{0x1e, 0x80, 0xa3, 0xe2, 0x23, 0x28, 0x1c, 0x39, 0x4e, 0x25, 0x7c, 0x42, 0xd5, 0xad, 0xa1, 0x7e },
{0x1f, 0x14, 0x27, 0x3f, 0x33, 0x95, 0x3b, 0x64, 0xf6, 0x5f, 0x34, 0x2e, 0xa7, 0xdb, 0x03, 0x10 },
{0x20, 0xa8, 0xed, 0x9c, 0x45, 0xc1, 0x6a, 0xf1, 0x61, 0x9b, 0x14, 0x1e, 0x58, 0xd8, 0xa7, 0x5e }
};
//#pragma GCC optimize("Os")
/*! разгибание ключа
Nk -- длина ключа 4 слова (128 бит)
*/
void kuzn_key_expansion(KuznCtx * ctx, const uint8_t* key, int klen, int ekb)
{
uint8x16_t *K = ctx->K;
uint8x16_t a, b;
K[0] = a = REV128(vld1q_u8(key+ 0));//__builtin_memcpy(&K[0],&key[16], 16);
K[1] = b = REV128(vld1q_u8(key+16));//__builtin_memcpy(&K[1],&key[ 0], 16);
// K[0] = a = vld1q_u8(key+16);
// K[1] = b = vld1q_u8(key+ 0);
int j, n = 0;
for(j=2; j<9; j+=2) {
int i;
for (i=0;i<8;i+=2){
/* можно вычислять констранты налету
Сx = L((v16qi){n+1,0...0})
*/
//register uint8x16_t v = a;
b = LS4(a ^ Cx[n++]) ^ b;
a = LS4(b ^ Cx[n++]) ^ a;
//b = v;
}
K[j] = a; K[j+1] = b;
}
}
#if defined(TEST_KUZN)
static uint8_t sbox[] = {
252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77,
233, 119, 240, 219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193,
249, 24, 101, 90, 226, 92, 239, 33, 129, 28, 60, 66, 139, 1, 142, 79,
5, 132, 2, 174, 227, 106, 143, 160, 6, 11, 237, 152, 127, 212, 211, 31,
235, 52, 44, 81, 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206, 204,
181, 112, 14, 86, 8, 12, 118, 18, 191, 114, 19, 71, 156,
183, 93, 135, 21, 161, 150, 41, 16, 123, 154, 199, 243, 145, 120, 111, 157, 158, 178,
177, 50, 117, 25, 61, 255, 53, 138, 126, 109, 84, 198, 128, 195, 189, 13, 87, 223,
245, 36, 169, 62, 168, 67, 201, 215, 121, 214, 246, 124, 34, 185, 3, 224, 15, 236,
222, 122, 148, 176, 188, 220, 232, 40, 80, 78, 51, 10, 74, 167, 151, 96, 115, 30, 0,
98, 68, 26, 184, 56, 130, 100, 159, 38, 65, 173, 69, 70, 146, 39, 94, 85, 47, 140, 163,
165, 125, 105, 213, 149, 59, 7, 88, 179, 64, 134, 172, 29, 247, 48, 55, 107, 228, 136,
217, 231, 137, 225, 27, 131, 73, 76, 63, 248, 254, 141, 83, 170, 144, 202, 216, 133,
97, 32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208, 190, 229, 108, 82, 89, 166,
116, 210, 230, 244, 180, 192, 209, 102, 175, 194, 57, 75, 99, 182
};
static const uint8_t LMT[256] = {
0x01, 0x94, 0x84, 0xdd, 0x10, 0xbd, 0x27, 0x5d, 0xb8, 0x7a, 0x48, 0x6c, 0x72, 0x76, 0xa2, 0x6e,
0x94, 0xa5, 0x64, 0x0d, 0x89, 0xa2, 0x7f, 0x4b, 0x6e, 0x16, 0xc3, 0x4c, 0xe8, 0xe3, 0xd0, 0x4d,
0x20, 0x3c, 0x48, 0xf8, 0x48, 0x48, 0xc8, 0x8e, 0x2a, 0xf5, 0x02, 0xdd, 0x14, 0x30, 0x44, 0x8e,
0x85, 0x44, 0xdf, 0x52, 0x7f, 0xc6, 0x98, 0x60, 0xd4, 0x52, 0x0e, 0x65, 0x07, 0x9f, 0x86, 0xea,
0x10, 0xd1, 0xd3, 0x91, 0x91, 0xfe, 0xf3, 0x01, 0xb1, 0x78, 0x58, 0x01, 0x49, 0x6b, 0x2d, 0xa9,
0xc2, 0x8d, 0x31, 0x64, 0xec, 0xeb, 0x0f, 0x2a, 0x37, 0x99, 0x90, 0xc4, 0xf6, 0x30, 0xb8, 0xf6,
0xc0, 0xb4, 0xa6, 0xff, 0x39, 0x2f, 0x54, 0x6c, 0xaf, 0xeb, 0xe1, 0xd4, 0xd7, 0x63, 0x64, 0xbf,
0x01, 0x54, 0x30, 0x7b, 0xef, 0x84, 0x08, 0x09, 0xd4, 0xd5, 0xa3, 0x8d, 0xa6, 0xa1, 0xc1, 0x0a,
0xfb, 0xde, 0xe0, 0xaf, 0x10, 0xc9, 0xf6, 0x49, 0xbe, 0xe7, 0x6e, 0xa4, 0x6a, 0x2b, 0x9c, 0xf3,
0x01, 0x6f, 0x5a, 0x3d, 0xbf, 0xad, 0xee, 0xab, 0xf1, 0xc4, 0xaf, 0x02, 0xd6, 0x1c, 0x89, 0xf2,
0xc0, 0x77, 0x44, 0x94, 0x60, 0x7c, 0x12, 0x8d, 0x2e, 0x2d, 0xbc, 0xeb, 0x11, 0x43, 0x48, 0x8e,
0xc2, 0x5d, 0x97, 0xf3, 0xe9, 0x1a, 0x8d, 0xcb, 0xbb, 0x06, 0xc5, 0x20, 0x1c, 0x68, 0x90, 0x93,
0x10, 0x96, 0xca, 0xd9, 0x30, 0x68, 0x2f, 0x14, 0x1a, 0x17, 0x0c, 0xca, 0x0c, 0x70, 0xda, 0xbf,
0x85, 0x74, 0x75, 0xd0, 0x5e, 0xbe, 0xb8, 0x87, 0x4e, 0x62, 0xec, 0x6b, 0x10, 0x87, 0xc6, 0x74,
0x20, 0x2d, 0x99, 0xe9, 0x95, 0x9f, 0xd4, 0x49, 0xe6, 0xd5, 0x76, 0xf2, 0x33, 0xc8, 0x20, 0x98,
0x94, 0x84, 0xdd, 0x10, 0xbd, 0x27, 0x5d, 0xb8, 0x7a, 0x48, 0x6c, 0x72, 0x76, 0xa2, 0x6e, 0xcf,
};
uint8_t LM[] = {
0x71, 0x05, 0x09, 0xB9, 0x61, 0xA2, 0x27, 0x0E,
0x04, 0x88, 0x5B, 0xB2, 0xE4, 0x36, 0x5F, 0x65,
0x5F, 0xCB, 0xAD, 0x0F, 0xBA, 0x2C, 0x04, 0xA5,
0xE5, 0x01, 0x54, 0xBA, 0x0F, 0x11, 0x2A, 0x76,
0xD4, 0x81, 0x1C, 0xFA, 0x39, 0x5E, 0x15, 0x24,
0x05, 0x71, 0x5E, 0x66, 0x17, 0x1C, 0xD0, 0x02,
0x2D, 0xF1, 0xE7, 0x28, 0x55, 0xA0, 0x4C, 0x9A,
0x0E, 0x02, 0xF6, 0x8A, 0x15, 0x9D, 0x39, 0x71,
};
static unsigned char Sbox_R[256] = {// S-Box Stribog 0x171 Rev8
0x3F,0xFB,0xD7,0xE0,0x9F,0xE5,0xA8,0x04,0x97,0x07,0xAD,0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50, 0x4E, 0x33, 0x0A, 0x4A,
0xA7, 0x97, 0x60, 0x73, 0x1E, 0x00, 0x62, 0x44, 0x1A, 0xB8, 0x38, 0x82, 0x64, 0x9F, 0x26, 0x41,
0xAD, 0x45, 0x46, 0x92, 0x27, 0x5E, 0x55, 0x2F, 0x8C, 0xA3, 0xA5, 0x7D, 0x69, 0xD5, 0x95, 0x3B,
0x07, 0x58, 0xB3, 0x40, 0x86, 0xAC, 0x1D, 0xF7, 0x30, 0x37, 0x6B, 0xE4, 0x88, 0xD9, 0xE7, 0x89,
0xE1, 0x1B, 0x83, 0x49, 0x4C, 0x3F, 0xF8, 0xFE, 0x8D, 0x53, 0xAA, 0x90, 0xCA, 0xD8, 0x85, 0x61,
0x20, 0x71, 0x67, 0xA4, 0x2D, 0x2B, 0x09, 0x5B, 0xCB, 0x9B, 0x25, 0xD0, 0xBE, 0xE5, 0x6C, 0x52,
0x59, 0xA6, 0x74, 0xD2, 0xE6, 0xF4, 0xB4, 0xC0, 0xD1, 0x66, 0xAF, 0xC2, 0x39, 0x4B, 0x63, 0xB6
};
uint8_t Rev8(uint8_t a){
const uint8_t m4=0x0F, m2=0x33, m1=0x55;
a = (a &~m4)>>4 | (a & m4)<<4;
a = (a &~m2)>>2 | (a & m2)<<2;
a = (a &~m1)>>1 | (a & m1)<<1;
return a;
}
#include <stdio.h>
#include <inttypes.h>
void kuzn_gen_tables()
{
uint8_t poly= 0x71;
uint8_t log[256];
uint8_t b;
int i;
log[0] = SYM;
b = 1;
printf("static const uint8_t exp_171[256] = {\n\t0x%02hhX, ", Rev8(b));
for(i = 0; i < 0xFF; i++) {
log[b] = i;
if(__builtin_add_overflow(b, b, &b)){// сдвиг
b ^= poly;
}
printf("0x%02hhX, ", Rev8(b));
if ((i&0xF) == 0xE) printf("\n\t");
}
printf("};\n");
printf("static const uint8_t log_LM[64] = {\n\t");
log[0] = SYM;
for(i = 0; i <= 63; i++) {
printf("0x%02hhX, ", log[LM[i]]);
if ((i&0x7) == 0x7) printf("\n\t");
}
printf("};\n");
printf("static const uint8_t log_Sbox[256] = {\n\t");
for(i = 0; i <= 0xFF; i++) {
printf("0x%02hhX, ", log[Sbox_R[Rev8(i)]]);
if ((i&0xF) == 0xF) printf("\n\t");
}
printf("};\n");
poly= 0xC3;
#if 0
b = 1;
printf("static const uint8_t exp_hi[16] = {\n\t0x%02hhX, ", b);
for(i = 0; i < 0xFF; i++) {
log[b] = i;
if(__builtin_add_overflow(b, b, &b)){// сдвиг
b ^= poly;
}
if ((i&0xF)==0xF) printf("0x%02hhX, ", b);
}
printf("};\n");
b = 1;
printf("static const uint8_t exp_lo[16] = {\n\t0x%02hhX, ", b);
for(i = 0; i < 0x0F; i++) {
log[b] = i;
if(__builtin_add_overflow(b, b, &b)){// сдвиг
b ^= poly;
}
printf("0x%02hhX, ", b);
}
printf("};\n");
#endif
b = 1;
printf("static const uint8_t exp_[256] = {\n\t0x%02hhX, ", b);
for(i = 0; i < 0xFF; i++) {
log[b] = i;
if(__builtin_add_overflow(b, b, &b)){// сдвиг
b ^= poly;
}
printf("0x%02hhX, ", b);
if ((i&0xF) == 0xE) printf("\n\t");
}
printf("};\n");
printf("static const uint8_t log_[256] = {\n\t");
for(i = 0; i <= 0xFF; i++) {
printf("0x%02hhX, ", log[i]);
if ((i&0xF) == 0xF) printf("\n\t");
}
printf("};\n");
printf("static const uint8_t log_LMT[256] = {\n\t");
log[0] = SYM;
for(i = 0; i <= 0xFF; i++) {
printf("0x%02hhX, ", log[LMT[i]]);
if ((i&0xF) == 0xF) printf("\n\t");
}
printf("};\n");
}
int main(){
kuzn_gen_tables();
KuznCtx ctx;
/* uint8x16_t key[2] = {
{0xef,0xcd,0xab,0x89,0x67,0x45,0x23,0x01,0x10,0x32,0x54,0x76,0x98,0xba,0xdc,0xfe},
{0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00,0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88},
}; */
uint8x16_t key[2] = {
{0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77},
{0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF}};
kuzn_key_expansion(&ctx, (uint8_t*)key, 0, 0);
int i;
printf("4.4.1 Алгоритм зашифрования\n");
uint8x16_t a = {0x88, 0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x77,0x66,0x55,0x44,0x33,0x22,0x11};
printf("plain text:\n");
for (i=0; i<16; i++) printf("%02x", a[15-i]);// 1122334455667700ffeeddccbbaa9988
printf("\n");
uint8x16_t b = kuzn_encrypt(&ctx, a);
printf("cipher text:\n");
for (i=0; i<16; i++) printf("%02x", b[15-i]);// 7f679d90bebc24305a468d42b9d4edcd
printf("\n");
return 0;
}
#endif