Atmosphere/libraries/libstratosphere/source/fssystem/fssystem_aes_ctr_storage.cpp

193 lines
7.4 KiB
C++
Raw Normal View History

2020-04-06 10:58:52 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-04-06 10:58:52 +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>
namespace ams::fssystem {
template<typename BasePointer>
void AesCtrStorage<BasePointer>::MakeIv(void *dst, size_t dst_size, u64 upper, s64 offset) {
2020-04-06 10:58:52 +00:00
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(dst_size == IvSize);
AMS_ASSERT(offset >= 0);
AMS_UNUSED(dst_size);
2020-04-06 10:58:52 +00:00
const uintptr_t out_addr = reinterpret_cast<uintptr_t>(dst);
util::StoreBigEndian(reinterpret_cast<u64 *>(out_addr + 0), upper);
util::StoreBigEndian(reinterpret_cast<s64 *>(out_addr + sizeof(u64)), static_cast<s64>(offset / BlockSize));
}
template<typename BasePointer>
AesCtrStorage<BasePointer>::AesCtrStorage(BasePointer base, const void *key, size_t key_size, const void *iv, size_t iv_size) : m_base_storage(std::move(base)) {
AMS_ASSERT(m_base_storage != nullptr);
2020-04-06 10:58:52 +00:00
AMS_ASSERT(key != nullptr);
AMS_ASSERT(iv != nullptr);
AMS_ASSERT(key_size == KeySize);
AMS_ASSERT(iv_size == IvSize);
AMS_UNUSED(key_size, iv_size);
2020-04-06 10:58:52 +00:00
2021-10-10 07:14:06 +00:00
std::memcpy(m_key, key, KeySize);
std::memcpy(m_iv, iv, IvSize);
2020-04-06 10:58:52 +00:00
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::Read(s64 offset, void *buffer, size_t size) {
2020-04-06 12:44:33 +00:00
/* Allow zero-size reads. */
2020-04-06 10:58:52 +00:00
R_SUCCEED_IF(size == 0);
/* Ensure buffer is valid. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* We can only read at block aligned offsets. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidArgument());
/* Read the data. */
2021-10-10 07:14:06 +00:00
R_TRY(m_base_storage->Read(offset, buffer, size));
2020-04-06 10:58:52 +00:00
/* Prepare to decrypt the data, with temporarily increased priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
/* Setup the counter. */
char ctr[IvSize];
2021-10-10 07:14:06 +00:00
std::memcpy(ctr, m_iv, IvSize);
2020-04-06 10:58:52 +00:00
AddCounter(ctr, IvSize, offset / BlockSize);
/* Decrypt, ensure we decrypt correctly. */
2021-10-10 07:14:06 +00:00
auto dec_size = crypto::DecryptAes128Ctr(buffer, size, m_key, KeySize, ctr, IvSize, buffer, size);
R_UNLESS(size == dec_size, fs::ResultUnexpectedInAesCtrStorageA());
2020-04-06 10:58:52 +00:00
return ResultSuccess();
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::Write(s64 offset, const void *buffer, size_t size) {
2020-04-06 10:58:52 +00:00
/* Allow zero-size writes. */
R_SUCCEED_IF(size == 0);
/* Ensure buffer is valid. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* We can only write at block aligned offsets. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidArgument());
/* Get a pooled buffer. */
PooledBuffer pooled_buffer;
const bool use_work_buffer = !IsDeviceAddress(buffer);
if (use_work_buffer) {
pooled_buffer.Allocate(size, BlockSize);
}
/* Setup the counter. */
char ctr[IvSize];
2021-10-10 07:14:06 +00:00
std::memcpy(ctr, m_iv, IvSize);
2020-04-06 10:58:52 +00:00
AddCounter(ctr, IvSize, offset / BlockSize);
/* Loop until all data is written. */
size_t remaining = size;
s64 cur_offset = 0;
while (remaining > 0) {
/* Determine data we're writing and where. */
const size_t write_size = use_work_buffer ? std::min(pooled_buffer.GetSize(), remaining) : remaining;
void *write_buf = use_work_buffer ? pooled_buffer.GetBuffer() : const_cast<void *>(buffer);
/* Encrypt the data, with temporarily increased priority. */
{
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
2021-10-10 07:14:06 +00:00
auto enc_size = crypto::EncryptAes128Ctr(write_buf, write_size, m_key, KeySize, ctr, IvSize, reinterpret_cast<const char *>(buffer) + cur_offset, write_size);
R_UNLESS(enc_size == write_size, fs::ResultUnexpectedInAesCtrStorageA());
2020-04-06 10:58:52 +00:00
}
/* Write the encrypted data. */
2021-10-10 07:14:06 +00:00
R_TRY(m_base_storage->Write(offset + cur_offset, write_buf, write_size));
2020-04-06 10:58:52 +00:00
/* Advance. */
cur_offset += write_size;
remaining -= write_size;
if (remaining > 0) {
AddCounter(ctr, IvSize, write_size / BlockSize);
}
}
return ResultSuccess();
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::Flush() {
2021-10-10 07:14:06 +00:00
return m_base_storage->Flush();
2020-04-06 10:58:52 +00:00
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::SetSize(s64 size) {
AMS_UNUSED(size);
return fs::ResultUnsupportedSetSizeForAesCtrStorage();
2020-04-06 10:58:52 +00:00
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::GetSize(s64 *out) {
2021-10-10 07:14:06 +00:00
return m_base_storage->GetSize(out);
2020-04-06 10:58:52 +00:00
}
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
2020-04-06 10:58:52 +00:00
/* Handle the zero size case. */
if (size == 0) {
if (op_id == fs::OperationId::QueryRange) {
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Clear();
}
return ResultSuccess();
}
/* Ensure alignment. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidArgument());
switch (op_id) {
case fs::OperationId::QueryRange:
{
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
2021-10-10 07:14:06 +00:00
R_TRY(m_base_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
2020-04-06 10:58:52 +00:00
fs::QueryRangeInfo info;
info.Clear();
info.aes_ctr_key_type = static_cast<s32>(fs::AesCtrKeyTypeFlag::InternalKeyForSoftwareAes);
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Merge(info);
}
break;
default:
{
2021-10-10 07:14:06 +00:00
R_TRY(m_base_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
2020-04-06 10:58:52 +00:00
}
break;
}
return ResultSuccess();
}
template class AesCtrStorage<fs::IStorage *>;
template class AesCtrStorage<std::shared_ptr<fs::IStorage>>;
2020-04-06 10:58:52 +00:00
}