diff options
author | bunnei <bunneidev@gmail.com> | 2018-08-04 20:33:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-04 20:33:11 +0200 |
commit | 2b06301dbfbfe79687219bf7783a6d1b47695401 (patch) | |
tree | 222cc27ecbc7f7e86d2edef8d36436600dee7d7a /src/core/crypto/aes_util.h | |
parent | Merge pull request #919 from lioncash/sign (diff) | |
parent | Add missing parameter to files.push_back() (diff) | |
download | yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.gz yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.bz2 yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.lz yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.xz yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.zst yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/crypto/aes_util.h | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h new file mode 100644 index 000000000..5b0b02738 --- /dev/null +++ b/src/core/crypto/aes_util.h @@ -0,0 +1,62 @@ +// 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/assert.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) { + Transcode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), op); + } + + void Transcode(const u8* src, size_t size, u8* dest, Op op); + + 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) { + 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; + + static std::vector<u8> CalculateNintendoTweak(size_t sector_id); +}; +} // namespace Core::Crypto |