| 1357 | } |
| 1358 | |
| 1359 | fs::file decrypt_self(const fs::file& elf_or_self, const u8* klic_key, SelfAdditionalInfo* out_info) |
| 1360 | { |
| 1361 | if (out_info) |
| 1362 | { |
| 1363 | *out_info = {}; |
| 1364 | } |
| 1365 | |
| 1366 | if (!elf_or_self) |
| 1367 | { |
| 1368 | return fs::file{}; |
| 1369 | } |
| 1370 | |
| 1371 | elf_or_self.seek(0); |
| 1372 | |
| 1373 | // Check SELF header first. Check for a debug SELF. |
| 1374 | if (elf_or_self.size() >= 4 && elf_or_self.read<u32>() == "SCE\0"_u32) |
| 1375 | { |
| 1376 | if (fs::file res = CheckDebugSelf(elf_or_self)) |
| 1377 | { |
| 1378 | // TODO: Decrypt |
| 1379 | return res; |
| 1380 | } |
| 1381 | |
| 1382 | // Check the ELF file class (32 or 64 bit). |
| 1383 | const bool isElf32 = IsSelfElf32(elf_or_self); |
| 1384 | |
| 1385 | // Start the decrypter on this SELF file. |
| 1386 | SELFDecrypter self_dec(elf_or_self); |
| 1387 | |
| 1388 | // Load the SELF file headers. |
| 1389 | if (!self_dec.LoadHeaders(isElf32, out_info)) |
| 1390 | { |
| 1391 | self_log.error("Failed to load SELF file headers!"); |
| 1392 | return fs::file{}; |
| 1393 | } |
| 1394 | |
| 1395 | // Load and decrypt the SELF file metadata. |
| 1396 | if (!self_dec.LoadMetadata(klic_key)) |
| 1397 | { |
| 1398 | (klic_key ? self_log.notice : self_log.error)("Failed to load SELF file metadata!"); |
| 1399 | return fs::file{}; |
| 1400 | } |
| 1401 | |
| 1402 | // Decrypt the SELF file data. |
| 1403 | if (!self_dec.DecryptData()) |
| 1404 | { |
| 1405 | (klic_key ? self_log.notice : self_log.error)("Failed to decrypt SELF file data!"); |
| 1406 | return fs::file{}; |
| 1407 | } |
| 1408 | |
| 1409 | // Make a new ELF file from this SELF. |
| 1410 | return self_dec.MakeElf(isElf32); |
| 1411 | } |
| 1412 | |
| 1413 | return {}; |
| 1414 | } |
| 1415 | |
| 1416 | bool verify_npdrm_self_headers(const fs::file& self, u8* klic_key, NPD_HEADER* npd_out) |
no test coverage detected