AES暗号化対応

git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/trunk@45 b08762b0-b915-fc4b-9d8c-17b2551a87ff
This commit is contained in:
yutaka 2007-10-11 08:15:35 +00:00
parent 9489eeb559
commit c3871ba1fa
4 changed files with 42 additions and 31 deletions

View File

@ -216,7 +216,7 @@ static u16 ReadAES(u32 block, void *dest, u16 count)
{ {
u32 offset = 0; // in bytes u32 offset = 0; // in bytes
//OS_TPrintf("ReadAES(%d, 0x%08X, %d) is calling.\n", block, dest, count); OS_TPrintf("ReadAES(%d, 0x%08X, %d) is calling.\n", block, dest, count);
MIi_NDmaPipeSetup( DMA_PIPE, (void*)SDIF_FI, (void*)REG_AES_IFIFO_ADDR, PIPE_SIZE ); MIi_NDmaPipeSetup( DMA_PIPE, (void*)SDIF_FI, (void*)REG_AES_IFIFO_ADDR, PIPE_SIZE );
@ -243,7 +243,7 @@ static u16 ReadAES(u32 block, void *dest, u16 count)
while ( AES_GET_CNT_BITS( reg_AES_AES_CNT, IFIFO_CNT ) ) while ( AES_GET_CNT_BITS( reg_AES_AES_CNT, IFIFO_CNT ) )
{ {
} }
if ( (offset & SECTOR_SIZE) == 0 ) if ( (offset & (SECTOR_SIZE-1)) == 0 )
{ {
WaitFifoFull(); WaitFifoFull();
if ( SDCARD_ErrStatus != SDMC_NORMAL ) if ( SDCARD_ErrStatus != SDMC_NORMAL )

View File

@ -74,8 +74,6 @@ BOOL FATFS_OpenSpecifiedMenu( const char* menufile )
#define HEADER_SIZE 0x1000 #define HEADER_SIZE 0x1000
#define AUTH_SIZE ROM_HEADER_SIGN_TARGET_SIZE #define AUTH_SIZE ROM_HEADER_SIGN_TARGET_SIZE
#define SLOT_SIZE 0x8000
#ifndef SDK_FINALROM #ifndef SDK_FINALROM
#define PROFILE_PXI_SEND 1000000000 #define PROFILE_PXI_SEND 1000000000
#define PROFILE_PXI_RECV 2000000000 #define PROFILE_PXI_RECV 2000000000
@ -85,7 +83,7 @@ extern u32 pf_cnt;
static BOOL FATFS_LoadBuffer(u32 offset, u32 size) static BOOL FATFS_LoadBuffer(u32 offset, u32 size)
{ {
u8* base = (u8*)MI_GetWramMapStart_B(); u8* base = (u8*)HW_FIRM_LOAD_BUFFER_BASE;
static int count = 0; static int count = 0;
// seek first // seek first
@ -100,9 +98,10 @@ static BOOL FATFS_LoadBuffer(u32 offset, u32 size)
// loading loop // loading loop
while (size > 0) while (size > 0)
{ {
u8* dest = base + count * SLOT_SIZE; // target buffer address u8* dest = base + count * HW_FIRM_LOAD_BUFFER_UNIT_SIZE; // target buffer address
u32 unit = size < SLOT_SIZE ? size : SLOT_SIZE; // size u32 unit = size < HW_FIRM_LOAD_BUFFER_UNIT_SIZE ? size : HW_FIRM_LOAD_BUFFER_UNIT_SIZE; // size
while (MI_GetWramBankMaster_B(count) != MI_WRAM_ARM7) // waiting to be master OS_TPrintf("%s: dest=%X, unit=%X\n", __func__, dest, unit);
while (MI_GetWramBankMaster_B(count) != MI_WRAM_ARM7) // waiting to be master
{ {
} }
#ifndef SDK_FINALROM #ifndef SDK_FINALROM
@ -119,7 +118,7 @@ static BOOL FATFS_LoadBuffer(u32 offset, u32 size)
profile[pf_cnt++] = (u32)PROFILE_PXI_SEND | FIRM_PXI_ID_LOAD_PIRIOD; // checkpoint profile[pf_cnt++] = (u32)PROFILE_PXI_SEND | FIRM_PXI_ID_LOAD_PIRIOD; // checkpoint
#endif #endif
PXI_NotifyID( FIRM_PXI_ID_LOAD_PIRIOD ); PXI_NotifyID( FIRM_PXI_ID_LOAD_PIRIOD );
count = (count + 1) & 0x7; count = (count + 1) % HW_FIRM_LOAD_BUFFER_UNIT_NUMS;
size -= unit; size -= unit;
} }
return TRUE; return TRUE;
@ -176,10 +175,15 @@ BOOL FATFS_LoadHeader( void )
AESKeySeed seed; AESKeySeed seed;
AESi_InitGameKeys((u8*)rh->s.game_code); AESi_InitGameKeys((u8*)rh->s.game_code);
PXI_RecvDataByFifo( PXI_FIFO_TAG_DATA, &seed, AES_BLOCK_SIZE ); PXI_RecvDataByFifo( PXI_FIFO_TAG_DATA, &seed, AES_BLOCK_SIZE );
AESi_WaitKey();
AESi_SetKeySeedA(&seed); // APP AESi_SetKeySeedA(&seed); // APP
//AESi_WaitKey();
//AESi_SetKeySeedB(&seed); // APP & HARD //AESi_SetKeySeedB(&seed); // APP & HARD
//AESi_WaitKey();
//AESi_SetKeySeedC(&seed); // //AESi_SetKeySeedC(&seed); //
//AESi_WaitKey();
//AESi_SetKeySeedD(&seed); // HARD //AESi_SetKeySeedD(&seed); // HARD
AESi_WaitKey();
AESi_SetKeyC(&seed); // Direct AESi_SetKeyC(&seed); // Direct
} }
@ -198,9 +202,9 @@ BOOL FATFS_LoadHeader( void )
static AESCounter* FATFSi_GetCounter( u32 offset ) static AESCounter* FATFSi_GetCounter( u32 offset )
{ {
static AESCounter counter; static AESCounter counter;
MI_CpuCopy8( rh->s.main_static_digest, &counter, 12 );
counter.words[3] = 0; MI_CpuCopy8( rh->s.main_static_digest, &counter, 16 );
AESi_AddCounter( &counter, offset - offsetof(ROM_Header, s.aes_target_rom_offset) ); AESi_AddCounter( &counter, offset - rh->s.aes_target_rom_offset );
return &counter; return &counter;
} }
@ -216,17 +220,24 @@ static AESCounter* FATFSi_GetCounter( u32 offset )
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
static void FATFSi_SetupAES( u32 offset, u32 size ) static void FATFSi_SetupAES( u32 offset, u32 size )
{ {
if ( !rh->s.enable_aes ) if ( rh->s.enable_aes &&
{ offset >= rh->s.aes_target_rom_offset &&
FATFS_DisableAES();
}
else if ( offset >= rh->s.aes_target_rom_offset &&
offset + size <= rh->s.aes_target_rom_offset + rh->s.aes_target_size ) offset + size <= rh->s.aes_target_rom_offset + rh->s.aes_target_size )
{ {
AESi_WaitKey(); AESi_WaitKey();
//AESi_LoadKey( AES_KEY_SLOT_A ); if (rh->s.developer_encrypt)
AESi_LoadKey( AES_KEY_SLOT_C ); {
FATFS_EnableAES( FATFSi_GetCounter( rh->s.main_ltd_rom_offset ) ); AESi_LoadKey( AES_KEY_SLOT_C );
}
else
{
AESi_LoadKey( AES_KEY_SLOT_A );
}
FATFS_EnableAES( FATFSi_GetCounter( offset ) );
}
else
{
FATFS_DisableAES();
} }
} }

View File

@ -27,8 +27,6 @@ static ROM_Header* const rh = (ROM_Header*)HW_TWL_ROM_HEADER_BUF;
#define AUTH_SIZE 0xe00 #define AUTH_SIZE 0xe00
#define RSA_BLOCK_SIZE 128 #define RSA_BLOCK_SIZE 128
#define SLOT_SIZE 0x8000
#define HASH_UNIT 0x800 // TODO: optimizing to maximize cache efficiency #define HASH_UNIT 0x800 // TODO: optimizing to maximize cache efficiency
/* /*
@ -230,13 +228,13 @@ extern u32 pf_cnt;
static BOOL MI_LoadBuffer(u8* dest, u32 size, SHA1_CTX *ctx) static BOOL MI_LoadBuffer(u8* dest, u32 size, SHA1_CTX *ctx)
{ {
u8* base = (void*)MI_GetWramMapStart_B(); u8* base = (u8*)HW_FIRM_LOAD_BUFFER_BASE;
static int count = 0; static int count = 0;
while (size > 0) while (size > 0)
{ {
u8* src = base + count * SLOT_SIZE; u8* src = base + count * HW_FIRM_LOAD_BUFFER_UNIT_SIZE;
u32 unit = size < SLOT_SIZE ? size : SLOT_SIZE; u32 unit = size < HW_FIRM_LOAD_BUFFER_UNIT_SIZE ? size : HW_FIRM_LOAD_BUFFER_UNIT_SIZE;
OS_TPrintf("%s: src=%X, unit=%X\n", __func__, src, unit);
if ( PXI_RecvID() != FIRM_PXI_ID_LOAD_PIRIOD ) if ( PXI_RecvID() != FIRM_PXI_ID_LOAD_PIRIOD )
{ {
return FALSE; return FALSE;
@ -266,7 +264,7 @@ static BOOL MI_LoadBuffer(u8* dest, u32 size, SHA1_CTX *ctx)
MI_CpuClearFast( src, unit ); MI_CpuClearFast( src, unit );
DC_FlushRange( src, unit ); DC_FlushRange( src, unit );
MIi_SetWramBankMaster_B(count, MI_WRAM_ARM7); MIi_SetWramBankMaster_B(count, MI_WRAM_ARM7);
count = (count + 1) & 0x07; count = (count + 1) % HW_FIRM_LOAD_BUFFER_UNIT_NUMS;
size -= unit; size -= unit;
dest += unit; dest += unit;
} }

View File

@ -17,12 +17,14 @@
#ifndef FIRM_MEMORYMAP_H_ #ifndef FIRM_MEMORYMAP_H_
#define FIRM_MEMORYMAP_H_ #define FIRM_MEMORYMAP_H_
#include <twl/memorymap.h> #include <twl/memorymap.h>
#ifdef SDK_ARM9 #include <firm/hw/common/mmap_firm.h>
#include <firm/hw/ARM9/mmap_firm.h>
#ifdef SDK_ARM9
#include <firm/hw/ARM9/mmap_firm.h>
#else //SDK_ARM7 #else //SDK_ARM7
#include <firm/hw/ARM7/mmap_firm.h> #include <firm/hw/ARM7/mmap_firm.h>
#endif #endif
/* FIRM_MEMORYMAP_H_ */ /* FIRM_MEMORYMAP_H_ */