From dd81610366dbcbf331be3234efe867d48b122be2 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 14 Apr 2020 02:45:28 -0700 Subject: [PATCH] loader: update for 10.0.0 --- .../include/stratosphere/fs/fs_code.hpp | 14 ++- .../include/stratosphere/fssystem.hpp | 1 + .../fssystem/fssystem_acid_sign_key.hpp | 111 ++++++++++++++++++ .../include/stratosphere/ldr/ldr_pm_api.hpp | 1 + .../include/stratosphere/ldr/ldr_types.hpp | 3 +- libstratosphere/source/fs/fs_code.cpp | 34 ++++-- libstratosphere/source/ldr/ldr_pm_api.cpp | 4 + .../crypto/crypto_rsa_pss_sha256_verifier.hpp | 16 +++ .../crypto/crypto_rsa_pss_verifier.hpp | 73 +++++++++++- .../crypto/impl/crypto_rsa_pss_impl.hpp | 19 +-- .../vapours/results/loader_results.hpp | 2 + 11 files changed, 249 insertions(+), 29 deletions(-) create mode 100644 libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp diff --git a/libstratosphere/include/stratosphere/fs/fs_code.hpp b/libstratosphere/include/stratosphere/fs/fs_code.hpp index cb5caf5a..6dde7c03 100644 --- a/libstratosphere/include/stratosphere/fs/fs_code.hpp +++ b/libstratosphere/include/stratosphere/fs/fs_code.hpp @@ -19,9 +19,17 @@ namespace ams::fs { - Result MountCode(const char *name, const char *path, ncm::ProgramId program_id); + struct CodeInfo { + u8 signature[crypto::Rsa2048PssSha256Verifier::SignatureSize]; + u8 hash[crypto::Rsa2048PssSha256Verifier::HashSize]; + bool is_signed; + u8 reserved[3]; + }; + static_assert(sizeof(CodeInfo) == crypto::Rsa2048PssSha256Verifier::SignatureSize + crypto::Rsa2048PssSha256Verifier::HashSize + 4); - Result MountCodeForAtmosphereWithRedirection(const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific); - Result MountCodeForAtmosphere(const char *name, const char *path, ncm::ProgramId program_id); + Result MountCode(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id); + + Result MountCodeForAtmosphereWithRedirection(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific); + Result MountCodeForAtmosphere(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id); } diff --git a/libstratosphere/include/stratosphere/fssystem.hpp b/libstratosphere/include/stratosphere/fssystem.hpp index 60a703ea..10385e0e 100644 --- a/libstratosphere/include/stratosphere/fssystem.hpp +++ b/libstratosphere/include/stratosphere/fssystem.hpp @@ -17,6 +17,7 @@ #pragma once #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp b/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp new file mode 100644 index 00000000..716d5061 --- /dev/null +++ b/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp @@ -0,0 +1,111 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::fssystem { + + constexpr inline size_t AcidSignatureKeyGenerationMax = 1; + + constexpr inline size_t AcidSignatureKeyModulusSize = 0x100; + + constexpr inline const u8 AcidSignatureKeyModulusDev[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = { + { + 0xD6, 0x34, 0xA5, 0x78, 0x6C, 0x68, 0xCE, 0x5A, 0xC2, 0x37, 0x17, 0xF3, 0x82, 0x45, 0xC6, 0x89, + 0xE1, 0x2D, 0x06, 0x67, 0xBF, 0xB4, 0x06, 0x19, 0x55, 0x6B, 0x27, 0x66, 0x0C, 0xA4, 0xB5, 0x87, + 0x81, 0x25, 0xF4, 0x30, 0xBC, 0x53, 0x08, 0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0xD1, 0xFB, 0xD7, 0x5D, + 0x11, 0x84, 0x64, 0xD2, 0x4D, 0xBB, 0x50, 0x00, 0x67, 0x54, 0xE2, 0x77, 0x89, 0xBA, 0x0B, 0xE7, + 0x05, 0x57, 0x9A, 0x22, 0x5A, 0xEC, 0x76, 0x1C, 0xFD, 0xE8, 0xA8, 0x18, 0x16, 0x41, 0x65, 0x03, + 0xFA, 0xC4, 0xA6, 0x31, 0x5C, 0x1A, 0x7F, 0xAB, 0x11, 0xC8, 0x4A, 0x99, 0xB9, 0xE6, 0xCF, 0x62, + 0x21, 0xA6, 0x72, 0x47, 0xDB, 0xBA, 0x96, 0x26, 0x4E, 0x2E, 0xD4, 0x8C, 0x46, 0xD6, 0xA7, 0x1A, + 0x6C, 0x32, 0xA7, 0xDF, 0x85, 0x1C, 0x03, 0xC3, 0x6D, 0xA9, 0xE9, 0x68, 0xF4, 0x17, 0x1E, 0xB2, + 0x70, 0x2A, 0xA1, 0xE5, 0xE1, 0xF3, 0x8F, 0x6F, 0x63, 0xAC, 0xEB, 0x72, 0x0B, 0x4C, 0x4A, 0x36, + 0x3C, 0x60, 0x91, 0x9F, 0x6E, 0x1C, 0x71, 0xEA, 0xD0, 0x78, 0x78, 0xA0, 0x2E, 0xC6, 0x32, 0x6B + } + }; + + constexpr inline const u8 AcidSignatureKeyModulusProd[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = { + { + 0xDD, 0xC8, 0xDD, 0xF2, 0x4E, 0x6D, 0xF0, 0xCA, 0x9E, 0xC7, 0x5D, 0xC7, 0x7B, 0xAD, 0xFE, 0x7D, + 0x23, 0x89, 0x69, 0xB6, 0xF2, 0x06, 0xA2, 0x02, 0x88, 0xE1, 0x55, 0x91, 0xAB, 0xCB, 0x4D, 0x50, + 0x2E, 0xFC, 0x9D, 0x94, 0x76, 0xD6, 0x4C, 0xD8, 0xFF, 0x10, 0xFA, 0x5E, 0x93, 0x0A, 0xB4, 0x57, + 0xAC, 0x51, 0xC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, 0xF6, 0xF3, 0xE1, 0xF6, + 0x5C, 0x07, 0xAA, 0x71, 0x6C, 0x13, 0xA4, 0xB1, 0xB3, 0x66, 0xBF, 0x90, 0x4C, 0x3D, 0xA2, 0xC4, + 0x0B, 0xB8, 0x3D, 0x7A, 0x8C, 0x19, 0xFA, 0xFF, 0x6B, 0xB9, 0x1F, 0x02, 0xCC, 0xB6, 0xD3, 0x0C, + 0x7D, 0x19, 0x1F, 0x47, 0xF9, 0xC7, 0x40, 0x01, 0xFA, 0x46, 0xEA, 0x0B, 0xD4, 0x02, 0xE0, 0x3D, + 0x30, 0x9A, 0x1A, 0x0F, 0xEA, 0xA7, 0x66, 0x55, 0xF7, 0xCB, 0x28, 0xE2, 0xBB, 0x99, 0xE4, 0x83, + 0xC3, 0x43, 0x03, 0xEE, 0xDC, 0x1F, 0x02, 0x23, 0xDD, 0xD1, 0x2D, 0x39, 0xA4, 0x65, 0x75, 0x03, + 0xEF, 0x37, 0x9C, 0x06, 0xD6, 0xFA, 0xA1, 0x15, 0xF0, 0xDB, 0x17, 0x47, 0x26, 0x4F, 0x49, 0x03 + } + }; + + static_assert(sizeof(AcidSignatureKeyModulusProd) == sizeof(AcidSignatureKeyModulusDev)); + + constexpr inline const u8 AcidSignatureKeyExponent[] = { + 0x01, 0x00, 0x01 + }; + + constexpr inline size_t AcidSignatureKeyExponentSize = util::size(AcidSignatureKeyExponent); + +} diff --git a/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp b/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp index 1b194755..a541310b 100644 --- a/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp +++ b/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp @@ -24,6 +24,7 @@ namespace ams::ldr::pm { Result GetProgramInfo(ProgramInfo *out, const ncm::ProgramLocation &loc); Result PinProgram(PinId *out, const ncm::ProgramLocation &loc); Result UnpinProgram(PinId pin_id); + Result SetEnabledProgramVerification(bool enabled); Result HasLaunchedProgram(bool *out, ncm::ProgramId program_id); /* Atmosphere extension API. */ diff --git a/libstratosphere/include/stratosphere/ldr/ldr_types.hpp b/libstratosphere/include/stratosphere/ldr/ldr_types.hpp index 462037f4..933fc1d9 100644 --- a/libstratosphere/include/stratosphere/ldr/ldr_types.hpp +++ b/libstratosphere/include/stratosphere/ldr/ldr_types.hpp @@ -221,7 +221,8 @@ namespace ams::ldr { }; u32 magic; - u8 reserved_04[8]; + u32 signature_key_generation; + u8 reserved_08[4]; u8 flags; u8 reserved_0D; u8 main_thread_priority; diff --git a/libstratosphere/source/fs/fs_code.cpp b/libstratosphere/source/fs/fs_code.cpp index fdc2bd12..c17245ef 100644 --- a/libstratosphere/source/fs/fs_code.cpp +++ b/libstratosphere/source/fs/fs_code.cpp @@ -20,14 +20,15 @@ namespace ams::fs { namespace { - Result OpenCodeFileSystemImpl(std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { + Result OpenCodeFileSystemImpl(CodeInfo *out_code_info, std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { /* Print a path suitable for the remote service. */ fssrv::sf::Path sf_path; R_TRY(FspPathPrintf(std::addressof(sf_path), "%s", path)); /* Open the filesystem using libnx bindings. */ + static_assert(sizeof(CodeInfo) == sizeof(::FsCodeInfo)); ::FsFileSystem fs; - R_TRY(fsldrOpenCodeFileSystem(program_id.value, sf_path.str, std::addressof(fs))); + R_TRY(fsldrOpenCodeFileSystem(reinterpret_cast<::FsCodeInfo *>(out_code_info), program_id.value, sf_path.str, std::addressof(fs))); /* Allocate a new filesystem wrapper. */ auto fsa = std::make_unique(fs); @@ -61,12 +62,12 @@ namespace ams::fs { return OpenPackageFileSystemImpl(out, sf_path.str); } - Result OpenSdCardCodeOrCodeFileSystemImpl(std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { + Result OpenSdCardCodeOrCodeFileSystemImpl(CodeInfo *out_code_info, std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { /* If we can open an sd card code fs, use it. */ R_SUCCEED_IF(R_SUCCEEDED(OpenSdCardCodeFileSystemImpl(out, program_id))); /* Otherwise, fall back to a normal code fs. */ - return OpenCodeFileSystemImpl(out, path, program_id); + return OpenCodeFileSystemImpl(out_code_info, out, path, program_id); } Result OpenHblCodeFileSystemImpl(std::unique_ptr *out) { @@ -226,7 +227,7 @@ namespace ams::fs { public: AtmosphereCodeFileSystem() : initialized(false) { /* ... */ } - Result Initialize(const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + Result Initialize(CodeInfo *out_code_info, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { AMS_ABORT_UNLESS(!this->initialized); /* If we're hbl, we need to open a hbl fs. */ @@ -238,7 +239,7 @@ namespace ams::fs { /* Open the code filesystem. */ std::unique_ptr fsa; - R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(out_code_info, std::addressof(fsa), path, program_id)); this->code_fs.emplace(std::move(fsa), program_id, is_specific); this->program_id = program_id; @@ -274,7 +275,10 @@ namespace ams::fs { } - Result MountCode(const char *name, const char *path, ncm::ProgramId program_id) { + Result MountCode(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -283,13 +287,16 @@ namespace ams::fs { /* Open the code file system. */ std::unique_ptr fsa; - R_TRY(OpenCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenCodeFileSystemImpl(out, std::addressof(fsa), path, program_id)); /* Register. */ return fsa::Register(name, std::move(fsa)); } - Result MountCodeForAtmosphereWithRedirection(const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + Result MountCodeForAtmosphereWithRedirection(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -301,13 +308,16 @@ namespace ams::fs { R_UNLESS(ams_code_fs != nullptr, fs::ResultAllocationFailureInCodeA()); /* Initialize the code file system. */ - R_TRY(ams_code_fs->Initialize(path, program_id, is_hbl, is_specific)); + R_TRY(ams_code_fs->Initialize(out, path, program_id, is_hbl, is_specific)); /* Register. */ return fsa::Register(name, std::move(ams_code_fs)); } - Result MountCodeForAtmosphere(const char *name, const char *path, ncm::ProgramId program_id) { + Result MountCodeForAtmosphere(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -316,7 +326,7 @@ namespace ams::fs { /* Open the code file system. */ std::unique_ptr fsa; - R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(out, std::addressof(fsa), path, program_id)); /* Create a wrapper fs. */ auto wrap_fsa = std::make_unique(std::move(fsa), program_id, false); diff --git a/libstratosphere/source/ldr/ldr_pm_api.cpp b/libstratosphere/source/ldr/ldr_pm_api.cpp index 4e14611a..d6d1c190 100644 --- a/libstratosphere/source/ldr/ldr_pm_api.cpp +++ b/libstratosphere/source/ldr/ldr_pm_api.cpp @@ -45,6 +45,10 @@ namespace ams::ldr::pm { return ldrPmAtmosphereGetProgramInfo(reinterpret_cast(out), reinterpret_cast(out_status), reinterpret_cast(&loc)); } + Result SetEnabledProgramVerification(bool enabled) { + return ldrPmSetEnabledProgramVerification(enabled); + } + Result AtmospherePinProgram(PinId *out, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) { static_assert(sizeof(*out) == sizeof(u64), "PinId definition!"); static_assert(sizeof(status) == sizeof(CfgOverrideStatus), "CfgOverrideStatus definition!"); diff --git a/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp b/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp index a2136e98..e25ca38d 100644 --- a/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp +++ b/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp @@ -42,6 +42,14 @@ namespace ams::crypto { return Rsa2048PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size, work_buf, work_buf_size); } + inline bool VerifyRsa2048PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + return Rsa2048PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size); + } + + inline bool VerifyRsa2048PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + return Rsa2048PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size, work_buf, work_buf_size); + } + inline bool VerifyRsa4096PssSha256(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size) { return Rsa4096PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size); } @@ -50,4 +58,12 @@ namespace ams::crypto { return Rsa4096PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size, work_buf, work_buf_size); } + inline bool VerifyRsa4096PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + return Rsa4096PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size); + } + + inline bool VerifyRsa4096PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + return Rsa4096PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size, work_buf, work_buf_size); + } + } diff --git a/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp b/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp index 61b4a4de..bd7ddcea 100644 --- a/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp +++ b/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp @@ -23,13 +23,14 @@ namespace ams::crypto { - template /* requires HashFunction */ + template /* requires HashFunction */ class RsaPssVerifier { NON_COPYABLE(RsaPssVerifier); NON_MOVEABLE(RsaPssVerifier); public: static constexpr size_t HashSize = Hash::HashSize; static constexpr size_t SaltSize = Hash::HashSize; + static constexpr size_t ModulusSize = _ModulusSize; static constexpr size_t SignatureSize = ModulusSize; static constexpr size_t MaximumExponentSize = 3; static constexpr size_t RequiredWorkBufferSize = RsaCalculator::RequiredWorkBufferSize; @@ -70,8 +71,15 @@ namespace ams::crypto { u8 message[SignatureSize]; ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; - return this->calculator.ExpMod(message, signature, SignatureSize) && - impl.Verify(message, sizeof(message), std::addressof(this->hash)); + if (!this->calculator.ExpMod(message, signature, SignatureSize)) { + return false; + } + + u8 calc_hash[Hash::HashSize]; + this->hash.GetHash(calc_hash, sizeof(calc_hash)); + ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); }; + + return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash)); } bool Verify(const void *signature, size_t size, void *work_buf, size_t work_buf_size) { @@ -83,8 +91,47 @@ namespace ams::crypto { u8 message[SignatureSize]; ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; - return this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size) && - impl.Verify(message, sizeof(message), std::addressof(this->hash)); + if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) { + return false; + } + + u8 calc_hash[Hash::HashSize]; + this->hash.GetHash(calc_hash, sizeof(calc_hash)); + ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); }; + + return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash)); + } + + bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size) { + AMS_ASSERT(this->state == State::Initialized); + AMS_ASSERT(size == SignatureSize); + ON_SCOPE_EXIT { this->state = State::Done; }; + + impl::RsaPssImpl impl; + u8 message[SignatureSize]; + ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; + + if (!this->calculator.ExpMod(message, signature, SignatureSize)) { + return false; + } + + return impl.Verify(message, sizeof(message), static_cast(hash), hash_size); + } + + bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + AMS_ASSERT(this->state == State::Initialized); + AMS_ASSERT(size == SignatureSize); + ON_SCOPE_EXIT { this->state = State::Done; }; + + impl::RsaPssImpl impl; + u8 message[SignatureSize]; + ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; + + if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) { + return false; + } + + return impl.Verify(message, sizeof(message), static_cast(hash), hash_size); } static bool Verify(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size) { @@ -104,6 +151,22 @@ namespace ams::crypto { verifier.Update(msg, msg_size); return verifier.Verify(sig, sig_size, work_buf, work_buf_size); } + + static bool VerifyWithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + RsaPssVerifier verifier; + if (!verifier.Initialize(mod, mod_size, exp, exp_size)) { + return false; + } + return verifier.VerifyWithHash(sig, sig_size, hash, hash_size); + } + + static bool VerifyWithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + RsaPssVerifier verifier; + if (!verifier.Initialize(mod, mod_size, exp, exp_size)) { + return false; + } + return verifier.VerifyWithHash(sig, sig_size, hash, hash_size, work_buf, work_buf_size); + } }; } diff --git a/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp b/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp index fc0af104..1ce73e72 100644 --- a/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp +++ b/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp @@ -31,19 +31,22 @@ namespace ams::crypto::impl { private: static constexpr u8 TailMagic = 0xBC; private: - static void ComputeHashWithPadding(void *dst, Hash *hash, const void *salt, size_t salt_size) { + static void ComputeHashWithPadding(void *dst, const u8 *user_hash, size_t user_hash_size, const void *salt, size_t salt_size) { + AMS_ASSERT(user_hash_size == HashSize); + /* Initialize our buffer. */ u8 buf[8 + HashSize]; std::memset(buf, 0, 8); - hash->GetHash(buf + 8, HashSize); + std::memcpy(buf + 8, user_hash, HashSize); ON_SCOPE_EXIT { ClearMemory(buf, sizeof(buf)); }; /* Calculate our hash. */ - hash->Initialize(); - hash->Update(buf, sizeof(buf)); - hash->Update(salt, salt_size); - hash->GetHash(dst, HashSize); + Hash hash; + hash.Initialize(); + hash.Update(buf, sizeof(buf)); + hash.Update(salt, salt_size); + hash.GetHash(dst, HashSize); } static void ApplyMGF1(u8 *dst, size_t dst_size, const void *src, size_t src_size) { @@ -71,7 +74,7 @@ namespace ams::crypto::impl { public: RsaPssImpl() { /* ... */ } - bool Verify(u8 *buf, size_t size, Hash *hash) { + bool Verify(u8 *buf, size_t size, const u8 *hash, size_t hash_size) { /* Validate sanity byte. */ bool is_valid = buf[size - 1] == TailMagic; @@ -112,7 +115,7 @@ namespace ams::crypto::impl { u8 cmp_hash[HashSize]; ON_SCOPE_EXIT { ClearMemory(cmp_hash, sizeof(cmp_hash)); }; - ComputeHashWithPadding(cmp_hash, hash, salt, salt_size); + ComputeHashWithPadding(cmp_hash, hash, hash_size, salt, salt_size); is_valid &= IsSameBytes(cmp_hash, h, HashSize); /* Succeed if all our checks succeeded. */ diff --git a/libvapours/include/vapours/results/loader_results.hpp b/libvapours/include/vapours/results/loader_results.hpp index 3a4be591..1ffa7ae1 100644 --- a/libvapours/include/vapours/results/loader_results.hpp +++ b/libvapours/include/vapours/results/loader_results.hpp @@ -31,6 +31,8 @@ namespace ams::ldr { R_DEFINE_ERROR_RESULT(NotPinned, 8); R_DEFINE_ERROR_RESULT(InvalidProgramId, 9); R_DEFINE_ERROR_RESULT(InvalidVersion, 10); + R_DEFINE_ERROR_RESULT(InvalidAcidSignature, 11); + R_DEFINE_ERROR_RESULT(InvalidNcaSignature, 12); R_DEFINE_ERROR_RESULT(InsufficientAddressSpace, 51); R_DEFINE_ERROR_RESULT(InvalidNro, 52);