From c4845df3d4b9fc3fc19dd936af87090ffb3fbdf2 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 16 Aug 2018 17:01:32 -0400 Subject: xts_encryption_layer: Implement XTSEncryptionLayer --- src/core/crypto/xts_encryption_layer.cpp | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/core/crypto/xts_encryption_layer.cpp (limited to 'src/core/crypto/xts_encryption_layer.cpp') diff --git a/src/core/crypto/xts_encryption_layer.cpp b/src/core/crypto/xts_encryption_layer.cpp new file mode 100644 index 000000000..431099580 --- /dev/null +++ b/src/core/crypto/xts_encryption_layer.cpp @@ -0,0 +1,54 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "common/assert.h" +#include "core/crypto/xts_encryption_layer.h" + +namespace Core::Crypto { + +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 % 0x4000 == 0) { + std::vector raw = base->ReadBytes(length, offset); + cipher.XTSTranscode(raw.data(), raw.size(), data, offset / 0x4000, 0x4000, Op::Decrypt); + return raw.size(); + } + if (length > 0x4000) { + const auto rem = length % 0x4000; + const auto read = length - rem; + return Read(data, read, offset) + Read(data + read, rem, offset + read); + } + std::vector buffer = base->ReadBytes(0x4000, offset); + if (buffer.size() < 0x4000) + buffer.resize(0x4000); + cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / 0x4000, 0x4000, + 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 block = base->ReadBytes(0x4000, offset - sector_offset); + if (block.size() < 0x4000) + block.resize(0x4000); + cipher.XTSTranscode(block.data(), block.size(), block.data(), (offset - sector_offset) / 0x4000, + 0x4000, Op::Decrypt); + const size_t read = 0x4000 - sector_offset; + + if (length + sector_offset < 0x4000) { + std::memcpy(data, block.data() + sector_offset, std::min(length, read)); + return std::min(length, read); + } + std::memcpy(data, block.data() + sector_offset, read); + return read + Read(data + read, length - read, offset + read); +} +} // namespace Core::Crypto -- cgit v1.2.3 From 42dc856ce136c75f587649863ec5bd936ba8b07a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 18 Aug 2018 21:14:57 -0400 Subject: crypto: Eliminate magic constants --- src/core/crypto/xts_encryption_layer.cpp | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src/core/crypto/xts_encryption_layer.cpp') 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 @@ namespace Core::Crypto { +constexpr u64 XTS_SECTOR_SIZE = 0x4000; + XTSEncryptionLayer::XTSEncryptionLayer(FileSys::VirtualFile base_, Key256 key_) : EncryptionLayer(std::move(base_)), cipher(key_, Mode::XTS) {} @@ -17,34 +19,35 @@ size_t XTSEncryptionLayer::Read(u8* data, size_t length, size_t offset) const { const auto sector_offset = offset & 0x3FFF; if (sector_offset == 0) { - if (length % 0x4000 == 0) { + if (length % XTS_SECTOR_SIZE == 0) { std::vector raw = base->ReadBytes(length, offset); - cipher.XTSTranscode(raw.data(), raw.size(), data, offset / 0x4000, 0x4000, Op::Decrypt); + cipher.XTSTranscode(raw.data(), raw.size(), data, offset / XTS_SECTOR_SIZE, + XTS_SECTOR_SIZE, Op::Decrypt); return raw.size(); } - if (length > 0x4000) { - const auto rem = length % 0x4000; + 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 buffer = base->ReadBytes(0x4000, offset); - if (buffer.size() < 0x4000) - buffer.resize(0x4000); - cipher.XTSTranscode(buffer.data(), buffer.size(), buffer.data(), offset / 0x4000, 0x4000, - Op::Decrypt); + std::vector 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 block = base->ReadBytes(0x4000, offset - sector_offset); - if (block.size() < 0x4000) - block.resize(0x4000); - cipher.XTSTranscode(block.data(), block.size(), block.data(), (offset - sector_offset) / 0x4000, - 0x4000, Op::Decrypt); - const size_t read = 0x4000 - 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 < 0x4000) { + if (length + sector_offset < XTS_SECTOR_SIZE) { std::memcpy(data, block.data() + sector_offset, std::min(length, read)); return std::min(length, read); } -- cgit v1.2.3 From 6314a799aa7e20789562d2e877949dfebb6194ce Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 24 Aug 2018 22:15:32 -0400 Subject: file_sys/crypto: Fix missing/unnecessary includes --- src/core/crypto/xts_encryption_layer.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/crypto/xts_encryption_layer.cpp') diff --git a/src/core/crypto/xts_encryption_layer.cpp b/src/core/crypto/xts_encryption_layer.cpp index c6e5df1ce..c10832cfe 100644 --- a/src/core/crypto/xts_encryption_layer.cpp +++ b/src/core/crypto/xts_encryption_layer.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include "common/assert.h" #include "core/crypto/xts_encryption_layer.h" -- cgit v1.2.3