2020-02-24 01:34:30 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-24 01:34:30 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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:
|
2021-10-09 22:40:06 +00:00
|
|
|
impl::StaticBigNum<ModulusSize * BITSIZEOF(u8)> m_modulus;
|
|
|
|
impl::StaticBigNum<ExponentSize * BITSIZEOF(u8)> m_exponent;
|
2020-02-24 01:34:30 +00:00
|
|
|
public:
|
|
|
|
RsaCalculator() { /* ... */ }
|
2021-10-09 22:40:06 +00:00
|
|
|
~RsaCalculator() { m_exponent.ClearToZero(); }
|
2020-02-24 01:34:30 +00:00
|
|
|
|
|
|
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
2021-10-09 22:40:06 +00:00
|
|
|
if (!m_modulus.Import(mod, mod_size) || m_modulus.IsZero()) {
|
2020-02-24 01:34:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-09 22:40:06 +00:00
|
|
|
if (!m_exponent.Import(exp, exp_size) || m_exponent.IsZero()) {
|
2020-02-24 01:34:30 +00:00
|
|
|
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);
|
|
|
|
|
2021-10-09 22:40:06 +00:00
|
|
|
return m_modulus.ExpMod(dst, src, size, m_exponent, static_cast<u32 *>(work_buf), work_buf_size);
|
2020-02-24 01:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|