summaryrefslogtreecommitdiffstats
path: root/src/core/crypto/xts_encryption_layer.cpp
blob: c6e5df1ce26ea30d3b1976749d44368a076bd515 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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 {

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