summaryrefslogtreecommitdiffstats
path: root/src/core/crypto/aes_util.h
blob: 9807b92341344f97df0199cedacf961a8362c2f4 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include "common/assert.h"
#include "core/file_sys/vfs.h"
#include "mbedtls/cipher.h"

namespace Crypto {

enum class Mode {
    CTR = MBEDTLS_CIPHER_AES_128_CTR,
    ECB = MBEDTLS_CIPHER_AES_128_ECB,
    XTS = MBEDTLS_CIPHER_AES_128_XTS,
};

enum class Op {
    ENCRYPT,
    DECRYPT,
};

template <typename Key, size_t KeySize = sizeof(Key)>
struct 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.");

    AESCipher(Key key, Mode mode) {
        mbedtls_cipher_init(&encryption_context);
        mbedtls_cipher_init(&decryption_context);

        ASSERT_MSG((mbedtls_cipher_setup(
                        &encryption_context,
                        mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode))) ||
                    mbedtls_cipher_setup(&decryption_context,
                                         mbedtls_cipher_info_from_type(
                                             static_cast<mbedtls_cipher_type_t>(mode)))) == 0,
                   "Failed to initialize mbedtls ciphers.");

        ASSERT(
            !mbedtls_cipher_setkey(&encryption_context, key.data(), KeySize * 8, MBEDTLS_ENCRYPT));
        ASSERT(
            !mbedtls_cipher_setkey(&decryption_context, key.data(), KeySize * 8, MBEDTLS_DECRYPT));
        //"Failed to set key on mbedtls ciphers.");
    }

    ~AESCipher() {
        mbedtls_cipher_free(&encryption_context);
        mbedtls_cipher_free(&decryption_context);
    }

    void SetIV(std::vector<u8> iv) {
        ASSERT_MSG((mbedtls_cipher_set_iv(&encryption_context, iv.data(), iv.size()) ||
                    mbedtls_cipher_set_iv(&decryption_context, iv.data(), iv.size())) == 0,
                   "Failed to set IV on mbedtls ciphers.");
    }

    template <typename Source, typename Dest>
    void Transcode(const Source* src, size_t size, Dest* dest, Op op) {
        size_t written = 0;

        const auto context = op == Op::ENCRYPT ? &encryption_context : &decryption_context;

        mbedtls_cipher_reset(context);

        if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) {
            mbedtls_cipher_update(context, reinterpret_cast<const u8*>(src), size,
                                  reinterpret_cast<u8*>(dest), &written);
            if (written != size)
                LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.",
                            size, written);
        } else {
            const auto block_size = mbedtls_cipher_get_block_size(context);

            for (size_t offset = 0; offset < size; offset += block_size) {
                auto length = std::min<size_t>(block_size, size - offset);
                mbedtls_cipher_update(context, reinterpret_cast<const u8*>(src) + offset, length,
                                      reinterpret_cast<u8*>(dest) + offset, &written);
                if (written != length)
                    LOG_WARNING(Crypto,
                                "Not all data was decrypted requested={:016X}, actual={:016X}.",
                                length, written);
            }
        }

        mbedtls_cipher_finish(context, nullptr, nullptr);
    }

    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) {
        if (size % sector_size > 0) {
            LOG_CRITICAL(Crypto, "Data size must be a multiple of sector size.");
            return;
        }

        for (size_t i = 0; i < size; i += sector_size) {
            SetIV(CalculateNintendoTweak(sector_id++));
            Transcode<u8, u8>(reinterpret_cast<const u8*>(src) + i, sector_size,
                              reinterpret_cast<u8*>(dest) + i, op);
        }
    }

private:
    mbedtls_cipher_context_t encryption_context;
    mbedtls_cipher_context_t decryption_context;

    static std::vector<u8> CalculateNintendoTweak(size_t sector_id) {
        std::vector<u8> out(0x10);
        for (size_t i = 0xF; i <= 0xF; --i) {
            out[i] = sector_id & 0xFF;
            sector_id >>= 8;
        }
        return out;
    }
};
} // namespace Crypto