2025-10-17 05:08:51 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2020-08-03 14:14:39 -04:00
|
|
|
#include <algorithm>
|
2018-07-29 19:00:09 -04:00
|
|
|
#include <cstring>
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/crypto/ctr_encryption_layer.h"
|
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
namespace Core::Crypto {
|
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
CTREncryptionLayer::CTREncryptionLayer(FileSys::VirtualFile base_, Key128 key_,
|
2021-05-16 01:46:30 -04:00
|
|
|
std::size_t base_offset_)
|
|
|
|
|
: EncryptionLayer(std::move(base_)), base_offset(base_offset_), cipher(key_, Mode::CTR) {}
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t CTREncryptionLayer::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2018-07-27 23:55:23 -04:00
|
|
|
if (length == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2025-10-17 05:08:51 +02:00
|
|
|
std::size_t total_read = 0;
|
|
|
|
|
// Handle an initial misaligned portion if needed.
|
|
|
|
|
if (auto const sector_offset = offset & 0xF; sector_offset != 0) {
|
|
|
|
|
const std::size_t aligned_off = offset - sector_offset;
|
|
|
|
|
std::array<u8, 0x10> block{};
|
|
|
|
|
if (auto const got = base->Read(block.data(), block.size(), aligned_off); got != 0) {
|
|
|
|
|
UpdateIV(base_offset + aligned_off);
|
|
|
|
|
cipher.Transcode(block.data(), got, block.data(), Op::Decrypt);
|
|
|
|
|
auto const to_copy = std::min<std::size_t>(length, got > sector_offset ? got - sector_offset : 0);
|
|
|
|
|
if (to_copy > 0) {
|
|
|
|
|
std::memcpy(data, block.data() + sector_offset, to_copy);
|
|
|
|
|
data += to_copy;
|
|
|
|
|
offset += to_copy;
|
|
|
|
|
length -= to_copy;
|
|
|
|
|
total_read += to_copy;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
2025-10-17 05:08:51 +02:00
|
|
|
if (length > 0) {
|
|
|
|
|
// Now aligned to 0x10
|
|
|
|
|
UpdateIV(base_offset + offset);
|
|
|
|
|
const std::size_t got = base->Read(data, length, offset);
|
|
|
|
|
if (got > 0) {
|
|
|
|
|
cipher.Transcode(data, got, data, Op::Decrypt);
|
|
|
|
|
total_read += got;
|
|
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
2025-10-17 05:08:51 +02:00
|
|
|
return total_read;
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 14:14:39 -04:00
|
|
|
void CTREncryptionLayer::SetIV(const IVData& iv_) {
|
|
|
|
|
iv = iv_;
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
void CTREncryptionLayer::UpdateIV(std::size_t offset) const {
|
2018-07-27 23:55:23 -04:00
|
|
|
offset >>= 4;
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < 8; ++i) {
|
2018-07-27 23:55:23 -04:00
|
|
|
iv[16 - i - 1] = offset & 0xFF;
|
|
|
|
|
offset >>= 8;
|
|
|
|
|
}
|
|
|
|
|
cipher.SetIV(iv);
|
|
|
|
|
}
|
2018-07-28 21:39:42 -04:00
|
|
|
} // namespace Core::Crypto
|