summaryrefslogtreecommitdiff
path: root/src/core/crypto
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-08-18 21:14:57 -0400
committerGravatar Zach Hilman2018-08-23 11:53:30 -0400
commit42dc856ce136c75f587649863ec5bd936ba8b07a (patch)
tree5b270705a38cc4187a5634f753c9caa2f398dfb8 /src/core/crypto
parentkey_manager: Add support for autogenerated keys (diff)
downloadyuzu-42dc856ce136c75f587649863ec5bd936ba8b07a.tar.gz
yuzu-42dc856ce136c75f587649863ec5bd936ba8b07a.tar.xz
yuzu-42dc856ce136c75f587649863ec5bd936ba8b07a.zip
crypto: Eliminate magic constants
Diffstat (limited to 'src/core/crypto')
-rw-r--r--src/core/crypto/key_manager.cpp2
-rw-r--r--src/core/crypto/xts_encryption_layer.cpp33
2 files changed, 19 insertions, 16 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index 994ac4eec..acf635a65 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -102,7 +102,7 @@ Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, const KeyManag
102 102
103 AESCipher<Key128> cipher(sd_kek, Mode::ECB); 103 AESCipher<Key128> cipher(sd_kek, Mode::ECB);
104 for (size_t i = 0; i < 2; ++i) { 104 for (size_t i = 0; i < 2; ++i) {
105 for (size_t j = 0; j < 0x20; ++j) 105 for (size_t j = 0; j < sd_key_sources[i].size(); ++j)
106 sd_key_sources[i][j] ^= sd_seed[j & 0xF]; 106 sd_key_sources[i][j] ^= sd_seed[j & 0xF];
107 cipher.Transcode(sd_key_sources[i].data(), sd_key_sources[i].size(), sd_keys[i].data(), 107 cipher.Transcode(sd_key_sources[i].data(), sd_key_sources[i].size(), sd_keys[i].data(),
108 Op::Decrypt); 108 Op::Decrypt);
diff --git a/src/core/crypto/xts_encryption_layer.cpp b/src/core/crypto/xts_encryption_layer.cpp
index 431099580..c6e5df1ce 100644
--- a/src/core/crypto/xts_encryption_layer.cpp
+++ b/src/core/crypto/xts_encryption_layer.cpp
@@ -8,6 +8,8 @@
8 8
9namespace Core::Crypto { 9namespace Core::Crypto {
10 10
11constexpr u64 XTS_SECTOR_SIZE = 0x4000;
12
11XTSEncryptionLayer::XTSEncryptionLayer(FileSys::VirtualFile base_, Key256 key_) 13XTSEncryptionLayer::XTSEncryptionLayer(FileSys::VirtualFile base_, Key256 key_)
12 : EncryptionLayer(std::move(base_)), cipher(key_, Mode::XTS) {} 14 : EncryptionLayer(std::move(base_)), cipher(key_, Mode::XTS) {}
13 15
@@ -17,34 +19,35 @@ size_t XTSEncryptionLayer::Read(u8* data, size_t length, size_t offset) const {
17 19
18 const auto sector_offset = offset & 0x3FFF; 20 const auto sector_offset = offset & 0x3FFF;
19 if (sector_offset == 0) { 21 if (sector_offset == 0) {
20 if (length % 0x4000 == 0) { 22 if (length % XTS_SECTOR_SIZE == 0) {
21 std::vector<u8> raw = base->ReadBytes(length, offset); 23 std::vector<u8> raw = base->ReadBytes(length, offset);
22 cipher.XTSTranscode(raw.data(), raw.size(), data, offset / 0x4000, 0x4000, Op::Decrypt); 24 cipher.XTSTranscode(raw.data(), raw.size(), data, offset / XTS_SECTOR_SIZE,
25 XTS_SECTOR_SIZE, Op::Decrypt);
23 return raw.size(); 26 return raw.size();
24 } 27 }
25 if (length > 0x4000) { 28 if (length > XTS_SECTOR_SIZE) {
26 const auto rem = length % 0x4000; 29 const auto rem = length % XTS_SECTOR_SIZE;
27 const auto read = length - rem; 30 const auto read = length - rem;
28 return Read(data, read, offset) + Read(data + read, rem, offset + read); 31 return Read(data, read, offset) + Read(data + read, rem, offset + read);
29 } 32 }
30 std::vector<u8> buffer = base->ReadBytes(0x4000, offset); 33 std::vector<u8> buffer = base->ReadBytes(XTS_SECTOR_SIZE, offset);
31 if (buffer.size() < 0x4000) 34 if (buffer.size() < XTS_SECTOR_SIZE)
32 buffer.resize(0x4000); 35 buffer.resize(XTS_SECTOR_SIZE);
33 cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / 0x4000, 0x4000, 36 cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / XTS_SECTOR_SIZE,
34 Op::Decrypt); 37 XTS_SECTOR_SIZE, Op::Decrypt);
35 std::memcpy(data, buffer.data(), std::min(buffer.size(), length)); 38 std::memcpy(data, buffer.data(), std::min(buffer.size(), length));
36 return std::min(buffer.size(), length); 39 return std::min(buffer.size(), length);
37 } 40 }
38 41
39 // offset does not fall on block boundary (0x4000) 42 // offset does not fall on block boundary (0x4000)
40 std::vector<u8> block = base->ReadBytes(0x4000, offset - sector_offset); 43 std::vector<u8> block = base->ReadBytes(0x4000, offset - sector_offset);
41 if (block.size() < 0x4000) 44 if (block.size() < XTS_SECTOR_SIZE)
42 block.resize(0x4000); 45 block.resize(XTS_SECTOR_SIZE);
43 cipher.XTSTranscode(block.data(), block.size(), block.data(), (offset - sector_offset) / 0x4000, 46 cipher.XTSTranscode(block.data(), block.size(), block.data(),
44 0x4000, Op::Decrypt); 47 (offset - sector_offset) / XTS_SECTOR_SIZE, XTS_SECTOR_SIZE, Op::Decrypt);
45 const size_t read = 0x4000 - sector_offset; 48 const size_t read = XTS_SECTOR_SIZE - sector_offset;
46 49
47 if (length + sector_offset < 0x4000) { 50 if (length + sector_offset < XTS_SECTOR_SIZE) {
48 std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read)); 51 std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read));
49 return std::min<u64>(length, read); 52 return std::min<u64>(length, read);
50 } 53 }