Add missing includes and use const where applicable

This commit is contained in:
Zach Hilman 2018-07-29 19:00:09 -04:00
parent 150527ec19
commit 03149d3e4a
11 changed files with 40 additions and 24 deletions

View File

@ -4,11 +4,16 @@
#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,
@ -20,8 +25,6 @@ enum class Op {
Decrypt,
};
struct CipherContext;
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.");

View File

@ -2,7 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <string.h>
#include <cstring>
#include "common/assert.h"
#include "core/crypto/ctr_encryption_layer.h"
@ -33,11 +33,10 @@ size_t CTREncryptionLayer::Read(u8* data, size_t length, size_t offset) const {
size_t read = 0x10 - sector_offset;
if (length + sector_offset < 0x10) {
memcpy(data, block.data() + sector_offset, std::min<u64>(length, read));
std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read));
return read;
}
memcpy(data, block.data() + sector_offset, read);
std::memcpy(data, block.data() + sector_offset, read);
return read + Read(data + read, length - read, offset + read);
}

View File

@ -4,6 +4,7 @@
#pragma once
#include <vector>
#include "core/crypto/aes_util.h"
#include "core/crypto/encryption_layer.h"
#include "core/crypto/key_manager.h"

View File

@ -10,7 +10,8 @@ namespace Core::Crypto {
// Basically non-functional class that implements all of the methods that are irrelevant to an
// EncryptionLayer. Reduces duplicate code.
struct EncryptionLayer : public FileSys::VfsFile {
class EncryptionLayer : public FileSys::VfsFile {
public:
explicit EncryptionLayer(FileSys::VirtualFile base);
size_t Read(u8* data, size_t length, size_t offset) const override = 0;

View File

@ -2,9 +2,11 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <array>
#include <fstream>
#include <locale>
#include <sstream>
#include <string_view>
#include <mbedtls/sha256.h>
#include "common/assert.h"
#include "common/common_paths.h"
@ -86,17 +88,18 @@ void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) {
if (is_title_keys) {
auto rights_id_raw = HexStringToArray<16>(out[0]);
u128 rights_id = *reinterpret_cast<std::array<u64, 2>*>(&rights_id_raw);
u128 rights_id{};
std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size());
Key128 key = HexStringToArray<16>(out[1]);
SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
} else {
std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower);
if (s128_file_id.find(out[0]) != s128_file_id.end()) {
const auto index = s128_file_id[out[0]];
const auto index = s128_file_id.at(out[0]);
Key128 key = HexStringToArray<16>(out[1]);
SetKey(index.type, key, index.field1, index.field2);
} else if (s256_file_id.find(out[0]) != s256_file_id.end()) {
const auto index = s256_file_id[out[0]];
const auto index = s256_file_id.at(out[0]);
Key256 key = HexStringToArray<32>(out[1]);
SetKey(index.type, key, index.field1, index.field2);
}
@ -143,7 +146,7 @@ void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) {
s256_keys[{id, field1, field2}] = key;
}
std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
{"master_key_00", {S128KeyType::Master, 0, 0}},
{"master_key_01", {S128KeyType::Master, 1, 0}},
{"master_key_02", {S128KeyType::Master, 2, 0}},
@ -187,7 +190,7 @@ std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id
{"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}},
};
std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
{"header_key", {S256KeyType::Header, 0, 0}},
{"sd_card_save_key", {S256KeyType::SDSave, 0, 0}},
{"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}},

View File

@ -5,6 +5,7 @@
#pragma once
#include <array>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <fmt/format.h>
@ -50,7 +51,7 @@ struct KeyIndex {
std::string DebugInfo() const {
u8 key_size = 16;
if (std::is_same_v<KeyType, S256KeyType>)
if constexpr (std::is_same_v<KeyType, S256KeyType>)
key_size = 32;
return fmt::format("key_size={:02X}, key={:02X}, field1={:016X}, field2={:016X}", key_size,
static_cast<u8>(type), field1, field2);
@ -110,7 +111,7 @@ private:
void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename,
bool title);
static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id;
static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id;
const static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id;
const static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id;
};
} // namespace Core::Crypto

View File

@ -93,8 +93,9 @@ VirtualDir XCI::GetLogoPartition() const {
}
std::shared_ptr<NCA> XCI::GetNCAByType(NCAContentType type) const {
auto iter = std::find_if(ncas.begin(), ncas.end(),
[type](std::shared_ptr<NCA> nca) { return nca->GetType() == type; });
const auto iter =
std::find_if(ncas.begin(), ncas.end(),
[type](const std::shared_ptr<NCA>& nca) { return nca->GetType() == type; });
return iter == ncas.end() ? nullptr : *iter;
}

View File

@ -4,10 +4,13 @@
#pragma once
#include <array>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/content_archive.h"
#include "core/file_sys/vfs.h"
#include "core/loader/loader.h"
namespace FileSys {

View File

@ -76,7 +76,7 @@ bool IsValidNCA(const NCAHeader& header) {
return header.magic == Common::MakeMagic('N', 'C', 'A', '3');
}
Core::Crypto::Key128 NCA::GetKeyAreaKey(NCASectionCryptoType type) {
Core::Crypto::Key128 NCA::GetKeyAreaKey(NCASectionCryptoType type) const {
u8 master_key_id = header.crypto_type;
if (header.crypto_type_2 > master_key_id)
master_key_id = header.crypto_type_2;
@ -105,7 +105,7 @@ Core::Crypto::Key128 NCA::GetKeyAreaKey(NCASectionCryptoType type) {
return out;
}
VirtualFile NCA::Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset) {
VirtualFile NCA::Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset) const {
if (!encrypted)
return in;

View File

@ -17,6 +17,9 @@
#include "core/loader/loader.h"
namespace FileSys {
union NCASectionHeader;
enum class NCAContentType : u8 {
Program = 0,
Meta = 1,
@ -61,8 +64,6 @@ struct NCAHeader {
};
static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
union NCASectionHeader;
inline bool IsDirectoryExeFS(const std::shared_ptr<VfsDirectory>& pfs) {
// According to switchbrew, an exefs must only contain these two files:
return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
@ -94,6 +95,9 @@ protected:
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
private:
Core::Crypto::Key128 GetKeyAreaKey(NCASectionCryptoType type) const;
VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset) const;
std::vector<VirtualDir> dirs;
std::vector<VirtualFile> files;
@ -108,9 +112,6 @@ private:
bool encrypted;
Core::Crypto::KeyManager keys;
Core::Crypto::Key128 GetKeyAreaKey(NCASectionCryptoType type);
VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset);
};
} // namespace FileSys

View File

@ -4,7 +4,10 @@
#pragma once
#include <memory>
#include "common/common_types.h"
#include "core/file_sys/card_image.h"
#include "core/loader/loader.h"
#include "core/loader/nca.h"
namespace Loader {