fs: update HashGeneratorFactorySelector to reflect 14.0.0

This commit is contained in:
Michael Scire
2022-03-24 21:57:28 -07:00
parent 64c6ef2de7
commit 20e53fcd82
8 changed files with 140 additions and 101 deletions

View File

@@ -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>

View File

@@ -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);
@@ -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;
@@ -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;
};
}

View File

@@ -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();
@@ -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;
@@ -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:

View File

@@ -1,73 +0,0 @@
/*
* 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: 13.4.0.0 */
class Sha256HashGenerator final : public ::ams::fssystem::IHash256Generator, public ::ams::fs::impl::Newable {
NON_COPYABLE(Sha256HashGenerator);
NON_MOVEABLE(Sha256HashGenerator);
private:
crypto::Sha256Generator m_generator;
public:
Sha256HashGenerator() = 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);
}
};
class Sha256HashGeneratorFactory final : public IHash256GeneratorFactory, public ::ams::fs::impl::Newable {
NON_COPYABLE(Sha256HashGeneratorFactory);
NON_MOVEABLE(Sha256HashGeneratorFactory);
public:
Sha256HashGeneratorFactory() = default;
protected:
virtual std::unique_ptr<IHash256Generator> DoCreate() override {
return std::unique_ptr<IHash256Generator>(new Sha256HashGenerator());
}
virtual void DoGenerateHash(void *dst, size_t dst_size, const void *src, size_t src_size) override {
crypto::GenerateSha256(dst, dst_size, src, src_size);
}
};
class Sha256HashGeneratorFactorySelector final : public IHash256GeneratorFactorySelector, public ::ams::fs::impl::Newable {
NON_COPYABLE(Sha256HashGeneratorFactorySelector);
NON_MOVEABLE(Sha256HashGeneratorFactorySelector);
private:
Sha256HashGeneratorFactory m_factory;
public:
Sha256HashGeneratorFactorySelector() = default;
protected:
virtual IHash256GeneratorFactory *DoGetFactory() override {
return std::addressof(m_factory);
}
};
}

View File

@@ -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();
}
}
};
}