2017-02-12 10:50:35 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-02-12 10:50:35 +00:00
|
|
|
|
|
|
|
#include "Common/Crypto/AES.h"
|
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
|
2019-06-17 20:36:48 +00:00
|
|
|
namespace Common::AES
|
2017-02-12 10:50:35 +00:00
|
|
|
{
|
2017-05-01 15:50:12 +00:00
|
|
|
std::vector<u8> DecryptEncrypt(const u8* key, u8* iv, const u8* src, size_t size, Mode mode)
|
2017-02-12 10:50:35 +00:00
|
|
|
{
|
|
|
|
mbedtls_aes_context aes_ctx;
|
|
|
|
std::vector<u8> buffer(size);
|
|
|
|
|
2017-05-01 15:50:12 +00:00
|
|
|
if (mode == Mode::Encrypt)
|
|
|
|
mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
|
|
|
|
else
|
|
|
|
mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
|
|
|
|
|
|
|
|
mbedtls_aes_crypt_cbc(&aes_ctx, mode == Mode::Encrypt ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT,
|
|
|
|
size, iv, src, buffer.data());
|
2017-02-12 10:50:35 +00:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2017-05-01 15:50:12 +00:00
|
|
|
|
|
|
|
std::vector<u8> Decrypt(const u8* key, u8* iv, const u8* src, size_t size)
|
|
|
|
{
|
|
|
|
return DecryptEncrypt(key, iv, src, size, Mode::Decrypt);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> Encrypt(const u8* key, u8* iv, const u8* src, size_t size)
|
|
|
|
{
|
|
|
|
return DecryptEncrypt(key, iv, src, size, Mode::Encrypt);
|
|
|
|
}
|
2019-06-17 20:36:48 +00:00
|
|
|
} // namespace Common::AES
|