2019-04-24 13:38:11 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-04-24 13:38:11 +01: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/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-04-24 13:38:11 +01:00
|
|
|
#include "spl_ctr_drbg.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::spl {
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2019-06-21 09:36:00 +01:00
|
|
|
void CtrDrbg::Update(const void *data) {
|
2021-10-10 08:14:06 +01:00
|
|
|
aes128ContextCreate(std::addressof(m_aes_ctx), m_key, true);
|
|
|
|
for (size_t offset = 0; offset < sizeof(m_work[1]); offset += BlockSize) {
|
|
|
|
IncrementCounter(m_counter);
|
|
|
|
aes128EncryptBlock(std::addressof(m_aes_ctx), std::addressof(m_work[1][offset]), m_counter);
|
2019-06-21 09:36:00 +01:00
|
|
|
}
|
2019-04-24 13:38:11 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
Xor(m_work[1], data, sizeof(m_work[1]));
|
2019-04-24 13:38:11 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memcpy(m_key, std::addressof(m_work[1][0]), sizeof(m_key));
|
|
|
|
std::memcpy(m_counter, std::addressof(m_work[1][BlockSize]), sizeof(m_key));
|
2019-06-21 09:36:00 +01:00
|
|
|
}
|
2019-04-24 13:38:11 +01:00
|
|
|
|
2019-06-21 09:36:00 +01:00
|
|
|
void CtrDrbg::Initialize(const void *seed) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memcpy(m_work[0], seed, sizeof(m_work[0]));
|
|
|
|
std::memset(m_key, 0, sizeof(m_key));
|
|
|
|
std::memset(m_counter, 0, sizeof(m_counter));
|
|
|
|
this->Update(m_work[0]);
|
|
|
|
m_reseed_counter = 1;
|
2019-04-24 13:38:11 +01:00
|
|
|
}
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2019-06-21 09:36:00 +01:00
|
|
|
void CtrDrbg::Reseed(const void *seed) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memcpy(m_work[0], seed, sizeof(m_work[0]));
|
|
|
|
this->Update(m_work[0]);
|
|
|
|
m_reseed_counter = 1;
|
2019-04-24 13:38:11 +01:00
|
|
|
}
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2019-06-21 09:36:00 +01:00
|
|
|
bool CtrDrbg::GenerateRandomBytes(void *out, size_t size) {
|
|
|
|
if (size > MaxRequestSize) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_reseed_counter > ReseedInterval) {
|
2019-06-21 09:36:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
aes128ContextCreate(std::addressof(m_aes_ctx), m_key, true);
|
2019-06-21 09:36:00 +01:00
|
|
|
u8 *cur_dst = reinterpret_cast<u8 *>(out);
|
|
|
|
|
|
|
|
size_t aligned_size = (size & ~(BlockSize - 1));
|
|
|
|
for (size_t offset = 0; offset < aligned_size; offset += BlockSize) {
|
2021-10-10 08:14:06 +01:00
|
|
|
IncrementCounter(m_counter);
|
|
|
|
aes128EncryptBlock(std::addressof(m_aes_ctx), cur_dst, m_counter);
|
2019-06-21 09:36:00 +01:00
|
|
|
cur_dst += BlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > aligned_size) {
|
2021-10-10 08:14:06 +01:00
|
|
|
IncrementCounter(m_counter);
|
|
|
|
aes128EncryptBlock(std::addressof(m_aes_ctx), m_work[1], m_counter);
|
|
|
|
std::memcpy(cur_dst, m_work[1], size - aligned_size);
|
2019-06-21 09:36:00 +01:00
|
|
|
}
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memset(m_work[0], 0, sizeof(m_work[0]));
|
|
|
|
this->Update(m_work[0]);
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_reseed_counter++;
|
2019-06-21 09:36:00 +01:00
|
|
|
return true;
|
2019-04-25 05:00:39 +01:00
|
|
|
|
2019-06-21 09:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|