| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <memory> |
| 7 |
|
| 8 |
#include "Common/CommonTypes.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Common::AES |
| 13 |
{ |
| 14 |
enum class Mode |
| 15 |
{ |
| 16 |
Decrypt, |
| 17 |
Encrypt, |
| 18 |
}; |
| 19 |
|
| 20 |
class Context |
| 21 |
{ |
| 22 |
protected: |
| 23 |
static constexpr size_t Nk = 4; |
| 24 |
static constexpr size_t Nb = 4; |
| 25 |
static constexpr size_t Nr = 10; |
| 26 |
static constexpr size_t WORD_SIZE = sizeof(u32); |
| 27 |
static constexpr size_t NUM_ROUND_KEYS = Nr + 1; |
| 28 |
|
| 29 |
public: |
| 30 |
static constexpr size_t KEY_SIZE = Nk * WORD_SIZE; |
| 31 |
static constexpr size_t BLOCK_SIZE = Nb * WORD_SIZE; |
| 32 |
|
| 33 |
Context() = default; |
| 34 |
virtual ~Context() = default; |
| 35 |
virtual bool Crypt(const u8* iv, u8* iv_out, const u8* buf_in, u8* buf_out, size_t len) const = 0; |
| 36 |
bool Crypt(const u8* iv, const u8* buf_in, u8* buf_out, size_t len) const |
| 37 |
{ |
| 38 |
return Crypt(iv, nullptr, buf_in, buf_out, len); |
| 39 |
} |
| 40 |
bool CryptIvZero(const u8* buf_in, u8* buf_out, size_t len) const |
| 41 |
{ |
| 42 |
return Crypt(nullptr, nullptr, buf_in, buf_out, len); |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
std::unique_ptr<Context> CreateContextEncrypt(const u8* key); |
| 47 |
std::unique_ptr<Context> CreateContextDecrypt(const u8* key); |
| 48 |
|
| 49 |
|
| 50 |
void CryptOFB(const u8* key, const u8* iv, u8* iv_out, const u8* buf_in, u8* buf_out, size_t size); |
| 51 |
|
| 52 |
} |