mirror of
https://github.com/rvtr/TDT.git
synced 2025-10-31 13:51:07 -04:00
Clean up formatting
No code changes outside of a very minor change to arm7/main.c (removes unused functions)
This commit is contained in:
parent
c9267de164
commit
68fc9a6586
@ -29,34 +29,29 @@
|
||||
---------------------------------------------------------------------------------*/
|
||||
#include "my_sdmmc.h"
|
||||
|
||||
//#include <dswifi7.h>
|
||||
//#include <maxmod7.h>
|
||||
#include <nds.h>
|
||||
#include <string.h>
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VblankHandler(void) {
|
||||
//---------------------------------------------------------------------------------
|
||||
// Wifi_Update();
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VcountHandler() {
|
||||
void VcountHandler()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
inputGetAndSend();
|
||||
}
|
||||
|
||||
volatile bool exitflag = false;
|
||||
volatile bool reboot = false;
|
||||
|
||||
// Custom POWER button handling, based on the default function:
|
||||
// https://github.com/devkitPro/libnds/blob/154a21cc3d57716f773ff2b10f815511c1b8ba9f/source/common/interrupts.c#L51-L69
|
||||
//---------------------------------------------------------------------------------
|
||||
TWL_CODE void i2cIRQHandlerCustom() {
|
||||
TWL_CODE void i2cIRQHandlerCustom()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
int cause = (i2cReadRegister(I2C_PM, I2CREGPM_PWRIF) & 0x3) | (i2cReadRegister(I2C_GPIO, 0x02)<<2);
|
||||
|
||||
switch (cause & 3) {
|
||||
switch (cause & 3)
|
||||
{
|
||||
case 1:
|
||||
reboot = true;
|
||||
exitflag = true;
|
||||
@ -68,14 +63,19 @@ TWL_CODE void i2cIRQHandlerCustom() {
|
||||
}
|
||||
}
|
||||
|
||||
void set_ctr(u32* ctr){
|
||||
void set_ctr(u32* ctr)
|
||||
{
|
||||
for (int i = 0; i < 4; i++) REG_AES_IV[i] = ctr[3-i];
|
||||
}
|
||||
|
||||
// 10 11 22 23 24 25
|
||||
void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged together dsi aes function adapted from this 3ds function
|
||||
REG_AES_CNT = ( AES_CNT_MODE(method) | //https://github.com/TiniVi/AHPCFW/blob/master/source/aes.c#L42
|
||||
AES_WRFIFO_FLUSH | //as long as the output changes when keyslot values change, it's good enough.
|
||||
//this is sort of a bodged together dsi aes function adapted from this 3ds function
|
||||
//https://github.com/TiniVi/AHPCFW/blob/master/source/aes.c#L42
|
||||
//as long as the output changes when keyslot values change, it's good enough.
|
||||
void aes(void* in, void* out, void* iv, u32 method)
|
||||
{
|
||||
REG_AES_CNT = ( AES_CNT_MODE(method) |
|
||||
AES_WRFIFO_FLUSH |
|
||||
AES_RDFIFO_FLUSH |
|
||||
AES_CNT_KEY_APPLY |
|
||||
AES_CNT_KEYSLOT(3) |
|
||||
@ -88,7 +88,7 @@ void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged
|
||||
REG_AES_CNT |= 0x80000000;
|
||||
|
||||
for (int j = 0; j < 0x10; j+=4) REG_AES_WRFIFO = *((u32*)(in+j));
|
||||
while(((REG_AES_CNT >> 0x5) & 0x1F) < 0x4); //wait for every word to get processed
|
||||
while (((REG_AES_CNT >> 0x5) & 0x1F) < 0x4); //wait for every word to get processed
|
||||
for (int j = 0; j < 0x10; j+=4) *((u32*)(out+j)) = REG_AES_RDFIFO;
|
||||
//REG_AES_CNT &= ~0x80000000;
|
||||
//if (method & (AES_CTR_DECRYPT | AES_CTR_ENCRYPT)) add_ctr((u8*)iv);
|
||||
@ -97,8 +97,9 @@ void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged
|
||||
int my_sdmmc_nand_startup();
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int main() {
|
||||
int main()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
// clear sound registers
|
||||
dmaFillWords(0, (void*)0x04000400, 0x100);
|
||||
|
||||
@ -116,7 +117,8 @@ int main() {
|
||||
fifoInit();
|
||||
touchInit();
|
||||
|
||||
if (isDSiMode() /*|| ((REG_SCFG_EXT & BIT(17)) && (REG_SCFG_EXT & BIT(18)))*/) {
|
||||
if (isDSiMode() /*|| ((REG_SCFG_EXT & BIT(17)) && (REG_SCFG_EXT & BIT(18)))*/)
|
||||
{
|
||||
u8 *out=(u8*)0x02300000;
|
||||
|
||||
#if USENATIVECONSOLEID
|
||||
@ -135,16 +137,18 @@ int main() {
|
||||
u8 *scratch=(u8*)0x02300200;
|
||||
u8 *key3=(u8*)0x40044D0;
|
||||
|
||||
|
||||
aes(in, base, iv, 2);
|
||||
|
||||
//write consecutive 0-255 values to any byte in key3 until we get the same aes output as "base" above - this reveals the hidden byte. this way we can uncover all 16 bytes of the key3 normalkey pretty easily.
|
||||
//greets to Martin Korth for this trick https://problemkaputt.de/gbatek.htm#dsiaesioports (Reading Write-Only Values)
|
||||
for(int i=0;i<16;i++){
|
||||
for(int j=0;j<256;j++){
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
for (int j=0;j<256;j++)
|
||||
{
|
||||
*(key3+i)=j & 0xFF;
|
||||
aes(in, scratch, iv, 2);
|
||||
if(!memcmp(scratch, base, 16)){
|
||||
if (!memcmp(scratch, base, 16))
|
||||
{
|
||||
out[i]=j;
|
||||
//hit++;
|
||||
break;
|
||||
@ -153,30 +157,26 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
my_sdmmc_nand_startup() ;
|
||||
my_sdmmc_nand_startup();
|
||||
my_sdmmc_get_cid(true, (u32*)0x2FFD7BC); // Get eMMC CID
|
||||
//sdmmc_nand_cid((u32*)0x2FFD7BC);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// mmInstall(FIFO_MAXMOD);
|
||||
|
||||
SetYtrigger(80);
|
||||
|
||||
// installWifiFIFO();
|
||||
installSoundFIFO();
|
||||
|
||||
installSystemFIFO();
|
||||
|
||||
irqSet(IRQ_VCOUNT, VcountHandler);
|
||||
irqSet(IRQ_VBLANK, VblankHandler);
|
||||
|
||||
irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);
|
||||
|
||||
// Keep the ARM7 mostly idle
|
||||
while (!exitflag) {
|
||||
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) {
|
||||
while (!exitflag)
|
||||
{
|
||||
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R)))
|
||||
{
|
||||
exitflag = true;
|
||||
}
|
||||
|
||||
@ -195,10 +195,13 @@ int main() {
|
||||
fifoWaitValue32(FIFO_USER_02);
|
||||
fifoCheckValue32(FIFO_USER_02);
|
||||
|
||||
if (reboot) {
|
||||
if (reboot)
|
||||
{
|
||||
i2cWriteRegister(I2C_PM, I2CREGPM_RESETFLAG, 1);
|
||||
i2cWriteRegister(I2C_PM, I2CREGPM_PWRCNT, 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
writePowerManagement(PM_CONTROL_REG,PM_SYSTEM_PWR);
|
||||
}
|
||||
|
||||
|
||||
@ -10,29 +10,35 @@
|
||||
static struct mmcdevice deviceSD;
|
||||
static struct mmcdevice deviceNAND;
|
||||
|
||||
/*mmcdevice *getMMCDevice(int drive) {
|
||||
if(drive==0) return &deviceNAND;
|
||||
/*mmcdevice *getMMCDevice(int drive)
|
||||
{
|
||||
if (drive==0) return &deviceNAND;
|
||||
return &deviceSD;
|
||||
}
|
||||
*/
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_geterror(struct mmcdevice *ctx) {
|
||||
int my_geterror(struct mmcdevice *ctx)
|
||||
//---------------------------------------------------------------------------------
|
||||
//if(ctx->error == 0x4) return -1;
|
||||
{
|
||||
//if (ctx->error == 0x4) return -1;
|
||||
//else return 0;
|
||||
return (ctx->error << 29) >> 31;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_setTarget(struct mmcdevice *ctx) {
|
||||
void my_setTarget(struct mmcdevice *ctx)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber);
|
||||
setckl(ctx->clk);
|
||||
if (ctx->SDOPT == 0) {
|
||||
if (ctx->SDOPT == 0)
|
||||
{
|
||||
sdmmc_mask16(REG_SDOPT, 0, 0x8000);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
sdmmc_mask16(REG_SDOPT, 0x8000, 0);
|
||||
}
|
||||
|
||||
@ -40,20 +46,21 @@ void my_setTarget(struct mmcdevice *ctx) {
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
const bool getSDRESP = (cmd << 15) >> 31;
|
||||
u16 flags = (cmd << 15) >> 31;
|
||||
const bool readdata = cmd & 0x20000;
|
||||
const bool writedata = cmd & 0x40000;
|
||||
|
||||
if(readdata || writedata)
|
||||
if (readdata || writedata)
|
||||
{
|
||||
flags |= TMIO_STAT0_DATAEND;
|
||||
}
|
||||
|
||||
ctx->error = 0;
|
||||
while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working?
|
||||
while ((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working?
|
||||
sdmmc_write16(REG_SDIRMASK0,0);
|
||||
sdmmc_write16(REG_SDIRMASK1,0);
|
||||
sdmmc_write16(REG_SDSTATUS0,0);
|
||||
@ -87,34 +94,34 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
#endif
|
||||
|
||||
u16 status0 = 0;
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
volatile u16 status1 = sdmmc_read16(REG_SDSTATUS1);
|
||||
#ifdef DATA32_SUPPORT
|
||||
volatile u16 ctl32 = sdmmc_read16(REG_SDDATACTL32);
|
||||
if((ctl32 & 0x100))
|
||||
if (ctl32 & 0x100)
|
||||
#else
|
||||
if((status1 & TMIO_STAT1_RXRDY))
|
||||
if (status1 & TMIO_STAT1_RXRDY)
|
||||
#endif
|
||||
{
|
||||
if(readdata)
|
||||
if (readdata)
|
||||
{
|
||||
if(rUseBuf)
|
||||
if (rUseBuf)
|
||||
{
|
||||
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
|
||||
if(size >= blkSize)
|
||||
if (size >= blkSize)
|
||||
{
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!((u32)rDataPtr32 & 3))
|
||||
if (!((u32)rDataPtr32 & 3))
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
for (u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
*rDataPtr32++ = sdmmc_read32(REG_SDFIFO32);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
for (u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
u32 data = sdmmc_read32(REG_SDFIFO32);
|
||||
*rDataPtr8++ = data;
|
||||
@ -124,16 +131,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!((u16)rDataPtr16 & 1))
|
||||
if (!((u16)rDataPtr16 & 1))
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
for (u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
*rDataPtr16++ = sdmmc_read16(REG_SDFIFO);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
for (u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
u16 data = sdmmc_read16(REG_SDFIFO);
|
||||
*rDataPtr8++ = data;
|
||||
@ -149,29 +156,29 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
}
|
||||
}
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!(ctl32 & 0x200))
|
||||
if (!(ctl32 & 0x200))
|
||||
#else
|
||||
if((status1 & TMIO_STAT1_TXRQ))
|
||||
if ((status1 & TMIO_STAT1_TXRQ))
|
||||
#endif
|
||||
{
|
||||
if(writedata)
|
||||
if (writedata)
|
||||
{
|
||||
if(tUseBuf)
|
||||
if (tUseBuf)
|
||||
{
|
||||
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
|
||||
if(size >= blkSize)
|
||||
if (size >= blkSize)
|
||||
{
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!((u32)tDataPtr32 & 3))
|
||||
if (!((u32)tDataPtr32 & 3))
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
for (u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
sdmmc_write32(REG_SDFIFO32, *tDataPtr32++);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
for (u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
u32 data = *tDataPtr8++;
|
||||
data |= (u32)*tDataPtr8++ << 8;
|
||||
@ -181,16 +188,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!((u16)tDataPtr16 & 1))
|
||||
if (!((u16)tDataPtr16 & 1))
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
for (u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
sdmmc_write16(REG_SDFIFO, *tDataPtr16++);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
for (u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
u16 data = *tDataPtr8++;
|
||||
data |= (u16)(*tDataPtr8++ << 8);
|
||||
@ -205,25 +212,25 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
sdmmc_mask16(REG_SDDATACTL32, 0x1000, 0);
|
||||
}
|
||||
}
|
||||
if(status1 & TMIO_MASK_GW)
|
||||
if (status1 & TMIO_MASK_GW)
|
||||
{
|
||||
ctx->error |= 4;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!(status1 & TMIO_STAT1_CMD_BUSY))
|
||||
if (!(status1 & TMIO_STAT1_CMD_BUSY))
|
||||
{
|
||||
status0 = sdmmc_read16(REG_SDSTATUS0);
|
||||
if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND)
|
||||
if (sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND)
|
||||
{
|
||||
ctx->error |= 0x1;
|
||||
}
|
||||
if(status0 & TMIO_STAT0_DATAEND)
|
||||
if (status0 & TMIO_STAT0_DATAEND)
|
||||
{
|
||||
ctx->error |= 0x2;
|
||||
}
|
||||
|
||||
if((status0 & flags) == flags)
|
||||
if ((status0 & flags) == flags)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -232,7 +239,7 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
sdmmc_write16(REG_SDSTATUS0,0);
|
||||
sdmmc_write16(REG_SDSTATUS1,0);
|
||||
|
||||
if(getSDRESP != 0)
|
||||
if (getSDRESP != 0)
|
||||
{
|
||||
ctx->ret[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16));
|
||||
ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16));
|
||||
@ -243,8 +250,9 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_cardinserted() {
|
||||
int my_sdmmc_cardinserted()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return 1; //my_sdmmc_cardready;
|
||||
}
|
||||
|
||||
@ -252,8 +260,9 @@ int my_sdmmc_cardinserted() {
|
||||
static bool my_sdmmc_controller_initialised = false;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_controller_init( bool force_init ) {
|
||||
void my_sdmmc_controller_init( bool force_init )
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
|
||||
if (!force_init && my_sdmmc_controller_initialised) return;
|
||||
|
||||
@ -313,11 +322,13 @@ void my_sdmmc_controller_init( bool force_init ) {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static u32 calcSDSize(u8* csd, int type) {
|
||||
static u32 calcSDSize(u8* csd, int type)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
u32 result = 0;
|
||||
if (type == -1) type = csd[14] >> 6;
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
u32 block_len = csd[9] & 0xf;
|
||||
@ -341,8 +352,9 @@ static u32 calcSDSize(u8* csd, int type) {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_init() {
|
||||
int my_sdmmc_sdcard_init()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
// We need to send at least 74 clock pulses.
|
||||
my_setTarget(&deviceSD);
|
||||
swiDelay(0x1980); // ~75-76 clocks
|
||||
@ -362,11 +374,13 @@ int my_sdmmc_sdcard_init() {
|
||||
// ACMD41
|
||||
my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp);
|
||||
temp2 = 1;
|
||||
} while ( !(deviceSD.error & 1) );
|
||||
}
|
||||
while ( !(deviceSD.error & 1) );
|
||||
|
||||
} while((deviceSD.ret[0] & 0x80000000) == 0);
|
||||
}
|
||||
while ((deviceSD.ret[0] & 0x80000000) == 0);
|
||||
|
||||
if(!((deviceSD.ret[0] >> 30) & 1) || !temp)
|
||||
if (!((deviceSD.ret[0] >> 30) & 1) || !temp)
|
||||
temp2 = 0;
|
||||
|
||||
deviceSD.isSDHC = temp2;
|
||||
@ -407,7 +421,7 @@ int my_sdmmc_sdcard_init() {
|
||||
sdmmc_mask16(REG_SDOPT, 0x8000, 0); // Switch to 4 bit mode.
|
||||
|
||||
// TODO: CMD6 to switch to high speed mode.
|
||||
if(cmd6Supported)
|
||||
if (cmd6Supported)
|
||||
{
|
||||
sdmmc_write16(REG_SDSTOP,0);
|
||||
sdmmc_write16(REG_SDBLKLEN32,64);
|
||||
@ -416,7 +430,7 @@ int my_sdmmc_sdcard_init() {
|
||||
deviceSD.size = 64;
|
||||
my_sdmmc_send_command(&deviceSD,0x31C06,0x80FFFFF1);
|
||||
sdmmc_write16(REG_SDBLKLEN,512);
|
||||
if(deviceSD.error & 0x4) return -9;
|
||||
if (deviceSD.error & 0x4) return -9;
|
||||
|
||||
deviceSD.clk = 0x200; // 33.513982 MHz
|
||||
setckl(0x200);
|
||||
@ -434,8 +448,9 @@ int my_sdmmc_sdcard_init() {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_init() {
|
||||
int my_sdmmc_nand_init()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
my_setTarget(&deviceNAND);
|
||||
swiDelay(0xF000);
|
||||
|
||||
@ -444,39 +459,40 @@ int my_sdmmc_nand_init() {
|
||||
do {
|
||||
do {
|
||||
my_sdmmc_send_command(&deviceNAND,0x10701,0x100000);
|
||||
} while ( !(deviceNAND.error & 1) );
|
||||
}
|
||||
while((deviceNAND.ret[0] & 0x80000000) == 0);
|
||||
while ( !(deviceNAND.error & 1) );
|
||||
}
|
||||
while ((deviceNAND.ret[0] & 0x80000000) == 0);
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10602,0x0);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0);
|
||||
deviceNAND.clk = 1;
|
||||
setckl(1);
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.SDOPT = 1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10410,0x200);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
if ((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.clk |= 0x200;
|
||||
|
||||
@ -486,8 +502,9 @@ int my_sdmmc_nand_init() {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *out) {
|
||||
int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *out)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
if (device->isSDHC == 0) sector_no <<= 9;
|
||||
my_setTarget(device);
|
||||
sdmmc_write16(REG_SDSTOP,0x100);
|
||||
@ -506,8 +523,9 @@ int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *in) {
|
||||
int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *in)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
if (device->isSDHC == 0)
|
||||
sector_no <<= 9;
|
||||
my_setTarget(device);
|
||||
@ -527,8 +545,9 @@ int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsector
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_get_cid(int devicenumber, u32 *cid) {
|
||||
void my_sdmmc_get_cid(int devicenumber, u32 *cid)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
|
||||
struct mmcdevice *device = (devicenumber == 1 ? &deviceNAND : &deviceSD);
|
||||
|
||||
@ -544,7 +563,7 @@ void my_sdmmc_get_cid(int devicenumber, u32 *cid) {
|
||||
// use cmd10 to read CID
|
||||
my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10);
|
||||
|
||||
for(int i = 0; i < 4; ++i)
|
||||
for (int i = 0; i < 4; ++i)
|
||||
cid[i] = device->ret[i];
|
||||
|
||||
// put sd card back to transfer mode
|
||||
@ -555,16 +574,17 @@ void my_sdmmc_get_cid(int devicenumber, u32 *cid) {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmcMsgHandler(int bytes, void *user_data) {
|
||||
void my_sdmmcMsgHandler(int bytes, void *user_data)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
FifoMessage msg;
|
||||
int retval = 0;
|
||||
|
||||
fifoGetDatamsg(FIFO_SDMMC, bytes, (u8*)&msg);
|
||||
|
||||
int oldIME = enterCriticalSection();
|
||||
switch (msg.type) {
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case SDMMC_SD_READ_SECTORS:
|
||||
retval = my_sdmmc_readsectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer);
|
||||
break;
|
||||
@ -585,28 +605,31 @@ void my_sdmmcMsgHandler(int bytes, void *user_data) {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_startup() {
|
||||
int my_sdmmc_nand_startup()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
my_sdmmc_controller_init(false);
|
||||
return my_sdmmc_nand_init();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sd_startup() {
|
||||
int my_sdmmc_sd_startup()
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
my_sdmmc_controller_init(false);
|
||||
return my_sdmmc_sdcard_init();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmcValueHandler(u32 value, void* user_data) {
|
||||
void my_sdmmcValueHandler(u32 value, void* user_data)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
int result = 0;
|
||||
int sdflag = 0;
|
||||
int oldIME = enterCriticalSection();
|
||||
|
||||
switch(value) {
|
||||
|
||||
switch (value)
|
||||
{
|
||||
case SDMMC_HAVE_SD:
|
||||
result = sdmmc_read16(REG_SDSTATUS0);
|
||||
break;
|
||||
@ -615,9 +638,12 @@ void my_sdmmcValueHandler(u32 value, void* user_data) {
|
||||
sdflag = 1;
|
||||
/* Falls through. */
|
||||
case SDMMC_NAND_START:
|
||||
if (sdmmc_read16(REG_SDSTATUS0) == 0) {
|
||||
if (sdmmc_read16(REG_SDSTATUS0) == 0)
|
||||
{
|
||||
result = 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
result = (sdflag == 1 ) ? my_sdmmc_sd_startup() : my_sdmmc_nand_startup();
|
||||
}
|
||||
break;
|
||||
@ -640,26 +666,30 @@ void my_sdmmcValueHandler(u32 value, void* user_data) {
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out) {
|
||||
int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return my_sdmmc_readsectors(&deviceSD, sector_no, numsectors, out);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, void *in) {
|
||||
int my_sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, void *in)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return my_sdmmc_writesectors(&deviceSD, sector_no, numsectors, in);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out) {
|
||||
int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return my_sdmmc_readsectors(&deviceNAND, sector_no, numsectors, out);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in) {
|
||||
int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return my_sdmmc_writesectors(&deviceNAND, sector_no, numsectors, in);
|
||||
}
|
||||
|
||||
|
||||
@ -136,11 +136,13 @@ int my_sdmmc_sdcard_init();
|
||||
int my_sdmmc_nand_init();
|
||||
void my_sdmmc_get_cid(int devicenumber, u32 *cid);
|
||||
|
||||
static inline void sdmmc_nand_cid( u32 *cid) {
|
||||
static inline void sdmmc_nand_cid( u32 *cid)
|
||||
{
|
||||
my_sdmmc_get_cid(MMC_DEVICE_NAND, cid);
|
||||
}
|
||||
|
||||
static inline void sdmmc_sdcard_cid( u32 *cid) {
|
||||
static inline void sdmmc_sdcard_cid( u32 *cid)
|
||||
{
|
||||
my_sdmmc_get_cid(MMC_DEVICE_SDCARD, cid);
|
||||
}
|
||||
|
||||
@ -153,32 +155,37 @@ extern u32 sdmmc_cid[];
|
||||
extern int sdmmc_curdevice;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline u16 sdmmc_read16(u16 reg) {
|
||||
static inline u16 sdmmc_read16(u16 reg)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return *(vu16*)(SDMMC_BASE + reg);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_write16(u16 reg, u16 val) {
|
||||
static inline void sdmmc_write16(u16 reg, u16 val)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
*(vu16*)(SDMMC_BASE + reg) = val;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline u32 sdmmc_read32(u16 reg) {
|
||||
static inline u32 sdmmc_read32(u16 reg)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
return *(vu32*)(SDMMC_BASE + reg);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_write32(u16 reg, u32 val) {
|
||||
static inline void sdmmc_write32(u16 reg, u32 val)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
*(vu32*)(SDMMC_BASE + reg) = val;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) {
|
||||
static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
u16 val = sdmmc_read16(reg);
|
||||
val &= ~clear;
|
||||
val |= set;
|
||||
@ -187,8 +194,9 @@ static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) {
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void setckl(u32 data) {
|
||||
static inline void setckl(u32 data)
|
||||
//---------------------------------------------------------------------------------
|
||||
{
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x100, 0);
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF);
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100);
|
||||
|
||||
@ -426,7 +426,7 @@ bool install(char* fpath, bool systemTitle)
|
||||
const char system[] = "\x1B[41mWARNING:\x1B[47m This is a system app,\ninstalling it is potentially\nmore risky than regular DSiWare.\n\x1B[33m";
|
||||
const char areYouSure[] = "Are you sure you want to install\n";
|
||||
char* msg = (char*)malloc(strlen(system) + strlen(areYouSure) + strlen(fpath) + 2);
|
||||
if(sdnandMode || h->tid_high == 0x00030004)
|
||||
if (sdnandMode || h->tid_high == 0x00030004)
|
||||
sprintf(msg, "%s%s?\n", areYouSure, fpath);
|
||||
else
|
||||
sprintf(msg, "%s%s%s?\n", system, areYouSure, fpath);
|
||||
|
||||
@ -75,7 +75,8 @@ void tmd_create(uint8_t* tmd, FILE* app)
|
||||
|
||||
// Phase 4 - offset 0x1AA (fill-in 0x80 value, 0x10 times)
|
||||
{
|
||||
for(size_t i = 0; i<0x10; i++) {
|
||||
for (size_t i = 0; i<0x10; i++)
|
||||
{
|
||||
tmd[0x1AA + i] = 0x80;
|
||||
}
|
||||
}
|
||||
@ -123,7 +124,7 @@ void tmd_create(uint8_t* tmd, FILE* app)
|
||||
|
||||
printProgressBar((float)fileread / (float)filesize);
|
||||
}
|
||||
while(buffer_read == SHA_BUFFER_SIZE);
|
||||
while (buffer_read == SHA_BUFFER_SIZE);
|
||||
|
||||
clearProgressBar();
|
||||
consoleSelect(&bottomScreen);
|
||||
@ -140,7 +141,8 @@ int maketmd(char* input, char* tmdPath)
|
||||
iprintf("MakeTMD for DSiWare Homebrew\n");
|
||||
iprintf("by Przemyslaw Skryjomski\n\t(Tuxality)\n");
|
||||
|
||||
if(input == NULL || tmdPath == NULL) {
|
||||
if (input == NULL || tmdPath == NULL)
|
||||
{
|
||||
iprintf("\x1B[33m"); //yellow
|
||||
iprintf("\nUsage: %s file.app <file.tmd>\n", "maketmd");
|
||||
iprintf("\x1B[47m"); //white
|
||||
@ -150,7 +152,8 @@ int maketmd(char* input, char* tmdPath)
|
||||
// APP file (input)
|
||||
FILE* app = fopen(input, "rb");
|
||||
|
||||
if(!app) {
|
||||
if (!app)
|
||||
{
|
||||
iprintf("\x1B[31m"); //red
|
||||
iprintf("Error at opening %s for reading.\n", input);
|
||||
iprintf("\x1B[47m"); //white
|
||||
|
||||
@ -9,9 +9,9 @@
|
||||
// https://github.com/Jimmy-Z/bfCL/blob/master/dsi.h
|
||||
// ported back to 32 bit for ARM9
|
||||
|
||||
static dsi_context nand_ctx ;
|
||||
static dsi_context boot2_ctx ;
|
||||
static dsi_es_context es_ctx ;
|
||||
static dsi_context nand_ctx;
|
||||
static dsi_context boot2_ctx;
|
||||
static dsi_es_context es_ctx;
|
||||
|
||||
static uint8_t nand_ctr_iv[16];
|
||||
static uint8_t boot2_ctr[16];
|
||||
@ -44,8 +44,8 @@ static void generate_key(uint8_t *generated_key, const uint32_t *console_id, con
|
||||
}
|
||||
u128_xor((uint8_t *)key, mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y);
|
||||
u128_add((uint8_t *)key, DSi_KEY_MAGIC);
|
||||
u128_lrot((uint8_t *)key, 42) ;
|
||||
memcpy(generated_key, key, 16) ;
|
||||
u128_lrot((uint8_t *)key, 42);
|
||||
memcpy(generated_key, key, 16);
|
||||
}
|
||||
|
||||
int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len)
|
||||
@ -63,7 +63,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
|
||||
|
||||
uint8_t key[16];
|
||||
generate_key(key, console_id, is3DS ? NAND_3DS : NAND);
|
||||
dsi_set_key(&nand_ctx, key) ;
|
||||
dsi_set_key(&nand_ctx, key);
|
||||
|
||||
u32 normalkey[4];
|
||||
u32 tadsrl_keyX[4] = {0x4E00004A, 0x4A00004E, 0, 0};
|
||||
@ -72,7 +72,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
|
||||
F_XY((u8 *)normalkey, (u8 *)tadsrl_keyX, DSi_ES_KEY_Y);
|
||||
dsi_es_init(&es_ctx, (u8*)normalkey);
|
||||
|
||||
dsi_set_key(&boot2_ctx, DSi_BOOT2_KEY) ;
|
||||
dsi_set_key(&boot2_ctx, DSi_BOOT2_KEY);
|
||||
|
||||
swiSHA1Calc(nand_ctr_iv, emmc_cid, 16);
|
||||
|
||||
@ -82,22 +82,22 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
|
||||
// offset as block offset, block as AES block
|
||||
void dsi_nand_crypt_1(uint8_t* out, const uint8_t* in, uint32_t offset)
|
||||
{
|
||||
uint8_t ctr[16] ;
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ;
|
||||
uint8_t ctr[16];
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv));
|
||||
u128_add32(ctr, offset);
|
||||
dsi_set_ctr(&nand_ctx, ctr) ;
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16) ;
|
||||
dsi_set_ctr(&nand_ctx, ctr);
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16);
|
||||
}
|
||||
|
||||
void dsi_nand_crypt(uint8_t* out, const uint8_t* in, uint32_t offset, unsigned count)
|
||||
{
|
||||
uint8_t ctr[16] ;
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ;
|
||||
uint8_t ctr[16];
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv));
|
||||
u128_add32(ctr, offset);
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
dsi_set_ctr(&nand_ctx, ctr) ;
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16) ;
|
||||
dsi_set_ctr(&nand_ctx, ctr);
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16);
|
||||
out += AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
u128_add32(ctr, 1);
|
||||
@ -106,7 +106,7 @@ void dsi_nand_crypt(uint8_t* out, const uint8_t* in, uint32_t offset, unsigned c
|
||||
|
||||
int dsi_es_block_crypt(uint8_t *buf, unsigned buf_len, crypt_mode_t mode)
|
||||
{
|
||||
if(mode == DECRYPT)
|
||||
if (mode == DECRYPT)
|
||||
return dsi_es_decrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20);
|
||||
else
|
||||
dsi_es_encrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20);
|
||||
@ -118,10 +118,10 @@ void dsi_boot2_crypt_set_ctr(uint32_t size_r)
|
||||
{
|
||||
for (int i=0;i<4;i++)
|
||||
{
|
||||
boot2_ctr[i] = (size_r) >> (8*i) ;
|
||||
boot2_ctr[i+4] = (-size_r) >> (8*i) ;
|
||||
boot2_ctr[i+8] = (~size_r) >> (8*i) ;
|
||||
boot2_ctr[i+12] = 0 ;
|
||||
boot2_ctr[i] = (size_r) >> (8*i);
|
||||
boot2_ctr[i+4] = (-size_r) >> (8*i);
|
||||
boot2_ctr[i+8] = (~size_r) >> (8*i);
|
||||
boot2_ctr[i+12] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,8 +129,8 @@ void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count)
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
dsi_set_ctr(&boot2_ctx, boot2_ctr) ;
|
||||
dsi_crypt_ctr(&boot2_ctx, in, out, 16) ;
|
||||
dsi_set_ctr(&boot2_ctx, boot2_ctr);
|
||||
dsi_crypt_ctr(&boot2_ctx, in, out, 16);
|
||||
out += AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
u128_add32(boot2_ctr, 1);
|
||||
|
||||
@ -16,14 +16,22 @@
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
const uint8_t DSi_KEY_MAGIC[16] = { 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a,
|
||||
0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff };
|
||||
const uint8_t DSi_NAND_KEY_Y[16] = { 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd,
|
||||
0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1 };
|
||||
const uint8_t DSi_ES_KEY_Y[16] = { 0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72,
|
||||
0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9 };
|
||||
const uint8_t DSi_BOOT2_KEY[16] = { 0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6,
|
||||
0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad };
|
||||
const uint8_t DSi_KEY_MAGIC[16] = {
|
||||
0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a,
|
||||
0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff
|
||||
};
|
||||
const uint8_t DSi_NAND_KEY_Y[16] = {
|
||||
0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd,
|
||||
0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1
|
||||
};
|
||||
const uint8_t DSi_ES_KEY_Y[16] = {
|
||||
0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72,
|
||||
0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9
|
||||
};
|
||||
const uint8_t DSi_BOOT2_KEY[16] = {
|
||||
0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6,
|
||||
0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad
|
||||
};
|
||||
|
||||
/************************ Functions *******************************************/
|
||||
|
||||
@ -31,7 +39,7 @@ void F_XY(uint8_t *key, const uint8_t *key_x, const uint8_t *key_y)
|
||||
{
|
||||
uint8_t key_xy[16];
|
||||
|
||||
for(int i=0; i<16; i++)
|
||||
for (int i=0; i<16; i++)
|
||||
key_xy[i] = key_x[i] ^ key_y[i];
|
||||
|
||||
memcpy(key, DSi_KEY_MAGIC, sizeof(DSi_KEY_MAGIC));
|
||||
@ -47,4 +55,3 @@ void F_XY_reverse(const uint8_t *key, uint8_t *key_xy)
|
||||
u128_rrot(key_xy, 42);
|
||||
u128_sub(key_xy, DSi_KEY_MAGIC);
|
||||
}
|
||||
|
||||
|
||||
@ -18,10 +18,10 @@ extern "C" {
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
extern const uint8_t DSi_KEY_MAGIC[16] ;
|
||||
extern const uint8_t DSi_NAND_KEY_Y[16] ;
|
||||
extern const uint8_t DSi_ES_KEY_Y[16] ;
|
||||
extern const uint8_t DSi_BOOT2_KEY[16] ;
|
||||
extern const uint8_t DSi_KEY_MAGIC[16];
|
||||
extern const uint8_t DSi_NAND_KEY_Y[16];
|
||||
extern const uint8_t DSi_ES_KEY_Y[16];
|
||||
extern const uint8_t DSi_BOOT2_KEY[16];
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
@ -33,4 +33,3 @@ void F_XY_reverse(const uint8_t *key, uint8_t *key_xy);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -12,12 +12,12 @@
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
bool nandio_startup() ;
|
||||
bool nandio_is_inserted() ;
|
||||
bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) ;
|
||||
bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) ;
|
||||
bool nandio_clear_status() ;
|
||||
bool nandio_shutdown() ;
|
||||
bool nandio_startup();
|
||||
bool nandio_is_inserted();
|
||||
bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer);
|
||||
bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer);
|
||||
bool nandio_clear_status();
|
||||
bool nandio_shutdown();
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
@ -61,7 +61,7 @@ void getConsoleID(u8 *consoleID)
|
||||
|
||||
F_XY_reverse(key, key_x); //work backwards from the normalkey to get key_x that has the consoleID
|
||||
|
||||
u128_xor(key_x, DSi_NAND_KEY_Y) ;
|
||||
u128_xor(key_x, DSi_NAND_KEY_Y);
|
||||
|
||||
memcpy(&consoleID[0], &key_x[0], 4);
|
||||
memcpy(&consoleID[4], &key_x[0xC], 4);
|
||||
@ -127,7 +127,9 @@ static bool read_sectors(sec_t start, sec_t len, void *buffer)
|
||||
((u8*)buffer)[0x38] = 'T';
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -135,14 +137,16 @@ static bool read_sectors(sec_t start, sec_t len, void *buffer)
|
||||
// len is guaranteed <= CRYPT_BUF_LEN
|
||||
static bool write_sectors(sec_t start, sec_t len, const void *buffer)
|
||||
{
|
||||
static u8 writeCopy[SECTOR_SIZE*16] ;
|
||||
memcpy(writeCopy, buffer, len * SECTOR_SIZE) ;
|
||||
static u8 writeCopy[SECTOR_SIZE*16];
|
||||
memcpy(writeCopy, buffer, len * SECTOR_SIZE);
|
||||
|
||||
dsi_nand_crypt(crypt_buf, writeCopy, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE);
|
||||
if (nand_WriteSectors(start, len, crypt_buf))
|
||||
{
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -210,10 +214,10 @@ bool nandio_shutdown()
|
||||
// this allows us to revert changes in the FAT if we did not properly finish
|
||||
// and did not push the changes to the other copies
|
||||
// to do this we read the first partition sector
|
||||
nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ;
|
||||
u8 stagingLevels = sector_buf[0x10] ;
|
||||
u8 reservedSectors = sector_buf[0x0E] ;
|
||||
u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ;
|
||||
nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf);
|
||||
u8 stagingLevels = sector_buf[0x10];
|
||||
u8 reservedSectors = sector_buf[0x0E];
|
||||
u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8);
|
||||
/*
|
||||
iprintf("[i] Staging for %i FAT copies\n",stagingLevels);
|
||||
iprintf("[i] Stages starting at %i\n",reservedSectors);
|
||||
@ -224,12 +228,12 @@ bool nandio_shutdown()
|
||||
for (u32 sector = 0;sector < sectorsPerFatCopy; sector++)
|
||||
{
|
||||
// read fat sector
|
||||
nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ;
|
||||
nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf);
|
||||
// write to each copy, except the source copy
|
||||
writingLocked = false;
|
||||
for (int stage = 1;stage < stagingLevels;stage++)
|
||||
{
|
||||
nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ;
|
||||
nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf);
|
||||
}
|
||||
writingLocked = true;
|
||||
}
|
||||
|
||||
@ -18,13 +18,13 @@ extern const DISC_INTERFACE io_dsi_nand;
|
||||
|
||||
void nandio_set_fat_sig_fix(uint32_t offset);
|
||||
|
||||
void getConsoleID(uint8_t *consoleID) ;
|
||||
void getConsoleID(uint8_t *consoleID);
|
||||
|
||||
extern bool nandio_shutdown() ;
|
||||
extern bool nandio_shutdown();
|
||||
|
||||
extern bool nandio_lock_writing() ;
|
||||
extern bool nandio_unlock_writing() ;
|
||||
extern bool nandio_force_fat_fix() ;
|
||||
extern bool nandio_lock_writing();
|
||||
extern bool nandio_unlock_writing();
|
||||
extern bool nandio_force_fat_fix();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -61,7 +61,8 @@ int parse_ncsd(const uint8_t sector0[SECTOR_SIZE])
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch (fs_type) {
|
||||
switch (fs_type)
|
||||
{
|
||||
case 1:
|
||||
case 3:
|
||||
case 4:
|
||||
|
||||
@ -42,21 +42,18 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t offset,
|
||||
length;
|
||||
uint32_t offset, length;
|
||||
} __PACKED ncsd_partition_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t digest[NCSD_SIGNATURESIZE] ;
|
||||
} __PACKED ncsd_sginature ;
|
||||
uint8_t digest[NCSD_SIGNATURESIZE];
|
||||
} __PACKED ncsd_sginature;
|
||||
|
||||
typedef struct {
|
||||
ncsd_sginature signature;
|
||||
uint32_t magic,
|
||||
size;
|
||||
uint32_t magic, size;
|
||||
uint64_t media_id;
|
||||
uint8_t fs_types[NCSD_PARTITIONS],
|
||||
crypt_types[NCSD_PARTITIONS];
|
||||
uint8_t fs_types[NCSD_PARTITIONS], crypt_types[NCSD_PARTITIONS];
|
||||
ncsd_partition_t partitions[NCSD_PARTITIONS];
|
||||
} __PACKED ncsd_header_t;
|
||||
|
||||
@ -159,10 +156,8 @@ int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS);
|
||||
|
||||
/************************ static code verification ****************************/
|
||||
|
||||
static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE,
|
||||
"sizeof(ncsd_header_t) should equal 0x160");
|
||||
static_assert(sizeof(mbr_t) == SECTOR_SIZE,
|
||||
"sizeof(mbr_t) should equal 0x200");
|
||||
static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE, "sizeof(ncsd_header_t) should equal 0x160");
|
||||
static_assert(sizeof(mbr_t) == SECTOR_SIZE, "sizeof(mbr_t) should equal 0x200");
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -5,28 +5,26 @@
|
||||
#include <time.h>
|
||||
#include "u128_math.h"
|
||||
|
||||
void dsi_set_key( dsi_context* ctx,
|
||||
const unsigned char key[16] )
|
||||
void dsi_set_key(dsi_context* ctx, const unsigned char key[16])
|
||||
{
|
||||
unsigned char keyswap[16];
|
||||
u128_swap(keyswap, key) ;
|
||||
u128_swap(keyswap, key);
|
||||
|
||||
aes_setkey_enc(&ctx->aes, keyswap, 128);
|
||||
}
|
||||
|
||||
void dsi_add_ctr( dsi_context* ctx,
|
||||
unsigned int carry)
|
||||
void dsi_add_ctr(dsi_context* ctx, unsigned int carry)
|
||||
{
|
||||
unsigned int counter[4];
|
||||
unsigned char *outctr = (unsigned char*)ctx->ctr;
|
||||
int sum;
|
||||
signed int i;
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
for (i = 0; i < 4; i++)
|
||||
counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) |
|
||||
(outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0);
|
||||
|
||||
for(i = 3; i >= 0; i--)
|
||||
for (i = 3; i >= 0; i--)
|
||||
{
|
||||
sum = counter[i] + carry;
|
||||
|
||||
@ -38,7 +36,7 @@ void dsi_add_ctr( dsi_context* ctx,
|
||||
counter[i] = sum;
|
||||
}
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
outctr[i * 4 + 0] = counter[i] >> 24;
|
||||
outctr[i * 4 + 1] = counter[i] >> 16;
|
||||
@ -47,38 +45,30 @@ void dsi_add_ctr( dsi_context* ctx,
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_set_ctr( dsi_context* ctx,
|
||||
const unsigned char ctr[16] )
|
||||
void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16])
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
ctx->ctr[i] = ctr[15-i];
|
||||
}
|
||||
|
||||
void dsi_init_ctr( dsi_context* ctx,
|
||||
const unsigned char key[16],
|
||||
const unsigned char ctr[12] )
|
||||
void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12])
|
||||
{
|
||||
dsi_set_key(ctx, key);
|
||||
dsi_set_ctr(ctx, ctr);
|
||||
}
|
||||
|
||||
void dsi_crypt_ctr( dsi_context* ctx,
|
||||
const void* in,
|
||||
void* out,
|
||||
unsigned int len)
|
||||
void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len)
|
||||
{
|
||||
unsigned int i;
|
||||
for(i = 0; i < len; i += 0x10)
|
||||
for (i = 0; i < len; i += 0x10)
|
||||
{
|
||||
dsi_crypt_ctr_block(ctx, in+i, out+i);
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16])
|
||||
{
|
||||
int i;
|
||||
unsigned char stream[16];
|
||||
@ -89,14 +79,14 @@ void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
|
||||
if (input)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
{
|
||||
output[i] = stream[15-i] ^ input[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
output[i] = stream[15-i];
|
||||
}
|
||||
|
||||
@ -104,12 +94,8 @@ void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
}
|
||||
|
||||
|
||||
void dsi_init_ccm( dsi_context* ctx,
|
||||
unsigned char key[16],
|
||||
unsigned int maclength,
|
||||
unsigned int payloadlength,
|
||||
unsigned int assoclength,
|
||||
unsigned char nonce[12] )
|
||||
void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength,
|
||||
unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12])
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -128,7 +114,7 @@ void dsi_init_ccm( dsi_context* ctx,
|
||||
ctx->mac[0] = (maclength<<3) | 2;
|
||||
if (assoclength)
|
||||
ctx->mac[0] |= (1<<6);
|
||||
for(i=0; i<12; i++)
|
||||
for (i=0; i<12; i++)
|
||||
ctx->mac[1+i] = nonce[11-i];
|
||||
ctx->mac[13] = payloadlength>>16;
|
||||
ctx->mac[14] = payloadlength>>8;
|
||||
@ -139,7 +125,7 @@ void dsi_init_ccm( dsi_context* ctx,
|
||||
// CCM CTR:
|
||||
// [1-byte flags] [12-byte nonce] [3-byte ctr]
|
||||
ctx->ctr[0] = 2;
|
||||
for(i=0; i<12; i++)
|
||||
for (i=0; i<12; i++)
|
||||
ctx->ctr[1+i] = nonce[11-i];
|
||||
ctx->ctr[13] = 0;
|
||||
ctx->ctr[14] = 0;
|
||||
@ -148,21 +134,18 @@ void dsi_init_ccm( dsi_context* ctx,
|
||||
dsi_crypt_ctr_block(ctx, 0, ctx->S0);
|
||||
}
|
||||
|
||||
void dsi_encrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac )
|
||||
void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
ctx->mac[i] ^= input[15-i];
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac);
|
||||
|
||||
if (mac)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
mac[i] = ctx->mac[15-i] ^ ctx->S0[i];
|
||||
}
|
||||
|
||||
@ -171,10 +154,7 @@ void dsi_encrypt_ccm_block( dsi_context* ctx,
|
||||
}
|
||||
|
||||
|
||||
void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac )
|
||||
void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac)
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -184,12 +164,12 @@ void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
dsi_crypt_ctr_block(ctx, input, output);
|
||||
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
ctx->mac[i] ^= output[15-i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
ctx->mac[i] ^= input[15-i];
|
||||
}
|
||||
|
||||
@ -198,22 +178,18 @@ void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
|
||||
if (mac)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
for (i=0; i<16; i++)
|
||||
mac[i] = ctx->mac[15-i] ^ ctx->S0[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dsi_decrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac )
|
||||
void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac)
|
||||
{
|
||||
unsigned char block[16];
|
||||
unsigned char ctr[16];
|
||||
|
||||
while(size > 16)
|
||||
while (size > 16)
|
||||
{
|
||||
dsi_decrypt_ccm_block(ctx, input, output, mac);
|
||||
|
||||
@ -237,15 +213,11 @@ void dsi_decrypt_ccm( dsi_context* ctx,
|
||||
}
|
||||
|
||||
|
||||
void dsi_encrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac )
|
||||
void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac)
|
||||
{
|
||||
unsigned char block[16];
|
||||
|
||||
while(size > 16)
|
||||
while (size > 16)
|
||||
{
|
||||
dsi_encrypt_ccm_block(ctx, input, output, mac);
|
||||
|
||||
@ -263,30 +235,25 @@ void dsi_encrypt_ccm( dsi_context* ctx,
|
||||
memcpy(output, block, size);
|
||||
}
|
||||
|
||||
void dsi_es_init( dsi_es_context* ctx,
|
||||
unsigned char key[16] )
|
||||
void dsi_es_init(dsi_es_context* ctx, unsigned char key[16])
|
||||
{
|
||||
memcpy(ctx->key, key, 16);
|
||||
ctx->randomnonce = 1;
|
||||
}
|
||||
|
||||
void dsi_es_set_nonce( dsi_es_context* ctx,
|
||||
unsigned char nonce[12] )
|
||||
void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12])
|
||||
{
|
||||
memcpy(ctx->nonce, nonce, 12);
|
||||
ctx->randomnonce = 0;
|
||||
}
|
||||
|
||||
void dsi_es_set_random_nonce( dsi_es_context* ctx )
|
||||
void dsi_es_set_random_nonce(dsi_es_context* ctx)
|
||||
{
|
||||
ctx->randomnonce = 1;
|
||||
}
|
||||
|
||||
|
||||
int dsi_es_decrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size )
|
||||
int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size)
|
||||
{
|
||||
unsigned char ctr[16];
|
||||
unsigned char nonce[12];
|
||||
@ -334,10 +301,7 @@ int dsi_es_decrypt( dsi_es_context* ctx,
|
||||
}
|
||||
|
||||
|
||||
void dsi_es_encrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size )
|
||||
void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size)
|
||||
{
|
||||
int i;
|
||||
unsigned char nonce[12];
|
||||
@ -348,9 +312,9 @@ void dsi_es_encrypt( dsi_es_context* ctx,
|
||||
|
||||
if (ctx->randomnonce)
|
||||
{
|
||||
srand( (unsigned int)time(0) );
|
||||
srand((unsigned int)time(0));
|
||||
|
||||
for(i=0; i<12; i++)
|
||||
for (i=0; i<12; i++)
|
||||
nonce[i] = rand();
|
||||
}
|
||||
else
|
||||
|
||||
@ -27,75 +27,39 @@ typedef struct
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void dsi_set_key( dsi_context* ctx,
|
||||
const unsigned char key[16] );
|
||||
void dsi_set_key(dsi_context* ctx, const unsigned char key[16]);
|
||||
|
||||
void dsi_add_ctr( dsi_context* ctx,
|
||||
unsigned int carry );
|
||||
void dsi_add_ctr(dsi_context* ctx, unsigned int carry);
|
||||
|
||||
void dsi_set_ctr( dsi_context* ctx,
|
||||
const unsigned char ctr[16] );
|
||||
void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16]);
|
||||
|
||||
void dsi_init_ctr( dsi_context* ctx,
|
||||
const unsigned char key[16],
|
||||
const unsigned char ctr[12] );
|
||||
void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12]);
|
||||
|
||||
void dsi_crypt_ctr( dsi_context* ctx,
|
||||
const void* in,
|
||||
void* out,
|
||||
unsigned int len);
|
||||
void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len);
|
||||
|
||||
void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16]);
|
||||
|
||||
void dsi_init_ccm( dsi_context* ctx,
|
||||
unsigned char key[16],
|
||||
unsigned int maclength,
|
||||
unsigned int payloadlength,
|
||||
unsigned int assoclength,
|
||||
unsigned char nonce[12] );
|
||||
void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength,
|
||||
unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12]);
|
||||
|
||||
void dsi_encrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac );
|
||||
void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac);
|
||||
|
||||
void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac );
|
||||
void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac);
|
||||
|
||||
|
||||
void dsi_decrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac );
|
||||
void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac);
|
||||
|
||||
void dsi_encrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac );
|
||||
void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac);
|
||||
|
||||
void dsi_es_init( dsi_es_context* ctx,
|
||||
unsigned char key[16] );
|
||||
void dsi_es_init(dsi_es_context* ctx, unsigned char key[16]);
|
||||
|
||||
void dsi_es_set_nonce( dsi_es_context* ctx,
|
||||
unsigned char nonce[12] );
|
||||
void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12]);
|
||||
|
||||
void dsi_es_set_random_nonce( dsi_es_context* ctx );
|
||||
void dsi_es_set_random_nonce(dsi_es_context* ctx);
|
||||
|
||||
int dsi_es_decrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size );
|
||||
int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size);
|
||||
|
||||
void dsi_es_encrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size );
|
||||
void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -11,12 +11,12 @@ void u128_lrot(uint8_t *num, uint32_t shift)
|
||||
{
|
||||
// rot: rotate to more significant.
|
||||
// LSB is tmp[0], MSB is tmp[15]
|
||||
const uint32_t byteshift = shift / 8 ;
|
||||
const uint32_t byteshift = shift / 8;
|
||||
const uint32_t bitshift = shift % 8;
|
||||
tmp[(i+byteshift) % 16] = (num[i] << bitshift)
|
||||
| ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff);
|
||||
}
|
||||
memcpy(num, tmp, 16) ;
|
||||
memcpy(num, tmp, 16);
|
||||
}
|
||||
|
||||
// rotate a 128bit, little endian by shift bits in direction of decreasing significance.
|
||||
@ -27,12 +27,12 @@ void u128_rrot(uint8_t *num, uint32_t shift)
|
||||
{
|
||||
// rot: rotate to less significant.
|
||||
// LSB is tmp[0], MSB is tmp[15]
|
||||
const uint32_t byteshift = shift / 8 ;
|
||||
const uint32_t byteshift = shift / 8;
|
||||
const uint32_t bitshift = shift % 8;
|
||||
tmp[i] = (num[(i+byteshift) % 16] >> bitshift)
|
||||
| ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff);
|
||||
}
|
||||
memcpy(num, tmp, 16) ;
|
||||
memcpy(num, tmp, 16);
|
||||
}
|
||||
|
||||
// xor two 128bit, little endian values and store the result into the first
|
||||
@ -40,7 +40,7 @@ void u128_xor(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] ^ b[i] ;
|
||||
a[i] = a[i] ^ b[i];
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ void u128_or(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] | b[i] ;
|
||||
a[i] = a[i] | b[i];
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ void u128_and(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] & b[i] ;
|
||||
a[i] = a[i] & b[i];
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,12 +66,12 @@ void u128_and(uint8_t *a, const uint8_t *b)
|
||||
// add two 128bit, little endian values and store the result into the first
|
||||
void u128_add(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
uint8_t carry = 0 ;
|
||||
uint8_t carry = 0;
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
uint16_t sum = a[i] + b[i] + carry ;
|
||||
a[i] = sum & 0xff ;
|
||||
carry = sum >> 8 ;
|
||||
uint16_t sum = a[i] + b[i] + carry;
|
||||
a[i] = sum & 0xff;
|
||||
carry = sum >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,21 +79,21 @@ void u128_add(uint8_t *a, const uint8_t *b)
|
||||
void u128_add32(uint8_t *a, const uint32_t b)
|
||||
{
|
||||
uint8_t _b[16];
|
||||
memset(_b, 0, sizeof(_b)) ;
|
||||
memset(_b, 0, sizeof(_b));
|
||||
for (int i=0;i<4;i++)
|
||||
_b[i] = b >> (i*8) ;
|
||||
_b[i] = b >> (i*8);
|
||||
u128_add(a, _b);
|
||||
}
|
||||
|
||||
// sub two 128bit, little endian values and store the result into the first
|
||||
void u128_sub(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
uint8_t carry = 0 ;
|
||||
uint8_t carry = 0;
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
uint16_t sub = a[i] - b[i] - (carry & 1);
|
||||
a[i] = sub & 0xff ;
|
||||
carry = sub >> 8 ;
|
||||
a[i] = sub & 0xff;
|
||||
carry = sub >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,31 +7,31 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
// left rotate by (shift % 128) bits
|
||||
void u128_lrot(uint8_t *num, uint32_t shift) ;
|
||||
void u128_lrot(uint8_t *num, uint32_t shift);
|
||||
|
||||
// right rotate by (shift % 128) bits
|
||||
void u128_rrot(uint8_t *num, uint32_t shift) ;
|
||||
void u128_rrot(uint8_t *num, uint32_t shift);
|
||||
|
||||
// bitwise xor strored to a
|
||||
void u128_xor(uint8_t *a, const uint8_t *b) ;
|
||||
void u128_xor(uint8_t *a, const uint8_t *b);
|
||||
|
||||
// bitwise or strored to a
|
||||
void u128_or(uint8_t *a, const uint8_t *b) ;
|
||||
void u128_or(uint8_t *a, const uint8_t *b);
|
||||
|
||||
// bitwise and strored to a
|
||||
void u128_and(uint8_t *a, const uint8_t *b) ;
|
||||
void u128_and(uint8_t *a, const uint8_t *b);
|
||||
|
||||
// add b to a and store in a
|
||||
void u128_add(uint8_t *a, const uint8_t *b) ;
|
||||
void u128_add(uint8_t *a, const uint8_t *b);
|
||||
|
||||
// add 32 bit value b to a and store in a
|
||||
void u128_add32(uint8_t *a, const uint32_t b) ;
|
||||
void u128_add32(uint8_t *a, const uint32_t b);
|
||||
|
||||
// substract b from a and store in a
|
||||
void u128_sub(uint8_t *a, const uint8_t *b) ;
|
||||
void u128_sub(uint8_t *a, const uint8_t *b);
|
||||
|
||||
// swap byte order
|
||||
void u128_swap(uint8_t *out, const uint8_t *in) ;
|
||||
void u128_swap(uint8_t *out, const uint8_t *in);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user