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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-07-27 08:51:19 +00:00
|
|
|
#include <memory>
|
2017-02-12 10:50:35 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2022-07-27 08:51:19 +00:00
|
|
|
// Dolphin only uses/implements AES-128-CBC.
|
|
|
|
|
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
|
|
|
enum class Mode
|
|
|
|
{
|
|
|
|
Decrypt,
|
|
|
|
Encrypt,
|
|
|
|
};
|
|
|
|
|
2022-07-27 08:51:19 +00:00
|
|
|
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);
|
|
|
|
|
2019-06-17 20:36:48 +00:00
|
|
|
} // namespace Common::AES
|