Алгоритм симметричного шифрования TwoFish
12.12.2011, 16:17. Показов 17512. Ответов 20
При реализации алгоритма возникли сложности. Сначала работа шла живо и весело, запрограммировал расширение ключа, шифрование... Но когда дело дошло до дешифровки получилась какая-то ерунда. Сообщение не расшифровывается. Как ни бился не могу понять в чем проблема.
Помогите, люди добрые! Вдруг свежий глаз заметит какую-нибудь оплошность или ошибку в алгоритме... Буду рад любой помощи!
TwoFish.h:
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
| #ifndef __TWO_FISH_H_
#define __TWO_FISH_H_
#include <iostream>
#include <iomanip>
using namespace std;
#ifndef UINT
typedef unsigned int UINT;
#endif
#ifndef BYTE
typedef unsigned char BYTE;
#endif
class TwoFish{
public:
TwoFish(char *key, size_t length);
TwoFish(BYTE *key, size_t length);
BYTE* encrypt(BYTE *plain);
BYTE* decrypt(BYTE *cipher);
void printSubkeys();
~TwoFish();
private:
void keySchedule(BYTE *key, size_t length);
unsigned long long h(UINT inputWord, UINT* inputWordArray, short arraySize);
BYTE q(BYTE x, int qt);
UINT keys[40];
short k;
UINT *SBox;
};
#endif |
|
TwoFish.cpp:
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
| #include "TwoFish.h"
/********************************Constructor*********************************/
TwoFish::TwoFish(char *key, size_t length){
BYTE *u_key = new BYTE[length];
for (int i=0;i < length; i++){
u_key[i] = (BYTE) key[i];
}
keySchedule(u_key, length);
}
TwoFish::TwoFish(BYTE *key, size_t length){
keySchedule(key, length);
}
/*********************************Destructor*********************************/
TwoFish::~TwoFish(){
delete [] SBox;
}
/*********************************Interface**********************************/
BYTE* TwoFish::encrypt(BYTE *plain){
UINT
A = (plain[0] << 24) + (plain[1] << 16) + (plain[2] << 8) + plain[3],
B = (plain[4] << 24) + (plain[5] << 16) + (plain[6] << 8) + plain[7],
C = (plain[8] << 24) + (plain[9] << 16) + (plain[10] << 8) + plain[11],
D = (plain[12] << 24) + (plain[13] << 16) + (plain[14] << 8) + plain[15];
//whitening
A ^= keys[0];
B ^= keys[1];
C ^= keys[2];
D ^= keys[3];
//sixteen roudns
for(int i = 0; i < 16; i++){
unsigned long long tA = h(A, SBox, k);
unsigned long long tB = h((B << 8), SBox, k);
D <<= 1;
C ^= ((tA + tB + keys[2 * i + 8]) & 0xFFFFFFFF);
D ^= ((tA + 2*tB + keys[2 * i + 9]) & 0xFFFFFFFF);
C >>= 1;
//swap until last round
if (i != 15) {
UINT tmp = C;
C = A;
A = tmp;
tmp = D;
D = B;
B = tmp;
}
}
//whitening
A ^= keys[4];
B ^= keys[5];
C ^= keys[6];
D ^= keys[7];
plain[0] = (A >> 24) & 0xFF;
plain[1] = (A >> 16) & 0xFF;
plain[2] = (A >> 8) & 0xFF;
plain[3] = A & 0xFF;
plain[4] = (B >> 24) & 0xFF;
plain[5] = (B >> 16) & 0xFF;
plain[6] = (B >> 8) & 0xFF;
plain[7] = B & 0xFF;
plain[8] = (C >> 24) & 0xFF;
plain[9] = (C >> 16) & 0xFF;
plain[10] = (C >> 8) & 0xFF;
plain[11] = C & 0xFF;
plain[12] = (D >> 24) & 0xFF;
plain[13] = (D >> 16) & 0xFF;
plain[14] = (D >> 8) & 0xFF;
plain[15] = D & 0xFF;
return plain;
}
BYTE* TwoFish::decrypt(BYTE *cipher){
UINT
A = (cipher[0] << 24) + (cipher[1] << 16) + (cipher[2] << 8) + cipher[3],
B = (cipher[4] << 24) + (cipher[5] << 16) + (cipher[6] << 8) + cipher[7],
C = (cipher[8] << 24) + (cipher[9] << 16) + (cipher[10] << 8) + cipher[11],
D = (cipher[12] << 24) + (cipher[13] << 16) + (cipher[14] << 8) + cipher[15];
//whitening
A ^= keys[4];
B ^= keys[5];
C ^= keys[6];
D ^= keys[7];
//sixteen roudns
for(int i = 16 - 1; i >= 0; i--){
unsigned long long tA = h(A, SBox, k);
unsigned long long tB = h((B << 8), SBox, k);
C <<= 1;
C ^= ((tA + tB + keys[2 * i + 8]) & 0xFFFFFFFF);
D ^= ((tA + 2*tB + keys[2 * i + 9]) & 0xFFFFFFFF);
D >>= 1;
//swap until last round
if (i > 0) {
UINT tmp = C;
C = A;
A = tmp;
tmp = D;
D = B;
B = tmp;
}
}
//whitening
A ^= keys[0];
B ^= keys[1];
C ^= keys[2];
D ^= keys[3];
cipher[0] = (A >> 24) & 0xFF;
cipher[1] = (A >> 16) & 0xFF;
cipher[2] = (A >> 8) & 0xFF;
cipher[3] = A & 0xFF;
cipher[4] = (B >> 24) & 0xFF;
cipher[5] = (B >> 16) & 0xFF;
cipher[6] = (B >> 8) & 0xFF;
cipher[7] = B & 0xFF;
cipher[8] = (C >> 24) & 0xFF;
cipher[9] = (C >> 16) & 0xFF;
cipher[10] = (C >> 8) & 0xFF;
cipher[11] = C & 0xFF;
cipher[12] = (D >> 24) & 0xFF;
cipher[13] = (D >> 16) & 0xFF;
cipher[14] = (D >> 8) & 0xFF;
cipher[15] = D & 0xFF;
return cipher;
}
/*******************************System methods*******************************/
void TwoFish::keySchedule(BYTE *user_key, size_t length){
short N = 0; //size of key
if ( length > 192 ) {
N = 256;
} else if ((length > 128) && (length <=192)) {
N = 192;
} else N = 128;
char *temp_key = new char[N];
//Filling with 0
for(size_t i = 0; i < N; i++){
temp_key[i] = ( i < length ) ? user_key[i] : 0;
}
//Initialization of variables
k = N / 64;
BYTE RS[4][8] = {
{0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E},
{0xA4, 0x56, 0x82, 0xF3, 0x1E, 0xC6, 0x68, 0xE5},
{0x02, 0xA1, 0xFC, 0xC1, 0x47, 0xAE, 0x3D, 0x19},
{0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E, 0x03}
};
UINT *Me = new UINT[k]; //even 32bit words
UINT *Mo = new UINT[k]; // odd 32bit words
for(int c1 = 0, c2 = 0, i = 0; i < 2 * k; i++){
if (i % 2 == 0) {
Me[c1] = 0;
for (int j = 4*i, shift = 3 ; j < 4*(i+1); j++, shift--){
Me[c1] += (temp_key[j] << (shift * 8));
}
c1++;
} else {
Mo[c2] = 0;
for (int j = 4*i, shift = 3 ; j < 4*(i+1); j++, shift--){
Mo[c2] = temp_key[j] << (shift * 8);
}
c2++;
}
}
SBox = new UINT[k];
for( int i = 0; i < k; i++){
SBox[k - 1 - i] = 0;
for (int j = 0; j < 4; j++){
UINT v = 0;
for (int t = 0; t < 8; t++){
v += RS[j][t] * temp_key[ 8*i + t ];
}
SBox[k - 1 - i] += ( v * (1 << (8 * j)));
}
}
//generating 32bit keys
UINT ro = (1 << 24) + (1 << 16) + (1 << 8) + 1;
for(int i = 0; i < 20; i++){
unsigned long long A = h( 2 * i * ro, Me, k);
unsigned long long B = h( (2 * i + 1) * ro, Mo, k);
B <<=8;
keys[2 * i] = (A + B) & 0xFFFFFFFF;
keys[2 * i + 1] = ((A + 2 * B) & 0xFFFFFFFF) << 9;
}
}
unsigned long long TwoFish::h(UINT inputWord, UINT *inputWordArray, short ArraySize){
BYTE x[4]; //splitted input word
x[0] = (inputWord >> 24) & 0xFF;
x[1] = (inputWord >> 16) & 0xFF;
x[2] = (inputWord >> 8) & 0xFF;
x[3] = inputWord & 0xFF;
BYTE **l = new BYTE*[ArraySize]; //splitted words of input array
for (int i = 0; i < ArraySize; i++) {
l[i] = new BYTE[4];
l[i][0] = (inputWordArray[i] >> 24) && 0xFF;
l[i][1] = (inputWordArray[i] >> 16) && 0xFF;
l[i][2] = (inputWordArray[i] >> 8) && 0xFF;
l[i][3] = inputWordArray[i] && 0xFF;
}
BYTE y[4] = {0};
if (ArraySize == 4) { // in case length of key is 256
y[0] = q(x[0], 1) ^ l[3][0];
y[1] = q(x[1], 0) ^ l[3][1];
y[2] = q(x[2], 0) ^ l[3][2];
y[3] = q(x[3], 1) ^ l[3][3];
} else {
y[0] = x[0];
y[1] = x[1];
y[2] = x[2];
y[3] = x[3];
}
if (ArraySize >= 3) { // in case length of key is 192 or 256
y[0] = q(y[0], 1) ^ l[2][0];
y[1] = q(y[1], 0) ^ l[2][1];
y[2] = q(y[2], 0) ^ l[2][2];
y[3] = q(y[3], 1) ^ l[2][3];
} else {
y[0] = x[0];
y[1] = x[1];
y[2] = x[2];
y[3] = x[3];
}
y[0] = q((q((q(y[0], 0) ^ l[1][0]), 0) ^ l[0][0]), 1);
y[1] = q((q((q(y[1], 1) ^ l[1][1]), 0) ^ l[0][1]), 0);
y[2] = q((q((q(y[2], 0) ^ l[1][2]), 1) ^ l[0][2]), 1);
y[3] = q((q((q(y[3], 1) ^ l[1][3]), 1) ^ l[0][3]), 0);
BYTE MDS[4][4] = {
{0x01, 0xEF, 0x5B, 0x5B},
{0x5B, 0xEF, 0xEF, 0x01},
{0xEF, 0x5B, 0x01, 0xEF},
{0xEF, 0x01, 0xEF, 0x5B}
};
unsigned long long H = 0;
for (int j = 0; j < 4; j++){
unsigned long long v = 0;
for (int t = 0; t < 4; t++){
v += MDS[j][t] * y[t];
}
H += v * (1 << (8 * j));;
}
return H;
}
BYTE TwoFish::q(BYTE x, int op){
assert( (op == 0) || (op == 1));
const BYTE qt[2][4][16] = {
//permutation table for q0 operation
{
{ 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
{ 0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5, 0xF, 0x4, 0xA, 0x6, 0x7, 0x0, 0x9, 0xD },
{ 0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0, 0xC, 0x8, 0xF, 0x3, 0x2, 0x4, 0x7, 0x1 },
{ 0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE, 0x9, 0xB, 0x3, 0x0, 0x8, 0x5, 0xC, 0xA }
},
//permutation table for q1 operation
{
{ 0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE, 0x3, 0x1, 0x9, 0x4, 0x0, 0xA, 0xC, 0x5 },
{ 0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7, 0x6, 0xD, 0xA, 0x5, 0xF, 0x9, 0x0, 0x8 },
{ 0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA, 0x0, 0xE, 0xD, 0x8, 0x2, 0xB, 0x3, 0xF },
{ 0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE, 0x6, 0x4, 0x7, 0xF, 0x2, 0x0, 0x8, 0xA }
},
};
//splitting byte into two nibbles
BYTE a0 = x / 16;
BYTE b0 = x % 16;
BYTE a1 = a0 ^ b0;
BYTE b1 = a0 ^ (b0 >> 1) ^ ((8 * a0) % 16);
BYTE a2 = qt[op][0][a1];
BYTE b2 = qt[op][1][b1];
BYTE a3 = a2 ^ b2;
BYTE b3 = a2 ^ (b2 >> 1) ^ ((8 * a2) % 16);
BYTE a4 = qt[op][2][a3];
BYTE b4 = qt[op][3][b3];
return (16*b4 + a4);
}
void TwoFish::printSubkeys(){
for(int i=0; i< 40; i++){
cout<<hex<<keys[i]<<endl;
}
} |
|
P.S. Так же хотелось бы узнать советы по поводу улучшения программы в общем
Добавлено через 14 часов 15 минут
Сегодня на свежую голову посмотрел еще раз алгоритм и почувствовал себя дибилом - в некоторых операциях алгоритма я использовал логический битовый сдвиг, когда нужно было использовать циклический сдвиг...
В общем прошу прощения за тему "сам спросил - сам ответил".
Выкладываю что получилось - авось кому-нибудь да пригодится, правда здесь шифрование и дешифрование производится для одного блока размером 128 бит...
TwoFish.h:
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
| #ifndef __TWO_FISH_H_
#define __TWO_FISH_H_
#include <iostream>
#include <iomanip>
#include <assert.h>
using namespace std;
#ifndef UINT
typedef unsigned int UINT;
#endif
#ifndef BYTE
typedef unsigned char BYTE;
#endif
class TwoFish{
public:
//consturctors
TwoFish(char *key, size_t length);
TwoFish(BYTE *key, size_t length);
//destructor
~TwoFish();
//interface
BYTE* encrypt(BYTE *plain);
BYTE* decrypt(BYTE *cypher);
void printSubkeys();
private:
//System methods
void keySchedule(BYTE *key, size_t length);
unsigned long long h(UINT inputWord, UINT* inputWordArray, short arraySize); //???
BYTE q(BYTE x, int qt);
BYTE ROR4(BYTE x);
UINT ROR(UINT x, BYTE shift);
UINT ROL(UINT x, BYTE shift);
//attributes
UINT keys[40];
short k;
UINT *SBox;
};
#endif |
|
TwoFish.cpp:
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
| #include "TwoFish.h"
/********************************Constructor*********************************/
TwoFish::TwoFish(char *key, size_t length){
BYTE *u_key = new BYTE[length];
for (int i=0;i < length; i++){
u_key[i] = (BYTE) key[i];
}
keySchedule(u_key, length);
}
TwoFish::TwoFish(BYTE *key, size_t length){
keySchedule(key, length);
}
/*********************************Destructor*********************************/
TwoFish::~TwoFish(){
delete [] SBox;
}
/*********************************Interface**********************************/
BYTE* TwoFish::encrypt(BYTE *plain){
UINT
A = (plain[0] << 24) + (plain[1] << 16) + (plain[2] << 8) + plain[3],
B = (plain[4] << 24) + (plain[5] << 16) + (plain[6] << 8) + plain[7],
C = (plain[8] << 24) + (plain[9] << 16) + (plain[10] << 8) + plain[11],
D = (plain[12] << 24) + (plain[13] << 16) + (plain[14] << 8) + plain[15];
//whitening
A ^= keys[0];
B ^= keys[1];
C ^= keys[2];
D ^= keys[3];
//sixteen roudns
for(int i = 0; i < 16; i++){
unsigned long long tA = h(A, SBox, k);
unsigned long long tB = h(ROL(B, 8), SBox, k); // ROL
D = ROL(D, 1); //ROL
C ^= ((tA + tB + keys[2 * i + 8]) & 0xFFFFFFFF);
D ^= ((tA + 2*tB + keys[2 * i + 9]) & 0xFFFFFFFF);
C = ROR(C, 1); //ROR
//swap until last round
if (i != 15) {
UINT tmp = C;
C = A;
A = tmp;
tmp = D;
D = B;
B = tmp;
}
}
//whitening
A ^= keys[4];
B ^= keys[5];
C ^= keys[6];
D ^= keys[7];
plain[0] = (A >> 24) & 0xFF;
plain[1] = (A >> 16) & 0xFF;
plain[2] = (A >> 8) & 0xFF;
plain[3] = A & 0xFF;
plain[4] = (B >> 24) & 0xFF;
plain[5] = (B >> 16) & 0xFF;
plain[6] = (B >> 8) & 0xFF;
plain[7] = B & 0xFF;
plain[8] = (C >> 24) & 0xFF;
plain[9] = (C >> 16) & 0xFF;
plain[10] = (C >> 8) & 0xFF;
plain[11] = C & 0xFF;
plain[12] = (D >> 24) & 0xFF;
plain[13] = (D >> 16) & 0xFF;
plain[14] = (D >> 8) & 0xFF;
plain[15] = D & 0xFF;
return plain;
}
BYTE* TwoFish::decrypt(BYTE *cipher){
UINT
A = (cipher[0] << 24) + (cipher[1] << 16) + (cipher[2] << 8) + cipher[3],
B = (cipher[4] << 24) + (cipher[5] << 16) + (cipher[6] << 8) + cipher[7],
C = (cipher[8] << 24) + (cipher[9] << 16) + (cipher[10] << 8) + cipher[11],
D = (cipher[12] << 24) + (cipher[13] << 16) + (cipher[14] << 8) + cipher[15];
//whitening
A ^= keys[4];
B ^= keys[5];
C ^= keys[6];
D ^= keys[7];
//sixteen roudns
for(int i = 16 - 1; i >= 0; i--){
unsigned long long tA = h(A, SBox, k);
unsigned long long tB = h(ROL(B,8), SBox, k); //ROL
C = ROL(C, 1); //ROL
C ^= ((tA + tB + keys[2 * i + 8]) & 0xFFFFFFFF);
D ^= ((tA + 2*tB + keys[2 * i + 9]) & 0xFFFFFFFF);
D = ROR(D, 1); //ROR
//swap until last round
if (i > 0) {
UINT tmp = C;
C = A;
A = tmp;
tmp = D;
D = B;
B = tmp;
}
}
//whitening
A ^= keys[0];
B ^= keys[1];
C ^= keys[2];
D ^= keys[3];
cipher[0] = (A >> 24) & 0xFF;
cipher[1] = (A >> 16) & 0xFF;
cipher[2] = (A >> 8) & 0xFF;
cipher[3] = A & 0xFF;
cipher[4] = (B >> 24) & 0xFF;
cipher[5] = (B >> 16) & 0xFF;
cipher[6] = (B >> 8) & 0xFF;
cipher[7] = B & 0xFF;
cipher[8] = (C >> 24) & 0xFF;
cipher[9] = (C >> 16) & 0xFF;
cipher[10] = (C >> 8) & 0xFF;
cipher[11] = C & 0xFF;
cipher[12] = (D >> 24) & 0xFF;
cipher[13] = (D >> 16) & 0xFF;
cipher[14] = (D >> 8) & 0xFF;
cipher[15] = D & 0xFF;
return cipher;
}
void TwoFish::printSubkeys(){
for(int i=0; i< 40; i++){
cout<<hex<<keys[i]<<endl;
}
}
/*******************************System methods*******************************/
void TwoFish::keySchedule(BYTE *user_key, size_t length){
short N = 0; //size of key
if ( length > 192 ) {
N = 256;
} else if ((length > 128) && (length <=192)) {
N = 192;
} else N = 128;
char *temp_key = new char[N];
//Filling with 0
for(int i = 0; i < N; i++){
temp_key[i] = ( i < length ) ? user_key[i] : 0;
}
//Initialization of variables
k = N / 64;
BYTE RS[4][8] = {
{0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E},
{0xA4, 0x56, 0x82, 0xF3, 0x1E, 0xC6, 0x68, 0xE5},
{0x02, 0xA1, 0xFC, 0xC1, 0x47, 0xAE, 0x3D, 0x19},
{0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E, 0x03}
};
UINT *Me = new UINT[k];
UINT *Mo = new UINT[k];
for(int c1 = 0, c2 = 0, i = 0; i < 2 * k; i++){
if (i % 2 == 0) {
Me[c1] = 0;
for (int j = 4*i, shift = 3 ; j < 4*(i+1); j++, shift--){
Me[c1] += (temp_key[j] << (shift * 8));
}
c1++;
} else {
Mo[c2] = 0;
for (int j = 4*i, shift = 3 ; j < 4*(i+1); j++, shift--){
Mo[c2] = temp_key[j] << (shift * 8);
}
c2++;
}
}
SBox = new UINT[k];
for( int i = 0; i < k; i++){
SBox[k - 1 - i] = 0;
for (int j = 0; j < 4; j++){
UINT v = 0;
for (int t = 0; t < 8; t++){
v += RS[j][t] * temp_key[ 8*i + t ];
}
SBox[k - 1 - i] += ( v * (1 << (8 * j)));
}
}
//generation 32bit keys
UINT ro = (1 << 24) + (1 << 16) + (1 << 8) + 1;
for(int i = 0; i < 20; i++){
unsigned long long A = h( 2 * i * ro, Me, k); //???
unsigned long long B = h( (2 * i + 1) * ro, Mo, k); //????
B = ROL(B,8); //ROL
keys[2 * i] = (A + B) & 0xFFFFFFFF;
keys[2 * i + 1] = ROL(((A + 2 * B) & 0xFFFFFFFF), 9); //ROL
}
}
unsigned long long TwoFish::h(UINT inputWord, UINT *inputWordArray, short ArraySize){ //???
BYTE x[4]; //splitted input word
x[0] = (inputWord >> 24) & 0xFF;
x[1] = (inputWord >> 16) & 0xFF;
x[2] = (inputWord >> 8) & 0xFF;
x[3] = inputWord & 0xFF;
BYTE **l = new BYTE*[ArraySize]; //splitted words of input array
for (int i = 0; i < ArraySize; i++) {
l[i] = new BYTE[4];
l[i][0] = (inputWordArray[i] >> 24) && 0xFF;
l[i][1] = (inputWordArray[i] >> 16) && 0xFF;
l[i][2] = (inputWordArray[i] >> 8) && 0xFF;
l[i][3] = inputWordArray[i] && 0xFF;
}
BYTE y[4] = {0};
if (ArraySize == 4) {
y[0] = q(x[0], 1) ^ l[3][0];
y[1] = q(x[1], 0) ^ l[3][1];
y[2] = q(x[2], 0) ^ l[3][2];
y[3] = q(x[3], 1) ^ l[3][3];
} else {
y[0] = x[0];
y[1] = x[1];
y[2] = x[2];
y[3] = x[3];
}
if (ArraySize >= 3) {
y[0] = q(y[0], 1) ^ l[2][0];
y[1] = q(y[1], 0) ^ l[2][1];
y[2] = q(y[2], 0) ^ l[2][2];
y[3] = q(y[3], 1) ^ l[2][3];
} else {
y[0] = x[0];
y[1] = x[1];
y[2] = x[2];
y[3] = x[3];
}
//__________________qx_____________qx_____________qx
y[0] = q((q((q(y[0], 0) ^ l[1][0]), 0) ^ l[0][0]), 1);
y[1] = q((q((q(y[1], 1) ^ l[1][1]), 0) ^ l[0][1]), 0);
y[2] = q((q((q(y[2], 0) ^ l[1][2]), 1) ^ l[0][2]), 1);
y[3] = q((q((q(y[3], 1) ^ l[1][3]), 1) ^ l[0][3]), 0);
BYTE MDS[4][4] = {
{0x01, 0xEF, 0x5B, 0x5B},
{0x5B, 0xEF, 0xEF, 0x01},
{0xEF, 0x5B, 0x01, 0xEF},
{0xEF, 0x01, 0xEF, 0x5B}
};
unsigned long long H = 0;
for (int j = 0; j < 4; j++){
unsigned long long v = 0;
for (int t = 0; t < 4; t++){
v += MDS[j][t] * y[t];
}
H += v * ((unsigned long long)(1 << (8 * j)));
}
return H;
}
BYTE TwoFish::q(BYTE x, int op){
assert( (op == 0) || (op == 1));
const BYTE qt[2][4][16] = {
//permutation table for q0 operation
{
{ 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
{ 0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5, 0xF, 0x4, 0xA, 0x6, 0x7, 0x0, 0x9, 0xD },
{ 0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0, 0xC, 0x8, 0xF, 0x3, 0x2, 0x4, 0x7, 0x1 },
{ 0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE, 0x9, 0xB, 0x3, 0x0, 0x8, 0x5, 0xC, 0xA }
},
//permutation table for q1 operation
{
{ 0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE, 0x3, 0x1, 0x9, 0x4, 0x0, 0xA, 0xC, 0x5 },
{ 0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7, 0x6, 0xD, 0xA, 0x5, 0xF, 0x9, 0x0, 0x8 },
{ 0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA, 0x0, 0xE, 0xD, 0x8, 0x2, 0xB, 0x3, 0xF },
{ 0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE, 0x6, 0x4, 0x7, 0xF, 0x2, 0x0, 0x8, 0xA }
},
};
//splitting byte into two nibbles
BYTE a0 = x / 16;
BYTE b0 = x % 16;
BYTE a1 = a0 ^ b0;
BYTE b1 = a0 ^ ROR4(b0) ^ ((8 * a0) % 16); //ROR4
BYTE a2 = qt[op][0][a1];
BYTE b2 = qt[op][1][b1];
BYTE a3 = a2 ^ b2;
BYTE b3 = a2 ^ ROR4(b2) ^ ((8 * a2) % 16); //ROR4
BYTE a4 = qt[op][2][a3];
BYTE b4 = qt[op][3][b3];
return (16*b4 + a4);
}
//Right circular shift for 4 least significant bits
BYTE TwoFish::ROR4(BYTE x){
return (((x << 3) & 0xF) | ( (x & 0xF) >> 1));
}
//Left circular shift for unsigned int
UINT TwoFish::ROL(UINT x, BYTE shift){
BYTE shl = shift % (sizeof(x)*8);
if (shl == 0) return x;
return ((x << shl) | (x >> (sizeof(x)*8 - shl)));
}
//Right circular shift for unsigned int
UINT TwoFish::ROR(UINT x, BYTE shift){
BYTE shl = shift % (sizeof(x)*8);
if (shl == 0) return x;
return ((x >> shl) | (x << (sizeof(x)*8 - shl)));
} |
|
P.S. Все еще хотелось бы узнать какие идеологические, эстетические и другие огрехи есть в данном коде, как его можно улучшить.
0
|