Implement hw accelerated AES

This commit is contained in:
Shawn Hoffman
2022-07-27 01:51:19 -07:00
parent fb45ed3981
commit 46ad8b9d68
13 changed files with 488 additions and 93 deletions

View File

@ -3,11 +3,12 @@
#pragma once
#include <cstddef>
#include <vector>
#include <memory>
#include "Common/CommonTypes.h"
// Dolphin only uses/implements AES-128-CBC.
namespace Common::AES
{
enum class Mode
@ -15,9 +16,34 @@ enum class Mode
Decrypt,
Encrypt,
};
std::vector<u8> DecryptEncrypt(const u8* key, u8* iv, const u8* src, size_t size, Mode mode);
// Convenience functions
std::vector<u8> Decrypt(const u8* key, u8* iv, const u8* src, size_t size);
std::vector<u8> Encrypt(const u8* key, u8* iv, const u8* src, size_t size);
class Context
{
protected:
static constexpr size_t Nk = 4;
static constexpr size_t Nb = 4;
static constexpr size_t Nr = 10;
static constexpr size_t WORD_SIZE = sizeof(u32);
static constexpr size_t NUM_ROUND_KEYS = Nr + 1;
public:
static constexpr size_t KEY_SIZE = Nk * WORD_SIZE;
static constexpr size_t BLOCK_SIZE = Nb * WORD_SIZE;
Context() = default;
virtual ~Context() = default;
virtual bool Crypt(const u8* iv, u8* iv_out, const u8* buf_in, u8* buf_out, size_t len) const = 0;
bool Crypt(const u8* iv, const u8* buf_in, u8* buf_out, size_t len) const
{
return Crypt(iv, nullptr, buf_in, buf_out, len);
}
bool CryptIvZero(const u8* buf_in, u8* buf_out, size_t len) const
{
return Crypt(nullptr, nullptr, buf_in, buf_out, len);
}
};
std::unique_ptr<Context> CreateContextEncrypt(const u8* key);
std::unique_ptr<Context> CreateContextDecrypt(const u8* key);
} // namespace Common::AES