summaryrefslogtreecommitdiffstats
path: root/src/core/crypto/aes_util.h
blob: 8ce9d6612acbefd50dec9b6b859a7050cd5586b3 (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
58
59
60
61
62
63
64
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <type_traits>
#include <vector>
#include "common/common_types.h"
#include "core/file_sys/vfs.h"

namespace Core::Crypto {

struct CipherContext;

enum class Mode {
    CTR = 11,
    ECB = 2,
    XTS = 70,
};

enum class Op {
    Encrypt,
    Decrypt,
};

template <typename Key, size_t KeySize = sizeof(Key)>
class AESCipher {
    static_assert(std::is_same_v<Key, std::array<u8, KeySize>>, "Key must be std::array of u8.");
    static_assert(KeySize == 0x10 || KeySize == 0x20, "KeySize must be 128 or 256.");

public:
    AESCipher(Key key, Mode mode);

    ~AESCipher();

    void SetIV(std::vector<u8> iv);

    template <typename Source, typename Dest>
    void Transcode(const Source* src, size_t size, Dest* dest, Op op) const {
        static_assert(std::is_trivially_copyable_v<Source> && std::is_trivially_copyable_v<Dest>,
                      "Transcode source and destination types must be trivially copyable.");
        Transcode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), op);
    }

    void Transcode(const u8* src, size_t size, u8* dest, Op op) const;

    template <typename Source, typename Dest>
    void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id,
                      size_t sector_size, Op op) {
        static_assert(std::is_trivially_copyable_v<Source> && std::is_trivially_copyable_v<Dest>,
                      "XTSTranscode source and destination types must be trivially copyable.");
        XTSTranscode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), sector_id,
                     sector_size, op);
    }

    void XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size,
                      Op op);

private:
    std::unique_ptr<CipherContext> ctx;
};
} // namespace Core::Crypto