1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-16 08:56:40 +00:00
Atmosphere/stratosphere/spl/source/spl_ctr_drbg.hpp

61 lines
2.1 KiB
C++
Raw Normal View History

2019-04-24 13:38:11 +01:00
/*
* Copyright (c) 2018-2020 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::spl {
2019-06-21 09:36:00 +01:00
/* Nintendo implements CTR_DRBG for their csrng. We will do the same. */
class CtrDrbg {
public:
static constexpr size_t MaxRequestSize = 0x10000;
static constexpr size_t ReseedInterval = 0x7FFFFFF0;
static constexpr size_t BlockSize = AES_BLOCK_SIZE;
static constexpr size_t SeedSize = 2 * AES_BLOCK_SIZE;
private:
Aes128Context aes_ctx;
u8 counter[BlockSize];
u8 key[BlockSize];
u8 work[2][SeedSize];
u32 reseed_counter;
private:
static void Xor(void *dst, const void *src, size_t size) {
const u8 *src_u8 = reinterpret_cast<const u8 *>(src);
u8 *dst_u8 = reinterpret_cast<u8 *>(dst);
for (size_t i = 0; i < size; i++) {
dst_u8[i] ^= src_u8[i];
}
2019-04-24 13:38:11 +01:00
}
2019-04-25 05:00:39 +01:00
2019-06-21 09:36:00 +01:00
static void IncrementCounter(void *ctr) {
u64 *ctr_64 = reinterpret_cast<u64 *>(ctr);
2019-04-25 05:00:39 +01:00
2019-06-21 09:36:00 +01:00
ctr_64[1] = __builtin_bswap64(__builtin_bswap64(ctr_64[1]) + 1);
if (!ctr_64[1]) {
ctr_64[0] = __builtin_bswap64(__builtin_bswap64(ctr_64[0]) + 1);
}
2019-04-24 13:38:11 +01:00
}
2019-06-21 09:36:00 +01:00
private:
void Update(const void *data);
public:
void Initialize(const void *seed);
void Reseed(const void *seed);
bool GenerateRandomBytes(void *out, size_t size);
};
}