-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.cc
173 lines (146 loc) · 4.36 KB
/
block.cc
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
#include "block.h"
// Cosntructor definition
Block::Block(char *c1, char *c2,char *c3,char *c4){
g[0] = new GF28(c1) ;
g[1] = new GF28(c2) ;
g[2] = new GF28(c3) ;
g[3] = new GF28(c4) ;
return ;
}
// Performs substituion using the S-Box
void Block::SubBytes(){
for(int i = 0 ; i < 4 ; ++i){
g[i]->SubWord1() ;
}
}
// Performs the shift row operation
void Block::ShiftRows(){
for (int i = 1; i < 4 ; ++i){
for(int j = 0 ; j < i ; ++j)
ShiftRowOnce(i) ;
}
}
// Shifts the row just once
void Block::ShiftRowOnce(int row){
unsigned char temp = g[0]->to_Array()[row] ;
for(int i = 0 ; i < 3; ++i)
g[i]->to_Array()[row] = g[i+1]->to_Array()[row] ;
g[3]->to_Array()[row] = temp ;
}
// Performs the mix col operation
void Block::MixColumns(){
// GF28 temp(const_cast<char *>(string("01010302").c_str())) ;
GF28 temp((char *)("00000000")) ;
temp = *(Table::Instance()->P) ;
unsigned char res[4] ;
for(int i=0; i < 4; ++i){
temp.ModProd(*g[i], res) ;
g[i]->copyW((unsigned char *)res) ;
}
// g[i]->copyW((unsigned char*)((*g[i])*temp) ) ;
// ((*g[i])*temp).display() ;
// *g[i] = (*g[i])*temp ;
}
void Block::InvSubBytes(){
for(int i = 0 ; i < 4 ; ++i){
g[i]->InvSubWord1() ;
}
}
void Block::InvShiftRows(){
for (int i = 1; i < 4 ; ++i){
for(int j = 0 ; j < i ; ++j)
InvShiftRowOnce(i) ;
}
}
void Block::InvShiftRowOnce(int row){
unsigned char temp = g[3]->to_Array()[row] ;
for(int i = 3 ; i > 0; --i)
g[i]->to_Array()[row] = g[i-1]->to_Array()[row] ;
g[0]->to_Array()[row] = temp ;
}
void Block::InvMixColumns(){
// GF28 temp(const_cast<char *>(string("090d0b0e").c_str())) ;
GF28 temp((char *)("00000000")) ;
temp = *(Table::Instance()->INVP) ;
unsigned char res[4] ;
for(int i=0; i < 4; ++i){
temp.ModProd(*g[i], res) ;
g[i]->copyW((unsigned char *)res) ;
}
// g[i]->copyW((unsigned char*)((*g[i])*temp) ) ;
// ((*g[i])*temp).display() ;
// *g[i] = (*g[i])*temp ;
}
void Block::AddRoundKey(Keys &k, int round){
unsigned char newW[4] ;
for(int i=0; i < 4 ; ++i){
for(int j = 0 ; j < 4 ; ++j){
newW[j] = g[i]->to_Array()[j] ^ k.to_Word()[round*4+i]->to_Array()[j] ;
}
g[i]->copyW(newW) ;
// *(dynamic_cast<GF28 *>(g[i])) = *(dynamic_cast<GF28 *>(&(g[i]->XOR(*(k.to_Word()[round*4+i]))))) ;
}
}
void Block::Encrypt(Keys &k){
printf("round[ 0].input %s\n", to_Array()) ;
AddRoundKey(k, 0) ;
printf("round[ 0].k_sch %s\n", k.to_Array(0)) ;
for(int round = 1 ; round < 10 ; ++round){
printf("round[ %d].start %s\n",round, to_Array()) ;
SubBytes() ;
printf("round[ %d].s_box %s\n",round, to_Array()) ;
ShiftRows() ;
printf("round[ %d].s_row %s\n",round, to_Array()) ;
MixColumns() ;
printf("round[ %d].m_col %s\n",round, to_Array()) ;
AddRoundKey(k, round) ;
printf("round[ %d].k_sch %s\n",round, k.to_Array(round)) ;
}
int round = 10 ;
printf("round[%d].start %s\n",round, to_Array()) ;
SubBytes();
printf("round[%d].s_box %s\n",round, to_Array()) ;
ShiftRows();
printf("round[%d].s_row %s\n",round, to_Array()) ;
AddRoundKey(k, 10);
printf("round[%d].k_sch %s\n",round, k.to_Array(round)) ;
printf("round[%d].output %s\n",round, to_Array()) ;
}
void Block::Decrypt(Keys &k){
printf("round[ 0].iinput %s\n", to_Array()) ;
AddRoundKey(k, 10) ;
printf("round[ 0].ik_sch %s\n", k.to_Array(10)) ;
for(int round = 9 ; round > 0 ; --round){
printf("round[ %d].istart %s\n",10-round, to_Array()) ;
InvShiftRows() ;
printf("round[ %d].is_row %s\n",10-round, to_Array()) ;
InvSubBytes() ;
printf("round[ %d].is_box %s\n",10-round, to_Array()) ;
AddRoundKey(k, round) ;
printf("round[ %d].ik_sch %s\n",10-round, k.to_Array(round)) ;
printf("round[ %d].ik_add %s\n",10-round, to_Array()) ;
InvMixColumns() ;
// printf("round[ %d].im_col: %s\n",10-round, to_Array()) ;
}
int round = 0 ;
printf("round[%d].istart %s\n",10-round, to_Array()) ;
InvShiftRows();
printf("round[%d].is_row %s\n",10-round, to_Array()) ;
InvSubBytes();
printf("round[%d].is_box %s\n",10-round, to_Array()) ;
AddRoundKey(k, 0);
printf("round[%d].ik_sch %s\n",10-round, k.to_Array(round)) ;
printf("round[%d].ioutput %s\n",10-round, to_Array()) ;
}
char *Block::to_Array(){
for(int i = 0 ; i < 4 ; ++i){
for(int j = 0 ; j < 4 ; ++j){
sprintf(&res[i*8+j*2], "%02x", g[i]->to_Array()[j]) ;
}
}
res[32] = '\0' ;
return res ;
}
Block::~Block(){
// delete g;
}