2018-08-16 21:01:32 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-08-25 02:15:32 +00:00
|
|
|
#include <algorithm>
|
2018-08-16 21:01:32 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/crypto/xts_encryption_layer.h"
|
|
|
|
|
|
|
|
namespace Core::Crypto {
|
|
|
|
|
2018-08-19 01:14:57 +00:00
|
|
|
constexpr u64 XTS_SECTOR_SIZE = 0x4000;
|
|
|
|
|
2018-08-16 21:01:32 +00:00
|
|
|
XTSEncryptionLayer::XTSEncryptionLayer(FileSys::VirtualFile base_, Key256 key_)
|
|
|
|
: EncryptionLayer(std::move(base_)), cipher(key_, Mode::XTS) {}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t XTSEncryptionLayer::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2018-08-16 21:01:32 +00:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const auto sector_offset = offset & 0x3FFF;
|
|
|
|
if (sector_offset == 0) {
|
2018-08-19 01:14:57 +00:00
|
|
|
if (length % XTS_SECTOR_SIZE == 0) {
|
2018-08-16 21:01:32 +00:00
|
|
|
std::vector<u8> raw = base->ReadBytes(length, offset);
|
2018-08-19 01:14:57 +00:00
|
|
|
cipher.XTSTranscode(raw.data(), raw.size(), data, offset / XTS_SECTOR_SIZE,
|
|
|
|
XTS_SECTOR_SIZE, Op::Decrypt);
|
2018-08-16 21:01:32 +00:00
|
|
|
return raw.size();
|
|
|
|
}
|
2018-08-19 01:14:57 +00:00
|
|
|
if (length > XTS_SECTOR_SIZE) {
|
|
|
|
const auto rem = length % XTS_SECTOR_SIZE;
|
2018-08-16 21:01:32 +00:00
|
|
|
const auto read = length - rem;
|
|
|
|
return Read(data, read, offset) + Read(data + read, rem, offset + read);
|
|
|
|
}
|
2018-08-19 01:14:57 +00:00
|
|
|
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);
|
2018-08-16 21:01:32 +00:00
|
|
|
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);
|
2018-08-19 01:14:57 +00:00
|
|
|
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);
|
2018-09-15 13:21:06 +00:00
|
|
|
const std::size_t read = XTS_SECTOR_SIZE - sector_offset;
|
2018-08-16 21:01:32 +00:00
|
|
|
|
2018-08-19 01:14:57 +00:00
|
|
|
if (length + sector_offset < XTS_SECTOR_SIZE) {
|
2018-08-16 21:01:32 +00:00
|
|
|
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
|