-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
211 lines (167 loc) · 6.96 KB
/
main.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
#include <stdio.h>
#include <stdint.h>
#define LSHIFT_nBIT(x, L, N) (((x << L) | (x >> (-L & (N - 1)))) & (((uint64_t)1 << N) - 1))
#define BUFF_SIZE 1024
size_t GOST_28147(uint8_t * to, uint8_t mode, uint8_t * key256b, uint8_t * from, size_t length);
void feistel_cipher(uint8_t mode, uint32_t * block32b_1, uint32_t * block32b_2, uint32_t * keys32b);
void round_of_feistel_cipher(uint32_t * block32b_1, uint32_t * block32b_2, uint32_t * keys32b, uint8_t round);
uint32_t substitution_table(uint32_t block32b, uint8_t sbox_row);
void substitution_table_by_4bits(uint8_t * blocks4b, uint8_t sbox_row);
void split_256bits_to_32bits(uint8_t * key256b, uint32_t * keys32b);
void split_64bits_to_32bits(uint64_t block64b, uint32_t * block32b_1, uint32_t * block32b_2);
void split_64bits_to_8bits(uint64_t block64b, uint8_t * blocks8b);
void split_32bits_to_8bits(uint32_t block32b, uint8_t * blocks4b);
uint64_t join_32bits_to_64bits(uint32_t block32b_1, uint32_t block32b_2);
uint64_t join_8bits_to_64bits(uint8_t * blocks8b);
uint32_t join_4bits_to_32bits(uint8_t * blocks4b);
static inline void print_array(uint8_t * array, size_t length);
static inline void print_bits(uint64_t x, register uint64_t Nbit);
// 1 | 4 -> 0xC
static const uint8_t Sbox[8][16] = {
{0xF, 0xC, 0x2, 0xA, 0x6, 0x4, 0x5, 0x0, 0x7, 0x9, 0xE, 0xD, 0x1, 0xB, 0x8, 0x3},
{0xB, 0x6, 0x3, 0x4, 0xC, 0xF, 0xE, 0x2, 0x7, 0xD, 0x8, 0x0, 0x5, 0xA, 0x9, 0x1},
{0x1, 0xC, 0xB, 0x0, 0xF, 0xE, 0x6, 0x5, 0xA, 0xD, 0x4, 0x8, 0x9, 0x3, 0x7, 0x2},
{0x1, 0x5, 0xE, 0xC, 0xA, 0x7, 0x0, 0xD, 0x6, 0x2, 0xB, 0x4, 0x9, 0x3, 0xF, 0x8},
{0x0, 0xC, 0x8, 0x9, 0xD, 0x2, 0xA, 0xB, 0x7, 0x3, 0x6, 0x5, 0x4, 0xE, 0xF, 0x1},
{0x8, 0x0, 0xF, 0x3, 0x2, 0x5, 0xE, 0xB, 0x1, 0xA, 0x4, 0x7, 0xC, 0x9, 0xD, 0x6},
{0x3, 0x0, 0x6, 0xF, 0x1, 0xE, 0x9, 0x2, 0xD, 0x8, 0xC, 0x4, 0xB, 0xA, 0x5, 0x7},
{0x1, 0xA, 0x6, 0x8, 0xF, 0xB, 0x0, 0x4, 0xC, 0x3, 0x5, 0x9, 0x7, 0xD, 0x2, 0xE},
};
int main(void) {
uint8_t encrypted[BUFF_SIZE], decrypted[BUFF_SIZE];
uint8_t key256b[32] = "this_is_a_pasw_for_GOST_28147_89";
uint8_t buffer[BUFF_SIZE], ch;
size_t position;
while ((ch = getchar()) != '\n' && position < BUFF_SIZE - 1)
buffer[position++] = ch;
buffer[position] = '\0';
printf("Open message:\n");
print_array(buffer, position);
printf("%s\n", buffer);
putchar('\n');
position = GOST_28147(encrypted, 'E', key256b, buffer, position);
printf("Encrypted message:\n");
print_array(encrypted, position);
printf("%s\n", encrypted);
putchar('\n');
printf("Decrypted message:\n");
position = GOST_28147(decrypted, 'D', key256b, encrypted, position);
print_array(decrypted, position);
printf("%s\n", decrypted);
putchar('\n');
return 0;
}
size_t GOST_28147(uint8_t * to, uint8_t mode, uint8_t * key256b, uint8_t * from, size_t length) {
length = length % 8 == 0 ? length : length + (8 - (length % 8));
uint32_t N1, N2, keys32b[8];
split_256bits_to_32bits(key256b, keys32b);
for (size_t i = 0; i < length; i += 8) {
split_64bits_to_32bits(
join_8bits_to_64bits(from + i),
&N1, &N2
);
feistel_cipher(mode, &N1, &N2, keys32b);
split_64bits_to_8bits(
join_32bits_to_64bits(N1, N2),
(to + i)
);
}
return length;
}
void feistel_cipher(uint8_t mode, uint32_t * block32b_1, uint32_t * block32b_2, uint32_t * keys32b) {
switch (mode) {
case 'E': case 'e': {
for (uint8_t round = 0; round < 24; ++round)
round_of_feistel_cipher(block32b_1, block32b_2, keys32b, round);
for (uint8_t round = 31; round >= 24; --round)
round_of_feistel_cipher(block32b_1, block32b_2, keys32b, round);
break;
}
case 'D': case 'd': {
for (uint8_t round = 0; round < 8; ++round)
round_of_feistel_cipher(block32b_1, block32b_2, keys32b, round);
for (uint8_t round = 31; round >= 8; --round)
round_of_feistel_cipher(block32b_1, block32b_2, keys32b, round);
break;
}
}
}
void round_of_feistel_cipher(uint32_t * block32b_1, uint32_t * block32b_2, uint32_t * keys32b, uint8_t round) {
uint32_t result_of_iter, temp;
result_of_iter = (*block32b_1 + keys32b[round % 8]) % UINT32_MAX;
result_of_iter = substitution_table(result_of_iter, round % 8);
result_of_iter = (uint32_t)LSHIFT_nBIT(result_of_iter, 11, 32);
temp = *block32b_1;
*block32b_1 = result_of_iter ^ *block32b_2;
*block32b_2 = temp;
}
uint32_t substitution_table(uint32_t block32b, uint8_t sbox_row) {
uint8_t blocks4bits[4];
split_32bits_to_8bits(block32b, blocks4bits);
substitution_table_by_4bits(blocks4bits, sbox_row);
return join_4bits_to_32bits(blocks4bits);
}
void substitution_table_by_4bits(uint8_t * blocks4b, uint8_t sbox_row) {
uint8_t block4b_1, block4b_2;
for (uint8_t i = 0; i < 4; ++i) {
block4b_1 = Sbox[sbox_row][blocks4b[i] & 0x0F];
block4b_2 = Sbox[sbox_row][blocks4b[i] >> 4];
blocks4b[i] = block4b_2;
blocks4b[i] = (blocks4b[i] << 4) | block4b_1;
}
}
void split_256bits_to_32bits(uint8_t * key256b, uint32_t * keys32b) {
uint8_t *p8 = key256b;
for (uint32_t *p32 = keys32b; p32 < keys32b + 8; ++p32) {
for (uint8_t i = 0; i < 4; ++i) {
*p32 = (*p32 << 8) | *(p8 + i);
}
p8 += 4;
}
}
void split_64bits_to_32bits(uint64_t block64b, uint32_t * block32b_1, uint32_t * block32b_2) {
*block32b_2 = (uint32_t)(block64b);
*block32b_1 = (uint32_t)(block64b >> 32);
}
void split_64bits_to_8bits(uint64_t block64b, uint8_t * blocks8b) {
for (size_t i = 0; i < 8; ++i) {
blocks8b[i] = (uint8_t)(block64b >> ((7 - i) * 8));
}
}
void split_32bits_to_8bits(uint32_t block32b, uint8_t * blocks8b) {
for (uint8_t i = 0; i < 4; ++i) {
blocks8b[i] = (uint8_t)(block32b >> (24 - (i * 8)));
}
}
uint64_t join_32bits_to_64bits(uint32_t block32b_1, uint32_t block32b_2) {
uint64_t block64b;
block64b = block32b_2;
block64b = (block64b << 32) | block32b_1;
return block64b;
}
uint64_t join_8bits_to_64bits(uint8_t * blocks8b) {
uint64_t block64b;
// block64b = 0000000000000000000000000000000000000000000000000000000000000000
for (uint8_t *p = blocks8b; p < blocks8b + 8; ++p) {
block64b = (block64b << 8) | *p;
}
return block64b;
}
uint32_t join_4bits_to_32bits(uint8_t * blocks4b) {
uint32_t block32b;
for (uint8_t i = 0; i < 4; ++i) {
block32b = (block32b << 8) | blocks4b[i];
}
return block32b;
}
static inline void print_array(uint8_t * array, size_t length) {
printf("[ ");
for (size_t i = 0; i < length; ++i)
printf("%d ", array[i]);
printf("]\n");
}
static inline void print_bits(uint64_t x, register uint64_t Nbit) {
for (Nbit = (uint64_t)1 << (Nbit - 1); Nbit > 0x00; Nbit >>= 1)
printf("%d", (x & Nbit) ? 1 : 0);
putchar('\n');
}