mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-30 06:25:20 -04:00
libs: begin adding capacity for doing crypto on generic os (using externally-preset keys)
This commit is contained in:
parent
6368d8063a
commit
706b8492fd
28 changed files with 1305 additions and 33 deletions
|
@ -13,11 +13,11 @@
|
|||
* 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_block_cipher.hpp>
|
||||
|
||||
|
||||
namespace ams::crypto::impl {
|
||||
|
@ -50,6 +50,8 @@ namespace ams::crypto::impl {
|
|||
#endif
|
||||
};
|
||||
|
||||
/* static_assert(HashFunction<Sha1Impl>); */
|
||||
static_assert(BlockCipher<AesImpl<16>>);
|
||||
static_assert(BlockCipher<AesImpl<24>>);
|
||||
static_assert(BlockCipher<AesImpl<32>>);
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 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 {
|
||||
|
||||
template<typename T>
|
||||
concept BlockCipher = requires(T &t, const void *cv, void *v, size_t sz, bool b) {
|
||||
{ T::BlockSize } -> std::convertible_to<size_t>;
|
||||
{ t.EncryptBlock(v, sz, cv, sz) } -> std::same_as<void>;
|
||||
{ t.DecryptBlock(v, sz, cv, sz) } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,200 @@
|
|||
/*
|
||||
* Copyright (c) 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_clear.hpp>
|
||||
#include <vapours/crypto/crypto_aes_encryptor.hpp>
|
||||
|
||||
namespace ams::crypto::impl {
|
||||
|
||||
template<typename BlockCipher>
|
||||
class CbcModeImpl {
|
||||
NON_COPYABLE(CbcModeImpl);
|
||||
NON_MOVEABLE(CbcModeImpl);
|
||||
public:
|
||||
static constexpr size_t KeySize = BlockCipher::KeySize;
|
||||
static constexpr size_t BlockSize = BlockCipher::BlockSize;
|
||||
static constexpr size_t IvSize = BlockCipher::BlockSize;
|
||||
private:
|
||||
enum State {
|
||||
State_None,
|
||||
State_Initialized,
|
||||
};
|
||||
private:
|
||||
const BlockCipher *m_block_cipher;
|
||||
u8 m_iv[IvSize];
|
||||
u8 m_buffer[BlockSize];
|
||||
size_t m_buffered_bytes;
|
||||
State m_state;
|
||||
public:
|
||||
CbcModeImpl() : m_state(State_None) { /* ... */ }
|
||||
|
||||
~CbcModeImpl() {
|
||||
ClearMemory(this, sizeof(*this));
|
||||
}
|
||||
|
||||
void Initialize(const BlockCipher *block_cipher, const void *iv, size_t iv_size) {
|
||||
AMS_ASSERT(iv_size == IvSize);
|
||||
AMS_UNUSED(iv_size);
|
||||
|
||||
m_block_cipher = block_cipher;
|
||||
std::memcpy(m_iv, iv, IvSize);
|
||||
m_buffered_bytes = 0;
|
||||
|
||||
m_state = State_Initialized;
|
||||
}
|
||||
|
||||
size_t GetBufferedDataSize() const {
|
||||
return m_buffered_bytes;
|
||||
}
|
||||
|
||||
size_t UpdateEncryption(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||
AMS_ASSERT(dst_size >= ((src_size + this->GetBufferedDataSize()) / BlockSize) * BlockSize);
|
||||
AMS_ASSERT(m_state == State_Initialized);
|
||||
|
||||
return this->Update(dst, dst_size, src, src_size, [&] (u8 *d, u8 *i, const u8 *s, size_t n) ALWAYS_INLINE_LAMBDA {
|
||||
this->EncryptBlocks(d, i, s, n);
|
||||
});
|
||||
}
|
||||
|
||||
size_t UpdateDecryption(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||
AMS_ASSERT(dst_size >= ((src_size + this->GetBufferedDataSize()) / BlockSize) * BlockSize);
|
||||
AMS_ASSERT(m_state == State_Initialized);
|
||||
|
||||
return this->Update(dst, dst_size, src, src_size, [&] (u8 *d, u8 *i, const u8 *s, size_t n) ALWAYS_INLINE_LAMBDA {
|
||||
this->DecryptBlocks(d, i, s, n);
|
||||
});
|
||||
}
|
||||
private:
|
||||
size_t Update(void *_dst, size_t dst_size, const void *_src, size_t src_size, auto ProcessBlocks) {
|
||||
AMS_UNUSED(dst_size);
|
||||
|
||||
u8 *dst = static_cast<u8 *>(_dst);
|
||||
const u8 *src = static_cast<const u8 *>(_src);
|
||||
size_t remaining = src_size;
|
||||
size_t processed = 0;
|
||||
|
||||
if (m_buffered_bytes > 0) {
|
||||
const size_t copy_size = std::min(BlockSize - m_buffered_bytes, remaining);
|
||||
|
||||
std::memcpy(m_buffer + m_buffered_bytes, src, copy_size);
|
||||
src += copy_size;
|
||||
remaining -= copy_size;
|
||||
m_buffered_bytes += copy_size;
|
||||
|
||||
if (m_buffered_bytes == BlockSize) {
|
||||
ProcessBlocks(dst, m_iv, m_buffer, 1);
|
||||
processed += BlockSize;
|
||||
dst += BlockSize;
|
||||
|
||||
m_buffered_bytes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (remaining >= BlockSize) {
|
||||
const size_t num_blocks = remaining / BlockSize;
|
||||
|
||||
ProcessBlocks(dst, m_iv, src, num_blocks);
|
||||
|
||||
const size_t processed_size = num_blocks * BlockSize;
|
||||
dst += processed_size;
|
||||
src += processed_size;
|
||||
remaining -= processed_size;
|
||||
processed += processed_size;
|
||||
}
|
||||
|
||||
if (remaining > 0) {
|
||||
std::memcpy(m_buffer, src, remaining);
|
||||
m_buffered_bytes = remaining;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
void EncryptBlocks(u8 *dst, u8 *iv, const u8 *src, size_t num_blocks) {
|
||||
const u8 *cur_iv = iv;
|
||||
|
||||
u8 block[BlockSize];
|
||||
while (num_blocks--) {
|
||||
for (size_t i = 0; i < BlockSize; ++i) {
|
||||
block[i] = src[i] ^ cur_iv[i];
|
||||
}
|
||||
|
||||
m_block_cipher->EncryptBlock(dst, BlockSize, block, BlockSize);
|
||||
|
||||
cur_iv = dst;
|
||||
src += BlockSize;
|
||||
dst += BlockSize;
|
||||
}
|
||||
|
||||
if (iv != cur_iv) {
|
||||
std::memcpy(iv, cur_iv, BlockSize);
|
||||
}
|
||||
}
|
||||
|
||||
void DecryptBlocks(u8 *dst, u8 *iv, const u8 *src, size_t num_blocks) {
|
||||
u8 next_iv[BlockSize];
|
||||
std::memcpy(next_iv, src + ((num_blocks - 1) * BlockSize), BlockSize);
|
||||
|
||||
if (src == dst) {
|
||||
src = src + ((num_blocks - 1) * BlockSize);
|
||||
dst = dst + ((num_blocks - 1) * BlockSize);
|
||||
|
||||
const u8 *cur_iv = (num_blocks == 1) ? iv : src - BlockSize;
|
||||
|
||||
while (num_blocks-- > 1) {
|
||||
m_block_cipher->DecryptBlock(dst, BlockSize, src, BlockSize);
|
||||
|
||||
for (size_t i = 0; i < BlockSize; ++i) {
|
||||
dst[i] ^= cur_iv[i];
|
||||
}
|
||||
|
||||
cur_iv -= BlockSize;
|
||||
src -= BlockSize;
|
||||
dst -= BlockSize;
|
||||
}
|
||||
|
||||
m_block_cipher->DecryptBlock(dst, BlockSize, src, BlockSize);
|
||||
|
||||
for (size_t i = 0; i < BlockSize; ++i) {
|
||||
dst[i] ^= iv[i];
|
||||
}
|
||||
} else {
|
||||
const u8 *cur_iv = iv;
|
||||
|
||||
while (num_blocks-- > 0) {
|
||||
m_block_cipher->DecryptBlock(dst, BlockSize, src, BlockSize);
|
||||
|
||||
for (size_t i = 0; i < BlockSize; ++i) {
|
||||
dst[i] ^= cur_iv[i];
|
||||
}
|
||||
|
||||
cur_iv = src;
|
||||
src += BlockSize;
|
||||
dst += BlockSize;
|
||||
}
|
||||
}
|
||||
|
||||
std::memcpy(iv, next_iv, BlockSize);
|
||||
}
|
||||
};
|
||||
|
||||
/* TODO: Optimized AES cbc impl specializations. */
|
||||
|
||||
}
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
namespace ams::crypto::impl {
|
||||
|
||||
template<typename Hash> /* requires HashFunction<Hash> */
|
||||
template<HashFunction Hash>
|
||||
class HmacImpl {
|
||||
NON_COPYABLE(HmacImpl);
|
||||
NON_MOVEABLE(HmacImpl);
|
||||
|
@ -61,7 +61,7 @@ namespace ams::crypto::impl {
|
|||
void GetMac(void *dst, size_t dst_size);
|
||||
};
|
||||
|
||||
template<typename Hash>
|
||||
template<HashFunction Hash>
|
||||
inline void HmacImpl<Hash>::Initialize(const void *key, size_t key_size) {
|
||||
/* Clear the key storage. */
|
||||
std::memset(m_key, 0, sizeof(m_key));
|
||||
|
@ -88,14 +88,14 @@ namespace ams::crypto::impl {
|
|||
m_state = State_Initialized;
|
||||
}
|
||||
|
||||
template<typename Hash>
|
||||
template<HashFunction Hash>
|
||||
inline void HmacImpl<Hash>::Update(const void *data, size_t data_size) {
|
||||
AMS_ASSERT(m_state == State_Initialized);
|
||||
|
||||
m_hash_function.Update(data, data_size);
|
||||
}
|
||||
|
||||
template<typename Hash>
|
||||
template<HashFunction Hash>
|
||||
inline void HmacImpl<Hash>::GetMac(void *dst, size_t dst_size) {
|
||||
AMS_ASSERT(m_state == State_Initialized || m_state == State_Done);
|
||||
AMS_ASSERT(dst_size >= MacSize);
|
||||
|
|
|
@ -56,6 +56,6 @@ namespace ams::crypto::impl {
|
|||
void ProcessLastBlock();
|
||||
};
|
||||
|
||||
/* static_assert(HashFunction<Md5Impl>); */
|
||||
static_assert(HashFunction<Md5Impl>);
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
namespace ams::crypto::impl {
|
||||
|
||||
template<typename Hash> requires HashFunction<Hash>
|
||||
template<HashFunction Hash>
|
||||
class RsaOaepImpl {
|
||||
NON_COPYABLE(RsaOaepImpl);
|
||||
NON_MOVEABLE(RsaOaepImpl);
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
namespace ams::crypto::impl {
|
||||
|
||||
template<typename Hash> requires HashFunction<Hash>
|
||||
template<HashFunction Hash>
|
||||
class RsaPssImpl {
|
||||
NON_COPYABLE(RsaPssImpl);
|
||||
NON_MOVEABLE(RsaPssImpl);
|
||||
|
|
|
@ -55,6 +55,6 @@ namespace ams::crypto::impl {
|
|||
void ProcessLastBlock();
|
||||
};
|
||||
|
||||
/* static_assert(HashFunction<Sha1Impl>); */
|
||||
static_assert(HashFunction<Sha1Impl>);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue