mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-10 07:06:34 +00:00
crypto: implement RSA-2048-PSS
This commit is contained in:
parent
d675aa3414
commit
f3629f863d
13 changed files with 692 additions and 3 deletions
|
@ -19,4 +19,5 @@
|
||||||
|
|
||||||
#include <vapours/crypto/crypto_memory_compare.hpp>
|
#include <vapours/crypto/crypto_memory_compare.hpp>
|
||||||
#include <vapours/crypto/crypto_memory_clear.hpp>
|
#include <vapours/crypto/crypto_memory_clear.hpp>
|
||||||
#include <vapours/crypto/impl/crypto_bignum.hpp>
|
#include <vapours/crypto/crypto_sha256_generator.hpp>
|
||||||
|
#include <vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp>
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/impl/crypto_bignum.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto {
|
||||||
|
|
||||||
|
template<size_t ModulusSize, size_t ExponentSize>
|
||||||
|
class RsaCalculator {
|
||||||
|
NON_COPYABLE(RsaCalculator);
|
||||||
|
NON_MOVEABLE(RsaCalculator);
|
||||||
|
public:
|
||||||
|
static constexpr inline size_t RequiredWorkBufferSize = 0x10 * ModulusSize;
|
||||||
|
private:
|
||||||
|
impl::StaticBigNum<ModulusSize * BITSIZEOF(u8)> modulus;
|
||||||
|
impl::StaticBigNum<ExponentSize * BITSIZEOF(u8)> exponent;
|
||||||
|
public:
|
||||||
|
RsaCalculator() { /* ... */ }
|
||||||
|
~RsaCalculator() { this->exponent.ClearToZero(); }
|
||||||
|
|
||||||
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
|
if (!this->modulus.Import(mod, mod_size) || this->modulus.IsZero()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!this->exponent.Import(exp, exp_size) || this->exponent.IsZero()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ExpMod(void *dst, const void *src, size_t size, void *work_buf, size_t work_buf_size) {
|
||||||
|
AMS_ASSERT(work_buf_size >= RequiredWorkBufferSize);
|
||||||
|
|
||||||
|
return this->modulus.ExpMod(dst, src, size, this->exponent, static_cast<u32 *>(work_buf), work_buf_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ExpMod(void *dst, const void *src, size_t size) {
|
||||||
|
u32 work_buf[RequiredWorkBufferSize / sizeof(u32)];
|
||||||
|
return this->ExpMod(dst, src, size, work_buf, sizeof(work_buf));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/crypto_rsa_calculator.hpp>
|
||||||
|
#include <vapours/crypto/crypto_rsa_pss_verifier.hpp>
|
||||||
|
#include <vapours/crypto/crypto_sha256_generator.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto {
|
||||||
|
|
||||||
|
namespace impl {
|
||||||
|
|
||||||
|
template<size_t Bits>
|
||||||
|
using RsaNPssSha256Verifier = ::ams::crypto::RsaPssVerifier<Bits / BITSIZEOF(u8), ::ams::crypto::Sha256Generator>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
using Rsa2048PssSha256Verifier = ::ams::crypto::impl::RsaNPssSha256Verifier<2048>;
|
||||||
|
using Rsa4096PssSha256Verifier = ::ams::crypto::impl::RsaNPssSha256Verifier<4096>;
|
||||||
|
|
||||||
|
inline bool VerifyRsa2048PssSha256(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 Rsa2048PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool VerifyRsa2048PssSha256(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, void *work_buf, size_t work_buf_size) {
|
||||||
|
return Rsa2048PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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, void *work_buf, size_t work_buf_size) {
|
||||||
|
return Rsa4096PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size, work_buf, work_buf_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/crypto_rsa_calculator.hpp>
|
||||||
|
#include <vapours/crypto/impl/crypto_rsa_pss_impl.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto {
|
||||||
|
|
||||||
|
template<size_t ModulusSize, typename Hash> /* requires HashFunction<Hash> */
|
||||||
|
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 SignatureSize = ModulusSize;
|
||||||
|
static constexpr size_t MaximumExponentSize = 3;
|
||||||
|
static constexpr size_t RequiredWorkBufferSize = RsaCalculator<ModulusSize, MaximumExponentSize>::RequiredWorkBufferSize;
|
||||||
|
private:
|
||||||
|
enum class State {
|
||||||
|
None,
|
||||||
|
Initialized,
|
||||||
|
Done,
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
RsaCalculator<ModulusSize, MaximumExponentSize> calculator;
|
||||||
|
Hash hash;
|
||||||
|
State state;
|
||||||
|
public:
|
||||||
|
RsaPssVerifier() : state(State::None) { /* ... */ }
|
||||||
|
~RsaPssVerifier() { }
|
||||||
|
|
||||||
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
|
this->hash.Initialize();
|
||||||
|
if (this->calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
|
this->state = State::Initialized;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update(const void *data, size_t size) {
|
||||||
|
return this->hash.Update(data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Verify(const void *signature, size_t size) {
|
||||||
|
AMS_ASSERT(this->state == State::Initialized);
|
||||||
|
AMS_ASSERT(size == SignatureSize);
|
||||||
|
ON_SCOPE_EXIT { this->state = State::Done; };
|
||||||
|
|
||||||
|
impl::RsaPssImpl<Hash> impl;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Verify(const void *signature, size_t 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<Hash> impl;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
RsaPssVerifier<ModulusSize, Hash> verifier;
|
||||||
|
if (!verifier.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
verifier.Update(msg, msg_size);
|
||||||
|
return verifier.Verify(sig, sig_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, void *work_buf, size_t work_buf_size) {
|
||||||
|
RsaPssVerifier<ModulusSize, Hash> verifier;
|
||||||
|
if (!verifier.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
verifier.Update(msg, msg_size);
|
||||||
|
return verifier.Verify(sig, sig_size, work_buf, work_buf_size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/impl/crypto_sha256_impl.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto {
|
||||||
|
|
||||||
|
class Sha256Generator {
|
||||||
|
private:
|
||||||
|
using Impl = impl::Sha256Impl;
|
||||||
|
public:
|
||||||
|
static constexpr size_t HashSize = Impl::HashSize;
|
||||||
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
|
|
||||||
|
static constexpr inline u8 Asn1Identifier[] = {
|
||||||
|
0x30, 0x31, /* Sequence, size 0x31 */
|
||||||
|
0x30, 0x0D, /* Sequence, size 0x0D */
|
||||||
|
0x06, 0x09, /* Object Identifier */
|
||||||
|
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* SHA-256 */
|
||||||
|
0x00, /* Null */
|
||||||
|
0x04, 0x20, /* Octet string, size 0x20 */
|
||||||
|
};
|
||||||
|
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
|
||||||
|
private:
|
||||||
|
Impl impl;
|
||||||
|
public:
|
||||||
|
Sha256Generator() { /* ... */ }
|
||||||
|
|
||||||
|
void Initialize() {
|
||||||
|
this->impl.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update(const void *data, size_t size) {
|
||||||
|
this->impl.Update(data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GetHash(void *dst, size_t size) {
|
||||||
|
this->impl.GetHash(dst, size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void GenerateSha256Hash(void *dst, size_t dst_size, const void *src, size_t src_size);
|
||||||
|
|
||||||
|
}
|
|
@ -147,7 +147,7 @@ namespace ams::crypto::impl {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t Bits>
|
template<size_t Bits>
|
||||||
class StackBigNum : public BigNum {
|
class StaticBigNum : public BigNum {
|
||||||
public:
|
public:
|
||||||
static constexpr size_t NumBits = Bits;
|
static constexpr size_t NumBits = Bits;
|
||||||
static constexpr size_t NumWords = util::AlignUp(NumBits, BitsPerWord) / BitsPerWord;
|
static constexpr size_t NumWords = util::AlignUp(NumBits, BitsPerWord) / BitsPerWord;
|
||||||
|
@ -155,7 +155,7 @@ namespace ams::crypto::impl {
|
||||||
private:
|
private:
|
||||||
Word word_buf[NumWords];
|
Word word_buf[NumWords];
|
||||||
public:
|
public:
|
||||||
constexpr StackBigNum() : word_buf() {
|
constexpr StaticBigNum() : word_buf() {
|
||||||
this->ReserveStatic(word_buf, NumWords);
|
this->ReserveStatic(word_buf, NumWords);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/crypto_memory_compare.hpp>
|
||||||
|
#include <vapours/crypto/crypto_memory_clear.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto::impl {
|
||||||
|
|
||||||
|
/* TODO: C++20
|
||||||
|
template<typename T>
|
||||||
|
concept HashFunction = requires(T &t, const void *cv, void *v, size_t sz) {
|
||||||
|
{ T::HashSize } -> std::same_as<size_t>;
|
||||||
|
{ T::BlockSize } -> std::same_as<size_t>;
|
||||||
|
{ t.Initialize() } -> std::same_as<void>;
|
||||||
|
{ t.Update(cv, sz) } -> std::same_as<void>;
|
||||||
|
{ t.GetHash(v, sz) } -> std::same_as<void>;
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/impl/crypto_hash_function.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto::impl {
|
||||||
|
|
||||||
|
template<typename Hash> /* requires HashFunction<Hash> */
|
||||||
|
class RsaPssImpl {
|
||||||
|
NON_COPYABLE(RsaPssImpl);
|
||||||
|
NON_MOVEABLE(RsaPssImpl);
|
||||||
|
public:
|
||||||
|
static constexpr size_t HashSize = Hash::HashSize;
|
||||||
|
private:
|
||||||
|
static constexpr u8 TailMagic = 0xBC;
|
||||||
|
private:
|
||||||
|
static void ComputeHashWithPadding(void *dst, Hash *hash, const void *salt, size_t salt_size) {
|
||||||
|
/* Initialize our buffer. */
|
||||||
|
u8 buf[8 + HashSize];
|
||||||
|
std::memset(buf, 0, 8);
|
||||||
|
hash->GetHash(buf + 8, 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ApplyMGF1(u8 *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
|
u8 buf[HashSize];
|
||||||
|
ON_SCOPE_EXIT { ClearMemory(buf, sizeof(buf)); };
|
||||||
|
|
||||||
|
const size_t required_iters = (dst_size + HashSize - 1) / HashSize;
|
||||||
|
for (size_t i = 0; i < required_iters; i++) {
|
||||||
|
Hash hash;
|
||||||
|
hash.Initialize();
|
||||||
|
hash.Update(src, src_size);
|
||||||
|
|
||||||
|
const u32 tmp = util::ConvertToBigEndian(static_cast<u32>(i));
|
||||||
|
hash.Update(std::addressof(tmp), sizeof(tmp));
|
||||||
|
|
||||||
|
hash.GetHash(buf, HashSize);
|
||||||
|
|
||||||
|
const size_t start = HashSize * i;
|
||||||
|
const size_t end = std::min(dst_size, start + HashSize);
|
||||||
|
for (size_t j = start; j < end; j++) {
|
||||||
|
dst[j] ^= buf[j - start];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
RsaPssImpl() { /* ... */ }
|
||||||
|
|
||||||
|
bool Verify(u8 *buf, size_t size, Hash *hash) {
|
||||||
|
/* Validate sanity byte. */
|
||||||
|
if (buf[size - 1] != TailMagic) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Decrypt maskedDB */
|
||||||
|
const size_t db_len = size - HashSize - 1;
|
||||||
|
u8 *db = buf;
|
||||||
|
u8 *h = db + db_len;
|
||||||
|
ApplyMGF1(db, db_len, h, HashSize);
|
||||||
|
|
||||||
|
/* Apply lmask. */
|
||||||
|
db[0] &= 0x7F;
|
||||||
|
|
||||||
|
/* Verify that DB is of the form 0000...0001 */
|
||||||
|
s32 salt_ofs = -1;
|
||||||
|
for (size_t i = 0; i < db_len; i++) {
|
||||||
|
if (db[i] != 0) {
|
||||||
|
salt_ofs = static_cast<s32>(i) + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (salt_ofs == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (db[salt_ofs - 1] != 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Verify salt. */
|
||||||
|
const u8 *salt = db + salt_ofs;
|
||||||
|
const size_t salt_size = db_len - salt_ofs;
|
||||||
|
if (salt_size == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Verify hash. */
|
||||||
|
u8 cmp_hash[HashSize];
|
||||||
|
ON_SCOPE_EXIT { ClearMemory(cmp_hash, sizeof(cmp_hash)); };
|
||||||
|
|
||||||
|
ComputeHashWithPadding(cmp_hash, hash, salt, salt_size);
|
||||||
|
return IsSameBytes(cmp_hash, h, HashSize);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util.hpp>
|
||||||
|
#include <vapours/crypto/impl/crypto_hash_function.hpp>
|
||||||
|
#include <vapours/crypto/crypto_memory_clear.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto::impl {
|
||||||
|
|
||||||
|
class Sha256Impl {
|
||||||
|
public:
|
||||||
|
static constexpr size_t HashSize = 0x20;
|
||||||
|
static constexpr size_t BlockSize = 0x40;
|
||||||
|
private:
|
||||||
|
struct State {
|
||||||
|
u32 intermediate_hash[HashSize / sizeof(u32)];
|
||||||
|
u8 buffer[BlockSize];
|
||||||
|
u64 bits_consumed;
|
||||||
|
size_t num_buffered;
|
||||||
|
bool finalized;
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
State state;
|
||||||
|
public:
|
||||||
|
Sha256Impl() { /* ... */ }
|
||||||
|
~Sha256Impl() {
|
||||||
|
static_assert(std::is_trivially_destructible<State>::value);
|
||||||
|
ClearMemory(std::addressof(this->state), sizeof(this->state));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Initialize();
|
||||||
|
void Update(const void *data, size_t size);
|
||||||
|
void GetHash(void *dst, size_t size);
|
||||||
|
};
|
||||||
|
|
||||||
|
/* static_assert(HashFunction<Sha256Impl>); */
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
#include <vapours/util/util_alignment.hpp>
|
#include <vapours/util/util_alignment.hpp>
|
||||||
#include <vapours/util/util_size.hpp>
|
#include <vapours/util/util_size.hpp>
|
||||||
|
#include <vapours/util/util_endian.hpp>
|
||||||
#include <vapours/util/util_fourcc.hpp>
|
#include <vapours/util/util_fourcc.hpp>
|
||||||
#include <vapours/util/util_bitpack.hpp>
|
#include <vapours/util/util_bitpack.hpp>
|
||||||
#include <vapours/util/util_bitset.hpp>
|
#include <vapours/util/util_bitset.hpp>
|
||||||
|
|
121
libraries/libvapours/include/vapours/util/util_endian.hpp
Normal file
121
libraries/libvapours/include/vapours/util/util_endian.hpp
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
/*
|
||||||
|
* 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/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
|
||||||
|
namespace ams::util {
|
||||||
|
|
||||||
|
/* TODO: C++20 std::endian */
|
||||||
|
|
||||||
|
constexpr bool IsLittleEndian() {
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool IsBigEndian() {
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static_assert(IsLittleEndian() ^ IsBigEndian());
|
||||||
|
|
||||||
|
template<typename U> /* requires unsigned_integral<U> */
|
||||||
|
constexpr ALWAYS_INLINE U SwapBytes(const U u) {
|
||||||
|
static_assert(BITSIZEOF(u8) == 8);
|
||||||
|
constexpr U ByteMask = 0xFFu;
|
||||||
|
if constexpr (std::is_same<U, u64>::value) {
|
||||||
|
return ((u & (ByteMask << 56)) >> 56) |
|
||||||
|
((u & (ByteMask << 48)) >> 40) |
|
||||||
|
((u & (ByteMask << 40)) >> 24) |
|
||||||
|
((u & (ByteMask << 32)) >> 8) |
|
||||||
|
((u & (ByteMask << 24)) << 8) |
|
||||||
|
((u & (ByteMask << 16)) << 24) |
|
||||||
|
((u & (ByteMask << 8)) << 40) |
|
||||||
|
((u & (ByteMask << 0)) << 56);
|
||||||
|
|
||||||
|
} else if constexpr (std::is_same<U, u32>::value) {
|
||||||
|
return ((u & (ByteMask << 24)) >> 24) |
|
||||||
|
((u & (ByteMask << 16)) >> 8) |
|
||||||
|
((u & (ByteMask << 8)) << 8) |
|
||||||
|
((u & (ByteMask << 0)) << 24);
|
||||||
|
|
||||||
|
} else if constexpr (std::is_same<U, u16>::value) {
|
||||||
|
return ((u & (ByteMask << 8)) >> 8) |
|
||||||
|
((u & (ByteMask << 0)) << 8);
|
||||||
|
|
||||||
|
} else if constexpr (std::is_same<U, u8>::value) {
|
||||||
|
return u;
|
||||||
|
} else {
|
||||||
|
static_assert(!std::is_same<U, U>::value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> /* requires integral<T> */
|
||||||
|
constexpr ALWAYS_INLINE void SwapBytes(T *ptr) {
|
||||||
|
using U = typename std::make_unsigned<T>::type;
|
||||||
|
|
||||||
|
*ptr = static_cast<T>(SwapBytes(static_cast<U>(*ptr)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) {
|
||||||
|
if constexpr (IsBigEndian()) {
|
||||||
|
return val;
|
||||||
|
} else {
|
||||||
|
static_assert(IsLittleEndian());
|
||||||
|
return SwapBytes(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) {
|
||||||
|
if constexpr (IsBigEndian()) {
|
||||||
|
return SwapBytes(val);
|
||||||
|
} else {
|
||||||
|
static_assert(IsLittleEndian());
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> /* requires integral<T> */
|
||||||
|
constexpr ALWAYS_INLINE T LoadBigEndian(T *ptr) {
|
||||||
|
return ConvertToBigEndian(*ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> /* requires integral<T> */
|
||||||
|
constexpr ALWAYS_INLINE T LoadLittleEndian(T *ptr) {
|
||||||
|
return ConvertToLittleEndian(*ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) {
|
||||||
|
*ptr = ConvertToBigEndian(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) {
|
||||||
|
*ptr = ConvertToLittleEndian(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* 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 <vapours.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto {
|
||||||
|
|
||||||
|
void GenerateSha256Hash(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
|
Sha256Generator gen;
|
||||||
|
|
||||||
|
gen.Initialize();
|
||||||
|
gen.Update(src, src_size);
|
||||||
|
gen.GetHash(dst, dst_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* 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 <vapours.hpp>
|
||||||
|
|
||||||
|
namespace ams::crypto::impl {
|
||||||
|
|
||||||
|
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
||||||
|
|
||||||
|
void Sha256Impl::Initialize() {
|
||||||
|
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
||||||
|
::sha256ContextCreate(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sha256Impl::Update(const void *data, size_t size) {
|
||||||
|
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
||||||
|
::sha256ContextUpdate(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)), data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sha256Impl::GetHash(void *dst, size_t size) {
|
||||||
|
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
||||||
|
AMS_ASSERT(size >= HashSize);
|
||||||
|
::sha256ContextGetHash(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)), dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/* TODO: Non-EL0 implementation. */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue