diff options
author | Arisotura <thetotalworm@gmail.com> | 2023-11-04 17:46:52 +0100 |
---|---|---|
committer | Arisotura <thetotalworm@gmail.com> | 2023-11-04 17:46:52 +0100 |
commit | 7837c169a15db672451a3f80ac17e3e7291d95c5 (patch) | |
tree | b37c53b69dc31a017e6243a5fc0096e042917f41 /src/DSi_AES.cpp | |
parent | 8f1b0d4a052db068a029cd5784e52da7ecc9813e (diff) |
convert AES
Diffstat (limited to 'src/DSi_AES.cpp')
-rw-r--r-- | src/DSi_AES.cpp | 117 |
1 files changed, 39 insertions, 78 deletions
diff --git a/src/DSi_AES.cpp b/src/DSi_AES.cpp index 1b65dc1..b1d89bd 100644 --- a/src/DSi_AES.cpp +++ b/src/DSi_AES.cpp @@ -21,63 +21,11 @@ #include "DSi.h" #include "DSi_NAND.h" #include "DSi_AES.h" -#include "FIFO.h" -#include "tiny-AES-c/aes.hpp" #include "Platform.h" using Platform::Log; using Platform::LogLevel; -namespace DSi_AES -{ - -u32 Cnt; - -u32 BlkCnt; -u32 RemExtra; -u32 RemBlocks; - -bool OutputFlush; - -u32 InputDMASize, OutputDMASize; -u32 AESMode; - -FIFO<u32, 16> InputFIFO; -FIFO<u32, 16> OutputFIFO; - -u8 IV[16]; - -u8 MAC[16]; - -u8 KeyNormal[4][16]; -u8 KeyX[4][16]; -u8 KeyY[4][16]; - -u8 CurKey[16]; -u8 CurMAC[16]; - -// output MAC for CCM encrypt -u8 OutputMAC[16]; -bool OutputMACDue; - -AES_ctx Ctx; - -void ROL16(u8* val, u32 n) -{ - u32 n_coarse = n >> 3; - u32 n_fine = n & 7; - u8 tmp[16]; - - for (u32 i = 0; i < 16; i++) - { - tmp[i] = val[(i - n_coarse) & 0xF]; - } - - for (u32 i = 0; i < 16; i++) - { - val[i] = (tmp[i] << n_fine) | (tmp[(i - 1) & 0xF] >> (8-n_fine)); - } -} #define _printhex(str, size) { for (int z = 0; z < (size); z++) printf("%02X", (str)[z]); printf("\n"); } #define _printhex2(str, size) { for (int z = 0; z < (size); z++) printf("%02X", (str)[z]); } @@ -86,19 +34,17 @@ void ROL16(u8* val, u32 n) #define _printhex2R(str, size) { for (int z = 0; z < (size); z++) printf("%02X", (str)[((size)-1)-z]); } -bool Init() +DSi_AES::DSi_AES() { const u8 zero[16] = {0}; AES_init_ctx_iv(&Ctx, zero, zero); - - return true; } -void DeInit() +DSi_AES::~DSi_AES() { } -void Reset() +void DSi_AES::Reset() { Cnt = 0; @@ -152,7 +98,7 @@ void Reset() *(u32*)&KeyY[3][8] = 0x202DDD1D; } -void DoSavestate(Savestate* file) +void DSi_AES::DoSavestate(Savestate* file) { file->Section("AESi"); @@ -190,7 +136,7 @@ void DoSavestate(Savestate* file) } -void ProcessBlock_CCM_Extra() +void DSi_AES::ProcessBlock_CCM_Extra() { u8 data[16]; u8 data_rev[16]; @@ -206,7 +152,7 @@ void ProcessBlock_CCM_Extra() AES_ECB_encrypt(&Ctx, CurMAC); } -void ProcessBlock_CCM_Decrypt() +void DSi_AES::ProcessBlock_CCM_Decrypt() { u8 data[16]; u8 data_rev[16]; @@ -234,7 +180,7 @@ void ProcessBlock_CCM_Decrypt() OutputFIFO.Write(*(u32*)&data[12]); } -void ProcessBlock_CCM_Encrypt() +void DSi_AES::ProcessBlock_CCM_Encrypt() { u8 data[16]; u8 data_rev[16]; @@ -262,7 +208,7 @@ void ProcessBlock_CCM_Encrypt() OutputFIFO.Write(*(u32*)&data[12]); } -void ProcessBlock_CTR() +void DSi_AES::ProcessBlock_CTR() { u8 data[16]; u8 data_rev[16]; @@ -287,7 +233,7 @@ void ProcessBlock_CTR() } -u32 ReadCnt() +u32 DSi_AES::ReadCnt() { u32 ret = Cnt; @@ -297,7 +243,7 @@ u32 ReadCnt() return ret; } -void WriteCnt(u32 val) +void DSi_AES::WriteCnt(u32 val) { u32 oldcnt = Cnt; Cnt = val & 0xFC1FF000; @@ -380,12 +326,12 @@ void WriteCnt(u32 val) // val, AESMode, (val >> 26) & 0x3, InputDMASize, OutputDMASize, RemBlocks, BlkCnt); } -void WriteBlkCnt(u32 val) +void DSi_AES::WriteBlkCnt(u32 val) { BlkCnt = val; } -u32 ReadOutputFIFO() +u32 DSi_AES::ReadOutputFIFO() { if (OutputFIFO.IsEmpty()) Log(LogLevel::Warn, "!!! AES OUTPUT FIFO EMPTY\n"); @@ -416,7 +362,7 @@ u32 ReadOutputFIFO() return ret; } -void WriteInputFIFO(u32 val) +void DSi_AES::WriteInputFIFO(u32 val) { // TODO: add some delay to processing @@ -429,7 +375,7 @@ void WriteInputFIFO(u32 val) Update(); } -void CheckInputDMA() +void DSi_AES::CheckInputDMA() { if (RemBlocks == 0 && RemExtra == 0) return; @@ -442,7 +388,7 @@ void CheckInputDMA() Update(); } -void CheckOutputDMA() +void DSi_AES::CheckOutputDMA() { if (OutputFIFO.Level() >= OutputDMASize) { @@ -451,7 +397,7 @@ void CheckOutputDMA() } } -void Update() +void DSi_AES::Update() { if (RemExtra > 0) { @@ -539,7 +485,7 @@ void Update() } -void WriteIV(u32 offset, u32 val, u32 mask) +void DSi_AES::WriteIV(u32 offset, u32 val, u32 mask) { u32 old = *(u32*)&IV[offset]; @@ -548,7 +494,7 @@ void WriteIV(u32 offset, u32 val, u32 mask) //printf("AES: IV: "); _printhex(IV, 16); } -void WriteMAC(u32 offset, u32 val, u32 mask) +void DSi_AES::WriteMAC(u32 offset, u32 val, u32 mask) { u32 old = *(u32*)&MAC[offset]; @@ -557,7 +503,24 @@ void WriteMAC(u32 offset, u32 val, u32 mask) //printf("AES: MAC: "); _printhex(MAC, 16); } -void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey) +void DSi_AES::ROL16(u8* val, u32 n) +{ + u32 n_coarse = n >> 3; + u32 n_fine = n & 7; + u8 tmp[16]; + + for (u32 i = 0; i < 16; i++) + { + tmp[i] = val[(i - n_coarse) & 0xF]; + } + + for (u32 i = 0; i < 16; i++) + { + val[i] = (tmp[i] << n_fine) | (tmp[(i - 1) & 0xF] >> (8-n_fine)); + } +} + +void DSi_AES::DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey) { const u8 key_const[16] = {0xFF, 0xFE, 0xFB, 0x4E, 0x29, 0x59, 0x02, 0x58, 0x2A, 0x68, 0x0F, 0x5F, 0x1A, 0x4F, 0x3E, 0x79}; u8 tmp[16]; @@ -578,7 +541,7 @@ void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey) memcpy(normalkey, tmp, 16); } -void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask) +void DSi_AES::WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask) { u32 old = *(u32*)&KeyNormal[slot][offset]; @@ -587,7 +550,7 @@ void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask) //printf("KeyNormal(%d): ", slot); _printhex(KeyNormal[slot], 16); } -void WriteKeyX(u32 slot, u32 offset, u32 val, u32 mask) +void DSi_AES::WriteKeyX(u32 slot, u32 offset, u32 val, u32 mask) { u32 old = *(u32*)&KeyX[slot][offset]; @@ -596,7 +559,7 @@ void WriteKeyX(u32 slot, u32 offset, u32 val, u32 mask) //printf("KeyX(%d): ", slot); _printhex(KeyX[slot], 16); } -void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask) +void DSi_AES::WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask) { u32 old = *(u32*)&KeyY[slot][offset]; @@ -609,5 +572,3 @@ void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask) DeriveNormalKey(KeyX[slot], KeyY[slot], KeyNormal[slot]); } } - -} |