fs: add AesCtrStorage

This commit is contained in:
Michael Scire 2020-04-06 03:58:52 -07:00
parent 496be5ecd4
commit 0e9974e7b3
9 changed files with 303 additions and 3 deletions

View file

@ -20,9 +20,9 @@
namespace ams::fs {
struct QueryRangeInfo {
u32 aes_ctr_key_type;
u32 speed_emulation_type;
u32 reserved[0x38 / sizeof(u32)];
s32 aes_ctr_key_type;
s32 speed_emulation_type;
u8 reserved[0x38];
void Clear() {
this->aes_ctr_key_type = 0;
@ -45,4 +45,8 @@ namespace ams::fs {
Result QueryRange(QueryRangeInfo *out, FileHandle handle, s64 offset, s64 size);
enum class AesCtrKeyTypeFlag : s32 {
InternalKeyForSoftwareAes = (1 << 0),
};
}

View file

@ -20,6 +20,8 @@
#include <stratosphere/fssystem/fssystem_partition_file_system.hpp>
#include <stratosphere/fssystem/fssystem_partition_file_system_meta.hpp>
#include <stratosphere/fssystem/fssystem_path_tool.hpp>
#include <stratosphere/fssystem/fssystem_thread_priority_changer.hpp>
#include <stratosphere/fssystem/fssystem_aes_ctr_storage.hpp>
#include <stratosphere/fssystem/fssystem_subdirectory_filesystem.hpp>
#include <stratosphere/fssystem/fssystem_directory_redirection_filesystem.hpp>
#include <stratosphere/fssystem/fssystem_directory_savedata_filesystem.hpp>

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018-2020 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/fs/fs_istorage.hpp>
#include <stratosphere/fs/impl/fs_newable.hpp>
namespace ams::fssystem {
class AesCtrStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(AesCtrStorage);
NON_MOVEABLE(AesCtrStorage);
public:
static constexpr size_t BlockSize = crypto::Aes128CtrEncryptor::BlockSize;
static constexpr size_t KeySize = crypto::Aes128CtrEncryptor::KeySize;
static constexpr size_t IvSize = crypto::Aes128CtrEncryptor::IvSize;
private:
IStorage * const base_storage;
char key[KeySize];
char iv[IvSize];
public:
static void MakeIv(void *dst, size_t dst_size, u64 upper, s64 offset);
public:
AesCtrStorage(IStorage *base, const void *key, size_t key_size, const void *iv, size_t iv_size);
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result Flush() override;
virtual Result SetSize(s64 size) override;
virtual Result GetSize(s64 *out) override;
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
};
}

View file

@ -105,4 +105,8 @@ namespace ams::fssystem {
void UnregisterAdditionalDeviceAddress(uintptr_t address);
bool IsAdditionalDeviceAddress(const void *ptr);
inline bool IsDeviceAddress(const void *buffer) {
return IsPooledBuffer(buffer) || IsAdditionalDeviceAddress(buffer);
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2018-2020 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/os.hpp>
namespace ams::fssystem {
class ScopedThreadPriorityChanger {
public:
enum class Mode {
Absolute,
Relative,
};
private:
/* TODO */
public:
ALWAYS_INLINE explicit ScopedThreadPriorityChanger(s32 priority, Mode mode) {
/* TODO */
}
ALWAYS_INLINE ~ScopedThreadPriorityChanger() {
/* TODO */
}
};
}

View file

@ -170,4 +170,6 @@ namespace ams::fssystem {
}
}
void AddCounter(void *counter, size_t counter_size, u64 value);
}

View file

@ -0,0 +1,179 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
void AesCtrStorage::MakeIv(void *dst, size_t dst_size, u64 upper, s64 offset) {
/* TODO: util::BytePtr? */
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(dst_size == IvSize);
AMS_ASSERT(offset >= 0);
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));
}
AesCtrStorage::AesCtrStorage(IStorage *base, const void *key, size_t key_size, const void *iv, size_t iv_size) : base_storage(base) {
AMS_ASSERT(base != nullptr);
AMS_ASSERT(key != nullptr);
AMS_ASSERT(iv != nullptr);
AMS_ASSERT(key_size == KeySize);
AMS_ASSERT(iv_size == IvSize);
std::memcpy(this->key, key, KeySize);
std::memcpy(this->iv, iv, IvSize);
}
Result AesCtrStorage::Read(s64 offset, void *buffer, size_t size) {
/* Allow zero-size writes. */
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. */
R_TRY(this->base_storage->Read(offset, buffer, size));
/* Prepare to decrypt the data, with temporarily increased priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
/* Setup the counter. */
char ctr[IvSize];
std::memcpy(ctr, this->iv, IvSize);
AddCounter(ctr, IvSize, offset / BlockSize);
/* Decrypt, ensure we decrypt correctly. */
auto dec_size = crypto::DecryptAes128Ctr(buffer, size, this->key, KeySize, ctr, IvSize, buffer, size);
AMS_ABORT_UNLESS(size == dec_size);
return ResultSuccess();
}
Result AesCtrStorage::Write(s64 offset, const void *buffer, size_t size) {
/* 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];
std::memcpy(ctr, this->iv, IvSize);
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);
auto enc_size = crypto::EncryptAes128Ctr(write_buf, write_size, this->key, KeySize, ctr, IvSize, reinterpret_cast<const char *>(buffer) + cur_offset, write_size);
AMS_ABORT_UNLESS(enc_size == write_size);
}
/* Write the encrypted data. */
R_TRY(this->base_storage->Write(offset + cur_offset, write_buf, write_size));
/* Advance. */
cur_offset += write_size;
remaining -= write_size;
if (remaining > 0) {
AddCounter(ctr, IvSize, write_size / BlockSize);
}
}
return ResultSuccess();
}
Result AesCtrStorage::Flush() {
return this->base_storage->Flush();
}
Result AesCtrStorage::SetSize(s64 size) {
return fs::ResultUnsupportedOperationInAesCtrStorageA();
}
Result AesCtrStorage::GetSize(s64 *out) {
return this->base_storage->GetSize(out);
}
Result AesCtrStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
/* 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());
R_TRY(this->base_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
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:
{
R_TRY(this->base_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
}
break;
}
return ResultSuccess();
}
}

View file

@ -149,4 +149,23 @@ namespace ams::fssystem {
return EnsureDirectoryRecursivelyImpl(fs, path, false);
}
void AddCounter(void *_counter, size_t counter_size, u64 value) {
u8 *counter = static_cast<u8 *>(_counter);
u64 remaining = value;
u8 carry = 0;
for (size_t i = 0; i < counter_size; i++) {
auto sum = counter[counter_size - 1 - i] + (remaining & 0xFF) + carry;
carry = static_cast<u8>(sum >> BITSIZEOF(u8));
auto sum8 = static_cast<u8>(sum & 0xFF);
counter[counter_size - 1 - i] = sum8;
remaining >>= BITSIZEOF(u8);
if (carry == 0 && remaining == 0) {
break;
}
}
}
}

View file

@ -258,6 +258,7 @@ namespace ams::fs {
R_DEFINE_ERROR_RESULT(UnsupportedOperationInMemoryStorageB, 6305);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileStorageA, 6306);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileStorageB, 6307);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInAesCtrStorageA, 6315);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileServiceObjectAdapterA, 6362);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemA, 6364);
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemB, 6365);