Browse Source
Merge pull request #1094 from DarkLordZach/nax0
Merge pull request #1094 from DarkLordZach/nax0
file_sys: Add support for NAX archivesnce_cpp
committed by
GitHub
31 changed files with 821 additions and 97 deletions
-
6src/core/CMakeLists.txt
-
7src/core/crypto/aes_util.cpp
-
8src/core/crypto/ctr_encryption_layer.cpp
-
172src/core/crypto/key_manager.cpp
-
68src/core/crypto/key_manager.h
-
58src/core/crypto/xts_encryption_layer.cpp
-
25src/core/crypto/xts_encryption_layer.h
-
11src/core/file_sys/bis_factory.cpp
-
12src/core/file_sys/card_image.cpp
-
2src/core/file_sys/card_image.h
-
10src/core/file_sys/content_archive.cpp
-
3src/core/file_sys/content_archive.h
-
14src/core/file_sys/registered_cache.cpp
-
4src/core/file_sys/registered_cache.h
-
15src/core/file_sys/sdmc_factory.cpp
-
7src/core/file_sys/sdmc_factory.h
-
7src/core/file_sys/vfs.cpp
-
4src/core/file_sys/vfs.h
-
169src/core/file_sys/xts_archive.cpp
-
69src/core/file_sys/xts_archive.h
-
42src/core/hle/service/filesystem/filesystem.cpp
-
6src/core/hle/service/filesystem/filesystem.h
-
29src/core/loader/loader.cpp
-
14src/core/loader/loader.h
-
66src/core/loader/nax.cpp
-
48src/core/loader/nax.h
-
11src/core/loader/xci.cpp
-
4src/yuzu/configuration/config.cpp
-
22src/yuzu/game_list.cpp
-
2src/yuzu/game_list_p.h
-
3src/yuzu/main.cpp
@ -0,0 +1,58 @@ |
|||
// Copyright 2018 yuzu emulator team
|
|||
// Licensed under GPLv2 or any later version
|
|||
// Refer to the license.txt file included.
|
|||
|
|||
#include <algorithm>
|
|||
#include <cstring>
|
|||
#include "common/assert.h"
|
|||
#include "core/crypto/xts_encryption_layer.h"
|
|||
|
|||
namespace Core::Crypto { |
|||
|
|||
constexpr u64 XTS_SECTOR_SIZE = 0x4000; |
|||
|
|||
XTSEncryptionLayer::XTSEncryptionLayer(FileSys::VirtualFile base_, Key256 key_) |
|||
: EncryptionLayer(std::move(base_)), cipher(key_, Mode::XTS) {} |
|||
|
|||
size_t XTSEncryptionLayer::Read(u8* data, size_t length, size_t offset) const { |
|||
if (length == 0) |
|||
return 0; |
|||
|
|||
const auto sector_offset = offset & 0x3FFF; |
|||
if (sector_offset == 0) { |
|||
if (length % XTS_SECTOR_SIZE == 0) { |
|||
std::vector<u8> raw = base->ReadBytes(length, offset); |
|||
cipher.XTSTranscode(raw.data(), raw.size(), data, offset / XTS_SECTOR_SIZE, |
|||
XTS_SECTOR_SIZE, Op::Decrypt); |
|||
return raw.size(); |
|||
} |
|||
if (length > XTS_SECTOR_SIZE) { |
|||
const auto rem = length % XTS_SECTOR_SIZE; |
|||
const auto read = length - rem; |
|||
return Read(data, read, offset) + Read(data + read, rem, offset + read); |
|||
} |
|||
std::vector<u8> buffer = base->ReadBytes(XTS_SECTOR_SIZE, offset); |
|||
if (buffer.size() < XTS_SECTOR_SIZE) |
|||
buffer.resize(XTS_SECTOR_SIZE); |
|||
cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / XTS_SECTOR_SIZE, |
|||
XTS_SECTOR_SIZE, Op::Decrypt); |
|||
std::memcpy(data, buffer.data(), std::min(buffer.size(), length)); |
|||
return std::min(buffer.size(), length); |
|||
} |
|||
|
|||
// offset does not fall on block boundary (0x4000)
|
|||
std::vector<u8> block = base->ReadBytes(0x4000, offset - sector_offset); |
|||
if (block.size() < XTS_SECTOR_SIZE) |
|||
block.resize(XTS_SECTOR_SIZE); |
|||
cipher.XTSTranscode(block.data(), block.size(), block.data(), |
|||
(offset - sector_offset) / XTS_SECTOR_SIZE, XTS_SECTOR_SIZE, Op::Decrypt); |
|||
const size_t read = XTS_SECTOR_SIZE - sector_offset; |
|||
|
|||
if (length + sector_offset < XTS_SECTOR_SIZE) { |
|||
std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read)); |
|||
return std::min<u64>(length, read); |
|||
} |
|||
std::memcpy(data, block.data() + sector_offset, read); |
|||
return read + Read(data + read, length - read, offset + read); |
|||
} |
|||
} // namespace Core::Crypto
|
|||
@ -0,0 +1,25 @@ |
|||
// Copyright 2018 yuzu emulator team |
|||
// Licensed under GPLv2 or any later version |
|||
// Refer to the license.txt file included. |
|||
|
|||
#pragma once |
|||
|
|||
#include "core/crypto/aes_util.h" |
|||
#include "core/crypto/encryption_layer.h" |
|||
#include "core/crypto/key_manager.h" |
|||
|
|||
namespace Core::Crypto { |
|||
|
|||
// Sits on top of a VirtualFile and provides XTS-mode AES decription. |
|||
class XTSEncryptionLayer : public EncryptionLayer { |
|||
public: |
|||
XTSEncryptionLayer(FileSys::VirtualFile base, Key256 key); |
|||
|
|||
size_t Read(u8* data, size_t length, size_t offset) const override; |
|||
|
|||
private: |
|||
// Must be mutable as operations modify cipher contexts. |
|||
mutable AESCipher<Key256> cipher; |
|||
}; |
|||
|
|||
} // namespace Core::Crypto |
|||
@ -0,0 +1,169 @@ |
|||
// Copyright 2018 yuzu emulator team
|
|||
// Licensed under GPLv2 or any later version
|
|||
// Refer to the license.txt file included.
|
|||
|
|||
#include <algorithm>
|
|||
#include <array>
|
|||
#include <cstring>
|
|||
#include <regex>
|
|||
#include <string>
|
|||
#include <mbedtls/md.h>
|
|||
#include <mbedtls/sha256.h>
|
|||
#include "common/assert.h"
|
|||
#include "common/hex_util.h"
|
|||
#include "common/logging/log.h"
|
|||
#include "core/crypto/aes_util.h"
|
|||
#include "core/crypto/xts_encryption_layer.h"
|
|||
#include "core/file_sys/partition_filesystem.h"
|
|||
#include "core/file_sys/vfs_offset.h"
|
|||
#include "core/file_sys/xts_archive.h"
|
|||
#include "core/loader/loader.h"
|
|||
|
|||
namespace FileSys { |
|||
|
|||
constexpr u64 NAX_HEADER_PADDING_SIZE = 0x4000; |
|||
|
|||
template <typename SourceData, typename SourceKey, typename Destination> |
|||
static bool CalculateHMAC256(Destination* out, const SourceKey* key, size_t key_length, |
|||
const SourceData* data, size_t data_length) { |
|||
mbedtls_md_context_t context; |
|||
mbedtls_md_init(&context); |
|||
|
|||
const auto key_f = reinterpret_cast<const u8*>(key); |
|||
const std::vector<u8> key_v(key_f, key_f + key_length); |
|||
|
|||
if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) || |
|||
mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) || |
|||
mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) || |
|||
mbedtls_md_hmac_finish(&context, reinterpret_cast<u8*>(out))) { |
|||
mbedtls_md_free(&context); |
|||
return false; |
|||
} |
|||
|
|||
mbedtls_md_free(&context); |
|||
return true; |
|||
} |
|||
|
|||
NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { |
|||
std::string path = FileUtil::SanitizePath(file->GetFullPath()); |
|||
static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca", |
|||
std::regex_constants::ECMAScript | |
|||
std::regex_constants::icase); |
|||
std::smatch match; |
|||
if (!std::regex_search(path, match, nax_path_regex)) { |
|||
status = Loader::ResultStatus::ErrorBadNAXFilePath; |
|||
return; |
|||
} |
|||
|
|||
std::string two_dir = match[1]; |
|||
std::string nca_id = match[2]; |
|||
std::transform(two_dir.begin(), two_dir.end(), two_dir.begin(), ::toupper); |
|||
std::transform(nca_id.begin(), nca_id.end(), nca_id.begin(), ::tolower); |
|||
|
|||
status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id)); |
|||
} |
|||
|
|||
NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id) |
|||
: file(std::move(file_)), header(std::make_unique<NAXHeader>()) { |
|||
Core::Crypto::SHA256Hash hash{}; |
|||
mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); |
|||
status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], |
|||
Common::HexArrayToString(nca_id, false))); |
|||
} |
|||
|
|||
Loader::ResultStatus NAX::Parse(std::string_view path) { |
|||
if (file->ReadObject(header.get()) != sizeof(NAXHeader)) |
|||
return Loader::ResultStatus::ErrorBadNAXHeader; |
|||
|
|||
if (header->magic != Common::MakeMagic('N', 'A', 'X', '0')) |
|||
return Loader::ResultStatus::ErrorBadNAXHeader; |
|||
|
|||
if (file->GetSize() < NAX_HEADER_PADDING_SIZE + header->file_size) |
|||
return Loader::ResultStatus::ErrorIncorrectNAXFileSize; |
|||
|
|||
keys.DeriveSDSeedLazy(); |
|||
std::array<Core::Crypto::Key256, 2> sd_keys{}; |
|||
const auto sd_keys_res = Core::Crypto::DeriveSDKeys(sd_keys, keys); |
|||
if (sd_keys_res != Loader::ResultStatus::Success) { |
|||
return sd_keys_res; |
|||
} |
|||
|
|||
const auto enc_keys = header->key_area; |
|||
|
|||
size_t i = 0; |
|||
for (; i < sd_keys.size(); ++i) { |
|||
std::array<Core::Crypto::Key128, 2> nax_keys{}; |
|||
if (!CalculateHMAC256(nax_keys.data(), sd_keys[i].data(), 0x10, std::string(path).c_str(), |
|||
path.size())) { |
|||
return Loader::ResultStatus::ErrorNAXKeyHMACFailed; |
|||
} |
|||
|
|||
for (size_t j = 0; j < nax_keys.size(); ++j) { |
|||
Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(nax_keys[j], |
|||
Core::Crypto::Mode::ECB); |
|||
cipher.Transcode(enc_keys[j].data(), 0x10, header->key_area[j].data(), |
|||
Core::Crypto::Op::Decrypt); |
|||
} |
|||
|
|||
Core::Crypto::SHA256Hash validation{}; |
|||
if (!CalculateHMAC256(validation.data(), &header->magic, 0x60, sd_keys[i].data() + 0x10, |
|||
0x10)) { |
|||
return Loader::ResultStatus::ErrorNAXValidationHMACFailed; |
|||
} |
|||
if (header->hmac == validation) |
|||
break; |
|||
} |
|||
|
|||
if (i == 2) { |
|||
return Loader::ResultStatus::ErrorNAXKeyDerivationFailed; |
|||
} |
|||
|
|||
type = static_cast<NAXContentType>(i); |
|||
|
|||
Core::Crypto::Key256 final_key{}; |
|||
std::memcpy(final_key.data(), &header->key_area, final_key.size()); |
|||
const auto enc_file = |
|||
std::make_shared<OffsetVfsFile>(file, header->file_size, NAX_HEADER_PADDING_SIZE); |
|||
dec_file = std::make_shared<Core::Crypto::XTSEncryptionLayer>(enc_file, final_key); |
|||
|
|||
return Loader::ResultStatus::Success; |
|||
} |
|||
|
|||
Loader::ResultStatus NAX::GetStatus() const { |
|||
return status; |
|||
} |
|||
|
|||
VirtualFile NAX::GetDecrypted() const { |
|||
return dec_file; |
|||
} |
|||
|
|||
std::shared_ptr<NCA> NAX::AsNCA() const { |
|||
if (type == NAXContentType::NCA) |
|||
return std::make_shared<NCA>(GetDecrypted()); |
|||
return nullptr; |
|||
} |
|||
|
|||
NAXContentType NAX::GetContentType() const { |
|||
return type; |
|||
} |
|||
|
|||
std::vector<std::shared_ptr<VfsFile>> NAX::GetFiles() const { |
|||
return {dec_file}; |
|||
} |
|||
|
|||
std::vector<std::shared_ptr<VfsDirectory>> NAX::GetSubdirectories() const { |
|||
return {}; |
|||
} |
|||
|
|||
std::string NAX::GetName() const { |
|||
return file->GetName(); |
|||
} |
|||
|
|||
std::shared_ptr<VfsDirectory> NAX::GetParentDirectory() const { |
|||
return file->GetContainingDirectory(); |
|||
} |
|||
|
|||
bool NAX::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { |
|||
return false; |
|||
} |
|||
} // namespace FileSys
|
|||
@ -0,0 +1,69 @@ |
|||
// Copyright 2018 yuzu emulator team |
|||
// Licensed under GPLv2 or any later version |
|||
// Refer to the license.txt file included. |
|||
|
|||
#pragma once |
|||
|
|||
#include <array> |
|||
#include <vector> |
|||
#include "common/common_types.h" |
|||
#include "common/swap.h" |
|||
#include "core/crypto/key_manager.h" |
|||
#include "core/file_sys/content_archive.h" |
|||
#include "core/file_sys/vfs.h" |
|||
#include "core/loader/loader.h" |
|||
|
|||
namespace FileSys { |
|||
|
|||
struct NAXHeader { |
|||
std::array<u8, 0x20> hmac; |
|||
u64_le magic; |
|||
std::array<Core::Crypto::Key128, 2> key_area; |
|||
u64_le file_size; |
|||
INSERT_PADDING_BYTES(0x30); |
|||
}; |
|||
static_assert(sizeof(NAXHeader) == 0x80, "NAXHeader has incorrect size."); |
|||
|
|||
enum class NAXContentType : u8 { |
|||
Save = 0, |
|||
NCA = 1, |
|||
}; |
|||
|
|||
class NAX : public ReadOnlyVfsDirectory { |
|||
public: |
|||
explicit NAX(VirtualFile file); |
|||
explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id); |
|||
|
|||
Loader::ResultStatus GetStatus() const; |
|||
|
|||
VirtualFile GetDecrypted() const; |
|||
|
|||
std::shared_ptr<NCA> AsNCA() const; |
|||
|
|||
NAXContentType GetContentType() const; |
|||
|
|||
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; |
|||
|
|||
std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; |
|||
|
|||
std::string GetName() const override; |
|||
|
|||
std::shared_ptr<VfsDirectory> GetParentDirectory() const override; |
|||
|
|||
protected: |
|||
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; |
|||
|
|||
private: |
|||
Loader::ResultStatus Parse(std::string_view path); |
|||
|
|||
std::unique_ptr<NAXHeader> header; |
|||
|
|||
VirtualFile file; |
|||
Loader::ResultStatus status; |
|||
NAXContentType type; |
|||
|
|||
VirtualFile dec_file; |
|||
|
|||
Core::Crypto::KeyManager keys; |
|||
}; |
|||
} // namespace FileSys |
|||
@ -0,0 +1,66 @@ |
|||
// Copyright 2018 yuzu emulator team
|
|||
// Licensed under GPLv2 or any later version
|
|||
// Refer to the license.txt file included.
|
|||
|
|||
#include "common/logging/log.h"
|
|||
#include "core/file_sys/content_archive.h"
|
|||
#include "core/file_sys/romfs.h"
|
|||
#include "core/file_sys/xts_archive.h"
|
|||
#include "core/hle/kernel/process.h"
|
|||
#include "core/loader/nax.h"
|
|||
#include "core/loader/nca.h"
|
|||
|
|||
namespace Loader { |
|||
|
|||
AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file) |
|||
: AppLoader(file), nax(std::make_unique<FileSys::NAX>(file)), |
|||
nca_loader(std::make_unique<AppLoader_NCA>(nax->GetDecrypted())) {} |
|||
|
|||
AppLoader_NAX::~AppLoader_NAX() = default; |
|||
|
|||
FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { |
|||
FileSys::NAX nax(file); |
|||
|
|||
if (nax.GetStatus() == ResultStatus::Success && nax.AsNCA() != nullptr && |
|||
nax.AsNCA()->GetStatus() == ResultStatus::Success) { |
|||
return FileType::NAX; |
|||
} |
|||
|
|||
return FileType::Error; |
|||
} |
|||
|
|||
ResultStatus AppLoader_NAX::Load(Kernel::SharedPtr<Kernel::Process>& process) { |
|||
if (is_loaded) { |
|||
return ResultStatus::ErrorAlreadyLoaded; |
|||
} |
|||
|
|||
if (nax->GetStatus() != ResultStatus::Success) |
|||
return nax->GetStatus(); |
|||
|
|||
const auto nca = nax->AsNCA(); |
|||
if (nca == nullptr) { |
|||
if (!Core::Crypto::KeyManager::KeyFileExists(false)) |
|||
return ResultStatus::ErrorMissingProductionKeyFile; |
|||
return ResultStatus::ErrorNAXInconvertibleToNCA; |
|||
} |
|||
|
|||
if (nca->GetStatus() != ResultStatus::Success) |
|||
return nca->GetStatus(); |
|||
|
|||
const auto result = nca_loader->Load(process); |
|||
if (result != ResultStatus::Success) |
|||
return result; |
|||
|
|||
is_loaded = true; |
|||
|
|||
return ResultStatus::Success; |
|||
} |
|||
|
|||
ResultStatus AppLoader_NAX::ReadRomFS(FileSys::VirtualFile& dir) { |
|||
return nca_loader->ReadRomFS(dir); |
|||
} |
|||
|
|||
ResultStatus AppLoader_NAX::ReadProgramId(u64& out_program_id) { |
|||
return nca_loader->ReadProgramId(out_program_id); |
|||
} |
|||
} // namespace Loader
|
|||
@ -0,0 +1,48 @@ |
|||
// Copyright 2018 yuzu emulator team |
|||
// Licensed under GPLv2 or any later version |
|||
// Refer to the license.txt file included. |
|||
|
|||
#pragma once |
|||
|
|||
#include <memory> |
|||
#include "common/common_types.h" |
|||
#include "core/loader/loader.h" |
|||
|
|||
namespace FileSys { |
|||
|
|||
class NAX; |
|||
|
|||
} // namespace FileSys |
|||
|
|||
namespace Loader { |
|||
|
|||
class AppLoader_NCA; |
|||
|
|||
/// Loads a NAX file |
|||
class AppLoader_NAX final : public AppLoader { |
|||
public: |
|||
explicit AppLoader_NAX(FileSys::VirtualFile file); |
|||
~AppLoader_NAX() override; |
|||
|
|||
/** |
|||
* Returns the type of the file |
|||
* @param file std::shared_ptr<VfsFile> open file |
|||
* @return FileType found, or FileType::Error if this loader doesn't know it |
|||
*/ |
|||
static FileType IdentifyType(const FileSys::VirtualFile& file); |
|||
|
|||
FileType GetFileType() override { |
|||
return IdentifyType(file); |
|||
} |
|||
|
|||
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; |
|||
|
|||
ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; |
|||
ResultStatus ReadProgramId(u64& out_program_id) override; |
|||
|
|||
private: |
|||
std::unique_ptr<FileSys::NAX> nax; |
|||
std::unique_ptr<AppLoader_NCA> nca_loader; |
|||
}; |
|||
|
|||
} // namespace Loader |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue