xts_encryption_layer: Implement XTSEncryptionLayer
This commit is contained in:
parent
10e5356e9a
commit
c4845df3d4
|
@ -0,0 +1,54 @@
|
||||||
|
// Copyright 2018 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "core/crypto/xts_encryption_layer.h"
|
||||||
|
|
||||||
|
namespace Core::Crypto {
|
||||||
|
|
||||||
|
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 % 0x4000 == 0) {
|
||||||
|
std::vector<u8> raw = base->ReadBytes(length, offset);
|
||||||
|
cipher.XTSTranscode(raw.data(), raw.size(), data, offset / 0x4000, 0x4000, Op::Decrypt);
|
||||||
|
return raw.size();
|
||||||
|
}
|
||||||
|
if (length > 0x4000) {
|
||||||
|
const auto rem = length % 0x4000;
|
||||||
|
const auto read = length - rem;
|
||||||
|
return Read(data, read, offset) + Read(data + read, rem, offset + read);
|
||||||
|
}
|
||||||
|
std::vector<u8> buffer = base->ReadBytes(0x4000, offset);
|
||||||
|
if (buffer.size() < 0x4000)
|
||||||
|
buffer.resize(0x4000);
|
||||||
|
cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / 0x4000, 0x4000,
|
||||||
|
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() < 0x4000)
|
||||||
|
block.resize(0x4000);
|
||||||
|
cipher.XTSTranscode(block.data(), block.size(), block.data(), (offset - sector_offset) / 0x4000,
|
||||||
|
0x4000, Op::Decrypt);
|
||||||
|
const size_t read = 0x4000 - sector_offset;
|
||||||
|
|
||||||
|
if (length + sector_offset < 0x4000) {
|
||||||
|
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,26 @@
|
||||||
|
// Copyright 2018 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#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
|
|
@ -178,7 +178,7 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting
|
||||||
return std::static_pointer_cast<VfsFile>(out);
|
return std::static_pointer_cast<VfsFile>(out);
|
||||||
}
|
}
|
||||||
case NCASectionCryptoType::XTS:
|
case NCASectionCryptoType::XTS:
|
||||||
// TODO(DarkLordZach): Implement XTSEncryptionLayer.
|
// TODO(DarkLordZach): Find a test case for XTS-encrypted NCAs
|
||||||
default:
|
default:
|
||||||
LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}",
|
LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}",
|
||||||
static_cast<u8>(s_header.raw.header.crypto_type));
|
static_cast<u8>(s_header.raw.header.crypto_type));
|
||||||
|
|
Loading…
Reference in New Issue