-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuznechik.cpp
365 lines (316 loc) · 11.2 KB
/
kuznechik.cpp
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
#include "kuznechik.h"
void encrypt_file( const char* input_file_name, const char* output_file_name, const char* key_1, const char* key_2)
{
kuznechik encryptor( input_file_name, block( key_1), block( key_2));
encryptor.encrypt_data( output_file_name);
}
void encrypt_file( const char* input_file_name, const char* output_file_name, const char* hexadecimal_key)
{
kuznechik encryptor( input_file_name, hexadecimal_key);
encryptor.encrypt_data( output_file_name);
}
void decrypt_file( const char* input_file_name, const char* output_file_name, const char* key_1, const char* key_2)
{
kuznechik encryptor( input_file_name, block( key_1), block( key_2));
encryptor.decrypt_data( output_file_name);
}
void decrypt_file( const char* input_file_name, const char* output_file_name, const char* hexadecimal_key)
{
kuznechik encryptor( input_file_name, hexadecimal_key);
encryptor.decrypt_data( output_file_name);
}
std::string hex_to_string ( const std::string input_string)
{
std::string output_string;
for ( int i = 0; i < input_string.length(); i += 2)
{
const char* p = std::lower_bound( hex_symbol_table, hex_symbol_table + 16, input_string[i]);
const char* q = std::lower_bound( hex_symbol_table, hex_symbol_table + 16, input_string[i + 1]);
output_string.push_back((( p - hex_symbol_table) << 4) | ( q - hex_symbol_table));
}
return output_string;
}
std::string string_to_hex ( const std::string input_string)
{
std::string output_string;
for ( int i = 0; i < input_string.length(); i ++)
output_string += char_to_hex_string( input_string[i]);
return output_string;
}
std::string char_to_hex_string( char c)
{
std::string hex = "0123456789abcdef";
std::string hex_str;
hex_str += hex[int(int( c)/16)];
hex_str += hex[int( c) % 16];
return hex_str;
}
void kuznechik::encrypt_data( const char* output_file_name, bool use_hex)
{
double start;
double end;
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp for
for ( int i = 0; i < data.size(); i++)
data[i] = encrypt_block( data[i]);
}
end = omp_get_wtime();
std::cout << "Encryption time: " << end - start << std::endl;
write_to_file( output_file_name, use_hex);
}
void kuznechik::decrypt_data( const char* output_file_name, bool use_hex)
{
// omp_set_num_threads(OMP_NUM_THREADS);
double start;
double end;
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp for
for ( int i = 0; i < data.size(); i++)
data[i] = decrypt_block( data[i]);
}
end = omp_get_wtime();
std::cout << "Decryption time: " << end - start << std::endl;
write_to_file( output_file_name, use_hex);
}
kuznechik::kuznechik( const char* file_name, const block key_1, const block key_2)
{
iteration_keys.resize( number_of_iteration_keys);
read_file_to_data_buffer( file_name);
calculate_iteration_constants();
generate_iteraion_keys( key_1, key_2);
}
kuznechik::kuznechik( const char* file_name, const char* hexadecimal_key)
{
assert( strlen( hexadecimal_key) == 64 && "Wrong key");
iteration_keys.resize( number_of_iteration_keys);
read_file_to_data_buffer( file_name, true);
calculate_iteration_constants();
std::string ascii_key_pair = hex_to_string( hexadecimal_key);
generate_iteraion_keys( ascii_key_pair.substr( 0, 15), ascii_key_pair.substr( 16));
}
void kuznechik::read_file_to_data_buffer( const char* file_name, bool is_hex)
{
std::ifstream input_file_stream( file_name);
assert( input_file_stream && "Can't find file");
std::string file_content( ( std::istreambuf_iterator<char>( input_file_stream)), std::istreambuf_iterator<char>());
if (is_hex == true)
file_content = hex_to_string( file_content);
int length_of_the_trailing_string = file_content.length() % block::size;
for ( int i = 0; i + block::size <= file_content.length(); i += block::size)
data.push_back( block( file_content.substr( i, block::size)));
if (length_of_the_trailing_string != 0)
{
std::string trailing_content = file_content.substr( file_content.length() - length_of_the_trailing_string);
for ( int i = 0; i < block::size - length_of_the_trailing_string; i++)
trailing_content.push_back( ' ');
data.push_back( block( trailing_content));
}
}
void kuznechik::calculate_iteration_constants()
{
// 123456789012345 15 zeros
std::string zero_string( "000000000000000");
for( int i = 0; i < 32; i++)
{
std::string iterational_string;
iterational_string.push_back( (char)i);
iterational_string += zero_string;
iteration_constants.push_back( L( block( iterational_string)));
}
}
unsigned char kuznechik::get_mask_value( int index) const
{
assert( index >= 0 && index < block::size && "Wrong index value");
return mask[index];
}
unsigned char kuznechik::get_substituted_value( int index) const
{
assert( index >= 0 && index <= UCHAR_MAX && "Wrong index value");
return substitution_table[index];
}
unsigned char kuznechik::get_reversed_substituted_value( int index) const
{
assert( index >= 0 && index <= UCHAR_MAX && "Wrong index value");
return substitution_table_reversed[index];
}
block kuznechik::get_iteration_constant( int index) const
{
assert( index >= 0 && index < 2 * block::size && "Wrong index value");
return iteration_constants.at( index);
}
block kuznechik::get_iteration_key( int index) const
{
assert( index >= 0 && index < number_of_iteration_keys && "Wrong index value");
return iteration_keys[index];
}
void kuznechik::set_iteration_key( int index, const block value)
{
assert( index >= 0 && index < number_of_iteration_keys && "Wrong index value");
iteration_keys[index] = value;
}
block kuznechik::S( const block input_block)
{
std::vector<unsigned char> transformed_data;
for ( int i = 0 ; i < block::size; i++)
transformed_data.push_back( get_substituted_value( input_block[i]));
return block( transformed_data);
}
block kuznechik::S_reversed( const block input_block)
{
std::vector<unsigned char> transformed_data;
for ( int i = 0 ; i < block::size; i++)
transformed_data.push_back( get_reversed_substituted_value( input_block[i]));
return block( transformed_data);
}
unsigned char kuznechik::GF_mul( unsigned char a, unsigned char b)
{
unsigned char c = 0;
for ( int i = 0; i < 8; i++)
{
if ( ( b & 1) == 1)
c ^= a;
unsigned char hi_bit = (char)( a & 0x80);
a <<= 1;
if( hi_bit == 0)
a ^= 0xC3;
b >>= 1;
}
return c;
}
block kuznechik::R( const block input_block)
{
std::vector<unsigned char> transformed_data( block::size);
unsigned char trailing_symbol = 0;
for ( int i = block::size - 1; i >= 0; i--)
{
if( i == 0)
transformed_data[block::size] = input_block[i];
else
transformed_data[i-1] = input_block[i];
trailing_symbol ^= GF_mul( input_block[i], get_mask_value(i));
}
transformed_data[block::size - 1] = trailing_symbol;
return block( transformed_data);
}
block kuznechik::R_reversed( const block input_block)
{
std::vector<unsigned char> transformed_data( block::size);
unsigned char leading_symbol = input_block[block::size - 1];
for ( int i = 1; i < block::size; i++)
{
transformed_data[i] = input_block[i - 1];
leading_symbol ^= GF_mul( transformed_data[i], get_mask_value(i));
}
transformed_data[0] = leading_symbol;
return block( transformed_data);
}
block kuznechik::L( const block input_block)
{
block transformed_block = input_block;
for ( int i = 0; i < block::size; i++)
transformed_block = R( transformed_block);
return transformed_block;
}
block kuznechik::L_reversed( const block input_block)
{
block transformed_block = input_block;
for ( int i = 0; i < block::size; i++)
transformed_block = R_reversed( transformed_block);
return transformed_block;
}
key_pair kuznechik::F( const key_pair input_key_pair, const block iteraion_constant)
{
block returned_key_1;
block returned_key_2 = input_key_pair.key_1;
returned_key_1 = L( S( input_key_pair.key_2 ^ iteraion_constant)) ^ returned_key_2;
return key_pair( returned_key_1, returned_key_2);
}
void kuznechik::generate_iteraion_keys( block key_1, block key_2)
{
iteration_keys[0] = key_1;
iteration_keys[1] = key_2;
key_pair key_pair_1_2( key_1, key_2);
key_pair key_pair_3_4;
for( int i = 0; i < 4; i++)
{
key_pair_3_4 = F( key_pair_1_2, get_iteration_constant(0 + 8 * i));
key_pair_1_2 = F( key_pair_3_4, get_iteration_constant(1 + 8 * i));
key_pair_3_4 = F( key_pair_1_2, get_iteration_constant(2 + 8 * i));
key_pair_1_2 = F( key_pair_3_4, get_iteration_constant(3 + 8 * i));
key_pair_3_4 = F( key_pair_1_2, get_iteration_constant(4 + 8 * i));
key_pair_1_2 = F( key_pair_3_4, get_iteration_constant(5 + 8 * i));
key_pair_3_4 = F( key_pair_1_2, get_iteration_constant(6 + 8 * i));
key_pair_1_2 = F( key_pair_3_4, get_iteration_constant(7 + 8 * i));
set_iteration_key( 2 * i + 2, key_pair_1_2.key_1);
set_iteration_key( 2 * i + 3, key_pair_1_2.key_2);
}
}
block kuznechik::encrypt_block( const block input_block)
{
block returned_block = input_block;
for( int i = 0; i < 9; i++)
{
returned_block = get_iteration_key( i) ^ returned_block;
returned_block = S( returned_block);
returned_block = L( returned_block);
}
returned_block = returned_block ^ get_iteration_key( 9);
return returned_block;
}
block kuznechik::decrypt_block( const block input_block)
{
block returned_block = input_block ^ get_iteration_key( 9);
for( int i = 8; i >= 0; i--)
{
returned_block = L_reversed( returned_block);
returned_block = S_reversed( returned_block);
returned_block = get_iteration_key(i) ^ returned_block;
}
return returned_block;
}
void kuznechik::write_to_file( const char* output_file, bool use_hex)
{
std::ofstream output_stream;
output_stream.open( output_file);
assert( output_stream.is_open() && "Can't open file");
for ( block i : data)
if ( use_hex == true)
output_stream << hex_to_string( std::string( i.get_data().begin(), i.get_data().end()));
else
output_stream << std::string( i.get_data().begin(), i.get_data().end());
}
/////////////////////////
block::block( std::vector<unsigned char> input_string) : data( input_string) { assert ( input_string.size() == size); };
block::block() { data.resize( size); }
block::block( std::string input_string)
{
assert( input_string.length() == size && "Wrong length of the block");
for ( int i = 0; i < size; i++)
data.push_back( input_string[i]);
}
unsigned char block::operator[] ( const int index) const
{
assert ( index < size && "given index causes overflow");
return data[index];
}
block operator ^ ( const block& a, const block& b)
{
block result;
for( int i = 0; i < result.size; i++)
result.data[i] = b[i]^a[i];
return result;
}
std::ostream& operator << ( std::ostream& os, const block& b)
{
return os << b.data << std::endl;
}
void block::print()
{
for( int i : data)
std::cout << (unsigned char)i;
std::cout << std::endl;
}