-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpch.cpp
230 lines (207 loc) · 6.6 KB
/
pch.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
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
#include "AES_Library.h"
/*-------------------------------------------------------------------------------
AES_Functions
Credits:
Kokkev(https://github.com/kokke), released into the public domain
https://en.wikipedia.org/wiki/Rijndael_S-box
-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------
AES_AddVector
Input: Iv
Output: Output
-------------------------------------------------------------------------------*/
void AES_AddVector(BYTE* Output, BYTE* Iv, int Blocksize)
{
for (int i = 0; i < Blocksize; i++)
{
Output[i] ^= Iv[i];
}
}
/*-------------------------------------------------------------------------------
AES_Initialize_SBox
Output: Sbox
-------------------------------------------------------------------------------*/
void AES_Initialize_SBox(BYTE* Sbox)
{
BYTE p = 1, q = 1;
/* loop invariant: p * q == 1 in the Galois field */
do {
/* multiply p by 3 */
p = p ^ (p << 1) ^ (p & 0x80 ? 0x1B : 0);
/* divide q by 3 (equals multiplication by 0xf6) */
q ^= q << 1;
q ^= q << 2;
q ^= q << 4;
q ^= q & 0x80 ? 0x09 : 0;
/* compute the affine transformation */
Sbox[p] = (q ^
((q << 1) | (q >> 7)) ^
((q << 2) | (q >> 6)) ^
((q << 3) | (q >> 5)) ^
((q << 4) | (q >> 4))) ^ 0x63;
} while (p != 1);
Sbox[0] = 0x63; // special case
}
/*-------------------------------------------------------------------------------
AES_Inverse_SBox
Input/Output: Sbox
-------------------------------------------------------------------------------*/
void AES_Inverse_SBox(BYTE* Sbox)
{
BYTE sbox[256];
for (int i = 0; i < 256; i++)
sbox[Sbox[i]] = i;
memcpy(Sbox, sbox, sizeof(sbox));
}
/*-------------------------------------------------------------------------------
AES_ExpandRoundKeys
Input: Key, KeySizeInWords, NumberOfRounds, Sbox
Output: RoundKey
-------------------------------------------------------------------------------*/
void AES_ExpandRoundKeys(BYTE const* Key, UINT KeySizeInWords, UINT NumberOfRounds, BYTE* RoundKey, BYTE* Sbox)
{
UINT i, j;
BYTE tmp[4];
const BYTE RoundConstant[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
memcpy(RoundKey, Key, KeySizeInWords * 4);
for (i = KeySizeInWords; i < 4 * (NumberOfRounds + 1); i++)
{
for (j = 0; j < 4; j++)
tmp[j] = RoundKey[(i - 1) * 4 + j];
if (i % KeySizeInWords == 0)
{
RotateLeft(tmp, 1, 1);
for (j = 0; j < 4; j++)
tmp[j] = Sbox[tmp[j]];
tmp[0] = tmp[0] ^ RoundConstant[i / KeySizeInWords];
}
if (KeySizeInWords == 8) // AES256
{
if (4 == i % KeySizeInWords)
{
for (j = 0; j < 4; j++)
tmp[j] = Sbox[tmp[j]];
}
}
for (j = 0; j < 4; j++)
RoundKey[i * 4 + j] = RoundKey[(i - KeySizeInWords) * 4 + j] ^ tmp[j];
}
}
/*-------------------------------------------------------------------------------
AES_AddRoundKey
Input: RoundNumber, RoundKey
Input/Output: State
-------------------------------------------------------------------------------*/
void AES_AddRoundKey(UINT RoundNumber, BYTE* RoundKey, BYTE* State)
{
BYTE i;
BYTE j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
*(State + (i * 4) + j) ^= RoundKey[(RoundNumber * 4 * 4) + (i * 4) + j];
}
}
}
/*-------------------------------------------------------------------------------
AES_SubstituteBytes
Input: Sbox
Input/Output: State
-------------------------------------------------------------------------------*/
void AES_SubstituteBytes(BYTE* State, BYTE* Sbox)
{
UINT i;
UINT j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
*(State + (j * 4 + i)) = Sbox[*(State + (j * 4 + i))];
}
}
}
/*-------------------------------------------------------------------------------
RotateLeft
Input: Cnt, Step
Input/Output: Line
-------------------------------------------------------------------------------*/
void RotateLeft(BYTE* Line, BYTE Cnt, BYTE Step)
{
BYTE z[4];
for (int i = 0; i < 4; i++)
{
z[i] = *(Line + (3 - i) * Step);
}
UINT* t1 = (UINT*)z;
UINT t2 = *t1;
*t1 = t2 << Cnt * 8 | t2 >> (4 - Cnt) * 8;
for (int i = 0; i < 4; i++)
{
*(Line + (3 - i) * Step) = z[i];
}
}
/*-------------------------------------------------------------------------------
AES_ShiftRows
Input: Mode
Input/Output: State
-------------------------------------------------------------------------------*/
void AES_ShiftRows(BYTE* State, BYTE Mode)
{
if (Mode == ENCRYPT)
{
RotateLeft(State + 1, 1, 4);
RotateLeft(State + 2, 2, 4);
RotateLeft(State + 3, 3, 4);
}
else // DECRYPT
{
RotateLeft(State + 1, 3, 4);
RotateLeft(State + 2, 2, 4);
RotateLeft(State + 3, 1, 4);
}
}
/*-------------------------------------------------------------------------------
GaloisFieldMultiplication
Inputs: Inp1, Inp2
Output: (function return)
-------------------------------------------------------------------------------*/
BYTE GaloisFieldMultiplication(BYTE Inp1, BYTE Inp2)
{
BYTE product = 0;
while (Inp1 && Inp2)
{
if (Inp2 & 1) // Inp2 is odd
product ^= Inp1;
Inp1 = (Inp1 & 0x80) ? (Inp1 << 1) ^ 0x11b : (Inp1 <<= 1);
Inp2 >>= 1;
}
return product;
}
/*-------------------------------------------------------------------------------
AES_MixColumns
Input: Mode
Input/Output: State
-------------------------------------------------------------------------------*/
void AES_MixColumns(BYTE* State, BYTE Mode)
{
BYTE M[2][16] =
{ { 02, 03, 01, 01, 01, 02, 03, 01, 01, 01, 02, 03, 03, 01, 01, 02 }, // Mix
{ 0x0e, 0x0b, 0x0d, 0x09, 0x09, 0x0e, 0x0b, 0x0d, 0x0d, 0x09, 0x0e, 0x0b, 0x0b, 0x0d, 0x09, 0x0e } }; // inverse mix
BYTE tmp[4];
BYTE state[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 4; k++)
{
tmp[k] = GaloisFieldMultiplication(M[Mode][j * 4 + k], *(State + (i * 4 + k)));
}
state[i][j] = tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
}
}
memcpy(State, state, sizeof(state));
}