-
Notifications
You must be signed in to change notification settings - Fork 0
/
KerberosAES.cpp
263 lines (190 loc) · 8.32 KB
/
KerberosAES.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
#include <Windows.h>
#include <bcrypt.h>
#include <string>
#include <vector>
#include <tuple>
#include <memory>
//*************************************************************************************************
const int aes_block_length = 16;
//*************************************************************************************************
std::tuple<int, int, int> checkLen(int len)
{
const int padSize = aes_block_length - (len % aes_block_length);
const int maxLength = len + padSize;
return {
len >= aes_block_length,
padSize,
maxLength
};
}
//*************************************************************************************************
std::vector<unsigned char> NFold(std::vector<unsigned char>& buffer, size_t size)
{
auto inBytesSize = buffer.size();
auto a = size;
auto b = inBytesSize;
while(b != 0)
{
auto c = b;
b = a % b;
a = c;
}
auto lcm = (size * inBytesSize) / a;
std::vector<unsigned char> result;
result.resize(size);
unsigned int tmpByte = 0;
for(long long i = (lcm - 1); i >= 0; i--)
{
unsigned int msbit = (inBytesSize << 3) - 1;
unsigned int div = i / inBytesSize;
msbit += ((inBytesSize << 3) + 13) * div;
msbit += (inBytesSize - (i % inBytesSize)) << 3;
msbit %= inBytesSize << 3;
auto rst = buffer[(inBytesSize - 1 - (msbit >> 3)) % inBytesSize] & 0xff;
auto rst2 = buffer[(inBytesSize - (msbit >> 3)) % inBytesSize] & 0xff;
msbit = ((rst << 8) | (rst2)) >> ((msbit & 7) + 1) & 0xff;
tmpByte += msbit;
msbit = result[i % size] & 0xff;
tmpByte += msbit;
result[i % size] = (tmpByte & 0xff);
tmpByte >>= 8;
}
if(tmpByte != 0)
{
for(long long i = size - 1; i >= 0; i--)
{
tmpByte += result[i] & 0xff;
result[i] = (tmpByte & 0xff);
tmpByte >>= 8;
}
}
return result;
}
//*************************************************************************************************
void swapBlocks(std::vector<unsigned char>* text)
{
auto blockOne = text->size() - (aes_block_length << 1);
auto blockTwo = text->size() - aes_block_length;
for(int i = 0; i < aes_block_length; i++)
{
auto temp = text->at(i + blockOne);
text->at(i + blockOne) = text->at(i + blockTwo);
text->at(i + blockTwo) = temp;
}
}
//*************************************************************************************************
std::vector<unsigned char> aesEncrypt(std::vector<unsigned char> data, std::vector<unsigned char> key)
{
BCRYPT_ALG_HANDLE handle;
auto bcryptResult = BCryptOpenAlgorithmProvider(&handle, BCRYPT_AES_ALGORITHM, 0, 0);
bcryptResult = BCryptSetProperty(handle, BCRYPT_CHAINING_MODE, (BYTE*)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
size_t InformationLength = sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + key.size();
auto Information = new unsigned char[InformationLength]();
if(!Information)
throw std::exception("Out of memory");
auto InformationGuard = std::unique_ptr<unsigned char[]>{ Information };
PBCRYPT_KEY_DATA_BLOB_HEADER header = reinterpret_cast<PBCRYPT_KEY_DATA_BLOB_HEADER>(Information);
header->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
header->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
header->cbKeyData = key.size();
std::copy(key.begin(), key.end(), (unsigned char*)(header + 1));
BCRYPT_KEY_HANDLE newKey = nullptr;
bcryptResult = BCryptImportKey(handle, nullptr, BCRYPT_KEY_DATA_BLOB, &newKey, nullptr, 0, Information, InformationLength, 0);
auto [flag, padSize, maxLength] = checkLen(data.size());
if(!flag)
return data;
std::vector<unsigned char> enc_text;
if(padSize == aes_block_length)
enc_text = data;
else
{
enc_text.insert(enc_text.end(), data.begin(), data.end());
enc_text.resize(data.size() + padSize);
}
std::vector<unsigned char> iv;
iv.resize(aes_block_length);
ULONG ret_size = 0;
auto status = BCryptEncrypt(newKey, enc_text.data(), enc_text.size(), nullptr, iv.data(), iv.size(), nullptr, 0, &ret_size, 0);
std::vector<unsigned char> encrypted;
encrypted.resize(ret_size);
status = BCryptEncrypt(newKey, enc_text.data(), enc_text.size(), nullptr, iv.data(), iv.size(), encrypted.data(), encrypted.size(), &ret_size, 0);
if(encrypted.size() >= (aes_block_length << 1))
swapBlocks(&encrypted);
encrypted.resize(data.size());
return encrypted;
}
//*************************************************************************************************
std::vector<unsigned char> DR(std::vector<unsigned char>& key, std::vector<unsigned char>& constant, int keySize, int blockSize)
{
std::vector<unsigned char> keyBytes;
keyBytes.resize(keySize);
std::vector<unsigned char> ki;
if(constant.size() != blockSize)
ki = NFold(constant, blockSize);
else
ki = constant;
int n = 0;
do
{
ki = aesEncrypt(ki, key);
if(n + blockSize >= keySize)
{
std::copy(ki.begin(), ki.begin() + (keySize - n), keyBytes.begin() + n);
break;
}
std::copy(ki.begin(), ki.begin() + blockSize, keyBytes.begin() + n);
n += blockSize;
} while(n < keySize);
return keyBytes;
}
//*************************************************************************************************
void aesDecrypt(std::vector<unsigned char> data, std::vector<unsigned char> key)
{
BCRYPT_ALG_HANDLE handle;
auto bcryptResult = BCryptOpenAlgorithmProvider(&handle, BCRYPT_AES_ALGORITHM, 0, 0);
bcryptResult = BCryptSetProperty(handle, BCRYPT_CHAINING_MODE, (BYTE*)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
size_t InformationLength = sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + key.size();
auto Information = new unsigned char[InformationLength]();
if(!Information)
throw std::exception("Out of memory");
auto InformationGuard = std::unique_ptr<unsigned char[]>{ Information };
PBCRYPT_KEY_DATA_BLOB_HEADER header = reinterpret_cast<PBCRYPT_KEY_DATA_BLOB_HEADER>(Information);
header->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
header->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
header->cbKeyData = key.size();
std::copy(key.begin(), key.end(), (unsigned char*)(header + 1));
BCRYPT_KEY_HANDLE newKey = nullptr;
bcryptResult = BCryptImportKey(handle, nullptr, BCRYPT_KEY_DATA_BLOB, &newKey, nullptr, 0, Information, InformationLength, 0);
auto [flag, padSize, maxLength] = checkLen(data.size());
std::vector<unsigned char> iv;
iv.resize(aes_block_length);
std::vector<unsigned char> enc_text;
if(padSize != aes_block_length)
{
auto offset = data.size() - (aes_block_length << 1) + padSize;
std::vector<unsigned char> depadded{ data.begin() + offset, data.begin() + offset + aes_block_length};
ULONG ret_size = 0;
auto status = BCryptDecrypt(newKey, depadded.data(), depadded.size(), nullptr, iv.data(), iv.size(), nullptr, 0, &ret_size, 0);
std::vector<unsigned char> depadded_decrypted;
depadded_decrypted.resize(ret_size);
status = BCryptDecrypt(newKey, depadded.data(), depadded.size(), nullptr, iv.data(), iv.size(), depadded_decrypted.data(), depadded_decrypted.size(), &ret_size, 0);
enc_text.insert(enc_text.end(), data.begin(), data.end());
enc_text.insert(enc_text.end(), depadded_decrypted.begin() + (depadded_decrypted.size() - padSize), depadded_decrypted.end());
int jjj = 0;
}
if(enc_text.size() >= (aes_block_length << 1))
swapBlocks(&enc_text);
iv.clear();
iv.resize(aes_block_length);
ULONG ret_size = 0;
auto status = BCryptDecrypt(newKey, enc_text.data(), enc_text.size(), nullptr, iv.data(), iv.size(), nullptr, 0, &ret_size, 0);
std::vector<unsigned char> decrypted;
decrypted.resize(ret_size);
status = BCryptDecrypt(newKey, enc_text.data(), enc_text.size(), nullptr, iv.data(), iv.size(), decrypted.data(), decrypted.size(), &ret_size, 0);
}
//*************************************************************************************************
int main()
{
return 0;
}
//*************************************************************************************************