Atmosphere/libraries/libvapours/source/crypto/impl/crypto_xts_mode_impl.cpp

145 lines
4.1 KiB
C++
Raw Permalink Normal View History

2020-04-06 06:25:28 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-04-06 06:25:28 +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/>.
*/
#include <vapours.hpp>
namespace ams::crypto::impl {
namespace {
/* TODO: Support non-Nintendo Endianness */
void MultiplyTweakGeneric(u64 *tweak) {
const u64 carry = tweak[1] & (static_cast<u64>(1) << (BITSIZEOF(u64) - 1));
tweak[1] = ((tweak[1] << 1) | (tweak[0] >> (BITSIZEOF(u64) - 1)));
tweak[0] = (tweak[0] << 1);
if (carry) {
tweak[0] ^= static_cast<u64>(0x87);
}
}
}
void XtsModeImpl::ProcessBlock(u8 *dst, const u8 *src) {
u8 tmp[BlockSize];
/* Xor. */
for (size_t i = 0; i < BlockSize; i++) {
tmp[i] = m_tweak[i] ^ src[i];
2020-04-06 06:25:28 +00:00
}
/* Crypt */
m_cipher_func(tmp, tmp, m_cipher_ctx);
2020-04-06 06:25:28 +00:00
/* Xor. */
for (size_t i = 0; i < BlockSize; i++) {
dst[i] = m_tweak[i] ^ tmp[i];
2020-04-06 06:25:28 +00:00
}
MultiplyTweakGeneric(reinterpret_cast<u64 *>(m_tweak));
2020-04-06 06:25:28 +00:00
}
size_t XtsModeImpl::FinalizeEncryption(void *dst, size_t dst_size) {
AMS_ASSERT(m_state == State_Processing);
2020-08-17 21:20:24 +00:00
AMS_UNUSED(dst_size);
2020-04-06 06:25:28 +00:00
u8 *dst_u8 = static_cast<u8 *>(dst);
size_t processed = 0;
if (m_num_buffered == 0) {
this->ProcessBlock(dst_u8, m_last_block);
2020-04-06 06:25:28 +00:00
processed = BlockSize;
} else {
this->ProcessBlock(m_last_block, m_last_block);
2020-04-06 06:25:28 +00:00
std::memcpy(m_buffer + m_num_buffered, m_last_block + m_num_buffered, BlockSize - m_num_buffered);
2020-04-06 06:25:28 +00:00
this->ProcessBlock(dst_u8, m_buffer);
2020-04-06 06:25:28 +00:00
std::memcpy(dst_u8 + BlockSize, m_last_block, m_num_buffered);
2020-04-06 06:25:28 +00:00
processed = BlockSize + m_num_buffered;
2020-04-06 06:25:28 +00:00
}
m_state = State_Done;
2020-04-06 06:25:28 +00:00
return processed;
}
size_t XtsModeImpl::FinalizeDecryption(void *dst, size_t dst_size) {
AMS_ASSERT(m_state == State_Processing);
2020-08-17 21:20:24 +00:00
AMS_UNUSED(dst_size);
2020-04-06 06:25:28 +00:00
u8 *dst_u8 = static_cast<u8 *>(dst);
size_t processed = 0;
if (m_num_buffered == 0) {
this->ProcessBlock(dst_u8, m_last_block);
2020-04-06 06:25:28 +00:00
processed = BlockSize;
} else {
u8 tmp_tweak[BlockSize];
std::memcpy(tmp_tweak, m_tweak, BlockSize);
MultiplyTweakGeneric(reinterpret_cast<u64 *>(m_tweak));
2020-04-06 06:25:28 +00:00
this->ProcessBlock(m_last_block, m_last_block);
2020-04-06 06:25:28 +00:00
std::memcpy(m_buffer + m_num_buffered, m_last_block + m_num_buffered, BlockSize - m_num_buffered);
2020-04-06 06:25:28 +00:00
std::memcpy(m_tweak, tmp_tweak, BlockSize);
2020-04-06 06:25:28 +00:00
this->ProcessBlock(dst_u8, m_buffer);
2020-04-06 06:25:28 +00:00
std::memcpy(dst_u8 + BlockSize, m_last_block, m_num_buffered);
2020-04-06 06:25:28 +00:00
processed = BlockSize + m_num_buffered;
2020-04-06 06:25:28 +00:00
}
m_state = State_Done;
2020-04-06 06:25:28 +00:00
return processed;
}
size_t XtsModeImpl::ProcessPartialData(u8 *dst, const u8 *src, size_t size) {
size_t processed = 0;
std::memcpy(m_buffer + m_num_buffered, src, size);
m_num_buffered += size;
2020-04-06 06:25:28 +00:00
if (m_num_buffered == BlockSize) {
if (m_state == State_Processing) {
this->ProcessBlock(dst, m_last_block);
2020-04-06 06:25:28 +00:00
processed += BlockSize;
}
std::memcpy(m_last_block, m_buffer, BlockSize);
m_num_buffered = 0;
2020-04-06 06:25:28 +00:00
m_state = State_Processing;
2020-04-06 06:25:28 +00:00
}
return processed;
}
size_t XtsModeImpl::ProcessRemainingData(u8 *dst, const u8 *src, size_t size) {
2020-08-17 21:20:24 +00:00
AMS_UNUSED(dst);
std::memcpy(m_buffer, src, size);
m_num_buffered = size;
2020-04-06 06:25:28 +00:00
return 0;
}
}