Skip to content

Commit

Permalink
fs: update HashGeneratorFactorySelector to reflect 14.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SciresM committed Mar 25, 2022
1 parent 64c6ef2 commit 20e53fc
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
#include <stratosphere/fssystem/fssystem_buffered_storage.hpp>
#include <stratosphere/fssystem/fssystem_hierarchical_integrity_verification_storage.hpp>
#include <stratosphere/fssystem/fssystem_integrity_romfs_storage.hpp>
#include <stratosphere/fssystem/fssystem_sha256_hash_generator.hpp>
#include <stratosphere/fssystem/fssystem_sha_hash_generator.hpp>
#include <stratosphere/fssystem/fssystem_local_file_system.hpp>
#include <stratosphere/fssystem/fssystem_file_system_proxy_api.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

namespace ams::fssystem {

/* ACCURATE_TO_VERSION: 13.4.0.0 */
enum HashAlgorithmType : u8 {
HashAlgorithmType_Sha2 = 0,
HashAlgorithmType_Sha3 = 1,
};

/* ACCURATE_TO_VERSION: 14.3.0.0 */
class IHash256Generator {
public:
static constexpr size_t HashSize = 256 / BITSIZEOF(u8);
Expand Down Expand Up @@ -49,7 +54,7 @@ namespace ams::fssystem {
virtual void DoGetHash(void *dst, size_t dst_size) = 0;
};

/* ACCURATE_TO_VERSION: 13.4.0.0 */
/* ACCURATE_TO_VERSION: 14.3.0.0 */
class IHash256GeneratorFactory {
public:
constexpr IHash256GeneratorFactory() = default;
Expand Down Expand Up @@ -78,9 +83,9 @@ namespace ams::fssystem {
constexpr IHash256GeneratorFactorySelector() = default;
virtual constexpr ~IHash256GeneratorFactorySelector() { /* ... */ }

IHash256GeneratorFactory *GetFactory() { return this->DoGetFactory(); }
IHash256GeneratorFactory *GetFactory(HashAlgorithmType alg) { return this->DoGetFactory(alg); }
protected:
virtual IHash256GeneratorFactory *DoGetFactory() = 0;
virtual IHash256GeneratorFactory *DoGetFactory(HashAlgorithmType alg) = 0;
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace ams::fssystem {
NcaHeader::EncryptionType m_header_encryption_type;
bool m_is_header_sign1_signature_valid;
GetDecompressorFunction m_get_decompressor;
IHash256GeneratorFactory *m_hash_generator_factory;
IHash256GeneratorFactorySelector *m_hash_generator_factory_selector;
public:
NcaReader();
~NcaReader();
Expand Down Expand Up @@ -154,7 +154,7 @@ namespace ams::fssystem {
Result ReadHeader(NcaFsHeader *dst, s32 index) const;

GetDecompressorFunction GetDecompressor() const;
IHash256GeneratorFactory *GetHashGeneratorFactory() const;
IHash256GeneratorFactorySelector *GetHashGeneratorFactorySelector() const;

bool GetHeaderSign1Valid() const;

Expand Down Expand Up @@ -288,9 +288,9 @@ namespace ams::fssystem {
Result CreateIndirectStorageMetaStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaPatchInfo &patch_info);
Result CreateIndirectStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fssystem::IndirectStorage> *out_ind, std::shared_ptr<fs::IStorage> base_storage, std::shared_ptr<fs::IStorage> original_data_storage, std::shared_ptr<fs::IStorage> meta_storage, const NcaPatchInfo &patch_info);

Result CreateSha256Storage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::HierarchicalSha256Data &sha256_data);
Result CreateSha256Storage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::HierarchicalSha256Data &sha256_data, IHash256GeneratorFactory *hgf);

Result CreateIntegrityVerificationStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::IntegrityMetaInfo &meta_info);
Result CreateIntegrityVerificationStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::IntegrityMetaInfo &meta_info, IHash256GeneratorFactory *hgf);

Result CreateCompressedStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fssystem::CompressedStorage> *out_cmp, std::shared_ptr<fs::IStorage> *out_meta, std::shared_ptr<fs::IStorage> base_storage, const NcaCompressionInfo &compression_info);
public:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/fssystem/fssystem_i_hash_256_generator.hpp>

