2020-05-11 22:04:51 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-05-11 22:04:51 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "fssystem_hierarchical_sha256_storage.hpp"
|
|
|
|
|
|
|
|
namespace ams::fssystem {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
s32 Log2(s32 value) {
|
|
|
|
AMS_ASSERT(value > 0);
|
|
|
|
AMS_ASSERT(util::IsPowerOfTwo(value));
|
|
|
|
|
|
|
|
s32 log = 0;
|
|
|
|
while ((value >>= 1) > 0) {
|
|
|
|
++log;
|
|
|
|
}
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
template<typename BaseStorageType>
|
|
|
|
Result HierarchicalSha256Storage<BaseStorageType>::Initialize(BaseStorageType *base_storages, s32 layer_count, size_t htbs, void *hash_buf, size_t hash_buf_size, fssystem::IHash256GeneratorFactory *hgf) {
|
2020-05-11 22:04:51 +00:00
|
|
|
/* Validate preconditions. */
|
|
|
|
AMS_ASSERT(layer_count == LayerCount);
|
|
|
|
AMS_ASSERT(util::IsPowerOfTwo(htbs));
|
|
|
|
AMS_ASSERT(hash_buf != nullptr);
|
2021-12-14 06:42:32 +00:00
|
|
|
AMS_ASSERT(hgf != nullptr);
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(layer_count);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Set size tracking members. */
|
2021-10-10 07:14:06 +00:00
|
|
|
m_hash_target_block_size = htbs;
|
|
|
|
m_log_size_ratio = Log2(m_hash_target_block_size / HashSize);
|
2021-12-14 06:42:32 +00:00
|
|
|
m_hash_generator_factory = hgf;
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Get the base storage size. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(base_storages[2]->GetSize(std::addressof(m_base_storage_size)));
|
2020-05-11 22:04:51 +00:00
|
|
|
{
|
2021-10-10 07:14:06 +00:00
|
|
|
auto size_guard = SCOPE_GUARD { m_base_storage_size = 0; };
|
|
|
|
R_UNLESS(m_base_storage_size <= static_cast<s64>(HashSize) << m_log_size_ratio << m_log_size_ratio, fs::ResultHierarchicalSha256BaseStorageTooLarge());
|
2020-05-11 22:04:51 +00:00
|
|
|
size_guard.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set hash buffer tracking members. */
|
2021-10-10 07:14:06 +00:00
|
|
|
m_base_storage = base_storages[2];
|
|
|
|
m_hash_buffer = static_cast<char *>(hash_buf);
|
|
|
|
m_hash_buffer_size = hash_buf_size;
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Read the master hash. */
|
|
|
|
u8 master_hash[HashSize];
|
|
|
|
R_TRY(base_storages[0]->Read(0, master_hash, HashSize));
|
|
|
|
|
|
|
|
/* Read and validate the data being hashed. */
|
|
|
|
s64 hash_storage_size;
|
|
|
|
R_TRY(base_storages[1]->GetSize(std::addressof(hash_storage_size)));
|
|
|
|
AMS_ASSERT(util::IsAligned(hash_storage_size, HashSize));
|
2021-10-10 07:14:06 +00:00
|
|
|
AMS_ASSERT(hash_storage_size <= m_hash_target_block_size);
|
|
|
|
AMS_ASSERT(hash_storage_size <= static_cast<s64>(m_hash_buffer_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(base_storages[1]->Read(0, m_hash_buffer, static_cast<size_t>(hash_storage_size)));
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Calculate and verify the master hash. */
|
|
|
|
u8 calc_hash[HashSize];
|
2021-12-14 06:42:32 +00:00
|
|
|
m_hash_generator_factory->GenerateHash(calc_hash, sizeof(calc_hash), m_hash_buffer, static_cast<size_t>(hash_storage_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
R_UNLESS(crypto::IsSameBytes(master_hash, calc_hash, HashSize), fs::ResultHierarchicalSha256HashVerificationFailed());
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
template<typename BaseStorageType>
|
|
|
|
Result HierarchicalSha256Storage<BaseStorageType>::Read(s64 offset, void *buffer, size_t size) {
|
2020-05-11 22:04:51 +00:00
|
|
|
/* Succeed if zero-size. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
|
|
|
|
|
|
|
/* Validate that we have a buffer to read into. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
|
|
|
|
/* Validate preconditions. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(util::IsAligned(offset, m_hash_target_block_size), fs::ResultInvalidArgument());
|
|
|
|
R_UNLESS(util::IsAligned(size, m_hash_target_block_size), fs::ResultInvalidArgument());
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Read the data. */
|
2022-02-11 03:43:00 +00:00
|
|
|
const size_t reduced_size = static_cast<size_t>(std::min<s64>(m_base_storage_size, util::AlignUp(offset + size, m_hash_target_block_size)) - offset);
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(m_base_storage->Read(offset, buffer, reduced_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Temporarily increase our thread priority. */
|
|
|
|
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
|
|
|
|
|
|
|
|
/* Setup tracking variables. */
|
|
|
|
auto cur_offset = offset;
|
|
|
|
auto remaining_size = reduced_size;
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
/* Generate the hash of the region we're validating. */
|
|
|
|
u8 hash[HashSize];
|
2021-10-10 07:14:06 +00:00
|
|
|
const auto cur_size = static_cast<size_t>(std::min<s64>(m_hash_target_block_size, remaining_size));
|
2021-12-14 06:42:32 +00:00
|
|
|
m_hash_generator_factory->GenerateHash(hash, sizeof(hash), static_cast<u8 *>(buffer) + (cur_offset - offset), cur_size);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
AMS_ASSERT(static_cast<size_t>(cur_offset >> m_log_size_ratio) < m_hash_buffer_size);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Check the hash. */
|
|
|
|
{
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_mutex);
|
2020-05-11 22:04:51 +00:00
|
|
|
auto clear_guard = SCOPE_GUARD { std::memset(buffer, 0, size); };
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(crypto::IsSameBytes(hash, std::addressof(m_hash_buffer[cur_offset >> m_log_size_ratio]), HashSize), fs::ResultHierarchicalSha256HashVerificationFailed());
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
clear_guard.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
cur_offset += cur_size;
|
|
|
|
remaining_size -= cur_size;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
template<typename BaseStorageType>
|
|
|
|
Result HierarchicalSha256Storage<BaseStorageType>::Write(s64 offset, const void *buffer, size_t size) {
|
2020-05-11 22:04:51 +00:00
|
|
|
/* Succeed if zero-size. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
|
|
|
|
|
|
|
/* Validate that we have a buffer to read into. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
|
|
|
|
/* Validate preconditions. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(util::IsAligned(offset, m_hash_target_block_size), fs::ResultInvalidArgument());
|
|
|
|
R_UNLESS(util::IsAligned(size, m_hash_target_block_size), fs::ResultInvalidArgument());
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Setup tracking variables. */
|
2022-02-11 03:49:05 +00:00
|
|
|
const size_t reduced_size = static_cast<size_t>(std::min<s64>(m_base_storage_size, util::AlignUp(offset + size, m_hash_target_block_size)) - offset);
|
2020-05-11 22:04:51 +00:00
|
|
|
auto cur_offset = offset;
|
|
|
|
auto remaining_size = reduced_size;
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
/* Generate the hash of the region we're validating. */
|
|
|
|
u8 hash[HashSize];
|
2021-10-10 07:14:06 +00:00
|
|
|
const auto cur_size = static_cast<size_t>(std::min<s64>(m_hash_target_block_size, remaining_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
{
|
|
|
|
/* Temporarily increase our thread priority. */
|
|
|
|
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
|
2021-12-14 06:42:32 +00:00
|
|
|
m_hash_generator_factory->GenerateHash(hash, sizeof(hash), static_cast<const u8 *>(buffer) + (cur_offset - offset), cur_size);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the data. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(m_base_storage->Write(cur_offset, static_cast<const u8 *>(buffer) + (cur_offset - offset), cur_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
/* Write the hash. */
|
|
|
|
{
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
std::memcpy(std::addressof(m_hash_buffer[cur_offset >> m_log_size_ratio]), hash, HashSize);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
cur_offset += cur_size;
|
|
|
|
remaining_size -= cur_size;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
template<typename BaseStorageType>
|
|
|
|
Result HierarchicalSha256Storage<BaseStorageType>::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
|
|
|
|
if (op_id == fs::OperationId::Invalidate) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_storage->OperateRange(fs::OperationId::Invalidate, offset, size));
|
2021-12-14 06:42:32 +00:00
|
|
|
} else {
|
|
|
|
/* Succeed if zero-size. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
/* Validate preconditions. */
|
|
|
|
R_UNLESS(util::IsAligned(offset, m_hash_target_block_size), fs::ResultInvalidArgument());
|
|
|
|
R_UNLESS(util::IsAligned(size, m_hash_target_block_size), fs::ResultInvalidArgument());
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
/* Determine size to use. */
|
|
|
|
const auto reduced_size = std::min<s64>(m_base_storage_size, util::AlignUp(offset + size, m_hash_target_block_size)) - offset;
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
/* Operate on the base storage. */
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_storage->OperateRange(dst, dst_size, op_id, offset, reduced_size, src, src_size));
|
2021-12-14 06:42:32 +00:00
|
|
|
}
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:42:32 +00:00
|
|
|
template class HierarchicalSha256Storage<fs::SubStorage>;
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|