From 15660bd8570735139d91d0165a2614747f570202 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 3 Aug 2020 14:14:39 -0400 Subject: aes_util: Allow SetIV to be non-allocating In a few places, the data to be set as the IV is already within an array. We shouldn't require this data to be heap-allocated if it doesn't need to be. This allows certain callers to reduce heap churn. --- src/core/crypto/aes_util.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/core/crypto/aes_util.cpp') diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 4be76bb43..330996b24 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include "common/assert.h" #include "common/logging/log.h" @@ -10,8 +11,10 @@ namespace Core::Crypto { namespace { -std::vector CalculateNintendoTweak(std::size_t sector_id) { - std::vector out(0x10); +using NintendoTweak = std::array; + +NintendoTweak CalculateNintendoTweak(std::size_t sector_id) { + NintendoTweak out{}; for (std::size_t i = 0xF; i <= 0xF; --i) { out[i] = sector_id & 0xFF; sector_id >>= 8; @@ -63,13 +66,6 @@ AESCipher::~AESCipher() { mbedtls_cipher_free(&ctx->decryption_context); } -template -void AESCipher::SetIV(std::vector iv) { - ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) || - mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0, - "Failed to set IV on mbedtls ciphers."); -} - template void AESCipher::Transcode(const u8* src, std::size_t size, u8* dest, Op op) const { auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; @@ -124,6 +120,13 @@ void AESCipher::XTSTranscode(const u8* src, std::size_t size, u8* } } +template +void AESCipher::SetIVImpl(const u8* data, std::size_t size) { + ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, data, size) || + mbedtls_cipher_set_iv(&ctx->decryption_context, data, size)) == 0, + "Failed to set IV on mbedtls ciphers."); +} + template class AESCipher; template class AESCipher; } // namespace Core::Crypto -- cgit v1.2.3