aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArisotura <thetotalworm@gmail.com>2020-06-21 12:20:02 +0200
committerArisotura <thetotalworm@gmail.com>2020-06-21 12:20:02 +0200
commitff69df2a15bdb6cac618a9b19f56ea1732e0f07e (patch)
treea5ec4b38d17c2b1f55d01fbd6c5b730df30f7e20 /src
parente43a216ab55f72a6ba1a98046abc3770a4217eb4 (diff)
add AES-CCM encrypt
makes the eShop work
Diffstat (limited to 'src')
-rw-r--r--src/DSi_AES.cpp56
1 files changed, 49 insertions, 7 deletions
diff --git a/src/DSi_AES.cpp b/src/DSi_AES.cpp
index c7c599e..dfa67bd 100644
--- a/src/DSi_AES.cpp
+++ b/src/DSi_AES.cpp
@@ -165,9 +165,37 @@ void ProcessBlock_CCM_Decrypt()
//printf("AES-CCM: "); _printhex2(data, 16);
Swap16(data_rev, data);
+
AES_CTR_xcrypt_buffer(&Ctx, data_rev, 16);
+ for (int i = 0; i < 16; i++) CurMAC[i] ^= data_rev[i];
+ AES_ECB_encrypt(&Ctx, CurMAC);
+
+ Swap16(data, data_rev);
+
+ //printf(" -> "); _printhex2(data, 16);
+
+ OutputFIFO->Write(*(u32*)&data[0]);
+ OutputFIFO->Write(*(u32*)&data[4]);
+ OutputFIFO->Write(*(u32*)&data[8]);
+ OutputFIFO->Write(*(u32*)&data[12]);
+}
+
+void ProcessBlock_CCM_Encrypt()
+{
+ u8 data[16];
+ u8 data_rev[16];
+
+ *(u32*)&data[0] = InputFIFO->Read();
+ *(u32*)&data[4] = InputFIFO->Read();
+ *(u32*)&data[8] = InputFIFO->Read();
+ *(u32*)&data[12] = InputFIFO->Read();
+
+ //printf("AES-CCM: "); _printhex2(data, 16);
+
+ Swap16(data_rev, data);
for (int i = 0; i < 16; i++) CurMAC[i] ^= data_rev[i];
+ AES_CTR_xcrypt_buffer(&Ctx, data_rev, 16);
AES_ECB_encrypt(&Ctx, CurMAC);
Swap16(data, data_rev);
@@ -232,7 +260,6 @@ void WriteCnt(u32 val)
OutputDMASize = dmasize_out[(val >> 14) & 0x3];
AESMode = (val >> 28) & 0x3;
- if (AESMode == 1) printf("AES-CCM TODO\n");
if (val & (1<<24))
{
@@ -245,6 +272,8 @@ void WriteCnt(u32 val)
// transfer start (checkme)
RemBlocks = BlkCnt >> 16;
+ if (AESMode == 0 && (!(val & (1<<20)))) printf("AES: CCM-DECRYPT MAC FROM WRFIFO, TODO\n");
+
if (RemBlocks > 0)
{
u8 key[16];
@@ -365,14 +394,9 @@ void Update()
switch (AESMode)
{
case 0: ProcessBlock_CCM_Decrypt(); break;
+ case 1: ProcessBlock_CCM_Encrypt(); break;
case 2:
case 3: ProcessBlock_CTR(); break;
- default:
- // dorp
- OutputFIFO->Write(InputFIFO->Read());
- OutputFIFO->Write(InputFIFO->Read());
- OutputFIFO->Write(InputFIFO->Read());
- OutputFIFO->Write(InputFIFO->Read());
}
RemBlocks--;
@@ -398,6 +422,24 @@ void Update()
if (CurMAC[15-i] != MAC[i]) Cnt &= ~(1<<21);
}
}
+ else if (AESMode == 1)
+ {
+ Ctx.Iv[13] = 0x00;
+ Ctx.Iv[14] = 0x00;
+ Ctx.Iv[15] = 0x00;
+ AES_CTR_xcrypt_buffer(&Ctx, CurMAC, 16);
+
+ u8 finalmac[16];
+ Swap16(finalmac, CurMAC);
+
+ OutputFIFO->Write(*(u32*)&finalmac[0]);
+ OutputFIFO->Write(*(u32*)&finalmac[4]);
+ OutputFIFO->Write(*(u32*)&finalmac[8]);
+ OutputFIFO->Write(*(u32*)&finalmac[12]);
+
+ // CHECKME
+ Cnt &= ~(1<<21);
+ }
else
{
// CHECKME