-
Notifications
You must be signed in to change notification settings - Fork 7
/
PfsCryptEngineSelectors.cpp
296 lines (228 loc) · 10.5 KB
/
PfsCryptEngineSelectors.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
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
#include "PfsCryptEngineSelectors.h"
#include <cstdint>
#include <string>
#include <cstring>
#include "PfsCryptEngineBase.h"
#include "FlagOperations.h"
//this macro unwraps 64 bit sector number into byte array
#define UINT64_TO_BYTEARRAY(x, y) \
{ (y)[7] = (unsigned char)((x) >> 56); (y)[6] = (unsigned char)((x) >> 48); \
(y)[5] = (unsigned char)((x) >> 40); (y)[4] = (unsigned char)((x) >> 32); \
(y)[3] = (unsigned char)((x) >> 24); (y)[2] = (unsigned char)((x) >> 16); \
(y)[1] = (unsigned char)((x) >> 8) ; (y)[0] = (unsigned char)(x); }
//this function increments byte array of size 0x10
int UINT128_BYTEARRAY_INC(unsigned char iv[0x10])
{
for(int i = 0; i < 0x10; i++)
{
if(iv[i] == 0xFF)
{
iv[i] = 0;
}
else
{
iv[i] = iv[i] + 1;
break;
}
}
return 0;
}
//#### GROUP 1 (possible keygen aes-cbc-cts dec/aes-cbc-cts enc) ####
//#### GROUP 2 (possible keygen aes-cmac-cts dec/aes-cmac-cts enc) (technically there is no dec/enc - this is pair of same functions since cmac) ####
unsigned char g_cmac_buffer[0x10] = {0};
int pfs_decrypt_unicv(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, const unsigned char* key, const unsigned char* tweak_mask, std::uint64_t tweak_key, std::uint32_t size, std::uint32_t block_size, const unsigned char* src, unsigned char* dst, std::uint16_t crypto_engine_flag, std::uint16_t key_id)
{
unsigned char tweak[0x10] = {0};
UINT64_TO_BYTEARRAY(tweak_key, tweak); //convert std::uint64_t tweak to byte array
memset(tweak + 8, 0, 8); //set upper tweak to 0
for(int i = 0; i < 0x10; i++)
tweak[i] = tweak[i] ^ tweak_mask[i]; // xor tweak with mask (kinda mimic tweak_enc_value in xts-aes)
if(size != 0)
{
std::uint32_t offset = 0;
std::uint32_t bytes_left = size;
do
{
std::uint64_t tweak_key_ofst = tweak_key + offset;
UINT64_TO_BYTEARRAY(tweak_key_ofst, tweak); // modify tweak (mimic xts-aes) by adding offset to the tweak
memset(tweak + 8, 0, 8); //set upper tweak to 0
for(int i = 0; i < 0x10; i++)
tweak[i] = tweak[i] ^ tweak_mask[i]; // xor tweak with mask (kinda mimic tweak_enc_value in xts-aes)
// select block_size if we did not yet reach tail of the data.
// or select bytes_left which will be the size of the tail in the end
int size_arg = 0;
if(block_size < bytes_left)
size_arg = block_size;
else
size_arg = bytes_left;
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_KEYGEN)
{
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
AESCMACDecryptWithKeygen_base(cryptops, iF00D, key, tweak, size_arg, src + offset, g_cmac_buffer, key_id);
else
AESCBCDecryptWithKeygen_base(cryptops, iF00D, key, tweak, size_arg, src + offset, dst + offset, key_id); //cbc decrypt with tweak as iv
}
else
{
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
AESCMACDecrypt_base(cryptops, key, tweak, size_arg, src + offset, g_cmac_buffer);
else
AESCBCDecrypt_base(cryptops, key, tweak, size_arg, src + offset, dst + offset); //cbc decrypt with tweak as iv
}
offset = offset + block_size;
bytes_left = bytes_left - block_size;
}
while(size > offset);
}
//copy result to dest buffer since cmac functions operate with global buffer
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
{
if(dst != src)
{
memcpy(dst, src, size);
}
}
return 0;
}
int pfs_encrypt_unicv(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, const unsigned char* key, const unsigned char* tweak_mask, std::uint64_t tweak_key, std::uint32_t size, std::uint32_t block_size, const unsigned char* src, unsigned char* dst, std::uint16_t crypto_engine_flag, std::uint16_t key_id)
{
unsigned char tweak[0x10] = {0};
UINT64_TO_BYTEARRAY(tweak_key, tweak); //convert std::uint64_t tweak to byte array
memset(tweak + 8, 0, 8); //set upper tweak to 0
for(int i = 0; i < 0x10; i++)
tweak[i] = tweak[i] ^ tweak_mask[i]; // xor tweak with mask (kinda mimic tweak_enc_value in xts-aes)
if(size != 0)
{
std::uint32_t offset = 0;
std::uint32_t bytes_left = size;
do
{
std::uint64_t tweak_key_ofst = tweak_key + offset;
UINT64_TO_BYTEARRAY(tweak_key_ofst, tweak); // modify tweak (mimic xts-aes) by adding offset to the tweak
memset(tweak + 8, 0, 8); //set upper tweak to 0
for(int i = 0; i < 0x10; i++)
tweak[i] = tweak[i] ^ tweak_mask[i]; // xor tweak with mask (kinda mimic tweak_enc_value in xts-aes)
// select block_size if we did not yet reach tail of the data.
// or select bytes_left which will be the size of the tail in the end
int size_arg = 0;
if(block_size < bytes_left)
size_arg = block_size;
else
size_arg = bytes_left;
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_KEYGEN)
{
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
AESCMACEncryptWithKeygen_base(cryptops, iF00D, key, tweak, size_arg, src + offset, g_cmac_buffer, key_id);
else
AESCBCEncryptWithKeygen_base(cryptops, iF00D, key, tweak, size_arg, src + offset, dst + offset, key_id); //cbc encrypt with tweak as iv
}
else
{
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
AESCMACEncrypt_base(cryptops, key, tweak, size_arg, src + offset, g_cmac_buffer);
else
AESCBCEncrypt_base(cryptops, key, tweak, size_arg, src + offset, dst + offset); //cbc encrypt with tweak as iv
}
offset = offset + block_size;
bytes_left = bytes_left - block_size;
}
while(size > offset);
}
//copy result to dest buffer since cmac functions operate with global buffer
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
{
if(dst != src)
{
memcpy(dst, src, size);
}
}
return 0;
}
//#### GROUP 3 (no keygen xts-aes dec/xts-aes enc) ####
//#### GROUP 4 (no keygen xts-cmac dec/xts-cmac enc) (technically there is no dec/enc - this is pair of same functions since cmac) ####
//looks like this method can decrypt multiple blocks when size > block_size
//assuming that it adds 1 to tweak_key when decrypting each next block
//in practice though it looks like this method is only used to decrypt single block
int pfs_decrypt_icv(std::shared_ptr<ICryptoOperations> cryptops, const unsigned char* key, const unsigned char* tweak_enc_key, std::uint32_t keysize, std::uint64_t tweak_key, std::uint32_t size, std::uint32_t block_size, const unsigned char* src, unsigned char* dst, std::uint16_t crypto_engine_flag)
{
unsigned char tweak[0x10] = {0};
if((block_size <= 0xF) || (size <= 0xF)) //block_size and size should be at least one block
return 0x80140609;
UINT64_TO_BYTEARRAY(tweak_key, tweak); //convert std::uint64_t tweak to byte array
memset(tweak + 8, 0, 8); //set upper tweak to 0
std::uint32_t offset = 0;
std::uint32_t bytes_left = size;
do
{
// select block_size if we did not yet reach tail of the data.
// or select bytes_left which will be the size of the tail in the end
int size_arg = 0;
if(block_size < bytes_left)
size_arg = block_size;
else
size_arg = bytes_left;
int result0 = 0;
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
result0 = XTSCMACDecrypt_base(cryptops, tweak, key, tweak_enc_key, keysize, size_arg, src + offset, g_cmac_buffer);
else
result0 = XTSAESDecrypt_base(cryptops, tweak, key, tweak_enc_key, keysize, size_arg, src + offset, dst + offset); //xts-aes decrypt
if(result0 != 0)
return result0;
UINT128_BYTEARRAY_INC(tweak); // increment tweak by 1 (not relevant ? since this function is only used to decrypt single block of data)
offset = offset + block_size;
bytes_left = bytes_left - block_size;
}
while(size > offset);
//copy result to dest buffer since cmac functions operate with global buffer
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
{
if(dst != src)
{
memcpy(dst, src, size);
}
}
return 0;
}
//looks like this method can encrypt multiple blocks when size > block_size
//assuming that it adds 1 to tweak_key when encrypting each next block
//in practice though it looks like this method is only used to decrypt single block
int pfs_encrypt_icv(std::shared_ptr<ICryptoOperations> cryptops, const unsigned char* key, const unsigned char* tweak_enc_key, std::uint32_t keysize, std::uint64_t tweak_key, std::uint32_t size, std::uint32_t block_size, const unsigned char* src, unsigned char* dst, std::uint16_t crypto_engine_flag)
{
unsigned char tweak[0x10] = {0};
if((block_size <= 0xF) || (size <= 0xF)) //block_size and size should be at least one block
return 0x80140609;
UINT64_TO_BYTEARRAY(tweak_key, tweak); //block_size and size should be at least one block
memset(tweak + 8, 0, 8); //set upper tweak to 0
std::uint32_t offset = 0;
std::uint32_t bytes_left = size;
do
{
// select block_size if we did not yet reach tail of the data.
// or select bytes_left which will be the size of the tail in the end
int size_arg = 0;
if(block_size < bytes_left)
size_arg = block_size;
else
size_arg = bytes_left;
int result0 = 0;
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
result0 = XTSCMACEncrypt_base(cryptops, tweak, key, tweak_enc_key, keysize, size_arg, src + offset, g_cmac_buffer);
else
result0 = XTSAESEncrypt_base(cryptops, tweak, key, tweak_enc_key, keysize, size_arg, src + offset, dst + offset); //xts-aes encrypt
if(result0 != 0)
return result0;
UINT128_BYTEARRAY_INC(tweak); // increment tweak by 1 (not relevant ? since this function is only used to decrypt single block of data)
offset = offset + block_size;
bytes_left = bytes_left - block_size;
}
while(size > offset);
//copy result to dest buffer since cmac functions operate with global buffer
if(crypto_engine_flag & CRYPTO_ENGINE_CRYPTO_USE_CMAC)
{
if(dst != src)
{
memcpy(dst, src, size);
}
}
return 0;
}