From f8fb9e2d034d122c72515083f75e2034abf6210d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 1 May 2017 17:50:12 +0200 Subject: IOS: Implement IOSC-like API This prevents the IOS crypto code and keys from being spread over the codebase. Things only have to be implemented once, and can be used everywhere from the IOS code. Additionally, since ES exposes some IOSC calls directly (DeleteObject and Encrypt/Decrypt), we need this for proper emulation. Currently, this only supports AES key objects. --- Source/Core/Common/Crypto/AES.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'Source/Core/Common/Crypto/AES.cpp') diff --git a/Source/Core/Common/Crypto/AES.cpp b/Source/Core/Common/Crypto/AES.cpp index e12ba2481c..00685beba4 100644 --- a/Source/Core/Common/Crypto/AES.cpp +++ b/Source/Core/Common/Crypto/AES.cpp @@ -10,15 +10,30 @@ namespace Common { namespace AES { -std::vector Decrypt(const u8* key, u8* iv, const u8* src, size_t size) +std::vector DecryptEncrypt(const u8* key, u8* iv, const u8* src, size_t size, Mode mode) { mbedtls_aes_context aes_ctx; std::vector buffer(size); - mbedtls_aes_setkey_dec(&aes_ctx, key, 128); - mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, size, iv, src, buffer.data()); + 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()); return buffer; } + +std::vector Decrypt(const u8* key, u8* iv, const u8* src, size_t size) +{ + return DecryptEncrypt(key, iv, src, size, Mode::Decrypt); +} + +std::vector Encrypt(const u8* key, u8* iv, const u8* src, size_t size) +{ + return DecryptEncrypt(key, iv, src, size, Mode::Encrypt); +} } // namespace AES } // namespace Common -- cgit v1.2.3