namespace ams::fssystem {

/* ACCURATE_TO_VERSION: 14.3.0.0 */

namespace impl {

template<typename Traits>
class ShaHashGenerator final : public ::ams::fssystem::IHash256Generator, public ::ams::fs::impl::Newable {
static_assert(Traits::Generator::HashSize == IHash256Generator::HashSize);
NON_COPYABLE(ShaHashGenerator);
NON_MOVEABLE(ShaHashGenerator);
private:
Traits::Generator m_generator;
public:
ShaHashGenerator() = default;
protected:
virtual void DoInitialize() override {
m_generator.Initialize();
}

virtual void DoUpdate(const void *data, size_t size) override {
m_generator.Update(data, size);
}

virtual void DoGetHash(void *dst, size_t dst_size) override {
m_generator.GetHash(dst, dst_size);
}
};

template<typename Traits>
class ShaHashGeneratorFactory final : public IHash256GeneratorFactory, public ::ams::fs::impl::Newable {
static_assert(Traits::Generator::HashSize == IHash256Generator::HashSize);
NON_COPYABLE(ShaHashGeneratorFactory);
NON_MOVEABLE(ShaHashGeneratorFactory);
public:
constexpr ShaHashGeneratorFactory() = default;
protected:
virtual std::unique_ptr<IHash256Generator> DoCreate() override {
return std::unique_ptr<IHash256Generator>(new ShaHashGenerator<Traits>());
}

virtual void DoGenerateHash(void *dst, size_t dst_size, const void *src, size_t src_size) override {
Traits::Generate(dst, dst_size, src, src_size);
}
};

struct Sha256Traits {
using Generator = crypto::Sha256Generator;

static ALWAYS_INLINE void Generate(void *dst, size_t dst_size, const void *src, size_t src_size) {
return crypto::GenerateSha256(dst, dst_size, src, src_size);
}
};

struct Sha3256Traits {
using Generator = crypto::Sha3256Generator;

static ALWAYS_INLINE void Generate(void *dst, size_t dst_size, const void *src, size_t src_size) {
return crypto::GenerateSha3256(dst, dst_size, src, src_size);
}
};
}

using Sha256HashGenerator = impl::ShaHashGenerator<impl::Sha256Traits>;
using Sha256HashGeneratorFactory = impl::ShaHashGeneratorFactory<impl::Sha256Traits>;

using Sha3256HashGenerator = impl::ShaHashGenerator<impl::Sha3256Traits>;
using Sha3256HashGeneratorFactory = impl::ShaHashGeneratorFactory<impl::Sha3256Traits>;

class ShaHashGeneratorFactorySelector final : public IHash256GeneratorFactorySelector, public ::ams::fs::impl::Newable {
NON_COPYABLE(ShaHashGeneratorFactorySelector);
NON_MOVEABLE(ShaHashGeneratorFactorySelector);
private:
Sha256HashGeneratorFactory m_sha256_factory;
Sha3256HashGeneratorFactory m_sha3_256_factory;
public:
constexpr ShaHashGeneratorFactorySelector() = default;
protected:
virtual IHash256GeneratorFactory *DoGetFactory(HashAlgorithmType alg) override {
switch (alg) {
case HashAlgorithmType_Sha2: return std::addressof(m_sha256_factory);
case HashAlgorithmType_Sha3: return std::addressof(m_sha3_256_factory);
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ namespace ams::fs::impl {

namespace {

constinit fssystem::Sha256HashGeneratorFactorySelector g_sha256_hash_generator_factory_selector;
constinit fssystem::ShaHashGeneratorFactorySelector g_sha_hash_generator_factory_selector;

}

fssystem::IHash256GeneratorFactorySelector *GetNcaHashGeneratorFactorySelector() {
return std::addressof(g_sha256_hash_generator_factory_selector);
return std::addressof(g_sha_hash_generator_factory_selector);
}

fssystem::IHash256GeneratorFactorySelector *GetSaveDataHashGeneratorFactorySelector() {
return std::addressof(g_sha256_hash_generator_factory_selector);
return std::addressof(g_sha_hash_generator_factory_selector);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ namespace ams::fssystem {
/* Process hash/integrity layer. */
switch (header_reader->GetHashType()) {
case NcaFsHeader::HashType::HierarchicalSha256Hash:
R_TRY(this->CreateSha256Storage(std::addressof(storage), std::move(storage), header_reader->GetHashData().hierarchical_sha256_data));
R_TRY(this->CreateSha256Storage(std::addressof(storage), std::move(storage), header_reader->GetHashData().hierarchical_sha256_data, m_hash_generator_factory_selector->GetFactory(fssystem::HashAlgorithmType_Sha2)));
break;
case NcaFsHeader::HashType::HierarchicalIntegrityHash:
R_TRY(this->CreateIntegrityVerificationStorage(std::addressof(storage), std::move(storage), header_reader->GetHashData().integrity_meta_info));
R_TRY(this->CreateIntegrityVerificationStorage(std::addressof(storage), std::move(storage), header_reader->GetHashData().integrity_meta_info, m_hash_generator_factory_selector->GetFactory(fssystem::HashAlgorithmType_Sha2)));
break;
default:
return fs::ResultInvalidNcaFsHeaderHashType();
Expand Down Expand Up @@ -988,7 +988,7 @@ namespace ams::fssystem {
return ResultSuccess();
}

Result NcaFileSystemDriver::CreateSha256Storage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::HierarchicalSha256Data &hash_data) {
Result NcaFileSystemDriver::CreateSha256Storage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::HierarchicalSha256Data &hash_data, IHash256GeneratorFactory *hgf) {
/* Validate preconditions. */
AMS_ASSERT(out != nullptr);
AMS_ASSERT(base_storage != nullptr);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ namespace ams::fssystem {
};

/* Initialize the verification storage. */
R_TRY(verification_storage->Initialize(layer_storages, util::size(layer_storages), hash_data.hash_block_size, buffer_hold_storage->GetBuffer(), hash_buffer_size, m_hash_generator_factory_selector->GetFactory()));
R_TRY(verification_storage->Initialize(layer_storages, util::size(layer_storages), hash_data.hash_block_size, buffer_hold_storage->GetBuffer(), hash_buffer_size, hgf));

/* Make the cache storage. */
auto cache_storage = fssystem::AllocateShared<CacheStorage>(std::move(verification_storage), hash_data.hash_block_size, static_cast<char *>(buffer_hold_storage->GetBuffer()) + hash_buffer_size, cache_buffer_size, CacheBlockCount);
Expand All @@ -1055,7 +1055,7 @@ namespace ams::fssystem {
return ResultSuccess();
}

Result NcaFileSystemDriver::CreateIntegrityVerificationStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::IntegrityMetaInfo &meta_info) {
Result NcaFileSystemDriver::CreateIntegrityVerificationStorage(std::shared_ptr<fs::IStorage> *out, std::shared_ptr<fs::IStorage> base_storage, const NcaFsHeader::HashData::IntegrityMetaInfo &meta_info, IHash256GeneratorFactory *hgf) {
/* Validate preconditions. */
AMS_ASSERT(out != nullptr);
AMS_ASSERT(base_storage != nullptr);
Expand Down Expand Up @@ -1094,7 +1094,7 @@ namespace ams::fssystem {
R_UNLESS(integrity_storage != nullptr, fs::ResultAllocationMemoryFailedAllocateShared());

/* Initialize the integrity storage. */
R_TRY(integrity_storage->Initialize(level_hash_info, meta_info.master_hash, storage_info, m_buffer_manager, m_hash_generator_factory_selector->GetFactory()));
R_TRY(integrity_storage->Initialize(level_hash_info, meta_info.master_hash, storage_info, m_buffer_manager, hgf));

/* Set the output. */
*out = std::move(integrity_storage);
Expand Down
20 changes: 10 additions & 10 deletions libraries/libstratosphere/source/fssystem/fssystem_nca_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace ams::fssystem {

}

NcaReader::NcaReader() : m_body_storage(), m_header_storage(), m_decrypt_aes_ctr(), m_decrypt_aes_ctr_external(), m_is_software_aes_prioritized(false), m_header_encryption_type(NcaHeader::EncryptionType::Auto), m_get_decompressor(), m_hash_generator_factory() {
NcaReader::NcaReader() : m_body_storage(), m_header_storage(), m_decrypt_aes_ctr(), m_decrypt_aes_ctr_external(), m_is_software_aes_prioritized(false), m_header_encryption_type(NcaHeader::EncryptionType::Auto), m_get_decompressor(), m_hash_generator_factory_selector() {
std::memset(std::addressof(m_header), 0, sizeof(m_header));
std::memset(std::addressof(m_decryption_keys), 0, sizeof(m_decryption_keys));
std::memset(std::addressof(m_external_decryption_key), 0, sizeof(m_external_decryption_key));
Expand Down Expand Up @@ -118,6 +118,9 @@ namespace ams::fssystem {
/* Validate the key index. */
R_UNLESS(m_header.key_index < NcaCryptoConfiguration::KeyAreaEncryptionKeyIndexCount, fs::ResultInvalidNcaKeyIndex());

/* Set our hash generator factory selector. */
m_hash_generator_factory_selector = hgf_selector;

/* Check if we have a rights id. */
constexpr const u8 ZeroRightsId[NcaHeader::RightsIdSize] = {};
if (crypto::IsSameBytes(ZeroRightsId, m_header.rights_id, NcaHeader::RightsIdSize)) {
Expand Down Expand Up @@ -145,10 +148,6 @@ namespace ams::fssystem {
/* Set our decompressor function getter. */
m_get_decompressor = compression_cfg.get_decompressor;

/* Set our hash generator factory. */
m_hash_generator_factory = hgf_selector->GetFactory();
AMS_ASSERT(m_hash_generator_factory != nullptr);

/* Set our storages. */
m_header_storage = std::move(work_header_storage);
m_body_storage = std::move(base_storage);
Expand Down Expand Up @@ -351,9 +350,9 @@ namespace ams::fssystem {
return m_get_decompressor;
}

IHash256GeneratorFactory *NcaReader::GetHashGeneratorFactory() const {
AMS_ASSERT(m_hash_generator_factory != nullptr);
return m_hash_generator_factory;
IHash256GeneratorFactorySelector *NcaReader::GetHashGeneratorFactorySelector() const {
AMS_ASSERT(m_hash_generator_factory_selector != nullptr);
return m_hash_generator_factory_selector;
}

NcaHeader::EncryptionType NcaReader::GetEncryptionType() const {
Expand Down Expand Up @@ -384,11 +383,12 @@ namespace ams::fssystem {
}

void NcaReader::GetHeaderSign2TargetHash(void *dst, size_t size) const {
AMS_ASSERT(m_hash_generator_factory != nullptr);
AMS_ASSERT(m_hash_generator_factory_selector!= nullptr);
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(size == IHash256Generator::HashSize);

return m_hash_generator_factory->GenerateHash(dst, size, static_cast<const void *>(std::addressof(m_header.magic)), NcaHeader::Size - NcaHeader::HeaderSignSize * NcaHeader::HeaderSignCount);
auto * const factory = m_hash_generator_factory_selector->GetFactory(fssystem::HashAlgorithmType_Sha2);
return factory->GenerateHash(dst, size, static_cast<const void *>(std::addressof(m_header.magic)), NcaHeader::Size - NcaHeader::HeaderSignSize * NcaHeader::HeaderSignCount);
}

Result NcaFsHeaderReader::Initialize(const NcaReader &reader, s32 index) {
Expand Down

0 comments on commit 20e53fc

Please sign in to comment.