-
Notifications
You must be signed in to change notification settings - Fork 4
/
gost_3412_2015_calc.c
230 lines (204 loc) · 5.37 KB
/
gost_3412_2015_calc.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
#include "gost_3412_2015_calc.h"
#define DEBUG_MODE
#ifdef DEBUG_MODE
static void
GOST_Kuz_PrintDebug(uint8_t *state)
{
int i;
for (i = 0; i < BLOCK_SIZE; i++)
printf("%02x", state[i]);
printf("\n");
}
#endif
static void
GOST_Kuz_S(const uint8_t *in_data, uint8_t *out_data)
{
int i;
for (i = 0; i < BLOCK_SIZE; i++)
out_data[i] = Pi[in_data[i]];
}
static void
GOST_Kuz_reverse_S(const uint8_t *in_data, uint8_t *out_data)
{
int i;
for (i = 0; i < BLOCK_SIZE; i++)
out_data[i] = reverse_Pi[in_data[i]];
}
static void
GOST_Kuz_X(const uint8_t *a, const uint8_t *b, uint8_t *c)
{
int i;
for (i = 0; i < BLOCK_SIZE; i++)
c[i] = a[i] ^ b[i];
}
static uint8_t
GOST_Kuz_GF_mul(uint8_t a, uint8_t b)
{
uint8_t c = 0;
uint8_t hi_bit;
int i;
for (i = 0; i < 8; i++)
{
if (b & 1)
c ^= a;
hi_bit = a & 0x80;
a <<= 1;
if (hi_bit)
a ^= 0xc3; //полином x^8+x^7+x^6+x+1
b >>= 1;
}
return c;
}
static void
GOST_Kuz_R(uint8_t *state)
{
int i;
uint8_t a_15 = 0;
vect internal;
for (i = 15; i >= 0; i--)
{
if (i - 1 >= 0)
internal[i - 1] = state[i];
a_15 ^= GOST_Kuz_GF_mul(state[i], l_vec[i]);
}
internal[15] = a_15;
memcpy(state, internal, BLOCK_SIZE);
}
static void
GOST_Kuz_reverse_R(uint8_t *state)
{
int i;
uint8_t a_0;
a_0 = state[15];
vect internal;
for (i = 1; i < 16; i++)
{
internal[i] = state[i - 1];
a_0 ^= GOST_Kuz_GF_mul(internal[i], l_vec[i]);
}
internal[0] = a_0;
memcpy(state, internal, BLOCK_SIZE);
}
static void
GOST_Kuz_L(const uint8_t *in_data, uint8_t *out_data)
{
int i;
vect internal;
memcpy(internal, in_data, BLOCK_SIZE);
for (i = 0; i < 16; i++)
GOST_Kuz_R(internal);
memcpy(out_data, internal, BLOCK_SIZE);
}
static void
GOST_Kuz_reverse_L(const uint8_t *in_data, uint8_t *out_data)
{
int i;
vect internal;
memcpy(internal, in_data, BLOCK_SIZE);
for (i = 0; i < 16; i++)
GOST_Kuz_reverse_R(internal);
memcpy(out_data, internal, BLOCK_SIZE);
}
static void
GOST_Kuz_Get_C()
{
int i;
vect iter_num[32];
for (i = 0; i < 32; i++)
{
memset(iter_num[i], 0, BLOCK_SIZE);
iter_num[i][0] = i+1;
}
for (i = 0; i < 32; i++)
GOST_Kuz_L(iter_num[i], iter_C[i]);
}
static void
GOST_Kuz_F(const uint8_t *in_key_1, const uint8_t *in_key_2,
uint8_t *out_key_1, uint8_t *out_key_2,
uint8_t *iter_const)
{
vect internal;
memcpy(out_key_2, in_key_1, BLOCK_SIZE);
GOST_Kuz_X(in_key_1, iter_const, internal);
GOST_Kuz_S(internal, internal);
GOST_Kuz_L(internal, internal);
GOST_Kuz_X(internal, in_key_2, out_key_1);
}
void
GOST_Kuz_Expand_Key(const uint8_t *key)
{
int i;
uint8_t key_1[KEY_SIZE/2];
uint8_t key_2[KEY_SIZE/2];
uint8_t iter_1[KEY_SIZE/2];
uint8_t iter_2[KEY_SIZE/2];
uint8_t iter_3[KEY_SIZE/2];
uint8_t iter_4[KEY_SIZE/2];
memcpy(key_1, key + KEY_SIZE/2, KEY_SIZE/2);
memcpy(key_2, key, KEY_SIZE/2);
GOST_Kuz_Get_C();
memcpy(iter_key[0], key_1, KEY_SIZE/2);
memcpy(iter_key[1], key_2, KEY_SIZE/2);
memcpy(iter_1, key_1, KEY_SIZE/2);
memcpy(iter_2, key_2, KEY_SIZE/2);
for (i = 0; i < 4; i++)
{
GOST_Kuz_F(iter_1, iter_2, iter_3, iter_4, iter_C[0 + 8 * i]);
GOST_Kuz_F(iter_3, iter_4, iter_1, iter_2, iter_C[1 + 8 * i]);
GOST_Kuz_F(iter_1, iter_2, iter_3, iter_4, iter_C[2 + 8 * i]);
GOST_Kuz_F(iter_3, iter_4, iter_1, iter_2, iter_C[3 + 8 * i]);
GOST_Kuz_F(iter_1, iter_2, iter_3, iter_4, iter_C[4 + 8 * i]);
GOST_Kuz_F(iter_3, iter_4, iter_1, iter_2, iter_C[5 + 8 * i]);
GOST_Kuz_F(iter_1, iter_2, iter_3, iter_4, iter_C[6 + 8 * i]);
GOST_Kuz_F(iter_3, iter_4, iter_1, iter_2, iter_C[7 + 8 * i]);
memcpy(iter_key[2 * i + 2], iter_1, KEY_SIZE/2);
memcpy(iter_key[2 * i + 3], iter_2, KEY_SIZE/2);
}
#ifdef DEBUG_MODE
printf("Iteration cipher keys:\n");
for (i = 0; i < 10; i++)
GOST_Kuz_PrintDebug(iter_key[i]);
#endif
}
void
GOST_Kuz_Encrypt(const uint8_t *blk, uint8_t *out_blk)
{
int i;
memcpy(out_blk, blk, BLOCK_SIZE);
#ifdef DEBUG_MODE
printf("Text:\n");
GOST_Kuz_PrintDebug(out_blk);
#endif
for(i = 0; i < 9; i++)
{
GOST_Kuz_X(iter_key[i], out_blk, out_blk);
GOST_Kuz_S(out_blk, out_blk);
GOST_Kuz_L(out_blk, out_blk);
}
GOST_Kuz_X(out_blk, iter_key[9], out_blk);
#ifdef DEBUG_MODE
printf("Encrypted text:\n");
GOST_Kuz_PrintDebug(out_blk);
#endif
}
void
GOST_Kuz_Decrypt(const uint8_t *blk, uint8_t *out_blk)
{
int i;
memcpy(out_blk, blk, BLOCK_SIZE);
#ifdef DEBUG_MODE
printf("Gipher text:\n");
GOST_Kuz_PrintDebug(out_blk);
#endif
GOST_Kuz_X(out_blk, iter_key[9], out_blk);
for(i = 8; i >= 0; i--)
{
GOST_Kuz_reverse_L(out_blk, out_blk);
GOST_Kuz_reverse_S(out_blk, out_blk);
GOST_Kuz_X(iter_key[i], out_blk, out_blk);
}
#ifdef DEBUG_MODE
printf("Decrypted text:\n");
GOST_Kuz_PrintDebug(out_blk);
#endif
}